blob: 8c58254d6e0aa59e2eeeebca8f64756a85e395f1 [file] [log] [blame]
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001//===- MicrosoftDemangle.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
Zachary Turnerf435a7e2018-07-20 17:27:48 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines a demangler for MSVC-style mangled symbols.
10//
11// This file has no dependencies on the rest of LLVM so that it can be
12// easily reused in other programs such as libcxxabi.
13//
14//===----------------------------------------------------------------------===//
15
Zachary Turner7ba90562018-11-01 15:07:32 +000016#include "llvm/Demangle/MicrosoftDemangle.h"
Zachary Turnerf435a7e2018-07-20 17:27:48 +000017#include "llvm/Demangle/Demangle.h"
Zachary Turner7ba90562018-11-01 15:07:32 +000018#include "llvm/Demangle/MicrosoftDemangleNodes.h"
Zachary Turnerf435a7e2018-07-20 17:27:48 +000019
Erik Pilkington5094e5e2019-01-17 20:37:51 +000020#include "llvm/Demangle/DemangleConfig.h"
Richard Smith8a57f2e2018-08-20 19:44:01 +000021#include "llvm/Demangle/StringView.h"
22#include "llvm/Demangle/Utility.h"
Zachary Turnerf435a7e2018-07-20 17:27:48 +000023
Zachary Turner66555a72018-08-20 19:15:35 +000024#include <array>
Zachary Turnerf435a7e2018-07-20 17:27:48 +000025#include <cctype>
Zachary Turner5ae08b82018-08-01 18:44:12 +000026#include <cstdio>
Zachary Turnerd742d642018-07-26 19:56:09 +000027#include <tuple>
Zachary Turnerf435a7e2018-07-20 17:27:48 +000028
Zachary Turner03312862018-08-27 03:48:03 +000029using namespace llvm;
30using namespace ms_demangle;
Zachary Turnerf435a7e2018-07-20 17:27:48 +000031
32static bool startsWithDigit(StringView S) {
33 return !S.empty() && std::isdigit(S.front());
34}
35
Zachary Turnerd742d642018-07-26 19:56:09 +000036
Zachary Turner03312862018-08-27 03:48:03 +000037struct NodeList {
38 Node *N = nullptr;
39 NodeList *Next = nullptr;
Zachary Turnerf435a7e2018-07-20 17:27:48 +000040};
41
Zachary Turner8fb9a712018-12-14 18:10:13 +000042static bool isMemberPointer(StringView MangledName, bool &Error) {
43 Error = false;
Zachary Turnerd742d642018-07-26 19:56:09 +000044 switch (MangledName.popFront()) {
Zachary Turner931e8792018-07-30 23:02:10 +000045 case '$':
46 // This is probably an rvalue reference (e.g. $$Q), and you cannot have an
47 // rvalue reference to a member.
48 return false;
Zachary Turnerd742d642018-07-26 19:56:09 +000049 case 'A':
50 // 'A' indicates a reference, and you cannot have a reference to a member
Zachary Turner931e8792018-07-30 23:02:10 +000051 // function or member.
Zachary Turnerd742d642018-07-26 19:56:09 +000052 return false;
53 case 'P':
54 case 'Q':
55 case 'R':
56 case 'S':
57 // These 4 values indicate some kind of pointer, but we still don't know
58 // what.
59 break;
60 default:
Nico Weber880d21d2019-06-04 15:13:30 +000061 // isMemberPointer() is called only if isPointerType() returns true,
62 // and it rejects other prefixes.
63 DEMANGLE_UNREACHABLE;
Zachary Turnerd742d642018-07-26 19:56:09 +000064 }
65
66 // If it starts with a number, then 6 indicates a non-member function
67 // pointer, and 8 indicates a member function pointer.
68 if (startsWithDigit(MangledName)) {
Nico Weber63b97d22019-04-08 19:46:53 +000069 if (MangledName[0] != '6' && MangledName[0] != '8') {
70 Error = true;
71 return false;
72 }
Zachary Turnerd742d642018-07-26 19:56:09 +000073 return (MangledName[0] == '8');
74 }
75
76 // Remove ext qualifiers since those can appear on either type and are
77 // therefore not indicative.
78 MangledName.consumeFront('E'); // 64-bit
79 MangledName.consumeFront('I'); // restrict
80 MangledName.consumeFront('F'); // unaligned
81
Nico Weber63b97d22019-04-08 19:46:53 +000082 if (MangledName.empty()) {
83 Error = true;
84 return false;
85 }
Zachary Turnerd742d642018-07-26 19:56:09 +000086
87 // The next value should be either ABCD (non-member) or QRST (member).
88 switch (MangledName.front()) {
89 case 'A':
90 case 'B':
91 case 'C':
92 case 'D':
93 return false;
94 case 'Q':
95 case 'R':
96 case 'S':
97 case 'T':
98 return true;
99 default:
Zachary Turner8fb9a712018-12-14 18:10:13 +0000100 Error = true;
101 return false;
Zachary Turnerd742d642018-07-26 19:56:09 +0000102 }
Zachary Turnerd742d642018-07-26 19:56:09 +0000103}
104
Zachary Turner03312862018-08-27 03:48:03 +0000105static SpecialIntrinsicKind
106consumeSpecialIntrinsicKind(StringView &MangledName) {
107 if (MangledName.consumeFront("?_7"))
108 return SpecialIntrinsicKind::Vftable;
109 if (MangledName.consumeFront("?_8"))
110 return SpecialIntrinsicKind::Vbtable;
111 if (MangledName.consumeFront("?_9"))
112 return SpecialIntrinsicKind::VcallThunk;
113 if (MangledName.consumeFront("?_A"))
114 return SpecialIntrinsicKind::Typeof;
115 if (MangledName.consumeFront("?_B"))
116 return SpecialIntrinsicKind::LocalStaticGuard;
117 if (MangledName.consumeFront("?_C"))
118 return SpecialIntrinsicKind::StringLiteralSymbol;
119 if (MangledName.consumeFront("?_P"))
120 return SpecialIntrinsicKind::UdtReturning;
121 if (MangledName.consumeFront("?_R0"))
122 return SpecialIntrinsicKind::RttiTypeDescriptor;
123 if (MangledName.consumeFront("?_R1"))
124 return SpecialIntrinsicKind::RttiBaseClassDescriptor;
125 if (MangledName.consumeFront("?_R2"))
126 return SpecialIntrinsicKind::RttiBaseClassArray;
127 if (MangledName.consumeFront("?_R3"))
128 return SpecialIntrinsicKind::RttiClassHierarchyDescriptor;
129 if (MangledName.consumeFront("?_R4"))
130 return SpecialIntrinsicKind::RttiCompleteObjLocator;
131 if (MangledName.consumeFront("?_S"))
132 return SpecialIntrinsicKind::LocalVftable;
133 if (MangledName.consumeFront("?__E"))
134 return SpecialIntrinsicKind::DynamicInitializer;
135 if (MangledName.consumeFront("?__F"))
136 return SpecialIntrinsicKind::DynamicAtexitDestructor;
137 if (MangledName.consumeFront("?__J"))
138 return SpecialIntrinsicKind::LocalStaticThreadGuard;
139 return SpecialIntrinsicKind::None;
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000140}
141
Zachary Turner71c91f92018-07-30 03:12:34 +0000142static bool startsWithLocalScopePattern(StringView S) {
143 if (!S.consumeFront('?'))
144 return false;
Zachary Turner71c91f92018-07-30 03:12:34 +0000145
146 size_t End = S.find('?');
147 if (End == StringView::npos)
148 return false;
149 StringView Candidate = S.substr(0, End);
150 if (Candidate.empty())
151 return false;
152
153 // \?[0-9]\?
154 // ?@? is the discriminator 0.
155 if (Candidate.size() == 1)
156 return Candidate[0] == '@' || (Candidate[0] >= '0' && Candidate[0] <= '9');
157
158 // If it's not 0-9, then it's an encoded number terminated with an @
159 if (Candidate.back() != '@')
160 return false;
161 Candidate = Candidate.dropBack();
162
163 // An encoded number starts with B-P and all subsequent digits are in A-P.
164 // Note that the reason the first digit cannot be A is two fold. First, it
165 // would create an ambiguity with ?A which delimits the beginning of an
166 // anonymous namespace. Second, A represents 0, and you don't start a multi
167 // digit number with a leading 0. Presumably the anonymous namespace
168 // ambiguity is also why single digit encoded numbers use 0-9 rather than A-J.
169 if (Candidate[0] < 'B' || Candidate[0] > 'P')
170 return false;
171 Candidate = Candidate.dropFront();
172 while (!Candidate.empty()) {
173 if (Candidate[0] < 'A' || Candidate[0] > 'P')
174 return false;
175 Candidate = Candidate.dropFront();
176 }
177
178 return true;
179}
180
Zachary Turner03312862018-08-27 03:48:03 +0000181static bool isTagType(StringView S) {
182 switch (S.front()) {
183 case 'T': // union
184 case 'U': // struct
185 case 'V': // class
186 case 'W': // enum
187 return true;
Zachary Turner38b78a72018-07-26 20:20:10 +0000188 }
Zachary Turner03312862018-08-27 03:48:03 +0000189 return false;
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000190}
191
Zachary Turnerb2fef1a2018-08-29 04:12:44 +0000192static bool isCustomType(StringView S) { return S[0] == '?'; }
193
Zachary Turner03312862018-08-27 03:48:03 +0000194static bool isPointerType(StringView S) {
195 if (S.startsWith("$$Q")) // foo &&
196 return true;
197
198 switch (S.front()) {
199 case 'A': // foo &
200 case 'P': // foo *
201 case 'Q': // foo *const
202 case 'R': // foo *volatile
203 case 'S': // foo *const volatile
204 return true;
205 }
206 return false;
207}
208
209static bool isArrayType(StringView S) { return S[0] == 'Y'; }
210
211static bool isFunctionType(StringView S) {
212 return S.startsWith("$$A8@@") || S.startsWith("$$A6");
213}
214
215static FunctionRefQualifier
216demangleFunctionRefQualifier(StringView &MangledName) {
217 if (MangledName.consumeFront('G'))
218 return FunctionRefQualifier::Reference;
219 else if (MangledName.consumeFront('H'))
220 return FunctionRefQualifier::RValueReference;
221 return FunctionRefQualifier::None;
222}
223
224static std::pair<Qualifiers, PointerAffinity>
225demanglePointerCVQualifiers(StringView &MangledName) {
226 if (MangledName.consumeFront("$$Q"))
227 return std::make_pair(Q_None, PointerAffinity::RValueReference);
228
229 switch (MangledName.popFront()) {
230 case 'A':
231 return std::make_pair(Q_None, PointerAffinity::Reference);
232 case 'P':
233 return std::make_pair(Q_None, PointerAffinity::Pointer);
234 case 'Q':
235 return std::make_pair(Q_Const, PointerAffinity::Pointer);
236 case 'R':
237 return std::make_pair(Q_Volatile, PointerAffinity::Pointer);
238 case 'S':
239 return std::make_pair(Qualifiers(Q_Const | Q_Volatile),
240 PointerAffinity::Pointer);
Zachary Turner970fdc32018-08-16 16:17:36 +0000241 }
Nico Weberc1a0e6f2019-06-04 15:38:00 +0000242 // This function is only called if isPointerType() returns true,
243 // and it only returns true for the six cases listed above.
244 DEMANGLE_UNREACHABLE;
Zachary Turner970fdc32018-08-16 16:17:36 +0000245}
246
Zachary Turner71c91f92018-07-30 03:12:34 +0000247StringView Demangler::copyString(StringView Borrowed) {
248 char *Stable = Arena.allocUnalignedBuffer(Borrowed.size() + 1);
249 std::strcpy(Stable, Borrowed.begin());
250
251 return {Stable, Borrowed.size()};
252}
253
Zachary Turner03312862018-08-27 03:48:03 +0000254SpecialTableSymbolNode *
255Demangler::demangleSpecialTableSymbolNode(StringView &MangledName,
256 SpecialIntrinsicKind K) {
257 NamedIdentifierNode *NI = Arena.alloc<NamedIdentifierNode>();
258 switch (K) {
259 case SpecialIntrinsicKind::Vftable:
260 NI->Name = "`vftable'";
261 break;
262 case SpecialIntrinsicKind::Vbtable:
263 NI->Name = "`vbtable'";
264 break;
265 case SpecialIntrinsicKind::LocalVftable:
266 NI->Name = "`local vftable'";
267 break;
268 case SpecialIntrinsicKind::RttiCompleteObjLocator:
269 NI->Name = "`RTTI Complete Object Locator'";
270 break;
271 default:
Erik Pilkington5094e5e2019-01-17 20:37:51 +0000272 DEMANGLE_UNREACHABLE;
Zachary Turner03312862018-08-27 03:48:03 +0000273 }
274 QualifiedNameNode *QN = demangleNameScopeChain(MangledName, NI);
275 SpecialTableSymbolNode *STSN = Arena.alloc<SpecialTableSymbolNode>();
276 STSN->Name = QN;
Zachary Turner469f0762018-08-17 21:18:05 +0000277 bool IsMember = false;
Nico Weberae050d22019-04-14 23:32:37 +0000278 if (MangledName.empty()) {
279 Error = true;
280 return nullptr;
281 }
Zachary Turner03312862018-08-27 03:48:03 +0000282 char Front = MangledName.popFront();
283 if (Front != '6' && Front != '7') {
284 Error = true;
285 return nullptr;
286 }
287
288 std::tie(STSN->Quals, IsMember) = demangleQualifiers(MangledName);
289 if (!MangledName.consumeFront('@'))
290 STSN->TargetName = demangleFullyQualifiedTypeName(MangledName);
291 return STSN;
292}
293
294LocalStaticGuardVariableNode *
Nico Weber88ab2812019-05-28 14:54:49 +0000295Demangler::demangleLocalStaticGuard(StringView &MangledName, bool IsThread) {
Zachary Turner03312862018-08-27 03:48:03 +0000296 LocalStaticGuardIdentifierNode *LSGI =
297 Arena.alloc<LocalStaticGuardIdentifierNode>();
Nico Weber88ab2812019-05-28 14:54:49 +0000298 LSGI->IsThread = IsThread;
Zachary Turner03312862018-08-27 03:48:03 +0000299 QualifiedNameNode *QN = demangleNameScopeChain(MangledName, LSGI);
300 LocalStaticGuardVariableNode *LSGVN =
301 Arena.alloc<LocalStaticGuardVariableNode>();
302 LSGVN->Name = QN;
303
304 if (MangledName.consumeFront("4IA"))
305 LSGVN->IsVisible = false;
306 else if (MangledName.consumeFront("5"))
307 LSGVN->IsVisible = true;
308 else {
309 Error = true;
310 return nullptr;
311 }
312
313 if (!MangledName.empty())
314 LSGI->ScopeIndex = demangleUnsigned(MangledName);
315 return LSGVN;
316}
317
318static NamedIdentifierNode *synthesizeNamedIdentifier(ArenaAllocator &Arena,
319 StringView Name) {
320 NamedIdentifierNode *Id = Arena.alloc<NamedIdentifierNode>();
321 Id->Name = Name;
322 return Id;
323}
324
325static QualifiedNameNode *synthesizeQualifiedName(ArenaAllocator &Arena,
326 IdentifierNode *Identifier) {
327 QualifiedNameNode *QN = Arena.alloc<QualifiedNameNode>();
328 QN->Components = Arena.alloc<NodeArrayNode>();
329 QN->Components->Count = 1;
330 QN->Components->Nodes = Arena.allocArray<Node *>(1);
331 QN->Components->Nodes[0] = Identifier;
332 return QN;
333}
334
335static QualifiedNameNode *synthesizeQualifiedName(ArenaAllocator &Arena,
336 StringView Name) {
337 NamedIdentifierNode *Id = synthesizeNamedIdentifier(Arena, Name);
338 return synthesizeQualifiedName(Arena, Id);
339}
340
341static VariableSymbolNode *synthesizeVariable(ArenaAllocator &Arena,
342 TypeNode *Type,
343 StringView VariableName) {
344 VariableSymbolNode *VSN = Arena.alloc<VariableSymbolNode>();
345 VSN->Type = Type;
346 VSN->Name = synthesizeQualifiedName(Arena, VariableName);
347 return VSN;
348}
349
350VariableSymbolNode *Demangler::demangleUntypedVariable(
351 ArenaAllocator &Arena, StringView &MangledName, StringView VariableName) {
352 NamedIdentifierNode *NI = synthesizeNamedIdentifier(Arena, VariableName);
353 QualifiedNameNode *QN = demangleNameScopeChain(MangledName, NI);
354 VariableSymbolNode *VSN = Arena.alloc<VariableSymbolNode>();
355 VSN->Name = QN;
356 if (MangledName.consumeFront("8"))
357 return VSN;
358
359 Error = true;
360 return nullptr;
361}
362
363VariableSymbolNode *
364Demangler::demangleRttiBaseClassDescriptorNode(ArenaAllocator &Arena,
365 StringView &MangledName) {
366 RttiBaseClassDescriptorNode *RBCDN =
367 Arena.alloc<RttiBaseClassDescriptorNode>();
368 RBCDN->NVOffset = demangleUnsigned(MangledName);
369 RBCDN->VBPtrOffset = demangleSigned(MangledName);
370 RBCDN->VBTableOffset = demangleUnsigned(MangledName);
371 RBCDN->Flags = demangleUnsigned(MangledName);
372 if (Error)
373 return nullptr;
374
375 VariableSymbolNode *VSN = Arena.alloc<VariableSymbolNode>();
376 VSN->Name = demangleNameScopeChain(MangledName, RBCDN);
377 MangledName.consumeFront('8');
378 return VSN;
379}
380
Zachary Turner32a8a202018-08-29 23:56:09 +0000381FunctionSymbolNode *Demangler::demangleInitFiniStub(StringView &MangledName,
382 bool IsDestructor) {
Zachary Turner03312862018-08-27 03:48:03 +0000383 DynamicStructorIdentifierNode *DSIN =
384 Arena.alloc<DynamicStructorIdentifierNode>();
385 DSIN->IsDestructor = IsDestructor;
Zachary Turner32a8a202018-08-29 23:56:09 +0000386
Zachary Turner78ab3cb2018-08-30 20:53:29 +0000387 bool IsKnownStaticDataMember = false;
388 if (MangledName.consumeFront('?'))
389 IsKnownStaticDataMember = true;
390
Nico Weber63b97d22019-04-08 19:46:53 +0000391 SymbolNode *Symbol = demangleDeclarator(MangledName);
392 if (Error)
393 return nullptr;
Zachary Turner32a8a202018-08-29 23:56:09 +0000394
Zachary Turner32a8a202018-08-29 23:56:09 +0000395 FunctionSymbolNode *FSN = nullptr;
Zachary Turner32a8a202018-08-29 23:56:09 +0000396
397 if (Symbol->kind() == NodeKind::VariableSymbol) {
398 DSIN->Variable = static_cast<VariableSymbolNode *>(Symbol);
Zachary Turner78ab3cb2018-08-30 20:53:29 +0000399
400 // Older versions of clang mangled this type of symbol incorrectly. They
401 // would omit the leading ? and they would only emit a single @ at the end.
402 // The correct mangling is a leading ? and 2 trailing @ signs. Handle
403 // both cases.
404 int AtCount = IsKnownStaticDataMember ? 2 : 1;
405 for (int I = 0; I < AtCount; ++I) {
406 if (MangledName.consumeFront('@'))
407 continue;
Zachary Turner32a8a202018-08-29 23:56:09 +0000408 Error = true;
409 return nullptr;
410 }
411
412 FSN = demangleFunctionEncoding(MangledName);
Nico Weber63b97d22019-04-08 19:46:53 +0000413 if (FSN)
414 FSN->Name = synthesizeQualifiedName(Arena, DSIN);
Zachary Turner32a8a202018-08-29 23:56:09 +0000415 } else {
Zachary Turner78ab3cb2018-08-30 20:53:29 +0000416 if (IsKnownStaticDataMember) {
417 // This was supposed to be a static data member, but we got a function.
418 Error = true;
419 return nullptr;
420 }
421
Zachary Turner32a8a202018-08-29 23:56:09 +0000422 FSN = static_cast<FunctionSymbolNode *>(Symbol);
423 DSIN->Name = Symbol->Name;
424 FSN->Name = synthesizeQualifiedName(Arena, DSIN);
425 }
426
Zachary Turner03312862018-08-27 03:48:03 +0000427 return FSN;
428}
429
430SymbolNode *Demangler::demangleSpecialIntrinsic(StringView &MangledName) {
431 SpecialIntrinsicKind SIK = consumeSpecialIntrinsicKind(MangledName);
Zachary Turner03312862018-08-27 03:48:03 +0000432
433 switch (SIK) {
Nico Weberd98a0a32019-06-04 16:25:28 +0000434 case SpecialIntrinsicKind::None:
435 return nullptr;
Zachary Turner03312862018-08-27 03:48:03 +0000436 case SpecialIntrinsicKind::StringLiteralSymbol:
437 return demangleStringLiteral(MangledName);
438 case SpecialIntrinsicKind::Vftable:
439 case SpecialIntrinsicKind::Vbtable:
440 case SpecialIntrinsicKind::LocalVftable:
441 case SpecialIntrinsicKind::RttiCompleteObjLocator:
442 return demangleSpecialTableSymbolNode(MangledName, SIK);
443 case SpecialIntrinsicKind::VcallThunk:
444 return demangleVcallThunkNode(MangledName);
445 case SpecialIntrinsicKind::LocalStaticGuard:
Nico Weber88ab2812019-05-28 14:54:49 +0000446 return demangleLocalStaticGuard(MangledName, /*IsThread=*/false);
447 case SpecialIntrinsicKind::LocalStaticThreadGuard:
448 return demangleLocalStaticGuard(MangledName, /*IsThread=*/true);
Zachary Turner03312862018-08-27 03:48:03 +0000449 case SpecialIntrinsicKind::RttiTypeDescriptor: {
450 TypeNode *T = demangleType(MangledName, QualifierMangleMode::Result);
Zachary Turner469f0762018-08-17 21:18:05 +0000451 if (Error)
452 break;
453 if (!MangledName.consumeFront("@8"))
Zachary Turner03312862018-08-27 03:48:03 +0000454 break;
Zachary Turner469f0762018-08-17 21:18:05 +0000455 if (!MangledName.empty())
Zachary Turner03312862018-08-27 03:48:03 +0000456 break;
457 return synthesizeVariable(Arena, T, "`RTTI Type Descriptor'");
458 }
459 case SpecialIntrinsicKind::RttiBaseClassArray:
460 return demangleUntypedVariable(Arena, MangledName,
461 "`RTTI Base Class Array'");
462 case SpecialIntrinsicKind::RttiClassHierarchyDescriptor:
463 return demangleUntypedVariable(Arena, MangledName,
464 "`RTTI Class Hierarchy Descriptor'");
465 case SpecialIntrinsicKind::RttiBaseClassDescriptor:
466 return demangleRttiBaseClassDescriptorNode(Arena, MangledName);
467 case SpecialIntrinsicKind::DynamicInitializer:
Nico Weber878df1c2019-06-04 18:06:28 +0000468 return demangleInitFiniStub(MangledName, /*IsDestructor=*/false);
Zachary Turner03312862018-08-27 03:48:03 +0000469 case SpecialIntrinsicKind::DynamicAtexitDestructor:
Nico Weber878df1c2019-06-04 18:06:28 +0000470 return demangleInitFiniStub(MangledName, /*IsDestructor=*/true);
Nico Weberd98a0a32019-06-04 16:25:28 +0000471 case SpecialIntrinsicKind::Typeof:
472 case SpecialIntrinsicKind::UdtReturning:
473 // It's unclear which tools produces these manglings, so demangling
474 // support is not (yet?) implemented.
Zachary Turner469f0762018-08-17 21:18:05 +0000475 break;
Nico Weberd98a0a32019-06-04 16:25:28 +0000476 case SpecialIntrinsicKind::Unknown:
477 DEMANGLE_UNREACHABLE; // Never returned by consumeSpecialIntrinsicKind.
Zachary Turner469f0762018-08-17 21:18:05 +0000478 }
Zachary Turner03312862018-08-27 03:48:03 +0000479 Error = true;
480 return nullptr;
Zachary Turner469f0762018-08-17 21:18:05 +0000481}
482
Zachary Turner03312862018-08-27 03:48:03 +0000483IdentifierNode *
484Demangler::demangleFunctionIdentifierCode(StringView &MangledName) {
485 assert(MangledName.startsWith('?'));
486 MangledName = MangledName.dropFront();
Nico Weber321de482019-04-03 23:19:39 +0000487 if (MangledName.empty()) {
488 Error = true;
489 return nullptr;
490 }
Zachary Turner03312862018-08-27 03:48:03 +0000491
492 if (MangledName.consumeFront("__"))
493 return demangleFunctionIdentifierCode(
494 MangledName, FunctionIdentifierCodeGroup::DoubleUnder);
Nico Weberb4f33bb2019-04-11 22:47:18 +0000495 if (MangledName.consumeFront("_"))
Zachary Turner03312862018-08-27 03:48:03 +0000496 return demangleFunctionIdentifierCode(MangledName,
497 FunctionIdentifierCodeGroup::Under);
498 return demangleFunctionIdentifierCode(MangledName,
499 FunctionIdentifierCodeGroup::Basic);
500}
501
502StructorIdentifierNode *
503Demangler::demangleStructorIdentifier(StringView &MangledName,
504 bool IsDestructor) {
505 StructorIdentifierNode *N = Arena.alloc<StructorIdentifierNode>();
506 N->IsDestructor = IsDestructor;
507 return N;
508}
509
510ConversionOperatorIdentifierNode *
511Demangler::demangleConversionOperatorIdentifier(StringView &MangledName) {
512 ConversionOperatorIdentifierNode *N =
513 Arena.alloc<ConversionOperatorIdentifierNode>();
514 return N;
515}
516
517LiteralOperatorIdentifierNode *
518Demangler::demangleLiteralOperatorIdentifier(StringView &MangledName) {
519 LiteralOperatorIdentifierNode *N =
520 Arena.alloc<LiteralOperatorIdentifierNode>();
Nico Weberaf2ee7d2019-04-11 23:20:18 +0000521 N->Name = demangleSimpleString(MangledName, /*Memorize=*/false);
Zachary Turner03312862018-08-27 03:48:03 +0000522 return N;
523}
524
Nico Weber03db6252019-04-11 23:11:33 +0000525IntrinsicFunctionKind
526Demangler::translateIntrinsicFunctionCode(char CH,
527 FunctionIdentifierCodeGroup Group) {
528 using IFK = IntrinsicFunctionKind;
529 if (!(CH >= '0' && CH <= '9') && !(CH >= 'A' && CH <= 'Z')) {
530 Error = true;
531 return IFK::None;
532 }
533
Zachary Turner03312862018-08-27 03:48:03 +0000534 // Not all ? identifiers are intrinsics *functions*. This function only maps
535 // operator codes for the special functions, all others are handled elsewhere,
536 // hence the IFK::None entries in the table.
Zachary Turner03312862018-08-27 03:48:03 +0000537 static IFK Basic[36] = {
538 IFK::None, // ?0 # Foo::Foo()
539 IFK::None, // ?1 # Foo::~Foo()
540 IFK::New, // ?2 # operator new
541 IFK::Delete, // ?3 # operator delete
542 IFK::Assign, // ?4 # operator=
543 IFK::RightShift, // ?5 # operator>>
544 IFK::LeftShift, // ?6 # operator<<
545 IFK::LogicalNot, // ?7 # operator!
546 IFK::Equals, // ?8 # operator==
547 IFK::NotEquals, // ?9 # operator!=
548 IFK::ArraySubscript, // ?A # operator[]
549 IFK::None, // ?B # Foo::operator <type>()
550 IFK::Pointer, // ?C # operator->
551 IFK::Dereference, // ?D # operator*
552 IFK::Increment, // ?E # operator++
553 IFK::Decrement, // ?F # operator--
554 IFK::Minus, // ?G # operator-
555 IFK::Plus, // ?H # operator+
556 IFK::BitwiseAnd, // ?I # operator&
557 IFK::MemberPointer, // ?J # operator->*
558 IFK::Divide, // ?K # operator/
559 IFK::Modulus, // ?L # operator%
560 IFK::LessThan, // ?M operator<
561 IFK::LessThanEqual, // ?N operator<=
562 IFK::GreaterThan, // ?O operator>
563 IFK::GreaterThanEqual, // ?P operator>=
564 IFK::Comma, // ?Q operator,
565 IFK::Parens, // ?R operator()
566 IFK::BitwiseNot, // ?S operator~
567 IFK::BitwiseXor, // ?T operator^
568 IFK::BitwiseOr, // ?U operator|
569 IFK::LogicalAnd, // ?V operator&&
570 IFK::LogicalOr, // ?W operator||
571 IFK::TimesEqual, // ?X operator*=
572 IFK::PlusEqual, // ?Y operator+=
573 IFK::MinusEqual, // ?Z operator-=
574 };
575 static IFK Under[36] = {
576 IFK::DivEqual, // ?_0 operator/=
577 IFK::ModEqual, // ?_1 operator%=
578 IFK::RshEqual, // ?_2 operator>>=
579 IFK::LshEqual, // ?_3 operator<<=
580 IFK::BitwiseAndEqual, // ?_4 operator&=
581 IFK::BitwiseOrEqual, // ?_5 operator|=
582 IFK::BitwiseXorEqual, // ?_6 operator^=
583 IFK::None, // ?_7 # vftable
584 IFK::None, // ?_8 # vbtable
585 IFK::None, // ?_9 # vcall
586 IFK::None, // ?_A # typeof
587 IFK::None, // ?_B # local static guard
588 IFK::None, // ?_C # string literal
589 IFK::VbaseDtor, // ?_D # vbase destructor
590 IFK::VecDelDtor, // ?_E # vector deleting destructor
591 IFK::DefaultCtorClosure, // ?_F # default constructor closure
592 IFK::ScalarDelDtor, // ?_G # scalar deleting destructor
593 IFK::VecCtorIter, // ?_H # vector constructor iterator
594 IFK::VecDtorIter, // ?_I # vector destructor iterator
595 IFK::VecVbaseCtorIter, // ?_J # vector vbase constructor iterator
596 IFK::VdispMap, // ?_K # virtual displacement map
597 IFK::EHVecCtorIter, // ?_L # eh vector constructor iterator
598 IFK::EHVecDtorIter, // ?_M # eh vector destructor iterator
599 IFK::EHVecVbaseCtorIter, // ?_N # eh vector vbase constructor iterator
600 IFK::CopyCtorClosure, // ?_O # copy constructor closure
601 IFK::None, // ?_P<name> # udt returning <name>
602 IFK::None, // ?_Q # <unknown>
603 IFK::None, // ?_R0 - ?_R4 # RTTI Codes
604 IFK::None, // ?_S # local vftable
605 IFK::LocalVftableCtorClosure, // ?_T # local vftable constructor closure
606 IFK::ArrayNew, // ?_U operator new[]
607 IFK::ArrayDelete, // ?_V operator delete[]
608 IFK::None, // ?_W <unused>
609 IFK::None, // ?_X <unused>
610 IFK::None, // ?_Y <unused>
611 IFK::None, // ?_Z <unused>
612 };
613 static IFK DoubleUnder[36] = {
614 IFK::None, // ?__0 <unused>
615 IFK::None, // ?__1 <unused>
616 IFK::None, // ?__2 <unused>
617 IFK::None, // ?__3 <unused>
618 IFK::None, // ?__4 <unused>
619 IFK::None, // ?__5 <unused>
620 IFK::None, // ?__6 <unused>
621 IFK::None, // ?__7 <unused>
622 IFK::None, // ?__8 <unused>
623 IFK::None, // ?__9 <unused>
624 IFK::ManVectorCtorIter, // ?__A managed vector ctor iterator
625 IFK::ManVectorDtorIter, // ?__B managed vector dtor iterator
626 IFK::EHVectorCopyCtorIter, // ?__C EH vector copy ctor iterator
627 IFK::EHVectorVbaseCopyCtorIter, // ?__D EH vector vbase copy ctor iter
628 IFK::None, // ?__E dynamic initializer for `T'
629 IFK::None, // ?__F dynamic atexit destructor for `T'
630 IFK::VectorCopyCtorIter, // ?__G vector copy constructor iter
631 IFK::VectorVbaseCopyCtorIter, // ?__H vector vbase copy ctor iter
632 IFK::ManVectorVbaseCopyCtorIter, // ?__I managed vector vbase copy ctor
633 // iter
634 IFK::None, // ?__J local static thread guard
635 IFK::None, // ?__K operator ""_name
Nico Webere8f21b12019-04-23 16:20:27 +0000636 IFK::CoAwait, // ?__L operator co_await
637 IFK::Spaceship, // ?__M operator<=>
Zachary Turner03312862018-08-27 03:48:03 +0000638 IFK::None, // ?__N <unused>
639 IFK::None, // ?__O <unused>
640 IFK::None, // ?__P <unused>
641 IFK::None, // ?__Q <unused>
642 IFK::None, // ?__R <unused>
643 IFK::None, // ?__S <unused>
644 IFK::None, // ?__T <unused>
645 IFK::None, // ?__U <unused>
646 IFK::None, // ?__V <unused>
647 IFK::None, // ?__W <unused>
648 IFK::None, // ?__X <unused>
649 IFK::None, // ?__Y <unused>
650 IFK::None, // ?__Z <unused>
651 };
652
653 int Index = (CH >= '0' && CH <= '9') ? (CH - '0') : (CH - 'A' + 10);
654 switch (Group) {
655 case FunctionIdentifierCodeGroup::Basic:
656 return Basic[Index];
657 case FunctionIdentifierCodeGroup::Under:
658 return Under[Index];
659 case FunctionIdentifierCodeGroup::DoubleUnder:
660 return DoubleUnder[Index];
661 }
Erik Pilkington5094e5e2019-01-17 20:37:51 +0000662 DEMANGLE_UNREACHABLE;
Zachary Turner03312862018-08-27 03:48:03 +0000663}
664
665IdentifierNode *
666Demangler::demangleFunctionIdentifierCode(StringView &MangledName,
667 FunctionIdentifierCodeGroup Group) {
Nico Weber63fe2592019-04-14 23:08:12 +0000668 if (MangledName.empty()) {
669 Error = true;
670 return nullptr;
671 }
Zachary Turner03312862018-08-27 03:48:03 +0000672 switch (Group) {
673 case FunctionIdentifierCodeGroup::Basic:
674 switch (char CH = MangledName.popFront()) {
675 case '0':
676 case '1':
677 return demangleStructorIdentifier(MangledName, CH == '1');
678 case 'B':
679 return demangleConversionOperatorIdentifier(MangledName);
680 default:
681 return Arena.alloc<IntrinsicFunctionIdentifierNode>(
682 translateIntrinsicFunctionCode(CH, Group));
683 }
Zachary Turner03312862018-08-27 03:48:03 +0000684 case FunctionIdentifierCodeGroup::Under:
685 return Arena.alloc<IntrinsicFunctionIdentifierNode>(
686 translateIntrinsicFunctionCode(MangledName.popFront(), Group));
687 case FunctionIdentifierCodeGroup::DoubleUnder:
688 switch (char CH = MangledName.popFront()) {
689 case 'K':
690 return demangleLiteralOperatorIdentifier(MangledName);
691 default:
692 return Arena.alloc<IntrinsicFunctionIdentifierNode>(
693 translateIntrinsicFunctionCode(CH, Group));
694 }
695 }
Zachary Turner03312862018-08-27 03:48:03 +0000696
Nico Weberef035182019-04-11 23:23:00 +0000697 DEMANGLE_UNREACHABLE;
Zachary Turner03312862018-08-27 03:48:03 +0000698}
699
700SymbolNode *Demangler::demangleEncodedSymbol(StringView &MangledName,
701 QualifiedNameNode *Name) {
Nico Weber63b97d22019-04-08 19:46:53 +0000702 if (MangledName.empty()) {
703 Error = true;
704 return nullptr;
705 }
706
Zachary Turner469f0762018-08-17 21:18:05 +0000707 // Read a variable.
708 switch (MangledName.front()) {
709 case '0':
710 case '1':
711 case '2':
712 case '3':
Zachary Turner03312862018-08-27 03:48:03 +0000713 case '4': {
714 StorageClass SC = demangleVariableStorageClass(MangledName);
715 return demangleVariableEncoding(MangledName, SC);
Zachary Turner469f0762018-08-17 21:18:05 +0000716 }
Zachary Turner03312862018-08-27 03:48:03 +0000717 }
718 FunctionSymbolNode *FSN = demangleFunctionEncoding(MangledName);
719
720 IdentifierNode *UQN = Name->getUnqualifiedIdentifier();
721 if (UQN->kind() == NodeKind::ConversionOperatorIdentifier) {
722 ConversionOperatorIdentifierNode *COIN =
723 static_cast<ConversionOperatorIdentifierNode *>(UQN);
Nico Weber63b97d22019-04-08 19:46:53 +0000724 if (FSN)
725 COIN->TargetType = FSN->Signature->ReturnType;
Zachary Turner03312862018-08-27 03:48:03 +0000726 }
727 return FSN;
Zachary Turner469f0762018-08-17 21:18:05 +0000728}
729
Nico Weber63b97d22019-04-08 19:46:53 +0000730SymbolNode *Demangler::demangleDeclarator(StringView &MangledName) {
731 // What follows is a main symbol name. This may include namespaces or class
732 // back references.
733 QualifiedNameNode *QN = demangleFullyQualifiedSymbolName(MangledName);
734 if (Error)
735 return nullptr;
736
737 SymbolNode *Symbol = demangleEncodedSymbol(MangledName, QN);
738 if (Error)
739 return nullptr;
740 Symbol->Name = QN;
741
742 IdentifierNode *UQN = QN->getUnqualifiedIdentifier();
743 if (UQN->kind() == NodeKind::ConversionOperatorIdentifier) {
744 ConversionOperatorIdentifierNode *COIN =
745 static_cast<ConversionOperatorIdentifierNode *>(UQN);
746 if (!COIN->TargetType) {
747 Error = true;
748 return nullptr;
749 }
750 }
751 return Symbol;
752}
753
Nico Weber82dc06c2019-05-27 23:10:42 +0000754SymbolNode *Demangler::demangleMD5Name(StringView &MangledName) {
755 assert(MangledName.startsWith("??@"));
756 // This is an MD5 mangled name. We can't demangle it, just return the
757 // mangled name.
758 // An MD5 mangled name is ??@ followed by 32 characters and a terminating @.
759 size_t MD5Last = MangledName.find('@', strlen("??@"));
760 if (MD5Last == StringView::npos) {
761 Error = true;
762 return nullptr;
763 }
764 const char *Start = MangledName.begin();
765 MangledName = MangledName.dropFront(MD5Last + 1);
766
767 // There are two additional special cases for MD5 names:
768 // 1. For complete object locators where the object name is long enough
769 // for the object to have an MD5 name, the complete object locator is
770 // called ??@...@??_R4@ (with a trailing "??_R4@" instead of the usual
771 // leading "??_R4". This is handled here.
772 // 2. For catchable types, in versions of MSVC before 2015 (<1900) or after
773 // 2017.2 (>= 1914), the catchable type mangling is _CT??@...@??@...@8
774 // instead of_CT??@...@8 with just one MD5 name. Since we don't yet
775 // demangle catchable types anywhere, this isn't handled for MD5 names
776 // either.
777 MangledName.consumeFront("??_R4@");
778
779 StringView MD5(Start, MangledName.begin());
780 SymbolNode *S = Arena.alloc<SymbolNode>(NodeKind::Md5Symbol);
781 S->Name = synthesizeQualifiedName(Arena, MD5);
782
783 return S;
784}
785
Nico Weberda298aa2019-09-23 13:13:37 +0000786SymbolNode *Demangler::demangleTypeinfoName(StringView &MangledName) {
787 assert(MangledName.startsWith('.'));
788 MangledName.consumeFront('.');
789
790 TypeNode *T = demangleType(MangledName, QualifierMangleMode::Result);
791 if (Error || !MangledName.empty()) {
792 Error = true;
793 return nullptr;
794 }
795 return synthesizeVariable(Arena, T, "`RTTI Type Descriptor Name'");
796}
797
Zachary Turner469f0762018-08-17 21:18:05 +0000798// Parser entry point.
Zachary Turner03312862018-08-27 03:48:03 +0000799SymbolNode *Demangler::parse(StringView &MangledName) {
Nico Weberda298aa2019-09-23 13:13:37 +0000800 // Typeinfo names are strings stored in RTTI data. They're not symbol names.
801 // It's still useful to demangle them. They're the only demangled entity
802 // that doesn't start with a "?" but a ".".
803 if (MangledName.startsWith('.'))
804 return demangleTypeinfoName(MangledName);
805
Nico Weber82dc06c2019-05-27 23:10:42 +0000806 if (MangledName.startsWith("??@"))
807 return demangleMD5Name(MangledName);
Zachary Turner83313f82018-08-16 16:17:17 +0000808
Nico Webercfe08bc2019-05-27 00:48:59 +0000809 // MSVC-style mangled symbols must start with '?'.
Zachary Turner03312862018-08-27 03:48:03 +0000810 if (!MangledName.startsWith('?')) {
811 Error = true;
812 return nullptr;
813 }
814
Zachary Turner469f0762018-08-17 21:18:05 +0000815 MangledName.consumeFront('?');
Zachary Turner970fdc32018-08-16 16:17:36 +0000816
Zachary Turner469f0762018-08-17 21:18:05 +0000817 // ?$ is a template instantiation, but all other names that start with ? are
818 // operators / special names.
Zachary Turner03312862018-08-27 03:48:03 +0000819 if (SymbolNode *SI = demangleSpecialIntrinsic(MangledName))
820 return SI;
Zachary Turner469f0762018-08-17 21:18:05 +0000821
Nico Weber63b97d22019-04-08 19:46:53 +0000822 return demangleDeclarator(MangledName);
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000823}
824
Zachary Turner7ba90562018-11-01 15:07:32 +0000825TagTypeNode *Demangler::parseTagUniqueName(StringView &MangledName) {
826 if (!MangledName.consumeFront(".?A"))
827 return nullptr;
828 MangledName.consumeFront(".?A");
829 if (MangledName.empty())
830 return nullptr;
831
832 return demangleClassType(MangledName);
833}
834
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000835// <type-encoding> ::= <storage-class> <variable-type>
836// <storage-class> ::= 0 # private static member
837// ::= 1 # protected static member
838// ::= 2 # public static member
839// ::= 3 # global
840// ::= 4 # static local
841
Zachary Turner03312862018-08-27 03:48:03 +0000842VariableSymbolNode *Demangler::demangleVariableEncoding(StringView &MangledName,
843 StorageClass SC) {
844 VariableSymbolNode *VSN = Arena.alloc<VariableSymbolNode>();
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000845
Zachary Turner03312862018-08-27 03:48:03 +0000846 VSN->Type = demangleType(MangledName, QualifierMangleMode::Drop);
847 VSN->SC = SC;
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000848
Nico Weber63b97d22019-04-08 19:46:53 +0000849 if (Error)
850 return nullptr;
851
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000852 // <variable-type> ::= <type> <cvr-qualifiers>
853 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
Zachary Turner03312862018-08-27 03:48:03 +0000854 switch (VSN->Type->kind()) {
855 case NodeKind::PointerType: {
856 PointerTypeNode *PTN = static_cast<PointerTypeNode *>(VSN->Type);
857
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000858 Qualifiers ExtraChildQuals = Q_None;
Zachary Turner03312862018-08-27 03:48:03 +0000859 PTN->Quals = Qualifiers(VSN->Type->Quals |
860 demanglePointerExtQualifiers(MangledName));
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000861
Zachary Turnerd742d642018-07-26 19:56:09 +0000862 bool IsMember = false;
Zachary Turner316109b2018-07-29 16:38:02 +0000863 std::tie(ExtraChildQuals, IsMember) = demangleQualifiers(MangledName);
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000864
Zachary Turner03312862018-08-27 03:48:03 +0000865 if (PTN->ClassParent) {
866 QualifiedNameNode *BackRefName =
867 demangleFullyQualifiedTypeName(MangledName);
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000868 (void)BackRefName;
869 }
Zachary Turner03312862018-08-27 03:48:03 +0000870 PTN->Pointee->Quals = Qualifiers(PTN->Pointee->Quals | ExtraChildQuals);
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000871
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000872 break;
873 }
874 default:
Zachary Turner03312862018-08-27 03:48:03 +0000875 VSN->Type->Quals = demangleQualifiers(MangledName).first;
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000876 break;
877 }
878
Zachary Turner03312862018-08-27 03:48:03 +0000879 return VSN;
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000880}
881
882// Sometimes numbers are encoded in mangled symbols. For example,
883// "int (*x)[20]" is a valid C type (x is a pointer to an array of
884// length 20), so we need some way to embed numbers as part of symbols.
885// This function parses it.
886//
887// <number> ::= [?] <non-negative integer>
888//
889// <non-negative integer> ::= <decimal digit> # when 1 <= Number <= 10
Nico Weber502cf4b2019-04-18 19:30:21 +0000890// ::= <hex digit>+ @ # when Number == 0 or >= 10
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000891//
892// <hex-digit> ::= [A-P] # A = 0, B = 1, ...
Zachary Turnerdbefc6c2018-08-10 14:31:04 +0000893std::pair<uint64_t, bool> Demangler::demangleNumber(StringView &MangledName) {
894 bool IsNegative = MangledName.consumeFront('?');
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000895
896 if (startsWithDigit(MangledName)) {
Zachary Turnerdbefc6c2018-08-10 14:31:04 +0000897 uint64_t Ret = MangledName[0] - '0' + 1;
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000898 MangledName = MangledName.dropFront(1);
Zachary Turnerdbefc6c2018-08-10 14:31:04 +0000899 return {Ret, IsNegative};
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000900 }
901
Zachary Turnerdbefc6c2018-08-10 14:31:04 +0000902 uint64_t Ret = 0;
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000903 for (size_t i = 0; i < MangledName.size(); ++i) {
904 char C = MangledName[i];
905 if (C == '@') {
906 MangledName = MangledName.dropFront(i + 1);
Zachary Turnerdbefc6c2018-08-10 14:31:04 +0000907 return {Ret, IsNegative};
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000908 }
909 if ('A' <= C && C <= 'P') {
910 Ret = (Ret << 4) + (C - 'A');
911 continue;
912 }
913 break;
914 }
915
916 Error = true;
Zachary Turnerdbefc6c2018-08-10 14:31:04 +0000917 return {0ULL, false};
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000918}
919
Zachary Turner469f0762018-08-17 21:18:05 +0000920uint64_t Demangler::demangleUnsigned(StringView &MangledName) {
921 bool IsNegative = false;
922 uint64_t Number = 0;
923 std::tie(Number, IsNegative) = demangleNumber(MangledName);
924 if (IsNegative)
925 Error = true;
926 return Number;
927}
928
929int64_t Demangler::demangleSigned(StringView &MangledName) {
930 bool IsNegative = false;
931 uint64_t Number = 0;
932 std::tie(Number, IsNegative) = demangleNumber(MangledName);
933 if (Number > INT64_MAX)
934 Error = true;
935 int64_t I = static_cast<int64_t>(Number);
936 return IsNegative ? -I : I;
937}
938
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000939// First 10 strings can be referenced by special BackReferences ?0, ?1, ..., ?9.
940// Memorize it.
941void Demangler::memorizeString(StringView S) {
Zachary Turnerd346cba2018-08-08 17:17:04 +0000942 if (Backrefs.NamesCount >= BackrefContext::Max)
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000943 return;
Zachary Turnerd346cba2018-08-08 17:17:04 +0000944 for (size_t i = 0; i < Backrefs.NamesCount; ++i)
Zachary Turner03312862018-08-27 03:48:03 +0000945 if (S == Backrefs.Names[i]->Name)
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000946 return;
Zachary Turner03312862018-08-27 03:48:03 +0000947 NamedIdentifierNode *N = Arena.alloc<NamedIdentifierNode>();
948 N->Name = S;
949 Backrefs.Names[Backrefs.NamesCount++] = N;
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000950}
951
Zachary Turner03312862018-08-27 03:48:03 +0000952NamedIdentifierNode *Demangler::demangleBackRefName(StringView &MangledName) {
Zachary Turnera7dffb12018-07-28 22:10:42 +0000953 assert(startsWithDigit(MangledName));
Zachary Turnerd9e925f2018-08-18 18:49:48 +0000954
955 size_t I = MangledName[0] - '0';
956 if (I >= Backrefs.NamesCount) {
957 Error = true;
958 return nullptr;
959 }
960
Zachary Turner172aea12018-08-02 17:08:03 +0000961 MangledName = MangledName.dropFront();
Zachary Turner03312862018-08-27 03:48:03 +0000962 return Backrefs.Names[I];
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000963}
964
Zachary Turnerb2fef1a2018-08-29 04:12:44 +0000965void Demangler::memorizeIdentifier(IdentifierNode *Identifier) {
966 // Render this class template name into a string buffer so that we can
967 // memorize it for the purpose of back-referencing.
Nico Weber1359d652018-09-15 18:24:20 +0000968 OutputStream OS;
Nico Weber6808bc02018-11-11 10:04:00 +0000969 if (!initializeOutputStream(nullptr, nullptr, OS, 1024))
Nico Weber1359d652018-09-15 18:24:20 +0000970 // FIXME: Propagate out-of-memory as an error?
971 std::terminate();
Zachary Turnerb2fef1a2018-08-29 04:12:44 +0000972 Identifier->output(OS, OF_Default);
973 OS << '\0';
974 char *Name = OS.getBuffer();
975
976 StringView Owned = copyString(Name);
977 memorizeString(Owned);
978 std::free(Name);
979}
980
Zachary Turner03312862018-08-27 03:48:03 +0000981IdentifierNode *
982Demangler::demangleTemplateInstantiationName(StringView &MangledName,
983 NameBackrefBehavior NBB) {
Zachary Turnera7dffb12018-07-28 22:10:42 +0000984 assert(MangledName.startsWith("?$"));
985 MangledName.consumeFront("?$");
986
Zachary Turnerd346cba2018-08-08 17:17:04 +0000987 BackrefContext OuterContext;
988 std::swap(OuterContext, Backrefs);
Zachary Turner54d4ffe2018-08-01 18:32:28 +0000989
Zachary Turner03312862018-08-27 03:48:03 +0000990 IdentifierNode *Identifier =
991 demangleUnqualifiedSymbolName(MangledName, NBB_Simple);
Zachary Turnerd346cba2018-08-08 17:17:04 +0000992 if (!Error)
Zachary Turner03312862018-08-27 03:48:03 +0000993 Identifier->TemplateParams = demangleTemplateParameterList(MangledName);
Zachary Turnerd346cba2018-08-08 17:17:04 +0000994
995 std::swap(OuterContext, Backrefs);
Zachary Turner54d4ffe2018-08-01 18:32:28 +0000996 if (Error)
997 return nullptr;
Zachary Turner71c91f92018-07-30 03:12:34 +0000998
Nico Weber64041d72019-04-15 16:42:44 +0000999 if (NBB & NBB_Template) {
1000 // NBB_Template is only set for types and non-leaf names ("a::" in "a::b").
Nico Weberc035c242019-04-16 14:10:34 +00001001 // Structors and conversion operators only makes sense in a leaf name, so
1002 // reject them in NBB_Template contexts.
1003 if (Identifier->kind() == NodeKind::ConversionOperatorIdentifier ||
1004 Identifier->kind() == NodeKind::StructorIdentifier) {
Nico Weber64041d72019-04-15 16:42:44 +00001005 Error = true;
1006 return nullptr;
1007 }
1008
Zachary Turnerb2fef1a2018-08-29 04:12:44 +00001009 memorizeIdentifier(Identifier);
Nico Weber64041d72019-04-15 16:42:44 +00001010 }
Zachary Turner71c91f92018-07-30 03:12:34 +00001011
Zachary Turner03312862018-08-27 03:48:03 +00001012 return Identifier;
Zachary Turnera7dffb12018-07-28 22:10:42 +00001013}
1014
Zachary Turner03312862018-08-27 03:48:03 +00001015NamedIdentifierNode *Demangler::demangleSimpleName(StringView &MangledName,
1016 bool Memorize) {
Zachary Turner931e8792018-07-30 23:02:10 +00001017 StringView S = demangleSimpleString(MangledName, Memorize);
1018 if (Error)
1019 return nullptr;
1020
Zachary Turner03312862018-08-27 03:48:03 +00001021 NamedIdentifierNode *Name = Arena.alloc<NamedIdentifierNode>();
1022 Name->Name = S;
1023 return Name;
Zachary Turner931e8792018-07-30 23:02:10 +00001024}
1025
Zachary Turner970fdc32018-08-16 16:17:36 +00001026static bool isRebasedHexDigit(char C) { return (C >= 'A' && C <= 'P'); }
1027
1028static uint8_t rebasedHexDigitToNumber(char C) {
1029 assert(isRebasedHexDigit(C));
1030 return (C <= 'J') ? (C - 'A') : (10 + C - 'K');
1031}
1032
1033uint8_t Demangler::demangleCharLiteral(StringView &MangledName) {
Nico Webera0ac65c2019-04-18 19:52:32 +00001034 assert(!MangledName.empty());
Zachary Turner970fdc32018-08-16 16:17:36 +00001035 if (!MangledName.startsWith('?'))
1036 return MangledName.popFront();
1037
1038 MangledName = MangledName.dropFront();
1039 if (MangledName.empty())
1040 goto CharLiteralError;
1041
1042 if (MangledName.consumeFront('$')) {
1043 // Two hex digits
1044 if (MangledName.size() < 2)
1045 goto CharLiteralError;
1046 StringView Nibbles = MangledName.substr(0, 2);
1047 if (!isRebasedHexDigit(Nibbles[0]) || !isRebasedHexDigit(Nibbles[1]))
1048 goto CharLiteralError;
1049 // Don't append the null terminator.
1050 uint8_t C1 = rebasedHexDigitToNumber(Nibbles[0]);
1051 uint8_t C2 = rebasedHexDigitToNumber(Nibbles[1]);
1052 MangledName = MangledName.dropFront(2);
1053 return (C1 << 4) | C2;
1054 }
1055
1056 if (startsWithDigit(MangledName)) {
1057 const char *Lookup = ",/\\:. \n\t'-";
1058 char C = Lookup[MangledName[0] - '0'];
1059 MangledName = MangledName.dropFront();
1060 return C;
1061 }
1062
1063 if (MangledName[0] >= 'a' && MangledName[0] <= 'z') {
1064 char Lookup[26] = {'\xE1', '\xE2', '\xE3', '\xE4', '\xE5', '\xE6', '\xE7',
1065 '\xE8', '\xE9', '\xEA', '\xEB', '\xEC', '\xED', '\xEE',
1066 '\xEF', '\xF0', '\xF1', '\xF2', '\xF3', '\xF4', '\xF5',
1067 '\xF6', '\xF7', '\xF8', '\xF9', '\xFA'};
1068 char C = Lookup[MangledName[0] - 'a'];
1069 MangledName = MangledName.dropFront();
1070 return C;
1071 }
1072
1073 if (MangledName[0] >= 'A' && MangledName[0] <= 'Z') {
1074 char Lookup[26] = {'\xC1', '\xC2', '\xC3', '\xC4', '\xC5', '\xC6', '\xC7',
1075 '\xC8', '\xC9', '\xCA', '\xCB', '\xCC', '\xCD', '\xCE',
1076 '\xCF', '\xD0', '\xD1', '\xD2', '\xD3', '\xD4', '\xD5',
1077 '\xD6', '\xD7', '\xD8', '\xD9', '\xDA'};
1078 char C = Lookup[MangledName[0] - 'A'];
1079 MangledName = MangledName.dropFront();
1080 return C;
1081 }
1082
1083CharLiteralError:
1084 Error = true;
1085 return '\0';
1086}
1087
1088wchar_t Demangler::demangleWcharLiteral(StringView &MangledName) {
Zachary Turnerd78fe2f2018-08-16 16:30:27 +00001089 uint8_t C1, C2;
1090
1091 C1 = demangleCharLiteral(MangledName);
Nico Weberf5c7f3a2019-04-22 15:05:18 +00001092 if (Error || MangledName.empty())
Zachary Turner970fdc32018-08-16 16:17:36 +00001093 goto WCharLiteralError;
Zachary Turnerd78fe2f2018-08-16 16:30:27 +00001094 C2 = demangleCharLiteral(MangledName);
Zachary Turner970fdc32018-08-16 16:17:36 +00001095 if (Error)
1096 goto WCharLiteralError;
1097
1098 return ((wchar_t)C1 << 8) | (wchar_t)C2;
1099
1100WCharLiteralError:
1101 Error = true;
1102 return L'\0';
1103}
1104
1105static void writeHexDigit(char *Buffer, uint8_t Digit) {
1106 assert(Digit <= 15);
1107 *Buffer = (Digit < 10) ? ('0' + Digit) : ('A' + Digit - 10);
1108}
1109
1110static void outputHex(OutputStream &OS, unsigned C) {
Nico Weberb5cd6162019-06-02 17:41:07 +00001111 assert (C != 0);
1112
Zachary Turner970fdc32018-08-16 16:17:36 +00001113 // It's easier to do the math if we can work from right to left, but we need
1114 // to print the numbers from left to right. So render this into a temporary
1115 // buffer first, then output the temporary buffer. Each byte is of the form
1116 // \xAB, which means that each byte needs 4 characters. Since there are at
1117 // most 4 bytes, we need a 4*4+1 = 17 character temporary buffer.
1118 char TempBuffer[17];
1119
1120 ::memset(TempBuffer, 0, sizeof(TempBuffer));
Nico Weber8fc99022019-04-21 16:58:25 +00001121 constexpr int MaxPos = sizeof(TempBuffer) - 1;
Zachary Turner970fdc32018-08-16 16:17:36 +00001122
Nico Weber8fc99022019-04-21 16:58:25 +00001123 int Pos = MaxPos - 1; // TempBuffer[MaxPos] is the terminating \0.
Zachary Turner970fdc32018-08-16 16:17:36 +00001124 while (C != 0) {
1125 for (int I = 0; I < 2; ++I) {
1126 writeHexDigit(&TempBuffer[Pos--], C % 16);
1127 C /= 16;
1128 }
Zachary Turner970fdc32018-08-16 16:17:36 +00001129 }
Nico Weberce67a412019-04-21 17:19:27 +00001130 TempBuffer[Pos--] = 'x';
1131 assert(Pos >= 0);
1132 TempBuffer[Pos--] = '\\';
Zachary Turner970fdc32018-08-16 16:17:36 +00001133 OS << StringView(&TempBuffer[Pos + 1]);
1134}
1135
1136static void outputEscapedChar(OutputStream &OS, unsigned C) {
1137 switch (C) {
Nico Weber8eeaf512019-04-20 23:59:06 +00001138 case '\0': // nul
1139 OS << "\\0";
1140 return;
Zachary Turner970fdc32018-08-16 16:17:36 +00001141 case '\'': // single quote
1142 OS << "\\\'";
1143 return;
1144 case '\"': // double quote
1145 OS << "\\\"";
1146 return;
1147 case '\\': // backslash
1148 OS << "\\\\";
1149 return;
1150 case '\a': // bell
1151 OS << "\\a";
1152 return;
1153 case '\b': // backspace
1154 OS << "\\b";
1155 return;
1156 case '\f': // form feed
1157 OS << "\\f";
1158 return;
1159 case '\n': // new line
1160 OS << "\\n";
1161 return;
1162 case '\r': // carriage return
1163 OS << "\\r";
1164 return;
1165 case '\t': // tab
1166 OS << "\\t";
1167 return;
1168 case '\v': // vertical tab
1169 OS << "\\v";
1170 return;
1171 default:
1172 break;
1173 }
1174
1175 if (C > 0x1F && C < 0x7F) {
1176 // Standard ascii char.
1177 OS << (char)C;
1178 return;
1179 }
1180
1181 outputHex(OS, C);
1182}
1183
Benjamin Kramerc55e9972018-10-13 22:18:22 +00001184static unsigned countTrailingNullBytes(const uint8_t *StringBytes, int Length) {
Zachary Turner970fdc32018-08-16 16:17:36 +00001185 const uint8_t *End = StringBytes + Length - 1;
Zachary Turner3461bfa2018-08-17 16:14:05 +00001186 unsigned Count = 0;
Zachary Turner970fdc32018-08-16 16:17:36 +00001187 while (Length > 0 && *End == 0) {
1188 --Length;
1189 --End;
Zachary Turner3461bfa2018-08-17 16:14:05 +00001190 ++Count;
Zachary Turner970fdc32018-08-16 16:17:36 +00001191 }
Zachary Turner3461bfa2018-08-17 16:14:05 +00001192 return Count;
Zachary Turner970fdc32018-08-16 16:17:36 +00001193}
1194
Benjamin Kramerc55e9972018-10-13 22:18:22 +00001195static unsigned countEmbeddedNulls(const uint8_t *StringBytes,
1196 unsigned Length) {
Zachary Turner970fdc32018-08-16 16:17:36 +00001197 unsigned Result = 0;
1198 for (unsigned I = 0; I < Length; ++I) {
1199 if (*StringBytes++ == 0)
1200 ++Result;
1201 }
1202 return Result;
1203}
1204
Nico Weber8d05eb82019-04-24 16:09:38 +00001205// A mangled (non-wide) string literal stores the total length of the string it
1206// refers to (passed in NumBytes), and it contains up to 32 bytes of actual text
1207// (passed in StringBytes, NumChars).
Benjamin Kramerc55e9972018-10-13 22:18:22 +00001208static unsigned guessCharByteSize(const uint8_t *StringBytes, unsigned NumChars,
Nico Weber8d05eb82019-04-24 16:09:38 +00001209 uint64_t NumBytes) {
Zachary Turner970fdc32018-08-16 16:17:36 +00001210 assert(NumBytes > 0);
1211
1212 // If the number of bytes is odd, this is guaranteed to be a char string.
1213 if (NumBytes % 2 == 1)
1214 return 1;
1215
1216 // All strings can encode at most 32 bytes of data. If it's less than that,
1217 // then we encoded the entire string. In this case we check for a 1-byte,
1218 // 2-byte, or 4-byte null terminator.
1219 if (NumBytes < 32) {
1220 unsigned TrailingNulls = countTrailingNullBytes(StringBytes, NumChars);
Nico Weber8eeaf512019-04-20 23:59:06 +00001221 if (TrailingNulls >= 4 && NumBytes % 4 == 0)
Zachary Turner970fdc32018-08-16 16:17:36 +00001222 return 4;
1223 if (TrailingNulls >= 2)
1224 return 2;
1225 return 1;
1226 }
1227
1228 // The whole string was not able to be encoded. Try to look at embedded null
1229 // terminators to guess. The heuristic is that we count all embedded null
1230 // terminators. If more than 2/3 are null, it's a char32. If more than 1/3
1231 // are null, it's a char16. Otherwise it's a char8. This obviously isn't
1232 // perfect and is biased towards languages that have ascii alphabets, but this
1233 // was always going to be best effort since the encoding is lossy.
1234 unsigned Nulls = countEmbeddedNulls(StringBytes, NumChars);
Nico Weber8eeaf512019-04-20 23:59:06 +00001235 if (Nulls >= 2 * NumChars / 3 && NumBytes % 4 == 0)
Zachary Turner970fdc32018-08-16 16:17:36 +00001236 return 4;
1237 if (Nulls >= NumChars / 3)
1238 return 2;
1239 return 1;
1240}
1241
1242static unsigned decodeMultiByteChar(const uint8_t *StringBytes,
1243 unsigned CharIndex, unsigned CharBytes) {
1244 assert(CharBytes == 1 || CharBytes == 2 || CharBytes == 4);
1245 unsigned Offset = CharIndex * CharBytes;
1246 unsigned Result = 0;
1247 StringBytes = StringBytes + Offset;
1248 for (unsigned I = 0; I < CharBytes; ++I) {
1249 unsigned C = static_cast<unsigned>(StringBytes[I]);
1250 Result |= C << (8 * I);
1251 }
1252 return Result;
1253}
1254
Zachary Turner03312862018-08-27 03:48:03 +00001255FunctionSymbolNode *Demangler::demangleVcallThunkNode(StringView &MangledName) {
1256 FunctionSymbolNode *FSN = Arena.alloc<FunctionSymbolNode>();
1257 VcallThunkIdentifierNode *VTIN = Arena.alloc<VcallThunkIdentifierNode>();
1258 FSN->Signature = Arena.alloc<ThunkSignatureNode>();
1259 FSN->Signature->FunctionClass = FC_NoParameterList;
1260
1261 FSN->Name = demangleNameScopeChain(MangledName, VTIN);
1262 if (!Error)
1263 Error = !MangledName.consumeFront("$B");
1264 if (!Error)
1265 VTIN->OffsetInVTable = demangleUnsigned(MangledName);
1266 if (!Error)
1267 Error = !MangledName.consumeFront('A');
1268 if (!Error)
1269 FSN->Signature->CallConvention = demangleCallingConvention(MangledName);
1270 return (Error) ? nullptr : FSN;
1271}
1272
1273EncodedStringLiteralNode *
1274Demangler::demangleStringLiteral(StringView &MangledName) {
Zachary Turnerd78fe2f2018-08-16 16:30:27 +00001275 // This function uses goto, so declare all variables up front.
Zachary Turner970fdc32018-08-16 16:17:36 +00001276 OutputStream OS;
1277 StringView CRC;
Zachary Turnerd78fe2f2018-08-16 16:30:27 +00001278 uint64_t StringByteSize;
1279 bool IsWcharT = false;
1280 bool IsNegative = false;
1281 size_t CrcEndPos = 0;
1282 char *ResultBuffer = nullptr;
1283
Zachary Turner03312862018-08-27 03:48:03 +00001284 EncodedStringLiteralNode *Result = Arena.alloc<EncodedStringLiteralNode>();
Zachary Turner970fdc32018-08-16 16:17:36 +00001285
Nico Webere145a542019-04-19 14:13:11 +00001286 // Must happen before the first `goto StringLiteralError`.
1287 if (!initializeOutputStream(nullptr, nullptr, OS, 1024))
1288 // FIXME: Propagate out-of-memory as an error?
1289 std::terminate();
1290
Zachary Turner970fdc32018-08-16 16:17:36 +00001291 // Prefix indicating the beginning of a string literal
Zachary Turner3461bfa2018-08-17 16:14:05 +00001292 if (!MangledName.consumeFront("@_"))
1293 goto StringLiteralError;
Zachary Turner970fdc32018-08-16 16:17:36 +00001294 if (MangledName.empty())
1295 goto StringLiteralError;
1296
1297 // Char Type (regular or wchar_t)
Zachary Turner970fdc32018-08-16 16:17:36 +00001298 switch (MangledName.popFront()) {
1299 case '1':
1300 IsWcharT = true;
Erik Pilkington5094e5e2019-01-17 20:37:51 +00001301 DEMANGLE_FALLTHROUGH;
Zachary Turner970fdc32018-08-16 16:17:36 +00001302 case '0':
1303 break;
1304 default:
1305 goto StringLiteralError;
1306 }
1307
1308 // Encoded Length
Zachary Turner970fdc32018-08-16 16:17:36 +00001309 std::tie(StringByteSize, IsNegative) = demangleNumber(MangledName);
Nico Webera0ac65c2019-04-18 19:52:32 +00001310 if (Error || IsNegative || StringByteSize < (IsWcharT ? 2 : 1))
Zachary Turner970fdc32018-08-16 16:17:36 +00001311 goto StringLiteralError;
1312
1313 // CRC 32 (always 8 characters plus a terminator)
Zachary Turnerd78fe2f2018-08-16 16:30:27 +00001314 CrcEndPos = MangledName.find('@');
Zachary Turner970fdc32018-08-16 16:17:36 +00001315 if (CrcEndPos == StringView::npos)
1316 goto StringLiteralError;
1317 CRC = MangledName.substr(0, CrcEndPos);
1318 MangledName = MangledName.dropFront(CrcEndPos + 1);
1319 if (MangledName.empty())
1320 goto StringLiteralError;
1321
Zachary Turner970fdc32018-08-16 16:17:36 +00001322 if (IsWcharT) {
Zachary Turner03312862018-08-27 03:48:03 +00001323 Result->Char = CharKind::Wchar;
Zachary Turner970fdc32018-08-16 16:17:36 +00001324 if (StringByteSize > 64)
Zachary Turner3461bfa2018-08-17 16:14:05 +00001325 Result->IsTruncated = true;
Zachary Turner970fdc32018-08-16 16:17:36 +00001326
1327 while (!MangledName.consumeFront('@')) {
Nico Webera0ac65c2019-04-18 19:52:32 +00001328 if (MangledName.size() < 2)
Nico Weber502cf4b2019-04-18 19:30:21 +00001329 goto StringLiteralError;
Zachary Turner970fdc32018-08-16 16:17:36 +00001330 wchar_t W = demangleWcharLiteral(MangledName);
Zachary Turner3461bfa2018-08-17 16:14:05 +00001331 if (StringByteSize != 2 || Result->IsTruncated)
Zachary Turner970fdc32018-08-16 16:17:36 +00001332 outputEscapedChar(OS, W);
1333 StringByteSize -= 2;
1334 if (Error)
1335 goto StringLiteralError;
1336 }
1337 } else {
Zachary Turner32a8a202018-08-29 23:56:09 +00001338 // The max byte length is actually 32, but some compilers mangled strings
1339 // incorrectly, so we have to assume it can go higher.
1340 constexpr unsigned MaxStringByteLength = 32 * 4;
Zachary Turner970fdc32018-08-16 16:17:36 +00001341 uint8_t StringBytes[MaxStringByteLength];
1342
1343 unsigned BytesDecoded = 0;
1344 while (!MangledName.consumeFront('@')) {
Nico Weberaa162682019-04-21 14:25:07 +00001345 if (MangledName.size() < 1 || BytesDecoded >= MaxStringByteLength)
Nico Weber502cf4b2019-04-18 19:30:21 +00001346 goto StringLiteralError;
Zachary Turner970fdc32018-08-16 16:17:36 +00001347 StringBytes[BytesDecoded++] = demangleCharLiteral(MangledName);
1348 }
1349
Zachary Turner32a8a202018-08-29 23:56:09 +00001350 if (StringByteSize > BytesDecoded)
1351 Result->IsTruncated = true;
1352
Zachary Turner970fdc32018-08-16 16:17:36 +00001353 unsigned CharBytes =
1354 guessCharByteSize(StringBytes, BytesDecoded, StringByteSize);
1355 assert(StringByteSize % CharBytes == 0);
1356 switch (CharBytes) {
1357 case 1:
Zachary Turner03312862018-08-27 03:48:03 +00001358 Result->Char = CharKind::Char;
Zachary Turner970fdc32018-08-16 16:17:36 +00001359 break;
1360 case 2:
Zachary Turner03312862018-08-27 03:48:03 +00001361 Result->Char = CharKind::Char16;
Zachary Turner970fdc32018-08-16 16:17:36 +00001362 break;
1363 case 4:
Zachary Turner03312862018-08-27 03:48:03 +00001364 Result->Char = CharKind::Char32;
Zachary Turner970fdc32018-08-16 16:17:36 +00001365 break;
1366 default:
Erik Pilkington5094e5e2019-01-17 20:37:51 +00001367 DEMANGLE_UNREACHABLE;
Zachary Turner970fdc32018-08-16 16:17:36 +00001368 }
1369 const unsigned NumChars = BytesDecoded / CharBytes;
1370 for (unsigned CharIndex = 0; CharIndex < NumChars; ++CharIndex) {
1371 unsigned NextChar =
1372 decodeMultiByteChar(StringBytes, CharIndex, CharBytes);
Zachary Turner3461bfa2018-08-17 16:14:05 +00001373 if (CharIndex + 1 < NumChars || Result->IsTruncated)
Zachary Turner970fdc32018-08-16 16:17:36 +00001374 outputEscapedChar(OS, NextChar);
1375 }
1376 }
1377
1378 OS << '\0';
Zachary Turnerd78fe2f2018-08-16 16:30:27 +00001379 ResultBuffer = OS.getBuffer();
Zachary Turner03312862018-08-27 03:48:03 +00001380 Result->DecodedString = copyString(ResultBuffer);
Zachary Turneraf738f72018-08-16 17:48:32 +00001381 std::free(ResultBuffer);
Zachary Turner970fdc32018-08-16 16:17:36 +00001382 return Result;
1383
1384StringLiteralError:
1385 Error = true;
Nico Webere145a542019-04-19 14:13:11 +00001386 std::free(OS.getBuffer());
Zachary Turner970fdc32018-08-16 16:17:36 +00001387 return nullptr;
1388}
1389
Nico Weberc7444dd2019-04-03 23:15:56 +00001390// Returns MangledName's prefix before the first '@', or an error if
1391// MangledName contains no '@' or the prefix has length 0.
Zachary Turner931e8792018-07-30 23:02:10 +00001392StringView Demangler::demangleSimpleString(StringView &MangledName,
1393 bool Memorize) {
1394 StringView S;
Zachary Turnera7dffb12018-07-28 22:10:42 +00001395 for (size_t i = 0; i < MangledName.size(); ++i) {
1396 if (MangledName[i] != '@')
1397 continue;
Nico Weberc7444dd2019-04-03 23:15:56 +00001398 if (i == 0)
1399 break;
Zachary Turner931e8792018-07-30 23:02:10 +00001400 S = MangledName.substr(0, i);
Zachary Turnera7dffb12018-07-28 22:10:42 +00001401 MangledName = MangledName.dropFront(i + 1);
1402
1403 if (Memorize)
Zachary Turner931e8792018-07-30 23:02:10 +00001404 memorizeString(S);
1405 return S;
Zachary Turnera7dffb12018-07-28 22:10:42 +00001406 }
1407
1408 Error = true;
Zachary Turner931e8792018-07-30 23:02:10 +00001409 return {};
Zachary Turnera7dffb12018-07-28 22:10:42 +00001410}
1411
Zachary Turner03312862018-08-27 03:48:03 +00001412NamedIdentifierNode *
1413Demangler::demangleAnonymousNamespaceName(StringView &MangledName) {
Zachary Turnera7dffb12018-07-28 22:10:42 +00001414 assert(MangledName.startsWith("?A"));
1415 MangledName.consumeFront("?A");
1416
Zachary Turner03312862018-08-27 03:48:03 +00001417 NamedIdentifierNode *Node = Arena.alloc<NamedIdentifierNode>();
1418 Node->Name = "`anonymous namespace'";
Zachary Turner91c98a82018-08-20 23:58:35 +00001419 size_t EndPos = MangledName.find('@');
1420 if (EndPos == StringView::npos) {
1421 Error = true;
1422 return nullptr;
1423 }
Zachary Turner0002dd42018-08-20 23:58:58 +00001424 StringView NamespaceKey = MangledName.substr(0, EndPos);
1425 memorizeString(NamespaceKey);
Zachary Turner91c98a82018-08-20 23:58:35 +00001426 MangledName = MangledName.substr(EndPos + 1);
1427 return Node;
Zachary Turnera7dffb12018-07-28 22:10:42 +00001428}
1429
Zachary Turner03312862018-08-27 03:48:03 +00001430NamedIdentifierNode *
1431Demangler::demangleLocallyScopedNamePiece(StringView &MangledName) {
Zachary Turner71c91f92018-07-30 03:12:34 +00001432 assert(startsWithLocalScopePattern(MangledName));
1433
Zachary Turner03312862018-08-27 03:48:03 +00001434 NamedIdentifierNode *Identifier = Arena.alloc<NamedIdentifierNode>();
Zachary Turner71c91f92018-07-30 03:12:34 +00001435 MangledName.consumeFront('?');
Nico Weberc5615c22019-04-03 23:29:05 +00001436 uint64_t Number = 0;
1437 bool IsNegative = false;
1438 std::tie(Number, IsNegative) = demangleNumber(MangledName);
1439 assert(!IsNegative);
Zachary Turner71c91f92018-07-30 03:12:34 +00001440
1441 // One ? to terminate the number
1442 MangledName.consumeFront('?');
1443
1444 assert(!Error);
Zachary Turner03312862018-08-27 03:48:03 +00001445 Node *Scope = parse(MangledName);
Zachary Turner71c91f92018-07-30 03:12:34 +00001446 if (Error)
1447 return nullptr;
1448
1449 // Render the parent symbol's name into a buffer.
Nico Weber1359d652018-09-15 18:24:20 +00001450 OutputStream OS;
Nico Weber6808bc02018-11-11 10:04:00 +00001451 if (!initializeOutputStream(nullptr, nullptr, OS, 1024))
Nico Weber1359d652018-09-15 18:24:20 +00001452 // FIXME: Propagate out-of-memory as an error?
1453 std::terminate();
Zachary Turner71c91f92018-07-30 03:12:34 +00001454 OS << '`';
Zachary Turner38d2edd2018-08-29 03:59:17 +00001455 Scope->output(OS, OF_Default);
Zachary Turner71c91f92018-07-30 03:12:34 +00001456 OS << '\'';
Nico Weberc5615c22019-04-03 23:29:05 +00001457 OS << "::`" << Number << "'";
Zachary Turner71c91f92018-07-30 03:12:34 +00001458 OS << '\0';
1459 char *Result = OS.getBuffer();
Zachary Turner03312862018-08-27 03:48:03 +00001460 Identifier->Name = copyString(Result);
Zachary Turner71c91f92018-07-30 03:12:34 +00001461 std::free(Result);
Zachary Turner03312862018-08-27 03:48:03 +00001462 return Identifier;
Zachary Turner71c91f92018-07-30 03:12:34 +00001463}
1464
Zachary Turnera7dffb12018-07-28 22:10:42 +00001465// Parses a type name in the form of A@B@C@@ which represents C::B::A.
Zachary Turner03312862018-08-27 03:48:03 +00001466QualifiedNameNode *
1467Demangler::demangleFullyQualifiedTypeName(StringView &MangledName) {
Nico Weberaf2ee7d2019-04-11 23:20:18 +00001468 IdentifierNode *Identifier =
1469 demangleUnqualifiedTypeName(MangledName, /*Memorize=*/true);
Zachary Turner54d4ffe2018-08-01 18:32:28 +00001470 if (Error)
1471 return nullptr;
Zachary Turner03312862018-08-27 03:48:03 +00001472 assert(Identifier);
Zachary Turnera7dffb12018-07-28 22:10:42 +00001473
Zachary Turner03312862018-08-27 03:48:03 +00001474 QualifiedNameNode *QN = demangleNameScopeChain(MangledName, Identifier);
Zachary Turner54d4ffe2018-08-01 18:32:28 +00001475 if (Error)
1476 return nullptr;
Zachary Turner03312862018-08-27 03:48:03 +00001477 assert(QN);
1478 return QN;
Zachary Turnera7dffb12018-07-28 22:10:42 +00001479}
1480
1481// Parses a symbol name in the form of A@B@C@@ which represents C::B::A.
1482// Symbol names have slightly different rules regarding what can appear
1483// so we separate out the implementations for flexibility.
Zachary Turner03312862018-08-27 03:48:03 +00001484QualifiedNameNode *
1485Demangler::demangleFullyQualifiedSymbolName(StringView &MangledName) {
Zachary Turner58d29cf2018-08-08 00:43:31 +00001486 // This is the final component of a symbol name (i.e. the leftmost component
1487 // of a mangled name. Since the only possible template instantiation that
1488 // can appear in this context is a function template, and since those are
1489 // not saved for the purposes of name backreferences, only backref simple
1490 // names.
Zachary Turner03312862018-08-27 03:48:03 +00001491 IdentifierNode *Identifier =
1492 demangleUnqualifiedSymbolName(MangledName, NBB_Simple);
Zachary Turner54d4ffe2018-08-01 18:32:28 +00001493 if (Error)
1494 return nullptr;
Zachary Turner3461bfa2018-08-17 16:14:05 +00001495
Zachary Turner03312862018-08-27 03:48:03 +00001496 QualifiedNameNode *QN = demangleNameScopeChain(MangledName, Identifier);
Zachary Turner54d4ffe2018-08-01 18:32:28 +00001497 if (Error)
1498 return nullptr;
Zachary Turner03312862018-08-27 03:48:03 +00001499
1500 if (Identifier->kind() == NodeKind::StructorIdentifier) {
Nico Webera9886f82019-04-03 23:23:32 +00001501 if (QN->Components->Count < 2) {
1502 Error = true;
1503 return nullptr;
1504 }
Zachary Turner03312862018-08-27 03:48:03 +00001505 StructorIdentifierNode *SIN =
1506 static_cast<StructorIdentifierNode *>(Identifier);
Zachary Turner03312862018-08-27 03:48:03 +00001507 Node *ClassNode = QN->Components->Nodes[QN->Components->Count - 2];
1508 SIN->Class = static_cast<IdentifierNode *>(ClassNode);
1509 }
1510 assert(QN);
1511 return QN;
Zachary Turnera7dffb12018-07-28 22:10:42 +00001512}
1513
Zachary Turner03312862018-08-27 03:48:03 +00001514IdentifierNode *Demangler::demangleUnqualifiedTypeName(StringView &MangledName,
1515 bool Memorize) {
Zachary Turnera7dffb12018-07-28 22:10:42 +00001516 // An inner-most name can be a back-reference, because a fully-qualified name
1517 // (e.g. Scope + Inner) can contain other fully qualified names inside of
1518 // them (for example template parameters), and these nested parameters can
1519 // refer to previously mangled types.
1520 if (startsWithDigit(MangledName))
Zachary Turner316109b2018-07-29 16:38:02 +00001521 return demangleBackRefName(MangledName);
Zachary Turnera7dffb12018-07-28 22:10:42 +00001522
1523 if (MangledName.startsWith("?$"))
Zachary Turner58d29cf2018-08-08 00:43:31 +00001524 return demangleTemplateInstantiationName(MangledName, NBB_Template);
Zachary Turnera7dffb12018-07-28 22:10:42 +00001525
Zachary Turner44ebbc22018-08-01 18:32:47 +00001526 return demangleSimpleName(MangledName, Memorize);
Zachary Turnera7dffb12018-07-28 22:10:42 +00001527}
1528
Zachary Turner03312862018-08-27 03:48:03 +00001529IdentifierNode *
1530Demangler::demangleUnqualifiedSymbolName(StringView &MangledName,
1531 NameBackrefBehavior NBB) {
Zachary Turner71c91f92018-07-30 03:12:34 +00001532 if (startsWithDigit(MangledName))
1533 return demangleBackRefName(MangledName);
1534 if (MangledName.startsWith("?$"))
Zachary Turner58d29cf2018-08-08 00:43:31 +00001535 return demangleTemplateInstantiationName(MangledName, NBB);
Zachary Turnera7dffb12018-07-28 22:10:42 +00001536 if (MangledName.startsWith('?'))
Zachary Turner03312862018-08-27 03:48:03 +00001537 return demangleFunctionIdentifierCode(MangledName);
Nico Weberaf2ee7d2019-04-11 23:20:18 +00001538 return demangleSimpleName(MangledName, /*Memorize=*/(NBB & NBB_Simple) != 0);
Zachary Turnera7dffb12018-07-28 22:10:42 +00001539}
1540
Zachary Turner03312862018-08-27 03:48:03 +00001541IdentifierNode *Demangler::demangleNameScopePiece(StringView &MangledName) {
Zachary Turnera7dffb12018-07-28 22:10:42 +00001542 if (startsWithDigit(MangledName))
Zachary Turner316109b2018-07-29 16:38:02 +00001543 return demangleBackRefName(MangledName);
Zachary Turnera7dffb12018-07-28 22:10:42 +00001544
1545 if (MangledName.startsWith("?$"))
Zachary Turner58d29cf2018-08-08 00:43:31 +00001546 return demangleTemplateInstantiationName(MangledName, NBB_Template);
Zachary Turnera7dffb12018-07-28 22:10:42 +00001547
1548 if (MangledName.startsWith("?A"))
Zachary Turner316109b2018-07-29 16:38:02 +00001549 return demangleAnonymousNamespaceName(MangledName);
Zachary Turnera7dffb12018-07-28 22:10:42 +00001550
Zachary Turner71c91f92018-07-30 03:12:34 +00001551 if (startsWithLocalScopePattern(MangledName))
1552 return demangleLocallyScopedNamePiece(MangledName);
1553
Nico Weberaf2ee7d2019-04-11 23:20:18 +00001554 return demangleSimpleName(MangledName, /*Memorize=*/true);
Zachary Turnera7dffb12018-07-28 22:10:42 +00001555}
1556
Zachary Turner03312862018-08-27 03:48:03 +00001557static NodeArrayNode *nodeListToNodeArray(ArenaAllocator &Arena, NodeList *Head,
1558 size_t Count) {
1559 NodeArrayNode *N = Arena.alloc<NodeArrayNode>();
1560 N->Count = Count;
1561 N->Nodes = Arena.allocArray<Node *>(Count);
1562 for (size_t I = 0; I < Count; ++I) {
1563 N->Nodes[I] = Head->N;
1564 Head = Head->Next;
1565 }
1566 return N;
1567}
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001568
Zachary Turner03312862018-08-27 03:48:03 +00001569QualifiedNameNode *
1570Demangler::demangleNameScopeChain(StringView &MangledName,
1571 IdentifierNode *UnqualifiedName) {
1572 NodeList *Head = Arena.alloc<NodeList>();
1573
1574 Head->N = UnqualifiedName;
1575
1576 size_t Count = 1;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001577 while (!MangledName.consumeFront("@")) {
Zachary Turner03312862018-08-27 03:48:03 +00001578 ++Count;
1579 NodeList *NewHead = Arena.alloc<NodeList>();
1580 NewHead->Next = Head;
1581 Head = NewHead;
1582
Zachary Turnera7dffb12018-07-28 22:10:42 +00001583 if (MangledName.empty()) {
1584 Error = true;
1585 return nullptr;
1586 }
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001587
1588 assert(!Error);
Zachary Turner03312862018-08-27 03:48:03 +00001589 IdentifierNode *Elem = demangleNameScopePiece(MangledName);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001590 if (Error)
1591 return nullptr;
1592
Zachary Turner03312862018-08-27 03:48:03 +00001593 Head->N = Elem;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001594 }
Zachary Turner03312862018-08-27 03:48:03 +00001595
1596 QualifiedNameNode *QN = Arena.alloc<QualifiedNameNode>();
1597 QN->Components = nodeListToNodeArray(Arena, Head, Count);
1598 return QN;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001599}
1600
Zachary Turner316109b2018-07-29 16:38:02 +00001601FuncClass Demangler::demangleFunctionClass(StringView &MangledName) {
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001602 switch (MangledName.popFront()) {
Zachary Turner29ec67b2018-08-10 21:09:05 +00001603 case '9':
Zachary Turner03312862018-08-27 03:48:03 +00001604 return FuncClass(FC_ExternC | FC_NoParameterList);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001605 case 'A':
Zachary Turner03312862018-08-27 03:48:03 +00001606 return FC_Private;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001607 case 'B':
Zachary Turner03312862018-08-27 03:48:03 +00001608 return FuncClass(FC_Private | FC_Far);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001609 case 'C':
Zachary Turner03312862018-08-27 03:48:03 +00001610 return FuncClass(FC_Private | FC_Static);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001611 case 'D':
Nico Weber54362472019-06-02 23:26:57 +00001612 return FuncClass(FC_Private | FC_Static | FC_Far);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001613 case 'E':
Zachary Turner03312862018-08-27 03:48:03 +00001614 return FuncClass(FC_Private | FC_Virtual);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001615 case 'F':
Nico Weber54362472019-06-02 23:26:57 +00001616 return FuncClass(FC_Private | FC_Virtual | FC_Far);
Zachary Turner32a8a202018-08-29 23:56:09 +00001617 case 'G':
1618 return FuncClass(FC_Private | FC_StaticThisAdjust);
1619 case 'H':
1620 return FuncClass(FC_Private | FC_StaticThisAdjust | FC_Far);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001621 case 'I':
Zachary Turner03312862018-08-27 03:48:03 +00001622 return FuncClass(FC_Protected);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001623 case 'J':
Zachary Turner03312862018-08-27 03:48:03 +00001624 return FuncClass(FC_Protected | FC_Far);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001625 case 'K':
Zachary Turner03312862018-08-27 03:48:03 +00001626 return FuncClass(FC_Protected | FC_Static);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001627 case 'L':
Zachary Turner03312862018-08-27 03:48:03 +00001628 return FuncClass(FC_Protected | FC_Static | FC_Far);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001629 case 'M':
Zachary Turner03312862018-08-27 03:48:03 +00001630 return FuncClass(FC_Protected | FC_Virtual);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001631 case 'N':
Zachary Turner03312862018-08-27 03:48:03 +00001632 return FuncClass(FC_Protected | FC_Virtual | FC_Far);
Zachary Turner469f0762018-08-17 21:18:05 +00001633 case 'O':
Zachary Turner03312862018-08-27 03:48:03 +00001634 return FuncClass(FC_Protected | FC_Virtual | FC_StaticThisAdjust);
Zachary Turner469f0762018-08-17 21:18:05 +00001635 case 'P':
Zachary Turner03312862018-08-27 03:48:03 +00001636 return FuncClass(FC_Protected | FC_Virtual | FC_StaticThisAdjust | FC_Far);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001637 case 'Q':
Zachary Turner03312862018-08-27 03:48:03 +00001638 return FuncClass(FC_Public);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001639 case 'R':
Zachary Turner03312862018-08-27 03:48:03 +00001640 return FuncClass(FC_Public | FC_Far);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001641 case 'S':
Zachary Turner03312862018-08-27 03:48:03 +00001642 return FuncClass(FC_Public | FC_Static);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001643 case 'T':
Zachary Turner03312862018-08-27 03:48:03 +00001644 return FuncClass(FC_Public | FC_Static | FC_Far);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001645 case 'U':
Zachary Turner03312862018-08-27 03:48:03 +00001646 return FuncClass(FC_Public | FC_Virtual);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001647 case 'V':
Zachary Turner03312862018-08-27 03:48:03 +00001648 return FuncClass(FC_Public | FC_Virtual | FC_Far);
Zachary Turner469f0762018-08-17 21:18:05 +00001649 case 'W':
Zachary Turner03312862018-08-27 03:48:03 +00001650 return FuncClass(FC_Public | FC_Virtual | FC_StaticThisAdjust);
Zachary Turner469f0762018-08-17 21:18:05 +00001651 case 'X':
Zachary Turner03312862018-08-27 03:48:03 +00001652 return FuncClass(FC_Public | FC_Virtual | FC_StaticThisAdjust | FC_Far);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001653 case 'Y':
Zachary Turner03312862018-08-27 03:48:03 +00001654 return FuncClass(FC_Global);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001655 case 'Z':
Zachary Turner03312862018-08-27 03:48:03 +00001656 return FuncClass(FC_Global | FC_Far);
Zachary Turner469f0762018-08-17 21:18:05 +00001657 case '$': {
Zachary Turner03312862018-08-27 03:48:03 +00001658 FuncClass VFlag = FC_VirtualThisAdjust;
Zachary Turner469f0762018-08-17 21:18:05 +00001659 if (MangledName.consumeFront('R'))
Zachary Turner03312862018-08-27 03:48:03 +00001660 VFlag = FuncClass(VFlag | FC_VirtualThisAdjustEx);
Nico Weberb4f33bb2019-04-11 22:47:18 +00001661 if (MangledName.empty())
1662 break;
Zachary Turner469f0762018-08-17 21:18:05 +00001663 switch (MangledName.popFront()) {
1664 case '0':
Zachary Turner03312862018-08-27 03:48:03 +00001665 return FuncClass(FC_Private | FC_Virtual | VFlag);
Zachary Turner469f0762018-08-17 21:18:05 +00001666 case '1':
Zachary Turner03312862018-08-27 03:48:03 +00001667 return FuncClass(FC_Private | FC_Virtual | VFlag | FC_Far);
Zachary Turner469f0762018-08-17 21:18:05 +00001668 case '2':
Zachary Turner03312862018-08-27 03:48:03 +00001669 return FuncClass(FC_Protected | FC_Virtual | VFlag);
Zachary Turner469f0762018-08-17 21:18:05 +00001670 case '3':
Zachary Turner03312862018-08-27 03:48:03 +00001671 return FuncClass(FC_Protected | FC_Virtual | VFlag | FC_Far);
Zachary Turner469f0762018-08-17 21:18:05 +00001672 case '4':
Zachary Turner03312862018-08-27 03:48:03 +00001673 return FuncClass(FC_Public | FC_Virtual | VFlag);
Zachary Turner469f0762018-08-17 21:18:05 +00001674 case '5':
Zachary Turner03312862018-08-27 03:48:03 +00001675 return FuncClass(FC_Public | FC_Virtual | VFlag | FC_Far);
Zachary Turner469f0762018-08-17 21:18:05 +00001676 }
1677 }
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001678 }
1679
1680 Error = true;
Zachary Turner03312862018-08-27 03:48:03 +00001681 return FC_Public;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001682}
1683
Zachary Turner316109b2018-07-29 16:38:02 +00001684CallingConv Demangler::demangleCallingConvention(StringView &MangledName) {
Nico Weber63b97d22019-04-08 19:46:53 +00001685 if (MangledName.empty()) {
1686 Error = true;
1687 return CallingConv::None;
1688 }
1689
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001690 switch (MangledName.popFront()) {
1691 case 'A':
1692 case 'B':
1693 return CallingConv::Cdecl;
1694 case 'C':
1695 case 'D':
1696 return CallingConv::Pascal;
1697 case 'E':
1698 case 'F':
1699 return CallingConv::Thiscall;
1700 case 'G':
1701 case 'H':
1702 return CallingConv::Stdcall;
1703 case 'I':
1704 case 'J':
1705 return CallingConv::Fastcall;
1706 case 'M':
1707 case 'N':
1708 return CallingConv::Clrcall;
1709 case 'O':
1710 case 'P':
1711 return CallingConv::Eabi;
1712 case 'Q':
1713 return CallingConv::Vectorcall;
1714 }
1715
1716 return CallingConv::None;
Martin Storsjo0f2abd82018-07-20 18:43:42 +00001717}
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001718
Zachary Turner316109b2018-07-29 16:38:02 +00001719StorageClass Demangler::demangleVariableStorageClass(StringView &MangledName) {
Nico Weberc1a0e6f2019-06-04 15:38:00 +00001720 assert(MangledName.front() >= '0' && MangledName.front() <= '4');
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001721
1722 switch (MangledName.popFront()) {
1723 case '0':
1724 return StorageClass::PrivateStatic;
1725 case '1':
1726 return StorageClass::ProtectedStatic;
1727 case '2':
1728 return StorageClass::PublicStatic;
1729 case '3':
1730 return StorageClass::Global;
1731 case '4':
1732 return StorageClass::FunctionLocalStatic;
1733 }
Nico Weberc1a0e6f2019-06-04 15:38:00 +00001734 DEMANGLE_UNREACHABLE;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001735}
1736
Zachary Turner316109b2018-07-29 16:38:02 +00001737std::pair<Qualifiers, bool>
1738Demangler::demangleQualifiers(StringView &MangledName) {
Nico Weber63b97d22019-04-08 19:46:53 +00001739 if (MangledName.empty()) {
1740 Error = true;
1741 return std::make_pair(Q_None, false);
1742 }
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001743
1744 switch (MangledName.popFront()) {
Zachary Turnerd742d642018-07-26 19:56:09 +00001745 // Member qualifiers
1746 case 'Q':
1747 return std::make_pair(Q_None, true);
1748 case 'R':
1749 return std::make_pair(Q_Const, true);
1750 case 'S':
1751 return std::make_pair(Q_Volatile, true);
1752 case 'T':
1753 return std::make_pair(Qualifiers(Q_Const | Q_Volatile), true);
1754 // Non-Member qualifiers
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001755 case 'A':
Zachary Turnerd742d642018-07-26 19:56:09 +00001756 return std::make_pair(Q_None, false);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001757 case 'B':
Zachary Turnerd742d642018-07-26 19:56:09 +00001758 return std::make_pair(Q_Const, false);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001759 case 'C':
Zachary Turnerd742d642018-07-26 19:56:09 +00001760 return std::make_pair(Q_Volatile, false);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001761 case 'D':
Zachary Turnerd742d642018-07-26 19:56:09 +00001762 return std::make_pair(Qualifiers(Q_Const | Q_Volatile), false);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001763 }
1764 Error = true;
Zachary Turnerd742d642018-07-26 19:56:09 +00001765 return std::make_pair(Q_None, false);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001766}
1767
1768// <variable-type> ::= <type> <cvr-qualifiers>
1769// ::= <type> <pointee-cvr-qualifiers> # pointers, references
Zachary Turner03312862018-08-27 03:48:03 +00001770TypeNode *Demangler::demangleType(StringView &MangledName,
1771 QualifierMangleMode QMM) {
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001772 Qualifiers Quals = Q_None;
Zachary Turnerd742d642018-07-26 19:56:09 +00001773 bool IsMember = false;
Zachary Turnerd742d642018-07-26 19:56:09 +00001774 if (QMM == QualifierMangleMode::Mangle) {
Zachary Turner316109b2018-07-29 16:38:02 +00001775 std::tie(Quals, IsMember) = demangleQualifiers(MangledName);
Zachary Turnerd742d642018-07-26 19:56:09 +00001776 } else if (QMM == QualifierMangleMode::Result) {
Zachary Turnerdf4cd7c2018-08-21 21:23:49 +00001777 if (MangledName.consumeFront('?'))
Zachary Turner316109b2018-07-29 16:38:02 +00001778 std::tie(Quals, IsMember) = demangleQualifiers(MangledName);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001779 }
1780
Nico Weber63b97d22019-04-08 19:46:53 +00001781 if (MangledName.empty()) {
1782 Error = true;
1783 return nullptr;
1784 }
1785
Zachary Turner03312862018-08-27 03:48:03 +00001786 TypeNode *Ty = nullptr;
Zachary Turner931e8792018-07-30 23:02:10 +00001787 if (isTagType(MangledName))
Zachary Turner316109b2018-07-29 16:38:02 +00001788 Ty = demangleClassType(MangledName);
Zachary Turner931e8792018-07-30 23:02:10 +00001789 else if (isPointerType(MangledName)) {
Zachary Turner8fb9a712018-12-14 18:10:13 +00001790 if (isMemberPointer(MangledName, Error))
Zachary Turner316109b2018-07-29 16:38:02 +00001791 Ty = demangleMemberPointerType(MangledName);
Zachary Turner8fb9a712018-12-14 18:10:13 +00001792 else if (!Error)
Zachary Turner316109b2018-07-29 16:38:02 +00001793 Ty = demanglePointerType(MangledName);
Zachary Turner8fb9a712018-12-14 18:10:13 +00001794 else
1795 return nullptr;
Zachary Turner931e8792018-07-30 23:02:10 +00001796 } else if (isArrayType(MangledName))
Zachary Turner316109b2018-07-29 16:38:02 +00001797 Ty = demangleArrayType(MangledName);
Zachary Turner931e8792018-07-30 23:02:10 +00001798 else if (isFunctionType(MangledName)) {
1799 if (MangledName.consumeFront("$$A8@@"))
Zachary Turner03312862018-08-27 03:48:03 +00001800 Ty = demangleFunctionType(MangledName, true);
Zachary Turner931e8792018-07-30 23:02:10 +00001801 else {
1802 assert(MangledName.startsWith("$$A6"));
1803 MangledName.consumeFront("$$A6");
Zachary Turner03312862018-08-27 03:48:03 +00001804 Ty = demangleFunctionType(MangledName, false);
Zachary Turner931e8792018-07-30 23:02:10 +00001805 }
Zachary Turnerb2fef1a2018-08-29 04:12:44 +00001806 } else if (isCustomType(MangledName)) {
1807 Ty = demangleCustomType(MangledName);
Zachary Turner931e8792018-07-30 23:02:10 +00001808 } else {
Zachary Turner03312862018-08-27 03:48:03 +00001809 Ty = demanglePrimitiveType(MangledName);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001810 }
Zachary Turner931e8792018-07-30 23:02:10 +00001811
Zachary Turner2cd32862018-12-14 17:43:56 +00001812 if (!Ty || Error)
1813 return Ty;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001814 Ty->Quals = Qualifiers(Ty->Quals | Quals);
1815 return Ty;
1816}
1817
Zachary Turner2fe49002019-01-08 21:05:51 +00001818bool Demangler::demangleThrowSpecification(StringView &MangledName) {
1819 if (MangledName.consumeFront("_E"))
1820 return true;
Zachary Turner38b78a72018-07-26 20:20:10 +00001821 if (MangledName.consumeFront('Z'))
Zachary Turner2fe49002019-01-08 21:05:51 +00001822 return false;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001823
Zachary Turner38b78a72018-07-26 20:20:10 +00001824 Error = true;
Zachary Turner2fe49002019-01-08 21:05:51 +00001825 return false;
Zachary Turner38b78a72018-07-26 20:20:10 +00001826}
1827
Zachary Turner03312862018-08-27 03:48:03 +00001828FunctionSignatureNode *Demangler::demangleFunctionType(StringView &MangledName,
1829 bool HasThisQuals) {
1830 FunctionSignatureNode *FTy = Arena.alloc<FunctionSignatureNode>();
Zachary Turner38b78a72018-07-26 20:20:10 +00001831
1832 if (HasThisQuals) {
Zachary Turner316109b2018-07-29 16:38:02 +00001833 FTy->Quals = demanglePointerExtQualifiers(MangledName);
Zachary Turner03312862018-08-27 03:48:03 +00001834 FTy->RefQualifier = demangleFunctionRefQualifier(MangledName);
Zachary Turner316109b2018-07-29 16:38:02 +00001835 FTy->Quals = Qualifiers(FTy->Quals | demangleQualifiers(MangledName).first);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001836 }
1837
1838 // Fields that appear on both member and non-member functions.
Zachary Turner316109b2018-07-29 16:38:02 +00001839 FTy->CallConvention = demangleCallingConvention(MangledName);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001840
1841 // <return-type> ::= <type>
1842 // ::= @ # structors (they have no declared return type)
1843 bool IsStructor = MangledName.consumeFront('@');
1844 if (!IsStructor)
Zachary Turner316109b2018-07-29 16:38:02 +00001845 FTy->ReturnType = demangleType(MangledName, QualifierMangleMode::Result);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001846
Nico Weber1dce8262019-06-04 19:10:08 +00001847 FTy->Params = demangleFunctionParameterList(MangledName, FTy->IsVariadic);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001848
Zachary Turner2fe49002019-01-08 21:05:51 +00001849 FTy->IsNoexcept = demangleThrowSpecification(MangledName);
Zachary Turner38b78a72018-07-26 20:20:10 +00001850
1851 return FTy;
1852}
1853
Zachary Turner03312862018-08-27 03:48:03 +00001854FunctionSymbolNode *
1855Demangler::demangleFunctionEncoding(StringView &MangledName) {
1856 FuncClass ExtraFlags = FC_None;
Zachary Turner469f0762018-08-17 21:18:05 +00001857 if (MangledName.consumeFront("$$J0"))
Zachary Turner03312862018-08-27 03:48:03 +00001858 ExtraFlags = FC_ExternC;
Zachary Turner469f0762018-08-17 21:18:05 +00001859
Nico Weber63b97d22019-04-08 19:46:53 +00001860 if (MangledName.empty()) {
1861 Error = true;
1862 return nullptr;
1863 }
1864
Zachary Turner316109b2018-07-29 16:38:02 +00001865 FuncClass FC = demangleFunctionClass(MangledName);
Zachary Turner469f0762018-08-17 21:18:05 +00001866 FC = FuncClass(ExtraFlags | FC);
1867
Zachary Turner03312862018-08-27 03:48:03 +00001868 FunctionSignatureNode *FSN = nullptr;
1869 ThunkSignatureNode *TTN = nullptr;
1870 if (FC & FC_StaticThisAdjust) {
1871 TTN = Arena.alloc<ThunkSignatureNode>();
1872 TTN->ThisAdjust.StaticOffset = demangleSigned(MangledName);
1873 } else if (FC & FC_VirtualThisAdjust) {
1874 TTN = Arena.alloc<ThunkSignatureNode>();
1875 if (FC & FC_VirtualThisAdjustEx) {
1876 TTN->ThisAdjust.VBPtrOffset = demangleSigned(MangledName);
1877 TTN->ThisAdjust.VBOffsetOffset = demangleSigned(MangledName);
Zachary Turner469f0762018-08-17 21:18:05 +00001878 }
Zachary Turner03312862018-08-27 03:48:03 +00001879 TTN->ThisAdjust.VtordispOffset = demangleSigned(MangledName);
1880 TTN->ThisAdjust.StaticOffset = demangleSigned(MangledName);
Zachary Turner469f0762018-08-17 21:18:05 +00001881 }
1882
Zachary Turner03312862018-08-27 03:48:03 +00001883 if (FC & FC_NoParameterList) {
Zachary Turner29ec67b2018-08-10 21:09:05 +00001884 // This is an extern "C" function whose full signature hasn't been mangled.
1885 // This happens when we need to mangle a local symbol inside of an extern
1886 // "C" function.
Zachary Turner03312862018-08-27 03:48:03 +00001887 FSN = Arena.alloc<FunctionSignatureNode>();
Zachary Turner29ec67b2018-08-10 21:09:05 +00001888 } else {
Zachary Turner03312862018-08-27 03:48:03 +00001889 bool HasThisQuals = !(FC & (FC_Global | FC_Static));
1890 FSN = demangleFunctionType(MangledName, HasThisQuals);
Zachary Turner29ec67b2018-08-10 21:09:05 +00001891 }
Nico Weber63b97d22019-04-08 19:46:53 +00001892
1893 if (Error)
1894 return nullptr;
1895
Zachary Turner03312862018-08-27 03:48:03 +00001896 if (TTN) {
1897 *static_cast<FunctionSignatureNode *>(TTN) = *FSN;
1898 FSN = TTN;
1899 }
1900 FSN->FunctionClass = FC;
Zachary Turner38b78a72018-07-26 20:20:10 +00001901
Zachary Turner03312862018-08-27 03:48:03 +00001902 FunctionSymbolNode *Symbol = Arena.alloc<FunctionSymbolNode>();
1903 Symbol->Signature = FSN;
1904 return Symbol;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001905}
1906
Zachary Turnerb2fef1a2018-08-29 04:12:44 +00001907CustomTypeNode *Demangler::demangleCustomType(StringView &MangledName) {
1908 assert(MangledName.startsWith('?'));
1909 MangledName.popFront();
1910
1911 CustomTypeNode *CTN = Arena.alloc<CustomTypeNode>();
Nico Weberaf2ee7d2019-04-11 23:20:18 +00001912 CTN->Identifier = demangleUnqualifiedTypeName(MangledName, /*Memorize=*/true);
Zachary Turnerb2fef1a2018-08-29 04:12:44 +00001913 if (!MangledName.consumeFront('@'))
1914 Error = true;
1915 if (Error)
1916 return nullptr;
1917 return CTN;
1918}
1919
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001920// Reads a primitive type.
Zachary Turner03312862018-08-27 03:48:03 +00001921PrimitiveTypeNode *Demangler::demanglePrimitiveType(StringView &MangledName) {
1922 if (MangledName.consumeFront("$$T"))
1923 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Nullptr);
Zachary Turner931e8792018-07-30 23:02:10 +00001924
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001925 switch (MangledName.popFront()) {
1926 case 'X':
Zachary Turner03312862018-08-27 03:48:03 +00001927 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Void);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001928 case 'D':
Zachary Turner03312862018-08-27 03:48:03 +00001929 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Char);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001930 case 'C':
Zachary Turner03312862018-08-27 03:48:03 +00001931 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Schar);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001932 case 'E':
Zachary Turner03312862018-08-27 03:48:03 +00001933 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Uchar);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001934 case 'F':
Zachary Turner03312862018-08-27 03:48:03 +00001935 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Short);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001936 case 'G':
Zachary Turner03312862018-08-27 03:48:03 +00001937 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Ushort);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001938 case 'H':
Zachary Turner03312862018-08-27 03:48:03 +00001939 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Int);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001940 case 'I':
Zachary Turner03312862018-08-27 03:48:03 +00001941 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Uint);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001942 case 'J':
Zachary Turner03312862018-08-27 03:48:03 +00001943 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Long);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001944 case 'K':
Zachary Turner03312862018-08-27 03:48:03 +00001945 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Ulong);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001946 case 'M':
Zachary Turner03312862018-08-27 03:48:03 +00001947 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Float);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001948 case 'N':
Zachary Turner03312862018-08-27 03:48:03 +00001949 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Double);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001950 case 'O':
Zachary Turner03312862018-08-27 03:48:03 +00001951 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Ldouble);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001952 case '_': {
Zachary Turner91ecedd2018-07-20 18:07:33 +00001953 if (MangledName.empty()) {
1954 Error = true;
1955 return nullptr;
1956 }
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001957 switch (MangledName.popFront()) {
1958 case 'N':
Zachary Turner03312862018-08-27 03:48:03 +00001959 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Bool);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001960 case 'J':
Zachary Turner03312862018-08-27 03:48:03 +00001961 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Int64);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001962 case 'K':
Zachary Turner03312862018-08-27 03:48:03 +00001963 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Uint64);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001964 case 'W':
Zachary Turner03312862018-08-27 03:48:03 +00001965 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Wchar);
Nico Webera2ca6e72019-05-28 15:30:04 +00001966 case 'Q':
1967 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Char8);
Zachary Turner931e8792018-07-30 23:02:10 +00001968 case 'S':
Zachary Turner03312862018-08-27 03:48:03 +00001969 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Char16);
Zachary Turner931e8792018-07-30 23:02:10 +00001970 case 'U':
Zachary Turner03312862018-08-27 03:48:03 +00001971 return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Char32);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001972 }
1973 break;
1974 }
1975 }
Zachary Turner03312862018-08-27 03:48:03 +00001976 Error = true;
1977 return nullptr;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001978}
1979
Zachary Turner03312862018-08-27 03:48:03 +00001980TagTypeNode *Demangler::demangleClassType(StringView &MangledName) {
1981 TagTypeNode *TT = nullptr;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001982
1983 switch (MangledName.popFront()) {
1984 case 'T':
Zachary Turner03312862018-08-27 03:48:03 +00001985 TT = Arena.alloc<TagTypeNode>(TagKind::Union);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001986 break;
1987 case 'U':
Zachary Turner03312862018-08-27 03:48:03 +00001988 TT = Arena.alloc<TagTypeNode>(TagKind::Struct);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001989 break;
1990 case 'V':
Zachary Turner03312862018-08-27 03:48:03 +00001991 TT = Arena.alloc<TagTypeNode>(TagKind::Class);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001992 break;
1993 case 'W':
Nico Webere5b62652019-04-11 22:59:25 +00001994 if (!MangledName.consumeFront('4')) {
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001995 Error = true;
1996 return nullptr;
1997 }
Zachary Turner03312862018-08-27 03:48:03 +00001998 TT = Arena.alloc<TagTypeNode>(TagKind::Enum);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001999 break;
2000 default:
2001 assert(false);
2002 }
2003
Zachary Turner03312862018-08-27 03:48:03 +00002004 TT->QualifiedName = demangleFullyQualifiedTypeName(MangledName);
2005 return TT;
Zachary Turnerd742d642018-07-26 19:56:09 +00002006}
2007
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002008// <pointer-type> ::= E? <pointer-cvr-qualifiers> <ext-qualifiers> <type>
2009// # the E is required for 64-bit non-static pointers
Zachary Turner03312862018-08-27 03:48:03 +00002010PointerTypeNode *Demangler::demanglePointerType(StringView &MangledName) {
2011 PointerTypeNode *Pointer = Arena.alloc<PointerTypeNode>();
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002012
Zachary Turner931e8792018-07-30 23:02:10 +00002013 std::tie(Pointer->Quals, Pointer->Affinity) =
2014 demanglePointerCVQualifiers(MangledName);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002015
2016 if (MangledName.consumeFront("6")) {
Zachary Turner03312862018-08-27 03:48:03 +00002017 Pointer->Pointee = demangleFunctionType(MangledName, false);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002018 return Pointer;
2019 }
2020
Zachary Turner316109b2018-07-29 16:38:02 +00002021 Qualifiers ExtQuals = demanglePointerExtQualifiers(MangledName);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002022 Pointer->Quals = Qualifiers(Pointer->Quals | ExtQuals);
2023
Zachary Turner316109b2018-07-29 16:38:02 +00002024 Pointer->Pointee = demangleType(MangledName, QualifierMangleMode::Mangle);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002025 return Pointer;
2026}
2027
Zachary Turner03312862018-08-27 03:48:03 +00002028PointerTypeNode *Demangler::demangleMemberPointerType(StringView &MangledName) {
2029 PointerTypeNode *Pointer = Arena.alloc<PointerTypeNode>();
Zachary Turnerd742d642018-07-26 19:56:09 +00002030
Zachary Turner03312862018-08-27 03:48:03 +00002031 std::tie(Pointer->Quals, Pointer->Affinity) =
2032 demanglePointerCVQualifiers(MangledName);
2033 assert(Pointer->Affinity == PointerAffinity::Pointer);
Zachary Turnerd742d642018-07-26 19:56:09 +00002034
Zachary Turner316109b2018-07-29 16:38:02 +00002035 Qualifiers ExtQuals = demanglePointerExtQualifiers(MangledName);
Zachary Turnerd742d642018-07-26 19:56:09 +00002036 Pointer->Quals = Qualifiers(Pointer->Quals | ExtQuals);
2037
Nico Weber63b97d22019-04-08 19:46:53 +00002038 // isMemberPointer() only returns true if there is at least one character
2039 // after the qualifiers.
Zachary Turner38b78a72018-07-26 20:20:10 +00002040 if (MangledName.consumeFront("8")) {
Zachary Turner32a8a202018-08-29 23:56:09 +00002041 Pointer->ClassParent = demangleFullyQualifiedTypeName(MangledName);
Zachary Turner03312862018-08-27 03:48:03 +00002042 Pointer->Pointee = demangleFunctionType(MangledName, true);
Zachary Turner38b78a72018-07-26 20:20:10 +00002043 } else {
2044 Qualifiers PointeeQuals = Q_None;
2045 bool IsMember = false;
Zachary Turner316109b2018-07-29 16:38:02 +00002046 std::tie(PointeeQuals, IsMember) = demangleQualifiers(MangledName);
Nico Weber63b97d22019-04-08 19:46:53 +00002047 assert(IsMember || Error);
Zachary Turner32a8a202018-08-29 23:56:09 +00002048 Pointer->ClassParent = demangleFullyQualifiedTypeName(MangledName);
Zachary Turnerd742d642018-07-26 19:56:09 +00002049
Zachary Turner316109b2018-07-29 16:38:02 +00002050 Pointer->Pointee = demangleType(MangledName, QualifierMangleMode::Drop);
Nico Weber63b97d22019-04-08 19:46:53 +00002051 if (Pointer->Pointee)
2052 Pointer->Pointee->Quals = PointeeQuals;
Zachary Turner38b78a72018-07-26 20:20:10 +00002053 }
2054
Zachary Turnerd742d642018-07-26 19:56:09 +00002055 return Pointer;
2056}
2057
Zachary Turner316109b2018-07-29 16:38:02 +00002058Qualifiers Demangler::demanglePointerExtQualifiers(StringView &MangledName) {
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002059 Qualifiers Quals = Q_None;
2060 if (MangledName.consumeFront('E'))
2061 Quals = Qualifiers(Quals | Q_Pointer64);
2062 if (MangledName.consumeFront('I'))
2063 Quals = Qualifiers(Quals | Q_Restrict);
2064 if (MangledName.consumeFront('F'))
2065 Quals = Qualifiers(Quals | Q_Unaligned);
2066
2067 return Quals;
2068}
2069
Zachary Turner03312862018-08-27 03:48:03 +00002070ArrayTypeNode *Demangler::demangleArrayType(StringView &MangledName) {
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002071 assert(MangledName.front() == 'Y');
2072 MangledName.popFront();
2073
Zachary Turnerdbefc6c2018-08-10 14:31:04 +00002074 uint64_t Rank = 0;
2075 bool IsNegative = false;
2076 std::tie(Rank, IsNegative) = demangleNumber(MangledName);
2077 if (IsNegative || Rank == 0) {
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002078 Error = true;
2079 return nullptr;
2080 }
2081
Zachary Turner03312862018-08-27 03:48:03 +00002082 ArrayTypeNode *ATy = Arena.alloc<ArrayTypeNode>();
2083 NodeList *Head = Arena.alloc<NodeList>();
2084 NodeList *Tail = Head;
2085
Zachary Turnerdbefc6c2018-08-10 14:31:04 +00002086 for (uint64_t I = 0; I < Rank; ++I) {
Zachary Turner03312862018-08-27 03:48:03 +00002087 uint64_t D = 0;
2088 std::tie(D, IsNegative) = demangleNumber(MangledName);
Nico Weber16725812019-04-03 23:27:18 +00002089 if (Error || IsNegative) {
Zachary Turnerdbefc6c2018-08-10 14:31:04 +00002090 Error = true;
2091 return nullptr;
2092 }
Zachary Turner03312862018-08-27 03:48:03 +00002093 Tail->N = Arena.alloc<IntegerLiteralNode>(D, IsNegative);
Zachary Turnerdbefc6c2018-08-10 14:31:04 +00002094 if (I + 1 < Rank) {
Zachary Turner03312862018-08-27 03:48:03 +00002095 Tail->Next = Arena.alloc<NodeList>();
2096 Tail = Tail->Next;
Zachary Turnerdbefc6c2018-08-10 14:31:04 +00002097 }
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002098 }
Zachary Turner03312862018-08-27 03:48:03 +00002099 ATy->Dimensions = nodeListToNodeArray(Arena, Head, Rank);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002100
2101 if (MangledName.consumeFront("$$C")) {
Zachary Turner2bbb23b2018-08-14 18:54:28 +00002102 bool IsMember = false;
2103 std::tie(ATy->Quals, IsMember) = demangleQualifiers(MangledName);
2104 if (IsMember) {
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002105 Error = true;
Zachary Turner2bbb23b2018-08-14 18:54:28 +00002106 return nullptr;
2107 }
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002108 }
2109
Zachary Turner316109b2018-07-29 16:38:02 +00002110 ATy->ElementType = demangleType(MangledName, QualifierMangleMode::Drop);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002111 return ATy;
2112}
2113
Nico Weber46385482019-06-04 18:49:05 +00002114// Reads a function's parameters.
Nico Weber1dce8262019-06-04 19:10:08 +00002115NodeArrayNode *Demangler::demangleFunctionParameterList(StringView &MangledName,
2116 bool &IsVariadic) {
Zachary Turner38b78a72018-07-26 20:20:10 +00002117 // Empty parameter list.
Zachary Turner38b78a72018-07-26 20:20:10 +00002118 if (MangledName.consumeFront('X'))
Nico Weber930994c2019-04-16 14:24:42 +00002119 return nullptr;
Zachary Turner38b78a72018-07-26 20:20:10 +00002120
Zachary Turner03312862018-08-27 03:48:03 +00002121 NodeList *Head = Arena.alloc<NodeList>();
2122 NodeList **Current = &Head;
2123 size_t Count = 0;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002124 while (!Error && !MangledName.startsWith('@') &&
2125 !MangledName.startsWith('Z')) {
Zachary Turner03312862018-08-27 03:48:03 +00002126 ++Count;
Zachary Turner23df1312018-07-26 22:13:39 +00002127
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002128 if (startsWithDigit(MangledName)) {
Zachary Turner30375de2018-07-26 22:24:01 +00002129 size_t N = MangledName[0] - '0';
Zachary Turnerd346cba2018-08-08 17:17:04 +00002130 if (N >= Backrefs.FunctionParamCount) {
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002131 Error = true;
Nico Weber930994c2019-04-16 14:24:42 +00002132 return nullptr;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002133 }
2134 MangledName = MangledName.dropFront();
2135
Zachary Turner03312862018-08-27 03:48:03 +00002136 *Current = Arena.alloc<NodeList>();
2137 (*Current)->N = Backrefs.FunctionParams[N];
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002138 Current = &(*Current)->Next;
2139 continue;
2140 }
2141
Zachary Turner23df1312018-07-26 22:13:39 +00002142 size_t OldSize = MangledName.size();
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002143
Zachary Turner03312862018-08-27 03:48:03 +00002144 *Current = Arena.alloc<NodeList>();
2145 TypeNode *TN = demangleType(MangledName, QualifierMangleMode::Drop);
Zachary Turner8fb9a712018-12-14 18:10:13 +00002146 if (!TN || Error)
2147 return nullptr;
Zachary Turner03312862018-08-27 03:48:03 +00002148
2149 (*Current)->N = TN;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002150
Zachary Turner23df1312018-07-26 22:13:39 +00002151 size_t CharsConsumed = OldSize - MangledName.size();
2152 assert(CharsConsumed != 0);
2153
2154 // Single-letter types are ignored for backreferences because memorizing
2155 // them doesn't save anything.
Zachary Turnerd346cba2018-08-08 17:17:04 +00002156 if (Backrefs.FunctionParamCount <= 9 && CharsConsumed > 1)
Zachary Turner03312862018-08-27 03:48:03 +00002157 Backrefs.FunctionParams[Backrefs.FunctionParamCount++] = TN;
Zachary Turner23df1312018-07-26 22:13:39 +00002158
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002159 Current = &(*Current)->Next;
2160 }
2161
Zachary Turner38b78a72018-07-26 20:20:10 +00002162 if (Error)
Nico Weber930994c2019-04-16 14:24:42 +00002163 return nullptr;
Zachary Turner38b78a72018-07-26 20:20:10 +00002164
Zachary Turner03312862018-08-27 03:48:03 +00002165 NodeArrayNode *NA = nodeListToNodeArray(Arena, Head, Count);
Zachary Turner38b78a72018-07-26 20:20:10 +00002166 // A non-empty parameter list is terminated by either 'Z' (variadic) parameter
2167 // list or '@' (non variadic). Careful not to consume "@Z", as in that case
2168 // the following Z could be a throw specifier.
2169 if (MangledName.consumeFront('@'))
Zachary Turner03312862018-08-27 03:48:03 +00002170 return NA;
Zachary Turner38b78a72018-07-26 20:20:10 +00002171
2172 if (MangledName.consumeFront('Z')) {
Nico Weber1dce8262019-06-04 19:10:08 +00002173 IsVariadic = true;
Zachary Turner03312862018-08-27 03:48:03 +00002174 return NA;
Zachary Turner38b78a72018-07-26 20:20:10 +00002175 }
2176
Nico Weber46385482019-06-04 18:49:05 +00002177 DEMANGLE_UNREACHABLE;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002178}
2179
Zachary Turner03312862018-08-27 03:48:03 +00002180NodeArrayNode *
Zachary Turnerd30700f2018-07-31 17:16:44 +00002181Demangler::demangleTemplateParameterList(StringView &MangledName) {
Simon Pilgrimd2a3e892019-10-02 11:48:45 +00002182 NodeList *Head = nullptr;
Zachary Turner03312862018-08-27 03:48:03 +00002183 NodeList **Current = &Head;
2184 size_t Count = 0;
2185
Nico Weber46385482019-06-04 18:49:05 +00002186 while (!MangledName.startsWith('@')) {
Zachary Turner32a8a202018-08-29 23:56:09 +00002187 if (MangledName.consumeFront("$S") || MangledName.consumeFront("$$V") ||
Zachary Turner78ab3cb2018-08-30 20:53:29 +00002188 MangledName.consumeFront("$$$V") || MangledName.consumeFront("$$Z")) {
2189 // parameter pack separator
Zachary Turner32a8a202018-08-29 23:56:09 +00002190 continue;
2191 }
2192
Zachary Turner03312862018-08-27 03:48:03 +00002193 ++Count;
Zachary Turner32a8a202018-08-29 23:56:09 +00002194
Zachary Turner23df1312018-07-26 22:13:39 +00002195 // Template parameter lists don't participate in back-referencing.
Zachary Turner03312862018-08-27 03:48:03 +00002196 *Current = Arena.alloc<NodeList>();
Zachary Turner931e8792018-07-30 23:02:10 +00002197
Zachary Turner03312862018-08-27 03:48:03 +00002198 NodeList &TP = **Current;
Zachary Turner66555a72018-08-20 19:15:35 +00002199
Zachary Turner03312862018-08-27 03:48:03 +00002200 TemplateParameterReferenceNode *TPRN = nullptr;
Zachary Turner32a8a202018-08-29 23:56:09 +00002201 if (MangledName.consumeFront("$$Y")) {
Zachary Turnerdbefc6c2018-08-10 14:31:04 +00002202 // Template alias
Zachary Turner03312862018-08-27 03:48:03 +00002203 TP.N = demangleFullyQualifiedTypeName(MangledName);
Zachary Turnerdbefc6c2018-08-10 14:31:04 +00002204 } else if (MangledName.consumeFront("$$B")) {
2205 // Array
Zachary Turner03312862018-08-27 03:48:03 +00002206 TP.N = demangleType(MangledName, QualifierMangleMode::Drop);
Zachary Turner073620b2018-08-10 19:57:36 +00002207 } else if (MangledName.consumeFront("$$C")) {
2208 // Type has qualifiers.
Zachary Turner03312862018-08-27 03:48:03 +00002209 TP.N = demangleType(MangledName, QualifierMangleMode::Mangle);
Zachary Turner66555a72018-08-20 19:15:35 +00002210 } else if (MangledName.startsWith("$1") || MangledName.startsWith("$H") ||
2211 MangledName.startsWith("$I") || MangledName.startsWith("$J")) {
Zachary Turner03312862018-08-27 03:48:03 +00002212 // Pointer to member
2213 TP.N = TPRN = Arena.alloc<TemplateParameterReferenceNode>();
2214 TPRN->IsMemberPointer = true;
2215
Zachary Turner66555a72018-08-20 19:15:35 +00002216 MangledName = MangledName.dropFront();
2217 // 1 - single inheritance <name>
2218 // H - multiple inheritance <name> <number>
Nico Weber880d21d2019-06-04 15:13:30 +00002219 // I - virtual inheritance <name> <number> <number>
Zachary Turner66555a72018-08-20 19:15:35 +00002220 // J - unspecified inheritance <name> <number> <number> <number>
2221 char InheritanceSpecifier = MangledName.popFront();
Zachary Turnerb2fef1a2018-08-29 04:12:44 +00002222 SymbolNode *S = nullptr;
2223 if (MangledName.startsWith('?')) {
2224 S = parse(MangledName);
Nico Weber09fb2022019-05-22 15:53:23 +00002225 if (Error || !S->Name) {
2226 Error = true;
Nico Weberf2d8f092019-04-11 22:23:35 +00002227 return nullptr;
Nico Weber09fb2022019-05-22 15:53:23 +00002228 }
Zachary Turnerb2fef1a2018-08-29 04:12:44 +00002229 memorizeIdentifier(S->Name->getUnqualifiedIdentifier());
2230 }
2231
Zachary Turner66555a72018-08-20 19:15:35 +00002232 switch (InheritanceSpecifier) {
2233 case 'J':
Zachary Turner03312862018-08-27 03:48:03 +00002234 TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
2235 demangleSigned(MangledName);
Erik Pilkington5094e5e2019-01-17 20:37:51 +00002236 DEMANGLE_FALLTHROUGH;
Zachary Turner66555a72018-08-20 19:15:35 +00002237 case 'I':
Zachary Turner03312862018-08-27 03:48:03 +00002238 TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
2239 demangleSigned(MangledName);
Erik Pilkington5094e5e2019-01-17 20:37:51 +00002240 DEMANGLE_FALLTHROUGH;
Zachary Turner66555a72018-08-20 19:15:35 +00002241 case 'H':
Zachary Turner03312862018-08-27 03:48:03 +00002242 TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
2243 demangleSigned(MangledName);
Erik Pilkington5094e5e2019-01-17 20:37:51 +00002244 DEMANGLE_FALLTHROUGH;
Zachary Turner66555a72018-08-20 19:15:35 +00002245 case '1':
2246 break;
2247 default:
Nico Weber880d21d2019-06-04 15:13:30 +00002248 DEMANGLE_UNREACHABLE;
Zachary Turner66555a72018-08-20 19:15:35 +00002249 }
Zachary Turner03312862018-08-27 03:48:03 +00002250 TPRN->Affinity = PointerAffinity::Pointer;
2251 TPRN->Symbol = S;
Zachary Turnerdbefc6c2018-08-10 14:31:04 +00002252 } else if (MangledName.startsWith("$E?")) {
2253 MangledName.consumeFront("$E");
2254 // Reference to symbol
Zachary Turner03312862018-08-27 03:48:03 +00002255 TP.N = TPRN = Arena.alloc<TemplateParameterReferenceNode>();
2256 TPRN->Symbol = parse(MangledName);
2257 TPRN->Affinity = PointerAffinity::Reference;
Zachary Turner66555a72018-08-20 19:15:35 +00002258 } else if (MangledName.startsWith("$F") || MangledName.startsWith("$G")) {
Zachary Turner03312862018-08-27 03:48:03 +00002259 TP.N = TPRN = Arena.alloc<TemplateParameterReferenceNode>();
2260
Zachary Turner66555a72018-08-20 19:15:35 +00002261 // Data member pointer.
2262 MangledName = MangledName.dropFront();
2263 char InheritanceSpecifier = MangledName.popFront();
2264
2265 switch (InheritanceSpecifier) {
2266 case 'G':
Zachary Turner03312862018-08-27 03:48:03 +00002267 TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
2268 demangleSigned(MangledName);
Erik Pilkington5094e5e2019-01-17 20:37:51 +00002269 DEMANGLE_FALLTHROUGH;
Zachary Turner66555a72018-08-20 19:15:35 +00002270 case 'F':
Zachary Turner03312862018-08-27 03:48:03 +00002271 TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
2272 demangleSigned(MangledName);
2273 TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
2274 demangleSigned(MangledName);
Zachary Turner66555a72018-08-20 19:15:35 +00002275 break;
2276 default:
Nico Weber880d21d2019-06-04 15:13:30 +00002277 DEMANGLE_UNREACHABLE;
Zachary Turner66555a72018-08-20 19:15:35 +00002278 }
Zachary Turner03312862018-08-27 03:48:03 +00002279 TPRN->IsMemberPointer = true;
Zachary Turner66555a72018-08-20 19:15:35 +00002280
Zachary Turnerdbefc6c2018-08-10 14:31:04 +00002281 } else if (MangledName.consumeFront("$0")) {
2282 // Integral non-type template parameter
2283 bool IsNegative = false;
2284 uint64_t Value = 0;
2285 std::tie(Value, IsNegative) = demangleNumber(MangledName);
2286
Zachary Turner03312862018-08-27 03:48:03 +00002287 TP.N = Arena.alloc<IntegerLiteralNode>(Value, IsNegative);
Zachary Turnerd30700f2018-07-31 17:16:44 +00002288 } else {
Zachary Turner03312862018-08-27 03:48:03 +00002289 TP.N = demangleType(MangledName, QualifierMangleMode::Drop);
Zachary Turnerd30700f2018-07-31 17:16:44 +00002290 }
Zachary Turner54d4ffe2018-08-01 18:32:28 +00002291 if (Error)
2292 return nullptr;
Zachary Turner23df1312018-07-26 22:13:39 +00002293
Zachary Turner66555a72018-08-20 19:15:35 +00002294 Current = &TP.Next;
Zachary Turner23df1312018-07-26 22:13:39 +00002295 }
2296
Nico Weber46385482019-06-04 18:49:05 +00002297 // The loop above returns nullptr on Error.
2298 assert(!Error);
Zachary Turner23df1312018-07-26 22:13:39 +00002299
2300 // Template parameter lists cannot be variadic, so it can only be terminated
Nico Weber46385482019-06-04 18:49:05 +00002301 // by @ (as opposed to 'Z' in the function parameter case).
2302 assert(MangledName.startsWith('@')); // The above loop exits only on '@'.
2303 MangledName.consumeFront('@');
2304 return nodeListToNodeArray(Arena, Head, Count);
Zachary Turner23df1312018-07-26 22:13:39 +00002305}
2306
Zachary Turner3a758e22018-08-01 18:33:04 +00002307void Demangler::dumpBackReferences() {
Zachary Turner5ae08b82018-08-01 18:44:12 +00002308 std::printf("%d function parameter backreferences\n",
Zachary Turnerd346cba2018-08-08 17:17:04 +00002309 (int)Backrefs.FunctionParamCount);
Zachary Turner3a758e22018-08-01 18:33:04 +00002310
2311 // Create an output stream so we can render each type.
Nico Weber1359d652018-09-15 18:24:20 +00002312 OutputStream OS;
Nico Weber6808bc02018-11-11 10:04:00 +00002313 if (!initializeOutputStream(nullptr, nullptr, OS, 1024))
Nico Weber1359d652018-09-15 18:24:20 +00002314 std::terminate();
Zachary Turnerd346cba2018-08-08 17:17:04 +00002315 for (size_t I = 0; I < Backrefs.FunctionParamCount; ++I) {
Zachary Turner3a758e22018-08-01 18:33:04 +00002316 OS.setCurrentPosition(0);
2317
Zachary Turner03312862018-08-27 03:48:03 +00002318 TypeNode *T = Backrefs.FunctionParams[I];
Zachary Turner38d2edd2018-08-29 03:59:17 +00002319 T->output(OS, OF_Default);
Zachary Turner3a758e22018-08-01 18:33:04 +00002320
Zachary Turner7563ebe2018-08-02 17:08:24 +00002321 std::printf(" [%d] - %.*s\n", (int)I, (int)OS.getCurrentPosition(),
Zachary Turner5ae08b82018-08-01 18:44:12 +00002322 OS.getBuffer());
Zachary Turner3a758e22018-08-01 18:33:04 +00002323 }
2324 std::free(OS.getBuffer());
2325
Zachary Turnerd346cba2018-08-08 17:17:04 +00002326 if (Backrefs.FunctionParamCount > 0)
Zachary Turner5ae08b82018-08-01 18:44:12 +00002327 std::printf("\n");
Zachary Turnerd346cba2018-08-08 17:17:04 +00002328 std::printf("%d name backreferences\n", (int)Backrefs.NamesCount);
2329 for (size_t I = 0; I < Backrefs.NamesCount; ++I) {
Zachary Turner03312862018-08-27 03:48:03 +00002330 std::printf(" [%d] - %.*s\n", (int)I, (int)Backrefs.Names[I]->Name.size(),
2331 Backrefs.Names[I]->Name.begin());
Zachary Turner3a758e22018-08-01 18:33:04 +00002332 }
Zachary Turnerd346cba2018-08-08 17:17:04 +00002333 if (Backrefs.NamesCount > 0)
Zachary Turner5ae08b82018-08-01 18:44:12 +00002334 std::printf("\n");
Zachary Turner3a758e22018-08-01 18:33:04 +00002335}
2336
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002337char *llvm::microsoftDemangle(const char *MangledName, char *Buf, size_t *N,
Zachary Turner3a758e22018-08-01 18:33:04 +00002338 int *Status, MSDemangleFlags Flags) {
Nico Weber1359d652018-09-15 18:24:20 +00002339 int InternalStatus = demangle_success;
Zachary Turner316109b2018-07-29 16:38:02 +00002340 Demangler D;
Nico Weber1359d652018-09-15 18:24:20 +00002341 OutputStream S;
2342
Zachary Turner316109b2018-07-29 16:38:02 +00002343 StringView Name{MangledName};
Nico Weber1359d652018-09-15 18:24:20 +00002344 SymbolNode *AST = D.parse(Name);
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002345
Zachary Turner3a758e22018-08-01 18:33:04 +00002346 if (Flags & MSDF_DumpBackrefs)
2347 D.dumpBackReferences();
Nico Weber1359d652018-09-15 18:24:20 +00002348
2349 if (D.Error)
2350 InternalStatus = demangle_invalid_mangled_name;
Nico Weber6808bc02018-11-11 10:04:00 +00002351 else if (!initializeOutputStream(Buf, N, S, 1024))
Nico Weber1359d652018-09-15 18:24:20 +00002352 InternalStatus = demangle_memory_alloc_failure;
2353 else {
2354 AST->output(S, OF_Default);
2355 S += '\0';
2356 if (N != nullptr)
2357 *N = S.getCurrentPosition();
2358 Buf = S.getBuffer();
Zachary Turner54d4ffe2018-08-01 18:32:28 +00002359 }
2360
Nico Weber1359d652018-09-15 18:24:20 +00002361 if (Status)
2362 *Status = InternalStatus;
2363 return InternalStatus == demangle_success ? Buf : nullptr;
Zachary Turnerf435a7e2018-07-20 17:27:48 +00002364}