aboutsummaryrefslogtreecommitdiff
path: root/gcc/xml.h
blob: 523a44dd146f9664fade937708a0adb54d5bc151 (plain)
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
/* 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 node
{
  virtual ~node () {}
  virtual void write_as_xml (pretty_printer *pp,
			     int depth, bool indent) const = 0;
  virtual text *dyn_cast_text ()
  {
    return 0;
  }
  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);

  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;
};

struct element : public node_with_children
{
  element (std::string kind, bool preserve_whitespace)
    : m_kind (std::move (kind)),
    m_preserve_whitespace (preserve_whitespace)
  {}

  void write_as_xml (pretty_printer *pp,
		     int depth, bool indent) const final override;

  void set_attr (const char *name, std::string value);

  std::string m_kind;
  bool m_preserve_whitespace;
  std::map<std::string, std::string> m_attributes;
  std::vector<std::string> m_key_insertion_order;
};

/* 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.  */