blob: a8d96fd2a9fd2752a5309573cdc6808b24a20584 [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;
Nathan Sidwellc6483042022-01-28 09:27:28 -08001790 // Parenthesize pointer-to-member deference argument.
1791 bool IsPtr = Kind.back() == '*';
1792 if (IsPtr)
1793 OB += '(';
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001794 RHS->print(OB);
Nathan Sidwellc6483042022-01-28 09:27:28 -08001795 if (IsPtr)
1796 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00001797 }
1798};
1799
Richard Smith1865d2f2020-10-22 19:29:36 -07001800class SubobjectExpr : public Node {
1801 const Node *Type;
1802 const Node *SubExpr;
1803 StringView Offset;
1804 NodeArray UnionSelectors;
1805 bool OnePastTheEnd;
1806
1807public:
1808 SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_,
1809 NodeArray UnionSelectors_, bool OnePastTheEnd_)
1810 : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_),
1811 UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {}
1812
1813 template<typename Fn> void match(Fn F) const {
1814 F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd);
1815 }
1816
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001817 void printLeft(OutputBuffer &OB) const override {
1818 SubExpr->print(OB);
1819 OB += ".<";
1820 Type->print(OB);
1821 OB += " at offset ";
Richard Smith1865d2f2020-10-22 19:29:36 -07001822 if (Offset.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001823 OB += "0";
Richard Smith1865d2f2020-10-22 19:29:36 -07001824 } else if (Offset[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001825 OB += "-";
1826 OB += Offset.dropFront();
Richard Smith1865d2f2020-10-22 19:29:36 -07001827 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001828 OB += Offset;
Richard Smith1865d2f2020-10-22 19:29:36 -07001829 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001830 OB += ">";
Richard Smith1865d2f2020-10-22 19:29:36 -07001831 }
1832};
1833
Richard Smithc20d1442018-08-20 20:14:49 +00001834class EnclosingExpr : public Node {
1835 const StringView Prefix;
1836 const Node *Infix;
1837 const StringView Postfix;
1838
1839public:
1840 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1841 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1842 Postfix(Postfix_) {}
1843
1844 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1845
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001846 void printLeft(OutputBuffer &OB) const override {
1847 OB += Prefix;
1848 Infix->print(OB);
1849 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +00001850 }
1851};
1852
1853class CastExpr : public Node {
1854 // cast_kind<to>(from)
1855 const StringView CastKind;
1856 const Node *To;
1857 const Node *From;
1858
1859public:
1860 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1861 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1862
1863 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1864
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001865 void printLeft(OutputBuffer &OB) const override {
1866 OB += CastKind;
1867 OB += "<";
1868 To->printLeft(OB);
1869 OB += ">(";
1870 From->printLeft(OB);
1871 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001872 }
1873};
1874
1875class SizeofParamPackExpr : public Node {
1876 const Node *Pack;
1877
1878public:
1879 SizeofParamPackExpr(const Node *Pack_)
1880 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1881
1882 template<typename Fn> void match(Fn F) const { F(Pack); }
1883
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001884 void printLeft(OutputBuffer &OB) const override {
1885 OB += "sizeof...(";
Richard Smithc20d1442018-08-20 20:14:49 +00001886 ParameterPackExpansion PPE(Pack);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001887 PPE.printLeft(OB);
1888 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001889 }
1890};
1891
1892class CallExpr : public Node {
1893 const Node *Callee;
1894 NodeArray Args;
1895
1896public:
1897 CallExpr(const Node *Callee_, NodeArray Args_)
1898 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1899
1900 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1901
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001902 void printLeft(OutputBuffer &OB) const override {
1903 Callee->print(OB);
1904 OB += "(";
1905 Args.printWithComma(OB);
1906 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001907 }
1908};
1909
1910class NewExpr : public Node {
1911 // new (expr_list) type(init_list)
1912 NodeArray ExprList;
1913 Node *Type;
1914 NodeArray InitList;
1915 bool IsGlobal; // ::operator new ?
1916 bool IsArray; // new[] ?
1917public:
1918 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1919 bool IsArray_)
1920 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1921 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1922
1923 template<typename Fn> void match(Fn F) const {
1924 F(ExprList, Type, InitList, IsGlobal, IsArray);
1925 }
1926
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001927 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001928 if (IsGlobal)
Nathan Sidwellc69bde22022-01-28 07:09:38 -08001929 OB += "::";
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001930 OB += "new";
Richard Smithc20d1442018-08-20 20:14:49 +00001931 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001932 OB += "[]";
Richard Smithc20d1442018-08-20 20:14:49 +00001933 if (!ExprList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001934 OB += "(";
1935 ExprList.printWithComma(OB);
1936 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001937 }
Nathan Sidwellc69bde22022-01-28 07:09:38 -08001938 OB += ' ';
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001939 Type->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001940 if (!InitList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001941 OB += "(";
1942 InitList.printWithComma(OB);
1943 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001944 }
Richard Smithc20d1442018-08-20 20:14:49 +00001945 }
1946};
1947
1948class DeleteExpr : public Node {
1949 Node *Op;
1950 bool IsGlobal;
1951 bool IsArray;
1952
1953public:
1954 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1955 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1956
1957 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1958
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001959 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001960 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001961 OB += "::";
1962 OB += "delete";
Richard Smithc20d1442018-08-20 20:14:49 +00001963 if (IsArray)
Nathan Sidwellc69bde22022-01-28 07:09:38 -08001964 OB += "[]";
1965 OB += ' ';
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001966 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001967 }
1968};
1969
1970class PrefixExpr : public Node {
1971 StringView Prefix;
1972 Node *Child;
1973
1974public:
1975 PrefixExpr(StringView Prefix_, Node *Child_)
1976 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1977
1978 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1979
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001980 void printLeft(OutputBuffer &OB) const override {
1981 OB += Prefix;
1982 OB += "(";
1983 Child->print(OB);
1984 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001985 }
1986};
1987
1988class FunctionParam : public Node {
1989 StringView Number;
1990
1991public:
1992 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
1993
1994 template<typename Fn> void match(Fn F) const { F(Number); }
1995
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001996 void printLeft(OutputBuffer &OB) const override {
1997 OB += "fp";
1998 OB += Number;
Richard Smithc20d1442018-08-20 20:14:49 +00001999 }
2000};
2001
2002class ConversionExpr : public Node {
2003 const Node *Type;
2004 NodeArray Expressions;
2005
2006public:
2007 ConversionExpr(const Node *Type_, NodeArray Expressions_)
2008 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
2009
2010 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
2011
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002012 void printLeft(OutputBuffer &OB) const override {
2013 OB += "(";
2014 Type->print(OB);
2015 OB += ")(";
2016 Expressions.printWithComma(OB);
2017 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002018 }
2019};
2020
Richard Smith1865d2f2020-10-22 19:29:36 -07002021class PointerToMemberConversionExpr : public Node {
2022 const Node *Type;
2023 const Node *SubExpr;
2024 StringView Offset;
2025
2026public:
2027 PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_,
2028 StringView Offset_)
2029 : Node(KPointerToMemberConversionExpr), Type(Type_), SubExpr(SubExpr_),
2030 Offset(Offset_) {}
2031
2032 template<typename Fn> void match(Fn F) const { F(Type, SubExpr, Offset); }
2033
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002034 void printLeft(OutputBuffer &OB) const override {
2035 OB += "(";
2036 Type->print(OB);
2037 OB += ")(";
2038 SubExpr->print(OB);
2039 OB += ")";
Richard Smith1865d2f2020-10-22 19:29:36 -07002040 }
2041};
2042
Richard Smithc20d1442018-08-20 20:14:49 +00002043class InitListExpr : public Node {
2044 const Node *Ty;
2045 NodeArray Inits;
2046public:
2047 InitListExpr(const Node *Ty_, NodeArray Inits_)
2048 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
2049
2050 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
2051
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002052 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002053 if (Ty)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002054 Ty->print(OB);
2055 OB += '{';
2056 Inits.printWithComma(OB);
2057 OB += '}';
Richard Smithc20d1442018-08-20 20:14:49 +00002058 }
2059};
2060
2061class BracedExpr : public Node {
2062 const Node *Elem;
2063 const Node *Init;
2064 bool IsArray;
2065public:
2066 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
2067 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
2068
2069 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
2070
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002071 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002072 if (IsArray) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002073 OB += '[';
2074 Elem->print(OB);
2075 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002076 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002077 OB += '.';
2078 Elem->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002079 }
2080 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002081 OB += " = ";
2082 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002083 }
2084};
2085
2086class BracedRangeExpr : public Node {
2087 const Node *First;
2088 const Node *Last;
2089 const Node *Init;
2090public:
2091 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
2092 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
2093
2094 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
2095
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002096 void printLeft(OutputBuffer &OB) const override {
2097 OB += '[';
2098 First->print(OB);
2099 OB += " ... ";
2100 Last->print(OB);
2101 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002102 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002103 OB += " = ";
2104 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002105 }
2106};
2107
2108class FoldExpr : public Node {
2109 const Node *Pack, *Init;
2110 StringView OperatorName;
2111 bool IsLeftFold;
2112
2113public:
2114 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
2115 const Node *Init_)
2116 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
2117 IsLeftFold(IsLeftFold_) {}
2118
2119 template<typename Fn> void match(Fn F) const {
2120 F(IsLeftFold, OperatorName, Pack, Init);
2121 }
2122
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002123 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002124 auto PrintPack = [&] {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002125 OB += '(';
2126 ParameterPackExpansion(Pack).print(OB);
2127 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002128 };
2129
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002130 OB += '(';
Richard Smithc20d1442018-08-20 20:14:49 +00002131
2132 if (IsLeftFold) {
2133 // init op ... op pack
2134 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002135 Init->print(OB);
2136 OB += ' ';
2137 OB += OperatorName;
2138 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002139 }
2140 // ... op pack
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002141 OB += "... ";
2142 OB += OperatorName;
2143 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002144 PrintPack();
2145 } else { // !IsLeftFold
2146 // pack op ...
2147 PrintPack();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002148 OB += ' ';
2149 OB += OperatorName;
2150 OB += " ...";
Richard Smithc20d1442018-08-20 20:14:49 +00002151 // pack op ... op init
2152 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002153 OB += ' ';
2154 OB += OperatorName;
2155 OB += ' ';
2156 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002157 }
2158 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002159 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002160 }
2161};
2162
2163class ThrowExpr : public Node {
2164 const Node *Op;
2165
2166public:
2167 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
2168
2169 template<typename Fn> void match(Fn F) const { F(Op); }
2170
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002171 void printLeft(OutputBuffer &OB) const override {
2172 OB += "throw ";
2173 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002174 }
2175};
2176
2177class BoolExpr : public Node {
2178 bool Value;
2179
2180public:
2181 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
2182
2183 template<typename Fn> void match(Fn F) const { F(Value); }
2184
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002185 void printLeft(OutputBuffer &OB) const override {
2186 OB += Value ? StringView("true") : StringView("false");
Richard Smithc20d1442018-08-20 20:14:49 +00002187 }
2188};
2189
Richard Smithdf1c14c2019-09-06 23:53:21 +00002190class StringLiteral : public Node {
2191 const Node *Type;
2192
2193public:
2194 StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
2195
2196 template<typename Fn> void match(Fn F) const { F(Type); }
2197
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002198 void printLeft(OutputBuffer &OB) const override {
2199 OB += "\"<";
2200 Type->print(OB);
2201 OB += ">\"";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002202 }
2203};
2204
2205class LambdaExpr : public Node {
2206 const Node *Type;
2207
Richard Smithdf1c14c2019-09-06 23:53:21 +00002208public:
2209 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2210
2211 template<typename Fn> void match(Fn F) const { F(Type); }
2212
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002213 void printLeft(OutputBuffer &OB) const override {
2214 OB += "[]";
Richard Smithfb917462019-09-09 22:26:04 +00002215 if (Type->getKind() == KClosureTypeName)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002216 static_cast<const ClosureTypeName *>(Type)->printDeclarator(OB);
2217 OB += "{...}";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002218 }
2219};
2220
Erik Pilkington0a170f12020-05-13 14:13:37 -04002221class EnumLiteral : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +00002222 // ty(integer)
2223 const Node *Ty;
2224 StringView Integer;
2225
2226public:
Erik Pilkington0a170f12020-05-13 14:13:37 -04002227 EnumLiteral(const Node *Ty_, StringView Integer_)
2228 : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00002229
2230 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2231
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002232 void printLeft(OutputBuffer &OB) const override {
2233 OB << "(";
2234 Ty->print(OB);
2235 OB << ")";
Erik Pilkington0a170f12020-05-13 14:13:37 -04002236
2237 if (Integer[0] == 'n')
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002238 OB << "-" << Integer.dropFront(1);
Erik Pilkington0a170f12020-05-13 14:13:37 -04002239 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002240 OB << Integer;
Richard Smithc20d1442018-08-20 20:14:49 +00002241 }
2242};
2243
2244class IntegerLiteral : public Node {
2245 StringView Type;
2246 StringView Value;
2247
2248public:
2249 IntegerLiteral(StringView Type_, StringView Value_)
2250 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2251
2252 template<typename Fn> void match(Fn F) const { F(Type, Value); }
2253
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002254 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002255 if (Type.size() > 3) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002256 OB += "(";
2257 OB += Type;
2258 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002259 }
2260
2261 if (Value[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002262 OB += "-";
2263 OB += Value.dropFront(1);
Richard Smithc20d1442018-08-20 20:14:49 +00002264 } else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002265 OB += Value;
Richard Smithc20d1442018-08-20 20:14:49 +00002266
2267 if (Type.size() <= 3)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002268 OB += Type;
Richard Smithc20d1442018-08-20 20:14:49 +00002269 }
2270};
2271
2272template <class Float> struct FloatData;
2273
2274namespace float_literal_impl {
2275constexpr Node::Kind getFloatLiteralKind(float *) {
2276 return Node::KFloatLiteral;
2277}
2278constexpr Node::Kind getFloatLiteralKind(double *) {
2279 return Node::KDoubleLiteral;
2280}
2281constexpr Node::Kind getFloatLiteralKind(long double *) {
2282 return Node::KLongDoubleLiteral;
2283}
2284}
2285
2286template <class Float> class FloatLiteralImpl : public Node {
2287 const StringView Contents;
2288
2289 static constexpr Kind KindForClass =
2290 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2291
2292public:
2293 FloatLiteralImpl(StringView Contents_)
2294 : Node(KindForClass), Contents(Contents_) {}
2295
2296 template<typename Fn> void match(Fn F) const { F(Contents); }
2297
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002298 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002299 const char *first = Contents.begin();
2300 const char *last = Contents.end() + 1;
2301
2302 const size_t N = FloatData<Float>::mangled_size;
2303 if (static_cast<std::size_t>(last - first) > N) {
2304 last = first + N;
2305 union {
2306 Float value;
2307 char buf[sizeof(Float)];
2308 };
2309 const char *t = first;
2310 char *e = buf;
2311 for (; t != last; ++t, ++e) {
2312 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2313 : static_cast<unsigned>(*t - 'a' + 10);
2314 ++t;
2315 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2316 : static_cast<unsigned>(*t - 'a' + 10);
2317 *e = static_cast<char>((d1 << 4) + d0);
2318 }
2319#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2320 std::reverse(buf, e);
2321#endif
2322 char num[FloatData<Float>::max_demangled_size] = {0};
2323 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002324 OB += StringView(num, num + n);
Richard Smithc20d1442018-08-20 20:14:49 +00002325 }
2326 }
2327};
2328
2329using FloatLiteral = FloatLiteralImpl<float>;
2330using DoubleLiteral = FloatLiteralImpl<double>;
2331using LongDoubleLiteral = FloatLiteralImpl<long double>;
2332
2333/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2334/// appropriate derived class.
2335template<typename Fn>
2336void Node::visit(Fn F) const {
2337 switch (K) {
2338#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2339 FOR_EACH_NODE_KIND(CASE)
2340#undef CASE
2341 }
2342 assert(0 && "unknown mangling node kind");
2343}
2344
2345/// Determine the kind of a node from its type.
2346template<typename NodeT> struct NodeKind;
2347#define SPECIALIZATION(X) \
2348 template<> struct NodeKind<X> { \
2349 static constexpr Node::Kind Kind = Node::K##X; \
2350 static constexpr const char *name() { return #X; } \
2351 };
2352FOR_EACH_NODE_KIND(SPECIALIZATION)
2353#undef SPECIALIZATION
2354
2355#undef FOR_EACH_NODE_KIND
2356
Pavel Labathba825192018-10-16 14:29:14 +00002357template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002358 const char *First;
2359 const char *Last;
2360
2361 // Name stack, this is used by the parser to hold temporary names that were
2362 // parsed. The parser collapses multiple names into new nodes to construct
2363 // the AST. Once the parser is finished, names.size() == 1.
2364 PODSmallVector<Node *, 32> Names;
2365
2366 // Substitution table. Itanium supports name substitutions as a means of
2367 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2368 // table.
2369 PODSmallVector<Node *, 32> Subs;
2370
Richard Smithdf1c14c2019-09-06 23:53:21 +00002371 using TemplateParamList = PODSmallVector<Node *, 8>;
2372
2373 class ScopedTemplateParamList {
2374 AbstractManglingParser *Parser;
2375 size_t OldNumTemplateParamLists;
2376 TemplateParamList Params;
2377
2378 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04002379 ScopedTemplateParamList(AbstractManglingParser *TheParser)
2380 : Parser(TheParser),
2381 OldNumTemplateParamLists(TheParser->TemplateParams.size()) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002382 Parser->TemplateParams.push_back(&Params);
2383 }
2384 ~ScopedTemplateParamList() {
2385 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2386 Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2387 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002388 };
2389
Richard Smithc20d1442018-08-20 20:14:49 +00002390 // Template parameter table. Like the above, but referenced like "T42_".
2391 // This has a smaller size compared to Subs and Names because it can be
2392 // stored on the stack.
Richard Smithdf1c14c2019-09-06 23:53:21 +00002393 TemplateParamList OuterTemplateParams;
2394
2395 // Lists of template parameters indexed by template parameter depth,
2396 // referenced like "TL2_4_". If nonempty, element 0 is always
2397 // OuterTemplateParams; inner elements are always template parameter lists of
2398 // lambda expressions. For a generic lambda with no explicit template
2399 // parameter list, the corresponding parameter list pointer will be null.
2400 PODSmallVector<TemplateParamList *, 4> TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00002401
2402 // Set of unresolved forward <template-param> references. These can occur in a
2403 // conversion operator's type, and are resolved in the enclosing <encoding>.
2404 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2405
Richard Smithc20d1442018-08-20 20:14:49 +00002406 bool TryToParseTemplateArgs = true;
2407 bool PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002408 size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2409
2410 unsigned NumSyntheticTemplateParameters[3] = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002411
2412 Alloc ASTAllocator;
2413
Pavel Labathba825192018-10-16 14:29:14 +00002414 AbstractManglingParser(const char *First_, const char *Last_)
2415 : First(First_), Last(Last_) {}
2416
2417 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002418
2419 void reset(const char *First_, const char *Last_) {
2420 First = First_;
2421 Last = Last_;
2422 Names.clear();
2423 Subs.clear();
2424 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002425 ParsingLambdaParamsAtLevel = (size_t)-1;
Richard Smithc20d1442018-08-20 20:14:49 +00002426 TryToParseTemplateArgs = true;
2427 PermitForwardTemplateReferences = false;
Richard Smith9a2307a2019-09-07 00:11:53 +00002428 for (int I = 0; I != 3; ++I)
2429 NumSyntheticTemplateParameters[I] = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00002430 ASTAllocator.reset();
2431 }
2432
Richard Smithb485b352018-08-24 23:30:26 +00002433 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002434 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2435 }
2436
2437 template <class It> NodeArray makeNodeArray(It begin, It end) {
2438 size_t sz = static_cast<size_t>(end - begin);
2439 void *mem = ASTAllocator.allocateNodeArray(sz);
2440 Node **data = new (mem) Node *[sz];
2441 std::copy(begin, end, data);
2442 return NodeArray(data, sz);
2443 }
2444
2445 NodeArray popTrailingNodeArray(size_t FromPosition) {
2446 assert(FromPosition <= Names.size());
2447 NodeArray res =
2448 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2449 Names.dropBack(FromPosition);
2450 return res;
2451 }
2452
2453 bool consumeIf(StringView S) {
2454 if (StringView(First, Last).startsWith(S)) {
2455 First += S.size();
2456 return true;
2457 }
2458 return false;
2459 }
2460
2461 bool consumeIf(char C) {
2462 if (First != Last && *First == C) {
2463 ++First;
2464 return true;
2465 }
2466 return false;
2467 }
2468
2469 char consume() { return First != Last ? *First++ : '\0'; }
2470
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08002471 char look(unsigned Lookahead = 0) const {
Richard Smithc20d1442018-08-20 20:14:49 +00002472 if (static_cast<size_t>(Last - First) <= Lookahead)
2473 return '\0';
2474 return First[Lookahead];
2475 }
2476
2477 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2478
2479 StringView parseNumber(bool AllowNegative = false);
2480 Qualifiers parseCVQualifiers();
2481 bool parsePositiveInteger(size_t *Out);
2482 StringView parseBareSourceName();
2483
2484 bool parseSeqId(size_t *Out);
2485 Node *parseSubstitution();
2486 Node *parseTemplateParam();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002487 Node *parseTemplateParamDecl();
Richard Smithc20d1442018-08-20 20:14:49 +00002488 Node *parseTemplateArgs(bool TagTemplates = false);
2489 Node *parseTemplateArg();
2490
2491 /// Parse the <expr> production.
2492 Node *parseExpr();
2493 Node *parsePrefixExpr(StringView Kind);
2494 Node *parseBinaryExpr(StringView Kind);
2495 Node *parseIntegerLiteral(StringView Lit);
2496 Node *parseExprPrimary();
2497 template <class Float> Node *parseFloatingLiteral();
2498 Node *parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00002499 Node *parseConversionExpr();
2500 Node *parseBracedExpr();
2501 Node *parseFoldExpr();
Richard Smith1865d2f2020-10-22 19:29:36 -07002502 Node *parsePointerToMemberConversionExpr();
2503 Node *parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00002504
2505 /// Parse the <type> production.
2506 Node *parseType();
2507 Node *parseFunctionType();
2508 Node *parseVectorType();
2509 Node *parseDecltype();
2510 Node *parseArrayType();
2511 Node *parsePointerToMemberType();
2512 Node *parseClassEnumType();
2513 Node *parseQualifiedType();
2514
2515 Node *parseEncoding();
2516 bool parseCallOffset();
2517 Node *parseSpecialName();
2518
2519 /// Holds some extra information about a <name> that is being parsed. This
2520 /// information is only pertinent if the <name> refers to an <encoding>.
2521 struct NameState {
2522 bool CtorDtorConversion = false;
2523 bool EndsWithTemplateArgs = false;
2524 Qualifiers CVQualifiers = QualNone;
2525 FunctionRefQual ReferenceQualifier = FrefQualNone;
2526 size_t ForwardTemplateRefsBegin;
2527
Pavel Labathba825192018-10-16 14:29:14 +00002528 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002529 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2530 };
2531
2532 bool resolveForwardTemplateRefs(NameState &State) {
2533 size_t I = State.ForwardTemplateRefsBegin;
2534 size_t E = ForwardTemplateRefs.size();
2535 for (; I < E; ++I) {
2536 size_t Idx = ForwardTemplateRefs[I]->Index;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002537 if (TemplateParams.empty() || !TemplateParams[0] ||
2538 Idx >= TemplateParams[0]->size())
Richard Smithc20d1442018-08-20 20:14:49 +00002539 return true;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002540 ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
Richard Smithc20d1442018-08-20 20:14:49 +00002541 }
2542 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2543 return false;
2544 }
2545
2546 /// Parse the <name> production>
2547 Node *parseName(NameState *State = nullptr);
2548 Node *parseLocalName(NameState *State);
2549 Node *parseOperatorName(NameState *State);
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002550 Node *parseUnqualifiedName(NameState *State, Node *Scope);
Richard Smithc20d1442018-08-20 20:14:49 +00002551 Node *parseUnnamedTypeName(NameState *State);
2552 Node *parseSourceName(NameState *State);
2553 Node *parseUnscopedName(NameState *State);
2554 Node *parseNestedName(NameState *State);
2555 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2556
2557 Node *parseAbiTags(Node *N);
2558
2559 /// Parse the <unresolved-name> production.
Nathan Sidwell77c52e22022-01-28 11:59:03 -08002560 Node *parseUnresolvedName(bool Global);
Richard Smithc20d1442018-08-20 20:14:49 +00002561 Node *parseSimpleId();
2562 Node *parseBaseUnresolvedName();
2563 Node *parseUnresolvedType();
2564 Node *parseDestructorName();
2565
2566 /// Top-level entry point into the parser.
2567 Node *parse();
2568};
2569
2570const char* parse_discriminator(const char* first, const char* last);
2571
2572// <name> ::= <nested-name> // N
2573// ::= <local-name> # See Scope Encoding below // Z
2574// ::= <unscoped-template-name> <template-args>
2575// ::= <unscoped-name>
2576//
2577// <unscoped-template-name> ::= <unscoped-name>
2578// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002579template <typename Derived, typename Alloc>
2580Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002581 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002582 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002583 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002584 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002585
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002586 Node *Result = nullptr;
2587 bool IsSubst = look() == 'S' && look(1) != 't';
2588 if (IsSubst) {
2589 // A substitution must lead to:
2590 // ::= <unscoped-template-name> <template-args>
2591 Result = getDerived().parseSubstitution();
2592 } else {
2593 // An unscoped name can be one of:
2594 // ::= <unscoped-name>
2595 // ::= <unscoped-template-name> <template-args>
2596 Result = getDerived().parseUnscopedName(State);
2597 }
2598 if (Result == nullptr)
2599 return nullptr;
2600
2601 if (look() == 'I') {
2602 // ::= <unscoped-template-name> <template-args>
2603 if (!IsSubst)
2604 // An unscoped-template-name is substitutable.
2605 Subs.push_back(Result);
Pavel Labathba825192018-10-16 14:29:14 +00002606 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002607 if (TA == nullptr)
2608 return nullptr;
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002609 if (State)
2610 State->EndsWithTemplateArgs = true;
2611 Result = make<NameWithTemplateArgs>(Result, TA);
2612 } else if (IsSubst) {
2613 // The substitution case must be followed by <template-args>.
2614 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002615 }
2616
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002617 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00002618}
2619
2620// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2621// := Z <function encoding> E s [<discriminator>]
2622// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002623template <typename Derived, typename Alloc>
2624Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002625 if (!consumeIf('Z'))
2626 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002627 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002628 if (Encoding == nullptr || !consumeIf('E'))
2629 return nullptr;
2630
2631 if (consumeIf('s')) {
2632 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002633 auto *StringLitName = make<NameType>("string literal");
2634 if (!StringLitName)
2635 return nullptr;
2636 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002637 }
2638
2639 if (consumeIf('d')) {
2640 parseNumber(true);
2641 if (!consumeIf('_'))
2642 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002643 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002644 if (N == nullptr)
2645 return nullptr;
2646 return make<LocalName>(Encoding, N);
2647 }
2648
Pavel Labathba825192018-10-16 14:29:14 +00002649 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002650 if (Entity == nullptr)
2651 return nullptr;
2652 First = parse_discriminator(First, Last);
2653 return make<LocalName>(Encoding, Entity);
2654}
2655
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08002656// <unscoped-name> ::= [L]* <unqualified-name>
2657// ::= St [L]* <unqualified-name> # ::std::
2658// [*] extension
Pavel Labathba825192018-10-16 14:29:14 +00002659template <typename Derived, typename Alloc>
2660Node *
2661AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002662 Node *Std = nullptr;
2663 if (consumeIf("St")) {
2664 Std = make<NameType>("std");
2665 if (Std == nullptr)
Nathan Sidwell200e97c2022-01-21 11:37:01 -08002666 return nullptr;
2667 }
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002668 consumeIf('L');
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002669
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002670 return getDerived().parseUnqualifiedName(State, Std);
Richard Smithc20d1442018-08-20 20:14:49 +00002671}
2672
2673// <unqualified-name> ::= <operator-name> [abi-tags]
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002674// ::= <ctor-dtor-name> [<abi-tags>]
2675// ::= <source-name> [<abi-tags>]
2676// ::= <unnamed-type-name> [<abi-tags>]
Richard Smithc20d1442018-08-20 20:14:49 +00002677// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002678template <typename Derived, typename Alloc>
2679Node *
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002680AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State,
2681 Node *Scope) {
Richard Smithc20d1442018-08-20 20:14:49 +00002682 Node *Result;
2683 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002684 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002685 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002686 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002687 else if (consumeIf("DC")) {
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002688 // Structured binding
Richard Smithc20d1442018-08-20 20:14:49 +00002689 size_t BindingsBegin = Names.size();
2690 do {
Pavel Labathba825192018-10-16 14:29:14 +00002691 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002692 if (Binding == nullptr)
2693 return nullptr;
2694 Names.push_back(Binding);
2695 } while (!consumeIf('E'));
2696 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002697 } else if (look() == 'C' || look() == 'D') {
2698 // A <ctor-dtor-name>.
2699 if (Scope == nullptr)
2700 return nullptr;
2701 Result = getDerived().parseCtorDtorName(Scope, State);
2702 } else {
Pavel Labathba825192018-10-16 14:29:14 +00002703 Result = getDerived().parseOperatorName(State);
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002704 }
Richard Smithc20d1442018-08-20 20:14:49 +00002705 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002706 Result = getDerived().parseAbiTags(Result);
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002707 if (Result != nullptr && Scope != nullptr)
2708 Result = make<NestedName>(Scope, Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002709 return Result;
2710}
2711
2712// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2713// ::= <closure-type-name>
2714//
2715// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2716//
2717// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002718template <typename Derived, typename Alloc>
2719Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002720AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2721 // <template-params> refer to the innermost <template-args>. Clear out any
2722 // outer args that we may have inserted into TemplateParams.
2723 if (State != nullptr)
2724 TemplateParams.clear();
2725
Richard Smithc20d1442018-08-20 20:14:49 +00002726 if (consumeIf("Ut")) {
2727 StringView Count = parseNumber();
2728 if (!consumeIf('_'))
2729 return nullptr;
2730 return make<UnnamedTypeName>(Count);
2731 }
2732 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002733 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2734 TemplateParams.size());
2735 ScopedTemplateParamList LambdaTemplateParams(this);
2736
2737 size_t ParamsBegin = Names.size();
2738 while (look() == 'T' &&
2739 StringView("yptn").find(look(1)) != StringView::npos) {
2740 Node *T = parseTemplateParamDecl();
2741 if (!T)
2742 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002743 Names.push_back(T);
2744 }
2745 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2746
2747 // FIXME: If TempParams is empty and none of the function parameters
2748 // includes 'auto', we should remove LambdaTemplateParams from the
2749 // TemplateParams list. Unfortunately, we don't find out whether there are
2750 // any 'auto' parameters until too late in an example such as:
2751 //
2752 // template<typename T> void f(
2753 // decltype([](decltype([]<typename T>(T v) {}),
2754 // auto) {})) {}
2755 // template<typename T> void f(
2756 // decltype([](decltype([]<typename T>(T w) {}),
2757 // int) {})) {}
2758 //
2759 // Here, the type of v is at level 2 but the type of w is at level 1. We
2760 // don't find this out until we encounter the type of the next parameter.
2761 //
2762 // However, compilers can't actually cope with the former example in
2763 // practice, and it's likely to be made ill-formed in future, so we don't
2764 // need to support it here.
2765 //
2766 // If we encounter an 'auto' in the function parameter types, we will
2767 // recreate a template parameter scope for it, but any intervening lambdas
2768 // will be parsed in the 'wrong' template parameter depth.
2769 if (TempParams.empty())
2770 TemplateParams.pop_back();
2771
Richard Smithc20d1442018-08-20 20:14:49 +00002772 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002773 do {
Pavel Labathba825192018-10-16 14:29:14 +00002774 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002775 if (P == nullptr)
2776 return nullptr;
2777 Names.push_back(P);
2778 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002779 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002780 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2781
Richard Smithc20d1442018-08-20 20:14:49 +00002782 StringView Count = parseNumber();
2783 if (!consumeIf('_'))
2784 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002785 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002786 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002787 if (consumeIf("Ub")) {
2788 (void)parseNumber();
2789 if (!consumeIf('_'))
2790 return nullptr;
2791 return make<NameType>("'block-literal'");
2792 }
Richard Smithc20d1442018-08-20 20:14:49 +00002793 return nullptr;
2794}
2795
2796// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002797template <typename Derived, typename Alloc>
2798Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002799 size_t Length = 0;
2800 if (parsePositiveInteger(&Length))
2801 return nullptr;
2802 if (numLeft() < Length || Length == 0)
2803 return nullptr;
2804 StringView Name(First, First + Length);
2805 First += Length;
2806 if (Name.startsWith("_GLOBAL__N"))
2807 return make<NameType>("(anonymous namespace)");
2808 return make<NameType>(Name);
2809}
2810
2811// <operator-name> ::= aa # &&
2812// ::= ad # & (unary)
2813// ::= an # &
2814// ::= aN # &=
2815// ::= aS # =
2816// ::= cl # ()
2817// ::= cm # ,
2818// ::= co # ~
2819// ::= cv <type> # (cast)
2820// ::= da # delete[]
2821// ::= de # * (unary)
2822// ::= dl # delete
2823// ::= dv # /
2824// ::= dV # /=
2825// ::= eo # ^
2826// ::= eO # ^=
2827// ::= eq # ==
2828// ::= ge # >=
2829// ::= gt # >
2830// ::= ix # []
2831// ::= le # <=
2832// ::= li <source-name> # operator ""
2833// ::= ls # <<
2834// ::= lS # <<=
2835// ::= lt # <
2836// ::= mi # -
2837// ::= mI # -=
2838// ::= ml # *
2839// ::= mL # *=
2840// ::= mm # -- (postfix in <expression> context)
2841// ::= na # new[]
2842// ::= ne # !=
2843// ::= ng # - (unary)
2844// ::= nt # !
2845// ::= nw # new
2846// ::= oo # ||
2847// ::= or # |
2848// ::= oR # |=
2849// ::= pm # ->*
2850// ::= pl # +
2851// ::= pL # +=
2852// ::= pp # ++ (postfix in <expression> context)
2853// ::= ps # + (unary)
2854// ::= pt # ->
2855// ::= qu # ?
2856// ::= rm # %
2857// ::= rM # %=
2858// ::= rs # >>
2859// ::= rS # >>=
2860// ::= ss # <=> C++2a
2861// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002862template <typename Derived, typename Alloc>
2863Node *
2864AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002865 switch (look()) {
2866 case 'a':
2867 switch (look(1)) {
2868 case 'a':
2869 First += 2;
2870 return make<NameType>("operator&&");
2871 case 'd':
2872 case 'n':
2873 First += 2;
2874 return make<NameType>("operator&");
2875 case 'N':
2876 First += 2;
2877 return make<NameType>("operator&=");
2878 case 'S':
2879 First += 2;
2880 return make<NameType>("operator=");
2881 }
2882 return nullptr;
2883 case 'c':
2884 switch (look(1)) {
2885 case 'l':
2886 First += 2;
2887 return make<NameType>("operator()");
2888 case 'm':
2889 First += 2;
2890 return make<NameType>("operator,");
2891 case 'o':
2892 First += 2;
2893 return make<NameType>("operator~");
2894 // ::= cv <type> # (cast)
2895 case 'v': {
2896 First += 2;
2897 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2898 // If we're parsing an encoding, State != nullptr and the conversion
2899 // operators' <type> could have a <template-param> that refers to some
2900 // <template-arg>s further ahead in the mangled name.
2901 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2902 PermitForwardTemplateReferences ||
2903 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002904 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002905 if (Ty == nullptr)
2906 return nullptr;
2907 if (State) State->CtorDtorConversion = true;
2908 return make<ConversionOperatorType>(Ty);
2909 }
2910 }
2911 return nullptr;
2912 case 'd':
2913 switch (look(1)) {
2914 case 'a':
2915 First += 2;
2916 return make<NameType>("operator delete[]");
2917 case 'e':
2918 First += 2;
2919 return make<NameType>("operator*");
2920 case 'l':
2921 First += 2;
2922 return make<NameType>("operator delete");
2923 case 'v':
2924 First += 2;
2925 return make<NameType>("operator/");
2926 case 'V':
2927 First += 2;
2928 return make<NameType>("operator/=");
2929 }
2930 return nullptr;
2931 case 'e':
2932 switch (look(1)) {
2933 case 'o':
2934 First += 2;
2935 return make<NameType>("operator^");
2936 case 'O':
2937 First += 2;
2938 return make<NameType>("operator^=");
2939 case 'q':
2940 First += 2;
2941 return make<NameType>("operator==");
2942 }
2943 return nullptr;
2944 case 'g':
2945 switch (look(1)) {
2946 case 'e':
2947 First += 2;
2948 return make<NameType>("operator>=");
2949 case 't':
2950 First += 2;
2951 return make<NameType>("operator>");
2952 }
2953 return nullptr;
2954 case 'i':
2955 if (look(1) == 'x') {
2956 First += 2;
2957 return make<NameType>("operator[]");
2958 }
2959 return nullptr;
2960 case 'l':
2961 switch (look(1)) {
2962 case 'e':
2963 First += 2;
2964 return make<NameType>("operator<=");
2965 // ::= li <source-name> # operator ""
2966 case 'i': {
2967 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002968 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002969 if (SN == nullptr)
2970 return nullptr;
2971 return make<LiteralOperator>(SN);
2972 }
2973 case 's':
2974 First += 2;
2975 return make<NameType>("operator<<");
2976 case 'S':
2977 First += 2;
2978 return make<NameType>("operator<<=");
2979 case 't':
2980 First += 2;
2981 return make<NameType>("operator<");
2982 }
2983 return nullptr;
2984 case 'm':
2985 switch (look(1)) {
2986 case 'i':
2987 First += 2;
2988 return make<NameType>("operator-");
2989 case 'I':
2990 First += 2;
2991 return make<NameType>("operator-=");
2992 case 'l':
2993 First += 2;
2994 return make<NameType>("operator*");
2995 case 'L':
2996 First += 2;
2997 return make<NameType>("operator*=");
2998 case 'm':
2999 First += 2;
3000 return make<NameType>("operator--");
3001 }
3002 return nullptr;
3003 case 'n':
3004 switch (look(1)) {
3005 case 'a':
3006 First += 2;
3007 return make<NameType>("operator new[]");
3008 case 'e':
3009 First += 2;
3010 return make<NameType>("operator!=");
3011 case 'g':
3012 First += 2;
3013 return make<NameType>("operator-");
3014 case 't':
3015 First += 2;
3016 return make<NameType>("operator!");
3017 case 'w':
3018 First += 2;
3019 return make<NameType>("operator new");
3020 }
3021 return nullptr;
3022 case 'o':
3023 switch (look(1)) {
3024 case 'o':
3025 First += 2;
3026 return make<NameType>("operator||");
3027 case 'r':
3028 First += 2;
3029 return make<NameType>("operator|");
3030 case 'R':
3031 First += 2;
3032 return make<NameType>("operator|=");
3033 }
3034 return nullptr;
3035 case 'p':
3036 switch (look(1)) {
3037 case 'm':
3038 First += 2;
3039 return make<NameType>("operator->*");
3040 case 'l':
3041 First += 2;
3042 return make<NameType>("operator+");
3043 case 'L':
3044 First += 2;
3045 return make<NameType>("operator+=");
3046 case 'p':
3047 First += 2;
3048 return make<NameType>("operator++");
3049 case 's':
3050 First += 2;
3051 return make<NameType>("operator+");
3052 case 't':
3053 First += 2;
3054 return make<NameType>("operator->");
3055 }
3056 return nullptr;
3057 case 'q':
3058 if (look(1) == 'u') {
3059 First += 2;
3060 return make<NameType>("operator?");
3061 }
3062 return nullptr;
3063 case 'r':
3064 switch (look(1)) {
3065 case 'm':
3066 First += 2;
3067 return make<NameType>("operator%");
3068 case 'M':
3069 First += 2;
3070 return make<NameType>("operator%=");
3071 case 's':
3072 First += 2;
3073 return make<NameType>("operator>>");
3074 case 'S':
3075 First += 2;
3076 return make<NameType>("operator>>=");
3077 }
3078 return nullptr;
3079 case 's':
3080 if (look(1) == 's') {
3081 First += 2;
3082 return make<NameType>("operator<=>");
3083 }
3084 return nullptr;
3085 // ::= v <digit> <source-name> # vendor extended operator
3086 case 'v':
3087 if (std::isdigit(look(1))) {
3088 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003089 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003090 if (SN == nullptr)
3091 return nullptr;
3092 return make<ConversionOperatorType>(SN);
3093 }
3094 return nullptr;
3095 }
3096 return nullptr;
3097}
3098
3099// <ctor-dtor-name> ::= C1 # complete object constructor
3100// ::= C2 # base object constructor
3101// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003102// extension ::= C4 # gcc old-style "[unified]" constructor
3103// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003104// ::= D0 # deleting destructor
3105// ::= D1 # complete object destructor
3106// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003107// extension ::= D4 # gcc old-style "[unified]" destructor
3108// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003109template <typename Derived, typename Alloc>
3110Node *
3111AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3112 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003113 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3114 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3115 switch (SSK) {
3116 case SpecialSubKind::string:
3117 case SpecialSubKind::istream:
3118 case SpecialSubKind::ostream:
3119 case SpecialSubKind::iostream:
3120 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003121 if (!SoFar)
3122 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003123 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003124 default:
3125 break;
3126 }
3127 }
3128
3129 if (consumeIf('C')) {
3130 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003131 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3132 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003133 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003134 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003135 ++First;
3136 if (State) State->CtorDtorConversion = true;
3137 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003138 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003139 return nullptr;
3140 }
Nico Weber29294792019-04-03 23:14:33 +00003141 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003142 }
3143
Nico Weber29294792019-04-03 23:14:33 +00003144 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3145 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003146 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003147 First += 2;
3148 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003149 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003150 }
3151
3152 return nullptr;
3153}
3154
3155// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3156// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3157//
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003158// <prefix> ::= <prefix> [L]* <unqualified-name>
Richard Smithc20d1442018-08-20 20:14:49 +00003159// ::= <template-prefix> <template-args>
3160// ::= <template-param>
3161// ::= <decltype>
3162// ::= # empty
3163// ::= <substitution>
3164// ::= <prefix> <data-member-prefix>
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003165// [*] extension
Richard Smithc20d1442018-08-20 20:14:49 +00003166//
3167// <data-member-prefix> := <member source-name> [<template-args>] M
3168//
3169// <template-prefix> ::= <prefix> <template unqualified-name>
3170// ::= <template-param>
3171// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003172template <typename Derived, typename Alloc>
3173Node *
3174AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003175 if (!consumeIf('N'))
3176 return nullptr;
3177
3178 Qualifiers CVTmp = parseCVQualifiers();
3179 if (State) State->CVQualifiers = CVTmp;
3180
3181 if (consumeIf('O')) {
3182 if (State) State->ReferenceQualifier = FrefQualRValue;
3183 } else if (consumeIf('R')) {
3184 if (State) State->ReferenceQualifier = FrefQualLValue;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003185 } else {
Richard Smithc20d1442018-08-20 20:14:49 +00003186 if (State) State->ReferenceQualifier = FrefQualNone;
Richard Smithb485b352018-08-24 23:30:26 +00003187 }
Richard Smithc20d1442018-08-20 20:14:49 +00003188
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003189 Node *SoFar = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003190 while (!consumeIf('E')) {
3191 consumeIf('L'); // extension
3192
Richard Smithc20d1442018-08-20 20:14:49 +00003193 if (consumeIf('M')) {
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003194 // <data-member-prefix> := <member source-name> [<template-args>] M
Richard Smithc20d1442018-08-20 20:14:49 +00003195 if (SoFar == nullptr)
3196 return nullptr;
3197 continue;
3198 }
3199
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003200 if (State)
3201 // Only set end-with-template on the case that does that.
3202 State->EndsWithTemplateArgs = false;
3203
Richard Smithc20d1442018-08-20 20:14:49 +00003204 if (look() == 'T') {
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003205 // ::= <template-param>
3206 if (SoFar != nullptr)
3207 return nullptr; // Cannot have a prefix.
3208 SoFar = getDerived().parseTemplateParam();
3209 } else if (look() == 'I') {
3210 // ::= <template-prefix> <template-args>
3211 if (SoFar == nullptr)
3212 return nullptr; // Must have a prefix.
Pavel Labathba825192018-10-16 14:29:14 +00003213 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003214 if (TA == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003215 return nullptr;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003216 if (SoFar->getKind() == Node::KNameWithTemplateArgs)
3217 // Semantically <template-args> <template-args> cannot be generated by a
3218 // C++ entity. There will always be [something like] a name between
3219 // them.
3220 return nullptr;
3221 if (State)
3222 State->EndsWithTemplateArgs = true;
Richard Smithc20d1442018-08-20 20:14:49 +00003223 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003224 } else if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
3225 // ::= <decltype>
3226 if (SoFar != nullptr)
3227 return nullptr; // Cannot have a prefix.
3228 SoFar = getDerived().parseDecltype();
3229 } else if (look() == 'S') {
3230 // ::= <substitution>
3231 if (SoFar != nullptr)
3232 return nullptr; // Cannot have a prefix.
3233 if (look(1) == 't') {
3234 // parseSubstition does not handle 'St'.
3235 First += 2;
3236 SoFar = make<NameType>("std");
3237 } else {
3238 SoFar = getDerived().parseSubstitution();
3239 }
Richard Smithc20d1442018-08-20 20:14:49 +00003240 if (SoFar == nullptr)
3241 return nullptr;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003242 continue; // Do not push a new substitution.
3243 } else {
Nathan Sidwell9a29c972022-01-25 12:23:31 -08003244 // ::= [<prefix>] <unqualified-name>
3245 SoFar = getDerived().parseUnqualifiedName(State, SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00003246 }
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003247 if (SoFar == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003248 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003249 Subs.push_back(SoFar);
3250 }
3251
3252 if (SoFar == nullptr || Subs.empty())
3253 return nullptr;
3254
3255 Subs.pop_back();
3256 return SoFar;
3257}
3258
3259// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003260template <typename Derived, typename Alloc>
3261Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3262 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003263 if (SN == nullptr)
3264 return nullptr;
3265 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003266 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003267 if (TA == nullptr)
3268 return nullptr;
3269 return make<NameWithTemplateArgs>(SN, TA);
3270 }
3271 return SN;
3272}
3273
3274// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3275// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003276template <typename Derived, typename Alloc>
3277Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003278 Node *Result;
3279 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003280 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003281 else
Pavel Labathba825192018-10-16 14:29:14 +00003282 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003283 if (Result == nullptr)
3284 return nullptr;
3285 return make<DtorName>(Result);
3286}
3287
3288// <unresolved-type> ::= <template-param>
3289// ::= <decltype>
3290// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003291template <typename Derived, typename Alloc>
3292Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003293 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003294 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003295 if (TP == nullptr)
3296 return nullptr;
3297 Subs.push_back(TP);
3298 return TP;
3299 }
3300 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003301 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003302 if (DT == nullptr)
3303 return nullptr;
3304 Subs.push_back(DT);
3305 return DT;
3306 }
Pavel Labathba825192018-10-16 14:29:14 +00003307 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003308}
3309
3310// <base-unresolved-name> ::= <simple-id> # unresolved name
3311// extension ::= <operator-name> # unresolved operator-function-id
3312// extension ::= <operator-name> <template-args> # unresolved operator template-id
3313// ::= on <operator-name> # unresolved operator-function-id
3314// ::= on <operator-name> <template-args> # unresolved operator template-id
3315// ::= dn <destructor-name> # destructor or pseudo-destructor;
3316// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003317template <typename Derived, typename Alloc>
3318Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003319 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003320 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003321
3322 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003323 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003324
3325 consumeIf("on");
3326
Pavel Labathba825192018-10-16 14:29:14 +00003327 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003328 if (Oper == nullptr)
3329 return nullptr;
3330 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003331 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003332 if (TA == nullptr)
3333 return nullptr;
3334 return make<NameWithTemplateArgs>(Oper, TA);
3335 }
3336 return Oper;
3337}
3338
3339// <unresolved-name>
3340// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3341// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3342// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3343// # A::x, N::y, A<T>::z; "gs" means leading "::"
Nathan Sidwell77c52e22022-01-28 11:59:03 -08003344// [gs] has been parsed by caller.
Richard Smithc20d1442018-08-20 20:14:49 +00003345// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3346// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3347// # T::N::x /decltype(p)::N::x
3348// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3349//
3350// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003351template <typename Derived, typename Alloc>
Nathan Sidwell77c52e22022-01-28 11:59:03 -08003352Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName(bool Global) {
Richard Smithc20d1442018-08-20 20:14:49 +00003353 Node *SoFar = nullptr;
3354
3355 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3356 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3357 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003358 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003359 if (SoFar == nullptr)
3360 return nullptr;
3361
3362 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003363 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003364 if (TA == nullptr)
3365 return nullptr;
3366 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003367 if (!SoFar)
3368 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003369 }
3370
3371 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003372 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003373 if (Qual == nullptr)
3374 return nullptr;
3375 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003376 if (!SoFar)
3377 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003378 }
3379
Pavel Labathba825192018-10-16 14:29:14 +00003380 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003381 if (Base == nullptr)
3382 return nullptr;
3383 return make<QualifiedName>(SoFar, Base);
3384 }
3385
Richard Smithc20d1442018-08-20 20:14:49 +00003386 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3387 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003388 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003389 if (SoFar == nullptr)
3390 return nullptr;
3391 if (Global)
3392 SoFar = make<GlobalQualifiedName>(SoFar);
3393 return SoFar;
3394 }
3395
3396 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3397 if (std::isdigit(look())) {
3398 do {
Pavel Labathba825192018-10-16 14:29:14 +00003399 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003400 if (Qual == nullptr)
3401 return nullptr;
3402 if (SoFar)
3403 SoFar = make<QualifiedName>(SoFar, Qual);
3404 else if (Global)
3405 SoFar = make<GlobalQualifiedName>(Qual);
3406 else
3407 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003408 if (!SoFar)
3409 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003410 } while (!consumeIf('E'));
3411 }
3412 // sr <unresolved-type> <base-unresolved-name>
3413 // sr <unresolved-type> <template-args> <base-unresolved-name>
3414 else {
Pavel Labathba825192018-10-16 14:29:14 +00003415 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003416 if (SoFar == nullptr)
3417 return nullptr;
3418
3419 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003420 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003421 if (TA == nullptr)
3422 return nullptr;
3423 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003424 if (!SoFar)
3425 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003426 }
3427 }
3428
3429 assert(SoFar != nullptr);
3430
Pavel Labathba825192018-10-16 14:29:14 +00003431 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003432 if (Base == nullptr)
3433 return nullptr;
3434 return make<QualifiedName>(SoFar, Base);
3435}
3436
3437// <abi-tags> ::= <abi-tag> [<abi-tags>]
3438// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003439template <typename Derived, typename Alloc>
3440Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003441 while (consumeIf('B')) {
3442 StringView SN = parseBareSourceName();
3443 if (SN.empty())
3444 return nullptr;
3445 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003446 if (!N)
3447 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003448 }
3449 return N;
3450}
3451
3452// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003453template <typename Alloc, typename Derived>
3454StringView
3455AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003456 const char *Tmp = First;
3457 if (AllowNegative)
3458 consumeIf('n');
3459 if (numLeft() == 0 || !std::isdigit(*First))
3460 return StringView();
3461 while (numLeft() != 0 && std::isdigit(*First))
3462 ++First;
3463 return StringView(Tmp, First);
3464}
3465
3466// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003467template <typename Alloc, typename Derived>
3468bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003469 *Out = 0;
3470 if (look() < '0' || look() > '9')
3471 return true;
3472 while (look() >= '0' && look() <= '9') {
3473 *Out *= 10;
3474 *Out += static_cast<size_t>(consume() - '0');
3475 }
3476 return false;
3477}
3478
Pavel Labathba825192018-10-16 14:29:14 +00003479template <typename Alloc, typename Derived>
3480StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003481 size_t Int = 0;
3482 if (parsePositiveInteger(&Int) || numLeft() < Int)
3483 return StringView();
3484 StringView R(First, First + Int);
3485 First += Int;
3486 return R;
3487}
3488
3489// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3490//
3491// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3492// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3493// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3494//
3495// <ref-qualifier> ::= R # & ref-qualifier
3496// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003497template <typename Derived, typename Alloc>
3498Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003499 Qualifiers CVQuals = parseCVQualifiers();
3500
3501 Node *ExceptionSpec = nullptr;
3502 if (consumeIf("Do")) {
3503 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003504 if (!ExceptionSpec)
3505 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003506 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003507 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003508 if (E == nullptr || !consumeIf('E'))
3509 return nullptr;
3510 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003511 if (!ExceptionSpec)
3512 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003513 } else if (consumeIf("Dw")) {
3514 size_t SpecsBegin = Names.size();
3515 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003516 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003517 if (T == nullptr)
3518 return nullptr;
3519 Names.push_back(T);
3520 }
3521 ExceptionSpec =
3522 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003523 if (!ExceptionSpec)
3524 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003525 }
3526
3527 consumeIf("Dx"); // transaction safe
3528
3529 if (!consumeIf('F'))
3530 return nullptr;
3531 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003532 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003533 if (ReturnType == nullptr)
3534 return nullptr;
3535
3536 FunctionRefQual ReferenceQualifier = FrefQualNone;
3537 size_t ParamsBegin = Names.size();
3538 while (true) {
3539 if (consumeIf('E'))
3540 break;
3541 if (consumeIf('v'))
3542 continue;
3543 if (consumeIf("RE")) {
3544 ReferenceQualifier = FrefQualLValue;
3545 break;
3546 }
3547 if (consumeIf("OE")) {
3548 ReferenceQualifier = FrefQualRValue;
3549 break;
3550 }
Pavel Labathba825192018-10-16 14:29:14 +00003551 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003552 if (T == nullptr)
3553 return nullptr;
3554 Names.push_back(T);
3555 }
3556
3557 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3558 return make<FunctionType>(ReturnType, Params, CVQuals,
3559 ReferenceQualifier, ExceptionSpec);
3560}
3561
3562// extension:
3563// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3564// ::= Dv [<dimension expression>] _ <element type>
3565// <extended element type> ::= <element type>
3566// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003567template <typename Derived, typename Alloc>
3568Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003569 if (!consumeIf("Dv"))
3570 return nullptr;
3571 if (look() >= '1' && look() <= '9') {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003572 Node *DimensionNumber = make<NameType>(parseNumber());
3573 if (!DimensionNumber)
3574 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003575 if (!consumeIf('_'))
3576 return nullptr;
3577 if (consumeIf('p'))
3578 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003579 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003580 if (ElemType == nullptr)
3581 return nullptr;
3582 return make<VectorType>(ElemType, DimensionNumber);
3583 }
3584
3585 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003586 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003587 if (!DimExpr)
3588 return nullptr;
3589 if (!consumeIf('_'))
3590 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003591 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003592 if (!ElemType)
3593 return nullptr;
3594 return make<VectorType>(ElemType, DimExpr);
3595 }
Pavel Labathba825192018-10-16 14:29:14 +00003596 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003597 if (!ElemType)
3598 return nullptr;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003599 return make<VectorType>(ElemType, /*Dimension=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003600}
3601
3602// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3603// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003604template <typename Derived, typename Alloc>
3605Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003606 if (!consumeIf('D'))
3607 return nullptr;
3608 if (!consumeIf('t') && !consumeIf('T'))
3609 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003610 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003611 if (E == nullptr)
3612 return nullptr;
3613 if (!consumeIf('E'))
3614 return nullptr;
3615 return make<EnclosingExpr>("decltype(", E, ")");
3616}
3617
3618// <array-type> ::= A <positive dimension number> _ <element type>
3619// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003620template <typename Derived, typename Alloc>
3621Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003622 if (!consumeIf('A'))
3623 return nullptr;
3624
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003625 Node *Dimension = nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003626
Richard Smithc20d1442018-08-20 20:14:49 +00003627 if (std::isdigit(look())) {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003628 Dimension = make<NameType>(parseNumber());
3629 if (!Dimension)
3630 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003631 if (!consumeIf('_'))
3632 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003633 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003634 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003635 if (DimExpr == nullptr)
3636 return nullptr;
3637 if (!consumeIf('_'))
3638 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003639 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003640 }
3641
Pavel Labathba825192018-10-16 14:29:14 +00003642 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003643 if (Ty == nullptr)
3644 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003645 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003646}
3647
3648// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003649template <typename Derived, typename Alloc>
3650Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003651 if (!consumeIf('M'))
3652 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003653 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003654 if (ClassType == nullptr)
3655 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003656 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003657 if (MemberType == nullptr)
3658 return nullptr;
3659 return make<PointerToMemberType>(ClassType, MemberType);
3660}
3661
3662// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3663// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3664// ::= Tu <name> # dependent elaborated type specifier using 'union'
3665// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003666template <typename Derived, typename Alloc>
3667Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003668 StringView ElabSpef;
3669 if (consumeIf("Ts"))
3670 ElabSpef = "struct";
3671 else if (consumeIf("Tu"))
3672 ElabSpef = "union";
3673 else if (consumeIf("Te"))
3674 ElabSpef = "enum";
3675
Pavel Labathba825192018-10-16 14:29:14 +00003676 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003677 if (Name == nullptr)
3678 return nullptr;
3679
3680 if (!ElabSpef.empty())
3681 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3682
3683 return Name;
3684}
3685
3686// <qualified-type> ::= <qualifiers> <type>
3687// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3688// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003689template <typename Derived, typename Alloc>
3690Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003691 if (consumeIf('U')) {
3692 StringView Qual = parseBareSourceName();
3693 if (Qual.empty())
3694 return nullptr;
3695
Richard Smithc20d1442018-08-20 20:14:49 +00003696 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3697 if (Qual.startsWith("objcproto")) {
3698 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3699 StringView Proto;
3700 {
3701 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3702 SaveLast(Last, ProtoSourceName.end());
3703 Proto = parseBareSourceName();
3704 }
3705 if (Proto.empty())
3706 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003707 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003708 if (Child == nullptr)
3709 return nullptr;
3710 return make<ObjCProtoName>(Child, Proto);
3711 }
3712
Alex Orlovf50df922021-03-24 10:21:32 +04003713 Node *TA = nullptr;
3714 if (look() == 'I') {
3715 TA = getDerived().parseTemplateArgs();
3716 if (TA == nullptr)
3717 return nullptr;
3718 }
3719
Pavel Labathba825192018-10-16 14:29:14 +00003720 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003721 if (Child == nullptr)
3722 return nullptr;
Alex Orlovf50df922021-03-24 10:21:32 +04003723 return make<VendorExtQualType>(Child, Qual, TA);
Richard Smithc20d1442018-08-20 20:14:49 +00003724 }
3725
3726 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003727 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003728 if (Ty == nullptr)
3729 return nullptr;
3730 if (Quals != QualNone)
3731 Ty = make<QualType>(Ty, Quals);
3732 return Ty;
3733}
3734
3735// <type> ::= <builtin-type>
3736// ::= <qualified-type>
3737// ::= <function-type>
3738// ::= <class-enum-type>
3739// ::= <array-type>
3740// ::= <pointer-to-member-type>
3741// ::= <template-param>
3742// ::= <template-template-param> <template-args>
3743// ::= <decltype>
3744// ::= P <type> # pointer
3745// ::= R <type> # l-value reference
3746// ::= O <type> # r-value reference (C++11)
3747// ::= C <type> # complex pair (C99)
3748// ::= G <type> # imaginary (C99)
3749// ::= <substitution> # See Compression below
3750// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3751// extension ::= <vector-type> # <vector-type> starts with Dv
3752//
3753// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3754// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003755template <typename Derived, typename Alloc>
3756Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003757 Node *Result = nullptr;
3758
Richard Smithc20d1442018-08-20 20:14:49 +00003759 switch (look()) {
3760 // ::= <qualified-type>
3761 case 'r':
3762 case 'V':
3763 case 'K': {
3764 unsigned AfterQuals = 0;
3765 if (look(AfterQuals) == 'r') ++AfterQuals;
3766 if (look(AfterQuals) == 'V') ++AfterQuals;
3767 if (look(AfterQuals) == 'K') ++AfterQuals;
3768
3769 if (look(AfterQuals) == 'F' ||
3770 (look(AfterQuals) == 'D' &&
3771 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3772 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003773 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003774 break;
3775 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003776 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003777 }
3778 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003779 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003780 break;
3781 }
3782 // <builtin-type> ::= v # void
3783 case 'v':
3784 ++First;
3785 return make<NameType>("void");
3786 // ::= w # wchar_t
3787 case 'w':
3788 ++First;
3789 return make<NameType>("wchar_t");
3790 // ::= b # bool
3791 case 'b':
3792 ++First;
3793 return make<NameType>("bool");
3794 // ::= c # char
3795 case 'c':
3796 ++First;
3797 return make<NameType>("char");
3798 // ::= a # signed char
3799 case 'a':
3800 ++First;
3801 return make<NameType>("signed char");
3802 // ::= h # unsigned char
3803 case 'h':
3804 ++First;
3805 return make<NameType>("unsigned char");
3806 // ::= s # short
3807 case 's':
3808 ++First;
3809 return make<NameType>("short");
3810 // ::= t # unsigned short
3811 case 't':
3812 ++First;
3813 return make<NameType>("unsigned short");
3814 // ::= i # int
3815 case 'i':
3816 ++First;
3817 return make<NameType>("int");
3818 // ::= j # unsigned int
3819 case 'j':
3820 ++First;
3821 return make<NameType>("unsigned int");
3822 // ::= l # long
3823 case 'l':
3824 ++First;
3825 return make<NameType>("long");
3826 // ::= m # unsigned long
3827 case 'm':
3828 ++First;
3829 return make<NameType>("unsigned long");
3830 // ::= x # long long, __int64
3831 case 'x':
3832 ++First;
3833 return make<NameType>("long long");
3834 // ::= y # unsigned long long, __int64
3835 case 'y':
3836 ++First;
3837 return make<NameType>("unsigned long long");
3838 // ::= n # __int128
3839 case 'n':
3840 ++First;
3841 return make<NameType>("__int128");
3842 // ::= o # unsigned __int128
3843 case 'o':
3844 ++First;
3845 return make<NameType>("unsigned __int128");
3846 // ::= f # float
3847 case 'f':
3848 ++First;
3849 return make<NameType>("float");
3850 // ::= d # double
3851 case 'd':
3852 ++First;
3853 return make<NameType>("double");
3854 // ::= e # long double, __float80
3855 case 'e':
3856 ++First;
3857 return make<NameType>("long double");
3858 // ::= g # __float128
3859 case 'g':
3860 ++First;
3861 return make<NameType>("__float128");
3862 // ::= z # ellipsis
3863 case 'z':
3864 ++First;
3865 return make<NameType>("...");
3866
3867 // <builtin-type> ::= u <source-name> # vendor extended type
3868 case 'u': {
3869 ++First;
3870 StringView Res = parseBareSourceName();
3871 if (Res.empty())
3872 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003873 // Typically, <builtin-type>s are not considered substitution candidates,
3874 // but the exception to that exception is vendor extended types (Itanium C++
3875 // ABI 5.9.1).
3876 Result = make<NameType>(Res);
3877 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003878 }
3879 case 'D':
3880 switch (look(1)) {
3881 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3882 case 'd':
3883 First += 2;
3884 return make<NameType>("decimal64");
3885 // ::= De # IEEE 754r decimal floating point (128 bits)
3886 case 'e':
3887 First += 2;
3888 return make<NameType>("decimal128");
3889 // ::= Df # IEEE 754r decimal floating point (32 bits)
3890 case 'f':
3891 First += 2;
3892 return make<NameType>("decimal32");
3893 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3894 case 'h':
3895 First += 2;
Stuart Bradye8bf5772021-06-07 16:30:22 +01003896 return make<NameType>("half");
Pengfei Wang50e90b82021-09-23 11:02:25 +08003897 // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits)
3898 case 'F': {
3899 First += 2;
3900 Node *DimensionNumber = make<NameType>(parseNumber());
3901 if (!DimensionNumber)
3902 return nullptr;
3903 if (!consumeIf('_'))
3904 return nullptr;
3905 return make<BinaryFPType>(DimensionNumber);
3906 }
Richard Smithc20d1442018-08-20 20:14:49 +00003907 // ::= Di # char32_t
3908 case 'i':
3909 First += 2;
3910 return make<NameType>("char32_t");
3911 // ::= Ds # char16_t
3912 case 's':
3913 First += 2;
3914 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003915 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3916 case 'u':
3917 First += 2;
3918 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003919 // ::= Da # auto (in dependent new-expressions)
3920 case 'a':
3921 First += 2;
3922 return make<NameType>("auto");
3923 // ::= Dc # decltype(auto)
3924 case 'c':
3925 First += 2;
3926 return make<NameType>("decltype(auto)");
3927 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3928 case 'n':
3929 First += 2;
3930 return make<NameType>("std::nullptr_t");
3931
3932 // ::= <decltype>
3933 case 't':
3934 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003935 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003936 break;
3937 }
3938 // extension ::= <vector-type> # <vector-type> starts with Dv
3939 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003940 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003941 break;
3942 }
3943 // ::= Dp <type> # pack expansion (C++0x)
3944 case 'p': {
3945 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003946 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003947 if (!Child)
3948 return nullptr;
3949 Result = make<ParameterPackExpansion>(Child);
3950 break;
3951 }
3952 // Exception specifier on a function type.
3953 case 'o':
3954 case 'O':
3955 case 'w':
3956 // Transaction safe function type.
3957 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003958 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003959 break;
3960 }
3961 break;
3962 // ::= <function-type>
3963 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003964 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003965 break;
3966 }
3967 // ::= <array-type>
3968 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003969 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003970 break;
3971 }
3972 // ::= <pointer-to-member-type>
3973 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003974 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003975 break;
3976 }
3977 // ::= <template-param>
3978 case 'T': {
3979 // This could be an elaborate type specifier on a <class-enum-type>.
3980 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003981 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003982 break;
3983 }
3984
Pavel Labathba825192018-10-16 14:29:14 +00003985 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003986 if (Result == nullptr)
3987 return nullptr;
3988
3989 // Result could be either of:
3990 // <type> ::= <template-param>
3991 // <type> ::= <template-template-param> <template-args>
3992 //
3993 // <template-template-param> ::= <template-param>
3994 // ::= <substitution>
3995 //
3996 // If this is followed by some <template-args>, and we're permitted to
3997 // parse them, take the second production.
3998
3999 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004000 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004001 if (TA == nullptr)
4002 return nullptr;
4003 Result = make<NameWithTemplateArgs>(Result, TA);
4004 }
4005 break;
4006 }
4007 // ::= P <type> # pointer
4008 case 'P': {
4009 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004010 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004011 if (Ptr == nullptr)
4012 return nullptr;
4013 Result = make<PointerType>(Ptr);
4014 break;
4015 }
4016 // ::= R <type> # l-value reference
4017 case 'R': {
4018 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004019 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004020 if (Ref == nullptr)
4021 return nullptr;
4022 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
4023 break;
4024 }
4025 // ::= O <type> # r-value reference (C++11)
4026 case 'O': {
4027 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004028 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004029 if (Ref == nullptr)
4030 return nullptr;
4031 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
4032 break;
4033 }
4034 // ::= C <type> # complex pair (C99)
4035 case 'C': {
4036 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004037 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004038 if (P == nullptr)
4039 return nullptr;
4040 Result = make<PostfixQualifiedType>(P, " complex");
4041 break;
4042 }
4043 // ::= G <type> # imaginary (C99)
4044 case 'G': {
4045 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004046 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004047 if (P == nullptr)
4048 return P;
4049 Result = make<PostfixQualifiedType>(P, " imaginary");
4050 break;
4051 }
4052 // ::= <substitution> # See Compression below
4053 case 'S': {
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004054 if (look(1) != 't') {
4055 Result = getDerived().parseSubstitution();
4056 if (Result == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00004057 return nullptr;
4058
4059 // Sub could be either of:
4060 // <type> ::= <substitution>
4061 // <type> ::= <template-template-param> <template-args>
4062 //
4063 // <template-template-param> ::= <template-param>
4064 // ::= <substitution>
4065 //
4066 // If this is followed by some <template-args>, and we're permitted to
4067 // parse them, take the second production.
4068
4069 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004070 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004071 if (TA == nullptr)
4072 return nullptr;
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004073 Result = make<NameWithTemplateArgs>(Result, TA);
4074 } else {
4075 // If all we parsed was a substitution, don't re-insert into the
4076 // substitution table.
4077 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00004078 }
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004079 break;
Richard Smithc20d1442018-08-20 20:14:49 +00004080 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00004081 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004082 }
4083 // ::= <class-enum-type>
4084 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004085 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004086 break;
4087 }
4088 }
4089
4090 // If we parsed a type, insert it into the substitution table. Note that all
4091 // <builtin-type>s and <substitution>s have already bailed out, because they
4092 // don't get substitutions.
4093 if (Result != nullptr)
4094 Subs.push_back(Result);
4095 return Result;
4096}
4097
Pavel Labathba825192018-10-16 14:29:14 +00004098template <typename Derived, typename Alloc>
4099Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4100 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004101 if (E == nullptr)
4102 return nullptr;
4103 return make<PrefixExpr>(Kind, E);
4104}
4105
Pavel Labathba825192018-10-16 14:29:14 +00004106template <typename Derived, typename Alloc>
4107Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4108 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004109 if (LHS == nullptr)
4110 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004111 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004112 if (RHS == nullptr)
4113 return nullptr;
4114 return make<BinaryExpr>(LHS, Kind, RHS);
4115}
4116
Pavel Labathba825192018-10-16 14:29:14 +00004117template <typename Derived, typename Alloc>
4118Node *
4119AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004120 StringView Tmp = parseNumber(true);
4121 if (!Tmp.empty() && consumeIf('E'))
4122 return make<IntegerLiteral>(Lit, Tmp);
4123 return nullptr;
4124}
4125
4126// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004127template <typename Alloc, typename Derived>
4128Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004129 Qualifiers CVR = QualNone;
4130 if (consumeIf('r'))
4131 CVR |= QualRestrict;
4132 if (consumeIf('V'))
4133 CVR |= QualVolatile;
4134 if (consumeIf('K'))
4135 CVR |= QualConst;
4136 return CVR;
4137}
4138
4139// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4140// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4141// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4142// ::= 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 -04004143// ::= fpT # 'this' expression (not part of standard?)
Pavel Labathba825192018-10-16 14:29:14 +00004144template <typename Derived, typename Alloc>
4145Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Erik Pilkington91c24af2020-05-13 22:19:45 -04004146 if (consumeIf("fpT"))
4147 return make<NameType>("this");
Richard Smithc20d1442018-08-20 20:14:49 +00004148 if (consumeIf("fp")) {
4149 parseCVQualifiers();
4150 StringView Num = parseNumber();
4151 if (!consumeIf('_'))
4152 return nullptr;
4153 return make<FunctionParam>(Num);
4154 }
4155 if (consumeIf("fL")) {
4156 if (parseNumber().empty())
4157 return nullptr;
4158 if (!consumeIf('p'))
4159 return nullptr;
4160 parseCVQualifiers();
4161 StringView Num = parseNumber();
4162 if (!consumeIf('_'))
4163 return nullptr;
4164 return make<FunctionParam>(Num);
4165 }
4166 return nullptr;
4167}
4168
Richard Smithc20d1442018-08-20 20:14:49 +00004169// cv <type> <expression> # conversion with one argument
4170// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004171template <typename Derived, typename Alloc>
4172Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004173 if (!consumeIf("cv"))
4174 return nullptr;
4175 Node *Ty;
4176 {
4177 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004178 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004179 }
4180
4181 if (Ty == nullptr)
4182 return nullptr;
4183
4184 if (consumeIf('_')) {
4185 size_t ExprsBegin = Names.size();
4186 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004187 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004188 if (E == nullptr)
4189 return E;
4190 Names.push_back(E);
4191 }
4192 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4193 return make<ConversionExpr>(Ty, Exprs);
4194 }
4195
Pavel Labathba825192018-10-16 14:29:14 +00004196 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004197 if (E[0] == nullptr)
4198 return nullptr;
4199 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4200}
4201
4202// <expr-primary> ::= L <type> <value number> E # integer literal
4203// ::= L <type> <value float> E # floating literal
4204// ::= L <string type> E # string literal
4205// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004206// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004207// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4208// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004209template <typename Derived, typename Alloc>
4210Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004211 if (!consumeIf('L'))
4212 return nullptr;
4213 switch (look()) {
4214 case 'w':
4215 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004216 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004217 case 'b':
4218 if (consumeIf("b0E"))
4219 return make<BoolExpr>(0);
4220 if (consumeIf("b1E"))
4221 return make<BoolExpr>(1);
4222 return nullptr;
4223 case 'c':
4224 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004225 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004226 case 'a':
4227 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004228 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004229 case 'h':
4230 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004231 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004232 case 's':
4233 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004234 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004235 case 't':
4236 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004237 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004238 case 'i':
4239 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004240 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004241 case 'j':
4242 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004243 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004244 case 'l':
4245 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004246 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004247 case 'm':
4248 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004249 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004250 case 'x':
4251 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004252 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004253 case 'y':
4254 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004255 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004256 case 'n':
4257 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004258 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004259 case 'o':
4260 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004261 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004262 case 'f':
4263 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004264 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004265 case 'd':
4266 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004267 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004268 case 'e':
4269 ++First;
Xing Xue3dc5e082020-04-15 09:59:06 -04004270#if defined(__powerpc__) || defined(__s390__)
4271 // Handle cases where long doubles encoded with e have the same size
4272 // and representation as doubles.
4273 return getDerived().template parseFloatingLiteral<double>();
4274#else
Pavel Labathba825192018-10-16 14:29:14 +00004275 return getDerived().template parseFloatingLiteral<long double>();
Xing Xue3dc5e082020-04-15 09:59:06 -04004276#endif
Richard Smithc20d1442018-08-20 20:14:49 +00004277 case '_':
4278 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004279 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004280 if (R != nullptr && consumeIf('E'))
4281 return R;
4282 }
4283 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004284 case 'A': {
4285 Node *T = getDerived().parseType();
4286 if (T == nullptr)
4287 return nullptr;
4288 // FIXME: We need to include the string contents in the mangling.
4289 if (consumeIf('E'))
4290 return make<StringLiteral>(T);
4291 return nullptr;
4292 }
4293 case 'D':
4294 if (consumeIf("DnE"))
4295 return make<NameType>("nullptr");
4296 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004297 case 'T':
4298 // Invalid mangled name per
4299 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4300 return nullptr;
Richard Smithfb917462019-09-09 22:26:04 +00004301 case 'U': {
4302 // FIXME: Should we support LUb... for block literals?
4303 if (look(1) != 'l')
4304 return nullptr;
4305 Node *T = parseUnnamedTypeName(nullptr);
4306 if (!T || !consumeIf('E'))
4307 return nullptr;
4308 return make<LambdaExpr>(T);
4309 }
Richard Smithc20d1442018-08-20 20:14:49 +00004310 default: {
4311 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004312 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004313 if (T == nullptr)
4314 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004315 StringView N = parseNumber(/*AllowNegative=*/true);
Richard Smithfb917462019-09-09 22:26:04 +00004316 if (N.empty())
4317 return nullptr;
4318 if (!consumeIf('E'))
4319 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004320 return make<EnumLiteral>(T, N);
Richard Smithc20d1442018-08-20 20:14:49 +00004321 }
4322 }
4323}
4324
4325// <braced-expression> ::= <expression>
4326// ::= di <field source-name> <braced-expression> # .name = expr
4327// ::= dx <index expression> <braced-expression> # [expr] = expr
4328// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004329template <typename Derived, typename Alloc>
4330Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004331 if (look() == 'd') {
4332 switch (look(1)) {
4333 case 'i': {
4334 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004335 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004336 if (Field == nullptr)
4337 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004338 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004339 if (Init == nullptr)
4340 return nullptr;
4341 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4342 }
4343 case 'x': {
4344 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004345 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004346 if (Index == nullptr)
4347 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004348 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004349 if (Init == nullptr)
4350 return nullptr;
4351 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4352 }
4353 case 'X': {
4354 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004355 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004356 if (RangeBegin == nullptr)
4357 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004358 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004359 if (RangeEnd == nullptr)
4360 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004361 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004362 if (Init == nullptr)
4363 return nullptr;
4364 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4365 }
4366 }
4367 }
Pavel Labathba825192018-10-16 14:29:14 +00004368 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004369}
4370
4371// (not yet in the spec)
4372// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4373// ::= fR <binary-operator-name> <expression> <expression>
4374// ::= fl <binary-operator-name> <expression>
4375// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004376template <typename Derived, typename Alloc>
4377Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004378 if (!consumeIf('f'))
4379 return nullptr;
4380
4381 char FoldKind = look();
4382 bool IsLeftFold, HasInitializer;
4383 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4384 if (FoldKind == 'l' || FoldKind == 'L')
4385 IsLeftFold = true;
4386 else if (FoldKind == 'r' || FoldKind == 'R')
4387 IsLeftFold = false;
4388 else
4389 return nullptr;
4390 ++First;
4391
4392 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4393 StringView OperatorName;
4394 if (consumeIf("aa")) OperatorName = "&&";
4395 else if (consumeIf("an")) OperatorName = "&";
4396 else if (consumeIf("aN")) OperatorName = "&=";
4397 else if (consumeIf("aS")) OperatorName = "=";
4398 else if (consumeIf("cm")) OperatorName = ",";
4399 else if (consumeIf("ds")) OperatorName = ".*";
4400 else if (consumeIf("dv")) OperatorName = "/";
4401 else if (consumeIf("dV")) OperatorName = "/=";
4402 else if (consumeIf("eo")) OperatorName = "^";
4403 else if (consumeIf("eO")) OperatorName = "^=";
4404 else if (consumeIf("eq")) OperatorName = "==";
4405 else if (consumeIf("ge")) OperatorName = ">=";
4406 else if (consumeIf("gt")) OperatorName = ">";
4407 else if (consumeIf("le")) OperatorName = "<=";
4408 else if (consumeIf("ls")) OperatorName = "<<";
4409 else if (consumeIf("lS")) OperatorName = "<<=";
4410 else if (consumeIf("lt")) OperatorName = "<";
4411 else if (consumeIf("mi")) OperatorName = "-";
4412 else if (consumeIf("mI")) OperatorName = "-=";
4413 else if (consumeIf("ml")) OperatorName = "*";
4414 else if (consumeIf("mL")) OperatorName = "*=";
4415 else if (consumeIf("ne")) OperatorName = "!=";
4416 else if (consumeIf("oo")) OperatorName = "||";
4417 else if (consumeIf("or")) OperatorName = "|";
4418 else if (consumeIf("oR")) OperatorName = "|=";
4419 else if (consumeIf("pl")) OperatorName = "+";
4420 else if (consumeIf("pL")) OperatorName = "+=";
4421 else if (consumeIf("rm")) OperatorName = "%";
4422 else if (consumeIf("rM")) OperatorName = "%=";
4423 else if (consumeIf("rs")) OperatorName = ">>";
4424 else if (consumeIf("rS")) OperatorName = ">>=";
4425 else return nullptr;
4426
Pavel Labathba825192018-10-16 14:29:14 +00004427 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004428 if (Pack == nullptr)
4429 return nullptr;
4430 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004431 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004432 if (Init == nullptr)
4433 return nullptr;
4434 }
4435
4436 if (IsLeftFold && Init)
4437 std::swap(Pack, Init);
4438
4439 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4440}
4441
Richard Smith1865d2f2020-10-22 19:29:36 -07004442// <expression> ::= mc <parameter type> <expr> [<offset number>] E
4443//
4444// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4445template <typename Derived, typename Alloc>
4446Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() {
4447 Node *Ty = getDerived().parseType();
4448 if (!Ty)
4449 return nullptr;
4450 Node *Expr = getDerived().parseExpr();
4451 if (!Expr)
4452 return nullptr;
4453 StringView Offset = getDerived().parseNumber(true);
4454 if (!consumeIf('E'))
4455 return nullptr;
4456 return make<PointerToMemberConversionExpr>(Ty, Expr, Offset);
4457}
4458
4459// <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
4460// <union-selector> ::= _ [<number>]
4461//
4462// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4463template <typename Derived, typename Alloc>
4464Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
4465 Node *Ty = getDerived().parseType();
4466 if (!Ty)
4467 return nullptr;
4468 Node *Expr = getDerived().parseExpr();
4469 if (!Expr)
4470 return nullptr;
4471 StringView Offset = getDerived().parseNumber(true);
4472 size_t SelectorsBegin = Names.size();
4473 while (consumeIf('_')) {
4474 Node *Selector = make<NameType>(parseNumber());
4475 if (!Selector)
4476 return nullptr;
4477 Names.push_back(Selector);
4478 }
4479 bool OnePastTheEnd = consumeIf('p');
4480 if (!consumeIf('E'))
4481 return nullptr;
4482 return make<SubobjectExpr>(
4483 Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
4484}
4485
Richard Smithc20d1442018-08-20 20:14:49 +00004486// <expression> ::= <unary operator-name> <expression>
4487// ::= <binary operator-name> <expression> <expression>
4488// ::= <ternary operator-name> <expression> <expression> <expression>
4489// ::= cl <expression>+ E # call
4490// ::= cv <type> <expression> # conversion with one argument
4491// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4492// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4493// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4494// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4495// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4496// ::= [gs] dl <expression> # delete expression
4497// ::= [gs] da <expression> # delete[] expression
4498// ::= pp_ <expression> # prefix ++
4499// ::= mm_ <expression> # prefix --
4500// ::= ti <type> # typeid (type)
4501// ::= te <expression> # typeid (expression)
4502// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4503// ::= sc <type> <expression> # static_cast<type> (expression)
4504// ::= cc <type> <expression> # const_cast<type> (expression)
4505// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4506// ::= st <type> # sizeof (a type)
4507// ::= sz <expression> # sizeof (an expression)
4508// ::= at <type> # alignof (a type)
4509// ::= az <expression> # alignof (an expression)
4510// ::= nx <expression> # noexcept (expression)
4511// ::= <template-param>
4512// ::= <function-param>
4513// ::= dt <expression> <unresolved-name> # expr.name
4514// ::= pt <expression> <unresolved-name> # expr->name
4515// ::= ds <expression> <expression> # expr.*expr
4516// ::= sZ <template-param> # size of a parameter pack
4517// ::= sZ <function-param> # size of a function parameter pack
4518// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4519// ::= sp <expression> # pack expansion
4520// ::= tw <expression> # throw expression
4521// ::= tr # throw with no operand (rethrow)
4522// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4523// # freestanding dependent name (e.g., T::x),
4524// # objectless nonstatic member reference
4525// ::= fL <binary-operator-name> <expression> <expression>
4526// ::= fR <binary-operator-name> <expression> <expression>
4527// ::= fl <binary-operator-name> <expression>
4528// ::= fr <binary-operator-name> <expression>
4529// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004530template <typename Derived, typename Alloc>
4531Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004532 bool Global = consumeIf("gs");
4533 if (numLeft() < 2)
4534 return nullptr;
4535
4536 switch (*First) {
4537 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004538 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004539 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004540 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004541 case 'f': {
4542 // Disambiguate a fold expression from a <function-param>.
4543 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004544 return getDerived().parseFunctionParam();
4545 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004546 }
4547 case 'a':
4548 switch (First[1]) {
4549 case 'a':
4550 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004551 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004552 case 'd':
4553 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004554 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004555 case 'n':
4556 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004557 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004558 case 'N':
4559 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004560 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004561 case 'S':
4562 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004563 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004564 case 't': {
4565 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004566 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004567 if (Ty == nullptr)
4568 return nullptr;
4569 return make<EnclosingExpr>("alignof (", Ty, ")");
4570 }
4571 case 'z': {
4572 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004573 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004574 if (Ty == nullptr)
4575 return nullptr;
4576 return make<EnclosingExpr>("alignof (", Ty, ")");
4577 }
4578 }
4579 return nullptr;
4580 case 'c':
4581 switch (First[1]) {
4582 // cc <type> <expression> # const_cast<type>(expression)
4583 case 'c': {
4584 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004585 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004586 if (Ty == nullptr)
4587 return Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004588 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004589 if (Ex == nullptr)
4590 return Ex;
4591 return make<CastExpr>("const_cast", Ty, Ex);
4592 }
4593 // cl <expression>+ E # call
4594 case 'l': {
4595 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004596 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004597 if (Callee == nullptr)
4598 return Callee;
4599 size_t ExprsBegin = Names.size();
4600 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004601 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004602 if (E == nullptr)
4603 return E;
4604 Names.push_back(E);
4605 }
4606 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4607 }
4608 case 'm':
4609 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004610 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004611 case 'o':
4612 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004613 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004614 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004615 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004616 }
4617 return nullptr;
4618 case 'd':
4619 switch (First[1]) {
4620 case 'a': {
4621 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004622 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004623 if (Ex == nullptr)
4624 return Ex;
4625 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4626 }
4627 case 'c': {
4628 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004629 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004630 if (T == nullptr)
4631 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004632 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004633 if (Ex == nullptr)
4634 return Ex;
4635 return make<CastExpr>("dynamic_cast", T, Ex);
4636 }
4637 case 'e':
4638 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004639 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004640 case 'l': {
4641 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004642 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004643 if (E == nullptr)
4644 return E;
4645 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4646 }
4647 case 'n':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08004648 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004649 case 's': {
4650 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004651 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004652 if (LHS == nullptr)
4653 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004654 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004655 if (RHS == nullptr)
4656 return nullptr;
4657 return make<MemberExpr>(LHS, ".*", RHS);
4658 }
4659 case 't': {
4660 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004661 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004662 if (LHS == nullptr)
4663 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004664 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004665 if (RHS == nullptr)
4666 return nullptr;
4667 return make<MemberExpr>(LHS, ".", RHS);
4668 }
4669 case 'v':
4670 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004671 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004672 case 'V':
4673 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004674 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004675 }
4676 return nullptr;
4677 case 'e':
4678 switch (First[1]) {
4679 case 'o':
4680 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004681 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004682 case 'O':
4683 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004684 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004685 case 'q':
4686 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004687 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004688 }
4689 return nullptr;
4690 case 'g':
4691 switch (First[1]) {
4692 case 'e':
4693 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004694 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004695 case 't':
4696 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004697 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004698 }
4699 return nullptr;
4700 case 'i':
4701 switch (First[1]) {
4702 case 'x': {
4703 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004704 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004705 if (Base == nullptr)
4706 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004707 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004708 if (Index == nullptr)
4709 return Index;
4710 return make<ArraySubscriptExpr>(Base, Index);
4711 }
4712 case 'l': {
4713 First += 2;
4714 size_t InitsBegin = Names.size();
4715 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004716 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004717 if (E == nullptr)
4718 return nullptr;
4719 Names.push_back(E);
4720 }
4721 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4722 }
4723 }
4724 return nullptr;
4725 case 'l':
4726 switch (First[1]) {
4727 case 'e':
4728 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004729 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004730 case 's':
4731 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004732 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004733 case 'S':
4734 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004735 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004736 case 't':
4737 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004738 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004739 }
4740 return nullptr;
4741 case 'm':
4742 switch (First[1]) {
Richard Smith1865d2f2020-10-22 19:29:36 -07004743 case 'c':
4744 First += 2;
4745 return parsePointerToMemberConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004746 case 'i':
4747 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004748 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004749 case 'I':
4750 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004751 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004752 case 'l':
4753 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004754 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004755 case 'L':
4756 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004757 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004758 case 'm':
4759 First += 2;
4760 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004761 return getDerived().parsePrefixExpr("--");
4762 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004763 if (Ex == nullptr)
4764 return nullptr;
4765 return make<PostfixExpr>(Ex, "--");
4766 }
4767 return nullptr;
4768 case 'n':
4769 switch (First[1]) {
4770 case 'a':
Nathan Sidwellc69bde22022-01-28 07:09:38 -08004771 case 'w': {
4772 // [gs] nw <expression>* _ <type> [pi <expression>*] E # new (expr-list) type [(init)]
4773 // [gs] na <expression>* _ <type> [pi <expression>*] E # new[] (expr-list) type [(init)]
4774 bool IsArray = First[1] == 'a';
4775 First += 2;
4776 size_t Exprs = Names.size();
4777 while (!consumeIf('_')) {
4778 Node *Ex = getDerived().parseExpr();
4779 if (Ex == nullptr)
4780 return nullptr;
4781 Names.push_back(Ex);
4782 }
4783 NodeArray ExprList = popTrailingNodeArray(Exprs);
4784 Node *Ty = getDerived().parseType();
4785 if (Ty == nullptr)
4786 return nullptr;
4787 bool HaveInits = consumeIf("pi");
4788 size_t InitsBegin = Names.size();
4789 while (!consumeIf('E')) {
4790 if (!HaveInits)
4791 return nullptr;
4792 Node *Init = getDerived().parseExpr();
4793 if (Init == nullptr)
4794 return Init;
4795 Names.push_back(Init);
4796 }
4797 NodeArray Inits = popTrailingNodeArray(InitsBegin);
4798 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
4799 }
Richard Smithc20d1442018-08-20 20:14:49 +00004800 case 'e':
4801 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004802 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004803 case 'g':
4804 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004805 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004806 case 't':
4807 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004808 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004809 case 'x':
4810 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004811 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004812 if (Ex == nullptr)
4813 return Ex;
4814 return make<EnclosingExpr>("noexcept (", Ex, ")");
4815 }
4816 return nullptr;
4817 case 'o':
4818 switch (First[1]) {
4819 case 'n':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08004820 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004821 case 'o':
4822 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004823 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004824 case 'r':
4825 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004826 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004827 case 'R':
4828 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004829 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004830 }
4831 return nullptr;
4832 case 'p':
4833 switch (First[1]) {
Nathan Sidwellc6483042022-01-28 09:27:28 -08004834 case 'm': {
Richard Smithc20d1442018-08-20 20:14:49 +00004835 First += 2;
Nathan Sidwellc6483042022-01-28 09:27:28 -08004836 Node *LHS = getDerived().parseExpr();
4837 if (LHS == nullptr)
4838 return LHS;
4839 Node *RHS = getDerived().parseExpr();
4840 if (RHS == nullptr)
4841 return nullptr;
4842 return make<MemberExpr>(LHS, "->*", RHS);
4843 }
Richard Smithc20d1442018-08-20 20:14:49 +00004844 case 'l':
4845 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004846 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004847 case 'L':
4848 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004849 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004850 case 'p': {
4851 First += 2;
4852 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004853 return getDerived().parsePrefixExpr("++");
4854 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004855 if (Ex == nullptr)
4856 return Ex;
4857 return make<PostfixExpr>(Ex, "++");
4858 }
4859 case 's':
4860 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004861 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004862 case 't': {
4863 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004864 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004865 if (L == nullptr)
4866 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004867 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004868 if (R == nullptr)
4869 return nullptr;
4870 return make<MemberExpr>(L, "->", R);
4871 }
4872 }
4873 return nullptr;
4874 case 'q':
4875 if (First[1] == 'u') {
4876 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004877 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004878 if (Cond == nullptr)
4879 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004880 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004881 if (LHS == nullptr)
4882 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004883 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004884 if (RHS == nullptr)
4885 return nullptr;
4886 return make<ConditionalExpr>(Cond, LHS, RHS);
4887 }
4888 return nullptr;
4889 case 'r':
4890 switch (First[1]) {
4891 case 'c': {
4892 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004893 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004894 if (T == nullptr)
4895 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004896 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004897 if (Ex == nullptr)
4898 return Ex;
4899 return make<CastExpr>("reinterpret_cast", T, Ex);
4900 }
4901 case 'm':
4902 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004903 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004904 case 'M':
4905 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004906 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004907 case 's':
4908 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004909 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004910 case 'S':
4911 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004912 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004913 }
4914 return nullptr;
4915 case 's':
4916 switch (First[1]) {
4917 case 'c': {
4918 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004919 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004920 if (T == nullptr)
4921 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004922 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004923 if (Ex == nullptr)
4924 return Ex;
4925 return make<CastExpr>("static_cast", T, Ex);
4926 }
Richard Smith1865d2f2020-10-22 19:29:36 -07004927 case 'o':
4928 First += 2;
4929 return parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004930 case 'p': {
4931 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004932 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004933 if (Child == nullptr)
4934 return nullptr;
4935 return make<ParameterPackExpansion>(Child);
4936 }
4937 case 'r':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08004938 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004939 case 't': {
4940 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004941 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004942 if (Ty == nullptr)
4943 return Ty;
4944 return make<EnclosingExpr>("sizeof (", Ty, ")");
4945 }
4946 case 'z': {
4947 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004948 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004949 if (Ex == nullptr)
4950 return Ex;
4951 return make<EnclosingExpr>("sizeof (", Ex, ")");
4952 }
4953 case 'Z':
4954 First += 2;
4955 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004956 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004957 if (R == nullptr)
4958 return nullptr;
4959 return make<SizeofParamPackExpr>(R);
4960 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004961 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004962 if (FP == nullptr)
4963 return nullptr;
4964 return make<EnclosingExpr>("sizeof... (", FP, ")");
4965 }
4966 return nullptr;
4967 case 'P': {
4968 First += 2;
4969 size_t ArgsBegin = Names.size();
4970 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004971 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004972 if (Arg == nullptr)
4973 return nullptr;
4974 Names.push_back(Arg);
4975 }
Richard Smithb485b352018-08-24 23:30:26 +00004976 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4977 if (!Pack)
4978 return nullptr;
4979 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00004980 }
4981 }
4982 return nullptr;
4983 case 't':
4984 switch (First[1]) {
4985 case 'e': {
4986 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004987 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004988 if (Ex == nullptr)
4989 return Ex;
4990 return make<EnclosingExpr>("typeid (", Ex, ")");
4991 }
4992 case 'i': {
4993 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004994 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004995 if (Ty == nullptr)
4996 return Ty;
4997 return make<EnclosingExpr>("typeid (", Ty, ")");
4998 }
4999 case 'l': {
5000 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005001 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005002 if (Ty == nullptr)
5003 return nullptr;
5004 size_t InitsBegin = Names.size();
5005 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005006 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005007 if (E == nullptr)
5008 return nullptr;
5009 Names.push_back(E);
5010 }
5011 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
5012 }
5013 case 'r':
5014 First += 2;
5015 return make<NameType>("throw");
5016 case 'w': {
5017 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005018 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005019 if (Ex == nullptr)
5020 return nullptr;
5021 return make<ThrowExpr>(Ex);
5022 }
5023 }
5024 return nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05005025 case 'u': {
5026 ++First;
5027 Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
5028 if (!Name)
5029 return nullptr;
5030 // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
5031 // standard encoding expects a <template-arg>, and would be otherwise be
5032 // interpreted as <type> node 'short' or 'ellipsis'. However, neither
5033 // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
5034 // actual conflict here.
Nathan Sidwella3b59002022-02-11 05:54:40 -08005035 bool IsUUID = false;
5036 Node *UUID = nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05005037 if (Name->getBaseName() == "__uuidof") {
Nathan Sidwella3b59002022-02-11 05:54:40 -08005038 if (consumeIf('t')) {
5039 UUID = getDerived().parseType();
5040 IsUUID = true;
5041 } else if (consumeIf('z')) {
5042 UUID = getDerived().parseExpr();
5043 IsUUID = true;
James Y Knight4a60efc2020-12-07 10:26:49 -05005044 }
5045 }
5046 size_t ExprsBegin = Names.size();
Nathan Sidwella3b59002022-02-11 05:54:40 -08005047 if (IsUUID) {
5048 if (UUID == nullptr)
5049 return nullptr;
5050 Names.push_back(UUID);
5051 } else {
5052 while (!consumeIf('E')) {
5053 Node *E = getDerived().parseTemplateArg();
5054 if (E == nullptr)
5055 return E;
5056 Names.push_back(E);
5057 }
James Y Knight4a60efc2020-12-07 10:26:49 -05005058 }
5059 return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin));
5060 }
Richard Smithc20d1442018-08-20 20:14:49 +00005061 case '1':
5062 case '2':
5063 case '3':
5064 case '4':
5065 case '5':
5066 case '6':
5067 case '7':
5068 case '8':
5069 case '9':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08005070 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00005071 }
5072 return nullptr;
5073}
5074
5075// <call-offset> ::= h <nv-offset> _
5076// ::= v <v-offset> _
5077//
5078// <nv-offset> ::= <offset number>
5079// # non-virtual base override
5080//
5081// <v-offset> ::= <offset number> _ <virtual offset number>
5082// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00005083template <typename Alloc, typename Derived>
5084bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00005085 // Just scan through the call offset, we never add this information into the
5086 // output.
5087 if (consumeIf('h'))
5088 return parseNumber(true).empty() || !consumeIf('_');
5089 if (consumeIf('v'))
5090 return parseNumber(true).empty() || !consumeIf('_') ||
5091 parseNumber(true).empty() || !consumeIf('_');
5092 return true;
5093}
5094
5095// <special-name> ::= TV <type> # virtual table
5096// ::= TT <type> # VTT structure (construction vtable index)
5097// ::= TI <type> # typeinfo structure
5098// ::= TS <type> # typeinfo name (null-terminated byte string)
5099// ::= Tc <call-offset> <call-offset> <base encoding>
5100// # base is the nominal target function of thunk
5101// # first call-offset is 'this' adjustment
5102// # second call-offset is result adjustment
5103// ::= T <call-offset> <base encoding>
5104// # base is the nominal target function of thunk
5105// ::= GV <object name> # Guard variable for one-time initialization
5106// # No <type>
5107// ::= TW <object name> # Thread-local wrapper
5108// ::= TH <object name> # Thread-local initialization
5109// ::= GR <object name> _ # First temporary
5110// ::= GR <object name> <seq-id> _ # Subsequent temporaries
5111// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
5112// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00005113template <typename Derived, typename Alloc>
5114Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00005115 switch (look()) {
5116 case 'T':
5117 switch (look(1)) {
Richard Smith1865d2f2020-10-22 19:29:36 -07005118 // TA <template-arg> # template parameter object
5119 //
5120 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
5121 case 'A': {
5122 First += 2;
5123 Node *Arg = getDerived().parseTemplateArg();
5124 if (Arg == nullptr)
5125 return nullptr;
5126 return make<SpecialName>("template parameter object for ", Arg);
5127 }
Richard Smithc20d1442018-08-20 20:14:49 +00005128 // TV <type> # virtual table
5129 case 'V': {
5130 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005131 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005132 if (Ty == nullptr)
5133 return nullptr;
5134 return make<SpecialName>("vtable for ", Ty);
5135 }
5136 // TT <type> # VTT structure (construction vtable index)
5137 case 'T': {
5138 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005139 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005140 if (Ty == nullptr)
5141 return nullptr;
5142 return make<SpecialName>("VTT for ", Ty);
5143 }
5144 // TI <type> # typeinfo structure
5145 case 'I': {
5146 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005147 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005148 if (Ty == nullptr)
5149 return nullptr;
5150 return make<SpecialName>("typeinfo for ", Ty);
5151 }
5152 // TS <type> # typeinfo name (null-terminated byte string)
5153 case 'S': {
5154 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005155 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005156 if (Ty == nullptr)
5157 return nullptr;
5158 return make<SpecialName>("typeinfo name for ", Ty);
5159 }
5160 // Tc <call-offset> <call-offset> <base encoding>
5161 case 'c': {
5162 First += 2;
5163 if (parseCallOffset() || parseCallOffset())
5164 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005165 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005166 if (Encoding == nullptr)
5167 return nullptr;
5168 return make<SpecialName>("covariant return thunk to ", Encoding);
5169 }
5170 // extension ::= TC <first type> <number> _ <second type>
5171 // # construction vtable for second-in-first
5172 case 'C': {
5173 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005174 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005175 if (FirstType == nullptr)
5176 return nullptr;
5177 if (parseNumber(true).empty() || !consumeIf('_'))
5178 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005179 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005180 if (SecondType == nullptr)
5181 return nullptr;
5182 return make<CtorVtableSpecialName>(SecondType, FirstType);
5183 }
5184 // TW <object name> # Thread-local wrapper
5185 case 'W': {
5186 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005187 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005188 if (Name == nullptr)
5189 return nullptr;
5190 return make<SpecialName>("thread-local wrapper routine for ", Name);
5191 }
5192 // TH <object name> # Thread-local initialization
5193 case 'H': {
5194 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005195 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005196 if (Name == nullptr)
5197 return nullptr;
5198 return make<SpecialName>("thread-local initialization routine for ", Name);
5199 }
5200 // T <call-offset> <base encoding>
5201 default: {
5202 ++First;
5203 bool IsVirt = look() == 'v';
5204 if (parseCallOffset())
5205 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005206 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005207 if (BaseEncoding == nullptr)
5208 return nullptr;
5209 if (IsVirt)
5210 return make<SpecialName>("virtual thunk to ", BaseEncoding);
5211 else
5212 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
5213 }
5214 }
5215 case 'G':
5216 switch (look(1)) {
5217 // GV <object name> # Guard variable for one-time initialization
5218 case 'V': {
5219 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005220 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005221 if (Name == nullptr)
5222 return nullptr;
5223 return make<SpecialName>("guard variable for ", Name);
5224 }
5225 // GR <object name> # reference temporary for object
5226 // GR <object name> _ # First temporary
5227 // GR <object name> <seq-id> _ # Subsequent temporaries
5228 case 'R': {
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 size_t Count;
5234 bool ParsedSeqId = !parseSeqId(&Count);
5235 if (!consumeIf('_') && ParsedSeqId)
5236 return nullptr;
5237 return make<SpecialName>("reference temporary for ", Name);
5238 }
5239 }
5240 }
5241 return nullptr;
5242}
5243
5244// <encoding> ::= <function name> <bare-function-type>
5245// ::= <data name>
5246// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00005247template <typename Derived, typename Alloc>
5248Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithfac39712020-07-09 21:08:39 -07005249 // The template parameters of an encoding are unrelated to those of the
5250 // enclosing context.
5251 class SaveTemplateParams {
5252 AbstractManglingParser *Parser;
5253 decltype(TemplateParams) OldParams;
Justin Lebar2c536232021-06-09 16:57:22 -07005254 decltype(OuterTemplateParams) OldOuterParams;
Richard Smithfac39712020-07-09 21:08:39 -07005255
5256 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04005257 SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
Richard Smithfac39712020-07-09 21:08:39 -07005258 OldParams = std::move(Parser->TemplateParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005259 OldOuterParams = std::move(Parser->OuterTemplateParams);
Richard Smithfac39712020-07-09 21:08:39 -07005260 Parser->TemplateParams.clear();
Justin Lebar2c536232021-06-09 16:57:22 -07005261 Parser->OuterTemplateParams.clear();
Richard Smithfac39712020-07-09 21:08:39 -07005262 }
5263 ~SaveTemplateParams() {
5264 Parser->TemplateParams = std::move(OldParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005265 Parser->OuterTemplateParams = std::move(OldOuterParams);
Richard Smithfac39712020-07-09 21:08:39 -07005266 }
5267 } SaveTemplateParams(this);
Richard Smithfd434322020-07-09 20:36:04 -07005268
Richard Smithc20d1442018-08-20 20:14:49 +00005269 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00005270 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00005271
5272 auto IsEndOfEncoding = [&] {
5273 // The set of chars that can potentially follow an <encoding> (none of which
5274 // can start a <type>). Enumerating these allows us to avoid speculative
5275 // parsing.
5276 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5277 };
5278
5279 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00005280 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00005281 if (Name == nullptr)
5282 return nullptr;
5283
5284 if (resolveForwardTemplateRefs(NameInfo))
5285 return nullptr;
5286
5287 if (IsEndOfEncoding())
5288 return Name;
5289
5290 Node *Attrs = nullptr;
5291 if (consumeIf("Ua9enable_ifI")) {
5292 size_t BeforeArgs = Names.size();
5293 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005294 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005295 if (Arg == nullptr)
5296 return nullptr;
5297 Names.push_back(Arg);
5298 }
5299 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00005300 if (!Attrs)
5301 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005302 }
5303
5304 Node *ReturnType = nullptr;
5305 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00005306 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005307 if (ReturnType == nullptr)
5308 return nullptr;
5309 }
5310
5311 if (consumeIf('v'))
5312 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5313 Attrs, NameInfo.CVQualifiers,
5314 NameInfo.ReferenceQualifier);
5315
5316 size_t ParamsBegin = Names.size();
5317 do {
Pavel Labathba825192018-10-16 14:29:14 +00005318 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005319 if (Ty == nullptr)
5320 return nullptr;
5321 Names.push_back(Ty);
5322 } while (!IsEndOfEncoding());
5323
5324 return make<FunctionEncoding>(ReturnType, Name,
5325 popTrailingNodeArray(ParamsBegin),
5326 Attrs, NameInfo.CVQualifiers,
5327 NameInfo.ReferenceQualifier);
5328}
5329
5330template <class Float>
5331struct FloatData;
5332
5333template <>
5334struct FloatData<float>
5335{
5336 static const size_t mangled_size = 8;
5337 static const size_t max_demangled_size = 24;
5338 static constexpr const char* spec = "%af";
5339};
5340
5341template <>
5342struct FloatData<double>
5343{
5344 static const size_t mangled_size = 16;
5345 static const size_t max_demangled_size = 32;
5346 static constexpr const char* spec = "%a";
5347};
5348
5349template <>
5350struct FloatData<long double>
5351{
5352#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5353 defined(__wasm__)
5354 static const size_t mangled_size = 32;
5355#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5356 static const size_t mangled_size = 16;
5357#else
5358 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5359#endif
Elliott Hughes5a360ea2020-04-10 17:42:00 -07005360 // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
5361 // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
5362 // Negatives are one character longer than positives.
5363 // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
5364 // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
5365 static const size_t max_demangled_size = 42;
Richard Smithc20d1442018-08-20 20:14:49 +00005366 static constexpr const char *spec = "%LaL";
5367};
5368
Pavel Labathba825192018-10-16 14:29:14 +00005369template <typename Alloc, typename Derived>
5370template <class Float>
5371Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005372 const size_t N = FloatData<Float>::mangled_size;
5373 if (numLeft() <= N)
5374 return nullptr;
5375 StringView Data(First, First + N);
5376 for (char C : Data)
5377 if (!std::isxdigit(C))
5378 return nullptr;
5379 First += N;
5380 if (!consumeIf('E'))
5381 return nullptr;
5382 return make<FloatLiteralImpl<Float>>(Data);
5383}
5384
5385// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005386template <typename Alloc, typename Derived>
5387bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005388 if (!(look() >= '0' && look() <= '9') &&
5389 !(look() >= 'A' && look() <= 'Z'))
5390 return true;
5391
5392 size_t Id = 0;
5393 while (true) {
5394 if (look() >= '0' && look() <= '9') {
5395 Id *= 36;
5396 Id += static_cast<size_t>(look() - '0');
5397 } else if (look() >= 'A' && look() <= 'Z') {
5398 Id *= 36;
5399 Id += static_cast<size_t>(look() - 'A') + 10;
5400 } else {
5401 *Out = Id;
5402 return false;
5403 }
5404 ++First;
5405 }
5406}
5407
5408// <substitution> ::= S <seq-id> _
5409// ::= S_
5410// <substitution> ::= Sa # ::std::allocator
5411// <substitution> ::= Sb # ::std::basic_string
5412// <substitution> ::= Ss # ::std::basic_string < char,
5413// ::std::char_traits<char>,
5414// ::std::allocator<char> >
5415// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5416// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5417// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08005418// The St case is handled specially in parseNestedName.
Pavel Labathba825192018-10-16 14:29:14 +00005419template <typename Derived, typename Alloc>
5420Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005421 if (!consumeIf('S'))
5422 return nullptr;
5423
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08005424 if (look() >= 'a' && look() <= 'z') {
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005425 SpecialSubKind Kind;
Richard Smithc20d1442018-08-20 20:14:49 +00005426 switch (look()) {
5427 case 'a':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005428 Kind = SpecialSubKind::allocator;
Richard Smithc20d1442018-08-20 20:14:49 +00005429 break;
5430 case 'b':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005431 Kind = SpecialSubKind::basic_string;
Richard Smithc20d1442018-08-20 20:14:49 +00005432 break;
5433 case 'd':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005434 Kind = SpecialSubKind::iostream;
5435 break;
5436 case 'i':
5437 Kind = SpecialSubKind::istream;
5438 break;
5439 case 'o':
5440 Kind = SpecialSubKind::ostream;
5441 break;
5442 case 's':
5443 Kind = SpecialSubKind::string;
Richard Smithc20d1442018-08-20 20:14:49 +00005444 break;
5445 default:
5446 return nullptr;
5447 }
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005448 ++First;
5449 auto *SpecialSub = make<SpecialSubstitution>(Kind);
Richard Smithb485b352018-08-24 23:30:26 +00005450 if (!SpecialSub)
5451 return nullptr;
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005452
Richard Smithc20d1442018-08-20 20:14:49 +00005453 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5454 // has ABI tags, the tags are appended to the substitution; the result is a
5455 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005456 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005457 if (WithTags != SpecialSub) {
5458 Subs.push_back(WithTags);
5459 SpecialSub = WithTags;
5460 }
5461 return SpecialSub;
5462 }
5463
5464 // ::= S_
5465 if (consumeIf('_')) {
5466 if (Subs.empty())
5467 return nullptr;
5468 return Subs[0];
5469 }
5470
5471 // ::= S <seq-id> _
5472 size_t Index = 0;
5473 if (parseSeqId(&Index))
5474 return nullptr;
5475 ++Index;
5476 if (!consumeIf('_') || Index >= Subs.size())
5477 return nullptr;
5478 return Subs[Index];
5479}
5480
5481// <template-param> ::= T_ # first template parameter
5482// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005483// ::= TL <level-1> __
5484// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005485template <typename Derived, typename Alloc>
5486Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005487 if (!consumeIf('T'))
5488 return nullptr;
5489
Richard Smithdf1c14c2019-09-06 23:53:21 +00005490 size_t Level = 0;
5491 if (consumeIf('L')) {
5492 if (parsePositiveInteger(&Level))
5493 return nullptr;
5494 ++Level;
5495 if (!consumeIf('_'))
5496 return nullptr;
5497 }
5498
Richard Smithc20d1442018-08-20 20:14:49 +00005499 size_t Index = 0;
5500 if (!consumeIf('_')) {
5501 if (parsePositiveInteger(&Index))
5502 return nullptr;
5503 ++Index;
5504 if (!consumeIf('_'))
5505 return nullptr;
5506 }
5507
Richard Smithc20d1442018-08-20 20:14:49 +00005508 // If we're in a context where this <template-param> refers to a
5509 // <template-arg> further ahead in the mangled name (currently just conversion
5510 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005511 // This can only happen at the outermost level.
5512 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005513 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5514 if (!ForwardRef)
5515 return nullptr;
5516 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5517 ForwardTemplateRefs.push_back(
5518 static_cast<ForwardTemplateReference *>(ForwardRef));
5519 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005520 }
5521
Richard Smithdf1c14c2019-09-06 23:53:21 +00005522 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5523 Index >= TemplateParams[Level]->size()) {
5524 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5525 // list are mangled as the corresponding artificial template type parameter.
5526 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5527 // This will be popped by the ScopedTemplateParamList in
5528 // parseUnnamedTypeName.
5529 if (Level == TemplateParams.size())
5530 TemplateParams.push_back(nullptr);
5531 return make<NameType>("auto");
5532 }
5533
Richard Smithc20d1442018-08-20 20:14:49 +00005534 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005535 }
5536
5537 return (*TemplateParams[Level])[Index];
5538}
5539
5540// <template-param-decl> ::= Ty # type parameter
5541// ::= Tn <type> # non-type parameter
5542// ::= Tt <template-param-decl>* E # template parameter
5543// ::= Tp <template-param-decl> # parameter pack
5544template <typename Derived, typename Alloc>
5545Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5546 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5547 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5548 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5549 if (N) TemplateParams.back()->push_back(N);
5550 return N;
5551 };
5552
5553 if (consumeIf("Ty")) {
5554 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5555 if (!Name)
5556 return nullptr;
5557 return make<TypeTemplateParamDecl>(Name);
5558 }
5559
5560 if (consumeIf("Tn")) {
5561 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5562 if (!Name)
5563 return nullptr;
5564 Node *Type = parseType();
5565 if (!Type)
5566 return nullptr;
5567 return make<NonTypeTemplateParamDecl>(Name, Type);
5568 }
5569
5570 if (consumeIf("Tt")) {
5571 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5572 if (!Name)
5573 return nullptr;
5574 size_t ParamsBegin = Names.size();
5575 ScopedTemplateParamList TemplateTemplateParamParams(this);
5576 while (!consumeIf("E")) {
5577 Node *P = parseTemplateParamDecl();
5578 if (!P)
5579 return nullptr;
5580 Names.push_back(P);
5581 }
5582 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5583 return make<TemplateTemplateParamDecl>(Name, Params);
5584 }
5585
5586 if (consumeIf("Tp")) {
5587 Node *P = parseTemplateParamDecl();
5588 if (!P)
5589 return nullptr;
5590 return make<TemplateParamPackDecl>(P);
5591 }
5592
5593 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005594}
5595
5596// <template-arg> ::= <type> # type or template
5597// ::= X <expression> E # expression
5598// ::= <expr-primary> # simple expressions
5599// ::= J <template-arg>* E # argument pack
5600// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005601template <typename Derived, typename Alloc>
5602Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005603 switch (look()) {
5604 case 'X': {
5605 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005606 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005607 if (Arg == nullptr || !consumeIf('E'))
5608 return nullptr;
5609 return Arg;
5610 }
5611 case 'J': {
5612 ++First;
5613 size_t ArgsBegin = Names.size();
5614 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005615 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005616 if (Arg == nullptr)
5617 return nullptr;
5618 Names.push_back(Arg);
5619 }
5620 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5621 return make<TemplateArgumentPack>(Args);
5622 }
5623 case 'L': {
5624 // ::= LZ <encoding> E # extension
5625 if (look(1) == 'Z') {
5626 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005627 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005628 if (Arg == nullptr || !consumeIf('E'))
5629 return nullptr;
5630 return Arg;
5631 }
5632 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005633 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005634 }
5635 default:
Pavel Labathba825192018-10-16 14:29:14 +00005636 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005637 }
5638}
5639
5640// <template-args> ::= I <template-arg>* E
5641// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005642template <typename Derived, typename Alloc>
5643Node *
5644AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005645 if (!consumeIf('I'))
5646 return nullptr;
5647
5648 // <template-params> refer to the innermost <template-args>. Clear out any
5649 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005650 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005651 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005652 TemplateParams.push_back(&OuterTemplateParams);
5653 OuterTemplateParams.clear();
5654 }
Richard Smithc20d1442018-08-20 20:14:49 +00005655
5656 size_t ArgsBegin = Names.size();
5657 while (!consumeIf('E')) {
5658 if (TagTemplates) {
5659 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005660 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005661 TemplateParams = std::move(OldParams);
5662 if (Arg == nullptr)
5663 return nullptr;
5664 Names.push_back(Arg);
5665 Node *TableEntry = Arg;
5666 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5667 TableEntry = make<ParameterPack>(
5668 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005669 if (!TableEntry)
5670 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005671 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005672 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005673 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005674 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005675 if (Arg == nullptr)
5676 return nullptr;
5677 Names.push_back(Arg);
5678 }
5679 }
5680 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5681}
5682
5683// <mangled-name> ::= _Z <encoding>
5684// ::= <type>
5685// extension ::= ___Z <encoding> _block_invoke
5686// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5687// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005688template <typename Derived, typename Alloc>
5689Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005690 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005691 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005692 if (Encoding == nullptr)
5693 return nullptr;
5694 if (look() == '.') {
5695 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5696 First = Last;
5697 }
5698 if (numLeft() != 0)
5699 return nullptr;
5700 return Encoding;
5701 }
5702
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005703 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005704 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005705 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5706 return nullptr;
5707 bool RequireNumber = consumeIf('_');
5708 if (parseNumber().empty() && RequireNumber)
5709 return nullptr;
5710 if (look() == '.')
5711 First = Last;
5712 if (numLeft() != 0)
5713 return nullptr;
5714 return make<SpecialName>("invocation function for block in ", Encoding);
5715 }
5716
Pavel Labathba825192018-10-16 14:29:14 +00005717 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005718 if (numLeft() != 0)
5719 return nullptr;
5720 return Ty;
5721}
5722
Pavel Labathba825192018-10-16 14:29:14 +00005723template <typename Alloc>
5724struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5725 using AbstractManglingParser<ManglingParser<Alloc>,
5726 Alloc>::AbstractManglingParser;
5727};
5728
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005729DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005730
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005731#endif // DEMANGLE_ITANIUMDEMANGLE_H