Skip to content

Commit 7f48ddc

Browse files
author
Daniel Kroening
committed
fix exprt::opX() accesses in cpp/
This improves type safety.
1 parent b20cf69 commit 7f48ddc

9 files changed

+71
-55
lines changed

src/cpp/cpp_constructor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ optionalt<codet> cpp_typecheckt::cpp_constructor(
235235
assert(initializer.id()==ID_code &&
236236
initializer.get(ID_statement)==ID_expression);
237237

238-
side_effect_expr_function_callt &func_ini=
239-
to_side_effect_expr_function_call(initializer.op0());
238+
side_effect_expr_function_callt &func_ini =
239+
to_side_effect_expr_function_call(to_unary_expr(initializer).op());
240240

241241
exprt &tmp_this=func_ini.arguments().front();
242242
DATA_INVARIANT(

src/cpp/cpp_static_assert.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ Author: Daniel Kroening, [email protected]
1212
#ifndef CPROVER_CPP_CPP_STATIC_ASSERT_H
1313
#define CPROVER_CPP_CPP_STATIC_ASSERT_H
1414

15-
#include <util/expr.h>
15+
#include <util/std_expr.h>
1616

17-
class cpp_static_assertt:public exprt
17+
class cpp_static_assertt : public binary_exprt
1818
{
1919
public:
20-
cpp_static_assertt():exprt(ID_cpp_static_assert)
20+
cpp_static_assertt() : binary_exprt(ID_cpp_static_assert)
2121
{
22-
operands().resize(2);
2322
}
2423

2524
exprt &cond()

src/cpp/cpp_typecheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ void cpp_typecheckt::do_not_typechecked()
243243
}
244244
else if(symbol.value.operands().size()==1)
245245
{
246-
value = symbol.value.op0();
246+
value = to_unary_expr(symbol.value).op();
247247
cont=true;
248248
}
249249
else

src/cpp/cpp_typecheck_code.cpp

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,29 @@ void cpp_typecheckt::typecheck_code(codet &code)
5555
// as an extension, we support indexed access into signed/unsigned
5656
// bitvectors, typically used with __CPROVER::(un)signedbv<N>
5757
exprt &expr = code.op0();
58-
if(
59-
expr.operands().size() == 2 && expr.op0().id() == ID_index &&
60-
expr.op0().operands().size() == 2)
58+
59+
if(expr.operands().size() == 2)
6160
{
62-
exprt array = expr.op0().op0();
63-
typecheck_expr(array);
61+
auto &binary_expr = to_binary_expr(expr);
6462

65-
if(array.type().id() == ID_signedbv || array.type().id() == ID_unsignedbv)
63+
if(binary_expr.op0().id() == ID_index)
6664
{
67-
shl_exprt shl{from_integer(1, array.type()), expr.op0().op1()};
68-
exprt rhs =
69-
if_exprt{equal_exprt{expr.op1(), from_integer(0, array.type())},
70-
bitand_exprt{array, bitnot_exprt{shl}},
71-
bitor_exprt{array, shl}};
72-
expr.op0() = expr.op0().op0();
73-
expr.op1() = rhs;
65+
exprt array = to_index_expr(binary_expr.op0()).array();
66+
typecheck_expr(array);
67+
68+
if(
69+
array.type().id() == ID_signedbv ||
70+
array.type().id() == ID_unsignedbv)
71+
{
72+
shl_exprt shl{from_integer(1, array.type()),
73+
to_index_expr(binary_expr.op0()).index()};
74+
exprt rhs = if_exprt{
75+
equal_exprt{binary_expr.op1(), from_integer(0, array.type())},
76+
bitand_exprt{array, bitnot_exprt{shl}},
77+
bitor_exprt{array, shl}};
78+
binary_expr.op0() = to_index_expr(binary_expr.op0()).array();
79+
binary_expr.op1() = rhs;
80+
}
7481
}
7582
}
7683

@@ -191,8 +198,10 @@ void cpp_typecheckt::typecheck_switch(codet &code)
191198
assert(decl.operands().size()==1);
192199

193200
// replace declaration by its symbol
194-
assert(decl.op0().op0().id()==ID_symbol);
195-
value = decl.op0().op0();
201+
DATA_INVARIANT(
202+
to_unary_expr(decl).op().op0().id() == ID_symbol,
203+
"declaration must have symbol as operand");
204+
value = to_unary_expr(decl).op().op0();
196205

197206
c_typecheck_baset::typecheck_switch(code);
198207

@@ -287,11 +296,11 @@ void cpp_typecheckt::typecheck_member_initializer(codet &code)
287296
// a reference member
288297
if(
289298
symbol_expr.id() == ID_dereference &&
290-
symbol_expr.op0().id() == ID_member &&
299+
to_dereference_expr(symbol_expr).pointer().id() == ID_member &&
291300
symbol_expr.get_bool(ID_C_implicit))
292301
{
293302
// treat references as normal pointers
294-
exprt tmp = symbol_expr.op0();
303+
exprt tmp = to_dereference_expr(symbol_expr).pointer();
295304
symbol_expr.swap(tmp);
296305
}
297306

@@ -316,18 +325,20 @@ void cpp_typecheckt::typecheck_member_initializer(codet &code)
316325

317326
if(
318327
symbol_expr.id() == ID_dereference &&
319-
symbol_expr.op0().id() == ID_member &&
328+
to_dereference_expr(symbol_expr).pointer().id() == ID_member &&
320329
symbol_expr.get_bool(ID_C_implicit))
321330
{
322331
// treat references as normal pointers
323-
exprt tmp = symbol_expr.op0();
332+
exprt tmp = to_dereference_expr(symbol_expr).pointer();
324333
symbol_expr.swap(tmp);
325334
}
326335
}
327336

328-
if(symbol_expr.id() == ID_member &&
329-
symbol_expr.op0().id() == ID_dereference &&
330-
symbol_expr.op0().op0() == cpp_scopes.current_scope().this_expr)
337+
if(
338+
symbol_expr.id() == ID_member &&
339+
to_member_expr(symbol_expr).op().id() == ID_dereference &&
340+
to_dereference_expr(to_member_expr(symbol_expr).op()).pointer() ==
341+
cpp_scopes.current_scope().this_expr)
331342
{
332343
if(is_reference(symbol_expr.type()))
333344
{

src/cpp/cpp_typecheck_compound_type.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,10 @@ void cpp_typecheckt::typecheck_member_function(
12781278
}
12791279

12801280
if(value.id() == ID_cpp_not_typechecked && value.has_operands())
1281-
move_member_initializers(initializers, type, value.op0());
1281+
{
1282+
move_member_initializers(
1283+
initializers, type, to_multi_ary_expr(value).op0());
1284+
}
12821285
else
12831286
move_member_initializers(initializers, type, value);
12841287

src/cpp/cpp_typecheck_constructor.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ static void copy_member(
6969
op1.add_source_location()=source_location;
7070

7171
side_effect_exprt assign(ID_assign, typet(), source_location);
72-
assign.copy_to_operands(op0.as_expr());
73-
assign.op0().add_source_location() = source_location;
74-
assign.copy_to_operands(op1);
72+
assign.copy_to_operands(op0.as_expr(), op1);
73+
to_binary_expr(assign).op0().add_source_location() = source_location;
7574

7675
code_expressiont code(assign);
7776
code.add_source_location() = source_location;
@@ -105,10 +104,10 @@ static void copy_array(
105104
side_effect_exprt assign(ID_assign, typet(), source_location);
106105

107106
assign.copy_to_operands(index_exprt(array.as_expr(), constant));
108-
assign.op0().add_source_location() = source_location;
109-
110107
assign.copy_to_operands(index_exprt(member, constant));
111-
assign.op1().add_source_location() = source_location;
108+
109+
to_binary_expr(assign).op0().add_source_location() = source_location;
110+
to_binary_expr(assign).op1().add_source_location() = source_location;
112111

113112
code_expressiont code(assign);
114113
code.add_source_location() = source_location;
@@ -295,7 +294,7 @@ void cpp_typecheckt::default_assignop(
295294
cpctor.operands().push_back(exprt(ID_cpp_declarator));
296295
cpctor.add_source_location()=source_location;
297296

298-
cpp_declaratort &declarator=(cpp_declaratort&) cpctor.op0();
297+
cpp_declaratort &declarator = static_cast<cpp_declaratort &>(cpctor.op0());
299298
declarator.add_source_location()=source_location;
300299

301300
cpp_namet &declarator_name=declarator.name();

src/cpp/cpp_typecheck_conversions.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ bool cpp_typecheckt::user_defined_conversion_sequence(
894894

895895
// simplify address
896896
if(expr.id()==ID_dereference)
897-
address=expr.op0();
897+
address = to_dereference_expr(expr).pointer();
898898

899899
pointer_typet ptr_sub=pointer_type(type);
900900
c_qualifierst qual_from;
@@ -1333,8 +1333,9 @@ bool cpp_typecheckt::reference_binding(
13331333
reference_compatible(returned_value, type, rank))
13341334
{
13351335
// returned values are lvalues in case of references only
1336-
assert(returned_value.id()==ID_dereference &&
1337-
is_reference(returned_value.op0().type()));
1336+
DATA_INVARIANT(
1337+
is_reference(to_dereference_expr(returned_value).op().type()),
1338+
"the returned value must be pointer to reference");
13381339

13391340
new_expr=returned_value.op0();
13401341

@@ -1484,7 +1485,7 @@ void cpp_typecheckt::implicit_typecast(exprt &expr, const typet &type)
14841485
e.id() == ID_initializer_list && cpp_is_pod(type) &&
14851486
e.operands().size() == 1)
14861487
{
1487-
e = expr.op0();
1488+
e = to_unary_expr(expr).op();
14881489
}
14891490

14901491
if(!implicit_conversion_sequence(e, type, expr))
@@ -1696,7 +1697,7 @@ bool cpp_typecheckt::dynamic_typecast(
16961697
if(type.id()==ID_pointer)
16971698
{
16981699
if(e.id()==ID_dereference && e.get_bool(ID_C_implicit))
1699-
e=expr.op0();
1700+
e = to_dereference_expr(expr).pointer();
17001701

17011702
if(e.type().id()==ID_pointer &&
17021703
cast_away_constness(e.type(), type))
@@ -1749,7 +1750,7 @@ bool cpp_typecheckt::reinterpret_typecast(
17491750
if(check_constantness && type.id()==ID_pointer)
17501751
{
17511752
if(e.id()==ID_dereference && e.get_bool(ID_C_implicit))
1752-
e=expr.op0();
1753+
e = to_dereference_expr(expr).pointer();
17531754

17541755
if(e.type().id()==ID_pointer &&
17551756
cast_away_constness(e.type(), type))
@@ -1845,7 +1846,7 @@ bool cpp_typecheckt::static_typecast(
18451846
if(check_constantness && type.id()==ID_pointer)
18461847
{
18471848
if(e.id()==ID_dereference && e.get_bool(ID_C_implicit))
1848-
e=expr.op0();
1849+
e = to_dereference_expr(expr).pointer();
18491850

18501851
if(e.type().id()==ID_pointer &&
18511852
cast_away_constness(e.type(), type))
@@ -1884,8 +1885,8 @@ bool cpp_typecheckt::static_typecast(
18841885
{
18851886
if(e.id()==ID_dereference)
18861887
{
1887-
make_ptr_typecast(e.op0(), type);
1888-
new_expr.swap(e.op0());
1888+
make_ptr_typecast(to_dereference_expr(e).pointer(), type);
1889+
new_expr.swap(to_dereference_expr(e).pointer());
18891890
return true;
18901891
}
18911892

src/cpp/cpp_typecheck_function.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ void cpp_typecheckt::convert_function(symbolt &symbol)
123123
PRECONDITION(symbol.value.get(ID_statement) == ID_block);
124124

125125
if(
126-
!symbol.value.has_operands() || !symbol.value.op0().has_operands() ||
127-
symbol.value.op0().op0().id() != ID_already_typechecked)
126+
!symbol.value.has_operands() ||
127+
!to_multi_ary_expr(symbol.value).op0().has_operands() ||
128+
to_multi_ary_expr(symbol.value).op0().op0().id() !=
129+
ID_already_typechecked)
128130
{
129131
symbol.value.copy_to_operands(
130132
dtor(msymb, to_symbol_expr(function_scope.this_expr)));

src/cpp/expr2cpp.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ std::string expr2cppt::convert_code_cpp_delete(
404404
return convert_norep(src, precedence);
405405
}
406406

407-
std::string tmp=convert(src.op0());
407+
std::string tmp = convert(to_unary_expr(src).op());
408408

409409
dest+=tmp+";\n";
410410

@@ -476,16 +476,17 @@ std::string expr2cppt::convert_code(
476476

477477
std::string expr2cppt::convert_extractbit(const exprt &src)
478478
{
479-
assert(src.operands().size()==2);
480-
return convert(src.op0())+"["+convert(src.op1())+"]";
479+
const auto &extractbit_expr = to_extractbit_expr(src);
480+
return convert(extractbit_expr.op0()) + "[" + convert(extractbit_expr.op1()) +
481+
"]";
481482
}
482483

483484
std::string expr2cppt::convert_extractbits(const exprt &src)
484485
{
485-
assert(src.operands().size()==3);
486-
return
487-
convert(src.op0())+".range("+convert(src.op1())+ ","+
488-
convert(src.op2())+")";
486+
const auto &extractbits_expr = to_extractbits_expr(src);
487+
return convert(extractbits_expr.src()) + ".range(" +
488+
convert(extractbits_expr.upper()) + "," +
489+
convert(extractbits_expr.lower()) + ")";
489490
}
490491

491492
std::string expr2cpp(const exprt &expr, const namespacet &ns)

0 commit comments

Comments
 (0)