cprover
json_expr.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Expressions in JSON
4 
5 Author: Peter Schrammel
6 
7 \*******************************************************************/
8 
11 
12 #include "json_expr.h"
13 
14 #include <util/arith_tools.h>
15 #include <util/c_types.h>
16 #include <util/config.h>
17 #include <util/expr_util.h>
18 #include <util/fixedbv.h>
19 #include <util/identifier.h>
20 #include <util/ieee_float.h>
21 #include <util/invariant.h>
22 #include <util/json.h>
23 #include <util/namespace.h>
24 #include <util/pointer_expr.h>
25 #include <util/std_expr.h>
26 
27 #include <langapi/language.h>
28 #include <langapi/mode.h>
29 
30 #include <memory>
31 
32 static exprt simplify_json_expr(const exprt &src)
33 {
34  if(src.id() == ID_constant)
35  {
36  if(src.type().id() == ID_pointer)
37  {
38  const constant_exprt &c = to_constant_expr(src);
39 
40  if(
41  c.get_value() != ID_NULL &&
43  src.operands().size() == 1 &&
44  to_unary_expr(src).op().id() != ID_constant)
45  // try to simplify the constant pointer
46  {
47  return simplify_json_expr(to_unary_expr(src).op());
48  }
49  }
50  }
51  else if(src.id() == ID_typecast)
52  {
53  return simplify_json_expr(to_typecast_expr(src).op());
54  }
55  else if(src.id() == ID_address_of)
56  {
57  const exprt &object = skip_typecast(to_address_of_expr(src).object());
58 
59  if(object.id() == ID_symbol)
60  {
61  // simplify expressions of the form &symbol
62  return simplify_json_expr(object);
63  }
64  else if(
65  object.id() == ID_member &&
66  id2string(to_member_expr(object).get_component_name()).find("@") !=
67  std::string::npos)
68  {
69  // simplify expressions of the form &member(object, @class_identifier)
70  return simplify_json_expr(object);
71  }
72  else if(
73  object.id() == ID_index &&
74  to_index_expr(object).index().id() == ID_constant &&
75  to_constant_expr(to_index_expr(object).index()).value_is_zero_string())
76  {
77  // simplify expressions of the form &array[0]
78  return simplify_json_expr(to_index_expr(object).array());
79  }
80  }
81  else if(
82  src.id() == ID_member &&
83  id2string(to_member_expr(src).get_component_name()).find("@") !=
84  std::string::npos)
85  {
86  // simplify expressions of the form member_expr(object, @class_identifier)
87  return simplify_json_expr(to_member_expr(src).struct_op());
88  }
89 
90  return src;
91 }
92 
99 json_objectt json(const typet &type, const namespacet &ns, const irep_idt &mode)
100 {
101  json_objectt result;
102 
103  if(type.id() == ID_unsignedbv)
104  {
105  result["name"] = json_stringt("integer");
106  result["width"] =
107  json_numbert(std::to_string(to_unsignedbv_type(type).get_width()));
108  }
109  else if(type.id() == ID_signedbv)
110  {
111  result["name"] = json_stringt("integer");
112  result["width"] =
113  json_numbert(std::to_string(to_signedbv_type(type).get_width()));
114  }
115  else if(type.id() == ID_floatbv)
116  {
117  result["name"] = json_stringt("float");
118  result["width"] =
119  json_numbert(std::to_string(to_floatbv_type(type).get_width()));
120  }
121  else if(type.id() == ID_bv)
122  {
123  result["name"] = json_stringt("integer");
124  result["width"] =
125  json_numbert(std::to_string(to_bv_type(type).get_width()));
126  }
127  else if(type.id() == ID_c_bit_field)
128  {
129  result["name"] = json_stringt("integer");
130  result["width"] =
131  json_numbert(std::to_string(to_c_bit_field_type(type).get_width()));
132  }
133  else if(type.id() == ID_c_enum_tag)
134  {
135  // we return the base type
136  return json(ns.follow_tag(to_c_enum_tag_type(type)).subtype(), ns, mode);
137  }
138  else if(type.id() == ID_fixedbv)
139  {
140  result["name"] = json_stringt("fixed");
141  result["width"] =
142  json_numbert(std::to_string(to_fixedbv_type(type).get_width()));
143  }
144  else if(type.id() == ID_pointer)
145  {
146  result["name"] = json_stringt("pointer");
147  result["subtype"] = json(to_pointer_type(type).subtype(), ns, mode);
148  }
149  else if(type.id() == ID_bool)
150  {
151  result["name"] = json_stringt("boolean");
152  }
153  else if(type.id() == ID_array)
154  {
155  result["name"] = json_stringt("array");
156  result["subtype"] = json(to_array_type(type).subtype(), ns, mode);
157  result["size"] = json(to_array_type(type).size(), ns, mode);
158  }
159  else if(type.id() == ID_vector)
160  {
161  result["name"] = json_stringt("vector");
162  result["subtype"] = json(to_vector_type(type).subtype(), ns, mode);
163  result["size"] = json(to_vector_type(type).size(), ns, mode);
164  }
165  else if(type.id() == ID_struct)
166  {
167  result["name"] = json_stringt("struct");
168  json_arrayt &members = result["members"].make_array();
169  const struct_typet::componentst &components =
170  to_struct_type(type).components();
171  for(const auto &component : components)
172  {
173  json_objectt e{{"name", json_stringt(component.get_name())},
174  {"type", json(component.type(), ns, mode)}};
175  members.push_back(std::move(e));
176  }
177  }
178  else if(type.id() == ID_struct_tag)
179  {
180  return json(ns.follow_tag(to_struct_tag_type(type)), ns, mode);
181  }
182  else if(type.id() == ID_union)
183  {
184  result["name"] = json_stringt("union");
185  json_arrayt &members = result["members"].make_array();
186  const union_typet::componentst &components =
187  to_union_type(type).components();
188  for(const auto &component : components)
189  {
190  json_objectt e{{"name", json_stringt(component.get_name())},
191  {"type", json(component.type(), ns, mode)}};
192  members.push_back(std::move(e));
193  }
194  }
195  else if(type.id() == ID_union_tag)
196  {
197  return json(ns.follow_tag(to_union_tag_type(type)), ns, mode);
198  }
199  else
200  result["name"] = json_stringt("unknown");
201 
202  return result;
203 }
204 
205 static std::string binary(const constant_exprt &src)
206 {
207  std::size_t width;
208  if(src.type().id() == ID_c_enum)
210  else
211  width = to_bitvector_type(src.type()).get_width();
212  const auto int_val = bvrep2integer(src.get_value(), width, false);
213  return integer2binary(int_val, width);
214 }
215 
222 json_objectt json(const exprt &expr, const namespacet &ns, const irep_idt &mode)
223 {
224  json_objectt result;
225 
226  if(expr.id() == ID_constant)
227  {
228  const constant_exprt &constant_expr = to_constant_expr(expr);
229 
230  const typet &type = expr.type();
231 
232  std::unique_ptr<languaget> lang;
233  if(mode != ID_unknown)
234  lang = std::unique_ptr<languaget>(get_language_from_mode(mode));
235  if(!lang)
236  lang = std::unique_ptr<languaget>(get_default_language());
237 
238  const typet &underlying_type =
239  type.id() == ID_c_bit_field ? to_c_bit_field_type(type).subtype() : type;
240 
241  std::string type_string;
242  bool error = lang->from_type(underlying_type, type_string, ns);
243  CHECK_RETURN(!error);
244 
245  std::string value_string;
246  lang->from_expr(expr, value_string, ns);
247 
248  if(
249  type.id() == ID_unsignedbv || type.id() == ID_signedbv ||
250  type.id() == ID_c_bit_field || type.id() == ID_c_bool)
251  {
252  std::size_t width = to_bitvector_type(type).get_width();
253 
254  result["name"] = json_stringt("integer");
255  result["binary"] = json_stringt(binary(constant_expr));
256  result["width"] = json_numbert(std::to_string(width));
257  result["type"] = json_stringt(type_string);
258  result["data"] = json_stringt(value_string);
259  }
260  else if(type.id() == ID_c_enum)
261  {
262  result["name"] = json_stringt("integer");
263  result["binary"] = json_stringt(binary(constant_expr));
264  result["width"] = json_numbert(std::to_string(
265  to_bitvector_type(to_c_enum_type(type).subtype()).get_width()));
266  result["type"] = json_stringt("enum");
267  result["data"] = json_stringt(value_string);
268  }
269  else if(type.id() == ID_c_enum_tag)
270  {
271  constant_exprt tmp(
272  to_constant_expr(expr).get_value(),
273  ns.follow_tag(to_c_enum_tag_type(type)));
274  return json(tmp, ns, mode);
275  }
276  else if(type.id() == ID_bv)
277  {
278  result["name"] = json_stringt("bitvector");
279  result["binary"] = json_stringt(binary(constant_expr));
280  }
281  else if(type.id() == ID_fixedbv)
282  {
283  result["name"] = json_stringt("fixed");
284  result["width"] =
285  json_numbert(std::to_string(to_bitvector_type(type).get_width()));
286  result["binary"] = json_stringt(binary(constant_expr));
287  result["data"] =
288  json_stringt(fixedbvt(to_constant_expr(expr)).to_ansi_c_string());
289  }
290  else if(type.id() == ID_floatbv)
291  {
292  result["name"] = json_stringt("float");
293  result["width"] =
294  json_numbert(std::to_string(to_bitvector_type(type).get_width()));
295  result["binary"] = json_stringt(binary(constant_expr));
296  result["data"] =
297  json_stringt(ieee_floatt(to_constant_expr(expr)).to_ansi_c_string());
298  }
299  else if(type.id() == ID_pointer)
300  {
301  result["name"] = json_stringt("pointer");
302  result["type"] = json_stringt(type_string);
303  exprt simpl_expr = simplify_json_expr(expr);
304  if(
305  simpl_expr.get(ID_value) == ID_NULL ||
306  // remove typecast on NULL
307  (simpl_expr.id() == ID_constant &&
308  simpl_expr.type().id() == ID_pointer &&
309  to_unary_expr(simpl_expr).op().get(ID_value) == ID_NULL))
310  {
311  result["data"] = json_stringt(value_string);
312  }
313  else if(simpl_expr.id() == ID_symbol)
314  {
315  const irep_idt &ptr_id = to_symbol_expr(simpl_expr).get_identifier();
316  identifiert identifier(id2string(ptr_id));
318  !identifier.components.empty(),
319  "pointer identifier should have non-empty components");
320  result["data"] = json_stringt(identifier.components.back());
321  }
322  }
323  else if(type.id() == ID_bool)
324  {
325  result["name"] = json_stringt("boolean");
326  result["binary"] = json_stringt(expr.is_true() ? "1" : "0");
327  result["data"] = jsont::json_boolean(expr.is_true());
328  }
329  else if(type.id() == ID_string)
330  {
331  result["name"] = json_stringt("string");
332  result["data"] = json_stringt(constant_expr.get_value());
333  }
334  else
335  {
336  result["name"] = json_stringt("unknown");
337  }
338  }
339  else if(expr.id() == ID_array)
340  {
341  result["name"] = json_stringt("array");
342  json_arrayt &elements = result["elements"].make_array();
343 
344  std::size_t index = 0;
345 
346  forall_operands(it, expr)
347  {
348  json_objectt e{{"index", json_numbert(std::to_string(index))},
349  {"value", json(*it, ns, mode)}};
350  elements.push_back(std::move(e));
351  index++;
352  }
353  }
354  else if(expr.id() == ID_struct)
355  {
356  result["name"] = json_stringt("struct");
357 
358  const typet &type = ns.follow(expr.type());
359 
360  // these are expected to have a struct type
361  if(type.id() == ID_struct)
362  {
363  const struct_typet &struct_type = to_struct_type(type);
364  const struct_typet::componentst &components = struct_type.components();
366  components.size() == expr.operands().size(),
367  "number of struct components should match with its type");
368 
369  json_arrayt &members = result["members"].make_array();
370  for(std::size_t m = 0; m < expr.operands().size(); m++)
371  {
372  json_objectt e{{"value", json(expr.operands()[m], ns, mode)},
373  {"name", json_stringt(components[m].get_name())}};
374  members.push_back(std::move(e));
375  }
376  }
377  }
378  else if(expr.id() == ID_union)
379  {
380  result["name"] = json_stringt("union");
381 
382  const union_exprt &union_expr = to_union_expr(expr);
383  result["member"] =
384  json_objectt({{"value", json(union_expr.op(), ns, mode)},
385  {"name", json_stringt(union_expr.get_component_name())}});
386  }
387  else
388  result["name"] = json_stringt("unknown");
389 
390  return result;
391 }
mp_integer bvrep2integer(const irep_idt &src, std::size_t width, bool is_signed)
convert a bit-vector representation (possibly signed) to integer
const floatbv_typet & to_floatbv_type(const typet &type)
Cast a typet to a floatbv_typet.
const signedbv_typet & to_signedbv_type(const typet &type)
Cast a typet to a signedbv_typet.
const bitvector_typet & to_bitvector_type(const typet &type)
Cast a typet to a bitvector_typet.
const fixedbv_typet & to_fixedbv_type(const typet &type)
Cast a typet to a fixedbv_typet.
const unsignedbv_typet & to_unsignedbv_type(const typet &type)
Cast a typet to an unsignedbv_typet.
const bv_typet & to_bv_type(const typet &type)
Cast a typet to a bv_typet.
const c_enum_typet & to_c_enum_type(const typet &type)
Cast a typet to a c_enum_typet.
Definition: c_types.h:277
const union_tag_typet & to_union_tag_type(const typet &type)
Cast a typet to a union_tag_typet.
Definition: c_types.h:189
const c_bit_field_typet & to_c_bit_field_type(const typet &type)
Cast a typet to a c_bit_field_typet.
Definition: c_types.h:47
const union_typet & to_union_type(const typet &type)
Cast a typet to a union_typet.
Definition: c_types.h:149
const c_enum_tag_typet & to_c_enum_tag_type(const typet &type)
Cast a typet to a c_enum_tag_typet.
Definition: c_types.h:317
std::size_t get_width() const
Definition: std_types.h:843
struct configt::ansi_ct ansi_c
A constant literal expression.
Definition: std_expr.h:2753
const irep_idt & get_value() const
Definition: std_expr.h:2761
bool value_is_zero_string() const
Definition: std_expr.cpp:16
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
Base class for all expressions.
Definition: expr.h:54
bool is_true() const
Return whether the expression is a constant representing true.
Definition: expr.cpp:33
typet & type()
Return the type of the expression.
Definition: expr.h:82
operandst & operands()
Definition: expr.h:92
componentst components
Definition: identifier.h:30
const irep_idt & id() const
Definition: irep.h:407
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:45
jsont & push_back(const jsont &json)
Definition: json.h:212
json_arrayt & make_array()
Definition: json.h:420
static jsont json_boolean(bool value)
Definition: json.h:97
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:49
const union_typet & follow_tag(const union_tag_typet &) const
Follow type tag of union type.
Definition: namespace.cpp:63
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
Structure type, corresponds to C style structs.
Definition: std_types.h:231
const componentst & components() const
Definition: std_types.h:147
std::vector< componentt > componentst
Definition: std_types.h:140
const irep_idt & get_identifier() const
Definition: std_expr.h:109
The type of an expression, extends irept.
Definition: type.h:28
const typet & subtype() const
Definition: type.h:47
const exprt & op() const
Definition: std_expr.h:293
Union constructor from single element.
Definition: std_expr.h:1602
irep_idt get_component_name() const
Definition: std_expr.h:1610
configt config
Definition: config.cpp:25
#define forall_operands(it, expr)
Definition: expr.h:18
const exprt & skip_typecast(const exprt &expr)
find the expression nested inside typecasts, if any
Definition: expr_util.cpp:215
Deprecated expression utility functions.
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
static std::string binary(const constant_exprt &src)
Definition: json_expr.cpp:205
static exprt simplify_json_expr(const exprt &src)
Definition: json_expr.cpp:32
json_objectt json(const typet &type, const namespacet &ns, const irep_idt &mode)
Output a CBMC type in json.
Definition: json_expr.cpp:99
Expressions in JSON.
Abstract interface to support a programming language.
std::unique_ptr< languaget > get_language_from_mode(const irep_idt &mode)
Get the language corresponding to the given mode.
Definition: mode.cpp:51
std::unique_ptr< languaget > get_default_language()
Returns the default language.
Definition: mode.cpp:139
const std::string integer2binary(const mp_integer &n, std::size_t width)
Definition: mp_arith.cpp:64
API to expression classes for Pointers.
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
Definition: pointer_expr.h:62
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast an exprt to an address_of_exprt.
Definition: pointer_expr.h:378
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:510
auto component(T &struct_expr, const irep_idt &name, const namespacet &ns) -> decltype(struct_expr.op0())
Definition: std_expr.cpp:48
API to expression classes.
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition: std_expr.h:2786
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:189
const typecast_exprt & to_typecast_expr(const exprt &expr)
Cast an exprt to a typecast_exprt.
Definition: std_expr.h:1900
const union_exprt & to_union_expr(const exprt &expr)
Cast an exprt to a union_exprt.
Definition: std_expr.h:1648
const member_exprt & to_member_expr(const exprt &expr)
Cast an exprt to a member_exprt.
Definition: std_expr.h:2697
const unary_exprt & to_unary_expr(const exprt &expr)
Cast an exprt to a unary_exprt.
Definition: std_expr.h:328
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition: std_expr.h:1382
const vector_typet & to_vector_type(const typet &type)
Cast a typet to a vector_typet.
Definition: std_types.h:1000
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:308
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:813
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition: std_types.h:474
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
bool NULL_is_zero
Definition: config.h:167