Go to the documentation of this file.
34 if(type.
id()==ID_const)
36 else if(type.
id()==ID_merged_type)
50 if(type.
id()==ID_volatile)
52 else if(type.
id()==ID_merged_type)
66 if(type.
id() == ID_auto)
69 type.
id() == ID_merged_type || type.
id() == ID_frontend_pointer ||
70 type.
id() == ID_pointer)
85 bool tag_only_declaration)
101 if(has_body || tag_only_declaration)
111 for(
const auto &
id : id_set)
113 return static_cast<cpp_scopet &
>(
id->get_parent());
128 type.
remove(ID_C_constant);
129 type.
remove(ID_C_volatile);
130 type.
remove(ID_C_restricted);
137 bool tag_only_declaration=type.
get_bool(ID_C_tag_only_declaration);
138 bool is_union = type.
id() == ID_union;
146 type.
set(ID_C_is_anonymous,
true);
160 if(type.
get_bool(ID_C_is_anonymous))
163 dest_scope=&
tag_scope(base_name, has_body, tag_only_declaration);
171 &cpp_typecheck_resolve.
resolve_scope(cpp_name, base_name, t_args);
188 const symbolt &symbol=*maybe_symbol;
208 error() <<
"error: compound tag '" << base_name
209 <<
"' declared previously\n"
210 <<
"location of previous definition: " << symbol.
location
221 symbol.
name=symbol_name;
243 error() <<
"cpp_typecheckt::typecheck_compound_type: "
244 <<
"symbol_table.move() failed" <<
eom;
256 id.class_identifier=new_symbol->
name;
275 qualifiers.
write(tag_type);
282 qualifiers.
write(tag_type);
297 bool is_cast_operator=
298 declaration.
type().
id()==
"cpp-cast-operator";
303 declarator.
name().
get_sub().front().id()==ID_operator);
337 error() <<
"declarator in compound needs to be simple name"
342 bool is_method=!is_typedef && final_type.
id()==ID_code;
349 final_type.
set(ID_C_member_name, symbol.
name);
353 if(is_virtual && !is_method)
356 error() <<
"only methods can be virtual" <<
eom;
360 if(is_inline && !is_method)
363 error() <<
"only methods can be inlined" <<
eom;
367 if(is_virtual && is_static)
370 error() <<
"static methods cannot be virtual" <<
eom;
374 if(is_cast_operator && is_static)
377 error() <<
"cast operators cannot be static" <<
eom;
384 error() <<
"constructors cannot be virtual" <<
eom;
391 error() <<
"only constructors can be explicit" <<
eom;
398 error() <<
"member function must return a value or void" <<
eom;
406 error() <<
"destructor with wrong name" <<
eom;
418 is_method || is_static)
429 identifier=base_name;
441 component.type().set(ID_C_is_operator,
true);
445 component.set(ID_is_cast_operator,
true);
451 const typet &method_qualifier=
452 static_cast<const typet &
>(declarator.
add(ID_method_qualifier));
457 component.type().set(ID_C_is_static,
true);
472 value.
id() == ID_code &&
486 virtual_name+=
"$const";
489 virtual_name +=
"$volatile";
492 virtual_name=
"@dtor";
495 std::set<irep_idt> virtual_bases;
497 for(
const auto &comp : components)
499 if(comp.get_bool(ID_is_virtual))
501 if(comp.get(ID_virtual_name) == virtual_name)
517 method_qualifier, value);
519 if(!value.
is_nil() && !is_static)
522 error() <<
"no initialization allowed here" <<
eom;
528 component.type().set(ID_C_is_virtual,
true);
529 component.type().set(ID_C_virtual_name, virtual_name);
539 error() <<
"expected 0 to mark pure virtual method, got " << i <<
eom;
560 vt_symb_type.
name= vt_name;
563 vt_symb_type.
mode=ID_cpp;
579 compo.
set(ID_is_vtptr,
true);
580 compo.
set(ID_access, ID_public);
581 components.push_back(compo);
586 INVARIANT(vt.
id()==ID_struct,
"Virtual tables must be stored as struct");
589 component.set(ID_virtual_name, virtual_name);
590 component.set(ID_is_virtual, is_virtual);
594 id2string(vt_name) +
"::" + virtual_name,
598 vt_entry.
set(ID_access, ID_public);
600 virtual_table.
components().push_back(vt_entry);
603 while(!virtual_bases.empty())
605 irep_idt virtual_base=*virtual_bases.begin();
626 for(
auto &arg : args)
628 irep_idt param_base_name = arg.get_base_name();
630 if(param_base_name.
empty())
640 arg_symb.
type=arg.type();
642 arg.set_identifier(arg_symb.
name);
651 lookup(args[0].get_identifier()).symbol_expr(),
659 expr_call.
arguments().reserve(args.size());
661 for(
const auto &arg : args)
686 components.push_back(new_compo);
697 virtual_bases.erase(virtual_bases.begin());
702 if(is_static && !is_method)
707 static_symbol.
name=identifier;
722 error() <<
"redeclaration of static member '" << static_symbol.
base_name
739 ops.push_back(value);
759 if(type.
id()==ID_array)
765 if(array_type.
size().
id() == ID_symbol)
789 if(base_name.
empty())
792 if(compound.
type().
id()==ID_code)
804 id.is_static_member=compound.
get_bool(ID_is_static);
827 for(
const auto &id_it : id_set)
833 if(!
id.is_class() && !
id.is_enum())
836 error() <<
"'" << base_name <<
"' already in compound scope" <<
eom;
850 id.is_static_member=compound.
get_bool(ID_is_static);
864 error() <<
"friend template not supported" <<
eom;
874 if(ftype.
id()!=ID_struct && ftype.
id()!=ID_union)
877 error() <<
"unexpected friend" <<
eom;
884 error() <<
"friend declaration must not have compound body" <<
eom;
900 std::cout <<
"friend declaration: " << declaration.
pretty() <<
'\n';
906 std::cout <<
"decl: " << sub_it.pretty() <<
"\n with value "
907 << sub_it.value().pretty() <<
'\n';
911 if(sub_it.value().is_not_nil())
915 cpp_declarator_converter.
is_friend =
true;
933 assert(symbol.
type.
id()==ID_struct ||
934 symbol.
type.
id()==ID_union);
942 if(type.
id()==ID_union)
945 error() <<
"union types must not have bases" <<
eom;
960 bool found_ctor=
false;
961 bool found_dtor=
false;
967 if(it->id()==ID_cpp_declaration)
981 declaration.
set(ID_C_access, access);
992 if(declaration.
type().
id()==ID_struct ||
993 declaration.
type().
id()==ID_union ||
994 declaration.
type().
id()==ID_c_enum)
996 declaration.
type().
set(ID_C_tag_only_declaration,
true);
1009 error() <<
"invalid storage class specified for field" <<
eom;
1017 final_type.
get_bool(ID_C_is_anonymous))
1020 if(final_type.
id()!=ID_union &&
1021 final_type.
id()!=ID_struct)
1024 error() <<
"member declaration does not declare anything"
1030 declaration, access, components);
1051 declaration, declarator, components,
1052 access, is_static, is_typedef, is_mutable);
1055 else if(it->id()==
"cpp-public")
1057 else if(it->id()==
"cpp-private")
1059 else if(it->id()==
"cpp-protected")
1060 access=ID_protected;
1078 dtor,
dtor.declarators()[0], components,
1079 ID_public,
false,
false,
false);
1083 if(symbol.
type.
id()==ID_struct)
1089 exprt cpp_public(
"cpp-public");
1105 if(it->id()==ID_cpp_declaration)
1117 declarator.name().get_base_name();
1120 if(declarator.value().is_not_nil())
1122 if(declarator.find(ID_member_initializers).is_nil())
1123 declarator.set(ID_member_initializers, ID_member_initializers);
1125 if(type.
id() == ID_union)
1128 {}, type.
components(), declarator.member_initializers());
1135 declarator.member_initializers());
1140 declarator.member_initializers());
1152 declaration, declarator, components,
1153 access, is_static, is_typedef, is_mutable);
1156 else if(it->id()==
"cpp-public")
1158 else if(it->id()==
"cpp-private")
1160 else if(it->id()==
"cpp-protected")
1161 access=ID_protected;
1179 exprt value(ID_cpp_not_typechecked);
1186 ID_public,
false,
false,
false);
1206 ID_public,
false,
false,
false);
1215 irept &initializers,
1220 if(!initializers.
get_sub().empty())
1224 initializers.
find(ID_C_source_location));
1229 error() <<
"only constructors are allowed to "
1230 <<
"have member initializers" <<
eom;
1237 error() <<
"only constructors with body are allowed to "
1238 <<
"have member initializers" <<
eom;
1242 if(
to_code(value).get_statement() != ID_block)
1245 exprt::operandst::iterator o_it=value.
operands().begin();
1248 o_it=value.
operands().insert(o_it,
static_cast<const exprt &
>(*it));
1255 const symbolt &compound_symbol,
1257 irept &initializers,
1258 const typet &method_qualifier,
1267 if(!method_qualifier.
id().
empty())
1270 error() <<
"method is static -- no qualifiers allowed" <<
eom;
1301 symbol.
name=identifier;
1304 symbol.
mode = compound_symbol.
mode;
1315 if(symbol_exists && new_symbol->
is_weak)
1318 *new_symbol = std::move(symbol);
1320 else if(symbol_exists)
1323 error() <<
"failed to insert new method symbol: " << symbol.
name <<
'\n'
1324 <<
"name of previous symbol: " << new_symbol->
name <<
'\n'
1325 <<
"location of previous symbol: " << new_symbol->
location <<
eom;
1339 const symbolt &compound_symbol,
1341 const typet &method_qualifier)
1345 if(compound_symbol.
type.
id() == ID_union)
1351 subtype.
set(ID_C_constant,
true);
1354 subtype.
set(ID_C_volatile,
true);
1364 parameters.insert(parameters.begin(), parameter);
1368 const symbolt &struct_union_symbol)
1380 for(
const auto &comp : struct_union_components)
1382 if(comp.type().id()==ID_code)
1385 error() <<
"anonymous struct/union member '"
1387 <<
"' shall not have function members" <<
eom;
1391 if(comp.get_anonymous())
1393 const symbolt &symbol=
lookup(comp.type().get(ID_identifier));
1399 const irep_idt &base_name=comp.get_base_name();
1404 error() <<
"'" << base_name <<
"' already in scope" <<
eom;
1410 id.identifier=comp.get_name();
1411 id.class_identifier=struct_union_symbol.
name;
1422 symbolt &struct_union_symbol =
1429 error() <<
"storage class is not allowed here" <<
eom;
1436 error() <<
"anonymous struct/union member is not POD" <<
eom;
1447 typet compound_type;
1449 if(struct_union_symbol.
type.
id() == ID_union)
1467 struct_union_symbol.
type.
set(ID_C_unnamed_object, base_name);
1472 const exprt &
object,
1478 assert(followed_type.
id()==ID_struct ||
1479 followed_type.
id()==ID_union);
1492 if(
component.get_name()==component_name)
1501 member.
set(ID_C_not_accessible,
true);
1507 error() <<
"error: member '" << component_name
1508 <<
"' is not accessible (" <<
component.get(ID_access) <<
")"
1514 if(
object.get_bool(ID_C_lvalue))
1515 member.
set(ID_C_lvalue,
true);
1518 object.type().get_bool(ID_C_constant) &&
1521 member.
type().
set(ID_C_constant,
true);
1534 if(component_type.
id()==ID_union ||
1535 component_type.
id()==ID_struct)
1538 if(
get_component(source_location, tmp, component_name, member))
1543 error() <<
"error: member '" << component_name
1544 <<
"' is not accessible" <<
eom;
1548 if(
object.get_bool(ID_C_lvalue))
1549 member.
set(ID_C_lvalue,
true);
1552 object.get_bool(ID_C_constant) &&
1555 member.
type().
set(ID_C_constant,
true);
1574 if(access == ID_noaccess)
1577 if(access==ID_public)
1580 assert(access==ID_private ||
1581 access==ID_protected);
1584 struct_union_type.
get(ID_name);
1587 !(pscope->is_root_scope());
1588 pscope = &(pscope->get_parent()))
1590 if(pscope->is_class())
1592 if(pscope->identifier==struct_identifier)
1611 const irept &friend_symb=*f_it;
1617 !(pscope->is_root_scope());
1618 pscope = &(pscope->get_parent()))
1620 if(friend_scope.
identifier==pscope->identifier)
1623 if(pscope->is_class())
1633 std::set<irep_idt> &set_bases)
const
1635 for(
const auto &b : type.
bases())
1637 DATA_INVARIANT(b.id() == ID_base,
"base class expression expected");
1641 set_bases.insert(base.
get(ID_name));
1648 std::list<irep_idt> &vbases)
const
1650 if(std::find(vbases.begin(), vbases.end(), type.
get(ID_name))!=vbases.end())
1653 for(
const auto &b : type.
bases())
1655 DATA_INVARIANT(b.id() == ID_base,
"base class expression expected");
1659 if(b.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);
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.
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
#define forall_subtypes(it, type)
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.
#define forall_irep(it, irep)
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
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.