summaryrefslogtreecommitdiff
path: root/externals/demangle/ItaniumDemangle.cpp
diff options
context:
space:
mode:
authorGravatar Kelebek12023-01-13 21:06:13 +0000
committerGravatar Kelebek12023-01-14 04:43:21 +0000
commit80a55c1663ac600103e3d475c1f72b04e2e76f0f (patch)
treeac18dcb7f4714b3324733724edd449c77356345c /externals/demangle/ItaniumDemangle.cpp
parentMerge pull request #9605 from german77/mouse_mapping (diff)
downloadyuzu-80a55c1663ac600103e3d475c1f72b04e2e76f0f.tar.gz
yuzu-80a55c1663ac600103e3d475c1f72b04e2e76f0f.tar.xz
yuzu-80a55c1663ac600103e3d475c1f72b04e2e76f0f.zip
Add stacktrace symbol demangling
Diffstat (limited to 'externals/demangle/ItaniumDemangle.cpp')
-rw-r--r--externals/demangle/ItaniumDemangle.cpp588
1 files changed, 588 insertions, 0 deletions
diff --git a/externals/demangle/ItaniumDemangle.cpp b/externals/demangle/ItaniumDemangle.cpp
new file mode 100644
index 000000000..3a3cbda18
--- /dev/null
+++ b/externals/demangle/ItaniumDemangle.cpp
@@ -0,0 +1,588 @@
1//===------------------------- ItaniumDemangle.cpp ------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-FileCopyrightText: Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10// FIXME: (possibly) incomplete list of features that clang mangles that this
11// file does not yet support:
12// - C++ modules TS
13
14#include "Demangle.h"
15#include "ItaniumDemangle.h"
16
17#include <cassert>
18#include <cctype>
19#include <cstdio>
20#include <cstdlib>
21#include <cstring>
22#include <functional>
23#include <numeric>
24#include <utility>
25#include <vector>
26
27using namespace llvm;
28using namespace llvm::itanium_demangle;
29
30constexpr const char *itanium_demangle::FloatData<float>::spec;
31constexpr const char *itanium_demangle::FloatData<double>::spec;
32constexpr const char *itanium_demangle::FloatData<long double>::spec;
33
34// <discriminator> := _ <non-negative number> # when number < 10
35// := __ <non-negative number> _ # when number >= 10
36// extension := decimal-digit+ # at the end of string
37const char *itanium_demangle::parse_discriminator(const char *first,
38 const char *last) {
39 // parse but ignore discriminator
40 if (first != last) {
41 if (*first == '_') {
42 const char *t1 = first + 1;
43 if (t1 != last) {
44 if (std::isdigit(*t1))
45 first = t1 + 1;
46 else if (*t1 == '_') {
47 for (++t1; t1 != last && std::isdigit(*t1); ++t1)
48 ;
49 if (t1 != last && *t1 == '_')
50 first = t1 + 1;
51 }
52 }
53 } else if (std::isdigit(*first)) {
54 const char *t1 = first + 1;
55 for (; t1 != last && std::isdigit(*t1); ++t1)
56 ;
57 if (t1 == last)
58 first = last;
59 }
60 }
61 return first;
62}
63
64#ifndef NDEBUG
65namespace {
66struct DumpVisitor {
67 unsigned Depth = 0;
68 bool PendingNewline = false;
69
70 template<typename NodeT> static constexpr bool wantsNewline(const NodeT *) {
71 return true;
72 }
73 static bool wantsNewline(NodeArray A) { return !A.empty(); }
74 static constexpr bool wantsNewline(...) { return false; }
75
76 template<typename ...Ts> static bool anyWantNewline(Ts ...Vs) {
77 for (bool B : {wantsNewline(Vs)...})
78 if (B)
79 return true;
80 return false;
81 }
82
83 void printStr(const char *S) { fprintf(stderr, "%s", S); }
84 void print(StringView SV) {
85 fprintf(stderr, "\"%.*s\"", (int)SV.size(), SV.begin());
86 }
87 void print(const Node *N) {
88 if (N)
89 N->visit(std::ref(*this));
90 else
91 printStr("<null>");
92 }
93 void print(NodeOrString NS) {
94 if (NS.isNode())
95 print(NS.asNode());
96 else if (NS.isString())
97 print(NS.asString());
98 else
99 printStr("NodeOrString()");
100 }
101 void print(NodeArray A) {
102 ++Depth;
103 printStr("{");
104 bool First = true;
105 for (const Node *N : A) {
106 if (First)
107 print(N);
108 else
109 printWithComma(N);
110 First = false;
111 }
112 printStr("}");
113 --Depth;
114 }
115
116 // Overload used when T is exactly 'bool', not merely convertible to 'bool'.
117 void print(bool B) { printStr(B ? "true" : "false"); }
118
119 template <class T>
120 typename std::enable_if<std::is_unsigned<T>::value>::type print(T N) {
121 fprintf(stderr, "%llu", (unsigned long long)N);
122 }
123
124 template <class T>
125 typename std::enable_if<std::is_signed<T>::value>::type print(T N) {
126 fprintf(stderr, "%lld", (long long)N);
127 }
128
129 void print(ReferenceKind RK) {
130 switch (RK) {
131 case ReferenceKind::LValue:
132 return printStr("ReferenceKind::LValue");
133 case ReferenceKind::RValue:
134 return printStr("ReferenceKind::RValue");
135 }
136 }
137 void print(FunctionRefQual RQ) {
138 switch (RQ) {
139 case FunctionRefQual::FrefQualNone:
140 return printStr("FunctionRefQual::FrefQualNone");
141 case FunctionRefQual::FrefQualLValue:
142 return printStr("FunctionRefQual::FrefQualLValue");
143 case FunctionRefQual::FrefQualRValue:
144 return printStr("FunctionRefQual::FrefQualRValue");
145 }
146 }
147 void print(Qualifiers Qs) {
148 if (!Qs) return printStr("QualNone");
149 struct QualName { Qualifiers Q; const char *Name; } Names[] = {
150 {QualConst, "QualConst"},
151 {QualVolatile, "QualVolatile"},
152 {QualRestrict, "QualRestrict"},
153 };
154 for (QualName Name : Names) {
155 if (Qs & Name.Q) {
156 printStr(Name.Name);
157 Qs = Qualifiers(Qs & ~Name.Q);
158 if (Qs) printStr(" | ");
159 }
160 }
161 }
162 void print(SpecialSubKind SSK) {
163 switch (SSK) {
164 case SpecialSubKind::allocator:
165 return printStr("SpecialSubKind::allocator");
166 case SpecialSubKind::basic_string:
167 return printStr("SpecialSubKind::basic_string");
168 case SpecialSubKind::string:
169 return printStr("SpecialSubKind::string");
170 case SpecialSubKind::istream:
171 return printStr("SpecialSubKind::istream");
172 case SpecialSubKind::ostream:
173 return printStr("SpecialSubKind::ostream");
174 case SpecialSubKind::iostream:
175 return printStr("SpecialSubKind::iostream");
176 }
177 }
178 void print(TemplateParamKind TPK) {
179 switch (TPK) {
180 case TemplateParamKind::Type:
181 return printStr("TemplateParamKind::Type");
182 case TemplateParamKind::NonType:
183 return printStr("TemplateParamKind::NonType");
184 case TemplateParamKind::Template:
185 return printStr("TemplateParamKind::Template");
186 }
187 }
188
189 void newLine() {
190 printStr("\n");
191 for (unsigned I = 0; I != Depth; ++I)
192 printStr(" ");
193 PendingNewline = false;
194 }
195
196 template<typename T> void printWithPendingNewline(T V) {
197 print(V);
198 if (wantsNewline(V))
199 PendingNewline = true;
200 }
201
202 template<typename T> void printWithComma(T V) {
203 if (PendingNewline || wantsNewline(V)) {
204 printStr(",");
205 newLine();
206 } else {
207 printStr(", ");
208 }
209
210 printWithPendingNewline(V);
211 }
212
213 struct CtorArgPrinter {
214 DumpVisitor &Visitor;
215
216 template<typename T, typename ...Rest> void operator()(T V, Rest ...Vs) {
217 if (Visitor.anyWantNewline(V, Vs...))
218 Visitor.newLine();
219 Visitor.printWithPendingNewline(V);
220 int PrintInOrder[] = { (Visitor.printWithComma(Vs), 0)..., 0 };
221 (void)PrintInOrder;
222 }
223 };
224
225 template<typename NodeT> void operator()(const NodeT *Node) {
226 Depth += 2;
227 fprintf(stderr, "%s(", itanium_demangle::NodeKind<NodeT>::name());
228 Node->match(CtorArgPrinter{*this});
229 fprintf(stderr, ")");
230 Depth -= 2;
231 }
232
233 void operator()(const ForwardTemplateReference *Node) {
234 Depth += 2;
235 fprintf(stderr, "ForwardTemplateReference(");
236 if (Node->Ref && !Node->Printing) {
237 Node->Printing = true;
238 CtorArgPrinter{*this}(Node->Ref);
239 Node->Printing = false;
240 } else {
241 CtorArgPrinter{*this}(Node->Index);
242 }
243 fprintf(stderr, ")");
244 Depth -= 2;
245 }
246};
247}
248
249void itanium_demangle::Node::dump() const {
250 DumpVisitor V;
251 visit(std::ref(V));
252 V.newLine();
253}
254#endif
255
256namespace {
257class BumpPointerAllocator {
258 struct BlockMeta {
259 BlockMeta* Next;
260 size_t Current;
261 };
262
263 static constexpr size_t AllocSize = 4096;
264 static constexpr size_t UsableAllocSize = AllocSize - sizeof(BlockMeta);
265
266 alignas(long double) char InitialBuffer[AllocSize];
267 BlockMeta* BlockList = nullptr;
268
269 void grow() {
270 char* NewMeta = static_cast<char *>(std::malloc(AllocSize));
271 if (NewMeta == nullptr)
272 std::terminate();
273 BlockList = new (NewMeta) BlockMeta{BlockList, 0};
274 }
275
276 void* allocateMassive(size_t NBytes) {
277 NBytes += sizeof(BlockMeta);
278 BlockMeta* NewMeta = reinterpret_cast<BlockMeta*>(std::malloc(NBytes));
279 if (NewMeta == nullptr)
280 std::terminate();
281 BlockList->Next = new (NewMeta) BlockMeta{BlockList->Next, 0};
282 return static_cast<void*>(NewMeta + 1);
283 }
284
285public:
286 BumpPointerAllocator()
287 : BlockList(new (InitialBuffer) BlockMeta{nullptr, 0}) {}
288
289 void* allocate(size_t N) {
290 N = (N + 15u) & ~15u;
291 if (N + BlockList->Current >= UsableAllocSize) {
292 if (N > UsableAllocSize)
293 return allocateMassive(N);
294 grow();
295 }
296 BlockList->Current += N;
297 return static_cast<void*>(reinterpret_cast<char*>(BlockList + 1) +
298 BlockList->Current - N);
299 }
300
301 void reset() {
302 while (BlockList) {
303 BlockMeta* Tmp = BlockList;
304 BlockList = BlockList->Next;
305 if (reinterpret_cast<char*>(Tmp) != InitialBuffer)
306 std::free(Tmp);
307 }
308 BlockList = new (InitialBuffer) BlockMeta{nullptr, 0};
309 }
310
311 ~BumpPointerAllocator() { reset(); }
312};
313
314class DefaultAllocator {
315 BumpPointerAllocator Alloc;
316
317public:
318 void reset() { Alloc.reset(); }
319
320 template<typename T, typename ...Args> T *makeNode(Args &&...args) {
321 return new (Alloc.allocate(sizeof(T)))
322 T(std::forward<Args>(args)...);
323 }
324
325 void *allocateNodeArray(size_t sz) {
326 return Alloc.allocate(sizeof(Node *) * sz);
327 }
328};
329} // unnamed namespace
330
331//===----------------------------------------------------------------------===//
332// Code beyond this point should not be synchronized with libc++abi.
333//===----------------------------------------------------------------------===//
334
335using Demangler = itanium_demangle::ManglingParser<DefaultAllocator>;
336
337char *llvm::itaniumDemangle(const char *MangledName, char *Buf,
338 size_t *N, int *Status) {
339 if (MangledName == nullptr || (Buf != nullptr && N == nullptr)) {
340 if (Status)
341 *Status = demangle_invalid_args;
342 return nullptr;
343 }
344
345 int InternalStatus = demangle_success;
346 Demangler Parser(MangledName, MangledName + std::strlen(MangledName));
347 OutputStream S;
348
349 Node *AST = Parser.parse();
350
351 if (AST == nullptr)
352 InternalStatus = demangle_invalid_mangled_name;
353 else if (!initializeOutputStream(Buf, N, S, 1024))
354 InternalStatus = demangle_memory_alloc_failure;
355 else {
356 assert(Parser.ForwardTemplateRefs.empty());
357 AST->print(S);
358 S += '\0';
359 if (N != nullptr)
360 *N = S.getCurrentPosition();
361 Buf = S.getBuffer();
362 }
363
364 if (Status)
365 *Status = InternalStatus;
366 return InternalStatus == demangle_success ? Buf : nullptr;
367}
368
369ItaniumPartialDemangler::ItaniumPartialDemangler()
370 : RootNode(nullptr), Context(new Demangler{nullptr, nullptr}) {}
371
372ItaniumPartialDemangler::~ItaniumPartialDemangler() {
373 delete static_cast<Demangler *>(Context);
374}
375
376ItaniumPartialDemangler::ItaniumPartialDemangler(
377 ItaniumPartialDemangler &&Other)
378 : RootNode(Other.RootNode), Context(Other.Context) {
379 Other.Context = Other.RootNode = nullptr;
380}
381
382ItaniumPartialDemangler &ItaniumPartialDemangler::
383operator=(ItaniumPartialDemangler &&Other) {
384 std::swap(RootNode, Other.RootNode);
385 std::swap(Context, Other.Context);
386 return *this;
387}
388
389// Demangle MangledName into an AST, storing it into this->RootNode.
390bool ItaniumPartialDemangler::partialDemangle(const char *MangledName) {
391 Demangler *Parser = static_cast<Demangler *>(Context);
392 size_t Len = std::strlen(MangledName);
393 Parser->reset(MangledName, MangledName + Len);
394 RootNode = Parser->parse();
395 return RootNode == nullptr;
396}
397
398static char *printNode(const Node *RootNode, char *Buf, size_t *N) {
399 OutputStream S;
400 if (!initializeOutputStream(Buf, N, S, 128))
401 return nullptr;
402 RootNode->print(S);
403 S += '\0';
404 if (N != nullptr)
405 *N = S.getCurrentPosition();
406 return S.getBuffer();
407}
408
409char *ItaniumPartialDemangler::getFunctionBaseName(char *Buf, size_t *N) const {
410 if (!isFunction())
411 return nullptr;
412
413 const Node *Name = static_cast<const FunctionEncoding *>(RootNode)->getName();
414
415 while (true) {
416 switch (Name->getKind()) {
417 case Node::KAbiTagAttr:
418 Name = static_cast<const AbiTagAttr *>(Name)->Base;
419 continue;
420 case Node::KStdQualifiedName:
421 Name = static_cast<const StdQualifiedName *>(Name)->Child;
422 continue;
423 case Node::KNestedName:
424 Name = static_cast<const NestedName *>(Name)->Name;
425 continue;
426 case Node::KLocalName:
427 Name = static_cast<const LocalName *>(Name)->Entity;
428 continue;
429 case Node::KNameWithTemplateArgs:
430 Name = static_cast<const NameWithTemplateArgs *>(Name)->Name;
431 continue;
432 default:
433 return printNode(Name, Buf, N);
434 }
435 }
436}
437
438char *ItaniumPartialDemangler::getFunctionDeclContextName(char *Buf,
439 size_t *N) const {
440 if (!isFunction())
441 return nullptr;
442 const Node *Name = static_cast<const FunctionEncoding *>(RootNode)->getName();
443
444 OutputStream S;
445 if (!initializeOutputStream(Buf, N, S, 128))
446 return nullptr;
447
448 KeepGoingLocalFunction:
449 while (true) {
450 if (Name->getKind() == Node::KAbiTagAttr) {
451 Name = static_cast<const AbiTagAttr *>(Name)->Base;
452 continue;
453 }
454 if (Name->getKind() == Node::KNameWithTemplateArgs) {
455 Name = static_cast<const NameWithTemplateArgs *>(Name)->Name;
456 continue;
457 }
458 break;
459 }
460
461 switch (Name->getKind()) {
462 case Node::KStdQualifiedName:
463 S += "std";
464 break;
465 case Node::KNestedName:
466 static_cast<const NestedName *>(Name)->Qual->print(S);
467 break;
468 case Node::KLocalName: {
469 auto *LN = static_cast<const LocalName *>(Name);
470 LN->Encoding->print(S);
471 S += "::";
472 Name = LN->Entity;
473 goto KeepGoingLocalFunction;
474 }
475 default:
476 break;
477 }
478 S += '\0';
479 if (N != nullptr)
480 *N = S.getCurrentPosition();
481 return S.getBuffer();
482}
483
484char *ItaniumPartialDemangler::getFunctionName(char *Buf, size_t *N) const {
485 if (!isFunction())
486 return nullptr;
487 auto *Name = static_cast<FunctionEncoding *>(RootNode)->getName();
488 return printNode(Name, Buf, N);
489}
490
491char *ItaniumPartialDemangler::getFunctionParameters(char *Buf,
492 size_t *N) const {
493 if (!isFunction())
494 return nullptr;
495 NodeArray Params = static_cast<FunctionEncoding *>(RootNode)->getParams();
496
497 OutputStream S;
498 if (!initializeOutputStream(Buf, N, S, 128))
499 return nullptr;
500
501 S += '(';
502 Params.printWithComma(S);
503 S += ')';
504 S += '\0';
505 if (N != nullptr)
506 *N = S.getCurrentPosition();
507 return S.getBuffer();
508}
509
510char *ItaniumPartialDemangler::getFunctionReturnType(
511 char *Buf, size_t *N) const {
512 if (!isFunction())
513 return nullptr;
514
515 OutputStream S;
516 if (!initializeOutputStream(Buf, N, S, 128))
517 return nullptr;
518
519 if (const Node *Ret =
520 static_cast<const FunctionEncoding *>(RootNode)->getReturnType())
521 Ret->print(S);
522
523 S += '\0';
524 if (N != nullptr)
525 *N = S.getCurrentPosition();
526 return S.getBuffer();
527}
528
529char *ItaniumPartialDemangler::finishDemangle(char *Buf, size_t *N) const {
530 assert(RootNode != nullptr && "must call partialDemangle()");
531 return printNode(static_cast<Node *>(RootNode), Buf, N);
532}
533
534bool ItaniumPartialDemangler::hasFunctionQualifiers() const {
535 assert(RootNode != nullptr && "must call partialDemangle()");
536 if (!isFunction())
537 return false;
538 auto *E = static_cast<const FunctionEncoding *>(RootNode);
539 return E->getCVQuals() != QualNone || E->getRefQual() != FrefQualNone;
540}
541
542bool ItaniumPartialDemangler::isCtorOrDtor() const {
543 const Node *N = static_cast<const Node *>(RootNode);
544 while (N) {
545 switch (N->getKind()) {
546 default:
547 return false;
548 case Node::KCtorDtorName:
549 return true;
550
551 case Node::KAbiTagAttr:
552 N = static_cast<const AbiTagAttr *>(N)->Base;
553 break;
554 case Node::KFunctionEncoding:
555 N = static_cast<const FunctionEncoding *>(N)->getName();
556 break;
557 case Node::KLocalName:
558 N = static_cast<const LocalName *>(N)->Entity;
559 break;
560 case Node::KNameWithTemplateArgs:
561 N = static_cast<const NameWithTemplateArgs *>(N)->Name;
562 break;
563 case Node::KNestedName:
564 N = static_cast<const NestedName *>(N)->Name;
565 break;
566 case Node::KStdQualifiedName:
567 N = static_cast<const StdQualifiedName *>(N)->Child;
568 break;
569 }
570 }
571 return false;
572}
573
574bool ItaniumPartialDemangler::isFunction() const {
575 assert(RootNode != nullptr && "must call partialDemangle()");
576 return static_cast<const Node *>(RootNode)->getKind() ==
577 Node::KFunctionEncoding;
578}
579
580bool ItaniumPartialDemangler::isSpecialName() const {
581 assert(RootNode != nullptr && "must call partialDemangle()");
582 auto K = static_cast<const Node *>(RootNode)->getKind();
583 return K == Node::KSpecialName || K == Node::KCtorVtableSpecialName;
584}
585
586bool ItaniumPartialDemangler::isData() const {
587 return !isFunction() && !isSpecialName();
588}