1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
/* JSON trees
Copyright (C) 2017-2025 Free Software Foundation, Inc.
Contributed by David Malcolm <dmalcolm@redhat.com>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#ifndef GCC_JSON_H
#define GCC_JSON_H
#include "label-text.h"
/* Implementation of JSON, a lightweight data-interchange format.
See http://www.json.org/
and http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
and https://tools.ietf.org/html/rfc7159
Supports parsing text into a DOM-like tree of json::value *, directly
creating such trees, and dumping json::value * to text. */
/* TODO: `libcpp/mkdeps.cc` wants JSON writing support for p1689r5 output;
extract this code and move to libiberty. */
namespace json
{
/* Forward decls of json::value and its subclasses (using indentation
to denote inheritance. */
class value;
class object;
class array;
class float_number;
class integer_number;
class string;
class literal;
/* An enum for discriminating the subclasses of json::value. */
enum kind
{
/* class json::object. */
JSON_OBJECT,
/* class json::array. */
JSON_ARRAY,
/* class json::integer_number. */
JSON_INTEGER,
/* class json::float_number. */
JSON_FLOAT,
/* class json::string. */
JSON_STRING,
/* class json::literal uses these three values to identify the
particular literal. */
JSON_TRUE,
JSON_FALSE,
JSON_NULL
};
namespace pointer { // json::pointer
/* Implementation of JSON pointer (RFC 6901). */
/* A token within a JSON pointer, expressing the parent of a particular
JSON value, and how it is descended from that parent.
A JSON pointer can be built as a list of these tokens. */
struct token
{
enum class kind
{
root_value,
object_member,
array_index
};
token ();
token (json::object &parent, const char *member);
token (json::array &parent, size_t index);
token (const token &other) = delete;
token (token &&other) = delete;
~token ();
token &
operator= (const token &other) = delete;
token &
operator= (token &&other);
json::value *m_parent;
union u
{
char *u_member;
size_t u_index;
} m_data;
enum kind m_kind;
};
} // namespace json::pointer
/* Typesafe way to work with properties in JSON objects. */
template <typename Traits>
struct property
{
explicit property (const char *key)
: m_key (label_text::borrow (key))
{}
explicit property (const char *key_prefix, const char *key)
: m_key (label_text::take (concat (key_prefix, key, nullptr)))
{}
label_text m_key;
};
using string_property = property<string>;
using integer_property = property<integer_number>;
using bool_property = property<literal>;
using json_property = property<value>;
using array_of_string_property = property<array>;
template <typename EnumType>
struct enum_traits
{
typedef EnumType enum_t;
static enum_t get_unknown_value ();
static bool maybe_get_value_from_string (const char *, enum_t &out);
static const char *get_string_for_value (enum_t value);
};
template <typename EnumType>
using enum_property = property<enum_traits<EnumType>>;
/* Base class of JSON value. */
class value
{
public:
virtual ~value () {}
virtual enum kind get_kind () const = 0;
virtual void print (pretty_printer *pp, bool formatted) const = 0;
virtual std::unique_ptr<value> clone () const = 0;
void dump (FILE *, bool formatted) const;
void DEBUG_FUNCTION dump () const;
virtual object *dyn_cast_object () { return nullptr; }
virtual array *dyn_cast_array () { return nullptr; }
virtual integer_number *dyn_cast_integer_number () { return nullptr; }
virtual string *dyn_cast_string () { return nullptr; }
static int compare (const json::value &val_a, const json::value &val_b);
const pointer::token &get_pointer_token () const { return m_pointer_token; }
pointer::token m_pointer_token;
};
/* Subclass of value for objects: a collection of key/value pairs
preserving the ordering in which keys were inserted.
Preserving the order eliminates non-determinism in the output,
making it easier for the user to compare repeated invocations. */
class object : public value
{
public:
~object ();
typedef hash_map <char *, value *,
simple_hashmap_traits<nofree_string_hash, value *> > map_t;
enum kind get_kind () const final override { return JSON_OBJECT; }
void print (pretty_printer *pp, bool formatted) const final override;
std::unique_ptr<value> clone () const final override;
object *dyn_cast_object () final override { return this; }
bool is_empty () const { return m_map.is_empty (); }
void set (const char *key, value *v);
/* Set the property KEY of this object, requiring V
to be of a specific json::value subclass.
This can be used to enforce type-checking, making it easier
to comply with a schema, e.g.
obj->set<some_subclass> ("property_name", value)
leading to a compile-time error if VALUE is not of the
appropriate subclass. */
template <typename JsonType>
void set (const char *key, std::unique_ptr<JsonType> v)
{
set (key, v.release ());
}
value *get (const char *key) const;
const map_t &get_map () const { return m_map; }
void set_string (const char *key, const char *utf8_value);
void set_integer (const char *key, long v);
void set_float (const char *key, double v);
/* Set to literal true/false. */
void set_bool (const char *key, bool v);
/* Typesafe access to properties by name (such as from a schema). */
void set_string (const string_property &property, const char *utf8_value);
void set_integer (const integer_property &property, long value);
void set_bool (const bool_property &property, bool value);
void set_array_of_string (const array_of_string_property &property,
std::unique_ptr<json::array> value);
template <typename EnumType>
bool maybe_get_enum (const enum_property<EnumType> &property,
EnumType &out) const;
template <typename EnumType>
void set_enum (const enum_property<EnumType> &property,
EnumType value);
static int compare (const json::object &obj_a, const json::object &obj_b);
size_t get_num_keys () const { return m_keys.length (); }
const char *get_key (size_t i) const { return m_keys[i]; }
std::unique_ptr<object> clone_as_object () const;
private:
map_t m_map;
/* Keep track of order in which keys were inserted. */
auto_vec <const char *> m_keys;
};
/* Subclass of value for arrays. */
class array : public value
{
public:
~array ();
enum kind get_kind () const final override { return JSON_ARRAY; }
void print (pretty_printer *pp, bool formatted) const final override;
std::unique_ptr<value> clone () const final override;
array *dyn_cast_array () final override { return this; }
void append (value *v);
void append_string (const char *utf8_value);
/* Append V to this array, requiring V
to be a specific json::value subclass.
This can be used to enforce type-checking, making it easier
to comply with a schema, e.g.
arr->append<some_subclass> (value)
leading to a compile-time error if VALUE is not of the
appropriate subclass. */
template <typename JsonType>
void append (std::unique_ptr<JsonType> v)
{
append (v.release ());
}
size_t size () const { return m_elements.length (); }
value *operator[] (size_t i) const { return m_elements[i]; }
value **begin () { return m_elements.begin (); }
value **end () { return m_elements.end (); }
const value * const *begin () const { return m_elements.begin (); }
const value * const *end () const { return m_elements.end (); }
size_t length () const { return m_elements.length (); }
value *get (size_t idx) const { return m_elements[idx]; }
private:
auto_vec<value *> m_elements;
};
/* Subclass of value for floating-point numbers. */
class float_number : public value
{
public:
float_number (double value) : m_value (value) {}
enum kind get_kind () const final override { return JSON_FLOAT; }
void print (pretty_printer *pp, bool formatted) const final override;
std::unique_ptr<value> clone () const final override;
double get () const { return m_value; }
private:
double m_value;
};
/* Subclass of value for integer-valued numbers. */
class integer_number : public value
{
public:
integer_number (long value) : m_value (value) {}
enum kind get_kind () const final override { return JSON_INTEGER; }
void print (pretty_printer *pp, bool formatted) const final override;
std::unique_ptr<value> clone () const final override;
integer_number *dyn_cast_integer_number () final override { return this; }
long get () const { return m_value; }
private:
long m_value;
};
/* Subclass of value for strings. */
class string : public value
{
public:
explicit string (const char *utf8);
string (const char *utf8, size_t len);
~string () { free (m_utf8); }
enum kind get_kind () const final override { return JSON_STRING; }
void print (pretty_printer *pp, bool formatted) const final override;
std::unique_ptr<value> clone () const final override;
string *dyn_cast_string () final override { return this; }
const char *get_string () const { return m_utf8; }
size_t get_length () const { return m_len; }
private:
char *m_utf8;
size_t m_len;
};
/* Subclass of value for the three JSON literals "true", "false",
and "null". */
class literal : public value
{
public:
literal (enum kind kind) : m_kind (kind) {}
/* Construct literal for a boolean value. */
literal (bool value): m_kind (value ? JSON_TRUE : JSON_FALSE) {}
enum kind get_kind () const final override { return m_kind; }
void print (pretty_printer *pp, bool formatted) const final override;
std::unique_ptr<value> clone () const final override;
private:
enum kind m_kind;
};
template <typename EnumType>
inline bool
object::maybe_get_enum (const enum_property<EnumType> &property,
EnumType &out) const
{
if (value *jv = get (property.m_key.get ()))
if (string *jstr = jv->dyn_cast_string ())
{
if (enum_traits<EnumType>::maybe_get_value_from_string
(jstr->get_string (), out))
return true;
}
return false;
}
template <typename EnumType>
inline void
object::set_enum (const enum_property<EnumType> &property,
EnumType value)
{
const char *str
= json::enum_traits<EnumType>::get_string_for_value (value);
set_string (property.m_key.get (), str);
}
} // namespace json
template <>
template <>
inline bool
is_a_helper <json::value *>::test (json::value *)
{
return true;
}
template <>
template <>
inline bool
is_a_helper <const json::value *>::test (const json::value *)
{
return true;
}
template <>
template <>
inline bool
is_a_helper <json::object *>::test (json::value *jv)
{
return jv->get_kind () == json::JSON_OBJECT;
}
template <>
template <>
inline bool
is_a_helper <const json::object *>::test (const json::value *jv)
{
return jv->get_kind () == json::JSON_OBJECT;
}
template <>
template <>
inline bool
is_a_helper <json::array *>::test (json::value *jv)
{
return jv->get_kind () == json::JSON_ARRAY;
}
template <>
template <>
inline bool
is_a_helper <const json::array *>::test (const json::value *jv)
{
return jv->get_kind () == json::JSON_ARRAY;
}
template <>
template <>
inline bool
is_a_helper <json::float_number *>::test (json::value *jv)
{
return jv->get_kind () == json::JSON_FLOAT;
}
template <>
template <>
inline bool
is_a_helper <const json::float_number *>::test (const json::value *jv)
{
return jv->get_kind () == json::JSON_FLOAT;
}
template <>
template <>
inline bool
is_a_helper <json::integer_number *>::test (json::value *jv)
{
return jv->get_kind () == json::JSON_INTEGER;
}
template <>
template <>
inline bool
is_a_helper <const json::integer_number *>::test (const json::value *jv)
{
return jv->get_kind () == json::JSON_INTEGER;
}
template <>
template <>
inline bool
is_a_helper <json::string *>::test (json::value *jv)
{
return jv->get_kind () == json::JSON_STRING;
}
template <>
template <>
inline bool
is_a_helper <const json::string *>::test (const json::value *jv)
{
return jv->get_kind () == json::JSON_STRING;
}
template <>
template <>
inline bool
is_a_helper<json::literal *>::test (json::value *jv)
{
return (jv->get_kind () == json::JSON_TRUE
|| jv->get_kind () == json::JSON_FALSE
|| jv->get_kind () == json::JSON_NULL);
}
template <>
template <>
inline bool
is_a_helper<const json::literal *>::test (const json::value *jv)
{
return (jv->get_kind () == json::JSON_TRUE
|| jv->get_kind () == json::JSON_FALSE
|| jv->get_kind () == json::JSON_NULL);
}
#if CHECKING_P
namespace selftest {
class location;
extern void assert_print_eq (const location &loc,
const json::value &jv,
bool formatted,
const char *expected_json);
} // namespace selftest
#endif /* #if CHECKING_P */
#endif /* GCC_JSON_H */
|