Skip to content

Commit bf29f34

Browse files
committed
C++ type checking: use reference_typet, pointer_typet
Hoist type conversions up for improved type safety and clarity.
1 parent 6cad8ca commit bf29f34

File tree

5 files changed

+70
-68
lines changed

5 files changed

+70
-68
lines changed

src/cpp/cpp_typecheck.h

Lines changed: 13 additions & 8 deletions
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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/cpp/cpp_typecheck_compound_type.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,18 +1680,17 @@ bool cpp_typecheckt::subtype_typecast(
16801680

16811681
void cpp_typecheckt::make_ptr_typecast(
16821682
exprt &expr,
1683-
const typet &dest_type)
1683+
const pointer_typet &dest_type)
16841684
{
16851685
typet src_type=expr.type();
16861686

16871687
PRECONDITION(src_type.id() == ID_pointer);
1688-
PRECONDITION(dest_type.id() == ID_pointer);
16891688

16901689
const struct_typet &src_struct = to_struct_type(
16911690
static_cast<const typet &>(follow(to_pointer_type(src_type).base_type())));
16921691

1693-
const struct_typet &dest_struct = to_struct_type(
1694-
static_cast<const typet &>(follow(to_pointer_type(dest_type).base_type())));
1692+
const struct_typet &dest_struct =
1693+
to_struct_type(static_cast<const typet &>(follow(dest_type.base_type())));
16951694

16961695
PRECONDITION(
16971696
subtype_typecast(src_struct, dest_struct) ||

0 commit comments

Comments
 (0)