-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathXMLWriter.cpp
More file actions
360 lines (329 loc) · 10.7 KB
/
Copy pathXMLWriter.cpp
File metadata and controls
360 lines (329 loc) · 10.7 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//
// This is a derivative work. originally part of the LLVM Project.
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
// Copyright (c) 2023 Krystian Stasiowski (sdkrystian@gmail.com)
// Copyright (c) 2025 Alan de Freitas (alandefreitas@gmail.com)
// Copyright (c) 2026 Gennaro Prota (gennaro.prota@gmail.com)
//
// Official repository: https://github.com/cppalliance/mrdocs
//
#include "XMLWriter.hpp"
#include <mrdocs/Metadata/Expression.hpp>
#include <mrdocs/Metadata/Type.hpp>
#include <mrdocs/Metadata/Template.hpp>
#include <mrdocs/Metadata/DocComment.hpp>
#include <mrdocs/Support/EnumToString.hpp>
#include <mrdocs/Support/MapReflectedType.hpp>
#include <string>
#include <string_view>
#include <type_traits>
namespace mrdocs::xml {
//------------------------------------------------
// Type traits
//------------------------------------------------
namespace {
template <typename T> struct is_optional : std::false_type {};
template <typename T> struct is_optional<Optional<T>> : std::true_type {};
template <typename T> struct is_vector : std::false_type {};
template <typename T, typename A> struct is_vector<std::vector<T, A>> : std::true_type {};
template <typename T> struct is_polymorphic : std::false_type {};
template <typename T> struct is_polymorphic<Polymorphic<T>> : std::true_type {};
std::string
removeSuffix(std::string_view s, std::string_view suffix)
{
return s.size() >= suffix.size() &&
s.substr(s.size() - suffix.size()) == suffix
? std::string(s.substr(0, s.size() - suffix.size()))
: std::string(s);
}
template <typename T>
std::string tagName()
{
std::string name(readableTypeName<T>());
for (std::string_view const suffix : {"Symbol", "Info", "TypeInfo", "TParam", "TArg", "Block", "Inline"})
{
name = removeSuffix(name, suffix);
}
return toKebabCase(name);
}
} // unnamed namespace
//------------------------------------------------
// XMLWriter
//------------------------------------------------
XMLWriter::XMLWriter(llvm::raw_ostream& os, Corpus const& corpus) noexcept
: tags_(os), os_(os), corpus_(corpus) {}
Expected<void>
XMLWriter::build()
{
os_ << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
<< "<mrdocs xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
" xsi:noNamespaceSchemaLocation=\"https://github.com/cppalliance/mrdocs/raw/develop/mrdocs.rnc\">\n";
(*this)(corpus_.globalNamespace());
os_ << "</mrdocs>\n";
return {};
}
//------------------------------------------------
// Symbol visitor
//------------------------------------------------
template <std::derived_from<Symbol> SymbolTy>
void
XMLWriter::operator()(SymbolTy const& I)
{
if (I.Extraction == ExtractionMode::Dependency)
return;
tags_.open(tagName<SymbolTy>());
write(I);
tags_.close(tagName<SymbolTy>());
corpus_.traverse(I, *this);
}
//------------------------------------------------
// Core recursive writer
//------------------------------------------------
template <typename T>
void
XMLWriter::write(T const& value)
{
using Type = std::decay_t<T>;
// Primitives: write as text content
if constexpr (std::is_same_v<Type, std::string> ||
std::is_same_v<Type, dom::String> ||
std::is_same_v<Type, llvm::StringRef>)
{
os_ << xmlEscape(value);
}
else if constexpr (std::is_integral_v<Type>)
{
os_ << value;
}
else if constexpr (std::is_same_v<Type, SymbolID>)
{
os_ << toBase64Str(value);
}
else if constexpr (describe::has_describe_enumerators<Type>::value)
{
os_ << toString(value);
}
// Wrappers: unwrap.
else if constexpr (is_optional<Type>::value)
{
if (value)
{
write(*value);
}
}
else if constexpr (is_polymorphic<Type>::value)
{
if (!value.valueless_after_move())
{
writePolymorphic(*value);
}
}
// Containers: iterate.
else if constexpr (is_vector<Type>::value)
{
for (auto const& item : value)
{
writeElement("item", item);
}
}
// Described types: recurse.
else // if constexpr (describe::has_describe_members<Type>::value)
{
writeMembers(value);
}
}
template <typename T>
void
XMLWriter::writeElement(std::string_view tag, T const& value)
{
using Type = std::decay_t<T>;
// Skip empty values.
if constexpr (std::is_same_v<Type, std::string> || std::is_same_v<Type, dom::String>)
{ if (value.empty()) return; }
else if constexpr (std::is_same_v<Type, bool>)
{ if (!value) return; }
else if constexpr (std::is_same_v<Type, SymbolID>)
{ if (!value) return; }
else if constexpr (is_optional<Type>::value)
{ if (!value) return; }
else if constexpr (is_vector<Type>::value)
{ if (value.empty()) return; }
else if constexpr (std::is_base_of_v<ExprInfo, Type>)
{ if (value.Written.empty()) return; }
// Primitives inline, compounds wrapped.
if constexpr (std::is_base_of_v<ExprInfo, Type>)
{
// ExprInfo and ConstantExprInfo: write the Written string.
tags_.indent() << "<" << tag << ">";
os_ << xmlEscape(value.Written);
os_ << "</" << tag << ">\n";
}
else if constexpr (std::is_same_v<Type, bool>)
{
// A false `bool` was skipped above. A true `bool` carries no
// text body: the element's presence alone encodes the value,
// so emit an empty element.
tags_.indent() << '<' << tag << "/>\n";
}
else if constexpr (std::is_same_v<Type, std::string> ||
std::is_same_v<Type, dom::String> ||
std::is_integral_v<Type> ||
std::is_same_v<Type, SymbolID> ||
describe::has_describe_enumerators<Type>::value)
{
tags_.indent() << "<" << tag << ">";
write(value);
os_ << "</" << tag << ">\n";
}
else if constexpr (is_optional<Type>::value)
{
writeElement(tag, *value);
}
else if constexpr (is_polymorphic<Type>::value)
{
if (!value.valueless_after_move())
{
writePolymorphic(*value);
}
}
else if constexpr (is_vector<Type>::value)
{
for (auto const& item : value)
writeElement(tag, item);
}
else if constexpr (describe::has_describe_members<Type>::value)
{
tags_.open(tagName<Type>());
writeMembers(value);
tags_.close(tagName<Type>());
}
}
template <typename T>
void
XMLWriter::writeMembers(T const& obj)
{
// Write base class members first.
if constexpr (describe::has_describe_bases<T>::value)
{
describe::for_each(
describe::describe_bases<T>{},
[&](auto D) {
using Base = typename std::decay_t<decltype(D)>::type;
if constexpr (describe::has_describe_members<Base>::value)
{
writeMembers(static_cast<Base const&>(obj));
}
});
}
// Write direct members.
if constexpr (describe::has_describe_members<T>::value)
{
describe::for_each(
describe::describe_members<T>{},
[&](auto D) {
writeElement(toKebabCase(D.name), obj.*D.pointer);
});
}
}
//------------------------------------------------
// Polymorphic dispatch
//------------------------------------------------
template <typename T>
void
XMLWriter::writePolymorphic(T const& value)
{
if constexpr (std::is_base_of_v<Type, T>)
{
switch (value.Kind)
{
#define INFO(Name) case TypeKind::Name: \
tags_.open(toKebabCase(#Name)); \
writeMembers(static_cast<Name##Type const&>(value)); \
tags_.close(toKebabCase(#Name)); \
break;
#include <mrdocs/Metadata/Type/TypeNodes.inc>
default: MRDOCS_UNREACHABLE();
}
}
else if constexpr (std::is_base_of_v<TParam, T>)
{
switch (value.Kind)
{
#define INFO(Name) case TParamKind::Name: \
tags_.open(toKebabCase(#Name) + "-tparam"); \
writeMembers(static_cast<Name##TParam const&>(value)); \
tags_.close(toKebabCase(#Name) + "-tparam"); \
break;
#include <mrdocs/Metadata/TParam/TParamInfoNodes.inc>
default: MRDOCS_UNREACHABLE();
}
}
else if constexpr (std::is_base_of_v<TArg, T>)
{
switch (value.Kind)
{
#define INFO(Name) case TArgKind::Name: \
tags_.open(toKebabCase(#Name) + "-targ"); \
writeMembers(static_cast<Name##TArg const&>(value)); \
tags_.close(toKebabCase(#Name) + "-targ"); \
break;
#include <mrdocs/Metadata/TArg/TArgInfoNodes.inc>
default: MRDOCS_UNREACHABLE();
}
}
else if constexpr (std::is_base_of_v<doc::Block, T>)
{
switch (value.Kind)
{
#define INFO(Name) case doc::BlockKind::Name: \
tags_.open(toKebabCase(#Name)); \
writeMembers(value.as##Name()); \
tags_.close(toKebabCase(#Name)); \
break;
#include <mrdocs/Metadata/DocComment/Block/BlockNodes.inc>
default: MRDOCS_UNREACHABLE();
}
}
else if constexpr (std::is_base_of_v<doc::Inline, T>)
{
switch (value.Kind)
{
#define INFO(Name) case doc::InlineKind::Name: \
tags_.open(toKebabCase(#Name)); \
writeMembers(value.as##Name()); \
tags_.close(toKebabCase(#Name)); \
break;
#include <mrdocs/Metadata/DocComment/Inline/InlineNodes.inc>
default: MRDOCS_UNREACHABLE();
}
}
else if constexpr (describe::has_describe_members<T>::value)
{
tags_.open(toKebabCase(readableTypeName<T>()));
writeMembers(value);
tags_.close(toKebabCase(readableTypeName<T>()));
}
}
//------------------------------------------------
// Explicit instantiations
//------------------------------------------------
#define INSTANTIATE(Type) \
template void XMLWriter::operator()<Type>(Type const&);
INSTANTIATE(NamespaceSymbol)
INSTANTIATE(EnumSymbol)
INSTANTIATE(EnumConstantSymbol)
INSTANTIATE(FunctionSymbol)
INSTANTIATE(OverloadsSymbol)
INSTANTIATE(GuideSymbol)
INSTANTIATE(ConceptSymbol)
INSTANTIATE(NamespaceAliasSymbol)
INSTANTIATE(UsingSymbol)
INSTANTIATE(RecordSymbol)
INSTANTIATE(TypedefSymbol)
INSTANTIATE(VariableSymbol)
#undef INSTANTIATE
} // namespace mrdocs::xml