blob: be72e15c8ac916a9c0d8f722001d494e57283690 [file] [log] [blame]
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +00001//===------------------------- ItaniumDemangle.cpp ------------------------===//
Rafael Espindolab940b662016-09-06 19:16:48 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000010// FIXME: (possibly) incomplete list of features that clang mangles that this
11// file does not yet support:
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000012// - C++ modules TS
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000013
Reid Klecknerb2881f12016-09-06 19:39:56 +000014#include "llvm/Demangle/Demangle.h"
Richard Smith8a57f2e2018-08-20 19:44:01 +000015#include "llvm/Demangle/ItaniumDemangle.h"
Serge Pavlov1a095522018-05-29 07:05:41 +000016
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000017#include <cassert>
David Blaikie99e17252018-03-21 17:31:49 +000018#include <cctype>
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000019#include <cstdio>
Rafael Espindolab940b662016-09-06 19:16:48 +000020#include <cstdlib>
21#include <cstring>
David Blaikie99e17252018-03-21 17:31:49 +000022#include <numeric>
Erik Pilkington3a6fed42018-07-27 17:27:40 +000023#include <utility>
David Blaikie99e17252018-03-21 17:31:49 +000024#include <vector>
Rafael Espindolab940b662016-09-06 19:16:48 +000025
Richard Smith8a57f2e2018-08-20 19:44:01 +000026using namespace llvm;
27using namespace llvm::itanium_demangle;
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000028
Richard Smith8a57f2e2018-08-20 19:44:01 +000029constexpr const char *itanium_demangle::FloatData<float>::spec;
30constexpr const char *itanium_demangle::FloatData<double>::spec;
31constexpr const char *itanium_demangle::FloatData<long double>::spec;
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000032
Richard Smith8a57f2e2018-08-20 19:44:01 +000033// <discriminator> := _ <non-negative number> # when number < 10
34// := __ <non-negative number> _ # when number >= 10
35// extension := decimal-digit+ # at the end of string
36const char *itanium_demangle::parse_discriminator(const char *first,
37 const char *last) {
38 // parse but ignore discriminator
39 if (first != last) {
40 if (*first == '_') {
41 const char *t1 = first + 1;
42 if (t1 != last) {
43 if (std::isdigit(*t1))
44 first = t1 + 1;
45 else if (*t1 == '_') {
46 for (++t1; t1 != last && std::isdigit(*t1); ++t1)
47 ;
48 if (t1 != last && *t1 == '_')
49 first = t1 + 1;
50 }
Erik Pilkington8c7013d2018-03-25 22:49:57 +000051 }
Richard Smith8a57f2e2018-08-20 19:44:01 +000052 } else if (std::isdigit(*first)) {
53 const char *t1 = first + 1;
54 for (; t1 != last && std::isdigit(*t1); ++t1)
55 ;
56 if (t1 == last)
57 first = last;
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000058 }
59 }
Richard Smith8a57f2e2018-08-20 19:44:01 +000060 return first;
Rafael Espindolab940b662016-09-06 19:16:48 +000061}
62
Richard Smith8a57f2e2018-08-20 19:44:01 +000063#ifndef NDEBUG
64namespace {
65struct DumpVisitor {
66 unsigned Depth = 0;
67 bool PendingNewline = false;
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000068
Richard Smith8a57f2e2018-08-20 19:44:01 +000069 template<typename NodeT> static constexpr bool wantsNewline(const NodeT *) {
70 return true;
71 }
72 static bool wantsNewline(NodeArray A) { return !A.empty(); }
73 static constexpr bool wantsNewline(...) { return false; }
74
75 template<typename ...Ts> static bool anyWantNewline(Ts ...Vs) {
76 for (bool B : {wantsNewline(Vs)...})
77 if (B)
78 return true;
79 return false;
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000080 }
81
Richard Smith8a57f2e2018-08-20 19:44:01 +000082 void printStr(const char *S) { fprintf(stderr, "%s", S); }
83 void print(StringView SV) {
84 fprintf(stderr, "\"%.*s\"", (int)SV.size(), SV.begin());
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000085 }
Richard Smith8a57f2e2018-08-20 19:44:01 +000086 void print(const Node *N) {
87 if (N)
88 N->visit(std::ref(*this));
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000089 else
Richard Smith8a57f2e2018-08-20 19:44:01 +000090 printStr("<null>");
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000091 }
Richard Smith8a57f2e2018-08-20 19:44:01 +000092 void print(NodeOrString NS) {
93 if (NS.isNode())
94 print(NS.asNode());
95 else if (NS.isString())
96 print(NS.asString());
97 else
98 printStr("NodeOrString()");
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000099 }
Richard Smith8a57f2e2018-08-20 19:44:01 +0000100 void print(NodeArray A) {
101 ++Depth;
102 printStr("{");
103 bool First = true;
104 for (const Node *N : A) {
105 if (First)
106 print(N);
107 else
108 printWithComma(N);
109 First = false;
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000110 }
Richard Smith8a57f2e2018-08-20 19:44:01 +0000111 printStr("}");
112 --Depth;
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000113 }
Richard Smith8a57f2e2018-08-20 19:44:01 +0000114 // Overload used when T is exactly 'bool', not merely convertible to 'bool'.
115 template<typename T, T * = (bool*)nullptr>
116 void print(T B) {
117 printStr(B ? "true" : "false");
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000118 }
Richard Smith8a57f2e2018-08-20 19:44:01 +0000119 void print(size_t N) {
120 fprintf(stderr, "%zu", N);
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000121 }
Richard Smith8a57f2e2018-08-20 19:44:01 +0000122 void print(ReferenceKind RK) {
123 switch (RK) {
124 case ReferenceKind::LValue:
125 return printStr("ReferenceKind::LValue");
126 case ReferenceKind::RValue:
127 return printStr("ReferenceKind::RValue");
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000128 }
129 }
Richard Smith8a57f2e2018-08-20 19:44:01 +0000130 void print(FunctionRefQual RQ) {
131 switch (RQ) {
132 case FunctionRefQual::FrefQualNone:
133 return printStr("FunctionRefQual::FrefQualNone");
134 case FunctionRefQual::FrefQualLValue:
135 return printStr("FunctionRefQual::FrefQualLValue");
136 case FunctionRefQual::FrefQualRValue:
137 return printStr("FunctionRefQual::FrefQualRValue");
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000138 }
139 }
Richard Smith8a57f2e2018-08-20 19:44:01 +0000140 void print(Qualifiers Qs) {
141 if (!Qs) return printStr("QualNone");
142 struct QualName { Qualifiers Q; const char *Name; } Names[] = {
143 {QualConst, "QualConst"},
144 {QualVolatile, "QualVolatile"},
145 {QualRestrict, "QualRestrict"},
Erik Pilkingtond43931d2018-04-09 18:33:01 +0000146 };
Richard Smith8a57f2e2018-08-20 19:44:01 +0000147 for (QualName Name : Names) {
148 if (Qs & Name.Q) {
149 printStr(Name.Name);
150 Qs = Qualifiers(Qs & ~Name.Q);
151 if (Qs) printStr(" | ");
Erik Pilkingtond43931d2018-04-09 18:33:01 +0000152 }
153 }
Erik Pilkingtond43931d2018-04-09 18:33:01 +0000154 }
Richard Smith8a57f2e2018-08-20 19:44:01 +0000155 void print(SpecialSubKind SSK) {
156 switch (SSK) {
157 case SpecialSubKind::allocator:
158 return printStr("SpecialSubKind::allocator");
159 case SpecialSubKind::basic_string:
160 return printStr("SpecialSubKind::basic_string");
161 case SpecialSubKind::string:
162 return printStr("SpecialSubKind::string");
163 case SpecialSubKind::istream:
164 return printStr("SpecialSubKind::istream");
165 case SpecialSubKind::ostream:
166 return printStr("SpecialSubKind::ostream");
167 case SpecialSubKind::iostream:
168 return printStr("SpecialSubKind::iostream");
169 }
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000170 }
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000171
Richard Smith8a57f2e2018-08-20 19:44:01 +0000172 void newLine() {
173 printStr("\n");
174 for (unsigned I = 0; I != Depth; ++I)
175 printStr(" ");
176 PendingNewline = false;
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000177 }
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000178
Richard Smith8a57f2e2018-08-20 19:44:01 +0000179 template<typename T> void printWithPendingNewline(T V) {
180 print(V);
181 if (wantsNewline(V))
182 PendingNewline = true;
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000183 }
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000184
Richard Smith8a57f2e2018-08-20 19:44:01 +0000185 template<typename T> void printWithComma(T V) {
186 if (PendingNewline || wantsNewline(V)) {
187 printStr(",");
188 newLine();
189 } else {
190 printStr(", ");
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000191 }
192
Richard Smith8a57f2e2018-08-20 19:44:01 +0000193 printWithPendingNewline(V);
194 }
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000195
Richard Smith8a57f2e2018-08-20 19:44:01 +0000196 struct CtorArgPrinter {
197 DumpVisitor &Visitor;
198
199 template<typename T, typename ...Rest> void operator()(T V, Rest ...Vs) {
200 if (Visitor.anyWantNewline(V, Vs...))
201 Visitor.newLine();
202 Visitor.printWithPendingNewline(V);
203 int PrintInOrder[] = { (Visitor.printWithComma(Vs), 0)..., 0 };
204 (void)PrintInOrder;
205 }
206 };
207
208 template<typename NodeT> void operator()(const NodeT *Node) {
209 Depth += 2;
210 fprintf(stderr, "%s(", itanium_demangle::NodeKind<NodeT>::name());
211 Node->match(CtorArgPrinter{*this});
212 fprintf(stderr, ")");
213 Depth -= 2;
214 }
215
216 void operator()(const ForwardTemplateReference *Node) {
217 Depth += 2;
218 fprintf(stderr, "ForwardTemplateReference(");
219 if (Node->Ref && !Node->Printing) {
220 Node->Printing = true;
221 CtorArgPrinter{*this}(Node->Ref);
222 Node->Printing = false;
223 } else {
224 CtorArgPrinter{*this}(Node->Index);
225 }
226 fprintf(stderr, ")");
227 Depth -= 2;
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000228 }
229};
Richard Smith8a57f2e2018-08-20 19:44:01 +0000230}
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000231
Richard Smith8a57f2e2018-08-20 19:44:01 +0000232void itanium_demangle::Node::dump() const {
233 DumpVisitor V;
234 visit(std::ref(V));
235 V.newLine();
236}
Rafael Espindolab940b662016-09-06 19:16:48 +0000237#endif
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000238
Richard Smith8a57f2e2018-08-20 19:44:01 +0000239namespace {
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000240class BumpPointerAllocator {
241 struct BlockMeta {
242 BlockMeta* Next;
243 size_t Current;
244 };
245
246 static constexpr size_t AllocSize = 4096;
247 static constexpr size_t UsableAllocSize = AllocSize - sizeof(BlockMeta);
248
Serge Pavlov892bd812018-07-05 06:22:39 +0000249 alignas(long double) char InitialBuffer[AllocSize];
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000250 BlockMeta* BlockList = nullptr;
251
252 void grow() {
Erik Pilkington28e08a02018-07-23 22:23:04 +0000253 char* NewMeta = static_cast<char *>(std::malloc(AllocSize));
254 if (NewMeta == nullptr)
255 std::terminate();
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000256 BlockList = new (NewMeta) BlockMeta{BlockList, 0};
257 }
258
259 void* allocateMassive(size_t NBytes) {
260 NBytes += sizeof(BlockMeta);
Erik Pilkington28e08a02018-07-23 22:23:04 +0000261 BlockMeta* NewMeta = reinterpret_cast<BlockMeta*>(std::malloc(NBytes));
262 if (NewMeta == nullptr)
263 std::terminate();
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000264 BlockList->Next = new (NewMeta) BlockMeta{BlockList->Next, 0};
265 return static_cast<void*>(NewMeta + 1);
266 }
267
268public:
269 BumpPointerAllocator()
270 : BlockList(new (InitialBuffer) BlockMeta{nullptr, 0}) {}
271
272 void* allocate(size_t N) {
273 N = (N + 15u) & ~15u;
274 if (N + BlockList->Current >= UsableAllocSize) {
275 if (N > UsableAllocSize)
276 return allocateMassive(N);
277 grow();
278 }
279 BlockList->Current += N;
280 return static_cast<void*>(reinterpret_cast<char*>(BlockList + 1) +
281 BlockList->Current - N);
282 }
283
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000284 void reset() {
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000285 while (BlockList) {
286 BlockMeta* Tmp = BlockList;
287 BlockList = BlockList->Next;
288 if (reinterpret_cast<char*>(Tmp) != InitialBuffer)
Erik Pilkington28e08a02018-07-23 22:23:04 +0000289 std::free(Tmp);
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000290 }
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000291 BlockList = new (InitialBuffer) BlockMeta{nullptr, 0};
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000292 }
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000293
294 ~BumpPointerAllocator() { reset(); }
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000295};
296
Richard Smitha6c34882018-08-16 21:40:57 +0000297class DefaultAllocator {
298 BumpPointerAllocator Alloc;
299
300public:
301 void reset() { Alloc.reset(); }
302
303 template<typename T, typename ...Args> T *makeNode(Args &&...args) {
304 return new (Alloc.allocate(sizeof(T)))
305 T(std::forward<Args>(args)...);
306 }
307
308 void *allocateNodeArray(size_t sz) {
309 return Alloc.allocate(sizeof(Node *) * sz);
310 }
311};
312
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000313bool initializeOutputStream(char *Buf, size_t *N, OutputStream &S,
314 size_t InitSize) {
315 size_t BufferSize;
316 if (Buf == nullptr) {
317 Buf = static_cast<char *>(std::malloc(InitSize));
318 if (Buf == nullptr)
319 return true;
320 BufferSize = InitSize;
321 } else
322 BufferSize = *N;
323
324 S.reset(Buf, BufferSize);
325 return false;
326}
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000327} // unnamed namespace
Rafael Espindolab940b662016-09-06 19:16:48 +0000328
Richard Smith8a57f2e2018-08-20 19:44:01 +0000329//===----------------------------------------------------------------------===//
330// Code beyond this point should not be synchronized with libc++abi.
331//===----------------------------------------------------------------------===//
332
333using Demangler = itanium_demangle::Db<DefaultAllocator>;
334
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000335char *llvm::itaniumDemangle(const char *MangledName, char *Buf,
336 size_t *N, int *Status) {
337 if (MangledName == nullptr || (Buf != nullptr && N == nullptr)) {
338 if (Status)
Zachary Turner8a0efd02018-07-17 19:42:29 +0000339 *Status = demangle_invalid_args;
Rafael Espindolab940b662016-09-06 19:16:48 +0000340 return nullptr;
341 }
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000342
Zachary Turner8a0efd02018-07-17 19:42:29 +0000343 int InternalStatus = demangle_success;
Richard Smith8a57f2e2018-08-20 19:44:01 +0000344 Demangler Parser(MangledName, MangledName + std::strlen(MangledName));
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000345 OutputStream S;
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000346
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000347 Node *AST = Parser.parse();
348
349 if (AST == nullptr)
Zachary Turner8a0efd02018-07-17 19:42:29 +0000350 InternalStatus = demangle_invalid_mangled_name;
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000351 else if (initializeOutputStream(Buf, N, S, 1024))
Zachary Turner8a0efd02018-07-17 19:42:29 +0000352 InternalStatus = demangle_memory_alloc_failure;
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000353 else {
Erik Pilkington8a1cb332018-03-25 22:50:33 +0000354 assert(Parser.ForwardTemplateRefs.empty());
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000355 AST->print(S);
356 S += '\0';
357 if (N != nullptr)
358 *N = S.getCurrentPosition();
359 Buf = S.getBuffer();
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000360 }
361
362 if (Status)
363 *Status = InternalStatus;
Zachary Turner8a0efd02018-07-17 19:42:29 +0000364 return InternalStatus == demangle_success ? Buf : nullptr;
Rafael Espindolab940b662016-09-06 19:16:48 +0000365}
Erik Pilkington67d82d62018-04-12 20:41:38 +0000366
Erik Pilkingtonac6a801c2018-08-13 16:37:47 +0000367bool llvm::itaniumFindTypesInMangledName(const char *MangledName, void *Ctx,
368 void (*Callback)(void *,
369 const char *)) {
Richard Smith8a57f2e2018-08-20 19:44:01 +0000370 Demangler Parser(MangledName, MangledName + std::strlen(MangledName));
Erik Pilkingtonac6a801c2018-08-13 16:37:47 +0000371 Parser.TypeCallback = Callback;
372 Parser.TypeCallbackContext = Ctx;
373 return Parser.parse() == nullptr;
374}
375
Erik Pilkington67d82d62018-04-12 20:41:38 +0000376ItaniumPartialDemangler::ItaniumPartialDemangler()
Richard Smith8a57f2e2018-08-20 19:44:01 +0000377 : RootNode(nullptr), Context(new Demangler{nullptr, nullptr}) {}
Erik Pilkington67d82d62018-04-12 20:41:38 +0000378
379ItaniumPartialDemangler::~ItaniumPartialDemangler() {
Richard Smith8a57f2e2018-08-20 19:44:01 +0000380 delete static_cast<Demangler *>(Context);
Erik Pilkington67d82d62018-04-12 20:41:38 +0000381}
382
383ItaniumPartialDemangler::ItaniumPartialDemangler(
384 ItaniumPartialDemangler &&Other)
385 : RootNode(Other.RootNode), Context(Other.Context) {
386 Other.Context = Other.RootNode = nullptr;
387}
388
389ItaniumPartialDemangler &ItaniumPartialDemangler::
390operator=(ItaniumPartialDemangler &&Other) {
391 std::swap(RootNode, Other.RootNode);
392 std::swap(Context, Other.Context);
393 return *this;
394}
395
396// Demangle MangledName into an AST, storing it into this->RootNode.
397bool ItaniumPartialDemangler::partialDemangle(const char *MangledName) {
Richard Smith8a57f2e2018-08-20 19:44:01 +0000398 Demangler *Parser = static_cast<Demangler *>(Context);
Erik Pilkington67d82d62018-04-12 20:41:38 +0000399 size_t Len = std::strlen(MangledName);
400 Parser->reset(MangledName, MangledName + Len);
401 RootNode = Parser->parse();
402 return RootNode == nullptr;
403}
404
Richard Smith8a57f2e2018-08-20 19:44:01 +0000405static char *printNode(const Node *RootNode, char *Buf, size_t *N) {
Erik Pilkington67d82d62018-04-12 20:41:38 +0000406 OutputStream S;
407 if (initializeOutputStream(Buf, N, S, 128))
408 return nullptr;
409 RootNode->print(S);
410 S += '\0';
411 if (N != nullptr)
412 *N = S.getCurrentPosition();
413 return S.getBuffer();
414}
415
416char *ItaniumPartialDemangler::getFunctionBaseName(char *Buf, size_t *N) const {
417 if (!isFunction())
418 return nullptr;
419
Richard Smith8a57f2e2018-08-20 19:44:01 +0000420 const Node *Name = static_cast<const FunctionEncoding *>(RootNode)->getName();
Erik Pilkington67d82d62018-04-12 20:41:38 +0000421
422 while (true) {
423 switch (Name->getKind()) {
424 case Node::KAbiTagAttr:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000425 Name = static_cast<const AbiTagAttr *>(Name)->Base;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000426 continue;
427 case Node::KStdQualifiedName:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000428 Name = static_cast<const StdQualifiedName *>(Name)->Child;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000429 continue;
430 case Node::KNestedName:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000431 Name = static_cast<const NestedName *>(Name)->Name;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000432 continue;
433 case Node::KLocalName:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000434 Name = static_cast<const LocalName *>(Name)->Entity;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000435 continue;
436 case Node::KNameWithTemplateArgs:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000437 Name = static_cast<const NameWithTemplateArgs *>(Name)->Name;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000438 continue;
439 default:
440 return printNode(Name, Buf, N);
441 }
442 }
443}
444
445char *ItaniumPartialDemangler::getFunctionDeclContextName(char *Buf,
446 size_t *N) const {
447 if (!isFunction())
448 return nullptr;
Richard Smith8a57f2e2018-08-20 19:44:01 +0000449 const Node *Name = static_cast<const FunctionEncoding *>(RootNode)->getName();
Erik Pilkington67d82d62018-04-12 20:41:38 +0000450
451 OutputStream S;
452 if (initializeOutputStream(Buf, N, S, 128))
453 return nullptr;
454
455 KeepGoingLocalFunction:
456 while (true) {
457 if (Name->getKind() == Node::KAbiTagAttr) {
Richard Smith8a57f2e2018-08-20 19:44:01 +0000458 Name = static_cast<const AbiTagAttr *>(Name)->Base;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000459 continue;
460 }
461 if (Name->getKind() == Node::KNameWithTemplateArgs) {
Richard Smith8a57f2e2018-08-20 19:44:01 +0000462 Name = static_cast<const NameWithTemplateArgs *>(Name)->Name;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000463 continue;
464 }
465 break;
466 }
467
468 switch (Name->getKind()) {
469 case Node::KStdQualifiedName:
470 S += "std";
471 break;
472 case Node::KNestedName:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000473 static_cast<const NestedName *>(Name)->Qual->print(S);
Erik Pilkington67d82d62018-04-12 20:41:38 +0000474 break;
475 case Node::KLocalName: {
Richard Smith8a57f2e2018-08-20 19:44:01 +0000476 auto *LN = static_cast<const LocalName *>(Name);
Erik Pilkington67d82d62018-04-12 20:41:38 +0000477 LN->Encoding->print(S);
478 S += "::";
479 Name = LN->Entity;
480 goto KeepGoingLocalFunction;
481 }
482 default:
483 break;
484 }
485 S += '\0';
486 if (N != nullptr)
487 *N = S.getCurrentPosition();
488 return S.getBuffer();
489}
490
491char *ItaniumPartialDemangler::getFunctionName(char *Buf, size_t *N) const {
492 if (!isFunction())
493 return nullptr;
494 auto *Name = static_cast<FunctionEncoding *>(RootNode)->getName();
495 return printNode(Name, Buf, N);
496}
497
498char *ItaniumPartialDemangler::getFunctionParameters(char *Buf,
499 size_t *N) const {
500 if (!isFunction())
501 return nullptr;
502 NodeArray Params = static_cast<FunctionEncoding *>(RootNode)->getParams();
503
504 OutputStream S;
505 if (initializeOutputStream(Buf, N, S, 128))
506 return nullptr;
507
508 S += '(';
509 Params.printWithComma(S);
510 S += ')';
511 S += '\0';
512 if (N != nullptr)
513 *N = S.getCurrentPosition();
514 return S.getBuffer();
515}
516
517char *ItaniumPartialDemangler::getFunctionReturnType(
518 char *Buf, size_t *N) const {
519 if (!isFunction())
520 return nullptr;
521
522 OutputStream S;
523 if (initializeOutputStream(Buf, N, S, 128))
524 return nullptr;
525
Richard Smith8a57f2e2018-08-20 19:44:01 +0000526 if (const Node *Ret =
527 static_cast<const FunctionEncoding *>(RootNode)->getReturnType())
Erik Pilkington67d82d62018-04-12 20:41:38 +0000528 Ret->print(S);
529
530 S += '\0';
531 if (N != nullptr)
532 *N = S.getCurrentPosition();
533 return S.getBuffer();
534}
535
536char *ItaniumPartialDemangler::finishDemangle(char *Buf, size_t *N) const {
537 assert(RootNode != nullptr && "must call partialDemangle()");
538 return printNode(static_cast<Node *>(RootNode), Buf, N);
539}
540
541bool ItaniumPartialDemangler::hasFunctionQualifiers() const {
542 assert(RootNode != nullptr && "must call partialDemangle()");
543 if (!isFunction())
544 return false;
Richard Smith8a57f2e2018-08-20 19:44:01 +0000545 auto *E = static_cast<const FunctionEncoding *>(RootNode);
Erik Pilkington67d82d62018-04-12 20:41:38 +0000546 return E->getCVQuals() != QualNone || E->getRefQual() != FrefQualNone;
547}
548
Fangrui Song79420ac2018-05-24 06:57:57 +0000549bool ItaniumPartialDemangler::isCtorOrDtor() const {
Richard Smith8a57f2e2018-08-20 19:44:01 +0000550 const Node *N = static_cast<const Node *>(RootNode);
Fangrui Song79420ac2018-05-24 06:57:57 +0000551 while (N) {
552 switch (N->getKind()) {
553 default:
554 return false;
555 case Node::KCtorDtorName:
556 return true;
557
558 case Node::KAbiTagAttr:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000559 N = static_cast<const AbiTagAttr *>(N)->Base;
Fangrui Song79420ac2018-05-24 06:57:57 +0000560 break;
561 case Node::KFunctionEncoding:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000562 N = static_cast<const FunctionEncoding *>(N)->getName();
Fangrui Song79420ac2018-05-24 06:57:57 +0000563 break;
564 case Node::KLocalName:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000565 N = static_cast<const LocalName *>(N)->Entity;
Fangrui Song79420ac2018-05-24 06:57:57 +0000566 break;
567 case Node::KNameWithTemplateArgs:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000568 N = static_cast<const NameWithTemplateArgs *>(N)->Name;
Fangrui Song79420ac2018-05-24 06:57:57 +0000569 break;
570 case Node::KNestedName:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000571 N = static_cast<const NestedName *>(N)->Name;
Fangrui Song79420ac2018-05-24 06:57:57 +0000572 break;
573 case Node::KStdQualifiedName:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000574 N = static_cast<const StdQualifiedName *>(N)->Child;
Fangrui Song79420ac2018-05-24 06:57:57 +0000575 break;
576 }
577 }
578 return false;
579}
580
Erik Pilkington67d82d62018-04-12 20:41:38 +0000581bool ItaniumPartialDemangler::isFunction() const {
582 assert(RootNode != nullptr && "must call partialDemangle()");
Richard Smith8a57f2e2018-08-20 19:44:01 +0000583 return static_cast<const Node *>(RootNode)->getKind() ==
584 Node::KFunctionEncoding;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000585}
586
587bool ItaniumPartialDemangler::isSpecialName() const {
588 assert(RootNode != nullptr && "must call partialDemangle()");
Richard Smith8a57f2e2018-08-20 19:44:01 +0000589 auto K = static_cast<const Node *>(RootNode)->getKind();
Erik Pilkington67d82d62018-04-12 20:41:38 +0000590 return K == Node::KSpecialName || K == Node::KCtorVtableSpecialName;
591}
592
593bool ItaniumPartialDemangler::isData() const {
594 return !isFunction() && !isSpecialName();
595}