]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/json.h
* doc/extend.texi (Common Function Attributes): Clarify
[thirdparty/gcc.git] / gcc / json.h
CommitLineData
9dcf2a11 1/* JSON trees
fbd26352 2 Copyright (C) 2017-2019 Free Software Foundation, Inc.
9dcf2a11 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;
42 class number;
43 class string;
44 class literal;
45
46/* An enum for discriminating the subclasses of json::value. */
47
48enum kind
49{
50 /* class json::object. */
51 JSON_OBJECT,
52
53 /* class json::array. */
54 JSON_ARRAY,
55
56 /* class json::number. */
57 JSON_NUMBER,
58
59 /* class json::string. */
60 JSON_STRING,
61
62 /* class json::literal uses these three values to identify the
63 particular literal. */
64 JSON_TRUE,
65 JSON_FALSE,
66 JSON_NULL
67};
68
69/* Base class of JSON value. */
70
71class value
72{
73 public:
74 virtual ~value () {}
75 virtual enum kind get_kind () const = 0;
76 virtual void print (pretty_printer *pp) const = 0;
77
78 void dump (FILE *) const;
79};
80
81/* Subclass of value for objects: an unordered collection of
82 key/value pairs. */
83
84class object : public value
85{
86 public:
87 ~object ();
88
89 enum kind get_kind () const FINAL OVERRIDE { return JSON_OBJECT; }
90 void print (pretty_printer *pp) const FINAL OVERRIDE;
91
92 void set (const char *key, value *v);
2593ab36 93 value *get (const char *key) const;
9dcf2a11 94
95 private:
96 typedef hash_map <char *, value *,
97 simple_hashmap_traits<nofree_string_hash, value *> > map_t;
98 map_t m_map;
99};
100
101/* Subclass of value for arrays. */
102
103class array : public value
104{
105 public:
106 ~array ();
107
108 enum kind get_kind () const FINAL OVERRIDE { return JSON_ARRAY; }
109 void print (pretty_printer *pp) const FINAL OVERRIDE;
110
e4aaf333 111 void append (value *v);
9dcf2a11 112
113 private:
114 auto_vec<value *> m_elements;
115};
116
117/* Subclass of value for numbers. */
118
119class number : public value
120{
121 public:
122 number (double value) : m_value (value) {}
123
124 enum kind get_kind () const FINAL OVERRIDE { return JSON_NUMBER; }
125 void print (pretty_printer *pp) const FINAL OVERRIDE;
126
127 double get () const { return m_value; }
128
129 private:
130 double m_value;
131};
132
133/* Subclass of value for strings. */
134
135class string : public value
136{
137 public:
e4aaf333 138 string (const char *utf8);
9dcf2a11 139 ~string () { free (m_utf8); }
140
141 enum kind get_kind () const FINAL OVERRIDE { return JSON_STRING; }
142 void print (pretty_printer *pp) const FINAL OVERRIDE;
143
144 const char *get_string () const { return m_utf8; }
145
146 private:
147 char *m_utf8;
148};
149
150/* Subclass of value for the three JSON literals "true", "false",
151 and "null". */
152
153class literal : public value
154{
155 public:
156 literal (enum kind kind) : m_kind (kind) {}
157
8d11df62 158 /* Construct literal for a boolean value. */
159 literal (bool value): m_kind (value ? JSON_TRUE : JSON_FALSE) {}
160
9dcf2a11 161 enum kind get_kind () const FINAL OVERRIDE { return m_kind; }
162 void print (pretty_printer *pp) const FINAL OVERRIDE;
163
164 private:
165 enum kind m_kind;
166};
167
168} // namespace json
169
170#endif /* GCC_JSON_H */