GCC Middle and Back End API Reference
json.h
Go to the documentation of this file.
1/* JSON trees
2 Copyright (C) 2017-2024 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/* Implementation of JSON, a lightweight data-interchange format.
25
26 See http://www.json.org/
27 and http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
28 and https://tools.ietf.org/html/rfc7159
29
30 Supports creating a DOM-like tree of json::value *, and then dumping
31 json::value * to text. */
32
33/* TODO: `libcpp/mkdeps.cc` wants JSON writing support for p1689r5 output;
34 extract this code and move to libiberty. */
35
36namespace json
37{
38
39/* Forward decls of json::value and its subclasses (using indentation
40 to denote inheritance. */
41
42class value;
43 class object;
44 class array;
45 class float_number;
46 class integer_number;
47 class string;
48 class literal;
49
50/* An enum for discriminating the subclasses of json::value. */
51
52enum kind
53{
54 /* class json::object. */
56
57 /* class json::array. */
59
60 /* class json::integer_number. */
62
63 /* class json::float_number. */
65
66 /* class json::string. */
68
69 /* class json::literal uses these three values to identify the
70 particular literal. */
74};
75
76/* Base class of JSON value. */
77
78class value
79{
80 public:
81 virtual ~value () {}
82 virtual enum kind get_kind () const = 0;
83 virtual void print (pretty_printer *pp, bool formatted) const = 0;
84
85 void dump (FILE *, bool formatted) const;
86};
87
88/* Subclass of value for objects: a collection of key/value pairs
89 preserving the ordering in which keys were inserted.
90
91 Preserving the order eliminates non-determinism in the output,
92 making it easier for the user to compare repeated invocations. */
93
94class object : public value
95{
96 public:
97 ~object ();
98
99 enum kind get_kind () const final override { return JSON_OBJECT; }
100 void print (pretty_printer *pp, bool formatted) const final override;
101
102 void set (const char *key, value *v);
103 value *get (const char *key) const;
104
105 void set_string (const char *key, const char *utf8_value);
106 void set_integer (const char *key, long v);
107 void set_float (const char *key, double v);
108
109 /* Set to literal true/false. */
110 void set_bool (const char *key, bool v);
111
112 private:
113 typedef hash_map <char *, value *,
116
117 /* Keep track of order in which keys were inserted. */
118 auto_vec <const char *> m_keys;
119};
120
121/* Subclass of value for arrays. */
122
123class array : public value
124{
125 public:
126 ~array ();
127
128 enum kind get_kind () const final override { return JSON_ARRAY; }
129 void print (pretty_printer *pp, bool formatted) const final override;
130
131 void append (value *v);
132
133 private:
135};
136
137/* Subclass of value for floating-point numbers. */
138
139class float_number : public value
140{
141 public:
143
144 enum kind get_kind () const final override { return JSON_FLOAT; }
145 void print (pretty_printer *pp, bool formatted) const final override;
146
147 double get () const { return m_value; }
148
149 private:
150 double m_value;
151};
152
153/* Subclass of value for integer-valued numbers. */
154
155class integer_number : public value
156{
157 public:
159
160 enum kind get_kind () const final override { return JSON_INTEGER; }
161 void print (pretty_printer *pp, bool formatted) const final override;
162
163 long get () const { return m_value; }
164
165 private:
167};
168
169
170/* Subclass of value for strings. */
171
172class string : public value
173{
174 public:
175 explicit string (const char *utf8);
176 string (const char *utf8, size_t len);
178
179 enum kind get_kind () const final override { return JSON_STRING; }
180 void print (pretty_printer *pp, bool formatted) const final override;
181
182 const char *get_string () const { return m_utf8; }
183 size_t get_length () const { return m_len; }
184
185 private:
186 char *m_utf8;
187 size_t m_len;
188};
189
190/* Subclass of value for the three JSON literals "true", "false",
191 and "null". */
192
193class literal : public value
194{
195 public:
197
198 /* Construct literal for a boolean value. */
200
201 enum kind get_kind () const final override { return m_kind; }
202 void print (pretty_printer *pp, bool formatted) const final override;
203
204 private:
206};
207
208} // namespace json
209
210#endif /* GCC_JSON_H */
Definition vec.h:1656
Definition hash-map.h:40
Definition json.h:124
enum kind get_kind() const final override
Definition json.h:128
void print(pretty_printer *pp, bool formatted) const final override
Definition json.cc:243
auto_vec< value * > m_elements
Definition json.h:134
~array()
Definition json.cc:232
void append(value *v)
Definition json.cc:273
Definition json.h:140
enum kind get_kind() const final override
Definition json.h:144
float_number(double value)
Definition json.h:142
void print(pretty_printer *pp, bool formatted) const final override
Definition json.cc:284
double get() const
Definition json.h:147
double m_value
Definition json.h:150
Definition json.h:156
long m_value
Definition json.h:166
enum kind get_kind() const final override
Definition json.h:160
integer_number(long value)
Definition json.h:158
long get() const
Definition json.h:163
void print(pretty_printer *pp, bool formatted) const final override
Definition json.cc:297
Definition json.h:194
void print(pretty_printer *pp, bool formatted) const final override
Definition json.cc:339
literal(bool value)
Definition json.h:199
enum kind m_kind
Definition json.h:205
literal(enum kind kind)
Definition json.h:196
enum kind get_kind() const final override
Definition json.h:201
Definition json.h:95
hash_map< char *, value *, simple_hashmap_traits< nofree_string_hash, value * > > map_t
Definition json.h:114
void set(const char *key, value *v)
Definition json.cc:152
void set_string(const char *key, const char *utf8_value)
Definition json.cc:195
void set_float(const char *key, double v)
Definition json.cc:213
enum kind get_kind() const final override
Definition json.h:99
auto_vec< const char * > m_keys
Definition json.h:118
void set_bool(const char *key, bool v)
Definition json.cc:222
map_t m_map
Definition json.h:115
void set_integer(const char *key, long v)
Definition json.cc:204
value * get(const char *key) const
Definition json.cc:180
~object()
Definition json.cc:98
void print(pretty_printer *pp, bool formatted) const final override
Definition json.cc:110
Definition json.h:173
string(const char *utf8)
Definition json.cc:310
enum kind get_kind() const final override
Definition json.h:179
~string()
Definition json.h:177
const char * get_string() const
Definition json.h:182
size_t m_len
Definition json.h:187
void print(pretty_printer *pp, bool formatted) const final override
Definition json.cc:328
char * m_utf8
Definition json.h:186
size_t get_length() const
Definition json.h:183
Definition json.h:79
virtual enum kind get_kind() const =0
virtual void print(pretty_printer *pp, bool formatted) const =0
void dump(FILE *, bool formatted) const
Definition json.cc:85
virtual ~value()
Definition json.h:81
Definition pretty-print.h:255
void final(rtx_insn *first, FILE *file, int optimize_p)
Definition final.cc:2002
free(str)
Definition diagnostic.h:193
kind
Definition json.h:53
@ JSON_ARRAY
Definition json.h:58
@ JSON_FALSE
Definition json.h:72
@ JSON_INTEGER
Definition json.h:61
@ JSON_NULL
Definition json.h:73
@ JSON_OBJECT
Definition json.h:55
@ JSON_TRUE
Definition json.h:71
@ JSON_FLOAT
Definition json.h:64
@ JSON_STRING
Definition json.h:67
Definition hash-map-traits.h:33