30 if(type.
id()==ID_const)
32 else if(type.
id()==ID_merged_type)
46 if(type.
id()==ID_volatile)
48 else if(type.
id()==ID_merged_type)
62 if(type.
id() == ID_auto)
65 type.
id() == ID_merged_type || type.
id() == ID_frontend_pointer ||
66 type.
id() == ID_pointer)
81 bool tag_only_declaration)
97 if(has_body || tag_only_declaration)
107 for(
const auto &
id :
id_set)
109 return static_cast<cpp_scopet &>(id->get_parent());
124 type.
remove(ID_C_constant);
125 type.
remove(ID_C_volatile);
126 type.
remove(ID_C_restricted);
133 bool tag_only_declaration=type.
get_bool(ID_C_tag_only_declaration);
141 type.
set(ID_C_is_anonymous,
true);
155 if(type.
get_bool(ID_C_is_anonymous))
158 dest_scope=&
tag_scope(base_name, has_body, tag_only_declaration);
166 &cpp_typecheck_resolve.
resolve_scope(cpp_name, base_name, t_args);
183 const symbolt &symbol=*maybe_symbol;
201 error() <<
"error: compound tag `" << base_name
202 <<
"' declared previously\n" 203 <<
"location of previous definition: " 214 symbol.
name=symbol_name;
236 error() <<
"cpp_typecheckt::typecheck_compound_type: " 237 <<
"symbol_table.move() failed" <<
eom;
249 id.class_identifier=new_symbol->
name;
264 qualifiers.
write(symbol_type);
265 type.
swap(symbol_type);
278 bool is_cast_operator=
279 declaration.
type().
id()==
"cpp-cast-operator";
284 declarator.
name().
get_sub().front().id()==ID_operator);
319 error() <<
"declarator in compound needs to be simple name" 324 bool is_method=!is_typedef && final_type.
id()==ID_code;
331 final_type.
set(ID_C_member_name, symbol.
name);
335 if(is_virtual && !is_method)
338 error() <<
"only methods can be virtual" <<
eom;
342 if(is_inline && !is_method)
345 error() <<
"only methods can be inlined" <<
eom;
349 if(is_virtual && is_static)
352 error() <<
"static methods cannot be virtual" <<
eom;
356 if(is_cast_operator && is_static)
359 error() <<
"cast operators cannot be static`" <<
eom;
366 error() <<
"constructors cannot be virtual" <<
eom;
373 error() <<
"only constructors can be explicit" <<
eom;
380 error() <<
"member function must return a value or void" <<
eom;
388 error() <<
"destructor with wrong name" <<
eom;
401 is_method || is_static)
412 identifier=base_name;
415 component.
set(ID_name, identifier);
416 component.
type()=final_type;
417 component.
set(ID_access, access);
418 component.
set(ID_base_name, base_name);
419 component.
set(ID_pretty_name, base_name);
424 component.
set(
"is_operator",
true);
425 component.
type().
set(
"#is_operator",
true);
429 component.
set(
"is_cast_operator",
true);
432 component.
set(
"is_explicit",
true);
435 const typet &method_qualifier=
436 static_cast<const typet &
>(declarator.
add(ID_method_qualifier));
440 component.
set(ID_is_static,
true);
441 component.
type().
set(
"#is_static",
true);
445 component.
set(ID_is_type,
true);
448 component.
set(
"is_mutable",
true);
456 value.
id() == ID_code &&
460 component.
set(ID_access,
"noaccess");
466 std::string virtual_name=
470 static_cast<const typet &>(component.
find(ID_type))));
473 virtual_name+=
"$const";
476 virtual_name +=
"$volatile";
478 if(component.
type().
get(ID_return_type)==ID_destructor)
479 virtual_name=
"@dtor";
482 std::set<irep_idt> virtual_bases;
484 for(
const auto &comp : components)
486 if(comp.get_bool(ID_is_virtual))
488 if(comp.get(
"virtual_name")==virtual_name)
503 symbol.
name, component, initializers,
504 method_qualifier, value);
506 if(!value.
is_nil() && !is_static)
509 error() <<
"no initialization allowed here" <<
eom;
515 component.
type().
set(ID_C_is_virtual,
true);
516 component.
type().
set(
"#virtual_name", virtual_name);
526 error() <<
"expected 0 to mark pure virtual method, got " << i <<
eom;
529 component.
set(
"is_pure_virtual",
true);
547 vt_symb_type.
name= vt_name;
550 vt_symb_type.
mode=ID_cpp;
564 compo.
set(ID_base_name,
"@vtable_pointer");
568 compo.
set(
"is_vtptr",
true);
569 compo.
set(ID_access, ID_public);
570 components.push_back(compo);
575 INVARIANT(vt.
id()==ID_struct,
"Virtual tables must be stored as struct");
578 component.
set(
"virtual_name", virtual_name);
579 component.
set(
"is_virtual", is_virtual);
585 vt_entry.
set(ID_base_name, virtual_name);
586 vt_entry.
set(ID_pretty_name, virtual_name);
587 vt_entry.
set(ID_access, ID_public);
589 virtual_table.components().push_back(vt_entry);
592 while(!virtual_bases.empty())
594 irep_idt virtual_base=*virtual_bases.begin();
602 func_symb.
mode=ID_cpp;
615 for(
auto &arg : args)
619 if(base_name.
empty())
626 arg_symb.
mode=ID_cpp;
630 arg.
set(ID_C_identifier, arg_symb.
name);
639 lookup(args[0].
get(ID_C_identifier)).symbol_expr(),
645 expr_call.arguments().reserve(args.size());
646 expr_call.arguments().push_back(late_cast);
648 for(
const auto &arg : args)
650 expr_call.arguments().push_back(
651 lookup(arg.
get(ID_C_identifier)).symbol_expr());
676 components.push_back(new_compo);
687 virtual_bases.erase(virtual_bases.begin());
692 if(is_static && !is_method)
697 static_symbol.
name=identifier;
698 static_symbol.
type=component.
type();
712 error() <<
"redeclaration of static member `" 738 ops.push_back(value);
752 components.push_back(component);
758 if(type.
id()==ID_array)
777 if(base_name.
empty())
780 if(compound.
type().
id()==ID_code)
791 compound.
find(ID_type).
get(ID_return_type)==ID_constructor;
793 id.is_static_member=compound.
get_bool(ID_is_static);
818 for(
const auto &id_it :
id_set)
824 if(!
id.is_class() && !
id.is_enum())
827 error() <<
"`" << base_name
828 <<
"' already in compound scope" <<
eom;
842 id.is_static_member=compound.
get_bool(ID_is_static);
859 error() <<
"friend template not supported" <<
eom;
870 if(ftype.
id()!=ID_struct && ftype.
id()!=ID_union)
873 error() <<
"unexpected friend" <<
eom;
880 error() <<
"friend declaration must not have compound body" <<
eom;
899 bool has_value = sub_it.value().is_not_nil();
940 assert(symbol.
type.
id()==ID_struct ||
941 symbol.
type.
id()==ID_union);
947 if(!type.find(ID_bases).get_sub().empty())
949 if(type.id()==ID_union)
952 error() <<
"union types must not have bases" <<
eom;
959 exprt &body=
static_cast<exprt &
>(type.add(ID_body));
966 type.get_bool(ID_C_class)?ID_private:ID_public;
968 bool found_ctor=
false;
969 bool found_dtor=
false;
975 if(it->id()==ID_cpp_declaration)
989 declaration.
set(ID_C_access, access);
1000 if(declaration.
type().
id()==ID_struct ||
1001 declaration.
type().
id()==ID_union ||
1002 declaration.
type().
id()==ID_c_enum)
1004 declaration.
type().
set(ID_C_tag_only_declaration,
true);
1017 error() <<
"invalid storage class specified for field" <<
eom;
1025 final_type.
get_bool(ID_C_is_anonymous))
1028 if(final_type.
id()!=ID_union &&
1029 final_type.
id()!=ID_struct)
1032 error() <<
"member declaration does not declare anything" 1038 declaration, access, components);
1059 declaration, declarator, components,
1060 access, is_static, is_typedef, is_mutable);
1063 else if(it->id()==
"cpp-public")
1065 else if(it->id()==
"cpp-private")
1067 else if(it->id()==
"cpp-protected")
1068 access=ID_protected;
1086 dtor,
dtor.declarators()[0], components,
1087 ID_public,
false,
false,
false);
1091 if(symbol.
type.
id()==ID_struct)
1097 exprt cpp_public(
"cpp-public");
1108 type.get_bool(ID_C_class)?ID_private:ID_public;
1114 if(it->id()==ID_cpp_declaration)
1126 declarator.name().get_base_name();
1129 if(declarator.value().is_not_nil())
1131 if(declarator.find(ID_member_initializers).is_nil())
1132 declarator.set(ID_member_initializers, ID_member_initializers);
1137 declarator.member_initializers());
1141 declarator.member_initializers());
1153 declaration, declarator, components,
1154 access, is_static, is_typedef, is_mutable);
1157 else if(it->id()==
"cpp-public")
1159 else if(it->id()==
"cpp-private")
1161 else if(it->id()==
"cpp-protected")
1162 access=ID_protected;
1180 exprt value(
"cpp_not_typechecked");
1181 value.copy_to_operands(cpctor.
declarators()[0].value());
1187 ID_public,
false,
false,
false);
1207 ID_public,
false,
false,
false);
1216 irept &initializers,
1221 type.
find(ID_return_type).
id()==ID_constructor;
1224 if(!initializers.
get_sub().empty())
1228 initializers.
find(ID_C_source_location));
1233 error() <<
"only constructors are allowed to " 1234 <<
"have member initializers" <<
eom;
1241 error() <<
"only constructors with body are allowed to " 1242 <<
"have member initializers" <<
eom;
1248 exprt::operandst::iterator o_it=value.
operands().begin();
1251 o_it=value.
operands().insert(o_it, static_cast<const exprt &>(*it));
1258 const irep_idt &compound_identifier,
1260 irept &initializers,
1261 const typet &method_qualifier,
1268 if(component.
get_bool(ID_is_static))
1270 if(!method_qualifier.
id().
empty())
1273 error() <<
"method is static -- no qualifiers allowed" <<
eom;
1280 compound_identifier,
1302 type.
set(ID_C_inlined,
true);
1304 symbol.
name=identifier;
1320 error() <<
"failed to insert new method symbol: " 1321 << symbol.
name <<
"\n" 1322 <<
"name of previous symbol: " 1323 << new_symbol->
name <<
"\n" 1324 <<
"location of previous symbol: " 1342 const typet &method_qualifier)
1358 subtype.
set(ID_C_constant,
true);
1361 subtype.
set(ID_C_volatile,
true);
1367 const symbolt &struct_union_symbol)
1379 for(
const auto &comp : struct_union_components)
1381 if(comp.type().id()==ID_code)
1384 error() <<
"anonymous struct/union member `" 1386 <<
"' shall not have function members" <<
eom;
1390 if(comp.get_anonymous())
1392 const symbolt &symbol=
lookup(comp.type().get(ID_identifier));
1398 const irep_idt &base_name=comp.get_base_name();
1403 error() <<
"`" << base_name <<
"' already in scope" <<
eom;
1409 id.identifier=comp.get_name();
1410 id.class_identifier=struct_union_symbol.
name;
1428 error() <<
"storage class is not allowed here" <<
eom;
1435 error() <<
"anonymous struct/union member is not POD" <<
eom;
1455 components.push_back(component);
1461 struct_union_symbol.
type.
set(
"#unnamed_object", base_name);
1466 const exprt &
object,
1472 assert(followed_type.
id()==ID_struct ||
1473 followed_type.
id()==ID_union);
1479 final_type.components();
1481 for(
const auto &component : components)
1483 member_exprt tmp(
object, component.get_name(), component.type());
1486 if(component.get_name()==component_name)
1495 member.
set(
"#not_accessible",
true);
1496 member.
set(ID_C_access, component.get(ID_access));
1501 error() <<
"error: member `" << component_name
1502 <<
"' is not accessible (" << component.get(ID_access) <<
")" 1508 if(
object.get_bool(ID_C_lvalue))
1509 member.
set(ID_C_lvalue,
true);
1511 if(
object.type().get_bool(ID_C_constant) &&
1512 !component.get_bool(
"is_mutable"))
1513 member.
type().
set(ID_C_constant,
true);
1524 const typet &component_type=
follow(component.type());
1526 if(component_type.
id()==ID_union ||
1527 component_type.
id()==ID_struct)
1530 if(
get_component(source_location, tmp, component_name, member))
1535 error() <<
"error: member `" << component_name
1536 <<
"' is not accessible" <<
eom;
1540 if(
object.get_bool(ID_C_lvalue))
1541 member.
set(ID_C_lvalue,
true);
1543 if(
object.get_bool(ID_C_constant) &&
1544 !component.get_bool(
"is_mutable"))
1545 member.
type().
set(ID_C_constant,
true);
1563 if(access==
"noaccess")
1566 if(access==ID_public)
1569 assert(access==ID_private ||
1570 access==ID_protected);
1573 struct_union_type.
get(ID_name);
1601 const irept &friend_symb=*f_it;
1625 std::set<irep_idt> &set_bases)
const 1631 assert(it->id()==ID_base);
1632 assert(it->get(ID_type)==ID_symbol);
1637 set_bases.insert(base.get(ID_name));
1644 std::list<irep_idt> &vbases)
const 1646 if(std::find(vbases.begin(), vbases.end(), type.
get(ID_name))!=vbases.end())
1653 assert(it->id()==ID_base);
1654 assert(it->get(ID_type)==ID_symbol);
1659 if(it->get_bool(ID_virtual))
1660 vbases.push_back(base.get(ID_name));
1670 if(from.
get(ID_name)==to.
get(ID_name))
1673 std::set<irep_idt> bases;
1677 return bases.find(to.
get(ID_name))!=bases.end();
1682 const typet &dest_type)
1686 assert(src_type.
id()== ID_pointer);
1687 assert(dest_type.
id()== ID_pointer);
C++ Language Type Checking.
const irep_idt & get_statement() const
const irep_idt & get_name() const
The type of an expression.
irep_idt name
The unique identifier.
void add_anonymous_members_to_scope(const symbolt &struct_union_symbol)
#define forall_subtypes(it, type)
bool find_cpctor(const symbolt &symbol) const
void check_fixed_size_array(typet &type)
check that an array has fixed size
static bool is_constructor(const irep_idt &method_name)
codet dtor(const symbolt &symb)
produces destructor code for a class object
void typecheck_type(typet &type)
const std::string & id2string(const irep_idt &d)
pointer_typet pointer_type(const typet &subtype)
const cpp_storage_spect & storage_spec() const
std::set< cpp_idt * > id_sett
void lookup(const irep_idt &base_name, lookup_kindt kind, id_sett &id_set)
static bool has_const(const typet &type)
static bool has_auto(const typet &type)
bool is_destructor() const
std::vector< irept > subt
irep_idt mode
Language mode.
void move_to_sub(irept &irep)
void name_anon_struct_union()
void get_bases(const struct_typet &type, std::set< irep_idt > &set_bases) const
const code_typet & to_code_type(const typet &type)
Cast a generic typet to a code_typet.
void set_base_name(const irep_idt &base_name)
void set_name(const irep_idt &name)
void full_member_initialization(const struct_union_typet &struct_union_type, irept &initializers)
Build the full initialization list of the constructor.
bool check_component_access(const struct_union_typet::componentt &component, const struct_union_typet &struct_union_type)
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
cpp_namet & to_cpp_name(irept &cpp_name)
std::vector< componentt > componentst
void move_to_operands(exprt &expr)
cpp_scopet & get_parent() const
std::vector< parametert > parameterst
void already_typechecked(irept &irep)
exprt value
Initial value of symbol.
const componentst & components() const
cpp_idt & put_into_scope(const symbolt &symbol, cpp_scopet &scope, bool is_friend=false)
irep_idt module
Name of module the symbol belongs to.
irep_idt pretty_name
Language-specific display name.
void default_dtor(const symbolt &symb, cpp_declarationt &dtor)
Note:
void move_member_initializers(irept &initializers, const typet &type, exprt &value)
void add_this_to_method_type(const irep_idt &compound_identifier, typet &method_type, const typet &method_qualifier)
cpp_scopet & get_global_scope()
bool is_simple_name() const
typet merge_type(const typet &declaration_type) const
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
#define CHECK_RETURN(CONDITION)
static mstreamt & eom(mstreamt &m)
void check_member_initializers(const irept &bases, const struct_typet::componentst &components, const irept &initializers)
Check a constructor initialization-list.
bool get_bool(const irep_namet &name) const
const cpp_member_spect & member_spec() const
void set_inline(bool value)
#define INVARIANT(CONDITION, REASON)
void set_base_name(const irep_idt &name)
void typecheck_compound_declarator(const symbolt &symbol, const cpp_declarationt &declaration, cpp_declaratort &declarator, struct_typet::componentst &components, const irep_idt &access, bool is_static, bool is_typedef, bool is_mutable)
Extract member of struct or union.
symbol_tablet & symbol_table
virtual symbolt * get_writeable(const irep_idt &name) override
Find a symbol in the symbol table for read-write access.
const irep_idt & get_base_name() const
void add_method_body(symbolt *_method_symbol)
const irep_idt & id() const
const source_locationt & source_location() const
const irep_idt & get_base_name() const
void elaborate_class_template(const typet &type)
elaborate class template instances
class code_blockt & make_block()
const declaratorst & declarators() const
source_locationt & location()
A reference into the symbol table.
virtual bool move(symbolt &symbol, symbolt *&new_symbol) override
Move a symbol into the symbol table.
source_locationt source_location
bool cpp_is_pod(const typet &type) const
void set_access(const irep_idt &access)
void typecheck_friend_declaration(symbolt &symbol, cpp_declarationt &cpp_declaration)
const irep_idt & get(const irep_namet &name) const
void go_to_global_scope()
void typecheck_member_function(const irep_idt &compound_identifier, struct_typet::componentt &component, irept &initializers, const typet &method_qualifier, exprt &value)
void do_virtual_table(const symbolt &symbol)
std::string cpp_type2name(const typet &type)
const exprt & size() const
Base class for tree-like data structures with sharing.
void convert_template_declaration(cpp_declarationt &declaration)
C++ Language Type Checking.
const typet & follow(const typet &) const
const struct_typet & to_struct_type(const typet &type)
Cast a generic typet to a struct_typet.
bool is_constructor() const
void default_assignop(const symbolt &symbol, cpp_declarationt &cpctor)
Generate declaration of the implicit default assignment operator.
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
bool has_operands() const
void default_cpctor(const symbolt &, cpp_declarationt &cpctor) const
Generate code for implicit default copy constructor.
void make_ptr_typecast(exprt &expr, const typet &dest_type)
std::vector< exprt > operandst
const source_locationt & source_location() const
irep_idt get_base_name() const
cpp_scopet & set_scope(const irep_idt &identifier)
A function call side effect.
bool get_component(const source_locationt &source_location, const exprt &object, const irep_idt &component_name, exprt &member)
irep_idt function_identifier(const typet &type)
for function overloading
typet type
Type of symbol.
source_locationt location
Source code location of definition of symbol.
cpp_declarationt & to_cpp_declaration(irept &irep)
void set_identifier(const irep_idt &identifier)
bool subtype_typecast(const struct_typet &from, const struct_typet &to) const
void typecheck_compound_bases(struct_typet &type)
Base type of C structs and unions, and C++ classes.
void default_ctor(const source_locationt &source_location, const irep_idt &base_name, cpp_declarationt &ctor) const
Generate code for implicit default constructors.
const array_typet & to_array_type(const typet &type)
Cast a generic typet to an array_typet.
bool find_assignop(const symbolt &symbol) const
Base class for all expressions.
void get_virtual_bases(const struct_typet &type, std::list< irep_idt > &vbases) const
bool contains(const irep_idt &base_name)
const parameterst & parameters() const
irep_idt base_name
Base (non-scoped) name.
cpp_scopet & current_scope()
irept & member_initializers()
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a generic typet to a struct_union_typet.
void set_pretty_name(const irep_idt &name)
std::string to_string(const string_constraintt &expr)
Used for debug printing.
symbolt & convert(const typet &type, const cpp_storage_spect &storage_spec, const cpp_member_spect &member_spec, cpp_declaratort &declarator)
const source_locationt & source_location() const
virtual void make_constant_index(exprt &expr)
irept & add(const irep_namet &name)
void typecheck_compound_type(struct_union_typet &type)
const std::string & get_string(const irep_namet &name) const
void set_identifier(const irep_idt &identifier)
dynamic_initializationst dynamic_initializations
const std::string & id_string() const
bool disable_access_control
virtual void write(typet &src) const override
#define Forall_operands(it, expr)
source_locationt & add_source_location()
const codet & to_code(const exprt &expr)
void typecheck_compound_body(symbolt &symbol)
Expression to hold a symbol (variable)
void set_anonymous(bool anonymous)
cpp_scopet & get_scope(const irep_idt &identifier)
const char * c_str() const
virtual void do_initializer(exprt &initializer, const typet &type, bool force_constant)
void put_compound_into_scope(const struct_union_typet::componentt &component)
A statement in a programming language.
exprt cpp_symbol_expr(const symbolt &symbol)
void remove(const irep_namet &name)
const typet & subtype() const
const symbolt * lookup(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
static bool has_volatile(const typet &type)
bool is_root_scope() const
void convert_anon_struct_union_member(const cpp_declarationt &declaration, const irep_idt &access, struct_typet::componentst &components)
cpp_idt & insert(const irep_idt &_base_name)
std::set< cpp_idt * > id_sett
void make_typecast(const typet &_type)
const irept & find(const irep_namet &name) const
cpp_scopet & tag_scope(const irep_idt &_base_name, bool has_body, bool tag_only_declaration)
const typet & return_type() const
codet cpp_constructor(const source_locationt &source_location, const exprt &object, const exprt::operandst &operands)
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See namespace_baset::lookup().
bool is_template_scope() const
void set(const irep_namet &name, const irep_idt &value)
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
bool simplify(exprt &expr, const namespacet &ns)
cpp_scopet & resolve_scope(const cpp_namet &cpp_name, irep_idt &base_name, cpp_template_args_non_tct &template_args)
#define forall_irep(it, irep)