Go to the documentation of this file.
33 if(type.
id()==ID_const)
35 else if(type.
id()==ID_merged_type)
51 if(type.
id()==ID_volatile)
53 else if(type.
id()==ID_merged_type)
69 if(type.
id() == ID_auto)
72 type.
id() == ID_merged_type || type.
id() == ID_frontend_pointer ||
73 type.
id() == ID_pointer)
90 bool tag_only_declaration)
106 if(has_body || tag_only_declaration)
116 for(
const auto &
id : id_set)
118 return static_cast<cpp_scopet &
>(
id->get_parent());
133 type.
remove(ID_C_constant);
134 type.
remove(ID_C_volatile);
135 type.
remove(ID_C_restricted);
142 bool tag_only_declaration=type.
get_bool(ID_C_tag_only_declaration);
143 bool is_union = type.
id() == ID_union;
151 type.
set(ID_C_is_anonymous,
true);
165 if(type.
get_bool(ID_C_is_anonymous))
168 dest_scope=&
tag_scope(base_name, has_body, tag_only_declaration);
176 &cpp_typecheck_resolve.
resolve_scope(cpp_name, base_name, t_args);
193 const symbolt &symbol=*maybe_symbol;
213 error() <<
"error: compound tag '" << base_name
214 <<
"' declared previously\n"
215 <<
"location of previous definition: " << symbol.
location
220 else if(symbol.
type.
id() != type.
id())
224 <<
" as different kind of tag" <<
eom;
233 symbol.
name=symbol_name;
255 error() <<
"cpp_typecheckt::typecheck_compound_type: "
256 <<
"symbol_table.move() failed" <<
eom;
268 id.class_identifier=new_symbol->
name;
287 qualifiers.
write(tag_type);
294 qualifiers.
write(tag_type);
309 bool is_cast_operator=
310 declaration.
type().
id()==
"cpp-cast-operator";
315 declarator.
name().
get_sub().front().id()==ID_operator);
332 if(final_type.
id() == ID_empty)
335 error() <<
"void-typed member not permitted" <<
eom;
356 error() <<
"declarator in compound needs to be simple name"
361 bool is_method=!is_typedef && final_type.
id()==ID_code;
368 final_type.
set(ID_C_member_name, symbol.
name);
372 if(is_virtual && !is_method)
375 error() <<
"only methods can be virtual" <<
eom;
379 if(is_inline && !is_method)
382 error() <<
"only methods can be inlined" <<
eom;
386 if(is_virtual && is_static)
389 error() <<
"static methods cannot be virtual" <<
eom;
393 if(is_cast_operator && is_static)
396 error() <<
"cast operators cannot be static" <<
eom;
403 error() <<
"constructors cannot be virtual" <<
eom;
410 error() <<
"only constructors can be explicit" <<
eom;
417 error() <<
"member function must return a value or void" <<
eom;
425 error() <<
"destructor with wrong name" <<
eom;
437 is_method || is_static)
448 identifier=base_name;
460 component.type().set(ID_C_is_operator,
true);
464 component.set(ID_is_cast_operator,
true);
470 const typet &method_qualifier=
471 static_cast<const typet &
>(declarator.
add(ID_method_qualifier));
476 component.type().set(ID_C_is_static,
true);
491 value.
id() == ID_code &&
505 virtual_name+=
"$const";
508 virtual_name +=
"$volatile";
511 virtual_name=
"@dtor";
514 std::set<irep_idt> virtual_bases;
516 for(
const auto &comp : components)
518 if(comp.get_bool(ID_is_virtual))
520 if(comp.get(ID_virtual_name) == virtual_name)
527 virtual_bases.insert(
537 method_qualifier, value);
539 if(!value.
is_nil() && !is_static)
542 error() <<
"no initialization allowed here" <<
eom;
548 component.type().set(ID_C_is_virtual,
true);
549 component.type().set(ID_C_virtual_name, virtual_name);
559 error() <<
"expected 0 to mark pure virtual method, got " << i <<
eom;
580 vt_symb_type.
name= vt_name;
583 vt_symb_type.
mode=ID_cpp;
599 compo.
set(ID_is_vtptr,
true);
600 compo.
set(ID_access, ID_public);
601 components.push_back(compo);
606 INVARIANT(vt.
id()==ID_struct,
"Virtual tables must be stored as struct");
609 component.set(ID_virtual_name, virtual_name);
610 component.set(ID_is_virtual, is_virtual);
614 id2string(vt_name) +
"::" + virtual_name,
618 vt_entry.
set(ID_access, ID_public);
620 virtual_table.
components().push_back(vt_entry);
623 while(!virtual_bases.empty())
625 irep_idt virtual_base=*virtual_bases.begin();
643 .
set(ID_identifier, virtual_base);
648 for(
auto &arg : args)
650 irep_idt param_base_name = arg.get_base_name();
652 if(param_base_name.
empty())
662 arg_symb.
type=arg.type();
664 arg.set_identifier(arg_symb.
name);
681 expr_call.
arguments().reserve(args.size());
683 for(
const auto &arg : args)
686 lookup(arg.get_identifier()).symbol_expr());
708 components.push_back(new_compo);
719 virtual_bases.erase(virtual_bases.begin());
724 if(is_static && !is_method)
729 static_symbol.
name=identifier;
744 error() <<
"redeclaration of static member '" << static_symbol.
base_name
761 ops.push_back(value);
781 if(type.
id()==ID_array)
787 if(array_type.
size().
id() == ID_symbol)
811 if(base_name.
empty())
814 if(compound.
type().
id()==ID_code)
826 id.is_static_member=compound.
get_bool(ID_is_static);
849 for(
const auto &id_it : id_set)
855 if(!
id.is_class() && !
id.is_enum())
858 error() <<
"'" << base_name <<
"' already in compound scope" <<
eom;
872 id.is_static_member=compound.
get_bool(ID_is_static);
886 error() <<
"friend template not supported" <<
eom;
896 if(ftype.
id()!=ID_struct && ftype.
id()!=ID_union)
899 error() <<
"unexpected friend" <<
eom;
906 error() <<
"friend declaration must not have compound body" <<
eom;
922 std::cout <<
"friend declaration: " << declaration.
pretty() <<
'\n';
928 std::cout <<
"decl: " << sub_it.pretty() <<
"\n with value "
929 << sub_it.value().pretty() <<
'\n';
933 if(sub_it.value().is_not_nil())
937 cpp_declarator_converter.
is_friend =
true;
955 assert(symbol.
type.
id()==ID_struct ||
956 symbol.
type.
id()==ID_union);
964 if(type.
id()==ID_union)
967 error() <<
"union types must not have bases" <<
eom;
982 bool found_ctor=
false;
983 bool found_dtor=
false;
989 if(it->id()==ID_cpp_declaration)
1003 declaration.
set(ID_C_access, access);
1014 if(declaration.
type().
id()==ID_struct ||
1015 declaration.
type().
id()==ID_union ||
1016 declaration.
type().
id()==ID_c_enum)
1018 declaration.
type().
set(ID_C_tag_only_declaration,
true);
1031 error() <<
"invalid storage class specified for field" <<
eom;
1039 final_type.
get_bool(ID_C_is_anonymous))
1042 if(final_type.
id()!=ID_union &&
1043 final_type.
id()!=ID_struct)
1046 error() <<
"member declaration does not declare anything"
1052 declaration, access, components);
1073 declaration, declarator, components,
1074 access, is_static, is_typedef, is_mutable);
1077 else if(it->id()==
"cpp-public")
1079 else if(it->id()==
"cpp-private")
1081 else if(it->id()==
"cpp-protected")
1082 access=ID_protected;
1100 dtor,
dtor.declarators()[0], components,
1101 ID_public,
false,
false,
false);
1105 if(symbol.
type.
id()==ID_struct)
1111 exprt cpp_public(
"cpp-public");
1127 if(it->id()==ID_cpp_declaration)
1139 declarator.name().get_base_name();
1142 if(declarator.value().is_not_nil())
1144 if(declarator.find(ID_member_initializers).is_nil())
1145 declarator.set(ID_member_initializers, ID_member_initializers);
1147 if(type.
id() == ID_union)
1150 {}, type.
components(), declarator.member_initializers());
1157 declarator.member_initializers());
1162 declarator.member_initializers());
1174 declaration, declarator, components,
1175 access, is_static, is_typedef, is_mutable);
1178 else if(it->id()==
"cpp-public")
1180 else if(it->id()==
"cpp-private")
1182 else if(it->id()==
"cpp-protected")
1183 access=ID_protected;
1201 exprt value(ID_cpp_not_typechecked);
1208 ID_public,
false,
false,
false);
1228 ID_public,
false,
false,
false);
1237 irept &initializers,
1242 if(!initializers.
get_sub().empty())
1246 initializers.
find(ID_C_source_location));
1251 error() <<
"only constructors are allowed to "
1252 <<
"have member initializers" <<
eom;
1259 error() <<
"only constructors with body are allowed to "
1260 <<
"have member initializers" <<
eom;
1264 if(
to_code(value).get_statement() != ID_block)
1267 exprt::operandst::iterator o_it=value.
operands().begin();
1268 for(
const auto &initializer : initializers.
get_sub())
1271 value.
operands().insert(o_it,
static_cast<const exprt &
>(initializer));
1278 const symbolt &compound_symbol,
1280 irept &initializers,
1281 const typet &method_qualifier,
1290 if(!method_qualifier.
id().
empty())
1293 error() <<
"method is static -- no qualifiers allowed" <<
eom;
1324 symbol.
name=identifier;
1327 symbol.
mode = compound_symbol.
mode;
1338 if(symbol_exists && new_symbol->
is_weak)
1341 *new_symbol = std::move(symbol);
1343 else if(symbol_exists)
1346 error() <<
"failed to insert new method symbol: " << symbol.
name <<
'\n'
1347 <<
"name of previous symbol: " << new_symbol->
name <<
'\n'
1348 <<
"location of previous symbol: " << new_symbol->
location <<
eom;
1362 const symbolt &compound_symbol,
1364 const typet &method_qualifier)
1368 if(compound_symbol.
type.
id() == ID_union)
1374 subtype.
set(ID_C_constant,
true);
1377 subtype.
set(ID_C_volatile,
true);
1387 parameters.insert(parameters.begin(), parameter);
1391 const symbolt &struct_union_symbol)
1403 for(
const auto &comp : struct_union_components)
1405 if(comp.type().id()==ID_code)
1408 error() <<
"anonymous struct/union member '"
1410 <<
"' shall not have function members" <<
eom;
1414 if(comp.get_anonymous())
1416 const symbolt &symbol=
lookup(comp.type().get(ID_identifier));
1422 const irep_idt &base_name=comp.get_base_name();
1427 error() <<
"'" << base_name <<
"' already in scope" <<
eom;
1433 id.identifier=comp.get_name();
1434 id.class_identifier=struct_union_symbol.
name;
1445 symbolt &struct_union_symbol =
1452 error() <<
"storage class is not allowed here" <<
eom;
1459 error() <<
"anonymous struct/union member is not POD" <<
eom;
1470 typet compound_type;
1472 if(struct_union_symbol.
type.
id() == ID_union)
1490 struct_union_symbol.
type.
set(ID_C_unnamed_object, base_name);
1495 const exprt &
object,
1501 assert(followed_type.
id()==ID_struct ||
1502 followed_type.
id()==ID_union);
1515 if(
component.get_name()==component_name)
1524 member.
set(ID_C_not_accessible,
true);
1530 error() <<
"error: member '" << component_name
1531 <<
"' is not accessible (" <<
component.get(ID_access) <<
")"
1537 if(
object.get_bool(ID_C_lvalue))
1538 member.
set(ID_C_lvalue,
true);
1541 object.type().get_bool(ID_C_constant) &&
1544 member.
type().
set(ID_C_constant,
true);
1557 if(component_type.
id()==ID_union ||
1558 component_type.
id()==ID_struct)
1561 if(
get_component(source_location, tmp, component_name, member))
1566 error() <<
"error: member '" << component_name
1567 <<
"' is not accessible" <<
eom;
1571 if(
object.get_bool(ID_C_lvalue))
1572 member.
set(ID_C_lvalue,
true);
1575 object.get_bool(ID_C_constant) &&
1578 member.
type().
set(ID_C_constant,
true);
1597 if(access == ID_noaccess)
1600 if(access==ID_public)
1603 assert(access==ID_private ||
1604 access==ID_protected);
1607 struct_union_type.
get(ID_name);
1610 !(pscope->is_root_scope());
1611 pscope = &(pscope->get_parent()))
1613 if(pscope->is_class())
1615 if(pscope->identifier==struct_identifier)
1632 for(
const auto &friend_symb : friends)
1638 !(pscope->is_root_scope());
1639 pscope = &(pscope->get_parent()))
1641 if(friend_scope.
identifier==pscope->identifier)
1644 if(pscope->is_class())
1654 std::set<irep_idt> &set_bases)
const
1656 for(
const auto &b : type.
bases())
1658 DATA_INVARIANT(b.id() == ID_base,
"base class expression expected");
1662 set_bases.insert(base.
get(ID_name));
1669 std::list<irep_idt> &vbases)
const
1671 if(std::find(vbases.begin(), vbases.end(), type.
get(ID_name))!=vbases.end())
1674 for(
const auto &b : type.
bases())
1676 DATA_INVARIANT(b.id() == ID_base,
"base class expression expected");
1680 if(b.get_bool(ID_virtual))
1681 vbases.push_back(base.
get(ID_name));
1691 if(from.
get(ID_name)==to.
get(ID_name))
1694 std::set<irep_idt> bases;
1698 return bases.find(to.
get(ID_name))!=bases.end();
1703 const typet &dest_type)
1707 assert(src_type.
id()== ID_pointer);
1708 assert(dest_type.
id()== ID_pointer);
virtual void do_initializer(exprt &initializer, const typet &type, bool force_constant)
void copy_to_operands(const exprt &expr)
Copy the given argument to the end of exprt's operands.
void do_virtual_table(const symbolt &symbol)
const componentst & components() const
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
irep_idt class_identifier
A codet representing sequential composition of program statements.
const char * c_str() const
const cpp_storage_spect & storage_spec() const
cpp_scopet & get_parent() const
void convert_anon_struct_union_member(const cpp_declarationt &declaration, const irep_idt &access, struct_typet::componentst &components)
cpp_scopet & tag_scope(const irep_idt &_base_name, bool has_body, bool tag_only_declaration)
void elaborate_class_template(const typet &type)
elaborate class template instances
#define Forall_operands(it, expr)
void set_inline(bool value)
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
bool check_component_access(const struct_union_typet::componentt &component, const struct_union_typet &struct_union_type)
void move_to_sub(irept &irep)
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a typet to a struct_union_typet.
id_sett lookup(const irep_idt &base_name_to_lookup, lookup_kindt kind)
#define CHECK_RETURN(CONDITION)
The type of an expression, extends irept.
std::vector< parametert > parameterst
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
cpp_declarationt & to_cpp_declaration(irept &irep)
const irept & find(const irep_idt &name) const
void check_fixed_size_array(typet &type)
check that an array has fixed size
Base type for structs and unions.
void typecheck_compound_body(symbolt &symbol)
typet type
Type of symbol.
void convert_parameter(const irep_idt ¤t_mode, code_typet::parametert ¶meter)
A side_effect_exprt representation of a function call side effect.
void set_identifier(const irep_idt &identifier)
static symbol_exprt typeless(const irep_idt &id)
Generate a symbol_exprt without a proper type.
void typecheck_member_function(const symbolt &compound_symbol, struct_typet::componentt &component, irept &initializers, const typet &method_qualifier, exprt &value)
void typecheck_compound_bases(struct_typet &type)
const type_with_subtypest & to_type_with_subtypes(const typet &type)
symbolt & convert(const typet &type, const cpp_storage_spect &storage_spec, const cpp_member_spect &member_spec, cpp_declaratort &declarator)
cpp_scopet & get_scope(const irep_idt &identifier)
Base class for all expressions.
std::vector< componentt > componentst
optionalt< codet > cpp_constructor(const source_locationt &source_location, const exprt &object, const exprt::operandst &operands)
irep_idt base_name
Base (non-scoped) name.
A struct tag type, i.e., struct_typet with an identifier.
codet dtor(const symbolt &symb, const symbol_exprt &this_expr)
produces destructor code for a class object
auto component(T &struct_expr, const irep_idt &name, const namespacet &ns) -> decltype(struct_expr.op0())
bool is_destructor() const
void make_incomplete()
A struct/union may be incomplete.
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
irep_idt default_access() const
Return the access specification for members where access has not been modified.
void name_anon_struct_union()
Expression to hold a symbol (variable)
cpp_idt & put_into_scope(const symbolt &symbol, cpp_scopet &scope, bool is_friend=false)
void set_name(const irep_idt &name)
bool contains(const irep_idt &base_name_to_lookup)
A union tag type, i.e., union_typet with an identifier.
const declaratorst & declarators() const
const cpp_member_spect & member_spec() const
void typecheck_friend_declaration(symbolt &symbol, cpp_declarationt &cpp_declaration)
const irep_idt & get(const irep_idt &name) const
irep_idt pretty_name
Language-specific display name.
void default_dtor(const symbolt &symb, cpp_declarationt &dtor)
Note:
cpp_scopet & get_global_scope()
const exprt & size() const
bool cpp_is_pod(const typet &type) const
const codet & to_code(const exprt &expr)
typet & type()
Return the type of the expression.
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
void typecheck_type(typet &) override
static bool has_const(const typet &type)
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
void default_ctor(const source_locationt &source_location, const irep_idt &base_name, cpp_declarationt &ctor) const
Generate code for implicit default constructors.
void go_to_global_scope()
irep_idt mode
Language mode.
void get_virtual_bases(const struct_typet &type, std::list< irep_idt > &vbases) const
void add_method_body(symbolt *_method_symbol)
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)
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
bool has_operands() const
Return true if there is at least one operand.
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
const std::string & id2string(const irep_idt &d)
cpp_scopet & current_scope()
const irep_idt & get_name() const
static bool failed(bool error_indicator)
source_locationt source_location
static optionalt< smt_termt > get_identifier(const exprt &expr, const std::unordered_map< exprt, smt_identifier_termt, irep_hash > &expression_handle_identifiers, const std::unordered_map< exprt, smt_identifier_termt, irep_hash > &expression_identifiers)
void set(const irep_idt &name, const irep_idt &value)
#define PRECONDITION(CONDITION)
const source_locationt & source_location() const
const irep_idt & get_identifier() const
symbol_exprt cpp_symbol_expr(const symbolt &symbol)
void set_pretty_name(const irep_idt &name)
void set_base_name(const irep_idt &base_name)
bool is_simple_name() const
void check_member_initializers(const struct_typet::basest &bases, const struct_typet::componentst &components, const irept &initializers)
Check a constructor initialization-list.
virtual void write(typet &src) const override
void set_inlined(bool value)
void make_ptr_typecast(exprt &expr, const typet &dest_type)
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
const std::string & get_string(const irep_idt &name) const
const basest & bases() const
Get the collection of base classes/structs.
symbol_tablet & symbol_table
pointer_typet pointer_type(const typet &subtype)
void put_compound_into_scope(const struct_union_typet::componentt &component)
std::unordered_set< irep_idt > deferred_typechecking
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
bool subtype_typecast(const struct_typet &from, const struct_typet &to) const
virtual void make_constant_index(exprt &expr)
const irep_idt & id() const
std::vector< exprt > operandst
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
const parameterst & parameters() const
source_locationt & add_source_location()
void convert_template_declaration(cpp_declarationt &declaration)
irept & member_initializers()
void typecheck_compound_type(struct_union_typet &) override
source_locationt & location()
void get_bases(const struct_typet &type, std::set< irep_idt > &set_bases) const
void set_base_name(const irep_idt &name)
virtual bool move(symbolt &symbol, symbolt *&new_symbol) override
Move a symbol into the symbol table.
codet representation of a "return from a function" statement.
void full_member_initialization(const struct_union_typet &struct_union_type, irept &initializers)
Build the full initialization list of the constructor.
Extract member of struct or union.
exprt value
Initial value of symbol.
void add_anonymous_members_to_scope(const symbolt &struct_union_symbol)
void add_this_to_method_type(const symbolt &compound_symbol, code_typet &method_type, const typet &method_qualifier)
Structure type, corresponds to C style structs.
irept & add(const irep_idt &name)
const source_locationt & source_location() const
bool is_constructor() const
cpp_scopet & resolve_scope(const cpp_namet &cpp_name, irep_idt &base_name, cpp_template_args_non_tct &template_args)
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
std::string cpp_type2name(const typet &type)
source_locationt location
Source code location of definition of symbol.
exprt::operandst & arguments()
static bool has_auto(const typet &type)
const typet & base_type() const
The type of the data what we point to.
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
static bool has_volatile(const typet &type)
void add_to_operands(const exprt &expr)
Add the given argument to the end of exprt's operands.
const symbolt * lookup(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
const typet & return_type() const
There are a large number of kinds of tree structured or tree-like data in CPROVER.
bool find_assignop(const symbolt &symbol) const
void remove(const irep_idt &name)
source_locationt & add_source_location()
const irep_idt & get_base_name() const
Semantic type conversion.
static bool is_constructor(const irep_idt &method_name)
const irep_idt & get_statement() const
irep_idt module
Name of module the symbol belongs to.
irep_idt get_base_name() const
bool find_cpctor(const symbolt &symbol) const
void default_assignop(const symbolt &symbol, cpp_declarationt &cpctor)
Generate declaration of the implicit default assignment operator.
bool is_template_scope() const
const multi_ary_exprt & to_multi_ary_expr(const exprt &expr)
Cast an exprt to a multi_ary_exprt.
void default_cpctor(const symbolt &, cpp_declarationt &cpctor) const
Generate code for implicit default copy constructor.
const source_locationt & source_location() const
bool is_incomplete() const
A struct/union may be incomplete.
dynamic_initializationst dynamic_initializations
bool disable_access_control
irep_idt name
The unique identifier.
cpp_scopet & set_scope(const irep_idt &identifier)
bool get_bool(const irep_idt &name) const
void move_member_initializers(irept &initializers, const code_typet &type, exprt &value)
cpp_namet & to_cpp_name(irept &cpp_name)
codet representation of an expression statement.
const typet & element_type() const
The type of the elements of the array.
cpp_idt & insert(const irep_idt &_base_name)
typet merge_type(const typet &declaration_type) const
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.