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)
536 method_qualifier, value);
538 if(!value.
is_nil() && !is_static)
541 error() <<
"no initialization allowed here" <<
eom;
547 component.type().set(ID_C_is_virtual,
true);
548 component.type().set(ID_C_virtual_name, virtual_name);
558 error() <<
"expected 0 to mark pure virtual method, got " << i <<
eom;
579 vt_symb_type.
name= vt_name;
582 vt_symb_type.
mode=ID_cpp;
598 compo.
set(ID_is_vtptr,
true);
599 compo.
set(ID_access, ID_public);
600 components.push_back(compo);
605 INVARIANT(vt.
id()==ID_struct,
"Virtual tables must be stored as struct");
608 component.set(ID_virtual_name, virtual_name);
609 component.set(ID_is_virtual, is_virtual);
613 id2string(vt_name) +
"::" + virtual_name,
617 vt_entry.
set(ID_access, ID_public);
619 virtual_table.
components().push_back(vt_entry);
622 while(!virtual_bases.empty())
624 irep_idt virtual_base=*virtual_bases.begin();
645 for(
auto &arg : args)
647 irep_idt param_base_name = arg.get_base_name();
649 if(param_base_name.
empty())
659 arg_symb.
type=arg.type();
661 arg.set_identifier(arg_symb.
name);
670 lookup(args[0].get_identifier()).symbol_expr(),
678 expr_call.
arguments().reserve(args.size());
680 for(
const auto &arg : args)
705 components.push_back(new_compo);
716 virtual_bases.erase(virtual_bases.begin());
721 if(is_static && !is_method)
726 static_symbol.
name=identifier;
741 error() <<
"redeclaration of static member '" << static_symbol.
base_name
758 ops.push_back(value);
778 if(type.
id()==ID_array)
784 if(array_type.
size().
id() == ID_symbol)
808 if(base_name.
empty())
811 if(compound.
type().
id()==ID_code)
823 id.is_static_member=compound.
get_bool(ID_is_static);
846 for(
const auto &id_it : id_set)
852 if(!
id.is_class() && !
id.is_enum())
855 error() <<
"'" << base_name <<
"' already in compound scope" <<
eom;
869 id.is_static_member=compound.
get_bool(ID_is_static);
883 error() <<
"friend template not supported" <<
eom;
893 if(ftype.
id()!=ID_struct && ftype.
id()!=ID_union)
896 error() <<
"unexpected friend" <<
eom;
903 error() <<
"friend declaration must not have compound body" <<
eom;
919 std::cout <<
"friend declaration: " << declaration.
pretty() <<
'\n';
925 std::cout <<
"decl: " << sub_it.pretty() <<
"\n with value "
926 << sub_it.value().pretty() <<
'\n';
930 if(sub_it.value().is_not_nil())
934 cpp_declarator_converter.
is_friend =
true;
952 assert(symbol.
type.
id()==ID_struct ||
953 symbol.
type.
id()==ID_union);
961 if(type.
id()==ID_union)
964 error() <<
"union types must not have bases" <<
eom;
979 bool found_ctor=
false;
980 bool found_dtor=
false;
986 if(it->id()==ID_cpp_declaration)
1000 declaration.
set(ID_C_access, access);
1011 if(declaration.
type().
id()==ID_struct ||
1012 declaration.
type().
id()==ID_union ||
1013 declaration.
type().
id()==ID_c_enum)
1015 declaration.
type().
set(ID_C_tag_only_declaration,
true);
1028 error() <<
"invalid storage class specified for field" <<
eom;
1036 final_type.
get_bool(ID_C_is_anonymous))
1039 if(final_type.
id()!=ID_union &&
1040 final_type.
id()!=ID_struct)
1043 error() <<
"member declaration does not declare anything"
1049 declaration, access, components);
1070 declaration, declarator, components,
1071 access, is_static, is_typedef, is_mutable);
1074 else if(it->id()==
"cpp-public")
1076 else if(it->id()==
"cpp-private")
1078 else if(it->id()==
"cpp-protected")
1079 access=ID_protected;
1097 dtor,
dtor.declarators()[0], components,
1098 ID_public,
false,
false,
false);
1102 if(symbol.
type.
id()==ID_struct)
1108 exprt cpp_public(
"cpp-public");
1124 if(it->id()==ID_cpp_declaration)
1136 declarator.name().get_base_name();
1139 if(declarator.value().is_not_nil())
1141 if(declarator.find(ID_member_initializers).is_nil())
1142 declarator.set(ID_member_initializers, ID_member_initializers);
1144 if(type.
id() == ID_union)
1147 {}, type.
components(), declarator.member_initializers());
1154 declarator.member_initializers());
1159 declarator.member_initializers());
1171 declaration, declarator, components,
1172 access, is_static, is_typedef, is_mutable);
1175 else if(it->id()==
"cpp-public")
1177 else if(it->id()==
"cpp-private")
1179 else if(it->id()==
"cpp-protected")
1180 access=ID_protected;
1198 exprt value(ID_cpp_not_typechecked);
1205 ID_public,
false,
false,
false);
1225 ID_public,
false,
false,
false);
1234 irept &initializers,
1239 if(!initializers.
get_sub().empty())
1243 initializers.
find(ID_C_source_location));
1248 error() <<
"only constructors are allowed to "
1249 <<
"have member initializers" <<
eom;
1256 error() <<
"only constructors with body are allowed to "
1257 <<
"have member initializers" <<
eom;
1261 if(
to_code(value).get_statement() != ID_block)
1264 exprt::operandst::iterator o_it=value.
operands().begin();
1265 for(
const auto &initializer : initializers.
get_sub())
1268 value.
operands().insert(o_it,
static_cast<const exprt &
>(initializer));
1275 const symbolt &compound_symbol,
1277 irept &initializers,
1278 const typet &method_qualifier,
1287 if(!method_qualifier.
id().
empty())
1290 error() <<
"method is static -- no qualifiers allowed" <<
eom;
1321 symbol.
name=identifier;
1324 symbol.
mode = compound_symbol.
mode;
1335 if(symbol_exists && new_symbol->
is_weak)
1338 *new_symbol = std::move(symbol);
1340 else if(symbol_exists)
1343 error() <<
"failed to insert new method symbol: " << symbol.
name <<
'\n'
1344 <<
"name of previous symbol: " << new_symbol->
name <<
'\n'
1345 <<
"location of previous symbol: " << new_symbol->
location <<
eom;
1359 const symbolt &compound_symbol,
1361 const typet &method_qualifier)
1365 if(compound_symbol.
type.
id() == ID_union)
1371 subtype.
set(ID_C_constant,
true);
1374 subtype.
set(ID_C_volatile,
true);
1384 parameters.insert(parameters.begin(), parameter);
1388 const symbolt &struct_union_symbol)
1400 for(
const auto &comp : struct_union_components)
1402 if(comp.type().id()==ID_code)
1405 error() <<
"anonymous struct/union member '"
1407 <<
"' shall not have function members" <<
eom;
1411 if(comp.get_anonymous())
1413 const symbolt &symbol=
lookup(comp.type().get(ID_identifier));
1419 const irep_idt &base_name=comp.get_base_name();
1424 error() <<
"'" << base_name <<
"' already in scope" <<
eom;
1430 id.identifier=comp.get_name();
1431 id.class_identifier=struct_union_symbol.
name;
1442 symbolt &struct_union_symbol =
1449 error() <<
"storage class is not allowed here" <<
eom;
1456 error() <<
"anonymous struct/union member is not POD" <<
eom;
1467 typet compound_type;
1469 if(struct_union_symbol.
type.
id() == ID_union)
1487 struct_union_symbol.
type.
set(ID_C_unnamed_object, base_name);
1492 const exprt &
object,
1498 assert(followed_type.
id()==ID_struct ||
1499 followed_type.
id()==ID_union);
1512 if(
component.get_name()==component_name)
1521 member.
set(ID_C_not_accessible,
true);
1527 error() <<
"error: member '" << component_name
1528 <<
"' is not accessible (" <<
component.get(ID_access) <<
")"
1534 if(
object.get_bool(ID_C_lvalue))
1535 member.
set(ID_C_lvalue,
true);
1538 object.type().get_bool(ID_C_constant) &&
1541 member.
type().
set(ID_C_constant,
true);
1554 if(component_type.
id()==ID_union ||
1555 component_type.
id()==ID_struct)
1558 if(
get_component(source_location, tmp, component_name, member))
1563 error() <<
"error: member '" << component_name
1564 <<
"' is not accessible" <<
eom;
1568 if(
object.get_bool(ID_C_lvalue))
1569 member.
set(ID_C_lvalue,
true);
1572 object.get_bool(ID_C_constant) &&
1575 member.
type().
set(ID_C_constant,
true);
1594 if(access == ID_noaccess)
1597 if(access==ID_public)
1600 assert(access==ID_private ||
1601 access==ID_protected);
1604 struct_union_type.
get(ID_name);
1607 !(pscope->is_root_scope());
1608 pscope = &(pscope->get_parent()))
1610 if(pscope->is_class())
1612 if(pscope->identifier==struct_identifier)
1629 for(
const auto &friend_symb : friends)
1635 !(pscope->is_root_scope());
1636 pscope = &(pscope->get_parent()))
1638 if(friend_scope.
identifier==pscope->identifier)
1641 if(pscope->is_class())
1651 std::set<irep_idt> &set_bases)
const
1653 for(
const auto &b : type.
bases())
1655 DATA_INVARIANT(b.id() == ID_base,
"base class expression expected");
1659 set_bases.insert(base.
get(ID_name));
1666 std::list<irep_idt> &vbases)
const
1668 if(std::find(vbases.begin(), vbases.end(), type.
get(ID_name))!=vbases.end())
1671 for(
const auto &b : type.
bases())
1673 DATA_INVARIANT(b.id() == ID_base,
"base class expression expected");
1677 if(b.get_bool(ID_virtual))
1678 vbases.push_back(base.
get(ID_name));
1688 if(from.
get(ID_name)==to.
get(ID_name))
1691 std::set<irep_idt> bases;
1695 return bases.find(to.
get(ID_name))!=bases.end();
1700 const typet &dest_type)
1704 assert(src_type.
id()== ID_pointer);
1705 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
const typet & subtype() 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)
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)
irept & add(const irep_namet &name)
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)
const irept & find(const irep_namet &name) const
void typecheck_compound_bases(struct_typet &type)
const symbolt & lookup(const irep_idt &name) const
Lookup a symbol in the namespace.
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)
irep_idt pretty_name
Language-specific display name.
const codet & to_code(const exprt &expr)
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
typet & type()
Return the type of the expression.
void typecheck_type(typet &) override
bool get_bool(const irep_namet &name) const
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
#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
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
void set_inlined(bool value)
void make_ptr_typecast(exprt &expr, const typet &dest_type)
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
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
void remove(const irep_namet &name)
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
C++ Language Type Checking.
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.
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.
const std::string & get_string(const irep_namet &name) const
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.
const source_locationt & source_location() const
codet representation of a "return from a function" statement.
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)
const irep_idt & get(const irep_namet &name) const
source_locationt location
Source code location of definition of symbol.
void set(const irep_namet &name, const irep_idt &value)
exprt::operandst & arguments()
static bool has_auto(const typet &type)
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
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
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)
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.
cpp_idt & insert(const irep_idt &_base_name)
C++ Language Type Checking.
typet merge_type(const typet &declaration_type) const
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.