]>
Commit | Line | Data |
---|---|---|
4a4412b9 | 1 | /* JSON trees |
a945c346 | 2 | Copyright (C) 2017-2024 Free Software Foundation, Inc. |
4a4412b9 DM |
3 | Contributed by David Malcolm <dmalcolm@redhat.com>. |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it under | |
8 | the terms of the GNU General Public License as published by the Free | |
9 | Software Foundation; either version 3, or (at your option) any later | |
10 | version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along 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 | ||
2486234b DM |
24 | /* This header uses std::unique_ptr, but <memory> can't be directly |
25 | included due to issues with macros. Hence <memory> must be included | |
26 | from system.h by defining INCLUDE_MEMORY in any source file using | |
27 | json.h. */ | |
28 | ||
29 | #ifndef INCLUDE_MEMORY | |
b8357103 | 30 | # error "You must define INCLUDE_MEMORY before including system.h to use json.h" |
2486234b DM |
31 | #endif |
32 | ||
4a4412b9 DM |
33 | /* Implementation of JSON, a lightweight data-interchange format. |
34 | ||
35 | See http://www.json.org/ | |
36 | and http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf | |
37 | and https://tools.ietf.org/html/rfc7159 | |
38 | ||
39 | Supports creating a DOM-like tree of json::value *, and then dumping | |
40 | json::value * to text. */ | |
41 | ||
024f135a BB |
42 | /* TODO: `libcpp/mkdeps.cc` wants JSON writing support for p1689r5 output; |
43 | extract this code and move to libiberty. */ | |
44 | ||
4a4412b9 DM |
45 | namespace json |
46 | { | |
47 | ||
48 | /* Forward decls of json::value and its subclasses (using indentation | |
49 | to denote inheritance. */ | |
50 | ||
51 | class value; | |
52 | class object; | |
53 | class array; | |
07622278 ML |
54 | class float_number; |
55 | class integer_number; | |
4a4412b9 DM |
56 | class string; |
57 | class literal; | |
58 | ||
59 | /* An enum for discriminating the subclasses of json::value. */ | |
60 | ||
61 | enum kind | |
62 | { | |
63 | /* class json::object. */ | |
64 | JSON_OBJECT, | |
65 | ||
66 | /* class json::array. */ | |
67 | JSON_ARRAY, | |
68 | ||
07622278 ML |
69 | /* class json::integer_number. */ |
70 | JSON_INTEGER, | |
71 | ||
72 | /* class json::float_number. */ | |
73 | JSON_FLOAT, | |
4a4412b9 DM |
74 | |
75 | /* class json::string. */ | |
76 | JSON_STRING, | |
77 | ||
78 | /* class json::literal uses these three values to identify the | |
79 | particular literal. */ | |
80 | JSON_TRUE, | |
81 | JSON_FALSE, | |
82 | JSON_NULL | |
83 | }; | |
84 | ||
85 | /* Base class of JSON value. */ | |
86 | ||
87 | class value | |
88 | { | |
89 | public: | |
90 | virtual ~value () {} | |
91 | virtual enum kind get_kind () const = 0; | |
3bd8241a | 92 | virtual void print (pretty_printer *pp, bool formatted) const = 0; |
4a4412b9 | 93 | |
3bd8241a | 94 | void dump (FILE *, bool formatted) const; |
6baa26c3 | 95 | void DEBUG_FUNCTION dump () const; |
4a4412b9 DM |
96 | }; |
97 | ||
7f1e15f7 DM |
98 | /* Subclass of value for objects: a collection of key/value pairs |
99 | preserving the ordering in which keys were inserted. | |
100 | ||
101 | Preserving the order eliminates non-determinism in the output, | |
102 | making it easier for the user to compare repeated invocations. */ | |
4a4412b9 DM |
103 | |
104 | class object : public value | |
105 | { | |
106 | public: | |
107 | ~object (); | |
108 | ||
ff171cb1 | 109 | enum kind get_kind () const final override { return JSON_OBJECT; } |
3bd8241a | 110 | void print (pretty_printer *pp, bool formatted) const final override; |
4a4412b9 | 111 | |
68c7747d DM |
112 | bool is_empty () const { return m_map.is_empty (); } |
113 | ||
4a4412b9 | 114 | void set (const char *key, value *v); |
2486234b DM |
115 | |
116 | /* Set the property KEY of this object, requiring V | |
117 | to be of a specific json::value subclass. | |
118 | ||
119 | This can be used to enforce type-checking, making it easier | |
120 | to comply with a schema, e.g. | |
121 | obj->set<some_subclass> ("property_name", value) | |
122 | leading to a compile-time error if VALUE is not of the | |
123 | appropriate subclass. */ | |
124 | template <typename JsonType> | |
125 | void set (const char *key, std::unique_ptr<JsonType> v) | |
126 | { | |
127 | set (key, v.release ()); | |
128 | } | |
129 | ||
30d3ba51 | 130 | value *get (const char *key) const; |
4a4412b9 | 131 | |
070944fd DM |
132 | void set_string (const char *key, const char *utf8_value); |
133 | void set_integer (const char *key, long v); | |
134 | void set_float (const char *key, double v); | |
135 | ||
136 | /* Set to literal true/false. */ | |
137 | void set_bool (const char *key, bool v); | |
138 | ||
4a4412b9 DM |
139 | private: |
140 | typedef hash_map <char *, value *, | |
141 | simple_hashmap_traits<nofree_string_hash, value *> > map_t; | |
142 | map_t m_map; | |
7f1e15f7 DM |
143 | |
144 | /* Keep track of order in which keys were inserted. */ | |
145 | auto_vec <const char *> m_keys; | |
4a4412b9 DM |
146 | }; |
147 | ||
148 | /* Subclass of value for arrays. */ | |
149 | ||
150 | class array : public value | |
151 | { | |
152 | public: | |
153 | ~array (); | |
154 | ||
ff171cb1 | 155 | enum kind get_kind () const final override { return JSON_ARRAY; } |
3bd8241a | 156 | void print (pretty_printer *pp, bool formatted) const final override; |
4a4412b9 | 157 | |
dad2580c | 158 | void append (value *v); |
3f14878f | 159 | void append_string (const char *utf8_value); |
4a4412b9 | 160 | |
2486234b DM |
161 | /* Append V to this array, requiring V |
162 | to be a specific json::value subclass. | |
163 | ||
164 | This can be used to enforce type-checking, making it easier | |
165 | to comply with a schema, e.g. | |
166 | arr->append<some_subclass> (value) | |
167 | leading to a compile-time error if VALUE is not of the | |
168 | appropriate subclass. */ | |
169 | template <typename JsonType> | |
170 | void append (std::unique_ptr<JsonType> v) | |
171 | { | |
172 | append (v.release ()); | |
173 | } | |
174 | ||
d7a688fc DM |
175 | size_t size () const { return m_elements.length (); } |
176 | value *operator[] (size_t i) const { return m_elements[i]; } | |
177 | ||
4a4412b9 DM |
178 | private: |
179 | auto_vec<value *> m_elements; | |
180 | }; | |
181 | ||
07622278 | 182 | /* Subclass of value for floating-point numbers. */ |
4a4412b9 | 183 | |
07622278 | 184 | class float_number : public value |
4a4412b9 DM |
185 | { |
186 | public: | |
07622278 | 187 | float_number (double value) : m_value (value) {} |
4a4412b9 | 188 | |
ff171cb1 | 189 | enum kind get_kind () const final override { return JSON_FLOAT; } |
3bd8241a | 190 | void print (pretty_printer *pp, bool formatted) const final override; |
4a4412b9 DM |
191 | |
192 | double get () const { return m_value; } | |
193 | ||
194 | private: | |
195 | double m_value; | |
196 | }; | |
197 | ||
07622278 ML |
198 | /* Subclass of value for integer-valued numbers. */ |
199 | ||
200 | class integer_number : public value | |
201 | { | |
202 | public: | |
203 | integer_number (long value) : m_value (value) {} | |
204 | ||
ff171cb1 | 205 | enum kind get_kind () const final override { return JSON_INTEGER; } |
3bd8241a | 206 | void print (pretty_printer *pp, bool formatted) const final override; |
07622278 ML |
207 | |
208 | long get () const { return m_value; } | |
209 | ||
210 | private: | |
211 | long m_value; | |
212 | }; | |
213 | ||
214 | ||
4a4412b9 DM |
215 | /* Subclass of value for strings. */ |
216 | ||
217 | class string : public value | |
218 | { | |
219 | public: | |
ee08aa9a LH |
220 | explicit string (const char *utf8); |
221 | string (const char *utf8, size_t len); | |
4a4412b9 DM |
222 | ~string () { free (m_utf8); } |
223 | ||
ff171cb1 | 224 | enum kind get_kind () const final override { return JSON_STRING; } |
3bd8241a | 225 | void print (pretty_printer *pp, bool formatted) const final override; |
4a4412b9 DM |
226 | |
227 | const char *get_string () const { return m_utf8; } | |
ee08aa9a | 228 | size_t get_length () const { return m_len; } |
4a4412b9 DM |
229 | |
230 | private: | |
231 | char *m_utf8; | |
ee08aa9a | 232 | size_t m_len; |
4a4412b9 DM |
233 | }; |
234 | ||
235 | /* Subclass of value for the three JSON literals "true", "false", | |
236 | and "null". */ | |
237 | ||
238 | class literal : public value | |
239 | { | |
240 | public: | |
241 | literal (enum kind kind) : m_kind (kind) {} | |
242 | ||
c8fda30f ML |
243 | /* Construct literal for a boolean value. */ |
244 | literal (bool value): m_kind (value ? JSON_TRUE : JSON_FALSE) {} | |
245 | ||
ff171cb1 | 246 | enum kind get_kind () const final override { return m_kind; } |
3bd8241a | 247 | void print (pretty_printer *pp, bool formatted) const final override; |
4a4412b9 DM |
248 | |
249 | private: | |
250 | enum kind m_kind; | |
251 | }; | |
252 | ||
253 | } // namespace json | |
254 | ||
255 | #endif /* GCC_JSON_H */ |