blob: d0eaf6d6648042c7bdb5bc8081d69e798376fdbb [file] [log] [blame]
Richard Smithc20d1442018-08-20 20:14:49 +00001//===------------------------- ItaniumDemangle.h ----------------*- C++ -*-===//
2//
Chandler Carruth8ee27c32019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Richard Smithc20d1442018-08-20 20:14:49 +00006//
7//===----------------------------------------------------------------------===//
8//
Nathan Sidwell5b0a8cf2022-01-24 06:38:47 -08009// Generic itanium demangler library.
10// There are two copies of this file in the source tree. The one under
11// libcxxabi is the original and the one under llvm is the copy. Use
12// cp-to-llvm.sh to update the copy. See README.txt for more details.
Richard Smithc20d1442018-08-20 20:14:49 +000013//
14//===----------------------------------------------------------------------===//
15
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +000016#ifndef DEMANGLE_ITANIUMDEMANGLE_H
17#define DEMANGLE_ITANIUMDEMANGLE_H
Richard Smithc20d1442018-08-20 20:14:49 +000018
19// FIXME: (possibly) incomplete list of features that clang mangles that this
20// file does not yet support:
21// - C++ modules TS
22
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +000023#include "DemangleConfig.h"
Richard Smithc20d1442018-08-20 20:14:49 +000024#include "StringView.h"
25#include "Utility.h"
Nathan Sidwellbbc632f2022-01-24 04:11:59 -080026#include <algorithm>
Richard Smithc20d1442018-08-20 20:14:49 +000027#include <cassert>
28#include <cctype>
29#include <cstdio>
30#include <cstdlib>
31#include <cstring>
Nathan Sidwellbbc632f2022-01-24 04:11:59 -080032#include <limits>
Richard Smithc20d1442018-08-20 20:14:49 +000033#include <utility>
34
35#define FOR_EACH_NODE_KIND(X) \
36 X(NodeArrayNode) \
37 X(DotSuffix) \
38 X(VendorExtQualType) \
39 X(QualType) \
40 X(ConversionOperatorType) \
41 X(PostfixQualifiedType) \
42 X(ElaboratedTypeSpefType) \
43 X(NameType) \
44 X(AbiTagAttr) \
45 X(EnableIfAttr) \
46 X(ObjCProtoName) \
47 X(PointerType) \
48 X(ReferenceType) \
49 X(PointerToMemberType) \
50 X(ArrayType) \
51 X(FunctionType) \
52 X(NoexceptSpec) \
53 X(DynamicExceptionSpec) \
54 X(FunctionEncoding) \
55 X(LiteralOperator) \
56 X(SpecialName) \
57 X(CtorVtableSpecialName) \
58 X(QualifiedName) \
59 X(NestedName) \
60 X(LocalName) \
61 X(VectorType) \
62 X(PixelVectorType) \
Pengfei Wang50e90b82021-09-23 11:02:25 +080063 X(BinaryFPType) \
Richard Smithdf1c14c2019-09-06 23:53:21 +000064 X(SyntheticTemplateParamName) \
65 X(TypeTemplateParamDecl) \
66 X(NonTypeTemplateParamDecl) \
67 X(TemplateTemplateParamDecl) \
68 X(TemplateParamPackDecl) \
Richard Smithc20d1442018-08-20 20:14:49 +000069 X(ParameterPack) \
70 X(TemplateArgumentPack) \
71 X(ParameterPackExpansion) \
72 X(TemplateArgs) \
73 X(ForwardTemplateReference) \
74 X(NameWithTemplateArgs) \
75 X(GlobalQualifiedName) \
Richard Smithc20d1442018-08-20 20:14:49 +000076 X(ExpandedSpecialSubstitution) \
77 X(SpecialSubstitution) \
78 X(CtorDtorName) \
79 X(DtorName) \
80 X(UnnamedTypeName) \
81 X(ClosureTypeName) \
82 X(StructuredBindingName) \
83 X(BinaryExpr) \
84 X(ArraySubscriptExpr) \
85 X(PostfixExpr) \
86 X(ConditionalExpr) \
87 X(MemberExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070088 X(SubobjectExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000089 X(EnclosingExpr) \
90 X(CastExpr) \
91 X(SizeofParamPackExpr) \
92 X(CallExpr) \
93 X(NewExpr) \
94 X(DeleteExpr) \
95 X(PrefixExpr) \
96 X(FunctionParam) \
97 X(ConversionExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070098 X(PointerToMemberConversionExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000099 X(InitListExpr) \
100 X(FoldExpr) \
101 X(ThrowExpr) \
102 X(BoolExpr) \
Richard Smithdf1c14c2019-09-06 23:53:21 +0000103 X(StringLiteral) \
104 X(LambdaExpr) \
Erik Pilkington0a170f12020-05-13 14:13:37 -0400105 X(EnumLiteral) \
Richard Smithc20d1442018-08-20 20:14:49 +0000106 X(IntegerLiteral) \
107 X(FloatLiteral) \
108 X(DoubleLiteral) \
109 X(LongDoubleLiteral) \
110 X(BracedExpr) \
111 X(BracedRangeExpr)
112
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000113DEMANGLE_NAMESPACE_BEGIN
114
Mikhail Borisov8452f062021-08-17 18:06:53 -0400115template <class T, size_t N> class PODSmallVector {
116 static_assert(std::is_pod<T>::value,
117 "T is required to be a plain old data type");
118
119 T *First = nullptr;
120 T *Last = nullptr;
121 T *Cap = nullptr;
122 T Inline[N] = {0};
123
124 bool isInline() const { return First == Inline; }
125
126 void clearInline() {
127 First = Inline;
128 Last = Inline;
129 Cap = Inline + N;
130 }
131
132 void reserve(size_t NewCap) {
133 size_t S = size();
134 if (isInline()) {
135 auto *Tmp = static_cast<T *>(std::malloc(NewCap * sizeof(T)));
136 if (Tmp == nullptr)
137 std::terminate();
138 std::copy(First, Last, Tmp);
139 First = Tmp;
140 } else {
141 First = static_cast<T *>(std::realloc(First, NewCap * sizeof(T)));
142 if (First == nullptr)
143 std::terminate();
144 }
145 Last = First + S;
146 Cap = First + NewCap;
147 }
148
149public:
150 PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
151
152 PODSmallVector(const PODSmallVector &) = delete;
153 PODSmallVector &operator=(const PODSmallVector &) = delete;
154
155 PODSmallVector(PODSmallVector &&Other) : PODSmallVector() {
156 if (Other.isInline()) {
157 std::copy(Other.begin(), Other.end(), First);
158 Last = First + Other.size();
159 Other.clear();
160 return;
161 }
162
163 First = Other.First;
164 Last = Other.Last;
165 Cap = Other.Cap;
166 Other.clearInline();
167 }
168
169 PODSmallVector &operator=(PODSmallVector &&Other) {
170 if (Other.isInline()) {
171 if (!isInline()) {
172 std::free(First);
173 clearInline();
174 }
175 std::copy(Other.begin(), Other.end(), First);
176 Last = First + Other.size();
177 Other.clear();
178 return *this;
179 }
180
181 if (isInline()) {
182 First = Other.First;
183 Last = Other.Last;
184 Cap = Other.Cap;
185 Other.clearInline();
186 return *this;
187 }
188
189 std::swap(First, Other.First);
190 std::swap(Last, Other.Last);
191 std::swap(Cap, Other.Cap);
192 Other.clear();
193 return *this;
194 }
195
196 // NOLINTNEXTLINE(readability-identifier-naming)
197 void push_back(const T &Elem) {
198 if (Last == Cap)
199 reserve(size() * 2);
200 *Last++ = Elem;
201 }
202
203 // NOLINTNEXTLINE(readability-identifier-naming)
204 void pop_back() {
205 assert(Last != First && "Popping empty vector!");
206 --Last;
207 }
208
209 void dropBack(size_t Index) {
210 assert(Index <= size() && "dropBack() can't expand!");
211 Last = First + Index;
212 }
213
214 T *begin() { return First; }
215 T *end() { return Last; }
216
217 bool empty() const { return First == Last; }
218 size_t size() const { return static_cast<size_t>(Last - First); }
219 T &back() {
220 assert(Last != First && "Calling back() on empty vector!");
221 return *(Last - 1);
222 }
223 T &operator[](size_t Index) {
224 assert(Index < size() && "Invalid access!");
225 return *(begin() + Index);
226 }
227 void clear() { Last = First; }
228
229 ~PODSmallVector() {
230 if (!isInline())
231 std::free(First);
232 }
233};
234
Richard Smithc20d1442018-08-20 20:14:49 +0000235// Base class of all AST nodes. The AST is built by the parser, then is
236// traversed by the printLeft/Right functions to produce a demangled string.
237class Node {
238public:
239 enum Kind : unsigned char {
240#define ENUMERATOR(NodeKind) K ## NodeKind,
241 FOR_EACH_NODE_KIND(ENUMERATOR)
242#undef ENUMERATOR
243 };
244
245 /// Three-way bool to track a cached value. Unknown is possible if this node
246 /// has an unexpanded parameter pack below it that may affect this cache.
247 enum class Cache : unsigned char { Yes, No, Unknown, };
248
249private:
250 Kind K;
251
252 // FIXME: Make these protected.
253public:
254 /// Tracks if this node has a component on its right side, in which case we
255 /// need to call printRight.
256 Cache RHSComponentCache;
257
258 /// Track if this node is a (possibly qualified) array type. This can affect
259 /// how we format the output string.
260 Cache ArrayCache;
261
262 /// Track if this node is a (possibly qualified) function type. This can
263 /// affect how we format the output string.
264 Cache FunctionCache;
265
266public:
267 Node(Kind K_, Cache RHSComponentCache_ = Cache::No,
268 Cache ArrayCache_ = Cache::No, Cache FunctionCache_ = Cache::No)
269 : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_),
270 FunctionCache(FunctionCache_) {}
271
272 /// Visit the most-derived object corresponding to this object.
273 template<typename Fn> void visit(Fn F) const;
274
275 // The following function is provided by all derived classes:
276 //
277 // Call F with arguments that, when passed to the constructor of this node,
278 // would construct an equivalent node.
279 //template<typename Fn> void match(Fn F) const;
280
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700281 bool hasRHSComponent(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000282 if (RHSComponentCache != Cache::Unknown)
283 return RHSComponentCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700284 return hasRHSComponentSlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000285 }
286
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700287 bool hasArray(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000288 if (ArrayCache != Cache::Unknown)
289 return ArrayCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700290 return hasArraySlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000291 }
292
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700293 bool hasFunction(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000294 if (FunctionCache != Cache::Unknown)
295 return FunctionCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700296 return hasFunctionSlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000297 }
298
299 Kind getKind() const { return K; }
300
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700301 virtual bool hasRHSComponentSlow(OutputBuffer &) const { return false; }
302 virtual bool hasArraySlow(OutputBuffer &) const { return false; }
303 virtual bool hasFunctionSlow(OutputBuffer &) const { return false; }
Richard Smithc20d1442018-08-20 20:14:49 +0000304
305 // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
306 // get at a node that actually represents some concrete syntax.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700307 virtual const Node *getSyntaxNode(OutputBuffer &) const { return this; }
Richard Smithc20d1442018-08-20 20:14:49 +0000308
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700309 void print(OutputBuffer &OB) const {
310 printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000311 if (RHSComponentCache != Cache::No)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700312 printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000313 }
314
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -0800315 // Print the "left" side of this Node into OutputBuffer.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700316 virtual void printLeft(OutputBuffer &) const = 0;
Richard Smithc20d1442018-08-20 20:14:49 +0000317
318 // Print the "right". This distinction is necessary to represent C++ types
319 // that appear on the RHS of their subtype, such as arrays or functions.
320 // Since most types don't have such a component, provide a default
321 // implementation.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700322 virtual void printRight(OutputBuffer &) const {}
Richard Smithc20d1442018-08-20 20:14:49 +0000323
324 virtual StringView getBaseName() const { return StringView(); }
325
326 // Silence compiler warnings, this dtor will never be called.
327 virtual ~Node() = default;
328
329#ifndef NDEBUG
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000330 DEMANGLE_DUMP_METHOD void dump() const;
Richard Smithc20d1442018-08-20 20:14:49 +0000331#endif
332};
333
334class NodeArray {
335 Node **Elements;
336 size_t NumElements;
337
338public:
339 NodeArray() : Elements(nullptr), NumElements(0) {}
340 NodeArray(Node **Elements_, size_t NumElements_)
341 : Elements(Elements_), NumElements(NumElements_) {}
342
343 bool empty() const { return NumElements == 0; }
344 size_t size() const { return NumElements; }
345
346 Node **begin() const { return Elements; }
347 Node **end() const { return Elements + NumElements; }
348
349 Node *operator[](size_t Idx) const { return Elements[Idx]; }
350
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700351 void printWithComma(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000352 bool FirstElement = true;
353 for (size_t Idx = 0; Idx != NumElements; ++Idx) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700354 size_t BeforeComma = OB.getCurrentPosition();
Richard Smithc20d1442018-08-20 20:14:49 +0000355 if (!FirstElement)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700356 OB += ", ";
357 size_t AfterComma = OB.getCurrentPosition();
358 Elements[Idx]->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000359
360 // Elements[Idx] is an empty parameter pack expansion, we should erase the
361 // comma we just printed.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700362 if (AfterComma == OB.getCurrentPosition()) {
363 OB.setCurrentPosition(BeforeComma);
Richard Smithc20d1442018-08-20 20:14:49 +0000364 continue;
365 }
366
367 FirstElement = false;
368 }
369 }
370};
371
372struct NodeArrayNode : Node {
373 NodeArray Array;
374 NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
375
376 template<typename Fn> void match(Fn F) const { F(Array); }
377
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700378 void printLeft(OutputBuffer &OB) const override { Array.printWithComma(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000379};
380
381class DotSuffix final : public Node {
382 const Node *Prefix;
383 const StringView Suffix;
384
385public:
386 DotSuffix(const Node *Prefix_, StringView Suffix_)
387 : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
388
389 template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
390
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700391 void printLeft(OutputBuffer &OB) const override {
392 Prefix->print(OB);
393 OB += " (";
394 OB += Suffix;
395 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000396 }
397};
398
399class VendorExtQualType final : public Node {
400 const Node *Ty;
401 StringView Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400402 const Node *TA;
Richard Smithc20d1442018-08-20 20:14:49 +0000403
404public:
Alex Orlovf50df922021-03-24 10:21:32 +0400405 VendorExtQualType(const Node *Ty_, StringView Ext_, const Node *TA_)
406 : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_), TA(TA_) {}
Richard Smithc20d1442018-08-20 20:14:49 +0000407
Alex Orlovf50df922021-03-24 10:21:32 +0400408 template <typename Fn> void match(Fn F) const { F(Ty, Ext, TA); }
Richard Smithc20d1442018-08-20 20:14:49 +0000409
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700410 void printLeft(OutputBuffer &OB) const override {
411 Ty->print(OB);
412 OB += " ";
413 OB += Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400414 if (TA != nullptr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700415 TA->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000416 }
417};
418
419enum FunctionRefQual : unsigned char {
420 FrefQualNone,
421 FrefQualLValue,
422 FrefQualRValue,
423};
424
425enum Qualifiers {
426 QualNone = 0,
427 QualConst = 0x1,
428 QualVolatile = 0x2,
429 QualRestrict = 0x4,
430};
431
432inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
433 return Q1 = static_cast<Qualifiers>(Q1 | Q2);
434}
435
Richard Smithdf1c14c2019-09-06 23:53:21 +0000436class QualType final : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +0000437protected:
438 const Qualifiers Quals;
439 const Node *Child;
440
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700441 void printQuals(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000442 if (Quals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700443 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000444 if (Quals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700445 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000446 if (Quals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700447 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000448 }
449
450public:
451 QualType(const Node *Child_, Qualifiers Quals_)
452 : Node(KQualType, Child_->RHSComponentCache,
453 Child_->ArrayCache, Child_->FunctionCache),
454 Quals(Quals_), Child(Child_) {}
455
456 template<typename Fn> void match(Fn F) const { F(Child, Quals); }
457
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700458 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
459 return Child->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000460 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700461 bool hasArraySlow(OutputBuffer &OB) const override {
462 return Child->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000463 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700464 bool hasFunctionSlow(OutputBuffer &OB) const override {
465 return Child->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000466 }
467
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700468 void printLeft(OutputBuffer &OB) const override {
469 Child->printLeft(OB);
470 printQuals(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000471 }
472
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700473 void printRight(OutputBuffer &OB) const override { Child->printRight(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000474};
475
476class ConversionOperatorType final : public Node {
477 const Node *Ty;
478
479public:
480 ConversionOperatorType(const Node *Ty_)
481 : Node(KConversionOperatorType), Ty(Ty_) {}
482
483 template<typename Fn> void match(Fn F) const { F(Ty); }
484
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700485 void printLeft(OutputBuffer &OB) const override {
486 OB += "operator ";
487 Ty->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000488 }
489};
490
491class PostfixQualifiedType final : public Node {
492 const Node *Ty;
493 const StringView Postfix;
494
495public:
496 PostfixQualifiedType(Node *Ty_, StringView Postfix_)
497 : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
498
499 template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
500
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700501 void printLeft(OutputBuffer &OB) const override {
502 Ty->printLeft(OB);
503 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +0000504 }
505};
506
507class NameType final : public Node {
508 const StringView Name;
509
510public:
511 NameType(StringView Name_) : Node(KNameType), Name(Name_) {}
512
513 template<typename Fn> void match(Fn F) const { F(Name); }
514
515 StringView getName() const { return Name; }
516 StringView getBaseName() const override { return Name; }
517
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700518 void printLeft(OutputBuffer &OB) const override { OB += Name; }
Richard Smithc20d1442018-08-20 20:14:49 +0000519};
520
521class ElaboratedTypeSpefType : public Node {
522 StringView Kind;
523 Node *Child;
524public:
525 ElaboratedTypeSpefType(StringView Kind_, Node *Child_)
526 : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
527
528 template<typename Fn> void match(Fn F) const { F(Kind, Child); }
529
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700530 void printLeft(OutputBuffer &OB) const override {
531 OB += Kind;
532 OB += ' ';
533 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000534 }
535};
536
537struct AbiTagAttr : Node {
538 Node *Base;
539 StringView Tag;
540
541 AbiTagAttr(Node* Base_, StringView Tag_)
542 : Node(KAbiTagAttr, Base_->RHSComponentCache,
543 Base_->ArrayCache, Base_->FunctionCache),
544 Base(Base_), Tag(Tag_) {}
545
546 template<typename Fn> void match(Fn F) const { F(Base, Tag); }
547
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700548 void printLeft(OutputBuffer &OB) const override {
549 Base->printLeft(OB);
550 OB += "[abi:";
551 OB += Tag;
552 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +0000553 }
554};
555
556class EnableIfAttr : public Node {
557 NodeArray Conditions;
558public:
559 EnableIfAttr(NodeArray Conditions_)
560 : Node(KEnableIfAttr), Conditions(Conditions_) {}
561
562 template<typename Fn> void match(Fn F) const { F(Conditions); }
563
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700564 void printLeft(OutputBuffer &OB) const override {
565 OB += " [enable_if:";
566 Conditions.printWithComma(OB);
567 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +0000568 }
569};
570
571class ObjCProtoName : public Node {
572 const Node *Ty;
573 StringView Protocol;
574
575 friend class PointerType;
576
577public:
578 ObjCProtoName(const Node *Ty_, StringView Protocol_)
579 : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
580
581 template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
582
583 bool isObjCObject() const {
584 return Ty->getKind() == KNameType &&
585 static_cast<const NameType *>(Ty)->getName() == "objc_object";
586 }
587
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700588 void printLeft(OutputBuffer &OB) const override {
589 Ty->print(OB);
590 OB += "<";
591 OB += Protocol;
592 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +0000593 }
594};
595
596class PointerType final : public Node {
597 const Node *Pointee;
598
599public:
600 PointerType(const Node *Pointee_)
601 : Node(KPointerType, Pointee_->RHSComponentCache),
602 Pointee(Pointee_) {}
603
604 template<typename Fn> void match(Fn F) const { F(Pointee); }
605
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700606 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
607 return Pointee->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000608 }
609
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700610 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000611 // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
612 if (Pointee->getKind() != KObjCProtoName ||
613 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700614 Pointee->printLeft(OB);
615 if (Pointee->hasArray(OB))
616 OB += " ";
617 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
618 OB += "(";
619 OB += "*";
Richard Smithc20d1442018-08-20 20:14:49 +0000620 } else {
621 const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700622 OB += "id<";
623 OB += objcProto->Protocol;
624 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +0000625 }
626 }
627
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700628 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000629 if (Pointee->getKind() != KObjCProtoName ||
630 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700631 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
632 OB += ")";
633 Pointee->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000634 }
635 }
636};
637
638enum class ReferenceKind {
639 LValue,
640 RValue,
641};
642
643// Represents either a LValue or an RValue reference type.
644class ReferenceType : public Node {
645 const Node *Pointee;
646 ReferenceKind RK;
647
648 mutable bool Printing = false;
649
650 // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
651 // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
652 // other combination collapses to a lvalue ref.
Mikhail Borisov05f77222021-08-17 18:10:57 -0400653 //
654 // A combination of a TemplateForwardReference and a back-ref Substitution
655 // from an ill-formed string may have created a cycle; use cycle detection to
656 // avoid looping forever.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700657 std::pair<ReferenceKind, const Node *> collapse(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000658 auto SoFar = std::make_pair(RK, Pointee);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400659 // Track the chain of nodes for the Floyd's 'tortoise and hare'
660 // cycle-detection algorithm, since getSyntaxNode(S) is impure
661 PODSmallVector<const Node *, 8> Prev;
Richard Smithc20d1442018-08-20 20:14:49 +0000662 for (;;) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700663 const Node *SN = SoFar.second->getSyntaxNode(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000664 if (SN->getKind() != KReferenceType)
665 break;
666 auto *RT = static_cast<const ReferenceType *>(SN);
667 SoFar.second = RT->Pointee;
668 SoFar.first = std::min(SoFar.first, RT->RK);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400669
670 // The middle of Prev is the 'slow' pointer moving at half speed
671 Prev.push_back(SoFar.second);
672 if (Prev.size() > 1 && SoFar.second == Prev[(Prev.size() - 1) / 2]) {
673 // Cycle detected
674 SoFar.second = nullptr;
675 break;
676 }
Richard Smithc20d1442018-08-20 20:14:49 +0000677 }
678 return SoFar;
679 }
680
681public:
682 ReferenceType(const Node *Pointee_, ReferenceKind RK_)
683 : Node(KReferenceType, Pointee_->RHSComponentCache),
684 Pointee(Pointee_), RK(RK_) {}
685
686 template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
687
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700688 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
689 return Pointee->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000690 }
691
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700692 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000693 if (Printing)
694 return;
695 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700696 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400697 if (!Collapsed.second)
698 return;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700699 Collapsed.second->printLeft(OB);
700 if (Collapsed.second->hasArray(OB))
701 OB += " ";
702 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
703 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +0000704
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700705 OB += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
Richard Smithc20d1442018-08-20 20:14:49 +0000706 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700707 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000708 if (Printing)
709 return;
710 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700711 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400712 if (!Collapsed.second)
713 return;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700714 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
715 OB += ")";
716 Collapsed.second->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000717 }
718};
719
720class PointerToMemberType final : public Node {
721 const Node *ClassType;
722 const Node *MemberType;
723
724public:
725 PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
726 : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
727 ClassType(ClassType_), MemberType(MemberType_) {}
728
729 template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
730
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700731 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
732 return MemberType->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000733 }
734
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700735 void printLeft(OutputBuffer &OB) const override {
736 MemberType->printLeft(OB);
737 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
738 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +0000739 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700740 OB += " ";
741 ClassType->print(OB);
742 OB += "::*";
Richard Smithc20d1442018-08-20 20:14:49 +0000743 }
744
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700745 void printRight(OutputBuffer &OB) const override {
746 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
747 OB += ")";
748 MemberType->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000749 }
750};
751
Richard Smithc20d1442018-08-20 20:14:49 +0000752class ArrayType final : public Node {
753 const Node *Base;
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800754 Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +0000755
756public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800757 ArrayType(const Node *Base_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +0000758 : Node(KArrayType,
759 /*RHSComponentCache=*/Cache::Yes,
760 /*ArrayCache=*/Cache::Yes),
761 Base(Base_), Dimension(Dimension_) {}
762
763 template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
764
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700765 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
766 bool hasArraySlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000767
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700768 void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000769
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700770 void printRight(OutputBuffer &OB) const override {
771 if (OB.back() != ']')
772 OB += " ";
773 OB += "[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800774 if (Dimension)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700775 Dimension->print(OB);
776 OB += "]";
777 Base->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000778 }
779};
780
781class FunctionType final : public Node {
782 const Node *Ret;
783 NodeArray Params;
784 Qualifiers CVQuals;
785 FunctionRefQual RefQual;
786 const Node *ExceptionSpec;
787
788public:
789 FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
790 FunctionRefQual RefQual_, const Node *ExceptionSpec_)
791 : Node(KFunctionType,
792 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
793 /*FunctionCache=*/Cache::Yes),
794 Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
795 ExceptionSpec(ExceptionSpec_) {}
796
797 template<typename Fn> void match(Fn F) const {
798 F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
799 }
800
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700801 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
802 bool hasFunctionSlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000803
804 // Handle C++'s ... quirky decl grammar by using the left & right
805 // distinction. Consider:
806 // int (*f(float))(char) {}
807 // f is a function that takes a float and returns a pointer to a function
808 // that takes a char and returns an int. If we're trying to print f, start
809 // by printing out the return types's left, then print our parameters, then
810 // finally print right of the return type.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700811 void printLeft(OutputBuffer &OB) const override {
812 Ret->printLeft(OB);
813 OB += " ";
Richard Smithc20d1442018-08-20 20:14:49 +0000814 }
815
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700816 void printRight(OutputBuffer &OB) const override {
817 OB += "(";
818 Params.printWithComma(OB);
819 OB += ")";
820 Ret->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000821
822 if (CVQuals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700823 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000824 if (CVQuals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700825 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000826 if (CVQuals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700827 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000828
829 if (RefQual == FrefQualLValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700830 OB += " &";
Richard Smithc20d1442018-08-20 20:14:49 +0000831 else if (RefQual == FrefQualRValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700832 OB += " &&";
Richard Smithc20d1442018-08-20 20:14:49 +0000833
834 if (ExceptionSpec != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700835 OB += ' ';
836 ExceptionSpec->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000837 }
838 }
839};
840
841class NoexceptSpec : public Node {
842 const Node *E;
843public:
844 NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
845
846 template<typename Fn> void match(Fn F) const { F(E); }
847
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700848 void printLeft(OutputBuffer &OB) const override {
849 OB += "noexcept(";
850 E->print(OB);
851 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000852 }
853};
854
855class DynamicExceptionSpec : public Node {
856 NodeArray Types;
857public:
858 DynamicExceptionSpec(NodeArray Types_)
859 : Node(KDynamicExceptionSpec), Types(Types_) {}
860
861 template<typename Fn> void match(Fn F) const { F(Types); }
862
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700863 void printLeft(OutputBuffer &OB) const override {
864 OB += "throw(";
865 Types.printWithComma(OB);
866 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +0000867 }
868};
869
870class FunctionEncoding final : public Node {
871 const Node *Ret;
872 const Node *Name;
873 NodeArray Params;
874 const Node *Attrs;
875 Qualifiers CVQuals;
876 FunctionRefQual RefQual;
877
878public:
879 FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
880 const Node *Attrs_, Qualifiers CVQuals_,
881 FunctionRefQual RefQual_)
882 : Node(KFunctionEncoding,
883 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
884 /*FunctionCache=*/Cache::Yes),
885 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
886 CVQuals(CVQuals_), RefQual(RefQual_) {}
887
888 template<typename Fn> void match(Fn F) const {
889 F(Ret, Name, Params, Attrs, CVQuals, RefQual);
890 }
891
892 Qualifiers getCVQuals() const { return CVQuals; }
893 FunctionRefQual getRefQual() const { return RefQual; }
894 NodeArray getParams() const { return Params; }
895 const Node *getReturnType() const { return Ret; }
896
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700897 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
898 bool hasFunctionSlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000899
900 const Node *getName() const { return Name; }
901
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700902 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000903 if (Ret) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700904 Ret->printLeft(OB);
905 if (!Ret->hasRHSComponent(OB))
906 OB += " ";
Richard Smithc20d1442018-08-20 20:14:49 +0000907 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700908 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000909 }
910
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700911 void printRight(OutputBuffer &OB) const override {
912 OB += "(";
913 Params.printWithComma(OB);
914 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000915 if (Ret)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700916 Ret->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000917
918 if (CVQuals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700919 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000920 if (CVQuals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700921 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000922 if (CVQuals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700923 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000924
925 if (RefQual == FrefQualLValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700926 OB += " &";
Richard Smithc20d1442018-08-20 20:14:49 +0000927 else if (RefQual == FrefQualRValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700928 OB += " &&";
Richard Smithc20d1442018-08-20 20:14:49 +0000929
930 if (Attrs != nullptr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700931 Attrs->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000932 }
933};
934
935class LiteralOperator : public Node {
936 const Node *OpName;
937
938public:
939 LiteralOperator(const Node *OpName_)
940 : Node(KLiteralOperator), OpName(OpName_) {}
941
942 template<typename Fn> void match(Fn F) const { F(OpName); }
943
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700944 void printLeft(OutputBuffer &OB) const override {
945 OB += "operator\"\" ";
946 OpName->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000947 }
948};
949
950class SpecialName final : public Node {
951 const StringView Special;
952 const Node *Child;
953
954public:
955 SpecialName(StringView Special_, const Node *Child_)
956 : Node(KSpecialName), Special(Special_), Child(Child_) {}
957
958 template<typename Fn> void match(Fn F) const { F(Special, Child); }
959
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700960 void printLeft(OutputBuffer &OB) const override {
961 OB += Special;
962 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000963 }
964};
965
966class CtorVtableSpecialName final : public Node {
967 const Node *FirstType;
968 const Node *SecondType;
969
970public:
971 CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
972 : Node(KCtorVtableSpecialName),
973 FirstType(FirstType_), SecondType(SecondType_) {}
974
975 template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
976
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700977 void printLeft(OutputBuffer &OB) const override {
978 OB += "construction vtable for ";
979 FirstType->print(OB);
980 OB += "-in-";
981 SecondType->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000982 }
983};
984
985struct NestedName : Node {
986 Node *Qual;
987 Node *Name;
988
989 NestedName(Node *Qual_, Node *Name_)
990 : Node(KNestedName), Qual(Qual_), Name(Name_) {}
991
992 template<typename Fn> void match(Fn F) const { F(Qual, Name); }
993
994 StringView getBaseName() const override { return Name->getBaseName(); }
995
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700996 void printLeft(OutputBuffer &OB) const override {
997 Qual->print(OB);
998 OB += "::";
999 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001000 }
1001};
1002
1003struct LocalName : Node {
1004 Node *Encoding;
1005 Node *Entity;
1006
1007 LocalName(Node *Encoding_, Node *Entity_)
1008 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
1009
1010 template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
1011
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001012 void printLeft(OutputBuffer &OB) const override {
1013 Encoding->print(OB);
1014 OB += "::";
1015 Entity->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001016 }
1017};
1018
1019class QualifiedName final : public Node {
1020 // qualifier::name
1021 const Node *Qualifier;
1022 const Node *Name;
1023
1024public:
1025 QualifiedName(const Node *Qualifier_, const Node *Name_)
1026 : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
1027
1028 template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
1029
1030 StringView getBaseName() const override { return Name->getBaseName(); }
1031
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001032 void printLeft(OutputBuffer &OB) const override {
1033 Qualifier->print(OB);
1034 OB += "::";
1035 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001036 }
1037};
1038
1039class VectorType final : public Node {
1040 const Node *BaseType;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001041 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001042
1043public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001044 VectorType(const Node *BaseType_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001045 : Node(KVectorType), BaseType(BaseType_),
1046 Dimension(Dimension_) {}
1047
1048 template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
1049
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001050 void printLeft(OutputBuffer &OB) const override {
1051 BaseType->print(OB);
1052 OB += " vector[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001053 if (Dimension)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001054 Dimension->print(OB);
1055 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001056 }
1057};
1058
1059class PixelVectorType final : public Node {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001060 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001061
1062public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001063 PixelVectorType(const Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001064 : Node(KPixelVectorType), Dimension(Dimension_) {}
1065
1066 template<typename Fn> void match(Fn F) const { F(Dimension); }
1067
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001068 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001069 // FIXME: This should demangle as "vector pixel".
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001070 OB += "pixel vector[";
1071 Dimension->print(OB);
1072 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001073 }
1074};
1075
Pengfei Wang50e90b82021-09-23 11:02:25 +08001076class BinaryFPType final : public Node {
1077 const Node *Dimension;
1078
1079public:
1080 BinaryFPType(const Node *Dimension_)
1081 : Node(KBinaryFPType), Dimension(Dimension_) {}
1082
1083 template<typename Fn> void match(Fn F) const { F(Dimension); }
1084
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001085 void printLeft(OutputBuffer &OB) const override {
1086 OB += "_Float";
1087 Dimension->print(OB);
Pengfei Wang50e90b82021-09-23 11:02:25 +08001088 }
1089};
1090
Richard Smithdf1c14c2019-09-06 23:53:21 +00001091enum class TemplateParamKind { Type, NonType, Template };
1092
1093/// An invented name for a template parameter for which we don't have a
1094/// corresponding template argument.
1095///
1096/// This node is created when parsing the <lambda-sig> for a lambda with
1097/// explicit template arguments, which might be referenced in the parameter
1098/// types appearing later in the <lambda-sig>.
1099class SyntheticTemplateParamName final : public Node {
1100 TemplateParamKind Kind;
1101 unsigned Index;
1102
1103public:
1104 SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_)
1105 : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {}
1106
1107 template<typename Fn> void match(Fn F) const { F(Kind, Index); }
1108
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001109 void printLeft(OutputBuffer &OB) const override {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001110 switch (Kind) {
1111 case TemplateParamKind::Type:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001112 OB += "$T";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001113 break;
1114 case TemplateParamKind::NonType:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001115 OB += "$N";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001116 break;
1117 case TemplateParamKind::Template:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001118 OB += "$TT";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001119 break;
1120 }
1121 if (Index > 0)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001122 OB << Index - 1;
Richard Smithdf1c14c2019-09-06 23:53:21 +00001123 }
1124};
1125
1126/// A template type parameter declaration, 'typename T'.
1127class TypeTemplateParamDecl final : public Node {
1128 Node *Name;
1129
1130public:
1131 TypeTemplateParamDecl(Node *Name_)
1132 : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {}
1133
1134 template<typename Fn> void match(Fn F) const { F(Name); }
1135
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001136 void printLeft(OutputBuffer &OB) const override { OB += "typename "; }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001137
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001138 void printRight(OutputBuffer &OB) const override { Name->print(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001139};
1140
1141/// A non-type template parameter declaration, 'int N'.
1142class NonTypeTemplateParamDecl final : public Node {
1143 Node *Name;
1144 Node *Type;
1145
1146public:
1147 NonTypeTemplateParamDecl(Node *Name_, Node *Type_)
1148 : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {}
1149
1150 template<typename Fn> void match(Fn F) const { F(Name, Type); }
1151
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001152 void printLeft(OutputBuffer &OB) const override {
1153 Type->printLeft(OB);
1154 if (!Type->hasRHSComponent(OB))
1155 OB += " ";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001156 }
1157
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001158 void printRight(OutputBuffer &OB) const override {
1159 Name->print(OB);
1160 Type->printRight(OB);
Richard Smithdf1c14c2019-09-06 23:53:21 +00001161 }
1162};
1163
1164/// A template template parameter declaration,
1165/// 'template<typename T> typename N'.
1166class TemplateTemplateParamDecl final : public Node {
1167 Node *Name;
1168 NodeArray Params;
1169
1170public:
1171 TemplateTemplateParamDecl(Node *Name_, NodeArray Params_)
1172 : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_),
1173 Params(Params_) {}
1174
1175 template<typename Fn> void match(Fn F) const { F(Name, Params); }
1176
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001177 void printLeft(OutputBuffer &OB) const override {
1178 OB += "template<";
1179 Params.printWithComma(OB);
1180 OB += "> typename ";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001181 }
1182
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001183 void printRight(OutputBuffer &OB) const override { Name->print(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001184};
1185
1186/// A template parameter pack declaration, 'typename ...T'.
1187class TemplateParamPackDecl final : public Node {
1188 Node *Param;
1189
1190public:
1191 TemplateParamPackDecl(Node *Param_)
1192 : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {}
1193
1194 template<typename Fn> void match(Fn F) const { F(Param); }
1195
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001196 void printLeft(OutputBuffer &OB) const override {
1197 Param->printLeft(OB);
1198 OB += "...";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001199 }
1200
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001201 void printRight(OutputBuffer &OB) const override { Param->printRight(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001202};
1203
Richard Smithc20d1442018-08-20 20:14:49 +00001204/// An unexpanded parameter pack (either in the expression or type context). If
1205/// this AST is correct, this node will have a ParameterPackExpansion node above
1206/// it.
1207///
1208/// This node is created when some <template-args> are found that apply to an
1209/// <encoding>, and is stored in the TemplateParams table. In order for this to
1210/// appear in the final AST, it has to referenced via a <template-param> (ie,
1211/// T_).
1212class ParameterPack final : public Node {
1213 NodeArray Data;
1214
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08001215 // Setup OutputBuffer for a pack expansion, unless we're already expanding
1216 // one.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001217 void initializePackExpansion(OutputBuffer &OB) const {
1218 if (OB.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
1219 OB.CurrentPackMax = static_cast<unsigned>(Data.size());
1220 OB.CurrentPackIndex = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00001221 }
1222 }
1223
1224public:
1225 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
1226 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
1227 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1228 return P->ArrayCache == Cache::No;
1229 }))
1230 ArrayCache = Cache::No;
1231 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1232 return P->FunctionCache == Cache::No;
1233 }))
1234 FunctionCache = Cache::No;
1235 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1236 return P->RHSComponentCache == Cache::No;
1237 }))
1238 RHSComponentCache = Cache::No;
1239 }
1240
1241 template<typename Fn> void match(Fn F) const { F(Data); }
1242
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001243 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
1244 initializePackExpansion(OB);
1245 size_t Idx = OB.CurrentPackIndex;
1246 return Idx < Data.size() && Data[Idx]->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001247 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001248 bool hasArraySlow(OutputBuffer &OB) const override {
1249 initializePackExpansion(OB);
1250 size_t Idx = OB.CurrentPackIndex;
1251 return Idx < Data.size() && Data[Idx]->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001252 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001253 bool hasFunctionSlow(OutputBuffer &OB) const override {
1254 initializePackExpansion(OB);
1255 size_t Idx = OB.CurrentPackIndex;
1256 return Idx < Data.size() && Data[Idx]->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001257 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001258 const Node *getSyntaxNode(OutputBuffer &OB) const override {
1259 initializePackExpansion(OB);
1260 size_t Idx = OB.CurrentPackIndex;
1261 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(OB) : this;
Richard Smithc20d1442018-08-20 20:14:49 +00001262 }
1263
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001264 void printLeft(OutputBuffer &OB) const override {
1265 initializePackExpansion(OB);
1266 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001267 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001268 Data[Idx]->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001269 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001270 void printRight(OutputBuffer &OB) const override {
1271 initializePackExpansion(OB);
1272 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001273 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001274 Data[Idx]->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001275 }
1276};
1277
1278/// A variadic template argument. This node represents an occurrence of
1279/// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1280/// one of it's Elements is. The parser inserts a ParameterPack into the
1281/// TemplateParams table if the <template-args> this pack belongs to apply to an
1282/// <encoding>.
1283class TemplateArgumentPack final : public Node {
1284 NodeArray Elements;
1285public:
1286 TemplateArgumentPack(NodeArray Elements_)
1287 : Node(KTemplateArgumentPack), Elements(Elements_) {}
1288
1289 template<typename Fn> void match(Fn F) const { F(Elements); }
1290
1291 NodeArray getElements() const { return Elements; }
1292
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001293 void printLeft(OutputBuffer &OB) const override {
1294 Elements.printWithComma(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001295 }
1296};
1297
1298/// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1299/// which each have Child->ParameterPackSize elements.
1300class ParameterPackExpansion final : public Node {
1301 const Node *Child;
1302
1303public:
1304 ParameterPackExpansion(const Node *Child_)
1305 : Node(KParameterPackExpansion), Child(Child_) {}
1306
1307 template<typename Fn> void match(Fn F) const { F(Child); }
1308
1309 const Node *getChild() const { return Child; }
1310
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001311 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001312 constexpr unsigned Max = std::numeric_limits<unsigned>::max();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001313 SwapAndRestore<unsigned> SavePackIdx(OB.CurrentPackIndex, Max);
1314 SwapAndRestore<unsigned> SavePackMax(OB.CurrentPackMax, Max);
1315 size_t StreamPos = OB.getCurrentPosition();
Richard Smithc20d1442018-08-20 20:14:49 +00001316
1317 // Print the first element in the pack. If Child contains a ParameterPack,
1318 // it will set up S.CurrentPackMax and print the first element.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001319 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001320
1321 // No ParameterPack was found in Child. This can occur if we've found a pack
1322 // expansion on a <function-param>.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001323 if (OB.CurrentPackMax == Max) {
1324 OB += "...";
Richard Smithc20d1442018-08-20 20:14:49 +00001325 return;
1326 }
1327
1328 // We found a ParameterPack, but it has no elements. Erase whatever we may
1329 // of printed.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001330 if (OB.CurrentPackMax == 0) {
1331 OB.setCurrentPosition(StreamPos);
Richard Smithc20d1442018-08-20 20:14:49 +00001332 return;
1333 }
1334
1335 // Else, iterate through the rest of the elements in the pack.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001336 for (unsigned I = 1, E = OB.CurrentPackMax; I < E; ++I) {
1337 OB += ", ";
1338 OB.CurrentPackIndex = I;
1339 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001340 }
1341 }
1342};
1343
1344class TemplateArgs final : public Node {
1345 NodeArray Params;
1346
1347public:
1348 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1349
1350 template<typename Fn> void match(Fn F) const { F(Params); }
1351
1352 NodeArray getParams() { return Params; }
1353
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001354 void printLeft(OutputBuffer &OB) const override {
1355 OB += "<";
1356 Params.printWithComma(OB);
1357 if (OB.back() == '>')
1358 OB += " ";
1359 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +00001360 }
1361};
1362
Richard Smithb485b352018-08-24 23:30:26 +00001363/// A forward-reference to a template argument that was not known at the point
1364/// where the template parameter name was parsed in a mangling.
1365///
1366/// This is created when demangling the name of a specialization of a
1367/// conversion function template:
1368///
1369/// \code
1370/// struct A {
1371/// template<typename T> operator T*();
1372/// };
1373/// \endcode
1374///
1375/// When demangling a specialization of the conversion function template, we
1376/// encounter the name of the template (including the \c T) before we reach
1377/// the template argument list, so we cannot substitute the parameter name
1378/// for the corresponding argument while parsing. Instead, we create a
1379/// \c ForwardTemplateReference node that is resolved after we parse the
1380/// template arguments.
Richard Smithc20d1442018-08-20 20:14:49 +00001381struct ForwardTemplateReference : Node {
1382 size_t Index;
1383 Node *Ref = nullptr;
1384
1385 // If we're currently printing this node. It is possible (though invalid) for
1386 // a forward template reference to refer to itself via a substitution. This
1387 // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1388 // out if more than one print* function is active.
1389 mutable bool Printing = false;
1390
1391 ForwardTemplateReference(size_t Index_)
1392 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1393 Cache::Unknown),
1394 Index(Index_) {}
1395
1396 // We don't provide a matcher for these, because the value of the node is
1397 // not determined by its construction parameters, and it generally needs
1398 // special handling.
1399 template<typename Fn> void match(Fn F) const = delete;
1400
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001401 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001402 if (Printing)
1403 return false;
1404 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001405 return Ref->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001406 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001407 bool hasArraySlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001408 if (Printing)
1409 return false;
1410 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001411 return Ref->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001412 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001413 bool hasFunctionSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001414 if (Printing)
1415 return false;
1416 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001417 return Ref->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001418 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001419 const Node *getSyntaxNode(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001420 if (Printing)
1421 return this;
1422 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001423 return Ref->getSyntaxNode(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001424 }
1425
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001426 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001427 if (Printing)
1428 return;
1429 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001430 Ref->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001431 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001432 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001433 if (Printing)
1434 return;
1435 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001436 Ref->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001437 }
1438};
1439
1440struct NameWithTemplateArgs : Node {
1441 // name<template_args>
1442 Node *Name;
1443 Node *TemplateArgs;
1444
1445 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1446 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1447
1448 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1449
1450 StringView getBaseName() const override { return Name->getBaseName(); }
1451
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001452 void printLeft(OutputBuffer &OB) const override {
1453 Name->print(OB);
1454 TemplateArgs->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001455 }
1456};
1457
1458class GlobalQualifiedName final : public Node {
1459 Node *Child;
1460
1461public:
1462 GlobalQualifiedName(Node* Child_)
1463 : Node(KGlobalQualifiedName), Child(Child_) {}
1464
1465 template<typename Fn> void match(Fn F) const { F(Child); }
1466
1467 StringView getBaseName() const override { return Child->getBaseName(); }
1468
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001469 void printLeft(OutputBuffer &OB) const override {
1470 OB += "::";
1471 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001472 }
1473};
1474
Richard Smithc20d1442018-08-20 20:14:49 +00001475enum class SpecialSubKind {
1476 allocator,
1477 basic_string,
1478 string,
1479 istream,
1480 ostream,
1481 iostream,
1482};
1483
1484class ExpandedSpecialSubstitution final : public Node {
1485 SpecialSubKind SSK;
1486
1487public:
1488 ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1489 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1490
1491 template<typename Fn> void match(Fn F) const { F(SSK); }
1492
1493 StringView getBaseName() const override {
1494 switch (SSK) {
1495 case SpecialSubKind::allocator:
1496 return StringView("allocator");
1497 case SpecialSubKind::basic_string:
1498 return StringView("basic_string");
1499 case SpecialSubKind::string:
1500 return StringView("basic_string");
1501 case SpecialSubKind::istream:
1502 return StringView("basic_istream");
1503 case SpecialSubKind::ostream:
1504 return StringView("basic_ostream");
1505 case SpecialSubKind::iostream:
1506 return StringView("basic_iostream");
1507 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001508 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001509 }
1510
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001511 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001512 switch (SSK) {
1513 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001514 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001515 break;
1516 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001517 OB += "std::basic_string";
Richard Smithb485b352018-08-24 23:30:26 +00001518 break;
Richard Smithc20d1442018-08-20 20:14:49 +00001519 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001520 OB += "std::basic_string<char, std::char_traits<char>, "
1521 "std::allocator<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001522 break;
1523 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001524 OB += "std::basic_istream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001525 break;
1526 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001527 OB += "std::basic_ostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001528 break;
1529 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001530 OB += "std::basic_iostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001531 break;
1532 }
1533 }
1534};
1535
1536class SpecialSubstitution final : public Node {
1537public:
1538 SpecialSubKind SSK;
1539
1540 SpecialSubstitution(SpecialSubKind SSK_)
1541 : Node(KSpecialSubstitution), SSK(SSK_) {}
1542
1543 template<typename Fn> void match(Fn F) const { F(SSK); }
1544
1545 StringView getBaseName() const override {
1546 switch (SSK) {
1547 case SpecialSubKind::allocator:
1548 return StringView("allocator");
1549 case SpecialSubKind::basic_string:
1550 return StringView("basic_string");
1551 case SpecialSubKind::string:
1552 return StringView("string");
1553 case SpecialSubKind::istream:
1554 return StringView("istream");
1555 case SpecialSubKind::ostream:
1556 return StringView("ostream");
1557 case SpecialSubKind::iostream:
1558 return StringView("iostream");
1559 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001560 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001561 }
1562
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001563 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001564 switch (SSK) {
1565 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001566 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001567 break;
1568 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001569 OB += "std::basic_string";
Richard Smithc20d1442018-08-20 20:14:49 +00001570 break;
1571 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001572 OB += "std::string";
Richard Smithc20d1442018-08-20 20:14:49 +00001573 break;
1574 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001575 OB += "std::istream";
Richard Smithc20d1442018-08-20 20:14:49 +00001576 break;
1577 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001578 OB += "std::ostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001579 break;
1580 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001581 OB += "std::iostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001582 break;
1583 }
1584 }
1585};
1586
1587class CtorDtorName final : public Node {
1588 const Node *Basename;
1589 const bool IsDtor;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001590 const int Variant;
Richard Smithc20d1442018-08-20 20:14:49 +00001591
1592public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001593 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1594 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1595 Variant(Variant_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001596
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001597 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
Richard Smithc20d1442018-08-20 20:14:49 +00001598
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001599 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001600 if (IsDtor)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001601 OB += "~";
1602 OB += Basename->getBaseName();
Richard Smithc20d1442018-08-20 20:14:49 +00001603 }
1604};
1605
1606class DtorName : public Node {
1607 const Node *Base;
1608
1609public:
1610 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1611
1612 template<typename Fn> void match(Fn F) const { F(Base); }
1613
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001614 void printLeft(OutputBuffer &OB) const override {
1615 OB += "~";
1616 Base->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001617 }
1618};
1619
1620class UnnamedTypeName : public Node {
1621 const StringView Count;
1622
1623public:
1624 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1625
1626 template<typename Fn> void match(Fn F) const { F(Count); }
1627
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001628 void printLeft(OutputBuffer &OB) const override {
1629 OB += "'unnamed";
1630 OB += Count;
1631 OB += "\'";
Richard Smithc20d1442018-08-20 20:14:49 +00001632 }
1633};
1634
1635class ClosureTypeName : public Node {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001636 NodeArray TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00001637 NodeArray Params;
1638 StringView Count;
1639
1640public:
Richard Smithdf1c14c2019-09-06 23:53:21 +00001641 ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_,
1642 StringView Count_)
1643 : Node(KClosureTypeName), TemplateParams(TemplateParams_),
1644 Params(Params_), Count(Count_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001645
Richard Smithdf1c14c2019-09-06 23:53:21 +00001646 template<typename Fn> void match(Fn F) const {
1647 F(TemplateParams, Params, Count);
1648 }
1649
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001650 void printDeclarator(OutputBuffer &OB) const {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001651 if (!TemplateParams.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001652 OB += "<";
1653 TemplateParams.printWithComma(OB);
1654 OB += ">";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001655 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001656 OB += "(";
1657 Params.printWithComma(OB);
1658 OB += ")";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001659 }
Richard Smithc20d1442018-08-20 20:14:49 +00001660
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001661 void printLeft(OutputBuffer &OB) const override {
1662 OB += "\'lambda";
1663 OB += Count;
1664 OB += "\'";
1665 printDeclarator(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001666 }
1667};
1668
1669class StructuredBindingName : public Node {
1670 NodeArray Bindings;
1671public:
1672 StructuredBindingName(NodeArray Bindings_)
1673 : Node(KStructuredBindingName), Bindings(Bindings_) {}
1674
1675 template<typename Fn> void match(Fn F) const { F(Bindings); }
1676
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001677 void printLeft(OutputBuffer &OB) const override {
1678 OB += '[';
1679 Bindings.printWithComma(OB);
1680 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00001681 }
1682};
1683
1684// -- Expression Nodes --
1685
1686class BinaryExpr : public Node {
1687 const Node *LHS;
1688 const StringView InfixOperator;
1689 const Node *RHS;
1690
1691public:
1692 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
1693 : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
1694 }
1695
1696 template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
1697
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001698 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001699 // might be a template argument expression, then we need to disambiguate
1700 // with parens.
1701 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001702 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +00001703
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001704 OB += "(";
1705 LHS->print(OB);
1706 OB += ") ";
1707 OB += InfixOperator;
1708 OB += " (";
1709 RHS->print(OB);
1710 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001711
1712 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001713 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001714 }
1715};
1716
1717class ArraySubscriptExpr : public Node {
1718 const Node *Op1;
1719 const Node *Op2;
1720
1721public:
1722 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
1723 : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
1724
1725 template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
1726
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001727 void printLeft(OutputBuffer &OB) const override {
1728 OB += "(";
1729 Op1->print(OB);
1730 OB += ")[";
1731 Op2->print(OB);
1732 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001733 }
1734};
1735
1736class PostfixExpr : public Node {
1737 const Node *Child;
1738 const StringView Operator;
1739
1740public:
1741 PostfixExpr(const Node *Child_, StringView Operator_)
1742 : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
1743
1744 template<typename Fn> void match(Fn F) const { F(Child, Operator); }
1745
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001746 void printLeft(OutputBuffer &OB) const override {
1747 OB += "(";
1748 Child->print(OB);
1749 OB += ")";
1750 OB += Operator;
Richard Smithc20d1442018-08-20 20:14:49 +00001751 }
1752};
1753
1754class ConditionalExpr : public Node {
1755 const Node *Cond;
1756 const Node *Then;
1757 const Node *Else;
1758
1759public:
1760 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
1761 : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
1762
1763 template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
1764
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001765 void printLeft(OutputBuffer &OB) const override {
1766 OB += "(";
1767 Cond->print(OB);
1768 OB += ") ? (";
1769 Then->print(OB);
1770 OB += ") : (";
1771 Else->print(OB);
1772 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001773 }
1774};
1775
1776class MemberExpr : public Node {
1777 const Node *LHS;
1778 const StringView Kind;
1779 const Node *RHS;
1780
1781public:
1782 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
1783 : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1784
1785 template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
1786
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001787 void printLeft(OutputBuffer &OB) const override {
1788 LHS->print(OB);
1789 OB += Kind;
1790 RHS->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001791 }
1792};
1793
Richard Smith1865d2f2020-10-22 19:29:36 -07001794class SubobjectExpr : public Node {
1795 const Node *Type;
1796 const Node *SubExpr;
1797 StringView Offset;
1798 NodeArray UnionSelectors;
1799 bool OnePastTheEnd;
1800
1801public:
1802 SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_,
1803 NodeArray UnionSelectors_, bool OnePastTheEnd_)
1804 : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_),
1805 UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {}
1806
1807 template<typename Fn> void match(Fn F) const {
1808 F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd);
1809 }
1810
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001811 void printLeft(OutputBuffer &OB) const override {
1812 SubExpr->print(OB);
1813 OB += ".<";
1814 Type->print(OB);
1815 OB += " at offset ";
Richard Smith1865d2f2020-10-22 19:29:36 -07001816 if (Offset.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001817 OB += "0";
Richard Smith1865d2f2020-10-22 19:29:36 -07001818 } else if (Offset[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001819 OB += "-";
1820 OB += Offset.dropFront();
Richard Smith1865d2f2020-10-22 19:29:36 -07001821 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001822 OB += Offset;
Richard Smith1865d2f2020-10-22 19:29:36 -07001823 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001824 OB += ">";
Richard Smith1865d2f2020-10-22 19:29:36 -07001825 }
1826};
1827
Richard Smithc20d1442018-08-20 20:14:49 +00001828class EnclosingExpr : public Node {
1829 const StringView Prefix;
1830 const Node *Infix;
1831 const StringView Postfix;
1832
1833public:
1834 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1835 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1836 Postfix(Postfix_) {}
1837
1838 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1839
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001840 void printLeft(OutputBuffer &OB) const override {
1841 OB += Prefix;
1842 Infix->print(OB);
1843 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +00001844 }
1845};
1846
1847class CastExpr : public Node {
1848 // cast_kind<to>(from)
1849 const StringView CastKind;
1850 const Node *To;
1851 const Node *From;
1852
1853public:
1854 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1855 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1856
1857 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1858
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001859 void printLeft(OutputBuffer &OB) const override {
1860 OB += CastKind;
1861 OB += "<";
1862 To->printLeft(OB);
1863 OB += ">(";
1864 From->printLeft(OB);
1865 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001866 }
1867};
1868
1869class SizeofParamPackExpr : public Node {
1870 const Node *Pack;
1871
1872public:
1873 SizeofParamPackExpr(const Node *Pack_)
1874 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1875
1876 template<typename Fn> void match(Fn F) const { F(Pack); }
1877
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001878 void printLeft(OutputBuffer &OB) const override {
1879 OB += "sizeof...(";
Richard Smithc20d1442018-08-20 20:14:49 +00001880 ParameterPackExpansion PPE(Pack);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001881 PPE.printLeft(OB);
1882 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001883 }
1884};
1885
1886class CallExpr : public Node {
1887 const Node *Callee;
1888 NodeArray Args;
1889
1890public:
1891 CallExpr(const Node *Callee_, NodeArray Args_)
1892 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1893
1894 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1895
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001896 void printLeft(OutputBuffer &OB) const override {
1897 Callee->print(OB);
1898 OB += "(";
1899 Args.printWithComma(OB);
1900 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001901 }
1902};
1903
1904class NewExpr : public Node {
1905 // new (expr_list) type(init_list)
1906 NodeArray ExprList;
1907 Node *Type;
1908 NodeArray InitList;
1909 bool IsGlobal; // ::operator new ?
1910 bool IsArray; // new[] ?
1911public:
1912 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1913 bool IsArray_)
1914 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1915 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1916
1917 template<typename Fn> void match(Fn F) const {
1918 F(ExprList, Type, InitList, IsGlobal, IsArray);
1919 }
1920
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001921 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001922 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001923 OB += "::operator ";
1924 OB += "new";
Richard Smithc20d1442018-08-20 20:14:49 +00001925 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001926 OB += "[]";
1927 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00001928 if (!ExprList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001929 OB += "(";
1930 ExprList.printWithComma(OB);
1931 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001932 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001933 Type->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001934 if (!InitList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001935 OB += "(";
1936 InitList.printWithComma(OB);
1937 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001938 }
Richard Smithc20d1442018-08-20 20:14:49 +00001939 }
1940};
1941
1942class DeleteExpr : public Node {
1943 Node *Op;
1944 bool IsGlobal;
1945 bool IsArray;
1946
1947public:
1948 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1949 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1950
1951 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1952
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001953 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001954 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001955 OB += "::";
1956 OB += "delete";
Richard Smithc20d1442018-08-20 20:14:49 +00001957 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001958 OB += "[] ";
1959 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001960 }
1961};
1962
1963class PrefixExpr : public Node {
1964 StringView Prefix;
1965 Node *Child;
1966
1967public:
1968 PrefixExpr(StringView Prefix_, Node *Child_)
1969 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1970
1971 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1972
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001973 void printLeft(OutputBuffer &OB) const override {
1974 OB += Prefix;
1975 OB += "(";
1976 Child->print(OB);
1977 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001978 }
1979};
1980
1981class FunctionParam : public Node {
1982 StringView Number;
1983
1984public:
1985 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
1986
1987 template<typename Fn> void match(Fn F) const { F(Number); }
1988
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001989 void printLeft(OutputBuffer &OB) const override {
1990 OB += "fp";
1991 OB += Number;
Richard Smithc20d1442018-08-20 20:14:49 +00001992 }
1993};
1994
1995class ConversionExpr : public Node {
1996 const Node *Type;
1997 NodeArray Expressions;
1998
1999public:
2000 ConversionExpr(const Node *Type_, NodeArray Expressions_)
2001 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
2002
2003 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
2004
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002005 void printLeft(OutputBuffer &OB) const override {
2006 OB += "(";
2007 Type->print(OB);
2008 OB += ")(";
2009 Expressions.printWithComma(OB);
2010 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002011 }
2012};
2013
Richard Smith1865d2f2020-10-22 19:29:36 -07002014class PointerToMemberConversionExpr : public Node {
2015 const Node *Type;
2016 const Node *SubExpr;
2017 StringView Offset;
2018
2019public:
2020 PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_,
2021 StringView Offset_)
2022 : Node(KPointerToMemberConversionExpr), Type(Type_), SubExpr(SubExpr_),
2023 Offset(Offset_) {}
2024
2025 template<typename Fn> void match(Fn F) const { F(Type, SubExpr, Offset); }
2026
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002027 void printLeft(OutputBuffer &OB) const override {
2028 OB += "(";
2029 Type->print(OB);
2030 OB += ")(";
2031 SubExpr->print(OB);
2032 OB += ")";
Richard Smith1865d2f2020-10-22 19:29:36 -07002033 }
2034};
2035
Richard Smithc20d1442018-08-20 20:14:49 +00002036class InitListExpr : public Node {
2037 const Node *Ty;
2038 NodeArray Inits;
2039public:
2040 InitListExpr(const Node *Ty_, NodeArray Inits_)
2041 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
2042
2043 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
2044
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002045 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002046 if (Ty)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002047 Ty->print(OB);
2048 OB += '{';
2049 Inits.printWithComma(OB);
2050 OB += '}';
Richard Smithc20d1442018-08-20 20:14:49 +00002051 }
2052};
2053
2054class BracedExpr : public Node {
2055 const Node *Elem;
2056 const Node *Init;
2057 bool IsArray;
2058public:
2059 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
2060 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
2061
2062 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
2063
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002064 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002065 if (IsArray) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002066 OB += '[';
2067 Elem->print(OB);
2068 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002069 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002070 OB += '.';
2071 Elem->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002072 }
2073 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002074 OB += " = ";
2075 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002076 }
2077};
2078
2079class BracedRangeExpr : public Node {
2080 const Node *First;
2081 const Node *Last;
2082 const Node *Init;
2083public:
2084 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
2085 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
2086
2087 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
2088
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002089 void printLeft(OutputBuffer &OB) const override {
2090 OB += '[';
2091 First->print(OB);
2092 OB += " ... ";
2093 Last->print(OB);
2094 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002095 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002096 OB += " = ";
2097 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002098 }
2099};
2100
2101class FoldExpr : public Node {
2102 const Node *Pack, *Init;
2103 StringView OperatorName;
2104 bool IsLeftFold;
2105
2106public:
2107 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
2108 const Node *Init_)
2109 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
2110 IsLeftFold(IsLeftFold_) {}
2111
2112 template<typename Fn> void match(Fn F) const {
2113 F(IsLeftFold, OperatorName, Pack, Init);
2114 }
2115
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002116 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002117 auto PrintPack = [&] {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002118 OB += '(';
2119 ParameterPackExpansion(Pack).print(OB);
2120 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002121 };
2122
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002123 OB += '(';
Richard Smithc20d1442018-08-20 20:14:49 +00002124
2125 if (IsLeftFold) {
2126 // init op ... op pack
2127 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002128 Init->print(OB);
2129 OB += ' ';
2130 OB += OperatorName;
2131 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002132 }
2133 // ... op pack
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002134 OB += "... ";
2135 OB += OperatorName;
2136 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002137 PrintPack();
2138 } else { // !IsLeftFold
2139 // pack op ...
2140 PrintPack();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002141 OB += ' ';
2142 OB += OperatorName;
2143 OB += " ...";
Richard Smithc20d1442018-08-20 20:14:49 +00002144 // pack op ... op init
2145 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002146 OB += ' ';
2147 OB += OperatorName;
2148 OB += ' ';
2149 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002150 }
2151 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002152 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002153 }
2154};
2155
2156class ThrowExpr : public Node {
2157 const Node *Op;
2158
2159public:
2160 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
2161
2162 template<typename Fn> void match(Fn F) const { F(Op); }
2163
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002164 void printLeft(OutputBuffer &OB) const override {
2165 OB += "throw ";
2166 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002167 }
2168};
2169
2170class BoolExpr : public Node {
2171 bool Value;
2172
2173public:
2174 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
2175
2176 template<typename Fn> void match(Fn F) const { F(Value); }
2177
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002178 void printLeft(OutputBuffer &OB) const override {
2179 OB += Value ? StringView("true") : StringView("false");
Richard Smithc20d1442018-08-20 20:14:49 +00002180 }
2181};
2182
Richard Smithdf1c14c2019-09-06 23:53:21 +00002183class StringLiteral : public Node {
2184 const Node *Type;
2185
2186public:
2187 StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
2188
2189 template<typename Fn> void match(Fn F) const { F(Type); }
2190
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002191 void printLeft(OutputBuffer &OB) const override {
2192 OB += "\"<";
2193 Type->print(OB);
2194 OB += ">\"";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002195 }
2196};
2197
2198class LambdaExpr : public Node {
2199 const Node *Type;
2200
Richard Smithdf1c14c2019-09-06 23:53:21 +00002201public:
2202 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2203
2204 template<typename Fn> void match(Fn F) const { F(Type); }
2205
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002206 void printLeft(OutputBuffer &OB) const override {
2207 OB += "[]";
Richard Smithfb917462019-09-09 22:26:04 +00002208 if (Type->getKind() == KClosureTypeName)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002209 static_cast<const ClosureTypeName *>(Type)->printDeclarator(OB);
2210 OB += "{...}";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002211 }
2212};
2213
Erik Pilkington0a170f12020-05-13 14:13:37 -04002214class EnumLiteral : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +00002215 // ty(integer)
2216 const Node *Ty;
2217 StringView Integer;
2218
2219public:
Erik Pilkington0a170f12020-05-13 14:13:37 -04002220 EnumLiteral(const Node *Ty_, StringView Integer_)
2221 : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00002222
2223 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2224
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002225 void printLeft(OutputBuffer &OB) const override {
2226 OB << "(";
2227 Ty->print(OB);
2228 OB << ")";
Erik Pilkington0a170f12020-05-13 14:13:37 -04002229
2230 if (Integer[0] == 'n')
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002231 OB << "-" << Integer.dropFront(1);
Erik Pilkington0a170f12020-05-13 14:13:37 -04002232 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002233 OB << Integer;
Richard Smithc20d1442018-08-20 20:14:49 +00002234 }
2235};
2236
2237class IntegerLiteral : public Node {
2238 StringView Type;
2239 StringView Value;
2240
2241public:
2242 IntegerLiteral(StringView Type_, StringView Value_)
2243 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2244
2245 template<typename Fn> void match(Fn F) const { F(Type, Value); }
2246
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002247 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002248 if (Type.size() > 3) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002249 OB += "(";
2250 OB += Type;
2251 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002252 }
2253
2254 if (Value[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002255 OB += "-";
2256 OB += Value.dropFront(1);
Richard Smithc20d1442018-08-20 20:14:49 +00002257 } else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002258 OB += Value;
Richard Smithc20d1442018-08-20 20:14:49 +00002259
2260 if (Type.size() <= 3)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002261 OB += Type;
Richard Smithc20d1442018-08-20 20:14:49 +00002262 }
2263};
2264
2265template <class Float> struct FloatData;
2266
2267namespace float_literal_impl {
2268constexpr Node::Kind getFloatLiteralKind(float *) {
2269 return Node::KFloatLiteral;
2270}
2271constexpr Node::Kind getFloatLiteralKind(double *) {
2272 return Node::KDoubleLiteral;
2273}
2274constexpr Node::Kind getFloatLiteralKind(long double *) {
2275 return Node::KLongDoubleLiteral;
2276}
2277}
2278
2279template <class Float> class FloatLiteralImpl : public Node {
2280 const StringView Contents;
2281
2282 static constexpr Kind KindForClass =
2283 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2284
2285public:
2286 FloatLiteralImpl(StringView Contents_)
2287 : Node(KindForClass), Contents(Contents_) {}
2288
2289 template<typename Fn> void match(Fn F) const { F(Contents); }
2290
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002291 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002292 const char *first = Contents.begin();
2293 const char *last = Contents.end() + 1;
2294
2295 const size_t N = FloatData<Float>::mangled_size;
2296 if (static_cast<std::size_t>(last - first) > N) {
2297 last = first + N;
2298 union {
2299 Float value;
2300 char buf[sizeof(Float)];
2301 };
2302 const char *t = first;
2303 char *e = buf;
2304 for (; t != last; ++t, ++e) {
2305 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2306 : static_cast<unsigned>(*t - 'a' + 10);
2307 ++t;
2308 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2309 : static_cast<unsigned>(*t - 'a' + 10);
2310 *e = static_cast<char>((d1 << 4) + d0);
2311 }
2312#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2313 std::reverse(buf, e);
2314#endif
2315 char num[FloatData<Float>::max_demangled_size] = {0};
2316 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002317 OB += StringView(num, num + n);
Richard Smithc20d1442018-08-20 20:14:49 +00002318 }
2319 }
2320};
2321
2322using FloatLiteral = FloatLiteralImpl<float>;
2323using DoubleLiteral = FloatLiteralImpl<double>;
2324using LongDoubleLiteral = FloatLiteralImpl<long double>;
2325
2326/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2327/// appropriate derived class.
2328template<typename Fn>
2329void Node::visit(Fn F) const {
2330 switch (K) {
2331#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2332 FOR_EACH_NODE_KIND(CASE)
2333#undef CASE
2334 }
2335 assert(0 && "unknown mangling node kind");
2336}
2337
2338/// Determine the kind of a node from its type.
2339template<typename NodeT> struct NodeKind;
2340#define SPECIALIZATION(X) \
2341 template<> struct NodeKind<X> { \
2342 static constexpr Node::Kind Kind = Node::K##X; \
2343 static constexpr const char *name() { return #X; } \
2344 };
2345FOR_EACH_NODE_KIND(SPECIALIZATION)
2346#undef SPECIALIZATION
2347
2348#undef FOR_EACH_NODE_KIND
2349
Pavel Labathba825192018-10-16 14:29:14 +00002350template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002351 const char *First;
2352 const char *Last;
2353
2354 // Name stack, this is used by the parser to hold temporary names that were
2355 // parsed. The parser collapses multiple names into new nodes to construct
2356 // the AST. Once the parser is finished, names.size() == 1.
2357 PODSmallVector<Node *, 32> Names;
2358
2359 // Substitution table. Itanium supports name substitutions as a means of
2360 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2361 // table.
2362 PODSmallVector<Node *, 32> Subs;
2363
Richard Smithdf1c14c2019-09-06 23:53:21 +00002364 using TemplateParamList = PODSmallVector<Node *, 8>;
2365
2366 class ScopedTemplateParamList {
2367 AbstractManglingParser *Parser;
2368 size_t OldNumTemplateParamLists;
2369 TemplateParamList Params;
2370
2371 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04002372 ScopedTemplateParamList(AbstractManglingParser *TheParser)
2373 : Parser(TheParser),
2374 OldNumTemplateParamLists(TheParser->TemplateParams.size()) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002375 Parser->TemplateParams.push_back(&Params);
2376 }
2377 ~ScopedTemplateParamList() {
2378 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2379 Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2380 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002381 };
2382
Richard Smithc20d1442018-08-20 20:14:49 +00002383 // Template parameter table. Like the above, but referenced like "T42_".
2384 // This has a smaller size compared to Subs and Names because it can be
2385 // stored on the stack.
Richard Smithdf1c14c2019-09-06 23:53:21 +00002386 TemplateParamList OuterTemplateParams;
2387
2388 // Lists of template parameters indexed by template parameter depth,
2389 // referenced like "TL2_4_". If nonempty, element 0 is always
2390 // OuterTemplateParams; inner elements are always template parameter lists of
2391 // lambda expressions. For a generic lambda with no explicit template
2392 // parameter list, the corresponding parameter list pointer will be null.
2393 PODSmallVector<TemplateParamList *, 4> TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00002394
2395 // Set of unresolved forward <template-param> references. These can occur in a
2396 // conversion operator's type, and are resolved in the enclosing <encoding>.
2397 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2398
Richard Smithc20d1442018-08-20 20:14:49 +00002399 bool TryToParseTemplateArgs = true;
2400 bool PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002401 size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2402
2403 unsigned NumSyntheticTemplateParameters[3] = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002404
2405 Alloc ASTAllocator;
2406
Pavel Labathba825192018-10-16 14:29:14 +00002407 AbstractManglingParser(const char *First_, const char *Last_)
2408 : First(First_), Last(Last_) {}
2409
2410 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002411
2412 void reset(const char *First_, const char *Last_) {
2413 First = First_;
2414 Last = Last_;
2415 Names.clear();
2416 Subs.clear();
2417 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002418 ParsingLambdaParamsAtLevel = (size_t)-1;
Richard Smithc20d1442018-08-20 20:14:49 +00002419 TryToParseTemplateArgs = true;
2420 PermitForwardTemplateReferences = false;
Richard Smith9a2307a2019-09-07 00:11:53 +00002421 for (int I = 0; I != 3; ++I)
2422 NumSyntheticTemplateParameters[I] = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00002423 ASTAllocator.reset();
2424 }
2425
Richard Smithb485b352018-08-24 23:30:26 +00002426 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002427 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2428 }
2429
2430 template <class It> NodeArray makeNodeArray(It begin, It end) {
2431 size_t sz = static_cast<size_t>(end - begin);
2432 void *mem = ASTAllocator.allocateNodeArray(sz);
2433 Node **data = new (mem) Node *[sz];
2434 std::copy(begin, end, data);
2435 return NodeArray(data, sz);
2436 }
2437
2438 NodeArray popTrailingNodeArray(size_t FromPosition) {
2439 assert(FromPosition <= Names.size());
2440 NodeArray res =
2441 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2442 Names.dropBack(FromPosition);
2443 return res;
2444 }
2445
2446 bool consumeIf(StringView S) {
2447 if (StringView(First, Last).startsWith(S)) {
2448 First += S.size();
2449 return true;
2450 }
2451 return false;
2452 }
2453
2454 bool consumeIf(char C) {
2455 if (First != Last && *First == C) {
2456 ++First;
2457 return true;
2458 }
2459 return false;
2460 }
2461
2462 char consume() { return First != Last ? *First++ : '\0'; }
2463
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08002464 char look(unsigned Lookahead = 0) const {
Richard Smithc20d1442018-08-20 20:14:49 +00002465 if (static_cast<size_t>(Last - First) <= Lookahead)
2466 return '\0';
2467 return First[Lookahead];
2468 }
2469
2470 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2471
2472 StringView parseNumber(bool AllowNegative = false);
2473 Qualifiers parseCVQualifiers();
2474 bool parsePositiveInteger(size_t *Out);
2475 StringView parseBareSourceName();
2476
2477 bool parseSeqId(size_t *Out);
2478 Node *parseSubstitution();
2479 Node *parseTemplateParam();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002480 Node *parseTemplateParamDecl();
Richard Smithc20d1442018-08-20 20:14:49 +00002481 Node *parseTemplateArgs(bool TagTemplates = false);
2482 Node *parseTemplateArg();
2483
2484 /// Parse the <expr> production.
2485 Node *parseExpr();
2486 Node *parsePrefixExpr(StringView Kind);
2487 Node *parseBinaryExpr(StringView Kind);
2488 Node *parseIntegerLiteral(StringView Lit);
2489 Node *parseExprPrimary();
2490 template <class Float> Node *parseFloatingLiteral();
2491 Node *parseFunctionParam();
2492 Node *parseNewExpr();
2493 Node *parseConversionExpr();
2494 Node *parseBracedExpr();
2495 Node *parseFoldExpr();
Richard Smith1865d2f2020-10-22 19:29:36 -07002496 Node *parsePointerToMemberConversionExpr();
2497 Node *parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00002498
2499 /// Parse the <type> production.
2500 Node *parseType();
2501 Node *parseFunctionType();
2502 Node *parseVectorType();
2503 Node *parseDecltype();
2504 Node *parseArrayType();
2505 Node *parsePointerToMemberType();
2506 Node *parseClassEnumType();
2507 Node *parseQualifiedType();
2508
2509 Node *parseEncoding();
2510 bool parseCallOffset();
2511 Node *parseSpecialName();
2512
2513 /// Holds some extra information about a <name> that is being parsed. This
2514 /// information is only pertinent if the <name> refers to an <encoding>.
2515 struct NameState {
2516 bool CtorDtorConversion = false;
2517 bool EndsWithTemplateArgs = false;
2518 Qualifiers CVQualifiers = QualNone;
2519 FunctionRefQual ReferenceQualifier = FrefQualNone;
2520 size_t ForwardTemplateRefsBegin;
2521
Pavel Labathba825192018-10-16 14:29:14 +00002522 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002523 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2524 };
2525
2526 bool resolveForwardTemplateRefs(NameState &State) {
2527 size_t I = State.ForwardTemplateRefsBegin;
2528 size_t E = ForwardTemplateRefs.size();
2529 for (; I < E; ++I) {
2530 size_t Idx = ForwardTemplateRefs[I]->Index;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002531 if (TemplateParams.empty() || !TemplateParams[0] ||
2532 Idx >= TemplateParams[0]->size())
Richard Smithc20d1442018-08-20 20:14:49 +00002533 return true;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002534 ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
Richard Smithc20d1442018-08-20 20:14:49 +00002535 }
2536 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2537 return false;
2538 }
2539
2540 /// Parse the <name> production>
2541 Node *parseName(NameState *State = nullptr);
2542 Node *parseLocalName(NameState *State);
2543 Node *parseOperatorName(NameState *State);
2544 Node *parseUnqualifiedName(NameState *State);
2545 Node *parseUnnamedTypeName(NameState *State);
2546 Node *parseSourceName(NameState *State);
2547 Node *parseUnscopedName(NameState *State);
2548 Node *parseNestedName(NameState *State);
2549 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2550
2551 Node *parseAbiTags(Node *N);
2552
2553 /// Parse the <unresolved-name> production.
Nathan Sidwell77c52e22022-01-28 11:59:03 -08002554 Node *parseUnresolvedName(bool Global);
Richard Smithc20d1442018-08-20 20:14:49 +00002555 Node *parseSimpleId();
2556 Node *parseBaseUnresolvedName();
2557 Node *parseUnresolvedType();
2558 Node *parseDestructorName();
2559
2560 /// Top-level entry point into the parser.
2561 Node *parse();
2562};
2563
2564const char* parse_discriminator(const char* first, const char* last);
2565
2566// <name> ::= <nested-name> // N
2567// ::= <local-name> # See Scope Encoding below // Z
2568// ::= <unscoped-template-name> <template-args>
2569// ::= <unscoped-name>
2570//
2571// <unscoped-template-name> ::= <unscoped-name>
2572// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002573template <typename Derived, typename Alloc>
2574Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002575 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002576 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002577 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002578 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002579
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002580 Node *Result = nullptr;
2581 bool IsSubst = look() == 'S' && look(1) != 't';
2582 if (IsSubst) {
2583 // A substitution must lead to:
2584 // ::= <unscoped-template-name> <template-args>
2585 Result = getDerived().parseSubstitution();
2586 } else {
2587 // An unscoped name can be one of:
2588 // ::= <unscoped-name>
2589 // ::= <unscoped-template-name> <template-args>
2590 Result = getDerived().parseUnscopedName(State);
2591 }
2592 if (Result == nullptr)
2593 return nullptr;
2594
2595 if (look() == 'I') {
2596 // ::= <unscoped-template-name> <template-args>
2597 if (!IsSubst)
2598 // An unscoped-template-name is substitutable.
2599 Subs.push_back(Result);
Pavel Labathba825192018-10-16 14:29:14 +00002600 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002601 if (TA == nullptr)
2602 return nullptr;
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002603 if (State)
2604 State->EndsWithTemplateArgs = true;
2605 Result = make<NameWithTemplateArgs>(Result, TA);
2606 } else if (IsSubst) {
2607 // The substitution case must be followed by <template-args>.
2608 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002609 }
2610
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002611 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00002612}
2613
2614// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2615// := Z <function encoding> E s [<discriminator>]
2616// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002617template <typename Derived, typename Alloc>
2618Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002619 if (!consumeIf('Z'))
2620 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002621 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002622 if (Encoding == nullptr || !consumeIf('E'))
2623 return nullptr;
2624
2625 if (consumeIf('s')) {
2626 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002627 auto *StringLitName = make<NameType>("string literal");
2628 if (!StringLitName)
2629 return nullptr;
2630 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002631 }
2632
2633 if (consumeIf('d')) {
2634 parseNumber(true);
2635 if (!consumeIf('_'))
2636 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002637 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002638 if (N == nullptr)
2639 return nullptr;
2640 return make<LocalName>(Encoding, N);
2641 }
2642
Pavel Labathba825192018-10-16 14:29:14 +00002643 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002644 if (Entity == nullptr)
2645 return nullptr;
2646 First = parse_discriminator(First, Last);
2647 return make<LocalName>(Encoding, Entity);
2648}
2649
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08002650// <unscoped-name> ::= [L]* <unqualified-name>
2651// ::= St [L]* <unqualified-name> # ::std::
2652// [*] extension
Pavel Labathba825192018-10-16 14:29:14 +00002653template <typename Derived, typename Alloc>
2654Node *
2655AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002656 bool IsStd = consumeIf("St");
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08002657 consumeIf('L');
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002658
2659 Node *Result = getDerived().parseUnqualifiedName(State);
2660 if (Result == nullptr)
2661 return nullptr;
Nathan Sidwell200e97c2022-01-21 11:37:01 -08002662 if (IsStd) {
2663 if (auto *Std = make<NameType>("std"))
2664 Result = make<NestedName>(Std, Result);
2665 else
2666 return nullptr;
2667 }
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002668
2669 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00002670}
2671
2672// <unqualified-name> ::= <operator-name> [abi-tags]
2673// ::= <ctor-dtor-name>
2674// ::= <source-name>
2675// ::= <unnamed-type-name>
2676// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002677template <typename Derived, typename Alloc>
2678Node *
2679AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002680 // <ctor-dtor-name>s are special-cased in parseNestedName().
2681 Node *Result;
2682 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002683 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002684 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002685 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002686 else if (consumeIf("DC")) {
2687 size_t BindingsBegin = Names.size();
2688 do {
Pavel Labathba825192018-10-16 14:29:14 +00002689 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002690 if (Binding == nullptr)
2691 return nullptr;
2692 Names.push_back(Binding);
2693 } while (!consumeIf('E'));
2694 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2695 } else
Pavel Labathba825192018-10-16 14:29:14 +00002696 Result = getDerived().parseOperatorName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002697 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002698 Result = getDerived().parseAbiTags(Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002699 return Result;
2700}
2701
2702// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2703// ::= <closure-type-name>
2704//
2705// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2706//
2707// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002708template <typename Derived, typename Alloc>
2709Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002710AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2711 // <template-params> refer to the innermost <template-args>. Clear out any
2712 // outer args that we may have inserted into TemplateParams.
2713 if (State != nullptr)
2714 TemplateParams.clear();
2715
Richard Smithc20d1442018-08-20 20:14:49 +00002716 if (consumeIf("Ut")) {
2717 StringView Count = parseNumber();
2718 if (!consumeIf('_'))
2719 return nullptr;
2720 return make<UnnamedTypeName>(Count);
2721 }
2722 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002723 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2724 TemplateParams.size());
2725 ScopedTemplateParamList LambdaTemplateParams(this);
2726
2727 size_t ParamsBegin = Names.size();
2728 while (look() == 'T' &&
2729 StringView("yptn").find(look(1)) != StringView::npos) {
2730 Node *T = parseTemplateParamDecl();
2731 if (!T)
2732 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002733 Names.push_back(T);
2734 }
2735 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2736
2737 // FIXME: If TempParams is empty and none of the function parameters
2738 // includes 'auto', we should remove LambdaTemplateParams from the
2739 // TemplateParams list. Unfortunately, we don't find out whether there are
2740 // any 'auto' parameters until too late in an example such as:
2741 //
2742 // template<typename T> void f(
2743 // decltype([](decltype([]<typename T>(T v) {}),
2744 // auto) {})) {}
2745 // template<typename T> void f(
2746 // decltype([](decltype([]<typename T>(T w) {}),
2747 // int) {})) {}
2748 //
2749 // Here, the type of v is at level 2 but the type of w is at level 1. We
2750 // don't find this out until we encounter the type of the next parameter.
2751 //
2752 // However, compilers can't actually cope with the former example in
2753 // practice, and it's likely to be made ill-formed in future, so we don't
2754 // need to support it here.
2755 //
2756 // If we encounter an 'auto' in the function parameter types, we will
2757 // recreate a template parameter scope for it, but any intervening lambdas
2758 // will be parsed in the 'wrong' template parameter depth.
2759 if (TempParams.empty())
2760 TemplateParams.pop_back();
2761
Richard Smithc20d1442018-08-20 20:14:49 +00002762 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002763 do {
Pavel Labathba825192018-10-16 14:29:14 +00002764 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002765 if (P == nullptr)
2766 return nullptr;
2767 Names.push_back(P);
2768 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002769 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002770 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2771
Richard Smithc20d1442018-08-20 20:14:49 +00002772 StringView Count = parseNumber();
2773 if (!consumeIf('_'))
2774 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002775 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002776 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002777 if (consumeIf("Ub")) {
2778 (void)parseNumber();
2779 if (!consumeIf('_'))
2780 return nullptr;
2781 return make<NameType>("'block-literal'");
2782 }
Richard Smithc20d1442018-08-20 20:14:49 +00002783 return nullptr;
2784}
2785
2786// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002787template <typename Derived, typename Alloc>
2788Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002789 size_t Length = 0;
2790 if (parsePositiveInteger(&Length))
2791 return nullptr;
2792 if (numLeft() < Length || Length == 0)
2793 return nullptr;
2794 StringView Name(First, First + Length);
2795 First += Length;
2796 if (Name.startsWith("_GLOBAL__N"))
2797 return make<NameType>("(anonymous namespace)");
2798 return make<NameType>(Name);
2799}
2800
2801// <operator-name> ::= aa # &&
2802// ::= ad # & (unary)
2803// ::= an # &
2804// ::= aN # &=
2805// ::= aS # =
2806// ::= cl # ()
2807// ::= cm # ,
2808// ::= co # ~
2809// ::= cv <type> # (cast)
2810// ::= da # delete[]
2811// ::= de # * (unary)
2812// ::= dl # delete
2813// ::= dv # /
2814// ::= dV # /=
2815// ::= eo # ^
2816// ::= eO # ^=
2817// ::= eq # ==
2818// ::= ge # >=
2819// ::= gt # >
2820// ::= ix # []
2821// ::= le # <=
2822// ::= li <source-name> # operator ""
2823// ::= ls # <<
2824// ::= lS # <<=
2825// ::= lt # <
2826// ::= mi # -
2827// ::= mI # -=
2828// ::= ml # *
2829// ::= mL # *=
2830// ::= mm # -- (postfix in <expression> context)
2831// ::= na # new[]
2832// ::= ne # !=
2833// ::= ng # - (unary)
2834// ::= nt # !
2835// ::= nw # new
2836// ::= oo # ||
2837// ::= or # |
2838// ::= oR # |=
2839// ::= pm # ->*
2840// ::= pl # +
2841// ::= pL # +=
2842// ::= pp # ++ (postfix in <expression> context)
2843// ::= ps # + (unary)
2844// ::= pt # ->
2845// ::= qu # ?
2846// ::= rm # %
2847// ::= rM # %=
2848// ::= rs # >>
2849// ::= rS # >>=
2850// ::= ss # <=> C++2a
2851// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002852template <typename Derived, typename Alloc>
2853Node *
2854AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002855 switch (look()) {
2856 case 'a':
2857 switch (look(1)) {
2858 case 'a':
2859 First += 2;
2860 return make<NameType>("operator&&");
2861 case 'd':
2862 case 'n':
2863 First += 2;
2864 return make<NameType>("operator&");
2865 case 'N':
2866 First += 2;
2867 return make<NameType>("operator&=");
2868 case 'S':
2869 First += 2;
2870 return make<NameType>("operator=");
2871 }
2872 return nullptr;
2873 case 'c':
2874 switch (look(1)) {
2875 case 'l':
2876 First += 2;
2877 return make<NameType>("operator()");
2878 case 'm':
2879 First += 2;
2880 return make<NameType>("operator,");
2881 case 'o':
2882 First += 2;
2883 return make<NameType>("operator~");
2884 // ::= cv <type> # (cast)
2885 case 'v': {
2886 First += 2;
2887 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2888 // If we're parsing an encoding, State != nullptr and the conversion
2889 // operators' <type> could have a <template-param> that refers to some
2890 // <template-arg>s further ahead in the mangled name.
2891 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2892 PermitForwardTemplateReferences ||
2893 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002894 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002895 if (Ty == nullptr)
2896 return nullptr;
2897 if (State) State->CtorDtorConversion = true;
2898 return make<ConversionOperatorType>(Ty);
2899 }
2900 }
2901 return nullptr;
2902 case 'd':
2903 switch (look(1)) {
2904 case 'a':
2905 First += 2;
2906 return make<NameType>("operator delete[]");
2907 case 'e':
2908 First += 2;
2909 return make<NameType>("operator*");
2910 case 'l':
2911 First += 2;
2912 return make<NameType>("operator delete");
2913 case 'v':
2914 First += 2;
2915 return make<NameType>("operator/");
2916 case 'V':
2917 First += 2;
2918 return make<NameType>("operator/=");
2919 }
2920 return nullptr;
2921 case 'e':
2922 switch (look(1)) {
2923 case 'o':
2924 First += 2;
2925 return make<NameType>("operator^");
2926 case 'O':
2927 First += 2;
2928 return make<NameType>("operator^=");
2929 case 'q':
2930 First += 2;
2931 return make<NameType>("operator==");
2932 }
2933 return nullptr;
2934 case 'g':
2935 switch (look(1)) {
2936 case 'e':
2937 First += 2;
2938 return make<NameType>("operator>=");
2939 case 't':
2940 First += 2;
2941 return make<NameType>("operator>");
2942 }
2943 return nullptr;
2944 case 'i':
2945 if (look(1) == 'x') {
2946 First += 2;
2947 return make<NameType>("operator[]");
2948 }
2949 return nullptr;
2950 case 'l':
2951 switch (look(1)) {
2952 case 'e':
2953 First += 2;
2954 return make<NameType>("operator<=");
2955 // ::= li <source-name> # operator ""
2956 case 'i': {
2957 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002958 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002959 if (SN == nullptr)
2960 return nullptr;
2961 return make<LiteralOperator>(SN);
2962 }
2963 case 's':
2964 First += 2;
2965 return make<NameType>("operator<<");
2966 case 'S':
2967 First += 2;
2968 return make<NameType>("operator<<=");
2969 case 't':
2970 First += 2;
2971 return make<NameType>("operator<");
2972 }
2973 return nullptr;
2974 case 'm':
2975 switch (look(1)) {
2976 case 'i':
2977 First += 2;
2978 return make<NameType>("operator-");
2979 case 'I':
2980 First += 2;
2981 return make<NameType>("operator-=");
2982 case 'l':
2983 First += 2;
2984 return make<NameType>("operator*");
2985 case 'L':
2986 First += 2;
2987 return make<NameType>("operator*=");
2988 case 'm':
2989 First += 2;
2990 return make<NameType>("operator--");
2991 }
2992 return nullptr;
2993 case 'n':
2994 switch (look(1)) {
2995 case 'a':
2996 First += 2;
2997 return make<NameType>("operator new[]");
2998 case 'e':
2999 First += 2;
3000 return make<NameType>("operator!=");
3001 case 'g':
3002 First += 2;
3003 return make<NameType>("operator-");
3004 case 't':
3005 First += 2;
3006 return make<NameType>("operator!");
3007 case 'w':
3008 First += 2;
3009 return make<NameType>("operator new");
3010 }
3011 return nullptr;
3012 case 'o':
3013 switch (look(1)) {
3014 case 'o':
3015 First += 2;
3016 return make<NameType>("operator||");
3017 case 'r':
3018 First += 2;
3019 return make<NameType>("operator|");
3020 case 'R':
3021 First += 2;
3022 return make<NameType>("operator|=");
3023 }
3024 return nullptr;
3025 case 'p':
3026 switch (look(1)) {
3027 case 'm':
3028 First += 2;
3029 return make<NameType>("operator->*");
3030 case 'l':
3031 First += 2;
3032 return make<NameType>("operator+");
3033 case 'L':
3034 First += 2;
3035 return make<NameType>("operator+=");
3036 case 'p':
3037 First += 2;
3038 return make<NameType>("operator++");
3039 case 's':
3040 First += 2;
3041 return make<NameType>("operator+");
3042 case 't':
3043 First += 2;
3044 return make<NameType>("operator->");
3045 }
3046 return nullptr;
3047 case 'q':
3048 if (look(1) == 'u') {
3049 First += 2;
3050 return make<NameType>("operator?");
3051 }
3052 return nullptr;
3053 case 'r':
3054 switch (look(1)) {
3055 case 'm':
3056 First += 2;
3057 return make<NameType>("operator%");
3058 case 'M':
3059 First += 2;
3060 return make<NameType>("operator%=");
3061 case 's':
3062 First += 2;
3063 return make<NameType>("operator>>");
3064 case 'S':
3065 First += 2;
3066 return make<NameType>("operator>>=");
3067 }
3068 return nullptr;
3069 case 's':
3070 if (look(1) == 's') {
3071 First += 2;
3072 return make<NameType>("operator<=>");
3073 }
3074 return nullptr;
3075 // ::= v <digit> <source-name> # vendor extended operator
3076 case 'v':
3077 if (std::isdigit(look(1))) {
3078 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003079 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003080 if (SN == nullptr)
3081 return nullptr;
3082 return make<ConversionOperatorType>(SN);
3083 }
3084 return nullptr;
3085 }
3086 return nullptr;
3087}
3088
3089// <ctor-dtor-name> ::= C1 # complete object constructor
3090// ::= C2 # base object constructor
3091// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003092// extension ::= C4 # gcc old-style "[unified]" constructor
3093// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003094// ::= D0 # deleting destructor
3095// ::= D1 # complete object destructor
3096// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003097// extension ::= D4 # gcc old-style "[unified]" destructor
3098// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003099template <typename Derived, typename Alloc>
3100Node *
3101AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3102 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003103 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3104 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3105 switch (SSK) {
3106 case SpecialSubKind::string:
3107 case SpecialSubKind::istream:
3108 case SpecialSubKind::ostream:
3109 case SpecialSubKind::iostream:
3110 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003111 if (!SoFar)
3112 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003113 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003114 default:
3115 break;
3116 }
3117 }
3118
3119 if (consumeIf('C')) {
3120 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003121 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3122 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003123 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003124 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003125 ++First;
3126 if (State) State->CtorDtorConversion = true;
3127 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003128 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003129 return nullptr;
3130 }
Nico Weber29294792019-04-03 23:14:33 +00003131 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003132 }
3133
Nico Weber29294792019-04-03 23:14:33 +00003134 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3135 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003136 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003137 First += 2;
3138 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003139 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003140 }
3141
3142 return nullptr;
3143}
3144
3145// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3146// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3147//
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003148// <prefix> ::= <prefix> [L]* <unqualified-name>
Richard Smithc20d1442018-08-20 20:14:49 +00003149// ::= <template-prefix> <template-args>
3150// ::= <template-param>
3151// ::= <decltype>
3152// ::= # empty
3153// ::= <substitution>
3154// ::= <prefix> <data-member-prefix>
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003155// [*] extension
Richard Smithc20d1442018-08-20 20:14:49 +00003156//
3157// <data-member-prefix> := <member source-name> [<template-args>] M
3158//
3159// <template-prefix> ::= <prefix> <template unqualified-name>
3160// ::= <template-param>
3161// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003162template <typename Derived, typename Alloc>
3163Node *
3164AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003165 if (!consumeIf('N'))
3166 return nullptr;
3167
3168 Qualifiers CVTmp = parseCVQualifiers();
3169 if (State) State->CVQualifiers = CVTmp;
3170
3171 if (consumeIf('O')) {
3172 if (State) State->ReferenceQualifier = FrefQualRValue;
3173 } else if (consumeIf('R')) {
3174 if (State) State->ReferenceQualifier = FrefQualLValue;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003175 } else {
Richard Smithc20d1442018-08-20 20:14:49 +00003176 if (State) State->ReferenceQualifier = FrefQualNone;
Richard Smithb485b352018-08-24 23:30:26 +00003177 }
Richard Smithc20d1442018-08-20 20:14:49 +00003178
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003179 Node *SoFar = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003180 while (!consumeIf('E')) {
3181 consumeIf('L'); // extension
3182
Richard Smithc20d1442018-08-20 20:14:49 +00003183 if (consumeIf('M')) {
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003184 // <data-member-prefix> := <member source-name> [<template-args>] M
Richard Smithc20d1442018-08-20 20:14:49 +00003185 if (SoFar == nullptr)
3186 return nullptr;
3187 continue;
3188 }
3189
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003190 if (State)
3191 // Only set end-with-template on the case that does that.
3192 State->EndsWithTemplateArgs = false;
3193
Richard Smithc20d1442018-08-20 20:14:49 +00003194 if (look() == 'T') {
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003195 // ::= <template-param>
3196 if (SoFar != nullptr)
3197 return nullptr; // Cannot have a prefix.
3198 SoFar = getDerived().parseTemplateParam();
3199 } else if (look() == 'I') {
3200 // ::= <template-prefix> <template-args>
3201 if (SoFar == nullptr)
3202 return nullptr; // Must have a prefix.
Pavel Labathba825192018-10-16 14:29:14 +00003203 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003204 if (TA == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003205 return nullptr;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003206 if (SoFar->getKind() == Node::KNameWithTemplateArgs)
3207 // Semantically <template-args> <template-args> cannot be generated by a
3208 // C++ entity. There will always be [something like] a name between
3209 // them.
3210 return nullptr;
3211 if (State)
3212 State->EndsWithTemplateArgs = true;
Richard Smithc20d1442018-08-20 20:14:49 +00003213 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003214 } else if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
3215 // ::= <decltype>
3216 if (SoFar != nullptr)
3217 return nullptr; // Cannot have a prefix.
3218 SoFar = getDerived().parseDecltype();
3219 } else if (look() == 'S') {
3220 // ::= <substitution>
3221 if (SoFar != nullptr)
3222 return nullptr; // Cannot have a prefix.
3223 if (look(1) == 't') {
3224 // parseSubstition does not handle 'St'.
3225 First += 2;
3226 SoFar = make<NameType>("std");
3227 } else {
3228 SoFar = getDerived().parseSubstitution();
3229 }
Richard Smithc20d1442018-08-20 20:14:49 +00003230 if (SoFar == nullptr)
3231 return nullptr;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003232 continue; // Do not push a new substitution.
3233 } else {
3234 Node *N = nullptr;
3235 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
3236 // An <unqualified-name> that's actually a <ctor-dtor-name>.
3237 if (SoFar == nullptr)
3238 return nullptr;
3239 N = getDerived().parseCtorDtorName(SoFar, State);
3240 if (N != nullptr)
3241 N = getDerived().parseAbiTags(N);
3242 } else {
3243 // ::= <prefix> <unqualified-name>
3244 N = getDerived().parseUnqualifiedName(State);
3245 }
3246 if (N == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003247 return nullptr;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003248 if (SoFar)
3249 SoFar = make<NestedName>(SoFar, N);
3250 else
3251 SoFar = N;
Richard Smithc20d1442018-08-20 20:14:49 +00003252 }
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003253 if (SoFar == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003254 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003255 Subs.push_back(SoFar);
3256 }
3257
3258 if (SoFar == nullptr || Subs.empty())
3259 return nullptr;
3260
3261 Subs.pop_back();
3262 return SoFar;
3263}
3264
3265// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003266template <typename Derived, typename Alloc>
3267Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3268 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003269 if (SN == nullptr)
3270 return nullptr;
3271 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003272 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003273 if (TA == nullptr)
3274 return nullptr;
3275 return make<NameWithTemplateArgs>(SN, TA);
3276 }
3277 return SN;
3278}
3279
3280// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3281// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003282template <typename Derived, typename Alloc>
3283Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003284 Node *Result;
3285 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003286 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003287 else
Pavel Labathba825192018-10-16 14:29:14 +00003288 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003289 if (Result == nullptr)
3290 return nullptr;
3291 return make<DtorName>(Result);
3292}
3293
3294// <unresolved-type> ::= <template-param>
3295// ::= <decltype>
3296// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003297template <typename Derived, typename Alloc>
3298Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003299 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003300 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003301 if (TP == nullptr)
3302 return nullptr;
3303 Subs.push_back(TP);
3304 return TP;
3305 }
3306 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003307 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003308 if (DT == nullptr)
3309 return nullptr;
3310 Subs.push_back(DT);
3311 return DT;
3312 }
Pavel Labathba825192018-10-16 14:29:14 +00003313 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003314}
3315
3316// <base-unresolved-name> ::= <simple-id> # unresolved name
3317// extension ::= <operator-name> # unresolved operator-function-id
3318// extension ::= <operator-name> <template-args> # unresolved operator template-id
3319// ::= on <operator-name> # unresolved operator-function-id
3320// ::= on <operator-name> <template-args> # unresolved operator template-id
3321// ::= dn <destructor-name> # destructor or pseudo-destructor;
3322// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003323template <typename Derived, typename Alloc>
3324Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003325 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003326 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003327
3328 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003329 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003330
3331 consumeIf("on");
3332
Pavel Labathba825192018-10-16 14:29:14 +00003333 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003334 if (Oper == nullptr)
3335 return nullptr;
3336 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003337 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003338 if (TA == nullptr)
3339 return nullptr;
3340 return make<NameWithTemplateArgs>(Oper, TA);
3341 }
3342 return Oper;
3343}
3344
3345// <unresolved-name>
3346// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3347// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3348// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3349// # A::x, N::y, A<T>::z; "gs" means leading "::"
Nathan Sidwell77c52e22022-01-28 11:59:03 -08003350// [gs] has been parsed by caller.
Richard Smithc20d1442018-08-20 20:14:49 +00003351// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3352// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3353// # T::N::x /decltype(p)::N::x
3354// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3355//
3356// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003357template <typename Derived, typename Alloc>
Nathan Sidwell77c52e22022-01-28 11:59:03 -08003358Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName(bool Global) {
Richard Smithc20d1442018-08-20 20:14:49 +00003359 Node *SoFar = nullptr;
3360
3361 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3362 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3363 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003364 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003365 if (SoFar == nullptr)
3366 return nullptr;
3367
3368 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003369 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003370 if (TA == nullptr)
3371 return nullptr;
3372 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003373 if (!SoFar)
3374 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003375 }
3376
3377 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003378 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003379 if (Qual == nullptr)
3380 return nullptr;
3381 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003382 if (!SoFar)
3383 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003384 }
3385
Pavel Labathba825192018-10-16 14:29:14 +00003386 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003387 if (Base == nullptr)
3388 return nullptr;
3389 return make<QualifiedName>(SoFar, Base);
3390 }
3391
Richard Smithc20d1442018-08-20 20:14:49 +00003392 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3393 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003394 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003395 if (SoFar == nullptr)
3396 return nullptr;
3397 if (Global)
3398 SoFar = make<GlobalQualifiedName>(SoFar);
3399 return SoFar;
3400 }
3401
3402 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3403 if (std::isdigit(look())) {
3404 do {
Pavel Labathba825192018-10-16 14:29:14 +00003405 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003406 if (Qual == nullptr)
3407 return nullptr;
3408 if (SoFar)
3409 SoFar = make<QualifiedName>(SoFar, Qual);
3410 else if (Global)
3411 SoFar = make<GlobalQualifiedName>(Qual);
3412 else
3413 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003414 if (!SoFar)
3415 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003416 } while (!consumeIf('E'));
3417 }
3418 // sr <unresolved-type> <base-unresolved-name>
3419 // sr <unresolved-type> <template-args> <base-unresolved-name>
3420 else {
Pavel Labathba825192018-10-16 14:29:14 +00003421 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003422 if (SoFar == nullptr)
3423 return nullptr;
3424
3425 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003426 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003427 if (TA == nullptr)
3428 return nullptr;
3429 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003430 if (!SoFar)
3431 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003432 }
3433 }
3434
3435 assert(SoFar != nullptr);
3436
Pavel Labathba825192018-10-16 14:29:14 +00003437 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003438 if (Base == nullptr)
3439 return nullptr;
3440 return make<QualifiedName>(SoFar, Base);
3441}
3442
3443// <abi-tags> ::= <abi-tag> [<abi-tags>]
3444// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003445template <typename Derived, typename Alloc>
3446Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003447 while (consumeIf('B')) {
3448 StringView SN = parseBareSourceName();
3449 if (SN.empty())
3450 return nullptr;
3451 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003452 if (!N)
3453 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003454 }
3455 return N;
3456}
3457
3458// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003459template <typename Alloc, typename Derived>
3460StringView
3461AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003462 const char *Tmp = First;
3463 if (AllowNegative)
3464 consumeIf('n');
3465 if (numLeft() == 0 || !std::isdigit(*First))
3466 return StringView();
3467 while (numLeft() != 0 && std::isdigit(*First))
3468 ++First;
3469 return StringView(Tmp, First);
3470}
3471
3472// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003473template <typename Alloc, typename Derived>
3474bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003475 *Out = 0;
3476 if (look() < '0' || look() > '9')
3477 return true;
3478 while (look() >= '0' && look() <= '9') {
3479 *Out *= 10;
3480 *Out += static_cast<size_t>(consume() - '0');
3481 }
3482 return false;
3483}
3484
Pavel Labathba825192018-10-16 14:29:14 +00003485template <typename Alloc, typename Derived>
3486StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003487 size_t Int = 0;
3488 if (parsePositiveInteger(&Int) || numLeft() < Int)
3489 return StringView();
3490 StringView R(First, First + Int);
3491 First += Int;
3492 return R;
3493}
3494
3495// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3496//
3497// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3498// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3499// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3500//
3501// <ref-qualifier> ::= R # & ref-qualifier
3502// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003503template <typename Derived, typename Alloc>
3504Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003505 Qualifiers CVQuals = parseCVQualifiers();
3506
3507 Node *ExceptionSpec = nullptr;
3508 if (consumeIf("Do")) {
3509 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003510 if (!ExceptionSpec)
3511 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003512 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003513 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003514 if (E == nullptr || !consumeIf('E'))
3515 return nullptr;
3516 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003517 if (!ExceptionSpec)
3518 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003519 } else if (consumeIf("Dw")) {
3520 size_t SpecsBegin = Names.size();
3521 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003522 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003523 if (T == nullptr)
3524 return nullptr;
3525 Names.push_back(T);
3526 }
3527 ExceptionSpec =
3528 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003529 if (!ExceptionSpec)
3530 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003531 }
3532
3533 consumeIf("Dx"); // transaction safe
3534
3535 if (!consumeIf('F'))
3536 return nullptr;
3537 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003538 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003539 if (ReturnType == nullptr)
3540 return nullptr;
3541
3542 FunctionRefQual ReferenceQualifier = FrefQualNone;
3543 size_t ParamsBegin = Names.size();
3544 while (true) {
3545 if (consumeIf('E'))
3546 break;
3547 if (consumeIf('v'))
3548 continue;
3549 if (consumeIf("RE")) {
3550 ReferenceQualifier = FrefQualLValue;
3551 break;
3552 }
3553 if (consumeIf("OE")) {
3554 ReferenceQualifier = FrefQualRValue;
3555 break;
3556 }
Pavel Labathba825192018-10-16 14:29:14 +00003557 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003558 if (T == nullptr)
3559 return nullptr;
3560 Names.push_back(T);
3561 }
3562
3563 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3564 return make<FunctionType>(ReturnType, Params, CVQuals,
3565 ReferenceQualifier, ExceptionSpec);
3566}
3567
3568// extension:
3569// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3570// ::= Dv [<dimension expression>] _ <element type>
3571// <extended element type> ::= <element type>
3572// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003573template <typename Derived, typename Alloc>
3574Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003575 if (!consumeIf("Dv"))
3576 return nullptr;
3577 if (look() >= '1' && look() <= '9') {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003578 Node *DimensionNumber = make<NameType>(parseNumber());
3579 if (!DimensionNumber)
3580 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003581 if (!consumeIf('_'))
3582 return nullptr;
3583 if (consumeIf('p'))
3584 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003585 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003586 if (ElemType == nullptr)
3587 return nullptr;
3588 return make<VectorType>(ElemType, DimensionNumber);
3589 }
3590
3591 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003592 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003593 if (!DimExpr)
3594 return nullptr;
3595 if (!consumeIf('_'))
3596 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003597 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003598 if (!ElemType)
3599 return nullptr;
3600 return make<VectorType>(ElemType, DimExpr);
3601 }
Pavel Labathba825192018-10-16 14:29:14 +00003602 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003603 if (!ElemType)
3604 return nullptr;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003605 return make<VectorType>(ElemType, /*Dimension=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003606}
3607
3608// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3609// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003610template <typename Derived, typename Alloc>
3611Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003612 if (!consumeIf('D'))
3613 return nullptr;
3614 if (!consumeIf('t') && !consumeIf('T'))
3615 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003616 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003617 if (E == nullptr)
3618 return nullptr;
3619 if (!consumeIf('E'))
3620 return nullptr;
3621 return make<EnclosingExpr>("decltype(", E, ")");
3622}
3623
3624// <array-type> ::= A <positive dimension number> _ <element type>
3625// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003626template <typename Derived, typename Alloc>
3627Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003628 if (!consumeIf('A'))
3629 return nullptr;
3630
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003631 Node *Dimension = nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003632
Richard Smithc20d1442018-08-20 20:14:49 +00003633 if (std::isdigit(look())) {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003634 Dimension = make<NameType>(parseNumber());
3635 if (!Dimension)
3636 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003637 if (!consumeIf('_'))
3638 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003639 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003640 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003641 if (DimExpr == nullptr)
3642 return nullptr;
3643 if (!consumeIf('_'))
3644 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003645 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003646 }
3647
Pavel Labathba825192018-10-16 14:29:14 +00003648 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003649 if (Ty == nullptr)
3650 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003651 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003652}
3653
3654// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003655template <typename Derived, typename Alloc>
3656Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003657 if (!consumeIf('M'))
3658 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003659 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003660 if (ClassType == nullptr)
3661 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003662 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003663 if (MemberType == nullptr)
3664 return nullptr;
3665 return make<PointerToMemberType>(ClassType, MemberType);
3666}
3667
3668// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3669// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3670// ::= Tu <name> # dependent elaborated type specifier using 'union'
3671// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003672template <typename Derived, typename Alloc>
3673Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003674 StringView ElabSpef;
3675 if (consumeIf("Ts"))
3676 ElabSpef = "struct";
3677 else if (consumeIf("Tu"))
3678 ElabSpef = "union";
3679 else if (consumeIf("Te"))
3680 ElabSpef = "enum";
3681
Pavel Labathba825192018-10-16 14:29:14 +00003682 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003683 if (Name == nullptr)
3684 return nullptr;
3685
3686 if (!ElabSpef.empty())
3687 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3688
3689 return Name;
3690}
3691
3692// <qualified-type> ::= <qualifiers> <type>
3693// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3694// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003695template <typename Derived, typename Alloc>
3696Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003697 if (consumeIf('U')) {
3698 StringView Qual = parseBareSourceName();
3699 if (Qual.empty())
3700 return nullptr;
3701
Richard Smithc20d1442018-08-20 20:14:49 +00003702 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3703 if (Qual.startsWith("objcproto")) {
3704 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3705 StringView Proto;
3706 {
3707 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3708 SaveLast(Last, ProtoSourceName.end());
3709 Proto = parseBareSourceName();
3710 }
3711 if (Proto.empty())
3712 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003713 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003714 if (Child == nullptr)
3715 return nullptr;
3716 return make<ObjCProtoName>(Child, Proto);
3717 }
3718
Alex Orlovf50df922021-03-24 10:21:32 +04003719 Node *TA = nullptr;
3720 if (look() == 'I') {
3721 TA = getDerived().parseTemplateArgs();
3722 if (TA == nullptr)
3723 return nullptr;
3724 }
3725
Pavel Labathba825192018-10-16 14:29:14 +00003726 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003727 if (Child == nullptr)
3728 return nullptr;
Alex Orlovf50df922021-03-24 10:21:32 +04003729 return make<VendorExtQualType>(Child, Qual, TA);
Richard Smithc20d1442018-08-20 20:14:49 +00003730 }
3731
3732 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003733 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003734 if (Ty == nullptr)
3735 return nullptr;
3736 if (Quals != QualNone)
3737 Ty = make<QualType>(Ty, Quals);
3738 return Ty;
3739}
3740
3741// <type> ::= <builtin-type>
3742// ::= <qualified-type>
3743// ::= <function-type>
3744// ::= <class-enum-type>
3745// ::= <array-type>
3746// ::= <pointer-to-member-type>
3747// ::= <template-param>
3748// ::= <template-template-param> <template-args>
3749// ::= <decltype>
3750// ::= P <type> # pointer
3751// ::= R <type> # l-value reference
3752// ::= O <type> # r-value reference (C++11)
3753// ::= C <type> # complex pair (C99)
3754// ::= G <type> # imaginary (C99)
3755// ::= <substitution> # See Compression below
3756// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3757// extension ::= <vector-type> # <vector-type> starts with Dv
3758//
3759// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3760// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003761template <typename Derived, typename Alloc>
3762Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003763 Node *Result = nullptr;
3764
Richard Smithc20d1442018-08-20 20:14:49 +00003765 switch (look()) {
3766 // ::= <qualified-type>
3767 case 'r':
3768 case 'V':
3769 case 'K': {
3770 unsigned AfterQuals = 0;
3771 if (look(AfterQuals) == 'r') ++AfterQuals;
3772 if (look(AfterQuals) == 'V') ++AfterQuals;
3773 if (look(AfterQuals) == 'K') ++AfterQuals;
3774
3775 if (look(AfterQuals) == 'F' ||
3776 (look(AfterQuals) == 'D' &&
3777 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3778 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003779 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003780 break;
3781 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003782 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003783 }
3784 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003785 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003786 break;
3787 }
3788 // <builtin-type> ::= v # void
3789 case 'v':
3790 ++First;
3791 return make<NameType>("void");
3792 // ::= w # wchar_t
3793 case 'w':
3794 ++First;
3795 return make<NameType>("wchar_t");
3796 // ::= b # bool
3797 case 'b':
3798 ++First;
3799 return make<NameType>("bool");
3800 // ::= c # char
3801 case 'c':
3802 ++First;
3803 return make<NameType>("char");
3804 // ::= a # signed char
3805 case 'a':
3806 ++First;
3807 return make<NameType>("signed char");
3808 // ::= h # unsigned char
3809 case 'h':
3810 ++First;
3811 return make<NameType>("unsigned char");
3812 // ::= s # short
3813 case 's':
3814 ++First;
3815 return make<NameType>("short");
3816 // ::= t # unsigned short
3817 case 't':
3818 ++First;
3819 return make<NameType>("unsigned short");
3820 // ::= i # int
3821 case 'i':
3822 ++First;
3823 return make<NameType>("int");
3824 // ::= j # unsigned int
3825 case 'j':
3826 ++First;
3827 return make<NameType>("unsigned int");
3828 // ::= l # long
3829 case 'l':
3830 ++First;
3831 return make<NameType>("long");
3832 // ::= m # unsigned long
3833 case 'm':
3834 ++First;
3835 return make<NameType>("unsigned long");
3836 // ::= x # long long, __int64
3837 case 'x':
3838 ++First;
3839 return make<NameType>("long long");
3840 // ::= y # unsigned long long, __int64
3841 case 'y':
3842 ++First;
3843 return make<NameType>("unsigned long long");
3844 // ::= n # __int128
3845 case 'n':
3846 ++First;
3847 return make<NameType>("__int128");
3848 // ::= o # unsigned __int128
3849 case 'o':
3850 ++First;
3851 return make<NameType>("unsigned __int128");
3852 // ::= f # float
3853 case 'f':
3854 ++First;
3855 return make<NameType>("float");
3856 // ::= d # double
3857 case 'd':
3858 ++First;
3859 return make<NameType>("double");
3860 // ::= e # long double, __float80
3861 case 'e':
3862 ++First;
3863 return make<NameType>("long double");
3864 // ::= g # __float128
3865 case 'g':
3866 ++First;
3867 return make<NameType>("__float128");
3868 // ::= z # ellipsis
3869 case 'z':
3870 ++First;
3871 return make<NameType>("...");
3872
3873 // <builtin-type> ::= u <source-name> # vendor extended type
3874 case 'u': {
3875 ++First;
3876 StringView Res = parseBareSourceName();
3877 if (Res.empty())
3878 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003879 // Typically, <builtin-type>s are not considered substitution candidates,
3880 // but the exception to that exception is vendor extended types (Itanium C++
3881 // ABI 5.9.1).
3882 Result = make<NameType>(Res);
3883 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003884 }
3885 case 'D':
3886 switch (look(1)) {
3887 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3888 case 'd':
3889 First += 2;
3890 return make<NameType>("decimal64");
3891 // ::= De # IEEE 754r decimal floating point (128 bits)
3892 case 'e':
3893 First += 2;
3894 return make<NameType>("decimal128");
3895 // ::= Df # IEEE 754r decimal floating point (32 bits)
3896 case 'f':
3897 First += 2;
3898 return make<NameType>("decimal32");
3899 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3900 case 'h':
3901 First += 2;
Stuart Bradye8bf5772021-06-07 16:30:22 +01003902 return make<NameType>("half");
Pengfei Wang50e90b82021-09-23 11:02:25 +08003903 // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits)
3904 case 'F': {
3905 First += 2;
3906 Node *DimensionNumber = make<NameType>(parseNumber());
3907 if (!DimensionNumber)
3908 return nullptr;
3909 if (!consumeIf('_'))
3910 return nullptr;
3911 return make<BinaryFPType>(DimensionNumber);
3912 }
Richard Smithc20d1442018-08-20 20:14:49 +00003913 // ::= Di # char32_t
3914 case 'i':
3915 First += 2;
3916 return make<NameType>("char32_t");
3917 // ::= Ds # char16_t
3918 case 's':
3919 First += 2;
3920 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003921 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3922 case 'u':
3923 First += 2;
3924 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003925 // ::= Da # auto (in dependent new-expressions)
3926 case 'a':
3927 First += 2;
3928 return make<NameType>("auto");
3929 // ::= Dc # decltype(auto)
3930 case 'c':
3931 First += 2;
3932 return make<NameType>("decltype(auto)");
3933 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3934 case 'n':
3935 First += 2;
3936 return make<NameType>("std::nullptr_t");
3937
3938 // ::= <decltype>
3939 case 't':
3940 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003941 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003942 break;
3943 }
3944 // extension ::= <vector-type> # <vector-type> starts with Dv
3945 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003946 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003947 break;
3948 }
3949 // ::= Dp <type> # pack expansion (C++0x)
3950 case 'p': {
3951 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003952 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003953 if (!Child)
3954 return nullptr;
3955 Result = make<ParameterPackExpansion>(Child);
3956 break;
3957 }
3958 // Exception specifier on a function type.
3959 case 'o':
3960 case 'O':
3961 case 'w':
3962 // Transaction safe function type.
3963 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003964 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003965 break;
3966 }
3967 break;
3968 // ::= <function-type>
3969 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003970 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003971 break;
3972 }
3973 // ::= <array-type>
3974 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003975 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003976 break;
3977 }
3978 // ::= <pointer-to-member-type>
3979 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003980 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003981 break;
3982 }
3983 // ::= <template-param>
3984 case 'T': {
3985 // This could be an elaborate type specifier on a <class-enum-type>.
3986 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003987 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003988 break;
3989 }
3990
Pavel Labathba825192018-10-16 14:29:14 +00003991 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003992 if (Result == nullptr)
3993 return nullptr;
3994
3995 // Result could be either of:
3996 // <type> ::= <template-param>
3997 // <type> ::= <template-template-param> <template-args>
3998 //
3999 // <template-template-param> ::= <template-param>
4000 // ::= <substitution>
4001 //
4002 // If this is followed by some <template-args>, and we're permitted to
4003 // parse them, take the second production.
4004
4005 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004006 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004007 if (TA == nullptr)
4008 return nullptr;
4009 Result = make<NameWithTemplateArgs>(Result, TA);
4010 }
4011 break;
4012 }
4013 // ::= P <type> # pointer
4014 case 'P': {
4015 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004016 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004017 if (Ptr == nullptr)
4018 return nullptr;
4019 Result = make<PointerType>(Ptr);
4020 break;
4021 }
4022 // ::= R <type> # l-value reference
4023 case 'R': {
4024 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004025 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004026 if (Ref == nullptr)
4027 return nullptr;
4028 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
4029 break;
4030 }
4031 // ::= O <type> # r-value reference (C++11)
4032 case 'O': {
4033 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004034 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004035 if (Ref == nullptr)
4036 return nullptr;
4037 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
4038 break;
4039 }
4040 // ::= C <type> # complex pair (C99)
4041 case 'C': {
4042 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004043 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004044 if (P == nullptr)
4045 return nullptr;
4046 Result = make<PostfixQualifiedType>(P, " complex");
4047 break;
4048 }
4049 // ::= G <type> # imaginary (C99)
4050 case 'G': {
4051 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004052 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004053 if (P == nullptr)
4054 return P;
4055 Result = make<PostfixQualifiedType>(P, " imaginary");
4056 break;
4057 }
4058 // ::= <substitution> # See Compression below
4059 case 'S': {
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004060 if (look(1) != 't') {
4061 Result = getDerived().parseSubstitution();
4062 if (Result == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00004063 return nullptr;
4064
4065 // Sub could be either of:
4066 // <type> ::= <substitution>
4067 // <type> ::= <template-template-param> <template-args>
4068 //
4069 // <template-template-param> ::= <template-param>
4070 // ::= <substitution>
4071 //
4072 // If this is followed by some <template-args>, and we're permitted to
4073 // parse them, take the second production.
4074
4075 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004076 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004077 if (TA == nullptr)
4078 return nullptr;
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004079 Result = make<NameWithTemplateArgs>(Result, TA);
4080 } else {
4081 // If all we parsed was a substitution, don't re-insert into the
4082 // substitution table.
4083 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00004084 }
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004085 break;
Richard Smithc20d1442018-08-20 20:14:49 +00004086 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00004087 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004088 }
4089 // ::= <class-enum-type>
4090 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004091 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004092 break;
4093 }
4094 }
4095
4096 // If we parsed a type, insert it into the substitution table. Note that all
4097 // <builtin-type>s and <substitution>s have already bailed out, because they
4098 // don't get substitutions.
4099 if (Result != nullptr)
4100 Subs.push_back(Result);
4101 return Result;
4102}
4103
Pavel Labathba825192018-10-16 14:29:14 +00004104template <typename Derived, typename Alloc>
4105Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4106 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004107 if (E == nullptr)
4108 return nullptr;
4109 return make<PrefixExpr>(Kind, E);
4110}
4111
Pavel Labathba825192018-10-16 14:29:14 +00004112template <typename Derived, typename Alloc>
4113Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4114 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004115 if (LHS == nullptr)
4116 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004117 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004118 if (RHS == nullptr)
4119 return nullptr;
4120 return make<BinaryExpr>(LHS, Kind, RHS);
4121}
4122
Pavel Labathba825192018-10-16 14:29:14 +00004123template <typename Derived, typename Alloc>
4124Node *
4125AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004126 StringView Tmp = parseNumber(true);
4127 if (!Tmp.empty() && consumeIf('E'))
4128 return make<IntegerLiteral>(Lit, Tmp);
4129 return nullptr;
4130}
4131
4132// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004133template <typename Alloc, typename Derived>
4134Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004135 Qualifiers CVR = QualNone;
4136 if (consumeIf('r'))
4137 CVR |= QualRestrict;
4138 if (consumeIf('V'))
4139 CVR |= QualVolatile;
4140 if (consumeIf('K'))
4141 CVR |= QualConst;
4142 return CVR;
4143}
4144
4145// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4146// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4147// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4148// ::= 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 -04004149// ::= fpT # 'this' expression (not part of standard?)
Pavel Labathba825192018-10-16 14:29:14 +00004150template <typename Derived, typename Alloc>
4151Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Erik Pilkington91c24af2020-05-13 22:19:45 -04004152 if (consumeIf("fpT"))
4153 return make<NameType>("this");
Richard Smithc20d1442018-08-20 20:14:49 +00004154 if (consumeIf("fp")) {
4155 parseCVQualifiers();
4156 StringView Num = parseNumber();
4157 if (!consumeIf('_'))
4158 return nullptr;
4159 return make<FunctionParam>(Num);
4160 }
4161 if (consumeIf("fL")) {
4162 if (parseNumber().empty())
4163 return nullptr;
4164 if (!consumeIf('p'))
4165 return nullptr;
4166 parseCVQualifiers();
4167 StringView Num = parseNumber();
4168 if (!consumeIf('_'))
4169 return nullptr;
4170 return make<FunctionParam>(Num);
4171 }
4172 return nullptr;
4173}
4174
4175// [gs] nw <expression>* _ <type> E # new (expr-list) type
4176// [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4177// [gs] na <expression>* _ <type> E # new[] (expr-list) type
4178// [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4179// <initializer> ::= pi <expression>* E # parenthesized initialization
Pavel Labathba825192018-10-16 14:29:14 +00004180template <typename Derived, typename Alloc>
4181Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004182 bool Global = consumeIf("gs");
4183 bool IsArray = look(1) == 'a';
4184 if (!consumeIf("nw") && !consumeIf("na"))
4185 return nullptr;
4186 size_t Exprs = Names.size();
4187 while (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00004188 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004189 if (Ex == nullptr)
4190 return nullptr;
4191 Names.push_back(Ex);
4192 }
4193 NodeArray ExprList = popTrailingNodeArray(Exprs);
Pavel Labathba825192018-10-16 14:29:14 +00004194 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004195 if (Ty == nullptr)
4196 return Ty;
4197 if (consumeIf("pi")) {
4198 size_t InitsBegin = Names.size();
4199 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004200 Node *Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004201 if (Init == nullptr)
4202 return Init;
4203 Names.push_back(Init);
4204 }
4205 NodeArray Inits = popTrailingNodeArray(InitsBegin);
4206 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
4207 } else if (!consumeIf('E'))
4208 return nullptr;
4209 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
4210}
4211
4212// cv <type> <expression> # conversion with one argument
4213// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004214template <typename Derived, typename Alloc>
4215Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004216 if (!consumeIf("cv"))
4217 return nullptr;
4218 Node *Ty;
4219 {
4220 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004221 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004222 }
4223
4224 if (Ty == nullptr)
4225 return nullptr;
4226
4227 if (consumeIf('_')) {
4228 size_t ExprsBegin = Names.size();
4229 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004230 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004231 if (E == nullptr)
4232 return E;
4233 Names.push_back(E);
4234 }
4235 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4236 return make<ConversionExpr>(Ty, Exprs);
4237 }
4238
Pavel Labathba825192018-10-16 14:29:14 +00004239 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004240 if (E[0] == nullptr)
4241 return nullptr;
4242 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4243}
4244
4245// <expr-primary> ::= L <type> <value number> E # integer literal
4246// ::= L <type> <value float> E # floating literal
4247// ::= L <string type> E # string literal
4248// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004249// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004250// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4251// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004252template <typename Derived, typename Alloc>
4253Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004254 if (!consumeIf('L'))
4255 return nullptr;
4256 switch (look()) {
4257 case 'w':
4258 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004259 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004260 case 'b':
4261 if (consumeIf("b0E"))
4262 return make<BoolExpr>(0);
4263 if (consumeIf("b1E"))
4264 return make<BoolExpr>(1);
4265 return nullptr;
4266 case 'c':
4267 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004268 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004269 case 'a':
4270 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004271 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004272 case 'h':
4273 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004274 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004275 case 's':
4276 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004277 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004278 case 't':
4279 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004280 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004281 case 'i':
4282 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004283 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004284 case 'j':
4285 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004286 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004287 case 'l':
4288 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004289 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004290 case 'm':
4291 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004292 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004293 case 'x':
4294 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004295 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004296 case 'y':
4297 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004298 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004299 case 'n':
4300 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004301 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004302 case 'o':
4303 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004304 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004305 case 'f':
4306 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004307 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004308 case 'd':
4309 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004310 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004311 case 'e':
4312 ++First;
Xing Xue3dc5e082020-04-15 09:59:06 -04004313#if defined(__powerpc__) || defined(__s390__)
4314 // Handle cases where long doubles encoded with e have the same size
4315 // and representation as doubles.
4316 return getDerived().template parseFloatingLiteral<double>();
4317#else
Pavel Labathba825192018-10-16 14:29:14 +00004318 return getDerived().template parseFloatingLiteral<long double>();
Xing Xue3dc5e082020-04-15 09:59:06 -04004319#endif
Richard Smithc20d1442018-08-20 20:14:49 +00004320 case '_':
4321 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004322 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004323 if (R != nullptr && consumeIf('E'))
4324 return R;
4325 }
4326 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004327 case 'A': {
4328 Node *T = getDerived().parseType();
4329 if (T == nullptr)
4330 return nullptr;
4331 // FIXME: We need to include the string contents in the mangling.
4332 if (consumeIf('E'))
4333 return make<StringLiteral>(T);
4334 return nullptr;
4335 }
4336 case 'D':
4337 if (consumeIf("DnE"))
4338 return make<NameType>("nullptr");
4339 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004340 case 'T':
4341 // Invalid mangled name per
4342 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4343 return nullptr;
Richard Smithfb917462019-09-09 22:26:04 +00004344 case 'U': {
4345 // FIXME: Should we support LUb... for block literals?
4346 if (look(1) != 'l')
4347 return nullptr;
4348 Node *T = parseUnnamedTypeName(nullptr);
4349 if (!T || !consumeIf('E'))
4350 return nullptr;
4351 return make<LambdaExpr>(T);
4352 }
Richard Smithc20d1442018-08-20 20:14:49 +00004353 default: {
4354 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004355 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004356 if (T == nullptr)
4357 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004358 StringView N = parseNumber(/*AllowNegative=*/true);
Richard Smithfb917462019-09-09 22:26:04 +00004359 if (N.empty())
4360 return nullptr;
4361 if (!consumeIf('E'))
4362 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004363 return make<EnumLiteral>(T, N);
Richard Smithc20d1442018-08-20 20:14:49 +00004364 }
4365 }
4366}
4367
4368// <braced-expression> ::= <expression>
4369// ::= di <field source-name> <braced-expression> # .name = expr
4370// ::= dx <index expression> <braced-expression> # [expr] = expr
4371// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004372template <typename Derived, typename Alloc>
4373Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004374 if (look() == 'd') {
4375 switch (look(1)) {
4376 case 'i': {
4377 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004378 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004379 if (Field == nullptr)
4380 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004381 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004382 if (Init == nullptr)
4383 return nullptr;
4384 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4385 }
4386 case 'x': {
4387 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004388 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004389 if (Index == nullptr)
4390 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004391 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004392 if (Init == nullptr)
4393 return nullptr;
4394 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4395 }
4396 case 'X': {
4397 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004398 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004399 if (RangeBegin == nullptr)
4400 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004401 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004402 if (RangeEnd == nullptr)
4403 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004404 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004405 if (Init == nullptr)
4406 return nullptr;
4407 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4408 }
4409 }
4410 }
Pavel Labathba825192018-10-16 14:29:14 +00004411 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004412}
4413
4414// (not yet in the spec)
4415// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4416// ::= fR <binary-operator-name> <expression> <expression>
4417// ::= fl <binary-operator-name> <expression>
4418// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004419template <typename Derived, typename Alloc>
4420Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004421 if (!consumeIf('f'))
4422 return nullptr;
4423
4424 char FoldKind = look();
4425 bool IsLeftFold, HasInitializer;
4426 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4427 if (FoldKind == 'l' || FoldKind == 'L')
4428 IsLeftFold = true;
4429 else if (FoldKind == 'r' || FoldKind == 'R')
4430 IsLeftFold = false;
4431 else
4432 return nullptr;
4433 ++First;
4434
4435 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4436 StringView OperatorName;
4437 if (consumeIf("aa")) OperatorName = "&&";
4438 else if (consumeIf("an")) OperatorName = "&";
4439 else if (consumeIf("aN")) OperatorName = "&=";
4440 else if (consumeIf("aS")) OperatorName = "=";
4441 else if (consumeIf("cm")) OperatorName = ",";
4442 else if (consumeIf("ds")) OperatorName = ".*";
4443 else if (consumeIf("dv")) OperatorName = "/";
4444 else if (consumeIf("dV")) OperatorName = "/=";
4445 else if (consumeIf("eo")) OperatorName = "^";
4446 else if (consumeIf("eO")) OperatorName = "^=";
4447 else if (consumeIf("eq")) OperatorName = "==";
4448 else if (consumeIf("ge")) OperatorName = ">=";
4449 else if (consumeIf("gt")) OperatorName = ">";
4450 else if (consumeIf("le")) OperatorName = "<=";
4451 else if (consumeIf("ls")) OperatorName = "<<";
4452 else if (consumeIf("lS")) OperatorName = "<<=";
4453 else if (consumeIf("lt")) OperatorName = "<";
4454 else if (consumeIf("mi")) OperatorName = "-";
4455 else if (consumeIf("mI")) OperatorName = "-=";
4456 else if (consumeIf("ml")) OperatorName = "*";
4457 else if (consumeIf("mL")) OperatorName = "*=";
4458 else if (consumeIf("ne")) OperatorName = "!=";
4459 else if (consumeIf("oo")) OperatorName = "||";
4460 else if (consumeIf("or")) OperatorName = "|";
4461 else if (consumeIf("oR")) OperatorName = "|=";
4462 else if (consumeIf("pl")) OperatorName = "+";
4463 else if (consumeIf("pL")) OperatorName = "+=";
4464 else if (consumeIf("rm")) OperatorName = "%";
4465 else if (consumeIf("rM")) OperatorName = "%=";
4466 else if (consumeIf("rs")) OperatorName = ">>";
4467 else if (consumeIf("rS")) OperatorName = ">>=";
4468 else return nullptr;
4469
Pavel Labathba825192018-10-16 14:29:14 +00004470 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004471 if (Pack == nullptr)
4472 return nullptr;
4473 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004474 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004475 if (Init == nullptr)
4476 return nullptr;
4477 }
4478
4479 if (IsLeftFold && Init)
4480 std::swap(Pack, Init);
4481
4482 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4483}
4484
Richard Smith1865d2f2020-10-22 19:29:36 -07004485// <expression> ::= mc <parameter type> <expr> [<offset number>] E
4486//
4487// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4488template <typename Derived, typename Alloc>
4489Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() {
4490 Node *Ty = getDerived().parseType();
4491 if (!Ty)
4492 return nullptr;
4493 Node *Expr = getDerived().parseExpr();
4494 if (!Expr)
4495 return nullptr;
4496 StringView Offset = getDerived().parseNumber(true);
4497 if (!consumeIf('E'))
4498 return nullptr;
4499 return make<PointerToMemberConversionExpr>(Ty, Expr, Offset);
4500}
4501
4502// <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
4503// <union-selector> ::= _ [<number>]
4504//
4505// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4506template <typename Derived, typename Alloc>
4507Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
4508 Node *Ty = getDerived().parseType();
4509 if (!Ty)
4510 return nullptr;
4511 Node *Expr = getDerived().parseExpr();
4512 if (!Expr)
4513 return nullptr;
4514 StringView Offset = getDerived().parseNumber(true);
4515 size_t SelectorsBegin = Names.size();
4516 while (consumeIf('_')) {
4517 Node *Selector = make<NameType>(parseNumber());
4518 if (!Selector)
4519 return nullptr;
4520 Names.push_back(Selector);
4521 }
4522 bool OnePastTheEnd = consumeIf('p');
4523 if (!consumeIf('E'))
4524 return nullptr;
4525 return make<SubobjectExpr>(
4526 Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
4527}
4528
Richard Smithc20d1442018-08-20 20:14:49 +00004529// <expression> ::= <unary operator-name> <expression>
4530// ::= <binary operator-name> <expression> <expression>
4531// ::= <ternary operator-name> <expression> <expression> <expression>
4532// ::= cl <expression>+ E # call
4533// ::= cv <type> <expression> # conversion with one argument
4534// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4535// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4536// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4537// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4538// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4539// ::= [gs] dl <expression> # delete expression
4540// ::= [gs] da <expression> # delete[] expression
4541// ::= pp_ <expression> # prefix ++
4542// ::= mm_ <expression> # prefix --
4543// ::= ti <type> # typeid (type)
4544// ::= te <expression> # typeid (expression)
4545// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4546// ::= sc <type> <expression> # static_cast<type> (expression)
4547// ::= cc <type> <expression> # const_cast<type> (expression)
4548// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4549// ::= st <type> # sizeof (a type)
4550// ::= sz <expression> # sizeof (an expression)
4551// ::= at <type> # alignof (a type)
4552// ::= az <expression> # alignof (an expression)
4553// ::= nx <expression> # noexcept (expression)
4554// ::= <template-param>
4555// ::= <function-param>
4556// ::= dt <expression> <unresolved-name> # expr.name
4557// ::= pt <expression> <unresolved-name> # expr->name
4558// ::= ds <expression> <expression> # expr.*expr
4559// ::= sZ <template-param> # size of a parameter pack
4560// ::= sZ <function-param> # size of a function parameter pack
4561// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4562// ::= sp <expression> # pack expansion
4563// ::= tw <expression> # throw expression
4564// ::= tr # throw with no operand (rethrow)
4565// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4566// # freestanding dependent name (e.g., T::x),
4567// # objectless nonstatic member reference
4568// ::= fL <binary-operator-name> <expression> <expression>
4569// ::= fR <binary-operator-name> <expression> <expression>
4570// ::= fl <binary-operator-name> <expression>
4571// ::= fr <binary-operator-name> <expression>
4572// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004573template <typename Derived, typename Alloc>
4574Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004575 bool Global = consumeIf("gs");
4576 if (numLeft() < 2)
4577 return nullptr;
4578
4579 switch (*First) {
4580 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004581 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004582 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004583 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004584 case 'f': {
4585 // Disambiguate a fold expression from a <function-param>.
4586 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004587 return getDerived().parseFunctionParam();
4588 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004589 }
4590 case 'a':
4591 switch (First[1]) {
4592 case 'a':
4593 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004594 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004595 case 'd':
4596 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004597 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004598 case 'n':
4599 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004600 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004601 case 'N':
4602 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004603 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004604 case 'S':
4605 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004606 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004607 case 't': {
4608 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004609 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004610 if (Ty == nullptr)
4611 return nullptr;
4612 return make<EnclosingExpr>("alignof (", Ty, ")");
4613 }
4614 case 'z': {
4615 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004616 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004617 if (Ty == nullptr)
4618 return nullptr;
4619 return make<EnclosingExpr>("alignof (", Ty, ")");
4620 }
4621 }
4622 return nullptr;
4623 case 'c':
4624 switch (First[1]) {
4625 // cc <type> <expression> # const_cast<type>(expression)
4626 case 'c': {
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 Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004631 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004632 if (Ex == nullptr)
4633 return Ex;
4634 return make<CastExpr>("const_cast", Ty, Ex);
4635 }
4636 // cl <expression>+ E # call
4637 case 'l': {
4638 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004639 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004640 if (Callee == nullptr)
4641 return Callee;
4642 size_t ExprsBegin = Names.size();
4643 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004644 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004645 if (E == nullptr)
4646 return E;
4647 Names.push_back(E);
4648 }
4649 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4650 }
4651 case 'm':
4652 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004653 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004654 case 'o':
4655 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004656 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004657 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004658 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004659 }
4660 return nullptr;
4661 case 'd':
4662 switch (First[1]) {
4663 case 'a': {
4664 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004665 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004666 if (Ex == nullptr)
4667 return Ex;
4668 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4669 }
4670 case 'c': {
4671 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004672 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004673 if (T == nullptr)
4674 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004675 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004676 if (Ex == nullptr)
4677 return Ex;
4678 return make<CastExpr>("dynamic_cast", T, Ex);
4679 }
4680 case 'e':
4681 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004682 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004683 case 'l': {
4684 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004685 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004686 if (E == nullptr)
4687 return E;
4688 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4689 }
4690 case 'n':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08004691 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004692 case 's': {
4693 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004694 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004695 if (LHS == nullptr)
4696 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004697 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004698 if (RHS == nullptr)
4699 return nullptr;
4700 return make<MemberExpr>(LHS, ".*", RHS);
4701 }
4702 case 't': {
4703 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004704 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004705 if (LHS == nullptr)
4706 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004707 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004708 if (RHS == nullptr)
4709 return nullptr;
4710 return make<MemberExpr>(LHS, ".", RHS);
4711 }
4712 case 'v':
4713 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004714 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004715 case 'V':
4716 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004717 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004718 }
4719 return nullptr;
4720 case 'e':
4721 switch (First[1]) {
4722 case 'o':
4723 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004724 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004725 case 'O':
4726 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004727 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004728 case 'q':
4729 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004730 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004731 }
4732 return nullptr;
4733 case 'g':
4734 switch (First[1]) {
4735 case 'e':
4736 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004737 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004738 case 't':
4739 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004740 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004741 }
4742 return nullptr;
4743 case 'i':
4744 switch (First[1]) {
4745 case 'x': {
4746 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004747 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004748 if (Base == nullptr)
4749 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004750 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004751 if (Index == nullptr)
4752 return Index;
4753 return make<ArraySubscriptExpr>(Base, Index);
4754 }
4755 case 'l': {
4756 First += 2;
4757 size_t InitsBegin = Names.size();
4758 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004759 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004760 if (E == nullptr)
4761 return nullptr;
4762 Names.push_back(E);
4763 }
4764 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4765 }
4766 }
4767 return nullptr;
4768 case 'l':
4769 switch (First[1]) {
4770 case 'e':
4771 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004772 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004773 case 's':
4774 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004775 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004776 case 'S':
4777 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004778 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004779 case 't':
4780 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004781 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004782 }
4783 return nullptr;
4784 case 'm':
4785 switch (First[1]) {
Richard Smith1865d2f2020-10-22 19:29:36 -07004786 case 'c':
4787 First += 2;
4788 return parsePointerToMemberConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004789 case 'i':
4790 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004791 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004792 case 'I':
4793 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004794 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004795 case 'l':
4796 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004797 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004798 case 'L':
4799 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004800 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004801 case 'm':
4802 First += 2;
4803 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004804 return getDerived().parsePrefixExpr("--");
4805 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004806 if (Ex == nullptr)
4807 return nullptr;
4808 return make<PostfixExpr>(Ex, "--");
4809 }
4810 return nullptr;
4811 case 'n':
4812 switch (First[1]) {
4813 case 'a':
4814 case 'w':
Pavel Labathba825192018-10-16 14:29:14 +00004815 return getDerived().parseNewExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004816 case 'e':
4817 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004818 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004819 case 'g':
4820 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004821 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004822 case 't':
4823 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004824 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004825 case 'x':
4826 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004827 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004828 if (Ex == nullptr)
4829 return Ex;
4830 return make<EnclosingExpr>("noexcept (", Ex, ")");
4831 }
4832 return nullptr;
4833 case 'o':
4834 switch (First[1]) {
4835 case 'n':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08004836 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004837 case 'o':
4838 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004839 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004840 case 'r':
4841 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004842 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004843 case 'R':
4844 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004845 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004846 }
4847 return nullptr;
4848 case 'p':
4849 switch (First[1]) {
4850 case 'm':
4851 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004852 return getDerived().parseBinaryExpr("->*");
Richard Smithc20d1442018-08-20 20:14:49 +00004853 case 'l':
4854 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004855 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004856 case 'L':
4857 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004858 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004859 case 'p': {
4860 First += 2;
4861 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004862 return getDerived().parsePrefixExpr("++");
4863 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004864 if (Ex == nullptr)
4865 return Ex;
4866 return make<PostfixExpr>(Ex, "++");
4867 }
4868 case 's':
4869 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004870 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004871 case 't': {
4872 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004873 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004874 if (L == nullptr)
4875 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004876 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004877 if (R == nullptr)
4878 return nullptr;
4879 return make<MemberExpr>(L, "->", R);
4880 }
4881 }
4882 return nullptr;
4883 case 'q':
4884 if (First[1] == 'u') {
4885 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004886 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004887 if (Cond == nullptr)
4888 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004889 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004890 if (LHS == nullptr)
4891 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004892 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004893 if (RHS == nullptr)
4894 return nullptr;
4895 return make<ConditionalExpr>(Cond, LHS, RHS);
4896 }
4897 return nullptr;
4898 case 'r':
4899 switch (First[1]) {
4900 case 'c': {
4901 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004902 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004903 if (T == nullptr)
4904 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004905 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004906 if (Ex == nullptr)
4907 return Ex;
4908 return make<CastExpr>("reinterpret_cast", T, Ex);
4909 }
4910 case 'm':
4911 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004912 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004913 case 'M':
4914 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004915 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004916 case 's':
4917 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004918 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004919 case 'S':
4920 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004921 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004922 }
4923 return nullptr;
4924 case 's':
4925 switch (First[1]) {
4926 case 'c': {
4927 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004928 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004929 if (T == nullptr)
4930 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004931 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004932 if (Ex == nullptr)
4933 return Ex;
4934 return make<CastExpr>("static_cast", T, Ex);
4935 }
Richard Smith1865d2f2020-10-22 19:29:36 -07004936 case 'o':
4937 First += 2;
4938 return parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004939 case 'p': {
4940 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004941 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004942 if (Child == nullptr)
4943 return nullptr;
4944 return make<ParameterPackExpansion>(Child);
4945 }
4946 case 'r':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08004947 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004948 case 't': {
4949 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004950 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004951 if (Ty == nullptr)
4952 return Ty;
4953 return make<EnclosingExpr>("sizeof (", Ty, ")");
4954 }
4955 case 'z': {
4956 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004957 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004958 if (Ex == nullptr)
4959 return Ex;
4960 return make<EnclosingExpr>("sizeof (", Ex, ")");
4961 }
4962 case 'Z':
4963 First += 2;
4964 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004965 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004966 if (R == nullptr)
4967 return nullptr;
4968 return make<SizeofParamPackExpr>(R);
4969 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004970 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004971 if (FP == nullptr)
4972 return nullptr;
4973 return make<EnclosingExpr>("sizeof... (", FP, ")");
4974 }
4975 return nullptr;
4976 case 'P': {
4977 First += 2;
4978 size_t ArgsBegin = Names.size();
4979 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004980 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004981 if (Arg == nullptr)
4982 return nullptr;
4983 Names.push_back(Arg);
4984 }
Richard Smithb485b352018-08-24 23:30:26 +00004985 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4986 if (!Pack)
4987 return nullptr;
4988 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00004989 }
4990 }
4991 return nullptr;
4992 case 't':
4993 switch (First[1]) {
4994 case 'e': {
4995 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004996 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004997 if (Ex == nullptr)
4998 return Ex;
4999 return make<EnclosingExpr>("typeid (", Ex, ")");
5000 }
5001 case 'i': {
5002 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005003 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005004 if (Ty == nullptr)
5005 return Ty;
5006 return make<EnclosingExpr>("typeid (", Ty, ")");
5007 }
5008 case 'l': {
5009 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005010 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005011 if (Ty == nullptr)
5012 return nullptr;
5013 size_t InitsBegin = Names.size();
5014 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005015 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005016 if (E == nullptr)
5017 return nullptr;
5018 Names.push_back(E);
5019 }
5020 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
5021 }
5022 case 'r':
5023 First += 2;
5024 return make<NameType>("throw");
5025 case 'w': {
5026 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005027 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005028 if (Ex == nullptr)
5029 return nullptr;
5030 return make<ThrowExpr>(Ex);
5031 }
5032 }
5033 return nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05005034 case 'u': {
5035 ++First;
5036 Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
5037 if (!Name)
5038 return nullptr;
5039 // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
5040 // standard encoding expects a <template-arg>, and would be otherwise be
5041 // interpreted as <type> node 'short' or 'ellipsis'. However, neither
5042 // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
5043 // actual conflict here.
5044 if (Name->getBaseName() == "__uuidof") {
5045 if (numLeft() < 2)
5046 return nullptr;
5047 if (*First == 't') {
5048 ++First;
5049 Node *Ty = getDerived().parseType();
5050 if (!Ty)
5051 return nullptr;
5052 return make<CallExpr>(Name, makeNodeArray(&Ty, &Ty + 1));
5053 }
5054 if (*First == 'z') {
5055 ++First;
5056 Node *Ex = getDerived().parseExpr();
5057 if (!Ex)
5058 return nullptr;
5059 return make<CallExpr>(Name, makeNodeArray(&Ex, &Ex + 1));
5060 }
5061 }
5062 size_t ExprsBegin = Names.size();
5063 while (!consumeIf('E')) {
5064 Node *E = getDerived().parseTemplateArg();
5065 if (E == nullptr)
5066 return E;
5067 Names.push_back(E);
5068 }
5069 return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin));
5070 }
Richard Smithc20d1442018-08-20 20:14:49 +00005071 case '1':
5072 case '2':
5073 case '3':
5074 case '4':
5075 case '5':
5076 case '6':
5077 case '7':
5078 case '8':
5079 case '9':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08005080 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00005081 }
5082 return nullptr;
5083}
5084
5085// <call-offset> ::= h <nv-offset> _
5086// ::= v <v-offset> _
5087//
5088// <nv-offset> ::= <offset number>
5089// # non-virtual base override
5090//
5091// <v-offset> ::= <offset number> _ <virtual offset number>
5092// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00005093template <typename Alloc, typename Derived>
5094bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00005095 // Just scan through the call offset, we never add this information into the
5096 // output.
5097 if (consumeIf('h'))
5098 return parseNumber(true).empty() || !consumeIf('_');
5099 if (consumeIf('v'))
5100 return parseNumber(true).empty() || !consumeIf('_') ||
5101 parseNumber(true).empty() || !consumeIf('_');
5102 return true;
5103}
5104
5105// <special-name> ::= TV <type> # virtual table
5106// ::= TT <type> # VTT structure (construction vtable index)
5107// ::= TI <type> # typeinfo structure
5108// ::= TS <type> # typeinfo name (null-terminated byte string)
5109// ::= Tc <call-offset> <call-offset> <base encoding>
5110// # base is the nominal target function of thunk
5111// # first call-offset is 'this' adjustment
5112// # second call-offset is result adjustment
5113// ::= T <call-offset> <base encoding>
5114// # base is the nominal target function of thunk
5115// ::= GV <object name> # Guard variable for one-time initialization
5116// # No <type>
5117// ::= TW <object name> # Thread-local wrapper
5118// ::= TH <object name> # Thread-local initialization
5119// ::= GR <object name> _ # First temporary
5120// ::= GR <object name> <seq-id> _ # Subsequent temporaries
5121// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
5122// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00005123template <typename Derived, typename Alloc>
5124Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00005125 switch (look()) {
5126 case 'T':
5127 switch (look(1)) {
Richard Smith1865d2f2020-10-22 19:29:36 -07005128 // TA <template-arg> # template parameter object
5129 //
5130 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
5131 case 'A': {
5132 First += 2;
5133 Node *Arg = getDerived().parseTemplateArg();
5134 if (Arg == nullptr)
5135 return nullptr;
5136 return make<SpecialName>("template parameter object for ", Arg);
5137 }
Richard Smithc20d1442018-08-20 20:14:49 +00005138 // TV <type> # virtual table
5139 case 'V': {
5140 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005141 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005142 if (Ty == nullptr)
5143 return nullptr;
5144 return make<SpecialName>("vtable for ", Ty);
5145 }
5146 // TT <type> # VTT structure (construction vtable index)
5147 case 'T': {
5148 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005149 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005150 if (Ty == nullptr)
5151 return nullptr;
5152 return make<SpecialName>("VTT for ", Ty);
5153 }
5154 // TI <type> # typeinfo structure
5155 case 'I': {
5156 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005157 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005158 if (Ty == nullptr)
5159 return nullptr;
5160 return make<SpecialName>("typeinfo for ", Ty);
5161 }
5162 // TS <type> # typeinfo name (null-terminated byte string)
5163 case 'S': {
5164 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005165 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005166 if (Ty == nullptr)
5167 return nullptr;
5168 return make<SpecialName>("typeinfo name for ", Ty);
5169 }
5170 // Tc <call-offset> <call-offset> <base encoding>
5171 case 'c': {
5172 First += 2;
5173 if (parseCallOffset() || parseCallOffset())
5174 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005175 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005176 if (Encoding == nullptr)
5177 return nullptr;
5178 return make<SpecialName>("covariant return thunk to ", Encoding);
5179 }
5180 // extension ::= TC <first type> <number> _ <second type>
5181 // # construction vtable for second-in-first
5182 case 'C': {
5183 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005184 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005185 if (FirstType == nullptr)
5186 return nullptr;
5187 if (parseNumber(true).empty() || !consumeIf('_'))
5188 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005189 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005190 if (SecondType == nullptr)
5191 return nullptr;
5192 return make<CtorVtableSpecialName>(SecondType, FirstType);
5193 }
5194 // TW <object name> # Thread-local wrapper
5195 case 'W': {
5196 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005197 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005198 if (Name == nullptr)
5199 return nullptr;
5200 return make<SpecialName>("thread-local wrapper routine for ", Name);
5201 }
5202 // TH <object name> # Thread-local initialization
5203 case 'H': {
5204 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005205 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005206 if (Name == nullptr)
5207 return nullptr;
5208 return make<SpecialName>("thread-local initialization routine for ", Name);
5209 }
5210 // T <call-offset> <base encoding>
5211 default: {
5212 ++First;
5213 bool IsVirt = look() == 'v';
5214 if (parseCallOffset())
5215 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005216 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005217 if (BaseEncoding == nullptr)
5218 return nullptr;
5219 if (IsVirt)
5220 return make<SpecialName>("virtual thunk to ", BaseEncoding);
5221 else
5222 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
5223 }
5224 }
5225 case 'G':
5226 switch (look(1)) {
5227 // GV <object name> # Guard variable for one-time initialization
5228 case 'V': {
5229 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005230 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005231 if (Name == nullptr)
5232 return nullptr;
5233 return make<SpecialName>("guard variable for ", Name);
5234 }
5235 // GR <object name> # reference temporary for object
5236 // GR <object name> _ # First temporary
5237 // GR <object name> <seq-id> _ # Subsequent temporaries
5238 case 'R': {
5239 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005240 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005241 if (Name == nullptr)
5242 return nullptr;
5243 size_t Count;
5244 bool ParsedSeqId = !parseSeqId(&Count);
5245 if (!consumeIf('_') && ParsedSeqId)
5246 return nullptr;
5247 return make<SpecialName>("reference temporary for ", Name);
5248 }
5249 }
5250 }
5251 return nullptr;
5252}
5253
5254// <encoding> ::= <function name> <bare-function-type>
5255// ::= <data name>
5256// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00005257template <typename Derived, typename Alloc>
5258Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithfac39712020-07-09 21:08:39 -07005259 // The template parameters of an encoding are unrelated to those of the
5260 // enclosing context.
5261 class SaveTemplateParams {
5262 AbstractManglingParser *Parser;
5263 decltype(TemplateParams) OldParams;
Justin Lebar2c536232021-06-09 16:57:22 -07005264 decltype(OuterTemplateParams) OldOuterParams;
Richard Smithfac39712020-07-09 21:08:39 -07005265
5266 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04005267 SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
Richard Smithfac39712020-07-09 21:08:39 -07005268 OldParams = std::move(Parser->TemplateParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005269 OldOuterParams = std::move(Parser->OuterTemplateParams);
Richard Smithfac39712020-07-09 21:08:39 -07005270 Parser->TemplateParams.clear();
Justin Lebar2c536232021-06-09 16:57:22 -07005271 Parser->OuterTemplateParams.clear();
Richard Smithfac39712020-07-09 21:08:39 -07005272 }
5273 ~SaveTemplateParams() {
5274 Parser->TemplateParams = std::move(OldParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005275 Parser->OuterTemplateParams = std::move(OldOuterParams);
Richard Smithfac39712020-07-09 21:08:39 -07005276 }
5277 } SaveTemplateParams(this);
Richard Smithfd434322020-07-09 20:36:04 -07005278
Richard Smithc20d1442018-08-20 20:14:49 +00005279 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00005280 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00005281
5282 auto IsEndOfEncoding = [&] {
5283 // The set of chars that can potentially follow an <encoding> (none of which
5284 // can start a <type>). Enumerating these allows us to avoid speculative
5285 // parsing.
5286 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5287 };
5288
5289 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00005290 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00005291 if (Name == nullptr)
5292 return nullptr;
5293
5294 if (resolveForwardTemplateRefs(NameInfo))
5295 return nullptr;
5296
5297 if (IsEndOfEncoding())
5298 return Name;
5299
5300 Node *Attrs = nullptr;
5301 if (consumeIf("Ua9enable_ifI")) {
5302 size_t BeforeArgs = Names.size();
5303 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005304 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005305 if (Arg == nullptr)
5306 return nullptr;
5307 Names.push_back(Arg);
5308 }
5309 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00005310 if (!Attrs)
5311 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005312 }
5313
5314 Node *ReturnType = nullptr;
5315 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00005316 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005317 if (ReturnType == nullptr)
5318 return nullptr;
5319 }
5320
5321 if (consumeIf('v'))
5322 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5323 Attrs, NameInfo.CVQualifiers,
5324 NameInfo.ReferenceQualifier);
5325
5326 size_t ParamsBegin = Names.size();
5327 do {
Pavel Labathba825192018-10-16 14:29:14 +00005328 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005329 if (Ty == nullptr)
5330 return nullptr;
5331 Names.push_back(Ty);
5332 } while (!IsEndOfEncoding());
5333
5334 return make<FunctionEncoding>(ReturnType, Name,
5335 popTrailingNodeArray(ParamsBegin),
5336 Attrs, NameInfo.CVQualifiers,
5337 NameInfo.ReferenceQualifier);
5338}
5339
5340template <class Float>
5341struct FloatData;
5342
5343template <>
5344struct FloatData<float>
5345{
5346 static const size_t mangled_size = 8;
5347 static const size_t max_demangled_size = 24;
5348 static constexpr const char* spec = "%af";
5349};
5350
5351template <>
5352struct FloatData<double>
5353{
5354 static const size_t mangled_size = 16;
5355 static const size_t max_demangled_size = 32;
5356 static constexpr const char* spec = "%a";
5357};
5358
5359template <>
5360struct FloatData<long double>
5361{
5362#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5363 defined(__wasm__)
5364 static const size_t mangled_size = 32;
5365#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5366 static const size_t mangled_size = 16;
5367#else
5368 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5369#endif
Elliott Hughes5a360ea2020-04-10 17:42:00 -07005370 // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
5371 // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
5372 // Negatives are one character longer than positives.
5373 // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
5374 // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
5375 static const size_t max_demangled_size = 42;
Richard Smithc20d1442018-08-20 20:14:49 +00005376 static constexpr const char *spec = "%LaL";
5377};
5378
Pavel Labathba825192018-10-16 14:29:14 +00005379template <typename Alloc, typename Derived>
5380template <class Float>
5381Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005382 const size_t N = FloatData<Float>::mangled_size;
5383 if (numLeft() <= N)
5384 return nullptr;
5385 StringView Data(First, First + N);
5386 for (char C : Data)
5387 if (!std::isxdigit(C))
5388 return nullptr;
5389 First += N;
5390 if (!consumeIf('E'))
5391 return nullptr;
5392 return make<FloatLiteralImpl<Float>>(Data);
5393}
5394
5395// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005396template <typename Alloc, typename Derived>
5397bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005398 if (!(look() >= '0' && look() <= '9') &&
5399 !(look() >= 'A' && look() <= 'Z'))
5400 return true;
5401
5402 size_t Id = 0;
5403 while (true) {
5404 if (look() >= '0' && look() <= '9') {
5405 Id *= 36;
5406 Id += static_cast<size_t>(look() - '0');
5407 } else if (look() >= 'A' && look() <= 'Z') {
5408 Id *= 36;
5409 Id += static_cast<size_t>(look() - 'A') + 10;
5410 } else {
5411 *Out = Id;
5412 return false;
5413 }
5414 ++First;
5415 }
5416}
5417
5418// <substitution> ::= S <seq-id> _
5419// ::= S_
5420// <substitution> ::= Sa # ::std::allocator
5421// <substitution> ::= Sb # ::std::basic_string
5422// <substitution> ::= Ss # ::std::basic_string < char,
5423// ::std::char_traits<char>,
5424// ::std::allocator<char> >
5425// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5426// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5427// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08005428// The St case is handled specially in parseNestedName.
Pavel Labathba825192018-10-16 14:29:14 +00005429template <typename Derived, typename Alloc>
5430Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005431 if (!consumeIf('S'))
5432 return nullptr;
5433
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08005434 if (look() >= 'a' && look() <= 'z') {
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005435 SpecialSubKind Kind;
Richard Smithc20d1442018-08-20 20:14:49 +00005436 switch (look()) {
5437 case 'a':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005438 Kind = SpecialSubKind::allocator;
Richard Smithc20d1442018-08-20 20:14:49 +00005439 break;
5440 case 'b':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005441 Kind = SpecialSubKind::basic_string;
Richard Smithc20d1442018-08-20 20:14:49 +00005442 break;
5443 case 'd':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005444 Kind = SpecialSubKind::iostream;
5445 break;
5446 case 'i':
5447 Kind = SpecialSubKind::istream;
5448 break;
5449 case 'o':
5450 Kind = SpecialSubKind::ostream;
5451 break;
5452 case 's':
5453 Kind = SpecialSubKind::string;
Richard Smithc20d1442018-08-20 20:14:49 +00005454 break;
5455 default:
5456 return nullptr;
5457 }
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005458 ++First;
5459 auto *SpecialSub = make<SpecialSubstitution>(Kind);
Richard Smithb485b352018-08-24 23:30:26 +00005460 if (!SpecialSub)
5461 return nullptr;
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005462
Richard Smithc20d1442018-08-20 20:14:49 +00005463 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5464 // has ABI tags, the tags are appended to the substitution; the result is a
5465 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005466 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005467 if (WithTags != SpecialSub) {
5468 Subs.push_back(WithTags);
5469 SpecialSub = WithTags;
5470 }
5471 return SpecialSub;
5472 }
5473
5474 // ::= S_
5475 if (consumeIf('_')) {
5476 if (Subs.empty())
5477 return nullptr;
5478 return Subs[0];
5479 }
5480
5481 // ::= S <seq-id> _
5482 size_t Index = 0;
5483 if (parseSeqId(&Index))
5484 return nullptr;
5485 ++Index;
5486 if (!consumeIf('_') || Index >= Subs.size())
5487 return nullptr;
5488 return Subs[Index];
5489}
5490
5491// <template-param> ::= T_ # first template parameter
5492// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005493// ::= TL <level-1> __
5494// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005495template <typename Derived, typename Alloc>
5496Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005497 if (!consumeIf('T'))
5498 return nullptr;
5499
Richard Smithdf1c14c2019-09-06 23:53:21 +00005500 size_t Level = 0;
5501 if (consumeIf('L')) {
5502 if (parsePositiveInteger(&Level))
5503 return nullptr;
5504 ++Level;
5505 if (!consumeIf('_'))
5506 return nullptr;
5507 }
5508
Richard Smithc20d1442018-08-20 20:14:49 +00005509 size_t Index = 0;
5510 if (!consumeIf('_')) {
5511 if (parsePositiveInteger(&Index))
5512 return nullptr;
5513 ++Index;
5514 if (!consumeIf('_'))
5515 return nullptr;
5516 }
5517
Richard Smithc20d1442018-08-20 20:14:49 +00005518 // If we're in a context where this <template-param> refers to a
5519 // <template-arg> further ahead in the mangled name (currently just conversion
5520 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005521 // This can only happen at the outermost level.
5522 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005523 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5524 if (!ForwardRef)
5525 return nullptr;
5526 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5527 ForwardTemplateRefs.push_back(
5528 static_cast<ForwardTemplateReference *>(ForwardRef));
5529 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005530 }
5531
Richard Smithdf1c14c2019-09-06 23:53:21 +00005532 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5533 Index >= TemplateParams[Level]->size()) {
5534 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5535 // list are mangled as the corresponding artificial template type parameter.
5536 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5537 // This will be popped by the ScopedTemplateParamList in
5538 // parseUnnamedTypeName.
5539 if (Level == TemplateParams.size())
5540 TemplateParams.push_back(nullptr);
5541 return make<NameType>("auto");
5542 }
5543
Richard Smithc20d1442018-08-20 20:14:49 +00005544 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005545 }
5546
5547 return (*TemplateParams[Level])[Index];
5548}
5549
5550// <template-param-decl> ::= Ty # type parameter
5551// ::= Tn <type> # non-type parameter
5552// ::= Tt <template-param-decl>* E # template parameter
5553// ::= Tp <template-param-decl> # parameter pack
5554template <typename Derived, typename Alloc>
5555Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5556 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5557 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5558 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5559 if (N) TemplateParams.back()->push_back(N);
5560 return N;
5561 };
5562
5563 if (consumeIf("Ty")) {
5564 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5565 if (!Name)
5566 return nullptr;
5567 return make<TypeTemplateParamDecl>(Name);
5568 }
5569
5570 if (consumeIf("Tn")) {
5571 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5572 if (!Name)
5573 return nullptr;
5574 Node *Type = parseType();
5575 if (!Type)
5576 return nullptr;
5577 return make<NonTypeTemplateParamDecl>(Name, Type);
5578 }
5579
5580 if (consumeIf("Tt")) {
5581 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5582 if (!Name)
5583 return nullptr;
5584 size_t ParamsBegin = Names.size();
5585 ScopedTemplateParamList TemplateTemplateParamParams(this);
5586 while (!consumeIf("E")) {
5587 Node *P = parseTemplateParamDecl();
5588 if (!P)
5589 return nullptr;
5590 Names.push_back(P);
5591 }
5592 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5593 return make<TemplateTemplateParamDecl>(Name, Params);
5594 }
5595
5596 if (consumeIf("Tp")) {
5597 Node *P = parseTemplateParamDecl();
5598 if (!P)
5599 return nullptr;
5600 return make<TemplateParamPackDecl>(P);
5601 }
5602
5603 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005604}
5605
5606// <template-arg> ::= <type> # type or template
5607// ::= X <expression> E # expression
5608// ::= <expr-primary> # simple expressions
5609// ::= J <template-arg>* E # argument pack
5610// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005611template <typename Derived, typename Alloc>
5612Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005613 switch (look()) {
5614 case 'X': {
5615 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005616 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005617 if (Arg == nullptr || !consumeIf('E'))
5618 return nullptr;
5619 return Arg;
5620 }
5621 case 'J': {
5622 ++First;
5623 size_t ArgsBegin = Names.size();
5624 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005625 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005626 if (Arg == nullptr)
5627 return nullptr;
5628 Names.push_back(Arg);
5629 }
5630 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5631 return make<TemplateArgumentPack>(Args);
5632 }
5633 case 'L': {
5634 // ::= LZ <encoding> E # extension
5635 if (look(1) == 'Z') {
5636 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005637 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005638 if (Arg == nullptr || !consumeIf('E'))
5639 return nullptr;
5640 return Arg;
5641 }
5642 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005643 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005644 }
5645 default:
Pavel Labathba825192018-10-16 14:29:14 +00005646 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005647 }
5648}
5649
5650// <template-args> ::= I <template-arg>* E
5651// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005652template <typename Derived, typename Alloc>
5653Node *
5654AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005655 if (!consumeIf('I'))
5656 return nullptr;
5657
5658 // <template-params> refer to the innermost <template-args>. Clear out any
5659 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005660 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005661 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005662 TemplateParams.push_back(&OuterTemplateParams);
5663 OuterTemplateParams.clear();
5664 }
Richard Smithc20d1442018-08-20 20:14:49 +00005665
5666 size_t ArgsBegin = Names.size();
5667 while (!consumeIf('E')) {
5668 if (TagTemplates) {
5669 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005670 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005671 TemplateParams = std::move(OldParams);
5672 if (Arg == nullptr)
5673 return nullptr;
5674 Names.push_back(Arg);
5675 Node *TableEntry = Arg;
5676 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5677 TableEntry = make<ParameterPack>(
5678 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005679 if (!TableEntry)
5680 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005681 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005682 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005683 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005684 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005685 if (Arg == nullptr)
5686 return nullptr;
5687 Names.push_back(Arg);
5688 }
5689 }
5690 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5691}
5692
5693// <mangled-name> ::= _Z <encoding>
5694// ::= <type>
5695// extension ::= ___Z <encoding> _block_invoke
5696// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5697// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005698template <typename Derived, typename Alloc>
5699Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005700 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005701 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005702 if (Encoding == nullptr)
5703 return nullptr;
5704 if (look() == '.') {
5705 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5706 First = Last;
5707 }
5708 if (numLeft() != 0)
5709 return nullptr;
5710 return Encoding;
5711 }
5712
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005713 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005714 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005715 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5716 return nullptr;
5717 bool RequireNumber = consumeIf('_');
5718 if (parseNumber().empty() && RequireNumber)
5719 return nullptr;
5720 if (look() == '.')
5721 First = Last;
5722 if (numLeft() != 0)
5723 return nullptr;
5724 return make<SpecialName>("invocation function for block in ", Encoding);
5725 }
5726
Pavel Labathba825192018-10-16 14:29:14 +00005727 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005728 if (numLeft() != 0)
5729 return nullptr;
5730 return Ty;
5731}
5732
Pavel Labathba825192018-10-16 14:29:14 +00005733template <typename Alloc>
5734struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5735 using AbstractManglingParser<ManglingParser<Alloc>,
5736 Alloc>::AbstractManglingParser;
5737};
5738
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005739DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005740
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005741#endif // DEMANGLE_ITANIUMDEMANGLE_H