blob: d79452e42ea293128b10ed61bd6c66a7a159771f [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)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001929 OB += "::operator ";
1930 OB += "new";
Richard Smithc20d1442018-08-20 20:14:49 +00001931 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001932 OB += "[]";
1933 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00001934 if (!ExprList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001935 OB += "(";
1936 ExprList.printWithComma(OB);
1937 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001938 }
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)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001964 OB += "[] ";
1965 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001966 }
1967};
1968
1969class PrefixExpr : public Node {
1970 StringView Prefix;
1971 Node *Child;
1972
1973public:
1974 PrefixExpr(StringView Prefix_, Node *Child_)
1975 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1976
1977 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1978
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001979 void printLeft(OutputBuffer &OB) const override {
1980 OB += Prefix;
1981 OB += "(";
1982 Child->print(OB);
1983 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001984 }
1985};
1986
1987class FunctionParam : public Node {
1988 StringView Number;
1989
1990public:
1991 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
1992
1993 template<typename Fn> void match(Fn F) const { F(Number); }
1994
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001995 void printLeft(OutputBuffer &OB) const override {
1996 OB += "fp";
1997 OB += Number;
Richard Smithc20d1442018-08-20 20:14:49 +00001998 }
1999};
2000
2001class ConversionExpr : public Node {
2002 const Node *Type;
2003 NodeArray Expressions;
2004
2005public:
2006 ConversionExpr(const Node *Type_, NodeArray Expressions_)
2007 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
2008
2009 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
2010
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002011 void printLeft(OutputBuffer &OB) const override {
2012 OB += "(";
2013 Type->print(OB);
2014 OB += ")(";
2015 Expressions.printWithComma(OB);
2016 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002017 }
2018};
2019
Richard Smith1865d2f2020-10-22 19:29:36 -07002020class PointerToMemberConversionExpr : public Node {
2021 const Node *Type;
2022 const Node *SubExpr;
2023 StringView Offset;
2024
2025public:
2026 PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_,
2027 StringView Offset_)
2028 : Node(KPointerToMemberConversionExpr), Type(Type_), SubExpr(SubExpr_),
2029 Offset(Offset_) {}
2030
2031 template<typename Fn> void match(Fn F) const { F(Type, SubExpr, Offset); }
2032
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002033 void printLeft(OutputBuffer &OB) const override {
2034 OB += "(";
2035 Type->print(OB);
2036 OB += ")(";
2037 SubExpr->print(OB);
2038 OB += ")";
Richard Smith1865d2f2020-10-22 19:29:36 -07002039 }
2040};
2041
Richard Smithc20d1442018-08-20 20:14:49 +00002042class InitListExpr : public Node {
2043 const Node *Ty;
2044 NodeArray Inits;
2045public:
2046 InitListExpr(const Node *Ty_, NodeArray Inits_)
2047 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
2048
2049 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
2050
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002051 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002052 if (Ty)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002053 Ty->print(OB);
2054 OB += '{';
2055 Inits.printWithComma(OB);
2056 OB += '}';
Richard Smithc20d1442018-08-20 20:14:49 +00002057 }
2058};
2059
2060class BracedExpr : public Node {
2061 const Node *Elem;
2062 const Node *Init;
2063 bool IsArray;
2064public:
2065 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
2066 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
2067
2068 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
2069
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002070 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002071 if (IsArray) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002072 OB += '[';
2073 Elem->print(OB);
2074 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002075 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002076 OB += '.';
2077 Elem->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002078 }
2079 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002080 OB += " = ";
2081 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002082 }
2083};
2084
2085class BracedRangeExpr : public Node {
2086 const Node *First;
2087 const Node *Last;
2088 const Node *Init;
2089public:
2090 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
2091 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
2092
2093 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
2094
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002095 void printLeft(OutputBuffer &OB) const override {
2096 OB += '[';
2097 First->print(OB);
2098 OB += " ... ";
2099 Last->print(OB);
2100 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002101 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002102 OB += " = ";
2103 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002104 }
2105};
2106
2107class FoldExpr : public Node {
2108 const Node *Pack, *Init;
2109 StringView OperatorName;
2110 bool IsLeftFold;
2111
2112public:
2113 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
2114 const Node *Init_)
2115 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
2116 IsLeftFold(IsLeftFold_) {}
2117
2118 template<typename Fn> void match(Fn F) const {
2119 F(IsLeftFold, OperatorName, Pack, Init);
2120 }
2121
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002122 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002123 auto PrintPack = [&] {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002124 OB += '(';
2125 ParameterPackExpansion(Pack).print(OB);
2126 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002127 };
2128
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002129 OB += '(';
Richard Smithc20d1442018-08-20 20:14:49 +00002130
2131 if (IsLeftFold) {
2132 // init op ... op pack
2133 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002134 Init->print(OB);
2135 OB += ' ';
2136 OB += OperatorName;
2137 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002138 }
2139 // ... op pack
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002140 OB += "... ";
2141 OB += OperatorName;
2142 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002143 PrintPack();
2144 } else { // !IsLeftFold
2145 // pack op ...
2146 PrintPack();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002147 OB += ' ';
2148 OB += OperatorName;
2149 OB += " ...";
Richard Smithc20d1442018-08-20 20:14:49 +00002150 // pack op ... op init
2151 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002152 OB += ' ';
2153 OB += OperatorName;
2154 OB += ' ';
2155 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002156 }
2157 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002158 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002159 }
2160};
2161
2162class ThrowExpr : public Node {
2163 const Node *Op;
2164
2165public:
2166 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
2167
2168 template<typename Fn> void match(Fn F) const { F(Op); }
2169
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002170 void printLeft(OutputBuffer &OB) const override {
2171 OB += "throw ";
2172 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002173 }
2174};
2175
2176class BoolExpr : public Node {
2177 bool Value;
2178
2179public:
2180 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
2181
2182 template<typename Fn> void match(Fn F) const { F(Value); }
2183
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002184 void printLeft(OutputBuffer &OB) const override {
2185 OB += Value ? StringView("true") : StringView("false");
Richard Smithc20d1442018-08-20 20:14:49 +00002186 }
2187};
2188
Richard Smithdf1c14c2019-09-06 23:53:21 +00002189class StringLiteral : public Node {
2190 const Node *Type;
2191
2192public:
2193 StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
2194
2195 template<typename Fn> void match(Fn F) const { F(Type); }
2196
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002197 void printLeft(OutputBuffer &OB) const override {
2198 OB += "\"<";
2199 Type->print(OB);
2200 OB += ">\"";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002201 }
2202};
2203
2204class LambdaExpr : public Node {
2205 const Node *Type;
2206
Richard Smithdf1c14c2019-09-06 23:53:21 +00002207public:
2208 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2209
2210 template<typename Fn> void match(Fn F) const { F(Type); }
2211
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002212 void printLeft(OutputBuffer &OB) const override {
2213 OB += "[]";
Richard Smithfb917462019-09-09 22:26:04 +00002214 if (Type->getKind() == KClosureTypeName)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002215 static_cast<const ClosureTypeName *>(Type)->printDeclarator(OB);
2216 OB += "{...}";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002217 }
2218};
2219
Erik Pilkington0a170f12020-05-13 14:13:37 -04002220class EnumLiteral : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +00002221 // ty(integer)
2222 const Node *Ty;
2223 StringView Integer;
2224
2225public:
Erik Pilkington0a170f12020-05-13 14:13:37 -04002226 EnumLiteral(const Node *Ty_, StringView Integer_)
2227 : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00002228
2229 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2230
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002231 void printLeft(OutputBuffer &OB) const override {
2232 OB << "(";
2233 Ty->print(OB);
2234 OB << ")";
Erik Pilkington0a170f12020-05-13 14:13:37 -04002235
2236 if (Integer[0] == 'n')
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002237 OB << "-" << Integer.dropFront(1);
Erik Pilkington0a170f12020-05-13 14:13:37 -04002238 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002239 OB << Integer;
Richard Smithc20d1442018-08-20 20:14:49 +00002240 }
2241};
2242
2243class IntegerLiteral : public Node {
2244 StringView Type;
2245 StringView Value;
2246
2247public:
2248 IntegerLiteral(StringView Type_, StringView Value_)
2249 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2250
2251 template<typename Fn> void match(Fn F) const { F(Type, Value); }
2252
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002253 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002254 if (Type.size() > 3) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002255 OB += "(";
2256 OB += Type;
2257 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002258 }
2259
2260 if (Value[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002261 OB += "-";
2262 OB += Value.dropFront(1);
Richard Smithc20d1442018-08-20 20:14:49 +00002263 } else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002264 OB += Value;
Richard Smithc20d1442018-08-20 20:14:49 +00002265
2266 if (Type.size() <= 3)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002267 OB += Type;
Richard Smithc20d1442018-08-20 20:14:49 +00002268 }
2269};
2270
2271template <class Float> struct FloatData;
2272
2273namespace float_literal_impl {
2274constexpr Node::Kind getFloatLiteralKind(float *) {
2275 return Node::KFloatLiteral;
2276}
2277constexpr Node::Kind getFloatLiteralKind(double *) {
2278 return Node::KDoubleLiteral;
2279}
2280constexpr Node::Kind getFloatLiteralKind(long double *) {
2281 return Node::KLongDoubleLiteral;
2282}
2283}
2284
2285template <class Float> class FloatLiteralImpl : public Node {
2286 const StringView Contents;
2287
2288 static constexpr Kind KindForClass =
2289 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2290
2291public:
2292 FloatLiteralImpl(StringView Contents_)
2293 : Node(KindForClass), Contents(Contents_) {}
2294
2295 template<typename Fn> void match(Fn F) const { F(Contents); }
2296
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002297 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002298 const char *first = Contents.begin();
2299 const char *last = Contents.end() + 1;
2300
2301 const size_t N = FloatData<Float>::mangled_size;
2302 if (static_cast<std::size_t>(last - first) > N) {
2303 last = first + N;
2304 union {
2305 Float value;
2306 char buf[sizeof(Float)];
2307 };
2308 const char *t = first;
2309 char *e = buf;
2310 for (; t != last; ++t, ++e) {
2311 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2312 : static_cast<unsigned>(*t - 'a' + 10);
2313 ++t;
2314 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2315 : static_cast<unsigned>(*t - 'a' + 10);
2316 *e = static_cast<char>((d1 << 4) + d0);
2317 }
2318#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2319 std::reverse(buf, e);
2320#endif
2321 char num[FloatData<Float>::max_demangled_size] = {0};
2322 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002323 OB += StringView(num, num + n);
Richard Smithc20d1442018-08-20 20:14:49 +00002324 }
2325 }
2326};
2327
2328using FloatLiteral = FloatLiteralImpl<float>;
2329using DoubleLiteral = FloatLiteralImpl<double>;
2330using LongDoubleLiteral = FloatLiteralImpl<long double>;
2331
2332/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2333/// appropriate derived class.
2334template<typename Fn>
2335void Node::visit(Fn F) const {
2336 switch (K) {
2337#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2338 FOR_EACH_NODE_KIND(CASE)
2339#undef CASE
2340 }
2341 assert(0 && "unknown mangling node kind");
2342}
2343
2344/// Determine the kind of a node from its type.
2345template<typename NodeT> struct NodeKind;
2346#define SPECIALIZATION(X) \
2347 template<> struct NodeKind<X> { \
2348 static constexpr Node::Kind Kind = Node::K##X; \
2349 static constexpr const char *name() { return #X; } \
2350 };
2351FOR_EACH_NODE_KIND(SPECIALIZATION)
2352#undef SPECIALIZATION
2353
2354#undef FOR_EACH_NODE_KIND
2355
Pavel Labathba825192018-10-16 14:29:14 +00002356template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002357 const char *First;
2358 const char *Last;
2359
2360 // Name stack, this is used by the parser to hold temporary names that were
2361 // parsed. The parser collapses multiple names into new nodes to construct
2362 // the AST. Once the parser is finished, names.size() == 1.
2363 PODSmallVector<Node *, 32> Names;
2364
2365 // Substitution table. Itanium supports name substitutions as a means of
2366 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2367 // table.
2368 PODSmallVector<Node *, 32> Subs;
2369
Richard Smithdf1c14c2019-09-06 23:53:21 +00002370 using TemplateParamList = PODSmallVector<Node *, 8>;
2371
2372 class ScopedTemplateParamList {
2373 AbstractManglingParser *Parser;
2374 size_t OldNumTemplateParamLists;
2375 TemplateParamList Params;
2376
2377 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04002378 ScopedTemplateParamList(AbstractManglingParser *TheParser)
2379 : Parser(TheParser),
2380 OldNumTemplateParamLists(TheParser->TemplateParams.size()) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002381 Parser->TemplateParams.push_back(&Params);
2382 }
2383 ~ScopedTemplateParamList() {
2384 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2385 Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2386 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002387 };
2388
Richard Smithc20d1442018-08-20 20:14:49 +00002389 // Template parameter table. Like the above, but referenced like "T42_".
2390 // This has a smaller size compared to Subs and Names because it can be
2391 // stored on the stack.
Richard Smithdf1c14c2019-09-06 23:53:21 +00002392 TemplateParamList OuterTemplateParams;
2393
2394 // Lists of template parameters indexed by template parameter depth,
2395 // referenced like "TL2_4_". If nonempty, element 0 is always
2396 // OuterTemplateParams; inner elements are always template parameter lists of
2397 // lambda expressions. For a generic lambda with no explicit template
2398 // parameter list, the corresponding parameter list pointer will be null.
2399 PODSmallVector<TemplateParamList *, 4> TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00002400
2401 // Set of unresolved forward <template-param> references. These can occur in a
2402 // conversion operator's type, and are resolved in the enclosing <encoding>.
2403 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2404
Richard Smithc20d1442018-08-20 20:14:49 +00002405 bool TryToParseTemplateArgs = true;
2406 bool PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002407 size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2408
2409 unsigned NumSyntheticTemplateParameters[3] = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002410
2411 Alloc ASTAllocator;
2412
Pavel Labathba825192018-10-16 14:29:14 +00002413 AbstractManglingParser(const char *First_, const char *Last_)
2414 : First(First_), Last(Last_) {}
2415
2416 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002417
2418 void reset(const char *First_, const char *Last_) {
2419 First = First_;
2420 Last = Last_;
2421 Names.clear();
2422 Subs.clear();
2423 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002424 ParsingLambdaParamsAtLevel = (size_t)-1;
Richard Smithc20d1442018-08-20 20:14:49 +00002425 TryToParseTemplateArgs = true;
2426 PermitForwardTemplateReferences = false;
Richard Smith9a2307a2019-09-07 00:11:53 +00002427 for (int I = 0; I != 3; ++I)
2428 NumSyntheticTemplateParameters[I] = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00002429 ASTAllocator.reset();
2430 }
2431
Richard Smithb485b352018-08-24 23:30:26 +00002432 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002433 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2434 }
2435
2436 template <class It> NodeArray makeNodeArray(It begin, It end) {
2437 size_t sz = static_cast<size_t>(end - begin);
2438 void *mem = ASTAllocator.allocateNodeArray(sz);
2439 Node **data = new (mem) Node *[sz];
2440 std::copy(begin, end, data);
2441 return NodeArray(data, sz);
2442 }
2443
2444 NodeArray popTrailingNodeArray(size_t FromPosition) {
2445 assert(FromPosition <= Names.size());
2446 NodeArray res =
2447 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2448 Names.dropBack(FromPosition);
2449 return res;
2450 }
2451
2452 bool consumeIf(StringView S) {
2453 if (StringView(First, Last).startsWith(S)) {
2454 First += S.size();
2455 return true;
2456 }
2457 return false;
2458 }
2459
2460 bool consumeIf(char C) {
2461 if (First != Last && *First == C) {
2462 ++First;
2463 return true;
2464 }
2465 return false;
2466 }
2467
2468 char consume() { return First != Last ? *First++ : '\0'; }
2469
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08002470 char look(unsigned Lookahead = 0) const {
Richard Smithc20d1442018-08-20 20:14:49 +00002471 if (static_cast<size_t>(Last - First) <= Lookahead)
2472 return '\0';
2473 return First[Lookahead];
2474 }
2475
2476 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2477
2478 StringView parseNumber(bool AllowNegative = false);
2479 Qualifiers parseCVQualifiers();
2480 bool parsePositiveInteger(size_t *Out);
2481 StringView parseBareSourceName();
2482
2483 bool parseSeqId(size_t *Out);
2484 Node *parseSubstitution();
2485 Node *parseTemplateParam();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002486 Node *parseTemplateParamDecl();
Richard Smithc20d1442018-08-20 20:14:49 +00002487 Node *parseTemplateArgs(bool TagTemplates = false);
2488 Node *parseTemplateArg();
2489
2490 /// Parse the <expr> production.
2491 Node *parseExpr();
2492 Node *parsePrefixExpr(StringView Kind);
2493 Node *parseBinaryExpr(StringView Kind);
2494 Node *parseIntegerLiteral(StringView Lit);
2495 Node *parseExprPrimary();
2496 template <class Float> Node *parseFloatingLiteral();
2497 Node *parseFunctionParam();
2498 Node *parseNewExpr();
2499 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);
2550 Node *parseUnqualifiedName(NameState *State);
2551 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 Sidwelle4cc3532022-01-24 04:28:09 -08002662 bool IsStd = consumeIf("St");
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08002663 consumeIf('L');
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002664
2665 Node *Result = getDerived().parseUnqualifiedName(State);
2666 if (Result == nullptr)
2667 return nullptr;
Nathan Sidwell200e97c2022-01-21 11:37:01 -08002668 if (IsStd) {
2669 if (auto *Std = make<NameType>("std"))
2670 Result = make<NestedName>(Std, Result);
2671 else
2672 return nullptr;
2673 }
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002674
2675 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00002676}
2677
2678// <unqualified-name> ::= <operator-name> [abi-tags]
2679// ::= <ctor-dtor-name>
2680// ::= <source-name>
2681// ::= <unnamed-type-name>
2682// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002683template <typename Derived, typename Alloc>
2684Node *
2685AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002686 // <ctor-dtor-name>s are special-cased in parseNestedName().
2687 Node *Result;
2688 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002689 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002690 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002691 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002692 else if (consumeIf("DC")) {
2693 size_t BindingsBegin = Names.size();
2694 do {
Pavel Labathba825192018-10-16 14:29:14 +00002695 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002696 if (Binding == nullptr)
2697 return nullptr;
2698 Names.push_back(Binding);
2699 } while (!consumeIf('E'));
2700 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2701 } else
Pavel Labathba825192018-10-16 14:29:14 +00002702 Result = getDerived().parseOperatorName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002703 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002704 Result = getDerived().parseAbiTags(Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002705 return Result;
2706}
2707
2708// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2709// ::= <closure-type-name>
2710//
2711// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2712//
2713// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002714template <typename Derived, typename Alloc>
2715Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002716AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2717 // <template-params> refer to the innermost <template-args>. Clear out any
2718 // outer args that we may have inserted into TemplateParams.
2719 if (State != nullptr)
2720 TemplateParams.clear();
2721
Richard Smithc20d1442018-08-20 20:14:49 +00002722 if (consumeIf("Ut")) {
2723 StringView Count = parseNumber();
2724 if (!consumeIf('_'))
2725 return nullptr;
2726 return make<UnnamedTypeName>(Count);
2727 }
2728 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002729 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2730 TemplateParams.size());
2731 ScopedTemplateParamList LambdaTemplateParams(this);
2732
2733 size_t ParamsBegin = Names.size();
2734 while (look() == 'T' &&
2735 StringView("yptn").find(look(1)) != StringView::npos) {
2736 Node *T = parseTemplateParamDecl();
2737 if (!T)
2738 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002739 Names.push_back(T);
2740 }
2741 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2742
2743 // FIXME: If TempParams is empty and none of the function parameters
2744 // includes 'auto', we should remove LambdaTemplateParams from the
2745 // TemplateParams list. Unfortunately, we don't find out whether there are
2746 // any 'auto' parameters until too late in an example such as:
2747 //
2748 // template<typename T> void f(
2749 // decltype([](decltype([]<typename T>(T v) {}),
2750 // auto) {})) {}
2751 // template<typename T> void f(
2752 // decltype([](decltype([]<typename T>(T w) {}),
2753 // int) {})) {}
2754 //
2755 // Here, the type of v is at level 2 but the type of w is at level 1. We
2756 // don't find this out until we encounter the type of the next parameter.
2757 //
2758 // However, compilers can't actually cope with the former example in
2759 // practice, and it's likely to be made ill-formed in future, so we don't
2760 // need to support it here.
2761 //
2762 // If we encounter an 'auto' in the function parameter types, we will
2763 // recreate a template parameter scope for it, but any intervening lambdas
2764 // will be parsed in the 'wrong' template parameter depth.
2765 if (TempParams.empty())
2766 TemplateParams.pop_back();
2767
Richard Smithc20d1442018-08-20 20:14:49 +00002768 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002769 do {
Pavel Labathba825192018-10-16 14:29:14 +00002770 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002771 if (P == nullptr)
2772 return nullptr;
2773 Names.push_back(P);
2774 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002775 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002776 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2777
Richard Smithc20d1442018-08-20 20:14:49 +00002778 StringView Count = parseNumber();
2779 if (!consumeIf('_'))
2780 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002781 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002782 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002783 if (consumeIf("Ub")) {
2784 (void)parseNumber();
2785 if (!consumeIf('_'))
2786 return nullptr;
2787 return make<NameType>("'block-literal'");
2788 }
Richard Smithc20d1442018-08-20 20:14:49 +00002789 return nullptr;
2790}
2791
2792// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002793template <typename Derived, typename Alloc>
2794Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002795 size_t Length = 0;
2796 if (parsePositiveInteger(&Length))
2797 return nullptr;
2798 if (numLeft() < Length || Length == 0)
2799 return nullptr;
2800 StringView Name(First, First + Length);
2801 First += Length;
2802 if (Name.startsWith("_GLOBAL__N"))
2803 return make<NameType>("(anonymous namespace)");
2804 return make<NameType>(Name);
2805}
2806
2807// <operator-name> ::= aa # &&
2808// ::= ad # & (unary)
2809// ::= an # &
2810// ::= aN # &=
2811// ::= aS # =
2812// ::= cl # ()
2813// ::= cm # ,
2814// ::= co # ~
2815// ::= cv <type> # (cast)
2816// ::= da # delete[]
2817// ::= de # * (unary)
2818// ::= dl # delete
2819// ::= dv # /
2820// ::= dV # /=
2821// ::= eo # ^
2822// ::= eO # ^=
2823// ::= eq # ==
2824// ::= ge # >=
2825// ::= gt # >
2826// ::= ix # []
2827// ::= le # <=
2828// ::= li <source-name> # operator ""
2829// ::= ls # <<
2830// ::= lS # <<=
2831// ::= lt # <
2832// ::= mi # -
2833// ::= mI # -=
2834// ::= ml # *
2835// ::= mL # *=
2836// ::= mm # -- (postfix in <expression> context)
2837// ::= na # new[]
2838// ::= ne # !=
2839// ::= ng # - (unary)
2840// ::= nt # !
2841// ::= nw # new
2842// ::= oo # ||
2843// ::= or # |
2844// ::= oR # |=
2845// ::= pm # ->*
2846// ::= pl # +
2847// ::= pL # +=
2848// ::= pp # ++ (postfix in <expression> context)
2849// ::= ps # + (unary)
2850// ::= pt # ->
2851// ::= qu # ?
2852// ::= rm # %
2853// ::= rM # %=
2854// ::= rs # >>
2855// ::= rS # >>=
2856// ::= ss # <=> C++2a
2857// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002858template <typename Derived, typename Alloc>
2859Node *
2860AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002861 switch (look()) {
2862 case 'a':
2863 switch (look(1)) {
2864 case 'a':
2865 First += 2;
2866 return make<NameType>("operator&&");
2867 case 'd':
2868 case 'n':
2869 First += 2;
2870 return make<NameType>("operator&");
2871 case 'N':
2872 First += 2;
2873 return make<NameType>("operator&=");
2874 case 'S':
2875 First += 2;
2876 return make<NameType>("operator=");
2877 }
2878 return nullptr;
2879 case 'c':
2880 switch (look(1)) {
2881 case 'l':
2882 First += 2;
2883 return make<NameType>("operator()");
2884 case 'm':
2885 First += 2;
2886 return make<NameType>("operator,");
2887 case 'o':
2888 First += 2;
2889 return make<NameType>("operator~");
2890 // ::= cv <type> # (cast)
2891 case 'v': {
2892 First += 2;
2893 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2894 // If we're parsing an encoding, State != nullptr and the conversion
2895 // operators' <type> could have a <template-param> that refers to some
2896 // <template-arg>s further ahead in the mangled name.
2897 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2898 PermitForwardTemplateReferences ||
2899 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002900 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002901 if (Ty == nullptr)
2902 return nullptr;
2903 if (State) State->CtorDtorConversion = true;
2904 return make<ConversionOperatorType>(Ty);
2905 }
2906 }
2907 return nullptr;
2908 case 'd':
2909 switch (look(1)) {
2910 case 'a':
2911 First += 2;
2912 return make<NameType>("operator delete[]");
2913 case 'e':
2914 First += 2;
2915 return make<NameType>("operator*");
2916 case 'l':
2917 First += 2;
2918 return make<NameType>("operator delete");
2919 case 'v':
2920 First += 2;
2921 return make<NameType>("operator/");
2922 case 'V':
2923 First += 2;
2924 return make<NameType>("operator/=");
2925 }
2926 return nullptr;
2927 case 'e':
2928 switch (look(1)) {
2929 case 'o':
2930 First += 2;
2931 return make<NameType>("operator^");
2932 case 'O':
2933 First += 2;
2934 return make<NameType>("operator^=");
2935 case 'q':
2936 First += 2;
2937 return make<NameType>("operator==");
2938 }
2939 return nullptr;
2940 case 'g':
2941 switch (look(1)) {
2942 case 'e':
2943 First += 2;
2944 return make<NameType>("operator>=");
2945 case 't':
2946 First += 2;
2947 return make<NameType>("operator>");
2948 }
2949 return nullptr;
2950 case 'i':
2951 if (look(1) == 'x') {
2952 First += 2;
2953 return make<NameType>("operator[]");
2954 }
2955 return nullptr;
2956 case 'l':
2957 switch (look(1)) {
2958 case 'e':
2959 First += 2;
2960 return make<NameType>("operator<=");
2961 // ::= li <source-name> # operator ""
2962 case 'i': {
2963 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002964 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002965 if (SN == nullptr)
2966 return nullptr;
2967 return make<LiteralOperator>(SN);
2968 }
2969 case 's':
2970 First += 2;
2971 return make<NameType>("operator<<");
2972 case 'S':
2973 First += 2;
2974 return make<NameType>("operator<<=");
2975 case 't':
2976 First += 2;
2977 return make<NameType>("operator<");
2978 }
2979 return nullptr;
2980 case 'm':
2981 switch (look(1)) {
2982 case 'i':
2983 First += 2;
2984 return make<NameType>("operator-");
2985 case 'I':
2986 First += 2;
2987 return make<NameType>("operator-=");
2988 case 'l':
2989 First += 2;
2990 return make<NameType>("operator*");
2991 case 'L':
2992 First += 2;
2993 return make<NameType>("operator*=");
2994 case 'm':
2995 First += 2;
2996 return make<NameType>("operator--");
2997 }
2998 return nullptr;
2999 case 'n':
3000 switch (look(1)) {
3001 case 'a':
3002 First += 2;
3003 return make<NameType>("operator new[]");
3004 case 'e':
3005 First += 2;
3006 return make<NameType>("operator!=");
3007 case 'g':
3008 First += 2;
3009 return make<NameType>("operator-");
3010 case 't':
3011 First += 2;
3012 return make<NameType>("operator!");
3013 case 'w':
3014 First += 2;
3015 return make<NameType>("operator new");
3016 }
3017 return nullptr;
3018 case 'o':
3019 switch (look(1)) {
3020 case 'o':
3021 First += 2;
3022 return make<NameType>("operator||");
3023 case 'r':
3024 First += 2;
3025 return make<NameType>("operator|");
3026 case 'R':
3027 First += 2;
3028 return make<NameType>("operator|=");
3029 }
3030 return nullptr;
3031 case 'p':
3032 switch (look(1)) {
3033 case 'm':
3034 First += 2;
3035 return make<NameType>("operator->*");
3036 case 'l':
3037 First += 2;
3038 return make<NameType>("operator+");
3039 case 'L':
3040 First += 2;
3041 return make<NameType>("operator+=");
3042 case 'p':
3043 First += 2;
3044 return make<NameType>("operator++");
3045 case 's':
3046 First += 2;
3047 return make<NameType>("operator+");
3048 case 't':
3049 First += 2;
3050 return make<NameType>("operator->");
3051 }
3052 return nullptr;
3053 case 'q':
3054 if (look(1) == 'u') {
3055 First += 2;
3056 return make<NameType>("operator?");
3057 }
3058 return nullptr;
3059 case 'r':
3060 switch (look(1)) {
3061 case 'm':
3062 First += 2;
3063 return make<NameType>("operator%");
3064 case 'M':
3065 First += 2;
3066 return make<NameType>("operator%=");
3067 case 's':
3068 First += 2;
3069 return make<NameType>("operator>>");
3070 case 'S':
3071 First += 2;
3072 return make<NameType>("operator>>=");
3073 }
3074 return nullptr;
3075 case 's':
3076 if (look(1) == 's') {
3077 First += 2;
3078 return make<NameType>("operator<=>");
3079 }
3080 return nullptr;
3081 // ::= v <digit> <source-name> # vendor extended operator
3082 case 'v':
3083 if (std::isdigit(look(1))) {
3084 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003085 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003086 if (SN == nullptr)
3087 return nullptr;
3088 return make<ConversionOperatorType>(SN);
3089 }
3090 return nullptr;
3091 }
3092 return nullptr;
3093}
3094
3095// <ctor-dtor-name> ::= C1 # complete object constructor
3096// ::= C2 # base object constructor
3097// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003098// extension ::= C4 # gcc old-style "[unified]" constructor
3099// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003100// ::= D0 # deleting destructor
3101// ::= D1 # complete object destructor
3102// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003103// extension ::= D4 # gcc old-style "[unified]" destructor
3104// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003105template <typename Derived, typename Alloc>
3106Node *
3107AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3108 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003109 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3110 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3111 switch (SSK) {
3112 case SpecialSubKind::string:
3113 case SpecialSubKind::istream:
3114 case SpecialSubKind::ostream:
3115 case SpecialSubKind::iostream:
3116 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003117 if (!SoFar)
3118 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003119 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003120 default:
3121 break;
3122 }
3123 }
3124
3125 if (consumeIf('C')) {
3126 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003127 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3128 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003129 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003130 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003131 ++First;
3132 if (State) State->CtorDtorConversion = true;
3133 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003134 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003135 return nullptr;
3136 }
Nico Weber29294792019-04-03 23:14:33 +00003137 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003138 }
3139
Nico Weber29294792019-04-03 23:14:33 +00003140 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3141 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003142 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003143 First += 2;
3144 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003145 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003146 }
3147
3148 return nullptr;
3149}
3150
3151// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3152// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3153//
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003154// <prefix> ::= <prefix> [L]* <unqualified-name>
Richard Smithc20d1442018-08-20 20:14:49 +00003155// ::= <template-prefix> <template-args>
3156// ::= <template-param>
3157// ::= <decltype>
3158// ::= # empty
3159// ::= <substitution>
3160// ::= <prefix> <data-member-prefix>
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003161// [*] extension
Richard Smithc20d1442018-08-20 20:14:49 +00003162//
3163// <data-member-prefix> := <member source-name> [<template-args>] M
3164//
3165// <template-prefix> ::= <prefix> <template unqualified-name>
3166// ::= <template-param>
3167// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003168template <typename Derived, typename Alloc>
3169Node *
3170AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003171 if (!consumeIf('N'))
3172 return nullptr;
3173
3174 Qualifiers CVTmp = parseCVQualifiers();
3175 if (State) State->CVQualifiers = CVTmp;
3176
3177 if (consumeIf('O')) {
3178 if (State) State->ReferenceQualifier = FrefQualRValue;
3179 } else if (consumeIf('R')) {
3180 if (State) State->ReferenceQualifier = FrefQualLValue;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003181 } else {
Richard Smithc20d1442018-08-20 20:14:49 +00003182 if (State) State->ReferenceQualifier = FrefQualNone;
Richard Smithb485b352018-08-24 23:30:26 +00003183 }
Richard Smithc20d1442018-08-20 20:14:49 +00003184
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003185 Node *SoFar = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003186 while (!consumeIf('E')) {
3187 consumeIf('L'); // extension
3188
Richard Smithc20d1442018-08-20 20:14:49 +00003189 if (consumeIf('M')) {
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003190 // <data-member-prefix> := <member source-name> [<template-args>] M
Richard Smithc20d1442018-08-20 20:14:49 +00003191 if (SoFar == nullptr)
3192 return nullptr;
3193 continue;
3194 }
3195
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003196 if (State)
3197 // Only set end-with-template on the case that does that.
3198 State->EndsWithTemplateArgs = false;
3199
Richard Smithc20d1442018-08-20 20:14:49 +00003200 if (look() == 'T') {
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003201 // ::= <template-param>
3202 if (SoFar != nullptr)
3203 return nullptr; // Cannot have a prefix.
3204 SoFar = getDerived().parseTemplateParam();
3205 } else if (look() == 'I') {
3206 // ::= <template-prefix> <template-args>
3207 if (SoFar == nullptr)
3208 return nullptr; // Must have a prefix.
Pavel Labathba825192018-10-16 14:29:14 +00003209 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003210 if (TA == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003211 return nullptr;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003212 if (SoFar->getKind() == Node::KNameWithTemplateArgs)
3213 // Semantically <template-args> <template-args> cannot be generated by a
3214 // C++ entity. There will always be [something like] a name between
3215 // them.
3216 return nullptr;
3217 if (State)
3218 State->EndsWithTemplateArgs = true;
Richard Smithc20d1442018-08-20 20:14:49 +00003219 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003220 } else if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
3221 // ::= <decltype>
3222 if (SoFar != nullptr)
3223 return nullptr; // Cannot have a prefix.
3224 SoFar = getDerived().parseDecltype();
3225 } else if (look() == 'S') {
3226 // ::= <substitution>
3227 if (SoFar != nullptr)
3228 return nullptr; // Cannot have a prefix.
3229 if (look(1) == 't') {
3230 // parseSubstition does not handle 'St'.
3231 First += 2;
3232 SoFar = make<NameType>("std");
3233 } else {
3234 SoFar = getDerived().parseSubstitution();
3235 }
Richard Smithc20d1442018-08-20 20:14:49 +00003236 if (SoFar == nullptr)
3237 return nullptr;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003238 continue; // Do not push a new substitution.
3239 } else {
3240 Node *N = nullptr;
3241 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
3242 // An <unqualified-name> that's actually a <ctor-dtor-name>.
3243 if (SoFar == nullptr)
3244 return nullptr;
3245 N = getDerived().parseCtorDtorName(SoFar, State);
3246 if (N != nullptr)
3247 N = getDerived().parseAbiTags(N);
3248 } else {
3249 // ::= <prefix> <unqualified-name>
3250 N = getDerived().parseUnqualifiedName(State);
3251 }
3252 if (N == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003253 return nullptr;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003254 if (SoFar)
3255 SoFar = make<NestedName>(SoFar, N);
3256 else
3257 SoFar = N;
Richard Smithc20d1442018-08-20 20:14:49 +00003258 }
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003259 if (SoFar == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003260 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003261 Subs.push_back(SoFar);
3262 }
3263
3264 if (SoFar == nullptr || Subs.empty())
3265 return nullptr;
3266
3267 Subs.pop_back();
3268 return SoFar;
3269}
3270
3271// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003272template <typename Derived, typename Alloc>
3273Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3274 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003275 if (SN == nullptr)
3276 return nullptr;
3277 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003278 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003279 if (TA == nullptr)
3280 return nullptr;
3281 return make<NameWithTemplateArgs>(SN, TA);
3282 }
3283 return SN;
3284}
3285
3286// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3287// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003288template <typename Derived, typename Alloc>
3289Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003290 Node *Result;
3291 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003292 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003293 else
Pavel Labathba825192018-10-16 14:29:14 +00003294 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003295 if (Result == nullptr)
3296 return nullptr;
3297 return make<DtorName>(Result);
3298}
3299
3300// <unresolved-type> ::= <template-param>
3301// ::= <decltype>
3302// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003303template <typename Derived, typename Alloc>
3304Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003305 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003306 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003307 if (TP == nullptr)
3308 return nullptr;
3309 Subs.push_back(TP);
3310 return TP;
3311 }
3312 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003313 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003314 if (DT == nullptr)
3315 return nullptr;
3316 Subs.push_back(DT);
3317 return DT;
3318 }
Pavel Labathba825192018-10-16 14:29:14 +00003319 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003320}
3321
3322// <base-unresolved-name> ::= <simple-id> # unresolved name
3323// extension ::= <operator-name> # unresolved operator-function-id
3324// extension ::= <operator-name> <template-args> # unresolved operator template-id
3325// ::= on <operator-name> # unresolved operator-function-id
3326// ::= on <operator-name> <template-args> # unresolved operator template-id
3327// ::= dn <destructor-name> # destructor or pseudo-destructor;
3328// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003329template <typename Derived, typename Alloc>
3330Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003331 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003332 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003333
3334 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003335 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003336
3337 consumeIf("on");
3338
Pavel Labathba825192018-10-16 14:29:14 +00003339 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003340 if (Oper == nullptr)
3341 return nullptr;
3342 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003343 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003344 if (TA == nullptr)
3345 return nullptr;
3346 return make<NameWithTemplateArgs>(Oper, TA);
3347 }
3348 return Oper;
3349}
3350
3351// <unresolved-name>
3352// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3353// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3354// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3355// # A::x, N::y, A<T>::z; "gs" means leading "::"
Nathan Sidwell77c52e22022-01-28 11:59:03 -08003356// [gs] has been parsed by caller.
Richard Smithc20d1442018-08-20 20:14:49 +00003357// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3358// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3359// # T::N::x /decltype(p)::N::x
3360// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3361//
3362// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003363template <typename Derived, typename Alloc>
Nathan Sidwell77c52e22022-01-28 11:59:03 -08003364Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName(bool Global) {
Richard Smithc20d1442018-08-20 20:14:49 +00003365 Node *SoFar = nullptr;
3366
3367 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3368 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3369 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003370 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003371 if (SoFar == nullptr)
3372 return nullptr;
3373
3374 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003375 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003376 if (TA == nullptr)
3377 return nullptr;
3378 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003379 if (!SoFar)
3380 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003381 }
3382
3383 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003384 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003385 if (Qual == nullptr)
3386 return nullptr;
3387 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003388 if (!SoFar)
3389 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003390 }
3391
Pavel Labathba825192018-10-16 14:29:14 +00003392 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003393 if (Base == nullptr)
3394 return nullptr;
3395 return make<QualifiedName>(SoFar, Base);
3396 }
3397
Richard Smithc20d1442018-08-20 20:14:49 +00003398 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3399 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003400 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003401 if (SoFar == nullptr)
3402 return nullptr;
3403 if (Global)
3404 SoFar = make<GlobalQualifiedName>(SoFar);
3405 return SoFar;
3406 }
3407
3408 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3409 if (std::isdigit(look())) {
3410 do {
Pavel Labathba825192018-10-16 14:29:14 +00003411 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003412 if (Qual == nullptr)
3413 return nullptr;
3414 if (SoFar)
3415 SoFar = make<QualifiedName>(SoFar, Qual);
3416 else if (Global)
3417 SoFar = make<GlobalQualifiedName>(Qual);
3418 else
3419 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003420 if (!SoFar)
3421 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003422 } while (!consumeIf('E'));
3423 }
3424 // sr <unresolved-type> <base-unresolved-name>
3425 // sr <unresolved-type> <template-args> <base-unresolved-name>
3426 else {
Pavel Labathba825192018-10-16 14:29:14 +00003427 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003428 if (SoFar == nullptr)
3429 return nullptr;
3430
3431 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003432 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003433 if (TA == nullptr)
3434 return nullptr;
3435 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003436 if (!SoFar)
3437 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003438 }
3439 }
3440
3441 assert(SoFar != nullptr);
3442
Pavel Labathba825192018-10-16 14:29:14 +00003443 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003444 if (Base == nullptr)
3445 return nullptr;
3446 return make<QualifiedName>(SoFar, Base);
3447}
3448
3449// <abi-tags> ::= <abi-tag> [<abi-tags>]
3450// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003451template <typename Derived, typename Alloc>
3452Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003453 while (consumeIf('B')) {
3454 StringView SN = parseBareSourceName();
3455 if (SN.empty())
3456 return nullptr;
3457 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003458 if (!N)
3459 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003460 }
3461 return N;
3462}
3463
3464// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003465template <typename Alloc, typename Derived>
3466StringView
3467AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003468 const char *Tmp = First;
3469 if (AllowNegative)
3470 consumeIf('n');
3471 if (numLeft() == 0 || !std::isdigit(*First))
3472 return StringView();
3473 while (numLeft() != 0 && std::isdigit(*First))
3474 ++First;
3475 return StringView(Tmp, First);
3476}
3477
3478// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003479template <typename Alloc, typename Derived>
3480bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003481 *Out = 0;
3482 if (look() < '0' || look() > '9')
3483 return true;
3484 while (look() >= '0' && look() <= '9') {
3485 *Out *= 10;
3486 *Out += static_cast<size_t>(consume() - '0');
3487 }
3488 return false;
3489}
3490
Pavel Labathba825192018-10-16 14:29:14 +00003491template <typename Alloc, typename Derived>
3492StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003493 size_t Int = 0;
3494 if (parsePositiveInteger(&Int) || numLeft() < Int)
3495 return StringView();
3496 StringView R(First, First + Int);
3497 First += Int;
3498 return R;
3499}
3500
3501// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3502//
3503// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3504// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3505// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3506//
3507// <ref-qualifier> ::= R # & ref-qualifier
3508// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003509template <typename Derived, typename Alloc>
3510Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003511 Qualifiers CVQuals = parseCVQualifiers();
3512
3513 Node *ExceptionSpec = nullptr;
3514 if (consumeIf("Do")) {
3515 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003516 if (!ExceptionSpec)
3517 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003518 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003519 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003520 if (E == nullptr || !consumeIf('E'))
3521 return nullptr;
3522 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003523 if (!ExceptionSpec)
3524 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003525 } else if (consumeIf("Dw")) {
3526 size_t SpecsBegin = Names.size();
3527 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003528 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003529 if (T == nullptr)
3530 return nullptr;
3531 Names.push_back(T);
3532 }
3533 ExceptionSpec =
3534 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003535 if (!ExceptionSpec)
3536 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003537 }
3538
3539 consumeIf("Dx"); // transaction safe
3540
3541 if (!consumeIf('F'))
3542 return nullptr;
3543 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003544 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003545 if (ReturnType == nullptr)
3546 return nullptr;
3547
3548 FunctionRefQual ReferenceQualifier = FrefQualNone;
3549 size_t ParamsBegin = Names.size();
3550 while (true) {
3551 if (consumeIf('E'))
3552 break;
3553 if (consumeIf('v'))
3554 continue;
3555 if (consumeIf("RE")) {
3556 ReferenceQualifier = FrefQualLValue;
3557 break;
3558 }
3559 if (consumeIf("OE")) {
3560 ReferenceQualifier = FrefQualRValue;
3561 break;
3562 }
Pavel Labathba825192018-10-16 14:29:14 +00003563 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003564 if (T == nullptr)
3565 return nullptr;
3566 Names.push_back(T);
3567 }
3568
3569 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3570 return make<FunctionType>(ReturnType, Params, CVQuals,
3571 ReferenceQualifier, ExceptionSpec);
3572}
3573
3574// extension:
3575// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3576// ::= Dv [<dimension expression>] _ <element type>
3577// <extended element type> ::= <element type>
3578// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003579template <typename Derived, typename Alloc>
3580Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003581 if (!consumeIf("Dv"))
3582 return nullptr;
3583 if (look() >= '1' && look() <= '9') {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003584 Node *DimensionNumber = make<NameType>(parseNumber());
3585 if (!DimensionNumber)
3586 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003587 if (!consumeIf('_'))
3588 return nullptr;
3589 if (consumeIf('p'))
3590 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003591 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003592 if (ElemType == nullptr)
3593 return nullptr;
3594 return make<VectorType>(ElemType, DimensionNumber);
3595 }
3596
3597 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003598 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003599 if (!DimExpr)
3600 return nullptr;
3601 if (!consumeIf('_'))
3602 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003603 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003604 if (!ElemType)
3605 return nullptr;
3606 return make<VectorType>(ElemType, DimExpr);
3607 }
Pavel Labathba825192018-10-16 14:29:14 +00003608 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003609 if (!ElemType)
3610 return nullptr;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003611 return make<VectorType>(ElemType, /*Dimension=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003612}
3613
3614// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3615// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003616template <typename Derived, typename Alloc>
3617Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003618 if (!consumeIf('D'))
3619 return nullptr;
3620 if (!consumeIf('t') && !consumeIf('T'))
3621 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003622 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003623 if (E == nullptr)
3624 return nullptr;
3625 if (!consumeIf('E'))
3626 return nullptr;
3627 return make<EnclosingExpr>("decltype(", E, ")");
3628}
3629
3630// <array-type> ::= A <positive dimension number> _ <element type>
3631// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003632template <typename Derived, typename Alloc>
3633Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003634 if (!consumeIf('A'))
3635 return nullptr;
3636
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003637 Node *Dimension = nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003638
Richard Smithc20d1442018-08-20 20:14:49 +00003639 if (std::isdigit(look())) {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003640 Dimension = make<NameType>(parseNumber());
3641 if (!Dimension)
3642 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003643 if (!consumeIf('_'))
3644 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003645 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003646 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003647 if (DimExpr == nullptr)
3648 return nullptr;
3649 if (!consumeIf('_'))
3650 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003651 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003652 }
3653
Pavel Labathba825192018-10-16 14:29:14 +00003654 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003655 if (Ty == nullptr)
3656 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003657 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003658}
3659
3660// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003661template <typename Derived, typename Alloc>
3662Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003663 if (!consumeIf('M'))
3664 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003665 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003666 if (ClassType == nullptr)
3667 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003668 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003669 if (MemberType == nullptr)
3670 return nullptr;
3671 return make<PointerToMemberType>(ClassType, MemberType);
3672}
3673
3674// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3675// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3676// ::= Tu <name> # dependent elaborated type specifier using 'union'
3677// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003678template <typename Derived, typename Alloc>
3679Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003680 StringView ElabSpef;
3681 if (consumeIf("Ts"))
3682 ElabSpef = "struct";
3683 else if (consumeIf("Tu"))
3684 ElabSpef = "union";
3685 else if (consumeIf("Te"))
3686 ElabSpef = "enum";
3687
Pavel Labathba825192018-10-16 14:29:14 +00003688 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003689 if (Name == nullptr)
3690 return nullptr;
3691
3692 if (!ElabSpef.empty())
3693 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3694
3695 return Name;
3696}
3697
3698// <qualified-type> ::= <qualifiers> <type>
3699// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3700// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003701template <typename Derived, typename Alloc>
3702Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003703 if (consumeIf('U')) {
3704 StringView Qual = parseBareSourceName();
3705 if (Qual.empty())
3706 return nullptr;
3707
Richard Smithc20d1442018-08-20 20:14:49 +00003708 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3709 if (Qual.startsWith("objcproto")) {
3710 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3711 StringView Proto;
3712 {
3713 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3714 SaveLast(Last, ProtoSourceName.end());
3715 Proto = parseBareSourceName();
3716 }
3717 if (Proto.empty())
3718 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003719 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003720 if (Child == nullptr)
3721 return nullptr;
3722 return make<ObjCProtoName>(Child, Proto);
3723 }
3724
Alex Orlovf50df922021-03-24 10:21:32 +04003725 Node *TA = nullptr;
3726 if (look() == 'I') {
3727 TA = getDerived().parseTemplateArgs();
3728 if (TA == nullptr)
3729 return nullptr;
3730 }
3731
Pavel Labathba825192018-10-16 14:29:14 +00003732 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003733 if (Child == nullptr)
3734 return nullptr;
Alex Orlovf50df922021-03-24 10:21:32 +04003735 return make<VendorExtQualType>(Child, Qual, TA);
Richard Smithc20d1442018-08-20 20:14:49 +00003736 }
3737
3738 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003739 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003740 if (Ty == nullptr)
3741 return nullptr;
3742 if (Quals != QualNone)
3743 Ty = make<QualType>(Ty, Quals);
3744 return Ty;
3745}
3746
3747// <type> ::= <builtin-type>
3748// ::= <qualified-type>
3749// ::= <function-type>
3750// ::= <class-enum-type>
3751// ::= <array-type>
3752// ::= <pointer-to-member-type>
3753// ::= <template-param>
3754// ::= <template-template-param> <template-args>
3755// ::= <decltype>
3756// ::= P <type> # pointer
3757// ::= R <type> # l-value reference
3758// ::= O <type> # r-value reference (C++11)
3759// ::= C <type> # complex pair (C99)
3760// ::= G <type> # imaginary (C99)
3761// ::= <substitution> # See Compression below
3762// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3763// extension ::= <vector-type> # <vector-type> starts with Dv
3764//
3765// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3766// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003767template <typename Derived, typename Alloc>
3768Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003769 Node *Result = nullptr;
3770
Richard Smithc20d1442018-08-20 20:14:49 +00003771 switch (look()) {
3772 // ::= <qualified-type>
3773 case 'r':
3774 case 'V':
3775 case 'K': {
3776 unsigned AfterQuals = 0;
3777 if (look(AfterQuals) == 'r') ++AfterQuals;
3778 if (look(AfterQuals) == 'V') ++AfterQuals;
3779 if (look(AfterQuals) == 'K') ++AfterQuals;
3780
3781 if (look(AfterQuals) == 'F' ||
3782 (look(AfterQuals) == 'D' &&
3783 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3784 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003785 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003786 break;
3787 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003788 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003789 }
3790 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003791 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003792 break;
3793 }
3794 // <builtin-type> ::= v # void
3795 case 'v':
3796 ++First;
3797 return make<NameType>("void");
3798 // ::= w # wchar_t
3799 case 'w':
3800 ++First;
3801 return make<NameType>("wchar_t");
3802 // ::= b # bool
3803 case 'b':
3804 ++First;
3805 return make<NameType>("bool");
3806 // ::= c # char
3807 case 'c':
3808 ++First;
3809 return make<NameType>("char");
3810 // ::= a # signed char
3811 case 'a':
3812 ++First;
3813 return make<NameType>("signed char");
3814 // ::= h # unsigned char
3815 case 'h':
3816 ++First;
3817 return make<NameType>("unsigned char");
3818 // ::= s # short
3819 case 's':
3820 ++First;
3821 return make<NameType>("short");
3822 // ::= t # unsigned short
3823 case 't':
3824 ++First;
3825 return make<NameType>("unsigned short");
3826 // ::= i # int
3827 case 'i':
3828 ++First;
3829 return make<NameType>("int");
3830 // ::= j # unsigned int
3831 case 'j':
3832 ++First;
3833 return make<NameType>("unsigned int");
3834 // ::= l # long
3835 case 'l':
3836 ++First;
3837 return make<NameType>("long");
3838 // ::= m # unsigned long
3839 case 'm':
3840 ++First;
3841 return make<NameType>("unsigned long");
3842 // ::= x # long long, __int64
3843 case 'x':
3844 ++First;
3845 return make<NameType>("long long");
3846 // ::= y # unsigned long long, __int64
3847 case 'y':
3848 ++First;
3849 return make<NameType>("unsigned long long");
3850 // ::= n # __int128
3851 case 'n':
3852 ++First;
3853 return make<NameType>("__int128");
3854 // ::= o # unsigned __int128
3855 case 'o':
3856 ++First;
3857 return make<NameType>("unsigned __int128");
3858 // ::= f # float
3859 case 'f':
3860 ++First;
3861 return make<NameType>("float");
3862 // ::= d # double
3863 case 'd':
3864 ++First;
3865 return make<NameType>("double");
3866 // ::= e # long double, __float80
3867 case 'e':
3868 ++First;
3869 return make<NameType>("long double");
3870 // ::= g # __float128
3871 case 'g':
3872 ++First;
3873 return make<NameType>("__float128");
3874 // ::= z # ellipsis
3875 case 'z':
3876 ++First;
3877 return make<NameType>("...");
3878
3879 // <builtin-type> ::= u <source-name> # vendor extended type
3880 case 'u': {
3881 ++First;
3882 StringView Res = parseBareSourceName();
3883 if (Res.empty())
3884 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003885 // Typically, <builtin-type>s are not considered substitution candidates,
3886 // but the exception to that exception is vendor extended types (Itanium C++
3887 // ABI 5.9.1).
3888 Result = make<NameType>(Res);
3889 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003890 }
3891 case 'D':
3892 switch (look(1)) {
3893 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3894 case 'd':
3895 First += 2;
3896 return make<NameType>("decimal64");
3897 // ::= De # IEEE 754r decimal floating point (128 bits)
3898 case 'e':
3899 First += 2;
3900 return make<NameType>("decimal128");
3901 // ::= Df # IEEE 754r decimal floating point (32 bits)
3902 case 'f':
3903 First += 2;
3904 return make<NameType>("decimal32");
3905 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3906 case 'h':
3907 First += 2;
Stuart Bradye8bf5772021-06-07 16:30:22 +01003908 return make<NameType>("half");
Pengfei Wang50e90b82021-09-23 11:02:25 +08003909 // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits)
3910 case 'F': {
3911 First += 2;
3912 Node *DimensionNumber = make<NameType>(parseNumber());
3913 if (!DimensionNumber)
3914 return nullptr;
3915 if (!consumeIf('_'))
3916 return nullptr;
3917 return make<BinaryFPType>(DimensionNumber);
3918 }
Richard Smithc20d1442018-08-20 20:14:49 +00003919 // ::= Di # char32_t
3920 case 'i':
3921 First += 2;
3922 return make<NameType>("char32_t");
3923 // ::= Ds # char16_t
3924 case 's':
3925 First += 2;
3926 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003927 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3928 case 'u':
3929 First += 2;
3930 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003931 // ::= Da # auto (in dependent new-expressions)
3932 case 'a':
3933 First += 2;
3934 return make<NameType>("auto");
3935 // ::= Dc # decltype(auto)
3936 case 'c':
3937 First += 2;
3938 return make<NameType>("decltype(auto)");
3939 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3940 case 'n':
3941 First += 2;
3942 return make<NameType>("std::nullptr_t");
3943
3944 // ::= <decltype>
3945 case 't':
3946 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003947 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003948 break;
3949 }
3950 // extension ::= <vector-type> # <vector-type> starts with Dv
3951 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003952 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003953 break;
3954 }
3955 // ::= Dp <type> # pack expansion (C++0x)
3956 case 'p': {
3957 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003958 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003959 if (!Child)
3960 return nullptr;
3961 Result = make<ParameterPackExpansion>(Child);
3962 break;
3963 }
3964 // Exception specifier on a function type.
3965 case 'o':
3966 case 'O':
3967 case 'w':
3968 // Transaction safe function type.
3969 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003970 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003971 break;
3972 }
3973 break;
3974 // ::= <function-type>
3975 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003976 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003977 break;
3978 }
3979 // ::= <array-type>
3980 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003981 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003982 break;
3983 }
3984 // ::= <pointer-to-member-type>
3985 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003986 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003987 break;
3988 }
3989 // ::= <template-param>
3990 case 'T': {
3991 // This could be an elaborate type specifier on a <class-enum-type>.
3992 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003993 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003994 break;
3995 }
3996
Pavel Labathba825192018-10-16 14:29:14 +00003997 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003998 if (Result == nullptr)
3999 return nullptr;
4000
4001 // Result could be either of:
4002 // <type> ::= <template-param>
4003 // <type> ::= <template-template-param> <template-args>
4004 //
4005 // <template-template-param> ::= <template-param>
4006 // ::= <substitution>
4007 //
4008 // If this is followed by some <template-args>, and we're permitted to
4009 // parse them, take the second production.
4010
4011 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004012 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004013 if (TA == nullptr)
4014 return nullptr;
4015 Result = make<NameWithTemplateArgs>(Result, TA);
4016 }
4017 break;
4018 }
4019 // ::= P <type> # pointer
4020 case 'P': {
4021 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004022 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004023 if (Ptr == nullptr)
4024 return nullptr;
4025 Result = make<PointerType>(Ptr);
4026 break;
4027 }
4028 // ::= R <type> # l-value reference
4029 case 'R': {
4030 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004031 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004032 if (Ref == nullptr)
4033 return nullptr;
4034 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
4035 break;
4036 }
4037 // ::= O <type> # r-value reference (C++11)
4038 case 'O': {
4039 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004040 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004041 if (Ref == nullptr)
4042 return nullptr;
4043 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
4044 break;
4045 }
4046 // ::= C <type> # complex pair (C99)
4047 case 'C': {
4048 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004049 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004050 if (P == nullptr)
4051 return nullptr;
4052 Result = make<PostfixQualifiedType>(P, " complex");
4053 break;
4054 }
4055 // ::= G <type> # imaginary (C99)
4056 case 'G': {
4057 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004058 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004059 if (P == nullptr)
4060 return P;
4061 Result = make<PostfixQualifiedType>(P, " imaginary");
4062 break;
4063 }
4064 // ::= <substitution> # See Compression below
4065 case 'S': {
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004066 if (look(1) != 't') {
4067 Result = getDerived().parseSubstitution();
4068 if (Result == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00004069 return nullptr;
4070
4071 // Sub could be either of:
4072 // <type> ::= <substitution>
4073 // <type> ::= <template-template-param> <template-args>
4074 //
4075 // <template-template-param> ::= <template-param>
4076 // ::= <substitution>
4077 //
4078 // If this is followed by some <template-args>, and we're permitted to
4079 // parse them, take the second production.
4080
4081 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004082 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004083 if (TA == nullptr)
4084 return nullptr;
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004085 Result = make<NameWithTemplateArgs>(Result, TA);
4086 } else {
4087 // If all we parsed was a substitution, don't re-insert into the
4088 // substitution table.
4089 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00004090 }
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004091 break;
Richard Smithc20d1442018-08-20 20:14:49 +00004092 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00004093 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004094 }
4095 // ::= <class-enum-type>
4096 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004097 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004098 break;
4099 }
4100 }
4101
4102 // If we parsed a type, insert it into the substitution table. Note that all
4103 // <builtin-type>s and <substitution>s have already bailed out, because they
4104 // don't get substitutions.
4105 if (Result != nullptr)
4106 Subs.push_back(Result);
4107 return Result;
4108}
4109
Pavel Labathba825192018-10-16 14:29:14 +00004110template <typename Derived, typename Alloc>
4111Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4112 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004113 if (E == nullptr)
4114 return nullptr;
4115 return make<PrefixExpr>(Kind, E);
4116}
4117
Pavel Labathba825192018-10-16 14:29:14 +00004118template <typename Derived, typename Alloc>
4119Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4120 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004121 if (LHS == nullptr)
4122 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004123 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004124 if (RHS == nullptr)
4125 return nullptr;
4126 return make<BinaryExpr>(LHS, Kind, RHS);
4127}
4128
Pavel Labathba825192018-10-16 14:29:14 +00004129template <typename Derived, typename Alloc>
4130Node *
4131AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004132 StringView Tmp = parseNumber(true);
4133 if (!Tmp.empty() && consumeIf('E'))
4134 return make<IntegerLiteral>(Lit, Tmp);
4135 return nullptr;
4136}
4137
4138// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004139template <typename Alloc, typename Derived>
4140Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004141 Qualifiers CVR = QualNone;
4142 if (consumeIf('r'))
4143 CVR |= QualRestrict;
4144 if (consumeIf('V'))
4145 CVR |= QualVolatile;
4146 if (consumeIf('K'))
4147 CVR |= QualConst;
4148 return CVR;
4149}
4150
4151// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4152// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4153// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4154// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L > 0, second and later parameters
Erik Pilkington91c24af2020-05-13 22:19:45 -04004155// ::= fpT # 'this' expression (not part of standard?)
Pavel Labathba825192018-10-16 14:29:14 +00004156template <typename Derived, typename Alloc>
4157Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Erik Pilkington91c24af2020-05-13 22:19:45 -04004158 if (consumeIf("fpT"))
4159 return make<NameType>("this");
Richard Smithc20d1442018-08-20 20:14:49 +00004160 if (consumeIf("fp")) {
4161 parseCVQualifiers();
4162 StringView Num = parseNumber();
4163 if (!consumeIf('_'))
4164 return nullptr;
4165 return make<FunctionParam>(Num);
4166 }
4167 if (consumeIf("fL")) {
4168 if (parseNumber().empty())
4169 return nullptr;
4170 if (!consumeIf('p'))
4171 return nullptr;
4172 parseCVQualifiers();
4173 StringView Num = parseNumber();
4174 if (!consumeIf('_'))
4175 return nullptr;
4176 return make<FunctionParam>(Num);
4177 }
4178 return nullptr;
4179}
4180
4181// [gs] nw <expression>* _ <type> E # new (expr-list) type
4182// [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4183// [gs] na <expression>* _ <type> E # new[] (expr-list) type
4184// [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4185// <initializer> ::= pi <expression>* E # parenthesized initialization
Pavel Labathba825192018-10-16 14:29:14 +00004186template <typename Derived, typename Alloc>
4187Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004188 bool Global = consumeIf("gs");
4189 bool IsArray = look(1) == 'a';
4190 if (!consumeIf("nw") && !consumeIf("na"))
4191 return nullptr;
4192 size_t Exprs = Names.size();
4193 while (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00004194 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004195 if (Ex == nullptr)
4196 return nullptr;
4197 Names.push_back(Ex);
4198 }
4199 NodeArray ExprList = popTrailingNodeArray(Exprs);
Pavel Labathba825192018-10-16 14:29:14 +00004200 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004201 if (Ty == nullptr)
4202 return Ty;
4203 if (consumeIf("pi")) {
4204 size_t InitsBegin = Names.size();
4205 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004206 Node *Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004207 if (Init == nullptr)
4208 return Init;
4209 Names.push_back(Init);
4210 }
4211 NodeArray Inits = popTrailingNodeArray(InitsBegin);
4212 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
4213 } else if (!consumeIf('E'))
4214 return nullptr;
4215 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
4216}
4217
4218// cv <type> <expression> # conversion with one argument
4219// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004220template <typename Derived, typename Alloc>
4221Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004222 if (!consumeIf("cv"))
4223 return nullptr;
4224 Node *Ty;
4225 {
4226 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004227 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004228 }
4229
4230 if (Ty == nullptr)
4231 return nullptr;
4232
4233 if (consumeIf('_')) {
4234 size_t ExprsBegin = Names.size();
4235 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004236 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004237 if (E == nullptr)
4238 return E;
4239 Names.push_back(E);
4240 }
4241 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4242 return make<ConversionExpr>(Ty, Exprs);
4243 }
4244
Pavel Labathba825192018-10-16 14:29:14 +00004245 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004246 if (E[0] == nullptr)
4247 return nullptr;
4248 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4249}
4250
4251// <expr-primary> ::= L <type> <value number> E # integer literal
4252// ::= L <type> <value float> E # floating literal
4253// ::= L <string type> E # string literal
4254// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004255// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004256// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4257// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004258template <typename Derived, typename Alloc>
4259Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004260 if (!consumeIf('L'))
4261 return nullptr;
4262 switch (look()) {
4263 case 'w':
4264 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004265 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004266 case 'b':
4267 if (consumeIf("b0E"))
4268 return make<BoolExpr>(0);
4269 if (consumeIf("b1E"))
4270 return make<BoolExpr>(1);
4271 return nullptr;
4272 case 'c':
4273 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004274 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004275 case 'a':
4276 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004277 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004278 case 'h':
4279 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004280 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004281 case 's':
4282 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004283 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004284 case 't':
4285 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004286 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004287 case 'i':
4288 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004289 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004290 case 'j':
4291 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004292 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004293 case 'l':
4294 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004295 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004296 case 'm':
4297 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004298 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004299 case 'x':
4300 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004301 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004302 case 'y':
4303 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004304 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004305 case 'n':
4306 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004307 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004308 case 'o':
4309 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004310 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004311 case 'f':
4312 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004313 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004314 case 'd':
4315 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004316 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004317 case 'e':
4318 ++First;
Xing Xue3dc5e082020-04-15 09:59:06 -04004319#if defined(__powerpc__) || defined(__s390__)
4320 // Handle cases where long doubles encoded with e have the same size
4321 // and representation as doubles.
4322 return getDerived().template parseFloatingLiteral<double>();
4323#else
Pavel Labathba825192018-10-16 14:29:14 +00004324 return getDerived().template parseFloatingLiteral<long double>();
Xing Xue3dc5e082020-04-15 09:59:06 -04004325#endif
Richard Smithc20d1442018-08-20 20:14:49 +00004326 case '_':
4327 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004328 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004329 if (R != nullptr && consumeIf('E'))
4330 return R;
4331 }
4332 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004333 case 'A': {
4334 Node *T = getDerived().parseType();
4335 if (T == nullptr)
4336 return nullptr;
4337 // FIXME: We need to include the string contents in the mangling.
4338 if (consumeIf('E'))
4339 return make<StringLiteral>(T);
4340 return nullptr;
4341 }
4342 case 'D':
4343 if (consumeIf("DnE"))
4344 return make<NameType>("nullptr");
4345 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004346 case 'T':
4347 // Invalid mangled name per
4348 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4349 return nullptr;
Richard Smithfb917462019-09-09 22:26:04 +00004350 case 'U': {
4351 // FIXME: Should we support LUb... for block literals?
4352 if (look(1) != 'l')
4353 return nullptr;
4354 Node *T = parseUnnamedTypeName(nullptr);
4355 if (!T || !consumeIf('E'))
4356 return nullptr;
4357 return make<LambdaExpr>(T);
4358 }
Richard Smithc20d1442018-08-20 20:14:49 +00004359 default: {
4360 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004361 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004362 if (T == nullptr)
4363 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004364 StringView N = parseNumber(/*AllowNegative=*/true);
Richard Smithfb917462019-09-09 22:26:04 +00004365 if (N.empty())
4366 return nullptr;
4367 if (!consumeIf('E'))
4368 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004369 return make<EnumLiteral>(T, N);
Richard Smithc20d1442018-08-20 20:14:49 +00004370 }
4371 }
4372}
4373
4374// <braced-expression> ::= <expression>
4375// ::= di <field source-name> <braced-expression> # .name = expr
4376// ::= dx <index expression> <braced-expression> # [expr] = expr
4377// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004378template <typename Derived, typename Alloc>
4379Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004380 if (look() == 'd') {
4381 switch (look(1)) {
4382 case 'i': {
4383 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004384 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004385 if (Field == nullptr)
4386 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004387 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004388 if (Init == nullptr)
4389 return nullptr;
4390 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4391 }
4392 case 'x': {
4393 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004394 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004395 if (Index == nullptr)
4396 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004397 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004398 if (Init == nullptr)
4399 return nullptr;
4400 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4401 }
4402 case 'X': {
4403 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004404 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004405 if (RangeBegin == nullptr)
4406 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004407 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004408 if (RangeEnd == nullptr)
4409 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004410 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004411 if (Init == nullptr)
4412 return nullptr;
4413 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4414 }
4415 }
4416 }
Pavel Labathba825192018-10-16 14:29:14 +00004417 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004418}
4419
4420// (not yet in the spec)
4421// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4422// ::= fR <binary-operator-name> <expression> <expression>
4423// ::= fl <binary-operator-name> <expression>
4424// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004425template <typename Derived, typename Alloc>
4426Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004427 if (!consumeIf('f'))
4428 return nullptr;
4429
4430 char FoldKind = look();
4431 bool IsLeftFold, HasInitializer;
4432 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4433 if (FoldKind == 'l' || FoldKind == 'L')
4434 IsLeftFold = true;
4435 else if (FoldKind == 'r' || FoldKind == 'R')
4436 IsLeftFold = false;
4437 else
4438 return nullptr;
4439 ++First;
4440
4441 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4442 StringView OperatorName;
4443 if (consumeIf("aa")) OperatorName = "&&";
4444 else if (consumeIf("an")) OperatorName = "&";
4445 else if (consumeIf("aN")) OperatorName = "&=";
4446 else if (consumeIf("aS")) OperatorName = "=";
4447 else if (consumeIf("cm")) OperatorName = ",";
4448 else if (consumeIf("ds")) OperatorName = ".*";
4449 else if (consumeIf("dv")) OperatorName = "/";
4450 else if (consumeIf("dV")) OperatorName = "/=";
4451 else if (consumeIf("eo")) OperatorName = "^";
4452 else if (consumeIf("eO")) OperatorName = "^=";
4453 else if (consumeIf("eq")) OperatorName = "==";
4454 else if (consumeIf("ge")) OperatorName = ">=";
4455 else if (consumeIf("gt")) OperatorName = ">";
4456 else if (consumeIf("le")) OperatorName = "<=";
4457 else if (consumeIf("ls")) OperatorName = "<<";
4458 else if (consumeIf("lS")) OperatorName = "<<=";
4459 else if (consumeIf("lt")) OperatorName = "<";
4460 else if (consumeIf("mi")) OperatorName = "-";
4461 else if (consumeIf("mI")) OperatorName = "-=";
4462 else if (consumeIf("ml")) OperatorName = "*";
4463 else if (consumeIf("mL")) OperatorName = "*=";
4464 else if (consumeIf("ne")) OperatorName = "!=";
4465 else if (consumeIf("oo")) OperatorName = "||";
4466 else if (consumeIf("or")) OperatorName = "|";
4467 else if (consumeIf("oR")) OperatorName = "|=";
4468 else if (consumeIf("pl")) OperatorName = "+";
4469 else if (consumeIf("pL")) OperatorName = "+=";
4470 else if (consumeIf("rm")) OperatorName = "%";
4471 else if (consumeIf("rM")) OperatorName = "%=";
4472 else if (consumeIf("rs")) OperatorName = ">>";
4473 else if (consumeIf("rS")) OperatorName = ">>=";
4474 else return nullptr;
4475
Pavel Labathba825192018-10-16 14:29:14 +00004476 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004477 if (Pack == nullptr)
4478 return nullptr;
4479 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004480 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004481 if (Init == nullptr)
4482 return nullptr;
4483 }
4484
4485 if (IsLeftFold && Init)
4486 std::swap(Pack, Init);
4487
4488 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4489}
4490
Richard Smith1865d2f2020-10-22 19:29:36 -07004491// <expression> ::= mc <parameter type> <expr> [<offset number>] E
4492//
4493// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4494template <typename Derived, typename Alloc>
4495Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() {
4496 Node *Ty = getDerived().parseType();
4497 if (!Ty)
4498 return nullptr;
4499 Node *Expr = getDerived().parseExpr();
4500 if (!Expr)
4501 return nullptr;
4502 StringView Offset = getDerived().parseNumber(true);
4503 if (!consumeIf('E'))
4504 return nullptr;
4505 return make<PointerToMemberConversionExpr>(Ty, Expr, Offset);
4506}
4507
4508// <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
4509// <union-selector> ::= _ [<number>]
4510//
4511// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4512template <typename Derived, typename Alloc>
4513Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
4514 Node *Ty = getDerived().parseType();
4515 if (!Ty)
4516 return nullptr;
4517 Node *Expr = getDerived().parseExpr();
4518 if (!Expr)
4519 return nullptr;
4520 StringView Offset = getDerived().parseNumber(true);
4521 size_t SelectorsBegin = Names.size();
4522 while (consumeIf('_')) {
4523 Node *Selector = make<NameType>(parseNumber());
4524 if (!Selector)
4525 return nullptr;
4526 Names.push_back(Selector);
4527 }
4528 bool OnePastTheEnd = consumeIf('p');
4529 if (!consumeIf('E'))
4530 return nullptr;
4531 return make<SubobjectExpr>(
4532 Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
4533}
4534
Richard Smithc20d1442018-08-20 20:14:49 +00004535// <expression> ::= <unary operator-name> <expression>
4536// ::= <binary operator-name> <expression> <expression>
4537// ::= <ternary operator-name> <expression> <expression> <expression>
4538// ::= cl <expression>+ E # call
4539// ::= cv <type> <expression> # conversion with one argument
4540// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4541// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4542// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4543// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4544// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4545// ::= [gs] dl <expression> # delete expression
4546// ::= [gs] da <expression> # delete[] expression
4547// ::= pp_ <expression> # prefix ++
4548// ::= mm_ <expression> # prefix --
4549// ::= ti <type> # typeid (type)
4550// ::= te <expression> # typeid (expression)
4551// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4552// ::= sc <type> <expression> # static_cast<type> (expression)
4553// ::= cc <type> <expression> # const_cast<type> (expression)
4554// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4555// ::= st <type> # sizeof (a type)
4556// ::= sz <expression> # sizeof (an expression)
4557// ::= at <type> # alignof (a type)
4558// ::= az <expression> # alignof (an expression)
4559// ::= nx <expression> # noexcept (expression)
4560// ::= <template-param>
4561// ::= <function-param>
4562// ::= dt <expression> <unresolved-name> # expr.name
4563// ::= pt <expression> <unresolved-name> # expr->name
4564// ::= ds <expression> <expression> # expr.*expr
4565// ::= sZ <template-param> # size of a parameter pack
4566// ::= sZ <function-param> # size of a function parameter pack
4567// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4568// ::= sp <expression> # pack expansion
4569// ::= tw <expression> # throw expression
4570// ::= tr # throw with no operand (rethrow)
4571// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4572// # freestanding dependent name (e.g., T::x),
4573// # objectless nonstatic member reference
4574// ::= fL <binary-operator-name> <expression> <expression>
4575// ::= fR <binary-operator-name> <expression> <expression>
4576// ::= fl <binary-operator-name> <expression>
4577// ::= fr <binary-operator-name> <expression>
4578// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004579template <typename Derived, typename Alloc>
4580Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004581 bool Global = consumeIf("gs");
4582 if (numLeft() < 2)
4583 return nullptr;
4584
4585 switch (*First) {
4586 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004587 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004588 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004589 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004590 case 'f': {
4591 // Disambiguate a fold expression from a <function-param>.
4592 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004593 return getDerived().parseFunctionParam();
4594 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004595 }
4596 case 'a':
4597 switch (First[1]) {
4598 case 'a':
4599 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004600 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004601 case 'd':
4602 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004603 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004604 case 'n':
4605 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004606 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004607 case 'N':
4608 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004609 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004610 case 'S':
4611 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004612 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004613 case 't': {
4614 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004615 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004616 if (Ty == nullptr)
4617 return nullptr;
4618 return make<EnclosingExpr>("alignof (", Ty, ")");
4619 }
4620 case 'z': {
4621 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004622 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004623 if (Ty == nullptr)
4624 return nullptr;
4625 return make<EnclosingExpr>("alignof (", Ty, ")");
4626 }
4627 }
4628 return nullptr;
4629 case 'c':
4630 switch (First[1]) {
4631 // cc <type> <expression> # const_cast<type>(expression)
4632 case 'c': {
4633 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004634 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004635 if (Ty == nullptr)
4636 return Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004637 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004638 if (Ex == nullptr)
4639 return Ex;
4640 return make<CastExpr>("const_cast", Ty, Ex);
4641 }
4642 // cl <expression>+ E # call
4643 case 'l': {
4644 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004645 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004646 if (Callee == nullptr)
4647 return Callee;
4648 size_t ExprsBegin = Names.size();
4649 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004650 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004651 if (E == nullptr)
4652 return E;
4653 Names.push_back(E);
4654 }
4655 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4656 }
4657 case 'm':
4658 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004659 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004660 case 'o':
4661 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004662 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004663 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004664 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004665 }
4666 return nullptr;
4667 case 'd':
4668 switch (First[1]) {
4669 case 'a': {
4670 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004671 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004672 if (Ex == nullptr)
4673 return Ex;
4674 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4675 }
4676 case 'c': {
4677 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004678 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004679 if (T == nullptr)
4680 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004681 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004682 if (Ex == nullptr)
4683 return Ex;
4684 return make<CastExpr>("dynamic_cast", T, Ex);
4685 }
4686 case 'e':
4687 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004688 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004689 case 'l': {
4690 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004691 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004692 if (E == nullptr)
4693 return E;
4694 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4695 }
4696 case 'n':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08004697 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004698 case 's': {
4699 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004700 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004701 if (LHS == nullptr)
4702 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004703 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004704 if (RHS == nullptr)
4705 return nullptr;
4706 return make<MemberExpr>(LHS, ".*", RHS);
4707 }
4708 case 't': {
4709 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004710 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004711 if (LHS == nullptr)
4712 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004713 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004714 if (RHS == nullptr)
4715 return nullptr;
4716 return make<MemberExpr>(LHS, ".", RHS);
4717 }
4718 case 'v':
4719 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004720 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004721 case 'V':
4722 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004723 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004724 }
4725 return nullptr;
4726 case 'e':
4727 switch (First[1]) {
4728 case 'o':
4729 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004730 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004731 case 'O':
4732 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004733 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004734 case 'q':
4735 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004736 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004737 }
4738 return nullptr;
4739 case 'g':
4740 switch (First[1]) {
4741 case 'e':
4742 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004743 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004744 case 't':
4745 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004746 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004747 }
4748 return nullptr;
4749 case 'i':
4750 switch (First[1]) {
4751 case 'x': {
4752 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004753 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004754 if (Base == nullptr)
4755 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004756 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004757 if (Index == nullptr)
4758 return Index;
4759 return make<ArraySubscriptExpr>(Base, Index);
4760 }
4761 case 'l': {
4762 First += 2;
4763 size_t InitsBegin = Names.size();
4764 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004765 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004766 if (E == nullptr)
4767 return nullptr;
4768 Names.push_back(E);
4769 }
4770 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4771 }
4772 }
4773 return nullptr;
4774 case 'l':
4775 switch (First[1]) {
4776 case 'e':
4777 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004778 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004779 case 's':
4780 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004781 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004782 case 'S':
4783 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004784 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004785 case 't':
4786 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004787 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004788 }
4789 return nullptr;
4790 case 'm':
4791 switch (First[1]) {
Richard Smith1865d2f2020-10-22 19:29:36 -07004792 case 'c':
4793 First += 2;
4794 return parsePointerToMemberConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004795 case 'i':
4796 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004797 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004798 case 'I':
4799 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004800 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004801 case 'l':
4802 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004803 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004804 case 'L':
4805 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004806 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004807 case 'm':
4808 First += 2;
4809 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004810 return getDerived().parsePrefixExpr("--");
4811 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004812 if (Ex == nullptr)
4813 return nullptr;
4814 return make<PostfixExpr>(Ex, "--");
4815 }
4816 return nullptr;
4817 case 'n':
4818 switch (First[1]) {
4819 case 'a':
4820 case 'w':
Pavel Labathba825192018-10-16 14:29:14 +00004821 return getDerived().parseNewExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004822 case 'e':
4823 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004824 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004825 case 'g':
4826 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004827 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004828 case 't':
4829 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004830 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004831 case 'x':
4832 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004833 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004834 if (Ex == nullptr)
4835 return Ex;
4836 return make<EnclosingExpr>("noexcept (", Ex, ")");
4837 }
4838 return nullptr;
4839 case 'o':
4840 switch (First[1]) {
4841 case 'n':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08004842 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004843 case 'o':
4844 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004845 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004846 case 'r':
4847 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004848 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004849 case 'R':
4850 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004851 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004852 }
4853 return nullptr;
4854 case 'p':
4855 switch (First[1]) {
Nathan Sidwellc6483042022-01-28 09:27:28 -08004856 case 'm': {
Richard Smithc20d1442018-08-20 20:14:49 +00004857 First += 2;
Nathan Sidwellc6483042022-01-28 09:27:28 -08004858 Node *LHS = getDerived().parseExpr();
4859 if (LHS == nullptr)
4860 return LHS;
4861 Node *RHS = getDerived().parseExpr();
4862 if (RHS == nullptr)
4863 return nullptr;
4864 return make<MemberExpr>(LHS, "->*", RHS);
4865 }
Richard Smithc20d1442018-08-20 20:14:49 +00004866 case 'l':
4867 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004868 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004869 case 'L':
4870 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004871 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004872 case 'p': {
4873 First += 2;
4874 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004875 return getDerived().parsePrefixExpr("++");
4876 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004877 if (Ex == nullptr)
4878 return Ex;
4879 return make<PostfixExpr>(Ex, "++");
4880 }
4881 case 's':
4882 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004883 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004884 case 't': {
4885 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004886 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004887 if (L == nullptr)
4888 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004889 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004890 if (R == nullptr)
4891 return nullptr;
4892 return make<MemberExpr>(L, "->", R);
4893 }
4894 }
4895 return nullptr;
4896 case 'q':
4897 if (First[1] == 'u') {
4898 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004899 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004900 if (Cond == nullptr)
4901 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004902 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004903 if (LHS == nullptr)
4904 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004905 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004906 if (RHS == nullptr)
4907 return nullptr;
4908 return make<ConditionalExpr>(Cond, LHS, RHS);
4909 }
4910 return nullptr;
4911 case 'r':
4912 switch (First[1]) {
4913 case 'c': {
4914 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004915 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004916 if (T == nullptr)
4917 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004918 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004919 if (Ex == nullptr)
4920 return Ex;
4921 return make<CastExpr>("reinterpret_cast", T, Ex);
4922 }
4923 case 'm':
4924 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004925 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004926 case 'M':
4927 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004928 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004929 case 's':
4930 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004931 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004932 case 'S':
4933 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004934 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004935 }
4936 return nullptr;
4937 case 's':
4938 switch (First[1]) {
4939 case 'c': {
4940 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004941 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004942 if (T == nullptr)
4943 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004944 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004945 if (Ex == nullptr)
4946 return Ex;
4947 return make<CastExpr>("static_cast", T, Ex);
4948 }
Richard Smith1865d2f2020-10-22 19:29:36 -07004949 case 'o':
4950 First += 2;
4951 return parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004952 case 'p': {
4953 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004954 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004955 if (Child == nullptr)
4956 return nullptr;
4957 return make<ParameterPackExpansion>(Child);
4958 }
4959 case 'r':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08004960 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004961 case 't': {
4962 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004963 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004964 if (Ty == nullptr)
4965 return Ty;
4966 return make<EnclosingExpr>("sizeof (", Ty, ")");
4967 }
4968 case 'z': {
4969 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004970 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004971 if (Ex == nullptr)
4972 return Ex;
4973 return make<EnclosingExpr>("sizeof (", Ex, ")");
4974 }
4975 case 'Z':
4976 First += 2;
4977 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004978 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004979 if (R == nullptr)
4980 return nullptr;
4981 return make<SizeofParamPackExpr>(R);
4982 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004983 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004984 if (FP == nullptr)
4985 return nullptr;
4986 return make<EnclosingExpr>("sizeof... (", FP, ")");
4987 }
4988 return nullptr;
4989 case 'P': {
4990 First += 2;
4991 size_t ArgsBegin = Names.size();
4992 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004993 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004994 if (Arg == nullptr)
4995 return nullptr;
4996 Names.push_back(Arg);
4997 }
Richard Smithb485b352018-08-24 23:30:26 +00004998 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4999 if (!Pack)
5000 return nullptr;
5001 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00005002 }
5003 }
5004 return nullptr;
5005 case 't':
5006 switch (First[1]) {
5007 case 'e': {
5008 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005009 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005010 if (Ex == nullptr)
5011 return Ex;
5012 return make<EnclosingExpr>("typeid (", Ex, ")");
5013 }
5014 case 'i': {
5015 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005016 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005017 if (Ty == nullptr)
5018 return Ty;
5019 return make<EnclosingExpr>("typeid (", Ty, ")");
5020 }
5021 case 'l': {
5022 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005023 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005024 if (Ty == nullptr)
5025 return nullptr;
5026 size_t InitsBegin = Names.size();
5027 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005028 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005029 if (E == nullptr)
5030 return nullptr;
5031 Names.push_back(E);
5032 }
5033 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
5034 }
5035 case 'r':
5036 First += 2;
5037 return make<NameType>("throw");
5038 case 'w': {
5039 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005040 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005041 if (Ex == nullptr)
5042 return nullptr;
5043 return make<ThrowExpr>(Ex);
5044 }
5045 }
5046 return nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05005047 case 'u': {
5048 ++First;
5049 Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
5050 if (!Name)
5051 return nullptr;
5052 // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
5053 // standard encoding expects a <template-arg>, and would be otherwise be
5054 // interpreted as <type> node 'short' or 'ellipsis'. However, neither
5055 // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
5056 // actual conflict here.
5057 if (Name->getBaseName() == "__uuidof") {
5058 if (numLeft() < 2)
5059 return nullptr;
5060 if (*First == 't') {
5061 ++First;
5062 Node *Ty = getDerived().parseType();
5063 if (!Ty)
5064 return nullptr;
5065 return make<CallExpr>(Name, makeNodeArray(&Ty, &Ty + 1));
5066 }
5067 if (*First == 'z') {
5068 ++First;
5069 Node *Ex = getDerived().parseExpr();
5070 if (!Ex)
5071 return nullptr;
5072 return make<CallExpr>(Name, makeNodeArray(&Ex, &Ex + 1));
5073 }
5074 }
5075 size_t ExprsBegin = Names.size();
5076 while (!consumeIf('E')) {
5077 Node *E = getDerived().parseTemplateArg();
5078 if (E == nullptr)
5079 return E;
5080 Names.push_back(E);
5081 }
5082 return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin));
5083 }
Richard Smithc20d1442018-08-20 20:14:49 +00005084 case '1':
5085 case '2':
5086 case '3':
5087 case '4':
5088 case '5':
5089 case '6':
5090 case '7':
5091 case '8':
5092 case '9':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08005093 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00005094 }
5095 return nullptr;
5096}
5097
5098// <call-offset> ::= h <nv-offset> _
5099// ::= v <v-offset> _
5100//
5101// <nv-offset> ::= <offset number>
5102// # non-virtual base override
5103//
5104// <v-offset> ::= <offset number> _ <virtual offset number>
5105// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00005106template <typename Alloc, typename Derived>
5107bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00005108 // Just scan through the call offset, we never add this information into the
5109 // output.
5110 if (consumeIf('h'))
5111 return parseNumber(true).empty() || !consumeIf('_');
5112 if (consumeIf('v'))
5113 return parseNumber(true).empty() || !consumeIf('_') ||
5114 parseNumber(true).empty() || !consumeIf('_');
5115 return true;
5116}
5117
5118// <special-name> ::= TV <type> # virtual table
5119// ::= TT <type> # VTT structure (construction vtable index)
5120// ::= TI <type> # typeinfo structure
5121// ::= TS <type> # typeinfo name (null-terminated byte string)
5122// ::= Tc <call-offset> <call-offset> <base encoding>
5123// # base is the nominal target function of thunk
5124// # first call-offset is 'this' adjustment
5125// # second call-offset is result adjustment
5126// ::= T <call-offset> <base encoding>
5127// # base is the nominal target function of thunk
5128// ::= GV <object name> # Guard variable for one-time initialization
5129// # No <type>
5130// ::= TW <object name> # Thread-local wrapper
5131// ::= TH <object name> # Thread-local initialization
5132// ::= GR <object name> _ # First temporary
5133// ::= GR <object name> <seq-id> _ # Subsequent temporaries
5134// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
5135// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00005136template <typename Derived, typename Alloc>
5137Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00005138 switch (look()) {
5139 case 'T':
5140 switch (look(1)) {
Richard Smith1865d2f2020-10-22 19:29:36 -07005141 // TA <template-arg> # template parameter object
5142 //
5143 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
5144 case 'A': {
5145 First += 2;
5146 Node *Arg = getDerived().parseTemplateArg();
5147 if (Arg == nullptr)
5148 return nullptr;
5149 return make<SpecialName>("template parameter object for ", Arg);
5150 }
Richard Smithc20d1442018-08-20 20:14:49 +00005151 // TV <type> # virtual table
5152 case 'V': {
5153 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005154 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005155 if (Ty == nullptr)
5156 return nullptr;
5157 return make<SpecialName>("vtable for ", Ty);
5158 }
5159 // TT <type> # VTT structure (construction vtable index)
5160 case 'T': {
5161 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005162 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005163 if (Ty == nullptr)
5164 return nullptr;
5165 return make<SpecialName>("VTT for ", Ty);
5166 }
5167 // TI <type> # typeinfo structure
5168 case 'I': {
5169 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005170 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005171 if (Ty == nullptr)
5172 return nullptr;
5173 return make<SpecialName>("typeinfo for ", Ty);
5174 }
5175 // TS <type> # typeinfo name (null-terminated byte string)
5176 case 'S': {
5177 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005178 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005179 if (Ty == nullptr)
5180 return nullptr;
5181 return make<SpecialName>("typeinfo name for ", Ty);
5182 }
5183 // Tc <call-offset> <call-offset> <base encoding>
5184 case 'c': {
5185 First += 2;
5186 if (parseCallOffset() || parseCallOffset())
5187 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005188 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005189 if (Encoding == nullptr)
5190 return nullptr;
5191 return make<SpecialName>("covariant return thunk to ", Encoding);
5192 }
5193 // extension ::= TC <first type> <number> _ <second type>
5194 // # construction vtable for second-in-first
5195 case 'C': {
5196 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005197 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005198 if (FirstType == nullptr)
5199 return nullptr;
5200 if (parseNumber(true).empty() || !consumeIf('_'))
5201 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005202 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005203 if (SecondType == nullptr)
5204 return nullptr;
5205 return make<CtorVtableSpecialName>(SecondType, FirstType);
5206 }
5207 // TW <object name> # Thread-local wrapper
5208 case 'W': {
5209 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005210 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005211 if (Name == nullptr)
5212 return nullptr;
5213 return make<SpecialName>("thread-local wrapper routine for ", Name);
5214 }
5215 // TH <object name> # Thread-local initialization
5216 case 'H': {
5217 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005218 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005219 if (Name == nullptr)
5220 return nullptr;
5221 return make<SpecialName>("thread-local initialization routine for ", Name);
5222 }
5223 // T <call-offset> <base encoding>
5224 default: {
5225 ++First;
5226 bool IsVirt = look() == 'v';
5227 if (parseCallOffset())
5228 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005229 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005230 if (BaseEncoding == nullptr)
5231 return nullptr;
5232 if (IsVirt)
5233 return make<SpecialName>("virtual thunk to ", BaseEncoding);
5234 else
5235 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
5236 }
5237 }
5238 case 'G':
5239 switch (look(1)) {
5240 // GV <object name> # Guard variable for one-time initialization
5241 case 'V': {
5242 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005243 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005244 if (Name == nullptr)
5245 return nullptr;
5246 return make<SpecialName>("guard variable for ", Name);
5247 }
5248 // GR <object name> # reference temporary for object
5249 // GR <object name> _ # First temporary
5250 // GR <object name> <seq-id> _ # Subsequent temporaries
5251 case 'R': {
5252 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005253 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005254 if (Name == nullptr)
5255 return nullptr;
5256 size_t Count;
5257 bool ParsedSeqId = !parseSeqId(&Count);
5258 if (!consumeIf('_') && ParsedSeqId)
5259 return nullptr;
5260 return make<SpecialName>("reference temporary for ", Name);
5261 }
5262 }
5263 }
5264 return nullptr;
5265}
5266
5267// <encoding> ::= <function name> <bare-function-type>
5268// ::= <data name>
5269// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00005270template <typename Derived, typename Alloc>
5271Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithfac39712020-07-09 21:08:39 -07005272 // The template parameters of an encoding are unrelated to those of the
5273 // enclosing context.
5274 class SaveTemplateParams {
5275 AbstractManglingParser *Parser;
5276 decltype(TemplateParams) OldParams;
Justin Lebar2c536232021-06-09 16:57:22 -07005277 decltype(OuterTemplateParams) OldOuterParams;
Richard Smithfac39712020-07-09 21:08:39 -07005278
5279 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04005280 SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
Richard Smithfac39712020-07-09 21:08:39 -07005281 OldParams = std::move(Parser->TemplateParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005282 OldOuterParams = std::move(Parser->OuterTemplateParams);
Richard Smithfac39712020-07-09 21:08:39 -07005283 Parser->TemplateParams.clear();
Justin Lebar2c536232021-06-09 16:57:22 -07005284 Parser->OuterTemplateParams.clear();
Richard Smithfac39712020-07-09 21:08:39 -07005285 }
5286 ~SaveTemplateParams() {
5287 Parser->TemplateParams = std::move(OldParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005288 Parser->OuterTemplateParams = std::move(OldOuterParams);
Richard Smithfac39712020-07-09 21:08:39 -07005289 }
5290 } SaveTemplateParams(this);
Richard Smithfd434322020-07-09 20:36:04 -07005291
Richard Smithc20d1442018-08-20 20:14:49 +00005292 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00005293 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00005294
5295 auto IsEndOfEncoding = [&] {
5296 // The set of chars that can potentially follow an <encoding> (none of which
5297 // can start a <type>). Enumerating these allows us to avoid speculative
5298 // parsing.
5299 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5300 };
5301
5302 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00005303 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00005304 if (Name == nullptr)
5305 return nullptr;
5306
5307 if (resolveForwardTemplateRefs(NameInfo))
5308 return nullptr;
5309
5310 if (IsEndOfEncoding())
5311 return Name;
5312
5313 Node *Attrs = nullptr;
5314 if (consumeIf("Ua9enable_ifI")) {
5315 size_t BeforeArgs = Names.size();
5316 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005317 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005318 if (Arg == nullptr)
5319 return nullptr;
5320 Names.push_back(Arg);
5321 }
5322 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00005323 if (!Attrs)
5324 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005325 }
5326
5327 Node *ReturnType = nullptr;
5328 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00005329 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005330 if (ReturnType == nullptr)
5331 return nullptr;
5332 }
5333
5334 if (consumeIf('v'))
5335 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5336 Attrs, NameInfo.CVQualifiers,
5337 NameInfo.ReferenceQualifier);
5338
5339 size_t ParamsBegin = Names.size();
5340 do {
Pavel Labathba825192018-10-16 14:29:14 +00005341 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005342 if (Ty == nullptr)
5343 return nullptr;
5344 Names.push_back(Ty);
5345 } while (!IsEndOfEncoding());
5346
5347 return make<FunctionEncoding>(ReturnType, Name,
5348 popTrailingNodeArray(ParamsBegin),
5349 Attrs, NameInfo.CVQualifiers,
5350 NameInfo.ReferenceQualifier);
5351}
5352
5353template <class Float>
5354struct FloatData;
5355
5356template <>
5357struct FloatData<float>
5358{
5359 static const size_t mangled_size = 8;
5360 static const size_t max_demangled_size = 24;
5361 static constexpr const char* spec = "%af";
5362};
5363
5364template <>
5365struct FloatData<double>
5366{
5367 static const size_t mangled_size = 16;
5368 static const size_t max_demangled_size = 32;
5369 static constexpr const char* spec = "%a";
5370};
5371
5372template <>
5373struct FloatData<long double>
5374{
5375#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5376 defined(__wasm__)
5377 static const size_t mangled_size = 32;
5378#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5379 static const size_t mangled_size = 16;
5380#else
5381 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5382#endif
Elliott Hughes5a360ea2020-04-10 17:42:00 -07005383 // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
5384 // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
5385 // Negatives are one character longer than positives.
5386 // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
5387 // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
5388 static const size_t max_demangled_size = 42;
Richard Smithc20d1442018-08-20 20:14:49 +00005389 static constexpr const char *spec = "%LaL";
5390};
5391
Pavel Labathba825192018-10-16 14:29:14 +00005392template <typename Alloc, typename Derived>
5393template <class Float>
5394Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005395 const size_t N = FloatData<Float>::mangled_size;
5396 if (numLeft() <= N)
5397 return nullptr;
5398 StringView Data(First, First + N);
5399 for (char C : Data)
5400 if (!std::isxdigit(C))
5401 return nullptr;
5402 First += N;
5403 if (!consumeIf('E'))
5404 return nullptr;
5405 return make<FloatLiteralImpl<Float>>(Data);
5406}
5407
5408// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005409template <typename Alloc, typename Derived>
5410bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005411 if (!(look() >= '0' && look() <= '9') &&
5412 !(look() >= 'A' && look() <= 'Z'))
5413 return true;
5414
5415 size_t Id = 0;
5416 while (true) {
5417 if (look() >= '0' && look() <= '9') {
5418 Id *= 36;
5419 Id += static_cast<size_t>(look() - '0');
5420 } else if (look() >= 'A' && look() <= 'Z') {
5421 Id *= 36;
5422 Id += static_cast<size_t>(look() - 'A') + 10;
5423 } else {
5424 *Out = Id;
5425 return false;
5426 }
5427 ++First;
5428 }
5429}
5430
5431// <substitution> ::= S <seq-id> _
5432// ::= S_
5433// <substitution> ::= Sa # ::std::allocator
5434// <substitution> ::= Sb # ::std::basic_string
5435// <substitution> ::= Ss # ::std::basic_string < char,
5436// ::std::char_traits<char>,
5437// ::std::allocator<char> >
5438// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5439// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5440// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08005441// The St case is handled specially in parseNestedName.
Pavel Labathba825192018-10-16 14:29:14 +00005442template <typename Derived, typename Alloc>
5443Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005444 if (!consumeIf('S'))
5445 return nullptr;
5446
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08005447 if (look() >= 'a' && look() <= 'z') {
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005448 SpecialSubKind Kind;
Richard Smithc20d1442018-08-20 20:14:49 +00005449 switch (look()) {
5450 case 'a':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005451 Kind = SpecialSubKind::allocator;
Richard Smithc20d1442018-08-20 20:14:49 +00005452 break;
5453 case 'b':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005454 Kind = SpecialSubKind::basic_string;
Richard Smithc20d1442018-08-20 20:14:49 +00005455 break;
5456 case 'd':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005457 Kind = SpecialSubKind::iostream;
5458 break;
5459 case 'i':
5460 Kind = SpecialSubKind::istream;
5461 break;
5462 case 'o':
5463 Kind = SpecialSubKind::ostream;
5464 break;
5465 case 's':
5466 Kind = SpecialSubKind::string;
Richard Smithc20d1442018-08-20 20:14:49 +00005467 break;
5468 default:
5469 return nullptr;
5470 }
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005471 ++First;
5472 auto *SpecialSub = make<SpecialSubstitution>(Kind);
Richard Smithb485b352018-08-24 23:30:26 +00005473 if (!SpecialSub)
5474 return nullptr;
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005475
Richard Smithc20d1442018-08-20 20:14:49 +00005476 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5477 // has ABI tags, the tags are appended to the substitution; the result is a
5478 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005479 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005480 if (WithTags != SpecialSub) {
5481 Subs.push_back(WithTags);
5482 SpecialSub = WithTags;
5483 }
5484 return SpecialSub;
5485 }
5486
5487 // ::= S_
5488 if (consumeIf('_')) {
5489 if (Subs.empty())
5490 return nullptr;
5491 return Subs[0];
5492 }
5493
5494 // ::= S <seq-id> _
5495 size_t Index = 0;
5496 if (parseSeqId(&Index))
5497 return nullptr;
5498 ++Index;
5499 if (!consumeIf('_') || Index >= Subs.size())
5500 return nullptr;
5501 return Subs[Index];
5502}
5503
5504// <template-param> ::= T_ # first template parameter
5505// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005506// ::= TL <level-1> __
5507// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005508template <typename Derived, typename Alloc>
5509Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005510 if (!consumeIf('T'))
5511 return nullptr;
5512
Richard Smithdf1c14c2019-09-06 23:53:21 +00005513 size_t Level = 0;
5514 if (consumeIf('L')) {
5515 if (parsePositiveInteger(&Level))
5516 return nullptr;
5517 ++Level;
5518 if (!consumeIf('_'))
5519 return nullptr;
5520 }
5521
Richard Smithc20d1442018-08-20 20:14:49 +00005522 size_t Index = 0;
5523 if (!consumeIf('_')) {
5524 if (parsePositiveInteger(&Index))
5525 return nullptr;
5526 ++Index;
5527 if (!consumeIf('_'))
5528 return nullptr;
5529 }
5530
Richard Smithc20d1442018-08-20 20:14:49 +00005531 // If we're in a context where this <template-param> refers to a
5532 // <template-arg> further ahead in the mangled name (currently just conversion
5533 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005534 // This can only happen at the outermost level.
5535 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005536 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5537 if (!ForwardRef)
5538 return nullptr;
5539 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5540 ForwardTemplateRefs.push_back(
5541 static_cast<ForwardTemplateReference *>(ForwardRef));
5542 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005543 }
5544
Richard Smithdf1c14c2019-09-06 23:53:21 +00005545 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5546 Index >= TemplateParams[Level]->size()) {
5547 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5548 // list are mangled as the corresponding artificial template type parameter.
5549 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5550 // This will be popped by the ScopedTemplateParamList in
5551 // parseUnnamedTypeName.
5552 if (Level == TemplateParams.size())
5553 TemplateParams.push_back(nullptr);
5554 return make<NameType>("auto");
5555 }
5556
Richard Smithc20d1442018-08-20 20:14:49 +00005557 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005558 }
5559
5560 return (*TemplateParams[Level])[Index];
5561}
5562
5563// <template-param-decl> ::= Ty # type parameter
5564// ::= Tn <type> # non-type parameter
5565// ::= Tt <template-param-decl>* E # template parameter
5566// ::= Tp <template-param-decl> # parameter pack
5567template <typename Derived, typename Alloc>
5568Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5569 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5570 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5571 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5572 if (N) TemplateParams.back()->push_back(N);
5573 return N;
5574 };
5575
5576 if (consumeIf("Ty")) {
5577 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5578 if (!Name)
5579 return nullptr;
5580 return make<TypeTemplateParamDecl>(Name);
5581 }
5582
5583 if (consumeIf("Tn")) {
5584 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5585 if (!Name)
5586 return nullptr;
5587 Node *Type = parseType();
5588 if (!Type)
5589 return nullptr;
5590 return make<NonTypeTemplateParamDecl>(Name, Type);
5591 }
5592
5593 if (consumeIf("Tt")) {
5594 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5595 if (!Name)
5596 return nullptr;
5597 size_t ParamsBegin = Names.size();
5598 ScopedTemplateParamList TemplateTemplateParamParams(this);
5599 while (!consumeIf("E")) {
5600 Node *P = parseTemplateParamDecl();
5601 if (!P)
5602 return nullptr;
5603 Names.push_back(P);
5604 }
5605 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5606 return make<TemplateTemplateParamDecl>(Name, Params);
5607 }
5608
5609 if (consumeIf("Tp")) {
5610 Node *P = parseTemplateParamDecl();
5611 if (!P)
5612 return nullptr;
5613 return make<TemplateParamPackDecl>(P);
5614 }
5615
5616 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005617}
5618
5619// <template-arg> ::= <type> # type or template
5620// ::= X <expression> E # expression
5621// ::= <expr-primary> # simple expressions
5622// ::= J <template-arg>* E # argument pack
5623// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005624template <typename Derived, typename Alloc>
5625Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005626 switch (look()) {
5627 case 'X': {
5628 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005629 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005630 if (Arg == nullptr || !consumeIf('E'))
5631 return nullptr;
5632 return Arg;
5633 }
5634 case 'J': {
5635 ++First;
5636 size_t ArgsBegin = Names.size();
5637 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005638 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005639 if (Arg == nullptr)
5640 return nullptr;
5641 Names.push_back(Arg);
5642 }
5643 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5644 return make<TemplateArgumentPack>(Args);
5645 }
5646 case 'L': {
5647 // ::= LZ <encoding> E # extension
5648 if (look(1) == 'Z') {
5649 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005650 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005651 if (Arg == nullptr || !consumeIf('E'))
5652 return nullptr;
5653 return Arg;
5654 }
5655 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005656 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005657 }
5658 default:
Pavel Labathba825192018-10-16 14:29:14 +00005659 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005660 }
5661}
5662
5663// <template-args> ::= I <template-arg>* E
5664// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005665template <typename Derived, typename Alloc>
5666Node *
5667AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005668 if (!consumeIf('I'))
5669 return nullptr;
5670
5671 // <template-params> refer to the innermost <template-args>. Clear out any
5672 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005673 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005674 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005675 TemplateParams.push_back(&OuterTemplateParams);
5676 OuterTemplateParams.clear();
5677 }
Richard Smithc20d1442018-08-20 20:14:49 +00005678
5679 size_t ArgsBegin = Names.size();
5680 while (!consumeIf('E')) {
5681 if (TagTemplates) {
5682 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005683 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005684 TemplateParams = std::move(OldParams);
5685 if (Arg == nullptr)
5686 return nullptr;
5687 Names.push_back(Arg);
5688 Node *TableEntry = Arg;
5689 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5690 TableEntry = make<ParameterPack>(
5691 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005692 if (!TableEntry)
5693 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005694 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005695 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005696 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005697 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005698 if (Arg == nullptr)
5699 return nullptr;
5700 Names.push_back(Arg);
5701 }
5702 }
5703 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5704}
5705
5706// <mangled-name> ::= _Z <encoding>
5707// ::= <type>
5708// extension ::= ___Z <encoding> _block_invoke
5709// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5710// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005711template <typename Derived, typename Alloc>
5712Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005713 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005714 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005715 if (Encoding == nullptr)
5716 return nullptr;
5717 if (look() == '.') {
5718 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5719 First = Last;
5720 }
5721 if (numLeft() != 0)
5722 return nullptr;
5723 return Encoding;
5724 }
5725
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005726 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005727 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005728 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5729 return nullptr;
5730 bool RequireNumber = consumeIf('_');
5731 if (parseNumber().empty() && RequireNumber)
5732 return nullptr;
5733 if (look() == '.')
5734 First = Last;
5735 if (numLeft() != 0)
5736 return nullptr;
5737 return make<SpecialName>("invocation function for block in ", Encoding);
5738 }
5739
Pavel Labathba825192018-10-16 14:29:14 +00005740 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005741 if (numLeft() != 0)
5742 return nullptr;
5743 return Ty;
5744}
5745
Pavel Labathba825192018-10-16 14:29:14 +00005746template <typename Alloc>
5747struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5748 using AbstractManglingParser<ManglingParser<Alloc>,
5749 Alloc>::AbstractManglingParser;
5750};
5751
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005752DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005753
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005754#endif // DEMANGLE_ITANIUMDEMANGLE_H