Skip to content

Commit c6a55e7

Browse files
committed
Move class_typet::baset to struct_typet
Structs can have base classes/structs. This enables wide use of the bases() API.
1 parent 45f469d commit c6a55e7

10 files changed

+134
-139
lines changed

src/cpp/cpp_declarator_converter.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,16 @@ symbolt &cpp_declarator_convertert::convert(
152152

153153
irep_idt identifier=symbol_expr.type().get(ID_identifier);
154154
const symbolt &symb=cpp_typecheck.lookup(identifier);
155-
const typet &type = symb.type;
156-
assert(type.id()==ID_struct);
155+
const struct_typet &type = to_struct_type(symb.type);
157156

158157
if(declarator.find(ID_member_initializers).is_nil())
159158
declarator.set(ID_member_initializers, ID_member_initializers);
160159

161160
cpp_typecheck.check_member_initializers(
162-
type.find(ID_bases),
163-
to_struct_type(type).components(),
164-
declarator.member_initializers());
161+
type.bases(), type.components(), declarator.member_initializers());
165162

166163
cpp_typecheck.full_member_initialization(
167-
to_struct_type(type),
168-
declarator.member_initializers());
164+
type, declarator.member_initializers());
169165
}
170166

171167
if(!storage_spec.is_extern())

src/cpp/cpp_exception_id.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Author: Daniel Kroening, [email protected]
1212
#include "cpp_exception_id.h"
1313

1414
#include <util/invariant.h>
15+
#include <util/std_types.h>
1516

1617
/// turns a type into a list of relevant exception IDs
1718
void cpp_exception_list_rec(
@@ -48,13 +49,8 @@ void cpp_exception_list_rec(
4849
dest.push_back("struct_"+src.get_string(ID_tag));
4950

5051
// now do any bases, recursively
51-
const irept::subt &bases=src.find(ID_bases).get_sub();
52-
53-
forall_irep(it, bases)
54-
{
55-
const typet &type=static_cast<const typet &>(it->find(ID_type));
56-
cpp_exception_list_rec(type, ns, suffix, dest);
57-
}
52+
for(const auto &b : to_struct_type(src).bases())
53+
cpp_exception_list_rec(b.type(), ns, suffix, dest);
5854
}
5955
else
6056
{

src/cpp/cpp_typecheck.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ class cpp_typecheckt:public c_typecheck_baset
262262
codet dtor(const symbolt &symb);
263263

264264
void check_member_initializers(
265-
const irept &bases,
265+
const struct_typet::basest &bases,
266266
const struct_typet::componentst &components,
267267
const irept &initializers);
268268

src/cpp/cpp_typecheck_bases.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,18 @@ void cpp_typecheckt::add_base_components(
141141
vbases.insert(from_name);
142142

143143
// look at the the parents of the base type
144-
forall_irep(it, from.find(ID_bases).get_sub())
144+
for(const auto &b : from.bases())
145145
{
146-
irep_idt sub_access=it->get(ID_access);
146+
irep_idt sub_access = b.get(ID_access);
147147

148148
if(access==ID_private)
149149
sub_access=ID_private;
150150
else if(access==ID_protected && sub_access!=ID_private)
151151
sub_access=ID_protected;
152152

153-
const symbolt &symb=
154-
lookup(it->find(ID_type).get(ID_identifier));
153+
const symbolt &symb = lookup(to_symbol_type(b.type()).get_identifier());
155154

156-
bool is_virtual=it->get_bool(ID_virtual);
155+
const bool is_virtual = b.get_bool(ID_virtual);
157156

158157
// recursive call
159158
add_base_components(

src/cpp/cpp_typecheck_compound_type.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,10 +1135,18 @@ void cpp_typecheckt::typecheck_compound_body(symbolt &symbol)
11351135
if(declarator.find(ID_member_initializers).is_nil())
11361136
declarator.set(ID_member_initializers, ID_member_initializers);
11371137

1138-
check_member_initializers(
1139-
type.add(ID_bases),
1140-
type.components(),
1141-
declarator.member_initializers());
1138+
if(type.id() == ID_union)
1139+
{
1140+
check_member_initializers(
1141+
{}, type.components(), declarator.member_initializers());
1142+
}
1143+
else
1144+
{
1145+
check_member_initializers(
1146+
to_struct_type(type).bases(),
1147+
type.components(),
1148+
declarator.member_initializers());
1149+
}
11421150

11431151
full_member_initialization(
11441152
type,
@@ -1634,15 +1642,12 @@ void cpp_typecheckt::get_bases(
16341642
const struct_typet &type,
16351643
std::set<irep_idt> &set_bases) const
16361644
{
1637-
const irept::subt &bases=type.find(ID_bases).get_sub();
1638-
1639-
forall_irep(it, bases)
1645+
for(const auto &b : type.bases())
16401646
{
1641-
assert(it->id()==ID_base);
1642-
assert(it->get(ID_type) == ID_symbol_type);
1647+
DATA_INVARIANT(b.id() == ID_base, "base class expression expected");
16431648

1644-
const struct_typet &base=
1645-
to_struct_type(lookup(it->find(ID_type).get(ID_identifier)).type);
1649+
const struct_typet &base =
1650+
to_struct_type(lookup(to_symbol_type(b.type()).get_identifier()).type);
16461651

16471652
set_bases.insert(base.get(ID_name));
16481653
get_bases(base, set_bases);
@@ -1656,17 +1661,14 @@ void cpp_typecheckt::get_virtual_bases(
16561661
if(std::find(vbases.begin(), vbases.end(), type.get(ID_name))!=vbases.end())
16571662
return;
16581663

1659-
const irept::subt &bases=type.find(ID_bases).get_sub();
1660-
1661-
forall_irep(it, bases)
1664+
for(const auto &b : type.bases())
16621665
{
1663-
assert(it->id()==ID_base);
1664-
assert(it->get(ID_type) == ID_symbol_type);
1666+
DATA_INVARIANT(b.id() == ID_base, "base class expression expected");
16651667

1666-
const struct_typet &base=
1667-
to_struct_type(lookup(it->find(ID_type).get(ID_identifier)).type);
1668+
const struct_typet &base =
1669+
to_struct_type(lookup(to_symbol_type(b.type()).get_identifier()).type);
16681670

1669-
if(it->get_bool(ID_virtual))
1671+
if(b.get_bool(ID_virtual))
16701672
vbases.push_back(base.get(ID_name));
16711673

16721674
get_virtual_bases(base, vbases);

src/cpp/cpp_typecheck_constructor.cpp

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,11 @@ void cpp_typecheckt::default_cpctor(
232232
exprt &block=declarator.value();
233233

234234
// First, we need to call the parent copy constructors
235-
const irept &bases=symbol.type.find(ID_bases);
236-
forall_irep(parent_it, bases.get_sub())
235+
for(const auto &b : to_struct_type(symbol.type).bases())
237236
{
238-
assert(parent_it->id()==ID_base);
239-
assert(parent_it->get(ID_type) == ID_symbol_type);
237+
DATA_INVARIANT(b.id() == ID_base, "base class expression expected");
240238

241-
const symbolt &parsymb=
242-
lookup(parent_it->find(ID_type).get(ID_identifier));
239+
const symbolt &parsymb = lookup(to_symbol_type(b.type()).get_identifier());
243240

244241
if(cpp_is_pod(parsymb.type))
245242
copy_parent(source_location, parsymb.base_name, param_identifier, block);
@@ -404,15 +401,11 @@ void cpp_typecheckt::default_assignop_value(
404401
std::string arg_name("ref");
405402

406403
// First, we copy the parents
407-
const irept &bases=symbol.type.find(ID_bases);
408-
409-
forall_irep(parent_it, bases.get_sub())
404+
for(const auto &b : to_struct_type(symbol.type).bases())
410405
{
411-
assert(parent_it->id()==ID_base);
412-
assert(parent_it->get(ID_type) == ID_symbol_type);
406+
DATA_INVARIANT(b.id() == ID_base, "base class expression expected");
413407

414-
const symbolt &symb=
415-
lookup(parent_it->find(ID_type).get(ID_identifier));
408+
const symbolt &symb = lookup(to_symbol_type(b.type()).get_identifier());
416409

417410
copy_parent(source_location, symb.base_name, arg_name, block);
418411
}
@@ -472,7 +465,7 @@ void cpp_typecheckt::default_assignop_value(
472465
/// \return If an invalid initializer is found, then the method outputs an error
473466
/// message and throws a 0 exception.
474467
void cpp_typecheckt::check_member_initializers(
475-
const irept &bases,
468+
const struct_typet::basest &bases,
476469
const struct_typet::componentst &components,
477470
const irept &initializers)
478471
{
@@ -496,12 +489,11 @@ void cpp_typecheckt::check_member_initializers(
496489

497490
// check for a direct parent
498491
bool ok=false;
499-
forall_irep(parent_it, bases.get_sub())
492+
for(const auto &b : bases)
500493
{
501-
assert(parent_it->get(ID_type) == ID_symbol_type);
502-
503-
if(member_type.get(ID_identifier)
504-
==parent_it->find(ID_type).get(ID_identifier))
494+
if(
495+
member_type.get(ID_identifier) ==
496+
to_symbol_type(b.type()).get_identifier())
505497
{
506498
ok=true;
507499
break;
@@ -546,10 +538,9 @@ void cpp_typecheckt::check_member_initializers(
546538
break;
547539

548540
// check for a direct parent
549-
forall_irep(parent_it, bases.get_sub())
541+
for(const auto &b : bases)
550542
{
551-
assert(parent_it->get(ID_type) == ID_symbol_type);
552-
if(symb.name==parent_it->find(ID_type).get(ID_identifier))
543+
if(symb.name == to_symbol_type(b.type()).get_identifier())
553544
{
554545
ok=true;
555546
break;
@@ -568,12 +559,11 @@ void cpp_typecheckt::check_member_initializers(
568559
typecheck_type(member_type);
569560

570561
// check for a direct parent
571-
forall_irep(parent_it, bases.get_sub())
562+
for(const auto &b : bases)
572563
{
573-
assert(parent_it->get(ID_type) == ID_symbol_type);
574-
575-
if(member_type.get(ID_identifier)==
576-
parent_it->find(ID_type).get(ID_identifier))
564+
if(
565+
member_type.get(ID_identifier) ==
566+
to_symbol_type(b.type()).get_identifier())
577567
{
578568
ok=true;
579569
break;
@@ -646,16 +636,13 @@ void cpp_typecheckt::full_member_initialization(
646636
final_initializers.move_to_sub(cond);
647637
}
648638

649-
const irept &bases=struct_union_type.find(ID_bases);
650-
651639
// Subsequently, we need to call the non-POD parent constructors
652-
forall_irep(parent_it, bases.get_sub())
640+
for(const auto &b : to_struct_type(struct_union_type).bases())
653641
{
654-
assert(parent_it->id()==ID_base);
655-
assert(parent_it->get(ID_type) == ID_symbol_type);
642+
DATA_INVARIANT(b.id() == ID_base, "base class expression expected");
656643

657-
const symbolt &ctorsymb=
658-
lookup(parent_it->find(ID_type).get(ID_identifier));
644+
const symbolt &ctorsymb =
645+
lookup(to_symbol_type(b.type()).get_identifier());
659646

660647
if(cpp_is_pod(ctorsymb.type))
661648
continue;
@@ -705,8 +692,9 @@ void cpp_typecheckt::full_member_initialization(
705692
if(member_type.id() != ID_symbol_type)
706693
break;
707694

708-
if(parent_it->find(ID_type).get(ID_identifier)==
709-
member_type.get(ID_identifier))
695+
if(
696+
to_symbol_type(b.type()).get_identifier() ==
697+
to_symbol_type(member_type).get_identifier())
710698
{
711699
final_initializers.move_to_sub(initializer);
712700
found=true;
@@ -724,7 +712,7 @@ void cpp_typecheckt::full_member_initialization(
724712
final_initializers.move_to_sub(mem_init);
725713
}
726714

727-
if(parent_it->get_bool(ID_virtual))
715+
if(b.get_bool(ID_virtual))
728716
{
729717
// TODO(tautschnig): this code doesn't seem to make much sense as the
730718
// ifthenelse only gets to have two operands (instead of three)

src/cpp/cpp_typecheck_destructor.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,18 @@ codet cpp_typecheckt::dtor(const symbolt &symbol)
130130
block.move_to_operands(dtor_code.value());
131131
}
132132

133-
const irept::subt &bases=symbol.type.find(ID_bases).get_sub();
133+
if(symbol.type.id() == ID_union)
134+
return block;
135+
136+
const auto &bases = to_struct_type(symbol.type).bases();
134137

135138
// call the base destructors in the reverse order
136-
for(irept::subt::const_reverse_iterator
137-
bit=bases.rbegin();
138-
bit!=bases.rend();
139+
for(class_typet::basest::const_reverse_iterator bit = bases.rbegin();
140+
bit != bases.rend();
139141
bit++)
140142
{
141-
assert(bit->id()==ID_base);
142-
assert(bit->find(ID_type).id() == ID_symbol_type);
143-
const symbolt &psymb = lookup(bit->find(ID_type).get(ID_identifier));
143+
DATA_INVARIANT(bit->id() == ID_base, "base class expression expected");
144+
const symbolt &psymb = lookup(to_symbol_type(bit->type()).get_identifier());
144145

145146
symbol_exprt this_ptr(ID_this, pointer_type(symbol.type));
146147
dereference_exprt object(this_ptr, psymb.type);

src/cpp/cpp_typecheck_expr.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ bool cpp_typecheckt::find_parent(
3333
const irep_idt &base_name,
3434
irep_idt &identifier)
3535
{
36-
forall_irep(bit, symb.type.find(ID_bases).get_sub())
36+
for(const auto &b : to_struct_type(symb.type).bases())
3737
{
38-
if(lookup(bit->find(ID_type).get(ID_identifier)).base_name == base_name)
38+
const irep_idt &id = to_symbol_type(b.type()).get_identifier();
39+
if(lookup(id).base_name == base_name)
3940
{
40-
identifier=bit->find(ID_type).get(ID_identifier);
41+
identifier = id;
4142
return true;
4243
}
4344
}

src/goto-programs/class_hierarchy.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ void class_hierarchy_grapht::populate(const symbol_tablet &symbol_table)
4343
{
4444
if(symbol_pair.second.is_type && symbol_pair.second.type.id() == ID_struct)
4545
{
46-
const class_typet &class_type = to_class_type(symbol_pair.second.type);
46+
const struct_typet &struct_type = to_struct_type(symbol_pair.second.type);
4747

48-
for(const auto &base : class_type.bases())
48+
for(const auto &base : struct_type.bases())
4949
{
5050
const irep_idt &parent = to_symbol_type(base.type()).get_identifier();
5151
if(!parent.empty())
@@ -157,11 +157,9 @@ void class_hierarchyt::operator()(const symbol_tablet &symbol_table)
157157
class_map[symbol_pair.first].is_abstract =
158158
struct_type.get_bool(ID_abstract);
159159

160-
const irept::subt &bases = struct_type.find(ID_bases).get_sub();
161-
162-
for(const auto &base : bases)
160+
for(const auto &base : struct_type.bases())
163161
{
164-
irep_idt parent = base.find(ID_type).get(ID_identifier);
162+
const irep_idt &parent = to_symbol_type(base.type()).get_identifier();
165163
if(parent.empty())
166164
continue;
167165

0 commit comments

Comments
 (0)