blob: 5c99c70e3cc634450555292ee7334711f3863a70 [file] [log] [blame]
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +00001//===------------------------- ItaniumDemangle.cpp ------------------------===//
Rafael Espindolab940b662016-09-06 19:16:48 +00002//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Rafael Espindolab940b662016-09-06 19:16:48 +00006//
7//===----------------------------------------------------------------------===//
8
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +00009// FIXME: (possibly) incomplete list of features that clang mangles that this
10// file does not yet support:
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000011// - C++ modules TS
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000012
Reid Klecknerb2881f12016-09-06 19:39:56 +000013#include "llvm/Demangle/Demangle.h"
Richard Smith8a57f2e2018-08-20 19:44:01 +000014#include "llvm/Demangle/ItaniumDemangle.h"
Serge Pavlov1a095522018-05-29 07:05:41 +000015
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000016#include <cassert>
David Blaikie99e17252018-03-21 17:31:49 +000017#include <cctype>
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +000018#include <cstdio>
Rafael Espindolab940b662016-09-06 19:16:48 +000019#include <cstdlib>
20#include <cstring>
David Blaikiea25e20692018-08-20 20:02:29 +000021#include <functional>
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 }
Erik Pilkingtonfbca8d52018-10-15 22:03:53 +0000114
Richard Smith8a57f2e2018-08-20 19:44:01 +0000115 // Overload used when T is exactly 'bool', not merely convertible to 'bool'.
Erik Pilkingtonfbca8d52018-10-15 22:03:53 +0000116 void print(bool B) { printStr(B ? "true" : "false"); }
117
118 template <class T>
119 typename std::enable_if<std::is_unsigned<T>::value>::type print(T N) {
120 fprintf(stderr, "%llu", (unsigned long long)N);
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000121 }
Erik Pilkingtonfbca8d52018-10-15 22:03:53 +0000122
123 template <class T>
124 typename std::enable_if<std::is_signed<T>::value>::type print(T N) {
125 fprintf(stderr, "%lld", (long long)N);
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000126 }
Erik Pilkingtonfbca8d52018-10-15 22:03:53 +0000127
Richard Smith8a57f2e2018-08-20 19:44:01 +0000128 void print(ReferenceKind RK) {
129 switch (RK) {
130 case ReferenceKind::LValue:
131 return printStr("ReferenceKind::LValue");
132 case ReferenceKind::RValue:
133 return printStr("ReferenceKind::RValue");
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000134 }
135 }
Richard Smith8a57f2e2018-08-20 19:44:01 +0000136 void print(FunctionRefQual RQ) {
137 switch (RQ) {
138 case FunctionRefQual::FrefQualNone:
139 return printStr("FunctionRefQual::FrefQualNone");
140 case FunctionRefQual::FrefQualLValue:
141 return printStr("FunctionRefQual::FrefQualLValue");
142 case FunctionRefQual::FrefQualRValue:
143 return printStr("FunctionRefQual::FrefQualRValue");
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000144 }
145 }
Richard Smith8a57f2e2018-08-20 19:44:01 +0000146 void print(Qualifiers Qs) {
147 if (!Qs) return printStr("QualNone");
148 struct QualName { Qualifiers Q; const char *Name; } Names[] = {
149 {QualConst, "QualConst"},
150 {QualVolatile, "QualVolatile"},
151 {QualRestrict, "QualRestrict"},
Erik Pilkingtond43931d2018-04-09 18:33:01 +0000152 };
Richard Smith8a57f2e2018-08-20 19:44:01 +0000153 for (QualName Name : Names) {
154 if (Qs & Name.Q) {
155 printStr(Name.Name);
156 Qs = Qualifiers(Qs & ~Name.Q);
157 if (Qs) printStr(" | ");
Erik Pilkingtond43931d2018-04-09 18:33:01 +0000158 }
159 }
Erik Pilkingtond43931d2018-04-09 18:33:01 +0000160 }
Richard Smith8a57f2e2018-08-20 19:44:01 +0000161 void print(SpecialSubKind SSK) {
162 switch (SSK) {
163 case SpecialSubKind::allocator:
164 return printStr("SpecialSubKind::allocator");
165 case SpecialSubKind::basic_string:
166 return printStr("SpecialSubKind::basic_string");
167 case SpecialSubKind::string:
168 return printStr("SpecialSubKind::string");
169 case SpecialSubKind::istream:
170 return printStr("SpecialSubKind::istream");
171 case SpecialSubKind::ostream:
172 return printStr("SpecialSubKind::ostream");
173 case SpecialSubKind::iostream:
174 return printStr("SpecialSubKind::iostream");
175 }
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000176 }
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000177
Richard Smith8a57f2e2018-08-20 19:44:01 +0000178 void newLine() {
179 printStr("\n");
180 for (unsigned I = 0; I != Depth; ++I)
181 printStr(" ");
182 PendingNewline = false;
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 printWithPendingNewline(T V) {
186 print(V);
187 if (wantsNewline(V))
188 PendingNewline = true;
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000189 }
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000190
Richard Smith8a57f2e2018-08-20 19:44:01 +0000191 template<typename T> void printWithComma(T V) {
192 if (PendingNewline || wantsNewline(V)) {
193 printStr(",");
194 newLine();
195 } else {
196 printStr(", ");
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000197 }
198
Richard Smith8a57f2e2018-08-20 19:44:01 +0000199 printWithPendingNewline(V);
200 }
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000201
Richard Smith8a57f2e2018-08-20 19:44:01 +0000202 struct CtorArgPrinter {
203 DumpVisitor &Visitor;
204
205 template<typename T, typename ...Rest> void operator()(T V, Rest ...Vs) {
206 if (Visitor.anyWantNewline(V, Vs...))
207 Visitor.newLine();
208 Visitor.printWithPendingNewline(V);
209 int PrintInOrder[] = { (Visitor.printWithComma(Vs), 0)..., 0 };
210 (void)PrintInOrder;
211 }
212 };
213
214 template<typename NodeT> void operator()(const NodeT *Node) {
215 Depth += 2;
216 fprintf(stderr, "%s(", itanium_demangle::NodeKind<NodeT>::name());
217 Node->match(CtorArgPrinter{*this});
218 fprintf(stderr, ")");
219 Depth -= 2;
220 }
221
222 void operator()(const ForwardTemplateReference *Node) {
223 Depth += 2;
224 fprintf(stderr, "ForwardTemplateReference(");
225 if (Node->Ref && !Node->Printing) {
226 Node->Printing = true;
227 CtorArgPrinter{*this}(Node->Ref);
228 Node->Printing = false;
229 } else {
230 CtorArgPrinter{*this}(Node->Index);
231 }
232 fprintf(stderr, ")");
233 Depth -= 2;
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000234 }
235};
Richard Smith8a57f2e2018-08-20 19:44:01 +0000236}
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000237
Richard Smith8a57f2e2018-08-20 19:44:01 +0000238void itanium_demangle::Node::dump() const {
239 DumpVisitor V;
240 visit(std::ref(V));
241 V.newLine();
242}
Rafael Espindolab940b662016-09-06 19:16:48 +0000243#endif
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000244
Richard Smith8a57f2e2018-08-20 19:44:01 +0000245namespace {
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000246class BumpPointerAllocator {
247 struct BlockMeta {
248 BlockMeta* Next;
249 size_t Current;
250 };
251
252 static constexpr size_t AllocSize = 4096;
253 static constexpr size_t UsableAllocSize = AllocSize - sizeof(BlockMeta);
254
Serge Pavlov892bd812018-07-05 06:22:39 +0000255 alignas(long double) char InitialBuffer[AllocSize];
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000256 BlockMeta* BlockList = nullptr;
257
258 void grow() {
Erik Pilkington28e08a02018-07-23 22:23:04 +0000259 char* NewMeta = static_cast<char *>(std::malloc(AllocSize));
260 if (NewMeta == nullptr)
261 std::terminate();
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000262 BlockList = new (NewMeta) BlockMeta{BlockList, 0};
263 }
264
265 void* allocateMassive(size_t NBytes) {
266 NBytes += sizeof(BlockMeta);
Erik Pilkington28e08a02018-07-23 22:23:04 +0000267 BlockMeta* NewMeta = reinterpret_cast<BlockMeta*>(std::malloc(NBytes));
268 if (NewMeta == nullptr)
269 std::terminate();
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000270 BlockList->Next = new (NewMeta) BlockMeta{BlockList->Next, 0};
271 return static_cast<void*>(NewMeta + 1);
272 }
273
274public:
275 BumpPointerAllocator()
276 : BlockList(new (InitialBuffer) BlockMeta{nullptr, 0}) {}
277
278 void* allocate(size_t N) {
279 N = (N + 15u) & ~15u;
280 if (N + BlockList->Current >= UsableAllocSize) {
281 if (N > UsableAllocSize)
282 return allocateMassive(N);
283 grow();
284 }
285 BlockList->Current += N;
286 return static_cast<void*>(reinterpret_cast<char*>(BlockList + 1) +
287 BlockList->Current - N);
288 }
289
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000290 void reset() {
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000291 while (BlockList) {
292 BlockMeta* Tmp = BlockList;
293 BlockList = BlockList->Next;
294 if (reinterpret_cast<char*>(Tmp) != InitialBuffer)
Erik Pilkington28e08a02018-07-23 22:23:04 +0000295 std::free(Tmp);
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000296 }
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000297 BlockList = new (InitialBuffer) BlockMeta{nullptr, 0};
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000298 }
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000299
300 ~BumpPointerAllocator() { reset(); }
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000301};
302
Richard Smitha6c34882018-08-16 21:40:57 +0000303class DefaultAllocator {
304 BumpPointerAllocator Alloc;
305
306public:
307 void reset() { Alloc.reset(); }
308
309 template<typename T, typename ...Args> T *makeNode(Args &&...args) {
310 return new (Alloc.allocate(sizeof(T)))
311 T(std::forward<Args>(args)...);
312 }
313
314 void *allocateNodeArray(size_t sz) {
315 return Alloc.allocate(sizeof(Node *) * sz);
316 }
317};
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000318} // unnamed namespace
Rafael Espindolab940b662016-09-06 19:16:48 +0000319
Richard Smith8a57f2e2018-08-20 19:44:01 +0000320//===----------------------------------------------------------------------===//
321// Code beyond this point should not be synchronized with libc++abi.
322//===----------------------------------------------------------------------===//
323
Pavel Labathf4c15822018-10-17 18:50:25 +0000324using Demangler = itanium_demangle::ManglingParser<DefaultAllocator>;
Richard Smith8a57f2e2018-08-20 19:44:01 +0000325
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000326char *llvm::itaniumDemangle(const char *MangledName, char *Buf,
327 size_t *N, int *Status) {
328 if (MangledName == nullptr || (Buf != nullptr && N == nullptr)) {
329 if (Status)
Zachary Turner8a0efd02018-07-17 19:42:29 +0000330 *Status = demangle_invalid_args;
Rafael Espindolab940b662016-09-06 19:16:48 +0000331 return nullptr;
332 }
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000333
Zachary Turner8a0efd02018-07-17 19:42:29 +0000334 int InternalStatus = demangle_success;
Richard Smith8a57f2e2018-08-20 19:44:01 +0000335 Demangler Parser(MangledName, MangledName + std::strlen(MangledName));
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000336 OutputStream S;
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000337
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000338 Node *AST = Parser.parse();
339
340 if (AST == nullptr)
Zachary Turner8a0efd02018-07-17 19:42:29 +0000341 InternalStatus = demangle_invalid_mangled_name;
Nico Weber6808bc02018-11-11 10:04:00 +0000342 else if (!initializeOutputStream(Buf, N, S, 1024))
Zachary Turner8a0efd02018-07-17 19:42:29 +0000343 InternalStatus = demangle_memory_alloc_failure;
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000344 else {
Erik Pilkington8a1cb332018-03-25 22:50:33 +0000345 assert(Parser.ForwardTemplateRefs.empty());
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000346 AST->print(S);
347 S += '\0';
348 if (N != nullptr)
349 *N = S.getCurrentPosition();
350 Buf = S.getBuffer();
Erik Pilkingtonbb7feae2018-03-19 15:18:23 +0000351 }
352
353 if (Status)
354 *Status = InternalStatus;
Zachary Turner8a0efd02018-07-17 19:42:29 +0000355 return InternalStatus == demangle_success ? Buf : nullptr;
Rafael Espindolab940b662016-09-06 19:16:48 +0000356}
Erik Pilkington67d82d62018-04-12 20:41:38 +0000357
Erik Pilkington67d82d62018-04-12 20:41:38 +0000358ItaniumPartialDemangler::ItaniumPartialDemangler()
Richard Smith8a57f2e2018-08-20 19:44:01 +0000359 : RootNode(nullptr), Context(new Demangler{nullptr, nullptr}) {}
Erik Pilkington67d82d62018-04-12 20:41:38 +0000360
361ItaniumPartialDemangler::~ItaniumPartialDemangler() {
Richard Smith8a57f2e2018-08-20 19:44:01 +0000362 delete static_cast<Demangler *>(Context);
Erik Pilkington67d82d62018-04-12 20:41:38 +0000363}
364
365ItaniumPartialDemangler::ItaniumPartialDemangler(
366 ItaniumPartialDemangler &&Other)
367 : RootNode(Other.RootNode), Context(Other.Context) {
368 Other.Context = Other.RootNode = nullptr;
369}
370
371ItaniumPartialDemangler &ItaniumPartialDemangler::
372operator=(ItaniumPartialDemangler &&Other) {
373 std::swap(RootNode, Other.RootNode);
374 std::swap(Context, Other.Context);
375 return *this;
376}
377
378// Demangle MangledName into an AST, storing it into this->RootNode.
379bool ItaniumPartialDemangler::partialDemangle(const char *MangledName) {
Richard Smith8a57f2e2018-08-20 19:44:01 +0000380 Demangler *Parser = static_cast<Demangler *>(Context);
Erik Pilkington67d82d62018-04-12 20:41:38 +0000381 size_t Len = std::strlen(MangledName);
382 Parser->reset(MangledName, MangledName + Len);
383 RootNode = Parser->parse();
384 return RootNode == nullptr;
385}
386
Richard Smith8a57f2e2018-08-20 19:44:01 +0000387static char *printNode(const Node *RootNode, char *Buf, size_t *N) {
Erik Pilkington67d82d62018-04-12 20:41:38 +0000388 OutputStream S;
Nico Weber6808bc02018-11-11 10:04:00 +0000389 if (!initializeOutputStream(Buf, N, S, 128))
Erik Pilkington67d82d62018-04-12 20:41:38 +0000390 return nullptr;
391 RootNode->print(S);
392 S += '\0';
393 if (N != nullptr)
394 *N = S.getCurrentPosition();
395 return S.getBuffer();
396}
397
398char *ItaniumPartialDemangler::getFunctionBaseName(char *Buf, size_t *N) const {
399 if (!isFunction())
400 return nullptr;
401
Richard Smith8a57f2e2018-08-20 19:44:01 +0000402 const Node *Name = static_cast<const FunctionEncoding *>(RootNode)->getName();
Erik Pilkington67d82d62018-04-12 20:41:38 +0000403
404 while (true) {
405 switch (Name->getKind()) {
406 case Node::KAbiTagAttr:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000407 Name = static_cast<const AbiTagAttr *>(Name)->Base;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000408 continue;
409 case Node::KStdQualifiedName:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000410 Name = static_cast<const StdQualifiedName *>(Name)->Child;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000411 continue;
412 case Node::KNestedName:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000413 Name = static_cast<const NestedName *>(Name)->Name;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000414 continue;
415 case Node::KLocalName:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000416 Name = static_cast<const LocalName *>(Name)->Entity;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000417 continue;
418 case Node::KNameWithTemplateArgs:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000419 Name = static_cast<const NameWithTemplateArgs *>(Name)->Name;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000420 continue;
421 default:
422 return printNode(Name, Buf, N);
423 }
424 }
425}
426
427char *ItaniumPartialDemangler::getFunctionDeclContextName(char *Buf,
428 size_t *N) const {
429 if (!isFunction())
430 return nullptr;
Richard Smith8a57f2e2018-08-20 19:44:01 +0000431 const Node *Name = static_cast<const FunctionEncoding *>(RootNode)->getName();
Erik Pilkington67d82d62018-04-12 20:41:38 +0000432
433 OutputStream S;
Nico Weber6808bc02018-11-11 10:04:00 +0000434 if (!initializeOutputStream(Buf, N, S, 128))
Erik Pilkington67d82d62018-04-12 20:41:38 +0000435 return nullptr;
436
437 KeepGoingLocalFunction:
438 while (true) {
439 if (Name->getKind() == Node::KAbiTagAttr) {
Richard Smith8a57f2e2018-08-20 19:44:01 +0000440 Name = static_cast<const AbiTagAttr *>(Name)->Base;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000441 continue;
442 }
443 if (Name->getKind() == Node::KNameWithTemplateArgs) {
Richard Smith8a57f2e2018-08-20 19:44:01 +0000444 Name = static_cast<const NameWithTemplateArgs *>(Name)->Name;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000445 continue;
446 }
447 break;
448 }
449
450 switch (Name->getKind()) {
451 case Node::KStdQualifiedName:
452 S += "std";
453 break;
454 case Node::KNestedName:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000455 static_cast<const NestedName *>(Name)->Qual->print(S);
Erik Pilkington67d82d62018-04-12 20:41:38 +0000456 break;
457 case Node::KLocalName: {
Richard Smith8a57f2e2018-08-20 19:44:01 +0000458 auto *LN = static_cast<const LocalName *>(Name);
Erik Pilkington67d82d62018-04-12 20:41:38 +0000459 LN->Encoding->print(S);
460 S += "::";
461 Name = LN->Entity;
462 goto KeepGoingLocalFunction;
463 }
464 default:
465 break;
466 }
467 S += '\0';
468 if (N != nullptr)
469 *N = S.getCurrentPosition();
470 return S.getBuffer();
471}
472
473char *ItaniumPartialDemangler::getFunctionName(char *Buf, size_t *N) const {
474 if (!isFunction())
475 return nullptr;
476 auto *Name = static_cast<FunctionEncoding *>(RootNode)->getName();
477 return printNode(Name, Buf, N);
478}
479
480char *ItaniumPartialDemangler::getFunctionParameters(char *Buf,
481 size_t *N) const {
482 if (!isFunction())
483 return nullptr;
484 NodeArray Params = static_cast<FunctionEncoding *>(RootNode)->getParams();
485
486 OutputStream S;
Nico Weber6808bc02018-11-11 10:04:00 +0000487 if (!initializeOutputStream(Buf, N, S, 128))
Erik Pilkington67d82d62018-04-12 20:41:38 +0000488 return nullptr;
489
490 S += '(';
491 Params.printWithComma(S);
492 S += ')';
493 S += '\0';
494 if (N != nullptr)
495 *N = S.getCurrentPosition();
496 return S.getBuffer();
497}
498
499char *ItaniumPartialDemangler::getFunctionReturnType(
500 char *Buf, size_t *N) const {
501 if (!isFunction())
502 return nullptr;
503
504 OutputStream S;
Nico Weber6808bc02018-11-11 10:04:00 +0000505 if (!initializeOutputStream(Buf, N, S, 128))
Erik Pilkington67d82d62018-04-12 20:41:38 +0000506 return nullptr;
507
Richard Smith8a57f2e2018-08-20 19:44:01 +0000508 if (const Node *Ret =
509 static_cast<const FunctionEncoding *>(RootNode)->getReturnType())
Erik Pilkington67d82d62018-04-12 20:41:38 +0000510 Ret->print(S);
511
512 S += '\0';
513 if (N != nullptr)
514 *N = S.getCurrentPosition();
515 return S.getBuffer();
516}
517
518char *ItaniumPartialDemangler::finishDemangle(char *Buf, size_t *N) const {
519 assert(RootNode != nullptr && "must call partialDemangle()");
520 return printNode(static_cast<Node *>(RootNode), Buf, N);
521}
522
523bool ItaniumPartialDemangler::hasFunctionQualifiers() const {
524 assert(RootNode != nullptr && "must call partialDemangle()");
525 if (!isFunction())
526 return false;
Richard Smith8a57f2e2018-08-20 19:44:01 +0000527 auto *E = static_cast<const FunctionEncoding *>(RootNode);
Erik Pilkington67d82d62018-04-12 20:41:38 +0000528 return E->getCVQuals() != QualNone || E->getRefQual() != FrefQualNone;
529}
530
Fangrui Song79420ac2018-05-24 06:57:57 +0000531bool ItaniumPartialDemangler::isCtorOrDtor() const {
Richard Smith8a57f2e2018-08-20 19:44:01 +0000532 const Node *N = static_cast<const Node *>(RootNode);
Fangrui Song79420ac2018-05-24 06:57:57 +0000533 while (N) {
534 switch (N->getKind()) {
535 default:
536 return false;
537 case Node::KCtorDtorName:
538 return true;
539
540 case Node::KAbiTagAttr:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000541 N = static_cast<const AbiTagAttr *>(N)->Base;
Fangrui Song79420ac2018-05-24 06:57:57 +0000542 break;
543 case Node::KFunctionEncoding:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000544 N = static_cast<const FunctionEncoding *>(N)->getName();
Fangrui Song79420ac2018-05-24 06:57:57 +0000545 break;
546 case Node::KLocalName:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000547 N = static_cast<const LocalName *>(N)->Entity;
Fangrui Song79420ac2018-05-24 06:57:57 +0000548 break;
549 case Node::KNameWithTemplateArgs:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000550 N = static_cast<const NameWithTemplateArgs *>(N)->Name;
Fangrui Song79420ac2018-05-24 06:57:57 +0000551 break;
552 case Node::KNestedName:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000553 N = static_cast<const NestedName *>(N)->Name;
Fangrui Song79420ac2018-05-24 06:57:57 +0000554 break;
555 case Node::KStdQualifiedName:
Richard Smith8a57f2e2018-08-20 19:44:01 +0000556 N = static_cast<const StdQualifiedName *>(N)->Child;
Fangrui Song79420ac2018-05-24 06:57:57 +0000557 break;
558 }
559 }
560 return false;
561}
562
Erik Pilkington67d82d62018-04-12 20:41:38 +0000563bool ItaniumPartialDemangler::isFunction() const {
564 assert(RootNode != nullptr && "must call partialDemangle()");
Richard Smith8a57f2e2018-08-20 19:44:01 +0000565 return static_cast<const Node *>(RootNode)->getKind() ==
566 Node::KFunctionEncoding;
Erik Pilkington67d82d62018-04-12 20:41:38 +0000567}
568
569bool ItaniumPartialDemangler::isSpecialName() const {
570 assert(RootNode != nullptr && "must call partialDemangle()");
Richard Smith8a57f2e2018-08-20 19:44:01 +0000571 auto K = static_cast<const Node *>(RootNode)->getKind();
Erik Pilkington67d82d62018-04-12 20:41:38 +0000572 return K == Node::KSpecialName || K == Node::KCtorVtableSpecialName;
573}
574
575bool ItaniumPartialDemangler::isData() const {
576 return !isFunction() && !isSpecialName();
577}