c++: Implement C++26 P2573R2 - = delete("should have a reason"); [PR114458]
[official-gcc.git] / gcc / json.h
blob97c68116b32928cf9ddab69978b8013f95fd1a88
1 /* JSON trees
2 Copyright (C) 2017-2024 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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.
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.
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/>. */
21 #ifndef GCC_JSON_H
22 #define GCC_JSON_H
24 /* Implementation of JSON, a lightweight data-interchange format.
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
30 Supports creating a DOM-like tree of json::value *, and then dumping
31 json::value * to text. */
33 /* TODO: `libcpp/mkdeps.cc` wants JSON writing support for p1689r5 output;
34 extract this code and move to libiberty. */
36 namespace json
39 /* Forward decls of json::value and its subclasses (using indentation
40 to denote inheritance. */
42 class value;
43 class object;
44 class array;
45 class float_number;
46 class integer_number;
47 class string;
48 class literal;
50 /* An enum for discriminating the subclasses of json::value. */
52 enum kind
54 /* class json::object. */
55 JSON_OBJECT,
57 /* class json::array. */
58 JSON_ARRAY,
60 /* class json::integer_number. */
61 JSON_INTEGER,
63 /* class json::float_number. */
64 JSON_FLOAT,
66 /* class json::string. */
67 JSON_STRING,
69 /* class json::literal uses these three values to identify the
70 particular literal. */
71 JSON_TRUE,
72 JSON_FALSE,
73 JSON_NULL
76 /* Base class of JSON value. */
78 class value
80 public:
81 virtual ~value () {}
82 virtual enum kind get_kind () const = 0;
83 virtual void print (pretty_printer *pp, bool formatted) const = 0;
85 void dump (FILE *, bool formatted) const;
88 /* Subclass of value for objects: a collection of key/value pairs
89 preserving the ordering in which keys were inserted.
91 Preserving the order eliminates non-determinism in the output,
92 making it easier for the user to compare repeated invocations. */
94 class object : public value
96 public:
97 ~object ();
99 enum kind get_kind () const final override { return JSON_OBJECT; }
100 void print (pretty_printer *pp, bool formatted) const final override;
102 void set (const char *key, value *v);
103 value *get (const char *key) const;
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);
109 /* Set to literal true/false. */
110 void set_bool (const char *key, bool v);
112 private:
113 typedef hash_map <char *, value *,
114 simple_hashmap_traits<nofree_string_hash, value *> > map_t;
115 map_t m_map;
117 /* Keep track of order in which keys were inserted. */
118 auto_vec <const char *> m_keys;
121 /* Subclass of value for arrays. */
123 class array : public value
125 public:
126 ~array ();
128 enum kind get_kind () const final override { return JSON_ARRAY; }
129 void print (pretty_printer *pp, bool formatted) const final override;
131 void append (value *v);
133 private:
134 auto_vec<value *> m_elements;
137 /* Subclass of value for floating-point numbers. */
139 class float_number : public value
141 public:
142 float_number (double value) : m_value (value) {}
144 enum kind get_kind () const final override { return JSON_FLOAT; }
145 void print (pretty_printer *pp, bool formatted) const final override;
147 double get () const { return m_value; }
149 private:
150 double m_value;
153 /* Subclass of value for integer-valued numbers. */
155 class integer_number : public value
157 public:
158 integer_number (long value) : m_value (value) {}
160 enum kind get_kind () const final override { return JSON_INTEGER; }
161 void print (pretty_printer *pp, bool formatted) const final override;
163 long get () const { return m_value; }
165 private:
166 long m_value;
170 /* Subclass of value for strings. */
172 class string : public value
174 public:
175 explicit string (const char *utf8);
176 string (const char *utf8, size_t len);
177 ~string () { free (m_utf8); }
179 enum kind get_kind () const final override { return JSON_STRING; }
180 void print (pretty_printer *pp, bool formatted) const final override;
182 const char *get_string () const { return m_utf8; }
183 size_t get_length () const { return m_len; }
185 private:
186 char *m_utf8;
187 size_t m_len;
190 /* Subclass of value for the three JSON literals "true", "false",
191 and "null". */
193 class literal : public value
195 public:
196 literal (enum kind kind) : m_kind (kind) {}
198 /* Construct literal for a boolean value. */
199 literal (bool value): m_kind (value ? JSON_TRUE : JSON_FALSE) {}
201 enum kind get_kind () const final override { return m_kind; }
202 void print (pretty_printer *pp, bool formatted) const final override;
204 private:
205 enum kind m_kind;
208 } // namespace json
210 #endif /* GCC_JSON_H */