CBMC
expr2java.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@cs.cmu.edu
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_JAVA_BYTECODE_EXPR2JAVA_H
11 #define CPROVER_JAVA_BYTECODE_EXPR2JAVA_H
12 
13 #include <cmath>
14 #include <sstream>
15 #include <string>
16 
17 #include <ansi-c/expr2c_class.h>
18 
19 class expr2javat:public expr2ct
20 {
21 public:
22  explicit expr2javat(const namespacet &_ns):expr2ct(_ns) { }
23  virtual std::string convert(const typet &src) override;
24  virtual std::string convert(const exprt &src) override;
25 
26 protected:
27  virtual std::string convert_with_precedence(
28  const exprt &src, unsigned &precedence) override;
29  std::string convert_java_this();
30  std::string convert_java_instanceof(const exprt &src);
31  std::string convert_java_new(const exprt &src);
32  std::string convert_code_java_new(const exprt &src, unsigned precedence);
33  std::string convert_code_java_delete(const exprt &src, unsigned precedence);
34  virtual std::string convert_struct(
35  const exprt &src, unsigned &precedence) override;
36  virtual std::string convert_code(const codet &src, unsigned indent) override;
37  virtual std::string convert_constant(
38  const constant_exprt &src, unsigned &precedence) override;
39  // Hides base class version
40  std::string convert_code_function_call(
41  const code_function_callt &src, unsigned indent);
42 
43  virtual std::string convert_rec(
44  const typet &src,
45  const c_qualifierst &qualifiers,
46  const std::string &declarator) override;
47 
48  // length of string representation of Java Char
49  // representation is '\u0000'
50  const std::size_t char_representation_length=8;
51 };
52 
53 std::string expr2java(const exprt &expr, const namespacet &ns);
54 std::string type2java(const typet &type, const namespacet &ns);
55 
59 template <typename float_type>
61 {
62  const bool is_float = std::is_same<float_type, float>::value;
63  const std::string class_name = is_float ? "Float" : "Double";
64  if(std::isnan(value))
65  return class_name + ".NaN";
66  if(std::isinf(value) && value >= 0.)
67  return class_name + ".POSITIVE_INFINITY";
68  if(std::isinf(value) && value <= 0.)
69  return class_name + ".NEGATIVE_INFINITY";
70  const std::string decimal = [&]() -> std::string {
71  // Using ostringstream instead of to_string to get string without
72  // trailing zeros
73  std::ostringstream raw_stream;
74  raw_stream << value;
75  const auto raw_decimal = raw_stream.str();
76  const bool is_scientific = raw_decimal.find('e') != std::string::npos;
77  if(
78  raw_decimal.find('.') == std::string::npos &&
79  !is_scientific) // don't add trailing .0 if in scientific notation
80  {
81  return raw_decimal + ".0";
82  }
83  return raw_decimal;
84  }();
85  const bool is_lossless = [&] {
86  try
87  {
88  return std::stod(decimal) == value;
89  }
90  catch(std::out_of_range &)
91  {
92  return false;
93  }
94  }();
95  const std::string lossless = [&]() -> std::string {
96  if(is_lossless)
97  return decimal;
98  std::ostringstream stream;
99  stream << std::hexfloat << value;
100  return stream.str();
101  }();
102  const auto literal = is_float ? lossless + 'f' : lossless;
103  if(is_lossless)
104  return literal;
105  return literal + " /* " + decimal + " */";
106 }
107 
108 #endif // CPROVER_JAVA_BYTECODE_EXPR2JAVA_H
floatbv_typet float_type()
Definition: c_types.cpp:177
goto_instruction_codet representation of a function call statement.
Data structure for representing an arbitrary statement in a program.
Definition: std_code_base.h:29
A constant literal expression.
Definition: std_expr.h:2990
std::string convert_code_java_new(const exprt &src, unsigned precedence)
Definition: expr2java.cpp:329
std::string convert_java_new(const exprt &src)
Definition: expr2java.cpp:334
virtual std::string convert_struct(const exprt &src, unsigned &precedence) override
Definition: expr2java.cpp:104
virtual std::string convert_with_precedence(const exprt &src, unsigned &precedence) override
Definition: expr2java.cpp:377
std::string convert_code_java_delete(const exprt &src, unsigned precedence)
Definition: expr2java.cpp:358
virtual std::string convert_rec(const typet &src, const c_qualifierst &qualifiers, const std::string &declarator) override
Definition: expr2java.cpp:243
std::string convert_code_function_call(const code_function_callt &src, unsigned indent)
Definition: expr2java.cpp:35
const std::size_t char_representation_length
Definition: expr2java.h:50
virtual std::string convert_code(const codet &src, unsigned indent) override
Definition: expr2java.cpp:438
std::string convert_java_instanceof(const exprt &src)
Definition: expr2java.cpp:321
virtual std::string convert(const typet &src) override
Definition: expr2java.cpp:25
std::string convert_java_this()
Definition: expr2java.cpp:316
expr2javat(const namespacet &_ns)
Definition: expr2java.h:22
virtual std::string convert_constant(const constant_exprt &src, unsigned &precedence) override
Definition: expr2java.cpp:169
Base class for all expressions.
Definition: expr.h:56
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
The type of an expression, extends irept.
Definition: type.h:29
std::string type2java(const typet &type, const namespacet &ns)
Definition: expr2java.cpp:461
std::string floating_point_to_java_string(float_type value)
Convert floating point number to a string without printing unnecessary zeros.
Definition: expr2java.h:60
std::string expr2java(const exprt &expr, const namespacet &ns)
Definition: expr2java.cpp:454
int isnan(double d)
Definition: math.c:163
int isinf(double d)
Definition: math.c:119