blob: c8a77adc9fafd6e9cbdfd13e7df83ec9e811d53b [file] [log] [blame]
Richard Smithc20d1442018-08-20 20:14:49 +00001//===------------------------- ItaniumDemangle.h ----------------*- C++ -*-===//
2//
Chandler Carruth8ee27c32019-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
Richard Smithc20d1442018-08-20 20:14:49 +00006//
7//===----------------------------------------------------------------------===//
8//
Nathan Sidwell5b0a8cf2022-01-24 06:38:47 -08009// Generic itanium demangler library.
10// There are two copies of this file in the source tree. The one under
11// libcxxabi is the original and the one under llvm is the copy. Use
12// cp-to-llvm.sh to update the copy. See README.txt for more details.
Richard Smithc20d1442018-08-20 20:14:49 +000013//
14//===----------------------------------------------------------------------===//
15
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +000016#ifndef DEMANGLE_ITANIUMDEMANGLE_H
17#define DEMANGLE_ITANIUMDEMANGLE_H
Richard Smithc20d1442018-08-20 20:14:49 +000018
19// FIXME: (possibly) incomplete list of features that clang mangles that this
20// file does not yet support:
21// - C++ modules TS
22
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +000023#include "DemangleConfig.h"
Richard Smithc20d1442018-08-20 20:14:49 +000024#include "StringView.h"
25#include "Utility.h"
Nathan Sidwellbbc632f2022-01-24 04:11:59 -080026#include <algorithm>
Richard Smithc20d1442018-08-20 20:14:49 +000027#include <cassert>
28#include <cctype>
29#include <cstdio>
30#include <cstdlib>
31#include <cstring>
Nathan Sidwellbbc632f2022-01-24 04:11:59 -080032#include <limits>
Richard Smithc20d1442018-08-20 20:14:49 +000033#include <utility>
34
35#define FOR_EACH_NODE_KIND(X) \
36 X(NodeArrayNode) \
37 X(DotSuffix) \
38 X(VendorExtQualType) \
39 X(QualType) \
40 X(ConversionOperatorType) \
41 X(PostfixQualifiedType) \
42 X(ElaboratedTypeSpefType) \
43 X(NameType) \
44 X(AbiTagAttr) \
45 X(EnableIfAttr) \
46 X(ObjCProtoName) \
47 X(PointerType) \
48 X(ReferenceType) \
49 X(PointerToMemberType) \
50 X(ArrayType) \
51 X(FunctionType) \
52 X(NoexceptSpec) \
53 X(DynamicExceptionSpec) \
54 X(FunctionEncoding) \
55 X(LiteralOperator) \
56 X(SpecialName) \
57 X(CtorVtableSpecialName) \
58 X(QualifiedName) \
59 X(NestedName) \
60 X(LocalName) \
61 X(VectorType) \
62 X(PixelVectorType) \
Pengfei Wang50e90b82021-09-23 11:02:25 +080063 X(BinaryFPType) \
Richard Smithdf1c14c2019-09-06 23:53:21 +000064 X(SyntheticTemplateParamName) \
65 X(TypeTemplateParamDecl) \
66 X(NonTypeTemplateParamDecl) \
67 X(TemplateTemplateParamDecl) \
68 X(TemplateParamPackDecl) \
Richard Smithc20d1442018-08-20 20:14:49 +000069 X(ParameterPack) \
70 X(TemplateArgumentPack) \
71 X(ParameterPackExpansion) \
72 X(TemplateArgs) \
73 X(ForwardTemplateReference) \
74 X(NameWithTemplateArgs) \
75 X(GlobalQualifiedName) \
Richard Smithc20d1442018-08-20 20:14:49 +000076 X(ExpandedSpecialSubstitution) \
77 X(SpecialSubstitution) \
78 X(CtorDtorName) \
79 X(DtorName) \
80 X(UnnamedTypeName) \
81 X(ClosureTypeName) \
82 X(StructuredBindingName) \
83 X(BinaryExpr) \
84 X(ArraySubscriptExpr) \
85 X(PostfixExpr) \
86 X(ConditionalExpr) \
87 X(MemberExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070088 X(SubobjectExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000089 X(EnclosingExpr) \
90 X(CastExpr) \
91 X(SizeofParamPackExpr) \
92 X(CallExpr) \
93 X(NewExpr) \
94 X(DeleteExpr) \
95 X(PrefixExpr) \
96 X(FunctionParam) \
97 X(ConversionExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070098 X(PointerToMemberConversionExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000099 X(InitListExpr) \
100 X(FoldExpr) \
101 X(ThrowExpr) \
102 X(BoolExpr) \
Richard Smithdf1c14c2019-09-06 23:53:21 +0000103 X(StringLiteral) \
104 X(LambdaExpr) \
Erik Pilkington0a170f12020-05-13 14:13:37 -0400105 X(EnumLiteral) \
Richard Smithc20d1442018-08-20 20:14:49 +0000106 X(IntegerLiteral) \
107 X(FloatLiteral) \
108 X(DoubleLiteral) \
109 X(LongDoubleLiteral) \
110 X(BracedExpr) \
111 X(BracedRangeExpr)
112
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000113DEMANGLE_NAMESPACE_BEGIN
114
Mikhail Borisov8452f062021-08-17 18:06:53 -0400115template <class T, size_t N> class PODSmallVector {
116 static_assert(std::is_pod<T>::value,
117 "T is required to be a plain old data type");
118
119 T *First = nullptr;
120 T *Last = nullptr;
121 T *Cap = nullptr;
122 T Inline[N] = {0};
123
124 bool isInline() const { return First == Inline; }
125
126 void clearInline() {
127 First = Inline;
128 Last = Inline;
129 Cap = Inline + N;
130 }
131
132 void reserve(size_t NewCap) {
133 size_t S = size();
134 if (isInline()) {
135 auto *Tmp = static_cast<T *>(std::malloc(NewCap * sizeof(T)));
136 if (Tmp == nullptr)
137 std::terminate();
138 std::copy(First, Last, Tmp);
139 First = Tmp;
140 } else {
141 First = static_cast<T *>(std::realloc(First, NewCap * sizeof(T)));
142 if (First == nullptr)
143 std::terminate();
144 }
145 Last = First + S;
146 Cap = First + NewCap;
147 }
148
149public:
150 PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
151
152 PODSmallVector(const PODSmallVector &) = delete;
153 PODSmallVector &operator=(const PODSmallVector &) = delete;
154
155 PODSmallVector(PODSmallVector &&Other) : PODSmallVector() {
156 if (Other.isInline()) {
157 std::copy(Other.begin(), Other.end(), First);
158 Last = First + Other.size();
159 Other.clear();
160 return;
161 }
162
163 First = Other.First;
164 Last = Other.Last;
165 Cap = Other.Cap;
166 Other.clearInline();
167 }
168
169 PODSmallVector &operator=(PODSmallVector &&Other) {
170 if (Other.isInline()) {
171 if (!isInline()) {
172 std::free(First);
173 clearInline();
174 }
175 std::copy(Other.begin(), Other.end(), First);
176 Last = First + Other.size();
177 Other.clear();
178 return *this;
179 }
180
181 if (isInline()) {
182 First = Other.First;
183 Last = Other.Last;
184 Cap = Other.Cap;
185 Other.clearInline();
186 return *this;
187 }
188
189 std::swap(First, Other.First);
190 std::swap(Last, Other.Last);
191 std::swap(Cap, Other.Cap);
192 Other.clear();
193 return *this;
194 }
195
196 // NOLINTNEXTLINE(readability-identifier-naming)
197 void push_back(const T &Elem) {
198 if (Last == Cap)
199 reserve(size() * 2);
200 *Last++ = Elem;
201 }
202
203 // NOLINTNEXTLINE(readability-identifier-naming)
204 void pop_back() {
205 assert(Last != First && "Popping empty vector!");
206 --Last;
207 }
208
209 void dropBack(size_t Index) {
210 assert(Index <= size() && "dropBack() can't expand!");
211 Last = First + Index;
212 }
213
214 T *begin() { return First; }
215 T *end() { return Last; }
216
217 bool empty() const { return First == Last; }
218 size_t size() const { return static_cast<size_t>(Last - First); }
219 T &back() {
220 assert(Last != First && "Calling back() on empty vector!");
221 return *(Last - 1);
222 }
223 T &operator[](size_t Index) {
224 assert(Index < size() && "Invalid access!");
225 return *(begin() + Index);
226 }
227 void clear() { Last = First; }
228
229 ~PODSmallVector() {
230 if (!isInline())
231 std::free(First);
232 }
233};
234
Richard Smithc20d1442018-08-20 20:14:49 +0000235// Base class of all AST nodes. The AST is built by the parser, then is
236// traversed by the printLeft/Right functions to produce a demangled string.
237class Node {
238public:
239 enum Kind : unsigned char {
240#define ENUMERATOR(NodeKind) K ## NodeKind,
241 FOR_EACH_NODE_KIND(ENUMERATOR)
242#undef ENUMERATOR
243 };
244
245 /// Three-way bool to track a cached value. Unknown is possible if this node
246 /// has an unexpanded parameter pack below it that may affect this cache.
247 enum class Cache : unsigned char { Yes, No, Unknown, };
248
249private:
250 Kind K;
251
252 // FIXME: Make these protected.
253public:
254 /// Tracks if this node has a component on its right side, in which case we
255 /// need to call printRight.
256 Cache RHSComponentCache;
257
258 /// Track if this node is a (possibly qualified) array type. This can affect
259 /// how we format the output string.
260 Cache ArrayCache;
261
262 /// Track if this node is a (possibly qualified) function type. This can
263 /// affect how we format the output string.
264 Cache FunctionCache;
265
266public:
267 Node(Kind K_, Cache RHSComponentCache_ = Cache::No,
268 Cache ArrayCache_ = Cache::No, Cache FunctionCache_ = Cache::No)
269 : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_),
270 FunctionCache(FunctionCache_) {}
271
272 /// Visit the most-derived object corresponding to this object.
273 template<typename Fn> void visit(Fn F) const;
274
275 // The following function is provided by all derived classes:
276 //
277 // Call F with arguments that, when passed to the constructor of this node,
278 // would construct an equivalent node.
279 //template<typename Fn> void match(Fn F) const;
280
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700281 bool hasRHSComponent(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000282 if (RHSComponentCache != Cache::Unknown)
283 return RHSComponentCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700284 return hasRHSComponentSlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000285 }
286
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700287 bool hasArray(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000288 if (ArrayCache != Cache::Unknown)
289 return ArrayCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700290 return hasArraySlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000291 }
292
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700293 bool hasFunction(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000294 if (FunctionCache != Cache::Unknown)
295 return FunctionCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700296 return hasFunctionSlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000297 }
298
299 Kind getKind() const { return K; }
300
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700301 virtual bool hasRHSComponentSlow(OutputBuffer &) const { return false; }
302 virtual bool hasArraySlow(OutputBuffer &) const { return false; }
303 virtual bool hasFunctionSlow(OutputBuffer &) const { return false; }
Richard Smithc20d1442018-08-20 20:14:49 +0000304
305 // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
306 // get at a node that actually represents some concrete syntax.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700307 virtual const Node *getSyntaxNode(OutputBuffer &) const { return this; }
Richard Smithc20d1442018-08-20 20:14:49 +0000308
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700309 void print(OutputBuffer &OB) const {
310 printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000311 if (RHSComponentCache != Cache::No)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700312 printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000313 }
314
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -0800315 // Print the "left" side of this Node into OutputBuffer.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700316 virtual void printLeft(OutputBuffer &) const = 0;
Richard Smithc20d1442018-08-20 20:14:49 +0000317
318 // Print the "right". This distinction is necessary to represent C++ types
319 // that appear on the RHS of their subtype, such as arrays or functions.
320 // Since most types don't have such a component, provide a default
321 // implementation.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700322 virtual void printRight(OutputBuffer &) const {}
Richard Smithc20d1442018-08-20 20:14:49 +0000323
324 virtual StringView getBaseName() const { return StringView(); }
325
326 // Silence compiler warnings, this dtor will never be called.
327 virtual ~Node() = default;
328
329#ifndef NDEBUG
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000330 DEMANGLE_DUMP_METHOD void dump() const;
Richard Smithc20d1442018-08-20 20:14:49 +0000331#endif
332};
333
334class NodeArray {
335 Node **Elements;
336 size_t NumElements;
337
338public:
339 NodeArray() : Elements(nullptr), NumElements(0) {}
340 NodeArray(Node **Elements_, size_t NumElements_)
341 : Elements(Elements_), NumElements(NumElements_) {}
342
343 bool empty() const { return NumElements == 0; }
344 size_t size() const { return NumElements; }
345
346 Node **begin() const { return Elements; }
347 Node **end() const { return Elements + NumElements; }
348
349 Node *operator[](size_t Idx) const { return Elements[Idx]; }
350
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700351 void printWithComma(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000352 bool FirstElement = true;
353 for (size_t Idx = 0; Idx != NumElements; ++Idx) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700354 size_t BeforeComma = OB.getCurrentPosition();
Richard Smithc20d1442018-08-20 20:14:49 +0000355 if (!FirstElement)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700356 OB += ", ";
357 size_t AfterComma = OB.getCurrentPosition();
358 Elements[Idx]->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000359
360 // Elements[Idx] is an empty parameter pack expansion, we should erase the
361 // comma we just printed.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700362 if (AfterComma == OB.getCurrentPosition()) {
363 OB.setCurrentPosition(BeforeComma);
Richard Smithc20d1442018-08-20 20:14:49 +0000364 continue;
365 }
366
367 FirstElement = false;
368 }
369 }
370};
371
372struct NodeArrayNode : Node {
373 NodeArray Array;
374 NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
375
376 template<typename Fn> void match(Fn F) const { F(Array); }
377
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700378 void printLeft(OutputBuffer &OB) const override { Array.printWithComma(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000379};
380
381class DotSuffix final : public Node {
382 const Node *Prefix;
383 const StringView Suffix;
384
385public:
386 DotSuffix(const Node *Prefix_, StringView Suffix_)
387 : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
388
389 template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
390
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700391 void printLeft(OutputBuffer &OB) const override {
392 Prefix->print(OB);
393 OB += " (";
394 OB += Suffix;
395 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000396 }
397};
398
399class VendorExtQualType final : public Node {
400 const Node *Ty;
401 StringView Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400402 const Node *TA;
Richard Smithc20d1442018-08-20 20:14:49 +0000403
404public:
Alex Orlovf50df922021-03-24 10:21:32 +0400405 VendorExtQualType(const Node *Ty_, StringView Ext_, const Node *TA_)
406 : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_), TA(TA_) {}
Richard Smithc20d1442018-08-20 20:14:49 +0000407
Alex Orlovf50df922021-03-24 10:21:32 +0400408 template <typename Fn> void match(Fn F) const { F(Ty, Ext, TA); }
Richard Smithc20d1442018-08-20 20:14:49 +0000409
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700410 void printLeft(OutputBuffer &OB) const override {
411 Ty->print(OB);
412 OB += " ";
413 OB += Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400414 if (TA != nullptr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700415 TA->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000416 }
417};
418
419enum FunctionRefQual : unsigned char {
420 FrefQualNone,
421 FrefQualLValue,
422 FrefQualRValue,
423};
424
425enum Qualifiers {
426 QualNone = 0,
427 QualConst = 0x1,
428 QualVolatile = 0x2,
429 QualRestrict = 0x4,
430};
431
432inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
433 return Q1 = static_cast<Qualifiers>(Q1 | Q2);
434}
435
Richard Smithdf1c14c2019-09-06 23:53:21 +0000436class QualType final : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +0000437protected:
438 const Qualifiers Quals;
439 const Node *Child;
440
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700441 void printQuals(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000442 if (Quals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700443 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000444 if (Quals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700445 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000446 if (Quals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700447 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000448 }
449
450public:
451 QualType(const Node *Child_, Qualifiers Quals_)
452 : Node(KQualType, Child_->RHSComponentCache,
453 Child_->ArrayCache, Child_->FunctionCache),
454 Quals(Quals_), Child(Child_) {}
455
456 template<typename Fn> void match(Fn F) const { F(Child, Quals); }
457
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700458 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
459 return Child->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000460 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700461 bool hasArraySlow(OutputBuffer &OB) const override {
462 return Child->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000463 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700464 bool hasFunctionSlow(OutputBuffer &OB) const override {
465 return Child->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000466 }
467
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700468 void printLeft(OutputBuffer &OB) const override {
469 Child->printLeft(OB);
470 printQuals(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000471 }
472
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700473 void printRight(OutputBuffer &OB) const override { Child->printRight(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000474};
475
476class ConversionOperatorType final : public Node {
477 const Node *Ty;
478
479public:
480 ConversionOperatorType(const Node *Ty_)
481 : Node(KConversionOperatorType), Ty(Ty_) {}
482
483 template<typename Fn> void match(Fn F) const { F(Ty); }
484
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700485 void printLeft(OutputBuffer &OB) const override {
486 OB += "operator ";
487 Ty->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000488 }
489};
490
491class PostfixQualifiedType final : public Node {
492 const Node *Ty;
493 const StringView Postfix;
494
495public:
496 PostfixQualifiedType(Node *Ty_, StringView Postfix_)
497 : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
498
499 template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
500
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700501 void printLeft(OutputBuffer &OB) const override {
502 Ty->printLeft(OB);
503 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +0000504 }
505};
506
507class NameType final : public Node {
508 const StringView Name;
509
510public:
511 NameType(StringView Name_) : Node(KNameType), Name(Name_) {}
512
513 template<typename Fn> void match(Fn F) const { F(Name); }
514
515 StringView getName() const { return Name; }
516 StringView getBaseName() const override { return Name; }
517
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700518 void printLeft(OutputBuffer &OB) const override { OB += Name; }
Richard Smithc20d1442018-08-20 20:14:49 +0000519};
520
521class ElaboratedTypeSpefType : public Node {
522 StringView Kind;
523 Node *Child;
524public:
525 ElaboratedTypeSpefType(StringView Kind_, Node *Child_)
526 : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
527
528 template<typename Fn> void match(Fn F) const { F(Kind, Child); }
529
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700530 void printLeft(OutputBuffer &OB) const override {
531 OB += Kind;
532 OB += ' ';
533 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000534 }
535};
536
537struct AbiTagAttr : Node {
538 Node *Base;
539 StringView Tag;
540
541 AbiTagAttr(Node* Base_, StringView Tag_)
542 : Node(KAbiTagAttr, Base_->RHSComponentCache,
543 Base_->ArrayCache, Base_->FunctionCache),
544 Base(Base_), Tag(Tag_) {}
545
546 template<typename Fn> void match(Fn F) const { F(Base, Tag); }
547
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700548 void printLeft(OutputBuffer &OB) const override {
549 Base->printLeft(OB);
550 OB += "[abi:";
551 OB += Tag;
552 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +0000553 }
554};
555
556class EnableIfAttr : public Node {
557 NodeArray Conditions;
558public:
559 EnableIfAttr(NodeArray Conditions_)
560 : Node(KEnableIfAttr), Conditions(Conditions_) {}
561
562 template<typename Fn> void match(Fn F) const { F(Conditions); }
563
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700564 void printLeft(OutputBuffer &OB) const override {
565 OB += " [enable_if:";
566 Conditions.printWithComma(OB);
567 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +0000568 }
569};
570
571class ObjCProtoName : public Node {
572 const Node *Ty;
573 StringView Protocol;
574
575 friend class PointerType;
576
577public:
578 ObjCProtoName(const Node *Ty_, StringView Protocol_)
579 : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
580
581 template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
582
583 bool isObjCObject() const {
584 return Ty->getKind() == KNameType &&
585 static_cast<const NameType *>(Ty)->getName() == "objc_object";
586 }
587
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700588 void printLeft(OutputBuffer &OB) const override {
589 Ty->print(OB);
590 OB += "<";
591 OB += Protocol;
592 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +0000593 }
594};
595
596class PointerType final : public Node {
597 const Node *Pointee;
598
599public:
600 PointerType(const Node *Pointee_)
601 : Node(KPointerType, Pointee_->RHSComponentCache),
602 Pointee(Pointee_) {}
603
604 template<typename Fn> void match(Fn F) const { F(Pointee); }
605
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700606 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
607 return Pointee->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000608 }
609
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700610 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000611 // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
612 if (Pointee->getKind() != KObjCProtoName ||
613 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700614 Pointee->printLeft(OB);
615 if (Pointee->hasArray(OB))
616 OB += " ";
617 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
618 OB += "(";
619 OB += "*";
Richard Smithc20d1442018-08-20 20:14:49 +0000620 } else {
621 const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700622 OB += "id<";
623 OB += objcProto->Protocol;
624 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +0000625 }
626 }
627
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700628 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000629 if (Pointee->getKind() != KObjCProtoName ||
630 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700631 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
632 OB += ")";
633 Pointee->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000634 }
635 }
636};
637
638enum class ReferenceKind {
639 LValue,
640 RValue,
641};
642
643// Represents either a LValue or an RValue reference type.
644class ReferenceType : public Node {
645 const Node *Pointee;
646 ReferenceKind RK;
647
648 mutable bool Printing = false;
649
650 // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
651 // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
652 // other combination collapses to a lvalue ref.
Mikhail Borisov05f77222021-08-17 18:10:57 -0400653 //
654 // A combination of a TemplateForwardReference and a back-ref Substitution
655 // from an ill-formed string may have created a cycle; use cycle detection to
656 // avoid looping forever.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700657 std::pair<ReferenceKind, const Node *> collapse(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000658 auto SoFar = std::make_pair(RK, Pointee);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400659 // Track the chain of nodes for the Floyd's 'tortoise and hare'
660 // cycle-detection algorithm, since getSyntaxNode(S) is impure
661 PODSmallVector<const Node *, 8> Prev;
Richard Smithc20d1442018-08-20 20:14:49 +0000662 for (;;) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700663 const Node *SN = SoFar.second->getSyntaxNode(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000664 if (SN->getKind() != KReferenceType)
665 break;
666 auto *RT = static_cast<const ReferenceType *>(SN);
667 SoFar.second = RT->Pointee;
668 SoFar.first = std::min(SoFar.first, RT->RK);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400669
670 // The middle of Prev is the 'slow' pointer moving at half speed
671 Prev.push_back(SoFar.second);
672 if (Prev.size() > 1 && SoFar.second == Prev[(Prev.size() - 1) / 2]) {
673 // Cycle detected
674 SoFar.second = nullptr;
675 break;
676 }
Richard Smithc20d1442018-08-20 20:14:49 +0000677 }
678 return SoFar;
679 }
680
681public:
682 ReferenceType(const Node *Pointee_, ReferenceKind RK_)
683 : Node(KReferenceType, Pointee_->RHSComponentCache),
684 Pointee(Pointee_), RK(RK_) {}
685
686 template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
687
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700688 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
689 return Pointee->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000690 }
691
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700692 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000693 if (Printing)
694 return;
695 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700696 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400697 if (!Collapsed.second)
698 return;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700699 Collapsed.second->printLeft(OB);
700 if (Collapsed.second->hasArray(OB))
701 OB += " ";
702 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
703 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +0000704
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700705 OB += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
Richard Smithc20d1442018-08-20 20:14:49 +0000706 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700707 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000708 if (Printing)
709 return;
710 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700711 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400712 if (!Collapsed.second)
713 return;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700714 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
715 OB += ")";
716 Collapsed.second->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000717 }
718};
719
720class PointerToMemberType final : public Node {
721 const Node *ClassType;
722 const Node *MemberType;
723
724public:
725 PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
726 : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
727 ClassType(ClassType_), MemberType(MemberType_) {}
728
729 template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
730
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700731 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
732 return MemberType->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000733 }
734
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700735 void printLeft(OutputBuffer &OB) const override {
736 MemberType->printLeft(OB);
737 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
738 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +0000739 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700740 OB += " ";
741 ClassType->print(OB);
742 OB += "::*";
Richard Smithc20d1442018-08-20 20:14:49 +0000743 }
744
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700745 void printRight(OutputBuffer &OB) const override {
746 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
747 OB += ")";
748 MemberType->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000749 }
750};
751
Richard Smithc20d1442018-08-20 20:14:49 +0000752class ArrayType final : public Node {
753 const Node *Base;
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800754 Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +0000755
756public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800757 ArrayType(const Node *Base_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +0000758 : Node(KArrayType,
759 /*RHSComponentCache=*/Cache::Yes,
760 /*ArrayCache=*/Cache::Yes),
761 Base(Base_), Dimension(Dimension_) {}
762
763 template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
764
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700765 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
766 bool hasArraySlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000767
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700768 void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000769
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700770 void printRight(OutputBuffer &OB) const override {
771 if (OB.back() != ']')
772 OB += " ";
773 OB += "[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800774 if (Dimension)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700775 Dimension->print(OB);
776 OB += "]";
777 Base->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000778 }
779};
780
781class FunctionType final : public Node {
782 const Node *Ret;
783 NodeArray Params;
784 Qualifiers CVQuals;
785 FunctionRefQual RefQual;
786 const Node *ExceptionSpec;
787
788public:
789 FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
790 FunctionRefQual RefQual_, const Node *ExceptionSpec_)
791 : Node(KFunctionType,
792 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
793 /*FunctionCache=*/Cache::Yes),
794 Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
795 ExceptionSpec(ExceptionSpec_) {}
796
797 template<typename Fn> void match(Fn F) const {
798 F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
799 }
800
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700801 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
802 bool hasFunctionSlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000803
804 // Handle C++'s ... quirky decl grammar by using the left & right
805 // distinction. Consider:
806 // int (*f(float))(char) {}
807 // f is a function that takes a float and returns a pointer to a function
808 // that takes a char and returns an int. If we're trying to print f, start
809 // by printing out the return types's left, then print our parameters, then
810 // finally print right of the return type.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700811 void printLeft(OutputBuffer &OB) const override {
812 Ret->printLeft(OB);
813 OB += " ";
Richard Smithc20d1442018-08-20 20:14:49 +0000814 }
815
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700816 void printRight(OutputBuffer &OB) const override {
817 OB += "(";
818 Params.printWithComma(OB);
819 OB += ")";
820 Ret->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000821
822 if (CVQuals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700823 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000824 if (CVQuals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700825 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000826 if (CVQuals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700827 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000828
829 if (RefQual == FrefQualLValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700830 OB += " &";
Richard Smithc20d1442018-08-20 20:14:49 +0000831 else if (RefQual == FrefQualRValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700832 OB += " &&";
Richard Smithc20d1442018-08-20 20:14:49 +0000833
834 if (ExceptionSpec != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700835 OB += ' ';
836 ExceptionSpec->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000837 }
838 }
839};
840
841class NoexceptSpec : public Node {
842 const Node *E;
843public:
844 NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
845
846 template<typename Fn> void match(Fn F) const { F(E); }
847
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700848 void printLeft(OutputBuffer &OB) const override {
849 OB += "noexcept(";
850 E->print(OB);
851 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000852 }
853};
854
855class DynamicExceptionSpec : public Node {
856 NodeArray Types;
857public:
858 DynamicExceptionSpec(NodeArray Types_)
859 : Node(KDynamicExceptionSpec), Types(Types_) {}
860
861 template<typename Fn> void match(Fn F) const { F(Types); }
862
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700863 void printLeft(OutputBuffer &OB) const override {
864 OB += "throw(";
865 Types.printWithComma(OB);
866 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +0000867 }
868};
869
870class FunctionEncoding final : public Node {
871 const Node *Ret;
872 const Node *Name;
873 NodeArray Params;
874 const Node *Attrs;
875 Qualifiers CVQuals;
876 FunctionRefQual RefQual;
877
878public:
879 FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
880 const Node *Attrs_, Qualifiers CVQuals_,
881 FunctionRefQual RefQual_)
882 : Node(KFunctionEncoding,
883 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
884 /*FunctionCache=*/Cache::Yes),
885 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
886 CVQuals(CVQuals_), RefQual(RefQual_) {}
887
888 template<typename Fn> void match(Fn F) const {
889 F(Ret, Name, Params, Attrs, CVQuals, RefQual);
890 }
891
892 Qualifiers getCVQuals() const { return CVQuals; }
893 FunctionRefQual getRefQual() const { return RefQual; }
894 NodeArray getParams() const { return Params; }
895 const Node *getReturnType() const { return Ret; }
896
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700897 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
898 bool hasFunctionSlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000899
900 const Node *getName() const { return Name; }
901
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700902 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000903 if (Ret) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700904 Ret->printLeft(OB);
905 if (!Ret->hasRHSComponent(OB))
906 OB += " ";
Richard Smithc20d1442018-08-20 20:14:49 +0000907 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700908 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000909 }
910
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700911 void printRight(OutputBuffer &OB) const override {
912 OB += "(";
913 Params.printWithComma(OB);
914 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000915 if (Ret)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700916 Ret->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000917
918 if (CVQuals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700919 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000920 if (CVQuals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700921 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000922 if (CVQuals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700923 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000924
925 if (RefQual == FrefQualLValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700926 OB += " &";
Richard Smithc20d1442018-08-20 20:14:49 +0000927 else if (RefQual == FrefQualRValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700928 OB += " &&";
Richard Smithc20d1442018-08-20 20:14:49 +0000929
930 if (Attrs != nullptr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700931 Attrs->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000932 }
933};
934
935class LiteralOperator : public Node {
936 const Node *OpName;
937
938public:
939 LiteralOperator(const Node *OpName_)
940 : Node(KLiteralOperator), OpName(OpName_) {}
941
942 template<typename Fn> void match(Fn F) const { F(OpName); }
943
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700944 void printLeft(OutputBuffer &OB) const override {
945 OB += "operator\"\" ";
946 OpName->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000947 }
948};
949
950class SpecialName final : public Node {
951 const StringView Special;
952 const Node *Child;
953
954public:
955 SpecialName(StringView Special_, const Node *Child_)
956 : Node(KSpecialName), Special(Special_), Child(Child_) {}
957
958 template<typename Fn> void match(Fn F) const { F(Special, Child); }
959
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700960 void printLeft(OutputBuffer &OB) const override {
961 OB += Special;
962 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000963 }
964};
965
966class CtorVtableSpecialName final : public Node {
967 const Node *FirstType;
968 const Node *SecondType;
969
970public:
971 CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
972 : Node(KCtorVtableSpecialName),
973 FirstType(FirstType_), SecondType(SecondType_) {}
974
975 template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
976
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700977 void printLeft(OutputBuffer &OB) const override {
978 OB += "construction vtable for ";
979 FirstType->print(OB);
980 OB += "-in-";
981 SecondType->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000982 }
983};
984
985struct NestedName : Node {
986 Node *Qual;
987 Node *Name;
988
989 NestedName(Node *Qual_, Node *Name_)
990 : Node(KNestedName), Qual(Qual_), Name(Name_) {}
991
992 template<typename Fn> void match(Fn F) const { F(Qual, Name); }
993
994 StringView getBaseName() const override { return Name->getBaseName(); }
995
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700996 void printLeft(OutputBuffer &OB) const override {
997 Qual->print(OB);
998 OB += "::";
999 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001000 }
1001};
1002
1003struct LocalName : Node {
1004 Node *Encoding;
1005 Node *Entity;
1006
1007 LocalName(Node *Encoding_, Node *Entity_)
1008 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
1009
1010 template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
1011
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001012 void printLeft(OutputBuffer &OB) const override {
1013 Encoding->print(OB);
1014 OB += "::";
1015 Entity->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001016 }
1017};
1018
1019class QualifiedName final : public Node {
1020 // qualifier::name
1021 const Node *Qualifier;
1022 const Node *Name;
1023
1024public:
1025 QualifiedName(const Node *Qualifier_, const Node *Name_)
1026 : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
1027
1028 template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
1029
1030 StringView getBaseName() const override { return Name->getBaseName(); }
1031
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001032 void printLeft(OutputBuffer &OB) const override {
1033 Qualifier->print(OB);
1034 OB += "::";
1035 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001036 }
1037};
1038
1039class VectorType final : public Node {
1040 const Node *BaseType;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001041 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001042
1043public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001044 VectorType(const Node *BaseType_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001045 : Node(KVectorType), BaseType(BaseType_),
1046 Dimension(Dimension_) {}
1047
1048 template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
1049
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001050 void printLeft(OutputBuffer &OB) const override {
1051 BaseType->print(OB);
1052 OB += " vector[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001053 if (Dimension)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001054 Dimension->print(OB);
1055 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001056 }
1057};
1058
1059class PixelVectorType final : public Node {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001060 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001061
1062public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001063 PixelVectorType(const Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001064 : Node(KPixelVectorType), Dimension(Dimension_) {}
1065
1066 template<typename Fn> void match(Fn F) const { F(Dimension); }
1067
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001068 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001069 // FIXME: This should demangle as "vector pixel".
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001070 OB += "pixel vector[";
1071 Dimension->print(OB);
1072 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001073 }
1074};
1075
Pengfei Wang50e90b82021-09-23 11:02:25 +08001076class BinaryFPType final : public Node {
1077 const Node *Dimension;
1078
1079public:
1080 BinaryFPType(const Node *Dimension_)
1081 : Node(KBinaryFPType), Dimension(Dimension_) {}
1082
1083 template<typename Fn> void match(Fn F) const { F(Dimension); }
1084
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001085 void printLeft(OutputBuffer &OB) const override {
1086 OB += "_Float";
1087 Dimension->print(OB);
Pengfei Wang50e90b82021-09-23 11:02:25 +08001088 }
1089};
1090
Richard Smithdf1c14c2019-09-06 23:53:21 +00001091enum class TemplateParamKind { Type, NonType, Template };
1092
1093/// An invented name for a template parameter for which we don't have a
1094/// corresponding template argument.
1095///
1096/// This node is created when parsing the <lambda-sig> for a lambda with
1097/// explicit template arguments, which might be referenced in the parameter
1098/// types appearing later in the <lambda-sig>.
1099class SyntheticTemplateParamName final : public Node {
1100 TemplateParamKind Kind;
1101 unsigned Index;
1102
1103public:
1104 SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_)
1105 : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {}
1106
1107 template<typename Fn> void match(Fn F) const { F(Kind, Index); }
1108
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001109 void printLeft(OutputBuffer &OB) const override {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001110 switch (Kind) {
1111 case TemplateParamKind::Type:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001112 OB += "$T";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001113 break;
1114 case TemplateParamKind::NonType:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001115 OB += "$N";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001116 break;
1117 case TemplateParamKind::Template:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001118 OB += "$TT";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001119 break;
1120 }
1121 if (Index > 0)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001122 OB << Index - 1;
Richard Smithdf1c14c2019-09-06 23:53:21 +00001123 }
1124};
1125
1126/// A template type parameter declaration, 'typename T'.
1127class TypeTemplateParamDecl final : public Node {
1128 Node *Name;
1129
1130public:
1131 TypeTemplateParamDecl(Node *Name_)
1132 : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {}
1133
1134 template<typename Fn> void match(Fn F) const { F(Name); }
1135
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001136 void printLeft(OutputBuffer &OB) const override { OB += "typename "; }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001137
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001138 void printRight(OutputBuffer &OB) const override { Name->print(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001139};
1140
1141/// A non-type template parameter declaration, 'int N'.
1142class NonTypeTemplateParamDecl final : public Node {
1143 Node *Name;
1144 Node *Type;
1145
1146public:
1147 NonTypeTemplateParamDecl(Node *Name_, Node *Type_)
1148 : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {}
1149
1150 template<typename Fn> void match(Fn F) const { F(Name, Type); }
1151
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001152 void printLeft(OutputBuffer &OB) const override {
1153 Type->printLeft(OB);
1154 if (!Type->hasRHSComponent(OB))
1155 OB += " ";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001156 }
1157
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001158 void printRight(OutputBuffer &OB) const override {
1159 Name->print(OB);
1160 Type->printRight(OB);
Richard Smithdf1c14c2019-09-06 23:53:21 +00001161 }
1162};
1163
1164/// A template template parameter declaration,
1165/// 'template<typename T> typename N'.
1166class TemplateTemplateParamDecl final : public Node {
1167 Node *Name;
1168 NodeArray Params;
1169
1170public:
1171 TemplateTemplateParamDecl(Node *Name_, NodeArray Params_)
1172 : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_),
1173 Params(Params_) {}
1174
1175 template<typename Fn> void match(Fn F) const { F(Name, Params); }
1176
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001177 void printLeft(OutputBuffer &OB) const override {
1178 OB += "template<";
1179 Params.printWithComma(OB);
1180 OB += "> typename ";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001181 }
1182
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001183 void printRight(OutputBuffer &OB) const override { Name->print(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001184};
1185
1186/// A template parameter pack declaration, 'typename ...T'.
1187class TemplateParamPackDecl final : public Node {
1188 Node *Param;
1189
1190public:
1191 TemplateParamPackDecl(Node *Param_)
1192 : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {}
1193
1194 template<typename Fn> void match(Fn F) const { F(Param); }
1195
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001196 void printLeft(OutputBuffer &OB) const override {
1197 Param->printLeft(OB);
1198 OB += "...";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001199 }
1200
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001201 void printRight(OutputBuffer &OB) const override { Param->printRight(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001202};
1203
Richard Smithc20d1442018-08-20 20:14:49 +00001204/// An unexpanded parameter pack (either in the expression or type context). If
1205/// this AST is correct, this node will have a ParameterPackExpansion node above
1206/// it.
1207///
1208/// This node is created when some <template-args> are found that apply to an
1209/// <encoding>, and is stored in the TemplateParams table. In order for this to
1210/// appear in the final AST, it has to referenced via a <template-param> (ie,
1211/// T_).
1212class ParameterPack final : public Node {
1213 NodeArray Data;
1214
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08001215 // Setup OutputBuffer for a pack expansion, unless we're already expanding
1216 // one.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001217 void initializePackExpansion(OutputBuffer &OB) const {
1218 if (OB.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
1219 OB.CurrentPackMax = static_cast<unsigned>(Data.size());
1220 OB.CurrentPackIndex = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00001221 }
1222 }
1223
1224public:
1225 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
1226 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
1227 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1228 return P->ArrayCache == Cache::No;
1229 }))
1230 ArrayCache = Cache::No;
1231 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1232 return P->FunctionCache == Cache::No;
1233 }))
1234 FunctionCache = Cache::No;
1235 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1236 return P->RHSComponentCache == Cache::No;
1237 }))
1238 RHSComponentCache = Cache::No;
1239 }
1240
1241 template<typename Fn> void match(Fn F) const { F(Data); }
1242
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001243 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
1244 initializePackExpansion(OB);
1245 size_t Idx = OB.CurrentPackIndex;
1246 return Idx < Data.size() && Data[Idx]->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001247 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001248 bool hasArraySlow(OutputBuffer &OB) const override {
1249 initializePackExpansion(OB);
1250 size_t Idx = OB.CurrentPackIndex;
1251 return Idx < Data.size() && Data[Idx]->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001252 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001253 bool hasFunctionSlow(OutputBuffer &OB) const override {
1254 initializePackExpansion(OB);
1255 size_t Idx = OB.CurrentPackIndex;
1256 return Idx < Data.size() && Data[Idx]->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001257 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001258 const Node *getSyntaxNode(OutputBuffer &OB) const override {
1259 initializePackExpansion(OB);
1260 size_t Idx = OB.CurrentPackIndex;
1261 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(OB) : this;
Richard Smithc20d1442018-08-20 20:14:49 +00001262 }
1263
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001264 void printLeft(OutputBuffer &OB) const override {
1265 initializePackExpansion(OB);
1266 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001267 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001268 Data[Idx]->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001269 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001270 void printRight(OutputBuffer &OB) const override {
1271 initializePackExpansion(OB);
1272 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001273 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001274 Data[Idx]->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001275 }
1276};
1277
1278/// A variadic template argument. This node represents an occurrence of
1279/// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1280/// one of it's Elements is. The parser inserts a ParameterPack into the
1281/// TemplateParams table if the <template-args> this pack belongs to apply to an
1282/// <encoding>.
1283class TemplateArgumentPack final : public Node {
1284 NodeArray Elements;
1285public:
1286 TemplateArgumentPack(NodeArray Elements_)
1287 : Node(KTemplateArgumentPack), Elements(Elements_) {}
1288
1289 template<typename Fn> void match(Fn F) const { F(Elements); }
1290
1291 NodeArray getElements() const { return Elements; }
1292
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001293 void printLeft(OutputBuffer &OB) const override {
1294 Elements.printWithComma(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001295 }
1296};
1297
1298/// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1299/// which each have Child->ParameterPackSize elements.
1300class ParameterPackExpansion final : public Node {
1301 const Node *Child;
1302
1303public:
1304 ParameterPackExpansion(const Node *Child_)
1305 : Node(KParameterPackExpansion), Child(Child_) {}
1306
1307 template<typename Fn> void match(Fn F) const { F(Child); }
1308
1309 const Node *getChild() const { return Child; }
1310
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001311 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001312 constexpr unsigned Max = std::numeric_limits<unsigned>::max();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001313 SwapAndRestore<unsigned> SavePackIdx(OB.CurrentPackIndex, Max);
1314 SwapAndRestore<unsigned> SavePackMax(OB.CurrentPackMax, Max);
1315 size_t StreamPos = OB.getCurrentPosition();
Richard Smithc20d1442018-08-20 20:14:49 +00001316
1317 // Print the first element in the pack. If Child contains a ParameterPack,
1318 // it will set up S.CurrentPackMax and print the first element.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001319 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001320
1321 // No ParameterPack was found in Child. This can occur if we've found a pack
1322 // expansion on a <function-param>.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001323 if (OB.CurrentPackMax == Max) {
1324 OB += "...";
Richard Smithc20d1442018-08-20 20:14:49 +00001325 return;
1326 }
1327
1328 // We found a ParameterPack, but it has no elements. Erase whatever we may
1329 // of printed.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001330 if (OB.CurrentPackMax == 0) {
1331 OB.setCurrentPosition(StreamPos);
Richard Smithc20d1442018-08-20 20:14:49 +00001332 return;
1333 }
1334
1335 // Else, iterate through the rest of the elements in the pack.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001336 for (unsigned I = 1, E = OB.CurrentPackMax; I < E; ++I) {
1337 OB += ", ";
1338 OB.CurrentPackIndex = I;
1339 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001340 }
1341 }
1342};
1343
1344class TemplateArgs final : public Node {
1345 NodeArray Params;
1346
1347public:
1348 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1349
1350 template<typename Fn> void match(Fn F) const { F(Params); }
1351
1352 NodeArray getParams() { return Params; }
1353
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001354 void printLeft(OutputBuffer &OB) const override {
1355 OB += "<";
1356 Params.printWithComma(OB);
1357 if (OB.back() == '>')
1358 OB += " ";
1359 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +00001360 }
1361};
1362
Richard Smithb485b352018-08-24 23:30:26 +00001363/// A forward-reference to a template argument that was not known at the point
1364/// where the template parameter name was parsed in a mangling.
1365///
1366/// This is created when demangling the name of a specialization of a
1367/// conversion function template:
1368///
1369/// \code
1370/// struct A {
1371/// template<typename T> operator T*();
1372/// };
1373/// \endcode
1374///
1375/// When demangling a specialization of the conversion function template, we
1376/// encounter the name of the template (including the \c T) before we reach
1377/// the template argument list, so we cannot substitute the parameter name
1378/// for the corresponding argument while parsing. Instead, we create a
1379/// \c ForwardTemplateReference node that is resolved after we parse the
1380/// template arguments.
Richard Smithc20d1442018-08-20 20:14:49 +00001381struct ForwardTemplateReference : Node {
1382 size_t Index;
1383 Node *Ref = nullptr;
1384
1385 // If we're currently printing this node. It is possible (though invalid) for
1386 // a forward template reference to refer to itself via a substitution. This
1387 // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1388 // out if more than one print* function is active.
1389 mutable bool Printing = false;
1390
1391 ForwardTemplateReference(size_t Index_)
1392 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1393 Cache::Unknown),
1394 Index(Index_) {}
1395
1396 // We don't provide a matcher for these, because the value of the node is
1397 // not determined by its construction parameters, and it generally needs
1398 // special handling.
1399 template<typename Fn> void match(Fn F) const = delete;
1400
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001401 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001402 if (Printing)
1403 return false;
1404 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001405 return Ref->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001406 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001407 bool hasArraySlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001408 if (Printing)
1409 return false;
1410 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001411 return Ref->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001412 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001413 bool hasFunctionSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001414 if (Printing)
1415 return false;
1416 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001417 return Ref->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001418 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001419 const Node *getSyntaxNode(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001420 if (Printing)
1421 return this;
1422 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001423 return Ref->getSyntaxNode(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001424 }
1425
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001426 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001427 if (Printing)
1428 return;
1429 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001430 Ref->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001431 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001432 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001433 if (Printing)
1434 return;
1435 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001436 Ref->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001437 }
1438};
1439
1440struct NameWithTemplateArgs : Node {
1441 // name<template_args>
1442 Node *Name;
1443 Node *TemplateArgs;
1444
1445 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1446 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1447
1448 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1449
1450 StringView getBaseName() const override { return Name->getBaseName(); }
1451
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001452 void printLeft(OutputBuffer &OB) const override {
1453 Name->print(OB);
1454 TemplateArgs->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001455 }
1456};
1457
1458class GlobalQualifiedName final : public Node {
1459 Node *Child;
1460
1461public:
1462 GlobalQualifiedName(Node* Child_)
1463 : Node(KGlobalQualifiedName), Child(Child_) {}
1464
1465 template<typename Fn> void match(Fn F) const { F(Child); }
1466
1467 StringView getBaseName() const override { return Child->getBaseName(); }
1468
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001469 void printLeft(OutputBuffer &OB) const override {
1470 OB += "::";
1471 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001472 }
1473};
1474
Richard Smithc20d1442018-08-20 20:14:49 +00001475enum class SpecialSubKind {
1476 allocator,
1477 basic_string,
1478 string,
1479 istream,
1480 ostream,
1481 iostream,
1482};
1483
1484class ExpandedSpecialSubstitution final : public Node {
1485 SpecialSubKind SSK;
1486
1487public:
1488 ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1489 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1490
1491 template<typename Fn> void match(Fn F) const { F(SSK); }
1492
1493 StringView getBaseName() const override {
1494 switch (SSK) {
1495 case SpecialSubKind::allocator:
1496 return StringView("allocator");
1497 case SpecialSubKind::basic_string:
1498 return StringView("basic_string");
1499 case SpecialSubKind::string:
1500 return StringView("basic_string");
1501 case SpecialSubKind::istream:
1502 return StringView("basic_istream");
1503 case SpecialSubKind::ostream:
1504 return StringView("basic_ostream");
1505 case SpecialSubKind::iostream:
1506 return StringView("basic_iostream");
1507 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001508 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001509 }
1510
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001511 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001512 switch (SSK) {
1513 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001514 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001515 break;
1516 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001517 OB += "std::basic_string";
Richard Smithb485b352018-08-24 23:30:26 +00001518 break;
Richard Smithc20d1442018-08-20 20:14:49 +00001519 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001520 OB += "std::basic_string<char, std::char_traits<char>, "
1521 "std::allocator<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001522 break;
1523 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001524 OB += "std::basic_istream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001525 break;
1526 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001527 OB += "std::basic_ostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001528 break;
1529 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001530 OB += "std::basic_iostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001531 break;
1532 }
1533 }
1534};
1535
1536class SpecialSubstitution final : public Node {
1537public:
1538 SpecialSubKind SSK;
1539
1540 SpecialSubstitution(SpecialSubKind SSK_)
1541 : Node(KSpecialSubstitution), SSK(SSK_) {}
1542
1543 template<typename Fn> void match(Fn F) const { F(SSK); }
1544
1545 StringView getBaseName() const override {
1546 switch (SSK) {
1547 case SpecialSubKind::allocator:
1548 return StringView("allocator");
1549 case SpecialSubKind::basic_string:
1550 return StringView("basic_string");
1551 case SpecialSubKind::string:
1552 return StringView("string");
1553 case SpecialSubKind::istream:
1554 return StringView("istream");
1555 case SpecialSubKind::ostream:
1556 return StringView("ostream");
1557 case SpecialSubKind::iostream:
1558 return StringView("iostream");
1559 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001560 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001561 }
1562
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001563 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001564 switch (SSK) {
1565 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001566 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001567 break;
1568 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001569 OB += "std::basic_string";
Richard Smithc20d1442018-08-20 20:14:49 +00001570 break;
1571 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001572 OB += "std::string";
Richard Smithc20d1442018-08-20 20:14:49 +00001573 break;
1574 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001575 OB += "std::istream";
Richard Smithc20d1442018-08-20 20:14:49 +00001576 break;
1577 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001578 OB += "std::ostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001579 break;
1580 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001581 OB += "std::iostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001582 break;
1583 }
1584 }
1585};
1586
1587class CtorDtorName final : public Node {
1588 const Node *Basename;
1589 const bool IsDtor;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001590 const int Variant;
Richard Smithc20d1442018-08-20 20:14:49 +00001591
1592public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001593 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1594 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1595 Variant(Variant_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001596
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001597 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
Richard Smithc20d1442018-08-20 20:14:49 +00001598
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001599 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001600 if (IsDtor)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001601 OB += "~";
1602 OB += Basename->getBaseName();
Richard Smithc20d1442018-08-20 20:14:49 +00001603 }
1604};
1605
1606class DtorName : public Node {
1607 const Node *Base;
1608
1609public:
1610 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1611
1612 template<typename Fn> void match(Fn F) const { F(Base); }
1613
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001614 void printLeft(OutputBuffer &OB) const override {
1615 OB += "~";
1616 Base->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001617 }
1618};
1619
1620class UnnamedTypeName : public Node {
1621 const StringView Count;
1622
1623public:
1624 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1625
1626 template<typename Fn> void match(Fn F) const { F(Count); }
1627
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001628 void printLeft(OutputBuffer &OB) const override {
1629 OB += "'unnamed";
1630 OB += Count;
1631 OB += "\'";
Richard Smithc20d1442018-08-20 20:14:49 +00001632 }
1633};
1634
1635class ClosureTypeName : public Node {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001636 NodeArray TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00001637 NodeArray Params;
1638 StringView Count;
1639
1640public:
Richard Smithdf1c14c2019-09-06 23:53:21 +00001641 ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_,
1642 StringView Count_)
1643 : Node(KClosureTypeName), TemplateParams(TemplateParams_),
1644 Params(Params_), Count(Count_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001645
Richard Smithdf1c14c2019-09-06 23:53:21 +00001646 template<typename Fn> void match(Fn F) const {
1647 F(TemplateParams, Params, Count);
1648 }
1649
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001650 void printDeclarator(OutputBuffer &OB) const {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001651 if (!TemplateParams.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001652 OB += "<";
1653 TemplateParams.printWithComma(OB);
1654 OB += ">";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001655 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001656 OB += "(";
1657 Params.printWithComma(OB);
1658 OB += ")";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001659 }
Richard Smithc20d1442018-08-20 20:14:49 +00001660
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001661 void printLeft(OutputBuffer &OB) const override {
1662 OB += "\'lambda";
1663 OB += Count;
1664 OB += "\'";
1665 printDeclarator(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001666 }
1667};
1668
1669class StructuredBindingName : public Node {
1670 NodeArray Bindings;
1671public:
1672 StructuredBindingName(NodeArray Bindings_)
1673 : Node(KStructuredBindingName), Bindings(Bindings_) {}
1674
1675 template<typename Fn> void match(Fn F) const { F(Bindings); }
1676
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001677 void printLeft(OutputBuffer &OB) const override {
1678 OB += '[';
1679 Bindings.printWithComma(OB);
1680 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00001681 }
1682};
1683
1684// -- Expression Nodes --
1685
1686class BinaryExpr : public Node {
1687 const Node *LHS;
1688 const StringView InfixOperator;
1689 const Node *RHS;
1690
1691public:
1692 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
1693 : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
1694 }
1695
1696 template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
1697
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001698 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001699 // might be a template argument expression, then we need to disambiguate
1700 // with parens.
1701 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001702 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +00001703
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001704 OB += "(";
1705 LHS->print(OB);
1706 OB += ") ";
1707 OB += InfixOperator;
1708 OB += " (";
1709 RHS->print(OB);
1710 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001711
1712 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001713 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001714 }
1715};
1716
1717class ArraySubscriptExpr : public Node {
1718 const Node *Op1;
1719 const Node *Op2;
1720
1721public:
1722 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
1723 : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
1724
1725 template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
1726
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001727 void printLeft(OutputBuffer &OB) const override {
1728 OB += "(";
1729 Op1->print(OB);
1730 OB += ")[";
1731 Op2->print(OB);
1732 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001733 }
1734};
1735
1736class PostfixExpr : public Node {
1737 const Node *Child;
1738 const StringView Operator;
1739
1740public:
1741 PostfixExpr(const Node *Child_, StringView Operator_)
1742 : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
1743
1744 template<typename Fn> void match(Fn F) const { F(Child, Operator); }
1745
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001746 void printLeft(OutputBuffer &OB) const override {
1747 OB += "(";
1748 Child->print(OB);
1749 OB += ")";
1750 OB += Operator;
Richard Smithc20d1442018-08-20 20:14:49 +00001751 }
1752};
1753
1754class ConditionalExpr : public Node {
1755 const Node *Cond;
1756 const Node *Then;
1757 const Node *Else;
1758
1759public:
1760 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
1761 : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
1762
1763 template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
1764
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001765 void printLeft(OutputBuffer &OB) const override {
1766 OB += "(";
1767 Cond->print(OB);
1768 OB += ") ? (";
1769 Then->print(OB);
1770 OB += ") : (";
1771 Else->print(OB);
1772 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001773 }
1774};
1775
1776class MemberExpr : public Node {
1777 const Node *LHS;
1778 const StringView Kind;
1779 const Node *RHS;
1780
1781public:
1782 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
1783 : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1784
1785 template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
1786
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001787 void printLeft(OutputBuffer &OB) const override {
1788 LHS->print(OB);
1789 OB += Kind;
1790 RHS->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001791 }
1792};
1793
Richard Smith1865d2f2020-10-22 19:29:36 -07001794class SubobjectExpr : public Node {
1795 const Node *Type;
1796 const Node *SubExpr;
1797 StringView Offset;
1798 NodeArray UnionSelectors;
1799 bool OnePastTheEnd;
1800
1801public:
1802 SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_,
1803 NodeArray UnionSelectors_, bool OnePastTheEnd_)
1804 : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_),
1805 UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {}
1806
1807 template<typename Fn> void match(Fn F) const {
1808 F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd);
1809 }
1810
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001811 void printLeft(OutputBuffer &OB) const override {
1812 SubExpr->print(OB);
1813 OB += ".<";
1814 Type->print(OB);
1815 OB += " at offset ";
Richard Smith1865d2f2020-10-22 19:29:36 -07001816 if (Offset.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001817 OB += "0";
Richard Smith1865d2f2020-10-22 19:29:36 -07001818 } else if (Offset[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001819 OB += "-";
1820 OB += Offset.dropFront();
Richard Smith1865d2f2020-10-22 19:29:36 -07001821 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001822 OB += Offset;
Richard Smith1865d2f2020-10-22 19:29:36 -07001823 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001824 OB += ">";
Richard Smith1865d2f2020-10-22 19:29:36 -07001825 }
1826};
1827
Richard Smithc20d1442018-08-20 20:14:49 +00001828class EnclosingExpr : public Node {
1829 const StringView Prefix;
1830 const Node *Infix;
1831 const StringView Postfix;
1832
1833public:
1834 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1835 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1836 Postfix(Postfix_) {}
1837
1838 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1839
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001840 void printLeft(OutputBuffer &OB) const override {
1841 OB += Prefix;
1842 Infix->print(OB);
1843 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +00001844 }
1845};
1846
1847class CastExpr : public Node {
1848 // cast_kind<to>(from)
1849 const StringView CastKind;
1850 const Node *To;
1851 const Node *From;
1852
1853public:
1854 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1855 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1856
1857 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1858
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001859 void printLeft(OutputBuffer &OB) const override {
1860 OB += CastKind;
1861 OB += "<";
1862 To->printLeft(OB);
1863 OB += ">(";
1864 From->printLeft(OB);
1865 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001866 }
1867};
1868
1869class SizeofParamPackExpr : public Node {
1870 const Node *Pack;
1871
1872public:
1873 SizeofParamPackExpr(const Node *Pack_)
1874 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1875
1876 template<typename Fn> void match(Fn F) const { F(Pack); }
1877
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001878 void printLeft(OutputBuffer &OB) const override {
1879 OB += "sizeof...(";
Richard Smithc20d1442018-08-20 20:14:49 +00001880 ParameterPackExpansion PPE(Pack);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001881 PPE.printLeft(OB);
1882 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001883 }
1884};
1885
1886class CallExpr : public Node {
1887 const Node *Callee;
1888 NodeArray Args;
1889
1890public:
1891 CallExpr(const Node *Callee_, NodeArray Args_)
1892 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1893
1894 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1895
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001896 void printLeft(OutputBuffer &OB) const override {
1897 Callee->print(OB);
1898 OB += "(";
1899 Args.printWithComma(OB);
1900 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001901 }
1902};
1903
1904class NewExpr : public Node {
1905 // new (expr_list) type(init_list)
1906 NodeArray ExprList;
1907 Node *Type;
1908 NodeArray InitList;
1909 bool IsGlobal; // ::operator new ?
1910 bool IsArray; // new[] ?
1911public:
1912 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1913 bool IsArray_)
1914 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1915 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1916
1917 template<typename Fn> void match(Fn F) const {
1918 F(ExprList, Type, InitList, IsGlobal, IsArray);
1919 }
1920
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001921 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001922 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001923 OB += "::operator ";
1924 OB += "new";
Richard Smithc20d1442018-08-20 20:14:49 +00001925 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001926 OB += "[]";
1927 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00001928 if (!ExprList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001929 OB += "(";
1930 ExprList.printWithComma(OB);
1931 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001932 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001933 Type->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001934 if (!InitList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001935 OB += "(";
1936 InitList.printWithComma(OB);
1937 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001938 }
Richard Smithc20d1442018-08-20 20:14:49 +00001939 }
1940};
1941
1942class DeleteExpr : public Node {
1943 Node *Op;
1944 bool IsGlobal;
1945 bool IsArray;
1946
1947public:
1948 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1949 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1950
1951 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1952
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001953 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001954 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001955 OB += "::";
1956 OB += "delete";
Richard Smithc20d1442018-08-20 20:14:49 +00001957 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001958 OB += "[] ";
1959 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001960 }
1961};
1962
1963class PrefixExpr : public Node {
1964 StringView Prefix;
1965 Node *Child;
1966
1967public:
1968 PrefixExpr(StringView Prefix_, Node *Child_)
1969 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1970
1971 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1972
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001973 void printLeft(OutputBuffer &OB) const override {
1974 OB += Prefix;
1975 OB += "(";
1976 Child->print(OB);
1977 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001978 }
1979};
1980
1981class FunctionParam : public Node {
1982 StringView Number;
1983
1984public:
1985 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
1986
1987 template<typename Fn> void match(Fn F) const { F(Number); }
1988
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001989 void printLeft(OutputBuffer &OB) const override {
1990 OB += "fp";
1991 OB += Number;
Richard Smithc20d1442018-08-20 20:14:49 +00001992 }
1993};
1994
1995class ConversionExpr : public Node {
1996 const Node *Type;
1997 NodeArray Expressions;
1998
1999public:
2000 ConversionExpr(const Node *Type_, NodeArray Expressions_)
2001 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
2002
2003 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
2004
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002005 void printLeft(OutputBuffer &OB) const override {
2006 OB += "(";
2007 Type->print(OB);
2008 OB += ")(";
2009 Expressions.printWithComma(OB);
2010 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002011 }
2012};
2013
Richard Smith1865d2f2020-10-22 19:29:36 -07002014class PointerToMemberConversionExpr : public Node {
2015 const Node *Type;
2016 const Node *SubExpr;
2017 StringView Offset;
2018
2019public:
2020 PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_,
2021 StringView Offset_)
2022 : Node(KPointerToMemberConversionExpr), Type(Type_), SubExpr(SubExpr_),
2023 Offset(Offset_) {}
2024
2025 template<typename Fn> void match(Fn F) const { F(Type, SubExpr, Offset); }
2026
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002027 void printLeft(OutputBuffer &OB) const override {
2028 OB += "(";
2029 Type->print(OB);
2030 OB += ")(";
2031 SubExpr->print(OB);
2032 OB += ")";
Richard Smith1865d2f2020-10-22 19:29:36 -07002033 }
2034};
2035
Richard Smithc20d1442018-08-20 20:14:49 +00002036class InitListExpr : public Node {
2037 const Node *Ty;
2038 NodeArray Inits;
2039public:
2040 InitListExpr(const Node *Ty_, NodeArray Inits_)
2041 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
2042
2043 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
2044
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002045 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002046 if (Ty)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002047 Ty->print(OB);
2048 OB += '{';
2049 Inits.printWithComma(OB);
2050 OB += '}';
Richard Smithc20d1442018-08-20 20:14:49 +00002051 }
2052};
2053
2054class BracedExpr : public Node {
2055 const Node *Elem;
2056 const Node *Init;
2057 bool IsArray;
2058public:
2059 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
2060 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
2061
2062 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
2063
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002064 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002065 if (IsArray) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002066 OB += '[';
2067 Elem->print(OB);
2068 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002069 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002070 OB += '.';
2071 Elem->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002072 }
2073 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002074 OB += " = ";
2075 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002076 }
2077};
2078
2079class BracedRangeExpr : public Node {
2080 const Node *First;
2081 const Node *Last;
2082 const Node *Init;
2083public:
2084 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
2085 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
2086
2087 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
2088
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002089 void printLeft(OutputBuffer &OB) const override {
2090 OB += '[';
2091 First->print(OB);
2092 OB += " ... ";
2093 Last->print(OB);
2094 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002095 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002096 OB += " = ";
2097 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002098 }
2099};
2100
2101class FoldExpr : public Node {
2102 const Node *Pack, *Init;
2103 StringView OperatorName;
2104 bool IsLeftFold;
2105
2106public:
2107 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
2108 const Node *Init_)
2109 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
2110 IsLeftFold(IsLeftFold_) {}
2111
2112 template<typename Fn> void match(Fn F) const {
2113 F(IsLeftFold, OperatorName, Pack, Init);
2114 }
2115
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002116 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002117 auto PrintPack = [&] {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002118 OB += '(';
2119 ParameterPackExpansion(Pack).print(OB);
2120 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002121 };
2122
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002123 OB += '(';
Richard Smithc20d1442018-08-20 20:14:49 +00002124
2125 if (IsLeftFold) {
2126 // init op ... op pack
2127 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002128 Init->print(OB);
2129 OB += ' ';
2130 OB += OperatorName;
2131 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002132 }
2133 // ... op pack
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002134 OB += "... ";
2135 OB += OperatorName;
2136 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002137 PrintPack();
2138 } else { // !IsLeftFold
2139 // pack op ...
2140 PrintPack();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002141 OB += ' ';
2142 OB += OperatorName;
2143 OB += " ...";
Richard Smithc20d1442018-08-20 20:14:49 +00002144 // pack op ... op init
2145 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002146 OB += ' ';
2147 OB += OperatorName;
2148 OB += ' ';
2149 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002150 }
2151 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002152 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002153 }
2154};
2155
2156class ThrowExpr : public Node {
2157 const Node *Op;
2158
2159public:
2160 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
2161
2162 template<typename Fn> void match(Fn F) const { F(Op); }
2163
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002164 void printLeft(OutputBuffer &OB) const override {
2165 OB += "throw ";
2166 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002167 }
2168};
2169
2170class BoolExpr : public Node {
2171 bool Value;
2172
2173public:
2174 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
2175
2176 template<typename Fn> void match(Fn F) const { F(Value); }
2177
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002178 void printLeft(OutputBuffer &OB) const override {
2179 OB += Value ? StringView("true") : StringView("false");
Richard Smithc20d1442018-08-20 20:14:49 +00002180 }
2181};
2182
Richard Smithdf1c14c2019-09-06 23:53:21 +00002183class StringLiteral : public Node {
2184 const Node *Type;
2185
2186public:
2187 StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
2188
2189 template<typename Fn> void match(Fn F) const { F(Type); }
2190
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002191 void printLeft(OutputBuffer &OB) const override {
2192 OB += "\"<";
2193 Type->print(OB);
2194 OB += ">\"";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002195 }
2196};
2197
2198class LambdaExpr : public Node {
2199 const Node *Type;
2200
Richard Smithdf1c14c2019-09-06 23:53:21 +00002201public:
2202 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2203
2204 template<typename Fn> void match(Fn F) const { F(Type); }
2205
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002206 void printLeft(OutputBuffer &OB) const override {
2207 OB += "[]";
Richard Smithfb917462019-09-09 22:26:04 +00002208 if (Type->getKind() == KClosureTypeName)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002209 static_cast<const ClosureTypeName *>(Type)->printDeclarator(OB);
2210 OB += "{...}";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002211 }
2212};
2213
Erik Pilkington0a170f12020-05-13 14:13:37 -04002214class EnumLiteral : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +00002215 // ty(integer)
2216 const Node *Ty;
2217 StringView Integer;
2218
2219public:
Erik Pilkington0a170f12020-05-13 14:13:37 -04002220 EnumLiteral(const Node *Ty_, StringView Integer_)
2221 : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00002222
2223 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2224
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002225 void printLeft(OutputBuffer &OB) const override {
2226 OB << "(";
2227 Ty->print(OB);
2228 OB << ")";
Erik Pilkington0a170f12020-05-13 14:13:37 -04002229
2230 if (Integer[0] == 'n')
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002231 OB << "-" << Integer.dropFront(1);
Erik Pilkington0a170f12020-05-13 14:13:37 -04002232 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002233 OB << Integer;
Richard Smithc20d1442018-08-20 20:14:49 +00002234 }
2235};
2236
2237class IntegerLiteral : public Node {
2238 StringView Type;
2239 StringView Value;
2240
2241public:
2242 IntegerLiteral(StringView Type_, StringView Value_)
2243 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2244
2245 template<typename Fn> void match(Fn F) const { F(Type, Value); }
2246
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002247 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002248 if (Type.size() > 3) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002249 OB += "(";
2250 OB += Type;
2251 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002252 }
2253
2254 if (Value[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002255 OB += "-";
2256 OB += Value.dropFront(1);
Richard Smithc20d1442018-08-20 20:14:49 +00002257 } else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002258 OB += Value;
Richard Smithc20d1442018-08-20 20:14:49 +00002259
2260 if (Type.size() <= 3)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002261 OB += Type;
Richard Smithc20d1442018-08-20 20:14:49 +00002262 }
2263};
2264
2265template <class Float> struct FloatData;
2266
2267namespace float_literal_impl {
2268constexpr Node::Kind getFloatLiteralKind(float *) {
2269 return Node::KFloatLiteral;
2270}
2271constexpr Node::Kind getFloatLiteralKind(double *) {
2272 return Node::KDoubleLiteral;
2273}
2274constexpr Node::Kind getFloatLiteralKind(long double *) {
2275 return Node::KLongDoubleLiteral;
2276}
2277}
2278
2279template <class Float> class FloatLiteralImpl : public Node {
2280 const StringView Contents;
2281
2282 static constexpr Kind KindForClass =
2283 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2284
2285public:
2286 FloatLiteralImpl(StringView Contents_)
2287 : Node(KindForClass), Contents(Contents_) {}
2288
2289 template<typename Fn> void match(Fn F) const { F(Contents); }
2290
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002291 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002292 const char *first = Contents.begin();
2293 const char *last = Contents.end() + 1;
2294
2295 const size_t N = FloatData<Float>::mangled_size;
2296 if (static_cast<std::size_t>(last - first) > N) {
2297 last = first + N;
2298 union {
2299 Float value;
2300 char buf[sizeof(Float)];
2301 };
2302 const char *t = first;
2303 char *e = buf;
2304 for (; t != last; ++t, ++e) {
2305 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2306 : static_cast<unsigned>(*t - 'a' + 10);
2307 ++t;
2308 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2309 : static_cast<unsigned>(*t - 'a' + 10);
2310 *e = static_cast<char>((d1 << 4) + d0);
2311 }
2312#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2313 std::reverse(buf, e);
2314#endif
2315 char num[FloatData<Float>::max_demangled_size] = {0};
2316 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002317 OB += StringView(num, num + n);
Richard Smithc20d1442018-08-20 20:14:49 +00002318 }
2319 }
2320};
2321
2322using FloatLiteral = FloatLiteralImpl<float>;
2323using DoubleLiteral = FloatLiteralImpl<double>;
2324using LongDoubleLiteral = FloatLiteralImpl<long double>;
2325
2326/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2327/// appropriate derived class.
2328template<typename Fn>
2329void Node::visit(Fn F) const {
2330 switch (K) {
2331#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2332 FOR_EACH_NODE_KIND(CASE)
2333#undef CASE
2334 }
2335 assert(0 && "unknown mangling node kind");
2336}
2337
2338/// Determine the kind of a node from its type.
2339template<typename NodeT> struct NodeKind;
2340#define SPECIALIZATION(X) \
2341 template<> struct NodeKind<X> { \
2342 static constexpr Node::Kind Kind = Node::K##X; \
2343 static constexpr const char *name() { return #X; } \
2344 };
2345FOR_EACH_NODE_KIND(SPECIALIZATION)
2346#undef SPECIALIZATION
2347
2348#undef FOR_EACH_NODE_KIND
2349
Pavel Labathba825192018-10-16 14:29:14 +00002350template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002351 const char *First;
2352 const char *Last;
2353
2354 // Name stack, this is used by the parser to hold temporary names that were
2355 // parsed. The parser collapses multiple names into new nodes to construct
2356 // the AST. Once the parser is finished, names.size() == 1.
2357 PODSmallVector<Node *, 32> Names;
2358
2359 // Substitution table. Itanium supports name substitutions as a means of
2360 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2361 // table.
2362 PODSmallVector<Node *, 32> Subs;
2363
Richard Smithdf1c14c2019-09-06 23:53:21 +00002364 using TemplateParamList = PODSmallVector<Node *, 8>;
2365
2366 class ScopedTemplateParamList {
2367 AbstractManglingParser *Parser;
2368 size_t OldNumTemplateParamLists;
2369 TemplateParamList Params;
2370
2371 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04002372 ScopedTemplateParamList(AbstractManglingParser *TheParser)
2373 : Parser(TheParser),
2374 OldNumTemplateParamLists(TheParser->TemplateParams.size()) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002375 Parser->TemplateParams.push_back(&Params);
2376 }
2377 ~ScopedTemplateParamList() {
2378 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2379 Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2380 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002381 };
2382
Richard Smithc20d1442018-08-20 20:14:49 +00002383 // Template parameter table. Like the above, but referenced like "T42_".
2384 // This has a smaller size compared to Subs and Names because it can be
2385 // stored on the stack.
Richard Smithdf1c14c2019-09-06 23:53:21 +00002386 TemplateParamList OuterTemplateParams;
2387
2388 // Lists of template parameters indexed by template parameter depth,
2389 // referenced like "TL2_4_". If nonempty, element 0 is always
2390 // OuterTemplateParams; inner elements are always template parameter lists of
2391 // lambda expressions. For a generic lambda with no explicit template
2392 // parameter list, the corresponding parameter list pointer will be null.
2393 PODSmallVector<TemplateParamList *, 4> TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00002394
2395 // Set of unresolved forward <template-param> references. These can occur in a
2396 // conversion operator's type, and are resolved in the enclosing <encoding>.
2397 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2398
Richard Smithc20d1442018-08-20 20:14:49 +00002399 bool TryToParseTemplateArgs = true;
2400 bool PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002401 size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2402
2403 unsigned NumSyntheticTemplateParameters[3] = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002404
2405 Alloc ASTAllocator;
2406
Pavel Labathba825192018-10-16 14:29:14 +00002407 AbstractManglingParser(const char *First_, const char *Last_)
2408 : First(First_), Last(Last_) {}
2409
2410 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002411
2412 void reset(const char *First_, const char *Last_) {
2413 First = First_;
2414 Last = Last_;
2415 Names.clear();
2416 Subs.clear();
2417 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002418 ParsingLambdaParamsAtLevel = (size_t)-1;
Richard Smithc20d1442018-08-20 20:14:49 +00002419 TryToParseTemplateArgs = true;
2420 PermitForwardTemplateReferences = false;
Richard Smith9a2307a2019-09-07 00:11:53 +00002421 for (int I = 0; I != 3; ++I)
2422 NumSyntheticTemplateParameters[I] = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00002423 ASTAllocator.reset();
2424 }
2425
Richard Smithb485b352018-08-24 23:30:26 +00002426 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002427 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2428 }
2429
2430 template <class It> NodeArray makeNodeArray(It begin, It end) {
2431 size_t sz = static_cast<size_t>(end - begin);
2432 void *mem = ASTAllocator.allocateNodeArray(sz);
2433 Node **data = new (mem) Node *[sz];
2434 std::copy(begin, end, data);
2435 return NodeArray(data, sz);
2436 }
2437
2438 NodeArray popTrailingNodeArray(size_t FromPosition) {
2439 assert(FromPosition <= Names.size());
2440 NodeArray res =
2441 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2442 Names.dropBack(FromPosition);
2443 return res;
2444 }
2445
2446 bool consumeIf(StringView S) {
2447 if (StringView(First, Last).startsWith(S)) {
2448 First += S.size();
2449 return true;
2450 }
2451 return false;
2452 }
2453
2454 bool consumeIf(char C) {
2455 if (First != Last && *First == C) {
2456 ++First;
2457 return true;
2458 }
2459 return false;
2460 }
2461
2462 char consume() { return First != Last ? *First++ : '\0'; }
2463
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08002464 char look(unsigned Lookahead = 0) const {
Richard Smithc20d1442018-08-20 20:14:49 +00002465 if (static_cast<size_t>(Last - First) <= Lookahead)
2466 return '\0';
2467 return First[Lookahead];
2468 }
2469
2470 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2471
2472 StringView parseNumber(bool AllowNegative = false);
2473 Qualifiers parseCVQualifiers();
2474 bool parsePositiveInteger(size_t *Out);
2475 StringView parseBareSourceName();
2476
2477 bool parseSeqId(size_t *Out);
2478 Node *parseSubstitution();
2479 Node *parseTemplateParam();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002480 Node *parseTemplateParamDecl();
Richard Smithc20d1442018-08-20 20:14:49 +00002481 Node *parseTemplateArgs(bool TagTemplates = false);
2482 Node *parseTemplateArg();
2483
2484 /// Parse the <expr> production.
2485 Node *parseExpr();
2486 Node *parsePrefixExpr(StringView Kind);
2487 Node *parseBinaryExpr(StringView Kind);
2488 Node *parseIntegerLiteral(StringView Lit);
2489 Node *parseExprPrimary();
2490 template <class Float> Node *parseFloatingLiteral();
2491 Node *parseFunctionParam();
2492 Node *parseNewExpr();
2493 Node *parseConversionExpr();
2494 Node *parseBracedExpr();
2495 Node *parseFoldExpr();
Richard Smith1865d2f2020-10-22 19:29:36 -07002496 Node *parsePointerToMemberConversionExpr();
2497 Node *parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00002498
2499 /// Parse the <type> production.
2500 Node *parseType();
2501 Node *parseFunctionType();
2502 Node *parseVectorType();
2503 Node *parseDecltype();
2504 Node *parseArrayType();
2505 Node *parsePointerToMemberType();
2506 Node *parseClassEnumType();
2507 Node *parseQualifiedType();
2508
2509 Node *parseEncoding();
2510 bool parseCallOffset();
2511 Node *parseSpecialName();
2512
2513 /// Holds some extra information about a <name> that is being parsed. This
2514 /// information is only pertinent if the <name> refers to an <encoding>.
2515 struct NameState {
2516 bool CtorDtorConversion = false;
2517 bool EndsWithTemplateArgs = false;
2518 Qualifiers CVQualifiers = QualNone;
2519 FunctionRefQual ReferenceQualifier = FrefQualNone;
2520 size_t ForwardTemplateRefsBegin;
2521
Pavel Labathba825192018-10-16 14:29:14 +00002522 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002523 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2524 };
2525
2526 bool resolveForwardTemplateRefs(NameState &State) {
2527 size_t I = State.ForwardTemplateRefsBegin;
2528 size_t E = ForwardTemplateRefs.size();
2529 for (; I < E; ++I) {
2530 size_t Idx = ForwardTemplateRefs[I]->Index;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002531 if (TemplateParams.empty() || !TemplateParams[0] ||
2532 Idx >= TemplateParams[0]->size())
Richard Smithc20d1442018-08-20 20:14:49 +00002533 return true;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002534 ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
Richard Smithc20d1442018-08-20 20:14:49 +00002535 }
2536 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2537 return false;
2538 }
2539
2540 /// Parse the <name> production>
2541 Node *parseName(NameState *State = nullptr);
2542 Node *parseLocalName(NameState *State);
2543 Node *parseOperatorName(NameState *State);
2544 Node *parseUnqualifiedName(NameState *State);
2545 Node *parseUnnamedTypeName(NameState *State);
2546 Node *parseSourceName(NameState *State);
2547 Node *parseUnscopedName(NameState *State);
2548 Node *parseNestedName(NameState *State);
2549 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2550
2551 Node *parseAbiTags(Node *N);
2552
2553 /// Parse the <unresolved-name> production.
Nathan Sidwell77c52e22022-01-28 11:59:03 -08002554 Node *parseUnresolvedName(bool Global);
Richard Smithc20d1442018-08-20 20:14:49 +00002555 Node *parseSimpleId();
2556 Node *parseBaseUnresolvedName();
2557 Node *parseUnresolvedType();
2558 Node *parseDestructorName();
2559
2560 /// Top-level entry point into the parser.
2561 Node *parse();
2562};
2563
2564const char* parse_discriminator(const char* first, const char* last);
2565
2566// <name> ::= <nested-name> // N
2567// ::= <local-name> # See Scope Encoding below // Z
2568// ::= <unscoped-template-name> <template-args>
2569// ::= <unscoped-name>
2570//
2571// <unscoped-template-name> ::= <unscoped-name>
2572// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002573template <typename Derived, typename Alloc>
2574Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002575 consumeIf('L'); // extension
2576
2577 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002578 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002579 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002580 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002581
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002582 Node *Result = nullptr;
2583 bool IsSubst = look() == 'S' && look(1) != 't';
2584 if (IsSubst) {
2585 // A substitution must lead to:
2586 // ::= <unscoped-template-name> <template-args>
2587 Result = getDerived().parseSubstitution();
2588 } else {
2589 // An unscoped name can be one of:
2590 // ::= <unscoped-name>
2591 // ::= <unscoped-template-name> <template-args>
2592 Result = getDerived().parseUnscopedName(State);
2593 }
2594 if (Result == nullptr)
2595 return nullptr;
2596
2597 if (look() == 'I') {
2598 // ::= <unscoped-template-name> <template-args>
2599 if (!IsSubst)
2600 // An unscoped-template-name is substitutable.
2601 Subs.push_back(Result);
Pavel Labathba825192018-10-16 14:29:14 +00002602 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002603 if (TA == nullptr)
2604 return nullptr;
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002605 if (State)
2606 State->EndsWithTemplateArgs = true;
2607 Result = make<NameWithTemplateArgs>(Result, TA);
2608 } else if (IsSubst) {
2609 // The substitution case must be followed by <template-args>.
2610 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002611 }
2612
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002613 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00002614}
2615
2616// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2617// := Z <function encoding> E s [<discriminator>]
2618// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002619template <typename Derived, typename Alloc>
2620Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002621 if (!consumeIf('Z'))
2622 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002623 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002624 if (Encoding == nullptr || !consumeIf('E'))
2625 return nullptr;
2626
2627 if (consumeIf('s')) {
2628 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002629 auto *StringLitName = make<NameType>("string literal");
2630 if (!StringLitName)
2631 return nullptr;
2632 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002633 }
2634
2635 if (consumeIf('d')) {
2636 parseNumber(true);
2637 if (!consumeIf('_'))
2638 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002639 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002640 if (N == nullptr)
2641 return nullptr;
2642 return make<LocalName>(Encoding, N);
2643 }
2644
Pavel Labathba825192018-10-16 14:29:14 +00002645 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002646 if (Entity == nullptr)
2647 return nullptr;
2648 First = parse_discriminator(First, Last);
2649 return make<LocalName>(Encoding, Entity);
2650}
2651
2652// <unscoped-name> ::= <unqualified-name>
2653// ::= St <unqualified-name> # ::std::
2654// extension ::= StL<unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00002655template <typename Derived, typename Alloc>
2656Node *
2657AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002658 bool IsStd = consumeIf("St");
2659 if (IsStd)
2660 consumeIf('L');
2661
2662 Node *Result = getDerived().parseUnqualifiedName(State);
2663 if (Result == nullptr)
2664 return nullptr;
Nathan Sidwell200e97c2022-01-21 11:37:01 -08002665 if (IsStd) {
2666 if (auto *Std = make<NameType>("std"))
2667 Result = make<NestedName>(Std, Result);
2668 else
2669 return nullptr;
2670 }
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002671
2672 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00002673}
2674
2675// <unqualified-name> ::= <operator-name> [abi-tags]
2676// ::= <ctor-dtor-name>
2677// ::= <source-name>
2678// ::= <unnamed-type-name>
2679// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002680template <typename Derived, typename Alloc>
2681Node *
2682AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002683 // <ctor-dtor-name>s are special-cased in parseNestedName().
2684 Node *Result;
2685 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002686 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002687 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002688 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002689 else if (consumeIf("DC")) {
2690 size_t BindingsBegin = Names.size();
2691 do {
Pavel Labathba825192018-10-16 14:29:14 +00002692 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002693 if (Binding == nullptr)
2694 return nullptr;
2695 Names.push_back(Binding);
2696 } while (!consumeIf('E'));
2697 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2698 } else
Pavel Labathba825192018-10-16 14:29:14 +00002699 Result = getDerived().parseOperatorName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002700 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002701 Result = getDerived().parseAbiTags(Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002702 return Result;
2703}
2704
2705// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2706// ::= <closure-type-name>
2707//
2708// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2709//
2710// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002711template <typename Derived, typename Alloc>
2712Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002713AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2714 // <template-params> refer to the innermost <template-args>. Clear out any
2715 // outer args that we may have inserted into TemplateParams.
2716 if (State != nullptr)
2717 TemplateParams.clear();
2718
Richard Smithc20d1442018-08-20 20:14:49 +00002719 if (consumeIf("Ut")) {
2720 StringView Count = parseNumber();
2721 if (!consumeIf('_'))
2722 return nullptr;
2723 return make<UnnamedTypeName>(Count);
2724 }
2725 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002726 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2727 TemplateParams.size());
2728 ScopedTemplateParamList LambdaTemplateParams(this);
2729
2730 size_t ParamsBegin = Names.size();
2731 while (look() == 'T' &&
2732 StringView("yptn").find(look(1)) != StringView::npos) {
2733 Node *T = parseTemplateParamDecl();
2734 if (!T)
2735 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002736 Names.push_back(T);
2737 }
2738 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2739
2740 // FIXME: If TempParams is empty and none of the function parameters
2741 // includes 'auto', we should remove LambdaTemplateParams from the
2742 // TemplateParams list. Unfortunately, we don't find out whether there are
2743 // any 'auto' parameters until too late in an example such as:
2744 //
2745 // template<typename T> void f(
2746 // decltype([](decltype([]<typename T>(T v) {}),
2747 // auto) {})) {}
2748 // template<typename T> void f(
2749 // decltype([](decltype([]<typename T>(T w) {}),
2750 // int) {})) {}
2751 //
2752 // Here, the type of v is at level 2 but the type of w is at level 1. We
2753 // don't find this out until we encounter the type of the next parameter.
2754 //
2755 // However, compilers can't actually cope with the former example in
2756 // practice, and it's likely to be made ill-formed in future, so we don't
2757 // need to support it here.
2758 //
2759 // If we encounter an 'auto' in the function parameter types, we will
2760 // recreate a template parameter scope for it, but any intervening lambdas
2761 // will be parsed in the 'wrong' template parameter depth.
2762 if (TempParams.empty())
2763 TemplateParams.pop_back();
2764
Richard Smithc20d1442018-08-20 20:14:49 +00002765 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002766 do {
Pavel Labathba825192018-10-16 14:29:14 +00002767 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002768 if (P == nullptr)
2769 return nullptr;
2770 Names.push_back(P);
2771 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002772 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002773 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2774
Richard Smithc20d1442018-08-20 20:14:49 +00002775 StringView Count = parseNumber();
2776 if (!consumeIf('_'))
2777 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002778 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002779 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002780 if (consumeIf("Ub")) {
2781 (void)parseNumber();
2782 if (!consumeIf('_'))
2783 return nullptr;
2784 return make<NameType>("'block-literal'");
2785 }
Richard Smithc20d1442018-08-20 20:14:49 +00002786 return nullptr;
2787}
2788
2789// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002790template <typename Derived, typename Alloc>
2791Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002792 size_t Length = 0;
2793 if (parsePositiveInteger(&Length))
2794 return nullptr;
2795 if (numLeft() < Length || Length == 0)
2796 return nullptr;
2797 StringView Name(First, First + Length);
2798 First += Length;
2799 if (Name.startsWith("_GLOBAL__N"))
2800 return make<NameType>("(anonymous namespace)");
2801 return make<NameType>(Name);
2802}
2803
2804// <operator-name> ::= aa # &&
2805// ::= ad # & (unary)
2806// ::= an # &
2807// ::= aN # &=
2808// ::= aS # =
2809// ::= cl # ()
2810// ::= cm # ,
2811// ::= co # ~
2812// ::= cv <type> # (cast)
2813// ::= da # delete[]
2814// ::= de # * (unary)
2815// ::= dl # delete
2816// ::= dv # /
2817// ::= dV # /=
2818// ::= eo # ^
2819// ::= eO # ^=
2820// ::= eq # ==
2821// ::= ge # >=
2822// ::= gt # >
2823// ::= ix # []
2824// ::= le # <=
2825// ::= li <source-name> # operator ""
2826// ::= ls # <<
2827// ::= lS # <<=
2828// ::= lt # <
2829// ::= mi # -
2830// ::= mI # -=
2831// ::= ml # *
2832// ::= mL # *=
2833// ::= mm # -- (postfix in <expression> context)
2834// ::= na # new[]
2835// ::= ne # !=
2836// ::= ng # - (unary)
2837// ::= nt # !
2838// ::= nw # new
2839// ::= oo # ||
2840// ::= or # |
2841// ::= oR # |=
2842// ::= pm # ->*
2843// ::= pl # +
2844// ::= pL # +=
2845// ::= pp # ++ (postfix in <expression> context)
2846// ::= ps # + (unary)
2847// ::= pt # ->
2848// ::= qu # ?
2849// ::= rm # %
2850// ::= rM # %=
2851// ::= rs # >>
2852// ::= rS # >>=
2853// ::= ss # <=> C++2a
2854// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002855template <typename Derived, typename Alloc>
2856Node *
2857AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002858 switch (look()) {
2859 case 'a':
2860 switch (look(1)) {
2861 case 'a':
2862 First += 2;
2863 return make<NameType>("operator&&");
2864 case 'd':
2865 case 'n':
2866 First += 2;
2867 return make<NameType>("operator&");
2868 case 'N':
2869 First += 2;
2870 return make<NameType>("operator&=");
2871 case 'S':
2872 First += 2;
2873 return make<NameType>("operator=");
2874 }
2875 return nullptr;
2876 case 'c':
2877 switch (look(1)) {
2878 case 'l':
2879 First += 2;
2880 return make<NameType>("operator()");
2881 case 'm':
2882 First += 2;
2883 return make<NameType>("operator,");
2884 case 'o':
2885 First += 2;
2886 return make<NameType>("operator~");
2887 // ::= cv <type> # (cast)
2888 case 'v': {
2889 First += 2;
2890 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2891 // If we're parsing an encoding, State != nullptr and the conversion
2892 // operators' <type> could have a <template-param> that refers to some
2893 // <template-arg>s further ahead in the mangled name.
2894 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2895 PermitForwardTemplateReferences ||
2896 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002897 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002898 if (Ty == nullptr)
2899 return nullptr;
2900 if (State) State->CtorDtorConversion = true;
2901 return make<ConversionOperatorType>(Ty);
2902 }
2903 }
2904 return nullptr;
2905 case 'd':
2906 switch (look(1)) {
2907 case 'a':
2908 First += 2;
2909 return make<NameType>("operator delete[]");
2910 case 'e':
2911 First += 2;
2912 return make<NameType>("operator*");
2913 case 'l':
2914 First += 2;
2915 return make<NameType>("operator delete");
2916 case 'v':
2917 First += 2;
2918 return make<NameType>("operator/");
2919 case 'V':
2920 First += 2;
2921 return make<NameType>("operator/=");
2922 }
2923 return nullptr;
2924 case 'e':
2925 switch (look(1)) {
2926 case 'o':
2927 First += 2;
2928 return make<NameType>("operator^");
2929 case 'O':
2930 First += 2;
2931 return make<NameType>("operator^=");
2932 case 'q':
2933 First += 2;
2934 return make<NameType>("operator==");
2935 }
2936 return nullptr;
2937 case 'g':
2938 switch (look(1)) {
2939 case 'e':
2940 First += 2;
2941 return make<NameType>("operator>=");
2942 case 't':
2943 First += 2;
2944 return make<NameType>("operator>");
2945 }
2946 return nullptr;
2947 case 'i':
2948 if (look(1) == 'x') {
2949 First += 2;
2950 return make<NameType>("operator[]");
2951 }
2952 return nullptr;
2953 case 'l':
2954 switch (look(1)) {
2955 case 'e':
2956 First += 2;
2957 return make<NameType>("operator<=");
2958 // ::= li <source-name> # operator ""
2959 case 'i': {
2960 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002961 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002962 if (SN == nullptr)
2963 return nullptr;
2964 return make<LiteralOperator>(SN);
2965 }
2966 case 's':
2967 First += 2;
2968 return make<NameType>("operator<<");
2969 case 'S':
2970 First += 2;
2971 return make<NameType>("operator<<=");
2972 case 't':
2973 First += 2;
2974 return make<NameType>("operator<");
2975 }
2976 return nullptr;
2977 case 'm':
2978 switch (look(1)) {
2979 case 'i':
2980 First += 2;
2981 return make<NameType>("operator-");
2982 case 'I':
2983 First += 2;
2984 return make<NameType>("operator-=");
2985 case 'l':
2986 First += 2;
2987 return make<NameType>("operator*");
2988 case 'L':
2989 First += 2;
2990 return make<NameType>("operator*=");
2991 case 'm':
2992 First += 2;
2993 return make<NameType>("operator--");
2994 }
2995 return nullptr;
2996 case 'n':
2997 switch (look(1)) {
2998 case 'a':
2999 First += 2;
3000 return make<NameType>("operator new[]");
3001 case 'e':
3002 First += 2;
3003 return make<NameType>("operator!=");
3004 case 'g':
3005 First += 2;
3006 return make<NameType>("operator-");
3007 case 't':
3008 First += 2;
3009 return make<NameType>("operator!");
3010 case 'w':
3011 First += 2;
3012 return make<NameType>("operator new");
3013 }
3014 return nullptr;
3015 case 'o':
3016 switch (look(1)) {
3017 case 'o':
3018 First += 2;
3019 return make<NameType>("operator||");
3020 case 'r':
3021 First += 2;
3022 return make<NameType>("operator|");
3023 case 'R':
3024 First += 2;
3025 return make<NameType>("operator|=");
3026 }
3027 return nullptr;
3028 case 'p':
3029 switch (look(1)) {
3030 case 'm':
3031 First += 2;
3032 return make<NameType>("operator->*");
3033 case 'l':
3034 First += 2;
3035 return make<NameType>("operator+");
3036 case 'L':
3037 First += 2;
3038 return make<NameType>("operator+=");
3039 case 'p':
3040 First += 2;
3041 return make<NameType>("operator++");
3042 case 's':
3043 First += 2;
3044 return make<NameType>("operator+");
3045 case 't':
3046 First += 2;
3047 return make<NameType>("operator->");
3048 }
3049 return nullptr;
3050 case 'q':
3051 if (look(1) == 'u') {
3052 First += 2;
3053 return make<NameType>("operator?");
3054 }
3055 return nullptr;
3056 case 'r':
3057 switch (look(1)) {
3058 case 'm':
3059 First += 2;
3060 return make<NameType>("operator%");
3061 case 'M':
3062 First += 2;
3063 return make<NameType>("operator%=");
3064 case 's':
3065 First += 2;
3066 return make<NameType>("operator>>");
3067 case 'S':
3068 First += 2;
3069 return make<NameType>("operator>>=");
3070 }
3071 return nullptr;
3072 case 's':
3073 if (look(1) == 's') {
3074 First += 2;
3075 return make<NameType>("operator<=>");
3076 }
3077 return nullptr;
3078 // ::= v <digit> <source-name> # vendor extended operator
3079 case 'v':
3080 if (std::isdigit(look(1))) {
3081 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003082 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003083 if (SN == nullptr)
3084 return nullptr;
3085 return make<ConversionOperatorType>(SN);
3086 }
3087 return nullptr;
3088 }
3089 return nullptr;
3090}
3091
3092// <ctor-dtor-name> ::= C1 # complete object constructor
3093// ::= C2 # base object constructor
3094// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003095// extension ::= C4 # gcc old-style "[unified]" constructor
3096// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003097// ::= D0 # deleting destructor
3098// ::= D1 # complete object destructor
3099// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003100// extension ::= D4 # gcc old-style "[unified]" destructor
3101// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003102template <typename Derived, typename Alloc>
3103Node *
3104AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3105 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003106 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3107 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3108 switch (SSK) {
3109 case SpecialSubKind::string:
3110 case SpecialSubKind::istream:
3111 case SpecialSubKind::ostream:
3112 case SpecialSubKind::iostream:
3113 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003114 if (!SoFar)
3115 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003116 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003117 default:
3118 break;
3119 }
3120 }
3121
3122 if (consumeIf('C')) {
3123 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003124 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3125 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003126 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003127 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003128 ++First;
3129 if (State) State->CtorDtorConversion = true;
3130 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003131 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003132 return nullptr;
3133 }
Nico Weber29294792019-04-03 23:14:33 +00003134 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003135 }
3136
Nico Weber29294792019-04-03 23:14:33 +00003137 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3138 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003139 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003140 First += 2;
3141 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003142 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003143 }
3144
3145 return nullptr;
3146}
3147
3148// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3149// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3150//
3151// <prefix> ::= <prefix> <unqualified-name>
3152// ::= <template-prefix> <template-args>
3153// ::= <template-param>
3154// ::= <decltype>
3155// ::= # empty
3156// ::= <substitution>
3157// ::= <prefix> <data-member-prefix>
3158// extension ::= L
3159//
3160// <data-member-prefix> := <member source-name> [<template-args>] M
3161//
3162// <template-prefix> ::= <prefix> <template unqualified-name>
3163// ::= <template-param>
3164// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003165template <typename Derived, typename Alloc>
3166Node *
3167AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003168 if (!consumeIf('N'))
3169 return nullptr;
3170
3171 Qualifiers CVTmp = parseCVQualifiers();
3172 if (State) State->CVQualifiers = CVTmp;
3173
3174 if (consumeIf('O')) {
3175 if (State) State->ReferenceQualifier = FrefQualRValue;
3176 } else if (consumeIf('R')) {
3177 if (State) State->ReferenceQualifier = FrefQualLValue;
3178 } else
3179 if (State) State->ReferenceQualifier = FrefQualNone;
3180
3181 Node *SoFar = nullptr;
3182 auto PushComponent = [&](Node *Comp) {
Richard Smithb485b352018-08-24 23:30:26 +00003183 if (!Comp) return false;
Richard Smithc20d1442018-08-20 20:14:49 +00003184 if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
3185 else SoFar = Comp;
3186 if (State) State->EndsWithTemplateArgs = false;
Richard Smithb485b352018-08-24 23:30:26 +00003187 return SoFar != nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003188 };
3189
Richard Smithb485b352018-08-24 23:30:26 +00003190 if (consumeIf("St")) {
Richard Smithc20d1442018-08-20 20:14:49 +00003191 SoFar = make<NameType>("std");
Richard Smithb485b352018-08-24 23:30:26 +00003192 if (!SoFar)
3193 return nullptr;
3194 }
Richard Smithc20d1442018-08-20 20:14:49 +00003195
3196 while (!consumeIf('E')) {
3197 consumeIf('L'); // extension
3198
3199 // <data-member-prefix> := <member source-name> [<template-args>] M
3200 if (consumeIf('M')) {
3201 if (SoFar == nullptr)
3202 return nullptr;
3203 continue;
3204 }
3205
3206 // ::= <template-param>
3207 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003208 if (!PushComponent(getDerived().parseTemplateParam()))
Richard Smithc20d1442018-08-20 20:14:49 +00003209 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003210 Subs.push_back(SoFar);
3211 continue;
3212 }
3213
3214 // ::= <template-prefix> <template-args>
3215 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003216 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003217 if (TA == nullptr || SoFar == nullptr)
3218 return nullptr;
3219 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003220 if (!SoFar)
3221 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003222 if (State) State->EndsWithTemplateArgs = true;
3223 Subs.push_back(SoFar);
3224 continue;
3225 }
3226
3227 // ::= <decltype>
3228 if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
Pavel Labathba825192018-10-16 14:29:14 +00003229 if (!PushComponent(getDerived().parseDecltype()))
Richard Smithc20d1442018-08-20 20:14:49 +00003230 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003231 Subs.push_back(SoFar);
3232 continue;
3233 }
3234
3235 // ::= <substitution>
3236 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00003237 Node *S = getDerived().parseSubstitution();
Richard Smithb485b352018-08-24 23:30:26 +00003238 if (!PushComponent(S))
Richard Smithc20d1442018-08-20 20:14:49 +00003239 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003240 if (SoFar != S)
3241 Subs.push_back(S);
3242 continue;
3243 }
3244
3245 // Parse an <unqualified-name> thats actually a <ctor-dtor-name>.
3246 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
3247 if (SoFar == nullptr)
3248 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003249 if (!PushComponent(getDerived().parseCtorDtorName(SoFar, State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003250 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003251 SoFar = getDerived().parseAbiTags(SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00003252 if (SoFar == nullptr)
3253 return nullptr;
3254 Subs.push_back(SoFar);
3255 continue;
3256 }
3257
3258 // ::= <prefix> <unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00003259 if (!PushComponent(getDerived().parseUnqualifiedName(State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003260 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003261 Subs.push_back(SoFar);
3262 }
3263
3264 if (SoFar == nullptr || Subs.empty())
3265 return nullptr;
3266
3267 Subs.pop_back();
3268 return SoFar;
3269}
3270
3271// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003272template <typename Derived, typename Alloc>
3273Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3274 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003275 if (SN == nullptr)
3276 return nullptr;
3277 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003278 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003279 if (TA == nullptr)
3280 return nullptr;
3281 return make<NameWithTemplateArgs>(SN, TA);
3282 }
3283 return SN;
3284}
3285
3286// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3287// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003288template <typename Derived, typename Alloc>
3289Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003290 Node *Result;
3291 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003292 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003293 else
Pavel Labathba825192018-10-16 14:29:14 +00003294 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003295 if (Result == nullptr)
3296 return nullptr;
3297 return make<DtorName>(Result);
3298}
3299
3300// <unresolved-type> ::= <template-param>
3301// ::= <decltype>
3302// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003303template <typename Derived, typename Alloc>
3304Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003305 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003306 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003307 if (TP == nullptr)
3308 return nullptr;
3309 Subs.push_back(TP);
3310 return TP;
3311 }
3312 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003313 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003314 if (DT == nullptr)
3315 return nullptr;
3316 Subs.push_back(DT);
3317 return DT;
3318 }
Pavel Labathba825192018-10-16 14:29:14 +00003319 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003320}
3321
3322// <base-unresolved-name> ::= <simple-id> # unresolved name
3323// extension ::= <operator-name> # unresolved operator-function-id
3324// extension ::= <operator-name> <template-args> # unresolved operator template-id
3325// ::= on <operator-name> # unresolved operator-function-id
3326// ::= on <operator-name> <template-args> # unresolved operator template-id
3327// ::= dn <destructor-name> # destructor or pseudo-destructor;
3328// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003329template <typename Derived, typename Alloc>
3330Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003331 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003332 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003333
3334 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003335 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003336
3337 consumeIf("on");
3338
Pavel Labathba825192018-10-16 14:29:14 +00003339 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003340 if (Oper == nullptr)
3341 return nullptr;
3342 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003343 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003344 if (TA == nullptr)
3345 return nullptr;
3346 return make<NameWithTemplateArgs>(Oper, TA);
3347 }
3348 return Oper;
3349}
3350
3351// <unresolved-name>
3352// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3353// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3354// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3355// # A::x, N::y, A<T>::z; "gs" means leading "::"
Nathan Sidwell77c52e22022-01-28 11:59:03 -08003356// [gs] has been parsed by caller.
Richard Smithc20d1442018-08-20 20:14:49 +00003357// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3358// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3359// # T::N::x /decltype(p)::N::x
3360// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3361//
3362// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003363template <typename Derived, typename Alloc>
Nathan Sidwell77c52e22022-01-28 11:59:03 -08003364Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName(bool Global) {
Richard Smithc20d1442018-08-20 20:14:49 +00003365 Node *SoFar = nullptr;
3366
3367 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3368 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3369 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003370 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003371 if (SoFar == nullptr)
3372 return nullptr;
3373
3374 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003375 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003376 if (TA == nullptr)
3377 return nullptr;
3378 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003379 if (!SoFar)
3380 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003381 }
3382
3383 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003384 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003385 if (Qual == nullptr)
3386 return nullptr;
3387 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003388 if (!SoFar)
3389 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003390 }
3391
Pavel Labathba825192018-10-16 14:29:14 +00003392 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003393 if (Base == nullptr)
3394 return nullptr;
3395 return make<QualifiedName>(SoFar, Base);
3396 }
3397
Richard Smithc20d1442018-08-20 20:14:49 +00003398 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3399 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003400 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003401 if (SoFar == nullptr)
3402 return nullptr;
3403 if (Global)
3404 SoFar = make<GlobalQualifiedName>(SoFar);
3405 return SoFar;
3406 }
3407
3408 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3409 if (std::isdigit(look())) {
3410 do {
Pavel Labathba825192018-10-16 14:29:14 +00003411 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003412 if (Qual == nullptr)
3413 return nullptr;
3414 if (SoFar)
3415 SoFar = make<QualifiedName>(SoFar, Qual);
3416 else if (Global)
3417 SoFar = make<GlobalQualifiedName>(Qual);
3418 else
3419 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003420 if (!SoFar)
3421 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003422 } while (!consumeIf('E'));
3423 }
3424 // sr <unresolved-type> <base-unresolved-name>
3425 // sr <unresolved-type> <template-args> <base-unresolved-name>
3426 else {
Pavel Labathba825192018-10-16 14:29:14 +00003427 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003428 if (SoFar == nullptr)
3429 return nullptr;
3430
3431 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003432 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003433 if (TA == nullptr)
3434 return nullptr;
3435 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003436 if (!SoFar)
3437 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003438 }
3439 }
3440
3441 assert(SoFar != nullptr);
3442
Pavel Labathba825192018-10-16 14:29:14 +00003443 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003444 if (Base == nullptr)
3445 return nullptr;
3446 return make<QualifiedName>(SoFar, Base);
3447}
3448
3449// <abi-tags> ::= <abi-tag> [<abi-tags>]
3450// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003451template <typename Derived, typename Alloc>
3452Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003453 while (consumeIf('B')) {
3454 StringView SN = parseBareSourceName();
3455 if (SN.empty())
3456 return nullptr;
3457 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003458 if (!N)
3459 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003460 }
3461 return N;
3462}
3463
3464// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003465template <typename Alloc, typename Derived>
3466StringView
3467AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003468 const char *Tmp = First;
3469 if (AllowNegative)
3470 consumeIf('n');
3471 if (numLeft() == 0 || !std::isdigit(*First))
3472 return StringView();
3473 while (numLeft() != 0 && std::isdigit(*First))
3474 ++First;
3475 return StringView(Tmp, First);
3476}
3477
3478// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003479template <typename Alloc, typename Derived>
3480bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003481 *Out = 0;
3482 if (look() < '0' || look() > '9')
3483 return true;
3484 while (look() >= '0' && look() <= '9') {
3485 *Out *= 10;
3486 *Out += static_cast<size_t>(consume() - '0');
3487 }
3488 return false;
3489}
3490
Pavel Labathba825192018-10-16 14:29:14 +00003491template <typename Alloc, typename Derived>
3492StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003493 size_t Int = 0;
3494 if (parsePositiveInteger(&Int) || numLeft() < Int)
3495 return StringView();
3496 StringView R(First, First + Int);
3497 First += Int;
3498 return R;
3499}
3500
3501// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3502//
3503// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3504// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3505// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3506//
3507// <ref-qualifier> ::= R # & ref-qualifier
3508// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003509template <typename Derived, typename Alloc>
3510Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003511 Qualifiers CVQuals = parseCVQualifiers();
3512
3513 Node *ExceptionSpec = nullptr;
3514 if (consumeIf("Do")) {
3515 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003516 if (!ExceptionSpec)
3517 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003518 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003519 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003520 if (E == nullptr || !consumeIf('E'))
3521 return nullptr;
3522 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003523 if (!ExceptionSpec)
3524 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003525 } else if (consumeIf("Dw")) {
3526 size_t SpecsBegin = Names.size();
3527 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003528 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003529 if (T == nullptr)
3530 return nullptr;
3531 Names.push_back(T);
3532 }
3533 ExceptionSpec =
3534 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003535 if (!ExceptionSpec)
3536 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003537 }
3538
3539 consumeIf("Dx"); // transaction safe
3540
3541 if (!consumeIf('F'))
3542 return nullptr;
3543 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003544 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003545 if (ReturnType == nullptr)
3546 return nullptr;
3547
3548 FunctionRefQual ReferenceQualifier = FrefQualNone;
3549 size_t ParamsBegin = Names.size();
3550 while (true) {
3551 if (consumeIf('E'))
3552 break;
3553 if (consumeIf('v'))
3554 continue;
3555 if (consumeIf("RE")) {
3556 ReferenceQualifier = FrefQualLValue;
3557 break;
3558 }
3559 if (consumeIf("OE")) {
3560 ReferenceQualifier = FrefQualRValue;
3561 break;
3562 }
Pavel Labathba825192018-10-16 14:29:14 +00003563 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003564 if (T == nullptr)
3565 return nullptr;
3566 Names.push_back(T);
3567 }
3568
3569 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3570 return make<FunctionType>(ReturnType, Params, CVQuals,
3571 ReferenceQualifier, ExceptionSpec);
3572}
3573
3574// extension:
3575// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3576// ::= Dv [<dimension expression>] _ <element type>
3577// <extended element type> ::= <element type>
3578// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003579template <typename Derived, typename Alloc>
3580Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003581 if (!consumeIf("Dv"))
3582 return nullptr;
3583 if (look() >= '1' && look() <= '9') {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003584 Node *DimensionNumber = make<NameType>(parseNumber());
3585 if (!DimensionNumber)
3586 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003587 if (!consumeIf('_'))
3588 return nullptr;
3589 if (consumeIf('p'))
3590 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003591 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003592 if (ElemType == nullptr)
3593 return nullptr;
3594 return make<VectorType>(ElemType, DimensionNumber);
3595 }
3596
3597 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003598 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003599 if (!DimExpr)
3600 return nullptr;
3601 if (!consumeIf('_'))
3602 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003603 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003604 if (!ElemType)
3605 return nullptr;
3606 return make<VectorType>(ElemType, DimExpr);
3607 }
Pavel Labathba825192018-10-16 14:29:14 +00003608 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003609 if (!ElemType)
3610 return nullptr;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003611 return make<VectorType>(ElemType, /*Dimension=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003612}
3613
3614// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3615// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003616template <typename Derived, typename Alloc>
3617Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003618 if (!consumeIf('D'))
3619 return nullptr;
3620 if (!consumeIf('t') && !consumeIf('T'))
3621 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003622 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003623 if (E == nullptr)
3624 return nullptr;
3625 if (!consumeIf('E'))
3626 return nullptr;
3627 return make<EnclosingExpr>("decltype(", E, ")");
3628}
3629
3630// <array-type> ::= A <positive dimension number> _ <element type>
3631// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003632template <typename Derived, typename Alloc>
3633Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003634 if (!consumeIf('A'))
3635 return nullptr;
3636
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003637 Node *Dimension = nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003638
Richard Smithc20d1442018-08-20 20:14:49 +00003639 if (std::isdigit(look())) {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003640 Dimension = make<NameType>(parseNumber());
3641 if (!Dimension)
3642 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003643 if (!consumeIf('_'))
3644 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003645 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003646 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003647 if (DimExpr == nullptr)
3648 return nullptr;
3649 if (!consumeIf('_'))
3650 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003651 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003652 }
3653
Pavel Labathba825192018-10-16 14:29:14 +00003654 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003655 if (Ty == nullptr)
3656 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003657 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003658}
3659
3660// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003661template <typename Derived, typename Alloc>
3662Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003663 if (!consumeIf('M'))
3664 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003665 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003666 if (ClassType == nullptr)
3667 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003668 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003669 if (MemberType == nullptr)
3670 return nullptr;
3671 return make<PointerToMemberType>(ClassType, MemberType);
3672}
3673
3674// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3675// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3676// ::= Tu <name> # dependent elaborated type specifier using 'union'
3677// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003678template <typename Derived, typename Alloc>
3679Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003680 StringView ElabSpef;
3681 if (consumeIf("Ts"))
3682 ElabSpef = "struct";
3683 else if (consumeIf("Tu"))
3684 ElabSpef = "union";
3685 else if (consumeIf("Te"))
3686 ElabSpef = "enum";
3687
Pavel Labathba825192018-10-16 14:29:14 +00003688 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003689 if (Name == nullptr)
3690 return nullptr;
3691
3692 if (!ElabSpef.empty())
3693 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3694
3695 return Name;
3696}
3697
3698// <qualified-type> ::= <qualifiers> <type>
3699// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3700// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003701template <typename Derived, typename Alloc>
3702Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003703 if (consumeIf('U')) {
3704 StringView Qual = parseBareSourceName();
3705 if (Qual.empty())
3706 return nullptr;
3707
Richard Smithc20d1442018-08-20 20:14:49 +00003708 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3709 if (Qual.startsWith("objcproto")) {
3710 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3711 StringView Proto;
3712 {
3713 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3714 SaveLast(Last, ProtoSourceName.end());
3715 Proto = parseBareSourceName();
3716 }
3717 if (Proto.empty())
3718 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003719 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003720 if (Child == nullptr)
3721 return nullptr;
3722 return make<ObjCProtoName>(Child, Proto);
3723 }
3724
Alex Orlovf50df922021-03-24 10:21:32 +04003725 Node *TA = nullptr;
3726 if (look() == 'I') {
3727 TA = getDerived().parseTemplateArgs();
3728 if (TA == nullptr)
3729 return nullptr;
3730 }
3731
Pavel Labathba825192018-10-16 14:29:14 +00003732 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003733 if (Child == nullptr)
3734 return nullptr;
Alex Orlovf50df922021-03-24 10:21:32 +04003735 return make<VendorExtQualType>(Child, Qual, TA);
Richard Smithc20d1442018-08-20 20:14:49 +00003736 }
3737
3738 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003739 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003740 if (Ty == nullptr)
3741 return nullptr;
3742 if (Quals != QualNone)
3743 Ty = make<QualType>(Ty, Quals);
3744 return Ty;
3745}
3746
3747// <type> ::= <builtin-type>
3748// ::= <qualified-type>
3749// ::= <function-type>
3750// ::= <class-enum-type>
3751// ::= <array-type>
3752// ::= <pointer-to-member-type>
3753// ::= <template-param>
3754// ::= <template-template-param> <template-args>
3755// ::= <decltype>
3756// ::= P <type> # pointer
3757// ::= R <type> # l-value reference
3758// ::= O <type> # r-value reference (C++11)
3759// ::= C <type> # complex pair (C99)
3760// ::= G <type> # imaginary (C99)
3761// ::= <substitution> # See Compression below
3762// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3763// extension ::= <vector-type> # <vector-type> starts with Dv
3764//
3765// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3766// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003767template <typename Derived, typename Alloc>
3768Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003769 Node *Result = nullptr;
3770
Richard Smithc20d1442018-08-20 20:14:49 +00003771 switch (look()) {
3772 // ::= <qualified-type>
3773 case 'r':
3774 case 'V':
3775 case 'K': {
3776 unsigned AfterQuals = 0;
3777 if (look(AfterQuals) == 'r') ++AfterQuals;
3778 if (look(AfterQuals) == 'V') ++AfterQuals;
3779 if (look(AfterQuals) == 'K') ++AfterQuals;
3780
3781 if (look(AfterQuals) == 'F' ||
3782 (look(AfterQuals) == 'D' &&
3783 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3784 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003785 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003786 break;
3787 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003788 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003789 }
3790 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003791 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003792 break;
3793 }
3794 // <builtin-type> ::= v # void
3795 case 'v':
3796 ++First;
3797 return make<NameType>("void");
3798 // ::= w # wchar_t
3799 case 'w':
3800 ++First;
3801 return make<NameType>("wchar_t");
3802 // ::= b # bool
3803 case 'b':
3804 ++First;
3805 return make<NameType>("bool");
3806 // ::= c # char
3807 case 'c':
3808 ++First;
3809 return make<NameType>("char");
3810 // ::= a # signed char
3811 case 'a':
3812 ++First;
3813 return make<NameType>("signed char");
3814 // ::= h # unsigned char
3815 case 'h':
3816 ++First;
3817 return make<NameType>("unsigned char");
3818 // ::= s # short
3819 case 's':
3820 ++First;
3821 return make<NameType>("short");
3822 // ::= t # unsigned short
3823 case 't':
3824 ++First;
3825 return make<NameType>("unsigned short");
3826 // ::= i # int
3827 case 'i':
3828 ++First;
3829 return make<NameType>("int");
3830 // ::= j # unsigned int
3831 case 'j':
3832 ++First;
3833 return make<NameType>("unsigned int");
3834 // ::= l # long
3835 case 'l':
3836 ++First;
3837 return make<NameType>("long");
3838 // ::= m # unsigned long
3839 case 'm':
3840 ++First;
3841 return make<NameType>("unsigned long");
3842 // ::= x # long long, __int64
3843 case 'x':
3844 ++First;
3845 return make<NameType>("long long");
3846 // ::= y # unsigned long long, __int64
3847 case 'y':
3848 ++First;
3849 return make<NameType>("unsigned long long");
3850 // ::= n # __int128
3851 case 'n':
3852 ++First;
3853 return make<NameType>("__int128");
3854 // ::= o # unsigned __int128
3855 case 'o':
3856 ++First;
3857 return make<NameType>("unsigned __int128");
3858 // ::= f # float
3859 case 'f':
3860 ++First;
3861 return make<NameType>("float");
3862 // ::= d # double
3863 case 'd':
3864 ++First;
3865 return make<NameType>("double");
3866 // ::= e # long double, __float80
3867 case 'e':
3868 ++First;
3869 return make<NameType>("long double");
3870 // ::= g # __float128
3871 case 'g':
3872 ++First;
3873 return make<NameType>("__float128");
3874 // ::= z # ellipsis
3875 case 'z':
3876 ++First;
3877 return make<NameType>("...");
3878
3879 // <builtin-type> ::= u <source-name> # vendor extended type
3880 case 'u': {
3881 ++First;
3882 StringView Res = parseBareSourceName();
3883 if (Res.empty())
3884 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003885 // Typically, <builtin-type>s are not considered substitution candidates,
3886 // but the exception to that exception is vendor extended types (Itanium C++
3887 // ABI 5.9.1).
3888 Result = make<NameType>(Res);
3889 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003890 }
3891 case 'D':
3892 switch (look(1)) {
3893 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3894 case 'd':
3895 First += 2;
3896 return make<NameType>("decimal64");
3897 // ::= De # IEEE 754r decimal floating point (128 bits)
3898 case 'e':
3899 First += 2;
3900 return make<NameType>("decimal128");
3901 // ::= Df # IEEE 754r decimal floating point (32 bits)
3902 case 'f':
3903 First += 2;
3904 return make<NameType>("decimal32");
3905 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3906 case 'h':
3907 First += 2;
Stuart Bradye8bf5772021-06-07 16:30:22 +01003908 return make<NameType>("half");
Pengfei Wang50e90b82021-09-23 11:02:25 +08003909 // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits)
3910 case 'F': {
3911 First += 2;
3912 Node *DimensionNumber = make<NameType>(parseNumber());
3913 if (!DimensionNumber)
3914 return nullptr;
3915 if (!consumeIf('_'))
3916 return nullptr;
3917 return make<BinaryFPType>(DimensionNumber);
3918 }
Richard Smithc20d1442018-08-20 20:14:49 +00003919 // ::= Di # char32_t
3920 case 'i':
3921 First += 2;
3922 return make<NameType>("char32_t");
3923 // ::= Ds # char16_t
3924 case 's':
3925 First += 2;
3926 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003927 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3928 case 'u':
3929 First += 2;
3930 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003931 // ::= Da # auto (in dependent new-expressions)
3932 case 'a':
3933 First += 2;
3934 return make<NameType>("auto");
3935 // ::= Dc # decltype(auto)
3936 case 'c':
3937 First += 2;
3938 return make<NameType>("decltype(auto)");
3939 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3940 case 'n':
3941 First += 2;
3942 return make<NameType>("std::nullptr_t");
3943
3944 // ::= <decltype>
3945 case 't':
3946 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003947 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003948 break;
3949 }
3950 // extension ::= <vector-type> # <vector-type> starts with Dv
3951 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003952 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003953 break;
3954 }
3955 // ::= Dp <type> # pack expansion (C++0x)
3956 case 'p': {
3957 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003958 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003959 if (!Child)
3960 return nullptr;
3961 Result = make<ParameterPackExpansion>(Child);
3962 break;
3963 }
3964 // Exception specifier on a function type.
3965 case 'o':
3966 case 'O':
3967 case 'w':
3968 // Transaction safe function type.
3969 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003970 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003971 break;
3972 }
3973 break;
3974 // ::= <function-type>
3975 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003976 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003977 break;
3978 }
3979 // ::= <array-type>
3980 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003981 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003982 break;
3983 }
3984 // ::= <pointer-to-member-type>
3985 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003986 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003987 break;
3988 }
3989 // ::= <template-param>
3990 case 'T': {
3991 // This could be an elaborate type specifier on a <class-enum-type>.
3992 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003993 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003994 break;
3995 }
3996
Pavel Labathba825192018-10-16 14:29:14 +00003997 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003998 if (Result == nullptr)
3999 return nullptr;
4000
4001 // Result could be either of:
4002 // <type> ::= <template-param>
4003 // <type> ::= <template-template-param> <template-args>
4004 //
4005 // <template-template-param> ::= <template-param>
4006 // ::= <substitution>
4007 //
4008 // If this is followed by some <template-args>, and we're permitted to
4009 // parse them, take the second production.
4010
4011 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004012 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004013 if (TA == nullptr)
4014 return nullptr;
4015 Result = make<NameWithTemplateArgs>(Result, TA);
4016 }
4017 break;
4018 }
4019 // ::= P <type> # pointer
4020 case 'P': {
4021 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004022 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004023 if (Ptr == nullptr)
4024 return nullptr;
4025 Result = make<PointerType>(Ptr);
4026 break;
4027 }
4028 // ::= R <type> # l-value reference
4029 case 'R': {
4030 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004031 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004032 if (Ref == nullptr)
4033 return nullptr;
4034 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
4035 break;
4036 }
4037 // ::= O <type> # r-value reference (C++11)
4038 case 'O': {
4039 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004040 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004041 if (Ref == nullptr)
4042 return nullptr;
4043 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
4044 break;
4045 }
4046 // ::= C <type> # complex pair (C99)
4047 case 'C': {
4048 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004049 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004050 if (P == nullptr)
4051 return nullptr;
4052 Result = make<PostfixQualifiedType>(P, " complex");
4053 break;
4054 }
4055 // ::= G <type> # imaginary (C99)
4056 case 'G': {
4057 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004058 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004059 if (P == nullptr)
4060 return P;
4061 Result = make<PostfixQualifiedType>(P, " imaginary");
4062 break;
4063 }
4064 // ::= <substitution> # See Compression below
4065 case 'S': {
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004066 if (look(1) != 't') {
4067 Result = getDerived().parseSubstitution();
4068 if (Result == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00004069 return nullptr;
4070
4071 // Sub could be either of:
4072 // <type> ::= <substitution>
4073 // <type> ::= <template-template-param> <template-args>
4074 //
4075 // <template-template-param> ::= <template-param>
4076 // ::= <substitution>
4077 //
4078 // If this is followed by some <template-args>, and we're permitted to
4079 // parse them, take the second production.
4080
4081 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004082 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004083 if (TA == nullptr)
4084 return nullptr;
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004085 Result = make<NameWithTemplateArgs>(Result, TA);
4086 } else {
4087 // If all we parsed was a substitution, don't re-insert into the
4088 // substitution table.
4089 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00004090 }
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004091 break;
Richard Smithc20d1442018-08-20 20:14:49 +00004092 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00004093 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004094 }
4095 // ::= <class-enum-type>
4096 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004097 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004098 break;
4099 }
4100 }
4101
4102 // If we parsed a type, insert it into the substitution table. Note that all
4103 // <builtin-type>s and <substitution>s have already bailed out, because they
4104 // don't get substitutions.
4105 if (Result != nullptr)
4106 Subs.push_back(Result);
4107 return Result;
4108}
4109
Pavel Labathba825192018-10-16 14:29:14 +00004110template <typename Derived, typename Alloc>
4111Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4112 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004113 if (E == nullptr)
4114 return nullptr;
4115 return make<PrefixExpr>(Kind, E);
4116}
4117
Pavel Labathba825192018-10-16 14:29:14 +00004118template <typename Derived, typename Alloc>
4119Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4120 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004121 if (LHS == nullptr)
4122 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004123 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004124 if (RHS == nullptr)
4125 return nullptr;
4126 return make<BinaryExpr>(LHS, Kind, RHS);
4127}
4128
Pavel Labathba825192018-10-16 14:29:14 +00004129template <typename Derived, typename Alloc>
4130Node *
4131AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004132 StringView Tmp = parseNumber(true);
4133 if (!Tmp.empty() && consumeIf('E'))
4134 return make<IntegerLiteral>(Lit, Tmp);
4135 return nullptr;
4136}
4137
4138// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004139template <typename Alloc, typename Derived>
4140Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004141 Qualifiers CVR = QualNone;
4142 if (consumeIf('r'))
4143 CVR |= QualRestrict;
4144 if (consumeIf('V'))
4145 CVR |= QualVolatile;
4146 if (consumeIf('K'))
4147 CVR |= QualConst;
4148 return CVR;
4149}
4150
4151// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4152// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4153// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4154// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L > 0, second and later parameters
Erik Pilkington91c24af2020-05-13 22:19:45 -04004155// ::= fpT # 'this' expression (not part of standard?)
Pavel Labathba825192018-10-16 14:29:14 +00004156template <typename Derived, typename Alloc>
4157Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Erik Pilkington91c24af2020-05-13 22:19:45 -04004158 if (consumeIf("fpT"))
4159 return make<NameType>("this");
Richard Smithc20d1442018-08-20 20:14:49 +00004160 if (consumeIf("fp")) {
4161 parseCVQualifiers();
4162 StringView Num = parseNumber();
4163 if (!consumeIf('_'))
4164 return nullptr;
4165 return make<FunctionParam>(Num);
4166 }
4167 if (consumeIf("fL")) {
4168 if (parseNumber().empty())
4169 return nullptr;
4170 if (!consumeIf('p'))
4171 return nullptr;
4172 parseCVQualifiers();
4173 StringView Num = parseNumber();
4174 if (!consumeIf('_'))
4175 return nullptr;
4176 return make<FunctionParam>(Num);
4177 }
4178 return nullptr;
4179}
4180
4181// [gs] nw <expression>* _ <type> E # new (expr-list) type
4182// [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4183// [gs] na <expression>* _ <type> E # new[] (expr-list) type
4184// [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4185// <initializer> ::= pi <expression>* E # parenthesized initialization
Pavel Labathba825192018-10-16 14:29:14 +00004186template <typename Derived, typename Alloc>
4187Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004188 bool Global = consumeIf("gs");
4189 bool IsArray = look(1) == 'a';
4190 if (!consumeIf("nw") && !consumeIf("na"))
4191 return nullptr;
4192 size_t Exprs = Names.size();
4193 while (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00004194 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004195 if (Ex == nullptr)
4196 return nullptr;
4197 Names.push_back(Ex);
4198 }
4199 NodeArray ExprList = popTrailingNodeArray(Exprs);
Pavel Labathba825192018-10-16 14:29:14 +00004200 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004201 if (Ty == nullptr)
4202 return Ty;
4203 if (consumeIf("pi")) {
4204 size_t InitsBegin = Names.size();
4205 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004206 Node *Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004207 if (Init == nullptr)
4208 return Init;
4209 Names.push_back(Init);
4210 }
4211 NodeArray Inits = popTrailingNodeArray(InitsBegin);
4212 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
4213 } else if (!consumeIf('E'))
4214 return nullptr;
4215 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
4216}
4217
4218// cv <type> <expression> # conversion with one argument
4219// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004220template <typename Derived, typename Alloc>
4221Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004222 if (!consumeIf("cv"))
4223 return nullptr;
4224 Node *Ty;
4225 {
4226 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004227 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004228 }
4229
4230 if (Ty == nullptr)
4231 return nullptr;
4232
4233 if (consumeIf('_')) {
4234 size_t ExprsBegin = Names.size();
4235 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004236 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004237 if (E == nullptr)
4238 return E;
4239 Names.push_back(E);
4240 }
4241 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4242 return make<ConversionExpr>(Ty, Exprs);
4243 }
4244
Pavel Labathba825192018-10-16 14:29:14 +00004245 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004246 if (E[0] == nullptr)
4247 return nullptr;
4248 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4249}
4250
4251// <expr-primary> ::= L <type> <value number> E # integer literal
4252// ::= L <type> <value float> E # floating literal
4253// ::= L <string type> E # string literal
4254// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004255// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004256// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4257// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004258template <typename Derived, typename Alloc>
4259Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004260 if (!consumeIf('L'))
4261 return nullptr;
4262 switch (look()) {
4263 case 'w':
4264 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004265 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004266 case 'b':
4267 if (consumeIf("b0E"))
4268 return make<BoolExpr>(0);
4269 if (consumeIf("b1E"))
4270 return make<BoolExpr>(1);
4271 return nullptr;
4272 case 'c':
4273 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004274 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004275 case 'a':
4276 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004277 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004278 case 'h':
4279 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004280 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004281 case 's':
4282 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004283 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004284 case 't':
4285 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004286 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004287 case 'i':
4288 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004289 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004290 case 'j':
4291 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004292 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004293 case 'l':
4294 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004295 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004296 case 'm':
4297 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004298 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004299 case 'x':
4300 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004301 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004302 case 'y':
4303 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004304 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004305 case 'n':
4306 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004307 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004308 case 'o':
4309 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004310 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004311 case 'f':
4312 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004313 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004314 case 'd':
4315 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004316 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004317 case 'e':
4318 ++First;
Xing Xue3dc5e082020-04-15 09:59:06 -04004319#if defined(__powerpc__) || defined(__s390__)
4320 // Handle cases where long doubles encoded with e have the same size
4321 // and representation as doubles.
4322 return getDerived().template parseFloatingLiteral<double>();
4323#else
Pavel Labathba825192018-10-16 14:29:14 +00004324 return getDerived().template parseFloatingLiteral<long double>();
Xing Xue3dc5e082020-04-15 09:59:06 -04004325#endif
Richard Smithc20d1442018-08-20 20:14:49 +00004326 case '_':
4327 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004328 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004329 if (R != nullptr && consumeIf('E'))
4330 return R;
4331 }
4332 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004333 case 'A': {
4334 Node *T = getDerived().parseType();
4335 if (T == nullptr)
4336 return nullptr;
4337 // FIXME: We need to include the string contents in the mangling.
4338 if (consumeIf('E'))
4339 return make<StringLiteral>(T);
4340 return nullptr;
4341 }
4342 case 'D':
4343 if (consumeIf("DnE"))
4344 return make<NameType>("nullptr");
4345 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004346 case 'T':
4347 // Invalid mangled name per
4348 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4349 return nullptr;
Richard Smithfb917462019-09-09 22:26:04 +00004350 case 'U': {
4351 // FIXME: Should we support LUb... for block literals?
4352 if (look(1) != 'l')
4353 return nullptr;
4354 Node *T = parseUnnamedTypeName(nullptr);
4355 if (!T || !consumeIf('E'))
4356 return nullptr;
4357 return make<LambdaExpr>(T);
4358 }
Richard Smithc20d1442018-08-20 20:14:49 +00004359 default: {
4360 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004361 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004362 if (T == nullptr)
4363 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004364 StringView N = parseNumber(/*AllowNegative=*/true);
Richard Smithfb917462019-09-09 22:26:04 +00004365 if (N.empty())
4366 return nullptr;
4367 if (!consumeIf('E'))
4368 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004369 return make<EnumLiteral>(T, N);
Richard Smithc20d1442018-08-20 20:14:49 +00004370 }
4371 }
4372}
4373
4374// <braced-expression> ::= <expression>
4375// ::= di <field source-name> <braced-expression> # .name = expr
4376// ::= dx <index expression> <braced-expression> # [expr] = expr
4377// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004378template <typename Derived, typename Alloc>
4379Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004380 if (look() == 'd') {
4381 switch (look(1)) {
4382 case 'i': {
4383 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004384 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004385 if (Field == nullptr)
4386 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004387 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004388 if (Init == nullptr)
4389 return nullptr;
4390 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4391 }
4392 case 'x': {
4393 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004394 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004395 if (Index == nullptr)
4396 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004397 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004398 if (Init == nullptr)
4399 return nullptr;
4400 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4401 }
4402 case 'X': {
4403 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004404 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004405 if (RangeBegin == nullptr)
4406 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004407 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004408 if (RangeEnd == nullptr)
4409 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004410 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004411 if (Init == nullptr)
4412 return nullptr;
4413 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4414 }
4415 }
4416 }
Pavel Labathba825192018-10-16 14:29:14 +00004417 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004418}
4419
4420// (not yet in the spec)
4421// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4422// ::= fR <binary-operator-name> <expression> <expression>
4423// ::= fl <binary-operator-name> <expression>
4424// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004425template <typename Derived, typename Alloc>
4426Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004427 if (!consumeIf('f'))
4428 return nullptr;
4429
4430 char FoldKind = look();
4431 bool IsLeftFold, HasInitializer;
4432 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4433 if (FoldKind == 'l' || FoldKind == 'L')
4434 IsLeftFold = true;
4435 else if (FoldKind == 'r' || FoldKind == 'R')
4436 IsLeftFold = false;
4437 else
4438 return nullptr;
4439 ++First;
4440
4441 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4442 StringView OperatorName;
4443 if (consumeIf("aa")) OperatorName = "&&";
4444 else if (consumeIf("an")) OperatorName = "&";
4445 else if (consumeIf("aN")) OperatorName = "&=";
4446 else if (consumeIf("aS")) OperatorName = "=";
4447 else if (consumeIf("cm")) OperatorName = ",";
4448 else if (consumeIf("ds")) OperatorName = ".*";
4449 else if (consumeIf("dv")) OperatorName = "/";
4450 else if (consumeIf("dV")) OperatorName = "/=";
4451 else if (consumeIf("eo")) OperatorName = "^";
4452 else if (consumeIf("eO")) OperatorName = "^=";
4453 else if (consumeIf("eq")) OperatorName = "==";
4454 else if (consumeIf("ge")) OperatorName = ">=";
4455 else if (consumeIf("gt")) OperatorName = ">";
4456 else if (consumeIf("le")) OperatorName = "<=";
4457 else if (consumeIf("ls")) OperatorName = "<<";
4458 else if (consumeIf("lS")) OperatorName = "<<=";
4459 else if (consumeIf("lt")) OperatorName = "<";
4460 else if (consumeIf("mi")) OperatorName = "-";
4461 else if (consumeIf("mI")) OperatorName = "-=";
4462 else if (consumeIf("ml")) OperatorName = "*";
4463 else if (consumeIf("mL")) OperatorName = "*=";
4464 else if (consumeIf("ne")) OperatorName = "!=";
4465 else if (consumeIf("oo")) OperatorName = "||";
4466 else if (consumeIf("or")) OperatorName = "|";
4467 else if (consumeIf("oR")) OperatorName = "|=";
4468 else if (consumeIf("pl")) OperatorName = "+";
4469 else if (consumeIf("pL")) OperatorName = "+=";
4470 else if (consumeIf("rm")) OperatorName = "%";
4471 else if (consumeIf("rM")) OperatorName = "%=";
4472 else if (consumeIf("rs")) OperatorName = ">>";
4473 else if (consumeIf("rS")) OperatorName = ">>=";
4474 else return nullptr;
4475
Pavel Labathba825192018-10-16 14:29:14 +00004476 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004477 if (Pack == nullptr)
4478 return nullptr;
4479 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004480 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004481 if (Init == nullptr)
4482 return nullptr;
4483 }
4484
4485 if (IsLeftFold && Init)
4486 std::swap(Pack, Init);
4487
4488 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4489}
4490
Richard Smith1865d2f2020-10-22 19:29:36 -07004491// <expression> ::= mc <parameter type> <expr> [<offset number>] E
4492//
4493// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4494template <typename Derived, typename Alloc>
4495Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() {
4496 Node *Ty = getDerived().parseType();
4497 if (!Ty)
4498 return nullptr;
4499 Node *Expr = getDerived().parseExpr();
4500 if (!Expr)
4501 return nullptr;
4502 StringView Offset = getDerived().parseNumber(true);
4503 if (!consumeIf('E'))
4504 return nullptr;
4505 return make<PointerToMemberConversionExpr>(Ty, Expr, Offset);
4506}
4507
4508// <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
4509// <union-selector> ::= _ [<number>]
4510//
4511// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4512template <typename Derived, typename Alloc>
4513Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
4514 Node *Ty = getDerived().parseType();
4515 if (!Ty)
4516 return nullptr;
4517 Node *Expr = getDerived().parseExpr();
4518 if (!Expr)
4519 return nullptr;
4520 StringView Offset = getDerived().parseNumber(true);
4521 size_t SelectorsBegin = Names.size();
4522 while (consumeIf('_')) {
4523 Node *Selector = make<NameType>(parseNumber());
4524 if (!Selector)
4525 return nullptr;
4526 Names.push_back(Selector);
4527 }
4528 bool OnePastTheEnd = consumeIf('p');
4529 if (!consumeIf('E'))
4530 return nullptr;
4531 return make<SubobjectExpr>(
4532 Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
4533}
4534
Richard Smithc20d1442018-08-20 20:14:49 +00004535// <expression> ::= <unary operator-name> <expression>
4536// ::= <binary operator-name> <expression> <expression>
4537// ::= <ternary operator-name> <expression> <expression> <expression>
4538// ::= cl <expression>+ E # call
4539// ::= cv <type> <expression> # conversion with one argument
4540// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4541// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4542// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4543// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4544// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4545// ::= [gs] dl <expression> # delete expression
4546// ::= [gs] da <expression> # delete[] expression
4547// ::= pp_ <expression> # prefix ++
4548// ::= mm_ <expression> # prefix --
4549// ::= ti <type> # typeid (type)
4550// ::= te <expression> # typeid (expression)
4551// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4552// ::= sc <type> <expression> # static_cast<type> (expression)
4553// ::= cc <type> <expression> # const_cast<type> (expression)
4554// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4555// ::= st <type> # sizeof (a type)
4556// ::= sz <expression> # sizeof (an expression)
4557// ::= at <type> # alignof (a type)
4558// ::= az <expression> # alignof (an expression)
4559// ::= nx <expression> # noexcept (expression)
4560// ::= <template-param>
4561// ::= <function-param>
4562// ::= dt <expression> <unresolved-name> # expr.name
4563// ::= pt <expression> <unresolved-name> # expr->name
4564// ::= ds <expression> <expression> # expr.*expr
4565// ::= sZ <template-param> # size of a parameter pack
4566// ::= sZ <function-param> # size of a function parameter pack
4567// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4568// ::= sp <expression> # pack expansion
4569// ::= tw <expression> # throw expression
4570// ::= tr # throw with no operand (rethrow)
4571// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4572// # freestanding dependent name (e.g., T::x),
4573// # objectless nonstatic member reference
4574// ::= fL <binary-operator-name> <expression> <expression>
4575// ::= fR <binary-operator-name> <expression> <expression>
4576// ::= fl <binary-operator-name> <expression>
4577// ::= fr <binary-operator-name> <expression>
4578// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004579template <typename Derived, typename Alloc>
4580Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004581 bool Global = consumeIf("gs");
4582 if (numLeft() < 2)
4583 return nullptr;
4584
4585 switch (*First) {
4586 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004587 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004588 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004589 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004590 case 'f': {
4591 // Disambiguate a fold expression from a <function-param>.
4592 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004593 return getDerived().parseFunctionParam();
4594 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004595 }
4596 case 'a':
4597 switch (First[1]) {
4598 case 'a':
4599 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004600 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004601 case 'd':
4602 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004603 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004604 case 'n':
4605 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004606 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004607 case 'N':
4608 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004609 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004610 case 'S':
4611 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004612 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004613 case 't': {
4614 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004615 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004616 if (Ty == nullptr)
4617 return nullptr;
4618 return make<EnclosingExpr>("alignof (", Ty, ")");
4619 }
4620 case 'z': {
4621 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004622 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004623 if (Ty == nullptr)
4624 return nullptr;
4625 return make<EnclosingExpr>("alignof (", Ty, ")");
4626 }
4627 }
4628 return nullptr;
4629 case 'c':
4630 switch (First[1]) {
4631 // cc <type> <expression> # const_cast<type>(expression)
4632 case 'c': {
4633 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004634 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004635 if (Ty == nullptr)
4636 return Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004637 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004638 if (Ex == nullptr)
4639 return Ex;
4640 return make<CastExpr>("const_cast", Ty, Ex);
4641 }
4642 // cl <expression>+ E # call
4643 case 'l': {
4644 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004645 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004646 if (Callee == nullptr)
4647 return Callee;
4648 size_t ExprsBegin = Names.size();
4649 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004650 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004651 if (E == nullptr)
4652 return E;
4653 Names.push_back(E);
4654 }
4655 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4656 }
4657 case 'm':
4658 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004659 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004660 case 'o':
4661 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004662 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004663 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004664 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004665 }
4666 return nullptr;
4667 case 'd':
4668 switch (First[1]) {
4669 case 'a': {
4670 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004671 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004672 if (Ex == nullptr)
4673 return Ex;
4674 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4675 }
4676 case 'c': {
4677 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004678 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004679 if (T == nullptr)
4680 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004681 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004682 if (Ex == nullptr)
4683 return Ex;
4684 return make<CastExpr>("dynamic_cast", T, Ex);
4685 }
4686 case 'e':
4687 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004688 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004689 case 'l': {
4690 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004691 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004692 if (E == nullptr)
4693 return E;
4694 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4695 }
4696 case 'n':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08004697 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004698 case 's': {
4699 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004700 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004701 if (LHS == nullptr)
4702 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004703 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004704 if (RHS == nullptr)
4705 return nullptr;
4706 return make<MemberExpr>(LHS, ".*", RHS);
4707 }
4708 case 't': {
4709 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004710 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004711 if (LHS == nullptr)
4712 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004713 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004714 if (RHS == nullptr)
4715 return nullptr;
4716 return make<MemberExpr>(LHS, ".", RHS);
4717 }
4718 case 'v':
4719 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004720 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004721 case 'V':
4722 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004723 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004724 }
4725 return nullptr;
4726 case 'e':
4727 switch (First[1]) {
4728 case 'o':
4729 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004730 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004731 case 'O':
4732 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004733 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004734 case 'q':
4735 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004736 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004737 }
4738 return nullptr;
4739 case 'g':
4740 switch (First[1]) {
4741 case 'e':
4742 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004743 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004744 case 't':
4745 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004746 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004747 }
4748 return nullptr;
4749 case 'i':
4750 switch (First[1]) {
4751 case 'x': {
4752 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004753 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004754 if (Base == nullptr)
4755 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004756 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004757 if (Index == nullptr)
4758 return Index;
4759 return make<ArraySubscriptExpr>(Base, Index);
4760 }
4761 case 'l': {
4762 First += 2;
4763 size_t InitsBegin = Names.size();
4764 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004765 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004766 if (E == nullptr)
4767 return nullptr;
4768 Names.push_back(E);
4769 }
4770 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4771 }
4772 }
4773 return nullptr;
4774 case 'l':
4775 switch (First[1]) {
4776 case 'e':
4777 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004778 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004779 case 's':
4780 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004781 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004782 case 'S':
4783 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004784 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004785 case 't':
4786 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004787 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004788 }
4789 return nullptr;
4790 case 'm':
4791 switch (First[1]) {
Richard Smith1865d2f2020-10-22 19:29:36 -07004792 case 'c':
4793 First += 2;
4794 return parsePointerToMemberConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004795 case 'i':
4796 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004797 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004798 case 'I':
4799 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004800 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004801 case 'l':
4802 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004803 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004804 case 'L':
4805 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004806 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004807 case 'm':
4808 First += 2;
4809 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004810 return getDerived().parsePrefixExpr("--");
4811 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004812 if (Ex == nullptr)
4813 return nullptr;
4814 return make<PostfixExpr>(Ex, "--");
4815 }
4816 return nullptr;
4817 case 'n':
4818 switch (First[1]) {
4819 case 'a':
4820 case 'w':
Pavel Labathba825192018-10-16 14:29:14 +00004821 return getDerived().parseNewExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004822 case 'e':
4823 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004824 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004825 case 'g':
4826 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004827 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004828 case 't':
4829 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004830 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004831 case 'x':
4832 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004833 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004834 if (Ex == nullptr)
4835 return Ex;
4836 return make<EnclosingExpr>("noexcept (", Ex, ")");
4837 }
4838 return nullptr;
4839 case 'o':
4840 switch (First[1]) {
4841 case 'n':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08004842 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004843 case 'o':
4844 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004845 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004846 case 'r':
4847 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004848 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004849 case 'R':
4850 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004851 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004852 }
4853 return nullptr;
4854 case 'p':
4855 switch (First[1]) {
4856 case 'm':
4857 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004858 return getDerived().parseBinaryExpr("->*");
Richard Smithc20d1442018-08-20 20:14:49 +00004859 case 'l':
4860 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004861 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004862 case 'L':
4863 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004864 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004865 case 'p': {
4866 First += 2;
4867 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004868 return getDerived().parsePrefixExpr("++");
4869 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004870 if (Ex == nullptr)
4871 return Ex;
4872 return make<PostfixExpr>(Ex, "++");
4873 }
4874 case 's':
4875 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004876 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004877 case 't': {
4878 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004879 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004880 if (L == nullptr)
4881 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004882 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004883 if (R == nullptr)
4884 return nullptr;
4885 return make<MemberExpr>(L, "->", R);
4886 }
4887 }
4888 return nullptr;
4889 case 'q':
4890 if (First[1] == 'u') {
4891 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004892 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004893 if (Cond == nullptr)
4894 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004895 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004896 if (LHS == nullptr)
4897 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004898 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004899 if (RHS == nullptr)
4900 return nullptr;
4901 return make<ConditionalExpr>(Cond, LHS, RHS);
4902 }
4903 return nullptr;
4904 case 'r':
4905 switch (First[1]) {
4906 case 'c': {
4907 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004908 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004909 if (T == nullptr)
4910 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004911 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004912 if (Ex == nullptr)
4913 return Ex;
4914 return make<CastExpr>("reinterpret_cast", T, Ex);
4915 }
4916 case 'm':
4917 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004918 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004919 case 'M':
4920 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004921 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004922 case 's':
4923 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004924 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004925 case 'S':
4926 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004927 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004928 }
4929 return nullptr;
4930 case 's':
4931 switch (First[1]) {
4932 case 'c': {
4933 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004934 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004935 if (T == nullptr)
4936 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004937 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004938 if (Ex == nullptr)
4939 return Ex;
4940 return make<CastExpr>("static_cast", T, Ex);
4941 }
Richard Smith1865d2f2020-10-22 19:29:36 -07004942 case 'o':
4943 First += 2;
4944 return parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004945 case 'p': {
4946 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004947 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004948 if (Child == nullptr)
4949 return nullptr;
4950 return make<ParameterPackExpansion>(Child);
4951 }
4952 case 'r':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08004953 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004954 case 't': {
4955 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004956 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004957 if (Ty == nullptr)
4958 return Ty;
4959 return make<EnclosingExpr>("sizeof (", Ty, ")");
4960 }
4961 case 'z': {
4962 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004963 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004964 if (Ex == nullptr)
4965 return Ex;
4966 return make<EnclosingExpr>("sizeof (", Ex, ")");
4967 }
4968 case 'Z':
4969 First += 2;
4970 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004971 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004972 if (R == nullptr)
4973 return nullptr;
4974 return make<SizeofParamPackExpr>(R);
4975 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004976 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004977 if (FP == nullptr)
4978 return nullptr;
4979 return make<EnclosingExpr>("sizeof... (", FP, ")");
4980 }
4981 return nullptr;
4982 case 'P': {
4983 First += 2;
4984 size_t ArgsBegin = Names.size();
4985 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004986 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004987 if (Arg == nullptr)
4988 return nullptr;
4989 Names.push_back(Arg);
4990 }
Richard Smithb485b352018-08-24 23:30:26 +00004991 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4992 if (!Pack)
4993 return nullptr;
4994 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00004995 }
4996 }
4997 return nullptr;
4998 case 't':
4999 switch (First[1]) {
5000 case 'e': {
5001 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005002 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005003 if (Ex == nullptr)
5004 return Ex;
5005 return make<EnclosingExpr>("typeid (", Ex, ")");
5006 }
5007 case 'i': {
5008 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005009 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005010 if (Ty == nullptr)
5011 return Ty;
5012 return make<EnclosingExpr>("typeid (", Ty, ")");
5013 }
5014 case 'l': {
5015 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005016 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005017 if (Ty == nullptr)
5018 return nullptr;
5019 size_t InitsBegin = Names.size();
5020 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005021 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005022 if (E == nullptr)
5023 return nullptr;
5024 Names.push_back(E);
5025 }
5026 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
5027 }
5028 case 'r':
5029 First += 2;
5030 return make<NameType>("throw");
5031 case 'w': {
5032 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005033 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005034 if (Ex == nullptr)
5035 return nullptr;
5036 return make<ThrowExpr>(Ex);
5037 }
5038 }
5039 return nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05005040 case 'u': {
5041 ++First;
5042 Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
5043 if (!Name)
5044 return nullptr;
5045 // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
5046 // standard encoding expects a <template-arg>, and would be otherwise be
5047 // interpreted as <type> node 'short' or 'ellipsis'. However, neither
5048 // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
5049 // actual conflict here.
5050 if (Name->getBaseName() == "__uuidof") {
5051 if (numLeft() < 2)
5052 return nullptr;
5053 if (*First == 't') {
5054 ++First;
5055 Node *Ty = getDerived().parseType();
5056 if (!Ty)
5057 return nullptr;
5058 return make<CallExpr>(Name, makeNodeArray(&Ty, &Ty + 1));
5059 }
5060 if (*First == 'z') {
5061 ++First;
5062 Node *Ex = getDerived().parseExpr();
5063 if (!Ex)
5064 return nullptr;
5065 return make<CallExpr>(Name, makeNodeArray(&Ex, &Ex + 1));
5066 }
5067 }
5068 size_t ExprsBegin = Names.size();
5069 while (!consumeIf('E')) {
5070 Node *E = getDerived().parseTemplateArg();
5071 if (E == nullptr)
5072 return E;
5073 Names.push_back(E);
5074 }
5075 return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin));
5076 }
Richard Smithc20d1442018-08-20 20:14:49 +00005077 case '1':
5078 case '2':
5079 case '3':
5080 case '4':
5081 case '5':
5082 case '6':
5083 case '7':
5084 case '8':
5085 case '9':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08005086 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00005087 }
5088 return nullptr;
5089}
5090
5091// <call-offset> ::= h <nv-offset> _
5092// ::= v <v-offset> _
5093//
5094// <nv-offset> ::= <offset number>
5095// # non-virtual base override
5096//
5097// <v-offset> ::= <offset number> _ <virtual offset number>
5098// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00005099template <typename Alloc, typename Derived>
5100bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00005101 // Just scan through the call offset, we never add this information into the
5102 // output.
5103 if (consumeIf('h'))
5104 return parseNumber(true).empty() || !consumeIf('_');
5105 if (consumeIf('v'))
5106 return parseNumber(true).empty() || !consumeIf('_') ||
5107 parseNumber(true).empty() || !consumeIf('_');
5108 return true;
5109}
5110
5111// <special-name> ::= TV <type> # virtual table
5112// ::= TT <type> # VTT structure (construction vtable index)
5113// ::= TI <type> # typeinfo structure
5114// ::= TS <type> # typeinfo name (null-terminated byte string)
5115// ::= Tc <call-offset> <call-offset> <base encoding>
5116// # base is the nominal target function of thunk
5117// # first call-offset is 'this' adjustment
5118// # second call-offset is result adjustment
5119// ::= T <call-offset> <base encoding>
5120// # base is the nominal target function of thunk
5121// ::= GV <object name> # Guard variable for one-time initialization
5122// # No <type>
5123// ::= TW <object name> # Thread-local wrapper
5124// ::= TH <object name> # Thread-local initialization
5125// ::= GR <object name> _ # First temporary
5126// ::= GR <object name> <seq-id> _ # Subsequent temporaries
5127// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
5128// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00005129template <typename Derived, typename Alloc>
5130Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00005131 switch (look()) {
5132 case 'T':
5133 switch (look(1)) {
Richard Smith1865d2f2020-10-22 19:29:36 -07005134 // TA <template-arg> # template parameter object
5135 //
5136 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
5137 case 'A': {
5138 First += 2;
5139 Node *Arg = getDerived().parseTemplateArg();
5140 if (Arg == nullptr)
5141 return nullptr;
5142 return make<SpecialName>("template parameter object for ", Arg);
5143 }
Richard Smithc20d1442018-08-20 20:14:49 +00005144 // TV <type> # virtual table
5145 case 'V': {
5146 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005147 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005148 if (Ty == nullptr)
5149 return nullptr;
5150 return make<SpecialName>("vtable for ", Ty);
5151 }
5152 // TT <type> # VTT structure (construction vtable index)
5153 case 'T': {
5154 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005155 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005156 if (Ty == nullptr)
5157 return nullptr;
5158 return make<SpecialName>("VTT for ", Ty);
5159 }
5160 // TI <type> # typeinfo structure
5161 case 'I': {
5162 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005163 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005164 if (Ty == nullptr)
5165 return nullptr;
5166 return make<SpecialName>("typeinfo for ", Ty);
5167 }
5168 // TS <type> # typeinfo name (null-terminated byte string)
5169 case 'S': {
5170 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005171 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005172 if (Ty == nullptr)
5173 return nullptr;
5174 return make<SpecialName>("typeinfo name for ", Ty);
5175 }
5176 // Tc <call-offset> <call-offset> <base encoding>
5177 case 'c': {
5178 First += 2;
5179 if (parseCallOffset() || parseCallOffset())
5180 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005181 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005182 if (Encoding == nullptr)
5183 return nullptr;
5184 return make<SpecialName>("covariant return thunk to ", Encoding);
5185 }
5186 // extension ::= TC <first type> <number> _ <second type>
5187 // # construction vtable for second-in-first
5188 case 'C': {
5189 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005190 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005191 if (FirstType == nullptr)
5192 return nullptr;
5193 if (parseNumber(true).empty() || !consumeIf('_'))
5194 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005195 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005196 if (SecondType == nullptr)
5197 return nullptr;
5198 return make<CtorVtableSpecialName>(SecondType, FirstType);
5199 }
5200 // TW <object name> # Thread-local wrapper
5201 case 'W': {
5202 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005203 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005204 if (Name == nullptr)
5205 return nullptr;
5206 return make<SpecialName>("thread-local wrapper routine for ", Name);
5207 }
5208 // TH <object name> # Thread-local initialization
5209 case 'H': {
5210 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005211 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005212 if (Name == nullptr)
5213 return nullptr;
5214 return make<SpecialName>("thread-local initialization routine for ", Name);
5215 }
5216 // T <call-offset> <base encoding>
5217 default: {
5218 ++First;
5219 bool IsVirt = look() == 'v';
5220 if (parseCallOffset())
5221 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005222 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005223 if (BaseEncoding == nullptr)
5224 return nullptr;
5225 if (IsVirt)
5226 return make<SpecialName>("virtual thunk to ", BaseEncoding);
5227 else
5228 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
5229 }
5230 }
5231 case 'G':
5232 switch (look(1)) {
5233 // GV <object name> # Guard variable for one-time initialization
5234 case 'V': {
5235 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005236 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005237 if (Name == nullptr)
5238 return nullptr;
5239 return make<SpecialName>("guard variable for ", Name);
5240 }
5241 // GR <object name> # reference temporary for object
5242 // GR <object name> _ # First temporary
5243 // GR <object name> <seq-id> _ # Subsequent temporaries
5244 case 'R': {
5245 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005246 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005247 if (Name == nullptr)
5248 return nullptr;
5249 size_t Count;
5250 bool ParsedSeqId = !parseSeqId(&Count);
5251 if (!consumeIf('_') && ParsedSeqId)
5252 return nullptr;
5253 return make<SpecialName>("reference temporary for ", Name);
5254 }
5255 }
5256 }
5257 return nullptr;
5258}
5259
5260// <encoding> ::= <function name> <bare-function-type>
5261// ::= <data name>
5262// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00005263template <typename Derived, typename Alloc>
5264Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithfac39712020-07-09 21:08:39 -07005265 // The template parameters of an encoding are unrelated to those of the
5266 // enclosing context.
5267 class SaveTemplateParams {
5268 AbstractManglingParser *Parser;
5269 decltype(TemplateParams) OldParams;
Justin Lebar2c536232021-06-09 16:57:22 -07005270 decltype(OuterTemplateParams) OldOuterParams;
Richard Smithfac39712020-07-09 21:08:39 -07005271
5272 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04005273 SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
Richard Smithfac39712020-07-09 21:08:39 -07005274 OldParams = std::move(Parser->TemplateParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005275 OldOuterParams = std::move(Parser->OuterTemplateParams);
Richard Smithfac39712020-07-09 21:08:39 -07005276 Parser->TemplateParams.clear();
Justin Lebar2c536232021-06-09 16:57:22 -07005277 Parser->OuterTemplateParams.clear();
Richard Smithfac39712020-07-09 21:08:39 -07005278 }
5279 ~SaveTemplateParams() {
5280 Parser->TemplateParams = std::move(OldParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005281 Parser->OuterTemplateParams = std::move(OldOuterParams);
Richard Smithfac39712020-07-09 21:08:39 -07005282 }
5283 } SaveTemplateParams(this);
Richard Smithfd434322020-07-09 20:36:04 -07005284
Richard Smithc20d1442018-08-20 20:14:49 +00005285 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00005286 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00005287
5288 auto IsEndOfEncoding = [&] {
5289 // The set of chars that can potentially follow an <encoding> (none of which
5290 // can start a <type>). Enumerating these allows us to avoid speculative
5291 // parsing.
5292 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5293 };
5294
5295 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00005296 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00005297 if (Name == nullptr)
5298 return nullptr;
5299
5300 if (resolveForwardTemplateRefs(NameInfo))
5301 return nullptr;
5302
5303 if (IsEndOfEncoding())
5304 return Name;
5305
5306 Node *Attrs = nullptr;
5307 if (consumeIf("Ua9enable_ifI")) {
5308 size_t BeforeArgs = Names.size();
5309 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005310 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005311 if (Arg == nullptr)
5312 return nullptr;
5313 Names.push_back(Arg);
5314 }
5315 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00005316 if (!Attrs)
5317 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005318 }
5319
5320 Node *ReturnType = nullptr;
5321 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00005322 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005323 if (ReturnType == nullptr)
5324 return nullptr;
5325 }
5326
5327 if (consumeIf('v'))
5328 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5329 Attrs, NameInfo.CVQualifiers,
5330 NameInfo.ReferenceQualifier);
5331
5332 size_t ParamsBegin = Names.size();
5333 do {
Pavel Labathba825192018-10-16 14:29:14 +00005334 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005335 if (Ty == nullptr)
5336 return nullptr;
5337 Names.push_back(Ty);
5338 } while (!IsEndOfEncoding());
5339
5340 return make<FunctionEncoding>(ReturnType, Name,
5341 popTrailingNodeArray(ParamsBegin),
5342 Attrs, NameInfo.CVQualifiers,
5343 NameInfo.ReferenceQualifier);
5344}
5345
5346template <class Float>
5347struct FloatData;
5348
5349template <>
5350struct FloatData<float>
5351{
5352 static const size_t mangled_size = 8;
5353 static const size_t max_demangled_size = 24;
5354 static constexpr const char* spec = "%af";
5355};
5356
5357template <>
5358struct FloatData<double>
5359{
5360 static const size_t mangled_size = 16;
5361 static const size_t max_demangled_size = 32;
5362 static constexpr const char* spec = "%a";
5363};
5364
5365template <>
5366struct FloatData<long double>
5367{
5368#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5369 defined(__wasm__)
5370 static const size_t mangled_size = 32;
5371#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5372 static const size_t mangled_size = 16;
5373#else
5374 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5375#endif
Elliott Hughes5a360ea2020-04-10 17:42:00 -07005376 // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
5377 // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
5378 // Negatives are one character longer than positives.
5379 // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
5380 // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
5381 static const size_t max_demangled_size = 42;
Richard Smithc20d1442018-08-20 20:14:49 +00005382 static constexpr const char *spec = "%LaL";
5383};
5384
Pavel Labathba825192018-10-16 14:29:14 +00005385template <typename Alloc, typename Derived>
5386template <class Float>
5387Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005388 const size_t N = FloatData<Float>::mangled_size;
5389 if (numLeft() <= N)
5390 return nullptr;
5391 StringView Data(First, First + N);
5392 for (char C : Data)
5393 if (!std::isxdigit(C))
5394 return nullptr;
5395 First += N;
5396 if (!consumeIf('E'))
5397 return nullptr;
5398 return make<FloatLiteralImpl<Float>>(Data);
5399}
5400
5401// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005402template <typename Alloc, typename Derived>
5403bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005404 if (!(look() >= '0' && look() <= '9') &&
5405 !(look() >= 'A' && look() <= 'Z'))
5406 return true;
5407
5408 size_t Id = 0;
5409 while (true) {
5410 if (look() >= '0' && look() <= '9') {
5411 Id *= 36;
5412 Id += static_cast<size_t>(look() - '0');
5413 } else if (look() >= 'A' && look() <= 'Z') {
5414 Id *= 36;
5415 Id += static_cast<size_t>(look() - 'A') + 10;
5416 } else {
5417 *Out = Id;
5418 return false;
5419 }
5420 ++First;
5421 }
5422}
5423
5424// <substitution> ::= S <seq-id> _
5425// ::= S_
5426// <substitution> ::= Sa # ::std::allocator
5427// <substitution> ::= Sb # ::std::basic_string
5428// <substitution> ::= Ss # ::std::basic_string < char,
5429// ::std::char_traits<char>,
5430// ::std::allocator<char> >
5431// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5432// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5433// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Pavel Labathba825192018-10-16 14:29:14 +00005434template <typename Derived, typename Alloc>
5435Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005436 if (!consumeIf('S'))
5437 return nullptr;
5438
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08005439 if (look() >= 'a' && look() <= 'z') {
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005440 SpecialSubKind Kind;
Richard Smithc20d1442018-08-20 20:14:49 +00005441 switch (look()) {
5442 case 'a':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005443 Kind = SpecialSubKind::allocator;
Richard Smithc20d1442018-08-20 20:14:49 +00005444 break;
5445 case 'b':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005446 Kind = SpecialSubKind::basic_string;
Richard Smithc20d1442018-08-20 20:14:49 +00005447 break;
5448 case 'd':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005449 Kind = SpecialSubKind::iostream;
5450 break;
5451 case 'i':
5452 Kind = SpecialSubKind::istream;
5453 break;
5454 case 'o':
5455 Kind = SpecialSubKind::ostream;
5456 break;
5457 case 's':
5458 Kind = SpecialSubKind::string;
Richard Smithc20d1442018-08-20 20:14:49 +00005459 break;
5460 default:
5461 return nullptr;
5462 }
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005463 ++First;
5464 auto *SpecialSub = make<SpecialSubstitution>(Kind);
Richard Smithb485b352018-08-24 23:30:26 +00005465 if (!SpecialSub)
5466 return nullptr;
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005467
Richard Smithc20d1442018-08-20 20:14:49 +00005468 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5469 // has ABI tags, the tags are appended to the substitution; the result is a
5470 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005471 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005472 if (WithTags != SpecialSub) {
5473 Subs.push_back(WithTags);
5474 SpecialSub = WithTags;
5475 }
5476 return SpecialSub;
5477 }
5478
5479 // ::= S_
5480 if (consumeIf('_')) {
5481 if (Subs.empty())
5482 return nullptr;
5483 return Subs[0];
5484 }
5485
5486 // ::= S <seq-id> _
5487 size_t Index = 0;
5488 if (parseSeqId(&Index))
5489 return nullptr;
5490 ++Index;
5491 if (!consumeIf('_') || Index >= Subs.size())
5492 return nullptr;
5493 return Subs[Index];
5494}
5495
5496// <template-param> ::= T_ # first template parameter
5497// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005498// ::= TL <level-1> __
5499// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005500template <typename Derived, typename Alloc>
5501Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005502 if (!consumeIf('T'))
5503 return nullptr;
5504
Richard Smithdf1c14c2019-09-06 23:53:21 +00005505 size_t Level = 0;
5506 if (consumeIf('L')) {
5507 if (parsePositiveInteger(&Level))
5508 return nullptr;
5509 ++Level;
5510 if (!consumeIf('_'))
5511 return nullptr;
5512 }
5513
Richard Smithc20d1442018-08-20 20:14:49 +00005514 size_t Index = 0;
5515 if (!consumeIf('_')) {
5516 if (parsePositiveInteger(&Index))
5517 return nullptr;
5518 ++Index;
5519 if (!consumeIf('_'))
5520 return nullptr;
5521 }
5522
Richard Smithc20d1442018-08-20 20:14:49 +00005523 // If we're in a context where this <template-param> refers to a
5524 // <template-arg> further ahead in the mangled name (currently just conversion
5525 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005526 // This can only happen at the outermost level.
5527 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005528 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5529 if (!ForwardRef)
5530 return nullptr;
5531 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5532 ForwardTemplateRefs.push_back(
5533 static_cast<ForwardTemplateReference *>(ForwardRef));
5534 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005535 }
5536
Richard Smithdf1c14c2019-09-06 23:53:21 +00005537 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5538 Index >= TemplateParams[Level]->size()) {
5539 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5540 // list are mangled as the corresponding artificial template type parameter.
5541 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5542 // This will be popped by the ScopedTemplateParamList in
5543 // parseUnnamedTypeName.
5544 if (Level == TemplateParams.size())
5545 TemplateParams.push_back(nullptr);
5546 return make<NameType>("auto");
5547 }
5548
Richard Smithc20d1442018-08-20 20:14:49 +00005549 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005550 }
5551
5552 return (*TemplateParams[Level])[Index];
5553}
5554
5555// <template-param-decl> ::= Ty # type parameter
5556// ::= Tn <type> # non-type parameter
5557// ::= Tt <template-param-decl>* E # template parameter
5558// ::= Tp <template-param-decl> # parameter pack
5559template <typename Derived, typename Alloc>
5560Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5561 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5562 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5563 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5564 if (N) TemplateParams.back()->push_back(N);
5565 return N;
5566 };
5567
5568 if (consumeIf("Ty")) {
5569 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5570 if (!Name)
5571 return nullptr;
5572 return make<TypeTemplateParamDecl>(Name);
5573 }
5574
5575 if (consumeIf("Tn")) {
5576 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5577 if (!Name)
5578 return nullptr;
5579 Node *Type = parseType();
5580 if (!Type)
5581 return nullptr;
5582 return make<NonTypeTemplateParamDecl>(Name, Type);
5583 }
5584
5585 if (consumeIf("Tt")) {
5586 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5587 if (!Name)
5588 return nullptr;
5589 size_t ParamsBegin = Names.size();
5590 ScopedTemplateParamList TemplateTemplateParamParams(this);
5591 while (!consumeIf("E")) {
5592 Node *P = parseTemplateParamDecl();
5593 if (!P)
5594 return nullptr;
5595 Names.push_back(P);
5596 }
5597 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5598 return make<TemplateTemplateParamDecl>(Name, Params);
5599 }
5600
5601 if (consumeIf("Tp")) {
5602 Node *P = parseTemplateParamDecl();
5603 if (!P)
5604 return nullptr;
5605 return make<TemplateParamPackDecl>(P);
5606 }
5607
5608 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005609}
5610
5611// <template-arg> ::= <type> # type or template
5612// ::= X <expression> E # expression
5613// ::= <expr-primary> # simple expressions
5614// ::= J <template-arg>* E # argument pack
5615// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005616template <typename Derived, typename Alloc>
5617Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005618 switch (look()) {
5619 case 'X': {
5620 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005621 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005622 if (Arg == nullptr || !consumeIf('E'))
5623 return nullptr;
5624 return Arg;
5625 }
5626 case 'J': {
5627 ++First;
5628 size_t ArgsBegin = Names.size();
5629 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005630 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005631 if (Arg == nullptr)
5632 return nullptr;
5633 Names.push_back(Arg);
5634 }
5635 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5636 return make<TemplateArgumentPack>(Args);
5637 }
5638 case 'L': {
5639 // ::= LZ <encoding> E # extension
5640 if (look(1) == 'Z') {
5641 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005642 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005643 if (Arg == nullptr || !consumeIf('E'))
5644 return nullptr;
5645 return Arg;
5646 }
5647 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005648 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005649 }
5650 default:
Pavel Labathba825192018-10-16 14:29:14 +00005651 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005652 }
5653}
5654
5655// <template-args> ::= I <template-arg>* E
5656// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005657template <typename Derived, typename Alloc>
5658Node *
5659AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005660 if (!consumeIf('I'))
5661 return nullptr;
5662
5663 // <template-params> refer to the innermost <template-args>. Clear out any
5664 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005665 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005666 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005667 TemplateParams.push_back(&OuterTemplateParams);
5668 OuterTemplateParams.clear();
5669 }
Richard Smithc20d1442018-08-20 20:14:49 +00005670
5671 size_t ArgsBegin = Names.size();
5672 while (!consumeIf('E')) {
5673 if (TagTemplates) {
5674 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005675 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005676 TemplateParams = std::move(OldParams);
5677 if (Arg == nullptr)
5678 return nullptr;
5679 Names.push_back(Arg);
5680 Node *TableEntry = Arg;
5681 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5682 TableEntry = make<ParameterPack>(
5683 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005684 if (!TableEntry)
5685 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005686 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005687 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005688 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005689 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005690 if (Arg == nullptr)
5691 return nullptr;
5692 Names.push_back(Arg);
5693 }
5694 }
5695 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5696}
5697
5698// <mangled-name> ::= _Z <encoding>
5699// ::= <type>
5700// extension ::= ___Z <encoding> _block_invoke
5701// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5702// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005703template <typename Derived, typename Alloc>
5704Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005705 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005706 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005707 if (Encoding == nullptr)
5708 return nullptr;
5709 if (look() == '.') {
5710 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5711 First = Last;
5712 }
5713 if (numLeft() != 0)
5714 return nullptr;
5715 return Encoding;
5716 }
5717
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005718 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005719 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005720 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5721 return nullptr;
5722 bool RequireNumber = consumeIf('_');
5723 if (parseNumber().empty() && RequireNumber)
5724 return nullptr;
5725 if (look() == '.')
5726 First = Last;
5727 if (numLeft() != 0)
5728 return nullptr;
5729 return make<SpecialName>("invocation function for block in ", Encoding);
5730 }
5731
Pavel Labathba825192018-10-16 14:29:14 +00005732 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005733 if (numLeft() != 0)
5734 return nullptr;
5735 return Ty;
5736}
5737
Pavel Labathba825192018-10-16 14:29:14 +00005738template <typename Alloc>
5739struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5740 using AbstractManglingParser<ManglingParser<Alloc>,
5741 Alloc>::AbstractManglingParser;
5742};
5743
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005744DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005745
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005746#endif // DEMANGLE_ITANIUMDEMANGLE_H