15
15
#include < util/arith_tools.h>
16
16
17
17
#include < ansi-c/c_qualifiers.h>
18
+ #include < ansi-c/c_misc.h>
18
19
#include < ansi-c/expr2c_class.h>
19
20
20
21
#include " java_types.h"
21
22
#include " expr2java.h"
22
23
23
- class expr2javat :public expr2ct
24
- {
25
- public:
26
- expr2javat (const namespacet &_ns):expr2ct(_ns) { }
27
-
28
- std::string convert (const exprt &src) override
29
- {
30
- return expr2ct::convert (src);
31
- }
32
-
33
- std::string convert (const typet &src) override
34
- {
35
- return expr2ct::convert (src);
36
- }
37
-
38
- protected:
39
- std::string convert (const exprt &src, unsigned &precedence) override ;
40
- std::string convert_java_this (const exprt &src, unsigned precedence);
41
- std::string convert_java_instanceof (const exprt &src, unsigned precedence);
42
- std::string convert_java_new (const exprt &src, unsigned precedence);
43
- std::string convert_code_java_delete (const exprt &src, unsigned precedence);
44
- std::string convert_struct (const exprt &src, unsigned &precedence) override ;
45
- std::string convert_code (const codet &src, unsigned indent) override ;
46
- std::string convert_constant (const constant_exprt &src, unsigned &precedence) override ;
47
- std::string convert_code_function_call (const code_function_callt &src, unsigned indent);
48
-
49
- std::string convert_rec (
50
- const typet &src,
51
- const c_qualifierst &qualifiers,
52
- const std::string &declarator) override ;
53
-
54
- typedef std::unordered_set<std::string, string_hash> id_sett;
55
- };
56
-
57
24
/* ******************************************************************\
58
25
59
26
Function: expr2javat::convert_code_function_call
@@ -83,7 +50,6 @@ std::string expr2javat::convert_code_function_call(
83
50
unsigned p;
84
51
std::string lhs_str=convert (src.lhs (), p);
85
52
86
- // TODO: ggf. Klammern je nach p
87
53
dest+=lhs_str;
88
54
dest+=' =' ;
89
55
}
@@ -124,8 +90,10 @@ std::string expr2javat::convert_code_function_call(
124
90
unsigned p;
125
91
std::string arg_str=convert (*it, p);
126
92
127
- if (first) first=false ; else dest+=" , " ;
128
- // TODO: ggf. Klammern je nach p
93
+ if (first)
94
+ first=false ;
95
+ else
96
+ dest+=" , " ;
129
97
dest+=arg_str;
130
98
}
131
99
}
@@ -244,15 +212,16 @@ std::string expr2javat::convert_constant(
244
212
else if (src.type ()==java_char_type ())
245
213
{
246
214
std::string dest;
247
- dest.reserve (10 );
215
+ dest.reserve (char_representation_length );
248
216
249
217
mp_integer int_value;
250
- to_integer (src, int_value);
218
+ if (!to_integer (src, int_value))
219
+ assert (false );
251
220
252
- dest+=' \' ' ;
221
+ dest+=" (char)' " ;
253
222
254
223
if (int_value>=' ' && int_value<127 )
255
- dest+=( char ) (int_value.to_long ());
224
+ dest+=static_cast < char > (int_value.to_long ());
256
225
else
257
226
{
258
227
std::string hex=integer2string (int_value, 16 );
@@ -265,6 +234,26 @@ std::string expr2javat::convert_constant(
265
234
dest+=' \' ' ;
266
235
return dest;
267
236
}
237
+ else if (src.type ()==java_byte_type ())
238
+ {
239
+ // No byte-literals in Java, so just cast:
240
+ mp_integer int_value;
241
+ if (!to_integer (src, int_value))
242
+ assert (false );
243
+ std::string dest=" (byte)" ;
244
+ dest+=integer2string (int_value);
245
+ return dest;
246
+ }
247
+ else if (src.type ()==java_short_type ())
248
+ {
249
+ // No short-literals in Java, so just cast:
250
+ mp_integer int_value;
251
+ if (!to_integer (src, int_value))
252
+ assert (false );
253
+ std::string dest=" (short)" ;
254
+ dest+=integer2string (int_value);
255
+ return dest;
256
+ }
268
257
else if (src.type ()==java_long_type ())
269
258
{
270
259
// long integer literals must have 'L' at the end
@@ -319,7 +308,7 @@ std::string expr2javat::convert_rec(
319
308
else if (src==java_double_type ())
320
309
return q+" double" +d;
321
310
else if (src==java_boolean_type ())
322
- return q+" bool " +d;
311
+ return q+" boolean " +d;
323
312
else if (src==java_byte_type ())
324
313
return q+" byte" +d;
325
314
else if (src.id ()==ID_code)
@@ -348,7 +337,8 @@ std::string expr2javat::convert_rec(
348
337
349
338
if (code_type.has_ellipsis ())
350
339
{
351
- if (!parameters.empty ()) dest+=" , " ;
340
+ if (!parameters.empty ())
341
+ dest+=" , " ;
352
342
dest+=" ..." ;
353
343
}
354
344
@@ -491,9 +481,10 @@ std::string expr2javat::convert(
491
481
const exprt &src,
492
482
unsigned &precedence)
493
483
{
484
+ const typet &type=ns.follow (src.type ());
494
485
if (src.id ()==" java-this" )
495
486
return convert_java_this (src, precedence=15 );
496
- if (src.id ()==" java_instanceof " )
487
+ if (src.id ()==ID_java_instanceof )
497
488
return convert_java_instanceof (src, precedence=15 );
498
489
else if (src.id ()==ID_side_effect &&
499
490
(src.get (ID_statement)==ID_java_new ||
@@ -509,9 +500,22 @@ std::string expr2javat::convert(
509
500
else if (src.id ()==" pod_constructor" )
510
501
return " pod_constructor" ;
511
502
else if (src.id ()==ID_virtual_function)
512
- return convert_function (src, " VIRTUAL_FUNCTION" , precedence=16 );
503
+ {
504
+ return " VIRTUAL_FUNCTION(" +
505
+ id2string (src.get (ID_C_class)) +
506
+ " ." +
507
+ id2string (src.get (ID_component_name)) +
508
+ " )" ;
509
+ }
513
510
else if (src.id ()==ID_java_string_literal)
514
- return ' "' +id2string (src.get (ID_value))+' "' ; // Todo: add escaping as needed
511
+ return ' "' +MetaString (src.get_string (ID_value))+' "' ;
512
+ else if (src.id ()==ID_constant && (type.id ()==ID_bool || type.id ()==ID_c_bool))
513
+ {
514
+ if (src.is_true ())
515
+ return " true" ;
516
+ else
517
+ return " false" ;
518
+ }
515
519
else
516
520
return expr2ct::convert (src, precedence);
517
521
}
@@ -536,7 +540,7 @@ std::string expr2javat::convert_code(
536
540
537
541
if (statement==ID_java_new ||
538
542
statement==ID_java_new_array)
539
- return convert_java_new (src,indent);
543
+ return convert_java_new (src, indent);
540
544
541
545
if (statement==ID_function_call)
542
546
return convert_code_function_call (to_code_function_call (src), indent);
0 commit comments