ItaniumDemangle.cpp 17 KB

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