blob: 7912b1ce00b36913f4f78fac363d95966fd80826 [file] [log] [blame]
Howard Hinnantd3da57f2011-05-05 15:27:28 +00001//===-------------------------- cxa_demangle.cpp --------------------------===//
2//
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
Howard Hinnantd3da57f2011-05-05 15:27:28 +00006//
7//===----------------------------------------------------------------------===//
8
Erik Pilkington1b7f8d52017-11-21 15:04:08 +00009// FIXME: (possibly) incomplete list of features that clang mangles that this
10// file does not yet support:
Erik Pilkington1b7f8d52017-11-21 15:04:08 +000011// - C++ modules TS
12
Richard Smith2077b62a2018-08-20 20:14:49 +000013#include "demangle/ItaniumDemangle.h"
Erik Pilkington5094e5e2019-01-17 20:37:51 +000014#include "__cxxabi_config.h"
Erik Pilkington862987a2018-01-31 20:17:06 +000015#include <cassert>
Zachary Turner0e3fbf62018-07-20 17:16:49 +000016#include <cctype>
Erik Pilkingtonbdfd1222017-07-28 00:53:30 +000017#include <cstdio>
Howard Hinnant862c4a02013-06-17 18:10:34 +000018#include <cstdlib>
19#include <cstring>
Richard Smith2077b62a2018-08-20 20:14:49 +000020#include <functional>
Zachary Turner0e3fbf62018-07-20 17:16:49 +000021#include <numeric>
Erik Pilkington3a6fed42018-07-27 17:27:40 +000022#include <utility>
Howard Hinnantd3da57f2011-05-05 15:27:28 +000023
Richard Smith2077b62a2018-08-20 20:14:49 +000024using namespace itanium_demangle;
Erik Pilkington862987a2018-01-31 20:17:06 +000025
Richard Smith2077b62a2018-08-20 20:14:49 +000026constexpr const char *itanium_demangle::FloatData<float>::spec;
27constexpr const char *itanium_demangle::FloatData<double>::spec;
28constexpr const char *itanium_demangle::FloatData<long double>::spec;
Erik Pilkington94d2ac72017-07-28 00:43:49 +000029
Richard Smith2077b62a2018-08-20 20:14:49 +000030// <discriminator> := _ <non-negative number> # when number < 10
31// := __ <non-negative number> _ # when number >= 10
32// extension := decimal-digit+ # at the end of string
33const char *itanium_demangle::parse_discriminator(const char *first,
34 const char *last) {
35 // parse but ignore discriminator
36 if (first != last) {
37 if (*first == '_') {
38 const char *t1 = first + 1;
39 if (t1 != last) {
40 if (std::isdigit(*t1))
41 first = t1 + 1;
42 else if (*t1 == '_') {
43 for (++t1; t1 != last && std::isdigit(*t1); ++t1)
44 ;
45 if (t1 != last && *t1 == '_')
46 first = t1 + 1;
47 }
Erik Pilkington8c7013d2018-03-25 22:49:57 +000048 }
Richard Smith2077b62a2018-08-20 20:14:49 +000049 } else if (std::isdigit(*first)) {
50 const char *t1 = first + 1;
51 for (; t1 != last && std::isdigit(*t1); ++t1)
52 ;
53 if (t1 == last)
54 first = last;
Erik Pilkington94d2ac72017-07-28 00:43:49 +000055 }
56 }
Richard Smith2077b62a2018-08-20 20:14:49 +000057 return first;
Erik Pilkington94d2ac72017-07-28 00:43:49 +000058}
59
Richard Smith2077b62a2018-08-20 20:14:49 +000060#ifndef NDEBUG
61namespace {
62struct DumpVisitor {
63 unsigned Depth = 0;
64 bool PendingNewline = false;
Erik Pilkington94d2ac72017-07-28 00:43:49 +000065
Richard Smith2077b62a2018-08-20 20:14:49 +000066 template<typename NodeT> static constexpr bool wantsNewline(const NodeT *) {
67 return true;
68 }
69 static bool wantsNewline(NodeArray A) { return !A.empty(); }
70 static constexpr bool wantsNewline(...) { return false; }
71
72 template<typename ...Ts> static bool anyWantNewline(Ts ...Vs) {
73 for (bool B : {wantsNewline(Vs)...})
74 if (B)
75 return true;
76 return false;
Erik Pilkington94d2ac72017-07-28 00:43:49 +000077 }
78
Richard Smith2077b62a2018-08-20 20:14:49 +000079 void printStr(const char *S) { fprintf(stderr, "%s", S); }
80 void print(StringView SV) {
81 fprintf(stderr, "\"%.*s\"", (int)SV.size(), SV.begin());
Erik Pilkington862987a2018-01-31 20:17:06 +000082 }
Richard Smith2077b62a2018-08-20 20:14:49 +000083 void print(const Node *N) {
84 if (N)
85 N->visit(std::ref(*this));
Erik Pilkington94d2ac72017-07-28 00:43:49 +000086 else
Richard Smith2077b62a2018-08-20 20:14:49 +000087 printStr("<null>");
Erik Pilkington94d2ac72017-07-28 00:43:49 +000088 }
Richard Smith2077b62a2018-08-20 20:14:49 +000089 void print(NodeOrString NS) {
90 if (NS.isNode())
91 print(NS.asNode());
92 else if (NS.isString())
93 print(NS.asString());
94 else
95 printStr("NodeOrString()");
Erik Pilkington94d2ac72017-07-28 00:43:49 +000096 }
Richard Smith2077b62a2018-08-20 20:14:49 +000097 void print(NodeArray A) {
98 ++Depth;
99 printStr("{");
100 bool First = true;
101 for (const Node *N : A) {
102 if (First)
103 print(N);
104 else
105 printWithComma(N);
106 First = false;
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000107 }
Richard Smith2077b62a2018-08-20 20:14:49 +0000108 printStr("}");
109 --Depth;
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000110 }
Erik Pilkingtonfbca8d52018-10-15 22:03:53 +0000111
Richard Smith2077b62a2018-08-20 20:14:49 +0000112 // Overload used when T is exactly 'bool', not merely convertible to 'bool'.
Erik Pilkingtonfbca8d52018-10-15 22:03:53 +0000113 void print(bool B) { printStr(B ? "true" : "false"); }
114
115 template <class T>
116 typename std::enable_if<std::is_unsigned<T>::value>::type print(T N) {
117 fprintf(stderr, "%llu", (unsigned long long)N);
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000118 }
Erik Pilkingtonfbca8d52018-10-15 22:03:53 +0000119
120 template <class T>
121 typename std::enable_if<std::is_signed<T>::value>::type print(T N) {
122 fprintf(stderr, "%lld", (long long)N);
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000123 }
Erik Pilkingtonfbca8d52018-10-15 22:03:53 +0000124
Richard Smith2077b62a2018-08-20 20:14:49 +0000125 void print(ReferenceKind RK) {
126 switch (RK) {
127 case ReferenceKind::LValue:
128 return printStr("ReferenceKind::LValue");
129 case ReferenceKind::RValue:
130 return printStr("ReferenceKind::RValue");
Erik Pilkington0bae6d82018-02-14 01:08:20 +0000131 }
132 }
Richard Smith2077b62a2018-08-20 20:14:49 +0000133 void print(FunctionRefQual RQ) {
134 switch (RQ) {
135 case FunctionRefQual::FrefQualNone:
136 return printStr("FunctionRefQual::FrefQualNone");
137 case FunctionRefQual::FrefQualLValue:
138 return printStr("FunctionRefQual::FrefQualLValue");
139 case FunctionRefQual::FrefQualRValue:
140 return printStr("FunctionRefQual::FrefQualRValue");
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000141 }
142 }
Richard Smith2077b62a2018-08-20 20:14:49 +0000143 void print(Qualifiers Qs) {
144 if (!Qs) return printStr("QualNone");
145 struct QualName { Qualifiers Q; const char *Name; } Names[] = {
146 {QualConst, "QualConst"},
147 {QualVolatile, "QualVolatile"},
148 {QualRestrict, "QualRestrict"},
Erik Pilkingtond43931d2018-04-09 18:33:01 +0000149 };
Richard Smith2077b62a2018-08-20 20:14:49 +0000150 for (QualName Name : Names) {
151 if (Qs & Name.Q) {
152 printStr(Name.Name);
153 Qs = Qualifiers(Qs & ~Name.Q);
154 if (Qs) printStr(" | ");
Erik Pilkingtond43931d2018-04-09 18:33:01 +0000155 }
156 }
Erik Pilkingtond43931d2018-04-09 18:33:01 +0000157 }
Richard Smith2077b62a2018-08-20 20:14:49 +0000158 void print(SpecialSubKind SSK) {
159 switch (SSK) {
160 case SpecialSubKind::allocator:
161 return printStr("SpecialSubKind::allocator");
162 case SpecialSubKind::basic_string:
163 return printStr("SpecialSubKind::basic_string");
164 case SpecialSubKind::string:
165 return printStr("SpecialSubKind::string");
166 case SpecialSubKind::istream:
167 return printStr("SpecialSubKind::istream");
168 case SpecialSubKind::ostream:
169 return printStr("SpecialSubKind::ostream");
170 case SpecialSubKind::iostream:
171 return printStr("SpecialSubKind::iostream");
172 }
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000173 }
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000174
Richard Smith2077b62a2018-08-20 20:14:49 +0000175 void newLine() {
176 printStr("\n");
177 for (unsigned I = 0; I != Depth; ++I)
178 printStr(" ");
179 PendingNewline = false;
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000180 }
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000181
Richard Smith2077b62a2018-08-20 20:14:49 +0000182 template<typename T> void printWithPendingNewline(T V) {
183 print(V);
184 if (wantsNewline(V))
185 PendingNewline = true;
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000186 }
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000187
Richard Smith2077b62a2018-08-20 20:14:49 +0000188 template<typename T> void printWithComma(T V) {
189 if (PendingNewline || wantsNewline(V)) {
190 printStr(",");
191 newLine();
192 } else {
193 printStr(", ");
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000194 }
195
Richard Smith2077b62a2018-08-20 20:14:49 +0000196 printWithPendingNewline(V);
197 }
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000198
Richard Smith2077b62a2018-08-20 20:14:49 +0000199 struct CtorArgPrinter {
200 DumpVisitor &Visitor;
201
202 template<typename T, typename ...Rest> void operator()(T V, Rest ...Vs) {
203 if (Visitor.anyWantNewline(V, Vs...))
204 Visitor.newLine();
205 Visitor.printWithPendingNewline(V);
206 int PrintInOrder[] = { (Visitor.printWithComma(Vs), 0)..., 0 };
207 (void)PrintInOrder;
208 }
209 };
210
211 template<typename NodeT> void operator()(const NodeT *Node) {
212 Depth += 2;
213 fprintf(stderr, "%s(", itanium_demangle::NodeKind<NodeT>::name());
214 Node->match(CtorArgPrinter{*this});
215 fprintf(stderr, ")");
216 Depth -= 2;
217 }
218
219 void operator()(const ForwardTemplateReference *Node) {
220 Depth += 2;
221 fprintf(stderr, "ForwardTemplateReference(");
222 if (Node->Ref && !Node->Printing) {
223 Node->Printing = true;
224 CtorArgPrinter{*this}(Node->Ref);
225 Node->Printing = false;
226 } else {
227 CtorArgPrinter{*this}(Node->Index);
228 }
229 fprintf(stderr, ")");
230 Depth -= 2;
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000231 }
232};
Richard Smith2077b62a2018-08-20 20:14:49 +0000233}
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000234
Richard Smith2077b62a2018-08-20 20:14:49 +0000235void itanium_demangle::Node::dump() const {
236 DumpVisitor V;
237 visit(std::ref(V));
238 V.newLine();
239}
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000240#endif
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000241
Richard Smith2077b62a2018-08-20 20:14:49 +0000242namespace {
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000243class BumpPointerAllocator {
244 struct BlockMeta {
245 BlockMeta* Next;
246 size_t Current;
247 };
Erik Pilkingtonffac6742017-07-08 18:54:07 +0000248
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000249 static constexpr size_t AllocSize = 4096;
250 static constexpr size_t UsableAllocSize = AllocSize - sizeof(BlockMeta);
Erik Pilkingtonffac6742017-07-08 18:54:07 +0000251
Serge Pavlov2f17e962018-07-05 06:24:29 +0000252 alignas(long double) char InitialBuffer[AllocSize];
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000253 BlockMeta* BlockList = nullptr;
254
255 void grow() {
Erik Pilkington28e08a02018-07-23 22:23:04 +0000256 char* NewMeta = static_cast<char *>(std::malloc(AllocSize));
257 if (NewMeta == nullptr)
258 std::terminate();
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000259 BlockList = new (NewMeta) BlockMeta{BlockList, 0};
260 }
261
262 void* allocateMassive(size_t NBytes) {
263 NBytes += sizeof(BlockMeta);
Erik Pilkington28e08a02018-07-23 22:23:04 +0000264 BlockMeta* NewMeta = reinterpret_cast<BlockMeta*>(std::malloc(NBytes));
265 if (NewMeta == nullptr)
266 std::terminate();
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000267 BlockList->Next = new (NewMeta) BlockMeta{BlockList->Next, 0};
268 return static_cast<void*>(NewMeta + 1);
269 }
270
271public:
272 BumpPointerAllocator()
273 : BlockList(new (InitialBuffer) BlockMeta{nullptr, 0}) {}
274
275 void* allocate(size_t N) {
276 N = (N + 15u) & ~15u;
277 if (N + BlockList->Current >= UsableAllocSize) {
278 if (N > UsableAllocSize)
279 return allocateMassive(N);
280 grow();
281 }
282 BlockList->Current += N;
283 return static_cast<void*>(reinterpret_cast<char*>(BlockList + 1) +
284 BlockList->Current - N);
285 }
286
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000287 void reset() {
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000288 while (BlockList) {
289 BlockMeta* Tmp = BlockList;
290 BlockList = BlockList->Next;
291 if (reinterpret_cast<char*>(Tmp) != InitialBuffer)
Erik Pilkington28e08a02018-07-23 22:23:04 +0000292 std::free(Tmp);
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000293 }
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000294 BlockList = new (InitialBuffer) BlockMeta{nullptr, 0};
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000295 }
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000296
297 ~BumpPointerAllocator() { reset(); }
Erik Pilkingtonffac6742017-07-08 18:54:07 +0000298};
299
Richard Smithdc64b9c2018-08-16 22:04:36 +0000300class DefaultAllocator {
301 BumpPointerAllocator Alloc;
302
303public:
304 void reset() { Alloc.reset(); }
305
306 template<typename T, typename ...Args> T *makeNode(Args &&...args) {
307 return new (Alloc.allocate(sizeof(T)))
308 T(std::forward<Args>(args)...);
309 }
310
311 void *allocateNodeArray(size_t sz) {
312 return Alloc.allocate(sizeof(Node *) * sz);
313 }
314};
Howard Hinnant862c4a02013-06-17 18:10:34 +0000315} // unnamed namespace
316
Richard Smith2077b62a2018-08-20 20:14:49 +0000317//===----------------------------------------------------------------------===//
318// Code beyond this point should not be synchronized with LLVM.
319//===----------------------------------------------------------------------===//
320
Pavel Labath49b29ea2018-10-16 14:29:14 +0000321using Demangler = itanium_demangle::ManglingParser<DefaultAllocator>;
Richard Smith2077b62a2018-08-20 20:14:49 +0000322
323namespace {
324enum : int {
325 demangle_invalid_args = -3,
326 demangle_invalid_mangled_name = -2,
327 demangle_memory_alloc_failure = -1,
328 demangle_success = 0,
329};
330}
331
Erik Pilkington862987a2018-01-31 20:17:06 +0000332namespace __cxxabiv1 {
Saleem Abdulrasool12315ed2015-12-04 02:14:58 +0000333extern "C" _LIBCXXABI_FUNC_VIS char *
Erik Pilkington98e70362018-03-06 14:21:10 +0000334__cxa_demangle(const char *MangledName, char *Buf, size_t *N, int *Status) {
335 if (MangledName == nullptr || (Buf != nullptr && N == nullptr)) {
336 if (Status)
Zachary Turner0e3fbf62018-07-20 17:16:49 +0000337 *Status = demangle_invalid_args;
Erik Pilkington98e70362018-03-06 14:21:10 +0000338 return nullptr;
339 }
340
Zachary Turner0e3fbf62018-07-20 17:16:49 +0000341 int InternalStatus = demangle_success;
Richard Smith2077b62a2018-08-20 20:14:49 +0000342 Demangler Parser(MangledName, MangledName + std::strlen(MangledName));
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000343 OutputStream S;
Erik Pilkington98e70362018-03-06 14:21:10 +0000344
Erik Pilkington98e70362018-03-06 14:21:10 +0000345 Node *AST = Parser.parse();
346
347 if (AST == nullptr)
Zachary Turner0e3fbf62018-07-20 17:16:49 +0000348 InternalStatus = demangle_invalid_mangled_name;
Nico Weber966c1802018-11-11 10:09:06 +0000349 else if (!initializeOutputStream(Buf, N, S, 1024))
Zachary Turner0e3fbf62018-07-20 17:16:49 +0000350 InternalStatus = demangle_memory_alloc_failure;
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000351 else {
Erik Pilkington8a1cb332018-03-25 22:50:33 +0000352 assert(Parser.ForwardTemplateRefs.empty());
Erik Pilkingtonf2a9b0f2018-04-12 20:41:06 +0000353 AST->print(S);
354 S += '\0';
355 if (N != nullptr)
356 *N = S.getCurrentPosition();
357 Buf = S.getBuffer();
Erik Pilkington98e70362018-03-06 14:21:10 +0000358 }
Erik Pilkington94d2ac72017-07-28 00:43:49 +0000359
Erik Pilkington98e70362018-03-06 14:21:10 +0000360 if (Status)
361 *Status = InternalStatus;
Zachary Turner0e3fbf62018-07-20 17:16:49 +0000362 return InternalStatus == demangle_success ? Buf : nullptr;
Howard Hinnantd3da57f2011-05-05 15:27:28 +0000363}
Howard Hinnant862c4a02013-06-17 18:10:34 +0000364} // __cxxabiv1