Skip to content

C++ front-end: Use ranged for to iterate over components and bases #3001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/cpp/cpp_constructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,8 @@ optionalt<codet> cpp_typecheckt::cpp_constructor(

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

Expand Down
64 changes: 30 additions & 34 deletions src/cpp/cpp_typecheck_constructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,23 +455,21 @@ void cpp_typecheckt::default_assignop_value(
}

// Then, we copy the members
const irept &components=symbol.type.find(ID_components);

forall_irep(mem_it, components.get_sub())
for(const auto &c : to_struct_type(symbol.type).components())
{
if(mem_it->get_bool(ID_from_base) ||
mem_it->get_bool(ID_is_type) ||
mem_it->get_bool(ID_is_static) ||
mem_it->get_bool("is_vtptr") ||
mem_it->get(ID_type)==ID_code)
if(
c.get_bool(ID_from_base) || c.get_bool(ID_is_type) ||
c.get_bool(ID_is_static) || c.get_bool("is_vtptr") ||
c.get(ID_type) == ID_code)
{
continue;
}

irep_idt mem_name=mem_it->get(ID_base_name);
const irep_idt &mem_name = c.get(ID_base_name);

if(mem_it->get(ID_type)==ID_array)
if(c.get(ID_type) == ID_array)
{
const exprt &size_expr=
to_array_type((typet&)mem_it->find(ID_type)).size();
const exprt &size_expr = to_array_type((typet &)c.find(ID_type)).size();

if(size_expr.id()==ID_infinity)
{
Expand Down Expand Up @@ -811,48 +809,48 @@ void cpp_typecheckt::full_member_initialization(
}

// Then, we add the member initializers
for(struct_typet::componentst::const_iterator mem_it =
components.begin(); mem_it!=components.end(); mem_it++)
for(const auto &c : components)
{
// Take care of virtual tables
if(mem_it->get_bool("is_vtptr"))
if(c.get_bool("is_vtptr"))
{
exprt name(ID_name);
name.set(ID_identifier, mem_it->get(ID_base_name));
name.add_source_location()=mem_it->source_location();
name.set(ID_identifier, c.get(ID_base_name));
name.add_source_location() = c.source_location();

cpp_namet cppname;
cppname.move_to_sub(name);

const symbolt &virtual_table_symbol_type =
lookup(mem_it->type().subtype().get(ID_identifier));
lookup(c.type().subtype().get(ID_identifier));

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

exprt var=virtual_table_symbol_var.symbol_expr();
address_of_exprt address(var);
assert(address.type()==mem_it->type());
assert(address.type() == c.type());

already_typechecked(address);

exprt ptrmember(ID_ptrmember);
ptrmember.set(ID_component_name, mem_it->get(ID_name));
ptrmember.set(ID_component_name, c.get(ID_name));
ptrmember.operands().push_back(exprt("cpp-this"));

code_assignt assign(ptrmember, address);
final_initializers.move_to_sub(assign);
continue;
}

if( mem_it->get_bool(ID_from_base)
|| mem_it->type().id()==ID_code
|| mem_it->get_bool(ID_is_type)
|| mem_it->get_bool(ID_is_static))
continue;
if(
c.get_bool(ID_from_base) || c.type().id() == ID_code ||
c.get_bool(ID_is_type) || c.get_bool(ID_is_static))
{
continue;
}

irep_idt mem_name=mem_it->get(ID_base_name);
const irep_idt &mem_name = c.get(ID_base_name);

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

// If the data member is a reference, it must be explicitly
// initialized
if(!found &&
mem_it->find(ID_type).id()==ID_pointer &&
mem_it->find(ID_type).get_bool(ID_C_reference))
if(
!found && c.find(ID_type).id() == ID_pointer &&
c.find(ID_type).get_bool(ID_C_reference))
{
error().source_location=mem_it->source_location();
error().source_location = c.source_location();
error() << "reference must be explicitly initialized" << eom;
throw 0;
}

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

for(std::size_t i=0; i < components.size(); i++)
for(const auto &component : components)
{
const struct_typet::componentt &component=components[i];

if(component.get(ID_base_name)!="operator=")
continue;

Expand Down
16 changes: 2 additions & 14 deletions src/cpp/cpp_typecheck_conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,17 +911,10 @@ bool cpp_typecheckt::user_defined_conversion_sequence(
if(from.id()==ID_struct)
from_struct=to_struct_type(from);

struct_typet to_struct=to_struct_type(to);

bool found=false;

for(struct_typet::componentst::const_iterator
it=to_struct.components().begin();
it != to_struct.components().end();
it++)
for(const auto &component : to_struct_type(to).components())
{
const irept &component=*it;

if(component.get_bool(ID_from_base))
continue;

Expand Down Expand Up @@ -1062,14 +1055,9 @@ bool cpp_typecheckt::user_defined_conversion_sequence(
// conversion operators
if(from.id()==ID_struct)
{
struct_typet from_struct=to_struct_type(from);

bool found=false;
for(struct_typet::componentst::const_iterator
it=from_struct.components().begin();
it != from_struct.components().end(); it++)
for(const auto &component : to_struct_type(from).components())
{
const irept &component=*it;
const typet comp_type=static_cast<const typet&>(component.find(ID_type));

if(component.get_bool(ID_from_base))
Expand Down
9 changes: 4 additions & 5 deletions src/cpp/cpp_typecheck_declaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,18 @@ void cpp_typecheckt::convert_anonymous_union(
// do scoping
symbolt union_symbol=
*symbol_table.get_writeable(follow(symbol.type).get(ID_name));
const irept::subt &components=union_symbol.type.add(ID_components).get_sub();

forall_irep(it, components)
for(const auto &c : to_union_type(union_symbol.type).components())
{
if(it->find(ID_type).id()==ID_code)
if(c.type().id() == ID_code)
{
error().source_location=union_symbol.type.source_location();
error() << "anonymous union `" << union_symbol.base_name
<< "' shall not have function members" << eom;
throw 0;
}

const irep_idt &base_name=it->get(ID_base_name);
const irep_idt &base_name = c.get(ID_base_name);

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

cpp_idt &id=cpp_scopes.current_scope().insert(base_name);
id.id_class = cpp_idt::id_classt::SYMBOL;
id.identifier=it->get(ID_name);
id.identifier = c.get(ID_name);
id.class_identifier=union_symbol.name;
id.is_member=true;
}
Expand Down
22 changes: 8 additions & 14 deletions src/cpp/cpp_typecheck_destructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ Author: Daniel Kroening, [email protected]

bool cpp_typecheckt::find_dtor(const symbolt &symbol) const
{
const irept &components=
symbol.type.find(ID_components);

forall_irep(cit, components.get_sub())
for(const auto &c : to_struct_type(symbol.type).components())
{
if(cit->get(ID_base_name)=="~"+id2string(symbol.base_name))
if(c.get(ID_base_name) == "~" + id2string(symbol.base_name))
return true;
}

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

// take care of virtual methods
for(struct_union_typet::componentst::const_iterator
cit=components.begin();
cit!=components.end();
cit++)
for(const auto &c : components)
{
if(cit->get_bool("is_vtptr"))
if(c.get_bool("is_vtptr"))
{
exprt name(ID_name);
name.set(ID_identifier, cit->get(ID_base_name));
name.set(ID_identifier, c.get(ID_base_name));

cpp_namet cppname;
cppname.move_to_sub(name);

const symbolt &virtual_table_symbol_type =
lookup(cit->type().subtype().get(ID_identifier));
lookup(c.type().subtype().get(ID_identifier));

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

exprt var=virtual_table_symbol_var.symbol_expr();
address_of_exprt address(var);
assert(address.type()==cit->type());
assert(address.type() == c.type());

already_typechecked(address);

exprt ptrmember(ID_ptrmember);
ptrmember.set(ID_component_name, cit->get(ID_name));
ptrmember.set(ID_component_name, c.get(ID_name));
ptrmember.operands().push_back(exprt("cpp-this"));

code_assignt assign(ptrmember, address);
Expand Down
29 changes: 8 additions & 21 deletions src/cpp/cpp_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,19 +512,9 @@ bool cpp_typecheckt::operator_is_overloaded(exprt &expr)

if(t0.id()==ID_struct)
{
const struct_typet &struct_type=
to_struct_type(t0);

const struct_typet::componentst &components=
struct_type.components();

for(struct_typet::componentst::const_iterator
it=components.begin();
it!=components.end();
it++)
for(const auto &c : to_struct_type(t0).components())
{
if(!it->get_bool(ID_from_base) &&
it->get(ID_base_name) == op_name)
if(!c.get_bool(ID_from_base) && c.get(ID_base_name) == op_name)
{
found_in_struct=true;
break;
Expand Down Expand Up @@ -2205,18 +2195,15 @@ void cpp_typecheckt::typecheck_side_effect_function_call(
const struct_typet::componentst &components=
to_struct_type(follow(tmp_object_expr.type())).components();

for(struct_typet::componentst::const_iterator
it=components.begin();
it!=components.end();
it++)
for(const auto &c : components)
{
const typet &type=it->type();
const typet &type = c.type();

if(!it->get_bool(ID_from_base) &&
type.id()==ID_code &&
type.find(ID_return_type).id()==ID_destructor)
if(
!c.get_bool(ID_from_base) && type.id() == ID_code &&
type.find(ID_return_type).id() == ID_destructor)
{
add_method_body(&symbol_table.get_writeable_ref(it->get(ID_name)));
add_method_body(&symbol_table.get_writeable_ref(c.get(ID_name)));
break;
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/cpp/cpp_typecheck_initializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,8 @@ void cpp_typecheckt::zero_initializer(

if(final_type.id()==ID_struct)
{
forall_irep(cit, final_type.find(ID_components).get_sub())
for(const auto &component : to_struct_type(final_type).components())
{
const exprt &component=static_cast<const exprt &>(*cit);

if(component.type().id()==ID_code)
continue;

Expand Down Expand Up @@ -249,10 +247,8 @@ void cpp_typecheckt::zero_initializer(

exprt comp=nil_exprt();

forall_irep(it, final_type.find(ID_components).get_sub())
for(const auto &component : to_union_type(final_type).components())
{
const exprt &component=static_cast<const exprt &>(*it);

assert(component.type().is_not_nil());

if(component.type().id()==ID_code)
Expand Down
3 changes: 1 addition & 2 deletions src/cpp/cpp_typecheck_virtual_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ void cpp_typecheckt::do_virtual_table(const symbolt &symbol)

struct_exprt values(symbol_typet(vt_symb_type.name));

for(std::size_t i=0; i < vt_type.components().size(); i++)
for(const auto &compo : vt_type.components())
{
const struct_typet::componentt &compo=vt_type.components()[i];
std::map<irep_idt, exprt>::const_iterator cit2 =
value_map.find(compo.get("base_name"));
assert(cit2!=value_map.end());
Expand Down
6 changes: 2 additions & 4 deletions src/cpp/template_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ void template_mapt::apply(typet &type) const
else if(type.id()==ID_struct ||
type.id()==ID_union)
{
irept::subt &components=type.add(ID_components).get_sub();

Forall_irep(it, components)
for(auto &c : to_struct_union_type(type).components())
{
typet &subtype=static_cast<typet &>(it->add(ID_type));
typet &subtype = static_cast<typet &>(c.add(ID_type));
apply(subtype);
}
}
Expand Down