Skip to content

Commit c89d376

Browse files
authored
Merge pull request #3001 from tautschnig/components-ranged-for
C++ front-end: Use ranged for to iterate over components and bases
2 parents f611794 + 716279f commit c89d376

9 files changed

+58
-102
lines changed

src/cpp/cpp_constructor.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,8 @@ optionalt<codet> cpp_typecheckt::cpp_constructor(
181181

182182
// set most-derived bits
183183
code_blockt block;
184-
for(std::size_t i=0; i < struct_type.components().size(); i++)
184+
for(const auto &component : struct_type.components())
185185
{
186-
const irept &component=struct_type.components()[i];
187186
if(component.get(ID_base_name)!="@most_derived")
188187
continue;
189188

src/cpp/cpp_typecheck_constructor.cpp

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -455,23 +455,21 @@ void cpp_typecheckt::default_assignop_value(
455455
}
456456

457457
// Then, we copy the members
458-
const irept &components=symbol.type.find(ID_components);
459-
460-
forall_irep(mem_it, components.get_sub())
458+
for(const auto &c : to_struct_type(symbol.type).components())
461459
{
462-
if(mem_it->get_bool(ID_from_base) ||
463-
mem_it->get_bool(ID_is_type) ||
464-
mem_it->get_bool(ID_is_static) ||
465-
mem_it->get_bool("is_vtptr") ||
466-
mem_it->get(ID_type)==ID_code)
460+
if(
461+
c.get_bool(ID_from_base) || c.get_bool(ID_is_type) ||
462+
c.get_bool(ID_is_static) || c.get_bool("is_vtptr") ||
463+
c.get(ID_type) == ID_code)
464+
{
467465
continue;
466+
}
468467

469-
irep_idt mem_name=mem_it->get(ID_base_name);
468+
const irep_idt &mem_name = c.get(ID_base_name);
470469

471-
if(mem_it->get(ID_type)==ID_array)
470+
if(c.get(ID_type) == ID_array)
472471
{
473-
const exprt &size_expr=
474-
to_array_type((typet&)mem_it->find(ID_type)).size();
472+
const exprt &size_expr = to_array_type((typet &)c.find(ID_type)).size();
475473

476474
if(size_expr.id()==ID_infinity)
477475
{
@@ -811,48 +809,48 @@ void cpp_typecheckt::full_member_initialization(
811809
}
812810

813811
// Then, we add the member initializers
814-
for(struct_typet::componentst::const_iterator mem_it =
815-
components.begin(); mem_it!=components.end(); mem_it++)
812+
for(const auto &c : components)
816813
{
817814
// Take care of virtual tables
818-
if(mem_it->get_bool("is_vtptr"))
815+
if(c.get_bool("is_vtptr"))
819816
{
820817
exprt name(ID_name);
821-
name.set(ID_identifier, mem_it->get(ID_base_name));
822-
name.add_source_location()=mem_it->source_location();
818+
name.set(ID_identifier, c.get(ID_base_name));
819+
name.add_source_location() = c.source_location();
823820

824821
cpp_namet cppname;
825822
cppname.move_to_sub(name);
826823

827824
const symbolt &virtual_table_symbol_type =
828-
lookup(mem_it->type().subtype().get(ID_identifier));
825+
lookup(c.type().subtype().get(ID_identifier));
829826

830827
const symbolt &virtual_table_symbol_var =
831828
lookup(id2string(virtual_table_symbol_type.name) + "@" +
832829
id2string(struct_union_type.get(ID_name)));
833830

834831
exprt var=virtual_table_symbol_var.symbol_expr();
835832
address_of_exprt address(var);
836-
assert(address.type()==mem_it->type());
833+
assert(address.type() == c.type());
837834

838835
already_typechecked(address);
839836

840837
exprt ptrmember(ID_ptrmember);
841-
ptrmember.set(ID_component_name, mem_it->get(ID_name));
838+
ptrmember.set(ID_component_name, c.get(ID_name));
842839
ptrmember.operands().push_back(exprt("cpp-this"));
843840

844841
code_assignt assign(ptrmember, address);
845842
final_initializers.move_to_sub(assign);
846843
continue;
847844
}
848845

849-
if( mem_it->get_bool(ID_from_base)
850-
|| mem_it->type().id()==ID_code
851-
|| mem_it->get_bool(ID_is_type)
852-
|| mem_it->get_bool(ID_is_static))
853-
continue;
846+
if(
847+
c.get_bool(ID_from_base) || c.type().id() == ID_code ||
848+
c.get_bool(ID_is_type) || c.get_bool(ID_is_static))
849+
{
850+
continue;
851+
}
854852

855-
irep_idt mem_name=mem_it->get(ID_base_name);
853+
const irep_idt &mem_name = c.get(ID_base_name);
856854

857855
// Check if the initialization list of the constructor
858856
// explicitly initializes the data member
@@ -880,18 +878,18 @@ void cpp_typecheckt::full_member_initialization(
880878

881879
// If the data member is a reference, it must be explicitly
882880
// initialized
883-
if(!found &&
884-
mem_it->find(ID_type).id()==ID_pointer &&
885-
mem_it->find(ID_type).get_bool(ID_C_reference))
881+
if(
882+
!found && c.find(ID_type).id() == ID_pointer &&
883+
c.find(ID_type).get_bool(ID_C_reference))
886884
{
887-
error().source_location=mem_it->source_location();
885+
error().source_location = c.source_location();
888886
error() << "reference must be explicitly initialized" << eom;
889887
throw 0;
890888
}
891889

892890
// If the data member is not POD and is not explicitly initialized,
893891
// then its default constructor is called.
894-
if(!found && !cpp_is_pod((const typet &)(mem_it->find(ID_type))))
892+
if(!found && !cpp_is_pod((const typet &)(c.find(ID_type))))
895893
{
896894
irept name(ID_name);
897895
name.set(ID_identifier, mem_name);
@@ -972,10 +970,8 @@ bool cpp_typecheckt::find_assignop(const symbolt &symbol) const
972970
const struct_typet &struct_type=to_struct_type(symbol.type);
973971
const struct_typet::componentst &components=struct_type.components();
974972

975-
for(std::size_t i=0; i < components.size(); i++)
973+
for(const auto &component : components)
976974
{
977-
const struct_typet::componentt &component=components[i];
978-
979975
if(component.get(ID_base_name)!="operator=")
980976
continue;
981977

src/cpp/cpp_typecheck_conversions.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -911,17 +911,10 @@ bool cpp_typecheckt::user_defined_conversion_sequence(
911911
if(from.id()==ID_struct)
912912
from_struct=to_struct_type(from);
913913

914-
struct_typet to_struct=to_struct_type(to);
915-
916914
bool found=false;
917915

918-
for(struct_typet::componentst::const_iterator
919-
it=to_struct.components().begin();
920-
it != to_struct.components().end();
921-
it++)
916+
for(const auto &component : to_struct_type(to).components())
922917
{
923-
const irept &component=*it;
924-
925918
if(component.get_bool(ID_from_base))
926919
continue;
927920

@@ -1062,14 +1055,9 @@ bool cpp_typecheckt::user_defined_conversion_sequence(
10621055
// conversion operators
10631056
if(from.id()==ID_struct)
10641057
{
1065-
struct_typet from_struct=to_struct_type(from);
1066-
10671058
bool found=false;
1068-
for(struct_typet::componentst::const_iterator
1069-
it=from_struct.components().begin();
1070-
it != from_struct.components().end(); it++)
1059+
for(const auto &component : to_struct_type(from).components())
10711060
{
1072-
const irept &component=*it;
10731061
const typet comp_type=static_cast<const typet&>(component.find(ID_type));
10741062

10751063
if(component.get_bool(ID_from_base))

src/cpp/cpp_typecheck_declaration.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,18 @@ void cpp_typecheckt::convert_anonymous_union(
6969
// do scoping
7070
symbolt union_symbol=
7171
*symbol_table.get_writeable(follow(symbol.type).get(ID_name));
72-
const irept::subt &components=union_symbol.type.add(ID_components).get_sub();
7372

74-
forall_irep(it, components)
73+
for(const auto &c : to_union_type(union_symbol.type).components())
7574
{
76-
if(it->find(ID_type).id()==ID_code)
75+
if(c.type().id() == ID_code)
7776
{
7877
error().source_location=union_symbol.type.source_location();
7978
error() << "anonymous union `" << union_symbol.base_name
8079
<< "' shall not have function members" << eom;
8180
throw 0;
8281
}
8382

84-
const irep_idt &base_name=it->get(ID_base_name);
83+
const irep_idt &base_name = c.get(ID_base_name);
8584

8685
if(cpp_scopes.current_scope().contains(base_name))
8786
{
@@ -93,7 +92,7 @@ void cpp_typecheckt::convert_anonymous_union(
9392

9493
cpp_idt &id=cpp_scopes.current_scope().insert(base_name);
9594
id.id_class = cpp_idt::id_classt::SYMBOL;
96-
id.identifier=it->get(ID_name);
95+
id.identifier = c.get(ID_name);
9796
id.class_identifier=union_symbol.name;
9897
id.is_member=true;
9998
}

src/cpp/cpp_typecheck_destructor.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@ Author: Daniel Kroening, [email protected]
1515

1616
bool cpp_typecheckt::find_dtor(const symbolt &symbol) const
1717
{
18-
const irept &components=
19-
symbol.type.find(ID_components);
20-
21-
forall_irep(cit, components.get_sub())
18+
for(const auto &c : to_struct_type(symbol.type).components())
2219
{
23-
if(cit->get(ID_base_name)=="~"+id2string(symbol.base_name))
20+
if(c.get(ID_base_name) == "~" + id2string(symbol.base_name))
2421
return true;
2522
}
2623

@@ -77,34 +74,31 @@ codet cpp_typecheckt::dtor(const symbolt &symbol)
7774
to_struct_union_type(symbol.type).components();
7875

7976
// take care of virtual methods
80-
for(struct_union_typet::componentst::const_iterator
81-
cit=components.begin();
82-
cit!=components.end();
83-
cit++)
77+
for(const auto &c : components)
8478
{
85-
if(cit->get_bool("is_vtptr"))
79+
if(c.get_bool("is_vtptr"))
8680
{
8781
exprt name(ID_name);
88-
name.set(ID_identifier, cit->get(ID_base_name));
82+
name.set(ID_identifier, c.get(ID_base_name));
8983

9084
cpp_namet cppname;
9185
cppname.move_to_sub(name);
9286

9387
const symbolt &virtual_table_symbol_type =
94-
lookup(cit->type().subtype().get(ID_identifier));
88+
lookup(c.type().subtype().get(ID_identifier));
9589

9690
const symbolt &virtual_table_symbol_var = lookup(
9791
id2string(virtual_table_symbol_type.name) + "@" +
9892
id2string(symbol.name));
9993

10094
exprt var=virtual_table_symbol_var.symbol_expr();
10195
address_of_exprt address(var);
102-
assert(address.type()==cit->type());
96+
assert(address.type() == c.type());
10397

10498
already_typechecked(address);
10599

106100
exprt ptrmember(ID_ptrmember);
107-
ptrmember.set(ID_component_name, cit->get(ID_name));
101+
ptrmember.set(ID_component_name, c.get(ID_name));
108102
ptrmember.operands().push_back(exprt("cpp-this"));
109103

110104
code_assignt assign(ptrmember, address);

src/cpp/cpp_typecheck_expr.cpp

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -512,19 +512,9 @@ bool cpp_typecheckt::operator_is_overloaded(exprt &expr)
512512

513513
if(t0.id()==ID_struct)
514514
{
515-
const struct_typet &struct_type=
516-
to_struct_type(t0);
517-
518-
const struct_typet::componentst &components=
519-
struct_type.components();
520-
521-
for(struct_typet::componentst::const_iterator
522-
it=components.begin();
523-
it!=components.end();
524-
it++)
515+
for(const auto &c : to_struct_type(t0).components())
525516
{
526-
if(!it->get_bool(ID_from_base) &&
527-
it->get(ID_base_name) == op_name)
517+
if(!c.get_bool(ID_from_base) && c.get(ID_base_name) == op_name)
528518
{
529519
found_in_struct=true;
530520
break;
@@ -2205,18 +2195,15 @@ void cpp_typecheckt::typecheck_side_effect_function_call(
22052195
const struct_typet::componentst &components=
22062196
to_struct_type(follow(tmp_object_expr.type())).components();
22072197

2208-
for(struct_typet::componentst::const_iterator
2209-
it=components.begin();
2210-
it!=components.end();
2211-
it++)
2198+
for(const auto &c : components)
22122199
{
2213-
const typet &type=it->type();
2200+
const typet &type = c.type();
22142201

2215-
if(!it->get_bool(ID_from_base) &&
2216-
type.id()==ID_code &&
2217-
type.find(ID_return_type).id()==ID_destructor)
2202+
if(
2203+
!c.get_bool(ID_from_base) && type.id() == ID_code &&
2204+
type.find(ID_return_type).id() == ID_destructor)
22182205
{
2219-
add_method_body(&symbol_table.get_writeable_ref(it->get(ID_name)));
2206+
add_method_body(&symbol_table.get_writeable_ref(c.get(ID_name)));
22202207
break;
22212208
}
22222209
}

src/cpp/cpp_typecheck_initializer.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,8 @@ void cpp_typecheckt::zero_initializer(
200200

201201
if(final_type.id()==ID_struct)
202202
{
203-
forall_irep(cit, final_type.find(ID_components).get_sub())
203+
for(const auto &component : to_struct_type(final_type).components())
204204
{
205-
const exprt &component=static_cast<const exprt &>(*cit);
206-
207205
if(component.type().id()==ID_code)
208206
continue;
209207

@@ -249,10 +247,8 @@ void cpp_typecheckt::zero_initializer(
249247

250248
exprt comp=nil_exprt();
251249

252-
forall_irep(it, final_type.find(ID_components).get_sub())
250+
for(const auto &component : to_union_type(final_type).components())
253251
{
254-
const exprt &component=static_cast<const exprt &>(*it);
255-
256252
assert(component.type().is_not_nil());
257253

258254
if(component.type().id()==ID_code)

src/cpp/cpp_typecheck_virtual_table.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ void cpp_typecheckt::do_virtual_table(const symbolt &symbol)
8282

8383
struct_exprt values(symbol_typet(vt_symb_type.name));
8484

85-
for(std::size_t i=0; i < vt_type.components().size(); i++)
85+
for(const auto &compo : vt_type.components())
8686
{
87-
const struct_typet::componentt &compo=vt_type.components()[i];
8887
std::map<irep_idt, exprt>::const_iterator cit2 =
8988
value_map.find(compo.get("base_name"));
9089
assert(cit2!=value_map.end());

src/cpp/template_map.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ void template_mapt::apply(typet &type) const
3131
else if(type.id()==ID_struct ||
3232
type.id()==ID_union)
3333
{
34-
irept::subt &components=type.add(ID_components).get_sub();
35-
36-
Forall_irep(it, components)
34+
for(auto &c : to_struct_union_type(type).components())
3735
{
38-
typet &subtype=static_cast<typet &>(it->add(ID_type));
36+
typet &subtype = static_cast<typet &>(c.add(ID_type));
3937
apply(subtype);
4038
}
4139
}

0 commit comments

Comments
 (0)