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