Branch data Line data Source code
1 : : /* Classes for representing XML trees.
2 : : Copyright (C) 2024-2025 Free Software Foundation, Inc.
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_XML_H
22 : : #define GCC_XML_H
23 : :
24 : : namespace xml {
25 : :
26 : : // Forward decls; indentation reflects inheritance
27 : : struct node;
28 : : struct text;
29 : : struct node_with_children;
30 : : struct document;
31 : : struct element;
32 : : struct doctypedecl;
33 : : struct comment;
34 : : struct raw;
35 : :
36 : 11967 : struct node
37 : : {
38 : : virtual ~node () {}
39 : : virtual void write_as_xml (pretty_printer *pp,
40 : : int depth, bool indent) const = 0;
41 : 508 : virtual text *dyn_cast_text ()
42 : : {
43 : 508 : return nullptr;
44 : : }
45 : 4 : virtual element *dyn_cast_element ()
46 : : {
47 : 4 : return nullptr;
48 : : }
49 : : void dump (FILE *out) const;
50 : 0 : void DEBUG_FUNCTION dump () const { dump (stderr); }
51 : : };
52 : :
53 : : struct text : public node
54 : : {
55 : 4865 : text (std::string str)
56 : 4865 : : m_str (std::move (str))
57 : : {}
58 : :
59 : : void write_as_xml (pretty_printer *pp,
60 : : int depth, bool indent) const final override;
61 : :
62 : 25600 : text *dyn_cast_text () final override
63 : : {
64 : 25600 : return this;
65 : : }
66 : :
67 : : std::string m_str;
68 : : };
69 : :
70 : 7060 : struct node_with_children : public node
71 : : {
72 : : void add_child (std::unique_ptr<node> node);
73 : : void add_text (std::string str);
74 : : void add_text_from_pp (pretty_printer &pp);
75 : : void add_comment (std::string str);
76 : :
77 : : element *find_child_element (std::string kind) const;
78 : :
79 : : std::vector<std::unique_ptr<node>> m_children;
80 : : };
81 : :
82 : 25 : struct document : public node_with_children
83 : : {
84 : : void write_as_xml (pretty_printer *pp,
85 : : int depth, bool indent) const final override;
86 : :
87 : : std::unique_ptr<doctypedecl> m_doctypedecl;
88 : : };
89 : :
90 : 17 : struct doctypedecl : public node
91 : : {
92 : : // still abstract
93 : : };
94 : :
95 : : struct element : public node_with_children
96 : : {
97 : 7035 : element (std::string kind, bool preserve_whitespace)
98 : 7035 : : m_kind (std::move (kind)),
99 : 7035 : m_preserve_whitespace (preserve_whitespace)
100 : 7035 : {}
101 : :
102 : 12 : element *dyn_cast_element () final override
103 : : {
104 : 12 : return this;
105 : : }
106 : :
107 : : void write_as_xml (pretty_printer *pp,
108 : : int depth, bool indent) const final override;
109 : :
110 : : void set_attr (const char *name, std::string value);
111 : : const char *get_attr (const char *name) const;
112 : :
113 : : std::string m_kind;
114 : : bool m_preserve_whitespace;
115 : : std::map<std::string, std::string> m_attributes;
116 : : std::vector<std::string> m_key_insertion_order;
117 : : };
118 : :
119 : : /* An XML comment. */
120 : :
121 : : struct comment : public node
122 : : {
123 : 8 : comment (std::string text)
124 : 8 : : m_text (std::move (text))
125 : : {
126 : : }
127 : :
128 : : void write_as_xml (pretty_printer *pp,
129 : : int depth, bool indent) const final override;
130 : :
131 : : std::string m_text;
132 : : };
133 : :
134 : : /* A fragment of raw XML source, to be spliced in directly.
135 : : Use sparingly. */
136 : :
137 : : struct raw : public node
138 : : {
139 : 17 : raw (std::string xml_src)
140 : 34 : : m_xml_src (xml_src)
141 : : {
142 : : }
143 : :
144 : : void write_as_xml (pretty_printer *pp,
145 : : int depth, bool indent) const final override;
146 : :
147 : : std::string m_xml_src;
148 : : };
149 : :
150 : : } // namespace xml
151 : :
152 : : #endif /* GCC_XML_H. */
|