blob: b25139d8a72baf46ceaf40d114699e9239bcff11 [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
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -0800313 // Print the "left" side of this Node into OutputBuffer.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700314 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
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08001213 // Setup OutputBuffer for a pack expansion, unless we're already expanding
1214 // one.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001215 void initializePackExpansion(OutputBuffer &OB) const {
1216 if (OB.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
1217 OB.CurrentPackMax = static_cast<unsigned>(Data.size());
1218 OB.CurrentPackIndex = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00001219 }
1220 }
1221
1222public:
1223 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
1224 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
1225 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1226 return P->ArrayCache == Cache::No;
1227 }))
1228 ArrayCache = Cache::No;
1229 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1230 return P->FunctionCache == Cache::No;
1231 }))
1232 FunctionCache = Cache::No;
1233 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1234 return P->RHSComponentCache == Cache::No;
1235 }))
1236 RHSComponentCache = Cache::No;
1237 }
1238
1239 template<typename Fn> void match(Fn F) const { F(Data); }
1240
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001241 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
1242 initializePackExpansion(OB);
1243 size_t Idx = OB.CurrentPackIndex;
1244 return Idx < Data.size() && Data[Idx]->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001245 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001246 bool hasArraySlow(OutputBuffer &OB) const override {
1247 initializePackExpansion(OB);
1248 size_t Idx = OB.CurrentPackIndex;
1249 return Idx < Data.size() && Data[Idx]->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001250 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001251 bool hasFunctionSlow(OutputBuffer &OB) const override {
1252 initializePackExpansion(OB);
1253 size_t Idx = OB.CurrentPackIndex;
1254 return Idx < Data.size() && Data[Idx]->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001255 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001256 const Node *getSyntaxNode(OutputBuffer &OB) const override {
1257 initializePackExpansion(OB);
1258 size_t Idx = OB.CurrentPackIndex;
1259 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(OB) : this;
Richard Smithc20d1442018-08-20 20:14:49 +00001260 }
1261
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001262 void printLeft(OutputBuffer &OB) const override {
1263 initializePackExpansion(OB);
1264 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001265 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001266 Data[Idx]->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001267 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001268 void printRight(OutputBuffer &OB) const override {
1269 initializePackExpansion(OB);
1270 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001271 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001272 Data[Idx]->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001273 }
1274};
1275
1276/// A variadic template argument. This node represents an occurrence of
1277/// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1278/// one of it's Elements is. The parser inserts a ParameterPack into the
1279/// TemplateParams table if the <template-args> this pack belongs to apply to an
1280/// <encoding>.
1281class TemplateArgumentPack final : public Node {
1282 NodeArray Elements;
1283public:
1284 TemplateArgumentPack(NodeArray Elements_)
1285 : Node(KTemplateArgumentPack), Elements(Elements_) {}
1286
1287 template<typename Fn> void match(Fn F) const { F(Elements); }
1288
1289 NodeArray getElements() const { return Elements; }
1290
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001291 void printLeft(OutputBuffer &OB) const override {
1292 Elements.printWithComma(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001293 }
1294};
1295
1296/// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1297/// which each have Child->ParameterPackSize elements.
1298class ParameterPackExpansion final : public Node {
1299 const Node *Child;
1300
1301public:
1302 ParameterPackExpansion(const Node *Child_)
1303 : Node(KParameterPackExpansion), Child(Child_) {}
1304
1305 template<typename Fn> void match(Fn F) const { F(Child); }
1306
1307 const Node *getChild() const { return Child; }
1308
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001309 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001310 constexpr unsigned Max = std::numeric_limits<unsigned>::max();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001311 SwapAndRestore<unsigned> SavePackIdx(OB.CurrentPackIndex, Max);
1312 SwapAndRestore<unsigned> SavePackMax(OB.CurrentPackMax, Max);
1313 size_t StreamPos = OB.getCurrentPosition();
Richard Smithc20d1442018-08-20 20:14:49 +00001314
1315 // Print the first element in the pack. If Child contains a ParameterPack,
1316 // it will set up S.CurrentPackMax and print the first element.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001317 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001318
1319 // No ParameterPack was found in Child. This can occur if we've found a pack
1320 // expansion on a <function-param>.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001321 if (OB.CurrentPackMax == Max) {
1322 OB += "...";
Richard Smithc20d1442018-08-20 20:14:49 +00001323 return;
1324 }
1325
1326 // We found a ParameterPack, but it has no elements. Erase whatever we may
1327 // of printed.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001328 if (OB.CurrentPackMax == 0) {
1329 OB.setCurrentPosition(StreamPos);
Richard Smithc20d1442018-08-20 20:14:49 +00001330 return;
1331 }
1332
1333 // Else, iterate through the rest of the elements in the pack.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001334 for (unsigned I = 1, E = OB.CurrentPackMax; I < E; ++I) {
1335 OB += ", ";
1336 OB.CurrentPackIndex = I;
1337 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001338 }
1339 }
1340};
1341
1342class TemplateArgs final : public Node {
1343 NodeArray Params;
1344
1345public:
1346 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1347
1348 template<typename Fn> void match(Fn F) const { F(Params); }
1349
1350 NodeArray getParams() { return Params; }
1351
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001352 void printLeft(OutputBuffer &OB) const override {
1353 OB += "<";
1354 Params.printWithComma(OB);
1355 if (OB.back() == '>')
1356 OB += " ";
1357 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +00001358 }
1359};
1360
Richard Smithb485b352018-08-24 23:30:26 +00001361/// A forward-reference to a template argument that was not known at the point
1362/// where the template parameter name was parsed in a mangling.
1363///
1364/// This is created when demangling the name of a specialization of a
1365/// conversion function template:
1366///
1367/// \code
1368/// struct A {
1369/// template<typename T> operator T*();
1370/// };
1371/// \endcode
1372///
1373/// When demangling a specialization of the conversion function template, we
1374/// encounter the name of the template (including the \c T) before we reach
1375/// the template argument list, so we cannot substitute the parameter name
1376/// for the corresponding argument while parsing. Instead, we create a
1377/// \c ForwardTemplateReference node that is resolved after we parse the
1378/// template arguments.
Richard Smithc20d1442018-08-20 20:14:49 +00001379struct ForwardTemplateReference : Node {
1380 size_t Index;
1381 Node *Ref = nullptr;
1382
1383 // If we're currently printing this node. It is possible (though invalid) for
1384 // a forward template reference to refer to itself via a substitution. This
1385 // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1386 // out if more than one print* function is active.
1387 mutable bool Printing = false;
1388
1389 ForwardTemplateReference(size_t Index_)
1390 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1391 Cache::Unknown),
1392 Index(Index_) {}
1393
1394 // We don't provide a matcher for these, because the value of the node is
1395 // not determined by its construction parameters, and it generally needs
1396 // special handling.
1397 template<typename Fn> void match(Fn F) const = delete;
1398
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001399 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001400 if (Printing)
1401 return false;
1402 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001403 return Ref->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001404 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001405 bool hasArraySlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001406 if (Printing)
1407 return false;
1408 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001409 return Ref->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001410 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001411 bool hasFunctionSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001412 if (Printing)
1413 return false;
1414 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001415 return Ref->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001416 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001417 const Node *getSyntaxNode(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001418 if (Printing)
1419 return this;
1420 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001421 return Ref->getSyntaxNode(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001422 }
1423
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001424 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001425 if (Printing)
1426 return;
1427 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001428 Ref->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001429 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001430 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001431 if (Printing)
1432 return;
1433 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001434 Ref->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001435 }
1436};
1437
1438struct NameWithTemplateArgs : Node {
1439 // name<template_args>
1440 Node *Name;
1441 Node *TemplateArgs;
1442
1443 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1444 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1445
1446 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1447
1448 StringView getBaseName() const override { return Name->getBaseName(); }
1449
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001450 void printLeft(OutputBuffer &OB) const override {
1451 Name->print(OB);
1452 TemplateArgs->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001453 }
1454};
1455
1456class GlobalQualifiedName final : public Node {
1457 Node *Child;
1458
1459public:
1460 GlobalQualifiedName(Node* Child_)
1461 : Node(KGlobalQualifiedName), Child(Child_) {}
1462
1463 template<typename Fn> void match(Fn F) const { F(Child); }
1464
1465 StringView getBaseName() const override { return Child->getBaseName(); }
1466
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001467 void printLeft(OutputBuffer &OB) const override {
1468 OB += "::";
1469 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001470 }
1471};
1472
1473struct StdQualifiedName : Node {
1474 Node *Child;
1475
1476 StdQualifiedName(Node *Child_) : Node(KStdQualifiedName), Child(Child_) {}
1477
1478 template<typename Fn> void match(Fn F) const { F(Child); }
1479
1480 StringView getBaseName() const override { return Child->getBaseName(); }
1481
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001482 void printLeft(OutputBuffer &OB) const override {
1483 OB += "std::";
1484 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001485 }
1486};
1487
1488enum class SpecialSubKind {
1489 allocator,
1490 basic_string,
1491 string,
1492 istream,
1493 ostream,
1494 iostream,
1495};
1496
1497class ExpandedSpecialSubstitution final : public Node {
1498 SpecialSubKind SSK;
1499
1500public:
1501 ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1502 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1503
1504 template<typename Fn> void match(Fn F) const { F(SSK); }
1505
1506 StringView getBaseName() const override {
1507 switch (SSK) {
1508 case SpecialSubKind::allocator:
1509 return StringView("allocator");
1510 case SpecialSubKind::basic_string:
1511 return StringView("basic_string");
1512 case SpecialSubKind::string:
1513 return StringView("basic_string");
1514 case SpecialSubKind::istream:
1515 return StringView("basic_istream");
1516 case SpecialSubKind::ostream:
1517 return StringView("basic_ostream");
1518 case SpecialSubKind::iostream:
1519 return StringView("basic_iostream");
1520 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001521 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001522 }
1523
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001524 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001525 switch (SSK) {
1526 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001527 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001528 break;
1529 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001530 OB += "std::basic_string";
Richard Smithb485b352018-08-24 23:30:26 +00001531 break;
Richard Smithc20d1442018-08-20 20:14:49 +00001532 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001533 OB += "std::basic_string<char, std::char_traits<char>, "
1534 "std::allocator<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001535 break;
1536 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001537 OB += "std::basic_istream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001538 break;
1539 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001540 OB += "std::basic_ostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001541 break;
1542 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001543 OB += "std::basic_iostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001544 break;
1545 }
1546 }
1547};
1548
1549class SpecialSubstitution final : public Node {
1550public:
1551 SpecialSubKind SSK;
1552
1553 SpecialSubstitution(SpecialSubKind SSK_)
1554 : Node(KSpecialSubstitution), SSK(SSK_) {}
1555
1556 template<typename Fn> void match(Fn F) const { F(SSK); }
1557
1558 StringView getBaseName() const override {
1559 switch (SSK) {
1560 case SpecialSubKind::allocator:
1561 return StringView("allocator");
1562 case SpecialSubKind::basic_string:
1563 return StringView("basic_string");
1564 case SpecialSubKind::string:
1565 return StringView("string");
1566 case SpecialSubKind::istream:
1567 return StringView("istream");
1568 case SpecialSubKind::ostream:
1569 return StringView("ostream");
1570 case SpecialSubKind::iostream:
1571 return StringView("iostream");
1572 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001573 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001574 }
1575
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001576 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001577 switch (SSK) {
1578 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001579 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001580 break;
1581 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001582 OB += "std::basic_string";
Richard Smithc20d1442018-08-20 20:14:49 +00001583 break;
1584 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001585 OB += "std::string";
Richard Smithc20d1442018-08-20 20:14:49 +00001586 break;
1587 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001588 OB += "std::istream";
Richard Smithc20d1442018-08-20 20:14:49 +00001589 break;
1590 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001591 OB += "std::ostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001592 break;
1593 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001594 OB += "std::iostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001595 break;
1596 }
1597 }
1598};
1599
1600class CtorDtorName final : public Node {
1601 const Node *Basename;
1602 const bool IsDtor;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001603 const int Variant;
Richard Smithc20d1442018-08-20 20:14:49 +00001604
1605public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001606 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1607 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1608 Variant(Variant_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001609
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001610 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
Richard Smithc20d1442018-08-20 20:14:49 +00001611
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001612 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001613 if (IsDtor)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001614 OB += "~";
1615 OB += Basename->getBaseName();
Richard Smithc20d1442018-08-20 20:14:49 +00001616 }
1617};
1618
1619class DtorName : public Node {
1620 const Node *Base;
1621
1622public:
1623 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1624
1625 template<typename Fn> void match(Fn F) const { F(Base); }
1626
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001627 void printLeft(OutputBuffer &OB) const override {
1628 OB += "~";
1629 Base->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001630 }
1631};
1632
1633class UnnamedTypeName : public Node {
1634 const StringView Count;
1635
1636public:
1637 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1638
1639 template<typename Fn> void match(Fn F) const { F(Count); }
1640
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001641 void printLeft(OutputBuffer &OB) const override {
1642 OB += "'unnamed";
1643 OB += Count;
1644 OB += "\'";
Richard Smithc20d1442018-08-20 20:14:49 +00001645 }
1646};
1647
1648class ClosureTypeName : public Node {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001649 NodeArray TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00001650 NodeArray Params;
1651 StringView Count;
1652
1653public:
Richard Smithdf1c14c2019-09-06 23:53:21 +00001654 ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_,
1655 StringView Count_)
1656 : Node(KClosureTypeName), TemplateParams(TemplateParams_),
1657 Params(Params_), Count(Count_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001658
Richard Smithdf1c14c2019-09-06 23:53:21 +00001659 template<typename Fn> void match(Fn F) const {
1660 F(TemplateParams, Params, Count);
1661 }
1662
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001663 void printDeclarator(OutputBuffer &OB) const {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001664 if (!TemplateParams.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001665 OB += "<";
1666 TemplateParams.printWithComma(OB);
1667 OB += ">";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001668 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001669 OB += "(";
1670 Params.printWithComma(OB);
1671 OB += ")";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001672 }
Richard Smithc20d1442018-08-20 20:14:49 +00001673
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001674 void printLeft(OutputBuffer &OB) const override {
1675 OB += "\'lambda";
1676 OB += Count;
1677 OB += "\'";
1678 printDeclarator(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001679 }
1680};
1681
1682class StructuredBindingName : public Node {
1683 NodeArray Bindings;
1684public:
1685 StructuredBindingName(NodeArray Bindings_)
1686 : Node(KStructuredBindingName), Bindings(Bindings_) {}
1687
1688 template<typename Fn> void match(Fn F) const { F(Bindings); }
1689
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001690 void printLeft(OutputBuffer &OB) const override {
1691 OB += '[';
1692 Bindings.printWithComma(OB);
1693 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00001694 }
1695};
1696
1697// -- Expression Nodes --
1698
1699class BinaryExpr : public Node {
1700 const Node *LHS;
1701 const StringView InfixOperator;
1702 const Node *RHS;
1703
1704public:
1705 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
1706 : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
1707 }
1708
1709 template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
1710
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001711 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001712 // might be a template argument expression, then we need to disambiguate
1713 // with parens.
1714 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001715 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +00001716
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001717 OB += "(";
1718 LHS->print(OB);
1719 OB += ") ";
1720 OB += InfixOperator;
1721 OB += " (";
1722 RHS->print(OB);
1723 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001724
1725 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001726 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001727 }
1728};
1729
1730class ArraySubscriptExpr : public Node {
1731 const Node *Op1;
1732 const Node *Op2;
1733
1734public:
1735 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
1736 : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
1737
1738 template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
1739
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001740 void printLeft(OutputBuffer &OB) const override {
1741 OB += "(";
1742 Op1->print(OB);
1743 OB += ")[";
1744 Op2->print(OB);
1745 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001746 }
1747};
1748
1749class PostfixExpr : public Node {
1750 const Node *Child;
1751 const StringView Operator;
1752
1753public:
1754 PostfixExpr(const Node *Child_, StringView Operator_)
1755 : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
1756
1757 template<typename Fn> void match(Fn F) const { F(Child, Operator); }
1758
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001759 void printLeft(OutputBuffer &OB) const override {
1760 OB += "(";
1761 Child->print(OB);
1762 OB += ")";
1763 OB += Operator;
Richard Smithc20d1442018-08-20 20:14:49 +00001764 }
1765};
1766
1767class ConditionalExpr : public Node {
1768 const Node *Cond;
1769 const Node *Then;
1770 const Node *Else;
1771
1772public:
1773 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
1774 : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
1775
1776 template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
1777
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001778 void printLeft(OutputBuffer &OB) const override {
1779 OB += "(";
1780 Cond->print(OB);
1781 OB += ") ? (";
1782 Then->print(OB);
1783 OB += ") : (";
1784 Else->print(OB);
1785 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001786 }
1787};
1788
1789class MemberExpr : public Node {
1790 const Node *LHS;
1791 const StringView Kind;
1792 const Node *RHS;
1793
1794public:
1795 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
1796 : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1797
1798 template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
1799
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001800 void printLeft(OutputBuffer &OB) const override {
1801 LHS->print(OB);
1802 OB += Kind;
1803 RHS->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001804 }
1805};
1806
Richard Smith1865d2f2020-10-22 19:29:36 -07001807class SubobjectExpr : public Node {
1808 const Node *Type;
1809 const Node *SubExpr;
1810 StringView Offset;
1811 NodeArray UnionSelectors;
1812 bool OnePastTheEnd;
1813
1814public:
1815 SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_,
1816 NodeArray UnionSelectors_, bool OnePastTheEnd_)
1817 : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_),
1818 UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {}
1819
1820 template<typename Fn> void match(Fn F) const {
1821 F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd);
1822 }
1823
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001824 void printLeft(OutputBuffer &OB) const override {
1825 SubExpr->print(OB);
1826 OB += ".<";
1827 Type->print(OB);
1828 OB += " at offset ";
Richard Smith1865d2f2020-10-22 19:29:36 -07001829 if (Offset.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001830 OB += "0";
Richard Smith1865d2f2020-10-22 19:29:36 -07001831 } else if (Offset[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001832 OB += "-";
1833 OB += Offset.dropFront();
Richard Smith1865d2f2020-10-22 19:29:36 -07001834 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001835 OB += Offset;
Richard Smith1865d2f2020-10-22 19:29:36 -07001836 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001837 OB += ">";
Richard Smith1865d2f2020-10-22 19:29:36 -07001838 }
1839};
1840
Richard Smithc20d1442018-08-20 20:14:49 +00001841class EnclosingExpr : public Node {
1842 const StringView Prefix;
1843 const Node *Infix;
1844 const StringView Postfix;
1845
1846public:
1847 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1848 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1849 Postfix(Postfix_) {}
1850
1851 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1852
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001853 void printLeft(OutputBuffer &OB) const override {
1854 OB += Prefix;
1855 Infix->print(OB);
1856 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +00001857 }
1858};
1859
1860class CastExpr : public Node {
1861 // cast_kind<to>(from)
1862 const StringView CastKind;
1863 const Node *To;
1864 const Node *From;
1865
1866public:
1867 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1868 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1869
1870 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1871
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001872 void printLeft(OutputBuffer &OB) const override {
1873 OB += CastKind;
1874 OB += "<";
1875 To->printLeft(OB);
1876 OB += ">(";
1877 From->printLeft(OB);
1878 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001879 }
1880};
1881
1882class SizeofParamPackExpr : public Node {
1883 const Node *Pack;
1884
1885public:
1886 SizeofParamPackExpr(const Node *Pack_)
1887 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1888
1889 template<typename Fn> void match(Fn F) const { F(Pack); }
1890
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001891 void printLeft(OutputBuffer &OB) const override {
1892 OB += "sizeof...(";
Richard Smithc20d1442018-08-20 20:14:49 +00001893 ParameterPackExpansion PPE(Pack);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001894 PPE.printLeft(OB);
1895 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001896 }
1897};
1898
1899class CallExpr : public Node {
1900 const Node *Callee;
1901 NodeArray Args;
1902
1903public:
1904 CallExpr(const Node *Callee_, NodeArray Args_)
1905 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1906
1907 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1908
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001909 void printLeft(OutputBuffer &OB) const override {
1910 Callee->print(OB);
1911 OB += "(";
1912 Args.printWithComma(OB);
1913 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001914 }
1915};
1916
1917class NewExpr : public Node {
1918 // new (expr_list) type(init_list)
1919 NodeArray ExprList;
1920 Node *Type;
1921 NodeArray InitList;
1922 bool IsGlobal; // ::operator new ?
1923 bool IsArray; // new[] ?
1924public:
1925 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1926 bool IsArray_)
1927 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1928 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1929
1930 template<typename Fn> void match(Fn F) const {
1931 F(ExprList, Type, InitList, IsGlobal, IsArray);
1932 }
1933
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001934 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001935 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001936 OB += "::operator ";
1937 OB += "new";
Richard Smithc20d1442018-08-20 20:14:49 +00001938 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001939 OB += "[]";
1940 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00001941 if (!ExprList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001942 OB += "(";
1943 ExprList.printWithComma(OB);
1944 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001945 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001946 Type->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001947 if (!InitList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001948 OB += "(";
1949 InitList.printWithComma(OB);
1950 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001951 }
Richard Smithc20d1442018-08-20 20:14:49 +00001952 }
1953};
1954
1955class DeleteExpr : public Node {
1956 Node *Op;
1957 bool IsGlobal;
1958 bool IsArray;
1959
1960public:
1961 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1962 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1963
1964 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1965
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001966 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001967 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001968 OB += "::";
1969 OB += "delete";
Richard Smithc20d1442018-08-20 20:14:49 +00001970 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001971 OB += "[] ";
1972 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001973 }
1974};
1975
1976class PrefixExpr : public Node {
1977 StringView Prefix;
1978 Node *Child;
1979
1980public:
1981 PrefixExpr(StringView Prefix_, Node *Child_)
1982 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1983
1984 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1985
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001986 void printLeft(OutputBuffer &OB) const override {
1987 OB += Prefix;
1988 OB += "(";
1989 Child->print(OB);
1990 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001991 }
1992};
1993
1994class FunctionParam : public Node {
1995 StringView Number;
1996
1997public:
1998 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
1999
2000 template<typename Fn> void match(Fn F) const { F(Number); }
2001
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002002 void printLeft(OutputBuffer &OB) const override {
2003 OB += "fp";
2004 OB += Number;
Richard Smithc20d1442018-08-20 20:14:49 +00002005 }
2006};
2007
2008class ConversionExpr : public Node {
2009 const Node *Type;
2010 NodeArray Expressions;
2011
2012public:
2013 ConversionExpr(const Node *Type_, NodeArray Expressions_)
2014 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
2015
2016 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
2017
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002018 void printLeft(OutputBuffer &OB) const override {
2019 OB += "(";
2020 Type->print(OB);
2021 OB += ")(";
2022 Expressions.printWithComma(OB);
2023 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002024 }
2025};
2026
Richard Smith1865d2f2020-10-22 19:29:36 -07002027class PointerToMemberConversionExpr : public Node {
2028 const Node *Type;
2029 const Node *SubExpr;
2030 StringView Offset;
2031
2032public:
2033 PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_,
2034 StringView Offset_)
2035 : Node(KPointerToMemberConversionExpr), Type(Type_), SubExpr(SubExpr_),
2036 Offset(Offset_) {}
2037
2038 template<typename Fn> void match(Fn F) const { F(Type, SubExpr, Offset); }
2039
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002040 void printLeft(OutputBuffer &OB) const override {
2041 OB += "(";
2042 Type->print(OB);
2043 OB += ")(";
2044 SubExpr->print(OB);
2045 OB += ")";
Richard Smith1865d2f2020-10-22 19:29:36 -07002046 }
2047};
2048
Richard Smithc20d1442018-08-20 20:14:49 +00002049class InitListExpr : public Node {
2050 const Node *Ty;
2051 NodeArray Inits;
2052public:
2053 InitListExpr(const Node *Ty_, NodeArray Inits_)
2054 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
2055
2056 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
2057
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002058 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002059 if (Ty)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002060 Ty->print(OB);
2061 OB += '{';
2062 Inits.printWithComma(OB);
2063 OB += '}';
Richard Smithc20d1442018-08-20 20:14:49 +00002064 }
2065};
2066
2067class BracedExpr : public Node {
2068 const Node *Elem;
2069 const Node *Init;
2070 bool IsArray;
2071public:
2072 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
2073 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
2074
2075 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
2076
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002077 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002078 if (IsArray) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002079 OB += '[';
2080 Elem->print(OB);
2081 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002082 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002083 OB += '.';
2084 Elem->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002085 }
2086 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002087 OB += " = ";
2088 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002089 }
2090};
2091
2092class BracedRangeExpr : public Node {
2093 const Node *First;
2094 const Node *Last;
2095 const Node *Init;
2096public:
2097 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
2098 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
2099
2100 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
2101
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002102 void printLeft(OutputBuffer &OB) const override {
2103 OB += '[';
2104 First->print(OB);
2105 OB += " ... ";
2106 Last->print(OB);
2107 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002108 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002109 OB += " = ";
2110 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002111 }
2112};
2113
2114class FoldExpr : public Node {
2115 const Node *Pack, *Init;
2116 StringView OperatorName;
2117 bool IsLeftFold;
2118
2119public:
2120 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
2121 const Node *Init_)
2122 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
2123 IsLeftFold(IsLeftFold_) {}
2124
2125 template<typename Fn> void match(Fn F) const {
2126 F(IsLeftFold, OperatorName, Pack, Init);
2127 }
2128
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002129 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002130 auto PrintPack = [&] {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002131 OB += '(';
2132 ParameterPackExpansion(Pack).print(OB);
2133 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002134 };
2135
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002136 OB += '(';
Richard Smithc20d1442018-08-20 20:14:49 +00002137
2138 if (IsLeftFold) {
2139 // init op ... op pack
2140 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002141 Init->print(OB);
2142 OB += ' ';
2143 OB += OperatorName;
2144 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002145 }
2146 // ... op pack
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002147 OB += "... ";
2148 OB += OperatorName;
2149 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002150 PrintPack();
2151 } else { // !IsLeftFold
2152 // pack op ...
2153 PrintPack();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002154 OB += ' ';
2155 OB += OperatorName;
2156 OB += " ...";
Richard Smithc20d1442018-08-20 20:14:49 +00002157 // pack op ... op init
2158 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002159 OB += ' ';
2160 OB += OperatorName;
2161 OB += ' ';
2162 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002163 }
2164 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002165 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002166 }
2167};
2168
2169class ThrowExpr : public Node {
2170 const Node *Op;
2171
2172public:
2173 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
2174
2175 template<typename Fn> void match(Fn F) const { F(Op); }
2176
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002177 void printLeft(OutputBuffer &OB) const override {
2178 OB += "throw ";
2179 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002180 }
2181};
2182
2183class BoolExpr : public Node {
2184 bool Value;
2185
2186public:
2187 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
2188
2189 template<typename Fn> void match(Fn F) const { F(Value); }
2190
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002191 void printLeft(OutputBuffer &OB) const override {
2192 OB += Value ? StringView("true") : StringView("false");
Richard Smithc20d1442018-08-20 20:14:49 +00002193 }
2194};
2195
Richard Smithdf1c14c2019-09-06 23:53:21 +00002196class StringLiteral : public Node {
2197 const Node *Type;
2198
2199public:
2200 StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
2201
2202 template<typename Fn> void match(Fn F) const { F(Type); }
2203
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002204 void printLeft(OutputBuffer &OB) const override {
2205 OB += "\"<";
2206 Type->print(OB);
2207 OB += ">\"";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002208 }
2209};
2210
2211class LambdaExpr : public Node {
2212 const Node *Type;
2213
Richard Smithdf1c14c2019-09-06 23:53:21 +00002214public:
2215 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2216
2217 template<typename Fn> void match(Fn F) const { F(Type); }
2218
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002219 void printLeft(OutputBuffer &OB) const override {
2220 OB += "[]";
Richard Smithfb917462019-09-09 22:26:04 +00002221 if (Type->getKind() == KClosureTypeName)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002222 static_cast<const ClosureTypeName *>(Type)->printDeclarator(OB);
2223 OB += "{...}";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002224 }
2225};
2226
Erik Pilkington0a170f12020-05-13 14:13:37 -04002227class EnumLiteral : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +00002228 // ty(integer)
2229 const Node *Ty;
2230 StringView Integer;
2231
2232public:
Erik Pilkington0a170f12020-05-13 14:13:37 -04002233 EnumLiteral(const Node *Ty_, StringView Integer_)
2234 : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00002235
2236 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2237
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002238 void printLeft(OutputBuffer &OB) const override {
2239 OB << "(";
2240 Ty->print(OB);
2241 OB << ")";
Erik Pilkington0a170f12020-05-13 14:13:37 -04002242
2243 if (Integer[0] == 'n')
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002244 OB << "-" << Integer.dropFront(1);
Erik Pilkington0a170f12020-05-13 14:13:37 -04002245 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002246 OB << Integer;
Richard Smithc20d1442018-08-20 20:14:49 +00002247 }
2248};
2249
2250class IntegerLiteral : public Node {
2251 StringView Type;
2252 StringView Value;
2253
2254public:
2255 IntegerLiteral(StringView Type_, StringView Value_)
2256 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2257
2258 template<typename Fn> void match(Fn F) const { F(Type, Value); }
2259
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002260 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002261 if (Type.size() > 3) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002262 OB += "(";
2263 OB += Type;
2264 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002265 }
2266
2267 if (Value[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002268 OB += "-";
2269 OB += Value.dropFront(1);
Richard Smithc20d1442018-08-20 20:14:49 +00002270 } else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002271 OB += Value;
Richard Smithc20d1442018-08-20 20:14:49 +00002272
2273 if (Type.size() <= 3)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002274 OB += Type;
Richard Smithc20d1442018-08-20 20:14:49 +00002275 }
2276};
2277
2278template <class Float> struct FloatData;
2279
2280namespace float_literal_impl {
2281constexpr Node::Kind getFloatLiteralKind(float *) {
2282 return Node::KFloatLiteral;
2283}
2284constexpr Node::Kind getFloatLiteralKind(double *) {
2285 return Node::KDoubleLiteral;
2286}
2287constexpr Node::Kind getFloatLiteralKind(long double *) {
2288 return Node::KLongDoubleLiteral;
2289}
2290}
2291
2292template <class Float> class FloatLiteralImpl : public Node {
2293 const StringView Contents;
2294
2295 static constexpr Kind KindForClass =
2296 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2297
2298public:
2299 FloatLiteralImpl(StringView Contents_)
2300 : Node(KindForClass), Contents(Contents_) {}
2301
2302 template<typename Fn> void match(Fn F) const { F(Contents); }
2303
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002304 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002305 const char *first = Contents.begin();
2306 const char *last = Contents.end() + 1;
2307
2308 const size_t N = FloatData<Float>::mangled_size;
2309 if (static_cast<std::size_t>(last - first) > N) {
2310 last = first + N;
2311 union {
2312 Float value;
2313 char buf[sizeof(Float)];
2314 };
2315 const char *t = first;
2316 char *e = buf;
2317 for (; t != last; ++t, ++e) {
2318 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2319 : static_cast<unsigned>(*t - 'a' + 10);
2320 ++t;
2321 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2322 : static_cast<unsigned>(*t - 'a' + 10);
2323 *e = static_cast<char>((d1 << 4) + d0);
2324 }
2325#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2326 std::reverse(buf, e);
2327#endif
2328 char num[FloatData<Float>::max_demangled_size] = {0};
2329 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002330 OB += StringView(num, num + n);
Richard Smithc20d1442018-08-20 20:14:49 +00002331 }
2332 }
2333};
2334
2335using FloatLiteral = FloatLiteralImpl<float>;
2336using DoubleLiteral = FloatLiteralImpl<double>;
2337using LongDoubleLiteral = FloatLiteralImpl<long double>;
2338
2339/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2340/// appropriate derived class.
2341template<typename Fn>
2342void Node::visit(Fn F) const {
2343 switch (K) {
2344#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2345 FOR_EACH_NODE_KIND(CASE)
2346#undef CASE
2347 }
2348 assert(0 && "unknown mangling node kind");
2349}
2350
2351/// Determine the kind of a node from its type.
2352template<typename NodeT> struct NodeKind;
2353#define SPECIALIZATION(X) \
2354 template<> struct NodeKind<X> { \
2355 static constexpr Node::Kind Kind = Node::K##X; \
2356 static constexpr const char *name() { return #X; } \
2357 };
2358FOR_EACH_NODE_KIND(SPECIALIZATION)
2359#undef SPECIALIZATION
2360
2361#undef FOR_EACH_NODE_KIND
2362
Pavel Labathba825192018-10-16 14:29:14 +00002363template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002364 const char *First;
2365 const char *Last;
2366
2367 // Name stack, this is used by the parser to hold temporary names that were
2368 // parsed. The parser collapses multiple names into new nodes to construct
2369 // the AST. Once the parser is finished, names.size() == 1.
2370 PODSmallVector<Node *, 32> Names;
2371
2372 // Substitution table. Itanium supports name substitutions as a means of
2373 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2374 // table.
2375 PODSmallVector<Node *, 32> Subs;
2376
Richard Smithdf1c14c2019-09-06 23:53:21 +00002377 using TemplateParamList = PODSmallVector<Node *, 8>;
2378
2379 class ScopedTemplateParamList {
2380 AbstractManglingParser *Parser;
2381 size_t OldNumTemplateParamLists;
2382 TemplateParamList Params;
2383
2384 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04002385 ScopedTemplateParamList(AbstractManglingParser *TheParser)
2386 : Parser(TheParser),
2387 OldNumTemplateParamLists(TheParser->TemplateParams.size()) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002388 Parser->TemplateParams.push_back(&Params);
2389 }
2390 ~ScopedTemplateParamList() {
2391 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2392 Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2393 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002394 };
2395
Richard Smithc20d1442018-08-20 20:14:49 +00002396 // Template parameter table. Like the above, but referenced like "T42_".
2397 // This has a smaller size compared to Subs and Names because it can be
2398 // stored on the stack.
Richard Smithdf1c14c2019-09-06 23:53:21 +00002399 TemplateParamList OuterTemplateParams;
2400
2401 // Lists of template parameters indexed by template parameter depth,
2402 // referenced like "TL2_4_". If nonempty, element 0 is always
2403 // OuterTemplateParams; inner elements are always template parameter lists of
2404 // lambda expressions. For a generic lambda with no explicit template
2405 // parameter list, the corresponding parameter list pointer will be null.
2406 PODSmallVector<TemplateParamList *, 4> TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00002407
2408 // Set of unresolved forward <template-param> references. These can occur in a
2409 // conversion operator's type, and are resolved in the enclosing <encoding>.
2410 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2411
Richard Smithc20d1442018-08-20 20:14:49 +00002412 bool TryToParseTemplateArgs = true;
2413 bool PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002414 size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2415
2416 unsigned NumSyntheticTemplateParameters[3] = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002417
2418 Alloc ASTAllocator;
2419
Pavel Labathba825192018-10-16 14:29:14 +00002420 AbstractManglingParser(const char *First_, const char *Last_)
2421 : First(First_), Last(Last_) {}
2422
2423 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002424
2425 void reset(const char *First_, const char *Last_) {
2426 First = First_;
2427 Last = Last_;
2428 Names.clear();
2429 Subs.clear();
2430 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002431 ParsingLambdaParamsAtLevel = (size_t)-1;
Richard Smithc20d1442018-08-20 20:14:49 +00002432 TryToParseTemplateArgs = true;
2433 PermitForwardTemplateReferences = false;
Richard Smith9a2307a2019-09-07 00:11:53 +00002434 for (int I = 0; I != 3; ++I)
2435 NumSyntheticTemplateParameters[I] = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00002436 ASTAllocator.reset();
2437 }
2438
Richard Smithb485b352018-08-24 23:30:26 +00002439 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002440 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2441 }
2442
2443 template <class It> NodeArray makeNodeArray(It begin, It end) {
2444 size_t sz = static_cast<size_t>(end - begin);
2445 void *mem = ASTAllocator.allocateNodeArray(sz);
2446 Node **data = new (mem) Node *[sz];
2447 std::copy(begin, end, data);
2448 return NodeArray(data, sz);
2449 }
2450
2451 NodeArray popTrailingNodeArray(size_t FromPosition) {
2452 assert(FromPosition <= Names.size());
2453 NodeArray res =
2454 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2455 Names.dropBack(FromPosition);
2456 return res;
2457 }
2458
2459 bool consumeIf(StringView S) {
2460 if (StringView(First, Last).startsWith(S)) {
2461 First += S.size();
2462 return true;
2463 }
2464 return false;
2465 }
2466
2467 bool consumeIf(char C) {
2468 if (First != Last && *First == C) {
2469 ++First;
2470 return true;
2471 }
2472 return false;
2473 }
2474
2475 char consume() { return First != Last ? *First++ : '\0'; }
2476
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08002477 char look(unsigned Lookahead = 0) const {
Richard Smithc20d1442018-08-20 20:14:49 +00002478 if (static_cast<size_t>(Last - First) <= Lookahead)
2479 return '\0';
2480 return First[Lookahead];
2481 }
2482
2483 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2484
2485 StringView parseNumber(bool AllowNegative = false);
2486 Qualifiers parseCVQualifiers();
2487 bool parsePositiveInteger(size_t *Out);
2488 StringView parseBareSourceName();
2489
2490 bool parseSeqId(size_t *Out);
2491 Node *parseSubstitution();
2492 Node *parseTemplateParam();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002493 Node *parseTemplateParamDecl();
Richard Smithc20d1442018-08-20 20:14:49 +00002494 Node *parseTemplateArgs(bool TagTemplates = false);
2495 Node *parseTemplateArg();
2496
2497 /// Parse the <expr> production.
2498 Node *parseExpr();
2499 Node *parsePrefixExpr(StringView Kind);
2500 Node *parseBinaryExpr(StringView Kind);
2501 Node *parseIntegerLiteral(StringView Lit);
2502 Node *parseExprPrimary();
2503 template <class Float> Node *parseFloatingLiteral();
2504 Node *parseFunctionParam();
2505 Node *parseNewExpr();
2506 Node *parseConversionExpr();
2507 Node *parseBracedExpr();
2508 Node *parseFoldExpr();
Richard Smith1865d2f2020-10-22 19:29:36 -07002509 Node *parsePointerToMemberConversionExpr();
2510 Node *parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00002511
2512 /// Parse the <type> production.
2513 Node *parseType();
2514 Node *parseFunctionType();
2515 Node *parseVectorType();
2516 Node *parseDecltype();
2517 Node *parseArrayType();
2518 Node *parsePointerToMemberType();
2519 Node *parseClassEnumType();
2520 Node *parseQualifiedType();
2521
2522 Node *parseEncoding();
2523 bool parseCallOffset();
2524 Node *parseSpecialName();
2525
2526 /// Holds some extra information about a <name> that is being parsed. This
2527 /// information is only pertinent if the <name> refers to an <encoding>.
2528 struct NameState {
2529 bool CtorDtorConversion = false;
2530 bool EndsWithTemplateArgs = false;
2531 Qualifiers CVQualifiers = QualNone;
2532 FunctionRefQual ReferenceQualifier = FrefQualNone;
2533 size_t ForwardTemplateRefsBegin;
2534
Pavel Labathba825192018-10-16 14:29:14 +00002535 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002536 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2537 };
2538
2539 bool resolveForwardTemplateRefs(NameState &State) {
2540 size_t I = State.ForwardTemplateRefsBegin;
2541 size_t E = ForwardTemplateRefs.size();
2542 for (; I < E; ++I) {
2543 size_t Idx = ForwardTemplateRefs[I]->Index;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002544 if (TemplateParams.empty() || !TemplateParams[0] ||
2545 Idx >= TemplateParams[0]->size())
Richard Smithc20d1442018-08-20 20:14:49 +00002546 return true;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002547 ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
Richard Smithc20d1442018-08-20 20:14:49 +00002548 }
2549 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2550 return false;
2551 }
2552
2553 /// Parse the <name> production>
2554 Node *parseName(NameState *State = nullptr);
2555 Node *parseLocalName(NameState *State);
2556 Node *parseOperatorName(NameState *State);
2557 Node *parseUnqualifiedName(NameState *State);
2558 Node *parseUnnamedTypeName(NameState *State);
2559 Node *parseSourceName(NameState *State);
2560 Node *parseUnscopedName(NameState *State);
2561 Node *parseNestedName(NameState *State);
2562 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2563
2564 Node *parseAbiTags(Node *N);
2565
2566 /// Parse the <unresolved-name> production.
2567 Node *parseUnresolvedName();
2568 Node *parseSimpleId();
2569 Node *parseBaseUnresolvedName();
2570 Node *parseUnresolvedType();
2571 Node *parseDestructorName();
2572
2573 /// Top-level entry point into the parser.
2574 Node *parse();
2575};
2576
2577const char* parse_discriminator(const char* first, const char* last);
2578
2579// <name> ::= <nested-name> // N
2580// ::= <local-name> # See Scope Encoding below // Z
2581// ::= <unscoped-template-name> <template-args>
2582// ::= <unscoped-name>
2583//
2584// <unscoped-template-name> ::= <unscoped-name>
2585// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002586template <typename Derived, typename Alloc>
2587Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002588 consumeIf('L'); // extension
2589
2590 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002591 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002592 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002593 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002594
2595 // ::= <unscoped-template-name> <template-args>
2596 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00002597 Node *S = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00002598 if (S == nullptr)
2599 return nullptr;
2600 if (look() != 'I')
2601 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002602 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002603 if (TA == nullptr)
2604 return nullptr;
2605 if (State) State->EndsWithTemplateArgs = true;
2606 return make<NameWithTemplateArgs>(S, TA);
2607 }
2608
Pavel Labathba825192018-10-16 14:29:14 +00002609 Node *N = getDerived().parseUnscopedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002610 if (N == nullptr)
2611 return nullptr;
2612 // ::= <unscoped-template-name> <template-args>
2613 if (look() == 'I') {
2614 Subs.push_back(N);
Pavel Labathba825192018-10-16 14:29:14 +00002615 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002616 if (TA == nullptr)
2617 return nullptr;
2618 if (State) State->EndsWithTemplateArgs = true;
2619 return make<NameWithTemplateArgs>(N, TA);
2620 }
2621 // ::= <unscoped-name>
2622 return N;
2623}
2624
2625// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2626// := Z <function encoding> E s [<discriminator>]
2627// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002628template <typename Derived, typename Alloc>
2629Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002630 if (!consumeIf('Z'))
2631 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002632 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002633 if (Encoding == nullptr || !consumeIf('E'))
2634 return nullptr;
2635
2636 if (consumeIf('s')) {
2637 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002638 auto *StringLitName = make<NameType>("string literal");
2639 if (!StringLitName)
2640 return nullptr;
2641 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002642 }
2643
2644 if (consumeIf('d')) {
2645 parseNumber(true);
2646 if (!consumeIf('_'))
2647 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002648 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002649 if (N == nullptr)
2650 return nullptr;
2651 return make<LocalName>(Encoding, N);
2652 }
2653
Pavel Labathba825192018-10-16 14:29:14 +00002654 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002655 if (Entity == nullptr)
2656 return nullptr;
2657 First = parse_discriminator(First, Last);
2658 return make<LocalName>(Encoding, Entity);
2659}
2660
2661// <unscoped-name> ::= <unqualified-name>
2662// ::= St <unqualified-name> # ::std::
2663// extension ::= StL<unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00002664template <typename Derived, typename Alloc>
2665Node *
2666AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
2667 if (consumeIf("StL") || consumeIf("St")) {
2668 Node *R = getDerived().parseUnqualifiedName(State);
2669 if (R == nullptr)
2670 return nullptr;
2671 return make<StdQualifiedName>(R);
2672 }
2673 return getDerived().parseUnqualifiedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002674}
2675
2676// <unqualified-name> ::= <operator-name> [abi-tags]
2677// ::= <ctor-dtor-name>
2678// ::= <source-name>
2679// ::= <unnamed-type-name>
2680// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002681template <typename Derived, typename Alloc>
2682Node *
2683AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002684 // <ctor-dtor-name>s are special-cased in parseNestedName().
2685 Node *Result;
2686 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002687 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002688 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002689 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002690 else if (consumeIf("DC")) {
2691 size_t BindingsBegin = Names.size();
2692 do {
Pavel Labathba825192018-10-16 14:29:14 +00002693 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002694 if (Binding == nullptr)
2695 return nullptr;
2696 Names.push_back(Binding);
2697 } while (!consumeIf('E'));
2698 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2699 } else
Pavel Labathba825192018-10-16 14:29:14 +00002700 Result = getDerived().parseOperatorName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002701 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002702 Result = getDerived().parseAbiTags(Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002703 return Result;
2704}
2705
2706// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2707// ::= <closure-type-name>
2708//
2709// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2710//
2711// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002712template <typename Derived, typename Alloc>
2713Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002714AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2715 // <template-params> refer to the innermost <template-args>. Clear out any
2716 // outer args that we may have inserted into TemplateParams.
2717 if (State != nullptr)
2718 TemplateParams.clear();
2719
Richard Smithc20d1442018-08-20 20:14:49 +00002720 if (consumeIf("Ut")) {
2721 StringView Count = parseNumber();
2722 if (!consumeIf('_'))
2723 return nullptr;
2724 return make<UnnamedTypeName>(Count);
2725 }
2726 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002727 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2728 TemplateParams.size());
2729 ScopedTemplateParamList LambdaTemplateParams(this);
2730
2731 size_t ParamsBegin = Names.size();
2732 while (look() == 'T' &&
2733 StringView("yptn").find(look(1)) != StringView::npos) {
2734 Node *T = parseTemplateParamDecl();
2735 if (!T)
2736 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002737 Names.push_back(T);
2738 }
2739 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2740
2741 // FIXME: If TempParams is empty and none of the function parameters
2742 // includes 'auto', we should remove LambdaTemplateParams from the
2743 // TemplateParams list. Unfortunately, we don't find out whether there are
2744 // any 'auto' parameters until too late in an example such as:
2745 //
2746 // template<typename T> void f(
2747 // decltype([](decltype([]<typename T>(T v) {}),
2748 // auto) {})) {}
2749 // template<typename T> void f(
2750 // decltype([](decltype([]<typename T>(T w) {}),
2751 // int) {})) {}
2752 //
2753 // Here, the type of v is at level 2 but the type of w is at level 1. We
2754 // don't find this out until we encounter the type of the next parameter.
2755 //
2756 // However, compilers can't actually cope with the former example in
2757 // practice, and it's likely to be made ill-formed in future, so we don't
2758 // need to support it here.
2759 //
2760 // If we encounter an 'auto' in the function parameter types, we will
2761 // recreate a template parameter scope for it, but any intervening lambdas
2762 // will be parsed in the 'wrong' template parameter depth.
2763 if (TempParams.empty())
2764 TemplateParams.pop_back();
2765
Richard Smithc20d1442018-08-20 20:14:49 +00002766 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002767 do {
Pavel Labathba825192018-10-16 14:29:14 +00002768 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002769 if (P == nullptr)
2770 return nullptr;
2771 Names.push_back(P);
2772 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002773 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002774 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2775
Richard Smithc20d1442018-08-20 20:14:49 +00002776 StringView Count = parseNumber();
2777 if (!consumeIf('_'))
2778 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002779 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002780 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002781 if (consumeIf("Ub")) {
2782 (void)parseNumber();
2783 if (!consumeIf('_'))
2784 return nullptr;
2785 return make<NameType>("'block-literal'");
2786 }
Richard Smithc20d1442018-08-20 20:14:49 +00002787 return nullptr;
2788}
2789
2790// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002791template <typename Derived, typename Alloc>
2792Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002793 size_t Length = 0;
2794 if (parsePositiveInteger(&Length))
2795 return nullptr;
2796 if (numLeft() < Length || Length == 0)
2797 return nullptr;
2798 StringView Name(First, First + Length);
2799 First += Length;
2800 if (Name.startsWith("_GLOBAL__N"))
2801 return make<NameType>("(anonymous namespace)");
2802 return make<NameType>(Name);
2803}
2804
2805// <operator-name> ::= aa # &&
2806// ::= ad # & (unary)
2807// ::= an # &
2808// ::= aN # &=
2809// ::= aS # =
2810// ::= cl # ()
2811// ::= cm # ,
2812// ::= co # ~
2813// ::= cv <type> # (cast)
2814// ::= da # delete[]
2815// ::= de # * (unary)
2816// ::= dl # delete
2817// ::= dv # /
2818// ::= dV # /=
2819// ::= eo # ^
2820// ::= eO # ^=
2821// ::= eq # ==
2822// ::= ge # >=
2823// ::= gt # >
2824// ::= ix # []
2825// ::= le # <=
2826// ::= li <source-name> # operator ""
2827// ::= ls # <<
2828// ::= lS # <<=
2829// ::= lt # <
2830// ::= mi # -
2831// ::= mI # -=
2832// ::= ml # *
2833// ::= mL # *=
2834// ::= mm # -- (postfix in <expression> context)
2835// ::= na # new[]
2836// ::= ne # !=
2837// ::= ng # - (unary)
2838// ::= nt # !
2839// ::= nw # new
2840// ::= oo # ||
2841// ::= or # |
2842// ::= oR # |=
2843// ::= pm # ->*
2844// ::= pl # +
2845// ::= pL # +=
2846// ::= pp # ++ (postfix in <expression> context)
2847// ::= ps # + (unary)
2848// ::= pt # ->
2849// ::= qu # ?
2850// ::= rm # %
2851// ::= rM # %=
2852// ::= rs # >>
2853// ::= rS # >>=
2854// ::= ss # <=> C++2a
2855// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002856template <typename Derived, typename Alloc>
2857Node *
2858AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002859 switch (look()) {
2860 case 'a':
2861 switch (look(1)) {
2862 case 'a':
2863 First += 2;
2864 return make<NameType>("operator&&");
2865 case 'd':
2866 case 'n':
2867 First += 2;
2868 return make<NameType>("operator&");
2869 case 'N':
2870 First += 2;
2871 return make<NameType>("operator&=");
2872 case 'S':
2873 First += 2;
2874 return make<NameType>("operator=");
2875 }
2876 return nullptr;
2877 case 'c':
2878 switch (look(1)) {
2879 case 'l':
2880 First += 2;
2881 return make<NameType>("operator()");
2882 case 'm':
2883 First += 2;
2884 return make<NameType>("operator,");
2885 case 'o':
2886 First += 2;
2887 return make<NameType>("operator~");
2888 // ::= cv <type> # (cast)
2889 case 'v': {
2890 First += 2;
2891 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2892 // If we're parsing an encoding, State != nullptr and the conversion
2893 // operators' <type> could have a <template-param> that refers to some
2894 // <template-arg>s further ahead in the mangled name.
2895 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2896 PermitForwardTemplateReferences ||
2897 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002898 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002899 if (Ty == nullptr)
2900 return nullptr;
2901 if (State) State->CtorDtorConversion = true;
2902 return make<ConversionOperatorType>(Ty);
2903 }
2904 }
2905 return nullptr;
2906 case 'd':
2907 switch (look(1)) {
2908 case 'a':
2909 First += 2;
2910 return make<NameType>("operator delete[]");
2911 case 'e':
2912 First += 2;
2913 return make<NameType>("operator*");
2914 case 'l':
2915 First += 2;
2916 return make<NameType>("operator delete");
2917 case 'v':
2918 First += 2;
2919 return make<NameType>("operator/");
2920 case 'V':
2921 First += 2;
2922 return make<NameType>("operator/=");
2923 }
2924 return nullptr;
2925 case 'e':
2926 switch (look(1)) {
2927 case 'o':
2928 First += 2;
2929 return make<NameType>("operator^");
2930 case 'O':
2931 First += 2;
2932 return make<NameType>("operator^=");
2933 case 'q':
2934 First += 2;
2935 return make<NameType>("operator==");
2936 }
2937 return nullptr;
2938 case 'g':
2939 switch (look(1)) {
2940 case 'e':
2941 First += 2;
2942 return make<NameType>("operator>=");
2943 case 't':
2944 First += 2;
2945 return make<NameType>("operator>");
2946 }
2947 return nullptr;
2948 case 'i':
2949 if (look(1) == 'x') {
2950 First += 2;
2951 return make<NameType>("operator[]");
2952 }
2953 return nullptr;
2954 case 'l':
2955 switch (look(1)) {
2956 case 'e':
2957 First += 2;
2958 return make<NameType>("operator<=");
2959 // ::= li <source-name> # operator ""
2960 case 'i': {
2961 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002962 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002963 if (SN == nullptr)
2964 return nullptr;
2965 return make<LiteralOperator>(SN);
2966 }
2967 case 's':
2968 First += 2;
2969 return make<NameType>("operator<<");
2970 case 'S':
2971 First += 2;
2972 return make<NameType>("operator<<=");
2973 case 't':
2974 First += 2;
2975 return make<NameType>("operator<");
2976 }
2977 return nullptr;
2978 case 'm':
2979 switch (look(1)) {
2980 case 'i':
2981 First += 2;
2982 return make<NameType>("operator-");
2983 case 'I':
2984 First += 2;
2985 return make<NameType>("operator-=");
2986 case 'l':
2987 First += 2;
2988 return make<NameType>("operator*");
2989 case 'L':
2990 First += 2;
2991 return make<NameType>("operator*=");
2992 case 'm':
2993 First += 2;
2994 return make<NameType>("operator--");
2995 }
2996 return nullptr;
2997 case 'n':
2998 switch (look(1)) {
2999 case 'a':
3000 First += 2;
3001 return make<NameType>("operator new[]");
3002 case 'e':
3003 First += 2;
3004 return make<NameType>("operator!=");
3005 case 'g':
3006 First += 2;
3007 return make<NameType>("operator-");
3008 case 't':
3009 First += 2;
3010 return make<NameType>("operator!");
3011 case 'w':
3012 First += 2;
3013 return make<NameType>("operator new");
3014 }
3015 return nullptr;
3016 case 'o':
3017 switch (look(1)) {
3018 case 'o':
3019 First += 2;
3020 return make<NameType>("operator||");
3021 case 'r':
3022 First += 2;
3023 return make<NameType>("operator|");
3024 case 'R':
3025 First += 2;
3026 return make<NameType>("operator|=");
3027 }
3028 return nullptr;
3029 case 'p':
3030 switch (look(1)) {
3031 case 'm':
3032 First += 2;
3033 return make<NameType>("operator->*");
3034 case 'l':
3035 First += 2;
3036 return make<NameType>("operator+");
3037 case 'L':
3038 First += 2;
3039 return make<NameType>("operator+=");
3040 case 'p':
3041 First += 2;
3042 return make<NameType>("operator++");
3043 case 's':
3044 First += 2;
3045 return make<NameType>("operator+");
3046 case 't':
3047 First += 2;
3048 return make<NameType>("operator->");
3049 }
3050 return nullptr;
3051 case 'q':
3052 if (look(1) == 'u') {
3053 First += 2;
3054 return make<NameType>("operator?");
3055 }
3056 return nullptr;
3057 case 'r':
3058 switch (look(1)) {
3059 case 'm':
3060 First += 2;
3061 return make<NameType>("operator%");
3062 case 'M':
3063 First += 2;
3064 return make<NameType>("operator%=");
3065 case 's':
3066 First += 2;
3067 return make<NameType>("operator>>");
3068 case 'S':
3069 First += 2;
3070 return make<NameType>("operator>>=");
3071 }
3072 return nullptr;
3073 case 's':
3074 if (look(1) == 's') {
3075 First += 2;
3076 return make<NameType>("operator<=>");
3077 }
3078 return nullptr;
3079 // ::= v <digit> <source-name> # vendor extended operator
3080 case 'v':
3081 if (std::isdigit(look(1))) {
3082 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003083 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003084 if (SN == nullptr)
3085 return nullptr;
3086 return make<ConversionOperatorType>(SN);
3087 }
3088 return nullptr;
3089 }
3090 return nullptr;
3091}
3092
3093// <ctor-dtor-name> ::= C1 # complete object constructor
3094// ::= C2 # base object constructor
3095// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003096// extension ::= C4 # gcc old-style "[unified]" constructor
3097// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003098// ::= D0 # deleting destructor
3099// ::= D1 # complete object destructor
3100// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003101// extension ::= D4 # gcc old-style "[unified]" destructor
3102// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003103template <typename Derived, typename Alloc>
3104Node *
3105AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3106 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003107 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3108 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3109 switch (SSK) {
3110 case SpecialSubKind::string:
3111 case SpecialSubKind::istream:
3112 case SpecialSubKind::ostream:
3113 case SpecialSubKind::iostream:
3114 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003115 if (!SoFar)
3116 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003117 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003118 default:
3119 break;
3120 }
3121 }
3122
3123 if (consumeIf('C')) {
3124 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003125 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3126 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003127 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003128 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003129 ++First;
3130 if (State) State->CtorDtorConversion = true;
3131 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003132 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003133 return nullptr;
3134 }
Nico Weber29294792019-04-03 23:14:33 +00003135 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003136 }
3137
Nico Weber29294792019-04-03 23:14:33 +00003138 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3139 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003140 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003141 First += 2;
3142 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003143 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003144 }
3145
3146 return nullptr;
3147}
3148
3149// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3150// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3151//
3152// <prefix> ::= <prefix> <unqualified-name>
3153// ::= <template-prefix> <template-args>
3154// ::= <template-param>
3155// ::= <decltype>
3156// ::= # empty
3157// ::= <substitution>
3158// ::= <prefix> <data-member-prefix>
3159// extension ::= L
3160//
3161// <data-member-prefix> := <member source-name> [<template-args>] M
3162//
3163// <template-prefix> ::= <prefix> <template unqualified-name>
3164// ::= <template-param>
3165// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003166template <typename Derived, typename Alloc>
3167Node *
3168AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003169 if (!consumeIf('N'))
3170 return nullptr;
3171
3172 Qualifiers CVTmp = parseCVQualifiers();
3173 if (State) State->CVQualifiers = CVTmp;
3174
3175 if (consumeIf('O')) {
3176 if (State) State->ReferenceQualifier = FrefQualRValue;
3177 } else if (consumeIf('R')) {
3178 if (State) State->ReferenceQualifier = FrefQualLValue;
3179 } else
3180 if (State) State->ReferenceQualifier = FrefQualNone;
3181
3182 Node *SoFar = nullptr;
3183 auto PushComponent = [&](Node *Comp) {
Richard Smithb485b352018-08-24 23:30:26 +00003184 if (!Comp) return false;
Richard Smithc20d1442018-08-20 20:14:49 +00003185 if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
3186 else SoFar = Comp;
3187 if (State) State->EndsWithTemplateArgs = false;
Richard Smithb485b352018-08-24 23:30:26 +00003188 return SoFar != nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003189 };
3190
Richard Smithb485b352018-08-24 23:30:26 +00003191 if (consumeIf("St")) {
Richard Smithc20d1442018-08-20 20:14:49 +00003192 SoFar = make<NameType>("std");
Richard Smithb485b352018-08-24 23:30:26 +00003193 if (!SoFar)
3194 return nullptr;
3195 }
Richard Smithc20d1442018-08-20 20:14:49 +00003196
3197 while (!consumeIf('E')) {
3198 consumeIf('L'); // extension
3199
3200 // <data-member-prefix> := <member source-name> [<template-args>] M
3201 if (consumeIf('M')) {
3202 if (SoFar == nullptr)
3203 return nullptr;
3204 continue;
3205 }
3206
3207 // ::= <template-param>
3208 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003209 if (!PushComponent(getDerived().parseTemplateParam()))
Richard Smithc20d1442018-08-20 20:14:49 +00003210 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003211 Subs.push_back(SoFar);
3212 continue;
3213 }
3214
3215 // ::= <template-prefix> <template-args>
3216 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003217 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003218 if (TA == nullptr || SoFar == nullptr)
3219 return nullptr;
3220 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003221 if (!SoFar)
3222 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003223 if (State) State->EndsWithTemplateArgs = true;
3224 Subs.push_back(SoFar);
3225 continue;
3226 }
3227
3228 // ::= <decltype>
3229 if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
Pavel Labathba825192018-10-16 14:29:14 +00003230 if (!PushComponent(getDerived().parseDecltype()))
Richard Smithc20d1442018-08-20 20:14:49 +00003231 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003232 Subs.push_back(SoFar);
3233 continue;
3234 }
3235
3236 // ::= <substitution>
3237 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00003238 Node *S = getDerived().parseSubstitution();
Richard Smithb485b352018-08-24 23:30:26 +00003239 if (!PushComponent(S))
Richard Smithc20d1442018-08-20 20:14:49 +00003240 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003241 if (SoFar != S)
3242 Subs.push_back(S);
3243 continue;
3244 }
3245
3246 // Parse an <unqualified-name> thats actually a <ctor-dtor-name>.
3247 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
3248 if (SoFar == nullptr)
3249 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003250 if (!PushComponent(getDerived().parseCtorDtorName(SoFar, State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003251 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003252 SoFar = getDerived().parseAbiTags(SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00003253 if (SoFar == nullptr)
3254 return nullptr;
3255 Subs.push_back(SoFar);
3256 continue;
3257 }
3258
3259 // ::= <prefix> <unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00003260 if (!PushComponent(getDerived().parseUnqualifiedName(State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003261 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003262 Subs.push_back(SoFar);
3263 }
3264
3265 if (SoFar == nullptr || Subs.empty())
3266 return nullptr;
3267
3268 Subs.pop_back();
3269 return SoFar;
3270}
3271
3272// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003273template <typename Derived, typename Alloc>
3274Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3275 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003276 if (SN == nullptr)
3277 return nullptr;
3278 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003279 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003280 if (TA == nullptr)
3281 return nullptr;
3282 return make<NameWithTemplateArgs>(SN, TA);
3283 }
3284 return SN;
3285}
3286
3287// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3288// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003289template <typename Derived, typename Alloc>
3290Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003291 Node *Result;
3292 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003293 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003294 else
Pavel Labathba825192018-10-16 14:29:14 +00003295 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003296 if (Result == nullptr)
3297 return nullptr;
3298 return make<DtorName>(Result);
3299}
3300
3301// <unresolved-type> ::= <template-param>
3302// ::= <decltype>
3303// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003304template <typename Derived, typename Alloc>
3305Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003306 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003307 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003308 if (TP == nullptr)
3309 return nullptr;
3310 Subs.push_back(TP);
3311 return TP;
3312 }
3313 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003314 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003315 if (DT == nullptr)
3316 return nullptr;
3317 Subs.push_back(DT);
3318 return DT;
3319 }
Pavel Labathba825192018-10-16 14:29:14 +00003320 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003321}
3322
3323// <base-unresolved-name> ::= <simple-id> # unresolved name
3324// extension ::= <operator-name> # unresolved operator-function-id
3325// extension ::= <operator-name> <template-args> # unresolved operator template-id
3326// ::= on <operator-name> # unresolved operator-function-id
3327// ::= on <operator-name> <template-args> # unresolved operator template-id
3328// ::= dn <destructor-name> # destructor or pseudo-destructor;
3329// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003330template <typename Derived, typename Alloc>
3331Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003332 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003333 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003334
3335 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003336 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003337
3338 consumeIf("on");
3339
Pavel Labathba825192018-10-16 14:29:14 +00003340 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003341 if (Oper == nullptr)
3342 return nullptr;
3343 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003344 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003345 if (TA == nullptr)
3346 return nullptr;
3347 return make<NameWithTemplateArgs>(Oper, TA);
3348 }
3349 return Oper;
3350}
3351
3352// <unresolved-name>
3353// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3354// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3355// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3356// # A::x, N::y, A<T>::z; "gs" means leading "::"
3357// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3358// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3359// # T::N::x /decltype(p)::N::x
3360// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3361//
3362// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003363template <typename Derived, typename Alloc>
3364Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003365 Node *SoFar = nullptr;
3366
3367 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3368 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3369 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003370 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003371 if (SoFar == nullptr)
3372 return nullptr;
3373
3374 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003375 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003376 if (TA == nullptr)
3377 return nullptr;
3378 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003379 if (!SoFar)
3380 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003381 }
3382
3383 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003384 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003385 if (Qual == nullptr)
3386 return nullptr;
3387 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003388 if (!SoFar)
3389 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003390 }
3391
Pavel Labathba825192018-10-16 14:29:14 +00003392 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003393 if (Base == nullptr)
3394 return nullptr;
3395 return make<QualifiedName>(SoFar, Base);
3396 }
3397
3398 bool Global = consumeIf("gs");
3399
3400 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3401 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003402 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003403 if (SoFar == nullptr)
3404 return nullptr;
3405 if (Global)
3406 SoFar = make<GlobalQualifiedName>(SoFar);
3407 return SoFar;
3408 }
3409
3410 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3411 if (std::isdigit(look())) {
3412 do {
Pavel Labathba825192018-10-16 14:29:14 +00003413 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003414 if (Qual == nullptr)
3415 return nullptr;
3416 if (SoFar)
3417 SoFar = make<QualifiedName>(SoFar, Qual);
3418 else if (Global)
3419 SoFar = make<GlobalQualifiedName>(Qual);
3420 else
3421 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003422 if (!SoFar)
3423 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003424 } while (!consumeIf('E'));
3425 }
3426 // sr <unresolved-type> <base-unresolved-name>
3427 // sr <unresolved-type> <template-args> <base-unresolved-name>
3428 else {
Pavel Labathba825192018-10-16 14:29:14 +00003429 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003430 if (SoFar == nullptr)
3431 return nullptr;
3432
3433 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003434 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003435 if (TA == nullptr)
3436 return nullptr;
3437 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003438 if (!SoFar)
3439 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003440 }
3441 }
3442
3443 assert(SoFar != nullptr);
3444
Pavel Labathba825192018-10-16 14:29:14 +00003445 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003446 if (Base == nullptr)
3447 return nullptr;
3448 return make<QualifiedName>(SoFar, Base);
3449}
3450
3451// <abi-tags> ::= <abi-tag> [<abi-tags>]
3452// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003453template <typename Derived, typename Alloc>
3454Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003455 while (consumeIf('B')) {
3456 StringView SN = parseBareSourceName();
3457 if (SN.empty())
3458 return nullptr;
3459 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003460 if (!N)
3461 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003462 }
3463 return N;
3464}
3465
3466// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003467template <typename Alloc, typename Derived>
3468StringView
3469AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003470 const char *Tmp = First;
3471 if (AllowNegative)
3472 consumeIf('n');
3473 if (numLeft() == 0 || !std::isdigit(*First))
3474 return StringView();
3475 while (numLeft() != 0 && std::isdigit(*First))
3476 ++First;
3477 return StringView(Tmp, First);
3478}
3479
3480// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003481template <typename Alloc, typename Derived>
3482bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003483 *Out = 0;
3484 if (look() < '0' || look() > '9')
3485 return true;
3486 while (look() >= '0' && look() <= '9') {
3487 *Out *= 10;
3488 *Out += static_cast<size_t>(consume() - '0');
3489 }
3490 return false;
3491}
3492
Pavel Labathba825192018-10-16 14:29:14 +00003493template <typename Alloc, typename Derived>
3494StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003495 size_t Int = 0;
3496 if (parsePositiveInteger(&Int) || numLeft() < Int)
3497 return StringView();
3498 StringView R(First, First + Int);
3499 First += Int;
3500 return R;
3501}
3502
3503// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3504//
3505// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3506// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3507// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3508//
3509// <ref-qualifier> ::= R # & ref-qualifier
3510// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003511template <typename Derived, typename Alloc>
3512Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003513 Qualifiers CVQuals = parseCVQualifiers();
3514
3515 Node *ExceptionSpec = nullptr;
3516 if (consumeIf("Do")) {
3517 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003518 if (!ExceptionSpec)
3519 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003520 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003521 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003522 if (E == nullptr || !consumeIf('E'))
3523 return nullptr;
3524 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003525 if (!ExceptionSpec)
3526 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003527 } else if (consumeIf("Dw")) {
3528 size_t SpecsBegin = Names.size();
3529 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003530 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003531 if (T == nullptr)
3532 return nullptr;
3533 Names.push_back(T);
3534 }
3535 ExceptionSpec =
3536 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003537 if (!ExceptionSpec)
3538 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003539 }
3540
3541 consumeIf("Dx"); // transaction safe
3542
3543 if (!consumeIf('F'))
3544 return nullptr;
3545 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003546 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003547 if (ReturnType == nullptr)
3548 return nullptr;
3549
3550 FunctionRefQual ReferenceQualifier = FrefQualNone;
3551 size_t ParamsBegin = Names.size();
3552 while (true) {
3553 if (consumeIf('E'))
3554 break;
3555 if (consumeIf('v'))
3556 continue;
3557 if (consumeIf("RE")) {
3558 ReferenceQualifier = FrefQualLValue;
3559 break;
3560 }
3561 if (consumeIf("OE")) {
3562 ReferenceQualifier = FrefQualRValue;
3563 break;
3564 }
Pavel Labathba825192018-10-16 14:29:14 +00003565 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003566 if (T == nullptr)
3567 return nullptr;
3568 Names.push_back(T);
3569 }
3570
3571 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3572 return make<FunctionType>(ReturnType, Params, CVQuals,
3573 ReferenceQualifier, ExceptionSpec);
3574}
3575
3576// extension:
3577// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3578// ::= Dv [<dimension expression>] _ <element type>
3579// <extended element type> ::= <element type>
3580// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003581template <typename Derived, typename Alloc>
3582Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003583 if (!consumeIf("Dv"))
3584 return nullptr;
3585 if (look() >= '1' && look() <= '9') {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003586 Node *DimensionNumber = make<NameType>(parseNumber());
3587 if (!DimensionNumber)
3588 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003589 if (!consumeIf('_'))
3590 return nullptr;
3591 if (consumeIf('p'))
3592 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003593 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003594 if (ElemType == nullptr)
3595 return nullptr;
3596 return make<VectorType>(ElemType, DimensionNumber);
3597 }
3598
3599 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003600 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003601 if (!DimExpr)
3602 return nullptr;
3603 if (!consumeIf('_'))
3604 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003605 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003606 if (!ElemType)
3607 return nullptr;
3608 return make<VectorType>(ElemType, DimExpr);
3609 }
Pavel Labathba825192018-10-16 14:29:14 +00003610 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003611 if (!ElemType)
3612 return nullptr;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003613 return make<VectorType>(ElemType, /*Dimension=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003614}
3615
3616// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3617// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003618template <typename Derived, typename Alloc>
3619Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003620 if (!consumeIf('D'))
3621 return nullptr;
3622 if (!consumeIf('t') && !consumeIf('T'))
3623 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003624 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003625 if (E == nullptr)
3626 return nullptr;
3627 if (!consumeIf('E'))
3628 return nullptr;
3629 return make<EnclosingExpr>("decltype(", E, ")");
3630}
3631
3632// <array-type> ::= A <positive dimension number> _ <element type>
3633// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003634template <typename Derived, typename Alloc>
3635Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003636 if (!consumeIf('A'))
3637 return nullptr;
3638
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003639 Node *Dimension = nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003640
Richard Smithc20d1442018-08-20 20:14:49 +00003641 if (std::isdigit(look())) {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003642 Dimension = make<NameType>(parseNumber());
3643 if (!Dimension)
3644 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003645 if (!consumeIf('_'))
3646 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003647 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003648 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003649 if (DimExpr == nullptr)
3650 return nullptr;
3651 if (!consumeIf('_'))
3652 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003653 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003654 }
3655
Pavel Labathba825192018-10-16 14:29:14 +00003656 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003657 if (Ty == nullptr)
3658 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003659 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003660}
3661
3662// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003663template <typename Derived, typename Alloc>
3664Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003665 if (!consumeIf('M'))
3666 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003667 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003668 if (ClassType == nullptr)
3669 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003670 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003671 if (MemberType == nullptr)
3672 return nullptr;
3673 return make<PointerToMemberType>(ClassType, MemberType);
3674}
3675
3676// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3677// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3678// ::= Tu <name> # dependent elaborated type specifier using 'union'
3679// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003680template <typename Derived, typename Alloc>
3681Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003682 StringView ElabSpef;
3683 if (consumeIf("Ts"))
3684 ElabSpef = "struct";
3685 else if (consumeIf("Tu"))
3686 ElabSpef = "union";
3687 else if (consumeIf("Te"))
3688 ElabSpef = "enum";
3689
Pavel Labathba825192018-10-16 14:29:14 +00003690 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003691 if (Name == nullptr)
3692 return nullptr;
3693
3694 if (!ElabSpef.empty())
3695 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3696
3697 return Name;
3698}
3699
3700// <qualified-type> ::= <qualifiers> <type>
3701// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3702// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003703template <typename Derived, typename Alloc>
3704Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003705 if (consumeIf('U')) {
3706 StringView Qual = parseBareSourceName();
3707 if (Qual.empty())
3708 return nullptr;
3709
Richard Smithc20d1442018-08-20 20:14:49 +00003710 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3711 if (Qual.startsWith("objcproto")) {
3712 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3713 StringView Proto;
3714 {
3715 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3716 SaveLast(Last, ProtoSourceName.end());
3717 Proto = parseBareSourceName();
3718 }
3719 if (Proto.empty())
3720 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003721 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003722 if (Child == nullptr)
3723 return nullptr;
3724 return make<ObjCProtoName>(Child, Proto);
3725 }
3726
Alex Orlovf50df922021-03-24 10:21:32 +04003727 Node *TA = nullptr;
3728 if (look() == 'I') {
3729 TA = getDerived().parseTemplateArgs();
3730 if (TA == nullptr)
3731 return nullptr;
3732 }
3733
Pavel Labathba825192018-10-16 14:29:14 +00003734 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003735 if (Child == nullptr)
3736 return nullptr;
Alex Orlovf50df922021-03-24 10:21:32 +04003737 return make<VendorExtQualType>(Child, Qual, TA);
Richard Smithc20d1442018-08-20 20:14:49 +00003738 }
3739
3740 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003741 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003742 if (Ty == nullptr)
3743 return nullptr;
3744 if (Quals != QualNone)
3745 Ty = make<QualType>(Ty, Quals);
3746 return Ty;
3747}
3748
3749// <type> ::= <builtin-type>
3750// ::= <qualified-type>
3751// ::= <function-type>
3752// ::= <class-enum-type>
3753// ::= <array-type>
3754// ::= <pointer-to-member-type>
3755// ::= <template-param>
3756// ::= <template-template-param> <template-args>
3757// ::= <decltype>
3758// ::= P <type> # pointer
3759// ::= R <type> # l-value reference
3760// ::= O <type> # r-value reference (C++11)
3761// ::= C <type> # complex pair (C99)
3762// ::= G <type> # imaginary (C99)
3763// ::= <substitution> # See Compression below
3764// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3765// extension ::= <vector-type> # <vector-type> starts with Dv
3766//
3767// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3768// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003769template <typename Derived, typename Alloc>
3770Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003771 Node *Result = nullptr;
3772
Richard Smithc20d1442018-08-20 20:14:49 +00003773 switch (look()) {
3774 // ::= <qualified-type>
3775 case 'r':
3776 case 'V':
3777 case 'K': {
3778 unsigned AfterQuals = 0;
3779 if (look(AfterQuals) == 'r') ++AfterQuals;
3780 if (look(AfterQuals) == 'V') ++AfterQuals;
3781 if (look(AfterQuals) == 'K') ++AfterQuals;
3782
3783 if (look(AfterQuals) == 'F' ||
3784 (look(AfterQuals) == 'D' &&
3785 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3786 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003787 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003788 break;
3789 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003790 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003791 }
3792 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003793 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003794 break;
3795 }
3796 // <builtin-type> ::= v # void
3797 case 'v':
3798 ++First;
3799 return make<NameType>("void");
3800 // ::= w # wchar_t
3801 case 'w':
3802 ++First;
3803 return make<NameType>("wchar_t");
3804 // ::= b # bool
3805 case 'b':
3806 ++First;
3807 return make<NameType>("bool");
3808 // ::= c # char
3809 case 'c':
3810 ++First;
3811 return make<NameType>("char");
3812 // ::= a # signed char
3813 case 'a':
3814 ++First;
3815 return make<NameType>("signed char");
3816 // ::= h # unsigned char
3817 case 'h':
3818 ++First;
3819 return make<NameType>("unsigned char");
3820 // ::= s # short
3821 case 's':
3822 ++First;
3823 return make<NameType>("short");
3824 // ::= t # unsigned short
3825 case 't':
3826 ++First;
3827 return make<NameType>("unsigned short");
3828 // ::= i # int
3829 case 'i':
3830 ++First;
3831 return make<NameType>("int");
3832 // ::= j # unsigned int
3833 case 'j':
3834 ++First;
3835 return make<NameType>("unsigned int");
3836 // ::= l # long
3837 case 'l':
3838 ++First;
3839 return make<NameType>("long");
3840 // ::= m # unsigned long
3841 case 'm':
3842 ++First;
3843 return make<NameType>("unsigned long");
3844 // ::= x # long long, __int64
3845 case 'x':
3846 ++First;
3847 return make<NameType>("long long");
3848 // ::= y # unsigned long long, __int64
3849 case 'y':
3850 ++First;
3851 return make<NameType>("unsigned long long");
3852 // ::= n # __int128
3853 case 'n':
3854 ++First;
3855 return make<NameType>("__int128");
3856 // ::= o # unsigned __int128
3857 case 'o':
3858 ++First;
3859 return make<NameType>("unsigned __int128");
3860 // ::= f # float
3861 case 'f':
3862 ++First;
3863 return make<NameType>("float");
3864 // ::= d # double
3865 case 'd':
3866 ++First;
3867 return make<NameType>("double");
3868 // ::= e # long double, __float80
3869 case 'e':
3870 ++First;
3871 return make<NameType>("long double");
3872 // ::= g # __float128
3873 case 'g':
3874 ++First;
3875 return make<NameType>("__float128");
3876 // ::= z # ellipsis
3877 case 'z':
3878 ++First;
3879 return make<NameType>("...");
3880
3881 // <builtin-type> ::= u <source-name> # vendor extended type
3882 case 'u': {
3883 ++First;
3884 StringView Res = parseBareSourceName();
3885 if (Res.empty())
3886 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003887 // Typically, <builtin-type>s are not considered substitution candidates,
3888 // but the exception to that exception is vendor extended types (Itanium C++
3889 // ABI 5.9.1).
3890 Result = make<NameType>(Res);
3891 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003892 }
3893 case 'D':
3894 switch (look(1)) {
3895 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3896 case 'd':
3897 First += 2;
3898 return make<NameType>("decimal64");
3899 // ::= De # IEEE 754r decimal floating point (128 bits)
3900 case 'e':
3901 First += 2;
3902 return make<NameType>("decimal128");
3903 // ::= Df # IEEE 754r decimal floating point (32 bits)
3904 case 'f':
3905 First += 2;
3906 return make<NameType>("decimal32");
3907 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3908 case 'h':
3909 First += 2;
Stuart Bradye8bf5772021-06-07 16:30:22 +01003910 return make<NameType>("half");
Pengfei Wang50e90b82021-09-23 11:02:25 +08003911 // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits)
3912 case 'F': {
3913 First += 2;
3914 Node *DimensionNumber = make<NameType>(parseNumber());
3915 if (!DimensionNumber)
3916 return nullptr;
3917 if (!consumeIf('_'))
3918 return nullptr;
3919 return make<BinaryFPType>(DimensionNumber);
3920 }
Richard Smithc20d1442018-08-20 20:14:49 +00003921 // ::= Di # char32_t
3922 case 'i':
3923 First += 2;
3924 return make<NameType>("char32_t");
3925 // ::= Ds # char16_t
3926 case 's':
3927 First += 2;
3928 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003929 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3930 case 'u':
3931 First += 2;
3932 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003933 // ::= Da # auto (in dependent new-expressions)
3934 case 'a':
3935 First += 2;
3936 return make<NameType>("auto");
3937 // ::= Dc # decltype(auto)
3938 case 'c':
3939 First += 2;
3940 return make<NameType>("decltype(auto)");
3941 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3942 case 'n':
3943 First += 2;
3944 return make<NameType>("std::nullptr_t");
3945
3946 // ::= <decltype>
3947 case 't':
3948 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003949 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003950 break;
3951 }
3952 // extension ::= <vector-type> # <vector-type> starts with Dv
3953 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003954 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003955 break;
3956 }
3957 // ::= Dp <type> # pack expansion (C++0x)
3958 case 'p': {
3959 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003960 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003961 if (!Child)
3962 return nullptr;
3963 Result = make<ParameterPackExpansion>(Child);
3964 break;
3965 }
3966 // Exception specifier on a function type.
3967 case 'o':
3968 case 'O':
3969 case 'w':
3970 // Transaction safe function type.
3971 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003972 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003973 break;
3974 }
3975 break;
3976 // ::= <function-type>
3977 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003978 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003979 break;
3980 }
3981 // ::= <array-type>
3982 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003983 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003984 break;
3985 }
3986 // ::= <pointer-to-member-type>
3987 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003988 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003989 break;
3990 }
3991 // ::= <template-param>
3992 case 'T': {
3993 // This could be an elaborate type specifier on a <class-enum-type>.
3994 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003995 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003996 break;
3997 }
3998
Pavel Labathba825192018-10-16 14:29:14 +00003999 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004000 if (Result == nullptr)
4001 return nullptr;
4002
4003 // Result could be either of:
4004 // <type> ::= <template-param>
4005 // <type> ::= <template-template-param> <template-args>
4006 //
4007 // <template-template-param> ::= <template-param>
4008 // ::= <substitution>
4009 //
4010 // If this is followed by some <template-args>, and we're permitted to
4011 // parse them, take the second production.
4012
4013 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004014 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004015 if (TA == nullptr)
4016 return nullptr;
4017 Result = make<NameWithTemplateArgs>(Result, TA);
4018 }
4019 break;
4020 }
4021 // ::= P <type> # pointer
4022 case 'P': {
4023 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004024 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004025 if (Ptr == nullptr)
4026 return nullptr;
4027 Result = make<PointerType>(Ptr);
4028 break;
4029 }
4030 // ::= R <type> # l-value reference
4031 case 'R': {
4032 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004033 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004034 if (Ref == nullptr)
4035 return nullptr;
4036 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
4037 break;
4038 }
4039 // ::= O <type> # r-value reference (C++11)
4040 case 'O': {
4041 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004042 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004043 if (Ref == nullptr)
4044 return nullptr;
4045 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
4046 break;
4047 }
4048 // ::= C <type> # complex pair (C99)
4049 case 'C': {
4050 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004051 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004052 if (P == nullptr)
4053 return nullptr;
4054 Result = make<PostfixQualifiedType>(P, " complex");
4055 break;
4056 }
4057 // ::= G <type> # imaginary (C99)
4058 case 'G': {
4059 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004060 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004061 if (P == nullptr)
4062 return P;
4063 Result = make<PostfixQualifiedType>(P, " imaginary");
4064 break;
4065 }
4066 // ::= <substitution> # See Compression below
4067 case 'S': {
4068 if (look(1) && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00004069 Node *Sub = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00004070 if (Sub == nullptr)
4071 return nullptr;
4072
4073 // Sub could be either of:
4074 // <type> ::= <substitution>
4075 // <type> ::= <template-template-param> <template-args>
4076 //
4077 // <template-template-param> ::= <template-param>
4078 // ::= <substitution>
4079 //
4080 // If this is followed by some <template-args>, and we're permitted to
4081 // parse them, take the second production.
4082
4083 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004084 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004085 if (TA == nullptr)
4086 return nullptr;
4087 Result = make<NameWithTemplateArgs>(Sub, TA);
4088 break;
4089 }
4090
4091 // If all we parsed was a substitution, don't re-insert into the
4092 // substitution table.
4093 return Sub;
4094 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00004095 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004096 }
4097 // ::= <class-enum-type>
4098 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004099 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004100 break;
4101 }
4102 }
4103
4104 // If we parsed a type, insert it into the substitution table. Note that all
4105 // <builtin-type>s and <substitution>s have already bailed out, because they
4106 // don't get substitutions.
4107 if (Result != nullptr)
4108 Subs.push_back(Result);
4109 return Result;
4110}
4111
Pavel Labathba825192018-10-16 14:29:14 +00004112template <typename Derived, typename Alloc>
4113Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4114 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004115 if (E == nullptr)
4116 return nullptr;
4117 return make<PrefixExpr>(Kind, E);
4118}
4119
Pavel Labathba825192018-10-16 14:29:14 +00004120template <typename Derived, typename Alloc>
4121Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4122 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004123 if (LHS == nullptr)
4124 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004125 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004126 if (RHS == nullptr)
4127 return nullptr;
4128 return make<BinaryExpr>(LHS, Kind, RHS);
4129}
4130
Pavel Labathba825192018-10-16 14:29:14 +00004131template <typename Derived, typename Alloc>
4132Node *
4133AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004134 StringView Tmp = parseNumber(true);
4135 if (!Tmp.empty() && consumeIf('E'))
4136 return make<IntegerLiteral>(Lit, Tmp);
4137 return nullptr;
4138}
4139
4140// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004141template <typename Alloc, typename Derived>
4142Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004143 Qualifiers CVR = QualNone;
4144 if (consumeIf('r'))
4145 CVR |= QualRestrict;
4146 if (consumeIf('V'))
4147 CVR |= QualVolatile;
4148 if (consumeIf('K'))
4149 CVR |= QualConst;
4150 return CVR;
4151}
4152
4153// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4154// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4155// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4156// ::= 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 -04004157// ::= fpT # 'this' expression (not part of standard?)
Pavel Labathba825192018-10-16 14:29:14 +00004158template <typename Derived, typename Alloc>
4159Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Erik Pilkington91c24af2020-05-13 22:19:45 -04004160 if (consumeIf("fpT"))
4161 return make<NameType>("this");
Richard Smithc20d1442018-08-20 20:14:49 +00004162 if (consumeIf("fp")) {
4163 parseCVQualifiers();
4164 StringView Num = parseNumber();
4165 if (!consumeIf('_'))
4166 return nullptr;
4167 return make<FunctionParam>(Num);
4168 }
4169 if (consumeIf("fL")) {
4170 if (parseNumber().empty())
4171 return nullptr;
4172 if (!consumeIf('p'))
4173 return nullptr;
4174 parseCVQualifiers();
4175 StringView Num = parseNumber();
4176 if (!consumeIf('_'))
4177 return nullptr;
4178 return make<FunctionParam>(Num);
4179 }
4180 return nullptr;
4181}
4182
4183// [gs] nw <expression>* _ <type> E # new (expr-list) type
4184// [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4185// [gs] na <expression>* _ <type> E # new[] (expr-list) type
4186// [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4187// <initializer> ::= pi <expression>* E # parenthesized initialization
Pavel Labathba825192018-10-16 14:29:14 +00004188template <typename Derived, typename Alloc>
4189Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004190 bool Global = consumeIf("gs");
4191 bool IsArray = look(1) == 'a';
4192 if (!consumeIf("nw") && !consumeIf("na"))
4193 return nullptr;
4194 size_t Exprs = Names.size();
4195 while (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00004196 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004197 if (Ex == nullptr)
4198 return nullptr;
4199 Names.push_back(Ex);
4200 }
4201 NodeArray ExprList = popTrailingNodeArray(Exprs);
Pavel Labathba825192018-10-16 14:29:14 +00004202 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004203 if (Ty == nullptr)
4204 return Ty;
4205 if (consumeIf("pi")) {
4206 size_t InitsBegin = Names.size();
4207 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004208 Node *Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004209 if (Init == nullptr)
4210 return Init;
4211 Names.push_back(Init);
4212 }
4213 NodeArray Inits = popTrailingNodeArray(InitsBegin);
4214 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
4215 } else if (!consumeIf('E'))
4216 return nullptr;
4217 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
4218}
4219
4220// cv <type> <expression> # conversion with one argument
4221// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004222template <typename Derived, typename Alloc>
4223Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004224 if (!consumeIf("cv"))
4225 return nullptr;
4226 Node *Ty;
4227 {
4228 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004229 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004230 }
4231
4232 if (Ty == nullptr)
4233 return nullptr;
4234
4235 if (consumeIf('_')) {
4236 size_t ExprsBegin = Names.size();
4237 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004238 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004239 if (E == nullptr)
4240 return E;
4241 Names.push_back(E);
4242 }
4243 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4244 return make<ConversionExpr>(Ty, Exprs);
4245 }
4246
Pavel Labathba825192018-10-16 14:29:14 +00004247 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004248 if (E[0] == nullptr)
4249 return nullptr;
4250 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4251}
4252
4253// <expr-primary> ::= L <type> <value number> E # integer literal
4254// ::= L <type> <value float> E # floating literal
4255// ::= L <string type> E # string literal
4256// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004257// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004258// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4259// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004260template <typename Derived, typename Alloc>
4261Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004262 if (!consumeIf('L'))
4263 return nullptr;
4264 switch (look()) {
4265 case 'w':
4266 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004267 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004268 case 'b':
4269 if (consumeIf("b0E"))
4270 return make<BoolExpr>(0);
4271 if (consumeIf("b1E"))
4272 return make<BoolExpr>(1);
4273 return nullptr;
4274 case 'c':
4275 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004276 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004277 case 'a':
4278 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004279 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004280 case 'h':
4281 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004282 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004283 case 's':
4284 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004285 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004286 case 't':
4287 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004288 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004289 case 'i':
4290 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004291 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004292 case 'j':
4293 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004294 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004295 case 'l':
4296 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004297 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004298 case 'm':
4299 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004300 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004301 case 'x':
4302 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004303 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004304 case 'y':
4305 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004306 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004307 case 'n':
4308 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004309 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004310 case 'o':
4311 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004312 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004313 case 'f':
4314 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004315 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004316 case 'd':
4317 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004318 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004319 case 'e':
4320 ++First;
Xing Xue3dc5e082020-04-15 09:59:06 -04004321#if defined(__powerpc__) || defined(__s390__)
4322 // Handle cases where long doubles encoded with e have the same size
4323 // and representation as doubles.
4324 return getDerived().template parseFloatingLiteral<double>();
4325#else
Pavel Labathba825192018-10-16 14:29:14 +00004326 return getDerived().template parseFloatingLiteral<long double>();
Xing Xue3dc5e082020-04-15 09:59:06 -04004327#endif
Richard Smithc20d1442018-08-20 20:14:49 +00004328 case '_':
4329 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004330 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004331 if (R != nullptr && consumeIf('E'))
4332 return R;
4333 }
4334 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004335 case 'A': {
4336 Node *T = getDerived().parseType();
4337 if (T == nullptr)
4338 return nullptr;
4339 // FIXME: We need to include the string contents in the mangling.
4340 if (consumeIf('E'))
4341 return make<StringLiteral>(T);
4342 return nullptr;
4343 }
4344 case 'D':
4345 if (consumeIf("DnE"))
4346 return make<NameType>("nullptr");
4347 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004348 case 'T':
4349 // Invalid mangled name per
4350 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4351 return nullptr;
Richard Smithfb917462019-09-09 22:26:04 +00004352 case 'U': {
4353 // FIXME: Should we support LUb... for block literals?
4354 if (look(1) != 'l')
4355 return nullptr;
4356 Node *T = parseUnnamedTypeName(nullptr);
4357 if (!T || !consumeIf('E'))
4358 return nullptr;
4359 return make<LambdaExpr>(T);
4360 }
Richard Smithc20d1442018-08-20 20:14:49 +00004361 default: {
4362 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004363 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004364 if (T == nullptr)
4365 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004366 StringView N = parseNumber(/*AllowNegative=*/true);
Richard Smithfb917462019-09-09 22:26:04 +00004367 if (N.empty())
4368 return nullptr;
4369 if (!consumeIf('E'))
4370 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004371 return make<EnumLiteral>(T, N);
Richard Smithc20d1442018-08-20 20:14:49 +00004372 }
4373 }
4374}
4375
4376// <braced-expression> ::= <expression>
4377// ::= di <field source-name> <braced-expression> # .name = expr
4378// ::= dx <index expression> <braced-expression> # [expr] = expr
4379// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004380template <typename Derived, typename Alloc>
4381Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004382 if (look() == 'd') {
4383 switch (look(1)) {
4384 case 'i': {
4385 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004386 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004387 if (Field == nullptr)
4388 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004389 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004390 if (Init == nullptr)
4391 return nullptr;
4392 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4393 }
4394 case 'x': {
4395 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004396 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004397 if (Index == nullptr)
4398 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004399 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004400 if (Init == nullptr)
4401 return nullptr;
4402 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4403 }
4404 case 'X': {
4405 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004406 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004407 if (RangeBegin == nullptr)
4408 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004409 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004410 if (RangeEnd == nullptr)
4411 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004412 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004413 if (Init == nullptr)
4414 return nullptr;
4415 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4416 }
4417 }
4418 }
Pavel Labathba825192018-10-16 14:29:14 +00004419 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004420}
4421
4422// (not yet in the spec)
4423// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4424// ::= fR <binary-operator-name> <expression> <expression>
4425// ::= fl <binary-operator-name> <expression>
4426// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004427template <typename Derived, typename Alloc>
4428Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004429 if (!consumeIf('f'))
4430 return nullptr;
4431
4432 char FoldKind = look();
4433 bool IsLeftFold, HasInitializer;
4434 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4435 if (FoldKind == 'l' || FoldKind == 'L')
4436 IsLeftFold = true;
4437 else if (FoldKind == 'r' || FoldKind == 'R')
4438 IsLeftFold = false;
4439 else
4440 return nullptr;
4441 ++First;
4442
4443 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4444 StringView OperatorName;
4445 if (consumeIf("aa")) OperatorName = "&&";
4446 else if (consumeIf("an")) OperatorName = "&";
4447 else if (consumeIf("aN")) OperatorName = "&=";
4448 else if (consumeIf("aS")) OperatorName = "=";
4449 else if (consumeIf("cm")) OperatorName = ",";
4450 else if (consumeIf("ds")) OperatorName = ".*";
4451 else if (consumeIf("dv")) OperatorName = "/";
4452 else if (consumeIf("dV")) OperatorName = "/=";
4453 else if (consumeIf("eo")) OperatorName = "^";
4454 else if (consumeIf("eO")) OperatorName = "^=";
4455 else if (consumeIf("eq")) OperatorName = "==";
4456 else if (consumeIf("ge")) OperatorName = ">=";
4457 else if (consumeIf("gt")) OperatorName = ">";
4458 else if (consumeIf("le")) OperatorName = "<=";
4459 else if (consumeIf("ls")) OperatorName = "<<";
4460 else if (consumeIf("lS")) OperatorName = "<<=";
4461 else if (consumeIf("lt")) OperatorName = "<";
4462 else if (consumeIf("mi")) OperatorName = "-";
4463 else if (consumeIf("mI")) OperatorName = "-=";
4464 else if (consumeIf("ml")) OperatorName = "*";
4465 else if (consumeIf("mL")) OperatorName = "*=";
4466 else if (consumeIf("ne")) OperatorName = "!=";
4467 else if (consumeIf("oo")) OperatorName = "||";
4468 else if (consumeIf("or")) OperatorName = "|";
4469 else if (consumeIf("oR")) OperatorName = "|=";
4470 else if (consumeIf("pl")) OperatorName = "+";
4471 else if (consumeIf("pL")) OperatorName = "+=";
4472 else if (consumeIf("rm")) OperatorName = "%";
4473 else if (consumeIf("rM")) OperatorName = "%=";
4474 else if (consumeIf("rs")) OperatorName = ">>";
4475 else if (consumeIf("rS")) OperatorName = ">>=";
4476 else return nullptr;
4477
Pavel Labathba825192018-10-16 14:29:14 +00004478 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004479 if (Pack == nullptr)
4480 return nullptr;
4481 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004482 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004483 if (Init == nullptr)
4484 return nullptr;
4485 }
4486
4487 if (IsLeftFold && Init)
4488 std::swap(Pack, Init);
4489
4490 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4491}
4492
Richard Smith1865d2f2020-10-22 19:29:36 -07004493// <expression> ::= mc <parameter type> <expr> [<offset number>] E
4494//
4495// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4496template <typename Derived, typename Alloc>
4497Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() {
4498 Node *Ty = getDerived().parseType();
4499 if (!Ty)
4500 return nullptr;
4501 Node *Expr = getDerived().parseExpr();
4502 if (!Expr)
4503 return nullptr;
4504 StringView Offset = getDerived().parseNumber(true);
4505 if (!consumeIf('E'))
4506 return nullptr;
4507 return make<PointerToMemberConversionExpr>(Ty, Expr, Offset);
4508}
4509
4510// <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
4511// <union-selector> ::= _ [<number>]
4512//
4513// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4514template <typename Derived, typename Alloc>
4515Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
4516 Node *Ty = getDerived().parseType();
4517 if (!Ty)
4518 return nullptr;
4519 Node *Expr = getDerived().parseExpr();
4520 if (!Expr)
4521 return nullptr;
4522 StringView Offset = getDerived().parseNumber(true);
4523 size_t SelectorsBegin = Names.size();
4524 while (consumeIf('_')) {
4525 Node *Selector = make<NameType>(parseNumber());
4526 if (!Selector)
4527 return nullptr;
4528 Names.push_back(Selector);
4529 }
4530 bool OnePastTheEnd = consumeIf('p');
4531 if (!consumeIf('E'))
4532 return nullptr;
4533 return make<SubobjectExpr>(
4534 Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
4535}
4536
Richard Smithc20d1442018-08-20 20:14:49 +00004537// <expression> ::= <unary operator-name> <expression>
4538// ::= <binary operator-name> <expression> <expression>
4539// ::= <ternary operator-name> <expression> <expression> <expression>
4540// ::= cl <expression>+ E # call
4541// ::= cv <type> <expression> # conversion with one argument
4542// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4543// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4544// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4545// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4546// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4547// ::= [gs] dl <expression> # delete expression
4548// ::= [gs] da <expression> # delete[] expression
4549// ::= pp_ <expression> # prefix ++
4550// ::= mm_ <expression> # prefix --
4551// ::= ti <type> # typeid (type)
4552// ::= te <expression> # typeid (expression)
4553// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4554// ::= sc <type> <expression> # static_cast<type> (expression)
4555// ::= cc <type> <expression> # const_cast<type> (expression)
4556// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4557// ::= st <type> # sizeof (a type)
4558// ::= sz <expression> # sizeof (an expression)
4559// ::= at <type> # alignof (a type)
4560// ::= az <expression> # alignof (an expression)
4561// ::= nx <expression> # noexcept (expression)
4562// ::= <template-param>
4563// ::= <function-param>
4564// ::= dt <expression> <unresolved-name> # expr.name
4565// ::= pt <expression> <unresolved-name> # expr->name
4566// ::= ds <expression> <expression> # expr.*expr
4567// ::= sZ <template-param> # size of a parameter pack
4568// ::= sZ <function-param> # size of a function parameter pack
4569// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4570// ::= sp <expression> # pack expansion
4571// ::= tw <expression> # throw expression
4572// ::= tr # throw with no operand (rethrow)
4573// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4574// # freestanding dependent name (e.g., T::x),
4575// # objectless nonstatic member reference
4576// ::= fL <binary-operator-name> <expression> <expression>
4577// ::= fR <binary-operator-name> <expression> <expression>
4578// ::= fl <binary-operator-name> <expression>
4579// ::= fr <binary-operator-name> <expression>
4580// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004581template <typename Derived, typename Alloc>
4582Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004583 bool Global = consumeIf("gs");
4584 if (numLeft() < 2)
4585 return nullptr;
4586
4587 switch (*First) {
4588 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004589 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004590 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004591 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004592 case 'f': {
4593 // Disambiguate a fold expression from a <function-param>.
4594 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004595 return getDerived().parseFunctionParam();
4596 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004597 }
4598 case 'a':
4599 switch (First[1]) {
4600 case 'a':
4601 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004602 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004603 case 'd':
4604 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004605 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004606 case 'n':
4607 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004608 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004609 case 'N':
4610 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004611 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004612 case 'S':
4613 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004614 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004615 case 't': {
4616 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004617 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004618 if (Ty == nullptr)
4619 return nullptr;
4620 return make<EnclosingExpr>("alignof (", Ty, ")");
4621 }
4622 case 'z': {
4623 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004624 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004625 if (Ty == nullptr)
4626 return nullptr;
4627 return make<EnclosingExpr>("alignof (", Ty, ")");
4628 }
4629 }
4630 return nullptr;
4631 case 'c':
4632 switch (First[1]) {
4633 // cc <type> <expression> # const_cast<type>(expression)
4634 case 'c': {
4635 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004636 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004637 if (Ty == nullptr)
4638 return Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004639 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004640 if (Ex == nullptr)
4641 return Ex;
4642 return make<CastExpr>("const_cast", Ty, Ex);
4643 }
4644 // cl <expression>+ E # call
4645 case 'l': {
4646 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004647 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004648 if (Callee == nullptr)
4649 return Callee;
4650 size_t ExprsBegin = Names.size();
4651 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004652 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004653 if (E == nullptr)
4654 return E;
4655 Names.push_back(E);
4656 }
4657 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4658 }
4659 case 'm':
4660 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004661 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004662 case 'o':
4663 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004664 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004665 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004666 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004667 }
4668 return nullptr;
4669 case 'd':
4670 switch (First[1]) {
4671 case 'a': {
4672 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004673 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004674 if (Ex == nullptr)
4675 return Ex;
4676 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4677 }
4678 case 'c': {
4679 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004680 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004681 if (T == nullptr)
4682 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004683 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004684 if (Ex == nullptr)
4685 return Ex;
4686 return make<CastExpr>("dynamic_cast", T, Ex);
4687 }
4688 case 'e':
4689 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004690 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004691 case 'l': {
4692 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004693 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004694 if (E == nullptr)
4695 return E;
4696 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4697 }
4698 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004699 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004700 case 's': {
4701 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004702 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004703 if (LHS == nullptr)
4704 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004705 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004706 if (RHS == nullptr)
4707 return nullptr;
4708 return make<MemberExpr>(LHS, ".*", RHS);
4709 }
4710 case 't': {
4711 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004712 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004713 if (LHS == nullptr)
4714 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004715 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004716 if (RHS == nullptr)
4717 return nullptr;
4718 return make<MemberExpr>(LHS, ".", RHS);
4719 }
4720 case 'v':
4721 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004722 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004723 case 'V':
4724 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004725 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004726 }
4727 return nullptr;
4728 case 'e':
4729 switch (First[1]) {
4730 case 'o':
4731 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004732 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004733 case 'O':
4734 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004735 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004736 case 'q':
4737 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004738 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004739 }
4740 return nullptr;
4741 case 'g':
4742 switch (First[1]) {
4743 case 'e':
4744 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004745 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004746 case 't':
4747 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004748 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004749 }
4750 return nullptr;
4751 case 'i':
4752 switch (First[1]) {
4753 case 'x': {
4754 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004755 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004756 if (Base == nullptr)
4757 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004758 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004759 if (Index == nullptr)
4760 return Index;
4761 return make<ArraySubscriptExpr>(Base, Index);
4762 }
4763 case 'l': {
4764 First += 2;
4765 size_t InitsBegin = Names.size();
4766 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004767 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004768 if (E == nullptr)
4769 return nullptr;
4770 Names.push_back(E);
4771 }
4772 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4773 }
4774 }
4775 return nullptr;
4776 case 'l':
4777 switch (First[1]) {
4778 case 'e':
4779 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004780 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004781 case 's':
4782 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004783 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004784 case 'S':
4785 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004786 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004787 case 't':
4788 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004789 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004790 }
4791 return nullptr;
4792 case 'm':
4793 switch (First[1]) {
Richard Smith1865d2f2020-10-22 19:29:36 -07004794 case 'c':
4795 First += 2;
4796 return parsePointerToMemberConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004797 case 'i':
4798 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004799 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004800 case 'I':
4801 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004802 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004803 case 'l':
4804 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004805 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004806 case 'L':
4807 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004808 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004809 case 'm':
4810 First += 2;
4811 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004812 return getDerived().parsePrefixExpr("--");
4813 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004814 if (Ex == nullptr)
4815 return nullptr;
4816 return make<PostfixExpr>(Ex, "--");
4817 }
4818 return nullptr;
4819 case 'n':
4820 switch (First[1]) {
4821 case 'a':
4822 case 'w':
Pavel Labathba825192018-10-16 14:29:14 +00004823 return getDerived().parseNewExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004824 case 'e':
4825 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004826 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004827 case 'g':
4828 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004829 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004830 case 't':
4831 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004832 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004833 case 'x':
4834 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004835 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004836 if (Ex == nullptr)
4837 return Ex;
4838 return make<EnclosingExpr>("noexcept (", Ex, ")");
4839 }
4840 return nullptr;
4841 case 'o':
4842 switch (First[1]) {
4843 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004844 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004845 case 'o':
4846 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004847 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004848 case 'r':
4849 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004850 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004851 case 'R':
4852 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004853 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004854 }
4855 return nullptr;
4856 case 'p':
4857 switch (First[1]) {
4858 case 'm':
4859 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004860 return getDerived().parseBinaryExpr("->*");
Richard Smithc20d1442018-08-20 20:14:49 +00004861 case 'l':
4862 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004863 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004864 case 'L':
4865 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004866 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004867 case 'p': {
4868 First += 2;
4869 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004870 return getDerived().parsePrefixExpr("++");
4871 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004872 if (Ex == nullptr)
4873 return Ex;
4874 return make<PostfixExpr>(Ex, "++");
4875 }
4876 case 's':
4877 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004878 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004879 case 't': {
4880 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004881 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004882 if (L == nullptr)
4883 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004884 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004885 if (R == nullptr)
4886 return nullptr;
4887 return make<MemberExpr>(L, "->", R);
4888 }
4889 }
4890 return nullptr;
4891 case 'q':
4892 if (First[1] == 'u') {
4893 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004894 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004895 if (Cond == nullptr)
4896 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004897 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004898 if (LHS == nullptr)
4899 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004900 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004901 if (RHS == nullptr)
4902 return nullptr;
4903 return make<ConditionalExpr>(Cond, LHS, RHS);
4904 }
4905 return nullptr;
4906 case 'r':
4907 switch (First[1]) {
4908 case 'c': {
4909 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004910 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004911 if (T == nullptr)
4912 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004913 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004914 if (Ex == nullptr)
4915 return Ex;
4916 return make<CastExpr>("reinterpret_cast", T, Ex);
4917 }
4918 case 'm':
4919 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004920 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004921 case 'M':
4922 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004923 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004924 case 's':
4925 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004926 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004927 case 'S':
4928 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004929 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004930 }
4931 return nullptr;
4932 case 's':
4933 switch (First[1]) {
4934 case 'c': {
4935 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004936 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004937 if (T == nullptr)
4938 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004939 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004940 if (Ex == nullptr)
4941 return Ex;
4942 return make<CastExpr>("static_cast", T, Ex);
4943 }
Richard Smith1865d2f2020-10-22 19:29:36 -07004944 case 'o':
4945 First += 2;
4946 return parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004947 case 'p': {
4948 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004949 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004950 if (Child == nullptr)
4951 return nullptr;
4952 return make<ParameterPackExpansion>(Child);
4953 }
4954 case 'r':
Pavel Labathba825192018-10-16 14:29:14 +00004955 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004956 case 't': {
4957 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004958 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004959 if (Ty == nullptr)
4960 return Ty;
4961 return make<EnclosingExpr>("sizeof (", Ty, ")");
4962 }
4963 case 'z': {
4964 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004965 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004966 if (Ex == nullptr)
4967 return Ex;
4968 return make<EnclosingExpr>("sizeof (", Ex, ")");
4969 }
4970 case 'Z':
4971 First += 2;
4972 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004973 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004974 if (R == nullptr)
4975 return nullptr;
4976 return make<SizeofParamPackExpr>(R);
4977 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004978 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004979 if (FP == nullptr)
4980 return nullptr;
4981 return make<EnclosingExpr>("sizeof... (", FP, ")");
4982 }
4983 return nullptr;
4984 case 'P': {
4985 First += 2;
4986 size_t ArgsBegin = Names.size();
4987 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004988 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004989 if (Arg == nullptr)
4990 return nullptr;
4991 Names.push_back(Arg);
4992 }
Richard Smithb485b352018-08-24 23:30:26 +00004993 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4994 if (!Pack)
4995 return nullptr;
4996 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00004997 }
4998 }
4999 return nullptr;
5000 case 't':
5001 switch (First[1]) {
5002 case 'e': {
5003 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005004 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005005 if (Ex == nullptr)
5006 return Ex;
5007 return make<EnclosingExpr>("typeid (", Ex, ")");
5008 }
5009 case 'i': {
5010 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005011 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005012 if (Ty == nullptr)
5013 return Ty;
5014 return make<EnclosingExpr>("typeid (", Ty, ")");
5015 }
5016 case 'l': {
5017 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005018 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005019 if (Ty == nullptr)
5020 return nullptr;
5021 size_t InitsBegin = Names.size();
5022 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005023 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005024 if (E == nullptr)
5025 return nullptr;
5026 Names.push_back(E);
5027 }
5028 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
5029 }
5030 case 'r':
5031 First += 2;
5032 return make<NameType>("throw");
5033 case 'w': {
5034 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005035 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005036 if (Ex == nullptr)
5037 return nullptr;
5038 return make<ThrowExpr>(Ex);
5039 }
5040 }
5041 return nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05005042 case 'u': {
5043 ++First;
5044 Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
5045 if (!Name)
5046 return nullptr;
5047 // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
5048 // standard encoding expects a <template-arg>, and would be otherwise be
5049 // interpreted as <type> node 'short' or 'ellipsis'. However, neither
5050 // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
5051 // actual conflict here.
5052 if (Name->getBaseName() == "__uuidof") {
5053 if (numLeft() < 2)
5054 return nullptr;
5055 if (*First == 't') {
5056 ++First;
5057 Node *Ty = getDerived().parseType();
5058 if (!Ty)
5059 return nullptr;
5060 return make<CallExpr>(Name, makeNodeArray(&Ty, &Ty + 1));
5061 }
5062 if (*First == 'z') {
5063 ++First;
5064 Node *Ex = getDerived().parseExpr();
5065 if (!Ex)
5066 return nullptr;
5067 return make<CallExpr>(Name, makeNodeArray(&Ex, &Ex + 1));
5068 }
5069 }
5070 size_t ExprsBegin = Names.size();
5071 while (!consumeIf('E')) {
5072 Node *E = getDerived().parseTemplateArg();
5073 if (E == nullptr)
5074 return E;
5075 Names.push_back(E);
5076 }
5077 return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin));
5078 }
Richard Smithc20d1442018-08-20 20:14:49 +00005079 case '1':
5080 case '2':
5081 case '3':
5082 case '4':
5083 case '5':
5084 case '6':
5085 case '7':
5086 case '8':
5087 case '9':
Pavel Labathba825192018-10-16 14:29:14 +00005088 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00005089 }
5090 return nullptr;
5091}
5092
5093// <call-offset> ::= h <nv-offset> _
5094// ::= v <v-offset> _
5095//
5096// <nv-offset> ::= <offset number>
5097// # non-virtual base override
5098//
5099// <v-offset> ::= <offset number> _ <virtual offset number>
5100// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00005101template <typename Alloc, typename Derived>
5102bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00005103 // Just scan through the call offset, we never add this information into the
5104 // output.
5105 if (consumeIf('h'))
5106 return parseNumber(true).empty() || !consumeIf('_');
5107 if (consumeIf('v'))
5108 return parseNumber(true).empty() || !consumeIf('_') ||
5109 parseNumber(true).empty() || !consumeIf('_');
5110 return true;
5111}
5112
5113// <special-name> ::= TV <type> # virtual table
5114// ::= TT <type> # VTT structure (construction vtable index)
5115// ::= TI <type> # typeinfo structure
5116// ::= TS <type> # typeinfo name (null-terminated byte string)
5117// ::= Tc <call-offset> <call-offset> <base encoding>
5118// # base is the nominal target function of thunk
5119// # first call-offset is 'this' adjustment
5120// # second call-offset is result adjustment
5121// ::= T <call-offset> <base encoding>
5122// # base is the nominal target function of thunk
5123// ::= GV <object name> # Guard variable for one-time initialization
5124// # No <type>
5125// ::= TW <object name> # Thread-local wrapper
5126// ::= TH <object name> # Thread-local initialization
5127// ::= GR <object name> _ # First temporary
5128// ::= GR <object name> <seq-id> _ # Subsequent temporaries
5129// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
5130// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00005131template <typename Derived, typename Alloc>
5132Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00005133 switch (look()) {
5134 case 'T':
5135 switch (look(1)) {
Richard Smith1865d2f2020-10-22 19:29:36 -07005136 // TA <template-arg> # template parameter object
5137 //
5138 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
5139 case 'A': {
5140 First += 2;
5141 Node *Arg = getDerived().parseTemplateArg();
5142 if (Arg == nullptr)
5143 return nullptr;
5144 return make<SpecialName>("template parameter object for ", Arg);
5145 }
Richard Smithc20d1442018-08-20 20:14:49 +00005146 // TV <type> # virtual table
5147 case 'V': {
5148 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005149 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005150 if (Ty == nullptr)
5151 return nullptr;
5152 return make<SpecialName>("vtable for ", Ty);
5153 }
5154 // TT <type> # VTT structure (construction vtable index)
5155 case 'T': {
5156 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005157 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005158 if (Ty == nullptr)
5159 return nullptr;
5160 return make<SpecialName>("VTT for ", Ty);
5161 }
5162 // TI <type> # typeinfo structure
5163 case 'I': {
5164 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005165 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005166 if (Ty == nullptr)
5167 return nullptr;
5168 return make<SpecialName>("typeinfo for ", Ty);
5169 }
5170 // TS <type> # typeinfo name (null-terminated byte string)
5171 case 'S': {
5172 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005173 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005174 if (Ty == nullptr)
5175 return nullptr;
5176 return make<SpecialName>("typeinfo name for ", Ty);
5177 }
5178 // Tc <call-offset> <call-offset> <base encoding>
5179 case 'c': {
5180 First += 2;
5181 if (parseCallOffset() || parseCallOffset())
5182 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005183 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005184 if (Encoding == nullptr)
5185 return nullptr;
5186 return make<SpecialName>("covariant return thunk to ", Encoding);
5187 }
5188 // extension ::= TC <first type> <number> _ <second type>
5189 // # construction vtable for second-in-first
5190 case 'C': {
5191 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005192 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005193 if (FirstType == nullptr)
5194 return nullptr;
5195 if (parseNumber(true).empty() || !consumeIf('_'))
5196 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005197 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005198 if (SecondType == nullptr)
5199 return nullptr;
5200 return make<CtorVtableSpecialName>(SecondType, FirstType);
5201 }
5202 // TW <object name> # Thread-local wrapper
5203 case 'W': {
5204 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005205 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005206 if (Name == nullptr)
5207 return nullptr;
5208 return make<SpecialName>("thread-local wrapper routine for ", Name);
5209 }
5210 // TH <object name> # Thread-local initialization
5211 case 'H': {
5212 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005213 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005214 if (Name == nullptr)
5215 return nullptr;
5216 return make<SpecialName>("thread-local initialization routine for ", Name);
5217 }
5218 // T <call-offset> <base encoding>
5219 default: {
5220 ++First;
5221 bool IsVirt = look() == 'v';
5222 if (parseCallOffset())
5223 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005224 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005225 if (BaseEncoding == nullptr)
5226 return nullptr;
5227 if (IsVirt)
5228 return make<SpecialName>("virtual thunk to ", BaseEncoding);
5229 else
5230 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
5231 }
5232 }
5233 case 'G':
5234 switch (look(1)) {
5235 // GV <object name> # Guard variable for one-time initialization
5236 case 'V': {
5237 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005238 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005239 if (Name == nullptr)
5240 return nullptr;
5241 return make<SpecialName>("guard variable for ", Name);
5242 }
5243 // GR <object name> # reference temporary for object
5244 // GR <object name> _ # First temporary
5245 // GR <object name> <seq-id> _ # Subsequent temporaries
5246 case 'R': {
5247 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005248 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005249 if (Name == nullptr)
5250 return nullptr;
5251 size_t Count;
5252 bool ParsedSeqId = !parseSeqId(&Count);
5253 if (!consumeIf('_') && ParsedSeqId)
5254 return nullptr;
5255 return make<SpecialName>("reference temporary for ", Name);
5256 }
5257 }
5258 }
5259 return nullptr;
5260}
5261
5262// <encoding> ::= <function name> <bare-function-type>
5263// ::= <data name>
5264// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00005265template <typename Derived, typename Alloc>
5266Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithfac39712020-07-09 21:08:39 -07005267 // The template parameters of an encoding are unrelated to those of the
5268 // enclosing context.
5269 class SaveTemplateParams {
5270 AbstractManglingParser *Parser;
5271 decltype(TemplateParams) OldParams;
Justin Lebar2c536232021-06-09 16:57:22 -07005272 decltype(OuterTemplateParams) OldOuterParams;
Richard Smithfac39712020-07-09 21:08:39 -07005273
5274 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04005275 SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
Richard Smithfac39712020-07-09 21:08:39 -07005276 OldParams = std::move(Parser->TemplateParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005277 OldOuterParams = std::move(Parser->OuterTemplateParams);
Richard Smithfac39712020-07-09 21:08:39 -07005278 Parser->TemplateParams.clear();
Justin Lebar2c536232021-06-09 16:57:22 -07005279 Parser->OuterTemplateParams.clear();
Richard Smithfac39712020-07-09 21:08:39 -07005280 }
5281 ~SaveTemplateParams() {
5282 Parser->TemplateParams = std::move(OldParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005283 Parser->OuterTemplateParams = std::move(OldOuterParams);
Richard Smithfac39712020-07-09 21:08:39 -07005284 }
5285 } SaveTemplateParams(this);
Richard Smithfd434322020-07-09 20:36:04 -07005286
Richard Smithc20d1442018-08-20 20:14:49 +00005287 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00005288 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00005289
5290 auto IsEndOfEncoding = [&] {
5291 // The set of chars that can potentially follow an <encoding> (none of which
5292 // can start a <type>). Enumerating these allows us to avoid speculative
5293 // parsing.
5294 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5295 };
5296
5297 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00005298 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00005299 if (Name == nullptr)
5300 return nullptr;
5301
5302 if (resolveForwardTemplateRefs(NameInfo))
5303 return nullptr;
5304
5305 if (IsEndOfEncoding())
5306 return Name;
5307
5308 Node *Attrs = nullptr;
5309 if (consumeIf("Ua9enable_ifI")) {
5310 size_t BeforeArgs = Names.size();
5311 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005312 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005313 if (Arg == nullptr)
5314 return nullptr;
5315 Names.push_back(Arg);
5316 }
5317 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00005318 if (!Attrs)
5319 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005320 }
5321
5322 Node *ReturnType = nullptr;
5323 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00005324 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005325 if (ReturnType == nullptr)
5326 return nullptr;
5327 }
5328
5329 if (consumeIf('v'))
5330 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5331 Attrs, NameInfo.CVQualifiers,
5332 NameInfo.ReferenceQualifier);
5333
5334 size_t ParamsBegin = Names.size();
5335 do {
Pavel Labathba825192018-10-16 14:29:14 +00005336 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005337 if (Ty == nullptr)
5338 return nullptr;
5339 Names.push_back(Ty);
5340 } while (!IsEndOfEncoding());
5341
5342 return make<FunctionEncoding>(ReturnType, Name,
5343 popTrailingNodeArray(ParamsBegin),
5344 Attrs, NameInfo.CVQualifiers,
5345 NameInfo.ReferenceQualifier);
5346}
5347
5348template <class Float>
5349struct FloatData;
5350
5351template <>
5352struct FloatData<float>
5353{
5354 static const size_t mangled_size = 8;
5355 static const size_t max_demangled_size = 24;
5356 static constexpr const char* spec = "%af";
5357};
5358
5359template <>
5360struct FloatData<double>
5361{
5362 static const size_t mangled_size = 16;
5363 static const size_t max_demangled_size = 32;
5364 static constexpr const char* spec = "%a";
5365};
5366
5367template <>
5368struct FloatData<long double>
5369{
5370#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5371 defined(__wasm__)
5372 static const size_t mangled_size = 32;
5373#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5374 static const size_t mangled_size = 16;
5375#else
5376 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5377#endif
Elliott Hughes5a360ea2020-04-10 17:42:00 -07005378 // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
5379 // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
5380 // Negatives are one character longer than positives.
5381 // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
5382 // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
5383 static const size_t max_demangled_size = 42;
Richard Smithc20d1442018-08-20 20:14:49 +00005384 static constexpr const char *spec = "%LaL";
5385};
5386
Pavel Labathba825192018-10-16 14:29:14 +00005387template <typename Alloc, typename Derived>
5388template <class Float>
5389Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005390 const size_t N = FloatData<Float>::mangled_size;
5391 if (numLeft() <= N)
5392 return nullptr;
5393 StringView Data(First, First + N);
5394 for (char C : Data)
5395 if (!std::isxdigit(C))
5396 return nullptr;
5397 First += N;
5398 if (!consumeIf('E'))
5399 return nullptr;
5400 return make<FloatLiteralImpl<Float>>(Data);
5401}
5402
5403// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005404template <typename Alloc, typename Derived>
5405bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005406 if (!(look() >= '0' && look() <= '9') &&
5407 !(look() >= 'A' && look() <= 'Z'))
5408 return true;
5409
5410 size_t Id = 0;
5411 while (true) {
5412 if (look() >= '0' && look() <= '9') {
5413 Id *= 36;
5414 Id += static_cast<size_t>(look() - '0');
5415 } else if (look() >= 'A' && look() <= 'Z') {
5416 Id *= 36;
5417 Id += static_cast<size_t>(look() - 'A') + 10;
5418 } else {
5419 *Out = Id;
5420 return false;
5421 }
5422 ++First;
5423 }
5424}
5425
5426// <substitution> ::= S <seq-id> _
5427// ::= S_
5428// <substitution> ::= Sa # ::std::allocator
5429// <substitution> ::= Sb # ::std::basic_string
5430// <substitution> ::= Ss # ::std::basic_string < char,
5431// ::std::char_traits<char>,
5432// ::std::allocator<char> >
5433// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5434// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5435// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Pavel Labathba825192018-10-16 14:29:14 +00005436template <typename Derived, typename Alloc>
5437Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005438 if (!consumeIf('S'))
5439 return nullptr;
5440
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08005441 if (look() >= 'a' && look() <= 'z') {
Richard Smithc20d1442018-08-20 20:14:49 +00005442 Node *SpecialSub;
5443 switch (look()) {
5444 case 'a':
5445 ++First;
5446 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::allocator);
5447 break;
5448 case 'b':
5449 ++First;
5450 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::basic_string);
5451 break;
5452 case 's':
5453 ++First;
5454 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::string);
5455 break;
5456 case 'i':
5457 ++First;
5458 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::istream);
5459 break;
5460 case 'o':
5461 ++First;
5462 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::ostream);
5463 break;
5464 case 'd':
5465 ++First;
5466 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::iostream);
5467 break;
5468 default:
5469 return nullptr;
5470 }
Richard Smithb485b352018-08-24 23:30:26 +00005471 if (!SpecialSub)
5472 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005473 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5474 // has ABI tags, the tags are appended to the substitution; the result is a
5475 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005476 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005477 if (WithTags != SpecialSub) {
5478 Subs.push_back(WithTags);
5479 SpecialSub = WithTags;
5480 }
5481 return SpecialSub;
5482 }
5483
5484 // ::= S_
5485 if (consumeIf('_')) {
5486 if (Subs.empty())
5487 return nullptr;
5488 return Subs[0];
5489 }
5490
5491 // ::= S <seq-id> _
5492 size_t Index = 0;
5493 if (parseSeqId(&Index))
5494 return nullptr;
5495 ++Index;
5496 if (!consumeIf('_') || Index >= Subs.size())
5497 return nullptr;
5498 return Subs[Index];
5499}
5500
5501// <template-param> ::= T_ # first template parameter
5502// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005503// ::= TL <level-1> __
5504// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005505template <typename Derived, typename Alloc>
5506Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005507 if (!consumeIf('T'))
5508 return nullptr;
5509
Richard Smithdf1c14c2019-09-06 23:53:21 +00005510 size_t Level = 0;
5511 if (consumeIf('L')) {
5512 if (parsePositiveInteger(&Level))
5513 return nullptr;
5514 ++Level;
5515 if (!consumeIf('_'))
5516 return nullptr;
5517 }
5518
Richard Smithc20d1442018-08-20 20:14:49 +00005519 size_t Index = 0;
5520 if (!consumeIf('_')) {
5521 if (parsePositiveInteger(&Index))
5522 return nullptr;
5523 ++Index;
5524 if (!consumeIf('_'))
5525 return nullptr;
5526 }
5527
Richard Smithc20d1442018-08-20 20:14:49 +00005528 // If we're in a context where this <template-param> refers to a
5529 // <template-arg> further ahead in the mangled name (currently just conversion
5530 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005531 // This can only happen at the outermost level.
5532 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005533 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5534 if (!ForwardRef)
5535 return nullptr;
5536 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5537 ForwardTemplateRefs.push_back(
5538 static_cast<ForwardTemplateReference *>(ForwardRef));
5539 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005540 }
5541
Richard Smithdf1c14c2019-09-06 23:53:21 +00005542 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5543 Index >= TemplateParams[Level]->size()) {
5544 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5545 // list are mangled as the corresponding artificial template type parameter.
5546 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5547 // This will be popped by the ScopedTemplateParamList in
5548 // parseUnnamedTypeName.
5549 if (Level == TemplateParams.size())
5550 TemplateParams.push_back(nullptr);
5551 return make<NameType>("auto");
5552 }
5553
Richard Smithc20d1442018-08-20 20:14:49 +00005554 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005555 }
5556
5557 return (*TemplateParams[Level])[Index];
5558}
5559
5560// <template-param-decl> ::= Ty # type parameter
5561// ::= Tn <type> # non-type parameter
5562// ::= Tt <template-param-decl>* E # template parameter
5563// ::= Tp <template-param-decl> # parameter pack
5564template <typename Derived, typename Alloc>
5565Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5566 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5567 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5568 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5569 if (N) TemplateParams.back()->push_back(N);
5570 return N;
5571 };
5572
5573 if (consumeIf("Ty")) {
5574 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5575 if (!Name)
5576 return nullptr;
5577 return make<TypeTemplateParamDecl>(Name);
5578 }
5579
5580 if (consumeIf("Tn")) {
5581 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5582 if (!Name)
5583 return nullptr;
5584 Node *Type = parseType();
5585 if (!Type)
5586 return nullptr;
5587 return make<NonTypeTemplateParamDecl>(Name, Type);
5588 }
5589
5590 if (consumeIf("Tt")) {
5591 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5592 if (!Name)
5593 return nullptr;
5594 size_t ParamsBegin = Names.size();
5595 ScopedTemplateParamList TemplateTemplateParamParams(this);
5596 while (!consumeIf("E")) {
5597 Node *P = parseTemplateParamDecl();
5598 if (!P)
5599 return nullptr;
5600 Names.push_back(P);
5601 }
5602 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5603 return make<TemplateTemplateParamDecl>(Name, Params);
5604 }
5605
5606 if (consumeIf("Tp")) {
5607 Node *P = parseTemplateParamDecl();
5608 if (!P)
5609 return nullptr;
5610 return make<TemplateParamPackDecl>(P);
5611 }
5612
5613 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005614}
5615
5616// <template-arg> ::= <type> # type or template
5617// ::= X <expression> E # expression
5618// ::= <expr-primary> # simple expressions
5619// ::= J <template-arg>* E # argument pack
5620// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005621template <typename Derived, typename Alloc>
5622Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005623 switch (look()) {
5624 case 'X': {
5625 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005626 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005627 if (Arg == nullptr || !consumeIf('E'))
5628 return nullptr;
5629 return Arg;
5630 }
5631 case 'J': {
5632 ++First;
5633 size_t ArgsBegin = Names.size();
5634 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005635 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005636 if (Arg == nullptr)
5637 return nullptr;
5638 Names.push_back(Arg);
5639 }
5640 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5641 return make<TemplateArgumentPack>(Args);
5642 }
5643 case 'L': {
5644 // ::= LZ <encoding> E # extension
5645 if (look(1) == 'Z') {
5646 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005647 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005648 if (Arg == nullptr || !consumeIf('E'))
5649 return nullptr;
5650 return Arg;
5651 }
5652 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005653 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005654 }
5655 default:
Pavel Labathba825192018-10-16 14:29:14 +00005656 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005657 }
5658}
5659
5660// <template-args> ::= I <template-arg>* E
5661// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005662template <typename Derived, typename Alloc>
5663Node *
5664AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005665 if (!consumeIf('I'))
5666 return nullptr;
5667
5668 // <template-params> refer to the innermost <template-args>. Clear out any
5669 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005670 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005671 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005672 TemplateParams.push_back(&OuterTemplateParams);
5673 OuterTemplateParams.clear();
5674 }
Richard Smithc20d1442018-08-20 20:14:49 +00005675
5676 size_t ArgsBegin = Names.size();
5677 while (!consumeIf('E')) {
5678 if (TagTemplates) {
5679 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005680 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005681 TemplateParams = std::move(OldParams);
5682 if (Arg == nullptr)
5683 return nullptr;
5684 Names.push_back(Arg);
5685 Node *TableEntry = Arg;
5686 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5687 TableEntry = make<ParameterPack>(
5688 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005689 if (!TableEntry)
5690 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005691 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005692 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005693 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005694 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005695 if (Arg == nullptr)
5696 return nullptr;
5697 Names.push_back(Arg);
5698 }
5699 }
5700 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5701}
5702
5703// <mangled-name> ::= _Z <encoding>
5704// ::= <type>
5705// extension ::= ___Z <encoding> _block_invoke
5706// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5707// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005708template <typename Derived, typename Alloc>
5709Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005710 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005711 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005712 if (Encoding == nullptr)
5713 return nullptr;
5714 if (look() == '.') {
5715 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5716 First = Last;
5717 }
5718 if (numLeft() != 0)
5719 return nullptr;
5720 return Encoding;
5721 }
5722
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005723 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005724 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005725 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5726 return nullptr;
5727 bool RequireNumber = consumeIf('_');
5728 if (parseNumber().empty() && RequireNumber)
5729 return nullptr;
5730 if (look() == '.')
5731 First = Last;
5732 if (numLeft() != 0)
5733 return nullptr;
5734 return make<SpecialName>("invocation function for block in ", Encoding);
5735 }
5736
Pavel Labathba825192018-10-16 14:29:14 +00005737 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005738 if (numLeft() != 0)
5739 return nullptr;
5740 return Ty;
5741}
5742
Pavel Labathba825192018-10-16 14:29:14 +00005743template <typename Alloc>
5744struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5745 using AbstractManglingParser<ManglingParser<Alloc>,
5746 Alloc>::AbstractManglingParser;
5747};
5748
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005749DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005750
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005751#endif // DEMANGLE_ITANIUMDEMANGLE_H