cprover
expr2java.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@cs.cmu.edu
6 
7 \*******************************************************************/
8 
9 #include "expr2java.h"
10 
11 #include <cassert>
12 #include <sstream>
13 
14 #include <util/namespace.h>
15 #include <util/std_types.h>
16 #include <util/std_expr.h>
17 #include <util/symbol.h>
18 #include <util/unicode.h>
19 #include <util/arith_tools.h>
20 #include <util/ieee_float.h>
21 
22 #include <ansi-c/c_misc.h>
23 #include <ansi-c/expr2c_class.h>
24 
25 #include "java_qualifiers.h"
26 #include "java_types.h"
27 
28 std::string expr2javat::convert(const typet &src)
29 {
30  return convert_rec(src, java_qualifierst(ns), "");
31 }
32 
33 std::string expr2javat::convert(const exprt &src)
34 {
35  return expr2ct::convert(src);
36 }
37 
39  const code_function_callt &src,
40  unsigned indent)
41 {
42  if(src.operands().size()!=3)
43  {
44  unsigned precedence;
45  return convert_norep(src, precedence);
46  }
47 
48  std::string dest=indent_str(indent);
49 
50  if(src.lhs().is_not_nil())
51  {
52  unsigned p;
53  std::string lhs_str=convert_with_precedence(src.lhs(), p);
54 
55  dest+=lhs_str;
56  dest+='=';
57  }
58 
59  const java_method_typet &method_type =
61 
62  bool has_this = method_type.has_this() && !src.arguments().empty();
63 
64  if(has_this)
65  {
66  unsigned p;
67  std::string this_str=convert_with_precedence(src.arguments()[0], p);
68  dest+=this_str;
69  dest+=" . "; // extra spaces for readability
70  }
71 
72  {
73  unsigned p;
74  std::string function_str=convert_with_precedence(src.function(), p);
75  dest+=function_str;
76  }
77 
78  dest+='(';
79 
80  const exprt::operandst &arguments=src.arguments();
81 
82  bool first=true;
83 
84  forall_expr(it, arguments)
85  {
86  if(has_this && it==arguments.begin())
87  {
88  }
89  else
90  {
91  unsigned p;
92  std::string arg_str=convert_with_precedence(*it, p);
93 
94  if(first)
95  first=false;
96  else
97  dest+=", ";
98  dest+=arg_str;
99  }
100  }
101 
102  dest+=");";
103 
104  return dest;
105 }
106 
108  const exprt &src,
109  unsigned &precedence)
110 {
111  const typet &full_type=ns.follow(src.type());
112 
113  if(full_type.id()!=ID_struct)
114  return convert_norep(src, precedence);
115 
116  const struct_typet &struct_type=to_struct_type(full_type);
117 
118  std::string dest="{ ";
119 
120  const struct_typet::componentst &components=
121  struct_type.components();
122 
123  assert(components.size()==src.operands().size());
124 
125  exprt::operandst::const_iterator o_it=src.operands().begin();
126 
127  bool first=true;
128  size_t last_size=0;
129 
130  for(const auto &c : components)
131  {
132  if(c.type().id() != ID_code)
133  {
134  std::string tmp=convert(*o_it);
135  std::string sep;
136 
137  if(first)
138  first=false;
139  else
140  {
141  if(last_size+40<dest.size())
142  {
143  sep=",\n ";
144  last_size=dest.size();
145  }
146  else
147  sep=", ";
148  }
149 
150  dest+=sep;
151  dest+='.';
152  dest += id2string(c.get_pretty_name());
153  dest+='=';
154  dest+=tmp;
155  }
156 
157  o_it++;
158  }
159 
160  dest+=" }";
161 
162  return dest;
163 }
164 
166  const constant_exprt &src,
167  unsigned &precedence)
168 {
169  if(src.type().id()==ID_c_bool)
170  {
171  if(!src.is_zero())
172  return "true";
173  else
174  return "false";
175  }
176  else if(src.type().id()==ID_bool)
177  {
178  if(src.is_true())
179  return "true";
180  else if(src.is_false())
181  return "false";
182  }
183  else if(src.type().id()==ID_pointer)
184  {
185  // Java writes 'null' for the null reference
186  if(src.is_zero())
187  return "null";
188  }
189  else if(src.type()==java_char_type())
190  {
191  std::string dest;
192  dest.reserve(char_representation_length);
193 
194  const char16_t int_value = numeric_cast_v<char16_t>(src);
195 
196  // Character literals in Java have type 'char', thus no cast is needed.
197  // This is different from C, where charater literals have type 'int'.
198  dest += '\'' + utf16_native_endian_to_java(int_value) + '\'';
199  return dest;
200  }
201  else if(src.type()==java_byte_type())
202  {
203  // No byte-literals in Java, so just cast:
204  const mp_integer int_value = numeric_cast_v<mp_integer>(src);
205  std::string dest="(byte)";
206  dest+=integer2string(int_value);
207  return dest;
208  }
209  else if(src.type()==java_short_type())
210  {
211  // No short-literals in Java, so just cast:
212  const mp_integer int_value = numeric_cast_v<mp_integer>(src);
213  std::string dest="(short)";
214  dest+=integer2string(int_value);
215  return dest;
216  }
217  else if(src.type()==java_long_type())
218  {
219  // long integer literals must have 'L' at the end
220  const mp_integer int_value = numeric_cast_v<mp_integer>(src);
221  std::string dest=integer2string(int_value);
222  dest+='L';
223  return dest;
224  }
225  else if((src.type()==java_float_type()) ||
226  (src.type()==java_double_type()))
227  {
228  // This converts NaNs to the canonical NaN
229  const ieee_floatt ieee_repr(to_constant_expr(src));
230  if(ieee_repr.is_double())
231  return floating_point_to_java_string(ieee_repr.to_double());
232  return floating_point_to_java_string(ieee_repr.to_float());
233  }
234 
235 
236  return expr2ct::convert_constant(src, precedence);
237 }
238 
240  const typet &src,
241  const qualifierst &qualifiers,
242  const std::string &declarator)
243 {
244  std::unique_ptr<qualifierst> clone = qualifiers.clone();
245  qualifierst &new_qualifiers = *clone;
246  new_qualifiers.read(src);
247 
248  const std::string d=
249  declarator==""?declarator:(" "+declarator);
250 
251  const std::string q=
252  new_qualifiers.as_string();
253 
254  if(src==java_int_type())
255  return q+"int"+d;
256  else if(src==java_long_type())
257  return q+"long"+d;
258  else if(src==java_short_type())
259  return q+"short"+d;
260  else if(src==java_byte_type())
261  return q+"byte"+d;
262  else if(src==java_char_type())
263  return q+"char"+d;
264  else if(src==java_float_type())
265  return q+"float"+d;
266  else if(src==java_double_type())
267  return q+"double"+d;
268  else if(src==java_boolean_type())
269  return q+"boolean"+d;
270  else if(src==java_byte_type())
271  return q+"byte"+d;
272  else if(src.id()==ID_code)
273  {
274  const java_method_typet &method_type = to_java_method_type(src);
275 
276  // Java doesn't really have syntax for function types,
277  // so we make one up, loosely inspired by the syntax
278  // of lambda expressions.
279 
280  std::string dest="";
281 
282  dest+='(';
283  const java_method_typet::parameterst &parameters = method_type.parameters();
284 
285  for(java_method_typet::parameterst::const_iterator it = parameters.begin();
286  it != parameters.end();
287  it++)
288  {
289  if(it!=parameters.begin())
290  dest+=", ";
291 
292  dest+=convert(it->type());
293  }
294 
295  if(method_type.has_ellipsis())
296  {
297  if(!parameters.empty())
298  dest+=", ";
299  dest+="...";
300  }
301 
302  dest+=')';
303 
304  const typet &return_type = method_type.return_type();
305  dest+=" -> "+convert(return_type);
306 
307  return q + dest;
308  }
309  else
310  return expr2ct::convert_rec(src, qualifiers, declarator);
311 }
312 
314  const exprt &src,
315  unsigned precedence)
316 {
317  return "this";
318 }
319 
321  const exprt &src,
322  unsigned precedence)
323 {
324  if(src.operands().size()!=2)
325  {
326  unsigned precedence;
327  return convert_norep(src, precedence);
328  }
329 
330  return convert(src.op0())+" instanceof "+convert(src.op1().type());
331 }
332 
334  const exprt &src,
335  unsigned precedence)
336 {
337  std::string dest;
338 
339  if(src.get(ID_statement)==ID_java_new_array ||
340  src.get(ID_statement)==ID_java_new_array_data)
341  {
342  dest="new";
343 
344  std::string tmp_size=
345  convert(static_cast<const exprt &>(src.find(ID_size)));
346 
347  dest+=' ';
348  dest+=convert(src.type().subtype());
349  dest+='[';
350  dest+=tmp_size;
351  dest+=']';
352  }
353  else
354  dest="new "+convert(src.type().subtype());
355 
356  return dest;
357 }
358 
360  const exprt &src,
361  unsigned indent)
362 {
363  std::string dest=indent_str(indent)+"delete ";
364 
365  if(src.operands().size()!=1)
366  {
367  unsigned precedence;
368  return convert_norep(src, precedence);
369  }
370 
371  std::string tmp=convert(src.op0());
372 
373  dest+=tmp+";\n";
374 
375  return dest;
376 }
377 
379  const exprt &src,
380  unsigned &precedence)
381 {
382  if(src.id()=="java-this")
383  return convert_java_this(src, precedence=15);
384  if(src.id()==ID_java_instanceof)
385  return convert_java_instanceof(src, precedence=15);
386  else if(src.id()==ID_side_effect &&
387  (src.get(ID_statement)==ID_java_new ||
388  src.get(ID_statement)==ID_java_new_array ||
389  src.get(ID_statement)==ID_java_new_array_data))
390  return convert_java_new(src, precedence=15);
391  else if(src.id()==ID_side_effect &&
392  src.get(ID_statement)==ID_throw)
393  return convert_function(src, "throw", precedence=16);
394  else if(src.id()==ID_code &&
395  to_code(src).get_statement()==ID_exception_landingpad)
396  {
397  const exprt &catch_expr=
399  return "catch_landingpad("+
400  convert(catch_expr.type())+
401  ' '+
402  convert(catch_expr)+
403  ')';
404  }
405  else if(src.id()==ID_unassigned)
406  return "?";
407  else if(src.id()=="pod_constructor")
408  return "pod_constructor";
409  else if(src.id()==ID_virtual_function)
410  {
411  return "VIRTUAL_FUNCTION(" +
412  id2string(src.get(ID_C_class)) +
413  "." +
414  id2string(src.get(ID_component_name)) +
415  ")";
416  }
417  else if(src.id()==ID_java_string_literal)
418  return '"'+MetaString(src.get_string(ID_value))+'"';
419  else if(src.id()==ID_constant)
420  return convert_constant(to_constant_expr(src), precedence=16);
421  else
422  return expr2ct::convert_with_precedence(src, precedence);
423 }
424 
426  const codet &src,
427  unsigned indent)
428 {
429  const irep_idt &statement=src.get(ID_statement);
430 
431  if(statement==ID_java_new ||
432  statement==ID_java_new_array)
433  return convert_java_new(src, indent);
434 
435  if(statement==ID_function_call)
437 
438  return expr2ct::convert_code(src, indent);
439 }
440 
441 std::string expr2java(const exprt &expr, const namespacet &ns)
442 {
443  expr2javat expr2java(ns);
444  expr2java.get_shorthands(expr);
445  return expr2java.convert(expr);
446 }
447 
448 std::string type2java(const typet &type, const namespacet &ns)
449 {
450  expr2javat expr2java(ns);
451  return expr2java.convert(type);
452 }
Java-specific type qualifiers.
const irep_idt & get_statement() const
Definition: std_code.h:56
virtual std::string convert_with_precedence(const exprt &src, unsigned &precedence) override
Definition: expr2java.cpp:378
The type of an expression, extends irept.
Definition: type.h:27
virtual std::string convert_struct(const exprt &src, unsigned &precedence) override
Definition: expr2java.cpp:107
std::string type2java(const typet &type, const namespacet &ns)
Definition: expr2java.cpp:448
virtual std::string convert_rec(const typet &src, const qualifierst &qualifiers, const std::string &declarator)
Definition: expr2c.cpp:188
BigInt mp_integer
Definition: mp_arith.h:22
std::string convert_java_new(const exprt &src, unsigned precedence)
Definition: expr2java.cpp:333
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
bool is_not_nil() const
Definition: irep.h:173
std::vector< parametert > parameterst
Definition: std_types.h:754
Symbol table entry.
const std::string integer2string(const mp_integer &n, unsigned base)
Definition: mp_arith.cpp:106
std::string convert_function(const exprt &src, const std::string &symbol, unsigned precedence)
Definition: expr2c.cpp:1263
typet java_boolean_type()
Definition: java_types.cpp:72
exprt & op0()
Definition: expr.h:84
typet java_double_type()
Definition: java_types.cpp:67
virtual void read(const typet &src)=0
bool has_ellipsis() const
Definition: std_types.h:849
std::string convert_code_java_delete(const exprt &src, unsigned precedence)
Definition: expr2java.cpp:359
typet java_int_type()
Definition: java_types.cpp:32
std::string convert_java_instanceof(const exprt &src, unsigned precedence)
Definition: expr2java.cpp:320
#define forall_expr(it, expr)
Definition: expr.h:31
std::vector< componentt > componentst
Definition: std_types.h:203
virtual std::string convert_constant(const constant_exprt &src, unsigned &precedence) override
Definition: expr2java.cpp:165
bool is_false() const
Return whether the expression is a constant representing false.
Definition: expr.cpp:99
const componentst & components() const
Definition: std_types.h:205
virtual std::string convert_code(const codet &src, unsigned indent) override
Definition: expr2java.cpp:425
ANSI-C Misc Utilities.
std::string convert_code(const codet &src)
Definition: expr2c.cpp:3334
virtual std::string convert_rec(const typet &src, const qualifierst &qualifiers, const std::string &declarator) override
Definition: expr2java.cpp:239
bool is_true() const
Return whether the expression is a constant representing true.
Definition: expr.cpp:90
typet java_long_type()
Definition: java_types.cpp:42
typet & type()
Return the type of the expression.
Definition: expr.h:68
A constant literal expression.
Definition: std_expr.h:4384
Structure type, corresponds to C style structs.
Definition: std_types.h:276
std::string floating_point_to_java_string(float_type value)
Convert floating point number to a string without printing unnecessary zeros.
Definition: expr2java.h:59
virtual std::string convert(const typet &src)
Definition: expr2c.cpp:183
virtual std::string convert_constant(const constant_exprt &src, unsigned &precedence)
Definition: expr2c.cpp:1787
const irep_idt & id() const
Definition: irep.h:259
static void utf16_native_endian_to_java(const wchar_t ch, std::ostringstream &result, const std::locale &loc)
Definition: unicode.cpp:259
argumentst & arguments()
Definition: std_code.h:1109
virtual std::unique_ptr< qualifierst > clone() const =0
API to expression classes.
double to_double() const
Note that calling from_double -> to_double can return different bit patterns for NaN.
exprt & op1()
Definition: expr.h:87
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:212
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:93
codet representation of a function call statement.
Definition: std_code.h:1036
const namespacet & ns
Definition: expr2c_class.h:46
const std::size_t char_representation_length
Definition: expr2java.h:49
typet java_byte_type()
Definition: java_types.cpp:52
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:62
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:349
dstringt has one field, an unsigned integer no which is an index into a static table of strings...
Definition: dstring.h:35
typet java_short_type()
Definition: java_types.cpp:47
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition: std_expr.h:4423
std::vector< exprt > operandst
Definition: expr.h:57
std::string convert_norep(const exprt &src, unsigned &precedence)
Definition: expr2c.cpp:1663
Pre-defined types.
virtual std::string convert(const typet &src) override
Definition: expr2java.cpp:28
const java_method_typet & to_java_method_type(const typet &type)
Definition: java_types.h:338
static code_landingpadt & to_code_landingpad(codet &code)
Definition: std_code.h:1994
exprt & function()
Definition: std_code.h:1099
virtual std::string as_string() const =0
Base class for all expressions.
Definition: expr.h:54
const parameterst & parameters() const
Definition: std_types.h:893
bool has_this() const
Definition: std_types.h:854
virtual std::string convert_with_precedence(const exprt &src, unsigned &precedence)
Definition: expr2c.cpp:3428
bool is_double() const
const std::string & get_string(const irep_namet &name) const
Definition: irep.h:272
bool is_zero() const
Return whether the expression is a constant representing 0.
Definition: expr.cpp:130
const codet & to_code(const exprt &expr)
Definition: std_code.h:136
const exprt & catch_expr() const
Definition: std_code.h:1976
std::string MetaString(const std::string &in)
Definition: c_misc.cpp:95
std::string convert_code_function_call(const code_function_callt &src, unsigned indent)
Definition: expr2java.cpp:38
Data structure for representing an arbitrary statement in a program.
Definition: std_code.h:34
typet java_char_type()
Definition: java_types.cpp:57
const typet & subtype() const
Definition: type.h:38
operandst & operands()
Definition: expr.h:78
std::string convert_java_this(const exprt &src, unsigned precedence)
Definition: expr2java.cpp:313
typet java_float_type()
Definition: java_types.cpp:62
const irept & find(const irep_namet &name) const
Definition: irep.cpp:284
float to_float() const
Note that calling from_float -> to_float can return different bit patterns for NaN.
const typet & return_type() const
Definition: std_types.h:883
static std::string indent_str(unsigned indent)
Definition: expr2c.cpp:2458
std::string expr2java(const exprt &expr, const namespacet &ns)
Definition: expr2java.cpp:441
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:1173