]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/json.h
Update copyright years.
[thirdparty/gcc.git] / gcc / json.h
CommitLineData
4a4412b9 1/* JSON trees
8d9254fc 2 Copyright (C) 2017-2020 Free Software Foundation, Inc.
4a4412b9
DM
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
33namespace json
34{
35
36/* Forward decls of json::value and its subclasses (using indentation
37 to denote inheritance. */
38
39class value;
40 class object;
41 class array;
07622278
ML
42 class float_number;
43 class integer_number;
4a4412b9
DM
44 class string;
45 class literal;
46
47/* An enum for discriminating the subclasses of json::value. */
48
49enum kind
50{
51 /* class json::object. */
52 JSON_OBJECT,
53
54 /* class json::array. */
55 JSON_ARRAY,
56
07622278
ML
57 /* class json::integer_number. */
58 JSON_INTEGER,
59
60 /* class json::float_number. */
61 JSON_FLOAT,
4a4412b9
DM
62
63 /* class json::string. */
64 JSON_STRING,
65
66 /* class json::literal uses these three values to identify the
67 particular literal. */
68 JSON_TRUE,
69 JSON_FALSE,
70 JSON_NULL
71};
72
73/* Base class of JSON value. */
74
75class value
76{
77 public:
78 virtual ~value () {}
79 virtual enum kind get_kind () const = 0;
80 virtual void print (pretty_printer *pp) const = 0;
81
82 void dump (FILE *) const;
83};
84
85/* Subclass of value for objects: an unordered collection of
86 key/value pairs. */
87
88class object : public value
89{
90 public:
91 ~object ();
92
93 enum kind get_kind () const FINAL OVERRIDE { return JSON_OBJECT; }
94 void print (pretty_printer *pp) const FINAL OVERRIDE;
95
96 void set (const char *key, value *v);
30d3ba51 97 value *get (const char *key) const;
4a4412b9
DM
98
99 private:
100 typedef hash_map <char *, value *,
101 simple_hashmap_traits<nofree_string_hash, value *> > map_t;
102 map_t m_map;
103};
104
105/* Subclass of value for arrays. */
106
107class array : public value
108{
109 public:
110 ~array ();
111
112 enum kind get_kind () const FINAL OVERRIDE { return JSON_ARRAY; }
113 void print (pretty_printer *pp) const FINAL OVERRIDE;
114
dad2580c 115 void append (value *v);
4a4412b9
DM
116
117 private:
118 auto_vec<value *> m_elements;
119};
120
07622278 121/* Subclass of value for floating-point numbers. */
4a4412b9 122
07622278 123class float_number : public value
4a4412b9
DM
124{
125 public:
07622278 126 float_number (double value) : m_value (value) {}
4a4412b9 127
07622278 128 enum kind get_kind () const FINAL OVERRIDE { return JSON_FLOAT; }
4a4412b9
DM
129 void print (pretty_printer *pp) const FINAL OVERRIDE;
130
131 double get () const { return m_value; }
132
133 private:
134 double m_value;
135};
136
07622278
ML
137/* Subclass of value for integer-valued numbers. */
138
139class integer_number : public value
140{
141 public:
142 integer_number (long value) : m_value (value) {}
143
144 enum kind get_kind () const FINAL OVERRIDE { return JSON_INTEGER; }
145 void print (pretty_printer *pp) const FINAL OVERRIDE;
146
147 long get () const { return m_value; }
148
149 private:
150 long m_value;
151};
152
153
4a4412b9
DM
154/* Subclass of value for strings. */
155
156class string : public value
157{
158 public:
dad2580c 159 string (const char *utf8);
4a4412b9
DM
160 ~string () { free (m_utf8); }
161
162 enum kind get_kind () const FINAL OVERRIDE { return JSON_STRING; }
163 void print (pretty_printer *pp) const FINAL OVERRIDE;
164
165 const char *get_string () const { return m_utf8; }
166
167 private:
168 char *m_utf8;
169};
170
171/* Subclass of value for the three JSON literals "true", "false",
172 and "null". */
173
174class literal : public value
175{
176 public:
177 literal (enum kind kind) : m_kind (kind) {}
178
c8fda30f
ML
179 /* Construct literal for a boolean value. */
180 literal (bool value): m_kind (value ? JSON_TRUE : JSON_FALSE) {}
181
4a4412b9
DM
182 enum kind get_kind () const FINAL OVERRIDE { return m_kind; }
183 void print (pretty_printer *pp) const FINAL OVERRIDE;
184
185 private:
186 enum kind m_kind;
187};
188
189} // namespace json
190
191#endif /* GCC_JSON_H */