Skip to content

Commit e6f7725

Browse files
committed
C front-end: Replace uses of namespacet::follow
This is deprecated. Use suitable variants of `follow_tag` instead.
1 parent 6cad8ca commit e6f7725

9 files changed

+219
-214
lines changed

Diff for: src/ansi-c/anonymous_member.cpp

+15-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "anonymous_member.h"
1313

14+
#include <util/c_types.h>
1415
#include <util/namespace.h>
1516
#include <util/std_expr.h>
1617

@@ -26,8 +27,11 @@ static member_exprt make_member_expr(
2627
result.set(ID_C_lvalue, true);
2728

2829
// todo: should to typedef chains properly
29-
const typet &type=
30-
ns.follow(struct_union.type());
30+
const typet &tag_type = struct_union.type();
31+
const typet &type =
32+
tag_type.id() == ID_struct_tag
33+
? static_cast<const typet &>(ns.follow_tag(to_struct_tag_type(tag_type)))
34+
: static_cast<const typet &>(ns.follow_tag(to_union_tag_type(tag_type)));
3135

3236
if(
3337
type.get_bool(ID_C_constant) || struct_union.type().get_bool(ID_C_constant))
@@ -43,11 +47,11 @@ exprt get_component_rec(
4347
const irep_idt &component_name,
4448
const namespacet &ns)
4549
{
46-
const struct_union_typet &struct_union_type=
47-
to_struct_union_type(ns.follow(struct_union.type()));
48-
49-
const struct_union_typet::componentst &components=
50-
struct_union_type.components();
50+
const typet &tag_type = struct_union.type();
51+
const struct_union_typet::componentst &components =
52+
tag_type.id() == ID_struct_tag
53+
? ns.follow_tag(to_struct_tag_type(tag_type)).components()
54+
: ns.follow_tag(to_union_tag_type(tag_type)).components();
5155

5256
for(const auto &comp : components)
5357
{
@@ -76,11 +80,10 @@ bool has_component_rec(
7680
const irep_idt &component_name,
7781
const namespacet &ns)
7882
{
79-
const struct_union_typet &struct_union_type=
80-
to_struct_union_type(ns.follow(type));
81-
82-
const struct_union_typet::componentst &components=
83-
struct_union_type.components();
83+
const struct_union_typet::componentst &components =
84+
type.id() == ID_struct_tag
85+
? ns.follow_tag(to_struct_tag_type(type)).components()
86+
: ns.follow_tag(to_union_tag_type(type)).components();
8487

8588
for(const auto &comp : components)
8689
{

Diff for: src/ansi-c/c_typecast.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,17 @@ typet c_typecastt::follow_with_qualifiers(const typet &src_type)
289289
// collect qualifiers
290290
c_qualifierst qualifiers(src_type);
291291

292-
while(result_type.id() == ID_struct_tag || result_type.id() == ID_union_tag)
292+
if(
293+
auto struct_tag_type = type_try_dynamic_cast<struct_tag_typet>(result_type))
294+
{
295+
const typet &followed_type = ns.follow_tag(*struct_tag_type);
296+
result_type = followed_type;
297+
qualifiers += c_qualifierst(followed_type);
298+
}
299+
else if(
300+
auto union_tag_type = type_try_dynamic_cast<union_tag_typet>(result_type))
293301
{
294-
const typet &followed_type = ns.follow(result_type);
302+
const typet &followed_type = ns.follow_tag(*union_tag_type);
295303
result_type = followed_type;
296304
qualifiers += c_qualifierst(followed_type);
297305
}

Diff for: src/ansi-c/c_typecheck_base.cpp

+14-15
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ void c_typecheck_baset::typecheck_symbol(symbolt &symbol)
5353
{
5454
bool is_function=symbol.type.id()==ID_code;
5555

56-
const typet &final_type=follow(symbol.type);
57-
5856
// set a few flags
5957
symbol.is_lvalue=!symbol.is_type && !symbol.is_macro;
6058

@@ -91,15 +89,15 @@ void c_typecheck_baset::typecheck_symbol(symbolt &symbol)
9189
}
9290

9391
// set the pretty name
94-
if(symbol.is_type && final_type.id() == ID_struct)
92+
if(symbol.is_type && symbol.type.id() == ID_struct)
9593
{
9694
symbol.pretty_name="struct "+id2string(symbol.base_name);
9795
}
98-
else if(symbol.is_type && final_type.id() == ID_union)
96+
else if(symbol.is_type && symbol.type.id() == ID_union)
9997
{
10098
symbol.pretty_name="union "+id2string(symbol.base_name);
10199
}
102-
else if(symbol.is_type && final_type.id() == ID_c_enum)
100+
else if(symbol.is_type && symbol.type.id() == ID_c_enum)
103101
{
104102
symbol.pretty_name="enum "+id2string(symbol.base_name);
105103
}
@@ -191,8 +189,8 @@ void c_typecheck_baset::typecheck_redefinition_type(
191189
symbolt &old_symbol,
192190
symbolt &new_symbol)
193191
{
194-
const typet &final_old=follow(old_symbol.type);
195-
const typet &final_new=follow(new_symbol.type);
192+
const typet &final_old = old_symbol.type;
193+
const typet &final_new = new_symbol.type;
196194

197195
// see if we had something incomplete before
198196
if(
@@ -260,16 +258,16 @@ void c_typecheck_baset::typecheck_redefinition_type(
260258
else if(
261259
config.ansi_c.os == configt::ansi_ct::ost::OS_WIN &&
262260
final_new.id() == ID_pointer && final_old.id() == ID_pointer &&
263-
follow(to_pointer_type(final_new).base_type()).id() == ID_c_enum &&
264-
follow(to_pointer_type(final_old).base_type()).id() == ID_c_enum)
261+
to_pointer_type(final_new).base_type().id() == ID_c_enum &&
262+
to_pointer_type(final_old).base_type().id() == ID_c_enum)
265263
{
266264
// under Windows, ignore this silently;
267265
// MSC doesn't think this is a problem, but GCC complains.
268266
}
269267
else
270268
{
271269
// see if it changed
272-
if(follow(new_symbol.type)!=follow(old_symbol.type))
270+
if(new_symbol.type != old_symbol.type)
273271
{
274272
error().source_location=new_symbol.location;
275273
error() << "type symbol '" << new_symbol.display_name()
@@ -321,8 +319,8 @@ void c_typecheck_baset::typecheck_redefinition_non_type(
321319
symbolt &old_symbol,
322320
symbolt &new_symbol)
323321
{
324-
const typet &final_old=follow(old_symbol.type);
325-
const typet &initial_new=follow(new_symbol.type);
322+
const typet &final_old = old_symbol.type;
323+
const typet &initial_new = new_symbol.type;
326324

327325
if(
328326
final_old.id() == ID_array &&
@@ -351,7 +349,7 @@ void c_typecheck_baset::typecheck_redefinition_non_type(
351349
if(new_symbol.type.id() != ID_code && !new_symbol.is_macro)
352350
do_initializer(new_symbol);
353351

354-
const typet &final_new=follow(new_symbol.type);
352+
const typet &final_new = new_symbol.type;
355353

356354
// K&R stuff?
357355
if(old_symbol.type.id()==ID_KnR)
@@ -548,9 +546,10 @@ void c_typecheck_baset::typecheck_redefinition_non_type(
548546
// int (*f) ();
549547
}
550548
else if(
551-
final_old.id() == ID_struct && final_new.id() == ID_struct &&
549+
final_old.id() == ID_struct_tag && final_new.id() == ID_struct_tag &&
552550
is_instantiation_of_flexible_array(
553-
to_struct_type(final_old), to_struct_type(final_new)))
551+
follow_tag(to_struct_tag_type(final_old)),
552+
follow_tag(to_struct_tag_type(final_new))))
554553
{
555554
old_symbol.type = new_symbol.type;
556555
}

Diff for: src/ansi-c/c_typecheck_code.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,13 @@ bool c_typecheck_baset::is_complete_type(const typet &type) const
387387
}
388388
else if(type.id()==ID_vector)
389389
return is_complete_type(to_vector_type(type).element_type());
390-
else if(type.id() == ID_struct_tag || type.id() == ID_union_tag)
390+
else if(auto struct_tag_type = type_try_dynamic_cast<struct_tag_typet>(type))
391391
{
392-
return is_complete_type(follow(type));
392+
return is_complete_type(follow_tag(*struct_tag_type));
393+
}
394+
else if(auto union_tag_type = type_try_dynamic_cast<union_tag_typet>(type))
395+
{
396+
return is_complete_type(follow_tag(*union_tag_type));
393397
}
394398

395399
return true;

Diff for: src/ansi-c/c_typecheck_expr.cpp

+41-45
Original file line numberDiff line numberDiff line change
@@ -451,14 +451,13 @@ void c_typecheck_baset::typecheck_expr_main(exprt &expr)
451451
exprt default_match=nil_exprt();
452452
exprt assoc_match=nil_exprt();
453453

454-
const typet &op_type = follow(op.type());
454+
const typet &op_type = op.type();
455455

456456
for(const auto &irep : generic_associations)
457457
{
458458
if(irep.get(ID_type_arg) == ID_default)
459459
default_match = static_cast<const exprt &>(irep.find(ID_value));
460-
else if(
461-
op_type == follow(static_cast<const typet &>(irep.find(ID_type_arg))))
460+
else if(op_type == static_cast<const typet &>(irep.find(ID_type_arg)))
462461
{
463462
assoc_match = static_cast<const exprt &>(irep.find(ID_value));
464463
}
@@ -590,11 +589,9 @@ void c_typecheck_baset::typecheck_expr_builtin_offsetof(exprt &expr)
590589

591590
for(const auto &op : member.operands())
592591
{
593-
type = follow(type);
594-
595592
if(op.id() == ID_member)
596593
{
597-
if(type.id()!=ID_union && type.id()!=ID_struct)
594+
if(type.id() != ID_union_tag && type.id() != ID_struct_tag)
598595
{
599596
error().source_location = expr.source_location();
600597
error() << "offsetof of member expects struct/union type, "
@@ -607,20 +604,23 @@ void c_typecheck_baset::typecheck_expr_builtin_offsetof(exprt &expr)
607604

608605
while(!found)
609606
{
610-
PRECONDITION(type.id() == ID_union || type.id() == ID_struct);
607+
PRECONDITION(type.id() == ID_union_tag || type.id() == ID_struct_tag);
611608

612-
const struct_union_typet &struct_union_type=
613-
to_struct_union_type(type);
609+
const struct_union_typet &struct_union_type =
610+
type.id() == ID_struct_tag ? static_cast<const struct_union_typet &>(
611+
follow_tag(to_struct_tag_type(type)))
612+
: static_cast<const struct_union_typet &>(
613+
follow_tag(to_union_tag_type(type)));
614614

615615
// direct member?
616616
if(struct_union_type.has_component(component_name))
617617
{
618618
found=true;
619619

620-
if(type.id()==ID_struct)
620+
if(type.id() == ID_struct_tag)
621621
{
622-
auto o_opt =
623-
member_offset_expr(to_struct_type(type), component_name, *this);
622+
auto o_opt = member_offset_expr(
623+
follow_tag(to_struct_tag_type(type)), component_name, *this);
624624

625625
if(!o_opt.has_value())
626626
{
@@ -650,10 +650,10 @@ void c_typecheck_baset::typecheck_expr_builtin_offsetof(exprt &expr)
650650
{
651651
if(has_component_rec(c.type(), component_name, *this))
652652
{
653-
if(type.id()==ID_struct)
653+
if(type.id() == ID_struct_tag)
654654
{
655655
auto o_opt = member_offset_expr(
656-
to_struct_type(type), c.get_name(), *this);
656+
follow_tag(to_struct_tag_type(type)), c.get_name(), *this);
657657

658658
if(!o_opt.has_value())
659659
{
@@ -669,9 +669,10 @@ void c_typecheck_baset::typecheck_expr_builtin_offsetof(exprt &expr)
669669
o_opt.value(), size_type()));
670670
}
671671

672-
typet tmp = follow(c.type());
672+
typet tmp = c.type();
673673
type=tmp;
674-
CHECK_RETURN(type.id() == ID_union || type.id() == ID_struct);
674+
CHECK_RETURN(
675+
type.id() == ID_union_tag || type.id() == ID_struct_tag);
675676
found2=true;
676677
break; // we run into another iteration of the outer loop
677678
}
@@ -1375,7 +1376,7 @@ void c_typecheck_baset::typecheck_expr_rel(
13751376

13761377
if(expr.id()==ID_equal || expr.id()==ID_notequal)
13771378
{
1378-
if(follow(o_type0)==follow(o_type1))
1379+
if(o_type0 == o_type1)
13791380
{
13801381
if(o_type0.id() != ID_array)
13811382
{
@@ -1528,10 +1529,7 @@ void c_typecheck_baset::typecheck_expr_member(exprt &expr)
15281529
exprt &op0 = to_unary_expr(expr).op();
15291530
typet type=op0.type();
15301531

1531-
type = follow(type);
1532-
1533-
if(type.id()!=ID_struct &&
1534-
type.id()!=ID_union)
1532+
if(type.id() != ID_struct_tag && type.id() != ID_union_tag)
15351533
{
15361534
error().source_location = expr.source_location();
15371535
error() << "member operator requires structure type "
@@ -1540,8 +1538,11 @@ void c_typecheck_baset::typecheck_expr_member(exprt &expr)
15401538
throw 0;
15411539
}
15421540

1543-
const struct_union_typet &struct_union_type=
1544-
to_struct_union_type(type);
1541+
const struct_union_typet &struct_union_type =
1542+
type.id() == ID_struct_tag ? static_cast<const struct_union_typet &>(
1543+
follow_tag(to_struct_tag_type(type)))
1544+
: static_cast<const struct_union_typet &>(
1545+
follow_tag(to_union_tag_type(type)));
15451546

15461547
if(struct_union_type.is_incomplete())
15471548
{
@@ -1583,7 +1584,9 @@ void c_typecheck_baset::typecheck_expr_member(exprt &expr)
15831584
if(op0.get_bool(ID_C_lvalue))
15841585
expr.set(ID_C_lvalue, true);
15851586

1586-
if(op0.type().get_bool(ID_C_constant) || type.get_bool(ID_C_constant))
1587+
if(
1588+
op0.type().get_bool(ID_C_constant) ||
1589+
struct_union_type.get_bool(ID_C_constant))
15871590
expr.type().set(ID_C_constant, true);
15881591

15891592
// copy method identifier
@@ -3643,11 +3646,10 @@ exprt c_typecheck_baset::do_special_functions(
36433646

36443647
// The value doesn't matter at all, we only care about the type.
36453648
// Need to sync with typeclass.h.
3646-
typet type = follow(object.type());
3647-
36483649
// use underlying type for bit fields
3649-
if(type.id() == ID_c_bit_field)
3650-
type = to_c_bit_field_type(type).underlying_type();
3650+
const typet &type = object.type().id() == ID_c_bit_field
3651+
? to_c_bit_field_type(object.type()).underlying_type()
3652+
: object.type();
36513653

36523654
unsigned type_number;
36533655

@@ -3659,23 +3661,17 @@ exprt c_typecheck_baset::do_special_functions(
36593661
}
36603662
else
36613663
{
3662-
type_number =
3663-
type.id() == ID_empty
3664-
? 0u
3665-
: (type.id() == ID_bool || type.id() == ID_c_bool)
3666-
? 4u
3667-
: (type.id() == ID_pointer || type.id() == ID_array)
3668-
? 5u
3669-
: type.id() == ID_floatbv
3670-
? 8u
3671-
: (type.id() == ID_complex &&
3672-
to_complex_type(type).subtype().id() == ID_floatbv)
3673-
? 9u
3674-
: type.id() == ID_struct
3675-
? 12u
3676-
: type.id() == ID_union
3677-
? 13u
3678-
: 1u; // int, short, char, enum_tag
3664+
type_number = type.id() == ID_empty ? 0u
3665+
: (type.id() == ID_bool || type.id() == ID_c_bool) ? 4u
3666+
: (type.id() == ID_pointer || type.id() == ID_array) ? 5u
3667+
: type.id() == ID_floatbv ? 8u
3668+
: (type.id() == ID_complex &&
3669+
to_complex_type(type).subtype().id() == ID_floatbv)
3670+
? 9u
3671+
: type.id() == ID_struct_tag ? 12u
3672+
: type.id() == ID_union_tag
3673+
? 13u
3674+
: 1u; // int, short, char, enum_tag
36793675
}
36803676

36813677
exprt tmp=from_integer(type_number, expr.type());

0 commit comments

Comments
 (0)