-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathexpr2java.cpp
465 lines (387 loc) · 10.8 KB
/
expr2java.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*******************************************************************\
Module:
Author: Daniel Kroening, [email protected]
\*******************************************************************/
#include "expr2java.h"
#include <util/namespace.h>
#include <util/std_expr.h>
#include <util/unicode.h>
#include <util/arith_tools.h>
#include <util/ieee_float.h>
#include <ansi-c/c_misc.h>
#include <ansi-c/expr2c_class.h>
#include "java_expr.h"
#include "java_qualifiers.h"
#include "java_string_literal_expr.h"
#include "java_types.h"
std::string expr2javat::convert(const typet &src)
{
return convert_rec(src, java_qualifierst(ns), "");
}
std::string expr2javat::convert(const exprt &src)
{
return expr2ct::convert(src);
}
std::string expr2javat::convert_code_function_call(
const code_function_callt &src,
unsigned indent)
{
if(src.operands().size()!=3)
{
unsigned precedence;
return convert_norep(src, precedence);
}
std::string dest=indent_str(indent);
if(src.lhs().is_not_nil())
{
unsigned p;
std::string lhs_str=convert_with_precedence(src.lhs(), p);
dest+=lhs_str;
dest+='=';
}
const java_method_typet &method_type =
to_java_method_type(src.function().type());
bool has_this = method_type.has_this() && !src.arguments().empty();
if(has_this)
{
unsigned p;
std::string this_str=convert_with_precedence(src.arguments()[0], p);
dest+=this_str;
dest+=" . "; // extra spaces for readability
}
{
unsigned p;
std::string function_str=convert_with_precedence(src.function(), p);
dest+=function_str;
}
dest+='(';
const exprt::operandst &arguments=src.arguments();
bool first=true;
forall_expr(it, arguments)
{
if(has_this && it==arguments.begin())
{
}
else
{
unsigned p;
std::string arg_str=convert_with_precedence(*it, p);
if(first)
first=false;
else
dest+=", ";
dest+=arg_str;
}
}
dest+=");";
return dest;
}
std::string expr2javat::convert_struct(
const exprt &src,
unsigned &precedence)
{
const typet &full_type = src.type().id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(src.type()))
: src.type();
if(full_type.id()!=ID_struct)
return convert_norep(src, precedence);
const struct_typet &struct_type=to_struct_type(full_type);
std::string dest="{ ";
const struct_typet::componentst &components=
struct_type.components();
DATA_INVARIANT(
components.size() == src.operands().size(),
"inconsistent number of components");
exprt::operandst::const_iterator o_it=src.operands().begin();
bool first=true;
size_t last_size=0;
for(const auto &c : components)
{
DATA_INVARIANT(
c.type().id() != ID_code, "struct member must not be of code type");
std::string tmp = convert(*o_it);
std::string sep;
if(first)
first = false;
else
{
if(last_size + 40 < dest.size())
{
sep = ",\n ";
last_size = dest.size();
}
else
sep = ", ";
}
dest += sep;
dest += '.';
irep_idt field_name = c.get_pretty_name();
if(field_name.empty())
field_name = c.get_name();
dest += id2string(field_name);
dest += '=';
dest += tmp;
o_it++;
}
dest+=" }";
return dest;
}
std::string expr2javat::convert_constant(
const constant_exprt &src,
unsigned &precedence)
{
if(src.type().id()==ID_c_bool)
{
if(!src.is_zero())
return "true";
else
return "false";
}
else if(src.is_boolean())
{
if(src.is_true())
return "true";
else if(src.is_false())
return "false";
}
else if(src.type().id()==ID_pointer)
{
// Java writes 'null' for the null reference
if(src.is_zero())
return "null";
}
else if(src.type()==java_char_type())
{
std::string dest;
dest.reserve(char_representation_length);
const char16_t int_value = numeric_cast_v<char16_t>(src);
// Character literals in Java have type 'char', thus no cast is needed.
// This is different from C, where charater literals have type 'int'.
dest += '\'' + utf16_native_endian_to_java(int_value) + '\'';
return dest;
}
else if(src.type()==java_byte_type())
{
// No byte-literals in Java, so just cast:
const mp_integer int_value = numeric_cast_v<mp_integer>(src);
std::string dest="(byte)";
dest+=integer2string(int_value);
return dest;
}
else if(src.type()==java_short_type())
{
// No short-literals in Java, so just cast:
const mp_integer int_value = numeric_cast_v<mp_integer>(src);
std::string dest="(short)";
dest+=integer2string(int_value);
return dest;
}
else if(src.type()==java_long_type())
{
// long integer literals must have 'L' at the end
const mp_integer int_value = numeric_cast_v<mp_integer>(src);
std::string dest=integer2string(int_value);
dest+='L';
return dest;
}
else if((src.type()==java_float_type()) ||
(src.type()==java_double_type()))
{
// This converts NaNs to the canonical NaN
const ieee_float_valuet ieee_repr(to_constant_expr(src));
if(ieee_repr.is_double())
return floating_point_to_java_string(ieee_repr.to_double());
return floating_point_to_java_string(ieee_repr.to_float());
}
return expr2ct::convert_constant(src, precedence);
}
std::string expr2javat::convert_rec(
const typet &src,
const c_qualifierst &qualifiers,
const std::string &declarator)
{
std::unique_ptr<c_qualifierst> clone = qualifiers.clone();
c_qualifierst &new_qualifiers = *clone;
new_qualifiers.read(src);
const std::string d = declarator.empty() ? declarator : (" " + declarator);
const std::string q=
new_qualifiers.as_string();
if(src==java_int_type())
return q+"int"+d;
else if(src==java_long_type())
return q+"long"+d;
else if(src==java_short_type())
return q+"short"+d;
else if(src==java_byte_type())
return q+"byte"+d;
else if(src==java_char_type())
return q+"char"+d;
else if(src==java_float_type())
return q+"float"+d;
else if(src==java_double_type())
return q+"double"+d;
else if(src==java_boolean_type())
return q+"boolean"+d;
else if(src==java_byte_type())
return q+"byte"+d;
else if(src.id()==ID_code)
{
const java_method_typet &method_type = to_java_method_type(src);
// Java doesn't really have syntax for function types,
// so we make one up, loosely inspired by the syntax
// of lambda expressions.
std::string dest;
dest+='(';
const java_method_typet::parameterst ¶meters = method_type.parameters();
for(java_method_typet::parameterst::const_iterator it = parameters.begin();
it != parameters.end();
it++)
{
if(it!=parameters.begin())
dest+=", ";
dest+=convert(it->type());
}
if(method_type.has_ellipsis())
{
if(!parameters.empty())
dest+=", ";
dest+="...";
}
dest+=')';
const typet &return_type = method_type.return_type();
dest+=" -> "+convert(return_type);
return q + dest;
}
else
return expr2ct::convert_rec(src, qualifiers, declarator);
}
std::string expr2javat::convert_java_this()
{
return id2string(ID_this);
}
std::string expr2javat::convert_java_instanceof(const exprt &src)
{
const auto &instanceof_expr = to_java_instanceof_expr(src);
return convert(instanceof_expr.tested_expr()) + " instanceof " +
convert(instanceof_expr.target_type());
}
std::string expr2javat::convert_code_java_new(const exprt &src, unsigned indent)
{
return indent_str(indent) + convert_java_new(src) + ";\n";
}
std::string expr2javat::convert_java_new(const exprt &src)
{
std::string dest;
if(src.get(ID_statement)==ID_java_new_array ||
src.get(ID_statement)==ID_java_new_array_data)
{
dest="new";
std::string tmp_size=
convert(static_cast<const exprt &>(src.find(ID_size)));
dest+=' ';
dest += convert(to_pointer_type(src.type()).base_type());
dest+='[';
dest+=tmp_size;
dest+=']';
}
else
dest = "new " + convert(to_pointer_type(src.type()).base_type());
return dest;
}
std::string expr2javat::convert_code_java_delete(
const exprt &src,
unsigned indent)
{
std::string dest=indent_str(indent)+"delete ";
if(src.operands().size()!=1)
{
unsigned precedence;
return convert_norep(src, precedence);
}
std::string tmp = convert(to_unary_expr(src).op());
dest+=tmp+";\n";
return dest;
}
std::string expr2javat::convert_with_precedence(
const exprt &src,
unsigned &precedence)
{
if(src.id()=="java-this")
{
precedence = 15;
return convert_java_this();
}
if(src.id()==ID_java_instanceof)
{
precedence = 15;
return convert_java_instanceof(src);
}
else if(src.id()==ID_side_effect &&
(src.get(ID_statement)==ID_java_new ||
src.get(ID_statement)==ID_java_new_array ||
src.get(ID_statement)==ID_java_new_array_data))
{
precedence = 15;
return convert_java_new(src);
}
else if(src.id()==ID_side_effect &&
src.get(ID_statement)==ID_throw)
{
precedence = 16;
return convert_function(src, "throw");
}
else if(src.id()==ID_code &&
to_code(src).get_statement()==ID_exception_landingpad)
{
const exprt &catch_expr=
to_code_landingpad(to_code(src)).catch_expr();
return "catch_landingpad("+
convert(catch_expr.type())+
' '+
convert(catch_expr)+
')';
}
else if(src.id()==ID_unassigned)
return "?";
else if(src.id()=="pod_constructor")
return "pod_constructor";
else if(src.id()==ID_virtual_function)
{
const class_method_descriptor_exprt &virtual_function =
to_class_method_descriptor_expr(src);
return "CLASS_METHOD_DESCRIPTOR(" + id2string(virtual_function.class_id()) +
"." + id2string(virtual_function.mangled_method_name()) + ")";
}
else if(
const auto &literal = expr_try_dynamic_cast<java_string_literal_exprt>(src))
{
return '"' + MetaString(id2string(literal->value())) + '"';
}
else if(src.is_constant())
return convert_constant(to_constant_expr(src), precedence=16);
else
return expr2ct::convert_with_precedence(src, precedence);
}
std::string expr2javat::convert_code(
const codet &src,
unsigned indent)
{
const irep_idt &statement=src.get(ID_statement);
if(statement==ID_java_new ||
statement==ID_java_new_array)
return convert_code_java_new(src, indent);
if(statement==ID_function_call)
return convert_code_function_call(to_code_function_call(src), indent);
return expr2ct::convert_code(src, indent);
}
std::string expr2java(const exprt &expr, const namespacet &ns)
{
expr2javat expr2java(ns);
expr2java.get_shorthands(expr);
return expr2java.convert(expr);
}
std::string type2java(const typet &type, const namespacet &ns)
{
expr2javat expr2java(ns);
return expr2java.convert(type);
}