ItaniumDemangle.cpp 17 KB

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