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);
145 type.
set(ID_C_is_anonymous,
true);
159 if(type.
get_bool(ID_C_is_anonymous))
162 dest_scope=&
tag_scope(base_name, has_body, tag_only_declaration);
170 &cpp_typecheck_resolve.
resolve_scope(cpp_name, base_name, t_args);
187 const symbolt &symbol=*maybe_symbol;
205 error() <<
"error: compound tag `" << base_name
206 <<
"' declared previously\n" 207 <<
"location of previous definition: " 218 symbol.
name=symbol_name;
240 error() <<
"cpp_typecheckt::typecheck_compound_type: " 241 <<
"symbol_table.move() failed" <<
eom;
253 id.class_identifier=new_symbol->
name;
268 qualifiers.
write(symbol_type);
269 type.
swap(symbol_type);
282 bool is_cast_operator=
283 declaration.
type().
id()==
"cpp-cast-operator";
288 declarator.
name().
get_sub().front().id()==ID_operator);
323 error() <<
"declarator in compound needs to be simple name" 328 bool is_method=!is_typedef && final_type.
id()==ID_code;
335 final_type.
set(ID_C_member_name, symbol.
name);
339 if(is_virtual && !is_method)
342 error() <<
"only methods can be virtual" <<
eom;
346 if(is_inline && !is_method)
349 error() <<
"only methods can be inlined" <<
eom;
353 if(is_virtual && is_static)
356 error() <<
"static methods cannot be virtual" <<
eom;
360 if(is_cast_operator && is_static)
363 error() <<
"cast operators cannot be static`" <<
eom;
370 error() <<
"constructors cannot be virtual" <<
eom;
377 error() <<
"only constructors can be explicit" <<
eom;
384 error() <<
"member function must return a value or void" <<
eom;
392 error() <<
"destructor with wrong name" <<
eom;
405 is_method || is_static)
416 identifier=base_name;
419 component.
set(ID_name, identifier);
420 component.
type()=final_type;
421 component.
set(ID_access, access);
422 component.
set(ID_base_name, base_name);
423 component.
set(ID_pretty_name, base_name);
428 component.
set(
"is_operator",
true);
429 component.
type().
set(ID_C_is_operator,
true);
433 component.
set(
"is_cast_operator",
true);
436 component.
set(
"is_explicit",
true);
439 const typet &method_qualifier=
440 static_cast<const typet &
>(declarator.
add(ID_method_qualifier));
444 component.
set(ID_is_static,
true);
445 component.
type().
set(ID_C_is_static,
true);
449 component.
set(ID_is_type,
true);
452 component.
set(
"is_mutable",
true);
460 value.
id() == ID_code &&
464 component.
set(ID_access,
"noaccess");
470 std::string virtual_name=
474 static_cast<const typet &>(component.
find(ID_type))));
477 virtual_name+=
"$const";
480 virtual_name +=
"$volatile";
482 if(component.
type().
get(ID_return_type)==ID_destructor)
483 virtual_name=
"@dtor";
486 std::set<irep_idt> virtual_bases;
488 for(
const auto &comp : components)
490 if(comp.get_bool(ID_is_virtual))
492 if(comp.get(
"virtual_name")==virtual_name)
507 symbol.
name, component, initializers,
508 method_qualifier, value);
510 if(!value.
is_nil() && !is_static)
513 error() <<
"no initialization allowed here" <<
eom;
519 component.
type().
set(ID_C_is_virtual,
true);
520 component.
type().
set(ID_C_virtual_name, virtual_name);
530 error() <<
"expected 0 to mark pure virtual method, got " << i <<
eom;
533 component.
set(
"is_pure_virtual",
true);
551 vt_symb_type.
name= vt_name;
554 vt_symb_type.
mode=ID_cpp;
568 compo.
set(ID_base_name,
"@vtable_pointer");
572 compo.
set(
"is_vtptr",
true);
573 compo.
set(ID_access, ID_public);
574 components.push_back(compo);
579 INVARIANT(vt.
id()==ID_struct,
"Virtual tables must be stored as struct");
582 component.
set(
"virtual_name", virtual_name);
583 component.
set(
"is_virtual", is_virtual);
589 vt_entry.
set(ID_base_name, virtual_name);
590 vt_entry.
set(ID_pretty_name, virtual_name);
591 vt_entry.
set(ID_access, ID_public);
593 virtual_table.components().push_back(vt_entry);
596 while(!virtual_bases.empty())
598 irep_idt virtual_base=*virtual_bases.begin();
606 func_symb.
mode=ID_cpp;
619 for(
auto &arg : args)
623 if(base_name.
empty())
630 arg_symb.
mode=ID_cpp;
634 arg.
set(ID_C_identifier, arg_symb.
name);
643 lookup(args[0].
get(ID_C_identifier)).symbol_expr(),
649 expr_call.arguments().reserve(args.size());
650 expr_call.arguments().push_back(late_cast);
652 for(
const auto &arg : args)
654 expr_call.arguments().push_back(
655 lookup(arg.
get(ID_C_identifier)).symbol_expr());
680 components.push_back(new_compo);
691 virtual_bases.erase(virtual_bases.begin());
696 if(is_static && !is_method)
701 static_symbol.
name=identifier;
702 static_symbol.
type=component.
type();
716 error() <<
"redeclaration of static member `" 742 ops.push_back(value);
756 components.push_back(component);
762 if(type.
id()==ID_array)
781 if(base_name.
empty())
784 if(compound.
type().
id()==ID_code)
795 compound.
find(ID_type).
get(ID_return_type)==ID_constructor;
797 id.is_static_member=compound.
get_bool(ID_is_static);
822 for(
const auto &id_it :
id_set)
828 if(!
id.is_class() && !
id.is_enum())
831 error() <<
"`" << base_name
832 <<
"' already in compound scope" <<
eom;
846 id.is_static_member=compound.
get_bool(ID_is_static);
863 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;
903 bool has_value = sub_it.value().is_not_nil();
944 assert(symbol.
type.
id()==ID_struct ||
945 symbol.
type.
id()==ID_union);
951 if(!type.find(ID_bases).get_sub().empty())
953 if(type.id()==ID_union)
956 error() <<
"union types must not have bases" <<
eom;
963 exprt &body=
static_cast<exprt &
>(type.add(ID_body));
970 type.get_bool(ID_C_class)?ID_private:ID_public;
972 bool found_ctor=
false;
973 bool found_dtor=
false;
979 if(it->id()==ID_cpp_declaration)
993 declaration.
set(ID_C_access, access);
1004 if(declaration.
type().
id()==ID_struct ||
1005 declaration.
type().
id()==ID_union ||
1006 declaration.
type().
id()==ID_c_enum)
1008 declaration.
type().
set(ID_C_tag_only_declaration,
true);
1021 error() <<
"invalid storage class specified for field" <<
eom;
1029 final_type.
get_bool(ID_C_is_anonymous))
1032 if(final_type.
id()!=ID_union &&
1033 final_type.
id()!=ID_struct)
1036 error() <<
"member declaration does not declare anything" 1042 declaration, access, components);
1063 declaration, declarator, components,
1064 access, is_static, is_typedef, is_mutable);
1067 else if(it->id()==
"cpp-public")
1069 else if(it->id()==
"cpp-private")
1071 else if(it->id()==
"cpp-protected")
1072 access=ID_protected;
1090 dtor,
dtor.declarators()[0], components,
1091 ID_public,
false,
false,
false);
1095 if(symbol.
type.
id()==ID_struct)
1101 exprt cpp_public(
"cpp-public");
1112 type.get_bool(ID_C_class)?ID_private:ID_public;
1118 if(it->id()==ID_cpp_declaration)
1130 declarator.name().get_base_name();
1133 if(declarator.value().is_not_nil())
1135 if(declarator.find(ID_member_initializers).is_nil())
1136 declarator.set(ID_member_initializers, ID_member_initializers);
1141 declarator.member_initializers());
1145 declarator.member_initializers());
1157 declaration, declarator, components,
1158 access, is_static, is_typedef, is_mutable);
1161 else if(it->id()==
"cpp-public")
1163 else if(it->id()==
"cpp-private")
1165 else if(it->id()==
"cpp-protected")
1166 access=ID_protected;
1184 exprt value(
"cpp_not_typechecked");
1185 value.copy_to_operands(cpctor.
declarators()[0].value());
1191 ID_public,
false,
false,
false);
1211 ID_public,
false,
false,
false);
1220 irept &initializers,
1225 type.
find(ID_return_type).
id()==ID_constructor;
1228 if(!initializers.
get_sub().empty())
1232 initializers.
find(ID_C_source_location));
1237 error() <<
"only constructors are allowed to " 1238 <<
"have member initializers" <<
eom;
1245 error() <<
"only constructors with body are allowed to " 1246 <<
"have member initializers" <<
eom;
1252 exprt::operandst::iterator o_it=value.
operands().begin();
1255 o_it=value.
operands().insert(o_it, static_cast<const exprt &>(*it));
1262 const irep_idt &compound_identifier,
1264 irept &initializers,
1265 const typet &method_qualifier,
1272 if(component.
get_bool(ID_is_static))
1274 if(!method_qualifier.
id().
empty())
1277 error() <<
"method is static -- no qualifiers allowed" <<
eom;
1284 compound_identifier,
1306 type.
set(ID_C_inlined,
true);
1308 symbol.
name=identifier;
1324 error() <<
"failed to insert new method symbol: " 1325 << symbol.
name <<
"\n" 1326 <<
"name of previous symbol: " 1327 << new_symbol->
name <<
"\n" 1328 <<
"location of previous symbol: " 1346 const typet &method_qualifier)
1362 subtype.
set(ID_C_constant,
true);
1365 subtype.
set(ID_C_volatile,
true);
1371 const symbolt &struct_union_symbol)
1383 for(
const auto &comp : struct_union_components)
1385 if(comp.type().id()==ID_code)
1388 error() <<
"anonymous struct/union member `" 1390 <<
"' shall not have function members" <<
eom;
1394 if(comp.get_anonymous())
1396 const symbolt &symbol=
lookup(comp.type().get(ID_identifier));
1402 const irep_idt &base_name=comp.get_base_name();
1407 error() <<
"`" << base_name <<
"' already in scope" <<
eom;
1413 id.identifier=comp.get_name();
1414 id.class_identifier=struct_union_symbol.
name;
1432 error() <<
"storage class is not allowed here" <<
eom;
1439 error() <<
"anonymous struct/union member is not POD" <<
eom;
1459 components.push_back(component);
1465 struct_union_symbol.
type.
set(ID_C_unnamed_object, base_name);
1470 const exprt &
object,
1476 assert(followed_type.
id()==ID_struct ||
1477 followed_type.
id()==ID_union);
1483 final_type.components();
1485 for(
const auto &component : components)
1487 member_exprt tmp(
object, component.get_name(), component.type());
1490 if(component.get_name()==component_name)
1499 member.
set(ID_C_not_accessible,
true);
1500 member.
set(ID_C_access, component.get(ID_access));
1505 error() <<
"error: member `" << component_name
1506 <<
"' is not accessible (" << component.get(ID_access) <<
")" 1512 if(
object.get_bool(ID_C_lvalue))
1513 member.
set(ID_C_lvalue,
true);
1515 if(
object.type().get_bool(ID_C_constant) &&
1516 !component.get_bool(
"is_mutable"))
1517 member.
type().
set(ID_C_constant,
true);
1527 const typet &component_type=
follow(component.type());
1529 if(component_type.
id()==ID_union ||
1530 component_type.
id()==ID_struct)
1533 if(
get_component(source_location, tmp, component_name, member))
1538 error() <<
"error: member `" << component_name
1539 <<
"' is not accessible" <<
eom;
1543 if(
object.get_bool(ID_C_lvalue))
1544 member.
set(ID_C_lvalue,
true);
1546 if(
object.get_bool(ID_C_constant) &&
1547 !component.get_bool(
"is_mutable"))
1548 member.
type().
set(ID_C_constant,
true);
1566 if(access==
"noaccess")
1569 if(access==ID_public)
1572 assert(access==ID_private ||
1573 access==ID_protected);
1576 struct_union_type.
get(ID_name);
1603 const irept &friend_symb=*f_it;
1627 std::set<irep_idt> &set_bases)
const 1633 assert(it->id()==ID_base);
1634 assert(it->get(ID_type)==ID_symbol);
1639 set_bases.insert(base.get(ID_name));
1646 std::list<irep_idt> &vbases)
const 1648 if(std::find(vbases.begin(), vbases.end(), type.
get(ID_name))!=vbases.end())
1655 assert(it->id()==ID_base);
1656 assert(it->get(ID_type)==ID_symbol);
1661 if(it->get_bool(ID_virtual))
1662 vbases.push_back(base.get(ID_name));
1672 if(from.
get(ID_name)==to.
get(ID_name))
1675 std::set<irep_idt> bases;
1679 return bases.find(to.
get(ID_name))!=bases.end();
1684 const typet &dest_type)
1688 assert(src_type.
id()== ID_pointer);
1689 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.
dstringt has one field, an unsigned integer no which is an index into a static table of strings...
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)