blob: db65c60e7e5e6cd1a07f82415bf65df3bc5219dc [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) \
76 X(StdQualifiedName) \
77 X(ExpandedSpecialSubstitution) \
78 X(SpecialSubstitution) \
79 X(CtorDtorName) \
80 X(DtorName) \
81 X(UnnamedTypeName) \
82 X(ClosureTypeName) \
83 X(StructuredBindingName) \
84 X(BinaryExpr) \
85 X(ArraySubscriptExpr) \
86 X(PostfixExpr) \
87 X(ConditionalExpr) \
88 X(MemberExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070089 X(SubobjectExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000090 X(EnclosingExpr) \
91 X(CastExpr) \
92 X(SizeofParamPackExpr) \
93 X(CallExpr) \
94 X(NewExpr) \
95 X(DeleteExpr) \
96 X(PrefixExpr) \
97 X(FunctionParam) \
98 X(ConversionExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070099 X(PointerToMemberConversionExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +0000100 X(InitListExpr) \
101 X(FoldExpr) \
102 X(ThrowExpr) \
103 X(BoolExpr) \
Richard Smithdf1c14c2019-09-06 23:53:21 +0000104 X(StringLiteral) \
105 X(LambdaExpr) \
Erik Pilkington0a170f12020-05-13 14:13:37 -0400106 X(EnumLiteral) \
Richard Smithc20d1442018-08-20 20:14:49 +0000107 X(IntegerLiteral) \
108 X(FloatLiteral) \
109 X(DoubleLiteral) \
110 X(LongDoubleLiteral) \
111 X(BracedExpr) \
112 X(BracedRangeExpr)
113
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000114DEMANGLE_NAMESPACE_BEGIN
115
Mikhail Borisov8452f062021-08-17 18:06:53 -0400116template <class T, size_t N> class PODSmallVector {
117 static_assert(std::is_pod<T>::value,
118 "T is required to be a plain old data type");
119
120 T *First = nullptr;
121 T *Last = nullptr;
122 T *Cap = nullptr;
123 T Inline[N] = {0};
124
125 bool isInline() const { return First == Inline; }
126
127 void clearInline() {
128 First = Inline;
129 Last = Inline;
130 Cap = Inline + N;
131 }
132
133 void reserve(size_t NewCap) {
134 size_t S = size();
135 if (isInline()) {
136 auto *Tmp = static_cast<T *>(std::malloc(NewCap * sizeof(T)));
137 if (Tmp == nullptr)
138 std::terminate();
139 std::copy(First, Last, Tmp);
140 First = Tmp;
141 } else {
142 First = static_cast<T *>(std::realloc(First, NewCap * sizeof(T)));
143 if (First == nullptr)
144 std::terminate();
145 }
146 Last = First + S;
147 Cap = First + NewCap;
148 }
149
150public:
151 PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
152
153 PODSmallVector(const PODSmallVector &) = delete;
154 PODSmallVector &operator=(const PODSmallVector &) = delete;
155
156 PODSmallVector(PODSmallVector &&Other) : PODSmallVector() {
157 if (Other.isInline()) {
158 std::copy(Other.begin(), Other.end(), First);
159 Last = First + Other.size();
160 Other.clear();
161 return;
162 }
163
164 First = Other.First;
165 Last = Other.Last;
166 Cap = Other.Cap;
167 Other.clearInline();
168 }
169
170 PODSmallVector &operator=(PODSmallVector &&Other) {
171 if (Other.isInline()) {
172 if (!isInline()) {
173 std::free(First);
174 clearInline();
175 }
176 std::copy(Other.begin(), Other.end(), First);
177 Last = First + Other.size();
178 Other.clear();
179 return *this;
180 }
181
182 if (isInline()) {
183 First = Other.First;
184 Last = Other.Last;
185 Cap = Other.Cap;
186 Other.clearInline();
187 return *this;
188 }
189
190 std::swap(First, Other.First);
191 std::swap(Last, Other.Last);
192 std::swap(Cap, Other.Cap);
193 Other.clear();
194 return *this;
195 }
196
197 // NOLINTNEXTLINE(readability-identifier-naming)
198 void push_back(const T &Elem) {
199 if (Last == Cap)
200 reserve(size() * 2);
201 *Last++ = Elem;
202 }
203
204 // NOLINTNEXTLINE(readability-identifier-naming)
205 void pop_back() {
206 assert(Last != First && "Popping empty vector!");
207 --Last;
208 }
209
210 void dropBack(size_t Index) {
211 assert(Index <= size() && "dropBack() can't expand!");
212 Last = First + Index;
213 }
214
215 T *begin() { return First; }
216 T *end() { return Last; }
217
218 bool empty() const { return First == Last; }
219 size_t size() const { return static_cast<size_t>(Last - First); }
220 T &back() {
221 assert(Last != First && "Calling back() on empty vector!");
222 return *(Last - 1);
223 }
224 T &operator[](size_t Index) {
225 assert(Index < size() && "Invalid access!");
226 return *(begin() + Index);
227 }
228 void clear() { Last = First; }
229
230 ~PODSmallVector() {
231 if (!isInline())
232 std::free(First);
233 }
234};
235
Richard Smithc20d1442018-08-20 20:14:49 +0000236// Base class of all AST nodes. The AST is built by the parser, then is
237// traversed by the printLeft/Right functions to produce a demangled string.
238class Node {
239public:
240 enum Kind : unsigned char {
241#define ENUMERATOR(NodeKind) K ## NodeKind,
242 FOR_EACH_NODE_KIND(ENUMERATOR)
243#undef ENUMERATOR
244 };
245
246 /// Three-way bool to track a cached value. Unknown is possible if this node
247 /// has an unexpanded parameter pack below it that may affect this cache.
248 enum class Cache : unsigned char { Yes, No, Unknown, };
249
250private:
251 Kind K;
252
253 // FIXME: Make these protected.
254public:
255 /// Tracks if this node has a component on its right side, in which case we
256 /// need to call printRight.
257 Cache RHSComponentCache;
258
259 /// Track if this node is a (possibly qualified) array type. This can affect
260 /// how we format the output string.
261 Cache ArrayCache;
262
263 /// Track if this node is a (possibly qualified) function type. This can
264 /// affect how we format the output string.
265 Cache FunctionCache;
266
267public:
268 Node(Kind K_, Cache RHSComponentCache_ = Cache::No,
269 Cache ArrayCache_ = Cache::No, Cache FunctionCache_ = Cache::No)
270 : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_),
271 FunctionCache(FunctionCache_) {}
272
273 /// Visit the most-derived object corresponding to this object.
274 template<typename Fn> void visit(Fn F) const;
275
276 // The following function is provided by all derived classes:
277 //
278 // Call F with arguments that, when passed to the constructor of this node,
279 // would construct an equivalent node.
280 //template<typename Fn> void match(Fn F) const;
281
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700282 bool hasRHSComponent(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000283 if (RHSComponentCache != Cache::Unknown)
284 return RHSComponentCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700285 return hasRHSComponentSlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000286 }
287
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700288 bool hasArray(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000289 if (ArrayCache != Cache::Unknown)
290 return ArrayCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700291 return hasArraySlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000292 }
293
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700294 bool hasFunction(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000295 if (FunctionCache != Cache::Unknown)
296 return FunctionCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700297 return hasFunctionSlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000298 }
299
300 Kind getKind() const { return K; }
301
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700302 virtual bool hasRHSComponentSlow(OutputBuffer &) const { return false; }
303 virtual bool hasArraySlow(OutputBuffer &) const { return false; }
304 virtual bool hasFunctionSlow(OutputBuffer &) const { return false; }
Richard Smithc20d1442018-08-20 20:14:49 +0000305
306 // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
307 // get at a node that actually represents some concrete syntax.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700308 virtual const Node *getSyntaxNode(OutputBuffer &) const { return this; }
Richard Smithc20d1442018-08-20 20:14:49 +0000309
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700310 void print(OutputBuffer &OB) const {
311 printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000312 if (RHSComponentCache != Cache::No)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700313 printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000314 }
315
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -0800316 // Print the "left" side of this Node into OutputBuffer.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700317 virtual void printLeft(OutputBuffer &) const = 0;
Richard Smithc20d1442018-08-20 20:14:49 +0000318
319 // Print the "right". This distinction is necessary to represent C++ types
320 // that appear on the RHS of their subtype, such as arrays or functions.
321 // Since most types don't have such a component, provide a default
322 // implementation.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700323 virtual void printRight(OutputBuffer &) const {}
Richard Smithc20d1442018-08-20 20:14:49 +0000324
325 virtual StringView getBaseName() const { return StringView(); }
326
327 // Silence compiler warnings, this dtor will never be called.
328 virtual ~Node() = default;
329
330#ifndef NDEBUG
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000331 DEMANGLE_DUMP_METHOD void dump() const;
Richard Smithc20d1442018-08-20 20:14:49 +0000332#endif
333};
334
335class NodeArray {
336 Node **Elements;
337 size_t NumElements;
338
339public:
340 NodeArray() : Elements(nullptr), NumElements(0) {}
341 NodeArray(Node **Elements_, size_t NumElements_)
342 : Elements(Elements_), NumElements(NumElements_) {}
343
344 bool empty() const { return NumElements == 0; }
345 size_t size() const { return NumElements; }
346
347 Node **begin() const { return Elements; }
348 Node **end() const { return Elements + NumElements; }
349
350 Node *operator[](size_t Idx) const { return Elements[Idx]; }
351
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700352 void printWithComma(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000353 bool FirstElement = true;
354 for (size_t Idx = 0; Idx != NumElements; ++Idx) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700355 size_t BeforeComma = OB.getCurrentPosition();
Richard Smithc20d1442018-08-20 20:14:49 +0000356 if (!FirstElement)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700357 OB += ", ";
358 size_t AfterComma = OB.getCurrentPosition();
359 Elements[Idx]->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000360
361 // Elements[Idx] is an empty parameter pack expansion, we should erase the
362 // comma we just printed.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700363 if (AfterComma == OB.getCurrentPosition()) {
364 OB.setCurrentPosition(BeforeComma);
Richard Smithc20d1442018-08-20 20:14:49 +0000365 continue;
366 }
367
368 FirstElement = false;
369 }
370 }
371};
372
373struct NodeArrayNode : Node {
374 NodeArray Array;
375 NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
376
377 template<typename Fn> void match(Fn F) const { F(Array); }
378
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700379 void printLeft(OutputBuffer &OB) const override { Array.printWithComma(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000380};
381
382class DotSuffix final : public Node {
383 const Node *Prefix;
384 const StringView Suffix;
385
386public:
387 DotSuffix(const Node *Prefix_, StringView Suffix_)
388 : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
389
390 template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
391
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700392 void printLeft(OutputBuffer &OB) const override {
393 Prefix->print(OB);
394 OB += " (";
395 OB += Suffix;
396 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000397 }
398};
399
400class VendorExtQualType final : public Node {
401 const Node *Ty;
402 StringView Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400403 const Node *TA;
Richard Smithc20d1442018-08-20 20:14:49 +0000404
405public:
Alex Orlovf50df922021-03-24 10:21:32 +0400406 VendorExtQualType(const Node *Ty_, StringView Ext_, const Node *TA_)
407 : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_), TA(TA_) {}
Richard Smithc20d1442018-08-20 20:14:49 +0000408
Alex Orlovf50df922021-03-24 10:21:32 +0400409 template <typename Fn> void match(Fn F) const { F(Ty, Ext, TA); }
Richard Smithc20d1442018-08-20 20:14:49 +0000410
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700411 void printLeft(OutputBuffer &OB) const override {
412 Ty->print(OB);
413 OB += " ";
414 OB += Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400415 if (TA != nullptr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700416 TA->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000417 }
418};
419
420enum FunctionRefQual : unsigned char {
421 FrefQualNone,
422 FrefQualLValue,
423 FrefQualRValue,
424};
425
426enum Qualifiers {
427 QualNone = 0,
428 QualConst = 0x1,
429 QualVolatile = 0x2,
430 QualRestrict = 0x4,
431};
432
433inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
434 return Q1 = static_cast<Qualifiers>(Q1 | Q2);
435}
436
Richard Smithdf1c14c2019-09-06 23:53:21 +0000437class QualType final : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +0000438protected:
439 const Qualifiers Quals;
440 const Node *Child;
441
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700442 void printQuals(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000443 if (Quals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700444 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000445 if (Quals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700446 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000447 if (Quals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700448 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000449 }
450
451public:
452 QualType(const Node *Child_, Qualifiers Quals_)
453 : Node(KQualType, Child_->RHSComponentCache,
454 Child_->ArrayCache, Child_->FunctionCache),
455 Quals(Quals_), Child(Child_) {}
456
457 template<typename Fn> void match(Fn F) const { F(Child, Quals); }
458
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700459 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
460 return Child->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000461 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700462 bool hasArraySlow(OutputBuffer &OB) const override {
463 return Child->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000464 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700465 bool hasFunctionSlow(OutputBuffer &OB) const override {
466 return Child->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000467 }
468
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700469 void printLeft(OutputBuffer &OB) const override {
470 Child->printLeft(OB);
471 printQuals(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000472 }
473
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700474 void printRight(OutputBuffer &OB) const override { Child->printRight(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000475};
476
477class ConversionOperatorType final : public Node {
478 const Node *Ty;
479
480public:
481 ConversionOperatorType(const Node *Ty_)
482 : Node(KConversionOperatorType), Ty(Ty_) {}
483
484 template<typename Fn> void match(Fn F) const { F(Ty); }
485
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700486 void printLeft(OutputBuffer &OB) const override {
487 OB += "operator ";
488 Ty->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000489 }
490};
491
492class PostfixQualifiedType final : public Node {
493 const Node *Ty;
494 const StringView Postfix;
495
496public:
497 PostfixQualifiedType(Node *Ty_, StringView Postfix_)
498 : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
499
500 template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
501
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700502 void printLeft(OutputBuffer &OB) const override {
503 Ty->printLeft(OB);
504 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +0000505 }
506};
507
508class NameType final : public Node {
509 const StringView Name;
510
511public:
512 NameType(StringView Name_) : Node(KNameType), Name(Name_) {}
513
514 template<typename Fn> void match(Fn F) const { F(Name); }
515
516 StringView getName() const { return Name; }
517 StringView getBaseName() const override { return Name; }
518
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700519 void printLeft(OutputBuffer &OB) const override { OB += Name; }
Richard Smithc20d1442018-08-20 20:14:49 +0000520};
521
522class ElaboratedTypeSpefType : public Node {
523 StringView Kind;
524 Node *Child;
525public:
526 ElaboratedTypeSpefType(StringView Kind_, Node *Child_)
527 : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
528
529 template<typename Fn> void match(Fn F) const { F(Kind, Child); }
530
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700531 void printLeft(OutputBuffer &OB) const override {
532 OB += Kind;
533 OB += ' ';
534 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000535 }
536};
537
538struct AbiTagAttr : Node {
539 Node *Base;
540 StringView Tag;
541
542 AbiTagAttr(Node* Base_, StringView Tag_)
543 : Node(KAbiTagAttr, Base_->RHSComponentCache,
544 Base_->ArrayCache, Base_->FunctionCache),
545 Base(Base_), Tag(Tag_) {}
546
547 template<typename Fn> void match(Fn F) const { F(Base, Tag); }
548
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700549 void printLeft(OutputBuffer &OB) const override {
550 Base->printLeft(OB);
551 OB += "[abi:";
552 OB += Tag;
553 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +0000554 }
555};
556
557class EnableIfAttr : public Node {
558 NodeArray Conditions;
559public:
560 EnableIfAttr(NodeArray Conditions_)
561 : Node(KEnableIfAttr), Conditions(Conditions_) {}
562
563 template<typename Fn> void match(Fn F) const { F(Conditions); }
564
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700565 void printLeft(OutputBuffer &OB) const override {
566 OB += " [enable_if:";
567 Conditions.printWithComma(OB);
568 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +0000569 }
570};
571
572class ObjCProtoName : public Node {
573 const Node *Ty;
574 StringView Protocol;
575
576 friend class PointerType;
577
578public:
579 ObjCProtoName(const Node *Ty_, StringView Protocol_)
580 : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
581
582 template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
583
584 bool isObjCObject() const {
585 return Ty->getKind() == KNameType &&
586 static_cast<const NameType *>(Ty)->getName() == "objc_object";
587 }
588
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700589 void printLeft(OutputBuffer &OB) const override {
590 Ty->print(OB);
591 OB += "<";
592 OB += Protocol;
593 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +0000594 }
595};
596
597class PointerType final : public Node {
598 const Node *Pointee;
599
600public:
601 PointerType(const Node *Pointee_)
602 : Node(KPointerType, Pointee_->RHSComponentCache),
603 Pointee(Pointee_) {}
604
605 template<typename Fn> void match(Fn F) const { F(Pointee); }
606
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700607 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
608 return Pointee->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000609 }
610
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700611 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000612 // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
613 if (Pointee->getKind() != KObjCProtoName ||
614 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700615 Pointee->printLeft(OB);
616 if (Pointee->hasArray(OB))
617 OB += " ";
618 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
619 OB += "(";
620 OB += "*";
Richard Smithc20d1442018-08-20 20:14:49 +0000621 } else {
622 const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700623 OB += "id<";
624 OB += objcProto->Protocol;
625 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +0000626 }
627 }
628
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700629 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000630 if (Pointee->getKind() != KObjCProtoName ||
631 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700632 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
633 OB += ")";
634 Pointee->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000635 }
636 }
637};
638
639enum class ReferenceKind {
640 LValue,
641 RValue,
642};
643
644// Represents either a LValue or an RValue reference type.
645class ReferenceType : public Node {
646 const Node *Pointee;
647 ReferenceKind RK;
648
649 mutable bool Printing = false;
650
651 // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
652 // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
653 // other combination collapses to a lvalue ref.
Mikhail Borisov05f77222021-08-17 18:10:57 -0400654 //
655 // A combination of a TemplateForwardReference and a back-ref Substitution
656 // from an ill-formed string may have created a cycle; use cycle detection to
657 // avoid looping forever.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700658 std::pair<ReferenceKind, const Node *> collapse(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000659 auto SoFar = std::make_pair(RK, Pointee);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400660 // Track the chain of nodes for the Floyd's 'tortoise and hare'
661 // cycle-detection algorithm, since getSyntaxNode(S) is impure
662 PODSmallVector<const Node *, 8> Prev;
Richard Smithc20d1442018-08-20 20:14:49 +0000663 for (;;) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700664 const Node *SN = SoFar.second->getSyntaxNode(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000665 if (SN->getKind() != KReferenceType)
666 break;
667 auto *RT = static_cast<const ReferenceType *>(SN);
668 SoFar.second = RT->Pointee;
669 SoFar.first = std::min(SoFar.first, RT->RK);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400670
671 // The middle of Prev is the 'slow' pointer moving at half speed
672 Prev.push_back(SoFar.second);
673 if (Prev.size() > 1 && SoFar.second == Prev[(Prev.size() - 1) / 2]) {
674 // Cycle detected
675 SoFar.second = nullptr;
676 break;
677 }
Richard Smithc20d1442018-08-20 20:14:49 +0000678 }
679 return SoFar;
680 }
681
682public:
683 ReferenceType(const Node *Pointee_, ReferenceKind RK_)
684 : Node(KReferenceType, Pointee_->RHSComponentCache),
685 Pointee(Pointee_), RK(RK_) {}
686
687 template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
688
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700689 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
690 return Pointee->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000691 }
692
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700693 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000694 if (Printing)
695 return;
696 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700697 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400698 if (!Collapsed.second)
699 return;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700700 Collapsed.second->printLeft(OB);
701 if (Collapsed.second->hasArray(OB))
702 OB += " ";
703 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
704 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +0000705
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700706 OB += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
Richard Smithc20d1442018-08-20 20:14:49 +0000707 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700708 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000709 if (Printing)
710 return;
711 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700712 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400713 if (!Collapsed.second)
714 return;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700715 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
716 OB += ")";
717 Collapsed.second->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000718 }
719};
720
721class PointerToMemberType final : public Node {
722 const Node *ClassType;
723 const Node *MemberType;
724
725public:
726 PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
727 : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
728 ClassType(ClassType_), MemberType(MemberType_) {}
729
730 template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
731
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700732 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
733 return MemberType->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000734 }
735
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700736 void printLeft(OutputBuffer &OB) const override {
737 MemberType->printLeft(OB);
738 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
739 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +0000740 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700741 OB += " ";
742 ClassType->print(OB);
743 OB += "::*";
Richard Smithc20d1442018-08-20 20:14:49 +0000744 }
745
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700746 void printRight(OutputBuffer &OB) const override {
747 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
748 OB += ")";
749 MemberType->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000750 }
751};
752
Richard Smithc20d1442018-08-20 20:14:49 +0000753class ArrayType final : public Node {
754 const Node *Base;
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800755 Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +0000756
757public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800758 ArrayType(const Node *Base_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +0000759 : Node(KArrayType,
760 /*RHSComponentCache=*/Cache::Yes,
761 /*ArrayCache=*/Cache::Yes),
762 Base(Base_), Dimension(Dimension_) {}
763
764 template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
765
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700766 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
767 bool hasArraySlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000768
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700769 void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000770
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700771 void printRight(OutputBuffer &OB) const override {
772 if (OB.back() != ']')
773 OB += " ";
774 OB += "[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800775 if (Dimension)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700776 Dimension->print(OB);
777 OB += "]";
778 Base->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000779 }
780};
781
782class FunctionType final : public Node {
783 const Node *Ret;
784 NodeArray Params;
785 Qualifiers CVQuals;
786 FunctionRefQual RefQual;
787 const Node *ExceptionSpec;
788
789public:
790 FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
791 FunctionRefQual RefQual_, const Node *ExceptionSpec_)
792 : Node(KFunctionType,
793 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
794 /*FunctionCache=*/Cache::Yes),
795 Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
796 ExceptionSpec(ExceptionSpec_) {}
797
798 template<typename Fn> void match(Fn F) const {
799 F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
800 }
801
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700802 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
803 bool hasFunctionSlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000804
805 // Handle C++'s ... quirky decl grammar by using the left & right
806 // distinction. Consider:
807 // int (*f(float))(char) {}
808 // f is a function that takes a float and returns a pointer to a function
809 // that takes a char and returns an int. If we're trying to print f, start
810 // by printing out the return types's left, then print our parameters, then
811 // finally print right of the return type.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700812 void printLeft(OutputBuffer &OB) const override {
813 Ret->printLeft(OB);
814 OB += " ";
Richard Smithc20d1442018-08-20 20:14:49 +0000815 }
816
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700817 void printRight(OutputBuffer &OB) const override {
818 OB += "(";
819 Params.printWithComma(OB);
820 OB += ")";
821 Ret->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000822
823 if (CVQuals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700824 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000825 if (CVQuals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700826 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000827 if (CVQuals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700828 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000829
830 if (RefQual == FrefQualLValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700831 OB += " &";
Richard Smithc20d1442018-08-20 20:14:49 +0000832 else if (RefQual == FrefQualRValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700833 OB += " &&";
Richard Smithc20d1442018-08-20 20:14:49 +0000834
835 if (ExceptionSpec != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700836 OB += ' ';
837 ExceptionSpec->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000838 }
839 }
840};
841
842class NoexceptSpec : public Node {
843 const Node *E;
844public:
845 NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
846
847 template<typename Fn> void match(Fn F) const { F(E); }
848
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700849 void printLeft(OutputBuffer &OB) const override {
850 OB += "noexcept(";
851 E->print(OB);
852 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000853 }
854};
855
856class DynamicExceptionSpec : public Node {
857 NodeArray Types;
858public:
859 DynamicExceptionSpec(NodeArray Types_)
860 : Node(KDynamicExceptionSpec), Types(Types_) {}
861
862 template<typename Fn> void match(Fn F) const { F(Types); }
863
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700864 void printLeft(OutputBuffer &OB) const override {
865 OB += "throw(";
866 Types.printWithComma(OB);
867 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +0000868 }
869};
870
871class FunctionEncoding final : public Node {
872 const Node *Ret;
873 const Node *Name;
874 NodeArray Params;
875 const Node *Attrs;
876 Qualifiers CVQuals;
877 FunctionRefQual RefQual;
878
879public:
880 FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
881 const Node *Attrs_, Qualifiers CVQuals_,
882 FunctionRefQual RefQual_)
883 : Node(KFunctionEncoding,
884 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
885 /*FunctionCache=*/Cache::Yes),
886 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
887 CVQuals(CVQuals_), RefQual(RefQual_) {}
888
889 template<typename Fn> void match(Fn F) const {
890 F(Ret, Name, Params, Attrs, CVQuals, RefQual);
891 }
892
893 Qualifiers getCVQuals() const { return CVQuals; }
894 FunctionRefQual getRefQual() const { return RefQual; }
895 NodeArray getParams() const { return Params; }
896 const Node *getReturnType() const { return Ret; }
897
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700898 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
899 bool hasFunctionSlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000900
901 const Node *getName() const { return Name; }
902
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700903 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000904 if (Ret) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700905 Ret->printLeft(OB);
906 if (!Ret->hasRHSComponent(OB))
907 OB += " ";
Richard Smithc20d1442018-08-20 20:14:49 +0000908 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700909 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000910 }
911
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700912 void printRight(OutputBuffer &OB) const override {
913 OB += "(";
914 Params.printWithComma(OB);
915 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000916 if (Ret)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700917 Ret->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000918
919 if (CVQuals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700920 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000921 if (CVQuals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700922 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000923 if (CVQuals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700924 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000925
926 if (RefQual == FrefQualLValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700927 OB += " &";
Richard Smithc20d1442018-08-20 20:14:49 +0000928 else if (RefQual == FrefQualRValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700929 OB += " &&";
Richard Smithc20d1442018-08-20 20:14:49 +0000930
931 if (Attrs != nullptr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700932 Attrs->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000933 }
934};
935
936class LiteralOperator : public Node {
937 const Node *OpName;
938
939public:
940 LiteralOperator(const Node *OpName_)
941 : Node(KLiteralOperator), OpName(OpName_) {}
942
943 template<typename Fn> void match(Fn F) const { F(OpName); }
944
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700945 void printLeft(OutputBuffer &OB) const override {
946 OB += "operator\"\" ";
947 OpName->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000948 }
949};
950
951class SpecialName final : public Node {
952 const StringView Special;
953 const Node *Child;
954
955public:
956 SpecialName(StringView Special_, const Node *Child_)
957 : Node(KSpecialName), Special(Special_), Child(Child_) {}
958
959 template<typename Fn> void match(Fn F) const { F(Special, Child); }
960
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700961 void printLeft(OutputBuffer &OB) const override {
962 OB += Special;
963 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000964 }
965};
966
967class CtorVtableSpecialName final : public Node {
968 const Node *FirstType;
969 const Node *SecondType;
970
971public:
972 CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
973 : Node(KCtorVtableSpecialName),
974 FirstType(FirstType_), SecondType(SecondType_) {}
975
976 template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
977
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700978 void printLeft(OutputBuffer &OB) const override {
979 OB += "construction vtable for ";
980 FirstType->print(OB);
981 OB += "-in-";
982 SecondType->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000983 }
984};
985
986struct NestedName : Node {
987 Node *Qual;
988 Node *Name;
989
990 NestedName(Node *Qual_, Node *Name_)
991 : Node(KNestedName), Qual(Qual_), Name(Name_) {}
992
993 template<typename Fn> void match(Fn F) const { F(Qual, Name); }
994
995 StringView getBaseName() const override { return Name->getBaseName(); }
996
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700997 void printLeft(OutputBuffer &OB) const override {
998 Qual->print(OB);
999 OB += "::";
1000 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001001 }
1002};
1003
1004struct LocalName : Node {
1005 Node *Encoding;
1006 Node *Entity;
1007
1008 LocalName(Node *Encoding_, Node *Entity_)
1009 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
1010
1011 template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
1012
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001013 void printLeft(OutputBuffer &OB) const override {
1014 Encoding->print(OB);
1015 OB += "::";
1016 Entity->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001017 }
1018};
1019
1020class QualifiedName final : public Node {
1021 // qualifier::name
1022 const Node *Qualifier;
1023 const Node *Name;
1024
1025public:
1026 QualifiedName(const Node *Qualifier_, const Node *Name_)
1027 : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
1028
1029 template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
1030
1031 StringView getBaseName() const override { return Name->getBaseName(); }
1032
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001033 void printLeft(OutputBuffer &OB) const override {
1034 Qualifier->print(OB);
1035 OB += "::";
1036 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001037 }
1038};
1039
1040class VectorType final : public Node {
1041 const Node *BaseType;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001042 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001043
1044public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001045 VectorType(const Node *BaseType_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001046 : Node(KVectorType), BaseType(BaseType_),
1047 Dimension(Dimension_) {}
1048
1049 template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
1050
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001051 void printLeft(OutputBuffer &OB) const override {
1052 BaseType->print(OB);
1053 OB += " vector[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001054 if (Dimension)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001055 Dimension->print(OB);
1056 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001057 }
1058};
1059
1060class PixelVectorType final : public Node {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001061 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001062
1063public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001064 PixelVectorType(const Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001065 : Node(KPixelVectorType), Dimension(Dimension_) {}
1066
1067 template<typename Fn> void match(Fn F) const { F(Dimension); }
1068
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001069 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001070 // FIXME: This should demangle as "vector pixel".
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001071 OB += "pixel vector[";
1072 Dimension->print(OB);
1073 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001074 }
1075};
1076
Pengfei Wang50e90b82021-09-23 11:02:25 +08001077class BinaryFPType final : public Node {
1078 const Node *Dimension;
1079
1080public:
1081 BinaryFPType(const Node *Dimension_)
1082 : Node(KBinaryFPType), Dimension(Dimension_) {}
1083
1084 template<typename Fn> void match(Fn F) const { F(Dimension); }
1085
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001086 void printLeft(OutputBuffer &OB) const override {
1087 OB += "_Float";
1088 Dimension->print(OB);
Pengfei Wang50e90b82021-09-23 11:02:25 +08001089 }
1090};
1091
Richard Smithdf1c14c2019-09-06 23:53:21 +00001092enum class TemplateParamKind { Type, NonType, Template };
1093
1094/// An invented name for a template parameter for which we don't have a
1095/// corresponding template argument.
1096///
1097/// This node is created when parsing the <lambda-sig> for a lambda with
1098/// explicit template arguments, which might be referenced in the parameter
1099/// types appearing later in the <lambda-sig>.
1100class SyntheticTemplateParamName final : public Node {
1101 TemplateParamKind Kind;
1102 unsigned Index;
1103
1104public:
1105 SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_)
1106 : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {}
1107
1108 template<typename Fn> void match(Fn F) const { F(Kind, Index); }
1109
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001110 void printLeft(OutputBuffer &OB) const override {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001111 switch (Kind) {
1112 case TemplateParamKind::Type:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001113 OB += "$T";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001114 break;
1115 case TemplateParamKind::NonType:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001116 OB += "$N";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001117 break;
1118 case TemplateParamKind::Template:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001119 OB += "$TT";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001120 break;
1121 }
1122 if (Index > 0)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001123 OB << Index - 1;
Richard Smithdf1c14c2019-09-06 23:53:21 +00001124 }
1125};
1126
1127/// A template type parameter declaration, 'typename T'.
1128class TypeTemplateParamDecl final : public Node {
1129 Node *Name;
1130
1131public:
1132 TypeTemplateParamDecl(Node *Name_)
1133 : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {}
1134
1135 template<typename Fn> void match(Fn F) const { F(Name); }
1136
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001137 void printLeft(OutputBuffer &OB) const override { OB += "typename "; }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001138
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001139 void printRight(OutputBuffer &OB) const override { Name->print(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001140};
1141
1142/// A non-type template parameter declaration, 'int N'.
1143class NonTypeTemplateParamDecl final : public Node {
1144 Node *Name;
1145 Node *Type;
1146
1147public:
1148 NonTypeTemplateParamDecl(Node *Name_, Node *Type_)
1149 : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {}
1150
1151 template<typename Fn> void match(Fn F) const { F(Name, Type); }
1152
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001153 void printLeft(OutputBuffer &OB) const override {
1154 Type->printLeft(OB);
1155 if (!Type->hasRHSComponent(OB))
1156 OB += " ";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001157 }
1158
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001159 void printRight(OutputBuffer &OB) const override {
1160 Name->print(OB);
1161 Type->printRight(OB);
Richard Smithdf1c14c2019-09-06 23:53:21 +00001162 }
1163};
1164
1165/// A template template parameter declaration,
1166/// 'template<typename T> typename N'.
1167class TemplateTemplateParamDecl final : public Node {
1168 Node *Name;
1169 NodeArray Params;
1170
1171public:
1172 TemplateTemplateParamDecl(Node *Name_, NodeArray Params_)
1173 : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_),
1174 Params(Params_) {}
1175
1176 template<typename Fn> void match(Fn F) const { F(Name, Params); }
1177
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001178 void printLeft(OutputBuffer &OB) const override {
1179 OB += "template<";
1180 Params.printWithComma(OB);
1181 OB += "> typename ";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001182 }
1183
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001184 void printRight(OutputBuffer &OB) const override { Name->print(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001185};
1186
1187/// A template parameter pack declaration, 'typename ...T'.
1188class TemplateParamPackDecl final : public Node {
1189 Node *Param;
1190
1191public:
1192 TemplateParamPackDecl(Node *Param_)
1193 : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {}
1194
1195 template<typename Fn> void match(Fn F) const { F(Param); }
1196
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001197 void printLeft(OutputBuffer &OB) const override {
1198 Param->printLeft(OB);
1199 OB += "...";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001200 }
1201
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001202 void printRight(OutputBuffer &OB) const override { Param->printRight(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001203};
1204
Richard Smithc20d1442018-08-20 20:14:49 +00001205/// An unexpanded parameter pack (either in the expression or type context). If
1206/// this AST is correct, this node will have a ParameterPackExpansion node above
1207/// it.
1208///
1209/// This node is created when some <template-args> are found that apply to an
1210/// <encoding>, and is stored in the TemplateParams table. In order for this to
1211/// appear in the final AST, it has to referenced via a <template-param> (ie,
1212/// T_).
1213class ParameterPack final : public Node {
1214 NodeArray Data;
1215
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08001216 // Setup OutputBuffer for a pack expansion, unless we're already expanding
1217 // one.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001218 void initializePackExpansion(OutputBuffer &OB) const {
1219 if (OB.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
1220 OB.CurrentPackMax = static_cast<unsigned>(Data.size());
1221 OB.CurrentPackIndex = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00001222 }
1223 }
1224
1225public:
1226 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
1227 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
1228 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1229 return P->ArrayCache == Cache::No;
1230 }))
1231 ArrayCache = Cache::No;
1232 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1233 return P->FunctionCache == Cache::No;
1234 }))
1235 FunctionCache = Cache::No;
1236 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1237 return P->RHSComponentCache == Cache::No;
1238 }))
1239 RHSComponentCache = Cache::No;
1240 }
1241
1242 template<typename Fn> void match(Fn F) const { F(Data); }
1243
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001244 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
1245 initializePackExpansion(OB);
1246 size_t Idx = OB.CurrentPackIndex;
1247 return Idx < Data.size() && Data[Idx]->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001248 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001249 bool hasArraySlow(OutputBuffer &OB) const override {
1250 initializePackExpansion(OB);
1251 size_t Idx = OB.CurrentPackIndex;
1252 return Idx < Data.size() && Data[Idx]->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001253 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001254 bool hasFunctionSlow(OutputBuffer &OB) const override {
1255 initializePackExpansion(OB);
1256 size_t Idx = OB.CurrentPackIndex;
1257 return Idx < Data.size() && Data[Idx]->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001258 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001259 const Node *getSyntaxNode(OutputBuffer &OB) const override {
1260 initializePackExpansion(OB);
1261 size_t Idx = OB.CurrentPackIndex;
1262 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(OB) : this;
Richard Smithc20d1442018-08-20 20:14:49 +00001263 }
1264
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001265 void printLeft(OutputBuffer &OB) const override {
1266 initializePackExpansion(OB);
1267 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001268 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001269 Data[Idx]->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001270 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001271 void printRight(OutputBuffer &OB) const override {
1272 initializePackExpansion(OB);
1273 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001274 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001275 Data[Idx]->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001276 }
1277};
1278
1279/// A variadic template argument. This node represents an occurrence of
1280/// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1281/// one of it's Elements is. The parser inserts a ParameterPack into the
1282/// TemplateParams table if the <template-args> this pack belongs to apply to an
1283/// <encoding>.
1284class TemplateArgumentPack final : public Node {
1285 NodeArray Elements;
1286public:
1287 TemplateArgumentPack(NodeArray Elements_)
1288 : Node(KTemplateArgumentPack), Elements(Elements_) {}
1289
1290 template<typename Fn> void match(Fn F) const { F(Elements); }
1291
1292 NodeArray getElements() const { return Elements; }
1293
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001294 void printLeft(OutputBuffer &OB) const override {
1295 Elements.printWithComma(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001296 }
1297};
1298
1299/// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1300/// which each have Child->ParameterPackSize elements.
1301class ParameterPackExpansion final : public Node {
1302 const Node *Child;
1303
1304public:
1305 ParameterPackExpansion(const Node *Child_)
1306 : Node(KParameterPackExpansion), Child(Child_) {}
1307
1308 template<typename Fn> void match(Fn F) const { F(Child); }
1309
1310 const Node *getChild() const { return Child; }
1311
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001312 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001313 constexpr unsigned Max = std::numeric_limits<unsigned>::max();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001314 SwapAndRestore<unsigned> SavePackIdx(OB.CurrentPackIndex, Max);
1315 SwapAndRestore<unsigned> SavePackMax(OB.CurrentPackMax, Max);
1316 size_t StreamPos = OB.getCurrentPosition();
Richard Smithc20d1442018-08-20 20:14:49 +00001317
1318 // Print the first element in the pack. If Child contains a ParameterPack,
1319 // it will set up S.CurrentPackMax and print the first element.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001320 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001321
1322 // No ParameterPack was found in Child. This can occur if we've found a pack
1323 // expansion on a <function-param>.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001324 if (OB.CurrentPackMax == Max) {
1325 OB += "...";
Richard Smithc20d1442018-08-20 20:14:49 +00001326 return;
1327 }
1328
1329 // We found a ParameterPack, but it has no elements. Erase whatever we may
1330 // of printed.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001331 if (OB.CurrentPackMax == 0) {
1332 OB.setCurrentPosition(StreamPos);
Richard Smithc20d1442018-08-20 20:14:49 +00001333 return;
1334 }
1335
1336 // Else, iterate through the rest of the elements in the pack.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001337 for (unsigned I = 1, E = OB.CurrentPackMax; I < E; ++I) {
1338 OB += ", ";
1339 OB.CurrentPackIndex = I;
1340 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001341 }
1342 }
1343};
1344
1345class TemplateArgs final : public Node {
1346 NodeArray Params;
1347
1348public:
1349 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1350
1351 template<typename Fn> void match(Fn F) const { F(Params); }
1352
1353 NodeArray getParams() { return Params; }
1354
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001355 void printLeft(OutputBuffer &OB) const override {
1356 OB += "<";
1357 Params.printWithComma(OB);
1358 if (OB.back() == '>')
1359 OB += " ";
1360 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +00001361 }
1362};
1363
Richard Smithb485b352018-08-24 23:30:26 +00001364/// A forward-reference to a template argument that was not known at the point
1365/// where the template parameter name was parsed in a mangling.
1366///
1367/// This is created when demangling the name of a specialization of a
1368/// conversion function template:
1369///
1370/// \code
1371/// struct A {
1372/// template<typename T> operator T*();
1373/// };
1374/// \endcode
1375///
1376/// When demangling a specialization of the conversion function template, we
1377/// encounter the name of the template (including the \c T) before we reach
1378/// the template argument list, so we cannot substitute the parameter name
1379/// for the corresponding argument while parsing. Instead, we create a
1380/// \c ForwardTemplateReference node that is resolved after we parse the
1381/// template arguments.
Richard Smithc20d1442018-08-20 20:14:49 +00001382struct ForwardTemplateReference : Node {
1383 size_t Index;
1384 Node *Ref = nullptr;
1385
1386 // If we're currently printing this node. It is possible (though invalid) for
1387 // a forward template reference to refer to itself via a substitution. This
1388 // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1389 // out if more than one print* function is active.
1390 mutable bool Printing = false;
1391
1392 ForwardTemplateReference(size_t Index_)
1393 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1394 Cache::Unknown),
1395 Index(Index_) {}
1396
1397 // We don't provide a matcher for these, because the value of the node is
1398 // not determined by its construction parameters, and it generally needs
1399 // special handling.
1400 template<typename Fn> void match(Fn F) const = delete;
1401
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001402 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001403 if (Printing)
1404 return false;
1405 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001406 return Ref->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001407 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001408 bool hasArraySlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001409 if (Printing)
1410 return false;
1411 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001412 return Ref->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001413 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001414 bool hasFunctionSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001415 if (Printing)
1416 return false;
1417 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001418 return Ref->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001419 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001420 const Node *getSyntaxNode(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001421 if (Printing)
1422 return this;
1423 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001424 return Ref->getSyntaxNode(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001425 }
1426
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001427 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001428 if (Printing)
1429 return;
1430 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001431 Ref->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001432 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001433 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001434 if (Printing)
1435 return;
1436 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001437 Ref->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001438 }
1439};
1440
1441struct NameWithTemplateArgs : Node {
1442 // name<template_args>
1443 Node *Name;
1444 Node *TemplateArgs;
1445
1446 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1447 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1448
1449 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1450
1451 StringView getBaseName() const override { return Name->getBaseName(); }
1452
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001453 void printLeft(OutputBuffer &OB) const override {
1454 Name->print(OB);
1455 TemplateArgs->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001456 }
1457};
1458
1459class GlobalQualifiedName final : public Node {
1460 Node *Child;
1461
1462public:
1463 GlobalQualifiedName(Node* Child_)
1464 : Node(KGlobalQualifiedName), Child(Child_) {}
1465
1466 template<typename Fn> void match(Fn F) const { F(Child); }
1467
1468 StringView getBaseName() const override { return Child->getBaseName(); }
1469
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001470 void printLeft(OutputBuffer &OB) const override {
1471 OB += "::";
1472 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001473 }
1474};
1475
1476struct StdQualifiedName : Node {
1477 Node *Child;
1478
1479 StdQualifiedName(Node *Child_) : Node(KStdQualifiedName), Child(Child_) {}
1480
1481 template<typename Fn> void match(Fn F) const { F(Child); }
1482
1483 StringView getBaseName() const override { return Child->getBaseName(); }
1484
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001485 void printLeft(OutputBuffer &OB) const override {
1486 OB += "std::";
1487 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001488 }
1489};
1490
1491enum class SpecialSubKind {
1492 allocator,
1493 basic_string,
1494 string,
1495 istream,
1496 ostream,
1497 iostream,
1498};
1499
1500class ExpandedSpecialSubstitution final : public Node {
1501 SpecialSubKind SSK;
1502
1503public:
1504 ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1505 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1506
1507 template<typename Fn> void match(Fn F) const { F(SSK); }
1508
1509 StringView getBaseName() const override {
1510 switch (SSK) {
1511 case SpecialSubKind::allocator:
1512 return StringView("allocator");
1513 case SpecialSubKind::basic_string:
1514 return StringView("basic_string");
1515 case SpecialSubKind::string:
1516 return StringView("basic_string");
1517 case SpecialSubKind::istream:
1518 return StringView("basic_istream");
1519 case SpecialSubKind::ostream:
1520 return StringView("basic_ostream");
1521 case SpecialSubKind::iostream:
1522 return StringView("basic_iostream");
1523 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001524 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001525 }
1526
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001527 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001528 switch (SSK) {
1529 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001530 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001531 break;
1532 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001533 OB += "std::basic_string";
Richard Smithb485b352018-08-24 23:30:26 +00001534 break;
Richard Smithc20d1442018-08-20 20:14:49 +00001535 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001536 OB += "std::basic_string<char, std::char_traits<char>, "
1537 "std::allocator<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001538 break;
1539 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001540 OB += "std::basic_istream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001541 break;
1542 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001543 OB += "std::basic_ostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001544 break;
1545 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001546 OB += "std::basic_iostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001547 break;
1548 }
1549 }
1550};
1551
1552class SpecialSubstitution final : public Node {
1553public:
1554 SpecialSubKind SSK;
1555
1556 SpecialSubstitution(SpecialSubKind SSK_)
1557 : Node(KSpecialSubstitution), SSK(SSK_) {}
1558
1559 template<typename Fn> void match(Fn F) const { F(SSK); }
1560
1561 StringView getBaseName() const override {
1562 switch (SSK) {
1563 case SpecialSubKind::allocator:
1564 return StringView("allocator");
1565 case SpecialSubKind::basic_string:
1566 return StringView("basic_string");
1567 case SpecialSubKind::string:
1568 return StringView("string");
1569 case SpecialSubKind::istream:
1570 return StringView("istream");
1571 case SpecialSubKind::ostream:
1572 return StringView("ostream");
1573 case SpecialSubKind::iostream:
1574 return StringView("iostream");
1575 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001576 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001577 }
1578
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001579 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001580 switch (SSK) {
1581 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001582 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001583 break;
1584 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001585 OB += "std::basic_string";
Richard Smithc20d1442018-08-20 20:14:49 +00001586 break;
1587 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001588 OB += "std::string";
Richard Smithc20d1442018-08-20 20:14:49 +00001589 break;
1590 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001591 OB += "std::istream";
Richard Smithc20d1442018-08-20 20:14:49 +00001592 break;
1593 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001594 OB += "std::ostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001595 break;
1596 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001597 OB += "std::iostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001598 break;
1599 }
1600 }
1601};
1602
1603class CtorDtorName final : public Node {
1604 const Node *Basename;
1605 const bool IsDtor;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001606 const int Variant;
Richard Smithc20d1442018-08-20 20:14:49 +00001607
1608public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001609 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1610 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1611 Variant(Variant_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001612
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001613 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
Richard Smithc20d1442018-08-20 20:14:49 +00001614
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001615 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001616 if (IsDtor)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001617 OB += "~";
1618 OB += Basename->getBaseName();
Richard Smithc20d1442018-08-20 20:14:49 +00001619 }
1620};
1621
1622class DtorName : public Node {
1623 const Node *Base;
1624
1625public:
1626 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1627
1628 template<typename Fn> void match(Fn F) const { F(Base); }
1629
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001630 void printLeft(OutputBuffer &OB) const override {
1631 OB += "~";
1632 Base->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001633 }
1634};
1635
1636class UnnamedTypeName : public Node {
1637 const StringView Count;
1638
1639public:
1640 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1641
1642 template<typename Fn> void match(Fn F) const { F(Count); }
1643
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001644 void printLeft(OutputBuffer &OB) const override {
1645 OB += "'unnamed";
1646 OB += Count;
1647 OB += "\'";
Richard Smithc20d1442018-08-20 20:14:49 +00001648 }
1649};
1650
1651class ClosureTypeName : public Node {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001652 NodeArray TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00001653 NodeArray Params;
1654 StringView Count;
1655
1656public:
Richard Smithdf1c14c2019-09-06 23:53:21 +00001657 ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_,
1658 StringView Count_)
1659 : Node(KClosureTypeName), TemplateParams(TemplateParams_),
1660 Params(Params_), Count(Count_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001661
Richard Smithdf1c14c2019-09-06 23:53:21 +00001662 template<typename Fn> void match(Fn F) const {
1663 F(TemplateParams, Params, Count);
1664 }
1665
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001666 void printDeclarator(OutputBuffer &OB) const {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001667 if (!TemplateParams.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001668 OB += "<";
1669 TemplateParams.printWithComma(OB);
1670 OB += ">";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001671 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001672 OB += "(";
1673 Params.printWithComma(OB);
1674 OB += ")";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001675 }
Richard Smithc20d1442018-08-20 20:14:49 +00001676
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001677 void printLeft(OutputBuffer &OB) const override {
1678 OB += "\'lambda";
1679 OB += Count;
1680 OB += "\'";
1681 printDeclarator(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001682 }
1683};
1684
1685class StructuredBindingName : public Node {
1686 NodeArray Bindings;
1687public:
1688 StructuredBindingName(NodeArray Bindings_)
1689 : Node(KStructuredBindingName), Bindings(Bindings_) {}
1690
1691 template<typename Fn> void match(Fn F) const { F(Bindings); }
1692
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001693 void printLeft(OutputBuffer &OB) const override {
1694 OB += '[';
1695 Bindings.printWithComma(OB);
1696 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00001697 }
1698};
1699
1700// -- Expression Nodes --
1701
1702class BinaryExpr : public Node {
1703 const Node *LHS;
1704 const StringView InfixOperator;
1705 const Node *RHS;
1706
1707public:
1708 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
1709 : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
1710 }
1711
1712 template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
1713
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001714 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001715 // might be a template argument expression, then we need to disambiguate
1716 // with parens.
1717 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001718 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +00001719
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001720 OB += "(";
1721 LHS->print(OB);
1722 OB += ") ";
1723 OB += InfixOperator;
1724 OB += " (";
1725 RHS->print(OB);
1726 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001727
1728 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001729 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001730 }
1731};
1732
1733class ArraySubscriptExpr : public Node {
1734 const Node *Op1;
1735 const Node *Op2;
1736
1737public:
1738 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
1739 : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
1740
1741 template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
1742
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001743 void printLeft(OutputBuffer &OB) const override {
1744 OB += "(";
1745 Op1->print(OB);
1746 OB += ")[";
1747 Op2->print(OB);
1748 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001749 }
1750};
1751
1752class PostfixExpr : public Node {
1753 const Node *Child;
1754 const StringView Operator;
1755
1756public:
1757 PostfixExpr(const Node *Child_, StringView Operator_)
1758 : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
1759
1760 template<typename Fn> void match(Fn F) const { F(Child, Operator); }
1761
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001762 void printLeft(OutputBuffer &OB) const override {
1763 OB += "(";
1764 Child->print(OB);
1765 OB += ")";
1766 OB += Operator;
Richard Smithc20d1442018-08-20 20:14:49 +00001767 }
1768};
1769
1770class ConditionalExpr : public Node {
1771 const Node *Cond;
1772 const Node *Then;
1773 const Node *Else;
1774
1775public:
1776 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
1777 : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
1778
1779 template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
1780
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001781 void printLeft(OutputBuffer &OB) const override {
1782 OB += "(";
1783 Cond->print(OB);
1784 OB += ") ? (";
1785 Then->print(OB);
1786 OB += ") : (";
1787 Else->print(OB);
1788 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001789 }
1790};
1791
1792class MemberExpr : public Node {
1793 const Node *LHS;
1794 const StringView Kind;
1795 const Node *RHS;
1796
1797public:
1798 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
1799 : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1800
1801 template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
1802
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001803 void printLeft(OutputBuffer &OB) const override {
1804 LHS->print(OB);
1805 OB += Kind;
1806 RHS->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001807 }
1808};
1809
Richard Smith1865d2f2020-10-22 19:29:36 -07001810class SubobjectExpr : public Node {
1811 const Node *Type;
1812 const Node *SubExpr;
1813 StringView Offset;
1814 NodeArray UnionSelectors;
1815 bool OnePastTheEnd;
1816
1817public:
1818 SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_,
1819 NodeArray UnionSelectors_, bool OnePastTheEnd_)
1820 : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_),
1821 UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {}
1822
1823 template<typename Fn> void match(Fn F) const {
1824 F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd);
1825 }
1826
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001827 void printLeft(OutputBuffer &OB) const override {
1828 SubExpr->print(OB);
1829 OB += ".<";
1830 Type->print(OB);
1831 OB += " at offset ";
Richard Smith1865d2f2020-10-22 19:29:36 -07001832 if (Offset.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001833 OB += "0";
Richard Smith1865d2f2020-10-22 19:29:36 -07001834 } else if (Offset[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001835 OB += "-";
1836 OB += Offset.dropFront();
Richard Smith1865d2f2020-10-22 19:29:36 -07001837 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001838 OB += Offset;
Richard Smith1865d2f2020-10-22 19:29:36 -07001839 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001840 OB += ">";
Richard Smith1865d2f2020-10-22 19:29:36 -07001841 }
1842};
1843
Richard Smithc20d1442018-08-20 20:14:49 +00001844class EnclosingExpr : public Node {
1845 const StringView Prefix;
1846 const Node *Infix;
1847 const StringView Postfix;
1848
1849public:
1850 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1851 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1852 Postfix(Postfix_) {}
1853
1854 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1855
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001856 void printLeft(OutputBuffer &OB) const override {
1857 OB += Prefix;
1858 Infix->print(OB);
1859 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +00001860 }
1861};
1862
1863class CastExpr : public Node {
1864 // cast_kind<to>(from)
1865 const StringView CastKind;
1866 const Node *To;
1867 const Node *From;
1868
1869public:
1870 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1871 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1872
1873 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1874
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001875 void printLeft(OutputBuffer &OB) const override {
1876 OB += CastKind;
1877 OB += "<";
1878 To->printLeft(OB);
1879 OB += ">(";
1880 From->printLeft(OB);
1881 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001882 }
1883};
1884
1885class SizeofParamPackExpr : public Node {
1886 const Node *Pack;
1887
1888public:
1889 SizeofParamPackExpr(const Node *Pack_)
1890 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1891
1892 template<typename Fn> void match(Fn F) const { F(Pack); }
1893
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001894 void printLeft(OutputBuffer &OB) const override {
1895 OB += "sizeof...(";
Richard Smithc20d1442018-08-20 20:14:49 +00001896 ParameterPackExpansion PPE(Pack);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001897 PPE.printLeft(OB);
1898 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001899 }
1900};
1901
1902class CallExpr : public Node {
1903 const Node *Callee;
1904 NodeArray Args;
1905
1906public:
1907 CallExpr(const Node *Callee_, NodeArray Args_)
1908 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1909
1910 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1911
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001912 void printLeft(OutputBuffer &OB) const override {
1913 Callee->print(OB);
1914 OB += "(";
1915 Args.printWithComma(OB);
1916 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001917 }
1918};
1919
1920class NewExpr : public Node {
1921 // new (expr_list) type(init_list)
1922 NodeArray ExprList;
1923 Node *Type;
1924 NodeArray InitList;
1925 bool IsGlobal; // ::operator new ?
1926 bool IsArray; // new[] ?
1927public:
1928 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1929 bool IsArray_)
1930 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1931 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1932
1933 template<typename Fn> void match(Fn F) const {
1934 F(ExprList, Type, InitList, IsGlobal, IsArray);
1935 }
1936
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001937 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001938 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001939 OB += "::operator ";
1940 OB += "new";
Richard Smithc20d1442018-08-20 20:14:49 +00001941 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001942 OB += "[]";
1943 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00001944 if (!ExprList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001945 OB += "(";
1946 ExprList.printWithComma(OB);
1947 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001948 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001949 Type->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001950 if (!InitList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001951 OB += "(";
1952 InitList.printWithComma(OB);
1953 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001954 }
Richard Smithc20d1442018-08-20 20:14:49 +00001955 }
1956};
1957
1958class DeleteExpr : public Node {
1959 Node *Op;
1960 bool IsGlobal;
1961 bool IsArray;
1962
1963public:
1964 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1965 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1966
1967 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1968
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001969 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001970 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001971 OB += "::";
1972 OB += "delete";
Richard Smithc20d1442018-08-20 20:14:49 +00001973 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001974 OB += "[] ";
1975 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001976 }
1977};
1978
1979class PrefixExpr : public Node {
1980 StringView Prefix;
1981 Node *Child;
1982
1983public:
1984 PrefixExpr(StringView Prefix_, Node *Child_)
1985 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1986
1987 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1988
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001989 void printLeft(OutputBuffer &OB) const override {
1990 OB += Prefix;
1991 OB += "(";
1992 Child->print(OB);
1993 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001994 }
1995};
1996
1997class FunctionParam : public Node {
1998 StringView Number;
1999
2000public:
2001 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
2002
2003 template<typename Fn> void match(Fn F) const { F(Number); }
2004
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002005 void printLeft(OutputBuffer &OB) const override {
2006 OB += "fp";
2007 OB += Number;
Richard Smithc20d1442018-08-20 20:14:49 +00002008 }
2009};
2010
2011class ConversionExpr : public Node {
2012 const Node *Type;
2013 NodeArray Expressions;
2014
2015public:
2016 ConversionExpr(const Node *Type_, NodeArray Expressions_)
2017 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
2018
2019 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
2020
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002021 void printLeft(OutputBuffer &OB) const override {
2022 OB += "(";
2023 Type->print(OB);
2024 OB += ")(";
2025 Expressions.printWithComma(OB);
2026 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002027 }
2028};
2029
Richard Smith1865d2f2020-10-22 19:29:36 -07002030class PointerToMemberConversionExpr : public Node {
2031 const Node *Type;
2032 const Node *SubExpr;
2033 StringView Offset;
2034
2035public:
2036 PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_,
2037 StringView Offset_)
2038 : Node(KPointerToMemberConversionExpr), Type(Type_), SubExpr(SubExpr_),
2039 Offset(Offset_) {}
2040
2041 template<typename Fn> void match(Fn F) const { F(Type, SubExpr, Offset); }
2042
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002043 void printLeft(OutputBuffer &OB) const override {
2044 OB += "(";
2045 Type->print(OB);
2046 OB += ")(";
2047 SubExpr->print(OB);
2048 OB += ")";
Richard Smith1865d2f2020-10-22 19:29:36 -07002049 }
2050};
2051
Richard Smithc20d1442018-08-20 20:14:49 +00002052class InitListExpr : public Node {
2053 const Node *Ty;
2054 NodeArray Inits;
2055public:
2056 InitListExpr(const Node *Ty_, NodeArray Inits_)
2057 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
2058
2059 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
2060
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002061 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002062 if (Ty)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002063 Ty->print(OB);
2064 OB += '{';
2065 Inits.printWithComma(OB);
2066 OB += '}';
Richard Smithc20d1442018-08-20 20:14:49 +00002067 }
2068};
2069
2070class BracedExpr : public Node {
2071 const Node *Elem;
2072 const Node *Init;
2073 bool IsArray;
2074public:
2075 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
2076 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
2077
2078 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
2079
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002080 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002081 if (IsArray) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002082 OB += '[';
2083 Elem->print(OB);
2084 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002085 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002086 OB += '.';
2087 Elem->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002088 }
2089 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002090 OB += " = ";
2091 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002092 }
2093};
2094
2095class BracedRangeExpr : public Node {
2096 const Node *First;
2097 const Node *Last;
2098 const Node *Init;
2099public:
2100 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
2101 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
2102
2103 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
2104
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002105 void printLeft(OutputBuffer &OB) const override {
2106 OB += '[';
2107 First->print(OB);
2108 OB += " ... ";
2109 Last->print(OB);
2110 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002111 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002112 OB += " = ";
2113 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002114 }
2115};
2116
2117class FoldExpr : public Node {
2118 const Node *Pack, *Init;
2119 StringView OperatorName;
2120 bool IsLeftFold;
2121
2122public:
2123 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
2124 const Node *Init_)
2125 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
2126 IsLeftFold(IsLeftFold_) {}
2127
2128 template<typename Fn> void match(Fn F) const {
2129 F(IsLeftFold, OperatorName, Pack, Init);
2130 }
2131
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002132 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002133 auto PrintPack = [&] {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002134 OB += '(';
2135 ParameterPackExpansion(Pack).print(OB);
2136 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002137 };
2138
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002139 OB += '(';
Richard Smithc20d1442018-08-20 20:14:49 +00002140
2141 if (IsLeftFold) {
2142 // init op ... op pack
2143 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002144 Init->print(OB);
2145 OB += ' ';
2146 OB += OperatorName;
2147 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002148 }
2149 // ... op pack
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002150 OB += "... ";
2151 OB += OperatorName;
2152 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002153 PrintPack();
2154 } else { // !IsLeftFold
2155 // pack op ...
2156 PrintPack();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002157 OB += ' ';
2158 OB += OperatorName;
2159 OB += " ...";
Richard Smithc20d1442018-08-20 20:14:49 +00002160 // pack op ... op init
2161 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002162 OB += ' ';
2163 OB += OperatorName;
2164 OB += ' ';
2165 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002166 }
2167 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002168 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002169 }
2170};
2171
2172class ThrowExpr : public Node {
2173 const Node *Op;
2174
2175public:
2176 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
2177
2178 template<typename Fn> void match(Fn F) const { F(Op); }
2179
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002180 void printLeft(OutputBuffer &OB) const override {
2181 OB += "throw ";
2182 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002183 }
2184};
2185
2186class BoolExpr : public Node {
2187 bool Value;
2188
2189public:
2190 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
2191
2192 template<typename Fn> void match(Fn F) const { F(Value); }
2193
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002194 void printLeft(OutputBuffer &OB) const override {
2195 OB += Value ? StringView("true") : StringView("false");
Richard Smithc20d1442018-08-20 20:14:49 +00002196 }
2197};
2198
Richard Smithdf1c14c2019-09-06 23:53:21 +00002199class StringLiteral : public Node {
2200 const Node *Type;
2201
2202public:
2203 StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
2204
2205 template<typename Fn> void match(Fn F) const { F(Type); }
2206
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002207 void printLeft(OutputBuffer &OB) const override {
2208 OB += "\"<";
2209 Type->print(OB);
2210 OB += ">\"";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002211 }
2212};
2213
2214class LambdaExpr : public Node {
2215 const Node *Type;
2216
Richard Smithdf1c14c2019-09-06 23:53:21 +00002217public:
2218 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2219
2220 template<typename Fn> void match(Fn F) const { F(Type); }
2221
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002222 void printLeft(OutputBuffer &OB) const override {
2223 OB += "[]";
Richard Smithfb917462019-09-09 22:26:04 +00002224 if (Type->getKind() == KClosureTypeName)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002225 static_cast<const ClosureTypeName *>(Type)->printDeclarator(OB);
2226 OB += "{...}";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002227 }
2228};
2229
Erik Pilkington0a170f12020-05-13 14:13:37 -04002230class EnumLiteral : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +00002231 // ty(integer)
2232 const Node *Ty;
2233 StringView Integer;
2234
2235public:
Erik Pilkington0a170f12020-05-13 14:13:37 -04002236 EnumLiteral(const Node *Ty_, StringView Integer_)
2237 : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00002238
2239 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2240
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002241 void printLeft(OutputBuffer &OB) const override {
2242 OB << "(";
2243 Ty->print(OB);
2244 OB << ")";
Erik Pilkington0a170f12020-05-13 14:13:37 -04002245
2246 if (Integer[0] == 'n')
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002247 OB << "-" << Integer.dropFront(1);
Erik Pilkington0a170f12020-05-13 14:13:37 -04002248 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002249 OB << Integer;
Richard Smithc20d1442018-08-20 20:14:49 +00002250 }
2251};
2252
2253class IntegerLiteral : public Node {
2254 StringView Type;
2255 StringView Value;
2256
2257public:
2258 IntegerLiteral(StringView Type_, StringView Value_)
2259 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2260
2261 template<typename Fn> void match(Fn F) const { F(Type, Value); }
2262
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002263 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002264 if (Type.size() > 3) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002265 OB += "(";
2266 OB += Type;
2267 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002268 }
2269
2270 if (Value[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002271 OB += "-";
2272 OB += Value.dropFront(1);
Richard Smithc20d1442018-08-20 20:14:49 +00002273 } else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002274 OB += Value;
Richard Smithc20d1442018-08-20 20:14:49 +00002275
2276 if (Type.size() <= 3)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002277 OB += Type;
Richard Smithc20d1442018-08-20 20:14:49 +00002278 }
2279};
2280
2281template <class Float> struct FloatData;
2282
2283namespace float_literal_impl {
2284constexpr Node::Kind getFloatLiteralKind(float *) {
2285 return Node::KFloatLiteral;
2286}
2287constexpr Node::Kind getFloatLiteralKind(double *) {
2288 return Node::KDoubleLiteral;
2289}
2290constexpr Node::Kind getFloatLiteralKind(long double *) {
2291 return Node::KLongDoubleLiteral;
2292}
2293}
2294
2295template <class Float> class FloatLiteralImpl : public Node {
2296 const StringView Contents;
2297
2298 static constexpr Kind KindForClass =
2299 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2300
2301public:
2302 FloatLiteralImpl(StringView Contents_)
2303 : Node(KindForClass), Contents(Contents_) {}
2304
2305 template<typename Fn> void match(Fn F) const { F(Contents); }
2306
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002307 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002308 const char *first = Contents.begin();
2309 const char *last = Contents.end() + 1;
2310
2311 const size_t N = FloatData<Float>::mangled_size;
2312 if (static_cast<std::size_t>(last - first) > N) {
2313 last = first + N;
2314 union {
2315 Float value;
2316 char buf[sizeof(Float)];
2317 };
2318 const char *t = first;
2319 char *e = buf;
2320 for (; t != last; ++t, ++e) {
2321 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2322 : static_cast<unsigned>(*t - 'a' + 10);
2323 ++t;
2324 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2325 : static_cast<unsigned>(*t - 'a' + 10);
2326 *e = static_cast<char>((d1 << 4) + d0);
2327 }
2328#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2329 std::reverse(buf, e);
2330#endif
2331 char num[FloatData<Float>::max_demangled_size] = {0};
2332 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002333 OB += StringView(num, num + n);
Richard Smithc20d1442018-08-20 20:14:49 +00002334 }
2335 }
2336};
2337
2338using FloatLiteral = FloatLiteralImpl<float>;
2339using DoubleLiteral = FloatLiteralImpl<double>;
2340using LongDoubleLiteral = FloatLiteralImpl<long double>;
2341
2342/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2343/// appropriate derived class.
2344template<typename Fn>
2345void Node::visit(Fn F) const {
2346 switch (K) {
2347#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2348 FOR_EACH_NODE_KIND(CASE)
2349#undef CASE
2350 }
2351 assert(0 && "unknown mangling node kind");
2352}
2353
2354/// Determine the kind of a node from its type.
2355template<typename NodeT> struct NodeKind;
2356#define SPECIALIZATION(X) \
2357 template<> struct NodeKind<X> { \
2358 static constexpr Node::Kind Kind = Node::K##X; \
2359 static constexpr const char *name() { return #X; } \
2360 };
2361FOR_EACH_NODE_KIND(SPECIALIZATION)
2362#undef SPECIALIZATION
2363
2364#undef FOR_EACH_NODE_KIND
2365
Pavel Labathba825192018-10-16 14:29:14 +00002366template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002367 const char *First;
2368 const char *Last;
2369
2370 // Name stack, this is used by the parser to hold temporary names that were
2371 // parsed. The parser collapses multiple names into new nodes to construct
2372 // the AST. Once the parser is finished, names.size() == 1.
2373 PODSmallVector<Node *, 32> Names;
2374
2375 // Substitution table. Itanium supports name substitutions as a means of
2376 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2377 // table.
2378 PODSmallVector<Node *, 32> Subs;
2379
Richard Smithdf1c14c2019-09-06 23:53:21 +00002380 using TemplateParamList = PODSmallVector<Node *, 8>;
2381
2382 class ScopedTemplateParamList {
2383 AbstractManglingParser *Parser;
2384 size_t OldNumTemplateParamLists;
2385 TemplateParamList Params;
2386
2387 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04002388 ScopedTemplateParamList(AbstractManglingParser *TheParser)
2389 : Parser(TheParser),
2390 OldNumTemplateParamLists(TheParser->TemplateParams.size()) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002391 Parser->TemplateParams.push_back(&Params);
2392 }
2393 ~ScopedTemplateParamList() {
2394 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2395 Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2396 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002397 };
2398
Richard Smithc20d1442018-08-20 20:14:49 +00002399 // Template parameter table. Like the above, but referenced like "T42_".
2400 // This has a smaller size compared to Subs and Names because it can be
2401 // stored on the stack.
Richard Smithdf1c14c2019-09-06 23:53:21 +00002402 TemplateParamList OuterTemplateParams;
2403
2404 // Lists of template parameters indexed by template parameter depth,
2405 // referenced like "TL2_4_". If nonempty, element 0 is always
2406 // OuterTemplateParams; inner elements are always template parameter lists of
2407 // lambda expressions. For a generic lambda with no explicit template
2408 // parameter list, the corresponding parameter list pointer will be null.
2409 PODSmallVector<TemplateParamList *, 4> TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00002410
2411 // Set of unresolved forward <template-param> references. These can occur in a
2412 // conversion operator's type, and are resolved in the enclosing <encoding>.
2413 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2414
Richard Smithc20d1442018-08-20 20:14:49 +00002415 bool TryToParseTemplateArgs = true;
2416 bool PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002417 size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2418
2419 unsigned NumSyntheticTemplateParameters[3] = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002420
2421 Alloc ASTAllocator;
2422
Pavel Labathba825192018-10-16 14:29:14 +00002423 AbstractManglingParser(const char *First_, const char *Last_)
2424 : First(First_), Last(Last_) {}
2425
2426 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002427
2428 void reset(const char *First_, const char *Last_) {
2429 First = First_;
2430 Last = Last_;
2431 Names.clear();
2432 Subs.clear();
2433 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002434 ParsingLambdaParamsAtLevel = (size_t)-1;
Richard Smithc20d1442018-08-20 20:14:49 +00002435 TryToParseTemplateArgs = true;
2436 PermitForwardTemplateReferences = false;
Richard Smith9a2307a2019-09-07 00:11:53 +00002437 for (int I = 0; I != 3; ++I)
2438 NumSyntheticTemplateParameters[I] = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00002439 ASTAllocator.reset();
2440 }
2441
Richard Smithb485b352018-08-24 23:30:26 +00002442 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002443 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2444 }
2445
2446 template <class It> NodeArray makeNodeArray(It begin, It end) {
2447 size_t sz = static_cast<size_t>(end - begin);
2448 void *mem = ASTAllocator.allocateNodeArray(sz);
2449 Node **data = new (mem) Node *[sz];
2450 std::copy(begin, end, data);
2451 return NodeArray(data, sz);
2452 }
2453
2454 NodeArray popTrailingNodeArray(size_t FromPosition) {
2455 assert(FromPosition <= Names.size());
2456 NodeArray res =
2457 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2458 Names.dropBack(FromPosition);
2459 return res;
2460 }
2461
2462 bool consumeIf(StringView S) {
2463 if (StringView(First, Last).startsWith(S)) {
2464 First += S.size();
2465 return true;
2466 }
2467 return false;
2468 }
2469
2470 bool consumeIf(char C) {
2471 if (First != Last && *First == C) {
2472 ++First;
2473 return true;
2474 }
2475 return false;
2476 }
2477
2478 char consume() { return First != Last ? *First++ : '\0'; }
2479
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08002480 char look(unsigned Lookahead = 0) const {
Richard Smithc20d1442018-08-20 20:14:49 +00002481 if (static_cast<size_t>(Last - First) <= Lookahead)
2482 return '\0';
2483 return First[Lookahead];
2484 }
2485
2486 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2487
2488 StringView parseNumber(bool AllowNegative = false);
2489 Qualifiers parseCVQualifiers();
2490 bool parsePositiveInteger(size_t *Out);
2491 StringView parseBareSourceName();
2492
2493 bool parseSeqId(size_t *Out);
2494 Node *parseSubstitution();
2495 Node *parseTemplateParam();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002496 Node *parseTemplateParamDecl();
Richard Smithc20d1442018-08-20 20:14:49 +00002497 Node *parseTemplateArgs(bool TagTemplates = false);
2498 Node *parseTemplateArg();
2499
2500 /// Parse the <expr> production.
2501 Node *parseExpr();
2502 Node *parsePrefixExpr(StringView Kind);
2503 Node *parseBinaryExpr(StringView Kind);
2504 Node *parseIntegerLiteral(StringView Lit);
2505 Node *parseExprPrimary();
2506 template <class Float> Node *parseFloatingLiteral();
2507 Node *parseFunctionParam();
2508 Node *parseNewExpr();
2509 Node *parseConversionExpr();
2510 Node *parseBracedExpr();
2511 Node *parseFoldExpr();
Richard Smith1865d2f2020-10-22 19:29:36 -07002512 Node *parsePointerToMemberConversionExpr();
2513 Node *parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00002514
2515 /// Parse the <type> production.
2516 Node *parseType();
2517 Node *parseFunctionType();
2518 Node *parseVectorType();
2519 Node *parseDecltype();
2520 Node *parseArrayType();
2521 Node *parsePointerToMemberType();
2522 Node *parseClassEnumType();
2523 Node *parseQualifiedType();
2524
2525 Node *parseEncoding();
2526 bool parseCallOffset();
2527 Node *parseSpecialName();
2528
2529 /// Holds some extra information about a <name> that is being parsed. This
2530 /// information is only pertinent if the <name> refers to an <encoding>.
2531 struct NameState {
2532 bool CtorDtorConversion = false;
2533 bool EndsWithTemplateArgs = false;
2534 Qualifiers CVQualifiers = QualNone;
2535 FunctionRefQual ReferenceQualifier = FrefQualNone;
2536 size_t ForwardTemplateRefsBegin;
2537
Pavel Labathba825192018-10-16 14:29:14 +00002538 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002539 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2540 };
2541
2542 bool resolveForwardTemplateRefs(NameState &State) {
2543 size_t I = State.ForwardTemplateRefsBegin;
2544 size_t E = ForwardTemplateRefs.size();
2545 for (; I < E; ++I) {
2546 size_t Idx = ForwardTemplateRefs[I]->Index;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002547 if (TemplateParams.empty() || !TemplateParams[0] ||
2548 Idx >= TemplateParams[0]->size())
Richard Smithc20d1442018-08-20 20:14:49 +00002549 return true;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002550 ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
Richard Smithc20d1442018-08-20 20:14:49 +00002551 }
2552 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2553 return false;
2554 }
2555
2556 /// Parse the <name> production>
2557 Node *parseName(NameState *State = nullptr);
2558 Node *parseLocalName(NameState *State);
2559 Node *parseOperatorName(NameState *State);
2560 Node *parseUnqualifiedName(NameState *State);
2561 Node *parseUnnamedTypeName(NameState *State);
2562 Node *parseSourceName(NameState *State);
2563 Node *parseUnscopedName(NameState *State);
2564 Node *parseNestedName(NameState *State);
2565 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2566
2567 Node *parseAbiTags(Node *N);
2568
2569 /// Parse the <unresolved-name> production.
2570 Node *parseUnresolvedName();
2571 Node *parseSimpleId();
2572 Node *parseBaseUnresolvedName();
2573 Node *parseUnresolvedType();
2574 Node *parseDestructorName();
2575
2576 /// Top-level entry point into the parser.
2577 Node *parse();
2578};
2579
2580const char* parse_discriminator(const char* first, const char* last);
2581
2582// <name> ::= <nested-name> // N
2583// ::= <local-name> # See Scope Encoding below // Z
2584// ::= <unscoped-template-name> <template-args>
2585// ::= <unscoped-name>
2586//
2587// <unscoped-template-name> ::= <unscoped-name>
2588// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002589template <typename Derived, typename Alloc>
2590Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002591 consumeIf('L'); // extension
2592
2593 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002594 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002595 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002596 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002597
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002598 Node *Result = nullptr;
2599 bool IsSubst = look() == 'S' && look(1) != 't';
2600 if (IsSubst) {
2601 // A substitution must lead to:
2602 // ::= <unscoped-template-name> <template-args>
2603 Result = getDerived().parseSubstitution();
2604 } else {
2605 // An unscoped name can be one of:
2606 // ::= <unscoped-name>
2607 // ::= <unscoped-template-name> <template-args>
2608 Result = getDerived().parseUnscopedName(State);
2609 }
2610 if (Result == nullptr)
2611 return nullptr;
2612
2613 if (look() == 'I') {
2614 // ::= <unscoped-template-name> <template-args>
2615 if (!IsSubst)
2616 // An unscoped-template-name is substitutable.
2617 Subs.push_back(Result);
Pavel Labathba825192018-10-16 14:29:14 +00002618 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002619 if (TA == nullptr)
2620 return nullptr;
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002621 if (State)
2622 State->EndsWithTemplateArgs = true;
2623 Result = make<NameWithTemplateArgs>(Result, TA);
2624 } else if (IsSubst) {
2625 // The substitution case must be followed by <template-args>.
2626 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002627 }
2628
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002629 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00002630}
2631
2632// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2633// := Z <function encoding> E s [<discriminator>]
2634// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002635template <typename Derived, typename Alloc>
2636Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002637 if (!consumeIf('Z'))
2638 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002639 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002640 if (Encoding == nullptr || !consumeIf('E'))
2641 return nullptr;
2642
2643 if (consumeIf('s')) {
2644 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002645 auto *StringLitName = make<NameType>("string literal");
2646 if (!StringLitName)
2647 return nullptr;
2648 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002649 }
2650
2651 if (consumeIf('d')) {
2652 parseNumber(true);
2653 if (!consumeIf('_'))
2654 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002655 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002656 if (N == nullptr)
2657 return nullptr;
2658 return make<LocalName>(Encoding, N);
2659 }
2660
Pavel Labathba825192018-10-16 14:29:14 +00002661 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002662 if (Entity == nullptr)
2663 return nullptr;
2664 First = parse_discriminator(First, Last);
2665 return make<LocalName>(Encoding, Entity);
2666}
2667
2668// <unscoped-name> ::= <unqualified-name>
2669// ::= St <unqualified-name> # ::std::
2670// extension ::= StL<unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00002671template <typename Derived, typename Alloc>
2672Node *
2673AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002674 bool IsStd = consumeIf("St");
2675 if (IsStd)
2676 consumeIf('L');
2677
2678 Node *Result = getDerived().parseUnqualifiedName(State);
2679 if (Result == nullptr)
2680 return nullptr;
2681 if (IsStd)
2682 Result = make<StdQualifiedName>(Result);
2683
2684 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00002685}
2686
2687// <unqualified-name> ::= <operator-name> [abi-tags]
2688// ::= <ctor-dtor-name>
2689// ::= <source-name>
2690// ::= <unnamed-type-name>
2691// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002692template <typename Derived, typename Alloc>
2693Node *
2694AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002695 // <ctor-dtor-name>s are special-cased in parseNestedName().
2696 Node *Result;
2697 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002698 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002699 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002700 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002701 else if (consumeIf("DC")) {
2702 size_t BindingsBegin = Names.size();
2703 do {
Pavel Labathba825192018-10-16 14:29:14 +00002704 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002705 if (Binding == nullptr)
2706 return nullptr;
2707 Names.push_back(Binding);
2708 } while (!consumeIf('E'));
2709 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2710 } else
Pavel Labathba825192018-10-16 14:29:14 +00002711 Result = getDerived().parseOperatorName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002712 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002713 Result = getDerived().parseAbiTags(Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002714 return Result;
2715}
2716
2717// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2718// ::= <closure-type-name>
2719//
2720// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2721//
2722// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002723template <typename Derived, typename Alloc>
2724Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002725AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2726 // <template-params> refer to the innermost <template-args>. Clear out any
2727 // outer args that we may have inserted into TemplateParams.
2728 if (State != nullptr)
2729 TemplateParams.clear();
2730
Richard Smithc20d1442018-08-20 20:14:49 +00002731 if (consumeIf("Ut")) {
2732 StringView Count = parseNumber();
2733 if (!consumeIf('_'))
2734 return nullptr;
2735 return make<UnnamedTypeName>(Count);
2736 }
2737 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002738 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2739 TemplateParams.size());
2740 ScopedTemplateParamList LambdaTemplateParams(this);
2741
2742 size_t ParamsBegin = Names.size();
2743 while (look() == 'T' &&
2744 StringView("yptn").find(look(1)) != StringView::npos) {
2745 Node *T = parseTemplateParamDecl();
2746 if (!T)
2747 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002748 Names.push_back(T);
2749 }
2750 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2751
2752 // FIXME: If TempParams is empty and none of the function parameters
2753 // includes 'auto', we should remove LambdaTemplateParams from the
2754 // TemplateParams list. Unfortunately, we don't find out whether there are
2755 // any 'auto' parameters until too late in an example such as:
2756 //
2757 // template<typename T> void f(
2758 // decltype([](decltype([]<typename T>(T v) {}),
2759 // auto) {})) {}
2760 // template<typename T> void f(
2761 // decltype([](decltype([]<typename T>(T w) {}),
2762 // int) {})) {}
2763 //
2764 // Here, the type of v is at level 2 but the type of w is at level 1. We
2765 // don't find this out until we encounter the type of the next parameter.
2766 //
2767 // However, compilers can't actually cope with the former example in
2768 // practice, and it's likely to be made ill-formed in future, so we don't
2769 // need to support it here.
2770 //
2771 // If we encounter an 'auto' in the function parameter types, we will
2772 // recreate a template parameter scope for it, but any intervening lambdas
2773 // will be parsed in the 'wrong' template parameter depth.
2774 if (TempParams.empty())
2775 TemplateParams.pop_back();
2776
Richard Smithc20d1442018-08-20 20:14:49 +00002777 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002778 do {
Pavel Labathba825192018-10-16 14:29:14 +00002779 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002780 if (P == nullptr)
2781 return nullptr;
2782 Names.push_back(P);
2783 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002784 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002785 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2786
Richard Smithc20d1442018-08-20 20:14:49 +00002787 StringView Count = parseNumber();
2788 if (!consumeIf('_'))
2789 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002790 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002791 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002792 if (consumeIf("Ub")) {
2793 (void)parseNumber();
2794 if (!consumeIf('_'))
2795 return nullptr;
2796 return make<NameType>("'block-literal'");
2797 }
Richard Smithc20d1442018-08-20 20:14:49 +00002798 return nullptr;
2799}
2800
2801// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002802template <typename Derived, typename Alloc>
2803Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002804 size_t Length = 0;
2805 if (parsePositiveInteger(&Length))
2806 return nullptr;
2807 if (numLeft() < Length || Length == 0)
2808 return nullptr;
2809 StringView Name(First, First + Length);
2810 First += Length;
2811 if (Name.startsWith("_GLOBAL__N"))
2812 return make<NameType>("(anonymous namespace)");
2813 return make<NameType>(Name);
2814}
2815
2816// <operator-name> ::= aa # &&
2817// ::= ad # & (unary)
2818// ::= an # &
2819// ::= aN # &=
2820// ::= aS # =
2821// ::= cl # ()
2822// ::= cm # ,
2823// ::= co # ~
2824// ::= cv <type> # (cast)
2825// ::= da # delete[]
2826// ::= de # * (unary)
2827// ::= dl # delete
2828// ::= dv # /
2829// ::= dV # /=
2830// ::= eo # ^
2831// ::= eO # ^=
2832// ::= eq # ==
2833// ::= ge # >=
2834// ::= gt # >
2835// ::= ix # []
2836// ::= le # <=
2837// ::= li <source-name> # operator ""
2838// ::= ls # <<
2839// ::= lS # <<=
2840// ::= lt # <
2841// ::= mi # -
2842// ::= mI # -=
2843// ::= ml # *
2844// ::= mL # *=
2845// ::= mm # -- (postfix in <expression> context)
2846// ::= na # new[]
2847// ::= ne # !=
2848// ::= ng # - (unary)
2849// ::= nt # !
2850// ::= nw # new
2851// ::= oo # ||
2852// ::= or # |
2853// ::= oR # |=
2854// ::= pm # ->*
2855// ::= pl # +
2856// ::= pL # +=
2857// ::= pp # ++ (postfix in <expression> context)
2858// ::= ps # + (unary)
2859// ::= pt # ->
2860// ::= qu # ?
2861// ::= rm # %
2862// ::= rM # %=
2863// ::= rs # >>
2864// ::= rS # >>=
2865// ::= ss # <=> C++2a
2866// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002867template <typename Derived, typename Alloc>
2868Node *
2869AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002870 switch (look()) {
2871 case 'a':
2872 switch (look(1)) {
2873 case 'a':
2874 First += 2;
2875 return make<NameType>("operator&&");
2876 case 'd':
2877 case 'n':
2878 First += 2;
2879 return make<NameType>("operator&");
2880 case 'N':
2881 First += 2;
2882 return make<NameType>("operator&=");
2883 case 'S':
2884 First += 2;
2885 return make<NameType>("operator=");
2886 }
2887 return nullptr;
2888 case 'c':
2889 switch (look(1)) {
2890 case 'l':
2891 First += 2;
2892 return make<NameType>("operator()");
2893 case 'm':
2894 First += 2;
2895 return make<NameType>("operator,");
2896 case 'o':
2897 First += 2;
2898 return make<NameType>("operator~");
2899 // ::= cv <type> # (cast)
2900 case 'v': {
2901 First += 2;
2902 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2903 // If we're parsing an encoding, State != nullptr and the conversion
2904 // operators' <type> could have a <template-param> that refers to some
2905 // <template-arg>s further ahead in the mangled name.
2906 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2907 PermitForwardTemplateReferences ||
2908 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002909 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002910 if (Ty == nullptr)
2911 return nullptr;
2912 if (State) State->CtorDtorConversion = true;
2913 return make<ConversionOperatorType>(Ty);
2914 }
2915 }
2916 return nullptr;
2917 case 'd':
2918 switch (look(1)) {
2919 case 'a':
2920 First += 2;
2921 return make<NameType>("operator delete[]");
2922 case 'e':
2923 First += 2;
2924 return make<NameType>("operator*");
2925 case 'l':
2926 First += 2;
2927 return make<NameType>("operator delete");
2928 case 'v':
2929 First += 2;
2930 return make<NameType>("operator/");
2931 case 'V':
2932 First += 2;
2933 return make<NameType>("operator/=");
2934 }
2935 return nullptr;
2936 case 'e':
2937 switch (look(1)) {
2938 case 'o':
2939 First += 2;
2940 return make<NameType>("operator^");
2941 case 'O':
2942 First += 2;
2943 return make<NameType>("operator^=");
2944 case 'q':
2945 First += 2;
2946 return make<NameType>("operator==");
2947 }
2948 return nullptr;
2949 case 'g':
2950 switch (look(1)) {
2951 case 'e':
2952 First += 2;
2953 return make<NameType>("operator>=");
2954 case 't':
2955 First += 2;
2956 return make<NameType>("operator>");
2957 }
2958 return nullptr;
2959 case 'i':
2960 if (look(1) == 'x') {
2961 First += 2;
2962 return make<NameType>("operator[]");
2963 }
2964 return nullptr;
2965 case 'l':
2966 switch (look(1)) {
2967 case 'e':
2968 First += 2;
2969 return make<NameType>("operator<=");
2970 // ::= li <source-name> # operator ""
2971 case 'i': {
2972 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002973 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002974 if (SN == nullptr)
2975 return nullptr;
2976 return make<LiteralOperator>(SN);
2977 }
2978 case 's':
2979 First += 2;
2980 return make<NameType>("operator<<");
2981 case 'S':
2982 First += 2;
2983 return make<NameType>("operator<<=");
2984 case 't':
2985 First += 2;
2986 return make<NameType>("operator<");
2987 }
2988 return nullptr;
2989 case 'm':
2990 switch (look(1)) {
2991 case 'i':
2992 First += 2;
2993 return make<NameType>("operator-");
2994 case 'I':
2995 First += 2;
2996 return make<NameType>("operator-=");
2997 case 'l':
2998 First += 2;
2999 return make<NameType>("operator*");
3000 case 'L':
3001 First += 2;
3002 return make<NameType>("operator*=");
3003 case 'm':
3004 First += 2;
3005 return make<NameType>("operator--");
3006 }
3007 return nullptr;
3008 case 'n':
3009 switch (look(1)) {
3010 case 'a':
3011 First += 2;
3012 return make<NameType>("operator new[]");
3013 case 'e':
3014 First += 2;
3015 return make<NameType>("operator!=");
3016 case 'g':
3017 First += 2;
3018 return make<NameType>("operator-");
3019 case 't':
3020 First += 2;
3021 return make<NameType>("operator!");
3022 case 'w':
3023 First += 2;
3024 return make<NameType>("operator new");
3025 }
3026 return nullptr;
3027 case 'o':
3028 switch (look(1)) {
3029 case 'o':
3030 First += 2;
3031 return make<NameType>("operator||");
3032 case 'r':
3033 First += 2;
3034 return make<NameType>("operator|");
3035 case 'R':
3036 First += 2;
3037 return make<NameType>("operator|=");
3038 }
3039 return nullptr;
3040 case 'p':
3041 switch (look(1)) {
3042 case 'm':
3043 First += 2;
3044 return make<NameType>("operator->*");
3045 case 'l':
3046 First += 2;
3047 return make<NameType>("operator+");
3048 case 'L':
3049 First += 2;
3050 return make<NameType>("operator+=");
3051 case 'p':
3052 First += 2;
3053 return make<NameType>("operator++");
3054 case 's':
3055 First += 2;
3056 return make<NameType>("operator+");
3057 case 't':
3058 First += 2;
3059 return make<NameType>("operator->");
3060 }
3061 return nullptr;
3062 case 'q':
3063 if (look(1) == 'u') {
3064 First += 2;
3065 return make<NameType>("operator?");
3066 }
3067 return nullptr;
3068 case 'r':
3069 switch (look(1)) {
3070 case 'm':
3071 First += 2;
3072 return make<NameType>("operator%");
3073 case 'M':
3074 First += 2;
3075 return make<NameType>("operator%=");
3076 case 's':
3077 First += 2;
3078 return make<NameType>("operator>>");
3079 case 'S':
3080 First += 2;
3081 return make<NameType>("operator>>=");
3082 }
3083 return nullptr;
3084 case 's':
3085 if (look(1) == 's') {
3086 First += 2;
3087 return make<NameType>("operator<=>");
3088 }
3089 return nullptr;
3090 // ::= v <digit> <source-name> # vendor extended operator
3091 case 'v':
3092 if (std::isdigit(look(1))) {
3093 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003094 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003095 if (SN == nullptr)
3096 return nullptr;
3097 return make<ConversionOperatorType>(SN);
3098 }
3099 return nullptr;
3100 }
3101 return nullptr;
3102}
3103
3104// <ctor-dtor-name> ::= C1 # complete object constructor
3105// ::= C2 # base object constructor
3106// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003107// extension ::= C4 # gcc old-style "[unified]" constructor
3108// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003109// ::= D0 # deleting destructor
3110// ::= D1 # complete object destructor
3111// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003112// extension ::= D4 # gcc old-style "[unified]" destructor
3113// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003114template <typename Derived, typename Alloc>
3115Node *
3116AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3117 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003118 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3119 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3120 switch (SSK) {
3121 case SpecialSubKind::string:
3122 case SpecialSubKind::istream:
3123 case SpecialSubKind::ostream:
3124 case SpecialSubKind::iostream:
3125 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003126 if (!SoFar)
3127 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003128 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003129 default:
3130 break;
3131 }
3132 }
3133
3134 if (consumeIf('C')) {
3135 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003136 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3137 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003138 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003139 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003140 ++First;
3141 if (State) State->CtorDtorConversion = true;
3142 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003143 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003144 return nullptr;
3145 }
Nico Weber29294792019-04-03 23:14:33 +00003146 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003147 }
3148
Nico Weber29294792019-04-03 23:14:33 +00003149 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3150 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003151 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003152 First += 2;
3153 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003154 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003155 }
3156
3157 return nullptr;
3158}
3159
3160// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3161// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3162//
3163// <prefix> ::= <prefix> <unqualified-name>
3164// ::= <template-prefix> <template-args>
3165// ::= <template-param>
3166// ::= <decltype>
3167// ::= # empty
3168// ::= <substitution>
3169// ::= <prefix> <data-member-prefix>
3170// extension ::= L
3171//
3172// <data-member-prefix> := <member source-name> [<template-args>] M
3173//
3174// <template-prefix> ::= <prefix> <template unqualified-name>
3175// ::= <template-param>
3176// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003177template <typename Derived, typename Alloc>
3178Node *
3179AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003180 if (!consumeIf('N'))
3181 return nullptr;
3182
3183 Qualifiers CVTmp = parseCVQualifiers();
3184 if (State) State->CVQualifiers = CVTmp;
3185
3186 if (consumeIf('O')) {
3187 if (State) State->ReferenceQualifier = FrefQualRValue;
3188 } else if (consumeIf('R')) {
3189 if (State) State->ReferenceQualifier = FrefQualLValue;
3190 } else
3191 if (State) State->ReferenceQualifier = FrefQualNone;
3192
3193 Node *SoFar = nullptr;
3194 auto PushComponent = [&](Node *Comp) {
Richard Smithb485b352018-08-24 23:30:26 +00003195 if (!Comp) return false;
Richard Smithc20d1442018-08-20 20:14:49 +00003196 if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
3197 else SoFar = Comp;
3198 if (State) State->EndsWithTemplateArgs = false;
Richard Smithb485b352018-08-24 23:30:26 +00003199 return SoFar != nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003200 };
3201
Richard Smithb485b352018-08-24 23:30:26 +00003202 if (consumeIf("St")) {
Richard Smithc20d1442018-08-20 20:14:49 +00003203 SoFar = make<NameType>("std");
Richard Smithb485b352018-08-24 23:30:26 +00003204 if (!SoFar)
3205 return nullptr;
3206 }
Richard Smithc20d1442018-08-20 20:14:49 +00003207
3208 while (!consumeIf('E')) {
3209 consumeIf('L'); // extension
3210
3211 // <data-member-prefix> := <member source-name> [<template-args>] M
3212 if (consumeIf('M')) {
3213 if (SoFar == nullptr)
3214 return nullptr;
3215 continue;
3216 }
3217
3218 // ::= <template-param>
3219 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003220 if (!PushComponent(getDerived().parseTemplateParam()))
Richard Smithc20d1442018-08-20 20:14:49 +00003221 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003222 Subs.push_back(SoFar);
3223 continue;
3224 }
3225
3226 // ::= <template-prefix> <template-args>
3227 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003228 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003229 if (TA == nullptr || SoFar == nullptr)
3230 return nullptr;
3231 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003232 if (!SoFar)
3233 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003234 if (State) State->EndsWithTemplateArgs = true;
3235 Subs.push_back(SoFar);
3236 continue;
3237 }
3238
3239 // ::= <decltype>
3240 if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
Pavel Labathba825192018-10-16 14:29:14 +00003241 if (!PushComponent(getDerived().parseDecltype()))
Richard Smithc20d1442018-08-20 20:14:49 +00003242 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003243 Subs.push_back(SoFar);
3244 continue;
3245 }
3246
3247 // ::= <substitution>
3248 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00003249 Node *S = getDerived().parseSubstitution();
Richard Smithb485b352018-08-24 23:30:26 +00003250 if (!PushComponent(S))
Richard Smithc20d1442018-08-20 20:14:49 +00003251 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003252 if (SoFar != S)
3253 Subs.push_back(S);
3254 continue;
3255 }
3256
3257 // Parse an <unqualified-name> thats actually a <ctor-dtor-name>.
3258 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
3259 if (SoFar == nullptr)
3260 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003261 if (!PushComponent(getDerived().parseCtorDtorName(SoFar, State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003262 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003263 SoFar = getDerived().parseAbiTags(SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00003264 if (SoFar == nullptr)
3265 return nullptr;
3266 Subs.push_back(SoFar);
3267 continue;
3268 }
3269
3270 // ::= <prefix> <unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00003271 if (!PushComponent(getDerived().parseUnqualifiedName(State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003272 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003273 Subs.push_back(SoFar);
3274 }
3275
3276 if (SoFar == nullptr || Subs.empty())
3277 return nullptr;
3278
3279 Subs.pop_back();
3280 return SoFar;
3281}
3282
3283// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003284template <typename Derived, typename Alloc>
3285Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3286 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003287 if (SN == nullptr)
3288 return nullptr;
3289 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003290 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003291 if (TA == nullptr)
3292 return nullptr;
3293 return make<NameWithTemplateArgs>(SN, TA);
3294 }
3295 return SN;
3296}
3297
3298// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3299// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003300template <typename Derived, typename Alloc>
3301Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003302 Node *Result;
3303 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003304 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003305 else
Pavel Labathba825192018-10-16 14:29:14 +00003306 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003307 if (Result == nullptr)
3308 return nullptr;
3309 return make<DtorName>(Result);
3310}
3311
3312// <unresolved-type> ::= <template-param>
3313// ::= <decltype>
3314// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003315template <typename Derived, typename Alloc>
3316Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003317 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003318 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003319 if (TP == nullptr)
3320 return nullptr;
3321 Subs.push_back(TP);
3322 return TP;
3323 }
3324 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003325 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003326 if (DT == nullptr)
3327 return nullptr;
3328 Subs.push_back(DT);
3329 return DT;
3330 }
Pavel Labathba825192018-10-16 14:29:14 +00003331 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003332}
3333
3334// <base-unresolved-name> ::= <simple-id> # unresolved name
3335// extension ::= <operator-name> # unresolved operator-function-id
3336// extension ::= <operator-name> <template-args> # unresolved operator template-id
3337// ::= on <operator-name> # unresolved operator-function-id
3338// ::= on <operator-name> <template-args> # unresolved operator template-id
3339// ::= dn <destructor-name> # destructor or pseudo-destructor;
3340// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003341template <typename Derived, typename Alloc>
3342Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003343 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003344 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003345
3346 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003347 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003348
3349 consumeIf("on");
3350
Pavel Labathba825192018-10-16 14:29:14 +00003351 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003352 if (Oper == nullptr)
3353 return nullptr;
3354 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003355 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003356 if (TA == nullptr)
3357 return nullptr;
3358 return make<NameWithTemplateArgs>(Oper, TA);
3359 }
3360 return Oper;
3361}
3362
3363// <unresolved-name>
3364// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3365// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3366// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3367// # A::x, N::y, A<T>::z; "gs" means leading "::"
3368// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3369// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3370// # T::N::x /decltype(p)::N::x
3371// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3372//
3373// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003374template <typename Derived, typename Alloc>
3375Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003376 Node *SoFar = nullptr;
3377
3378 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3379 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3380 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003381 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003382 if (SoFar == nullptr)
3383 return nullptr;
3384
3385 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003386 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003387 if (TA == nullptr)
3388 return nullptr;
3389 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003390 if (!SoFar)
3391 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003392 }
3393
3394 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003395 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003396 if (Qual == nullptr)
3397 return nullptr;
3398 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003399 if (!SoFar)
3400 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003401 }
3402
Pavel Labathba825192018-10-16 14:29:14 +00003403 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003404 if (Base == nullptr)
3405 return nullptr;
3406 return make<QualifiedName>(SoFar, Base);
3407 }
3408
3409 bool Global = consumeIf("gs");
3410
3411 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3412 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003413 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003414 if (SoFar == nullptr)
3415 return nullptr;
3416 if (Global)
3417 SoFar = make<GlobalQualifiedName>(SoFar);
3418 return SoFar;
3419 }
3420
3421 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3422 if (std::isdigit(look())) {
3423 do {
Pavel Labathba825192018-10-16 14:29:14 +00003424 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003425 if (Qual == nullptr)
3426 return nullptr;
3427 if (SoFar)
3428 SoFar = make<QualifiedName>(SoFar, Qual);
3429 else if (Global)
3430 SoFar = make<GlobalQualifiedName>(Qual);
3431 else
3432 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003433 if (!SoFar)
3434 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003435 } while (!consumeIf('E'));
3436 }
3437 // sr <unresolved-type> <base-unresolved-name>
3438 // sr <unresolved-type> <template-args> <base-unresolved-name>
3439 else {
Pavel Labathba825192018-10-16 14:29:14 +00003440 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003441 if (SoFar == nullptr)
3442 return nullptr;
3443
3444 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003445 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003446 if (TA == nullptr)
3447 return nullptr;
3448 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003449 if (!SoFar)
3450 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003451 }
3452 }
3453
3454 assert(SoFar != nullptr);
3455
Pavel Labathba825192018-10-16 14:29:14 +00003456 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003457 if (Base == nullptr)
3458 return nullptr;
3459 return make<QualifiedName>(SoFar, Base);
3460}
3461
3462// <abi-tags> ::= <abi-tag> [<abi-tags>]
3463// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003464template <typename Derived, typename Alloc>
3465Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003466 while (consumeIf('B')) {
3467 StringView SN = parseBareSourceName();
3468 if (SN.empty())
3469 return nullptr;
3470 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003471 if (!N)
3472 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003473 }
3474 return N;
3475}
3476
3477// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003478template <typename Alloc, typename Derived>
3479StringView
3480AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003481 const char *Tmp = First;
3482 if (AllowNegative)
3483 consumeIf('n');
3484 if (numLeft() == 0 || !std::isdigit(*First))
3485 return StringView();
3486 while (numLeft() != 0 && std::isdigit(*First))
3487 ++First;
3488 return StringView(Tmp, First);
3489}
3490
3491// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003492template <typename Alloc, typename Derived>
3493bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003494 *Out = 0;
3495 if (look() < '0' || look() > '9')
3496 return true;
3497 while (look() >= '0' && look() <= '9') {
3498 *Out *= 10;
3499 *Out += static_cast<size_t>(consume() - '0');
3500 }
3501 return false;
3502}
3503
Pavel Labathba825192018-10-16 14:29:14 +00003504template <typename Alloc, typename Derived>
3505StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003506 size_t Int = 0;
3507 if (parsePositiveInteger(&Int) || numLeft() < Int)
3508 return StringView();
3509 StringView R(First, First + Int);
3510 First += Int;
3511 return R;
3512}
3513
3514// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3515//
3516// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3517// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3518// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3519//
3520// <ref-qualifier> ::= R # & ref-qualifier
3521// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003522template <typename Derived, typename Alloc>
3523Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003524 Qualifiers CVQuals = parseCVQualifiers();
3525
3526 Node *ExceptionSpec = nullptr;
3527 if (consumeIf("Do")) {
3528 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003529 if (!ExceptionSpec)
3530 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003531 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003532 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003533 if (E == nullptr || !consumeIf('E'))
3534 return nullptr;
3535 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003536 if (!ExceptionSpec)
3537 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003538 } else if (consumeIf("Dw")) {
3539 size_t SpecsBegin = Names.size();
3540 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003541 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003542 if (T == nullptr)
3543 return nullptr;
3544 Names.push_back(T);
3545 }
3546 ExceptionSpec =
3547 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003548 if (!ExceptionSpec)
3549 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003550 }
3551
3552 consumeIf("Dx"); // transaction safe
3553
3554 if (!consumeIf('F'))
3555 return nullptr;
3556 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003557 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003558 if (ReturnType == nullptr)
3559 return nullptr;
3560
3561 FunctionRefQual ReferenceQualifier = FrefQualNone;
3562 size_t ParamsBegin = Names.size();
3563 while (true) {
3564 if (consumeIf('E'))
3565 break;
3566 if (consumeIf('v'))
3567 continue;
3568 if (consumeIf("RE")) {
3569 ReferenceQualifier = FrefQualLValue;
3570 break;
3571 }
3572 if (consumeIf("OE")) {
3573 ReferenceQualifier = FrefQualRValue;
3574 break;
3575 }
Pavel Labathba825192018-10-16 14:29:14 +00003576 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003577 if (T == nullptr)
3578 return nullptr;
3579 Names.push_back(T);
3580 }
3581
3582 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3583 return make<FunctionType>(ReturnType, Params, CVQuals,
3584 ReferenceQualifier, ExceptionSpec);
3585}
3586
3587// extension:
3588// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3589// ::= Dv [<dimension expression>] _ <element type>
3590// <extended element type> ::= <element type>
3591// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003592template <typename Derived, typename Alloc>
3593Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003594 if (!consumeIf("Dv"))
3595 return nullptr;
3596 if (look() >= '1' && look() <= '9') {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003597 Node *DimensionNumber = make<NameType>(parseNumber());
3598 if (!DimensionNumber)
3599 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003600 if (!consumeIf('_'))
3601 return nullptr;
3602 if (consumeIf('p'))
3603 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003604 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003605 if (ElemType == nullptr)
3606 return nullptr;
3607 return make<VectorType>(ElemType, DimensionNumber);
3608 }
3609
3610 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003611 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003612 if (!DimExpr)
3613 return nullptr;
3614 if (!consumeIf('_'))
3615 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003616 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003617 if (!ElemType)
3618 return nullptr;
3619 return make<VectorType>(ElemType, DimExpr);
3620 }
Pavel Labathba825192018-10-16 14:29:14 +00003621 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003622 if (!ElemType)
3623 return nullptr;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003624 return make<VectorType>(ElemType, /*Dimension=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003625}
3626
3627// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3628// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003629template <typename Derived, typename Alloc>
3630Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003631 if (!consumeIf('D'))
3632 return nullptr;
3633 if (!consumeIf('t') && !consumeIf('T'))
3634 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003635 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003636 if (E == nullptr)
3637 return nullptr;
3638 if (!consumeIf('E'))
3639 return nullptr;
3640 return make<EnclosingExpr>("decltype(", E, ")");
3641}
3642
3643// <array-type> ::= A <positive dimension number> _ <element type>
3644// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003645template <typename Derived, typename Alloc>
3646Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003647 if (!consumeIf('A'))
3648 return nullptr;
3649
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003650 Node *Dimension = nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003651
Richard Smithc20d1442018-08-20 20:14:49 +00003652 if (std::isdigit(look())) {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003653 Dimension = make<NameType>(parseNumber());
3654 if (!Dimension)
3655 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003656 if (!consumeIf('_'))
3657 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003658 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003659 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003660 if (DimExpr == nullptr)
3661 return nullptr;
3662 if (!consumeIf('_'))
3663 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003664 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003665 }
3666
Pavel Labathba825192018-10-16 14:29:14 +00003667 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003668 if (Ty == nullptr)
3669 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003670 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003671}
3672
3673// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003674template <typename Derived, typename Alloc>
3675Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003676 if (!consumeIf('M'))
3677 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003678 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003679 if (ClassType == nullptr)
3680 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003681 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003682 if (MemberType == nullptr)
3683 return nullptr;
3684 return make<PointerToMemberType>(ClassType, MemberType);
3685}
3686
3687// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3688// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3689// ::= Tu <name> # dependent elaborated type specifier using 'union'
3690// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003691template <typename Derived, typename Alloc>
3692Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003693 StringView ElabSpef;
3694 if (consumeIf("Ts"))
3695 ElabSpef = "struct";
3696 else if (consumeIf("Tu"))
3697 ElabSpef = "union";
3698 else if (consumeIf("Te"))
3699 ElabSpef = "enum";
3700
Pavel Labathba825192018-10-16 14:29:14 +00003701 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003702 if (Name == nullptr)
3703 return nullptr;
3704
3705 if (!ElabSpef.empty())
3706 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3707
3708 return Name;
3709}
3710
3711// <qualified-type> ::= <qualifiers> <type>
3712// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3713// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003714template <typename Derived, typename Alloc>
3715Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003716 if (consumeIf('U')) {
3717 StringView Qual = parseBareSourceName();
3718 if (Qual.empty())
3719 return nullptr;
3720
Richard Smithc20d1442018-08-20 20:14:49 +00003721 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3722 if (Qual.startsWith("objcproto")) {
3723 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3724 StringView Proto;
3725 {
3726 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3727 SaveLast(Last, ProtoSourceName.end());
3728 Proto = parseBareSourceName();
3729 }
3730 if (Proto.empty())
3731 return nullptr;
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;
3735 return make<ObjCProtoName>(Child, Proto);
3736 }
3737
Alex Orlovf50df922021-03-24 10:21:32 +04003738 Node *TA = nullptr;
3739 if (look() == 'I') {
3740 TA = getDerived().parseTemplateArgs();
3741 if (TA == nullptr)
3742 return nullptr;
3743 }
3744
Pavel Labathba825192018-10-16 14:29:14 +00003745 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003746 if (Child == nullptr)
3747 return nullptr;
Alex Orlovf50df922021-03-24 10:21:32 +04003748 return make<VendorExtQualType>(Child, Qual, TA);
Richard Smithc20d1442018-08-20 20:14:49 +00003749 }
3750
3751 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003752 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003753 if (Ty == nullptr)
3754 return nullptr;
3755 if (Quals != QualNone)
3756 Ty = make<QualType>(Ty, Quals);
3757 return Ty;
3758}
3759
3760// <type> ::= <builtin-type>
3761// ::= <qualified-type>
3762// ::= <function-type>
3763// ::= <class-enum-type>
3764// ::= <array-type>
3765// ::= <pointer-to-member-type>
3766// ::= <template-param>
3767// ::= <template-template-param> <template-args>
3768// ::= <decltype>
3769// ::= P <type> # pointer
3770// ::= R <type> # l-value reference
3771// ::= O <type> # r-value reference (C++11)
3772// ::= C <type> # complex pair (C99)
3773// ::= G <type> # imaginary (C99)
3774// ::= <substitution> # See Compression below
3775// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3776// extension ::= <vector-type> # <vector-type> starts with Dv
3777//
3778// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3779// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003780template <typename Derived, typename Alloc>
3781Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003782 Node *Result = nullptr;
3783
Richard Smithc20d1442018-08-20 20:14:49 +00003784 switch (look()) {
3785 // ::= <qualified-type>
3786 case 'r':
3787 case 'V':
3788 case 'K': {
3789 unsigned AfterQuals = 0;
3790 if (look(AfterQuals) == 'r') ++AfterQuals;
3791 if (look(AfterQuals) == 'V') ++AfterQuals;
3792 if (look(AfterQuals) == 'K') ++AfterQuals;
3793
3794 if (look(AfterQuals) == 'F' ||
3795 (look(AfterQuals) == 'D' &&
3796 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3797 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003798 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003799 break;
3800 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003801 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003802 }
3803 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003804 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003805 break;
3806 }
3807 // <builtin-type> ::= v # void
3808 case 'v':
3809 ++First;
3810 return make<NameType>("void");
3811 // ::= w # wchar_t
3812 case 'w':
3813 ++First;
3814 return make<NameType>("wchar_t");
3815 // ::= b # bool
3816 case 'b':
3817 ++First;
3818 return make<NameType>("bool");
3819 // ::= c # char
3820 case 'c':
3821 ++First;
3822 return make<NameType>("char");
3823 // ::= a # signed char
3824 case 'a':
3825 ++First;
3826 return make<NameType>("signed char");
3827 // ::= h # unsigned char
3828 case 'h':
3829 ++First;
3830 return make<NameType>("unsigned char");
3831 // ::= s # short
3832 case 's':
3833 ++First;
3834 return make<NameType>("short");
3835 // ::= t # unsigned short
3836 case 't':
3837 ++First;
3838 return make<NameType>("unsigned short");
3839 // ::= i # int
3840 case 'i':
3841 ++First;
3842 return make<NameType>("int");
3843 // ::= j # unsigned int
3844 case 'j':
3845 ++First;
3846 return make<NameType>("unsigned int");
3847 // ::= l # long
3848 case 'l':
3849 ++First;
3850 return make<NameType>("long");
3851 // ::= m # unsigned long
3852 case 'm':
3853 ++First;
3854 return make<NameType>("unsigned long");
3855 // ::= x # long long, __int64
3856 case 'x':
3857 ++First;
3858 return make<NameType>("long long");
3859 // ::= y # unsigned long long, __int64
3860 case 'y':
3861 ++First;
3862 return make<NameType>("unsigned long long");
3863 // ::= n # __int128
3864 case 'n':
3865 ++First;
3866 return make<NameType>("__int128");
3867 // ::= o # unsigned __int128
3868 case 'o':
3869 ++First;
3870 return make<NameType>("unsigned __int128");
3871 // ::= f # float
3872 case 'f':
3873 ++First;
3874 return make<NameType>("float");
3875 // ::= d # double
3876 case 'd':
3877 ++First;
3878 return make<NameType>("double");
3879 // ::= e # long double, __float80
3880 case 'e':
3881 ++First;
3882 return make<NameType>("long double");
3883 // ::= g # __float128
3884 case 'g':
3885 ++First;
3886 return make<NameType>("__float128");
3887 // ::= z # ellipsis
3888 case 'z':
3889 ++First;
3890 return make<NameType>("...");
3891
3892 // <builtin-type> ::= u <source-name> # vendor extended type
3893 case 'u': {
3894 ++First;
3895 StringView Res = parseBareSourceName();
3896 if (Res.empty())
3897 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003898 // Typically, <builtin-type>s are not considered substitution candidates,
3899 // but the exception to that exception is vendor extended types (Itanium C++
3900 // ABI 5.9.1).
3901 Result = make<NameType>(Res);
3902 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003903 }
3904 case 'D':
3905 switch (look(1)) {
3906 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3907 case 'd':
3908 First += 2;
3909 return make<NameType>("decimal64");
3910 // ::= De # IEEE 754r decimal floating point (128 bits)
3911 case 'e':
3912 First += 2;
3913 return make<NameType>("decimal128");
3914 // ::= Df # IEEE 754r decimal floating point (32 bits)
3915 case 'f':
3916 First += 2;
3917 return make<NameType>("decimal32");
3918 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3919 case 'h':
3920 First += 2;
Stuart Bradye8bf5772021-06-07 16:30:22 +01003921 return make<NameType>("half");
Pengfei Wang50e90b82021-09-23 11:02:25 +08003922 // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits)
3923 case 'F': {
3924 First += 2;
3925 Node *DimensionNumber = make<NameType>(parseNumber());
3926 if (!DimensionNumber)
3927 return nullptr;
3928 if (!consumeIf('_'))
3929 return nullptr;
3930 return make<BinaryFPType>(DimensionNumber);
3931 }
Richard Smithc20d1442018-08-20 20:14:49 +00003932 // ::= Di # char32_t
3933 case 'i':
3934 First += 2;
3935 return make<NameType>("char32_t");
3936 // ::= Ds # char16_t
3937 case 's':
3938 First += 2;
3939 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003940 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3941 case 'u':
3942 First += 2;
3943 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003944 // ::= Da # auto (in dependent new-expressions)
3945 case 'a':
3946 First += 2;
3947 return make<NameType>("auto");
3948 // ::= Dc # decltype(auto)
3949 case 'c':
3950 First += 2;
3951 return make<NameType>("decltype(auto)");
3952 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3953 case 'n':
3954 First += 2;
3955 return make<NameType>("std::nullptr_t");
3956
3957 // ::= <decltype>
3958 case 't':
3959 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003960 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003961 break;
3962 }
3963 // extension ::= <vector-type> # <vector-type> starts with Dv
3964 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003965 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003966 break;
3967 }
3968 // ::= Dp <type> # pack expansion (C++0x)
3969 case 'p': {
3970 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003971 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003972 if (!Child)
3973 return nullptr;
3974 Result = make<ParameterPackExpansion>(Child);
3975 break;
3976 }
3977 // Exception specifier on a function type.
3978 case 'o':
3979 case 'O':
3980 case 'w':
3981 // Transaction safe function type.
3982 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003983 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003984 break;
3985 }
3986 break;
3987 // ::= <function-type>
3988 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003989 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003990 break;
3991 }
3992 // ::= <array-type>
3993 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003994 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003995 break;
3996 }
3997 // ::= <pointer-to-member-type>
3998 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003999 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00004000 break;
4001 }
4002 // ::= <template-param>
4003 case 'T': {
4004 // This could be an elaborate type specifier on a <class-enum-type>.
4005 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00004006 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004007 break;
4008 }
4009
Pavel Labathba825192018-10-16 14:29:14 +00004010 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004011 if (Result == nullptr)
4012 return nullptr;
4013
4014 // Result could be either of:
4015 // <type> ::= <template-param>
4016 // <type> ::= <template-template-param> <template-args>
4017 //
4018 // <template-template-param> ::= <template-param>
4019 // ::= <substitution>
4020 //
4021 // If this is followed by some <template-args>, and we're permitted to
4022 // parse them, take the second production.
4023
4024 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004025 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004026 if (TA == nullptr)
4027 return nullptr;
4028 Result = make<NameWithTemplateArgs>(Result, TA);
4029 }
4030 break;
4031 }
4032 // ::= P <type> # pointer
4033 case 'P': {
4034 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004035 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004036 if (Ptr == nullptr)
4037 return nullptr;
4038 Result = make<PointerType>(Ptr);
4039 break;
4040 }
4041 // ::= R <type> # l-value reference
4042 case 'R': {
4043 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004044 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004045 if (Ref == nullptr)
4046 return nullptr;
4047 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
4048 break;
4049 }
4050 // ::= O <type> # r-value reference (C++11)
4051 case 'O': {
4052 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004053 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004054 if (Ref == nullptr)
4055 return nullptr;
4056 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
4057 break;
4058 }
4059 // ::= C <type> # complex pair (C99)
4060 case 'C': {
4061 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004062 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004063 if (P == nullptr)
4064 return nullptr;
4065 Result = make<PostfixQualifiedType>(P, " complex");
4066 break;
4067 }
4068 // ::= G <type> # imaginary (C99)
4069 case 'G': {
4070 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004071 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004072 if (P == nullptr)
4073 return P;
4074 Result = make<PostfixQualifiedType>(P, " imaginary");
4075 break;
4076 }
4077 // ::= <substitution> # See Compression below
4078 case 'S': {
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004079 if (look(1) != 't') {
4080 Result = getDerived().parseSubstitution();
4081 if (Result == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00004082 return nullptr;
4083
4084 // Sub could be either of:
4085 // <type> ::= <substitution>
4086 // <type> ::= <template-template-param> <template-args>
4087 //
4088 // <template-template-param> ::= <template-param>
4089 // ::= <substitution>
4090 //
4091 // If this is followed by some <template-args>, and we're permitted to
4092 // parse them, take the second production.
4093
4094 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004095 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004096 if (TA == nullptr)
4097 return nullptr;
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004098 Result = make<NameWithTemplateArgs>(Result, TA);
4099 } else {
4100 // If all we parsed was a substitution, don't re-insert into the
4101 // substitution table.
4102 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00004103 }
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004104 break;
Richard Smithc20d1442018-08-20 20:14:49 +00004105 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00004106 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004107 }
4108 // ::= <class-enum-type>
4109 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004110 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004111 break;
4112 }
4113 }
4114
4115 // If we parsed a type, insert it into the substitution table. Note that all
4116 // <builtin-type>s and <substitution>s have already bailed out, because they
4117 // don't get substitutions.
4118 if (Result != nullptr)
4119 Subs.push_back(Result);
4120 return Result;
4121}
4122
Pavel Labathba825192018-10-16 14:29:14 +00004123template <typename Derived, typename Alloc>
4124Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4125 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004126 if (E == nullptr)
4127 return nullptr;
4128 return make<PrefixExpr>(Kind, E);
4129}
4130
Pavel Labathba825192018-10-16 14:29:14 +00004131template <typename Derived, typename Alloc>
4132Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4133 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004134 if (LHS == nullptr)
4135 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004136 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004137 if (RHS == nullptr)
4138 return nullptr;
4139 return make<BinaryExpr>(LHS, Kind, RHS);
4140}
4141
Pavel Labathba825192018-10-16 14:29:14 +00004142template <typename Derived, typename Alloc>
4143Node *
4144AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004145 StringView Tmp = parseNumber(true);
4146 if (!Tmp.empty() && consumeIf('E'))
4147 return make<IntegerLiteral>(Lit, Tmp);
4148 return nullptr;
4149}
4150
4151// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004152template <typename Alloc, typename Derived>
4153Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004154 Qualifiers CVR = QualNone;
4155 if (consumeIf('r'))
4156 CVR |= QualRestrict;
4157 if (consumeIf('V'))
4158 CVR |= QualVolatile;
4159 if (consumeIf('K'))
4160 CVR |= QualConst;
4161 return CVR;
4162}
4163
4164// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4165// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4166// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4167// ::= 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 -04004168// ::= fpT # 'this' expression (not part of standard?)
Pavel Labathba825192018-10-16 14:29:14 +00004169template <typename Derived, typename Alloc>
4170Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Erik Pilkington91c24af2020-05-13 22:19:45 -04004171 if (consumeIf("fpT"))
4172 return make<NameType>("this");
Richard Smithc20d1442018-08-20 20:14:49 +00004173 if (consumeIf("fp")) {
4174 parseCVQualifiers();
4175 StringView Num = parseNumber();
4176 if (!consumeIf('_'))
4177 return nullptr;
4178 return make<FunctionParam>(Num);
4179 }
4180 if (consumeIf("fL")) {
4181 if (parseNumber().empty())
4182 return nullptr;
4183 if (!consumeIf('p'))
4184 return nullptr;
4185 parseCVQualifiers();
4186 StringView Num = parseNumber();
4187 if (!consumeIf('_'))
4188 return nullptr;
4189 return make<FunctionParam>(Num);
4190 }
4191 return nullptr;
4192}
4193
4194// [gs] nw <expression>* _ <type> E # new (expr-list) type
4195// [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4196// [gs] na <expression>* _ <type> E # new[] (expr-list) type
4197// [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4198// <initializer> ::= pi <expression>* E # parenthesized initialization
Pavel Labathba825192018-10-16 14:29:14 +00004199template <typename Derived, typename Alloc>
4200Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004201 bool Global = consumeIf("gs");
4202 bool IsArray = look(1) == 'a';
4203 if (!consumeIf("nw") && !consumeIf("na"))
4204 return nullptr;
4205 size_t Exprs = Names.size();
4206 while (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00004207 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004208 if (Ex == nullptr)
4209 return nullptr;
4210 Names.push_back(Ex);
4211 }
4212 NodeArray ExprList = popTrailingNodeArray(Exprs);
Pavel Labathba825192018-10-16 14:29:14 +00004213 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004214 if (Ty == nullptr)
4215 return Ty;
4216 if (consumeIf("pi")) {
4217 size_t InitsBegin = Names.size();
4218 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004219 Node *Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004220 if (Init == nullptr)
4221 return Init;
4222 Names.push_back(Init);
4223 }
4224 NodeArray Inits = popTrailingNodeArray(InitsBegin);
4225 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
4226 } else if (!consumeIf('E'))
4227 return nullptr;
4228 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
4229}
4230
4231// cv <type> <expression> # conversion with one argument
4232// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004233template <typename Derived, typename Alloc>
4234Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004235 if (!consumeIf("cv"))
4236 return nullptr;
4237 Node *Ty;
4238 {
4239 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004240 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004241 }
4242
4243 if (Ty == nullptr)
4244 return nullptr;
4245
4246 if (consumeIf('_')) {
4247 size_t ExprsBegin = Names.size();
4248 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004249 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004250 if (E == nullptr)
4251 return E;
4252 Names.push_back(E);
4253 }
4254 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4255 return make<ConversionExpr>(Ty, Exprs);
4256 }
4257
Pavel Labathba825192018-10-16 14:29:14 +00004258 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004259 if (E[0] == nullptr)
4260 return nullptr;
4261 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4262}
4263
4264// <expr-primary> ::= L <type> <value number> E # integer literal
4265// ::= L <type> <value float> E # floating literal
4266// ::= L <string type> E # string literal
4267// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004268// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004269// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4270// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004271template <typename Derived, typename Alloc>
4272Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004273 if (!consumeIf('L'))
4274 return nullptr;
4275 switch (look()) {
4276 case 'w':
4277 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004278 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004279 case 'b':
4280 if (consumeIf("b0E"))
4281 return make<BoolExpr>(0);
4282 if (consumeIf("b1E"))
4283 return make<BoolExpr>(1);
4284 return nullptr;
4285 case 'c':
4286 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004287 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004288 case 'a':
4289 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004290 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004291 case 'h':
4292 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004293 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004294 case 's':
4295 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004296 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004297 case 't':
4298 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004299 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004300 case 'i':
4301 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004302 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004303 case 'j':
4304 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004305 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004306 case 'l':
4307 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004308 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004309 case 'm':
4310 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004311 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004312 case 'x':
4313 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004314 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004315 case 'y':
4316 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004317 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004318 case 'n':
4319 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004320 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004321 case 'o':
4322 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004323 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004324 case 'f':
4325 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004326 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004327 case 'd':
4328 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004329 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004330 case 'e':
4331 ++First;
Xing Xue3dc5e082020-04-15 09:59:06 -04004332#if defined(__powerpc__) || defined(__s390__)
4333 // Handle cases where long doubles encoded with e have the same size
4334 // and representation as doubles.
4335 return getDerived().template parseFloatingLiteral<double>();
4336#else
Pavel Labathba825192018-10-16 14:29:14 +00004337 return getDerived().template parseFloatingLiteral<long double>();
Xing Xue3dc5e082020-04-15 09:59:06 -04004338#endif
Richard Smithc20d1442018-08-20 20:14:49 +00004339 case '_':
4340 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004341 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004342 if (R != nullptr && consumeIf('E'))
4343 return R;
4344 }
4345 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004346 case 'A': {
4347 Node *T = getDerived().parseType();
4348 if (T == nullptr)
4349 return nullptr;
4350 // FIXME: We need to include the string contents in the mangling.
4351 if (consumeIf('E'))
4352 return make<StringLiteral>(T);
4353 return nullptr;
4354 }
4355 case 'D':
4356 if (consumeIf("DnE"))
4357 return make<NameType>("nullptr");
4358 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004359 case 'T':
4360 // Invalid mangled name per
4361 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4362 return nullptr;
Richard Smithfb917462019-09-09 22:26:04 +00004363 case 'U': {
4364 // FIXME: Should we support LUb... for block literals?
4365 if (look(1) != 'l')
4366 return nullptr;
4367 Node *T = parseUnnamedTypeName(nullptr);
4368 if (!T || !consumeIf('E'))
4369 return nullptr;
4370 return make<LambdaExpr>(T);
4371 }
Richard Smithc20d1442018-08-20 20:14:49 +00004372 default: {
4373 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004374 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004375 if (T == nullptr)
4376 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004377 StringView N = parseNumber(/*AllowNegative=*/true);
Richard Smithfb917462019-09-09 22:26:04 +00004378 if (N.empty())
4379 return nullptr;
4380 if (!consumeIf('E'))
4381 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004382 return make<EnumLiteral>(T, N);
Richard Smithc20d1442018-08-20 20:14:49 +00004383 }
4384 }
4385}
4386
4387// <braced-expression> ::= <expression>
4388// ::= di <field source-name> <braced-expression> # .name = expr
4389// ::= dx <index expression> <braced-expression> # [expr] = expr
4390// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004391template <typename Derived, typename Alloc>
4392Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004393 if (look() == 'd') {
4394 switch (look(1)) {
4395 case 'i': {
4396 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004397 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004398 if (Field == nullptr)
4399 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004400 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004401 if (Init == nullptr)
4402 return nullptr;
4403 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4404 }
4405 case 'x': {
4406 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004407 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004408 if (Index == 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<BracedExpr>(Index, Init, /*isArray=*/true);
4414 }
4415 case 'X': {
4416 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004417 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004418 if (RangeBegin == nullptr)
4419 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004420 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004421 if (RangeEnd == nullptr)
4422 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004423 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004424 if (Init == nullptr)
4425 return nullptr;
4426 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4427 }
4428 }
4429 }
Pavel Labathba825192018-10-16 14:29:14 +00004430 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004431}
4432
4433// (not yet in the spec)
4434// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4435// ::= fR <binary-operator-name> <expression> <expression>
4436// ::= fl <binary-operator-name> <expression>
4437// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004438template <typename Derived, typename Alloc>
4439Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004440 if (!consumeIf('f'))
4441 return nullptr;
4442
4443 char FoldKind = look();
4444 bool IsLeftFold, HasInitializer;
4445 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4446 if (FoldKind == 'l' || FoldKind == 'L')
4447 IsLeftFold = true;
4448 else if (FoldKind == 'r' || FoldKind == 'R')
4449 IsLeftFold = false;
4450 else
4451 return nullptr;
4452 ++First;
4453
4454 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4455 StringView OperatorName;
4456 if (consumeIf("aa")) OperatorName = "&&";
4457 else if (consumeIf("an")) OperatorName = "&";
4458 else if (consumeIf("aN")) OperatorName = "&=";
4459 else if (consumeIf("aS")) OperatorName = "=";
4460 else if (consumeIf("cm")) OperatorName = ",";
4461 else if (consumeIf("ds")) OperatorName = ".*";
4462 else if (consumeIf("dv")) OperatorName = "/";
4463 else if (consumeIf("dV")) OperatorName = "/=";
4464 else if (consumeIf("eo")) OperatorName = "^";
4465 else if (consumeIf("eO")) OperatorName = "^=";
4466 else if (consumeIf("eq")) OperatorName = "==";
4467 else if (consumeIf("ge")) OperatorName = ">=";
4468 else if (consumeIf("gt")) OperatorName = ">";
4469 else if (consumeIf("le")) OperatorName = "<=";
4470 else if (consumeIf("ls")) OperatorName = "<<";
4471 else if (consumeIf("lS")) OperatorName = "<<=";
4472 else if (consumeIf("lt")) OperatorName = "<";
4473 else if (consumeIf("mi")) OperatorName = "-";
4474 else if (consumeIf("mI")) OperatorName = "-=";
4475 else if (consumeIf("ml")) OperatorName = "*";
4476 else if (consumeIf("mL")) OperatorName = "*=";
4477 else if (consumeIf("ne")) OperatorName = "!=";
4478 else if (consumeIf("oo")) OperatorName = "||";
4479 else if (consumeIf("or")) OperatorName = "|";
4480 else if (consumeIf("oR")) OperatorName = "|=";
4481 else if (consumeIf("pl")) OperatorName = "+";
4482 else if (consumeIf("pL")) OperatorName = "+=";
4483 else if (consumeIf("rm")) OperatorName = "%";
4484 else if (consumeIf("rM")) OperatorName = "%=";
4485 else if (consumeIf("rs")) OperatorName = ">>";
4486 else if (consumeIf("rS")) OperatorName = ">>=";
4487 else return nullptr;
4488
Pavel Labathba825192018-10-16 14:29:14 +00004489 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004490 if (Pack == nullptr)
4491 return nullptr;
4492 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004493 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004494 if (Init == nullptr)
4495 return nullptr;
4496 }
4497
4498 if (IsLeftFold && Init)
4499 std::swap(Pack, Init);
4500
4501 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4502}
4503
Richard Smith1865d2f2020-10-22 19:29:36 -07004504// <expression> ::= mc <parameter type> <expr> [<offset number>] E
4505//
4506// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4507template <typename Derived, typename Alloc>
4508Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() {
4509 Node *Ty = getDerived().parseType();
4510 if (!Ty)
4511 return nullptr;
4512 Node *Expr = getDerived().parseExpr();
4513 if (!Expr)
4514 return nullptr;
4515 StringView Offset = getDerived().parseNumber(true);
4516 if (!consumeIf('E'))
4517 return nullptr;
4518 return make<PointerToMemberConversionExpr>(Ty, Expr, Offset);
4519}
4520
4521// <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
4522// <union-selector> ::= _ [<number>]
4523//
4524// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4525template <typename Derived, typename Alloc>
4526Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
4527 Node *Ty = getDerived().parseType();
4528 if (!Ty)
4529 return nullptr;
4530 Node *Expr = getDerived().parseExpr();
4531 if (!Expr)
4532 return nullptr;
4533 StringView Offset = getDerived().parseNumber(true);
4534 size_t SelectorsBegin = Names.size();
4535 while (consumeIf('_')) {
4536 Node *Selector = make<NameType>(parseNumber());
4537 if (!Selector)
4538 return nullptr;
4539 Names.push_back(Selector);
4540 }
4541 bool OnePastTheEnd = consumeIf('p');
4542 if (!consumeIf('E'))
4543 return nullptr;
4544 return make<SubobjectExpr>(
4545 Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
4546}
4547
Richard Smithc20d1442018-08-20 20:14:49 +00004548// <expression> ::= <unary operator-name> <expression>
4549// ::= <binary operator-name> <expression> <expression>
4550// ::= <ternary operator-name> <expression> <expression> <expression>
4551// ::= cl <expression>+ E # call
4552// ::= cv <type> <expression> # conversion with one argument
4553// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4554// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4555// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4556// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4557// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4558// ::= [gs] dl <expression> # delete expression
4559// ::= [gs] da <expression> # delete[] expression
4560// ::= pp_ <expression> # prefix ++
4561// ::= mm_ <expression> # prefix --
4562// ::= ti <type> # typeid (type)
4563// ::= te <expression> # typeid (expression)
4564// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4565// ::= sc <type> <expression> # static_cast<type> (expression)
4566// ::= cc <type> <expression> # const_cast<type> (expression)
4567// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4568// ::= st <type> # sizeof (a type)
4569// ::= sz <expression> # sizeof (an expression)
4570// ::= at <type> # alignof (a type)
4571// ::= az <expression> # alignof (an expression)
4572// ::= nx <expression> # noexcept (expression)
4573// ::= <template-param>
4574// ::= <function-param>
4575// ::= dt <expression> <unresolved-name> # expr.name
4576// ::= pt <expression> <unresolved-name> # expr->name
4577// ::= ds <expression> <expression> # expr.*expr
4578// ::= sZ <template-param> # size of a parameter pack
4579// ::= sZ <function-param> # size of a function parameter pack
4580// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4581// ::= sp <expression> # pack expansion
4582// ::= tw <expression> # throw expression
4583// ::= tr # throw with no operand (rethrow)
4584// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4585// # freestanding dependent name (e.g., T::x),
4586// # objectless nonstatic member reference
4587// ::= fL <binary-operator-name> <expression> <expression>
4588// ::= fR <binary-operator-name> <expression> <expression>
4589// ::= fl <binary-operator-name> <expression>
4590// ::= fr <binary-operator-name> <expression>
4591// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004592template <typename Derived, typename Alloc>
4593Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004594 bool Global = consumeIf("gs");
4595 if (numLeft() < 2)
4596 return nullptr;
4597
4598 switch (*First) {
4599 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004600 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004601 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004602 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004603 case 'f': {
4604 // Disambiguate a fold expression from a <function-param>.
4605 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004606 return getDerived().parseFunctionParam();
4607 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004608 }
4609 case 'a':
4610 switch (First[1]) {
4611 case 'a':
4612 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004613 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004614 case 'd':
4615 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004616 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004617 case 'n':
4618 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004619 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004620 case 'N':
4621 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004622 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004623 case 'S':
4624 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004625 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004626 case 't': {
4627 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004628 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004629 if (Ty == nullptr)
4630 return nullptr;
4631 return make<EnclosingExpr>("alignof (", Ty, ")");
4632 }
4633 case 'z': {
4634 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004635 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004636 if (Ty == nullptr)
4637 return nullptr;
4638 return make<EnclosingExpr>("alignof (", Ty, ")");
4639 }
4640 }
4641 return nullptr;
4642 case 'c':
4643 switch (First[1]) {
4644 // cc <type> <expression> # const_cast<type>(expression)
4645 case 'c': {
4646 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004647 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004648 if (Ty == nullptr)
4649 return Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004650 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004651 if (Ex == nullptr)
4652 return Ex;
4653 return make<CastExpr>("const_cast", Ty, Ex);
4654 }
4655 // cl <expression>+ E # call
4656 case 'l': {
4657 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004658 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004659 if (Callee == nullptr)
4660 return Callee;
4661 size_t ExprsBegin = Names.size();
4662 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004663 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004664 if (E == nullptr)
4665 return E;
4666 Names.push_back(E);
4667 }
4668 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4669 }
4670 case 'm':
4671 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004672 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004673 case 'o':
4674 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004675 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004676 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004677 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004678 }
4679 return nullptr;
4680 case 'd':
4681 switch (First[1]) {
4682 case 'a': {
4683 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004684 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004685 if (Ex == nullptr)
4686 return Ex;
4687 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4688 }
4689 case 'c': {
4690 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004691 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004692 if (T == nullptr)
4693 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004694 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004695 if (Ex == nullptr)
4696 return Ex;
4697 return make<CastExpr>("dynamic_cast", T, Ex);
4698 }
4699 case 'e':
4700 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004701 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004702 case 'l': {
4703 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004704 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004705 if (E == nullptr)
4706 return E;
4707 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4708 }
4709 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004710 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004711 case 's': {
4712 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004713 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004714 if (LHS == nullptr)
4715 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004716 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004717 if (RHS == nullptr)
4718 return nullptr;
4719 return make<MemberExpr>(LHS, ".*", RHS);
4720 }
4721 case 't': {
4722 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004723 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004724 if (LHS == nullptr)
4725 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004726 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004727 if (RHS == nullptr)
4728 return nullptr;
4729 return make<MemberExpr>(LHS, ".", RHS);
4730 }
4731 case 'v':
4732 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004733 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004734 case 'V':
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 'e':
4740 switch (First[1]) {
4741 case 'o':
4742 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004743 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004744 case 'O':
4745 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004746 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004747 case 'q':
4748 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004749 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004750 }
4751 return nullptr;
4752 case 'g':
4753 switch (First[1]) {
4754 case 'e':
4755 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004756 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004757 case 't':
4758 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004759 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004760 }
4761 return nullptr;
4762 case 'i':
4763 switch (First[1]) {
4764 case 'x': {
4765 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004766 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004767 if (Base == nullptr)
4768 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004769 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004770 if (Index == nullptr)
4771 return Index;
4772 return make<ArraySubscriptExpr>(Base, Index);
4773 }
4774 case 'l': {
4775 First += 2;
4776 size_t InitsBegin = Names.size();
4777 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004778 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004779 if (E == nullptr)
4780 return nullptr;
4781 Names.push_back(E);
4782 }
4783 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4784 }
4785 }
4786 return nullptr;
4787 case 'l':
4788 switch (First[1]) {
4789 case 'e':
4790 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004791 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004792 case 's':
4793 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004794 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004795 case 'S':
4796 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004797 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004798 case 't':
4799 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004800 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004801 }
4802 return nullptr;
4803 case 'm':
4804 switch (First[1]) {
Richard Smith1865d2f2020-10-22 19:29:36 -07004805 case 'c':
4806 First += 2;
4807 return parsePointerToMemberConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004808 case 'i':
4809 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004810 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004811 case 'I':
4812 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004813 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004814 case 'l':
4815 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004816 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004817 case 'L':
4818 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004819 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004820 case 'm':
4821 First += 2;
4822 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004823 return getDerived().parsePrefixExpr("--");
4824 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004825 if (Ex == nullptr)
4826 return nullptr;
4827 return make<PostfixExpr>(Ex, "--");
4828 }
4829 return nullptr;
4830 case 'n':
4831 switch (First[1]) {
4832 case 'a':
4833 case 'w':
Pavel Labathba825192018-10-16 14:29:14 +00004834 return getDerived().parseNewExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004835 case 'e':
4836 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004837 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004838 case 'g':
4839 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004840 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004841 case 't':
4842 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004843 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004844 case 'x':
4845 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004846 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004847 if (Ex == nullptr)
4848 return Ex;
4849 return make<EnclosingExpr>("noexcept (", Ex, ")");
4850 }
4851 return nullptr;
4852 case 'o':
4853 switch (First[1]) {
4854 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004855 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004856 case 'o':
4857 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004858 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004859 case 'r':
4860 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004861 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004862 case 'R':
4863 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004864 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004865 }
4866 return nullptr;
4867 case 'p':
4868 switch (First[1]) {
4869 case 'm':
4870 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004871 return getDerived().parseBinaryExpr("->*");
Richard Smithc20d1442018-08-20 20:14:49 +00004872 case 'l':
4873 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004874 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004875 case 'L':
4876 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004877 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004878 case 'p': {
4879 First += 2;
4880 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004881 return getDerived().parsePrefixExpr("++");
4882 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004883 if (Ex == nullptr)
4884 return Ex;
4885 return make<PostfixExpr>(Ex, "++");
4886 }
4887 case 's':
4888 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004889 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004890 case 't': {
4891 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004892 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004893 if (L == nullptr)
4894 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004895 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004896 if (R == nullptr)
4897 return nullptr;
4898 return make<MemberExpr>(L, "->", R);
4899 }
4900 }
4901 return nullptr;
4902 case 'q':
4903 if (First[1] == 'u') {
4904 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004905 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004906 if (Cond == nullptr)
4907 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004908 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004909 if (LHS == nullptr)
4910 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004911 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004912 if (RHS == nullptr)
4913 return nullptr;
4914 return make<ConditionalExpr>(Cond, LHS, RHS);
4915 }
4916 return nullptr;
4917 case 'r':
4918 switch (First[1]) {
4919 case 'c': {
4920 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004921 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004922 if (T == nullptr)
4923 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004924 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004925 if (Ex == nullptr)
4926 return Ex;
4927 return make<CastExpr>("reinterpret_cast", T, Ex);
4928 }
4929 case 'm':
4930 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004931 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004932 case 'M':
4933 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004934 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004935 case 's':
4936 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004937 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004938 case 'S':
4939 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004940 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004941 }
4942 return nullptr;
4943 case 's':
4944 switch (First[1]) {
4945 case 'c': {
4946 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004947 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004948 if (T == nullptr)
4949 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004950 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004951 if (Ex == nullptr)
4952 return Ex;
4953 return make<CastExpr>("static_cast", T, Ex);
4954 }
Richard Smith1865d2f2020-10-22 19:29:36 -07004955 case 'o':
4956 First += 2;
4957 return parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004958 case 'p': {
4959 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004960 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004961 if (Child == nullptr)
4962 return nullptr;
4963 return make<ParameterPackExpansion>(Child);
4964 }
4965 case 'r':
Pavel Labathba825192018-10-16 14:29:14 +00004966 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004967 case 't': {
4968 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004969 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004970 if (Ty == nullptr)
4971 return Ty;
4972 return make<EnclosingExpr>("sizeof (", Ty, ")");
4973 }
4974 case 'z': {
4975 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004976 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004977 if (Ex == nullptr)
4978 return Ex;
4979 return make<EnclosingExpr>("sizeof (", Ex, ")");
4980 }
4981 case 'Z':
4982 First += 2;
4983 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004984 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004985 if (R == nullptr)
4986 return nullptr;
4987 return make<SizeofParamPackExpr>(R);
4988 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004989 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004990 if (FP == nullptr)
4991 return nullptr;
4992 return make<EnclosingExpr>("sizeof... (", FP, ")");
4993 }
4994 return nullptr;
4995 case 'P': {
4996 First += 2;
4997 size_t ArgsBegin = Names.size();
4998 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004999 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005000 if (Arg == nullptr)
5001 return nullptr;
5002 Names.push_back(Arg);
5003 }
Richard Smithb485b352018-08-24 23:30:26 +00005004 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
5005 if (!Pack)
5006 return nullptr;
5007 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00005008 }
5009 }
5010 return nullptr;
5011 case 't':
5012 switch (First[1]) {
5013 case 'e': {
5014 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005015 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005016 if (Ex == nullptr)
5017 return Ex;
5018 return make<EnclosingExpr>("typeid (", Ex, ")");
5019 }
5020 case 'i': {
5021 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005022 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005023 if (Ty == nullptr)
5024 return Ty;
5025 return make<EnclosingExpr>("typeid (", Ty, ")");
5026 }
5027 case 'l': {
5028 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005029 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005030 if (Ty == nullptr)
5031 return nullptr;
5032 size_t InitsBegin = Names.size();
5033 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005034 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005035 if (E == nullptr)
5036 return nullptr;
5037 Names.push_back(E);
5038 }
5039 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
5040 }
5041 case 'r':
5042 First += 2;
5043 return make<NameType>("throw");
5044 case 'w': {
5045 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005046 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005047 if (Ex == nullptr)
5048 return nullptr;
5049 return make<ThrowExpr>(Ex);
5050 }
5051 }
5052 return nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05005053 case 'u': {
5054 ++First;
5055 Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
5056 if (!Name)
5057 return nullptr;
5058 // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
5059 // standard encoding expects a <template-arg>, and would be otherwise be
5060 // interpreted as <type> node 'short' or 'ellipsis'. However, neither
5061 // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
5062 // actual conflict here.
5063 if (Name->getBaseName() == "__uuidof") {
5064 if (numLeft() < 2)
5065 return nullptr;
5066 if (*First == 't') {
5067 ++First;
5068 Node *Ty = getDerived().parseType();
5069 if (!Ty)
5070 return nullptr;
5071 return make<CallExpr>(Name, makeNodeArray(&Ty, &Ty + 1));
5072 }
5073 if (*First == 'z') {
5074 ++First;
5075 Node *Ex = getDerived().parseExpr();
5076 if (!Ex)
5077 return nullptr;
5078 return make<CallExpr>(Name, makeNodeArray(&Ex, &Ex + 1));
5079 }
5080 }
5081 size_t ExprsBegin = Names.size();
5082 while (!consumeIf('E')) {
5083 Node *E = getDerived().parseTemplateArg();
5084 if (E == nullptr)
5085 return E;
5086 Names.push_back(E);
5087 }
5088 return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin));
5089 }
Richard Smithc20d1442018-08-20 20:14:49 +00005090 case '1':
5091 case '2':
5092 case '3':
5093 case '4':
5094 case '5':
5095 case '6':
5096 case '7':
5097 case '8':
5098 case '9':
Pavel Labathba825192018-10-16 14:29:14 +00005099 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00005100 }
5101 return nullptr;
5102}
5103
5104// <call-offset> ::= h <nv-offset> _
5105// ::= v <v-offset> _
5106//
5107// <nv-offset> ::= <offset number>
5108// # non-virtual base override
5109//
5110// <v-offset> ::= <offset number> _ <virtual offset number>
5111// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00005112template <typename Alloc, typename Derived>
5113bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00005114 // Just scan through the call offset, we never add this information into the
5115 // output.
5116 if (consumeIf('h'))
5117 return parseNumber(true).empty() || !consumeIf('_');
5118 if (consumeIf('v'))
5119 return parseNumber(true).empty() || !consumeIf('_') ||
5120 parseNumber(true).empty() || !consumeIf('_');
5121 return true;
5122}
5123
5124// <special-name> ::= TV <type> # virtual table
5125// ::= TT <type> # VTT structure (construction vtable index)
5126// ::= TI <type> # typeinfo structure
5127// ::= TS <type> # typeinfo name (null-terminated byte string)
5128// ::= Tc <call-offset> <call-offset> <base encoding>
5129// # base is the nominal target function of thunk
5130// # first call-offset is 'this' adjustment
5131// # second call-offset is result adjustment
5132// ::= T <call-offset> <base encoding>
5133// # base is the nominal target function of thunk
5134// ::= GV <object name> # Guard variable for one-time initialization
5135// # No <type>
5136// ::= TW <object name> # Thread-local wrapper
5137// ::= TH <object name> # Thread-local initialization
5138// ::= GR <object name> _ # First temporary
5139// ::= GR <object name> <seq-id> _ # Subsequent temporaries
5140// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
5141// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00005142template <typename Derived, typename Alloc>
5143Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00005144 switch (look()) {
5145 case 'T':
5146 switch (look(1)) {
Richard Smith1865d2f2020-10-22 19:29:36 -07005147 // TA <template-arg> # template parameter object
5148 //
5149 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
5150 case 'A': {
5151 First += 2;
5152 Node *Arg = getDerived().parseTemplateArg();
5153 if (Arg == nullptr)
5154 return nullptr;
5155 return make<SpecialName>("template parameter object for ", Arg);
5156 }
Richard Smithc20d1442018-08-20 20:14:49 +00005157 // TV <type> # virtual table
5158 case 'V': {
5159 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005160 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005161 if (Ty == nullptr)
5162 return nullptr;
5163 return make<SpecialName>("vtable for ", Ty);
5164 }
5165 // TT <type> # VTT structure (construction vtable index)
5166 case 'T': {
5167 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005168 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005169 if (Ty == nullptr)
5170 return nullptr;
5171 return make<SpecialName>("VTT for ", Ty);
5172 }
5173 // TI <type> # typeinfo structure
5174 case 'I': {
5175 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005176 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005177 if (Ty == nullptr)
5178 return nullptr;
5179 return make<SpecialName>("typeinfo for ", Ty);
5180 }
5181 // TS <type> # typeinfo name (null-terminated byte string)
5182 case 'S': {
5183 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005184 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005185 if (Ty == nullptr)
5186 return nullptr;
5187 return make<SpecialName>("typeinfo name for ", Ty);
5188 }
5189 // Tc <call-offset> <call-offset> <base encoding>
5190 case 'c': {
5191 First += 2;
5192 if (parseCallOffset() || parseCallOffset())
5193 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005194 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005195 if (Encoding == nullptr)
5196 return nullptr;
5197 return make<SpecialName>("covariant return thunk to ", Encoding);
5198 }
5199 // extension ::= TC <first type> <number> _ <second type>
5200 // # construction vtable for second-in-first
5201 case 'C': {
5202 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005203 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005204 if (FirstType == nullptr)
5205 return nullptr;
5206 if (parseNumber(true).empty() || !consumeIf('_'))
5207 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005208 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005209 if (SecondType == nullptr)
5210 return nullptr;
5211 return make<CtorVtableSpecialName>(SecondType, FirstType);
5212 }
5213 // TW <object name> # Thread-local wrapper
5214 case 'W': {
5215 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005216 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005217 if (Name == nullptr)
5218 return nullptr;
5219 return make<SpecialName>("thread-local wrapper routine for ", Name);
5220 }
5221 // TH <object name> # Thread-local initialization
5222 case 'H': {
5223 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005224 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005225 if (Name == nullptr)
5226 return nullptr;
5227 return make<SpecialName>("thread-local initialization routine for ", Name);
5228 }
5229 // T <call-offset> <base encoding>
5230 default: {
5231 ++First;
5232 bool IsVirt = look() == 'v';
5233 if (parseCallOffset())
5234 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005235 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005236 if (BaseEncoding == nullptr)
5237 return nullptr;
5238 if (IsVirt)
5239 return make<SpecialName>("virtual thunk to ", BaseEncoding);
5240 else
5241 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
5242 }
5243 }
5244 case 'G':
5245 switch (look(1)) {
5246 // GV <object name> # Guard variable for one-time initialization
5247 case 'V': {
5248 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005249 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005250 if (Name == nullptr)
5251 return nullptr;
5252 return make<SpecialName>("guard variable for ", Name);
5253 }
5254 // GR <object name> # reference temporary for object
5255 // GR <object name> _ # First temporary
5256 // GR <object name> <seq-id> _ # Subsequent temporaries
5257 case 'R': {
5258 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005259 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005260 if (Name == nullptr)
5261 return nullptr;
5262 size_t Count;
5263 bool ParsedSeqId = !parseSeqId(&Count);
5264 if (!consumeIf('_') && ParsedSeqId)
5265 return nullptr;
5266 return make<SpecialName>("reference temporary for ", Name);
5267 }
5268 }
5269 }
5270 return nullptr;
5271}
5272
5273// <encoding> ::= <function name> <bare-function-type>
5274// ::= <data name>
5275// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00005276template <typename Derived, typename Alloc>
5277Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithfac39712020-07-09 21:08:39 -07005278 // The template parameters of an encoding are unrelated to those of the
5279 // enclosing context.
5280 class SaveTemplateParams {
5281 AbstractManglingParser *Parser;
5282 decltype(TemplateParams) OldParams;
Justin Lebar2c536232021-06-09 16:57:22 -07005283 decltype(OuterTemplateParams) OldOuterParams;
Richard Smithfac39712020-07-09 21:08:39 -07005284
5285 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04005286 SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
Richard Smithfac39712020-07-09 21:08:39 -07005287 OldParams = std::move(Parser->TemplateParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005288 OldOuterParams = std::move(Parser->OuterTemplateParams);
Richard Smithfac39712020-07-09 21:08:39 -07005289 Parser->TemplateParams.clear();
Justin Lebar2c536232021-06-09 16:57:22 -07005290 Parser->OuterTemplateParams.clear();
Richard Smithfac39712020-07-09 21:08:39 -07005291 }
5292 ~SaveTemplateParams() {
5293 Parser->TemplateParams = std::move(OldParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005294 Parser->OuterTemplateParams = std::move(OldOuterParams);
Richard Smithfac39712020-07-09 21:08:39 -07005295 }
5296 } SaveTemplateParams(this);
Richard Smithfd434322020-07-09 20:36:04 -07005297
Richard Smithc20d1442018-08-20 20:14:49 +00005298 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00005299 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00005300
5301 auto IsEndOfEncoding = [&] {
5302 // The set of chars that can potentially follow an <encoding> (none of which
5303 // can start a <type>). Enumerating these allows us to avoid speculative
5304 // parsing.
5305 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5306 };
5307
5308 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00005309 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00005310 if (Name == nullptr)
5311 return nullptr;
5312
5313 if (resolveForwardTemplateRefs(NameInfo))
5314 return nullptr;
5315
5316 if (IsEndOfEncoding())
5317 return Name;
5318
5319 Node *Attrs = nullptr;
5320 if (consumeIf("Ua9enable_ifI")) {
5321 size_t BeforeArgs = Names.size();
5322 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005323 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005324 if (Arg == nullptr)
5325 return nullptr;
5326 Names.push_back(Arg);
5327 }
5328 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00005329 if (!Attrs)
5330 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005331 }
5332
5333 Node *ReturnType = nullptr;
5334 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00005335 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005336 if (ReturnType == nullptr)
5337 return nullptr;
5338 }
5339
5340 if (consumeIf('v'))
5341 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5342 Attrs, NameInfo.CVQualifiers,
5343 NameInfo.ReferenceQualifier);
5344
5345 size_t ParamsBegin = Names.size();
5346 do {
Pavel Labathba825192018-10-16 14:29:14 +00005347 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005348 if (Ty == nullptr)
5349 return nullptr;
5350 Names.push_back(Ty);
5351 } while (!IsEndOfEncoding());
5352
5353 return make<FunctionEncoding>(ReturnType, Name,
5354 popTrailingNodeArray(ParamsBegin),
5355 Attrs, NameInfo.CVQualifiers,
5356 NameInfo.ReferenceQualifier);
5357}
5358
5359template <class Float>
5360struct FloatData;
5361
5362template <>
5363struct FloatData<float>
5364{
5365 static const size_t mangled_size = 8;
5366 static const size_t max_demangled_size = 24;
5367 static constexpr const char* spec = "%af";
5368};
5369
5370template <>
5371struct FloatData<double>
5372{
5373 static const size_t mangled_size = 16;
5374 static const size_t max_demangled_size = 32;
5375 static constexpr const char* spec = "%a";
5376};
5377
5378template <>
5379struct FloatData<long double>
5380{
5381#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5382 defined(__wasm__)
5383 static const size_t mangled_size = 32;
5384#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5385 static const size_t mangled_size = 16;
5386#else
5387 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5388#endif
Elliott Hughes5a360ea2020-04-10 17:42:00 -07005389 // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
5390 // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
5391 // Negatives are one character longer than positives.
5392 // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
5393 // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
5394 static const size_t max_demangled_size = 42;
Richard Smithc20d1442018-08-20 20:14:49 +00005395 static constexpr const char *spec = "%LaL";
5396};
5397
Pavel Labathba825192018-10-16 14:29:14 +00005398template <typename Alloc, typename Derived>
5399template <class Float>
5400Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005401 const size_t N = FloatData<Float>::mangled_size;
5402 if (numLeft() <= N)
5403 return nullptr;
5404 StringView Data(First, First + N);
5405 for (char C : Data)
5406 if (!std::isxdigit(C))
5407 return nullptr;
5408 First += N;
5409 if (!consumeIf('E'))
5410 return nullptr;
5411 return make<FloatLiteralImpl<Float>>(Data);
5412}
5413
5414// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005415template <typename Alloc, typename Derived>
5416bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005417 if (!(look() >= '0' && look() <= '9') &&
5418 !(look() >= 'A' && look() <= 'Z'))
5419 return true;
5420
5421 size_t Id = 0;
5422 while (true) {
5423 if (look() >= '0' && look() <= '9') {
5424 Id *= 36;
5425 Id += static_cast<size_t>(look() - '0');
5426 } else if (look() >= 'A' && look() <= 'Z') {
5427 Id *= 36;
5428 Id += static_cast<size_t>(look() - 'A') + 10;
5429 } else {
5430 *Out = Id;
5431 return false;
5432 }
5433 ++First;
5434 }
5435}
5436
5437// <substitution> ::= S <seq-id> _
5438// ::= S_
5439// <substitution> ::= Sa # ::std::allocator
5440// <substitution> ::= Sb # ::std::basic_string
5441// <substitution> ::= Ss # ::std::basic_string < char,
5442// ::std::char_traits<char>,
5443// ::std::allocator<char> >
5444// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5445// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5446// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Pavel Labathba825192018-10-16 14:29:14 +00005447template <typename Derived, typename Alloc>
5448Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005449 if (!consumeIf('S'))
5450 return nullptr;
5451
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08005452 if (look() >= 'a' && look() <= 'z') {
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005453 SpecialSubKind Kind;
Richard Smithc20d1442018-08-20 20:14:49 +00005454 switch (look()) {
5455 case 'a':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005456 Kind = SpecialSubKind::allocator;
Richard Smithc20d1442018-08-20 20:14:49 +00005457 break;
5458 case 'b':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005459 Kind = SpecialSubKind::basic_string;
Richard Smithc20d1442018-08-20 20:14:49 +00005460 break;
5461 case 'd':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005462 Kind = SpecialSubKind::iostream;
5463 break;
5464 case 'i':
5465 Kind = SpecialSubKind::istream;
5466 break;
5467 case 'o':
5468 Kind = SpecialSubKind::ostream;
5469 break;
5470 case 's':
5471 Kind = SpecialSubKind::string;
Richard Smithc20d1442018-08-20 20:14:49 +00005472 break;
5473 default:
5474 return nullptr;
5475 }
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005476 ++First;
5477 auto *SpecialSub = make<SpecialSubstitution>(Kind);
Richard Smithb485b352018-08-24 23:30:26 +00005478 if (!SpecialSub)
5479 return nullptr;
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005480
Richard Smithc20d1442018-08-20 20:14:49 +00005481 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5482 // has ABI tags, the tags are appended to the substitution; the result is a
5483 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005484 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005485 if (WithTags != SpecialSub) {
5486 Subs.push_back(WithTags);
5487 SpecialSub = WithTags;
5488 }
5489 return SpecialSub;
5490 }
5491
5492 // ::= S_
5493 if (consumeIf('_')) {
5494 if (Subs.empty())
5495 return nullptr;
5496 return Subs[0];
5497 }
5498
5499 // ::= S <seq-id> _
5500 size_t Index = 0;
5501 if (parseSeqId(&Index))
5502 return nullptr;
5503 ++Index;
5504 if (!consumeIf('_') || Index >= Subs.size())
5505 return nullptr;
5506 return Subs[Index];
5507}
5508
5509// <template-param> ::= T_ # first template parameter
5510// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005511// ::= TL <level-1> __
5512// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005513template <typename Derived, typename Alloc>
5514Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005515 if (!consumeIf('T'))
5516 return nullptr;
5517
Richard Smithdf1c14c2019-09-06 23:53:21 +00005518 size_t Level = 0;
5519 if (consumeIf('L')) {
5520 if (parsePositiveInteger(&Level))
5521 return nullptr;
5522 ++Level;
5523 if (!consumeIf('_'))
5524 return nullptr;
5525 }
5526
Richard Smithc20d1442018-08-20 20:14:49 +00005527 size_t Index = 0;
5528 if (!consumeIf('_')) {
5529 if (parsePositiveInteger(&Index))
5530 return nullptr;
5531 ++Index;
5532 if (!consumeIf('_'))
5533 return nullptr;
5534 }
5535
Richard Smithc20d1442018-08-20 20:14:49 +00005536 // If we're in a context where this <template-param> refers to a
5537 // <template-arg> further ahead in the mangled name (currently just conversion
5538 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005539 // This can only happen at the outermost level.
5540 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005541 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5542 if (!ForwardRef)
5543 return nullptr;
5544 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5545 ForwardTemplateRefs.push_back(
5546 static_cast<ForwardTemplateReference *>(ForwardRef));
5547 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005548 }
5549
Richard Smithdf1c14c2019-09-06 23:53:21 +00005550 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5551 Index >= TemplateParams[Level]->size()) {
5552 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5553 // list are mangled as the corresponding artificial template type parameter.
5554 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5555 // This will be popped by the ScopedTemplateParamList in
5556 // parseUnnamedTypeName.
5557 if (Level == TemplateParams.size())
5558 TemplateParams.push_back(nullptr);
5559 return make<NameType>("auto");
5560 }
5561
Richard Smithc20d1442018-08-20 20:14:49 +00005562 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005563 }
5564
5565 return (*TemplateParams[Level])[Index];
5566}
5567
5568// <template-param-decl> ::= Ty # type parameter
5569// ::= Tn <type> # non-type parameter
5570// ::= Tt <template-param-decl>* E # template parameter
5571// ::= Tp <template-param-decl> # parameter pack
5572template <typename Derived, typename Alloc>
5573Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5574 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5575 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5576 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5577 if (N) TemplateParams.back()->push_back(N);
5578 return N;
5579 };
5580
5581 if (consumeIf("Ty")) {
5582 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5583 if (!Name)
5584 return nullptr;
5585 return make<TypeTemplateParamDecl>(Name);
5586 }
5587
5588 if (consumeIf("Tn")) {
5589 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5590 if (!Name)
5591 return nullptr;
5592 Node *Type = parseType();
5593 if (!Type)
5594 return nullptr;
5595 return make<NonTypeTemplateParamDecl>(Name, Type);
5596 }
5597
5598 if (consumeIf("Tt")) {
5599 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5600 if (!Name)
5601 return nullptr;
5602 size_t ParamsBegin = Names.size();
5603 ScopedTemplateParamList TemplateTemplateParamParams(this);
5604 while (!consumeIf("E")) {
5605 Node *P = parseTemplateParamDecl();
5606 if (!P)
5607 return nullptr;
5608 Names.push_back(P);
5609 }
5610 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5611 return make<TemplateTemplateParamDecl>(Name, Params);
5612 }
5613
5614 if (consumeIf("Tp")) {
5615 Node *P = parseTemplateParamDecl();
5616 if (!P)
5617 return nullptr;
5618 return make<TemplateParamPackDecl>(P);
5619 }
5620
5621 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005622}
5623
5624// <template-arg> ::= <type> # type or template
5625// ::= X <expression> E # expression
5626// ::= <expr-primary> # simple expressions
5627// ::= J <template-arg>* E # argument pack
5628// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005629template <typename Derived, typename Alloc>
5630Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005631 switch (look()) {
5632 case 'X': {
5633 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005634 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005635 if (Arg == nullptr || !consumeIf('E'))
5636 return nullptr;
5637 return Arg;
5638 }
5639 case 'J': {
5640 ++First;
5641 size_t ArgsBegin = Names.size();
5642 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005643 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005644 if (Arg == nullptr)
5645 return nullptr;
5646 Names.push_back(Arg);
5647 }
5648 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5649 return make<TemplateArgumentPack>(Args);
5650 }
5651 case 'L': {
5652 // ::= LZ <encoding> E # extension
5653 if (look(1) == 'Z') {
5654 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005655 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005656 if (Arg == nullptr || !consumeIf('E'))
5657 return nullptr;
5658 return Arg;
5659 }
5660 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005661 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005662 }
5663 default:
Pavel Labathba825192018-10-16 14:29:14 +00005664 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005665 }
5666}
5667
5668// <template-args> ::= I <template-arg>* E
5669// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005670template <typename Derived, typename Alloc>
5671Node *
5672AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005673 if (!consumeIf('I'))
5674 return nullptr;
5675
5676 // <template-params> refer to the innermost <template-args>. Clear out any
5677 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005678 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005679 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005680 TemplateParams.push_back(&OuterTemplateParams);
5681 OuterTemplateParams.clear();
5682 }
Richard Smithc20d1442018-08-20 20:14:49 +00005683
5684 size_t ArgsBegin = Names.size();
5685 while (!consumeIf('E')) {
5686 if (TagTemplates) {
5687 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005688 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005689 TemplateParams = std::move(OldParams);
5690 if (Arg == nullptr)
5691 return nullptr;
5692 Names.push_back(Arg);
5693 Node *TableEntry = Arg;
5694 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5695 TableEntry = make<ParameterPack>(
5696 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005697 if (!TableEntry)
5698 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005699 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005700 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005701 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005702 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005703 if (Arg == nullptr)
5704 return nullptr;
5705 Names.push_back(Arg);
5706 }
5707 }
5708 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5709}
5710
5711// <mangled-name> ::= _Z <encoding>
5712// ::= <type>
5713// extension ::= ___Z <encoding> _block_invoke
5714// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5715// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005716template <typename Derived, typename Alloc>
5717Node *AbstractManglingParser<Derived, Alloc>::parse() {
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)
5721 return nullptr;
5722 if (look() == '.') {
5723 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5724 First = Last;
5725 }
5726 if (numLeft() != 0)
5727 return nullptr;
5728 return Encoding;
5729 }
5730
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005731 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005732 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005733 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5734 return nullptr;
5735 bool RequireNumber = consumeIf('_');
5736 if (parseNumber().empty() && RequireNumber)
5737 return nullptr;
5738 if (look() == '.')
5739 First = Last;
5740 if (numLeft() != 0)
5741 return nullptr;
5742 return make<SpecialName>("invocation function for block in ", Encoding);
5743 }
5744
Pavel Labathba825192018-10-16 14:29:14 +00005745 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005746 if (numLeft() != 0)
5747 return nullptr;
5748 return Ty;
5749}
5750
Pavel Labathba825192018-10-16 14:29:14 +00005751template <typename Alloc>
5752struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5753 using AbstractManglingParser<ManglingParser<Alloc>,
5754 Alloc>::AbstractManglingParser;
5755};
5756
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005757DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005758
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005759#endif // DEMANGLE_ITANIUMDEMANGLE_H