Skip to content

Commit f77b1bb

Browse files
author
pkesseli
committed
Updated handling of nested structs.
1 parent 1c3728b commit f77b1bb

File tree

4 files changed

+59
-77
lines changed

4 files changed

+59
-77
lines changed
-23 Bytes
Binary file not shown.

regression/test_gen/java1/TestGenTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ public C(int c_field_0, B c_field_1)
3333

3434
public class TestGenTest
3535
{
36-
public void f(B param0, C param1, int param2)
36+
public static void f(B param0, C param1, int param2)
3737
{
38-
if (param0 == null || param1 == null) return;
39-
4038
int a_field_0=param0.a_field_0;
4139
int a_field_1=param0.a_field_1;
4240
int b_field_0=param0.b_field_0;

src/test_gen/java_test_case_generator.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ inputst generate_inputs(const symbol_tablet &st, const goto_functionst &gf,
3030
{
3131
const irep_idt &id=to_symbol_expr(lhs).get_identifier();
3232
if (st.has_symbol(id) && !is_meta(id))
33-
result.insert(std::make_pair(id, step.full_lhs_value));
33+
{
34+
const exprt &value=step.full_lhs_value;
35+
result.insert(std::make_pair(id, value));
36+
}
3437
}
3538
}
3639
}

src/test_gen/java_test_source_factory.cpp

Lines changed: 54 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <test_gen/java_test_source_factory.h>
1313

1414
#define INDENT_SPACES " "
15-
#define ID_value_alias "value-alias"
15+
#define JAVA_NS_PREFIX_LEN 6u
1616

1717
namespace
1818
{
@@ -33,49 +33,46 @@ void add_test_class_name(std::string &result, const std::string &func_name)
3333
result+="() {\n";
3434
}
3535

36-
void add_decl_with_init_prefix(std::string &result, const symbol_tablet &st,
37-
const typet &type)
38-
{
39-
const namespacet ns(st);
40-
result+=type2java(type, ns);
41-
result+=' ';
42-
}
43-
44-
void add_decl_with_init_prefix(std::string &result, const symbol_tablet &st,
45-
const symbolt &symbol)
46-
{
47-
add_decl_with_init_prefix(result, st, symbol.type);
48-
}
49-
5036
void add_symbol(std::string &result, const symbolt &s)
5137
{
5238
// XXX: Should be expr2java(...) once functional.
5339
const irep_idt &n=s.pretty_name.empty() ? s.base_name : s.pretty_name;
5440
result+=id2string(n);
5541
}
5642

57-
std::string get_symbol(const symbolt &symbol)
43+
void add_qualified_class_name(std::string &result, const namespacet &ns,
44+
const typet &type)
5845
{
59-
std::string result;
60-
add_symbol(result, symbol);
61-
return result;
46+
if (ID_symbol == type.id())
47+
{
48+
const std::string &id=id2string(to_symbol_type(type).get_identifier());
49+
assert(id.size() >= JAVA_NS_PREFIX_LEN);
50+
result+=id.substr(JAVA_NS_PREFIX_LEN);
51+
} else result+=id2string(to_struct_type(type).get_tag());
6252
}
6353

64-
void addQualifiedClassName(std::string &result, const namespacet &ns,
65-
const typet &type)
54+
void add_decl_with_init_prefix(std::string &result, const symbol_tablet &st,
55+
const symbolt &symbol)
6656
{
67-
result+=type2java(type, ns);
57+
const namespacet ns(st);
58+
if (ID_pointer != symbol.type.id()) result+=type2java(symbol.type, ns);
59+
else add_qualified_class_name(result, ns, symbol.type.subtype());
60+
result+=' ';
6861
}
6962

7063
void add_value(std::string &result, const symbol_tablet &st,
71-
const std::string &this_name, const struct_exprt &value);
64+
const struct_exprt &value, const std::string &this_name);
7265

7366
void add_value(std::string &result, const symbol_tablet &st, const exprt &value,
7467
const std::string var_name="")
7568
{
7669
const namespacet ns(st);
77-
if (ID_struct != value.id()) result+=expr2java(value, ns);
78-
else add_value(result, st, var_name, to_struct_expr(value));
70+
const irep_idt &id=value.id();
71+
if (ID_address_of == id) add_value(result, st,
72+
to_address_of_expr(value).object());
73+
else if (ID_struct == id) add_value(result, st, to_struct_expr(value),
74+
var_name);
75+
else result+=expr2java(value, ns);
7976
}
8077

8178
class member_factoryt
@@ -97,79 +94,64 @@ class member_factoryt
9794
void operator()(const exprt &value)
9895
{
9996
assert(comp_index < comps.size());
100-
result+="Reflector.setInstanceField(";
101-
result+=this_name;
102-
const struct_typet::componentt &comp=comps[comp_index++];
103-
result+=",\"";
104-
result+=id2string(comp.get_pretty_name());
105-
add_value(result, st, value);
106-
result+=");\n";
97+
if (ID_struct == value.id())
98+
{
99+
member_factoryt mem_fac(result, st, this_name, value.type());
100+
const struct_exprt::operandst &ops=value.operands();
101+
std::for_each(ops.begin(), ops.end(), mem_fac);
102+
if (comp_index < comps.size() - 1) result+=";\n";
103+
} else
104+
{
105+
const struct_typet::componentt &comp=comps[comp_index];
106+
if (ID_symbol != comp.type().id())
107+
{
108+
indent(result, 2u)+="Reflector.setInstanceField(";
109+
result+=this_name;
110+
result+=",\"";
111+
result+=id2string(comp.get_pretty_name());
112+
result+="\",";
113+
add_value(result, st, value);
114+
result+=")";
115+
if (comp_index < comps.size() - 1) result+=";\n";
116+
}
117+
}
118+
++comp_index;
107119
}
108120
};
109121

110122
void add_value(std::string &result, const symbol_tablet &st,
111-
const std::string &this_name, const struct_exprt &value)
123+
const struct_exprt &value, const std::string &this_name)
112124
{
113-
const std::string &alias=value.get_string(ID_value_alias);
114-
if (!alias.empty())
115-
{
116-
result+=alias;
117-
return;
118-
}
119125
const namespacet ns(st);
120126
result+='(';
121127
const typet &type=value.type();
122-
result+=type2java(value.type(), ns);
128+
add_qualified_class_name(result, ns, type);
123129
result+=") Reflector.forceInstance(\"";
124-
addQualifiedClassName(result, ns, type);
130+
add_qualified_class_name(result, ns, type);
125131
result+="\");\n";
126132
member_factoryt mem_fac(result, st, this_name, type);
127133
const struct_exprt::operandst &ops=value.operands();
128134
std::for_each(ops.begin(), ops.end(), mem_fac);
129135
}
130136

131137
void add_assign_value(std::string &result, const symbol_tablet &st,
132-
const std::string &symbol, const exprt &value)
138+
const symbolt &symbol, const exprt &value)
133139
{
134-
result+=symbol;
140+
std::string symbol_name;
141+
add_symbol(symbol_name, symbol);
142+
result+=symbol_name;
135143
result+='=';
136-
add_value(result, st, value);
144+
add_value(result, st, value, symbol_name);
137145
result+=";\n";
138146
}
139147

140-
void add_assign_value(std::string &result, const symbol_tablet &st,
141-
const symbolt &symbol, const exprt &value)
142-
{
143-
add_assign_value(result, st, get_symbol(symbol), value);
144-
}
145-
146-
void add_nested_objects(std::string &result, const symbol_tablet &st,
147-
const std::string &this_name, exprt &value)
148-
{
149-
if (ID_struct != value.id()) return;
150-
size_t i=0;
151-
struct_exprt::operandst &ops=value.operands();
152-
for (struct_exprt::operandst::value_type &op : ops)
153-
{
154-
if (ID_struct != op.id()) continue;
155-
std::string name(this_name);
156-
name+='_';
157-
name+=std::to_string(i);
158-
add_nested_objects(result, st, this_name, op);
159-
add_decl_with_init_prefix(result, st, op.type());
160-
add_assign_value(result, st, name, op);
161-
op.set(ID_value_alias, name);
162-
}
163-
}
164-
165148
void add_global_state_assignments(std::string &result, const symbol_tablet &st,
166149
inputst &inputs)
167150
{
168151
for (inputst::value_type &input : inputs)
169152
{
170153
const symbolt &symbol=st.lookup(input.first);
171154
if (!symbol.is_static_lifetime) continue;
172-
add_nested_objects(result, st, get_symbol(symbol), input.second);
173155
add_assign_value(indent(result, 2u), st, symbol, input.second);
174156
}
175157
}
@@ -194,7 +176,6 @@ void add_func_call_parameters(std::string &result, const symbol_tablet &st,
194176
const symbolt &symbol=st.lookup(param);
195177
const inputst::iterator value=inputs.find(param);
196178
assert(inputs.end() != value);
197-
add_nested_objects(result, st, get_symbol(symbol), value->second);
198179
add_decl_with_init_prefix(indent(result, 2u), st, symbol);
199180
add_assign_value(result, st, symbol, value->second);
200181
}
@@ -203,7 +184,7 @@ void add_func_call_parameters(std::string &result, const symbol_tablet &st,
203184
std::string &add_func_call(std::string &result, const symbol_tablet &st,
204185
const irep_idt &func_id)
205186
{
206-
// XXX: Should be expr2java(...) once functional.
187+
// XXX: Should be expr2java(...) once functional.
207188
const symbolt &s=st.lookup(func_id);
208189
const std::string func_name_with_brackets(id2string(s.pretty_name));
209190
const size_t sz=func_name_with_brackets.size();
@@ -216,7 +197,7 @@ std::string &add_func_call(std::string &result, const symbol_tablet &st,
216197
add_symbol(result, st.lookup(param));
217198
result+=',';
218199
}
219-
(*result.rbegin())=')';
200+
*result.rbegin()=')';
220201
result+=";\n";
221202
indent(result)+="}\n";
222203
return result+="}\n";

0 commit comments

Comments
 (0)