Skip to content

Commit 7a70b13

Browse files
author
Daniel Kroening
authored
Merge pull request #4984 from diffblue/cpp-opX
fix exprt::opX() accesses in cpp/
2 parents 7f1691b + 6457a5e commit 7a70b13

13 files changed

+203
-176
lines changed

src/cpp/cpp_constructor.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,10 @@ 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+
auto &statement_expr = to_code_expression(to_code(initializer));
239+
240+
side_effect_expr_function_callt &func_ini =
241+
to_side_effect_expr_function_call(statement_expr.expression());
240242

241243
exprt &tmp_this=func_ini.arguments().front();
242244
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: 30 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,7 @@ 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+
value = to_code_decl(to_code(to_unary_expr(decl).op())).symbol();
196202

197203
c_typecheck_baset::typecheck_switch(code);
198204

@@ -287,11 +293,11 @@ void cpp_typecheckt::typecheck_member_initializer(codet &code)
287293
// a reference member
288294
if(
289295
symbol_expr.id() == ID_dereference &&
290-
symbol_expr.op0().id() == ID_member &&
296+
to_dereference_expr(symbol_expr).pointer().id() == ID_member &&
291297
symbol_expr.get_bool(ID_C_implicit))
292298
{
293299
// treat references as normal pointers
294-
exprt tmp = symbol_expr.op0();
300+
exprt tmp = to_dereference_expr(symbol_expr).pointer();
295301
symbol_expr.swap(tmp);
296302
}
297303

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

317323
if(
318324
symbol_expr.id() == ID_dereference &&
319-
symbol_expr.op0().id() == ID_member &&
325+
to_dereference_expr(symbol_expr).pointer().id() == ID_member &&
320326
symbol_expr.get_bool(ID_C_implicit))
321327
{
322328
// treat references as normal pointers
323-
exprt tmp = symbol_expr.op0();
329+
exprt tmp = to_dereference_expr(symbol_expr).pointer();
324330
symbol_expr.swap(tmp);
325331
}
326332
}
327333

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)
334+
if(
335+
symbol_expr.id() == ID_member &&
336+
to_member_expr(symbol_expr).op().id() == ID_dereference &&
337+
to_dereference_expr(to_member_expr(symbol_expr).op()).pointer() ==
338+
cpp_scopes.current_scope().this_expr)
331339
{
332340
if(is_reference(symbol_expr.type()))
333341
{

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: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ static void copy_member(
6868
op1.copy_to_operands(cpp_namet(arg_name, source_location).as_expr());
6969
op1.add_source_location()=source_location;
7070

71-
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);
71+
side_effect_expr_assignt assign(op0.as_expr(), op1, typet(), source_location);
72+
assign.lhs().add_source_location() = source_location;
7573

7674
code_expressiont code(assign);
7775
code.add_source_location() = source_location;
@@ -102,13 +100,14 @@ static void copy_array(
102100
ID_component_cpp_name, cpp_namet(member_base_name, source_location));
103101
member.copy_to_operands(cpp_namet(arg_name, source_location).as_expr());
104102

105-
side_effect_exprt assign(ID_assign, typet(), source_location);
103+
side_effect_expr_assignt assign(
104+
index_exprt(array.as_expr(), constant),
105+
index_exprt(member, constant),
106+
typet(),
107+
source_location);
106108

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

113112
code_expressiont code(assign);
114113
code.add_source_location() = source_location;
@@ -186,7 +185,8 @@ void cpp_typecheckt::default_cpctor(
186185
irept &initializers=decl0.add(ID_member_initializers);
187186
initializers.id(ID_member_initializers);
188187

189-
cpp_declaratort &declarator=static_cast<cpp_declaratort &>(cpctor.op0());
188+
cpp_declaratort &declarator =
189+
static_cast<cpp_declaratort &>(to_multi_ary_expr(cpctor).op0());
190190
exprt &block=declarator.value();
191191

192192
// First, we need to call the parent copy constructors
@@ -295,7 +295,8 @@ void cpp_typecheckt::default_assignop(
295295
cpctor.operands().push_back(exprt(ID_cpp_declarator));
296296
cpctor.add_source_location()=source_location;
297297

298-
cpp_declaratort &declarator=(cpp_declaratort&) cpctor.op0();
298+
cpp_declaratort &declarator =
299+
static_cast<cpp_declaratort &>(to_multi_ary_expr(cpctor).op0());
299300
declarator.add_source_location()=source_location;
300301

301302
cpp_namet &declarator_name=declarator.name();

src/cpp/cpp_typecheck_conversions.cpp

Lines changed: 11 additions & 10 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,10 +1333,11 @@ 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

1339-
new_expr=returned_value.op0();
1340+
new_expr = to_multi_ary_expr(returned_value).op0();
13401341

13411342
if(returned_value.type() != type.subtype())
13421343
{
@@ -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

0 commit comments

Comments
 (0)