@@ -15,6 +15,8 @@ Author: Peter Schrammel
15
15
#include " std_expr.h"
16
16
#include " config.h"
17
17
#include " identifier.h"
18
+ #include " language.h"
19
+ #include < langapi/mode.h>
18
20
19
21
#include " json_expr.h"
20
22
@@ -113,19 +115,26 @@ json_objectt json(const source_locationt &location)
113
115
Function: json
114
116
115
117
Inputs:
118
+ type - a type
119
+ ns - a namespace
120
+ mode - language in which the code was written;
121
+ for now ID_C and ID_java are supported
116
122
117
- Outputs:
123
+ Outputs: a json object
118
124
119
125
Purpose:
126
+ Output a CBMC type in json.
127
+ The `mode` argument is used to correctly report types.
120
128
121
129
\*******************************************************************/
122
130
123
131
json_objectt json (
124
132
const typet &type,
125
- const namespacet &ns)
133
+ const namespacet &ns,
134
+ const irep_idt &mode)
126
135
{
127
136
if (type.id ()==ID_symbol)
128
- return json (ns.follow (type), ns);
137
+ return json (ns.follow (type), ns, mode );
129
138
130
139
json_objectt result;
131
140
@@ -161,7 +170,7 @@ json_objectt json(
161
170
else if (type.id ()==ID_c_enum_tag)
162
171
{
163
172
// we return the base type
164
- return json (ns.follow_tag (to_c_enum_tag_type (type)).subtype (), ns);
173
+ return json (ns.follow_tag (to_c_enum_tag_type (type)).subtype (), ns, mode );
165
174
}
166
175
else if (type.id ()==ID_fixedbv)
167
176
{
@@ -172,7 +181,7 @@ json_objectt json(
172
181
else if (type.id ()==ID_pointer)
173
182
{
174
183
result[" name" ]=json_stringt (" pointer" );
175
- result[" subtype" ]=json (type.subtype (), ns);
184
+ result[" subtype" ]=json (type.subtype (), ns, mode );
176
185
}
177
186
else if (type.id ()==ID_bool)
178
187
{
@@ -181,13 +190,13 @@ json_objectt json(
181
190
else if (type.id ()==ID_array)
182
191
{
183
192
result[" name" ]=json_stringt (" array" );
184
- result[" subtype" ]=json (type.subtype (), ns);
193
+ result[" subtype" ]=json (type.subtype (), ns, mode );
185
194
}
186
195
else if (type.id ()==ID_vector)
187
196
{
188
197
result[" name" ]=json_stringt (" vector" );
189
- result[" subtype" ]=json (type.subtype (), ns);
190
- result[" size" ]=json (to_vector_type (type).size (), ns);
198
+ result[" subtype" ]=json (type.subtype (), ns, mode );
199
+ result[" size" ]=json (to_vector_type (type).size (), ns, mode );
191
200
}
192
201
else if (type.id ()==ID_struct)
193
202
{
@@ -199,7 +208,7 @@ json_objectt json(
199
208
{
200
209
json_objectt &e=members.push_back ().make_object ();
201
210
e[" name" ]=json_stringt (id2string (component.get_name ()));
202
- e[" type" ]=json (component.type (), ns);
211
+ e[" type" ]=json (component.type (), ns, mode );
203
212
}
204
213
}
205
214
else if (type.id ()==ID_union)
@@ -212,7 +221,7 @@ json_objectt json(
212
221
{
213
222
json_objectt &e=members.push_back ().make_object ();
214
223
e[" name" ]=json_stringt (id2string (component.get_name ()));
215
- e[" type" ]=json (component.type (), ns);
224
+ e[" type" ]=json (component.type (), ns, mode );
216
225
}
217
226
}
218
227
else
@@ -226,16 +235,23 @@ json_objectt json(
226
235
Function: json
227
236
228
237
Inputs:
238
+ expr - an expression
239
+ ns - a namespace
240
+ mode - language in which the code was written;
241
+ for now ID_C and ID_java are supported
229
242
230
- Outputs:
243
+ Outputs: a json object
231
244
232
245
Purpose:
246
+ Output a CBMC expression in json.
247
+ The mode is used to correctly report types.
233
248
234
249
\*******************************************************************/
235
250
236
251
json_objectt json (
237
252
const exprt &expr,
238
- const namespacet &ns)
253
+ const namespacet &ns,
254
+ const irep_idt &mode)
239
255
{
240
256
json_objectt result;
241
257
@@ -258,42 +274,47 @@ json_objectt json(
258
274
type.id ()==ID_c_bit_field?type.subtype ():
259
275
type;
260
276
261
- bool is_signed=underlying_type.id ()==ID_signedbv;
262
-
263
- std::string sig=is_signed?" " :" unsigned " ;
277
+ languaget *lang;
278
+ if (mode==ID_unknown)
279
+ lang=get_default_language ();
280
+ else
281
+ {
282
+ lang=get_language_from_mode (mode);
283
+ if (!lang)
284
+ lang=get_default_language ();
285
+ }
264
286
265
- if (width==config.ansi_c .char_width )
266
- result[" c_type" ]=json_stringt (sig+" char" );
267
- else if (width==config.ansi_c .int_width )
268
- result[" c_type" ]=json_stringt (sig+" int" );
269
- else if (width==config.ansi_c .short_int_width )
270
- result[" c_type" ]=json_stringt (sig+" short int" );
271
- else if (width==config.ansi_c .long_int_width )
272
- result[" c_type" ]=json_stringt (sig+" long int" );
273
- else if (width==config.ansi_c .long_long_int_width )
274
- result[" c_type" ]=json_stringt (sig+" long long int" );
287
+ std::string type_string;
288
+ if (!lang->from_type (underlying_type, type_string, ns))
289
+ result[" type" ]=json_stringt (type_string);
290
+ else
291
+ assert (false && " unknown type" );
275
292
276
293
mp_integer i;
277
294
if (!to_integer (expr, i))
278
295
result[" data" ]=json_stringt (integer2string (i));
296
+ else
297
+ assert (false && " could not convert data to integer" );
279
298
}
280
299
else if (type.id ()==ID_c_enum)
281
300
{
282
301
result[" name" ]=json_stringt (" integer" );
283
302
result[" binary" ]=json_stringt (id2string (constant_expr.get_value ()));
284
303
result[" width" ]=json_numbert (type.subtype ().get_string (ID_width));
285
- result[" c_type " ]=json_stringt (" enum" );
304
+ result[" type " ]=json_stringt (" enum" );
286
305
287
306
mp_integer i;
288
307
if (!to_integer (expr, i))
289
308
result[" data" ]=json_stringt (integer2string (i));
309
+ else
310
+ assert (false && " could not convert data to integer" );
290
311
}
291
312
else if (type.id ()==ID_c_enum_tag)
292
313
{
293
314
constant_exprt tmp;
294
315
tmp.type ()=ns.follow_tag (to_c_enum_tag_type (type));
295
- tmp.set_value (constant_expr .get_value ());
296
- return json (tmp, ns);
316
+ tmp.set_value (to_constant_expr (expr) .get_value ());
317
+ return json (tmp, ns, mode );
297
318
}
298
319
else if (type.id ()==ID_bv)
299
320
{
@@ -342,7 +363,7 @@ json_objectt json(
342
363
else if (type.id ()==ID_c_bool)
343
364
{
344
365
result[" name" ]=json_stringt (" integer" );
345
- result[" c_type " ]=json_stringt (" _Bool" );
366
+ result[" type " ]=json_stringt (" _Bool" );
346
367
result[" binary" ]=json_stringt (expr.get_string (ID_value));
347
368
mp_integer b;
348
369
to_integer (to_constant_expr (expr), b);
@@ -369,7 +390,7 @@ json_objectt json(
369
390
{
370
391
json_objectt &e=elements.push_back ().make_object ();
371
392
e[" index" ]=json_numbert (std::to_string (index ));
372
- e[" value" ]=json (*it, ns);
393
+ e[" value" ]=json (*it, ns, mode );
373
394
index ++;
374
395
}
375
396
}
@@ -388,7 +409,7 @@ json_objectt json(
388
409
for (unsigned m=0 ; m<expr.operands ().size (); m++)
389
410
{
390
411
json_objectt &e=members.push_back ().make_object ();
391
- e[" value" ]=json (expr.operands ()[m], ns);
412
+ e[" value" ]=json (expr.operands ()[m], ns, mode );
392
413
e[" name" ]=json_stringt (id2string (components[m].get_name ()));
393
414
}
394
415
}
@@ -400,7 +421,7 @@ json_objectt json(
400
421
assert (expr.operands ().size ()==1 );
401
422
402
423
json_objectt &e=result[" member" ].make_object ();
403
- e[" value" ]=json (expr.op0 (), ns);
424
+ e[" value" ]=json (expr.op0 (), ns, mode );
404
425
e[" name" ]=json_stringt (id2string (to_union_expr (expr).get_component_name ()));
405
426
}
406
427
else
0 commit comments