GCC Middle and Back End API Reference
xml.h
Go to the documentation of this file.
1/* Classes for representing XML trees.
2 Copyright (C) 2024-2025 Free Software Foundation, Inc.
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_XML_H
22#define GCC_XML_H
23
24namespace xml {
25
26// Forward decls; indentation reflects inheritance
27struct 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
36struct node
37{
38 virtual ~node () {}
39 virtual void write_as_xml (pretty_printer *pp,
40 int depth, bool indent) const = 0;
41 virtual text *dyn_cast_text ()
42 {
43 return nullptr;
44 }
46 {
47 return nullptr;
48 }
49 void dump (FILE *out) const;
50 void DEBUG_FUNCTION dump () const { dump (stderr); }
51};
52
53struct text : public node
54{
55 text (std::string str)
56 : m_str (std::move (str))
57 {}
58
60 int depth, bool indent) const final override;
61
63 {
64 return this;
65 }
66
67 std::string m_str;
68};
69
70struct node_with_children : public node
71{
72 void add_child (std::unique_ptr<node> node);
73 void add_text (std::string str);
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
83{
85 int depth, bool indent) const final override;
86
87 std::unique_ptr<doctypedecl> m_doctypedecl;
88};
89
90struct doctypedecl : public node
91{
92 // still abstract
93};
94
96{
97 element (std::string kind, bool preserve_whitespace)
98 : m_kind (std::move (kind)),
99 m_preserve_whitespace (preserve_whitespace)
100 {}
101
103 {
104 return this;
105 }
106
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;
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
121struct comment : public node
122{
123 comment (std::string text)
124 : m_text (std::move (text))
125 {
126 }
127
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
137struct raw : public node
138{
139 raw (std::string xml_src)
140 : m_xml_src (xml_src)
141 {
142 }
143
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. */
Definition pretty-print.h:241
void final(rtx_insn *first, FILE *file, int optimize_p)
Definition final.cc:2008
@ value
Definition logical-location.h:59
Definition diagnostic-path.h:28
Definition ira-emit.cc:158
Definition xml.h:122
std::string m_text
Definition xml.h:131
comment(std::string text)
Definition xml.h:123
void write_as_xml(pretty_printer *pp, int depth, bool indent) const final override
Definition xml.cc:224
Definition xml.h:91
Definition xml.h:83
std::unique_ptr< doctypedecl > m_doctypedecl
Definition xml.h:87
void write_as_xml(pretty_printer *pp, int depth, bool indent) const final override
Definition xml.cc:150
Definition xml.h:96
element * dyn_cast_element() final override
Definition xml.h:102
void set_attr(const char *name, std::string value)
Definition xml.cc:204
std::map< std::string, std::string > m_attributes
Definition xml.h:115
element(std::string kind, bool preserve_whitespace)
Definition xml.h:97
bool m_preserve_whitespace
Definition xml.h:114
std::string m_kind
Definition xml.h:113
std::vector< std::string > m_key_insertion_order
Definition xml.h:116
const char * get_attr(const char *name) const
Definition xml.cc:213
void write_as_xml(pretty_printer *pp, int depth, bool indent) const final override
Definition xml.cc:162
Definition xml.h:71
void add_text_from_pp(pretty_printer &pp)
Definition xml.cc:126
void add_child(std::unique_ptr< node > node)
Definition xml.cc:106
std::vector< std::unique_ptr< node > > m_children
Definition xml.h:79
element * find_child_element(std::string kind) const
Definition xml.cc:138
void add_text(std::string str)
Definition xml.cc:113
void add_comment(std::string str)
Definition xml.cc:132
Definition xml.h:37
void DEBUG_FUNCTION dump() const
Definition xml.h:50
virtual void write_as_xml(pretty_printer *pp, int depth, bool indent) const =0
virtual ~node()
Definition xml.h:38
virtual element * dyn_cast_element()
Definition xml.h:45
virtual text * dyn_cast_text()
Definition xml.h:41
Definition xml.h:138
std::string m_xml_src
Definition xml.h:147
void write_as_xml(pretty_printer *pp, int depth, bool indent) const final override
Definition xml.cc:242
raw(std::string xml_src)
Definition xml.h:139
Definition xml.h:54
void write_as_xml(pretty_printer *pp, int depth, bool indent) const final override
Definition xml.cc:91
std::string m_str
Definition xml.h:67
text(std::string str)
Definition xml.h:55
text * dyn_cast_text() final override
Definition xml.h:62
#define DEBUG_FUNCTION
Definition system.h:1236