Skip to content

C front-end: Replace uses of namespacet::follow #8218

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
Apr 27, 2024
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
23 changes: 7 additions & 16 deletions src/ansi-c/anonymous_member.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ static member_exprt make_member_expr(
result.set(ID_C_lvalue, true);

// todo: should to typedef chains properly
const typet &type=
ns.follow(struct_union.type());

const auto &tag_type = to_struct_or_union_tag_type(struct_union.type());
if(
type.get_bool(ID_C_constant) || struct_union.type().get_bool(ID_C_constant))
ns.follow_tag(tag_type).get_bool(ID_C_constant) ||
struct_union.type().get_bool(ID_C_constant))
{
result.type().set(ID_C_constant, true);
}
Expand All @@ -43,13 +42,9 @@ exprt get_component_rec(
const irep_idt &component_name,
const namespacet &ns)
{
const struct_union_typet &struct_union_type=
to_struct_union_type(ns.follow(struct_union.type()));

const struct_union_typet::componentst &components=
struct_union_type.components();
const auto &tag_type = to_struct_or_union_tag_type(struct_union.type());

for(const auto &comp : components)
for(const auto &comp : ns.follow_tag(tag_type).components())
{
const typet &type = comp.type();

Expand All @@ -76,13 +71,9 @@ bool has_component_rec(
const irep_idt &component_name,
const namespacet &ns)
{
const struct_union_typet &struct_union_type=
to_struct_union_type(ns.follow(type));

const struct_union_typet::componentst &components=
struct_union_type.components();
const auto &tag_type = to_struct_or_union_tag_type(type);

for(const auto &comp : components)
for(const auto &comp : ns.follow_tag(tag_type).components())
{
if(comp.get_name()==component_name)
{
Expand Down
12 changes: 10 additions & 2 deletions src/ansi-c/c_typecast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,17 @@ typet c_typecastt::follow_with_qualifiers(const typet &src_type)
// collect qualifiers
c_qualifierst qualifiers(src_type);

while(result_type.id() == ID_struct_tag || result_type.id() == ID_union_tag)
if(
auto struct_tag_type = type_try_dynamic_cast<struct_tag_typet>(result_type))
{
const typet &followed_type = ns.follow_tag(*struct_tag_type);
result_type = followed_type;
qualifiers += c_qualifierst(followed_type);
}
else if(
auto union_tag_type = type_try_dynamic_cast<union_tag_typet>(result_type))
{
const typet &followed_type = ns.follow(result_type);
const typet &followed_type = ns.follow_tag(*union_tag_type);
result_type = followed_type;
qualifiers += c_qualifierst(followed_type);
}
Expand Down
29 changes: 14 additions & 15 deletions src/ansi-c/c_typecheck_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
{
bool is_function=symbol.type.id()==ID_code;

const typet &final_type=follow(symbol.type);

// set a few flags
symbol.is_lvalue=!symbol.is_type && !symbol.is_macro;

Expand Down Expand Up @@ -91,15 +89,15 @@
}

// set the pretty name
if(symbol.is_type && final_type.id() == ID_struct)
if(symbol.is_type && symbol.type.id() == ID_struct)
{
symbol.pretty_name="struct "+id2string(symbol.base_name);
}
else if(symbol.is_type && final_type.id() == ID_union)
else if(symbol.is_type && symbol.type.id() == ID_union)
{
symbol.pretty_name="union "+id2string(symbol.base_name);
}
else if(symbol.is_type && final_type.id() == ID_c_enum)
else if(symbol.is_type && symbol.type.id() == ID_c_enum)
{
symbol.pretty_name="enum "+id2string(symbol.base_name);
}
Expand Down Expand Up @@ -191,8 +189,8 @@
symbolt &old_symbol,
symbolt &new_symbol)
{
const typet &final_old=follow(old_symbol.type);
const typet &final_new=follow(new_symbol.type);
const typet &final_old = old_symbol.type;
const typet &final_new = new_symbol.type;

// see if we had something incomplete before
if(
Expand Down Expand Up @@ -260,16 +258,16 @@
else if(
config.ansi_c.os == configt::ansi_ct::ost::OS_WIN &&
final_new.id() == ID_pointer && final_old.id() == ID_pointer &&
follow(to_pointer_type(final_new).base_type()).id() == ID_c_enum &&
follow(to_pointer_type(final_old).base_type()).id() == ID_c_enum)
to_pointer_type(final_new).base_type().id() == ID_c_enum &&
to_pointer_type(final_old).base_type().id() == ID_c_enum)

Check warning on line 262 in src/ansi-c/c_typecheck_base.cpp

View check run for this annotation

Codecov / codecov/patch

src/ansi-c/c_typecheck_base.cpp#L262

Added line #L262 was not covered by tests
{
// under Windows, ignore this silently;
// MSC doesn't think this is a problem, but GCC complains.
}
else
{
// see if it changed
if(follow(new_symbol.type)!=follow(old_symbol.type))
if(new_symbol.type != old_symbol.type)
{
error().source_location=new_symbol.location;
error() << "type symbol '" << new_symbol.display_name()
Expand Down Expand Up @@ -321,8 +319,8 @@
symbolt &old_symbol,
symbolt &new_symbol)
{
const typet &final_old=follow(old_symbol.type);
const typet &initial_new=follow(new_symbol.type);
const typet &final_old = old_symbol.type;
const typet &initial_new = new_symbol.type;

if(
final_old.id() == ID_array &&
Expand Down Expand Up @@ -351,7 +349,7 @@
if(new_symbol.type.id() != ID_code && !new_symbol.is_macro)
do_initializer(new_symbol);

const typet &final_new=follow(new_symbol.type);
const typet &final_new = new_symbol.type;

// K&R stuff?
if(old_symbol.type.id()==ID_KnR)
Expand Down Expand Up @@ -548,9 +546,10 @@
// int (*f) ();
}
else if(
final_old.id() == ID_struct && final_new.id() == ID_struct &&
final_old.id() == ID_struct_tag && final_new.id() == ID_struct_tag &&
is_instantiation_of_flexible_array(
to_struct_type(final_old), to_struct_type(final_new)))
follow_tag(to_struct_tag_type(final_old)),
follow_tag(to_struct_tag_type(final_new))))
{
old_symbol.type = new_symbol.type;
}
Expand Down
8 changes: 6 additions & 2 deletions src/ansi-c/c_typecheck_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,13 @@ bool c_typecheck_baset::is_complete_type(const typet &type) const
}
else if(type.id()==ID_vector)
return is_complete_type(to_vector_type(type).element_type());
else if(type.id() == ID_struct_tag || type.id() == ID_union_tag)
else if(auto struct_tag_type = type_try_dynamic_cast<struct_tag_typet>(type))
{
return is_complete_type(follow(type));
return is_complete_type(follow_tag(*struct_tag_type));
}
else if(auto union_tag_type = type_try_dynamic_cast<union_tag_typet>(type))
{
return is_complete_type(follow_tag(*union_tag_type));
}

return true;
Expand Down
82 changes: 37 additions & 45 deletions src/ansi-c/c_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,14 +451,13 @@ void c_typecheck_baset::typecheck_expr_main(exprt &expr)
exprt default_match=nil_exprt();
exprt assoc_match=nil_exprt();

const typet &op_type = follow(op.type());
const typet &op_type = op.type();

for(const auto &irep : generic_associations)
{
if(irep.get(ID_type_arg) == ID_default)
default_match = static_cast<const exprt &>(irep.find(ID_value));
else if(
op_type == follow(static_cast<const typet &>(irep.find(ID_type_arg))))
else if(op_type == static_cast<const typet &>(irep.find(ID_type_arg)))
{
assoc_match = static_cast<const exprt &>(irep.find(ID_value));
}
Expand Down Expand Up @@ -590,11 +589,9 @@ void c_typecheck_baset::typecheck_expr_builtin_offsetof(exprt &expr)

for(const auto &op : member.operands())
{
type = follow(type);

if(op.id() == ID_member)
{
if(type.id()!=ID_union && type.id()!=ID_struct)
if(type.id() != ID_union_tag && type.id() != ID_struct_tag)
{
error().source_location = expr.source_location();
error() << "offsetof of member expects struct/union type, "
Expand All @@ -607,20 +604,20 @@ void c_typecheck_baset::typecheck_expr_builtin_offsetof(exprt &expr)

while(!found)
{
PRECONDITION(type.id() == ID_union || type.id() == ID_struct);
PRECONDITION(type.id() == ID_union_tag || type.id() == ID_struct_tag);

const struct_union_typet &struct_union_type=
to_struct_union_type(type);
const struct_union_typet &struct_union_type =
follow_tag(to_struct_or_union_tag_type(type));

// direct member?
if(struct_union_type.has_component(component_name))
{
found=true;

if(type.id()==ID_struct)
if(type.id() == ID_struct_tag)
{
auto o_opt =
member_offset_expr(to_struct_type(type), component_name, *this);
auto o_opt = member_offset_expr(
follow_tag(to_struct_tag_type(type)), component_name, *this);

if(!o_opt.has_value())
{
Expand Down Expand Up @@ -650,10 +647,10 @@ void c_typecheck_baset::typecheck_expr_builtin_offsetof(exprt &expr)
{
if(has_component_rec(c.type(), component_name, *this))
{
if(type.id()==ID_struct)
if(type.id() == ID_struct_tag)
{
auto o_opt = member_offset_expr(
to_struct_type(type), c.get_name(), *this);
follow_tag(to_struct_tag_type(type)), c.get_name(), *this);

if(!o_opt.has_value())
{
Expand All @@ -669,9 +666,10 @@ void c_typecheck_baset::typecheck_expr_builtin_offsetof(exprt &expr)
o_opt.value(), size_type()));
}

typet tmp = follow(c.type());
typet tmp = c.type();
type=tmp;
CHECK_RETURN(type.id() == ID_union || type.id() == ID_struct);
CHECK_RETURN(
type.id() == ID_union_tag || type.id() == ID_struct_tag);
found2=true;
break; // we run into another iteration of the outer loop
}
Expand Down Expand Up @@ -1377,7 +1375,7 @@ void c_typecheck_baset::typecheck_expr_rel(

if(expr.id()==ID_equal || expr.id()==ID_notequal)
{
if(follow(o_type0)==follow(o_type1))
if(o_type0 == o_type1)
{
if(o_type0.id() != ID_array)
{
Expand Down Expand Up @@ -1530,10 +1528,7 @@ void c_typecheck_baset::typecheck_expr_member(exprt &expr)
exprt &op0 = to_unary_expr(expr).op();
typet type=op0.type();

type = follow(type);

if(type.id()!=ID_struct &&
type.id()!=ID_union)
if(type.id() != ID_struct_tag && type.id() != ID_union_tag)
{
error().source_location = expr.source_location();
error() << "member operator requires structure type "
Expand All @@ -1542,8 +1537,8 @@ void c_typecheck_baset::typecheck_expr_member(exprt &expr)
throw 0;
}

const struct_union_typet &struct_union_type=
to_struct_union_type(type);
const struct_union_typet &struct_union_type =
follow_tag(to_struct_or_union_tag_type(type));

if(struct_union_type.is_incomplete())
{
Expand Down Expand Up @@ -1585,8 +1580,12 @@ void c_typecheck_baset::typecheck_expr_member(exprt &expr)
if(op0.get_bool(ID_C_lvalue))
expr.set(ID_C_lvalue, true);

if(op0.type().get_bool(ID_C_constant) || type.get_bool(ID_C_constant))
if(
op0.type().get_bool(ID_C_constant) ||
struct_union_type.get_bool(ID_C_constant))
{
expr.type().set(ID_C_constant, true);
}

// copy method identifier
const irep_idt &identifier=component.get(ID_C_identifier);
Expand Down Expand Up @@ -3649,11 +3648,10 @@ exprt c_typecheck_baset::do_special_functions(

// The value doesn't matter at all, we only care about the type.
// Need to sync with typeclass.h.
typet type = follow(object.type());

// use underlying type for bit fields
if(type.id() == ID_c_bit_field)
type = to_c_bit_field_type(type).underlying_type();
const typet &type = object.type().id() == ID_c_bit_field
? to_c_bit_field_type(object.type()).underlying_type()
: object.type();

unsigned type_number;

Expand All @@ -3665,23 +3663,17 @@ exprt c_typecheck_baset::do_special_functions(
}
else
{
type_number =
type.id() == ID_empty
? 0u
: (type.id() == ID_bool || type.id() == ID_c_bool)
? 4u
: (type.id() == ID_pointer || type.id() == ID_array)
? 5u
: type.id() == ID_floatbv
? 8u
: (type.id() == ID_complex &&
to_complex_type(type).subtype().id() == ID_floatbv)
? 9u
: type.id() == ID_struct
? 12u
: type.id() == ID_union
? 13u
: 1u; // int, short, char, enum_tag
type_number = type.id() == ID_empty ? 0u
: (type.id() == ID_bool || type.id() == ID_c_bool) ? 4u
: (type.id() == ID_pointer || type.id() == ID_array) ? 5u
: type.id() == ID_floatbv ? 8u
: (type.id() == ID_complex &&
to_complex_type(type).subtype().id() == ID_floatbv)
? 9u
: type.id() == ID_struct_tag ? 12u
: type.id() == ID_union_tag
? 13u
: 1u; // int, short, char, enum_tag
}

exprt tmp=from_integer(type_number, expr.type());
Expand Down
Loading
Loading