ItaniumDemangle.cpp 16 KB

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