Skip to content

Commit ca235a9

Browse files
authored
Merge pull request #3027 from tautschnig/bases
Move class_typet::baset to struct_typet
2 parents 02707cb + 1f5e03c commit ca235a9

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
@@ -186,14 +186,11 @@ void cpp_typecheckt::default_cpctor(
186186
exprt &block=declarator.value();
187187

188188
// First, we need to call the parent copy constructors
189-
const irept &bases=symbol.type.find(ID_bases);
190-
forall_irep(parent_it, bases.get_sub())
189+
for(const auto &b : to_struct_type(symbol.type).bases())
191190
{
192-
assert(parent_it->id()==ID_base);
193-
assert(parent_it->get(ID_type) == ID_symbol_type);
191+
DATA_INVARIANT(b.id() == ID_base, "base class expression expected");
194192

195-
const symbolt &parsymb=
196-
lookup(parent_it->find(ID_type).get(ID_identifier));
193+
const symbolt &parsymb = lookup(to_symbol_type(b.type()).get_identifier());
197194

198195
if(cpp_is_pod(parsymb.type))
199196
copy_parent(source_location, parsymb.base_name, param_identifier, block);
@@ -358,15 +355,11 @@ void cpp_typecheckt::default_assignop_value(
358355
std::string arg_name("ref");
359356

360357
// First, we copy the parents
361-
const irept &bases=symbol.type.find(ID_bases);
362-
363-
forall_irep(parent_it, bases.get_sub())
358+
for(const auto &b : to_struct_type(symbol.type).bases())
364359
{
365-
assert(parent_it->id()==ID_base);
366-
assert(parent_it->get(ID_type) == ID_symbol_type);
360+
DATA_INVARIANT(b.id() == ID_base, "base class expression expected");
367361

368-
const symbolt &symb=
369-
lookup(parent_it->find(ID_type).get(ID_identifier));
362+
const symbolt &symb = lookup(to_symbol_type(b.type()).get_identifier());
370363

371364
copy_parent(source_location, symb.base_name, arg_name, block);
372365
}
@@ -426,7 +419,7 @@ void cpp_typecheckt::default_assignop_value(
426419
/// \return If an invalid initializer is found, then the method outputs an error
427420
/// message and throws a 0 exception.
428421
void cpp_typecheckt::check_member_initializers(
429-
const irept &bases,
422+
const struct_typet::basest &bases,
430423
const struct_typet::componentst &components,
431424
const irept &initializers)
432425
{
@@ -450,12 +443,11 @@ void cpp_typecheckt::check_member_initializers(
450443

451444
// check for a direct parent
452445
bool ok=false;
453-
forall_irep(parent_it, bases.get_sub())
446+
for(const auto &b : bases)
454447
{
455-
assert(parent_it->get(ID_type) == ID_symbol_type);
456-
457-
if(member_type.get(ID_identifier)
458-
==parent_it->find(ID_type).get(ID_identifier))
448+
if(
449+
to_symbol_type(member_type).get_identifier() ==
450+
to_symbol_type(b.type()).get_identifier())
459451
{
460452
ok=true;
461453
break;
@@ -500,10 +492,9 @@ void cpp_typecheckt::check_member_initializers(
500492
break;
501493

502494
// check for a direct parent
503-
forall_irep(parent_it, bases.get_sub())
495+
for(const auto &b : bases)
504496
{
505-
assert(parent_it->get(ID_type) == ID_symbol_type);
506-
if(symb.name==parent_it->find(ID_type).get(ID_identifier))
497+
if(symb.name == to_symbol_type(b.type()).get_identifier())
507498
{
508499
ok=true;
509500
break;
@@ -522,12 +513,11 @@ void cpp_typecheckt::check_member_initializers(
522513
typecheck_type(member_type);
523514

524515
// check for a direct parent
525-
forall_irep(parent_it, bases.get_sub())
516+
for(const auto &b : bases)
526517
{
527-
assert(parent_it->get(ID_type) == ID_symbol_type);
528-
529-
if(member_type.get(ID_identifier)==
530-
parent_it->find(ID_type).get(ID_identifier))
518+
if(
519+
member_type.get(ID_identifier) ==
520+
to_symbol_type(b.type()).get_identifier())
531521
{
532522
ok=true;
533523
break;
@@ -600,16 +590,13 @@ void cpp_typecheckt::full_member_initialization(
600590
final_initializers.move_to_sub(cond);
601591
}
602592

603-
const irept &bases=struct_union_type.find(ID_bases);
604-
605593
// Subsequently, we need to call the non-POD parent constructors
606-
forall_irep(parent_it, bases.get_sub())
594+
for(const auto &b : to_struct_type(struct_union_type).bases())
607595
{
608-
assert(parent_it->id()==ID_base);
609-
assert(parent_it->get(ID_type) == ID_symbol_type);
596+
DATA_INVARIANT(b.id() == ID_base, "base class expression expected");
610597

611-
const symbolt &ctorsymb=
612-
lookup(parent_it->find(ID_type).get(ID_identifier));
598+
const symbolt &ctorsymb =
599+
lookup(to_symbol_type(b.type()).get_identifier());
613600

614601
if(cpp_is_pod(ctorsymb.type))
615602
continue;
@@ -659,8 +646,9 @@ void cpp_typecheckt::full_member_initialization(
659646
if(member_type.id() != ID_symbol_type)
660647
break;
661648

662-
if(parent_it->find(ID_type).get(ID_identifier)==
663-
member_type.get(ID_identifier))
649+
if(
650+
to_symbol_type(b.type()).get_identifier() ==
651+
to_symbol_type(member_type).get_identifier())
664652
{
665653
final_initializers.move_to_sub(initializer);
666654
found=true;
@@ -678,7 +666,7 @@ void cpp_typecheckt::full_member_initialization(
678666
final_initializers.move_to_sub(mem_init);
679667
}
680668

681-
if(parent_it->get_bool(ID_virtual))
669+
if(b.get_bool(ID_virtual))
682670
{
683671
// TODO(tautschnig): this code doesn't seem to make much sense as the
684672
// 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)