Skip to content

Commit 245c7b5

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 0c55a08 commit 245c7b5

10 files changed

+132
-136
lines changed

src/cpp/cpp_declarator_converter.cpp

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

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

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

162161
cpp_typecheck.check_member_initializers(
163-
type.find(ID_bases),
164-
to_struct_type(type).components(),
165-
declarator.member_initializers());
162+
type.bases(), type.components(), declarator.member_initializers());
166163

167164
cpp_typecheck.full_member_initialization(
168-
to_struct_type(type),
169-
declarator.member_initializers());
165+
type, declarator.member_initializers());
170166
}
171167

172168
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
@@ -1136,10 +1136,18 @@ void cpp_typecheckt::typecheck_compound_body(symbolt &symbol)
11361136
if(declarator.find(ID_member_initializers).is_nil())
11371137
declarator.set(ID_member_initializers, ID_member_initializers);
11381138

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

11441152
full_member_initialization(
11451153
type,
@@ -1635,15 +1643,12 @@ void cpp_typecheckt::get_bases(
16351643
const struct_typet &type,
16361644
std::set<irep_idt> &set_bases) const
16371645
{
1638-
const irept::subt &bases=type.find(ID_bases).get_sub();
1639-
1640-
forall_irep(it, bases)
1646+
for(const auto &b : type.bases())
16411647
{
1642-
assert(it->id()==ID_base);
1643-
assert(it->get(ID_type) == ID_symbol_type);
1648+
DATA_INVARIANT(b.id() == ID_base, "base class expression expected");
16441649

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

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

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

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

1670-
if(it->get_bool(ID_virtual))
1672+
if(b.get_bool(ID_virtual))
16711673
vbases.push_back(base.get(ID_name));
16721674

16731675
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
@@ -248,14 +248,11 @@ void cpp_typecheckt::default_cpctor(
248248
exprt &block=declarator.value();
249249

250250
// First, we need to call the parent copy constructors
251-
const irept &bases=symbol.type.find(ID_bases);
252-
forall_irep(parent_it, bases.get_sub())
251+
for(const auto &b : to_struct_type(symbol.type).bases())
253252
{
254-
assert(parent_it->id()==ID_base);
255-
assert(parent_it->get(ID_type) == ID_symbol_type);
253+
DATA_INVARIANT(b.id() == ID_base, "base class expression expected");
256254

257-
const symbolt &parsymb=
258-
lookup(parent_it->find(ID_type).get(ID_identifier));
255+
const symbolt &parsymb = lookup(to_symbol_type(b.type()).get_identifier());
259256

260257
if(cpp_is_pod(parsymb.type))
261258
copy_parent(source_location, parsymb.base_name, param_identifier, block);
@@ -443,15 +440,11 @@ void cpp_typecheckt::default_assignop_value(
443440
std::string arg_name("ref");
444441

445442
// First, we copy the parents
446-
const irept &bases=symbol.type.find(ID_bases);
447-
448-
forall_irep(parent_it, bases.get_sub())
443+
for(const auto &b : to_struct_type(symbol.type).bases())
449444
{
450-
assert(parent_it->id()==ID_base);
451-
assert(parent_it->get(ID_type) == ID_symbol_type);
445+
DATA_INVARIANT(b.id() == ID_base, "base class expression expected");
452446

453-
const symbolt &symb=
454-
lookup(parent_it->find(ID_type).get(ID_identifier));
447+
const symbolt &symb = lookup(to_symbol_type(b.type()).get_identifier());
455448

456449
copy_parent(source_location, symb.base_name, arg_name, block);
457450
}
@@ -511,7 +504,7 @@ void cpp_typecheckt::default_assignop_value(
511504
/// \return If an invalid initializer is found, then the method outputs an error
512505
/// message and throws a 0 exception.
513506
void cpp_typecheckt::check_member_initializers(
514-
const irept &bases,
507+
const struct_typet::basest &bases,
515508
const struct_typet::componentst &components,
516509
const irept &initializers)
517510
{
@@ -537,12 +530,11 @@ void cpp_typecheckt::check_member_initializers(
537530

538531
// check for a direct parent
539532
bool ok=false;
540-
forall_irep(parent_it, bases.get_sub())
533+
for(const auto &b : bases)
541534
{
542-
assert(parent_it->get(ID_type) == ID_symbol_type);
543-
544-
if(member_type.get(ID_identifier)
545-
==parent_it->find(ID_type).get(ID_identifier))
535+
if(
536+
member_type.get(ID_identifier) ==
537+
to_symbol_type(b.type()).get_identifier())
546538
{
547539
ok=true;
548540
break;
@@ -587,10 +579,9 @@ void cpp_typecheckt::check_member_initializers(
587579
break;
588580

589581
// check for a direct parent
590-
forall_irep(parent_it, bases.get_sub())
582+
for(const auto &b : bases)
591583
{
592-
assert(parent_it->get(ID_type) == ID_symbol_type);
593-
if(symb.name==parent_it->find(ID_type).get(ID_identifier))
584+
if(symb.name == to_symbol_type(b.type()).get_identifier())
594585
{
595586
ok=true;
596587
break;
@@ -609,12 +600,11 @@ void cpp_typecheckt::check_member_initializers(
609600
typecheck_type(member_type);
610601

611602
// check for a direct parent
612-
forall_irep(parent_it, bases.get_sub())
603+
for(const auto &b : bases)
613604
{
614-
assert(parent_it->get(ID_type) == ID_symbol_type);
615-
616-
if(member_type.get(ID_identifier)==
617-
parent_it->find(ID_type).get(ID_identifier))
605+
if(
606+
member_type.get(ID_identifier) ==
607+
to_symbol_type(b.type()).get_identifier())
618608
{
619609
ok=true;
620610
break;
@@ -699,16 +689,13 @@ void cpp_typecheckt::full_member_initialization(
699689
final_initializers.move_to_sub(cond);
700690
}
701691

702-
const irept &bases=struct_union_type.find(ID_bases);
703-
704692
// Subsequently, we need to call the non-POD parent constructors
705-
forall_irep(parent_it, bases.get_sub())
693+
for(const auto &b : to_struct_type(struct_union_type).bases())
706694
{
707-
assert(parent_it->id()==ID_base);
708-
assert(parent_it->get(ID_type) == ID_symbol_type);
695+
DATA_INVARIANT(b.id() == ID_base, "base class expression expected");
709696

710-
const symbolt &ctorsymb=
711-
lookup(parent_it->find(ID_type).get(ID_identifier));
697+
const symbolt &ctorsymb =
698+
lookup(to_symbol_type(b.type()).get_identifier());
712699

713700
if(cpp_is_pod(ctorsymb.type))
714701
continue;
@@ -760,8 +747,9 @@ void cpp_typecheckt::full_member_initialization(
760747
if(member_type.id() != ID_symbol_type)
761748
break;
762749

763-
if(parent_it->find(ID_type).get(ID_identifier)==
764-
member_type.get(ID_identifier))
750+
if(
751+
to_symbol_type(b.type()).get_identifier() ==
752+
to_symbol_type(member_type).get_identifier())
765753
{
766754
final_initializers.move_to_sub(initializer);
767755
found=true;
@@ -783,7 +771,7 @@ void cpp_typecheckt::full_member_initialization(
783771
final_initializers.move_to_sub(mem_init);
784772
}
785773

786-
if(parent_it->get_bool(ID_virtual))
774+
if(b.get_bool(ID_virtual))
787775
{
788776
// TODO(tautschnig): this code doesn't seem to make much sense as the
789777
// ifthenelse only gets to have two operands (instead of three)

src/cpp/cpp_typecheck_destructor.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,14 @@ codet cpp_typecheckt::dtor(const symbolt &symbol)
145145
block.move_to_operands(dtor_code.value());
146146
}
147147

148-
const irept::subt &bases=symbol.type.find(ID_bases).get_sub();
148+
if(symbol.type.id() == ID_union)
149+
return block;
150+
151+
const auto &bases = to_struct_type(symbol.type).bases();
149152

150153
// call the base destructors in the reverse order
151-
for(irept::subt::const_reverse_iterator
152-
bit=bases.rbegin();
153-
bit!=bases.rend();
154+
for(class_typet::basest::const_reverse_iterator bit = bases.rbegin();
155+
bit != bases.rend();
154156
bit++)
155157
{
156158
assert(bit->id()==ID_base);

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)