annotate libcxxabi/src/cxa_demangle.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents 1d019706d866
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===-------------------------- cxa_demangle.cpp --------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 // FIXME: (possibly) incomplete list of features that clang mangles that this
anatofuz
parents:
diff changeset
10 // file does not yet support:
anatofuz
parents:
diff changeset
11 // - C++ modules TS
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 #include "demangle/ItaniumDemangle.h"
anatofuz
parents:
diff changeset
14 #include "__cxxabi_config.h"
anatofuz
parents:
diff changeset
15 #include <cassert>
anatofuz
parents:
diff changeset
16 #include <cctype>
anatofuz
parents:
diff changeset
17 #include <cstdio>
anatofuz
parents:
diff changeset
18 #include <cstdlib>
anatofuz
parents:
diff changeset
19 #include <cstring>
anatofuz
parents:
diff changeset
20 #include <functional>
anatofuz
parents:
diff changeset
21 #include <numeric>
anatofuz
parents:
diff changeset
22 #include <utility>
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 using namespace itanium_demangle;
anatofuz
parents:
diff changeset
25
anatofuz
parents:
diff changeset
26 constexpr const char *itanium_demangle::FloatData<float>::spec;
anatofuz
parents:
diff changeset
27 constexpr const char *itanium_demangle::FloatData<double>::spec;
anatofuz
parents:
diff changeset
28 constexpr const char *itanium_demangle::FloatData<long double>::spec;
anatofuz
parents:
diff changeset
29
anatofuz
parents:
diff changeset
30 // <discriminator> := _ <non-negative number> # when number < 10
anatofuz
parents:
diff changeset
31 // := __ <non-negative number> _ # when number >= 10
anatofuz
parents:
diff changeset
32 // extension := decimal-digit+ # at the end of string
anatofuz
parents:
diff changeset
33 const char *itanium_demangle::parse_discriminator(const char *first,
anatofuz
parents:
diff changeset
34 const char *last) {
anatofuz
parents:
diff changeset
35 // parse but ignore discriminator
anatofuz
parents:
diff changeset
36 if (first != last) {
anatofuz
parents:
diff changeset
37 if (*first == '_') {
anatofuz
parents:
diff changeset
38 const char *t1 = first + 1;
anatofuz
parents:
diff changeset
39 if (t1 != last) {
anatofuz
parents:
diff changeset
40 if (std::isdigit(*t1))
anatofuz
parents:
diff changeset
41 first = t1 + 1;
anatofuz
parents:
diff changeset
42 else if (*t1 == '_') {
anatofuz
parents:
diff changeset
43 for (++t1; t1 != last && std::isdigit(*t1); ++t1)
anatofuz
parents:
diff changeset
44 ;
anatofuz
parents:
diff changeset
45 if (t1 != last && *t1 == '_')
anatofuz
parents:
diff changeset
46 first = t1 + 1;
anatofuz
parents:
diff changeset
47 }
anatofuz
parents:
diff changeset
48 }
anatofuz
parents:
diff changeset
49 } else if (std::isdigit(*first)) {
anatofuz
parents:
diff changeset
50 const char *t1 = first + 1;
anatofuz
parents:
diff changeset
51 for (; t1 != last && std::isdigit(*t1); ++t1)
anatofuz
parents:
diff changeset
52 ;
anatofuz
parents:
diff changeset
53 if (t1 == last)
anatofuz
parents:
diff changeset
54 first = last;
anatofuz
parents:
diff changeset
55 }
anatofuz
parents:
diff changeset
56 }
anatofuz
parents:
diff changeset
57 return first;
anatofuz
parents:
diff changeset
58 }
anatofuz
parents:
diff changeset
59
anatofuz
parents:
diff changeset
60 #ifndef NDEBUG
anatofuz
parents:
diff changeset
61 namespace {
anatofuz
parents:
diff changeset
62 struct DumpVisitor {
anatofuz
parents:
diff changeset
63 unsigned Depth = 0;
anatofuz
parents:
diff changeset
64 bool PendingNewline = false;
anatofuz
parents:
diff changeset
65
anatofuz
parents:
diff changeset
66 template<typename NodeT> static constexpr bool wantsNewline(const NodeT *) {
anatofuz
parents:
diff changeset
67 return true;
anatofuz
parents:
diff changeset
68 }
anatofuz
parents:
diff changeset
69 static bool wantsNewline(NodeArray A) { return !A.empty(); }
anatofuz
parents:
diff changeset
70 static constexpr bool wantsNewline(...) { return false; }
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 template<typename ...Ts> static bool anyWantNewline(Ts ...Vs) {
anatofuz
parents:
diff changeset
73 for (bool B : {wantsNewline(Vs)...})
anatofuz
parents:
diff changeset
74 if (B)
anatofuz
parents:
diff changeset
75 return true;
anatofuz
parents:
diff changeset
76 return false;
anatofuz
parents:
diff changeset
77 }
anatofuz
parents:
diff changeset
78
anatofuz
parents:
diff changeset
79 void printStr(const char *S) { fprintf(stderr, "%s", S); }
anatofuz
parents:
diff changeset
80 void print(StringView SV) {
anatofuz
parents:
diff changeset
81 fprintf(stderr, "\"%.*s\"", (int)SV.size(), SV.begin());
anatofuz
parents:
diff changeset
82 }
anatofuz
parents:
diff changeset
83 void print(const Node *N) {
anatofuz
parents:
diff changeset
84 if (N)
anatofuz
parents:
diff changeset
85 N->visit(std::ref(*this));
anatofuz
parents:
diff changeset
86 else
anatofuz
parents:
diff changeset
87 printStr("<null>");
anatofuz
parents:
diff changeset
88 }
anatofuz
parents:
diff changeset
89 void print(NodeArray A) {
anatofuz
parents:
diff changeset
90 ++Depth;
anatofuz
parents:
diff changeset
91 printStr("{");
anatofuz
parents:
diff changeset
92 bool First = true;
anatofuz
parents:
diff changeset
93 for (const Node *N : A) {
anatofuz
parents:
diff changeset
94 if (First)
anatofuz
parents:
diff changeset
95 print(N);
anatofuz
parents:
diff changeset
96 else
anatofuz
parents:
diff changeset
97 printWithComma(N);
anatofuz
parents:
diff changeset
98 First = false;
anatofuz
parents:
diff changeset
99 }
anatofuz
parents:
diff changeset
100 printStr("}");
anatofuz
parents:
diff changeset
101 --Depth;
anatofuz
parents:
diff changeset
102 }
anatofuz
parents:
diff changeset
103
anatofuz
parents:
diff changeset
104 // Overload used when T is exactly 'bool', not merely convertible to 'bool'.
anatofuz
parents:
diff changeset
105 void print(bool B) { printStr(B ? "true" : "false"); }
anatofuz
parents:
diff changeset
106
anatofuz
parents:
diff changeset
107 template <class T>
anatofuz
parents:
diff changeset
108 typename std::enable_if<std::is_unsigned<T>::value>::type print(T N) {
anatofuz
parents:
diff changeset
109 fprintf(stderr, "%llu", (unsigned long long)N);
anatofuz
parents:
diff changeset
110 }
anatofuz
parents:
diff changeset
111
anatofuz
parents:
diff changeset
112 template <class T>
anatofuz
parents:
diff changeset
113 typename std::enable_if<std::is_signed<T>::value>::type print(T N) {
anatofuz
parents:
diff changeset
114 fprintf(stderr, "%lld", (long long)N);
anatofuz
parents:
diff changeset
115 }
anatofuz
parents:
diff changeset
116
anatofuz
parents:
diff changeset
117 void print(ReferenceKind RK) {
anatofuz
parents:
diff changeset
118 switch (RK) {
anatofuz
parents:
diff changeset
119 case ReferenceKind::LValue:
anatofuz
parents:
diff changeset
120 return printStr("ReferenceKind::LValue");
anatofuz
parents:
diff changeset
121 case ReferenceKind::RValue:
anatofuz
parents:
diff changeset
122 return printStr("ReferenceKind::RValue");
anatofuz
parents:
diff changeset
123 }
anatofuz
parents:
diff changeset
124 }
anatofuz
parents:
diff changeset
125 void print(FunctionRefQual RQ) {
anatofuz
parents:
diff changeset
126 switch (RQ) {
anatofuz
parents:
diff changeset
127 case FunctionRefQual::FrefQualNone:
anatofuz
parents:
diff changeset
128 return printStr("FunctionRefQual::FrefQualNone");
anatofuz
parents:
diff changeset
129 case FunctionRefQual::FrefQualLValue:
anatofuz
parents:
diff changeset
130 return printStr("FunctionRefQual::FrefQualLValue");
anatofuz
parents:
diff changeset
131 case FunctionRefQual::FrefQualRValue:
anatofuz
parents:
diff changeset
132 return printStr("FunctionRefQual::FrefQualRValue");
anatofuz
parents:
diff changeset
133 }
anatofuz
parents:
diff changeset
134 }
anatofuz
parents:
diff changeset
135 void print(Qualifiers Qs) {
anatofuz
parents:
diff changeset
136 if (!Qs) return printStr("QualNone");
anatofuz
parents:
diff changeset
137 struct QualName { Qualifiers Q; const char *Name; } Names[] = {
anatofuz
parents:
diff changeset
138 {QualConst, "QualConst"},
anatofuz
parents:
diff changeset
139 {QualVolatile, "QualVolatile"},
anatofuz
parents:
diff changeset
140 {QualRestrict, "QualRestrict"},
anatofuz
parents:
diff changeset
141 };
anatofuz
parents:
diff changeset
142 for (QualName Name : Names) {
anatofuz
parents:
diff changeset
143 if (Qs & Name.Q) {
anatofuz
parents:
diff changeset
144 printStr(Name.Name);
anatofuz
parents:
diff changeset
145 Qs = Qualifiers(Qs & ~Name.Q);
anatofuz
parents:
diff changeset
146 if (Qs) printStr(" | ");
anatofuz
parents:
diff changeset
147 }
anatofuz
parents:
diff changeset
148 }
anatofuz
parents:
diff changeset
149 }
anatofuz
parents:
diff changeset
150 void print(SpecialSubKind SSK) {
anatofuz
parents:
diff changeset
151 switch (SSK) {
anatofuz
parents:
diff changeset
152 case SpecialSubKind::allocator:
anatofuz
parents:
diff changeset
153 return printStr("SpecialSubKind::allocator");
anatofuz
parents:
diff changeset
154 case SpecialSubKind::basic_string:
anatofuz
parents:
diff changeset
155 return printStr("SpecialSubKind::basic_string");
anatofuz
parents:
diff changeset
156 case SpecialSubKind::string:
anatofuz
parents:
diff changeset
157 return printStr("SpecialSubKind::string");
anatofuz
parents:
diff changeset
158 case SpecialSubKind::istream:
anatofuz
parents:
diff changeset
159 return printStr("SpecialSubKind::istream");
anatofuz
parents:
diff changeset
160 case SpecialSubKind::ostream:
anatofuz
parents:
diff changeset
161 return printStr("SpecialSubKind::ostream");
anatofuz
parents:
diff changeset
162 case SpecialSubKind::iostream:
anatofuz
parents:
diff changeset
163 return printStr("SpecialSubKind::iostream");
anatofuz
parents:
diff changeset
164 }
anatofuz
parents:
diff changeset
165 }
anatofuz
parents:
diff changeset
166 void print(TemplateParamKind TPK) {
anatofuz
parents:
diff changeset
167 switch (TPK) {
anatofuz
parents:
diff changeset
168 case TemplateParamKind::Type:
anatofuz
parents:
diff changeset
169 return printStr("TemplateParamKind::Type");
anatofuz
parents:
diff changeset
170 case TemplateParamKind::NonType:
anatofuz
parents:
diff changeset
171 return printStr("TemplateParamKind::NonType");
anatofuz
parents:
diff changeset
172 case TemplateParamKind::Template:
anatofuz
parents:
diff changeset
173 return printStr("TemplateParamKind::Template");
anatofuz
parents:
diff changeset
174 }
anatofuz
parents:
diff changeset
175 }
anatofuz
parents:
diff changeset
176
anatofuz
parents:
diff changeset
177 void newLine() {
anatofuz
parents:
diff changeset
178 printStr("\n");
anatofuz
parents:
diff changeset
179 for (unsigned I = 0; I != Depth; ++I)
anatofuz
parents:
diff changeset
180 printStr(" ");
anatofuz
parents:
diff changeset
181 PendingNewline = false;
anatofuz
parents:
diff changeset
182 }
anatofuz
parents:
diff changeset
183
anatofuz
parents:
diff changeset
184 template<typename T> void printWithPendingNewline(T V) {
anatofuz
parents:
diff changeset
185 print(V);
anatofuz
parents:
diff changeset
186 if (wantsNewline(V))
anatofuz
parents:
diff changeset
187 PendingNewline = true;
anatofuz
parents:
diff changeset
188 }
anatofuz
parents:
diff changeset
189
anatofuz
parents:
diff changeset
190 template<typename T> void printWithComma(T V) {
anatofuz
parents:
diff changeset
191 if (PendingNewline || wantsNewline(V)) {
anatofuz
parents:
diff changeset
192 printStr(",");
anatofuz
parents:
diff changeset
193 newLine();
anatofuz
parents:
diff changeset
194 } else {
anatofuz
parents:
diff changeset
195 printStr(", ");
anatofuz
parents:
diff changeset
196 }
anatofuz
parents:
diff changeset
197
anatofuz
parents:
diff changeset
198 printWithPendingNewline(V);
anatofuz
parents:
diff changeset
199 }
anatofuz
parents:
diff changeset
200
anatofuz
parents:
diff changeset
201 struct CtorArgPrinter {
anatofuz
parents:
diff changeset
202 DumpVisitor &Visitor;
anatofuz
parents:
diff changeset
203
anatofuz
parents:
diff changeset
204 template<typename T, typename ...Rest> void operator()(T V, Rest ...Vs) {
anatofuz
parents:
diff changeset
205 if (Visitor.anyWantNewline(V, Vs...))
anatofuz
parents:
diff changeset
206 Visitor.newLine();
anatofuz
parents:
diff changeset
207 Visitor.printWithPendingNewline(V);
anatofuz
parents:
diff changeset
208 int PrintInOrder[] = { (Visitor.printWithComma(Vs), 0)..., 0 };
anatofuz
parents:
diff changeset
209 (void)PrintInOrder;
anatofuz
parents:
diff changeset
210 }
anatofuz
parents:
diff changeset
211 };
anatofuz
parents:
diff changeset
212
anatofuz
parents:
diff changeset
213 template<typename NodeT> void operator()(const NodeT *Node) {
anatofuz
parents:
diff changeset
214 Depth += 2;
anatofuz
parents:
diff changeset
215 fprintf(stderr, "%s(", itanium_demangle::NodeKind<NodeT>::name());
anatofuz
parents:
diff changeset
216 Node->match(CtorArgPrinter{*this});
anatofuz
parents:
diff changeset
217 fprintf(stderr, ")");
anatofuz
parents:
diff changeset
218 Depth -= 2;
anatofuz
parents:
diff changeset
219 }
anatofuz
parents:
diff changeset
220
anatofuz
parents:
diff changeset
221 void operator()(const ForwardTemplateReference *Node) {
anatofuz
parents:
diff changeset
222 Depth += 2;
anatofuz
parents:
diff changeset
223 fprintf(stderr, "ForwardTemplateReference(");
anatofuz
parents:
diff changeset
224 if (Node->Ref && !Node->Printing) {
anatofuz
parents:
diff changeset
225 Node->Printing = true;
anatofuz
parents:
diff changeset
226 CtorArgPrinter{*this}(Node->Ref);
anatofuz
parents:
diff changeset
227 Node->Printing = false;
anatofuz
parents:
diff changeset
228 } else {
anatofuz
parents:
diff changeset
229 CtorArgPrinter{*this}(Node->Index);
anatofuz
parents:
diff changeset
230 }
anatofuz
parents:
diff changeset
231 fprintf(stderr, ")");
anatofuz
parents:
diff changeset
232 Depth -= 2;
anatofuz
parents:
diff changeset
233 }
anatofuz
parents:
diff changeset
234 };
anatofuz
parents:
diff changeset
235 }
anatofuz
parents:
diff changeset
236
anatofuz
parents:
diff changeset
237 void itanium_demangle::Node::dump() const {
anatofuz
parents:
diff changeset
238 DumpVisitor V;
anatofuz
parents:
diff changeset
239 visit(std::ref(V));
anatofuz
parents:
diff changeset
240 V.newLine();
anatofuz
parents:
diff changeset
241 }
anatofuz
parents:
diff changeset
242 #endif
anatofuz
parents:
diff changeset
243
anatofuz
parents:
diff changeset
244 namespace {
anatofuz
parents:
diff changeset
245 class BumpPointerAllocator {
anatofuz
parents:
diff changeset
246 struct BlockMeta {
anatofuz
parents:
diff changeset
247 BlockMeta* Next;
anatofuz
parents:
diff changeset
248 size_t Current;
anatofuz
parents:
diff changeset
249 };
anatofuz
parents:
diff changeset
250
anatofuz
parents:
diff changeset
251 static constexpr size_t AllocSize = 4096;
anatofuz
parents:
diff changeset
252 static constexpr size_t UsableAllocSize = AllocSize - sizeof(BlockMeta);
anatofuz
parents:
diff changeset
253
anatofuz
parents:
diff changeset
254 alignas(long double) char InitialBuffer[AllocSize];
anatofuz
parents:
diff changeset
255 BlockMeta* BlockList = nullptr;
anatofuz
parents:
diff changeset
256
anatofuz
parents:
diff changeset
257 void grow() {
anatofuz
parents:
diff changeset
258 char* NewMeta = static_cast<char *>(std::malloc(AllocSize));
anatofuz
parents:
diff changeset
259 if (NewMeta == nullptr)
anatofuz
parents:
diff changeset
260 std::terminate();
anatofuz
parents:
diff changeset
261 BlockList = new (NewMeta) BlockMeta{BlockList, 0};
anatofuz
parents:
diff changeset
262 }
anatofuz
parents:
diff changeset
263
anatofuz
parents:
diff changeset
264 void* allocateMassive(size_t NBytes) {
anatofuz
parents:
diff changeset
265 NBytes += sizeof(BlockMeta);
anatofuz
parents:
diff changeset
266 BlockMeta* NewMeta = reinterpret_cast<BlockMeta*>(std::malloc(NBytes));
anatofuz
parents:
diff changeset
267 if (NewMeta == nullptr)
anatofuz
parents:
diff changeset
268 std::terminate();
anatofuz
parents:
diff changeset
269 BlockList->Next = new (NewMeta) BlockMeta{BlockList->Next, 0};
anatofuz
parents:
diff changeset
270 return static_cast<void*>(NewMeta + 1);
anatofuz
parents:
diff changeset
271 }
anatofuz
parents:
diff changeset
272
anatofuz
parents:
diff changeset
273 public:
anatofuz
parents:
diff changeset
274 BumpPointerAllocator()
anatofuz
parents:
diff changeset
275 : BlockList(new (InitialBuffer) BlockMeta{nullptr, 0}) {}
anatofuz
parents:
diff changeset
276
anatofuz
parents:
diff changeset
277 void* allocate(size_t N) {
anatofuz
parents:
diff changeset
278 N = (N + 15u) & ~15u;
anatofuz
parents:
diff changeset
279 if (N + BlockList->Current >= UsableAllocSize) {
anatofuz
parents:
diff changeset
280 if (N > UsableAllocSize)
anatofuz
parents:
diff changeset
281 return allocateMassive(N);
anatofuz
parents:
diff changeset
282 grow();
anatofuz
parents:
diff changeset
283 }
anatofuz
parents:
diff changeset
284 BlockList->Current += N;
anatofuz
parents:
diff changeset
285 return static_cast<void*>(reinterpret_cast<char*>(BlockList + 1) +
anatofuz
parents:
diff changeset
286 BlockList->Current - N);
anatofuz
parents:
diff changeset
287 }
anatofuz
parents:
diff changeset
288
anatofuz
parents:
diff changeset
289 void reset() {
anatofuz
parents:
diff changeset
290 while (BlockList) {
anatofuz
parents:
diff changeset
291 BlockMeta* Tmp = BlockList;
anatofuz
parents:
diff changeset
292 BlockList = BlockList->Next;
anatofuz
parents:
diff changeset
293 if (reinterpret_cast<char*>(Tmp) != InitialBuffer)
anatofuz
parents:
diff changeset
294 std::free(Tmp);
anatofuz
parents:
diff changeset
295 }
anatofuz
parents:
diff changeset
296 BlockList = new (InitialBuffer) BlockMeta{nullptr, 0};
anatofuz
parents:
diff changeset
297 }
anatofuz
parents:
diff changeset
298
anatofuz
parents:
diff changeset
299 ~BumpPointerAllocator() { reset(); }
anatofuz
parents:
diff changeset
300 };
anatofuz
parents:
diff changeset
301
anatofuz
parents:
diff changeset
302 class DefaultAllocator {
anatofuz
parents:
diff changeset
303 BumpPointerAllocator Alloc;
anatofuz
parents:
diff changeset
304
anatofuz
parents:
diff changeset
305 public:
anatofuz
parents:
diff changeset
306 void reset() { Alloc.reset(); }
anatofuz
parents:
diff changeset
307
anatofuz
parents:
diff changeset
308 template<typename T, typename ...Args> T *makeNode(Args &&...args) {
anatofuz
parents:
diff changeset
309 return new (Alloc.allocate(sizeof(T)))
anatofuz
parents:
diff changeset
310 T(std::forward<Args>(args)...);
anatofuz
parents:
diff changeset
311 }
anatofuz
parents:
diff changeset
312
anatofuz
parents:
diff changeset
313 void *allocateNodeArray(size_t sz) {
anatofuz
parents:
diff changeset
314 return Alloc.allocate(sizeof(Node *) * sz);
anatofuz
parents:
diff changeset
315 }
anatofuz
parents:
diff changeset
316 };
anatofuz
parents:
diff changeset
317 } // unnamed namespace
anatofuz
parents:
diff changeset
318
anatofuz
parents:
diff changeset
319 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
320 // Code beyond this point should not be synchronized with LLVM.
anatofuz
parents:
diff changeset
321 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
322
anatofuz
parents:
diff changeset
323 using Demangler = itanium_demangle::ManglingParser<DefaultAllocator>;
anatofuz
parents:
diff changeset
324
anatofuz
parents:
diff changeset
325 namespace {
anatofuz
parents:
diff changeset
326 enum : int {
anatofuz
parents:
diff changeset
327 demangle_invalid_args = -3,
anatofuz
parents:
diff changeset
328 demangle_invalid_mangled_name = -2,
anatofuz
parents:
diff changeset
329 demangle_memory_alloc_failure = -1,
anatofuz
parents:
diff changeset
330 demangle_success = 0,
anatofuz
parents:
diff changeset
331 };
anatofuz
parents:
diff changeset
332 }
anatofuz
parents:
diff changeset
333
anatofuz
parents:
diff changeset
334 namespace __cxxabiv1 {
anatofuz
parents:
diff changeset
335 extern "C" _LIBCXXABI_FUNC_VIS char *
anatofuz
parents:
diff changeset
336 __cxa_demangle(const char *MangledName, char *Buf, size_t *N, int *Status) {
anatofuz
parents:
diff changeset
337 if (MangledName == nullptr || (Buf != nullptr && N == nullptr)) {
anatofuz
parents:
diff changeset
338 if (Status)
anatofuz
parents:
diff changeset
339 *Status = demangle_invalid_args;
anatofuz
parents:
diff changeset
340 return nullptr;
anatofuz
parents:
diff changeset
341 }
anatofuz
parents:
diff changeset
342
anatofuz
parents:
diff changeset
343 int InternalStatus = demangle_success;
anatofuz
parents:
diff changeset
344 Demangler Parser(MangledName, MangledName + std::strlen(MangledName));
anatofuz
parents:
diff changeset
345 OutputStream S;
anatofuz
parents:
diff changeset
346
anatofuz
parents:
diff changeset
347 Node *AST = Parser.parse();
anatofuz
parents:
diff changeset
348
anatofuz
parents:
diff changeset
349 if (AST == nullptr)
anatofuz
parents:
diff changeset
350 InternalStatus = demangle_invalid_mangled_name;
anatofuz
parents:
diff changeset
351 else if (!initializeOutputStream(Buf, N, S, 1024))
anatofuz
parents:
diff changeset
352 InternalStatus = demangle_memory_alloc_failure;
anatofuz
parents:
diff changeset
353 else {
anatofuz
parents:
diff changeset
354 assert(Parser.ForwardTemplateRefs.empty());
anatofuz
parents:
diff changeset
355 AST->print(S);
anatofuz
parents:
diff changeset
356 S += '\0';
anatofuz
parents:
diff changeset
357 if (N != nullptr)
anatofuz
parents:
diff changeset
358 *N = S.getCurrentPosition();
anatofuz
parents:
diff changeset
359 Buf = S.getBuffer();
anatofuz
parents:
diff changeset
360 }
anatofuz
parents:
diff changeset
361
anatofuz
parents:
diff changeset
362 if (Status)
anatofuz
parents:
diff changeset
363 *Status = InternalStatus;
anatofuz
parents:
diff changeset
364 return InternalStatus == demangle_success ? Buf : nullptr;
anatofuz
parents:
diff changeset
365 }
anatofuz
parents:
diff changeset
366 } // __cxxabiv1