1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
/* Classes for representing XML trees.
Copyright (C) 2024-2025 Free Software Foundation, Inc.
Contributed by David Malcolm <dmalcolm@redhat.com>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#ifndef GCC_XML_H
#define GCC_XML_H
namespace xml {
// Forward decls; indentation reflects inheritance
struct node;
struct text;
struct node_with_children;
struct document;
struct element;
struct doctypedecl;
struct comment;
struct raw;
struct node
{
virtual ~node () {}
virtual void write_as_xml (pretty_printer *pp,
int depth, bool indent) const = 0;
virtual text *dyn_cast_text ()
{
return nullptr;
}
virtual element *dyn_cast_element ()
{
return nullptr;
}
void dump (FILE *out) const;
void DEBUG_FUNCTION dump () const { dump (stderr); }
};
struct text : public node
{
text (std::string str)
: m_str (std::move (str))
{}
void write_as_xml (pretty_printer *pp,
int depth, bool indent) const final override;
text *dyn_cast_text () final override
{
return this;
}
std::string m_str;
};
struct node_with_children : public node
{
void add_child (std::unique_ptr<node> node);
void add_text (std::string str);
void add_text_from_pp (pretty_printer &pp);
void add_comment (std::string str);
element *find_child_element (std::string kind) const;
std::vector<std::unique_ptr<node>> m_children;
};
struct document : public node_with_children
{
void write_as_xml (pretty_printer *pp,
int depth, bool indent) const final override;
std::unique_ptr<doctypedecl> m_doctypedecl;
};
struct doctypedecl : public node
{
// still abstract
};
struct element : public node_with_children
{
element (std::string kind, bool preserve_whitespace)
: m_kind (std::move (kind)),
m_preserve_whitespace (preserve_whitespace)
{}
element *dyn_cast_element () final override
{
return this;
}
void write_as_xml (pretty_printer *pp,
int depth, bool indent) const final override;
void set_attr (const char *name, std::string value);
const char *get_attr (const char *name) const;
std::string m_kind;
bool m_preserve_whitespace;
std::map<std::string, std::string> m_attributes;
std::vector<std::string> m_key_insertion_order;
};
/* An XML comment. */
struct comment : public node
{
comment (std::string text)
: m_text (std::move (text))
{
}
void write_as_xml (pretty_printer *pp,
int depth, bool indent) const final override;
std::string m_text;
};
/* A fragment of raw XML source, to be spliced in directly.
Use sparingly. */
struct raw : public node
{
raw (std::string xml_src)
: m_xml_src (xml_src)
{
}
void write_as_xml (pretty_printer *pp,
int depth, bool indent) const final override;
std::string m_xml_src;
};
} // namespace xml
#endif /* GCC_XML_H. */
|