blob: 85e1511346c0bdbe65eb24848d37ddbf89dd2c16 [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//
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00009// Generic itanium demangler library. This file has two byte-per-byte identical
10// copies in the source tree, one in libcxxabi, and the other in llvm.
Richard Smithc20d1442018-08-20 20:14:49 +000011//
12//===----------------------------------------------------------------------===//
13
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +000014#ifndef DEMANGLE_ITANIUMDEMANGLE_H
15#define DEMANGLE_ITANIUMDEMANGLE_H
Richard Smithc20d1442018-08-20 20:14:49 +000016
17// FIXME: (possibly) incomplete list of features that clang mangles that this
18// file does not yet support:
19// - C++ modules TS
20
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +000021#include "DemangleConfig.h"
Richard Smithc20d1442018-08-20 20:14:49 +000022#include "StringView.h"
23#include "Utility.h"
Richard Smithc20d1442018-08-20 20:14:49 +000024#include <cassert>
25#include <cctype>
26#include <cstdio>
27#include <cstdlib>
28#include <cstring>
29#include <numeric>
30#include <utility>
31
32#define FOR_EACH_NODE_KIND(X) \
33 X(NodeArrayNode) \
34 X(DotSuffix) \
35 X(VendorExtQualType) \
36 X(QualType) \
37 X(ConversionOperatorType) \
38 X(PostfixQualifiedType) \
39 X(ElaboratedTypeSpefType) \
40 X(NameType) \
41 X(AbiTagAttr) \
42 X(EnableIfAttr) \
43 X(ObjCProtoName) \
44 X(PointerType) \
45 X(ReferenceType) \
46 X(PointerToMemberType) \
47 X(ArrayType) \
48 X(FunctionType) \
49 X(NoexceptSpec) \
50 X(DynamicExceptionSpec) \
51 X(FunctionEncoding) \
52 X(LiteralOperator) \
53 X(SpecialName) \
54 X(CtorVtableSpecialName) \
55 X(QualifiedName) \
56 X(NestedName) \
57 X(LocalName) \
58 X(VectorType) \
59 X(PixelVectorType) \
Pengfei Wang50e90b82021-09-23 11:02:25 +080060 X(BinaryFPType) \
Richard Smithdf1c14c2019-09-06 23:53:21 +000061 X(SyntheticTemplateParamName) \
62 X(TypeTemplateParamDecl) \
63 X(NonTypeTemplateParamDecl) \
64 X(TemplateTemplateParamDecl) \
65 X(TemplateParamPackDecl) \
Richard Smithc20d1442018-08-20 20:14:49 +000066 X(ParameterPack) \
67 X(TemplateArgumentPack) \
68 X(ParameterPackExpansion) \
69 X(TemplateArgs) \
70 X(ForwardTemplateReference) \
71 X(NameWithTemplateArgs) \
72 X(GlobalQualifiedName) \
73 X(StdQualifiedName) \
74 X(ExpandedSpecialSubstitution) \
75 X(SpecialSubstitution) \
76 X(CtorDtorName) \
77 X(DtorName) \
78 X(UnnamedTypeName) \
79 X(ClosureTypeName) \
80 X(StructuredBindingName) \
81 X(BinaryExpr) \
82 X(ArraySubscriptExpr) \
83 X(PostfixExpr) \
84 X(ConditionalExpr) \
85 X(MemberExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070086 X(SubobjectExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000087 X(EnclosingExpr) \
88 X(CastExpr) \
89 X(SizeofParamPackExpr) \
90 X(CallExpr) \
91 X(NewExpr) \
92 X(DeleteExpr) \
93 X(PrefixExpr) \
94 X(FunctionParam) \
95 X(ConversionExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070096 X(PointerToMemberConversionExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000097 X(InitListExpr) \
98 X(FoldExpr) \
99 X(ThrowExpr) \
100 X(BoolExpr) \
Richard Smithdf1c14c2019-09-06 23:53:21 +0000101 X(StringLiteral) \
102 X(LambdaExpr) \
Erik Pilkington0a170f12020-05-13 14:13:37 -0400103 X(EnumLiteral) \
Richard Smithc20d1442018-08-20 20:14:49 +0000104 X(IntegerLiteral) \
105 X(FloatLiteral) \
106 X(DoubleLiteral) \
107 X(LongDoubleLiteral) \
108 X(BracedExpr) \
109 X(BracedRangeExpr)
110
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000111DEMANGLE_NAMESPACE_BEGIN
112
Mikhail Borisov8452f062021-08-17 18:06:53 -0400113template <class T, size_t N> class PODSmallVector {
114 static_assert(std::is_pod<T>::value,
115 "T is required to be a plain old data type");
116
117 T *First = nullptr;
118 T *Last = nullptr;
119 T *Cap = nullptr;
120 T Inline[N] = {0};
121
122 bool isInline() const { return First == Inline; }
123
124 void clearInline() {
125 First = Inline;
126 Last = Inline;
127 Cap = Inline + N;
128 }
129
130 void reserve(size_t NewCap) {
131 size_t S = size();
132 if (isInline()) {
133 auto *Tmp = static_cast<T *>(std::malloc(NewCap * sizeof(T)));
134 if (Tmp == nullptr)
135 std::terminate();
136 std::copy(First, Last, Tmp);
137 First = Tmp;
138 } else {
139 First = static_cast<T *>(std::realloc(First, NewCap * sizeof(T)));
140 if (First == nullptr)
141 std::terminate();
142 }
143 Last = First + S;
144 Cap = First + NewCap;
145 }
146
147public:
148 PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
149
150 PODSmallVector(const PODSmallVector &) = delete;
151 PODSmallVector &operator=(const PODSmallVector &) = delete;
152
153 PODSmallVector(PODSmallVector &&Other) : PODSmallVector() {
154 if (Other.isInline()) {
155 std::copy(Other.begin(), Other.end(), First);
156 Last = First + Other.size();
157 Other.clear();
158 return;
159 }
160
161 First = Other.First;
162 Last = Other.Last;
163 Cap = Other.Cap;
164 Other.clearInline();
165 }
166
167 PODSmallVector &operator=(PODSmallVector &&Other) {
168 if (Other.isInline()) {
169 if (!isInline()) {
170 std::free(First);
171 clearInline();
172 }
173 std::copy(Other.begin(), Other.end(), First);
174 Last = First + Other.size();
175 Other.clear();
176 return *this;
177 }
178
179 if (isInline()) {
180 First = Other.First;
181 Last = Other.Last;
182 Cap = Other.Cap;
183 Other.clearInline();
184 return *this;
185 }
186
187 std::swap(First, Other.First);
188 std::swap(Last, Other.Last);
189 std::swap(Cap, Other.Cap);
190 Other.clear();
191 return *this;
192 }
193
194 // NOLINTNEXTLINE(readability-identifier-naming)
195 void push_back(const T &Elem) {
196 if (Last == Cap)
197 reserve(size() * 2);
198 *Last++ = Elem;
199 }
200
201 // NOLINTNEXTLINE(readability-identifier-naming)
202 void pop_back() {
203 assert(Last != First && "Popping empty vector!");
204 --Last;
205 }
206
207 void dropBack(size_t Index) {
208 assert(Index <= size() && "dropBack() can't expand!");
209 Last = First + Index;
210 }
211
212 T *begin() { return First; }
213 T *end() { return Last; }
214
215 bool empty() const { return First == Last; }
216 size_t size() const { return static_cast<size_t>(Last - First); }
217 T &back() {
218 assert(Last != First && "Calling back() on empty vector!");
219 return *(Last - 1);
220 }
221 T &operator[](size_t Index) {
222 assert(Index < size() && "Invalid access!");
223 return *(begin() + Index);
224 }
225 void clear() { Last = First; }
226
227 ~PODSmallVector() {
228 if (!isInline())
229 std::free(First);
230 }
231};
232
Richard Smithc20d1442018-08-20 20:14:49 +0000233// Base class of all AST nodes. The AST is built by the parser, then is
234// traversed by the printLeft/Right functions to produce a demangled string.
235class Node {
236public:
237 enum Kind : unsigned char {
238#define ENUMERATOR(NodeKind) K ## NodeKind,
239 FOR_EACH_NODE_KIND(ENUMERATOR)
240#undef ENUMERATOR
241 };
242
243 /// Three-way bool to track a cached value. Unknown is possible if this node
244 /// has an unexpanded parameter pack below it that may affect this cache.
245 enum class Cache : unsigned char { Yes, No, Unknown, };
246
247private:
248 Kind K;
249
250 // FIXME: Make these protected.
251public:
252 /// Tracks if this node has a component on its right side, in which case we
253 /// need to call printRight.
254 Cache RHSComponentCache;
255
256 /// Track if this node is a (possibly qualified) array type. This can affect
257 /// how we format the output string.
258 Cache ArrayCache;
259
260 /// Track if this node is a (possibly qualified) function type. This can
261 /// affect how we format the output string.
262 Cache FunctionCache;
263
264public:
265 Node(Kind K_, Cache RHSComponentCache_ = Cache::No,
266 Cache ArrayCache_ = Cache::No, Cache FunctionCache_ = Cache::No)
267 : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_),
268 FunctionCache(FunctionCache_) {}
269
270 /// Visit the most-derived object corresponding to this object.
271 template<typename Fn> void visit(Fn F) const;
272
273 // The following function is provided by all derived classes:
274 //
275 // Call F with arguments that, when passed to the constructor of this node,
276 // would construct an equivalent node.
277 //template<typename Fn> void match(Fn F) const;
278
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700279 bool hasRHSComponent(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000280 if (RHSComponentCache != Cache::Unknown)
281 return RHSComponentCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700282 return hasRHSComponentSlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000283 }
284
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700285 bool hasArray(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000286 if (ArrayCache != Cache::Unknown)
287 return ArrayCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700288 return hasArraySlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000289 }
290
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700291 bool hasFunction(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000292 if (FunctionCache != Cache::Unknown)
293 return FunctionCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700294 return hasFunctionSlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000295 }
296
297 Kind getKind() const { return K; }
298
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700299 virtual bool hasRHSComponentSlow(OutputBuffer &) const { return false; }
300 virtual bool hasArraySlow(OutputBuffer &) const { return false; }
301 virtual bool hasFunctionSlow(OutputBuffer &) const { return false; }
Richard Smithc20d1442018-08-20 20:14:49 +0000302
303 // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
304 // get at a node that actually represents some concrete syntax.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700305 virtual const Node *getSyntaxNode(OutputBuffer &) const { return this; }
Richard Smithc20d1442018-08-20 20:14:49 +0000306
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700307 void print(OutputBuffer &OB) const {
308 printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000309 if (RHSComponentCache != Cache::No)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700310 printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000311 }
312
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700313 // Print the "left" side of this Node into OutputString.
314 virtual void printLeft(OutputBuffer &) const = 0;
Richard Smithc20d1442018-08-20 20:14:49 +0000315
316 // Print the "right". This distinction is necessary to represent C++ types
317 // that appear on the RHS of their subtype, such as arrays or functions.
318 // Since most types don't have such a component, provide a default
319 // implementation.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700320 virtual void printRight(OutputBuffer &) const {}
Richard Smithc20d1442018-08-20 20:14:49 +0000321
322 virtual StringView getBaseName() const { return StringView(); }
323
324 // Silence compiler warnings, this dtor will never be called.
325 virtual ~Node() = default;
326
327#ifndef NDEBUG
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000328 DEMANGLE_DUMP_METHOD void dump() const;
Richard Smithc20d1442018-08-20 20:14:49 +0000329#endif
330};
331
332class NodeArray {
333 Node **Elements;
334 size_t NumElements;
335
336public:
337 NodeArray() : Elements(nullptr), NumElements(0) {}
338 NodeArray(Node **Elements_, size_t NumElements_)
339 : Elements(Elements_), NumElements(NumElements_) {}
340
341 bool empty() const { return NumElements == 0; }
342 size_t size() const { return NumElements; }
343
344 Node **begin() const { return Elements; }
345 Node **end() const { return Elements + NumElements; }
346
347 Node *operator[](size_t Idx) const { return Elements[Idx]; }
348
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700349 void printWithComma(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000350 bool FirstElement = true;
351 for (size_t Idx = 0; Idx != NumElements; ++Idx) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700352 size_t BeforeComma = OB.getCurrentPosition();
Richard Smithc20d1442018-08-20 20:14:49 +0000353 if (!FirstElement)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700354 OB += ", ";
355 size_t AfterComma = OB.getCurrentPosition();
356 Elements[Idx]->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000357
358 // Elements[Idx] is an empty parameter pack expansion, we should erase the
359 // comma we just printed.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700360 if (AfterComma == OB.getCurrentPosition()) {
361 OB.setCurrentPosition(BeforeComma);
Richard Smithc20d1442018-08-20 20:14:49 +0000362 continue;
363 }
364
365 FirstElement = false;
366 }
367 }
368};
369
370struct NodeArrayNode : Node {
371 NodeArray Array;
372 NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
373
374 template<typename Fn> void match(Fn F) const { F(Array); }
375
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700376 void printLeft(OutputBuffer &OB) const override { Array.printWithComma(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000377};
378
379class DotSuffix final : public Node {
380 const Node *Prefix;
381 const StringView Suffix;
382
383public:
384 DotSuffix(const Node *Prefix_, StringView Suffix_)
385 : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
386
387 template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
388
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700389 void printLeft(OutputBuffer &OB) const override {
390 Prefix->print(OB);
391 OB += " (";
392 OB += Suffix;
393 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000394 }
395};
396
397class VendorExtQualType final : public Node {
398 const Node *Ty;
399 StringView Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400400 const Node *TA;
Richard Smithc20d1442018-08-20 20:14:49 +0000401
402public:
Alex Orlovf50df922021-03-24 10:21:32 +0400403 VendorExtQualType(const Node *Ty_, StringView Ext_, const Node *TA_)
404 : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_), TA(TA_) {}
Richard Smithc20d1442018-08-20 20:14:49 +0000405
Alex Orlovf50df922021-03-24 10:21:32 +0400406 template <typename Fn> void match(Fn F) const { F(Ty, Ext, TA); }
Richard Smithc20d1442018-08-20 20:14:49 +0000407
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700408 void printLeft(OutputBuffer &OB) const override {
409 Ty->print(OB);
410 OB += " ";
411 OB += Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400412 if (TA != nullptr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700413 TA->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000414 }
415};
416
417enum FunctionRefQual : unsigned char {
418 FrefQualNone,
419 FrefQualLValue,
420 FrefQualRValue,
421};
422
423enum Qualifiers {
424 QualNone = 0,
425 QualConst = 0x1,
426 QualVolatile = 0x2,
427 QualRestrict = 0x4,
428};
429
430inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
431 return Q1 = static_cast<Qualifiers>(Q1 | Q2);
432}
433
Richard Smithdf1c14c2019-09-06 23:53:21 +0000434class QualType final : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +0000435protected:
436 const Qualifiers Quals;
437 const Node *Child;
438
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700439 void printQuals(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000440 if (Quals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700441 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000442 if (Quals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700443 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000444 if (Quals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700445 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000446 }
447
448public:
449 QualType(const Node *Child_, Qualifiers Quals_)
450 : Node(KQualType, Child_->RHSComponentCache,
451 Child_->ArrayCache, Child_->FunctionCache),
452 Quals(Quals_), Child(Child_) {}
453
454 template<typename Fn> void match(Fn F) const { F(Child, Quals); }
455
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700456 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
457 return Child->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000458 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700459 bool hasArraySlow(OutputBuffer &OB) const override {
460 return Child->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000461 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700462 bool hasFunctionSlow(OutputBuffer &OB) const override {
463 return Child->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000464 }
465
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700466 void printLeft(OutputBuffer &OB) const override {
467 Child->printLeft(OB);
468 printQuals(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000469 }
470
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700471 void printRight(OutputBuffer &OB) const override { Child->printRight(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000472};
473
474class ConversionOperatorType final : public Node {
475 const Node *Ty;
476
477public:
478 ConversionOperatorType(const Node *Ty_)
479 : Node(KConversionOperatorType), Ty(Ty_) {}
480
481 template<typename Fn> void match(Fn F) const { F(Ty); }
482
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700483 void printLeft(OutputBuffer &OB) const override {
484 OB += "operator ";
485 Ty->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000486 }
487};
488
489class PostfixQualifiedType final : public Node {
490 const Node *Ty;
491 const StringView Postfix;
492
493public:
494 PostfixQualifiedType(Node *Ty_, StringView Postfix_)
495 : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
496
497 template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
498
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700499 void printLeft(OutputBuffer &OB) const override {
500 Ty->printLeft(OB);
501 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +0000502 }
503};
504
505class NameType final : public Node {
506 const StringView Name;
507
508public:
509 NameType(StringView Name_) : Node(KNameType), Name(Name_) {}
510
511 template<typename Fn> void match(Fn F) const { F(Name); }
512
513 StringView getName() const { return Name; }
514 StringView getBaseName() const override { return Name; }
515
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700516 void printLeft(OutputBuffer &OB) const override { OB += Name; }
Richard Smithc20d1442018-08-20 20:14:49 +0000517};
518
519class ElaboratedTypeSpefType : public Node {
520 StringView Kind;
521 Node *Child;
522public:
523 ElaboratedTypeSpefType(StringView Kind_, Node *Child_)
524 : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
525
526 template<typename Fn> void match(Fn F) const { F(Kind, Child); }
527
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700528 void printLeft(OutputBuffer &OB) const override {
529 OB += Kind;
530 OB += ' ';
531 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000532 }
533};
534
535struct AbiTagAttr : Node {
536 Node *Base;
537 StringView Tag;
538
539 AbiTagAttr(Node* Base_, StringView Tag_)
540 : Node(KAbiTagAttr, Base_->RHSComponentCache,
541 Base_->ArrayCache, Base_->FunctionCache),
542 Base(Base_), Tag(Tag_) {}
543
544 template<typename Fn> void match(Fn F) const { F(Base, Tag); }
545
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700546 void printLeft(OutputBuffer &OB) const override {
547 Base->printLeft(OB);
548 OB += "[abi:";
549 OB += Tag;
550 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +0000551 }
552};
553
554class EnableIfAttr : public Node {
555 NodeArray Conditions;
556public:
557 EnableIfAttr(NodeArray Conditions_)
558 : Node(KEnableIfAttr), Conditions(Conditions_) {}
559
560 template<typename Fn> void match(Fn F) const { F(Conditions); }
561
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700562 void printLeft(OutputBuffer &OB) const override {
563 OB += " [enable_if:";
564 Conditions.printWithComma(OB);
565 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +0000566 }
567};
568
569class ObjCProtoName : public Node {
570 const Node *Ty;
571 StringView Protocol;
572
573 friend class PointerType;
574
575public:
576 ObjCProtoName(const Node *Ty_, StringView Protocol_)
577 : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
578
579 template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
580
581 bool isObjCObject() const {
582 return Ty->getKind() == KNameType &&
583 static_cast<const NameType *>(Ty)->getName() == "objc_object";
584 }
585
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700586 void printLeft(OutputBuffer &OB) const override {
587 Ty->print(OB);
588 OB += "<";
589 OB += Protocol;
590 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +0000591 }
592};
593
594class PointerType final : public Node {
595 const Node *Pointee;
596
597public:
598 PointerType(const Node *Pointee_)
599 : Node(KPointerType, Pointee_->RHSComponentCache),
600 Pointee(Pointee_) {}
601
602 template<typename Fn> void match(Fn F) const { F(Pointee); }
603
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700604 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
605 return Pointee->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000606 }
607
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700608 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000609 // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
610 if (Pointee->getKind() != KObjCProtoName ||
611 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700612 Pointee->printLeft(OB);
613 if (Pointee->hasArray(OB))
614 OB += " ";
615 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
616 OB += "(";
617 OB += "*";
Richard Smithc20d1442018-08-20 20:14:49 +0000618 } else {
619 const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700620 OB += "id<";
621 OB += objcProto->Protocol;
622 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +0000623 }
624 }
625
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700626 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000627 if (Pointee->getKind() != KObjCProtoName ||
628 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700629 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
630 OB += ")";
631 Pointee->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000632 }
633 }
634};
635
636enum class ReferenceKind {
637 LValue,
638 RValue,
639};
640
641// Represents either a LValue or an RValue reference type.
642class ReferenceType : public Node {
643 const Node *Pointee;
644 ReferenceKind RK;
645
646 mutable bool Printing = false;
647
648 // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
649 // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
650 // other combination collapses to a lvalue ref.
Mikhail Borisov05f77222021-08-17 18:10:57 -0400651 //
652 // A combination of a TemplateForwardReference and a back-ref Substitution
653 // from an ill-formed string may have created a cycle; use cycle detection to
654 // avoid looping forever.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700655 std::pair<ReferenceKind, const Node *> collapse(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000656 auto SoFar = std::make_pair(RK, Pointee);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400657 // Track the chain of nodes for the Floyd's 'tortoise and hare'
658 // cycle-detection algorithm, since getSyntaxNode(S) is impure
659 PODSmallVector<const Node *, 8> Prev;
Richard Smithc20d1442018-08-20 20:14:49 +0000660 for (;;) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700661 const Node *SN = SoFar.second->getSyntaxNode(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000662 if (SN->getKind() != KReferenceType)
663 break;
664 auto *RT = static_cast<const ReferenceType *>(SN);
665 SoFar.second = RT->Pointee;
666 SoFar.first = std::min(SoFar.first, RT->RK);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400667
668 // The middle of Prev is the 'slow' pointer moving at half speed
669 Prev.push_back(SoFar.second);
670 if (Prev.size() > 1 && SoFar.second == Prev[(Prev.size() - 1) / 2]) {
671 // Cycle detected
672 SoFar.second = nullptr;
673 break;
674 }
Richard Smithc20d1442018-08-20 20:14:49 +0000675 }
676 return SoFar;
677 }
678
679public:
680 ReferenceType(const Node *Pointee_, ReferenceKind RK_)
681 : Node(KReferenceType, Pointee_->RHSComponentCache),
682 Pointee(Pointee_), RK(RK_) {}
683
684 template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
685
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700686 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
687 return Pointee->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000688 }
689
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700690 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000691 if (Printing)
692 return;
693 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700694 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400695 if (!Collapsed.second)
696 return;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700697 Collapsed.second->printLeft(OB);
698 if (Collapsed.second->hasArray(OB))
699 OB += " ";
700 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
701 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +0000702
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700703 OB += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
Richard Smithc20d1442018-08-20 20:14:49 +0000704 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700705 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000706 if (Printing)
707 return;
708 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700709 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400710 if (!Collapsed.second)
711 return;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700712 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
713 OB += ")";
714 Collapsed.second->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000715 }
716};
717
718class PointerToMemberType final : public Node {
719 const Node *ClassType;
720 const Node *MemberType;
721
722public:
723 PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
724 : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
725 ClassType(ClassType_), MemberType(MemberType_) {}
726
727 template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
728
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700729 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
730 return MemberType->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000731 }
732
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700733 void printLeft(OutputBuffer &OB) const override {
734 MemberType->printLeft(OB);
735 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
736 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +0000737 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700738 OB += " ";
739 ClassType->print(OB);
740 OB += "::*";
Richard Smithc20d1442018-08-20 20:14:49 +0000741 }
742
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700743 void printRight(OutputBuffer &OB) const override {
744 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
745 OB += ")";
746 MemberType->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000747 }
748};
749
Richard Smithc20d1442018-08-20 20:14:49 +0000750class ArrayType final : public Node {
751 const Node *Base;
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800752 Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +0000753
754public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800755 ArrayType(const Node *Base_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +0000756 : Node(KArrayType,
757 /*RHSComponentCache=*/Cache::Yes,
758 /*ArrayCache=*/Cache::Yes),
759 Base(Base_), Dimension(Dimension_) {}
760
761 template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
762
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700763 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
764 bool hasArraySlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000765
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700766 void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000767
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700768 void printRight(OutputBuffer &OB) const override {
769 if (OB.back() != ']')
770 OB += " ";
771 OB += "[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800772 if (Dimension)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700773 Dimension->print(OB);
774 OB += "]";
775 Base->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000776 }
777};
778
779class FunctionType final : public Node {
780 const Node *Ret;
781 NodeArray Params;
782 Qualifiers CVQuals;
783 FunctionRefQual RefQual;
784 const Node *ExceptionSpec;
785
786public:
787 FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
788 FunctionRefQual RefQual_, const Node *ExceptionSpec_)
789 : Node(KFunctionType,
790 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
791 /*FunctionCache=*/Cache::Yes),
792 Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
793 ExceptionSpec(ExceptionSpec_) {}
794
795 template<typename Fn> void match(Fn F) const {
796 F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
797 }
798
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700799 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
800 bool hasFunctionSlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000801
802 // Handle C++'s ... quirky decl grammar by using the left & right
803 // distinction. Consider:
804 // int (*f(float))(char) {}
805 // f is a function that takes a float and returns a pointer to a function
806 // that takes a char and returns an int. If we're trying to print f, start
807 // by printing out the return types's left, then print our parameters, then
808 // finally print right of the return type.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700809 void printLeft(OutputBuffer &OB) const override {
810 Ret->printLeft(OB);
811 OB += " ";
Richard Smithc20d1442018-08-20 20:14:49 +0000812 }
813
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700814 void printRight(OutputBuffer &OB) const override {
815 OB += "(";
816 Params.printWithComma(OB);
817 OB += ")";
818 Ret->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000819
820 if (CVQuals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700821 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000822 if (CVQuals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700823 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000824 if (CVQuals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700825 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000826
827 if (RefQual == FrefQualLValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700828 OB += " &";
Richard Smithc20d1442018-08-20 20:14:49 +0000829 else if (RefQual == FrefQualRValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700830 OB += " &&";
Richard Smithc20d1442018-08-20 20:14:49 +0000831
832 if (ExceptionSpec != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700833 OB += ' ';
834 ExceptionSpec->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000835 }
836 }
837};
838
839class NoexceptSpec : public Node {
840 const Node *E;
841public:
842 NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
843
844 template<typename Fn> void match(Fn F) const { F(E); }
845
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700846 void printLeft(OutputBuffer &OB) const override {
847 OB += "noexcept(";
848 E->print(OB);
849 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000850 }
851};
852
853class DynamicExceptionSpec : public Node {
854 NodeArray Types;
855public:
856 DynamicExceptionSpec(NodeArray Types_)
857 : Node(KDynamicExceptionSpec), Types(Types_) {}
858
859 template<typename Fn> void match(Fn F) const { F(Types); }
860
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700861 void printLeft(OutputBuffer &OB) const override {
862 OB += "throw(";
863 Types.printWithComma(OB);
864 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +0000865 }
866};
867
868class FunctionEncoding final : public Node {
869 const Node *Ret;
870 const Node *Name;
871 NodeArray Params;
872 const Node *Attrs;
873 Qualifiers CVQuals;
874 FunctionRefQual RefQual;
875
876public:
877 FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
878 const Node *Attrs_, Qualifiers CVQuals_,
879 FunctionRefQual RefQual_)
880 : Node(KFunctionEncoding,
881 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
882 /*FunctionCache=*/Cache::Yes),
883 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
884 CVQuals(CVQuals_), RefQual(RefQual_) {}
885
886 template<typename Fn> void match(Fn F) const {
887 F(Ret, Name, Params, Attrs, CVQuals, RefQual);
888 }
889
890 Qualifiers getCVQuals() const { return CVQuals; }
891 FunctionRefQual getRefQual() const { return RefQual; }
892 NodeArray getParams() const { return Params; }
893 const Node *getReturnType() const { return Ret; }
894
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700895 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
896 bool hasFunctionSlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000897
898 const Node *getName() const { return Name; }
899
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700900 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000901 if (Ret) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700902 Ret->printLeft(OB);
903 if (!Ret->hasRHSComponent(OB))
904 OB += " ";
Richard Smithc20d1442018-08-20 20:14:49 +0000905 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700906 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000907 }
908
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700909 void printRight(OutputBuffer &OB) const override {
910 OB += "(";
911 Params.printWithComma(OB);
912 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000913 if (Ret)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700914 Ret->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000915
916 if (CVQuals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700917 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000918 if (CVQuals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700919 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000920 if (CVQuals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700921 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000922
923 if (RefQual == FrefQualLValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700924 OB += " &";
Richard Smithc20d1442018-08-20 20:14:49 +0000925 else if (RefQual == FrefQualRValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700926 OB += " &&";
Richard Smithc20d1442018-08-20 20:14:49 +0000927
928 if (Attrs != nullptr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700929 Attrs->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000930 }
931};
932
933class LiteralOperator : public Node {
934 const Node *OpName;
935
936public:
937 LiteralOperator(const Node *OpName_)
938 : Node(KLiteralOperator), OpName(OpName_) {}
939
940 template<typename Fn> void match(Fn F) const { F(OpName); }
941
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700942 void printLeft(OutputBuffer &OB) const override {
943 OB += "operator\"\" ";
944 OpName->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000945 }
946};
947
948class SpecialName final : public Node {
949 const StringView Special;
950 const Node *Child;
951
952public:
953 SpecialName(StringView Special_, const Node *Child_)
954 : Node(KSpecialName), Special(Special_), Child(Child_) {}
955
956 template<typename Fn> void match(Fn F) const { F(Special, Child); }
957
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700958 void printLeft(OutputBuffer &OB) const override {
959 OB += Special;
960 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000961 }
962};
963
964class CtorVtableSpecialName final : public Node {
965 const Node *FirstType;
966 const Node *SecondType;
967
968public:
969 CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
970 : Node(KCtorVtableSpecialName),
971 FirstType(FirstType_), SecondType(SecondType_) {}
972
973 template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
974
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700975 void printLeft(OutputBuffer &OB) const override {
976 OB += "construction vtable for ";
977 FirstType->print(OB);
978 OB += "-in-";
979 SecondType->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000980 }
981};
982
983struct NestedName : Node {
984 Node *Qual;
985 Node *Name;
986
987 NestedName(Node *Qual_, Node *Name_)
988 : Node(KNestedName), Qual(Qual_), Name(Name_) {}
989
990 template<typename Fn> void match(Fn F) const { F(Qual, Name); }
991
992 StringView getBaseName() const override { return Name->getBaseName(); }
993
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700994 void printLeft(OutputBuffer &OB) const override {
995 Qual->print(OB);
996 OB += "::";
997 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000998 }
999};
1000
1001struct LocalName : Node {
1002 Node *Encoding;
1003 Node *Entity;
1004
1005 LocalName(Node *Encoding_, Node *Entity_)
1006 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
1007
1008 template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
1009
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001010 void printLeft(OutputBuffer &OB) const override {
1011 Encoding->print(OB);
1012 OB += "::";
1013 Entity->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001014 }
1015};
1016
1017class QualifiedName final : public Node {
1018 // qualifier::name
1019 const Node *Qualifier;
1020 const Node *Name;
1021
1022public:
1023 QualifiedName(const Node *Qualifier_, const Node *Name_)
1024 : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
1025
1026 template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
1027
1028 StringView getBaseName() const override { return Name->getBaseName(); }
1029
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001030 void printLeft(OutputBuffer &OB) const override {
1031 Qualifier->print(OB);
1032 OB += "::";
1033 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001034 }
1035};
1036
1037class VectorType final : public Node {
1038 const Node *BaseType;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001039 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001040
1041public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001042 VectorType(const Node *BaseType_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001043 : Node(KVectorType), BaseType(BaseType_),
1044 Dimension(Dimension_) {}
1045
1046 template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
1047
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001048 void printLeft(OutputBuffer &OB) const override {
1049 BaseType->print(OB);
1050 OB += " vector[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001051 if (Dimension)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001052 Dimension->print(OB);
1053 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001054 }
1055};
1056
1057class PixelVectorType final : public Node {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001058 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001059
1060public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001061 PixelVectorType(const Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001062 : Node(KPixelVectorType), Dimension(Dimension_) {}
1063
1064 template<typename Fn> void match(Fn F) const { F(Dimension); }
1065
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001066 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001067 // FIXME: This should demangle as "vector pixel".
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001068 OB += "pixel vector[";
1069 Dimension->print(OB);
1070 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001071 }
1072};
1073
Pengfei Wang50e90b82021-09-23 11:02:25 +08001074class BinaryFPType final : public Node {
1075 const Node *Dimension;
1076
1077public:
1078 BinaryFPType(const Node *Dimension_)
1079 : Node(KBinaryFPType), Dimension(Dimension_) {}
1080
1081 template<typename Fn> void match(Fn F) const { F(Dimension); }
1082
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001083 void printLeft(OutputBuffer &OB) const override {
1084 OB += "_Float";
1085 Dimension->print(OB);
Pengfei Wang50e90b82021-09-23 11:02:25 +08001086 }
1087};
1088
Richard Smithdf1c14c2019-09-06 23:53:21 +00001089enum class TemplateParamKind { Type, NonType, Template };
1090
1091/// An invented name for a template parameter for which we don't have a
1092/// corresponding template argument.
1093///
1094/// This node is created when parsing the <lambda-sig> for a lambda with
1095/// explicit template arguments, which might be referenced in the parameter
1096/// types appearing later in the <lambda-sig>.
1097class SyntheticTemplateParamName final : public Node {
1098 TemplateParamKind Kind;
1099 unsigned Index;
1100
1101public:
1102 SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_)
1103 : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {}
1104
1105 template<typename Fn> void match(Fn F) const { F(Kind, Index); }
1106
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001107 void printLeft(OutputBuffer &OB) const override {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001108 switch (Kind) {
1109 case TemplateParamKind::Type:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001110 OB += "$T";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001111 break;
1112 case TemplateParamKind::NonType:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001113 OB += "$N";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001114 break;
1115 case TemplateParamKind::Template:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001116 OB += "$TT";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001117 break;
1118 }
1119 if (Index > 0)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001120 OB << Index - 1;
Richard Smithdf1c14c2019-09-06 23:53:21 +00001121 }
1122};
1123
1124/// A template type parameter declaration, 'typename T'.
1125class TypeTemplateParamDecl final : public Node {
1126 Node *Name;
1127
1128public:
1129 TypeTemplateParamDecl(Node *Name_)
1130 : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {}
1131
1132 template<typename Fn> void match(Fn F) const { F(Name); }
1133
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001134 void printLeft(OutputBuffer &OB) const override { OB += "typename "; }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001135
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001136 void printRight(OutputBuffer &OB) const override { Name->print(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001137};
1138
1139/// A non-type template parameter declaration, 'int N'.
1140class NonTypeTemplateParamDecl final : public Node {
1141 Node *Name;
1142 Node *Type;
1143
1144public:
1145 NonTypeTemplateParamDecl(Node *Name_, Node *Type_)
1146 : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {}
1147
1148 template<typename Fn> void match(Fn F) const { F(Name, Type); }
1149
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001150 void printLeft(OutputBuffer &OB) const override {
1151 Type->printLeft(OB);
1152 if (!Type->hasRHSComponent(OB))
1153 OB += " ";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001154 }
1155
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001156 void printRight(OutputBuffer &OB) const override {
1157 Name->print(OB);
1158 Type->printRight(OB);
Richard Smithdf1c14c2019-09-06 23:53:21 +00001159 }
1160};
1161
1162/// A template template parameter declaration,
1163/// 'template<typename T> typename N'.
1164class TemplateTemplateParamDecl final : public Node {
1165 Node *Name;
1166 NodeArray Params;
1167
1168public:
1169 TemplateTemplateParamDecl(Node *Name_, NodeArray Params_)
1170 : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_),
1171 Params(Params_) {}
1172
1173 template<typename Fn> void match(Fn F) const { F(Name, Params); }
1174
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001175 void printLeft(OutputBuffer &OB) const override {
1176 OB += "template<";
1177 Params.printWithComma(OB);
1178 OB += "> typename ";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001179 }
1180
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001181 void printRight(OutputBuffer &OB) const override { Name->print(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001182};
1183
1184/// A template parameter pack declaration, 'typename ...T'.
1185class TemplateParamPackDecl final : public Node {
1186 Node *Param;
1187
1188public:
1189 TemplateParamPackDecl(Node *Param_)
1190 : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {}
1191
1192 template<typename Fn> void match(Fn F) const { F(Param); }
1193
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001194 void printLeft(OutputBuffer &OB) const override {
1195 Param->printLeft(OB);
1196 OB += "...";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001197 }
1198
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001199 void printRight(OutputBuffer &OB) const override { Param->printRight(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001200};
1201
Richard Smithc20d1442018-08-20 20:14:49 +00001202/// An unexpanded parameter pack (either in the expression or type context). If
1203/// this AST is correct, this node will have a ParameterPackExpansion node above
1204/// it.
1205///
1206/// This node is created when some <template-args> are found that apply to an
1207/// <encoding>, and is stored in the TemplateParams table. In order for this to
1208/// appear in the final AST, it has to referenced via a <template-param> (ie,
1209/// T_).
1210class ParameterPack final : public Node {
1211 NodeArray Data;
1212
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001213 // Setup OutputString for a pack expansion unless we're already expanding one.
1214 void initializePackExpansion(OutputBuffer &OB) const {
1215 if (OB.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
1216 OB.CurrentPackMax = static_cast<unsigned>(Data.size());
1217 OB.CurrentPackIndex = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00001218 }
1219 }
1220
1221public:
1222 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
1223 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
1224 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1225 return P->ArrayCache == Cache::No;
1226 }))
1227 ArrayCache = Cache::No;
1228 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1229 return P->FunctionCache == Cache::No;
1230 }))
1231 FunctionCache = Cache::No;
1232 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1233 return P->RHSComponentCache == Cache::No;
1234 }))
1235 RHSComponentCache = Cache::No;
1236 }
1237
1238 template<typename Fn> void match(Fn F) const { F(Data); }
1239
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001240 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
1241 initializePackExpansion(OB);
1242 size_t Idx = OB.CurrentPackIndex;
1243 return Idx < Data.size() && Data[Idx]->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001244 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001245 bool hasArraySlow(OutputBuffer &OB) const override {
1246 initializePackExpansion(OB);
1247 size_t Idx = OB.CurrentPackIndex;
1248 return Idx < Data.size() && Data[Idx]->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001249 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001250 bool hasFunctionSlow(OutputBuffer &OB) const override {
1251 initializePackExpansion(OB);
1252 size_t Idx = OB.CurrentPackIndex;
1253 return Idx < Data.size() && Data[Idx]->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001254 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001255 const Node *getSyntaxNode(OutputBuffer &OB) const override {
1256 initializePackExpansion(OB);
1257 size_t Idx = OB.CurrentPackIndex;
1258 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(OB) : this;
Richard Smithc20d1442018-08-20 20:14:49 +00001259 }
1260
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001261 void printLeft(OutputBuffer &OB) const override {
1262 initializePackExpansion(OB);
1263 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001264 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001265 Data[Idx]->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001266 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001267 void printRight(OutputBuffer &OB) const override {
1268 initializePackExpansion(OB);
1269 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001270 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001271 Data[Idx]->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001272 }
1273};
1274
1275/// A variadic template argument. This node represents an occurrence of
1276/// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1277/// one of it's Elements is. The parser inserts a ParameterPack into the
1278/// TemplateParams table if the <template-args> this pack belongs to apply to an
1279/// <encoding>.
1280class TemplateArgumentPack final : public Node {
1281 NodeArray Elements;
1282public:
1283 TemplateArgumentPack(NodeArray Elements_)
1284 : Node(KTemplateArgumentPack), Elements(Elements_) {}
1285
1286 template<typename Fn> void match(Fn F) const { F(Elements); }
1287
1288 NodeArray getElements() const { return Elements; }
1289
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001290 void printLeft(OutputBuffer &OB) const override {
1291 Elements.printWithComma(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001292 }
1293};
1294
1295/// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1296/// which each have Child->ParameterPackSize elements.
1297class ParameterPackExpansion final : public Node {
1298 const Node *Child;
1299
1300public:
1301 ParameterPackExpansion(const Node *Child_)
1302 : Node(KParameterPackExpansion), Child(Child_) {}
1303
1304 template<typename Fn> void match(Fn F) const { F(Child); }
1305
1306 const Node *getChild() const { return Child; }
1307
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001308 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001309 constexpr unsigned Max = std::numeric_limits<unsigned>::max();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001310 SwapAndRestore<unsigned> SavePackIdx(OB.CurrentPackIndex, Max);
1311 SwapAndRestore<unsigned> SavePackMax(OB.CurrentPackMax, Max);
1312 size_t StreamPos = OB.getCurrentPosition();
Richard Smithc20d1442018-08-20 20:14:49 +00001313
1314 // Print the first element in the pack. If Child contains a ParameterPack,
1315 // it will set up S.CurrentPackMax and print the first element.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001316 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001317
1318 // No ParameterPack was found in Child. This can occur if we've found a pack
1319 // expansion on a <function-param>.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001320 if (OB.CurrentPackMax == Max) {
1321 OB += "...";
Richard Smithc20d1442018-08-20 20:14:49 +00001322 return;
1323 }
1324
1325 // We found a ParameterPack, but it has no elements. Erase whatever we may
1326 // of printed.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001327 if (OB.CurrentPackMax == 0) {
1328 OB.setCurrentPosition(StreamPos);
Richard Smithc20d1442018-08-20 20:14:49 +00001329 return;
1330 }
1331
1332 // Else, iterate through the rest of the elements in the pack.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001333 for (unsigned I = 1, E = OB.CurrentPackMax; I < E; ++I) {
1334 OB += ", ";
1335 OB.CurrentPackIndex = I;
1336 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001337 }
1338 }
1339};
1340
1341class TemplateArgs final : public Node {
1342 NodeArray Params;
1343
1344public:
1345 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1346
1347 template<typename Fn> void match(Fn F) const { F(Params); }
1348
1349 NodeArray getParams() { return Params; }
1350
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001351 void printLeft(OutputBuffer &OB) const override {
1352 OB += "<";
1353 Params.printWithComma(OB);
1354 if (OB.back() == '>')
1355 OB += " ";
1356 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +00001357 }
1358};
1359
Richard Smithb485b352018-08-24 23:30:26 +00001360/// A forward-reference to a template argument that was not known at the point
1361/// where the template parameter name was parsed in a mangling.
1362///
1363/// This is created when demangling the name of a specialization of a
1364/// conversion function template:
1365///
1366/// \code
1367/// struct A {
1368/// template<typename T> operator T*();
1369/// };
1370/// \endcode
1371///
1372/// When demangling a specialization of the conversion function template, we
1373/// encounter the name of the template (including the \c T) before we reach
1374/// the template argument list, so we cannot substitute the parameter name
1375/// for the corresponding argument while parsing. Instead, we create a
1376/// \c ForwardTemplateReference node that is resolved after we parse the
1377/// template arguments.
Richard Smithc20d1442018-08-20 20:14:49 +00001378struct ForwardTemplateReference : Node {
1379 size_t Index;
1380 Node *Ref = nullptr;
1381
1382 // If we're currently printing this node. It is possible (though invalid) for
1383 // a forward template reference to refer to itself via a substitution. This
1384 // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1385 // out if more than one print* function is active.
1386 mutable bool Printing = false;
1387
1388 ForwardTemplateReference(size_t Index_)
1389 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1390 Cache::Unknown),
1391 Index(Index_) {}
1392
1393 // We don't provide a matcher for these, because the value of the node is
1394 // not determined by its construction parameters, and it generally needs
1395 // special handling.
1396 template<typename Fn> void match(Fn F) const = delete;
1397
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001398 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001399 if (Printing)
1400 return false;
1401 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001402 return Ref->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001403 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001404 bool hasArraySlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001405 if (Printing)
1406 return false;
1407 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001408 return Ref->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001409 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001410 bool hasFunctionSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001411 if (Printing)
1412 return false;
1413 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001414 return Ref->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001415 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001416 const Node *getSyntaxNode(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001417 if (Printing)
1418 return this;
1419 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001420 return Ref->getSyntaxNode(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001421 }
1422
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001423 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001424 if (Printing)
1425 return;
1426 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001427 Ref->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001428 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001429 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001430 if (Printing)
1431 return;
1432 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001433 Ref->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001434 }
1435};
1436
1437struct NameWithTemplateArgs : Node {
1438 // name<template_args>
1439 Node *Name;
1440 Node *TemplateArgs;
1441
1442 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1443 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1444
1445 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1446
1447 StringView getBaseName() const override { return Name->getBaseName(); }
1448
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001449 void printLeft(OutputBuffer &OB) const override {
1450 Name->print(OB);
1451 TemplateArgs->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001452 }
1453};
1454
1455class GlobalQualifiedName final : public Node {
1456 Node *Child;
1457
1458public:
1459 GlobalQualifiedName(Node* Child_)
1460 : Node(KGlobalQualifiedName), Child(Child_) {}
1461
1462 template<typename Fn> void match(Fn F) const { F(Child); }
1463
1464 StringView getBaseName() const override { return Child->getBaseName(); }
1465
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001466 void printLeft(OutputBuffer &OB) const override {
1467 OB += "::";
1468 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001469 }
1470};
1471
1472struct StdQualifiedName : Node {
1473 Node *Child;
1474
1475 StdQualifiedName(Node *Child_) : Node(KStdQualifiedName), Child(Child_) {}
1476
1477 template<typename Fn> void match(Fn F) const { F(Child); }
1478
1479 StringView getBaseName() const override { return Child->getBaseName(); }
1480
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001481 void printLeft(OutputBuffer &OB) const override {
1482 OB += "std::";
1483 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001484 }
1485};
1486
1487enum class SpecialSubKind {
1488 allocator,
1489 basic_string,
1490 string,
1491 istream,
1492 ostream,
1493 iostream,
1494};
1495
1496class ExpandedSpecialSubstitution final : public Node {
1497 SpecialSubKind SSK;
1498
1499public:
1500 ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1501 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1502
1503 template<typename Fn> void match(Fn F) const { F(SSK); }
1504
1505 StringView getBaseName() const override {
1506 switch (SSK) {
1507 case SpecialSubKind::allocator:
1508 return StringView("allocator");
1509 case SpecialSubKind::basic_string:
1510 return StringView("basic_string");
1511 case SpecialSubKind::string:
1512 return StringView("basic_string");
1513 case SpecialSubKind::istream:
1514 return StringView("basic_istream");
1515 case SpecialSubKind::ostream:
1516 return StringView("basic_ostream");
1517 case SpecialSubKind::iostream:
1518 return StringView("basic_iostream");
1519 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001520 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001521 }
1522
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001523 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001524 switch (SSK) {
1525 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001526 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001527 break;
1528 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001529 OB += "std::basic_string";
Richard Smithb485b352018-08-24 23:30:26 +00001530 break;
Richard Smithc20d1442018-08-20 20:14:49 +00001531 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001532 OB += "std::basic_string<char, std::char_traits<char>, "
1533 "std::allocator<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001534 break;
1535 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001536 OB += "std::basic_istream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001537 break;
1538 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001539 OB += "std::basic_ostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001540 break;
1541 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001542 OB += "std::basic_iostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001543 break;
1544 }
1545 }
1546};
1547
1548class SpecialSubstitution final : public Node {
1549public:
1550 SpecialSubKind SSK;
1551
1552 SpecialSubstitution(SpecialSubKind SSK_)
1553 : Node(KSpecialSubstitution), SSK(SSK_) {}
1554
1555 template<typename Fn> void match(Fn F) const { F(SSK); }
1556
1557 StringView getBaseName() const override {
1558 switch (SSK) {
1559 case SpecialSubKind::allocator:
1560 return StringView("allocator");
1561 case SpecialSubKind::basic_string:
1562 return StringView("basic_string");
1563 case SpecialSubKind::string:
1564 return StringView("string");
1565 case SpecialSubKind::istream:
1566 return StringView("istream");
1567 case SpecialSubKind::ostream:
1568 return StringView("ostream");
1569 case SpecialSubKind::iostream:
1570 return StringView("iostream");
1571 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001572 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001573 }
1574
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001575 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001576 switch (SSK) {
1577 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001578 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001579 break;
1580 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001581 OB += "std::basic_string";
Richard Smithc20d1442018-08-20 20:14:49 +00001582 break;
1583 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001584 OB += "std::string";
Richard Smithc20d1442018-08-20 20:14:49 +00001585 break;
1586 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001587 OB += "std::istream";
Richard Smithc20d1442018-08-20 20:14:49 +00001588 break;
1589 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001590 OB += "std::ostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001591 break;
1592 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001593 OB += "std::iostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001594 break;
1595 }
1596 }
1597};
1598
1599class CtorDtorName final : public Node {
1600 const Node *Basename;
1601 const bool IsDtor;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001602 const int Variant;
Richard Smithc20d1442018-08-20 20:14:49 +00001603
1604public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001605 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1606 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1607 Variant(Variant_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001608
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001609 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
Richard Smithc20d1442018-08-20 20:14:49 +00001610
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001611 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001612 if (IsDtor)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001613 OB += "~";
1614 OB += Basename->getBaseName();
Richard Smithc20d1442018-08-20 20:14:49 +00001615 }
1616};
1617
1618class DtorName : public Node {
1619 const Node *Base;
1620
1621public:
1622 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1623
1624 template<typename Fn> void match(Fn F) const { F(Base); }
1625
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001626 void printLeft(OutputBuffer &OB) const override {
1627 OB += "~";
1628 Base->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001629 }
1630};
1631
1632class UnnamedTypeName : public Node {
1633 const StringView Count;
1634
1635public:
1636 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1637
1638 template<typename Fn> void match(Fn F) const { F(Count); }
1639
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001640 void printLeft(OutputBuffer &OB) const override {
1641 OB += "'unnamed";
1642 OB += Count;
1643 OB += "\'";
Richard Smithc20d1442018-08-20 20:14:49 +00001644 }
1645};
1646
1647class ClosureTypeName : public Node {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001648 NodeArray TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00001649 NodeArray Params;
1650 StringView Count;
1651
1652public:
Richard Smithdf1c14c2019-09-06 23:53:21 +00001653 ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_,
1654 StringView Count_)
1655 : Node(KClosureTypeName), TemplateParams(TemplateParams_),
1656 Params(Params_), Count(Count_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001657
Richard Smithdf1c14c2019-09-06 23:53:21 +00001658 template<typename Fn> void match(Fn F) const {
1659 F(TemplateParams, Params, Count);
1660 }
1661
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001662 void printDeclarator(OutputBuffer &OB) const {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001663 if (!TemplateParams.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001664 OB += "<";
1665 TemplateParams.printWithComma(OB);
1666 OB += ">";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001667 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001668 OB += "(";
1669 Params.printWithComma(OB);
1670 OB += ")";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001671 }
Richard Smithc20d1442018-08-20 20:14:49 +00001672
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001673 void printLeft(OutputBuffer &OB) const override {
1674 OB += "\'lambda";
1675 OB += Count;
1676 OB += "\'";
1677 printDeclarator(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001678 }
1679};
1680
1681class StructuredBindingName : public Node {
1682 NodeArray Bindings;
1683public:
1684 StructuredBindingName(NodeArray Bindings_)
1685 : Node(KStructuredBindingName), Bindings(Bindings_) {}
1686
1687 template<typename Fn> void match(Fn F) const { F(Bindings); }
1688
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001689 void printLeft(OutputBuffer &OB) const override {
1690 OB += '[';
1691 Bindings.printWithComma(OB);
1692 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00001693 }
1694};
1695
1696// -- Expression Nodes --
1697
1698class BinaryExpr : public Node {
1699 const Node *LHS;
1700 const StringView InfixOperator;
1701 const Node *RHS;
1702
1703public:
1704 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
1705 : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
1706 }
1707
1708 template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
1709
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001710 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001711 // might be a template argument expression, then we need to disambiguate
1712 // with parens.
1713 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001714 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +00001715
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001716 OB += "(";
1717 LHS->print(OB);
1718 OB += ") ";
1719 OB += InfixOperator;
1720 OB += " (";
1721 RHS->print(OB);
1722 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001723
1724 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001725 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001726 }
1727};
1728
1729class ArraySubscriptExpr : public Node {
1730 const Node *Op1;
1731 const Node *Op2;
1732
1733public:
1734 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
1735 : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
1736
1737 template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
1738
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001739 void printLeft(OutputBuffer &OB) const override {
1740 OB += "(";
1741 Op1->print(OB);
1742 OB += ")[";
1743 Op2->print(OB);
1744 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001745 }
1746};
1747
1748class PostfixExpr : public Node {
1749 const Node *Child;
1750 const StringView Operator;
1751
1752public:
1753 PostfixExpr(const Node *Child_, StringView Operator_)
1754 : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
1755
1756 template<typename Fn> void match(Fn F) const { F(Child, Operator); }
1757
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001758 void printLeft(OutputBuffer &OB) const override {
1759 OB += "(";
1760 Child->print(OB);
1761 OB += ")";
1762 OB += Operator;
Richard Smithc20d1442018-08-20 20:14:49 +00001763 }
1764};
1765
1766class ConditionalExpr : public Node {
1767 const Node *Cond;
1768 const Node *Then;
1769 const Node *Else;
1770
1771public:
1772 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
1773 : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
1774
1775 template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
1776
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001777 void printLeft(OutputBuffer &OB) const override {
1778 OB += "(";
1779 Cond->print(OB);
1780 OB += ") ? (";
1781 Then->print(OB);
1782 OB += ") : (";
1783 Else->print(OB);
1784 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001785 }
1786};
1787
1788class MemberExpr : public Node {
1789 const Node *LHS;
1790 const StringView Kind;
1791 const Node *RHS;
1792
1793public:
1794 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
1795 : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1796
1797 template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
1798
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001799 void printLeft(OutputBuffer &OB) const override {
1800 LHS->print(OB);
1801 OB += Kind;
1802 RHS->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001803 }
1804};
1805
Richard Smith1865d2f2020-10-22 19:29:36 -07001806class SubobjectExpr : public Node {
1807 const Node *Type;
1808 const Node *SubExpr;
1809 StringView Offset;
1810 NodeArray UnionSelectors;
1811 bool OnePastTheEnd;
1812
1813public:
1814 SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_,
1815 NodeArray UnionSelectors_, bool OnePastTheEnd_)
1816 : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_),
1817 UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {}
1818
1819 template<typename Fn> void match(Fn F) const {
1820 F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd);
1821 }
1822
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001823 void printLeft(OutputBuffer &OB) const override {
1824 SubExpr->print(OB);
1825 OB += ".<";
1826 Type->print(OB);
1827 OB += " at offset ";
Richard Smith1865d2f2020-10-22 19:29:36 -07001828 if (Offset.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001829 OB += "0";
Richard Smith1865d2f2020-10-22 19:29:36 -07001830 } else if (Offset[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001831 OB += "-";
1832 OB += Offset.dropFront();
Richard Smith1865d2f2020-10-22 19:29:36 -07001833 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001834 OB += Offset;
Richard Smith1865d2f2020-10-22 19:29:36 -07001835 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001836 OB += ">";
Richard Smith1865d2f2020-10-22 19:29:36 -07001837 }
1838};
1839
Richard Smithc20d1442018-08-20 20:14:49 +00001840class EnclosingExpr : public Node {
1841 const StringView Prefix;
1842 const Node *Infix;
1843 const StringView Postfix;
1844
1845public:
1846 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1847 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1848 Postfix(Postfix_) {}
1849
1850 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1851
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001852 void printLeft(OutputBuffer &OB) const override {
1853 OB += Prefix;
1854 Infix->print(OB);
1855 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +00001856 }
1857};
1858
1859class CastExpr : public Node {
1860 // cast_kind<to>(from)
1861 const StringView CastKind;
1862 const Node *To;
1863 const Node *From;
1864
1865public:
1866 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1867 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1868
1869 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1870
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001871 void printLeft(OutputBuffer &OB) const override {
1872 OB += CastKind;
1873 OB += "<";
1874 To->printLeft(OB);
1875 OB += ">(";
1876 From->printLeft(OB);
1877 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001878 }
1879};
1880
1881class SizeofParamPackExpr : public Node {
1882 const Node *Pack;
1883
1884public:
1885 SizeofParamPackExpr(const Node *Pack_)
1886 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1887
1888 template<typename Fn> void match(Fn F) const { F(Pack); }
1889
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001890 void printLeft(OutputBuffer &OB) const override {
1891 OB += "sizeof...(";
Richard Smithc20d1442018-08-20 20:14:49 +00001892 ParameterPackExpansion PPE(Pack);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001893 PPE.printLeft(OB);
1894 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001895 }
1896};
1897
1898class CallExpr : public Node {
1899 const Node *Callee;
1900 NodeArray Args;
1901
1902public:
1903 CallExpr(const Node *Callee_, NodeArray Args_)
1904 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1905
1906 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1907
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001908 void printLeft(OutputBuffer &OB) const override {
1909 Callee->print(OB);
1910 OB += "(";
1911 Args.printWithComma(OB);
1912 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001913 }
1914};
1915
1916class NewExpr : public Node {
1917 // new (expr_list) type(init_list)
1918 NodeArray ExprList;
1919 Node *Type;
1920 NodeArray InitList;
1921 bool IsGlobal; // ::operator new ?
1922 bool IsArray; // new[] ?
1923public:
1924 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1925 bool IsArray_)
1926 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1927 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1928
1929 template<typename Fn> void match(Fn F) const {
1930 F(ExprList, Type, InitList, IsGlobal, IsArray);
1931 }
1932
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001933 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001934 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001935 OB += "::operator ";
1936 OB += "new";
Richard Smithc20d1442018-08-20 20:14:49 +00001937 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001938 OB += "[]";
1939 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00001940 if (!ExprList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001941 OB += "(";
1942 ExprList.printWithComma(OB);
1943 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001944 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001945 Type->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001946 if (!InitList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001947 OB += "(";
1948 InitList.printWithComma(OB);
1949 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001950 }
Richard Smithc20d1442018-08-20 20:14:49 +00001951 }
1952};
1953
1954class DeleteExpr : public Node {
1955 Node *Op;
1956 bool IsGlobal;
1957 bool IsArray;
1958
1959public:
1960 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1961 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1962
1963 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1964
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001965 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001966 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001967 OB += "::";
1968 OB += "delete";
Richard Smithc20d1442018-08-20 20:14:49 +00001969 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001970 OB += "[] ";
1971 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001972 }
1973};
1974
1975class PrefixExpr : public Node {
1976 StringView Prefix;
1977 Node *Child;
1978
1979public:
1980 PrefixExpr(StringView Prefix_, Node *Child_)
1981 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1982
1983 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1984
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001985 void printLeft(OutputBuffer &OB) const override {
1986 OB += Prefix;
1987 OB += "(";
1988 Child->print(OB);
1989 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001990 }
1991};
1992
1993class FunctionParam : public Node {
1994 StringView Number;
1995
1996public:
1997 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
1998
1999 template<typename Fn> void match(Fn F) const { F(Number); }
2000
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002001 void printLeft(OutputBuffer &OB) const override {
2002 OB += "fp";
2003 OB += Number;
Richard Smithc20d1442018-08-20 20:14:49 +00002004 }
2005};
2006
2007class ConversionExpr : public Node {
2008 const Node *Type;
2009 NodeArray Expressions;
2010
2011public:
2012 ConversionExpr(const Node *Type_, NodeArray Expressions_)
2013 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
2014
2015 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
2016
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002017 void printLeft(OutputBuffer &OB) const override {
2018 OB += "(";
2019 Type->print(OB);
2020 OB += ")(";
2021 Expressions.printWithComma(OB);
2022 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002023 }
2024};
2025
Richard Smith1865d2f2020-10-22 19:29:36 -07002026class PointerToMemberConversionExpr : public Node {
2027 const Node *Type;
2028 const Node *SubExpr;
2029 StringView Offset;
2030
2031public:
2032 PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_,
2033 StringView Offset_)
2034 : Node(KPointerToMemberConversionExpr), Type(Type_), SubExpr(SubExpr_),
2035 Offset(Offset_) {}
2036
2037 template<typename Fn> void match(Fn F) const { F(Type, SubExpr, Offset); }
2038
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002039 void printLeft(OutputBuffer &OB) const override {
2040 OB += "(";
2041 Type->print(OB);
2042 OB += ")(";
2043 SubExpr->print(OB);
2044 OB += ")";
Richard Smith1865d2f2020-10-22 19:29:36 -07002045 }
2046};
2047
Richard Smithc20d1442018-08-20 20:14:49 +00002048class InitListExpr : public Node {
2049 const Node *Ty;
2050 NodeArray Inits;
2051public:
2052 InitListExpr(const Node *Ty_, NodeArray Inits_)
2053 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
2054
2055 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
2056
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002057 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002058 if (Ty)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002059 Ty->print(OB);
2060 OB += '{';
2061 Inits.printWithComma(OB);
2062 OB += '}';
Richard Smithc20d1442018-08-20 20:14:49 +00002063 }
2064};
2065
2066class BracedExpr : public Node {
2067 const Node *Elem;
2068 const Node *Init;
2069 bool IsArray;
2070public:
2071 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
2072 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
2073
2074 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
2075
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002076 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002077 if (IsArray) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002078 OB += '[';
2079 Elem->print(OB);
2080 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002081 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002082 OB += '.';
2083 Elem->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002084 }
2085 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002086 OB += " = ";
2087 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002088 }
2089};
2090
2091class BracedRangeExpr : public Node {
2092 const Node *First;
2093 const Node *Last;
2094 const Node *Init;
2095public:
2096 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
2097 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
2098
2099 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
2100
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002101 void printLeft(OutputBuffer &OB) const override {
2102 OB += '[';
2103 First->print(OB);
2104 OB += " ... ";
2105 Last->print(OB);
2106 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002107 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002108 OB += " = ";
2109 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002110 }
2111};
2112
2113class FoldExpr : public Node {
2114 const Node *Pack, *Init;
2115 StringView OperatorName;
2116 bool IsLeftFold;
2117
2118public:
2119 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
2120 const Node *Init_)
2121 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
2122 IsLeftFold(IsLeftFold_) {}
2123
2124 template<typename Fn> void match(Fn F) const {
2125 F(IsLeftFold, OperatorName, Pack, Init);
2126 }
2127
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002128 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002129 auto PrintPack = [&] {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002130 OB += '(';
2131 ParameterPackExpansion(Pack).print(OB);
2132 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002133 };
2134
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002135 OB += '(';
Richard Smithc20d1442018-08-20 20:14:49 +00002136
2137 if (IsLeftFold) {
2138 // init op ... op pack
2139 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002140 Init->print(OB);
2141 OB += ' ';
2142 OB += OperatorName;
2143 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002144 }
2145 // ... op pack
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002146 OB += "... ";
2147 OB += OperatorName;
2148 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002149 PrintPack();
2150 } else { // !IsLeftFold
2151 // pack op ...
2152 PrintPack();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002153 OB += ' ';
2154 OB += OperatorName;
2155 OB += " ...";
Richard Smithc20d1442018-08-20 20:14:49 +00002156 // pack op ... op init
2157 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002158 OB += ' ';
2159 OB += OperatorName;
2160 OB += ' ';
2161 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002162 }
2163 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002164 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002165 }
2166};
2167
2168class ThrowExpr : public Node {
2169 const Node *Op;
2170
2171public:
2172 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
2173
2174 template<typename Fn> void match(Fn F) const { F(Op); }
2175
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002176 void printLeft(OutputBuffer &OB) const override {
2177 OB += "throw ";
2178 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002179 }
2180};
2181
2182class BoolExpr : public Node {
2183 bool Value;
2184
2185public:
2186 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
2187
2188 template<typename Fn> void match(Fn F) const { F(Value); }
2189
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002190 void printLeft(OutputBuffer &OB) const override {
2191 OB += Value ? StringView("true") : StringView("false");
Richard Smithc20d1442018-08-20 20:14:49 +00002192 }
2193};
2194
Richard Smithdf1c14c2019-09-06 23:53:21 +00002195class StringLiteral : public Node {
2196 const Node *Type;
2197
2198public:
2199 StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
2200
2201 template<typename Fn> void match(Fn F) const { F(Type); }
2202
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002203 void printLeft(OutputBuffer &OB) const override {
2204 OB += "\"<";
2205 Type->print(OB);
2206 OB += ">\"";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002207 }
2208};
2209
2210class LambdaExpr : public Node {
2211 const Node *Type;
2212
Richard Smithdf1c14c2019-09-06 23:53:21 +00002213public:
2214 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2215
2216 template<typename Fn> void match(Fn F) const { F(Type); }
2217
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002218 void printLeft(OutputBuffer &OB) const override {
2219 OB += "[]";
Richard Smithfb917462019-09-09 22:26:04 +00002220 if (Type->getKind() == KClosureTypeName)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002221 static_cast<const ClosureTypeName *>(Type)->printDeclarator(OB);
2222 OB += "{...}";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002223 }
2224};
2225
Erik Pilkington0a170f12020-05-13 14:13:37 -04002226class EnumLiteral : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +00002227 // ty(integer)
2228 const Node *Ty;
2229 StringView Integer;
2230
2231public:
Erik Pilkington0a170f12020-05-13 14:13:37 -04002232 EnumLiteral(const Node *Ty_, StringView Integer_)
2233 : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00002234
2235 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2236
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002237 void printLeft(OutputBuffer &OB) const override {
2238 OB << "(";
2239 Ty->print(OB);
2240 OB << ")";
Erik Pilkington0a170f12020-05-13 14:13:37 -04002241
2242 if (Integer[0] == 'n')
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002243 OB << "-" << Integer.dropFront(1);
Erik Pilkington0a170f12020-05-13 14:13:37 -04002244 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002245 OB << Integer;
Richard Smithc20d1442018-08-20 20:14:49 +00002246 }
2247};
2248
2249class IntegerLiteral : public Node {
2250 StringView Type;
2251 StringView Value;
2252
2253public:
2254 IntegerLiteral(StringView Type_, StringView Value_)
2255 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2256
2257 template<typename Fn> void match(Fn F) const { F(Type, Value); }
2258
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002259 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002260 if (Type.size() > 3) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002261 OB += "(";
2262 OB += Type;
2263 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002264 }
2265
2266 if (Value[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002267 OB += "-";
2268 OB += Value.dropFront(1);
Richard Smithc20d1442018-08-20 20:14:49 +00002269 } else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002270 OB += Value;
Richard Smithc20d1442018-08-20 20:14:49 +00002271
2272 if (Type.size() <= 3)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002273 OB += Type;
Richard Smithc20d1442018-08-20 20:14:49 +00002274 }
2275};
2276
2277template <class Float> struct FloatData;
2278
2279namespace float_literal_impl {
2280constexpr Node::Kind getFloatLiteralKind(float *) {
2281 return Node::KFloatLiteral;
2282}
2283constexpr Node::Kind getFloatLiteralKind(double *) {
2284 return Node::KDoubleLiteral;
2285}
2286constexpr Node::Kind getFloatLiteralKind(long double *) {
2287 return Node::KLongDoubleLiteral;
2288}
2289}
2290
2291template <class Float> class FloatLiteralImpl : public Node {
2292 const StringView Contents;
2293
2294 static constexpr Kind KindForClass =
2295 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2296
2297public:
2298 FloatLiteralImpl(StringView Contents_)
2299 : Node(KindForClass), Contents(Contents_) {}
2300
2301 template<typename Fn> void match(Fn F) const { F(Contents); }
2302
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002303 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002304 const char *first = Contents.begin();
2305 const char *last = Contents.end() + 1;
2306
2307 const size_t N = FloatData<Float>::mangled_size;
2308 if (static_cast<std::size_t>(last - first) > N) {
2309 last = first + N;
2310 union {
2311 Float value;
2312 char buf[sizeof(Float)];
2313 };
2314 const char *t = first;
2315 char *e = buf;
2316 for (; t != last; ++t, ++e) {
2317 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2318 : static_cast<unsigned>(*t - 'a' + 10);
2319 ++t;
2320 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2321 : static_cast<unsigned>(*t - 'a' + 10);
2322 *e = static_cast<char>((d1 << 4) + d0);
2323 }
2324#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2325 std::reverse(buf, e);
2326#endif
2327 char num[FloatData<Float>::max_demangled_size] = {0};
2328 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002329 OB += StringView(num, num + n);
Richard Smithc20d1442018-08-20 20:14:49 +00002330 }
2331 }
2332};
2333
2334using FloatLiteral = FloatLiteralImpl<float>;
2335using DoubleLiteral = FloatLiteralImpl<double>;
2336using LongDoubleLiteral = FloatLiteralImpl<long double>;
2337
2338/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2339/// appropriate derived class.
2340template<typename Fn>
2341void Node::visit(Fn F) const {
2342 switch (K) {
2343#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2344 FOR_EACH_NODE_KIND(CASE)
2345#undef CASE
2346 }
2347 assert(0 && "unknown mangling node kind");
2348}
2349
2350/// Determine the kind of a node from its type.
2351template<typename NodeT> struct NodeKind;
2352#define SPECIALIZATION(X) \
2353 template<> struct NodeKind<X> { \
2354 static constexpr Node::Kind Kind = Node::K##X; \
2355 static constexpr const char *name() { return #X; } \
2356 };
2357FOR_EACH_NODE_KIND(SPECIALIZATION)
2358#undef SPECIALIZATION
2359
2360#undef FOR_EACH_NODE_KIND
2361
Pavel Labathba825192018-10-16 14:29:14 +00002362template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002363 const char *First;
2364 const char *Last;
2365
2366 // Name stack, this is used by the parser to hold temporary names that were
2367 // parsed. The parser collapses multiple names into new nodes to construct
2368 // the AST. Once the parser is finished, names.size() == 1.
2369 PODSmallVector<Node *, 32> Names;
2370
2371 // Substitution table. Itanium supports name substitutions as a means of
2372 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2373 // table.
2374 PODSmallVector<Node *, 32> Subs;
2375
Richard Smithdf1c14c2019-09-06 23:53:21 +00002376 using TemplateParamList = PODSmallVector<Node *, 8>;
2377
2378 class ScopedTemplateParamList {
2379 AbstractManglingParser *Parser;
2380 size_t OldNumTemplateParamLists;
2381 TemplateParamList Params;
2382
2383 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04002384 ScopedTemplateParamList(AbstractManglingParser *TheParser)
2385 : Parser(TheParser),
2386 OldNumTemplateParamLists(TheParser->TemplateParams.size()) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002387 Parser->TemplateParams.push_back(&Params);
2388 }
2389 ~ScopedTemplateParamList() {
2390 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2391 Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2392 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002393 };
2394
Richard Smithc20d1442018-08-20 20:14:49 +00002395 // Template parameter table. Like the above, but referenced like "T42_".
2396 // This has a smaller size compared to Subs and Names because it can be
2397 // stored on the stack.
Richard Smithdf1c14c2019-09-06 23:53:21 +00002398 TemplateParamList OuterTemplateParams;
2399
2400 // Lists of template parameters indexed by template parameter depth,
2401 // referenced like "TL2_4_". If nonempty, element 0 is always
2402 // OuterTemplateParams; inner elements are always template parameter lists of
2403 // lambda expressions. For a generic lambda with no explicit template
2404 // parameter list, the corresponding parameter list pointer will be null.
2405 PODSmallVector<TemplateParamList *, 4> TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00002406
2407 // Set of unresolved forward <template-param> references. These can occur in a
2408 // conversion operator's type, and are resolved in the enclosing <encoding>.
2409 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2410
Richard Smithc20d1442018-08-20 20:14:49 +00002411 bool TryToParseTemplateArgs = true;
2412 bool PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002413 size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2414
2415 unsigned NumSyntheticTemplateParameters[3] = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002416
2417 Alloc ASTAllocator;
2418
Pavel Labathba825192018-10-16 14:29:14 +00002419 AbstractManglingParser(const char *First_, const char *Last_)
2420 : First(First_), Last(Last_) {}
2421
2422 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002423
2424 void reset(const char *First_, const char *Last_) {
2425 First = First_;
2426 Last = Last_;
2427 Names.clear();
2428 Subs.clear();
2429 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002430 ParsingLambdaParamsAtLevel = (size_t)-1;
Richard Smithc20d1442018-08-20 20:14:49 +00002431 TryToParseTemplateArgs = true;
2432 PermitForwardTemplateReferences = false;
Richard Smith9a2307a2019-09-07 00:11:53 +00002433 for (int I = 0; I != 3; ++I)
2434 NumSyntheticTemplateParameters[I] = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00002435 ASTAllocator.reset();
2436 }
2437
Richard Smithb485b352018-08-24 23:30:26 +00002438 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002439 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2440 }
2441
2442 template <class It> NodeArray makeNodeArray(It begin, It end) {
2443 size_t sz = static_cast<size_t>(end - begin);
2444 void *mem = ASTAllocator.allocateNodeArray(sz);
2445 Node **data = new (mem) Node *[sz];
2446 std::copy(begin, end, data);
2447 return NodeArray(data, sz);
2448 }
2449
2450 NodeArray popTrailingNodeArray(size_t FromPosition) {
2451 assert(FromPosition <= Names.size());
2452 NodeArray res =
2453 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2454 Names.dropBack(FromPosition);
2455 return res;
2456 }
2457
2458 bool consumeIf(StringView S) {
2459 if (StringView(First, Last).startsWith(S)) {
2460 First += S.size();
2461 return true;
2462 }
2463 return false;
2464 }
2465
2466 bool consumeIf(char C) {
2467 if (First != Last && *First == C) {
2468 ++First;
2469 return true;
2470 }
2471 return false;
2472 }
2473
2474 char consume() { return First != Last ? *First++ : '\0'; }
2475
2476 char look(unsigned Lookahead = 0) {
2477 if (static_cast<size_t>(Last - First) <= Lookahead)
2478 return '\0';
2479 return First[Lookahead];
2480 }
2481
2482 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2483
2484 StringView parseNumber(bool AllowNegative = false);
2485 Qualifiers parseCVQualifiers();
2486 bool parsePositiveInteger(size_t *Out);
2487 StringView parseBareSourceName();
2488
2489 bool parseSeqId(size_t *Out);
2490 Node *parseSubstitution();
2491 Node *parseTemplateParam();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002492 Node *parseTemplateParamDecl();
Richard Smithc20d1442018-08-20 20:14:49 +00002493 Node *parseTemplateArgs(bool TagTemplates = false);
2494 Node *parseTemplateArg();
2495
2496 /// Parse the <expr> production.
2497 Node *parseExpr();
2498 Node *parsePrefixExpr(StringView Kind);
2499 Node *parseBinaryExpr(StringView Kind);
2500 Node *parseIntegerLiteral(StringView Lit);
2501 Node *parseExprPrimary();
2502 template <class Float> Node *parseFloatingLiteral();
2503 Node *parseFunctionParam();
2504 Node *parseNewExpr();
2505 Node *parseConversionExpr();
2506 Node *parseBracedExpr();
2507 Node *parseFoldExpr();
Richard Smith1865d2f2020-10-22 19:29:36 -07002508 Node *parsePointerToMemberConversionExpr();
2509 Node *parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00002510
2511 /// Parse the <type> production.
2512 Node *parseType();
2513 Node *parseFunctionType();
2514 Node *parseVectorType();
2515 Node *parseDecltype();
2516 Node *parseArrayType();
2517 Node *parsePointerToMemberType();
2518 Node *parseClassEnumType();
2519 Node *parseQualifiedType();
2520
2521 Node *parseEncoding();
2522 bool parseCallOffset();
2523 Node *parseSpecialName();
2524
2525 /// Holds some extra information about a <name> that is being parsed. This
2526 /// information is only pertinent if the <name> refers to an <encoding>.
2527 struct NameState {
2528 bool CtorDtorConversion = false;
2529 bool EndsWithTemplateArgs = false;
2530 Qualifiers CVQualifiers = QualNone;
2531 FunctionRefQual ReferenceQualifier = FrefQualNone;
2532 size_t ForwardTemplateRefsBegin;
2533
Pavel Labathba825192018-10-16 14:29:14 +00002534 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002535 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2536 };
2537
2538 bool resolveForwardTemplateRefs(NameState &State) {
2539 size_t I = State.ForwardTemplateRefsBegin;
2540 size_t E = ForwardTemplateRefs.size();
2541 for (; I < E; ++I) {
2542 size_t Idx = ForwardTemplateRefs[I]->Index;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002543 if (TemplateParams.empty() || !TemplateParams[0] ||
2544 Idx >= TemplateParams[0]->size())
Richard Smithc20d1442018-08-20 20:14:49 +00002545 return true;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002546 ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
Richard Smithc20d1442018-08-20 20:14:49 +00002547 }
2548 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2549 return false;
2550 }
2551
2552 /// Parse the <name> production>
2553 Node *parseName(NameState *State = nullptr);
2554 Node *parseLocalName(NameState *State);
2555 Node *parseOperatorName(NameState *State);
2556 Node *parseUnqualifiedName(NameState *State);
2557 Node *parseUnnamedTypeName(NameState *State);
2558 Node *parseSourceName(NameState *State);
2559 Node *parseUnscopedName(NameState *State);
2560 Node *parseNestedName(NameState *State);
2561 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2562
2563 Node *parseAbiTags(Node *N);
2564
2565 /// Parse the <unresolved-name> production.
2566 Node *parseUnresolvedName();
2567 Node *parseSimpleId();
2568 Node *parseBaseUnresolvedName();
2569 Node *parseUnresolvedType();
2570 Node *parseDestructorName();
2571
2572 /// Top-level entry point into the parser.
2573 Node *parse();
2574};
2575
2576const char* parse_discriminator(const char* first, const char* last);
2577
2578// <name> ::= <nested-name> // N
2579// ::= <local-name> # See Scope Encoding below // Z
2580// ::= <unscoped-template-name> <template-args>
2581// ::= <unscoped-name>
2582//
2583// <unscoped-template-name> ::= <unscoped-name>
2584// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002585template <typename Derived, typename Alloc>
2586Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002587 consumeIf('L'); // extension
2588
2589 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002590 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002591 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002592 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002593
2594 // ::= <unscoped-template-name> <template-args>
2595 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00002596 Node *S = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00002597 if (S == nullptr)
2598 return nullptr;
2599 if (look() != 'I')
2600 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002601 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002602 if (TA == nullptr)
2603 return nullptr;
2604 if (State) State->EndsWithTemplateArgs = true;
2605 return make<NameWithTemplateArgs>(S, TA);
2606 }
2607
Pavel Labathba825192018-10-16 14:29:14 +00002608 Node *N = getDerived().parseUnscopedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002609 if (N == nullptr)
2610 return nullptr;
2611 // ::= <unscoped-template-name> <template-args>
2612 if (look() == 'I') {
2613 Subs.push_back(N);
Pavel Labathba825192018-10-16 14:29:14 +00002614 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002615 if (TA == nullptr)
2616 return nullptr;
2617 if (State) State->EndsWithTemplateArgs = true;
2618 return make<NameWithTemplateArgs>(N, TA);
2619 }
2620 // ::= <unscoped-name>
2621 return N;
2622}
2623
2624// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2625// := Z <function encoding> E s [<discriminator>]
2626// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002627template <typename Derived, typename Alloc>
2628Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002629 if (!consumeIf('Z'))
2630 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002631 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002632 if (Encoding == nullptr || !consumeIf('E'))
2633 return nullptr;
2634
2635 if (consumeIf('s')) {
2636 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002637 auto *StringLitName = make<NameType>("string literal");
2638 if (!StringLitName)
2639 return nullptr;
2640 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002641 }
2642
2643 if (consumeIf('d')) {
2644 parseNumber(true);
2645 if (!consumeIf('_'))
2646 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002647 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002648 if (N == nullptr)
2649 return nullptr;
2650 return make<LocalName>(Encoding, N);
2651 }
2652
Pavel Labathba825192018-10-16 14:29:14 +00002653 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002654 if (Entity == nullptr)
2655 return nullptr;
2656 First = parse_discriminator(First, Last);
2657 return make<LocalName>(Encoding, Entity);
2658}
2659
2660// <unscoped-name> ::= <unqualified-name>
2661// ::= St <unqualified-name> # ::std::
2662// extension ::= StL<unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00002663template <typename Derived, typename Alloc>
2664Node *
2665AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
2666 if (consumeIf("StL") || consumeIf("St")) {
2667 Node *R = getDerived().parseUnqualifiedName(State);
2668 if (R == nullptr)
2669 return nullptr;
2670 return make<StdQualifiedName>(R);
2671 }
2672 return getDerived().parseUnqualifiedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002673}
2674
2675// <unqualified-name> ::= <operator-name> [abi-tags]
2676// ::= <ctor-dtor-name>
2677// ::= <source-name>
2678// ::= <unnamed-type-name>
2679// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002680template <typename Derived, typename Alloc>
2681Node *
2682AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002683 // <ctor-dtor-name>s are special-cased in parseNestedName().
2684 Node *Result;
2685 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002686 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002687 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002688 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002689 else if (consumeIf("DC")) {
2690 size_t BindingsBegin = Names.size();
2691 do {
Pavel Labathba825192018-10-16 14:29:14 +00002692 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002693 if (Binding == nullptr)
2694 return nullptr;
2695 Names.push_back(Binding);
2696 } while (!consumeIf('E'));
2697 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2698 } else
Pavel Labathba825192018-10-16 14:29:14 +00002699 Result = getDerived().parseOperatorName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002700 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002701 Result = getDerived().parseAbiTags(Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002702 return Result;
2703}
2704
2705// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2706// ::= <closure-type-name>
2707//
2708// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2709//
2710// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002711template <typename Derived, typename Alloc>
2712Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002713AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2714 // <template-params> refer to the innermost <template-args>. Clear out any
2715 // outer args that we may have inserted into TemplateParams.
2716 if (State != nullptr)
2717 TemplateParams.clear();
2718
Richard Smithc20d1442018-08-20 20:14:49 +00002719 if (consumeIf("Ut")) {
2720 StringView Count = parseNumber();
2721 if (!consumeIf('_'))
2722 return nullptr;
2723 return make<UnnamedTypeName>(Count);
2724 }
2725 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002726 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2727 TemplateParams.size());
2728 ScopedTemplateParamList LambdaTemplateParams(this);
2729
2730 size_t ParamsBegin = Names.size();
2731 while (look() == 'T' &&
2732 StringView("yptn").find(look(1)) != StringView::npos) {
2733 Node *T = parseTemplateParamDecl();
2734 if (!T)
2735 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002736 Names.push_back(T);
2737 }
2738 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2739
2740 // FIXME: If TempParams is empty and none of the function parameters
2741 // includes 'auto', we should remove LambdaTemplateParams from the
2742 // TemplateParams list. Unfortunately, we don't find out whether there are
2743 // any 'auto' parameters until too late in an example such as:
2744 //
2745 // template<typename T> void f(
2746 // decltype([](decltype([]<typename T>(T v) {}),
2747 // auto) {})) {}
2748 // template<typename T> void f(
2749 // decltype([](decltype([]<typename T>(T w) {}),
2750 // int) {})) {}
2751 //
2752 // Here, the type of v is at level 2 but the type of w is at level 1. We
2753 // don't find this out until we encounter the type of the next parameter.
2754 //
2755 // However, compilers can't actually cope with the former example in
2756 // practice, and it's likely to be made ill-formed in future, so we don't
2757 // need to support it here.
2758 //
2759 // If we encounter an 'auto' in the function parameter types, we will
2760 // recreate a template parameter scope for it, but any intervening lambdas
2761 // will be parsed in the 'wrong' template parameter depth.
2762 if (TempParams.empty())
2763 TemplateParams.pop_back();
2764
Richard Smithc20d1442018-08-20 20:14:49 +00002765 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002766 do {
Pavel Labathba825192018-10-16 14:29:14 +00002767 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002768 if (P == nullptr)
2769 return nullptr;
2770 Names.push_back(P);
2771 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002772 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002773 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2774
Richard Smithc20d1442018-08-20 20:14:49 +00002775 StringView Count = parseNumber();
2776 if (!consumeIf('_'))
2777 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002778 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002779 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002780 if (consumeIf("Ub")) {
2781 (void)parseNumber();
2782 if (!consumeIf('_'))
2783 return nullptr;
2784 return make<NameType>("'block-literal'");
2785 }
Richard Smithc20d1442018-08-20 20:14:49 +00002786 return nullptr;
2787}
2788
2789// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002790template <typename Derived, typename Alloc>
2791Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002792 size_t Length = 0;
2793 if (parsePositiveInteger(&Length))
2794 return nullptr;
2795 if (numLeft() < Length || Length == 0)
2796 return nullptr;
2797 StringView Name(First, First + Length);
2798 First += Length;
2799 if (Name.startsWith("_GLOBAL__N"))
2800 return make<NameType>("(anonymous namespace)");
2801 return make<NameType>(Name);
2802}
2803
2804// <operator-name> ::= aa # &&
2805// ::= ad # & (unary)
2806// ::= an # &
2807// ::= aN # &=
2808// ::= aS # =
2809// ::= cl # ()
2810// ::= cm # ,
2811// ::= co # ~
2812// ::= cv <type> # (cast)
2813// ::= da # delete[]
2814// ::= de # * (unary)
2815// ::= dl # delete
2816// ::= dv # /
2817// ::= dV # /=
2818// ::= eo # ^
2819// ::= eO # ^=
2820// ::= eq # ==
2821// ::= ge # >=
2822// ::= gt # >
2823// ::= ix # []
2824// ::= le # <=
2825// ::= li <source-name> # operator ""
2826// ::= ls # <<
2827// ::= lS # <<=
2828// ::= lt # <
2829// ::= mi # -
2830// ::= mI # -=
2831// ::= ml # *
2832// ::= mL # *=
2833// ::= mm # -- (postfix in <expression> context)
2834// ::= na # new[]
2835// ::= ne # !=
2836// ::= ng # - (unary)
2837// ::= nt # !
2838// ::= nw # new
2839// ::= oo # ||
2840// ::= or # |
2841// ::= oR # |=
2842// ::= pm # ->*
2843// ::= pl # +
2844// ::= pL # +=
2845// ::= pp # ++ (postfix in <expression> context)
2846// ::= ps # + (unary)
2847// ::= pt # ->
2848// ::= qu # ?
2849// ::= rm # %
2850// ::= rM # %=
2851// ::= rs # >>
2852// ::= rS # >>=
2853// ::= ss # <=> C++2a
2854// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002855template <typename Derived, typename Alloc>
2856Node *
2857AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002858 switch (look()) {
2859 case 'a':
2860 switch (look(1)) {
2861 case 'a':
2862 First += 2;
2863 return make<NameType>("operator&&");
2864 case 'd':
2865 case 'n':
2866 First += 2;
2867 return make<NameType>("operator&");
2868 case 'N':
2869 First += 2;
2870 return make<NameType>("operator&=");
2871 case 'S':
2872 First += 2;
2873 return make<NameType>("operator=");
2874 }
2875 return nullptr;
2876 case 'c':
2877 switch (look(1)) {
2878 case 'l':
2879 First += 2;
2880 return make<NameType>("operator()");
2881 case 'm':
2882 First += 2;
2883 return make<NameType>("operator,");
2884 case 'o':
2885 First += 2;
2886 return make<NameType>("operator~");
2887 // ::= cv <type> # (cast)
2888 case 'v': {
2889 First += 2;
2890 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2891 // If we're parsing an encoding, State != nullptr and the conversion
2892 // operators' <type> could have a <template-param> that refers to some
2893 // <template-arg>s further ahead in the mangled name.
2894 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2895 PermitForwardTemplateReferences ||
2896 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002897 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002898 if (Ty == nullptr)
2899 return nullptr;
2900 if (State) State->CtorDtorConversion = true;
2901 return make<ConversionOperatorType>(Ty);
2902 }
2903 }
2904 return nullptr;
2905 case 'd':
2906 switch (look(1)) {
2907 case 'a':
2908 First += 2;
2909 return make<NameType>("operator delete[]");
2910 case 'e':
2911 First += 2;
2912 return make<NameType>("operator*");
2913 case 'l':
2914 First += 2;
2915 return make<NameType>("operator delete");
2916 case 'v':
2917 First += 2;
2918 return make<NameType>("operator/");
2919 case 'V':
2920 First += 2;
2921 return make<NameType>("operator/=");
2922 }
2923 return nullptr;
2924 case 'e':
2925 switch (look(1)) {
2926 case 'o':
2927 First += 2;
2928 return make<NameType>("operator^");
2929 case 'O':
2930 First += 2;
2931 return make<NameType>("operator^=");
2932 case 'q':
2933 First += 2;
2934 return make<NameType>("operator==");
2935 }
2936 return nullptr;
2937 case 'g':
2938 switch (look(1)) {
2939 case 'e':
2940 First += 2;
2941 return make<NameType>("operator>=");
2942 case 't':
2943 First += 2;
2944 return make<NameType>("operator>");
2945 }
2946 return nullptr;
2947 case 'i':
2948 if (look(1) == 'x') {
2949 First += 2;
2950 return make<NameType>("operator[]");
2951 }
2952 return nullptr;
2953 case 'l':
2954 switch (look(1)) {
2955 case 'e':
2956 First += 2;
2957 return make<NameType>("operator<=");
2958 // ::= li <source-name> # operator ""
2959 case 'i': {
2960 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002961 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002962 if (SN == nullptr)
2963 return nullptr;
2964 return make<LiteralOperator>(SN);
2965 }
2966 case 's':
2967 First += 2;
2968 return make<NameType>("operator<<");
2969 case 'S':
2970 First += 2;
2971 return make<NameType>("operator<<=");
2972 case 't':
2973 First += 2;
2974 return make<NameType>("operator<");
2975 }
2976 return nullptr;
2977 case 'm':
2978 switch (look(1)) {
2979 case 'i':
2980 First += 2;
2981 return make<NameType>("operator-");
2982 case 'I':
2983 First += 2;
2984 return make<NameType>("operator-=");
2985 case 'l':
2986 First += 2;
2987 return make<NameType>("operator*");
2988 case 'L':
2989 First += 2;
2990 return make<NameType>("operator*=");
2991 case 'm':
2992 First += 2;
2993 return make<NameType>("operator--");
2994 }
2995 return nullptr;
2996 case 'n':
2997 switch (look(1)) {
2998 case 'a':
2999 First += 2;
3000 return make<NameType>("operator new[]");
3001 case 'e':
3002 First += 2;
3003 return make<NameType>("operator!=");
3004 case 'g':
3005 First += 2;
3006 return make<NameType>("operator-");
3007 case 't':
3008 First += 2;
3009 return make<NameType>("operator!");
3010 case 'w':
3011 First += 2;
3012 return make<NameType>("operator new");
3013 }
3014 return nullptr;
3015 case 'o':
3016 switch (look(1)) {
3017 case 'o':
3018 First += 2;
3019 return make<NameType>("operator||");
3020 case 'r':
3021 First += 2;
3022 return make<NameType>("operator|");
3023 case 'R':
3024 First += 2;
3025 return make<NameType>("operator|=");
3026 }
3027 return nullptr;
3028 case 'p':
3029 switch (look(1)) {
3030 case 'm':
3031 First += 2;
3032 return make<NameType>("operator->*");
3033 case 'l':
3034 First += 2;
3035 return make<NameType>("operator+");
3036 case 'L':
3037 First += 2;
3038 return make<NameType>("operator+=");
3039 case 'p':
3040 First += 2;
3041 return make<NameType>("operator++");
3042 case 's':
3043 First += 2;
3044 return make<NameType>("operator+");
3045 case 't':
3046 First += 2;
3047 return make<NameType>("operator->");
3048 }
3049 return nullptr;
3050 case 'q':
3051 if (look(1) == 'u') {
3052 First += 2;
3053 return make<NameType>("operator?");
3054 }
3055 return nullptr;
3056 case 'r':
3057 switch (look(1)) {
3058 case 'm':
3059 First += 2;
3060 return make<NameType>("operator%");
3061 case 'M':
3062 First += 2;
3063 return make<NameType>("operator%=");
3064 case 's':
3065 First += 2;
3066 return make<NameType>("operator>>");
3067 case 'S':
3068 First += 2;
3069 return make<NameType>("operator>>=");
3070 }
3071 return nullptr;
3072 case 's':
3073 if (look(1) == 's') {
3074 First += 2;
3075 return make<NameType>("operator<=>");
3076 }
3077 return nullptr;
3078 // ::= v <digit> <source-name> # vendor extended operator
3079 case 'v':
3080 if (std::isdigit(look(1))) {
3081 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003082 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003083 if (SN == nullptr)
3084 return nullptr;
3085 return make<ConversionOperatorType>(SN);
3086 }
3087 return nullptr;
3088 }
3089 return nullptr;
3090}
3091
3092// <ctor-dtor-name> ::= C1 # complete object constructor
3093// ::= C2 # base object constructor
3094// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003095// extension ::= C4 # gcc old-style "[unified]" constructor
3096// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003097// ::= D0 # deleting destructor
3098// ::= D1 # complete object destructor
3099// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003100// extension ::= D4 # gcc old-style "[unified]" destructor
3101// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003102template <typename Derived, typename Alloc>
3103Node *
3104AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3105 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003106 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3107 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3108 switch (SSK) {
3109 case SpecialSubKind::string:
3110 case SpecialSubKind::istream:
3111 case SpecialSubKind::ostream:
3112 case SpecialSubKind::iostream:
3113 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003114 if (!SoFar)
3115 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003116 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003117 default:
3118 break;
3119 }
3120 }
3121
3122 if (consumeIf('C')) {
3123 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003124 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3125 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003126 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003127 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003128 ++First;
3129 if (State) State->CtorDtorConversion = true;
3130 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003131 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003132 return nullptr;
3133 }
Nico Weber29294792019-04-03 23:14:33 +00003134 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003135 }
3136
Nico Weber29294792019-04-03 23:14:33 +00003137 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3138 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003139 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003140 First += 2;
3141 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003142 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003143 }
3144
3145 return nullptr;
3146}
3147
3148// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3149// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3150//
3151// <prefix> ::= <prefix> <unqualified-name>
3152// ::= <template-prefix> <template-args>
3153// ::= <template-param>
3154// ::= <decltype>
3155// ::= # empty
3156// ::= <substitution>
3157// ::= <prefix> <data-member-prefix>
3158// extension ::= L
3159//
3160// <data-member-prefix> := <member source-name> [<template-args>] M
3161//
3162// <template-prefix> ::= <prefix> <template unqualified-name>
3163// ::= <template-param>
3164// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003165template <typename Derived, typename Alloc>
3166Node *
3167AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003168 if (!consumeIf('N'))
3169 return nullptr;
3170
3171 Qualifiers CVTmp = parseCVQualifiers();
3172 if (State) State->CVQualifiers = CVTmp;
3173
3174 if (consumeIf('O')) {
3175 if (State) State->ReferenceQualifier = FrefQualRValue;
3176 } else if (consumeIf('R')) {
3177 if (State) State->ReferenceQualifier = FrefQualLValue;
3178 } else
3179 if (State) State->ReferenceQualifier = FrefQualNone;
3180
3181 Node *SoFar = nullptr;
3182 auto PushComponent = [&](Node *Comp) {
Richard Smithb485b352018-08-24 23:30:26 +00003183 if (!Comp) return false;
Richard Smithc20d1442018-08-20 20:14:49 +00003184 if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
3185 else SoFar = Comp;
3186 if (State) State->EndsWithTemplateArgs = false;
Richard Smithb485b352018-08-24 23:30:26 +00003187 return SoFar != nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003188 };
3189
Richard Smithb485b352018-08-24 23:30:26 +00003190 if (consumeIf("St")) {
Richard Smithc20d1442018-08-20 20:14:49 +00003191 SoFar = make<NameType>("std");
Richard Smithb485b352018-08-24 23:30:26 +00003192 if (!SoFar)
3193 return nullptr;
3194 }
Richard Smithc20d1442018-08-20 20:14:49 +00003195
3196 while (!consumeIf('E')) {
3197 consumeIf('L'); // extension
3198
3199 // <data-member-prefix> := <member source-name> [<template-args>] M
3200 if (consumeIf('M')) {
3201 if (SoFar == nullptr)
3202 return nullptr;
3203 continue;
3204 }
3205
3206 // ::= <template-param>
3207 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003208 if (!PushComponent(getDerived().parseTemplateParam()))
Richard Smithc20d1442018-08-20 20:14:49 +00003209 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003210 Subs.push_back(SoFar);
3211 continue;
3212 }
3213
3214 // ::= <template-prefix> <template-args>
3215 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003216 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003217 if (TA == nullptr || SoFar == nullptr)
3218 return nullptr;
3219 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003220 if (!SoFar)
3221 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003222 if (State) State->EndsWithTemplateArgs = true;
3223 Subs.push_back(SoFar);
3224 continue;
3225 }
3226
3227 // ::= <decltype>
3228 if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
Pavel Labathba825192018-10-16 14:29:14 +00003229 if (!PushComponent(getDerived().parseDecltype()))
Richard Smithc20d1442018-08-20 20:14:49 +00003230 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003231 Subs.push_back(SoFar);
3232 continue;
3233 }
3234
3235 // ::= <substitution>
3236 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00003237 Node *S = getDerived().parseSubstitution();
Richard Smithb485b352018-08-24 23:30:26 +00003238 if (!PushComponent(S))
Richard Smithc20d1442018-08-20 20:14:49 +00003239 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003240 if (SoFar != S)
3241 Subs.push_back(S);
3242 continue;
3243 }
3244
3245 // Parse an <unqualified-name> thats actually a <ctor-dtor-name>.
3246 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
3247 if (SoFar == nullptr)
3248 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003249 if (!PushComponent(getDerived().parseCtorDtorName(SoFar, State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003250 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003251 SoFar = getDerived().parseAbiTags(SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00003252 if (SoFar == nullptr)
3253 return nullptr;
3254 Subs.push_back(SoFar);
3255 continue;
3256 }
3257
3258 // ::= <prefix> <unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00003259 if (!PushComponent(getDerived().parseUnqualifiedName(State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003260 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003261 Subs.push_back(SoFar);
3262 }
3263
3264 if (SoFar == nullptr || Subs.empty())
3265 return nullptr;
3266
3267 Subs.pop_back();
3268 return SoFar;
3269}
3270
3271// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003272template <typename Derived, typename Alloc>
3273Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3274 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003275 if (SN == nullptr)
3276 return nullptr;
3277 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003278 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003279 if (TA == nullptr)
3280 return nullptr;
3281 return make<NameWithTemplateArgs>(SN, TA);
3282 }
3283 return SN;
3284}
3285
3286// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3287// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003288template <typename Derived, typename Alloc>
3289Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003290 Node *Result;
3291 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003292 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003293 else
Pavel Labathba825192018-10-16 14:29:14 +00003294 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003295 if (Result == nullptr)
3296 return nullptr;
3297 return make<DtorName>(Result);
3298}
3299
3300// <unresolved-type> ::= <template-param>
3301// ::= <decltype>
3302// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003303template <typename Derived, typename Alloc>
3304Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003305 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003306 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003307 if (TP == nullptr)
3308 return nullptr;
3309 Subs.push_back(TP);
3310 return TP;
3311 }
3312 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003313 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003314 if (DT == nullptr)
3315 return nullptr;
3316 Subs.push_back(DT);
3317 return DT;
3318 }
Pavel Labathba825192018-10-16 14:29:14 +00003319 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003320}
3321
3322// <base-unresolved-name> ::= <simple-id> # unresolved name
3323// extension ::= <operator-name> # unresolved operator-function-id
3324// extension ::= <operator-name> <template-args> # unresolved operator template-id
3325// ::= on <operator-name> # unresolved operator-function-id
3326// ::= on <operator-name> <template-args> # unresolved operator template-id
3327// ::= dn <destructor-name> # destructor or pseudo-destructor;
3328// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003329template <typename Derived, typename Alloc>
3330Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003331 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003332 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003333
3334 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003335 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003336
3337 consumeIf("on");
3338
Pavel Labathba825192018-10-16 14:29:14 +00003339 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003340 if (Oper == nullptr)
3341 return nullptr;
3342 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003343 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003344 if (TA == nullptr)
3345 return nullptr;
3346 return make<NameWithTemplateArgs>(Oper, TA);
3347 }
3348 return Oper;
3349}
3350
3351// <unresolved-name>
3352// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3353// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3354// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3355// # A::x, N::y, A<T>::z; "gs" means leading "::"
3356// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3357// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3358// # T::N::x /decltype(p)::N::x
3359// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3360//
3361// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003362template <typename Derived, typename Alloc>
3363Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003364 Node *SoFar = nullptr;
3365
3366 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3367 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3368 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003369 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003370 if (SoFar == nullptr)
3371 return nullptr;
3372
3373 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003374 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003375 if (TA == nullptr)
3376 return nullptr;
3377 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003378 if (!SoFar)
3379 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003380 }
3381
3382 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003383 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003384 if (Qual == nullptr)
3385 return nullptr;
3386 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003387 if (!SoFar)
3388 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003389 }
3390
Pavel Labathba825192018-10-16 14:29:14 +00003391 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003392 if (Base == nullptr)
3393 return nullptr;
3394 return make<QualifiedName>(SoFar, Base);
3395 }
3396
3397 bool Global = consumeIf("gs");
3398
3399 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3400 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003401 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003402 if (SoFar == nullptr)
3403 return nullptr;
3404 if (Global)
3405 SoFar = make<GlobalQualifiedName>(SoFar);
3406 return SoFar;
3407 }
3408
3409 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3410 if (std::isdigit(look())) {
3411 do {
Pavel Labathba825192018-10-16 14:29:14 +00003412 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003413 if (Qual == nullptr)
3414 return nullptr;
3415 if (SoFar)
3416 SoFar = make<QualifiedName>(SoFar, Qual);
3417 else if (Global)
3418 SoFar = make<GlobalQualifiedName>(Qual);
3419 else
3420 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003421 if (!SoFar)
3422 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003423 } while (!consumeIf('E'));
3424 }
3425 // sr <unresolved-type> <base-unresolved-name>
3426 // sr <unresolved-type> <template-args> <base-unresolved-name>
3427 else {
Pavel Labathba825192018-10-16 14:29:14 +00003428 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003429 if (SoFar == nullptr)
3430 return nullptr;
3431
3432 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003433 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003434 if (TA == nullptr)
3435 return nullptr;
3436 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003437 if (!SoFar)
3438 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003439 }
3440 }
3441
3442 assert(SoFar != nullptr);
3443
Pavel Labathba825192018-10-16 14:29:14 +00003444 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003445 if (Base == nullptr)
3446 return nullptr;
3447 return make<QualifiedName>(SoFar, Base);
3448}
3449
3450// <abi-tags> ::= <abi-tag> [<abi-tags>]
3451// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003452template <typename Derived, typename Alloc>
3453Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003454 while (consumeIf('B')) {
3455 StringView SN = parseBareSourceName();
3456 if (SN.empty())
3457 return nullptr;
3458 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003459 if (!N)
3460 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003461 }
3462 return N;
3463}
3464
3465// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003466template <typename Alloc, typename Derived>
3467StringView
3468AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003469 const char *Tmp = First;
3470 if (AllowNegative)
3471 consumeIf('n');
3472 if (numLeft() == 0 || !std::isdigit(*First))
3473 return StringView();
3474 while (numLeft() != 0 && std::isdigit(*First))
3475 ++First;
3476 return StringView(Tmp, First);
3477}
3478
3479// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003480template <typename Alloc, typename Derived>
3481bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003482 *Out = 0;
3483 if (look() < '0' || look() > '9')
3484 return true;
3485 while (look() >= '0' && look() <= '9') {
3486 *Out *= 10;
3487 *Out += static_cast<size_t>(consume() - '0');
3488 }
3489 return false;
3490}
3491
Pavel Labathba825192018-10-16 14:29:14 +00003492template <typename Alloc, typename Derived>
3493StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003494 size_t Int = 0;
3495 if (parsePositiveInteger(&Int) || numLeft() < Int)
3496 return StringView();
3497 StringView R(First, First + Int);
3498 First += Int;
3499 return R;
3500}
3501
3502// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3503//
3504// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3505// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3506// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3507//
3508// <ref-qualifier> ::= R # & ref-qualifier
3509// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003510template <typename Derived, typename Alloc>
3511Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003512 Qualifiers CVQuals = parseCVQualifiers();
3513
3514 Node *ExceptionSpec = nullptr;
3515 if (consumeIf("Do")) {
3516 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003517 if (!ExceptionSpec)
3518 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003519 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003520 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003521 if (E == nullptr || !consumeIf('E'))
3522 return nullptr;
3523 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003524 if (!ExceptionSpec)
3525 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003526 } else if (consumeIf("Dw")) {
3527 size_t SpecsBegin = Names.size();
3528 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003529 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003530 if (T == nullptr)
3531 return nullptr;
3532 Names.push_back(T);
3533 }
3534 ExceptionSpec =
3535 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003536 if (!ExceptionSpec)
3537 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003538 }
3539
3540 consumeIf("Dx"); // transaction safe
3541
3542 if (!consumeIf('F'))
3543 return nullptr;
3544 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003545 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003546 if (ReturnType == nullptr)
3547 return nullptr;
3548
3549 FunctionRefQual ReferenceQualifier = FrefQualNone;
3550 size_t ParamsBegin = Names.size();
3551 while (true) {
3552 if (consumeIf('E'))
3553 break;
3554 if (consumeIf('v'))
3555 continue;
3556 if (consumeIf("RE")) {
3557 ReferenceQualifier = FrefQualLValue;
3558 break;
3559 }
3560 if (consumeIf("OE")) {
3561 ReferenceQualifier = FrefQualRValue;
3562 break;
3563 }
Pavel Labathba825192018-10-16 14:29:14 +00003564 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003565 if (T == nullptr)
3566 return nullptr;
3567 Names.push_back(T);
3568 }
3569
3570 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3571 return make<FunctionType>(ReturnType, Params, CVQuals,
3572 ReferenceQualifier, ExceptionSpec);
3573}
3574
3575// extension:
3576// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3577// ::= Dv [<dimension expression>] _ <element type>
3578// <extended element type> ::= <element type>
3579// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003580template <typename Derived, typename Alloc>
3581Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003582 if (!consumeIf("Dv"))
3583 return nullptr;
3584 if (look() >= '1' && look() <= '9') {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003585 Node *DimensionNumber = make<NameType>(parseNumber());
3586 if (!DimensionNumber)
3587 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003588 if (!consumeIf('_'))
3589 return nullptr;
3590 if (consumeIf('p'))
3591 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003592 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003593 if (ElemType == nullptr)
3594 return nullptr;
3595 return make<VectorType>(ElemType, DimensionNumber);
3596 }
3597
3598 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003599 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003600 if (!DimExpr)
3601 return nullptr;
3602 if (!consumeIf('_'))
3603 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003604 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003605 if (!ElemType)
3606 return nullptr;
3607 return make<VectorType>(ElemType, DimExpr);
3608 }
Pavel Labathba825192018-10-16 14:29:14 +00003609 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003610 if (!ElemType)
3611 return nullptr;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003612 return make<VectorType>(ElemType, /*Dimension=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003613}
3614
3615// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3616// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003617template <typename Derived, typename Alloc>
3618Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003619 if (!consumeIf('D'))
3620 return nullptr;
3621 if (!consumeIf('t') && !consumeIf('T'))
3622 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003623 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003624 if (E == nullptr)
3625 return nullptr;
3626 if (!consumeIf('E'))
3627 return nullptr;
3628 return make<EnclosingExpr>("decltype(", E, ")");
3629}
3630
3631// <array-type> ::= A <positive dimension number> _ <element type>
3632// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003633template <typename Derived, typename Alloc>
3634Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003635 if (!consumeIf('A'))
3636 return nullptr;
3637
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003638 Node *Dimension = nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003639
Richard Smithc20d1442018-08-20 20:14:49 +00003640 if (std::isdigit(look())) {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003641 Dimension = make<NameType>(parseNumber());
3642 if (!Dimension)
3643 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003644 if (!consumeIf('_'))
3645 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003646 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003647 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003648 if (DimExpr == nullptr)
3649 return nullptr;
3650 if (!consumeIf('_'))
3651 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003652 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003653 }
3654
Pavel Labathba825192018-10-16 14:29:14 +00003655 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003656 if (Ty == nullptr)
3657 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003658 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003659}
3660
3661// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003662template <typename Derived, typename Alloc>
3663Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003664 if (!consumeIf('M'))
3665 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003666 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003667 if (ClassType == nullptr)
3668 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003669 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003670 if (MemberType == nullptr)
3671 return nullptr;
3672 return make<PointerToMemberType>(ClassType, MemberType);
3673}
3674
3675// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3676// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3677// ::= Tu <name> # dependent elaborated type specifier using 'union'
3678// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003679template <typename Derived, typename Alloc>
3680Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003681 StringView ElabSpef;
3682 if (consumeIf("Ts"))
3683 ElabSpef = "struct";
3684 else if (consumeIf("Tu"))
3685 ElabSpef = "union";
3686 else if (consumeIf("Te"))
3687 ElabSpef = "enum";
3688
Pavel Labathba825192018-10-16 14:29:14 +00003689 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003690 if (Name == nullptr)
3691 return nullptr;
3692
3693 if (!ElabSpef.empty())
3694 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3695
3696 return Name;
3697}
3698
3699// <qualified-type> ::= <qualifiers> <type>
3700// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3701// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003702template <typename Derived, typename Alloc>
3703Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003704 if (consumeIf('U')) {
3705 StringView Qual = parseBareSourceName();
3706 if (Qual.empty())
3707 return nullptr;
3708
Richard Smithc20d1442018-08-20 20:14:49 +00003709 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3710 if (Qual.startsWith("objcproto")) {
3711 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3712 StringView Proto;
3713 {
3714 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3715 SaveLast(Last, ProtoSourceName.end());
3716 Proto = parseBareSourceName();
3717 }
3718 if (Proto.empty())
3719 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003720 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003721 if (Child == nullptr)
3722 return nullptr;
3723 return make<ObjCProtoName>(Child, Proto);
3724 }
3725
Alex Orlovf50df922021-03-24 10:21:32 +04003726 Node *TA = nullptr;
3727 if (look() == 'I') {
3728 TA = getDerived().parseTemplateArgs();
3729 if (TA == nullptr)
3730 return nullptr;
3731 }
3732
Pavel Labathba825192018-10-16 14:29:14 +00003733 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003734 if (Child == nullptr)
3735 return nullptr;
Alex Orlovf50df922021-03-24 10:21:32 +04003736 return make<VendorExtQualType>(Child, Qual, TA);
Richard Smithc20d1442018-08-20 20:14:49 +00003737 }
3738
3739 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003740 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003741 if (Ty == nullptr)
3742 return nullptr;
3743 if (Quals != QualNone)
3744 Ty = make<QualType>(Ty, Quals);
3745 return Ty;
3746}
3747
3748// <type> ::= <builtin-type>
3749// ::= <qualified-type>
3750// ::= <function-type>
3751// ::= <class-enum-type>
3752// ::= <array-type>
3753// ::= <pointer-to-member-type>
3754// ::= <template-param>
3755// ::= <template-template-param> <template-args>
3756// ::= <decltype>
3757// ::= P <type> # pointer
3758// ::= R <type> # l-value reference
3759// ::= O <type> # r-value reference (C++11)
3760// ::= C <type> # complex pair (C99)
3761// ::= G <type> # imaginary (C99)
3762// ::= <substitution> # See Compression below
3763// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3764// extension ::= <vector-type> # <vector-type> starts with Dv
3765//
3766// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3767// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003768template <typename Derived, typename Alloc>
3769Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003770 Node *Result = nullptr;
3771
Richard Smithc20d1442018-08-20 20:14:49 +00003772 switch (look()) {
3773 // ::= <qualified-type>
3774 case 'r':
3775 case 'V':
3776 case 'K': {
3777 unsigned AfterQuals = 0;
3778 if (look(AfterQuals) == 'r') ++AfterQuals;
3779 if (look(AfterQuals) == 'V') ++AfterQuals;
3780 if (look(AfterQuals) == 'K') ++AfterQuals;
3781
3782 if (look(AfterQuals) == 'F' ||
3783 (look(AfterQuals) == 'D' &&
3784 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3785 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003786 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003787 break;
3788 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003789 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003790 }
3791 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003792 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003793 break;
3794 }
3795 // <builtin-type> ::= v # void
3796 case 'v':
3797 ++First;
3798 return make<NameType>("void");
3799 // ::= w # wchar_t
3800 case 'w':
3801 ++First;
3802 return make<NameType>("wchar_t");
3803 // ::= b # bool
3804 case 'b':
3805 ++First;
3806 return make<NameType>("bool");
3807 // ::= c # char
3808 case 'c':
3809 ++First;
3810 return make<NameType>("char");
3811 // ::= a # signed char
3812 case 'a':
3813 ++First;
3814 return make<NameType>("signed char");
3815 // ::= h # unsigned char
3816 case 'h':
3817 ++First;
3818 return make<NameType>("unsigned char");
3819 // ::= s # short
3820 case 's':
3821 ++First;
3822 return make<NameType>("short");
3823 // ::= t # unsigned short
3824 case 't':
3825 ++First;
3826 return make<NameType>("unsigned short");
3827 // ::= i # int
3828 case 'i':
3829 ++First;
3830 return make<NameType>("int");
3831 // ::= j # unsigned int
3832 case 'j':
3833 ++First;
3834 return make<NameType>("unsigned int");
3835 // ::= l # long
3836 case 'l':
3837 ++First;
3838 return make<NameType>("long");
3839 // ::= m # unsigned long
3840 case 'm':
3841 ++First;
3842 return make<NameType>("unsigned long");
3843 // ::= x # long long, __int64
3844 case 'x':
3845 ++First;
3846 return make<NameType>("long long");
3847 // ::= y # unsigned long long, __int64
3848 case 'y':
3849 ++First;
3850 return make<NameType>("unsigned long long");
3851 // ::= n # __int128
3852 case 'n':
3853 ++First;
3854 return make<NameType>("__int128");
3855 // ::= o # unsigned __int128
3856 case 'o':
3857 ++First;
3858 return make<NameType>("unsigned __int128");
3859 // ::= f # float
3860 case 'f':
3861 ++First;
3862 return make<NameType>("float");
3863 // ::= d # double
3864 case 'd':
3865 ++First;
3866 return make<NameType>("double");
3867 // ::= e # long double, __float80
3868 case 'e':
3869 ++First;
3870 return make<NameType>("long double");
3871 // ::= g # __float128
3872 case 'g':
3873 ++First;
3874 return make<NameType>("__float128");
3875 // ::= z # ellipsis
3876 case 'z':
3877 ++First;
3878 return make<NameType>("...");
3879
3880 // <builtin-type> ::= u <source-name> # vendor extended type
3881 case 'u': {
3882 ++First;
3883 StringView Res = parseBareSourceName();
3884 if (Res.empty())
3885 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003886 // Typically, <builtin-type>s are not considered substitution candidates,
3887 // but the exception to that exception is vendor extended types (Itanium C++
3888 // ABI 5.9.1).
3889 Result = make<NameType>(Res);
3890 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003891 }
3892 case 'D':
3893 switch (look(1)) {
3894 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3895 case 'd':
3896 First += 2;
3897 return make<NameType>("decimal64");
3898 // ::= De # IEEE 754r decimal floating point (128 bits)
3899 case 'e':
3900 First += 2;
3901 return make<NameType>("decimal128");
3902 // ::= Df # IEEE 754r decimal floating point (32 bits)
3903 case 'f':
3904 First += 2;
3905 return make<NameType>("decimal32");
3906 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3907 case 'h':
3908 First += 2;
Stuart Bradye8bf5772021-06-07 16:30:22 +01003909 return make<NameType>("half");
Pengfei Wang50e90b82021-09-23 11:02:25 +08003910 // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits)
3911 case 'F': {
3912 First += 2;
3913 Node *DimensionNumber = make<NameType>(parseNumber());
3914 if (!DimensionNumber)
3915 return nullptr;
3916 if (!consumeIf('_'))
3917 return nullptr;
3918 return make<BinaryFPType>(DimensionNumber);
3919 }
Richard Smithc20d1442018-08-20 20:14:49 +00003920 // ::= Di # char32_t
3921 case 'i':
3922 First += 2;
3923 return make<NameType>("char32_t");
3924 // ::= Ds # char16_t
3925 case 's':
3926 First += 2;
3927 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003928 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3929 case 'u':
3930 First += 2;
3931 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003932 // ::= Da # auto (in dependent new-expressions)
3933 case 'a':
3934 First += 2;
3935 return make<NameType>("auto");
3936 // ::= Dc # decltype(auto)
3937 case 'c':
3938 First += 2;
3939 return make<NameType>("decltype(auto)");
3940 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3941 case 'n':
3942 First += 2;
3943 return make<NameType>("std::nullptr_t");
3944
3945 // ::= <decltype>
3946 case 't':
3947 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003948 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003949 break;
3950 }
3951 // extension ::= <vector-type> # <vector-type> starts with Dv
3952 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003953 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003954 break;
3955 }
3956 // ::= Dp <type> # pack expansion (C++0x)
3957 case 'p': {
3958 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003959 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003960 if (!Child)
3961 return nullptr;
3962 Result = make<ParameterPackExpansion>(Child);
3963 break;
3964 }
3965 // Exception specifier on a function type.
3966 case 'o':
3967 case 'O':
3968 case 'w':
3969 // Transaction safe function type.
3970 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003971 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003972 break;
3973 }
3974 break;
3975 // ::= <function-type>
3976 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003977 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003978 break;
3979 }
3980 // ::= <array-type>
3981 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003982 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003983 break;
3984 }
3985 // ::= <pointer-to-member-type>
3986 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003987 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003988 break;
3989 }
3990 // ::= <template-param>
3991 case 'T': {
3992 // This could be an elaborate type specifier on a <class-enum-type>.
3993 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003994 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003995 break;
3996 }
3997
Pavel Labathba825192018-10-16 14:29:14 +00003998 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003999 if (Result == nullptr)
4000 return nullptr;
4001
4002 // Result could be either of:
4003 // <type> ::= <template-param>
4004 // <type> ::= <template-template-param> <template-args>
4005 //
4006 // <template-template-param> ::= <template-param>
4007 // ::= <substitution>
4008 //
4009 // If this is followed by some <template-args>, and we're permitted to
4010 // parse them, take the second production.
4011
4012 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004013 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004014 if (TA == nullptr)
4015 return nullptr;
4016 Result = make<NameWithTemplateArgs>(Result, TA);
4017 }
4018 break;
4019 }
4020 // ::= P <type> # pointer
4021 case 'P': {
4022 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004023 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004024 if (Ptr == nullptr)
4025 return nullptr;
4026 Result = make<PointerType>(Ptr);
4027 break;
4028 }
4029 // ::= R <type> # l-value reference
4030 case 'R': {
4031 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004032 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004033 if (Ref == nullptr)
4034 return nullptr;
4035 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
4036 break;
4037 }
4038 // ::= O <type> # r-value reference (C++11)
4039 case 'O': {
4040 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004041 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004042 if (Ref == nullptr)
4043 return nullptr;
4044 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
4045 break;
4046 }
4047 // ::= C <type> # complex pair (C99)
4048 case 'C': {
4049 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004050 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004051 if (P == nullptr)
4052 return nullptr;
4053 Result = make<PostfixQualifiedType>(P, " complex");
4054 break;
4055 }
4056 // ::= G <type> # imaginary (C99)
4057 case 'G': {
4058 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004059 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004060 if (P == nullptr)
4061 return P;
4062 Result = make<PostfixQualifiedType>(P, " imaginary");
4063 break;
4064 }
4065 // ::= <substitution> # See Compression below
4066 case 'S': {
4067 if (look(1) && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00004068 Node *Sub = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00004069 if (Sub == nullptr)
4070 return nullptr;
4071
4072 // Sub could be either of:
4073 // <type> ::= <substitution>
4074 // <type> ::= <template-template-param> <template-args>
4075 //
4076 // <template-template-param> ::= <template-param>
4077 // ::= <substitution>
4078 //
4079 // If this is followed by some <template-args>, and we're permitted to
4080 // parse them, take the second production.
4081
4082 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004083 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004084 if (TA == nullptr)
4085 return nullptr;
4086 Result = make<NameWithTemplateArgs>(Sub, TA);
4087 break;
4088 }
4089
4090 // If all we parsed was a substitution, don't re-insert into the
4091 // substitution table.
4092 return Sub;
4093 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00004094 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004095 }
4096 // ::= <class-enum-type>
4097 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004098 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004099 break;
4100 }
4101 }
4102
4103 // If we parsed a type, insert it into the substitution table. Note that all
4104 // <builtin-type>s and <substitution>s have already bailed out, because they
4105 // don't get substitutions.
4106 if (Result != nullptr)
4107 Subs.push_back(Result);
4108 return Result;
4109}
4110
Pavel Labathba825192018-10-16 14:29:14 +00004111template <typename Derived, typename Alloc>
4112Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4113 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004114 if (E == nullptr)
4115 return nullptr;
4116 return make<PrefixExpr>(Kind, E);
4117}
4118
Pavel Labathba825192018-10-16 14:29:14 +00004119template <typename Derived, typename Alloc>
4120Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4121 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004122 if (LHS == nullptr)
4123 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004124 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004125 if (RHS == nullptr)
4126 return nullptr;
4127 return make<BinaryExpr>(LHS, Kind, RHS);
4128}
4129
Pavel Labathba825192018-10-16 14:29:14 +00004130template <typename Derived, typename Alloc>
4131Node *
4132AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004133 StringView Tmp = parseNumber(true);
4134 if (!Tmp.empty() && consumeIf('E'))
4135 return make<IntegerLiteral>(Lit, Tmp);
4136 return nullptr;
4137}
4138
4139// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004140template <typename Alloc, typename Derived>
4141Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004142 Qualifiers CVR = QualNone;
4143 if (consumeIf('r'))
4144 CVR |= QualRestrict;
4145 if (consumeIf('V'))
4146 CVR |= QualVolatile;
4147 if (consumeIf('K'))
4148 CVR |= QualConst;
4149 return CVR;
4150}
4151
4152// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4153// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4154// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4155// ::= 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 -04004156// ::= fpT # 'this' expression (not part of standard?)
Pavel Labathba825192018-10-16 14:29:14 +00004157template <typename Derived, typename Alloc>
4158Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Erik Pilkington91c24af2020-05-13 22:19:45 -04004159 if (consumeIf("fpT"))
4160 return make<NameType>("this");
Richard Smithc20d1442018-08-20 20:14:49 +00004161 if (consumeIf("fp")) {
4162 parseCVQualifiers();
4163 StringView Num = parseNumber();
4164 if (!consumeIf('_'))
4165 return nullptr;
4166 return make<FunctionParam>(Num);
4167 }
4168 if (consumeIf("fL")) {
4169 if (parseNumber().empty())
4170 return nullptr;
4171 if (!consumeIf('p'))
4172 return nullptr;
4173 parseCVQualifiers();
4174 StringView Num = parseNumber();
4175 if (!consumeIf('_'))
4176 return nullptr;
4177 return make<FunctionParam>(Num);
4178 }
4179 return nullptr;
4180}
4181
4182// [gs] nw <expression>* _ <type> E # new (expr-list) type
4183// [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4184// [gs] na <expression>* _ <type> E # new[] (expr-list) type
4185// [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4186// <initializer> ::= pi <expression>* E # parenthesized initialization
Pavel Labathba825192018-10-16 14:29:14 +00004187template <typename Derived, typename Alloc>
4188Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004189 bool Global = consumeIf("gs");
4190 bool IsArray = look(1) == 'a';
4191 if (!consumeIf("nw") && !consumeIf("na"))
4192 return nullptr;
4193 size_t Exprs = Names.size();
4194 while (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00004195 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004196 if (Ex == nullptr)
4197 return nullptr;
4198 Names.push_back(Ex);
4199 }
4200 NodeArray ExprList = popTrailingNodeArray(Exprs);
Pavel Labathba825192018-10-16 14:29:14 +00004201 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004202 if (Ty == nullptr)
4203 return Ty;
4204 if (consumeIf("pi")) {
4205 size_t InitsBegin = Names.size();
4206 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004207 Node *Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004208 if (Init == nullptr)
4209 return Init;
4210 Names.push_back(Init);
4211 }
4212 NodeArray Inits = popTrailingNodeArray(InitsBegin);
4213 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
4214 } else if (!consumeIf('E'))
4215 return nullptr;
4216 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
4217}
4218
4219// cv <type> <expression> # conversion with one argument
4220// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004221template <typename Derived, typename Alloc>
4222Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004223 if (!consumeIf("cv"))
4224 return nullptr;
4225 Node *Ty;
4226 {
4227 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004228 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004229 }
4230
4231 if (Ty == nullptr)
4232 return nullptr;
4233
4234 if (consumeIf('_')) {
4235 size_t ExprsBegin = Names.size();
4236 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004237 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004238 if (E == nullptr)
4239 return E;
4240 Names.push_back(E);
4241 }
4242 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4243 return make<ConversionExpr>(Ty, Exprs);
4244 }
4245
Pavel Labathba825192018-10-16 14:29:14 +00004246 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004247 if (E[0] == nullptr)
4248 return nullptr;
4249 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4250}
4251
4252// <expr-primary> ::= L <type> <value number> E # integer literal
4253// ::= L <type> <value float> E # floating literal
4254// ::= L <string type> E # string literal
4255// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004256// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004257// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4258// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004259template <typename Derived, typename Alloc>
4260Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004261 if (!consumeIf('L'))
4262 return nullptr;
4263 switch (look()) {
4264 case 'w':
4265 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004266 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004267 case 'b':
4268 if (consumeIf("b0E"))
4269 return make<BoolExpr>(0);
4270 if (consumeIf("b1E"))
4271 return make<BoolExpr>(1);
4272 return nullptr;
4273 case 'c':
4274 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004275 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004276 case 'a':
4277 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004278 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004279 case 'h':
4280 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004281 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004282 case 's':
4283 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004284 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004285 case 't':
4286 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004287 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004288 case 'i':
4289 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004290 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004291 case 'j':
4292 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004293 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004294 case 'l':
4295 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004296 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004297 case 'm':
4298 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004299 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004300 case 'x':
4301 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004302 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004303 case 'y':
4304 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004305 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004306 case 'n':
4307 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004308 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004309 case 'o':
4310 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004311 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004312 case 'f':
4313 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004314 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004315 case 'd':
4316 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004317 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004318 case 'e':
4319 ++First;
Xing Xue3dc5e082020-04-15 09:59:06 -04004320#if defined(__powerpc__) || defined(__s390__)
4321 // Handle cases where long doubles encoded with e have the same size
4322 // and representation as doubles.
4323 return getDerived().template parseFloatingLiteral<double>();
4324#else
Pavel Labathba825192018-10-16 14:29:14 +00004325 return getDerived().template parseFloatingLiteral<long double>();
Xing Xue3dc5e082020-04-15 09:59:06 -04004326#endif
Richard Smithc20d1442018-08-20 20:14:49 +00004327 case '_':
4328 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004329 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004330 if (R != nullptr && consumeIf('E'))
4331 return R;
4332 }
4333 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004334 case 'A': {
4335 Node *T = getDerived().parseType();
4336 if (T == nullptr)
4337 return nullptr;
4338 // FIXME: We need to include the string contents in the mangling.
4339 if (consumeIf('E'))
4340 return make<StringLiteral>(T);
4341 return nullptr;
4342 }
4343 case 'D':
4344 if (consumeIf("DnE"))
4345 return make<NameType>("nullptr");
4346 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004347 case 'T':
4348 // Invalid mangled name per
4349 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4350 return nullptr;
Richard Smithfb917462019-09-09 22:26:04 +00004351 case 'U': {
4352 // FIXME: Should we support LUb... for block literals?
4353 if (look(1) != 'l')
4354 return nullptr;
4355 Node *T = parseUnnamedTypeName(nullptr);
4356 if (!T || !consumeIf('E'))
4357 return nullptr;
4358 return make<LambdaExpr>(T);
4359 }
Richard Smithc20d1442018-08-20 20:14:49 +00004360 default: {
4361 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004362 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004363 if (T == nullptr)
4364 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004365 StringView N = parseNumber(/*AllowNegative=*/true);
Richard Smithfb917462019-09-09 22:26:04 +00004366 if (N.empty())
4367 return nullptr;
4368 if (!consumeIf('E'))
4369 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004370 return make<EnumLiteral>(T, N);
Richard Smithc20d1442018-08-20 20:14:49 +00004371 }
4372 }
4373}
4374
4375// <braced-expression> ::= <expression>
4376// ::= di <field source-name> <braced-expression> # .name = expr
4377// ::= dx <index expression> <braced-expression> # [expr] = expr
4378// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004379template <typename Derived, typename Alloc>
4380Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004381 if (look() == 'd') {
4382 switch (look(1)) {
4383 case 'i': {
4384 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004385 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004386 if (Field == nullptr)
4387 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004388 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004389 if (Init == nullptr)
4390 return nullptr;
4391 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4392 }
4393 case 'x': {
4394 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004395 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004396 if (Index == nullptr)
4397 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004398 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004399 if (Init == nullptr)
4400 return nullptr;
4401 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4402 }
4403 case 'X': {
4404 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004405 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004406 if (RangeBegin == nullptr)
4407 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004408 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004409 if (RangeEnd == nullptr)
4410 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004411 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004412 if (Init == nullptr)
4413 return nullptr;
4414 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4415 }
4416 }
4417 }
Pavel Labathba825192018-10-16 14:29:14 +00004418 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004419}
4420
4421// (not yet in the spec)
4422// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4423// ::= fR <binary-operator-name> <expression> <expression>
4424// ::= fl <binary-operator-name> <expression>
4425// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004426template <typename Derived, typename Alloc>
4427Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004428 if (!consumeIf('f'))
4429 return nullptr;
4430
4431 char FoldKind = look();
4432 bool IsLeftFold, HasInitializer;
4433 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4434 if (FoldKind == 'l' || FoldKind == 'L')
4435 IsLeftFold = true;
4436 else if (FoldKind == 'r' || FoldKind == 'R')
4437 IsLeftFold = false;
4438 else
4439 return nullptr;
4440 ++First;
4441
4442 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4443 StringView OperatorName;
4444 if (consumeIf("aa")) OperatorName = "&&";
4445 else if (consumeIf("an")) OperatorName = "&";
4446 else if (consumeIf("aN")) OperatorName = "&=";
4447 else if (consumeIf("aS")) OperatorName = "=";
4448 else if (consumeIf("cm")) OperatorName = ",";
4449 else if (consumeIf("ds")) OperatorName = ".*";
4450 else if (consumeIf("dv")) OperatorName = "/";
4451 else if (consumeIf("dV")) OperatorName = "/=";
4452 else if (consumeIf("eo")) OperatorName = "^";
4453 else if (consumeIf("eO")) OperatorName = "^=";
4454 else if (consumeIf("eq")) OperatorName = "==";
4455 else if (consumeIf("ge")) OperatorName = ">=";
4456 else if (consumeIf("gt")) OperatorName = ">";
4457 else if (consumeIf("le")) OperatorName = "<=";
4458 else if (consumeIf("ls")) OperatorName = "<<";
4459 else if (consumeIf("lS")) OperatorName = "<<=";
4460 else if (consumeIf("lt")) OperatorName = "<";
4461 else if (consumeIf("mi")) OperatorName = "-";
4462 else if (consumeIf("mI")) OperatorName = "-=";
4463 else if (consumeIf("ml")) OperatorName = "*";
4464 else if (consumeIf("mL")) OperatorName = "*=";
4465 else if (consumeIf("ne")) OperatorName = "!=";
4466 else if (consumeIf("oo")) OperatorName = "||";
4467 else if (consumeIf("or")) OperatorName = "|";
4468 else if (consumeIf("oR")) OperatorName = "|=";
4469 else if (consumeIf("pl")) OperatorName = "+";
4470 else if (consumeIf("pL")) OperatorName = "+=";
4471 else if (consumeIf("rm")) OperatorName = "%";
4472 else if (consumeIf("rM")) OperatorName = "%=";
4473 else if (consumeIf("rs")) OperatorName = ">>";
4474 else if (consumeIf("rS")) OperatorName = ">>=";
4475 else return nullptr;
4476
Pavel Labathba825192018-10-16 14:29:14 +00004477 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004478 if (Pack == nullptr)
4479 return nullptr;
4480 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004481 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004482 if (Init == nullptr)
4483 return nullptr;
4484 }
4485
4486 if (IsLeftFold && Init)
4487 std::swap(Pack, Init);
4488
4489 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4490}
4491
Richard Smith1865d2f2020-10-22 19:29:36 -07004492// <expression> ::= mc <parameter type> <expr> [<offset number>] E
4493//
4494// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4495template <typename Derived, typename Alloc>
4496Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() {
4497 Node *Ty = getDerived().parseType();
4498 if (!Ty)
4499 return nullptr;
4500 Node *Expr = getDerived().parseExpr();
4501 if (!Expr)
4502 return nullptr;
4503 StringView Offset = getDerived().parseNumber(true);
4504 if (!consumeIf('E'))
4505 return nullptr;
4506 return make<PointerToMemberConversionExpr>(Ty, Expr, Offset);
4507}
4508
4509// <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
4510// <union-selector> ::= _ [<number>]
4511//
4512// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4513template <typename Derived, typename Alloc>
4514Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
4515 Node *Ty = getDerived().parseType();
4516 if (!Ty)
4517 return nullptr;
4518 Node *Expr = getDerived().parseExpr();
4519 if (!Expr)
4520 return nullptr;
4521 StringView Offset = getDerived().parseNumber(true);
4522 size_t SelectorsBegin = Names.size();
4523 while (consumeIf('_')) {
4524 Node *Selector = make<NameType>(parseNumber());
4525 if (!Selector)
4526 return nullptr;
4527 Names.push_back(Selector);
4528 }
4529 bool OnePastTheEnd = consumeIf('p');
4530 if (!consumeIf('E'))
4531 return nullptr;
4532 return make<SubobjectExpr>(
4533 Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
4534}
4535
Richard Smithc20d1442018-08-20 20:14:49 +00004536// <expression> ::= <unary operator-name> <expression>
4537// ::= <binary operator-name> <expression> <expression>
4538// ::= <ternary operator-name> <expression> <expression> <expression>
4539// ::= cl <expression>+ E # call
4540// ::= cv <type> <expression> # conversion with one argument
4541// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4542// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4543// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4544// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4545// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4546// ::= [gs] dl <expression> # delete expression
4547// ::= [gs] da <expression> # delete[] expression
4548// ::= pp_ <expression> # prefix ++
4549// ::= mm_ <expression> # prefix --
4550// ::= ti <type> # typeid (type)
4551// ::= te <expression> # typeid (expression)
4552// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4553// ::= sc <type> <expression> # static_cast<type> (expression)
4554// ::= cc <type> <expression> # const_cast<type> (expression)
4555// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4556// ::= st <type> # sizeof (a type)
4557// ::= sz <expression> # sizeof (an expression)
4558// ::= at <type> # alignof (a type)
4559// ::= az <expression> # alignof (an expression)
4560// ::= nx <expression> # noexcept (expression)
4561// ::= <template-param>
4562// ::= <function-param>
4563// ::= dt <expression> <unresolved-name> # expr.name
4564// ::= pt <expression> <unresolved-name> # expr->name
4565// ::= ds <expression> <expression> # expr.*expr
4566// ::= sZ <template-param> # size of a parameter pack
4567// ::= sZ <function-param> # size of a function parameter pack
4568// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4569// ::= sp <expression> # pack expansion
4570// ::= tw <expression> # throw expression
4571// ::= tr # throw with no operand (rethrow)
4572// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4573// # freestanding dependent name (e.g., T::x),
4574// # objectless nonstatic member reference
4575// ::= fL <binary-operator-name> <expression> <expression>
4576// ::= fR <binary-operator-name> <expression> <expression>
4577// ::= fl <binary-operator-name> <expression>
4578// ::= fr <binary-operator-name> <expression>
4579// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004580template <typename Derived, typename Alloc>
4581Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004582 bool Global = consumeIf("gs");
4583 if (numLeft() < 2)
4584 return nullptr;
4585
4586 switch (*First) {
4587 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004588 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004589 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004590 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004591 case 'f': {
4592 // Disambiguate a fold expression from a <function-param>.
4593 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004594 return getDerived().parseFunctionParam();
4595 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004596 }
4597 case 'a':
4598 switch (First[1]) {
4599 case 'a':
4600 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004601 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004602 case 'd':
4603 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004604 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004605 case 'n':
4606 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004607 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004608 case 'N':
4609 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004610 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004611 case 'S':
4612 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004613 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004614 case 't': {
4615 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004616 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004617 if (Ty == nullptr)
4618 return nullptr;
4619 return make<EnclosingExpr>("alignof (", Ty, ")");
4620 }
4621 case 'z': {
4622 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004623 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004624 if (Ty == nullptr)
4625 return nullptr;
4626 return make<EnclosingExpr>("alignof (", Ty, ")");
4627 }
4628 }
4629 return nullptr;
4630 case 'c':
4631 switch (First[1]) {
4632 // cc <type> <expression> # const_cast<type>(expression)
4633 case 'c': {
4634 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004635 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004636 if (Ty == nullptr)
4637 return Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004638 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004639 if (Ex == nullptr)
4640 return Ex;
4641 return make<CastExpr>("const_cast", Ty, Ex);
4642 }
4643 // cl <expression>+ E # call
4644 case 'l': {
4645 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004646 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004647 if (Callee == nullptr)
4648 return Callee;
4649 size_t ExprsBegin = Names.size();
4650 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004651 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004652 if (E == nullptr)
4653 return E;
4654 Names.push_back(E);
4655 }
4656 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4657 }
4658 case 'm':
4659 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004660 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004661 case 'o':
4662 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004663 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004664 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004665 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004666 }
4667 return nullptr;
4668 case 'd':
4669 switch (First[1]) {
4670 case 'a': {
4671 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004672 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004673 if (Ex == nullptr)
4674 return Ex;
4675 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4676 }
4677 case 'c': {
4678 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004679 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004680 if (T == nullptr)
4681 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004682 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004683 if (Ex == nullptr)
4684 return Ex;
4685 return make<CastExpr>("dynamic_cast", T, Ex);
4686 }
4687 case 'e':
4688 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004689 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004690 case 'l': {
4691 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004692 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004693 if (E == nullptr)
4694 return E;
4695 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4696 }
4697 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004698 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004699 case 's': {
4700 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004701 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004702 if (LHS == nullptr)
4703 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004704 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004705 if (RHS == nullptr)
4706 return nullptr;
4707 return make<MemberExpr>(LHS, ".*", RHS);
4708 }
4709 case 't': {
4710 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004711 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004712 if (LHS == nullptr)
4713 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004714 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004715 if (RHS == nullptr)
4716 return nullptr;
4717 return make<MemberExpr>(LHS, ".", RHS);
4718 }
4719 case 'v':
4720 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004721 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004722 case 'V':
4723 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004724 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004725 }
4726 return nullptr;
4727 case 'e':
4728 switch (First[1]) {
4729 case 'o':
4730 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004731 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004732 case 'O':
4733 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004734 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004735 case 'q':
4736 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004737 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004738 }
4739 return nullptr;
4740 case 'g':
4741 switch (First[1]) {
4742 case 'e':
4743 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004744 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004745 case 't':
4746 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004747 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004748 }
4749 return nullptr;
4750 case 'i':
4751 switch (First[1]) {
4752 case 'x': {
4753 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004754 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004755 if (Base == nullptr)
4756 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004757 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004758 if (Index == nullptr)
4759 return Index;
4760 return make<ArraySubscriptExpr>(Base, Index);
4761 }
4762 case 'l': {
4763 First += 2;
4764 size_t InitsBegin = Names.size();
4765 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004766 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004767 if (E == nullptr)
4768 return nullptr;
4769 Names.push_back(E);
4770 }
4771 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4772 }
4773 }
4774 return nullptr;
4775 case 'l':
4776 switch (First[1]) {
4777 case 'e':
4778 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004779 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004780 case 's':
4781 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004782 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004783 case 'S':
4784 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004785 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004786 case 't':
4787 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004788 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004789 }
4790 return nullptr;
4791 case 'm':
4792 switch (First[1]) {
Richard Smith1865d2f2020-10-22 19:29:36 -07004793 case 'c':
4794 First += 2;
4795 return parsePointerToMemberConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004796 case 'i':
4797 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004798 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004799 case 'I':
4800 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004801 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004802 case 'l':
4803 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004804 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004805 case 'L':
4806 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004807 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004808 case 'm':
4809 First += 2;
4810 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004811 return getDerived().parsePrefixExpr("--");
4812 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004813 if (Ex == nullptr)
4814 return nullptr;
4815 return make<PostfixExpr>(Ex, "--");
4816 }
4817 return nullptr;
4818 case 'n':
4819 switch (First[1]) {
4820 case 'a':
4821 case 'w':
Pavel Labathba825192018-10-16 14:29:14 +00004822 return getDerived().parseNewExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004823 case 'e':
4824 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004825 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004826 case 'g':
4827 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004828 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004829 case 't':
4830 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004831 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004832 case 'x':
4833 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004834 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004835 if (Ex == nullptr)
4836 return Ex;
4837 return make<EnclosingExpr>("noexcept (", Ex, ")");
4838 }
4839 return nullptr;
4840 case 'o':
4841 switch (First[1]) {
4842 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004843 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004844 case 'o':
4845 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004846 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004847 case 'r':
4848 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004849 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004850 case 'R':
4851 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004852 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004853 }
4854 return nullptr;
4855 case 'p':
4856 switch (First[1]) {
4857 case 'm':
4858 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004859 return getDerived().parseBinaryExpr("->*");
Richard Smithc20d1442018-08-20 20:14:49 +00004860 case 'l':
4861 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004862 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004863 case 'L':
4864 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004865 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004866 case 'p': {
4867 First += 2;
4868 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004869 return getDerived().parsePrefixExpr("++");
4870 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004871 if (Ex == nullptr)
4872 return Ex;
4873 return make<PostfixExpr>(Ex, "++");
4874 }
4875 case 's':
4876 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004877 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004878 case 't': {
4879 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004880 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004881 if (L == nullptr)
4882 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004883 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004884 if (R == nullptr)
4885 return nullptr;
4886 return make<MemberExpr>(L, "->", R);
4887 }
4888 }
4889 return nullptr;
4890 case 'q':
4891 if (First[1] == 'u') {
4892 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004893 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004894 if (Cond == nullptr)
4895 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004896 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004897 if (LHS == nullptr)
4898 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004899 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004900 if (RHS == nullptr)
4901 return nullptr;
4902 return make<ConditionalExpr>(Cond, LHS, RHS);
4903 }
4904 return nullptr;
4905 case 'r':
4906 switch (First[1]) {
4907 case 'c': {
4908 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004909 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004910 if (T == nullptr)
4911 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004912 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004913 if (Ex == nullptr)
4914 return Ex;
4915 return make<CastExpr>("reinterpret_cast", T, Ex);
4916 }
4917 case 'm':
4918 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004919 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004920 case 'M':
4921 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004922 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004923 case 's':
4924 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004925 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004926 case 'S':
4927 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004928 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004929 }
4930 return nullptr;
4931 case 's':
4932 switch (First[1]) {
4933 case 'c': {
4934 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004935 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004936 if (T == nullptr)
4937 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004938 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004939 if (Ex == nullptr)
4940 return Ex;
4941 return make<CastExpr>("static_cast", T, Ex);
4942 }
Richard Smith1865d2f2020-10-22 19:29:36 -07004943 case 'o':
4944 First += 2;
4945 return parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004946 case 'p': {
4947 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004948 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004949 if (Child == nullptr)
4950 return nullptr;
4951 return make<ParameterPackExpansion>(Child);
4952 }
4953 case 'r':
Pavel Labathba825192018-10-16 14:29:14 +00004954 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004955 case 't': {
4956 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004957 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004958 if (Ty == nullptr)
4959 return Ty;
4960 return make<EnclosingExpr>("sizeof (", Ty, ")");
4961 }
4962 case 'z': {
4963 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004964 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004965 if (Ex == nullptr)
4966 return Ex;
4967 return make<EnclosingExpr>("sizeof (", Ex, ")");
4968 }
4969 case 'Z':
4970 First += 2;
4971 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004972 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004973 if (R == nullptr)
4974 return nullptr;
4975 return make<SizeofParamPackExpr>(R);
4976 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004977 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004978 if (FP == nullptr)
4979 return nullptr;
4980 return make<EnclosingExpr>("sizeof... (", FP, ")");
4981 }
4982 return nullptr;
4983 case 'P': {
4984 First += 2;
4985 size_t ArgsBegin = Names.size();
4986 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004987 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004988 if (Arg == nullptr)
4989 return nullptr;
4990 Names.push_back(Arg);
4991 }
Richard Smithb485b352018-08-24 23:30:26 +00004992 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4993 if (!Pack)
4994 return nullptr;
4995 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00004996 }
4997 }
4998 return nullptr;
4999 case 't':
5000 switch (First[1]) {
5001 case 'e': {
5002 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005003 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005004 if (Ex == nullptr)
5005 return Ex;
5006 return make<EnclosingExpr>("typeid (", Ex, ")");
5007 }
5008 case 'i': {
5009 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005010 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005011 if (Ty == nullptr)
5012 return Ty;
5013 return make<EnclosingExpr>("typeid (", Ty, ")");
5014 }
5015 case 'l': {
5016 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005017 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005018 if (Ty == nullptr)
5019 return nullptr;
5020 size_t InitsBegin = Names.size();
5021 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005022 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005023 if (E == nullptr)
5024 return nullptr;
5025 Names.push_back(E);
5026 }
5027 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
5028 }
5029 case 'r':
5030 First += 2;
5031 return make<NameType>("throw");
5032 case 'w': {
5033 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005034 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005035 if (Ex == nullptr)
5036 return nullptr;
5037 return make<ThrowExpr>(Ex);
5038 }
5039 }
5040 return nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05005041 case 'u': {
5042 ++First;
5043 Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
5044 if (!Name)
5045 return nullptr;
5046 // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
5047 // standard encoding expects a <template-arg>, and would be otherwise be
5048 // interpreted as <type> node 'short' or 'ellipsis'. However, neither
5049 // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
5050 // actual conflict here.
5051 if (Name->getBaseName() == "__uuidof") {
5052 if (numLeft() < 2)
5053 return nullptr;
5054 if (*First == 't') {
5055 ++First;
5056 Node *Ty = getDerived().parseType();
5057 if (!Ty)
5058 return nullptr;
5059 return make<CallExpr>(Name, makeNodeArray(&Ty, &Ty + 1));
5060 }
5061 if (*First == 'z') {
5062 ++First;
5063 Node *Ex = getDerived().parseExpr();
5064 if (!Ex)
5065 return nullptr;
5066 return make<CallExpr>(Name, makeNodeArray(&Ex, &Ex + 1));
5067 }
5068 }
5069 size_t ExprsBegin = Names.size();
5070 while (!consumeIf('E')) {
5071 Node *E = getDerived().parseTemplateArg();
5072 if (E == nullptr)
5073 return E;
5074 Names.push_back(E);
5075 }
5076 return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin));
5077 }
Richard Smithc20d1442018-08-20 20:14:49 +00005078 case '1':
5079 case '2':
5080 case '3':
5081 case '4':
5082 case '5':
5083 case '6':
5084 case '7':
5085 case '8':
5086 case '9':
Pavel Labathba825192018-10-16 14:29:14 +00005087 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00005088 }
5089 return nullptr;
5090}
5091
5092// <call-offset> ::= h <nv-offset> _
5093// ::= v <v-offset> _
5094//
5095// <nv-offset> ::= <offset number>
5096// # non-virtual base override
5097//
5098// <v-offset> ::= <offset number> _ <virtual offset number>
5099// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00005100template <typename Alloc, typename Derived>
5101bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00005102 // Just scan through the call offset, we never add this information into the
5103 // output.
5104 if (consumeIf('h'))
5105 return parseNumber(true).empty() || !consumeIf('_');
5106 if (consumeIf('v'))
5107 return parseNumber(true).empty() || !consumeIf('_') ||
5108 parseNumber(true).empty() || !consumeIf('_');
5109 return true;
5110}
5111
5112// <special-name> ::= TV <type> # virtual table
5113// ::= TT <type> # VTT structure (construction vtable index)
5114// ::= TI <type> # typeinfo structure
5115// ::= TS <type> # typeinfo name (null-terminated byte string)
5116// ::= Tc <call-offset> <call-offset> <base encoding>
5117// # base is the nominal target function of thunk
5118// # first call-offset is 'this' adjustment
5119// # second call-offset is result adjustment
5120// ::= T <call-offset> <base encoding>
5121// # base is the nominal target function of thunk
5122// ::= GV <object name> # Guard variable for one-time initialization
5123// # No <type>
5124// ::= TW <object name> # Thread-local wrapper
5125// ::= TH <object name> # Thread-local initialization
5126// ::= GR <object name> _ # First temporary
5127// ::= GR <object name> <seq-id> _ # Subsequent temporaries
5128// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
5129// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00005130template <typename Derived, typename Alloc>
5131Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00005132 switch (look()) {
5133 case 'T':
5134 switch (look(1)) {
Richard Smith1865d2f2020-10-22 19:29:36 -07005135 // TA <template-arg> # template parameter object
5136 //
5137 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
5138 case 'A': {
5139 First += 2;
5140 Node *Arg = getDerived().parseTemplateArg();
5141 if (Arg == nullptr)
5142 return nullptr;
5143 return make<SpecialName>("template parameter object for ", Arg);
5144 }
Richard Smithc20d1442018-08-20 20:14:49 +00005145 // TV <type> # virtual table
5146 case 'V': {
5147 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005148 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005149 if (Ty == nullptr)
5150 return nullptr;
5151 return make<SpecialName>("vtable for ", Ty);
5152 }
5153 // TT <type> # VTT structure (construction vtable index)
5154 case 'T': {
5155 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005156 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005157 if (Ty == nullptr)
5158 return nullptr;
5159 return make<SpecialName>("VTT for ", Ty);
5160 }
5161 // TI <type> # typeinfo structure
5162 case 'I': {
5163 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005164 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005165 if (Ty == nullptr)
5166 return nullptr;
5167 return make<SpecialName>("typeinfo for ", Ty);
5168 }
5169 // TS <type> # typeinfo name (null-terminated byte string)
5170 case 'S': {
5171 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005172 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005173 if (Ty == nullptr)
5174 return nullptr;
5175 return make<SpecialName>("typeinfo name for ", Ty);
5176 }
5177 // Tc <call-offset> <call-offset> <base encoding>
5178 case 'c': {
5179 First += 2;
5180 if (parseCallOffset() || parseCallOffset())
5181 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005182 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005183 if (Encoding == nullptr)
5184 return nullptr;
5185 return make<SpecialName>("covariant return thunk to ", Encoding);
5186 }
5187 // extension ::= TC <first type> <number> _ <second type>
5188 // # construction vtable for second-in-first
5189 case 'C': {
5190 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005191 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005192 if (FirstType == nullptr)
5193 return nullptr;
5194 if (parseNumber(true).empty() || !consumeIf('_'))
5195 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005196 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005197 if (SecondType == nullptr)
5198 return nullptr;
5199 return make<CtorVtableSpecialName>(SecondType, FirstType);
5200 }
5201 // TW <object name> # Thread-local wrapper
5202 case 'W': {
5203 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005204 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005205 if (Name == nullptr)
5206 return nullptr;
5207 return make<SpecialName>("thread-local wrapper routine for ", Name);
5208 }
5209 // TH <object name> # Thread-local initialization
5210 case 'H': {
5211 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005212 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005213 if (Name == nullptr)
5214 return nullptr;
5215 return make<SpecialName>("thread-local initialization routine for ", Name);
5216 }
5217 // T <call-offset> <base encoding>
5218 default: {
5219 ++First;
5220 bool IsVirt = look() == 'v';
5221 if (parseCallOffset())
5222 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005223 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005224 if (BaseEncoding == nullptr)
5225 return nullptr;
5226 if (IsVirt)
5227 return make<SpecialName>("virtual thunk to ", BaseEncoding);
5228 else
5229 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
5230 }
5231 }
5232 case 'G':
5233 switch (look(1)) {
5234 // GV <object name> # Guard variable for one-time initialization
5235 case 'V': {
5236 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005237 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005238 if (Name == nullptr)
5239 return nullptr;
5240 return make<SpecialName>("guard variable for ", Name);
5241 }
5242 // GR <object name> # reference temporary for object
5243 // GR <object name> _ # First temporary
5244 // GR <object name> <seq-id> _ # Subsequent temporaries
5245 case 'R': {
5246 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005247 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005248 if (Name == nullptr)
5249 return nullptr;
5250 size_t Count;
5251 bool ParsedSeqId = !parseSeqId(&Count);
5252 if (!consumeIf('_') && ParsedSeqId)
5253 return nullptr;
5254 return make<SpecialName>("reference temporary for ", Name);
5255 }
5256 }
5257 }
5258 return nullptr;
5259}
5260
5261// <encoding> ::= <function name> <bare-function-type>
5262// ::= <data name>
5263// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00005264template <typename Derived, typename Alloc>
5265Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithfac39712020-07-09 21:08:39 -07005266 // The template parameters of an encoding are unrelated to those of the
5267 // enclosing context.
5268 class SaveTemplateParams {
5269 AbstractManglingParser *Parser;
5270 decltype(TemplateParams) OldParams;
Justin Lebar2c536232021-06-09 16:57:22 -07005271 decltype(OuterTemplateParams) OldOuterParams;
Richard Smithfac39712020-07-09 21:08:39 -07005272
5273 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04005274 SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
Richard Smithfac39712020-07-09 21:08:39 -07005275 OldParams = std::move(Parser->TemplateParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005276 OldOuterParams = std::move(Parser->OuterTemplateParams);
Richard Smithfac39712020-07-09 21:08:39 -07005277 Parser->TemplateParams.clear();
Justin Lebar2c536232021-06-09 16:57:22 -07005278 Parser->OuterTemplateParams.clear();
Richard Smithfac39712020-07-09 21:08:39 -07005279 }
5280 ~SaveTemplateParams() {
5281 Parser->TemplateParams = std::move(OldParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005282 Parser->OuterTemplateParams = std::move(OldOuterParams);
Richard Smithfac39712020-07-09 21:08:39 -07005283 }
5284 } SaveTemplateParams(this);
Richard Smithfd434322020-07-09 20:36:04 -07005285
Richard Smithc20d1442018-08-20 20:14:49 +00005286 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00005287 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00005288
5289 auto IsEndOfEncoding = [&] {
5290 // The set of chars that can potentially follow an <encoding> (none of which
5291 // can start a <type>). Enumerating these allows us to avoid speculative
5292 // parsing.
5293 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5294 };
5295
5296 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00005297 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00005298 if (Name == nullptr)
5299 return nullptr;
5300
5301 if (resolveForwardTemplateRefs(NameInfo))
5302 return nullptr;
5303
5304 if (IsEndOfEncoding())
5305 return Name;
5306
5307 Node *Attrs = nullptr;
5308 if (consumeIf("Ua9enable_ifI")) {
5309 size_t BeforeArgs = Names.size();
5310 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005311 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005312 if (Arg == nullptr)
5313 return nullptr;
5314 Names.push_back(Arg);
5315 }
5316 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00005317 if (!Attrs)
5318 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005319 }
5320
5321 Node *ReturnType = nullptr;
5322 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00005323 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005324 if (ReturnType == nullptr)
5325 return nullptr;
5326 }
5327
5328 if (consumeIf('v'))
5329 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5330 Attrs, NameInfo.CVQualifiers,
5331 NameInfo.ReferenceQualifier);
5332
5333 size_t ParamsBegin = Names.size();
5334 do {
Pavel Labathba825192018-10-16 14:29:14 +00005335 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005336 if (Ty == nullptr)
5337 return nullptr;
5338 Names.push_back(Ty);
5339 } while (!IsEndOfEncoding());
5340
5341 return make<FunctionEncoding>(ReturnType, Name,
5342 popTrailingNodeArray(ParamsBegin),
5343 Attrs, NameInfo.CVQualifiers,
5344 NameInfo.ReferenceQualifier);
5345}
5346
5347template <class Float>
5348struct FloatData;
5349
5350template <>
5351struct FloatData<float>
5352{
5353 static const size_t mangled_size = 8;
5354 static const size_t max_demangled_size = 24;
5355 static constexpr const char* spec = "%af";
5356};
5357
5358template <>
5359struct FloatData<double>
5360{
5361 static const size_t mangled_size = 16;
5362 static const size_t max_demangled_size = 32;
5363 static constexpr const char* spec = "%a";
5364};
5365
5366template <>
5367struct FloatData<long double>
5368{
5369#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5370 defined(__wasm__)
5371 static const size_t mangled_size = 32;
5372#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5373 static const size_t mangled_size = 16;
5374#else
5375 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5376#endif
Elliott Hughes5a360ea2020-04-10 17:42:00 -07005377 // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
5378 // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
5379 // Negatives are one character longer than positives.
5380 // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
5381 // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
5382 static const size_t max_demangled_size = 42;
Richard Smithc20d1442018-08-20 20:14:49 +00005383 static constexpr const char *spec = "%LaL";
5384};
5385
Pavel Labathba825192018-10-16 14:29:14 +00005386template <typename Alloc, typename Derived>
5387template <class Float>
5388Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005389 const size_t N = FloatData<Float>::mangled_size;
5390 if (numLeft() <= N)
5391 return nullptr;
5392 StringView Data(First, First + N);
5393 for (char C : Data)
5394 if (!std::isxdigit(C))
5395 return nullptr;
5396 First += N;
5397 if (!consumeIf('E'))
5398 return nullptr;
5399 return make<FloatLiteralImpl<Float>>(Data);
5400}
5401
5402// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005403template <typename Alloc, typename Derived>
5404bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005405 if (!(look() >= '0' && look() <= '9') &&
5406 !(look() >= 'A' && look() <= 'Z'))
5407 return true;
5408
5409 size_t Id = 0;
5410 while (true) {
5411 if (look() >= '0' && look() <= '9') {
5412 Id *= 36;
5413 Id += static_cast<size_t>(look() - '0');
5414 } else if (look() >= 'A' && look() <= 'Z') {
5415 Id *= 36;
5416 Id += static_cast<size_t>(look() - 'A') + 10;
5417 } else {
5418 *Out = Id;
5419 return false;
5420 }
5421 ++First;
5422 }
5423}
5424
5425// <substitution> ::= S <seq-id> _
5426// ::= S_
5427// <substitution> ::= Sa # ::std::allocator
5428// <substitution> ::= Sb # ::std::basic_string
5429// <substitution> ::= Ss # ::std::basic_string < char,
5430// ::std::char_traits<char>,
5431// ::std::allocator<char> >
5432// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5433// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5434// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Pavel Labathba825192018-10-16 14:29:14 +00005435template <typename Derived, typename Alloc>
5436Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005437 if (!consumeIf('S'))
5438 return nullptr;
5439
5440 if (std::islower(look())) {
5441 Node *SpecialSub;
5442 switch (look()) {
5443 case 'a':
5444 ++First;
5445 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::allocator);
5446 break;
5447 case 'b':
5448 ++First;
5449 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::basic_string);
5450 break;
5451 case 's':
5452 ++First;
5453 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::string);
5454 break;
5455 case 'i':
5456 ++First;
5457 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::istream);
5458 break;
5459 case 'o':
5460 ++First;
5461 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::ostream);
5462 break;
5463 case 'd':
5464 ++First;
5465 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::iostream);
5466 break;
5467 default:
5468 return nullptr;
5469 }
Richard Smithb485b352018-08-24 23:30:26 +00005470 if (!SpecialSub)
5471 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005472 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5473 // has ABI tags, the tags are appended to the substitution; the result is a
5474 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005475 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005476 if (WithTags != SpecialSub) {
5477 Subs.push_back(WithTags);
5478 SpecialSub = WithTags;
5479 }
5480 return SpecialSub;
5481 }
5482
5483 // ::= S_
5484 if (consumeIf('_')) {
5485 if (Subs.empty())
5486 return nullptr;
5487 return Subs[0];
5488 }
5489
5490 // ::= S <seq-id> _
5491 size_t Index = 0;
5492 if (parseSeqId(&Index))
5493 return nullptr;
5494 ++Index;
5495 if (!consumeIf('_') || Index >= Subs.size())
5496 return nullptr;
5497 return Subs[Index];
5498}
5499
5500// <template-param> ::= T_ # first template parameter
5501// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005502// ::= TL <level-1> __
5503// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005504template <typename Derived, typename Alloc>
5505Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005506 if (!consumeIf('T'))
5507 return nullptr;
5508
Richard Smithdf1c14c2019-09-06 23:53:21 +00005509 size_t Level = 0;
5510 if (consumeIf('L')) {
5511 if (parsePositiveInteger(&Level))
5512 return nullptr;
5513 ++Level;
5514 if (!consumeIf('_'))
5515 return nullptr;
5516 }
5517
Richard Smithc20d1442018-08-20 20:14:49 +00005518 size_t Index = 0;
5519 if (!consumeIf('_')) {
5520 if (parsePositiveInteger(&Index))
5521 return nullptr;
5522 ++Index;
5523 if (!consumeIf('_'))
5524 return nullptr;
5525 }
5526
Richard Smithc20d1442018-08-20 20:14:49 +00005527 // If we're in a context where this <template-param> refers to a
5528 // <template-arg> further ahead in the mangled name (currently just conversion
5529 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005530 // This can only happen at the outermost level.
5531 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005532 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5533 if (!ForwardRef)
5534 return nullptr;
5535 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5536 ForwardTemplateRefs.push_back(
5537 static_cast<ForwardTemplateReference *>(ForwardRef));
5538 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005539 }
5540
Richard Smithdf1c14c2019-09-06 23:53:21 +00005541 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5542 Index >= TemplateParams[Level]->size()) {
5543 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5544 // list are mangled as the corresponding artificial template type parameter.
5545 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5546 // This will be popped by the ScopedTemplateParamList in
5547 // parseUnnamedTypeName.
5548 if (Level == TemplateParams.size())
5549 TemplateParams.push_back(nullptr);
5550 return make<NameType>("auto");
5551 }
5552
Richard Smithc20d1442018-08-20 20:14:49 +00005553 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005554 }
5555
5556 return (*TemplateParams[Level])[Index];
5557}
5558
5559// <template-param-decl> ::= Ty # type parameter
5560// ::= Tn <type> # non-type parameter
5561// ::= Tt <template-param-decl>* E # template parameter
5562// ::= Tp <template-param-decl> # parameter pack
5563template <typename Derived, typename Alloc>
5564Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5565 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5566 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5567 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5568 if (N) TemplateParams.back()->push_back(N);
5569 return N;
5570 };
5571
5572 if (consumeIf("Ty")) {
5573 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5574 if (!Name)
5575 return nullptr;
5576 return make<TypeTemplateParamDecl>(Name);
5577 }
5578
5579 if (consumeIf("Tn")) {
5580 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5581 if (!Name)
5582 return nullptr;
5583 Node *Type = parseType();
5584 if (!Type)
5585 return nullptr;
5586 return make<NonTypeTemplateParamDecl>(Name, Type);
5587 }
5588
5589 if (consumeIf("Tt")) {
5590 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5591 if (!Name)
5592 return nullptr;
5593 size_t ParamsBegin = Names.size();
5594 ScopedTemplateParamList TemplateTemplateParamParams(this);
5595 while (!consumeIf("E")) {
5596 Node *P = parseTemplateParamDecl();
5597 if (!P)
5598 return nullptr;
5599 Names.push_back(P);
5600 }
5601 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5602 return make<TemplateTemplateParamDecl>(Name, Params);
5603 }
5604
5605 if (consumeIf("Tp")) {
5606 Node *P = parseTemplateParamDecl();
5607 if (!P)
5608 return nullptr;
5609 return make<TemplateParamPackDecl>(P);
5610 }
5611
5612 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005613}
5614
5615// <template-arg> ::= <type> # type or template
5616// ::= X <expression> E # expression
5617// ::= <expr-primary> # simple expressions
5618// ::= J <template-arg>* E # argument pack
5619// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005620template <typename Derived, typename Alloc>
5621Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005622 switch (look()) {
5623 case 'X': {
5624 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005625 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005626 if (Arg == nullptr || !consumeIf('E'))
5627 return nullptr;
5628 return Arg;
5629 }
5630 case 'J': {
5631 ++First;
5632 size_t ArgsBegin = Names.size();
5633 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005634 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005635 if (Arg == nullptr)
5636 return nullptr;
5637 Names.push_back(Arg);
5638 }
5639 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5640 return make<TemplateArgumentPack>(Args);
5641 }
5642 case 'L': {
5643 // ::= LZ <encoding> E # extension
5644 if (look(1) == 'Z') {
5645 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005646 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005647 if (Arg == nullptr || !consumeIf('E'))
5648 return nullptr;
5649 return Arg;
5650 }
5651 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005652 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005653 }
5654 default:
Pavel Labathba825192018-10-16 14:29:14 +00005655 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005656 }
5657}
5658
5659// <template-args> ::= I <template-arg>* E
5660// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005661template <typename Derived, typename Alloc>
5662Node *
5663AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005664 if (!consumeIf('I'))
5665 return nullptr;
5666
5667 // <template-params> refer to the innermost <template-args>. Clear out any
5668 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005669 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005670 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005671 TemplateParams.push_back(&OuterTemplateParams);
5672 OuterTemplateParams.clear();
5673 }
Richard Smithc20d1442018-08-20 20:14:49 +00005674
5675 size_t ArgsBegin = Names.size();
5676 while (!consumeIf('E')) {
5677 if (TagTemplates) {
5678 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005679 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005680 TemplateParams = std::move(OldParams);
5681 if (Arg == nullptr)
5682 return nullptr;
5683 Names.push_back(Arg);
5684 Node *TableEntry = Arg;
5685 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5686 TableEntry = make<ParameterPack>(
5687 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005688 if (!TableEntry)
5689 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005690 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005691 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005692 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005693 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005694 if (Arg == nullptr)
5695 return nullptr;
5696 Names.push_back(Arg);
5697 }
5698 }
5699 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5700}
5701
5702// <mangled-name> ::= _Z <encoding>
5703// ::= <type>
5704// extension ::= ___Z <encoding> _block_invoke
5705// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5706// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005707template <typename Derived, typename Alloc>
5708Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005709 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005710 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005711 if (Encoding == nullptr)
5712 return nullptr;
5713 if (look() == '.') {
5714 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5715 First = Last;
5716 }
5717 if (numLeft() != 0)
5718 return nullptr;
5719 return Encoding;
5720 }
5721
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005722 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005723 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005724 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5725 return nullptr;
5726 bool RequireNumber = consumeIf('_');
5727 if (parseNumber().empty() && RequireNumber)
5728 return nullptr;
5729 if (look() == '.')
5730 First = Last;
5731 if (numLeft() != 0)
5732 return nullptr;
5733 return make<SpecialName>("invocation function for block in ", Encoding);
5734 }
5735
Pavel Labathba825192018-10-16 14:29:14 +00005736 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005737 if (numLeft() != 0)
5738 return nullptr;
5739 return Ty;
5740}
5741
Pavel Labathba825192018-10-16 14:29:14 +00005742template <typename Alloc>
5743struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5744 using AbstractManglingParser<ManglingParser<Alloc>,
5745 Alloc>::AbstractManglingParser;
5746};
5747
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005748DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005749
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005750#endif // DEMANGLE_ITANIUMDEMANGLE_H