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