Skip to content

Commit ae57b25

Browse files
authored
Merge pull request #3005 from tautschnig/bottom-up
Construct expression bottom-up rather than top-down
2 parents 8d396f3 + fbaf241 commit ae57b25

File tree

1 file changed

+40
-86
lines changed

1 file changed

+40
-86
lines changed

src/cpp/cpp_typecheck_constructor.cpp

Lines changed: 40 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,24 @@ static void copy_parent(
2828
const irep_idt &arg_name,
2929
exprt &block)
3030
{
31-
block.operands().push_back(codet());
32-
33-
codet &code=to_code(block.operands().back());
34-
code.add_source_location()=source_location;
35-
36-
code.set_statement(ID_assign);
37-
code.operands().push_back(exprt(ID_dereference));
38-
39-
code.op0().operands().push_back(exprt("explicit-typecast"));
40-
41-
exprt &op0=code.op0().op0();
42-
43-
op0.operands().push_back(exprt("cpp-this"));
44-
op0.type()=
45-
pointer_type(cpp_namet(parent_base_name, source_location).as_type());
31+
exprt op0(
32+
"explicit-typecast",
33+
pointer_type(cpp_namet(parent_base_name, source_location).as_type()));
34+
op0.copy_to_operands(exprt("cpp-this"));
4635
op0.add_source_location()=source_location;
4736

48-
code.operands().push_back(exprt("explicit-typecast"));
49-
exprt &op1=code.op1();
50-
51-
op1.type() =
52-
pointer_type(cpp_namet(parent_base_name, source_location).as_type());
37+
exprt op1(
38+
"explicit-typecast",
39+
pointer_type(cpp_namet(parent_base_name, source_location).as_type()));
5340
op1.type().set(ID_C_reference, true);
5441
op1.type().subtype().set(ID_C_constant, true);
55-
56-
op1.operands().push_back(exprt(ID_cpp_name));
57-
op1.op0().get_sub().push_back(irept(ID_name));
58-
op1.op0().get_sub().back().set(ID_identifier, arg_name);
59-
op1.op0().get_sub().back().set(ID_C_source_location, source_location);
42+
op1.get_sub().push_back(cpp_namet(arg_name, source_location));
6043
op1.add_source_location()=source_location;
44+
45+
code_assignt code(dereference_exprt(op0), op1);
46+
code.add_source_location() = source_location;
47+
48+
block.operands().push_back(code);
6149
}
6250

6351
/// \param member_base_name: name of a member
@@ -69,39 +57,22 @@ static void copy_member(
6957
const irep_idt &arg_name,
7058
exprt &block)
7159
{
72-
block.operands().push_back(exprt(ID_code));
73-
exprt &code=block.operands().back();
60+
cpp_namet op0(member_base_name, source_location);
7461

75-
code.set(ID_statement, ID_expression);
76-
code.add(ID_type)=typet(ID_code);
77-
code.operands().push_back(exprt(ID_side_effect));
78-
code.op0().set(ID_statement, ID_assign);
79-
code.op0().operands().push_back(exprt(ID_cpp_name));
80-
code.add_source_location()=source_location;
81-
82-
exprt &op0=code.op0().op0();
83-
op0.add_source_location()=source_location;
84-
85-
op0.get_sub().push_back(irept(ID_name));
86-
op0.get_sub().back().set(ID_identifier, member_base_name);
87-
op0.get_sub().back().set(ID_C_source_location, source_location);
88-
89-
code.op0().operands().push_back(exprt(ID_member));
62+
exprt op1(ID_member);
63+
op1.add(ID_component_cpp_name, cpp_namet(member_base_name, source_location));
64+
op1.copy_to_operands(cpp_namet(arg_name, source_location).as_expr());
65+
op1.add_source_location()=source_location;
9066

91-
exprt &op1=code.op0().op1();
67+
side_effect_exprt assign(ID_assign, typet(), source_location);
68+
assign.copy_to_operands(op0.as_expr());
69+
assign.op0().add_source_location() = source_location;
70+
assign.copy_to_operands(op1);
9271

93-
op1.add(ID_component_cpp_name).id(ID_cpp_name);
94-
op1.add(ID_component_cpp_name).get_sub().push_back(irept(ID_name));
95-
op1.add(ID_component_cpp_name).get_sub().back().set(
96-
ID_identifier, member_base_name);
97-
op1.add(ID_component_cpp_name).get_sub().back().set(
98-
ID_C_source_location, source_location);
72+
code_expressiont code(assign);
73+
code.add_source_location() = source_location;
9974

100-
op1.operands().push_back(exprt(ID_cpp_name));
101-
op1.op0().get_sub().push_back(irept(ID_name));
102-
op1.op0().get_sub().back().set(ID_identifier, arg_name);
103-
op1.op0().get_sub().back().set(ID_C_source_location, source_location);
104-
op1.add_source_location()=source_location;
75+
block.operands().push_back(code);
10576
}
10677

10778
/// \param member_base_name: name of array member
@@ -116,44 +87,27 @@ static void copy_array(
11687
exprt &block)
11788
{
11889
// Build the index expression
119-
exprt constant=from_integer(i, index_type());
90+
const exprt constant = from_integer(i, index_type());
12091

121-
block.operands().push_back(exprt(ID_code));
122-
exprt &code=block.operands().back();
123-
code.add_source_location()=source_location;
124-
125-
code.set(ID_statement, ID_expression);
126-
code.add(ID_type)=typet(ID_code);
127-
code.operands().push_back(exprt(ID_side_effect));
128-
code.op0().set(ID_statement, ID_assign);
129-
code.op0().operands().push_back(exprt(ID_index));
130-
exprt &op0=code.op0().op0();
131-
op0.operands().push_back(exprt(ID_cpp_name));
132-
op0.add_source_location()=source_location;
92+
const cpp_namet array(member_base_name, source_location);
13393

134-
op0.op0().get_sub().push_back(irept(ID_name));
135-
op0.op0().get_sub().back().set(ID_identifier, member_base_name);
136-
op0.op0().get_sub().back().set(ID_C_source_location, source_location);
137-
op0.copy_to_operands(constant);
94+
exprt member(ID_member);
95+
member.add(
96+
ID_component_cpp_name, cpp_namet(member_base_name, source_location));
97+
member.copy_to_operands(cpp_namet(arg_name, source_location).as_expr());
13898

139-
code.op0().operands().push_back(exprt(ID_index));
99+
side_effect_exprt assign(ID_assign, typet(), source_location);
140100

141-
exprt &op1=code.op0().op1();
142-
op1.operands().push_back(exprt(ID_member));
143-
op1.op0().add(ID_component_cpp_name).id(ID_cpp_name);
144-
op1.op0().add(ID_component_cpp_name).get_sub().push_back(irept(ID_name));
145-
op1.op0().add(ID_component_cpp_name).get_sub().back().set(
146-
ID_identifier, member_base_name);
147-
op1.op0().add(ID_component_cpp_name).get_sub().back().set(
148-
ID_C_source_location, source_location);
101+
assign.copy_to_operands(index_exprt(array.as_expr(), constant));
102+
assign.op0().add_source_location() = source_location;
149103

150-
op1.op0().operands().push_back(exprt(ID_cpp_name));
151-
op1.op0().op0().get_sub().push_back(irept(ID_name));
152-
op1.op0().op0().get_sub().back().set(ID_identifier, arg_name);
153-
op1.op0().op0().get_sub().back().set(ID_C_source_location, source_location);
154-
op1.copy_to_operands(constant);
104+
assign.copy_to_operands(index_exprt(member, constant));
105+
assign.op1().add_source_location() = source_location;
155106

156-
op1.add_source_location()=source_location;
107+
code_expressiont code(assign);
108+
code.add_source_location() = source_location;
109+
110+
block.operands().push_back(code);
157111
}
158112

159113
/// Generate code for implicit default constructors

0 commit comments

Comments
 (0)