Skip to content

Commit 6f628d8

Browse files
authored
Merge pull request #8227 from tautschnig/cleanup/no-follow-cpp
C++ front-end: Replace uses of namespacet::follow
2 parents 6cad8ca + 2b2fd14 commit 6f628d8

13 files changed

+346
-270
lines changed

src/cpp/cpp_constructor.cpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ std::optional<codet> cpp_typecheckt::cpp_constructor(
3030

3131
elaborate_class_template(object_tc.type());
3232

33-
typet tmp_type(follow(object_tc.type()));
34-
CHECK_RETURN(!is_reference(tmp_type));
33+
CHECK_RETURN(!is_reference(object_tc.type()));
3534

36-
if(tmp_type.id()==ID_array)
35+
if(object_tc.type().id() == ID_array)
3736
{
3837
// We allow only one operand and it must be tagged with '#array_ini'.
3938
// Note that the operand is an array that is used for copy-initialization.
@@ -53,11 +52,10 @@ std::optional<codet> cpp_typecheckt::cpp_constructor(
5352
operands.empty() || operands.size() == 1,
5453
"array constructor must have at most one operand");
5554

56-
if(operands.empty() && cpp_is_pod(tmp_type))
55+
if(operands.empty() && cpp_is_pod(object_tc.type()))
5756
return {};
5857

59-
const exprt &size_expr=
60-
to_array_type(tmp_type).size();
58+
const exprt &size_expr = to_array_type(object_tc.type()).size();
6159

6260
if(size_expr.id() == ID_infinity)
6361
return {}; // don't initialize
@@ -74,7 +72,7 @@ std::optional<codet> cpp_typecheckt::cpp_constructor(
7472
throw 0;
7573
}
7674

77-
/*if(cpp_is_pod(tmp_type))
75+
/*if(cpp_is_pod(object_tc.type()))
7876
{
7977
code_expressiont new_code;
8078
exprt op_tc=operands.front();
@@ -119,7 +117,7 @@ std::optional<codet> cpp_typecheckt::cpp_constructor(
119117
return std::move(new_code);
120118
}
121119
}
122-
else if(cpp_is_pod(tmp_type))
120+
else if(cpp_is_pod(object_tc.type()))
123121
{
124122
exprt::operandst operands_tc=operands;
125123

@@ -152,11 +150,11 @@ std::optional<codet> cpp_typecheckt::cpp_constructor(
152150
throw 0;
153151
}
154152
}
155-
else if(tmp_type.id()==ID_union)
153+
else if(object_tc.type().id() == ID_union_tag)
156154
{
157155
UNREACHABLE; // Todo: union
158156
}
159-
else if(tmp_type.id()==ID_struct)
157+
else if(object_tc.type().id() == ID_struct_tag)
160158
{
161159
exprt::operandst operands_tc=operands;
162160

@@ -166,8 +164,8 @@ std::optional<codet> cpp_typecheckt::cpp_constructor(
166164
add_implicit_dereference(op);
167165
}
168166

169-
const struct_typet &struct_type=
170-
to_struct_type(tmp_type);
167+
const struct_typet &struct_type =
168+
follow_tag(to_struct_tag_type(object_tc.type()));
171169

172170
// set most-derived bits
173171
code_blockt block;

src/cpp/cpp_destructor.cpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ std::optional<codet> cpp_typecheckt::cpp_destructor(
2222
{
2323
elaborate_class_template(object.type());
2424

25-
typet tmp_type(follow(object.type()));
26-
CHECK_RETURN(!is_reference(tmp_type));
25+
CHECK_RETURN(!is_reference(object.type()));
2726

2827
// PODs don't need a destructor
29-
if(cpp_is_pod(tmp_type))
28+
if(cpp_is_pod(object.type()))
3029
return {};
3130

32-
if(tmp_type.id()==ID_array)
31+
if(object.type().id() == ID_array)
3332
{
34-
const exprt &size_expr=
35-
to_array_type(tmp_type).size();
33+
const exprt &size_expr = to_array_type(object.type()).size();
3634

3735
if(size_expr.id() == ID_infinity)
3836
return {}; // don't initialize
@@ -70,8 +68,8 @@ std::optional<codet> cpp_typecheckt::cpp_destructor(
7068
}
7169
else
7270
{
73-
const struct_typet &struct_type=
74-
to_struct_type(tmp_type);
71+
const struct_typet &struct_type =
72+
follow_tag(to_struct_tag_type(object.type()));
7573

7674
// enter struct scope
7775
cpp_save_scopet save_scope(cpp_scopes);

src/cpp/cpp_typecheck.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ const struct_typet &cpp_typecheckt::this_struct_type()
6767
CHECK_RETURN(this_expr.is_not_nil());
6868
CHECK_RETURN(this_expr.type().id() == ID_pointer);
6969

70-
const typet &t = follow(to_pointer_type(this_expr.type()).base_type());
71-
CHECK_RETURN(t.id() == ID_struct);
72-
return to_struct_type(t);
70+
const typet &t = to_pointer_type(this_expr.type()).base_type();
71+
CHECK_RETURN(t.id() == ID_struct_tag);
72+
return follow_tag(to_struct_tag_type(t));
7373
}
7474

7575
std::string cpp_typecheckt::to_string(const exprt &expr)

src/cpp/cpp_typecheck.h

+13-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Author: Daniel Kroening, [email protected]
2525
#include <set>
2626
#include <unordered_set>
2727

28+
class pointer_typet;
29+
class reference_typet;
30+
2831
bool cpp_typecheck(
2932
cpp_parse_treet &cpp_parse_tree,
3033
symbol_table_baset &symbol_table,
@@ -524,14 +527,18 @@ class cpp_typecheckt:public c_typecheck_baset
524527
bool user_defined_conversion_sequence(
525528
const exprt &expr, const typet &type, exprt &new_expr, unsigned &rank);
526529

527-
bool reference_related(
528-
const exprt &expr, const typet &type) const;
530+
bool reference_related(const exprt &expr, const reference_typet &type) const;
529531

530532
bool reference_compatible(
531-
const exprt &expr, const typet &type, unsigned &rank) const;
533+
const exprt &expr,
534+
const reference_typet &type,
535+
unsigned &rank) const;
532536

533537
bool reference_binding(
534-
exprt expr, const typet &type, exprt &new_expr, unsigned &rank);
538+
exprt expr,
539+
const reference_typet &type,
540+
exprt &new_expr,
541+
unsigned &rank);
535542

536543
bool implicit_conversion_sequence(
537544
const exprt &expr, const typet &type, exprt &new_expr, unsigned &rank);
@@ -542,7 +549,7 @@ class cpp_typecheckt:public c_typecheck_baset
542549
bool implicit_conversion_sequence(
543550
const exprt &expr, const typet &type, exprt &new_expr);
544551

545-
void reference_initializer(exprt &expr, const typet &type);
552+
void reference_initializer(exprt &expr, const reference_typet &type);
546553

547554
void implicit_typecast(exprt &expr, const typet &type) override;
548555

@@ -556,9 +563,7 @@ class cpp_typecheckt:public c_typecheck_baset
556563
const struct_typet &from,
557564
const struct_typet &to) const;
558565

559-
void make_ptr_typecast(
560-
exprt &expr,
561-
const typet &dest_type);
566+
void make_ptr_typecast(exprt &expr, const pointer_typet &dest_type);
562567

563568
// the C++ typecasts
564569

src/cpp/cpp_typecheck_code.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ Author: Daniel Kroening, [email protected]
99
/// \file
1010
/// C++ Language Type Checking
1111

12-
#include "cpp_typecheck.h"
13-
1412
#include <util/arith_tools.h>
1513
#include <util/bitvector_expr.h>
14+
#include <util/c_types.h>
1615
#include <util/pointer_expr.h>
1716
#include <util/source_location.h>
1817

1918
#include "cpp_declarator_converter.h"
2019
#include "cpp_exception_id.h"
20+
#include "cpp_typecheck.h"
2121
#include "cpp_typecheck_fargs.h"
2222
#include "cpp_util.h"
2323

@@ -258,8 +258,7 @@ void cpp_typecheckt::typecheck_member_initializer(codet &code)
258258
PRECONDITION(this_expr.is_not_nil());
259259

260260
make_ptr_typecast(
261-
this_expr,
262-
code_type.parameters().front().type());
261+
this_expr, to_pointer_type(code_type.parameters().front().type()));
263262

264263
function_call.arguments().push_back(this_expr);
265264

@@ -354,7 +353,8 @@ void cpp_typecheckt::typecheck_member_initializer(codet &code)
354353
throw 0;
355354
}
356355

357-
reference_initializer(code.op0(), symbol_expr.type());
356+
reference_initializer(
357+
code.op0(), to_reference_type(symbol_expr.type()));
358358

359359
// assign the pointers
360360
symbol_expr.type().remove(ID_C_reference);
@@ -421,8 +421,13 @@ void cpp_typecheckt::typecheck_decl(codet &code)
421421

422422
CHECK_RETURN(type.is_not_nil());
423423

424-
if(declaration.declarators().empty() &&
425-
follow(type).get_bool(ID_C_is_anonymous))
424+
if(
425+
declaration.declarators().empty() &&
426+
((type.id() == ID_struct_tag &&
427+
follow_tag(to_struct_tag_type(type)).get_bool(ID_C_is_anonymous)) ||
428+
(type.id() == ID_union_tag &&
429+
follow_tag(to_union_tag_type(type)).get_bool(ID_C_is_anonymous)) ||
430+
type.get_bool(ID_C_is_anonymous)))
426431
{
427432
if(type.id() != ID_union_tag)
428433
{
@@ -470,8 +475,7 @@ void cpp_typecheckt::typecheck_decl(codet &code)
470475
{
471476
decl_statement.copy_to_operands(symbol.value);
472477
DATA_INVARIANT(
473-
has_auto(symbol.type) ||
474-
follow(decl_statement.op1().type()) == follow(symbol.type),
478+
has_auto(symbol.type) || decl_statement.op1().type() == symbol.type,
475479
"declarator type should match symbol type");
476480
}
477481

src/cpp/cpp_typecheck_compound_type.cpp

+44-23
Original file line numberDiff line numberDiff line change
@@ -1019,15 +1019,21 @@ void cpp_typecheckt::typecheck_compound_body(symbolt &symbol)
10191019
throw 0;
10201020
}
10211021

1022-
typet final_type=follow(declaration.type());
1023-
10241022
// anonymous member?
1025-
if(declaration.declarators().empty() &&
1026-
final_type.get_bool(ID_C_is_anonymous))
1023+
if(
1024+
declaration.declarators().empty() &&
1025+
((declaration.type().id() == ID_struct_tag &&
1026+
follow_tag(to_struct_tag_type(declaration.type()))
1027+
.get_bool(ID_C_is_anonymous)) ||
1028+
(declaration.type().id() == ID_union_tag &&
1029+
follow_tag(to_union_tag_type(declaration.type()))
1030+
.get_bool(ID_C_is_anonymous)) ||
1031+
declaration.type().get_bool(ID_C_is_anonymous)))
10271032
{
10281033
// we only allow this on struct/union types
1029-
if(final_type.id()!=ID_union &&
1030-
final_type.id()!=ID_struct)
1034+
if(
1035+
declaration.type().id() != ID_union_tag &&
1036+
declaration.type().id() != ID_struct_tag)
10311037
{
10321038
error().source_location=declaration.type().source_location();
10331039
error() << "member declaration does not declare anything"
@@ -1423,8 +1429,14 @@ void cpp_typecheckt::convert_anon_struct_union_member(
14231429
const irep_idt &access,
14241430
struct_typet::componentst &components)
14251431
{
1432+
const struct_union_typet &final_type =
1433+
declaration.type().id() == ID_struct_tag
1434+
? static_cast<const struct_union_typet &>(
1435+
follow_tag(to_struct_tag_type(declaration.type())))
1436+
: static_cast<const struct_union_typet &>(
1437+
follow_tag(to_union_tag_type(declaration.type())));
14261438
symbolt &struct_union_symbol =
1427-
symbol_table.get_writeable_ref(follow(declaration.type()).get(ID_name));
1439+
symbol_table.get_writeable_ref(final_type.get(ID_name));
14281440

14291441
if(declaration.storage_spec().is_static() ||
14301442
declaration.storage_spec().is_mutable())
@@ -1477,13 +1489,15 @@ bool cpp_typecheckt::get_component(
14771489
const irep_idt &component_name,
14781490
exprt &member)
14791491
{
1480-
const typet &followed_type=follow(object.type());
1481-
14821492
PRECONDITION(
1483-
followed_type.id() == ID_struct || followed_type.id() == ID_union);
1493+
object.type().id() == ID_struct_tag || object.type().id() == ID_union_tag);
14841494

1485-
struct_union_typet final_type=
1486-
to_struct_union_type(followed_type);
1495+
struct_union_typet final_type =
1496+
object.type().id() == ID_struct_tag
1497+
? static_cast<const struct_union_typet &>(
1498+
follow_tag(to_struct_tag_type(object.type())))
1499+
: static_cast<const struct_union_typet &>(
1500+
follow_tag(to_union_tag_type(object.type())));
14871501

14881502
const struct_union_typet::componentst &components=
14891503
final_type.components();
@@ -1529,14 +1543,22 @@ bool cpp_typecheckt::get_component(
15291543

15301544
return true; // component found
15311545
}
1532-
else if(follow(component.type()).find(ID_C_unnamed_object).is_not_nil())
1546+
else if(
1547+
(component.type().id() == ID_struct_tag &&
1548+
follow_tag(to_struct_tag_type(component.type()))
1549+
.find(ID_C_unnamed_object)
1550+
.is_not_nil()) ||
1551+
(component.type().id() == ID_union_tag &&
1552+
follow_tag(to_union_tag_type(component.type()))
1553+
.find(ID_C_unnamed_object)
1554+
.is_not_nil()) ||
1555+
component.type().find(ID_C_unnamed_object).is_not_nil())
15331556
{
15341557
// could be anonymous union or struct
15351558

1536-
const typet &component_type=follow(component.type());
1537-
1538-
if(component_type.id()==ID_union ||
1539-
component_type.id()==ID_struct)
1559+
if(
1560+
component.type().id() == ID_union_tag ||
1561+
component.type().id() == ID_struct_tag)
15401562
{
15411563
// recursive call!
15421564
if(get_component(source_location, tmp, component_name, member))
@@ -1680,18 +1702,17 @@ bool cpp_typecheckt::subtype_typecast(
16801702

16811703
void cpp_typecheckt::make_ptr_typecast(
16821704
exprt &expr,
1683-
const typet &dest_type)
1705+
const pointer_typet &dest_type)
16841706
{
16851707
typet src_type=expr.type();
16861708

16871709
PRECONDITION(src_type.id() == ID_pointer);
1688-
PRECONDITION(dest_type.id() == ID_pointer);
16891710

1690-
const struct_typet &src_struct = to_struct_type(
1691-
static_cast<const typet &>(follow(to_pointer_type(src_type).base_type())));
1711+
const struct_typet &src_struct =
1712+
follow_tag(to_struct_tag_type(to_pointer_type(src_type).base_type()));
16921713

1693-
const struct_typet &dest_struct = to_struct_type(
1694-
static_cast<const typet &>(follow(to_pointer_type(dest_type).base_type())));
1714+
const struct_typet &dest_struct =
1715+
follow_tag(to_struct_tag_type(dest_type.base_type()));
16951716

16961717
PRECONDITION(
16971718
subtype_typecast(src_struct, dest_struct) ||

0 commit comments

Comments
 (0)