@@ -21,6 +21,8 @@ Author: Peter Schrammel
21
21
#include " config.h"
22
22
#include " identifier.h"
23
23
#include " language.h"
24
+ #include " invariant.h"
25
+
24
26
#include < langapi/mode.h>
25
27
26
28
#include < memory>
@@ -228,6 +230,27 @@ json_objectt json(
228
230
229
231
if (expr.id ()==ID_constant)
230
232
{
233
+ std::unique_ptr<languaget> lang;
234
+ if (mode==ID_unknown)
235
+ lang=std::unique_ptr<languaget>(get_default_language ());
236
+ else
237
+ {
238
+ lang=std::unique_ptr<languaget>(get_language_from_mode (mode));
239
+ if (!lang)
240
+ lang=std::unique_ptr<languaget>(get_default_language ());
241
+ }
242
+
243
+ const typet &underlying_type=
244
+ type.id ()==ID_c_bit_field?type.subtype ():
245
+ type;
246
+
247
+ std::string type_string;
248
+ bool error=lang->from_type (underlying_type, type_string, ns);
249
+ CHECK_RETURN (!error);
250
+
251
+ std::string value_string;
252
+ lang->from_expr (expr, value_string, ns);
253
+
231
254
const constant_exprt &constant_expr=to_constant_expr (expr);
232
255
if (type.id ()==ID_unsignedbv ||
233
256
type.id ()==ID_signedbv ||
@@ -238,45 +261,16 @@ json_objectt json(
238
261
result[" name" ]=json_stringt (" integer" );
239
262
result[" binary" ]=json_stringt (id2string (constant_expr.get_value ()));
240
263
result[" width" ]=json_numbert (std::to_string (width));
241
-
242
- const typet &underlying_type=
243
- type.id ()==ID_c_bit_field?type.subtype ():
244
- type;
245
-
246
- std::unique_ptr<languaget> lang;
247
- if (mode==ID_unknown)
248
- lang=std::unique_ptr<languaget>(get_default_language ());
249
- else
250
- {
251
- lang=std::unique_ptr<languaget>(get_language_from_mode (mode));
252
- if (!lang)
253
- lang=std::unique_ptr<languaget>(get_default_language ());
254
- }
255
-
256
- std::string type_string;
257
- if (!lang->from_type (underlying_type, type_string, ns))
258
- result[" type" ]=json_stringt (type_string);
259
- else
260
- assert (false && " unknown type" );
261
-
262
- mp_integer i;
263
- if (!to_integer (expr, i))
264
- result[" data" ]=json_stringt (integer2string (i));
265
- else
266
- assert (false && " could not convert data to integer" );
264
+ result[" type" ]=json_stringt (type_string);
265
+ result[" data" ]=json_stringt (value_string);
267
266
}
268
267
else if (type.id ()==ID_c_enum)
269
268
{
270
269
result[" name" ]=json_stringt (" integer" );
271
270
result[" binary" ]=json_stringt (id2string (constant_expr.get_value ()));
272
271
result[" width" ]=json_numbert (type.subtype ().get_string (ID_width));
273
272
result[" type" ]=json_stringt (" enum" );
274
-
275
- mp_integer i;
276
- if (!to_integer (expr, i))
277
- result[" data" ]=json_stringt (integer2string (i));
278
- else
279
- assert (false && " could not convert data to integer" );
273
+ result[" data" ]=json_stringt (value_string);
280
274
}
281
275
else if (type.id ()==ID_c_enum_tag)
282
276
{
@@ -309,17 +303,20 @@ json_objectt json(
309
303
else if (type.id ()==ID_pointer)
310
304
{
311
305
result[" name" ]=json_stringt (" pointer" );
306
+ result[" type" ]=json_stringt (type_string);
312
307
exprt simpl_expr=simplify_json_expr (expr, ns);
313
308
if (simpl_expr.get (ID_value)==ID_NULL ||
314
309
// remove typecast on NULL
315
310
(simpl_expr.id ()==ID_constant && simpl_expr.type ().id ()==ID_pointer &&
316
311
simpl_expr.op0 ().get (ID_value)==ID_NULL))
317
- result[" data" ]=json_stringt (" NULL " );
312
+ result[" data" ]=json_stringt (value_string );
318
313
else if (simpl_expr.id ()==ID_symbol)
319
314
{
320
315
const irep_idt &ptr_id=to_symbol_expr (simpl_expr).get_identifier ();
321
316
identifiert identifier (id2string (ptr_id));
322
- assert (!identifier.components .empty ());
317
+ DATA_INVARIANT (
318
+ !identifier.components .empty (),
319
+ " pointer identifier should have non-empty components" );
323
320
result[" data" ]=json_stringt (identifier.components .back ());
324
321
}
325
322
}
@@ -332,11 +329,10 @@ json_objectt json(
332
329
else if (type.id ()==ID_c_bool)
333
330
{
334
331
result[" name" ]=json_stringt (" integer" );
335
- result[" type" ]=json_stringt (" _Bool" );
332
+ result[" width" ]=json_numbert (type.get_string (ID_width));
333
+ result[" type" ]=json_stringt (type_string);
336
334
result[" binary" ]=json_stringt (expr.get_string (ID_value));
337
- mp_integer b;
338
- to_integer (to_constant_expr (expr), b);
339
- result[" data" ]=json_stringt (integer2string (b));
335
+ result[" data" ]=json_stringt (value_string);
340
336
}
341
337
else if (type.id ()==ID_string)
342
338
{
@@ -372,7 +368,9 @@ json_objectt json(
372
368
{
373
369
const struct_typet &struct_type=to_struct_type (type);
374
370
const struct_typet::componentst &components=struct_type.components ();
375
- assert (components.size ()==expr.operands ().size ());
371
+ DATA_INVARIANT (
372
+ components.size ()==expr.operands ().size (),
373
+ " number of struct components should match with its type" );
376
374
377
375
json_arrayt &members=result[" members" ].make_array ();
378
376
for (unsigned m=0 ; m<expr.operands ().size (); m++)
@@ -387,11 +385,10 @@ json_objectt json(
387
385
{
388
386
result[" name" ]=json_stringt (" union" );
389
387
390
- assert (expr.operands ().size ()==1 );
391
-
388
+ const union_exprt &union_expr=to_union_expr (expr);
392
389
json_objectt &e=result[" member" ].make_object ();
393
- e[" value" ]=json (expr. op0 (), ns, mode);
394
- e[" name" ]=json_stringt (id2string (to_union_expr (expr) .get_component_name ()));
390
+ e[" value" ]=json (union_expr. op (), ns, mode);
391
+ e[" name" ]=json_stringt (id2string (union_expr .get_component_name ()));
395
392
}
396
393
else
397
394
result[" name" ]=json_stringt (" unknown" );
0 commit comments