blob: 01f414a7257bf93f1bd0ce0da9b9271ca308a693 [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"
Nathan Sidwellbbc632f2022-01-24 04:11:59 -080024#include <algorithm>
Richard Smithc20d1442018-08-20 20:14:49 +000025#include <cassert>
26#include <cctype>
27#include <cstdio>
28#include <cstdlib>
29#include <cstring>
Nathan Sidwellbbc632f2022-01-24 04:11:59 -080030#include <limits>
Richard Smithc20d1442018-08-20 20:14:49 +000031#include <utility>
32
33#define FOR_EACH_NODE_KIND(X) \
34 X(NodeArrayNode) \
35 X(DotSuffix) \
36 X(VendorExtQualType) \
37 X(QualType) \
38 X(ConversionOperatorType) \
39 X(PostfixQualifiedType) \
40 X(ElaboratedTypeSpefType) \
41 X(NameType) \
42 X(AbiTagAttr) \
43 X(EnableIfAttr) \
44 X(ObjCProtoName) \
45 X(PointerType) \
46 X(ReferenceType) \
47 X(PointerToMemberType) \
48 X(ArrayType) \
49 X(FunctionType) \
50 X(NoexceptSpec) \
51 X(DynamicExceptionSpec) \
52 X(FunctionEncoding) \
53 X(LiteralOperator) \
54 X(SpecialName) \
55 X(CtorVtableSpecialName) \
56 X(QualifiedName) \
57 X(NestedName) \
58 X(LocalName) \
59 X(VectorType) \
60 X(PixelVectorType) \
Pengfei Wang50e90b82021-09-23 11:02:25 +080061 X(BinaryFPType) \
Richard Smithdf1c14c2019-09-06 23:53:21 +000062 X(SyntheticTemplateParamName) \
63 X(TypeTemplateParamDecl) \
64 X(NonTypeTemplateParamDecl) \
65 X(TemplateTemplateParamDecl) \
66 X(TemplateParamPackDecl) \
Richard Smithc20d1442018-08-20 20:14:49 +000067 X(ParameterPack) \
68 X(TemplateArgumentPack) \
69 X(ParameterPackExpansion) \
70 X(TemplateArgs) \
71 X(ForwardTemplateReference) \
72 X(NameWithTemplateArgs) \
73 X(GlobalQualifiedName) \
74 X(StdQualifiedName) \
75 X(ExpandedSpecialSubstitution) \
76 X(SpecialSubstitution) \
77 X(CtorDtorName) \
78 X(DtorName) \
79 X(UnnamedTypeName) \
80 X(ClosureTypeName) \
81 X(StructuredBindingName) \
82 X(BinaryExpr) \
83 X(ArraySubscriptExpr) \
84 X(PostfixExpr) \
85 X(ConditionalExpr) \
86 X(MemberExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070087 X(SubobjectExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000088 X(EnclosingExpr) \
89 X(CastExpr) \
90 X(SizeofParamPackExpr) \
91 X(CallExpr) \
92 X(NewExpr) \
93 X(DeleteExpr) \
94 X(PrefixExpr) \
95 X(FunctionParam) \
96 X(ConversionExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070097 X(PointerToMemberConversionExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000098 X(InitListExpr) \
99 X(FoldExpr) \
100 X(ThrowExpr) \
101 X(BoolExpr) \
Richard Smithdf1c14c2019-09-06 23:53:21 +0000102 X(StringLiteral) \
103 X(LambdaExpr) \
Erik Pilkington0a170f12020-05-13 14:13:37 -0400104 X(EnumLiteral) \
Richard Smithc20d1442018-08-20 20:14:49 +0000105 X(IntegerLiteral) \
106 X(FloatLiteral) \
107 X(DoubleLiteral) \
108 X(LongDoubleLiteral) \
109 X(BracedExpr) \
110 X(BracedRangeExpr)
111
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000112DEMANGLE_NAMESPACE_BEGIN
113
Mikhail Borisov8452f062021-08-17 18:06:53 -0400114template <class T, size_t N> class PODSmallVector {
115 static_assert(std::is_pod<T>::value,
116 "T is required to be a plain old data type");
117
118 T *First = nullptr;
119 T *Last = nullptr;
120 T *Cap = nullptr;
121 T Inline[N] = {0};
122
123 bool isInline() const { return First == Inline; }
124
125 void clearInline() {
126 First = Inline;
127 Last = Inline;
128 Cap = Inline + N;
129 }
130
131 void reserve(size_t NewCap) {
132 size_t S = size();
133 if (isInline()) {
134 auto *Tmp = static_cast<T *>(std::malloc(NewCap * sizeof(T)));
135 if (Tmp == nullptr)
136 std::terminate();
137 std::copy(First, Last, Tmp);
138 First = Tmp;
139 } else {
140 First = static_cast<T *>(std::realloc(First, NewCap * sizeof(T)));
141 if (First == nullptr)
142 std::terminate();
143 }
144 Last = First + S;
145 Cap = First + NewCap;
146 }
147
148public:
149 PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
150
151 PODSmallVector(const PODSmallVector &) = delete;
152 PODSmallVector &operator=(const PODSmallVector &) = delete;
153
154 PODSmallVector(PODSmallVector &&Other) : PODSmallVector() {
155 if (Other.isInline()) {
156 std::copy(Other.begin(), Other.end(), First);
157 Last = First + Other.size();
158 Other.clear();
159 return;
160 }
161
162 First = Other.First;
163 Last = Other.Last;
164 Cap = Other.Cap;
165 Other.clearInline();
166 }
167
168 PODSmallVector &operator=(PODSmallVector &&Other) {
169 if (Other.isInline()) {
170 if (!isInline()) {
171 std::free(First);
172 clearInline();
173 }
174 std::copy(Other.begin(), Other.end(), First);
175 Last = First + Other.size();
176 Other.clear();
177 return *this;
178 }
179
180 if (isInline()) {
181 First = Other.First;
182 Last = Other.Last;
183 Cap = Other.Cap;
184 Other.clearInline();
185 return *this;
186 }
187
188 std::swap(First, Other.First);
189 std::swap(Last, Other.Last);
190 std::swap(Cap, Other.Cap);
191 Other.clear();
192 return *this;
193 }
194
195 // NOLINTNEXTLINE(readability-identifier-naming)
196 void push_back(const T &Elem) {
197 if (Last == Cap)
198 reserve(size() * 2);
199 *Last++ = Elem;
200 }
201
202 // NOLINTNEXTLINE(readability-identifier-naming)
203 void pop_back() {
204 assert(Last != First && "Popping empty vector!");
205 --Last;
206 }
207
208 void dropBack(size_t Index) {
209 assert(Index <= size() && "dropBack() can't expand!");
210 Last = First + Index;
211 }
212
213 T *begin() { return First; }
214 T *end() { return Last; }
215
216 bool empty() const { return First == Last; }
217 size_t size() const { return static_cast<size_t>(Last - First); }
218 T &back() {
219 assert(Last != First && "Calling back() on empty vector!");
220 return *(Last - 1);
221 }
222 T &operator[](size_t Index) {
223 assert(Index < size() && "Invalid access!");
224 return *(begin() + Index);
225 }
226 void clear() { Last = First; }
227
228 ~PODSmallVector() {
229 if (!isInline())
230 std::free(First);
231 }
232};
233
Richard Smithc20d1442018-08-20 20:14:49 +0000234// Base class of all AST nodes. The AST is built by the parser, then is
235// traversed by the printLeft/Right functions to produce a demangled string.
236class Node {
237public:
238 enum Kind : unsigned char {
239#define ENUMERATOR(NodeKind) K ## NodeKind,
240 FOR_EACH_NODE_KIND(ENUMERATOR)
241#undef ENUMERATOR
242 };
243
244 /// Three-way bool to track a cached value. Unknown is possible if this node
245 /// has an unexpanded parameter pack below it that may affect this cache.
246 enum class Cache : unsigned char { Yes, No, Unknown, };
247
248private:
249 Kind K;
250
251 // FIXME: Make these protected.
252public:
253 /// Tracks if this node has a component on its right side, in which case we
254 /// need to call printRight.
255 Cache RHSComponentCache;
256
257 /// Track if this node is a (possibly qualified) array type. This can affect
258 /// how we format the output string.
259 Cache ArrayCache;
260
261 /// Track if this node is a (possibly qualified) function type. This can
262 /// affect how we format the output string.
263 Cache FunctionCache;
264
265public:
266 Node(Kind K_, Cache RHSComponentCache_ = Cache::No,
267 Cache ArrayCache_ = Cache::No, Cache FunctionCache_ = Cache::No)
268 : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_),
269 FunctionCache(FunctionCache_) {}
270
271 /// Visit the most-derived object corresponding to this object.
272 template<typename Fn> void visit(Fn F) const;
273
274 // The following function is provided by all derived classes:
275 //
276 // Call F with arguments that, when passed to the constructor of this node,
277 // would construct an equivalent node.
278 //template<typename Fn> void match(Fn F) const;
279
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700280 bool hasRHSComponent(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000281 if (RHSComponentCache != Cache::Unknown)
282 return RHSComponentCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700283 return hasRHSComponentSlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000284 }
285
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700286 bool hasArray(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000287 if (ArrayCache != Cache::Unknown)
288 return ArrayCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700289 return hasArraySlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000290 }
291
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700292 bool hasFunction(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000293 if (FunctionCache != Cache::Unknown)
294 return FunctionCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700295 return hasFunctionSlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000296 }
297
298 Kind getKind() const { return K; }
299
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700300 virtual bool hasRHSComponentSlow(OutputBuffer &) const { return false; }
301 virtual bool hasArraySlow(OutputBuffer &) const { return false; }
302 virtual bool hasFunctionSlow(OutputBuffer &) const { return false; }
Richard Smithc20d1442018-08-20 20:14:49 +0000303
304 // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
305 // get at a node that actually represents some concrete syntax.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700306 virtual const Node *getSyntaxNode(OutputBuffer &) const { return this; }
Richard Smithc20d1442018-08-20 20:14:49 +0000307
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700308 void print(OutputBuffer &OB) const {
309 printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000310 if (RHSComponentCache != Cache::No)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700311 printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000312 }
313
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -0800314 // Print the "left" side of this Node into OutputBuffer.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700315 virtual void printLeft(OutputBuffer &) const = 0;
Richard Smithc20d1442018-08-20 20:14:49 +0000316
317 // Print the "right". This distinction is necessary to represent C++ types
318 // that appear on the RHS of their subtype, such as arrays or functions.
319 // Since most types don't have such a component, provide a default
320 // implementation.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700321 virtual void printRight(OutputBuffer &) const {}
Richard Smithc20d1442018-08-20 20:14:49 +0000322
323 virtual StringView getBaseName() const { return StringView(); }
324
325 // Silence compiler warnings, this dtor will never be called.
326 virtual ~Node() = default;
327
328#ifndef NDEBUG
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000329 DEMANGLE_DUMP_METHOD void dump() const;
Richard Smithc20d1442018-08-20 20:14:49 +0000330#endif
331};
332
333class NodeArray {
334 Node **Elements;
335 size_t NumElements;
336
337public:
338 NodeArray() : Elements(nullptr), NumElements(0) {}
339 NodeArray(Node **Elements_, size_t NumElements_)
340 : Elements(Elements_), NumElements(NumElements_) {}
341
342 bool empty() const { return NumElements == 0; }
343 size_t size() const { return NumElements; }
344
345 Node **begin() const { return Elements; }
346 Node **end() const { return Elements + NumElements; }
347
348 Node *operator[](size_t Idx) const { return Elements[Idx]; }
349
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700350 void printWithComma(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000351 bool FirstElement = true;
352 for (size_t Idx = 0; Idx != NumElements; ++Idx) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700353 size_t BeforeComma = OB.getCurrentPosition();
Richard Smithc20d1442018-08-20 20:14:49 +0000354 if (!FirstElement)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700355 OB += ", ";
356 size_t AfterComma = OB.getCurrentPosition();
357 Elements[Idx]->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000358
359 // Elements[Idx] is an empty parameter pack expansion, we should erase the
360 // comma we just printed.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700361 if (AfterComma == OB.getCurrentPosition()) {
362 OB.setCurrentPosition(BeforeComma);
Richard Smithc20d1442018-08-20 20:14:49 +0000363 continue;
364 }
365
366 FirstElement = false;
367 }
368 }
369};
370
371struct NodeArrayNode : Node {
372 NodeArray Array;
373 NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
374
375 template<typename Fn> void match(Fn F) const { F(Array); }
376
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700377 void printLeft(OutputBuffer &OB) const override { Array.printWithComma(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000378};
379
380class DotSuffix final : public Node {
381 const Node *Prefix;
382 const StringView Suffix;
383
384public:
385 DotSuffix(const Node *Prefix_, StringView Suffix_)
386 : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
387
388 template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
389
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700390 void printLeft(OutputBuffer &OB) const override {
391 Prefix->print(OB);
392 OB += " (";
393 OB += Suffix;
394 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000395 }
396};
397
398class VendorExtQualType final : public Node {
399 const Node *Ty;
400 StringView Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400401 const Node *TA;
Richard Smithc20d1442018-08-20 20:14:49 +0000402
403public:
Alex Orlovf50df922021-03-24 10:21:32 +0400404 VendorExtQualType(const Node *Ty_, StringView Ext_, const Node *TA_)
405 : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_), TA(TA_) {}
Richard Smithc20d1442018-08-20 20:14:49 +0000406
Alex Orlovf50df922021-03-24 10:21:32 +0400407 template <typename Fn> void match(Fn F) const { F(Ty, Ext, TA); }
Richard Smithc20d1442018-08-20 20:14:49 +0000408
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700409 void printLeft(OutputBuffer &OB) const override {
410 Ty->print(OB);
411 OB += " ";
412 OB += Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400413 if (TA != nullptr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700414 TA->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000415 }
416};
417
418enum FunctionRefQual : unsigned char {
419 FrefQualNone,
420 FrefQualLValue,
421 FrefQualRValue,
422};
423
424enum Qualifiers {
425 QualNone = 0,
426 QualConst = 0x1,
427 QualVolatile = 0x2,
428 QualRestrict = 0x4,
429};
430
431inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
432 return Q1 = static_cast<Qualifiers>(Q1 | Q2);
433}
434
Richard Smithdf1c14c2019-09-06 23:53:21 +0000435class QualType final : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +0000436protected:
437 const Qualifiers Quals;
438 const Node *Child;
439
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700440 void printQuals(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000441 if (Quals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700442 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000443 if (Quals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700444 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000445 if (Quals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700446 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000447 }
448
449public:
450 QualType(const Node *Child_, Qualifiers Quals_)
451 : Node(KQualType, Child_->RHSComponentCache,
452 Child_->ArrayCache, Child_->FunctionCache),
453 Quals(Quals_), Child(Child_) {}
454
455 template<typename Fn> void match(Fn F) const { F(Child, Quals); }
456
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700457 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
458 return Child->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000459 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700460 bool hasArraySlow(OutputBuffer &OB) const override {
461 return Child->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000462 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700463 bool hasFunctionSlow(OutputBuffer &OB) const override {
464 return Child->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000465 }
466
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700467 void printLeft(OutputBuffer &OB) const override {
468 Child->printLeft(OB);
469 printQuals(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000470 }
471
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700472 void printRight(OutputBuffer &OB) const override { Child->printRight(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000473};
474
475class ConversionOperatorType final : public Node {
476 const Node *Ty;
477
478public:
479 ConversionOperatorType(const Node *Ty_)
480 : Node(KConversionOperatorType), Ty(Ty_) {}
481
482 template<typename Fn> void match(Fn F) const { F(Ty); }
483
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700484 void printLeft(OutputBuffer &OB) const override {
485 OB += "operator ";
486 Ty->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000487 }
488};
489
490class PostfixQualifiedType final : public Node {
491 const Node *Ty;
492 const StringView Postfix;
493
494public:
495 PostfixQualifiedType(Node *Ty_, StringView Postfix_)
496 : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
497
498 template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
499
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700500 void printLeft(OutputBuffer &OB) const override {
501 Ty->printLeft(OB);
502 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +0000503 }
504};
505
506class NameType final : public Node {
507 const StringView Name;
508
509public:
510 NameType(StringView Name_) : Node(KNameType), Name(Name_) {}
511
512 template<typename Fn> void match(Fn F) const { F(Name); }
513
514 StringView getName() const { return Name; }
515 StringView getBaseName() const override { return Name; }
516
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700517 void printLeft(OutputBuffer &OB) const override { OB += Name; }
Richard Smithc20d1442018-08-20 20:14:49 +0000518};
519
520class ElaboratedTypeSpefType : public Node {
521 StringView Kind;
522 Node *Child;
523public:
524 ElaboratedTypeSpefType(StringView Kind_, Node *Child_)
525 : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
526
527 template<typename Fn> void match(Fn F) const { F(Kind, Child); }
528
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700529 void printLeft(OutputBuffer &OB) const override {
530 OB += Kind;
531 OB += ' ';
532 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000533 }
534};
535
536struct AbiTagAttr : Node {
537 Node *Base;
538 StringView Tag;
539
540 AbiTagAttr(Node* Base_, StringView Tag_)
541 : Node(KAbiTagAttr, Base_->RHSComponentCache,
542 Base_->ArrayCache, Base_->FunctionCache),
543 Base(Base_), Tag(Tag_) {}
544
545 template<typename Fn> void match(Fn F) const { F(Base, Tag); }
546
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700547 void printLeft(OutputBuffer &OB) const override {
548 Base->printLeft(OB);
549 OB += "[abi:";
550 OB += Tag;
551 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +0000552 }
553};
554
555class EnableIfAttr : public Node {
556 NodeArray Conditions;
557public:
558 EnableIfAttr(NodeArray Conditions_)
559 : Node(KEnableIfAttr), Conditions(Conditions_) {}
560
561 template<typename Fn> void match(Fn F) const { F(Conditions); }
562
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700563 void printLeft(OutputBuffer &OB) const override {
564 OB += " [enable_if:";
565 Conditions.printWithComma(OB);
566 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +0000567 }
568};
569
570class ObjCProtoName : public Node {
571 const Node *Ty;
572 StringView Protocol;
573
574 friend class PointerType;
575
576public:
577 ObjCProtoName(const Node *Ty_, StringView Protocol_)
578 : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
579
580 template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
581
582 bool isObjCObject() const {
583 return Ty->getKind() == KNameType &&
584 static_cast<const NameType *>(Ty)->getName() == "objc_object";
585 }
586
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700587 void printLeft(OutputBuffer &OB) const override {
588 Ty->print(OB);
589 OB += "<";
590 OB += Protocol;
591 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +0000592 }
593};
594
595class PointerType final : public Node {
596 const Node *Pointee;
597
598public:
599 PointerType(const Node *Pointee_)
600 : Node(KPointerType, Pointee_->RHSComponentCache),
601 Pointee(Pointee_) {}
602
603 template<typename Fn> void match(Fn F) const { F(Pointee); }
604
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700605 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
606 return Pointee->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000607 }
608
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700609 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000610 // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
611 if (Pointee->getKind() != KObjCProtoName ||
612 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700613 Pointee->printLeft(OB);
614 if (Pointee->hasArray(OB))
615 OB += " ";
616 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
617 OB += "(";
618 OB += "*";
Richard Smithc20d1442018-08-20 20:14:49 +0000619 } else {
620 const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700621 OB += "id<";
622 OB += objcProto->Protocol;
623 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +0000624 }
625 }
626
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700627 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000628 if (Pointee->getKind() != KObjCProtoName ||
629 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700630 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
631 OB += ")";
632 Pointee->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000633 }
634 }
635};
636
637enum class ReferenceKind {
638 LValue,
639 RValue,
640};
641
642// Represents either a LValue or an RValue reference type.
643class ReferenceType : public Node {
644 const Node *Pointee;
645 ReferenceKind RK;
646
647 mutable bool Printing = false;
648
649 // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
650 // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
651 // other combination collapses to a lvalue ref.
Mikhail Borisov05f77222021-08-17 18:10:57 -0400652 //
653 // A combination of a TemplateForwardReference and a back-ref Substitution
654 // from an ill-formed string may have created a cycle; use cycle detection to
655 // avoid looping forever.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700656 std::pair<ReferenceKind, const Node *> collapse(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000657 auto SoFar = std::make_pair(RK, Pointee);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400658 // Track the chain of nodes for the Floyd's 'tortoise and hare'
659 // cycle-detection algorithm, since getSyntaxNode(S) is impure
660 PODSmallVector<const Node *, 8> Prev;
Richard Smithc20d1442018-08-20 20:14:49 +0000661 for (;;) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700662 const Node *SN = SoFar.second->getSyntaxNode(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000663 if (SN->getKind() != KReferenceType)
664 break;
665 auto *RT = static_cast<const ReferenceType *>(SN);
666 SoFar.second = RT->Pointee;
667 SoFar.first = std::min(SoFar.first, RT->RK);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400668
669 // The middle of Prev is the 'slow' pointer moving at half speed
670 Prev.push_back(SoFar.second);
671 if (Prev.size() > 1 && SoFar.second == Prev[(Prev.size() - 1) / 2]) {
672 // Cycle detected
673 SoFar.second = nullptr;
674 break;
675 }
Richard Smithc20d1442018-08-20 20:14:49 +0000676 }
677 return SoFar;
678 }
679
680public:
681 ReferenceType(const Node *Pointee_, ReferenceKind RK_)
682 : Node(KReferenceType, Pointee_->RHSComponentCache),
683 Pointee(Pointee_), RK(RK_) {}
684
685 template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
686
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700687 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
688 return Pointee->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000689 }
690
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700691 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000692 if (Printing)
693 return;
694 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700695 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400696 if (!Collapsed.second)
697 return;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700698 Collapsed.second->printLeft(OB);
699 if (Collapsed.second->hasArray(OB))
700 OB += " ";
701 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
702 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +0000703
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700704 OB += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
Richard Smithc20d1442018-08-20 20:14:49 +0000705 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700706 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000707 if (Printing)
708 return;
709 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700710 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400711 if (!Collapsed.second)
712 return;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700713 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
714 OB += ")";
715 Collapsed.second->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000716 }
717};
718
719class PointerToMemberType final : public Node {
720 const Node *ClassType;
721 const Node *MemberType;
722
723public:
724 PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
725 : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
726 ClassType(ClassType_), MemberType(MemberType_) {}
727
728 template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
729
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700730 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
731 return MemberType->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000732 }
733
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700734 void printLeft(OutputBuffer &OB) const override {
735 MemberType->printLeft(OB);
736 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
737 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +0000738 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700739 OB += " ";
740 ClassType->print(OB);
741 OB += "::*";
Richard Smithc20d1442018-08-20 20:14:49 +0000742 }
743
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700744 void printRight(OutputBuffer &OB) const override {
745 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
746 OB += ")";
747 MemberType->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000748 }
749};
750
Richard Smithc20d1442018-08-20 20:14:49 +0000751class ArrayType final : public Node {
752 const Node *Base;
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800753 Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +0000754
755public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800756 ArrayType(const Node *Base_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +0000757 : Node(KArrayType,
758 /*RHSComponentCache=*/Cache::Yes,
759 /*ArrayCache=*/Cache::Yes),
760 Base(Base_), Dimension(Dimension_) {}
761
762 template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
763
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700764 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
765 bool hasArraySlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000766
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700767 void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000768
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700769 void printRight(OutputBuffer &OB) const override {
770 if (OB.back() != ']')
771 OB += " ";
772 OB += "[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800773 if (Dimension)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700774 Dimension->print(OB);
775 OB += "]";
776 Base->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000777 }
778};
779
780class FunctionType final : public Node {
781 const Node *Ret;
782 NodeArray Params;
783 Qualifiers CVQuals;
784 FunctionRefQual RefQual;
785 const Node *ExceptionSpec;
786
787public:
788 FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
789 FunctionRefQual RefQual_, const Node *ExceptionSpec_)
790 : Node(KFunctionType,
791 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
792 /*FunctionCache=*/Cache::Yes),
793 Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
794 ExceptionSpec(ExceptionSpec_) {}
795
796 template<typename Fn> void match(Fn F) const {
797 F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
798 }
799
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700800 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
801 bool hasFunctionSlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000802
803 // Handle C++'s ... quirky decl grammar by using the left & right
804 // distinction. Consider:
805 // int (*f(float))(char) {}
806 // f is a function that takes a float and returns a pointer to a function
807 // that takes a char and returns an int. If we're trying to print f, start
808 // by printing out the return types's left, then print our parameters, then
809 // finally print right of the return type.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700810 void printLeft(OutputBuffer &OB) const override {
811 Ret->printLeft(OB);
812 OB += " ";
Richard Smithc20d1442018-08-20 20:14:49 +0000813 }
814
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700815 void printRight(OutputBuffer &OB) const override {
816 OB += "(";
817 Params.printWithComma(OB);
818 OB += ")";
819 Ret->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000820
821 if (CVQuals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700822 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000823 if (CVQuals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700824 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000825 if (CVQuals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700826 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000827
828 if (RefQual == FrefQualLValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700829 OB += " &";
Richard Smithc20d1442018-08-20 20:14:49 +0000830 else if (RefQual == FrefQualRValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700831 OB += " &&";
Richard Smithc20d1442018-08-20 20:14:49 +0000832
833 if (ExceptionSpec != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700834 OB += ' ';
835 ExceptionSpec->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000836 }
837 }
838};
839
840class NoexceptSpec : public Node {
841 const Node *E;
842public:
843 NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
844
845 template<typename Fn> void match(Fn F) const { F(E); }
846
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700847 void printLeft(OutputBuffer &OB) const override {
848 OB += "noexcept(";
849 E->print(OB);
850 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000851 }
852};
853
854class DynamicExceptionSpec : public Node {
855 NodeArray Types;
856public:
857 DynamicExceptionSpec(NodeArray Types_)
858 : Node(KDynamicExceptionSpec), Types(Types_) {}
859
860 template<typename Fn> void match(Fn F) const { F(Types); }
861
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700862 void printLeft(OutputBuffer &OB) const override {
863 OB += "throw(";
864 Types.printWithComma(OB);
865 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +0000866 }
867};
868
869class FunctionEncoding final : public Node {
870 const Node *Ret;
871 const Node *Name;
872 NodeArray Params;
873 const Node *Attrs;
874 Qualifiers CVQuals;
875 FunctionRefQual RefQual;
876
877public:
878 FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
879 const Node *Attrs_, Qualifiers CVQuals_,
880 FunctionRefQual RefQual_)
881 : Node(KFunctionEncoding,
882 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
883 /*FunctionCache=*/Cache::Yes),
884 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
885 CVQuals(CVQuals_), RefQual(RefQual_) {}
886
887 template<typename Fn> void match(Fn F) const {
888 F(Ret, Name, Params, Attrs, CVQuals, RefQual);
889 }
890
891 Qualifiers getCVQuals() const { return CVQuals; }
892 FunctionRefQual getRefQual() const { return RefQual; }
893 NodeArray getParams() const { return Params; }
894 const Node *getReturnType() const { return Ret; }
895
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700896 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
897 bool hasFunctionSlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000898
899 const Node *getName() const { return Name; }
900
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700901 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000902 if (Ret) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700903 Ret->printLeft(OB);
904 if (!Ret->hasRHSComponent(OB))
905 OB += " ";
Richard Smithc20d1442018-08-20 20:14:49 +0000906 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700907 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000908 }
909
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700910 void printRight(OutputBuffer &OB) const override {
911 OB += "(";
912 Params.printWithComma(OB);
913 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000914 if (Ret)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700915 Ret->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000916
917 if (CVQuals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700918 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000919 if (CVQuals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700920 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000921 if (CVQuals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700922 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000923
924 if (RefQual == FrefQualLValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700925 OB += " &";
Richard Smithc20d1442018-08-20 20:14:49 +0000926 else if (RefQual == FrefQualRValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700927 OB += " &&";
Richard Smithc20d1442018-08-20 20:14:49 +0000928
929 if (Attrs != nullptr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700930 Attrs->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000931 }
932};
933
934class LiteralOperator : public Node {
935 const Node *OpName;
936
937public:
938 LiteralOperator(const Node *OpName_)
939 : Node(KLiteralOperator), OpName(OpName_) {}
940
941 template<typename Fn> void match(Fn F) const { F(OpName); }
942
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700943 void printLeft(OutputBuffer &OB) const override {
944 OB += "operator\"\" ";
945 OpName->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000946 }
947};
948
949class SpecialName final : public Node {
950 const StringView Special;
951 const Node *Child;
952
953public:
954 SpecialName(StringView Special_, const Node *Child_)
955 : Node(KSpecialName), Special(Special_), Child(Child_) {}
956
957 template<typename Fn> void match(Fn F) const { F(Special, Child); }
958
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700959 void printLeft(OutputBuffer &OB) const override {
960 OB += Special;
961 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000962 }
963};
964
965class CtorVtableSpecialName final : public Node {
966 const Node *FirstType;
967 const Node *SecondType;
968
969public:
970 CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
971 : Node(KCtorVtableSpecialName),
972 FirstType(FirstType_), SecondType(SecondType_) {}
973
974 template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
975
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700976 void printLeft(OutputBuffer &OB) const override {
977 OB += "construction vtable for ";
978 FirstType->print(OB);
979 OB += "-in-";
980 SecondType->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000981 }
982};
983
984struct NestedName : Node {
985 Node *Qual;
986 Node *Name;
987
988 NestedName(Node *Qual_, Node *Name_)
989 : Node(KNestedName), Qual(Qual_), Name(Name_) {}
990
991 template<typename Fn> void match(Fn F) const { F(Qual, Name); }
992
993 StringView getBaseName() const override { return Name->getBaseName(); }
994
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700995 void printLeft(OutputBuffer &OB) const override {
996 Qual->print(OB);
997 OB += "::";
998 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000999 }
1000};
1001
1002struct LocalName : Node {
1003 Node *Encoding;
1004 Node *Entity;
1005
1006 LocalName(Node *Encoding_, Node *Entity_)
1007 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
1008
1009 template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
1010
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001011 void printLeft(OutputBuffer &OB) const override {
1012 Encoding->print(OB);
1013 OB += "::";
1014 Entity->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001015 }
1016};
1017
1018class QualifiedName final : public Node {
1019 // qualifier::name
1020 const Node *Qualifier;
1021 const Node *Name;
1022
1023public:
1024 QualifiedName(const Node *Qualifier_, const Node *Name_)
1025 : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
1026
1027 template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
1028
1029 StringView getBaseName() const override { return Name->getBaseName(); }
1030
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001031 void printLeft(OutputBuffer &OB) const override {
1032 Qualifier->print(OB);
1033 OB += "::";
1034 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001035 }
1036};
1037
1038class VectorType final : public Node {
1039 const Node *BaseType;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001040 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001041
1042public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001043 VectorType(const Node *BaseType_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001044 : Node(KVectorType), BaseType(BaseType_),
1045 Dimension(Dimension_) {}
1046
1047 template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
1048
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001049 void printLeft(OutputBuffer &OB) const override {
1050 BaseType->print(OB);
1051 OB += " vector[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001052 if (Dimension)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001053 Dimension->print(OB);
1054 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001055 }
1056};
1057
1058class PixelVectorType final : public Node {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001059 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001060
1061public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001062 PixelVectorType(const Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001063 : Node(KPixelVectorType), Dimension(Dimension_) {}
1064
1065 template<typename Fn> void match(Fn F) const { F(Dimension); }
1066
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001067 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001068 // FIXME: This should demangle as "vector pixel".
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001069 OB += "pixel vector[";
1070 Dimension->print(OB);
1071 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001072 }
1073};
1074
Pengfei Wang50e90b82021-09-23 11:02:25 +08001075class BinaryFPType final : public Node {
1076 const Node *Dimension;
1077
1078public:
1079 BinaryFPType(const Node *Dimension_)
1080 : Node(KBinaryFPType), Dimension(Dimension_) {}
1081
1082 template<typename Fn> void match(Fn F) const { F(Dimension); }
1083
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001084 void printLeft(OutputBuffer &OB) const override {
1085 OB += "_Float";
1086 Dimension->print(OB);
Pengfei Wang50e90b82021-09-23 11:02:25 +08001087 }
1088};
1089
Richard Smithdf1c14c2019-09-06 23:53:21 +00001090enum class TemplateParamKind { Type, NonType, Template };
1091
1092/// An invented name for a template parameter for which we don't have a
1093/// corresponding template argument.
1094///
1095/// This node is created when parsing the <lambda-sig> for a lambda with
1096/// explicit template arguments, which might be referenced in the parameter
1097/// types appearing later in the <lambda-sig>.
1098class SyntheticTemplateParamName final : public Node {
1099 TemplateParamKind Kind;
1100 unsigned Index;
1101
1102public:
1103 SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_)
1104 : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {}
1105
1106 template<typename Fn> void match(Fn F) const { F(Kind, Index); }
1107
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001108 void printLeft(OutputBuffer &OB) const override {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001109 switch (Kind) {
1110 case TemplateParamKind::Type:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001111 OB += "$T";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001112 break;
1113 case TemplateParamKind::NonType:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001114 OB += "$N";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001115 break;
1116 case TemplateParamKind::Template:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001117 OB += "$TT";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001118 break;
1119 }
1120 if (Index > 0)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001121 OB << Index - 1;
Richard Smithdf1c14c2019-09-06 23:53:21 +00001122 }
1123};
1124
1125/// A template type parameter declaration, 'typename T'.
1126class TypeTemplateParamDecl final : public Node {
1127 Node *Name;
1128
1129public:
1130 TypeTemplateParamDecl(Node *Name_)
1131 : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {}
1132
1133 template<typename Fn> void match(Fn F) const { F(Name); }
1134
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001135 void printLeft(OutputBuffer &OB) const override { OB += "typename "; }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001136
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001137 void printRight(OutputBuffer &OB) const override { Name->print(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001138};
1139
1140/// A non-type template parameter declaration, 'int N'.
1141class NonTypeTemplateParamDecl final : public Node {
1142 Node *Name;
1143 Node *Type;
1144
1145public:
1146 NonTypeTemplateParamDecl(Node *Name_, Node *Type_)
1147 : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {}
1148
1149 template<typename Fn> void match(Fn F) const { F(Name, Type); }
1150
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001151 void printLeft(OutputBuffer &OB) const override {
1152 Type->printLeft(OB);
1153 if (!Type->hasRHSComponent(OB))
1154 OB += " ";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001155 }
1156
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001157 void printRight(OutputBuffer &OB) const override {
1158 Name->print(OB);
1159 Type->printRight(OB);
Richard Smithdf1c14c2019-09-06 23:53:21 +00001160 }
1161};
1162
1163/// A template template parameter declaration,
1164/// 'template<typename T> typename N'.
1165class TemplateTemplateParamDecl final : public Node {
1166 Node *Name;
1167 NodeArray Params;
1168
1169public:
1170 TemplateTemplateParamDecl(Node *Name_, NodeArray Params_)
1171 : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_),
1172 Params(Params_) {}
1173
1174 template<typename Fn> void match(Fn F) const { F(Name, Params); }
1175
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001176 void printLeft(OutputBuffer &OB) const override {
1177 OB += "template<";
1178 Params.printWithComma(OB);
1179 OB += "> typename ";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001180 }
1181
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001182 void printRight(OutputBuffer &OB) const override { Name->print(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001183};
1184
1185/// A template parameter pack declaration, 'typename ...T'.
1186class TemplateParamPackDecl final : public Node {
1187 Node *Param;
1188
1189public:
1190 TemplateParamPackDecl(Node *Param_)
1191 : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {}
1192
1193 template<typename Fn> void match(Fn F) const { F(Param); }
1194
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001195 void printLeft(OutputBuffer &OB) const override {
1196 Param->printLeft(OB);
1197 OB += "...";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001198 }
1199
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001200 void printRight(OutputBuffer &OB) const override { Param->printRight(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001201};
1202
Richard Smithc20d1442018-08-20 20:14:49 +00001203/// An unexpanded parameter pack (either in the expression or type context). If
1204/// this AST is correct, this node will have a ParameterPackExpansion node above
1205/// it.
1206///
1207/// This node is created when some <template-args> are found that apply to an
1208/// <encoding>, and is stored in the TemplateParams table. In order for this to
1209/// appear in the final AST, it has to referenced via a <template-param> (ie,
1210/// T_).
1211class ParameterPack final : public Node {
1212 NodeArray Data;
1213
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08001214 // Setup OutputBuffer for a pack expansion, unless we're already expanding
1215 // one.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001216 void initializePackExpansion(OutputBuffer &OB) const {
1217 if (OB.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
1218 OB.CurrentPackMax = static_cast<unsigned>(Data.size());
1219 OB.CurrentPackIndex = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00001220 }
1221 }
1222
1223public:
1224 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
1225 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
1226 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1227 return P->ArrayCache == Cache::No;
1228 }))
1229 ArrayCache = Cache::No;
1230 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1231 return P->FunctionCache == Cache::No;
1232 }))
1233 FunctionCache = Cache::No;
1234 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1235 return P->RHSComponentCache == Cache::No;
1236 }))
1237 RHSComponentCache = Cache::No;
1238 }
1239
1240 template<typename Fn> void match(Fn F) const { F(Data); }
1241
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001242 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
1243 initializePackExpansion(OB);
1244 size_t Idx = OB.CurrentPackIndex;
1245 return Idx < Data.size() && Data[Idx]->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001246 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001247 bool hasArraySlow(OutputBuffer &OB) const override {
1248 initializePackExpansion(OB);
1249 size_t Idx = OB.CurrentPackIndex;
1250 return Idx < Data.size() && Data[Idx]->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001251 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001252 bool hasFunctionSlow(OutputBuffer &OB) const override {
1253 initializePackExpansion(OB);
1254 size_t Idx = OB.CurrentPackIndex;
1255 return Idx < Data.size() && Data[Idx]->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001256 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001257 const Node *getSyntaxNode(OutputBuffer &OB) const override {
1258 initializePackExpansion(OB);
1259 size_t Idx = OB.CurrentPackIndex;
1260 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(OB) : this;
Richard Smithc20d1442018-08-20 20:14:49 +00001261 }
1262
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001263 void printLeft(OutputBuffer &OB) const override {
1264 initializePackExpansion(OB);
1265 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001266 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001267 Data[Idx]->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001268 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001269 void printRight(OutputBuffer &OB) const override {
1270 initializePackExpansion(OB);
1271 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001272 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001273 Data[Idx]->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001274 }
1275};
1276
1277/// A variadic template argument. This node represents an occurrence of
1278/// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1279/// one of it's Elements is. The parser inserts a ParameterPack into the
1280/// TemplateParams table if the <template-args> this pack belongs to apply to an
1281/// <encoding>.
1282class TemplateArgumentPack final : public Node {
1283 NodeArray Elements;
1284public:
1285 TemplateArgumentPack(NodeArray Elements_)
1286 : Node(KTemplateArgumentPack), Elements(Elements_) {}
1287
1288 template<typename Fn> void match(Fn F) const { F(Elements); }
1289
1290 NodeArray getElements() const { return Elements; }
1291
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001292 void printLeft(OutputBuffer &OB) const override {
1293 Elements.printWithComma(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001294 }
1295};
1296
1297/// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1298/// which each have Child->ParameterPackSize elements.
1299class ParameterPackExpansion final : public Node {
1300 const Node *Child;
1301
1302public:
1303 ParameterPackExpansion(const Node *Child_)
1304 : Node(KParameterPackExpansion), Child(Child_) {}
1305
1306 template<typename Fn> void match(Fn F) const { F(Child); }
1307
1308 const Node *getChild() const { return Child; }
1309
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001310 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001311 constexpr unsigned Max = std::numeric_limits<unsigned>::max();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001312 SwapAndRestore<unsigned> SavePackIdx(OB.CurrentPackIndex, Max);
1313 SwapAndRestore<unsigned> SavePackMax(OB.CurrentPackMax, Max);
1314 size_t StreamPos = OB.getCurrentPosition();
Richard Smithc20d1442018-08-20 20:14:49 +00001315
1316 // Print the first element in the pack. If Child contains a ParameterPack,
1317 // it will set up S.CurrentPackMax and print the first element.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001318 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001319
1320 // No ParameterPack was found in Child. This can occur if we've found a pack
1321 // expansion on a <function-param>.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001322 if (OB.CurrentPackMax == Max) {
1323 OB += "...";
Richard Smithc20d1442018-08-20 20:14:49 +00001324 return;
1325 }
1326
1327 // We found a ParameterPack, but it has no elements. Erase whatever we may
1328 // of printed.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001329 if (OB.CurrentPackMax == 0) {
1330 OB.setCurrentPosition(StreamPos);
Richard Smithc20d1442018-08-20 20:14:49 +00001331 return;
1332 }
1333
1334 // Else, iterate through the rest of the elements in the pack.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001335 for (unsigned I = 1, E = OB.CurrentPackMax; I < E; ++I) {
1336 OB += ", ";
1337 OB.CurrentPackIndex = I;
1338 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001339 }
1340 }
1341};
1342
1343class TemplateArgs final : public Node {
1344 NodeArray Params;
1345
1346public:
1347 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1348
1349 template<typename Fn> void match(Fn F) const { F(Params); }
1350
1351 NodeArray getParams() { return Params; }
1352
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001353 void printLeft(OutputBuffer &OB) const override {
1354 OB += "<";
1355 Params.printWithComma(OB);
1356 if (OB.back() == '>')
1357 OB += " ";
1358 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +00001359 }
1360};
1361
Richard Smithb485b352018-08-24 23:30:26 +00001362/// A forward-reference to a template argument that was not known at the point
1363/// where the template parameter name was parsed in a mangling.
1364///
1365/// This is created when demangling the name of a specialization of a
1366/// conversion function template:
1367///
1368/// \code
1369/// struct A {
1370/// template<typename T> operator T*();
1371/// };
1372/// \endcode
1373///
1374/// When demangling a specialization of the conversion function template, we
1375/// encounter the name of the template (including the \c T) before we reach
1376/// the template argument list, so we cannot substitute the parameter name
1377/// for the corresponding argument while parsing. Instead, we create a
1378/// \c ForwardTemplateReference node that is resolved after we parse the
1379/// template arguments.
Richard Smithc20d1442018-08-20 20:14:49 +00001380struct ForwardTemplateReference : Node {
1381 size_t Index;
1382 Node *Ref = nullptr;
1383
1384 // If we're currently printing this node. It is possible (though invalid) for
1385 // a forward template reference to refer to itself via a substitution. This
1386 // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1387 // out if more than one print* function is active.
1388 mutable bool Printing = false;
1389
1390 ForwardTemplateReference(size_t Index_)
1391 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1392 Cache::Unknown),
1393 Index(Index_) {}
1394
1395 // We don't provide a matcher for these, because the value of the node is
1396 // not determined by its construction parameters, and it generally needs
1397 // special handling.
1398 template<typename Fn> void match(Fn F) const = delete;
1399
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001400 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001401 if (Printing)
1402 return false;
1403 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001404 return Ref->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001405 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001406 bool hasArraySlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001407 if (Printing)
1408 return false;
1409 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001410 return Ref->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001411 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001412 bool hasFunctionSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001413 if (Printing)
1414 return false;
1415 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001416 return Ref->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001417 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001418 const Node *getSyntaxNode(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001419 if (Printing)
1420 return this;
1421 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001422 return Ref->getSyntaxNode(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001423 }
1424
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001425 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001426 if (Printing)
1427 return;
1428 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001429 Ref->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001430 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001431 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001432 if (Printing)
1433 return;
1434 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001435 Ref->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001436 }
1437};
1438
1439struct NameWithTemplateArgs : Node {
1440 // name<template_args>
1441 Node *Name;
1442 Node *TemplateArgs;
1443
1444 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1445 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1446
1447 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1448
1449 StringView getBaseName() const override { return Name->getBaseName(); }
1450
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001451 void printLeft(OutputBuffer &OB) const override {
1452 Name->print(OB);
1453 TemplateArgs->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001454 }
1455};
1456
1457class GlobalQualifiedName final : public Node {
1458 Node *Child;
1459
1460public:
1461 GlobalQualifiedName(Node* Child_)
1462 : Node(KGlobalQualifiedName), Child(Child_) {}
1463
1464 template<typename Fn> void match(Fn F) const { F(Child); }
1465
1466 StringView getBaseName() const override { return Child->getBaseName(); }
1467
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001468 void printLeft(OutputBuffer &OB) const override {
1469 OB += "::";
1470 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001471 }
1472};
1473
1474struct StdQualifiedName : Node {
1475 Node *Child;
1476
1477 StdQualifiedName(Node *Child_) : Node(KStdQualifiedName), Child(Child_) {}
1478
1479 template<typename Fn> void match(Fn F) const { F(Child); }
1480
1481 StringView getBaseName() const override { return Child->getBaseName(); }
1482
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001483 void printLeft(OutputBuffer &OB) const override {
1484 OB += "std::";
1485 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001486 }
1487};
1488
1489enum class SpecialSubKind {
1490 allocator,
1491 basic_string,
1492 string,
1493 istream,
1494 ostream,
1495 iostream,
1496};
1497
1498class ExpandedSpecialSubstitution final : public Node {
1499 SpecialSubKind SSK;
1500
1501public:
1502 ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1503 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1504
1505 template<typename Fn> void match(Fn F) const { F(SSK); }
1506
1507 StringView getBaseName() const override {
1508 switch (SSK) {
1509 case SpecialSubKind::allocator:
1510 return StringView("allocator");
1511 case SpecialSubKind::basic_string:
1512 return StringView("basic_string");
1513 case SpecialSubKind::string:
1514 return StringView("basic_string");
1515 case SpecialSubKind::istream:
1516 return StringView("basic_istream");
1517 case SpecialSubKind::ostream:
1518 return StringView("basic_ostream");
1519 case SpecialSubKind::iostream:
1520 return StringView("basic_iostream");
1521 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001522 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001523 }
1524
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001525 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001526 switch (SSK) {
1527 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001528 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001529 break;
1530 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001531 OB += "std::basic_string";
Richard Smithb485b352018-08-24 23:30:26 +00001532 break;
Richard Smithc20d1442018-08-20 20:14:49 +00001533 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001534 OB += "std::basic_string<char, std::char_traits<char>, "
1535 "std::allocator<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001536 break;
1537 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001538 OB += "std::basic_istream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001539 break;
1540 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001541 OB += "std::basic_ostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001542 break;
1543 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001544 OB += "std::basic_iostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001545 break;
1546 }
1547 }
1548};
1549
1550class SpecialSubstitution final : public Node {
1551public:
1552 SpecialSubKind SSK;
1553
1554 SpecialSubstitution(SpecialSubKind SSK_)
1555 : Node(KSpecialSubstitution), SSK(SSK_) {}
1556
1557 template<typename Fn> void match(Fn F) const { F(SSK); }
1558
1559 StringView getBaseName() const override {
1560 switch (SSK) {
1561 case SpecialSubKind::allocator:
1562 return StringView("allocator");
1563 case SpecialSubKind::basic_string:
1564 return StringView("basic_string");
1565 case SpecialSubKind::string:
1566 return StringView("string");
1567 case SpecialSubKind::istream:
1568 return StringView("istream");
1569 case SpecialSubKind::ostream:
1570 return StringView("ostream");
1571 case SpecialSubKind::iostream:
1572 return StringView("iostream");
1573 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001574 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001575 }
1576
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001577 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001578 switch (SSK) {
1579 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001580 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001581 break;
1582 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001583 OB += "std::basic_string";
Richard Smithc20d1442018-08-20 20:14:49 +00001584 break;
1585 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001586 OB += "std::string";
Richard Smithc20d1442018-08-20 20:14:49 +00001587 break;
1588 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001589 OB += "std::istream";
Richard Smithc20d1442018-08-20 20:14:49 +00001590 break;
1591 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001592 OB += "std::ostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001593 break;
1594 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001595 OB += "std::iostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001596 break;
1597 }
1598 }
1599};
1600
1601class CtorDtorName final : public Node {
1602 const Node *Basename;
1603 const bool IsDtor;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001604 const int Variant;
Richard Smithc20d1442018-08-20 20:14:49 +00001605
1606public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001607 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1608 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1609 Variant(Variant_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001610
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001611 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
Richard Smithc20d1442018-08-20 20:14:49 +00001612
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001613 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001614 if (IsDtor)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001615 OB += "~";
1616 OB += Basename->getBaseName();
Richard Smithc20d1442018-08-20 20:14:49 +00001617 }
1618};
1619
1620class DtorName : public Node {
1621 const Node *Base;
1622
1623public:
1624 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1625
1626 template<typename Fn> void match(Fn F) const { F(Base); }
1627
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001628 void printLeft(OutputBuffer &OB) const override {
1629 OB += "~";
1630 Base->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001631 }
1632};
1633
1634class UnnamedTypeName : public Node {
1635 const StringView Count;
1636
1637public:
1638 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1639
1640 template<typename Fn> void match(Fn F) const { F(Count); }
1641
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001642 void printLeft(OutputBuffer &OB) const override {
1643 OB += "'unnamed";
1644 OB += Count;
1645 OB += "\'";
Richard Smithc20d1442018-08-20 20:14:49 +00001646 }
1647};
1648
1649class ClosureTypeName : public Node {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001650 NodeArray TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00001651 NodeArray Params;
1652 StringView Count;
1653
1654public:
Richard Smithdf1c14c2019-09-06 23:53:21 +00001655 ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_,
1656 StringView Count_)
1657 : Node(KClosureTypeName), TemplateParams(TemplateParams_),
1658 Params(Params_), Count(Count_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001659
Richard Smithdf1c14c2019-09-06 23:53:21 +00001660 template<typename Fn> void match(Fn F) const {
1661 F(TemplateParams, Params, Count);
1662 }
1663
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001664 void printDeclarator(OutputBuffer &OB) const {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001665 if (!TemplateParams.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001666 OB += "<";
1667 TemplateParams.printWithComma(OB);
1668 OB += ">";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001669 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001670 OB += "(";
1671 Params.printWithComma(OB);
1672 OB += ")";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001673 }
Richard Smithc20d1442018-08-20 20:14:49 +00001674
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001675 void printLeft(OutputBuffer &OB) const override {
1676 OB += "\'lambda";
1677 OB += Count;
1678 OB += "\'";
1679 printDeclarator(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001680 }
1681};
1682
1683class StructuredBindingName : public Node {
1684 NodeArray Bindings;
1685public:
1686 StructuredBindingName(NodeArray Bindings_)
1687 : Node(KStructuredBindingName), Bindings(Bindings_) {}
1688
1689 template<typename Fn> void match(Fn F) const { F(Bindings); }
1690
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001691 void printLeft(OutputBuffer &OB) const override {
1692 OB += '[';
1693 Bindings.printWithComma(OB);
1694 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00001695 }
1696};
1697
1698// -- Expression Nodes --
1699
1700class BinaryExpr : public Node {
1701 const Node *LHS;
1702 const StringView InfixOperator;
1703 const Node *RHS;
1704
1705public:
1706 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
1707 : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
1708 }
1709
1710 template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
1711
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001712 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001713 // might be a template argument expression, then we need to disambiguate
1714 // with parens.
1715 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001716 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +00001717
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001718 OB += "(";
1719 LHS->print(OB);
1720 OB += ") ";
1721 OB += InfixOperator;
1722 OB += " (";
1723 RHS->print(OB);
1724 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001725
1726 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001727 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001728 }
1729};
1730
1731class ArraySubscriptExpr : public Node {
1732 const Node *Op1;
1733 const Node *Op2;
1734
1735public:
1736 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
1737 : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
1738
1739 template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
1740
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001741 void printLeft(OutputBuffer &OB) const override {
1742 OB += "(";
1743 Op1->print(OB);
1744 OB += ")[";
1745 Op2->print(OB);
1746 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001747 }
1748};
1749
1750class PostfixExpr : public Node {
1751 const Node *Child;
1752 const StringView Operator;
1753
1754public:
1755 PostfixExpr(const Node *Child_, StringView Operator_)
1756 : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
1757
1758 template<typename Fn> void match(Fn F) const { F(Child, Operator); }
1759
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001760 void printLeft(OutputBuffer &OB) const override {
1761 OB += "(";
1762 Child->print(OB);
1763 OB += ")";
1764 OB += Operator;
Richard Smithc20d1442018-08-20 20:14:49 +00001765 }
1766};
1767
1768class ConditionalExpr : public Node {
1769 const Node *Cond;
1770 const Node *Then;
1771 const Node *Else;
1772
1773public:
1774 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
1775 : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
1776
1777 template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
1778
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001779 void printLeft(OutputBuffer &OB) const override {
1780 OB += "(";
1781 Cond->print(OB);
1782 OB += ") ? (";
1783 Then->print(OB);
1784 OB += ") : (";
1785 Else->print(OB);
1786 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001787 }
1788};
1789
1790class MemberExpr : public Node {
1791 const Node *LHS;
1792 const StringView Kind;
1793 const Node *RHS;
1794
1795public:
1796 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
1797 : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1798
1799 template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
1800
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001801 void printLeft(OutputBuffer &OB) const override {
1802 LHS->print(OB);
1803 OB += Kind;
1804 RHS->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001805 }
1806};
1807
Richard Smith1865d2f2020-10-22 19:29:36 -07001808class SubobjectExpr : public Node {
1809 const Node *Type;
1810 const Node *SubExpr;
1811 StringView Offset;
1812 NodeArray UnionSelectors;
1813 bool OnePastTheEnd;
1814
1815public:
1816 SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_,
1817 NodeArray UnionSelectors_, bool OnePastTheEnd_)
1818 : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_),
1819 UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {}
1820
1821 template<typename Fn> void match(Fn F) const {
1822 F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd);
1823 }
1824
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001825 void printLeft(OutputBuffer &OB) const override {
1826 SubExpr->print(OB);
1827 OB += ".<";
1828 Type->print(OB);
1829 OB += " at offset ";
Richard Smith1865d2f2020-10-22 19:29:36 -07001830 if (Offset.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001831 OB += "0";
Richard Smith1865d2f2020-10-22 19:29:36 -07001832 } else if (Offset[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001833 OB += "-";
1834 OB += Offset.dropFront();
Richard Smith1865d2f2020-10-22 19:29:36 -07001835 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001836 OB += Offset;
Richard Smith1865d2f2020-10-22 19:29:36 -07001837 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001838 OB += ">";
Richard Smith1865d2f2020-10-22 19:29:36 -07001839 }
1840};
1841
Richard Smithc20d1442018-08-20 20:14:49 +00001842class EnclosingExpr : public Node {
1843 const StringView Prefix;
1844 const Node *Infix;
1845 const StringView Postfix;
1846
1847public:
1848 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1849 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1850 Postfix(Postfix_) {}
1851
1852 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1853
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001854 void printLeft(OutputBuffer &OB) const override {
1855 OB += Prefix;
1856 Infix->print(OB);
1857 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +00001858 }
1859};
1860
1861class CastExpr : public Node {
1862 // cast_kind<to>(from)
1863 const StringView CastKind;
1864 const Node *To;
1865 const Node *From;
1866
1867public:
1868 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1869 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1870
1871 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1872
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001873 void printLeft(OutputBuffer &OB) const override {
1874 OB += CastKind;
1875 OB += "<";
1876 To->printLeft(OB);
1877 OB += ">(";
1878 From->printLeft(OB);
1879 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001880 }
1881};
1882
1883class SizeofParamPackExpr : public Node {
1884 const Node *Pack;
1885
1886public:
1887 SizeofParamPackExpr(const Node *Pack_)
1888 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1889
1890 template<typename Fn> void match(Fn F) const { F(Pack); }
1891
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001892 void printLeft(OutputBuffer &OB) const override {
1893 OB += "sizeof...(";
Richard Smithc20d1442018-08-20 20:14:49 +00001894 ParameterPackExpansion PPE(Pack);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001895 PPE.printLeft(OB);
1896 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001897 }
1898};
1899
1900class CallExpr : public Node {
1901 const Node *Callee;
1902 NodeArray Args;
1903
1904public:
1905 CallExpr(const Node *Callee_, NodeArray Args_)
1906 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1907
1908 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1909
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001910 void printLeft(OutputBuffer &OB) const override {
1911 Callee->print(OB);
1912 OB += "(";
1913 Args.printWithComma(OB);
1914 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001915 }
1916};
1917
1918class NewExpr : public Node {
1919 // new (expr_list) type(init_list)
1920 NodeArray ExprList;
1921 Node *Type;
1922 NodeArray InitList;
1923 bool IsGlobal; // ::operator new ?
1924 bool IsArray; // new[] ?
1925public:
1926 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1927 bool IsArray_)
1928 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1929 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1930
1931 template<typename Fn> void match(Fn F) const {
1932 F(ExprList, Type, InitList, IsGlobal, IsArray);
1933 }
1934
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001935 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001936 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001937 OB += "::operator ";
1938 OB += "new";
Richard Smithc20d1442018-08-20 20:14:49 +00001939 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001940 OB += "[]";
1941 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00001942 if (!ExprList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001943 OB += "(";
1944 ExprList.printWithComma(OB);
1945 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001946 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001947 Type->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001948 if (!InitList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001949 OB += "(";
1950 InitList.printWithComma(OB);
1951 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001952 }
Richard Smithc20d1442018-08-20 20:14:49 +00001953 }
1954};
1955
1956class DeleteExpr : public Node {
1957 Node *Op;
1958 bool IsGlobal;
1959 bool IsArray;
1960
1961public:
1962 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1963 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1964
1965 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1966
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001967 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001968 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001969 OB += "::";
1970 OB += "delete";
Richard Smithc20d1442018-08-20 20:14:49 +00001971 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001972 OB += "[] ";
1973 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001974 }
1975};
1976
1977class PrefixExpr : public Node {
1978 StringView Prefix;
1979 Node *Child;
1980
1981public:
1982 PrefixExpr(StringView Prefix_, Node *Child_)
1983 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1984
1985 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1986
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001987 void printLeft(OutputBuffer &OB) const override {
1988 OB += Prefix;
1989 OB += "(";
1990 Child->print(OB);
1991 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001992 }
1993};
1994
1995class FunctionParam : public Node {
1996 StringView Number;
1997
1998public:
1999 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
2000
2001 template<typename Fn> void match(Fn F) const { F(Number); }
2002
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002003 void printLeft(OutputBuffer &OB) const override {
2004 OB += "fp";
2005 OB += Number;
Richard Smithc20d1442018-08-20 20:14:49 +00002006 }
2007};
2008
2009class ConversionExpr : public Node {
2010 const Node *Type;
2011 NodeArray Expressions;
2012
2013public:
2014 ConversionExpr(const Node *Type_, NodeArray Expressions_)
2015 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
2016
2017 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
2018
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002019 void printLeft(OutputBuffer &OB) const override {
2020 OB += "(";
2021 Type->print(OB);
2022 OB += ")(";
2023 Expressions.printWithComma(OB);
2024 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002025 }
2026};
2027
Richard Smith1865d2f2020-10-22 19:29:36 -07002028class PointerToMemberConversionExpr : public Node {
2029 const Node *Type;
2030 const Node *SubExpr;
2031 StringView Offset;
2032
2033public:
2034 PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_,
2035 StringView Offset_)
2036 : Node(KPointerToMemberConversionExpr), Type(Type_), SubExpr(SubExpr_),
2037 Offset(Offset_) {}
2038
2039 template<typename Fn> void match(Fn F) const { F(Type, SubExpr, Offset); }
2040
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002041 void printLeft(OutputBuffer &OB) const override {
2042 OB += "(";
2043 Type->print(OB);
2044 OB += ")(";
2045 SubExpr->print(OB);
2046 OB += ")";
Richard Smith1865d2f2020-10-22 19:29:36 -07002047 }
2048};
2049
Richard Smithc20d1442018-08-20 20:14:49 +00002050class InitListExpr : public Node {
2051 const Node *Ty;
2052 NodeArray Inits;
2053public:
2054 InitListExpr(const Node *Ty_, NodeArray Inits_)
2055 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
2056
2057 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
2058
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002059 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002060 if (Ty)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002061 Ty->print(OB);
2062 OB += '{';
2063 Inits.printWithComma(OB);
2064 OB += '}';
Richard Smithc20d1442018-08-20 20:14:49 +00002065 }
2066};
2067
2068class BracedExpr : public Node {
2069 const Node *Elem;
2070 const Node *Init;
2071 bool IsArray;
2072public:
2073 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
2074 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
2075
2076 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
2077
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002078 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002079 if (IsArray) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002080 OB += '[';
2081 Elem->print(OB);
2082 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002083 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002084 OB += '.';
2085 Elem->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002086 }
2087 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002088 OB += " = ";
2089 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002090 }
2091};
2092
2093class BracedRangeExpr : public Node {
2094 const Node *First;
2095 const Node *Last;
2096 const Node *Init;
2097public:
2098 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
2099 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
2100
2101 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
2102
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002103 void printLeft(OutputBuffer &OB) const override {
2104 OB += '[';
2105 First->print(OB);
2106 OB += " ... ";
2107 Last->print(OB);
2108 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002109 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002110 OB += " = ";
2111 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002112 }
2113};
2114
2115class FoldExpr : public Node {
2116 const Node *Pack, *Init;
2117 StringView OperatorName;
2118 bool IsLeftFold;
2119
2120public:
2121 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
2122 const Node *Init_)
2123 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
2124 IsLeftFold(IsLeftFold_) {}
2125
2126 template<typename Fn> void match(Fn F) const {
2127 F(IsLeftFold, OperatorName, Pack, Init);
2128 }
2129
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002130 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002131 auto PrintPack = [&] {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002132 OB += '(';
2133 ParameterPackExpansion(Pack).print(OB);
2134 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002135 };
2136
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002137 OB += '(';
Richard Smithc20d1442018-08-20 20:14:49 +00002138
2139 if (IsLeftFold) {
2140 // init op ... op pack
2141 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002142 Init->print(OB);
2143 OB += ' ';
2144 OB += OperatorName;
2145 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002146 }
2147 // ... op pack
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002148 OB += "... ";
2149 OB += OperatorName;
2150 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002151 PrintPack();
2152 } else { // !IsLeftFold
2153 // pack op ...
2154 PrintPack();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002155 OB += ' ';
2156 OB += OperatorName;
2157 OB += " ...";
Richard Smithc20d1442018-08-20 20:14:49 +00002158 // pack op ... op init
2159 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002160 OB += ' ';
2161 OB += OperatorName;
2162 OB += ' ';
2163 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002164 }
2165 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002166 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002167 }
2168};
2169
2170class ThrowExpr : public Node {
2171 const Node *Op;
2172
2173public:
2174 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
2175
2176 template<typename Fn> void match(Fn F) const { F(Op); }
2177
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002178 void printLeft(OutputBuffer &OB) const override {
2179 OB += "throw ";
2180 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002181 }
2182};
2183
2184class BoolExpr : public Node {
2185 bool Value;
2186
2187public:
2188 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
2189
2190 template<typename Fn> void match(Fn F) const { F(Value); }
2191
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002192 void printLeft(OutputBuffer &OB) const override {
2193 OB += Value ? StringView("true") : StringView("false");
Richard Smithc20d1442018-08-20 20:14:49 +00002194 }
2195};
2196
Richard Smithdf1c14c2019-09-06 23:53:21 +00002197class StringLiteral : public Node {
2198 const Node *Type;
2199
2200public:
2201 StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
2202
2203 template<typename Fn> void match(Fn F) const { F(Type); }
2204
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002205 void printLeft(OutputBuffer &OB) const override {
2206 OB += "\"<";
2207 Type->print(OB);
2208 OB += ">\"";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002209 }
2210};
2211
2212class LambdaExpr : public Node {
2213 const Node *Type;
2214
Richard Smithdf1c14c2019-09-06 23:53:21 +00002215public:
2216 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2217
2218 template<typename Fn> void match(Fn F) const { F(Type); }
2219
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002220 void printLeft(OutputBuffer &OB) const override {
2221 OB += "[]";
Richard Smithfb917462019-09-09 22:26:04 +00002222 if (Type->getKind() == KClosureTypeName)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002223 static_cast<const ClosureTypeName *>(Type)->printDeclarator(OB);
2224 OB += "{...}";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002225 }
2226};
2227
Erik Pilkington0a170f12020-05-13 14:13:37 -04002228class EnumLiteral : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +00002229 // ty(integer)
2230 const Node *Ty;
2231 StringView Integer;
2232
2233public:
Erik Pilkington0a170f12020-05-13 14:13:37 -04002234 EnumLiteral(const Node *Ty_, StringView Integer_)
2235 : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00002236
2237 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2238
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002239 void printLeft(OutputBuffer &OB) const override {
2240 OB << "(";
2241 Ty->print(OB);
2242 OB << ")";
Erik Pilkington0a170f12020-05-13 14:13:37 -04002243
2244 if (Integer[0] == 'n')
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002245 OB << "-" << Integer.dropFront(1);
Erik Pilkington0a170f12020-05-13 14:13:37 -04002246 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002247 OB << Integer;
Richard Smithc20d1442018-08-20 20:14:49 +00002248 }
2249};
2250
2251class IntegerLiteral : public Node {
2252 StringView Type;
2253 StringView Value;
2254
2255public:
2256 IntegerLiteral(StringView Type_, StringView Value_)
2257 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2258
2259 template<typename Fn> void match(Fn F) const { F(Type, Value); }
2260
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002261 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002262 if (Type.size() > 3) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002263 OB += "(";
2264 OB += Type;
2265 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002266 }
2267
2268 if (Value[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002269 OB += "-";
2270 OB += Value.dropFront(1);
Richard Smithc20d1442018-08-20 20:14:49 +00002271 } else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002272 OB += Value;
Richard Smithc20d1442018-08-20 20:14:49 +00002273
2274 if (Type.size() <= 3)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002275 OB += Type;
Richard Smithc20d1442018-08-20 20:14:49 +00002276 }
2277};
2278
2279template <class Float> struct FloatData;
2280
2281namespace float_literal_impl {
2282constexpr Node::Kind getFloatLiteralKind(float *) {
2283 return Node::KFloatLiteral;
2284}
2285constexpr Node::Kind getFloatLiteralKind(double *) {
2286 return Node::KDoubleLiteral;
2287}
2288constexpr Node::Kind getFloatLiteralKind(long double *) {
2289 return Node::KLongDoubleLiteral;
2290}
2291}
2292
2293template <class Float> class FloatLiteralImpl : public Node {
2294 const StringView Contents;
2295
2296 static constexpr Kind KindForClass =
2297 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2298
2299public:
2300 FloatLiteralImpl(StringView Contents_)
2301 : Node(KindForClass), Contents(Contents_) {}
2302
2303 template<typename Fn> void match(Fn F) const { F(Contents); }
2304
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002305 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002306 const char *first = Contents.begin();
2307 const char *last = Contents.end() + 1;
2308
2309 const size_t N = FloatData<Float>::mangled_size;
2310 if (static_cast<std::size_t>(last - first) > N) {
2311 last = first + N;
2312 union {
2313 Float value;
2314 char buf[sizeof(Float)];
2315 };
2316 const char *t = first;
2317 char *e = buf;
2318 for (; t != last; ++t, ++e) {
2319 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2320 : static_cast<unsigned>(*t - 'a' + 10);
2321 ++t;
2322 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2323 : static_cast<unsigned>(*t - 'a' + 10);
2324 *e = static_cast<char>((d1 << 4) + d0);
2325 }
2326#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2327 std::reverse(buf, e);
2328#endif
2329 char num[FloatData<Float>::max_demangled_size] = {0};
2330 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002331 OB += StringView(num, num + n);
Richard Smithc20d1442018-08-20 20:14:49 +00002332 }
2333 }
2334};
2335
2336using FloatLiteral = FloatLiteralImpl<float>;
2337using DoubleLiteral = FloatLiteralImpl<double>;
2338using LongDoubleLiteral = FloatLiteralImpl<long double>;
2339
2340/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2341/// appropriate derived class.
2342template<typename Fn>
2343void Node::visit(Fn F) const {
2344 switch (K) {
2345#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2346 FOR_EACH_NODE_KIND(CASE)
2347#undef CASE
2348 }
2349 assert(0 && "unknown mangling node kind");
2350}
2351
2352/// Determine the kind of a node from its type.
2353template<typename NodeT> struct NodeKind;
2354#define SPECIALIZATION(X) \
2355 template<> struct NodeKind<X> { \
2356 static constexpr Node::Kind Kind = Node::K##X; \
2357 static constexpr const char *name() { return #X; } \
2358 };
2359FOR_EACH_NODE_KIND(SPECIALIZATION)
2360#undef SPECIALIZATION
2361
2362#undef FOR_EACH_NODE_KIND
2363
Pavel Labathba825192018-10-16 14:29:14 +00002364template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002365 const char *First;
2366 const char *Last;
2367
2368 // Name stack, this is used by the parser to hold temporary names that were
2369 // parsed. The parser collapses multiple names into new nodes to construct
2370 // the AST. Once the parser is finished, names.size() == 1.
2371 PODSmallVector<Node *, 32> Names;
2372
2373 // Substitution table. Itanium supports name substitutions as a means of
2374 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2375 // table.
2376 PODSmallVector<Node *, 32> Subs;
2377
Richard Smithdf1c14c2019-09-06 23:53:21 +00002378 using TemplateParamList = PODSmallVector<Node *, 8>;
2379
2380 class ScopedTemplateParamList {
2381 AbstractManglingParser *Parser;
2382 size_t OldNumTemplateParamLists;
2383 TemplateParamList Params;
2384
2385 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04002386 ScopedTemplateParamList(AbstractManglingParser *TheParser)
2387 : Parser(TheParser),
2388 OldNumTemplateParamLists(TheParser->TemplateParams.size()) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002389 Parser->TemplateParams.push_back(&Params);
2390 }
2391 ~ScopedTemplateParamList() {
2392 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2393 Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2394 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002395 };
2396
Richard Smithc20d1442018-08-20 20:14:49 +00002397 // Template parameter table. Like the above, but referenced like "T42_".
2398 // This has a smaller size compared to Subs and Names because it can be
2399 // stored on the stack.
Richard Smithdf1c14c2019-09-06 23:53:21 +00002400 TemplateParamList OuterTemplateParams;
2401
2402 // Lists of template parameters indexed by template parameter depth,
2403 // referenced like "TL2_4_". If nonempty, element 0 is always
2404 // OuterTemplateParams; inner elements are always template parameter lists of
2405 // lambda expressions. For a generic lambda with no explicit template
2406 // parameter list, the corresponding parameter list pointer will be null.
2407 PODSmallVector<TemplateParamList *, 4> TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00002408
2409 // Set of unresolved forward <template-param> references. These can occur in a
2410 // conversion operator's type, and are resolved in the enclosing <encoding>.
2411 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2412
Richard Smithc20d1442018-08-20 20:14:49 +00002413 bool TryToParseTemplateArgs = true;
2414 bool PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002415 size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2416
2417 unsigned NumSyntheticTemplateParameters[3] = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002418
2419 Alloc ASTAllocator;
2420
Pavel Labathba825192018-10-16 14:29:14 +00002421 AbstractManglingParser(const char *First_, const char *Last_)
2422 : First(First_), Last(Last_) {}
2423
2424 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002425
2426 void reset(const char *First_, const char *Last_) {
2427 First = First_;
2428 Last = Last_;
2429 Names.clear();
2430 Subs.clear();
2431 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002432 ParsingLambdaParamsAtLevel = (size_t)-1;
Richard Smithc20d1442018-08-20 20:14:49 +00002433 TryToParseTemplateArgs = true;
2434 PermitForwardTemplateReferences = false;
Richard Smith9a2307a2019-09-07 00:11:53 +00002435 for (int I = 0; I != 3; ++I)
2436 NumSyntheticTemplateParameters[I] = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00002437 ASTAllocator.reset();
2438 }
2439
Richard Smithb485b352018-08-24 23:30:26 +00002440 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002441 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2442 }
2443
2444 template <class It> NodeArray makeNodeArray(It begin, It end) {
2445 size_t sz = static_cast<size_t>(end - begin);
2446 void *mem = ASTAllocator.allocateNodeArray(sz);
2447 Node **data = new (mem) Node *[sz];
2448 std::copy(begin, end, data);
2449 return NodeArray(data, sz);
2450 }
2451
2452 NodeArray popTrailingNodeArray(size_t FromPosition) {
2453 assert(FromPosition <= Names.size());
2454 NodeArray res =
2455 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2456 Names.dropBack(FromPosition);
2457 return res;
2458 }
2459
2460 bool consumeIf(StringView S) {
2461 if (StringView(First, Last).startsWith(S)) {
2462 First += S.size();
2463 return true;
2464 }
2465 return false;
2466 }
2467
2468 bool consumeIf(char C) {
2469 if (First != Last && *First == C) {
2470 ++First;
2471 return true;
2472 }
2473 return false;
2474 }
2475
2476 char consume() { return First != Last ? *First++ : '\0'; }
2477
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08002478 char look(unsigned Lookahead = 0) const {
Richard Smithc20d1442018-08-20 20:14:49 +00002479 if (static_cast<size_t>(Last - First) <= Lookahead)
2480 return '\0';
2481 return First[Lookahead];
2482 }
2483
2484 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2485
2486 StringView parseNumber(bool AllowNegative = false);
2487 Qualifiers parseCVQualifiers();
2488 bool parsePositiveInteger(size_t *Out);
2489 StringView parseBareSourceName();
2490
2491 bool parseSeqId(size_t *Out);
2492 Node *parseSubstitution();
2493 Node *parseTemplateParam();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002494 Node *parseTemplateParamDecl();
Richard Smithc20d1442018-08-20 20:14:49 +00002495 Node *parseTemplateArgs(bool TagTemplates = false);
2496 Node *parseTemplateArg();
2497
2498 /// Parse the <expr> production.
2499 Node *parseExpr();
2500 Node *parsePrefixExpr(StringView Kind);
2501 Node *parseBinaryExpr(StringView Kind);
2502 Node *parseIntegerLiteral(StringView Lit);
2503 Node *parseExprPrimary();
2504 template <class Float> Node *parseFloatingLiteral();
2505 Node *parseFunctionParam();
2506 Node *parseNewExpr();
2507 Node *parseConversionExpr();
2508 Node *parseBracedExpr();
2509 Node *parseFoldExpr();
Richard Smith1865d2f2020-10-22 19:29:36 -07002510 Node *parsePointerToMemberConversionExpr();
2511 Node *parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00002512
2513 /// Parse the <type> production.
2514 Node *parseType();
2515 Node *parseFunctionType();
2516 Node *parseVectorType();
2517 Node *parseDecltype();
2518 Node *parseArrayType();
2519 Node *parsePointerToMemberType();
2520 Node *parseClassEnumType();
2521 Node *parseQualifiedType();
2522
2523 Node *parseEncoding();
2524 bool parseCallOffset();
2525 Node *parseSpecialName();
2526
2527 /// Holds some extra information about a <name> that is being parsed. This
2528 /// information is only pertinent if the <name> refers to an <encoding>.
2529 struct NameState {
2530 bool CtorDtorConversion = false;
2531 bool EndsWithTemplateArgs = false;
2532 Qualifiers CVQualifiers = QualNone;
2533 FunctionRefQual ReferenceQualifier = FrefQualNone;
2534 size_t ForwardTemplateRefsBegin;
2535
Pavel Labathba825192018-10-16 14:29:14 +00002536 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002537 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2538 };
2539
2540 bool resolveForwardTemplateRefs(NameState &State) {
2541 size_t I = State.ForwardTemplateRefsBegin;
2542 size_t E = ForwardTemplateRefs.size();
2543 for (; I < E; ++I) {
2544 size_t Idx = ForwardTemplateRefs[I]->Index;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002545 if (TemplateParams.empty() || !TemplateParams[0] ||
2546 Idx >= TemplateParams[0]->size())
Richard Smithc20d1442018-08-20 20:14:49 +00002547 return true;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002548 ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
Richard Smithc20d1442018-08-20 20:14:49 +00002549 }
2550 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2551 return false;
2552 }
2553
2554 /// Parse the <name> production>
2555 Node *parseName(NameState *State = nullptr);
2556 Node *parseLocalName(NameState *State);
2557 Node *parseOperatorName(NameState *State);
2558 Node *parseUnqualifiedName(NameState *State);
2559 Node *parseUnnamedTypeName(NameState *State);
2560 Node *parseSourceName(NameState *State);
2561 Node *parseUnscopedName(NameState *State);
2562 Node *parseNestedName(NameState *State);
2563 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2564
2565 Node *parseAbiTags(Node *N);
2566
2567 /// Parse the <unresolved-name> production.
2568 Node *parseUnresolvedName();
2569 Node *parseSimpleId();
2570 Node *parseBaseUnresolvedName();
2571 Node *parseUnresolvedType();
2572 Node *parseDestructorName();
2573
2574 /// Top-level entry point into the parser.
2575 Node *parse();
2576};
2577
2578const char* parse_discriminator(const char* first, const char* last);
2579
2580// <name> ::= <nested-name> // N
2581// ::= <local-name> # See Scope Encoding below // Z
2582// ::= <unscoped-template-name> <template-args>
2583// ::= <unscoped-name>
2584//
2585// <unscoped-template-name> ::= <unscoped-name>
2586// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002587template <typename Derived, typename Alloc>
2588Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002589 consumeIf('L'); // extension
2590
2591 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002592 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002593 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002594 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002595
2596 // ::= <unscoped-template-name> <template-args>
2597 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00002598 Node *S = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00002599 if (S == nullptr)
2600 return nullptr;
2601 if (look() != 'I')
2602 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002603 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002604 if (TA == nullptr)
2605 return nullptr;
2606 if (State) State->EndsWithTemplateArgs = true;
2607 return make<NameWithTemplateArgs>(S, TA);
2608 }
2609
Pavel Labathba825192018-10-16 14:29:14 +00002610 Node *N = getDerived().parseUnscopedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002611 if (N == nullptr)
2612 return nullptr;
2613 // ::= <unscoped-template-name> <template-args>
2614 if (look() == 'I') {
2615 Subs.push_back(N);
Pavel Labathba825192018-10-16 14:29:14 +00002616 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002617 if (TA == nullptr)
2618 return nullptr;
2619 if (State) State->EndsWithTemplateArgs = true;
2620 return make<NameWithTemplateArgs>(N, TA);
2621 }
2622 // ::= <unscoped-name>
2623 return N;
2624}
2625
2626// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2627// := Z <function encoding> E s [<discriminator>]
2628// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002629template <typename Derived, typename Alloc>
2630Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002631 if (!consumeIf('Z'))
2632 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002633 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002634 if (Encoding == nullptr || !consumeIf('E'))
2635 return nullptr;
2636
2637 if (consumeIf('s')) {
2638 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002639 auto *StringLitName = make<NameType>("string literal");
2640 if (!StringLitName)
2641 return nullptr;
2642 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002643 }
2644
2645 if (consumeIf('d')) {
2646 parseNumber(true);
2647 if (!consumeIf('_'))
2648 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002649 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002650 if (N == nullptr)
2651 return nullptr;
2652 return make<LocalName>(Encoding, N);
2653 }
2654
Pavel Labathba825192018-10-16 14:29:14 +00002655 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002656 if (Entity == nullptr)
2657 return nullptr;
2658 First = parse_discriminator(First, Last);
2659 return make<LocalName>(Encoding, Entity);
2660}
2661
2662// <unscoped-name> ::= <unqualified-name>
2663// ::= St <unqualified-name> # ::std::
2664// extension ::= StL<unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00002665template <typename Derived, typename Alloc>
2666Node *
2667AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
2668 if (consumeIf("StL") || consumeIf("St")) {
2669 Node *R = getDerived().parseUnqualifiedName(State);
2670 if (R == nullptr)
2671 return nullptr;
2672 return make<StdQualifiedName>(R);
2673 }
2674 return getDerived().parseUnqualifiedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002675}
2676
2677// <unqualified-name> ::= <operator-name> [abi-tags]
2678// ::= <ctor-dtor-name>
2679// ::= <source-name>
2680// ::= <unnamed-type-name>
2681// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002682template <typename Derived, typename Alloc>
2683Node *
2684AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002685 // <ctor-dtor-name>s are special-cased in parseNestedName().
2686 Node *Result;
2687 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002688 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002689 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002690 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002691 else if (consumeIf("DC")) {
2692 size_t BindingsBegin = Names.size();
2693 do {
Pavel Labathba825192018-10-16 14:29:14 +00002694 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002695 if (Binding == nullptr)
2696 return nullptr;
2697 Names.push_back(Binding);
2698 } while (!consumeIf('E'));
2699 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2700 } else
Pavel Labathba825192018-10-16 14:29:14 +00002701 Result = getDerived().parseOperatorName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002702 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002703 Result = getDerived().parseAbiTags(Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002704 return Result;
2705}
2706
2707// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2708// ::= <closure-type-name>
2709//
2710// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2711//
2712// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002713template <typename Derived, typename Alloc>
2714Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002715AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2716 // <template-params> refer to the innermost <template-args>. Clear out any
2717 // outer args that we may have inserted into TemplateParams.
2718 if (State != nullptr)
2719 TemplateParams.clear();
2720
Richard Smithc20d1442018-08-20 20:14:49 +00002721 if (consumeIf("Ut")) {
2722 StringView Count = parseNumber();
2723 if (!consumeIf('_'))
2724 return nullptr;
2725 return make<UnnamedTypeName>(Count);
2726 }
2727 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002728 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2729 TemplateParams.size());
2730 ScopedTemplateParamList LambdaTemplateParams(this);
2731
2732 size_t ParamsBegin = Names.size();
2733 while (look() == 'T' &&
2734 StringView("yptn").find(look(1)) != StringView::npos) {
2735 Node *T = parseTemplateParamDecl();
2736 if (!T)
2737 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002738 Names.push_back(T);
2739 }
2740 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2741
2742 // FIXME: If TempParams is empty and none of the function parameters
2743 // includes 'auto', we should remove LambdaTemplateParams from the
2744 // TemplateParams list. Unfortunately, we don't find out whether there are
2745 // any 'auto' parameters until too late in an example such as:
2746 //
2747 // template<typename T> void f(
2748 // decltype([](decltype([]<typename T>(T v) {}),
2749 // auto) {})) {}
2750 // template<typename T> void f(
2751 // decltype([](decltype([]<typename T>(T w) {}),
2752 // int) {})) {}
2753 //
2754 // Here, the type of v is at level 2 but the type of w is at level 1. We
2755 // don't find this out until we encounter the type of the next parameter.
2756 //
2757 // However, compilers can't actually cope with the former example in
2758 // practice, and it's likely to be made ill-formed in future, so we don't
2759 // need to support it here.
2760 //
2761 // If we encounter an 'auto' in the function parameter types, we will
2762 // recreate a template parameter scope for it, but any intervening lambdas
2763 // will be parsed in the 'wrong' template parameter depth.
2764 if (TempParams.empty())
2765 TemplateParams.pop_back();
2766
Richard Smithc20d1442018-08-20 20:14:49 +00002767 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002768 do {
Pavel Labathba825192018-10-16 14:29:14 +00002769 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002770 if (P == nullptr)
2771 return nullptr;
2772 Names.push_back(P);
2773 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002774 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002775 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2776
Richard Smithc20d1442018-08-20 20:14:49 +00002777 StringView Count = parseNumber();
2778 if (!consumeIf('_'))
2779 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002780 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002781 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002782 if (consumeIf("Ub")) {
2783 (void)parseNumber();
2784 if (!consumeIf('_'))
2785 return nullptr;
2786 return make<NameType>("'block-literal'");
2787 }
Richard Smithc20d1442018-08-20 20:14:49 +00002788 return nullptr;
2789}
2790
2791// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002792template <typename Derived, typename Alloc>
2793Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002794 size_t Length = 0;
2795 if (parsePositiveInteger(&Length))
2796 return nullptr;
2797 if (numLeft() < Length || Length == 0)
2798 return nullptr;
2799 StringView Name(First, First + Length);
2800 First += Length;
2801 if (Name.startsWith("_GLOBAL__N"))
2802 return make<NameType>("(anonymous namespace)");
2803 return make<NameType>(Name);
2804}
2805
2806// <operator-name> ::= aa # &&
2807// ::= ad # & (unary)
2808// ::= an # &
2809// ::= aN # &=
2810// ::= aS # =
2811// ::= cl # ()
2812// ::= cm # ,
2813// ::= co # ~
2814// ::= cv <type> # (cast)
2815// ::= da # delete[]
2816// ::= de # * (unary)
2817// ::= dl # delete
2818// ::= dv # /
2819// ::= dV # /=
2820// ::= eo # ^
2821// ::= eO # ^=
2822// ::= eq # ==
2823// ::= ge # >=
2824// ::= gt # >
2825// ::= ix # []
2826// ::= le # <=
2827// ::= li <source-name> # operator ""
2828// ::= ls # <<
2829// ::= lS # <<=
2830// ::= lt # <
2831// ::= mi # -
2832// ::= mI # -=
2833// ::= ml # *
2834// ::= mL # *=
2835// ::= mm # -- (postfix in <expression> context)
2836// ::= na # new[]
2837// ::= ne # !=
2838// ::= ng # - (unary)
2839// ::= nt # !
2840// ::= nw # new
2841// ::= oo # ||
2842// ::= or # |
2843// ::= oR # |=
2844// ::= pm # ->*
2845// ::= pl # +
2846// ::= pL # +=
2847// ::= pp # ++ (postfix in <expression> context)
2848// ::= ps # + (unary)
2849// ::= pt # ->
2850// ::= qu # ?
2851// ::= rm # %
2852// ::= rM # %=
2853// ::= rs # >>
2854// ::= rS # >>=
2855// ::= ss # <=> C++2a
2856// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002857template <typename Derived, typename Alloc>
2858Node *
2859AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002860 switch (look()) {
2861 case 'a':
2862 switch (look(1)) {
2863 case 'a':
2864 First += 2;
2865 return make<NameType>("operator&&");
2866 case 'd':
2867 case 'n':
2868 First += 2;
2869 return make<NameType>("operator&");
2870 case 'N':
2871 First += 2;
2872 return make<NameType>("operator&=");
2873 case 'S':
2874 First += 2;
2875 return make<NameType>("operator=");
2876 }
2877 return nullptr;
2878 case 'c':
2879 switch (look(1)) {
2880 case 'l':
2881 First += 2;
2882 return make<NameType>("operator()");
2883 case 'm':
2884 First += 2;
2885 return make<NameType>("operator,");
2886 case 'o':
2887 First += 2;
2888 return make<NameType>("operator~");
2889 // ::= cv <type> # (cast)
2890 case 'v': {
2891 First += 2;
2892 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2893 // If we're parsing an encoding, State != nullptr and the conversion
2894 // operators' <type> could have a <template-param> that refers to some
2895 // <template-arg>s further ahead in the mangled name.
2896 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2897 PermitForwardTemplateReferences ||
2898 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002899 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002900 if (Ty == nullptr)
2901 return nullptr;
2902 if (State) State->CtorDtorConversion = true;
2903 return make<ConversionOperatorType>(Ty);
2904 }
2905 }
2906 return nullptr;
2907 case 'd':
2908 switch (look(1)) {
2909 case 'a':
2910 First += 2;
2911 return make<NameType>("operator delete[]");
2912 case 'e':
2913 First += 2;
2914 return make<NameType>("operator*");
2915 case 'l':
2916 First += 2;
2917 return make<NameType>("operator delete");
2918 case 'v':
2919 First += 2;
2920 return make<NameType>("operator/");
2921 case 'V':
2922 First += 2;
2923 return make<NameType>("operator/=");
2924 }
2925 return nullptr;
2926 case 'e':
2927 switch (look(1)) {
2928 case 'o':
2929 First += 2;
2930 return make<NameType>("operator^");
2931 case 'O':
2932 First += 2;
2933 return make<NameType>("operator^=");
2934 case 'q':
2935 First += 2;
2936 return make<NameType>("operator==");
2937 }
2938 return nullptr;
2939 case 'g':
2940 switch (look(1)) {
2941 case 'e':
2942 First += 2;
2943 return make<NameType>("operator>=");
2944 case 't':
2945 First += 2;
2946 return make<NameType>("operator>");
2947 }
2948 return nullptr;
2949 case 'i':
2950 if (look(1) == 'x') {
2951 First += 2;
2952 return make<NameType>("operator[]");
2953 }
2954 return nullptr;
2955 case 'l':
2956 switch (look(1)) {
2957 case 'e':
2958 First += 2;
2959 return make<NameType>("operator<=");
2960 // ::= li <source-name> # operator ""
2961 case 'i': {
2962 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002963 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002964 if (SN == nullptr)
2965 return nullptr;
2966 return make<LiteralOperator>(SN);
2967 }
2968 case 's':
2969 First += 2;
2970 return make<NameType>("operator<<");
2971 case 'S':
2972 First += 2;
2973 return make<NameType>("operator<<=");
2974 case 't':
2975 First += 2;
2976 return make<NameType>("operator<");
2977 }
2978 return nullptr;
2979 case 'm':
2980 switch (look(1)) {
2981 case 'i':
2982 First += 2;
2983 return make<NameType>("operator-");
2984 case 'I':
2985 First += 2;
2986 return make<NameType>("operator-=");
2987 case 'l':
2988 First += 2;
2989 return make<NameType>("operator*");
2990 case 'L':
2991 First += 2;
2992 return make<NameType>("operator*=");
2993 case 'm':
2994 First += 2;
2995 return make<NameType>("operator--");
2996 }
2997 return nullptr;
2998 case 'n':
2999 switch (look(1)) {
3000 case 'a':
3001 First += 2;
3002 return make<NameType>("operator new[]");
3003 case 'e':
3004 First += 2;
3005 return make<NameType>("operator!=");
3006 case 'g':
3007 First += 2;
3008 return make<NameType>("operator-");
3009 case 't':
3010 First += 2;
3011 return make<NameType>("operator!");
3012 case 'w':
3013 First += 2;
3014 return make<NameType>("operator new");
3015 }
3016 return nullptr;
3017 case 'o':
3018 switch (look(1)) {
3019 case 'o':
3020 First += 2;
3021 return make<NameType>("operator||");
3022 case 'r':
3023 First += 2;
3024 return make<NameType>("operator|");
3025 case 'R':
3026 First += 2;
3027 return make<NameType>("operator|=");
3028 }
3029 return nullptr;
3030 case 'p':
3031 switch (look(1)) {
3032 case 'm':
3033 First += 2;
3034 return make<NameType>("operator->*");
3035 case 'l':
3036 First += 2;
3037 return make<NameType>("operator+");
3038 case 'L':
3039 First += 2;
3040 return make<NameType>("operator+=");
3041 case 'p':
3042 First += 2;
3043 return make<NameType>("operator++");
3044 case 's':
3045 First += 2;
3046 return make<NameType>("operator+");
3047 case 't':
3048 First += 2;
3049 return make<NameType>("operator->");
3050 }
3051 return nullptr;
3052 case 'q':
3053 if (look(1) == 'u') {
3054 First += 2;
3055 return make<NameType>("operator?");
3056 }
3057 return nullptr;
3058 case 'r':
3059 switch (look(1)) {
3060 case 'm':
3061 First += 2;
3062 return make<NameType>("operator%");
3063 case 'M':
3064 First += 2;
3065 return make<NameType>("operator%=");
3066 case 's':
3067 First += 2;
3068 return make<NameType>("operator>>");
3069 case 'S':
3070 First += 2;
3071 return make<NameType>("operator>>=");
3072 }
3073 return nullptr;
3074 case 's':
3075 if (look(1) == 's') {
3076 First += 2;
3077 return make<NameType>("operator<=>");
3078 }
3079 return nullptr;
3080 // ::= v <digit> <source-name> # vendor extended operator
3081 case 'v':
3082 if (std::isdigit(look(1))) {
3083 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003084 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003085 if (SN == nullptr)
3086 return nullptr;
3087 return make<ConversionOperatorType>(SN);
3088 }
3089 return nullptr;
3090 }
3091 return nullptr;
3092}
3093
3094// <ctor-dtor-name> ::= C1 # complete object constructor
3095// ::= C2 # base object constructor
3096// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003097// extension ::= C4 # gcc old-style "[unified]" constructor
3098// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003099// ::= D0 # deleting destructor
3100// ::= D1 # complete object destructor
3101// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003102// extension ::= D4 # gcc old-style "[unified]" destructor
3103// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003104template <typename Derived, typename Alloc>
3105Node *
3106AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3107 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003108 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3109 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3110 switch (SSK) {
3111 case SpecialSubKind::string:
3112 case SpecialSubKind::istream:
3113 case SpecialSubKind::ostream:
3114 case SpecialSubKind::iostream:
3115 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003116 if (!SoFar)
3117 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003118 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003119 default:
3120 break;
3121 }
3122 }
3123
3124 if (consumeIf('C')) {
3125 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003126 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3127 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003128 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003129 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003130 ++First;
3131 if (State) State->CtorDtorConversion = true;
3132 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003133 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003134 return nullptr;
3135 }
Nico Weber29294792019-04-03 23:14:33 +00003136 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003137 }
3138
Nico Weber29294792019-04-03 23:14:33 +00003139 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3140 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003141 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003142 First += 2;
3143 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003144 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003145 }
3146
3147 return nullptr;
3148}
3149
3150// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3151// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3152//
3153// <prefix> ::= <prefix> <unqualified-name>
3154// ::= <template-prefix> <template-args>
3155// ::= <template-param>
3156// ::= <decltype>
3157// ::= # empty
3158// ::= <substitution>
3159// ::= <prefix> <data-member-prefix>
3160// extension ::= L
3161//
3162// <data-member-prefix> := <member source-name> [<template-args>] M
3163//
3164// <template-prefix> ::= <prefix> <template unqualified-name>
3165// ::= <template-param>
3166// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003167template <typename Derived, typename Alloc>
3168Node *
3169AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003170 if (!consumeIf('N'))
3171 return nullptr;
3172
3173 Qualifiers CVTmp = parseCVQualifiers();
3174 if (State) State->CVQualifiers = CVTmp;
3175
3176 if (consumeIf('O')) {
3177 if (State) State->ReferenceQualifier = FrefQualRValue;
3178 } else if (consumeIf('R')) {
3179 if (State) State->ReferenceQualifier = FrefQualLValue;
3180 } else
3181 if (State) State->ReferenceQualifier = FrefQualNone;
3182
3183 Node *SoFar = nullptr;
3184 auto PushComponent = [&](Node *Comp) {
Richard Smithb485b352018-08-24 23:30:26 +00003185 if (!Comp) return false;
Richard Smithc20d1442018-08-20 20:14:49 +00003186 if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
3187 else SoFar = Comp;
3188 if (State) State->EndsWithTemplateArgs = false;
Richard Smithb485b352018-08-24 23:30:26 +00003189 return SoFar != nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003190 };
3191
Richard Smithb485b352018-08-24 23:30:26 +00003192 if (consumeIf("St")) {
Richard Smithc20d1442018-08-20 20:14:49 +00003193 SoFar = make<NameType>("std");
Richard Smithb485b352018-08-24 23:30:26 +00003194 if (!SoFar)
3195 return nullptr;
3196 }
Richard Smithc20d1442018-08-20 20:14:49 +00003197
3198 while (!consumeIf('E')) {
3199 consumeIf('L'); // extension
3200
3201 // <data-member-prefix> := <member source-name> [<template-args>] M
3202 if (consumeIf('M')) {
3203 if (SoFar == nullptr)
3204 return nullptr;
3205 continue;
3206 }
3207
3208 // ::= <template-param>
3209 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003210 if (!PushComponent(getDerived().parseTemplateParam()))
Richard Smithc20d1442018-08-20 20:14:49 +00003211 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003212 Subs.push_back(SoFar);
3213 continue;
3214 }
3215
3216 // ::= <template-prefix> <template-args>
3217 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003218 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003219 if (TA == nullptr || SoFar == nullptr)
3220 return nullptr;
3221 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003222 if (!SoFar)
3223 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003224 if (State) State->EndsWithTemplateArgs = true;
3225 Subs.push_back(SoFar);
3226 continue;
3227 }
3228
3229 // ::= <decltype>
3230 if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
Pavel Labathba825192018-10-16 14:29:14 +00003231 if (!PushComponent(getDerived().parseDecltype()))
Richard Smithc20d1442018-08-20 20:14:49 +00003232 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003233 Subs.push_back(SoFar);
3234 continue;
3235 }
3236
3237 // ::= <substitution>
3238 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00003239 Node *S = getDerived().parseSubstitution();
Richard Smithb485b352018-08-24 23:30:26 +00003240 if (!PushComponent(S))
Richard Smithc20d1442018-08-20 20:14:49 +00003241 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003242 if (SoFar != S)
3243 Subs.push_back(S);
3244 continue;
3245 }
3246
3247 // Parse an <unqualified-name> thats actually a <ctor-dtor-name>.
3248 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
3249 if (SoFar == nullptr)
3250 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003251 if (!PushComponent(getDerived().parseCtorDtorName(SoFar, State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003252 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003253 SoFar = getDerived().parseAbiTags(SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00003254 if (SoFar == nullptr)
3255 return nullptr;
3256 Subs.push_back(SoFar);
3257 continue;
3258 }
3259
3260 // ::= <prefix> <unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00003261 if (!PushComponent(getDerived().parseUnqualifiedName(State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003262 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003263 Subs.push_back(SoFar);
3264 }
3265
3266 if (SoFar == nullptr || Subs.empty())
3267 return nullptr;
3268
3269 Subs.pop_back();
3270 return SoFar;
3271}
3272
3273// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003274template <typename Derived, typename Alloc>
3275Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3276 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003277 if (SN == nullptr)
3278 return nullptr;
3279 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003280 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003281 if (TA == nullptr)
3282 return nullptr;
3283 return make<NameWithTemplateArgs>(SN, TA);
3284 }
3285 return SN;
3286}
3287
3288// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3289// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003290template <typename Derived, typename Alloc>
3291Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003292 Node *Result;
3293 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003294 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003295 else
Pavel Labathba825192018-10-16 14:29:14 +00003296 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003297 if (Result == nullptr)
3298 return nullptr;
3299 return make<DtorName>(Result);
3300}
3301
3302// <unresolved-type> ::= <template-param>
3303// ::= <decltype>
3304// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003305template <typename Derived, typename Alloc>
3306Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003307 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003308 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003309 if (TP == nullptr)
3310 return nullptr;
3311 Subs.push_back(TP);
3312 return TP;
3313 }
3314 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003315 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003316 if (DT == nullptr)
3317 return nullptr;
3318 Subs.push_back(DT);
3319 return DT;
3320 }
Pavel Labathba825192018-10-16 14:29:14 +00003321 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003322}
3323
3324// <base-unresolved-name> ::= <simple-id> # unresolved name
3325// extension ::= <operator-name> # unresolved operator-function-id
3326// extension ::= <operator-name> <template-args> # unresolved operator template-id
3327// ::= on <operator-name> # unresolved operator-function-id
3328// ::= on <operator-name> <template-args> # unresolved operator template-id
3329// ::= dn <destructor-name> # destructor or pseudo-destructor;
3330// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003331template <typename Derived, typename Alloc>
3332Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003333 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003334 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003335
3336 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003337 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003338
3339 consumeIf("on");
3340
Pavel Labathba825192018-10-16 14:29:14 +00003341 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003342 if (Oper == nullptr)
3343 return nullptr;
3344 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003345 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003346 if (TA == nullptr)
3347 return nullptr;
3348 return make<NameWithTemplateArgs>(Oper, TA);
3349 }
3350 return Oper;
3351}
3352
3353// <unresolved-name>
3354// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3355// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3356// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3357// # A::x, N::y, A<T>::z; "gs" means leading "::"
3358// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3359// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3360// # T::N::x /decltype(p)::N::x
3361// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3362//
3363// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003364template <typename Derived, typename Alloc>
3365Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003366 Node *SoFar = nullptr;
3367
3368 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3369 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3370 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003371 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003372 if (SoFar == nullptr)
3373 return nullptr;
3374
3375 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003376 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003377 if (TA == nullptr)
3378 return nullptr;
3379 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003380 if (!SoFar)
3381 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003382 }
3383
3384 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003385 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003386 if (Qual == nullptr)
3387 return nullptr;
3388 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003389 if (!SoFar)
3390 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003391 }
3392
Pavel Labathba825192018-10-16 14:29:14 +00003393 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003394 if (Base == nullptr)
3395 return nullptr;
3396 return make<QualifiedName>(SoFar, Base);
3397 }
3398
3399 bool Global = consumeIf("gs");
3400
3401 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3402 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003403 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003404 if (SoFar == nullptr)
3405 return nullptr;
3406 if (Global)
3407 SoFar = make<GlobalQualifiedName>(SoFar);
3408 return SoFar;
3409 }
3410
3411 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3412 if (std::isdigit(look())) {
3413 do {
Pavel Labathba825192018-10-16 14:29:14 +00003414 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003415 if (Qual == nullptr)
3416 return nullptr;
3417 if (SoFar)
3418 SoFar = make<QualifiedName>(SoFar, Qual);
3419 else if (Global)
3420 SoFar = make<GlobalQualifiedName>(Qual);
3421 else
3422 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003423 if (!SoFar)
3424 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003425 } while (!consumeIf('E'));
3426 }
3427 // sr <unresolved-type> <base-unresolved-name>
3428 // sr <unresolved-type> <template-args> <base-unresolved-name>
3429 else {
Pavel Labathba825192018-10-16 14:29:14 +00003430 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003431 if (SoFar == nullptr)
3432 return nullptr;
3433
3434 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003435 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003436 if (TA == nullptr)
3437 return nullptr;
3438 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003439 if (!SoFar)
3440 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003441 }
3442 }
3443
3444 assert(SoFar != nullptr);
3445
Pavel Labathba825192018-10-16 14:29:14 +00003446 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003447 if (Base == nullptr)
3448 return nullptr;
3449 return make<QualifiedName>(SoFar, Base);
3450}
3451
3452// <abi-tags> ::= <abi-tag> [<abi-tags>]
3453// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003454template <typename Derived, typename Alloc>
3455Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003456 while (consumeIf('B')) {
3457 StringView SN = parseBareSourceName();
3458 if (SN.empty())
3459 return nullptr;
3460 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003461 if (!N)
3462 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003463 }
3464 return N;
3465}
3466
3467// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003468template <typename Alloc, typename Derived>
3469StringView
3470AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003471 const char *Tmp = First;
3472 if (AllowNegative)
3473 consumeIf('n');
3474 if (numLeft() == 0 || !std::isdigit(*First))
3475 return StringView();
3476 while (numLeft() != 0 && std::isdigit(*First))
3477 ++First;
3478 return StringView(Tmp, First);
3479}
3480
3481// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003482template <typename Alloc, typename Derived>
3483bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003484 *Out = 0;
3485 if (look() < '0' || look() > '9')
3486 return true;
3487 while (look() >= '0' && look() <= '9') {
3488 *Out *= 10;
3489 *Out += static_cast<size_t>(consume() - '0');
3490 }
3491 return false;
3492}
3493
Pavel Labathba825192018-10-16 14:29:14 +00003494template <typename Alloc, typename Derived>
3495StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003496 size_t Int = 0;
3497 if (parsePositiveInteger(&Int) || numLeft() < Int)
3498 return StringView();
3499 StringView R(First, First + Int);
3500 First += Int;
3501 return R;
3502}
3503
3504// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3505//
3506// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3507// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3508// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3509//
3510// <ref-qualifier> ::= R # & ref-qualifier
3511// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003512template <typename Derived, typename Alloc>
3513Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003514 Qualifiers CVQuals = parseCVQualifiers();
3515
3516 Node *ExceptionSpec = nullptr;
3517 if (consumeIf("Do")) {
3518 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003519 if (!ExceptionSpec)
3520 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003521 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003522 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003523 if (E == nullptr || !consumeIf('E'))
3524 return nullptr;
3525 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003526 if (!ExceptionSpec)
3527 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003528 } else if (consumeIf("Dw")) {
3529 size_t SpecsBegin = Names.size();
3530 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003531 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003532 if (T == nullptr)
3533 return nullptr;
3534 Names.push_back(T);
3535 }
3536 ExceptionSpec =
3537 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003538 if (!ExceptionSpec)
3539 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003540 }
3541
3542 consumeIf("Dx"); // transaction safe
3543
3544 if (!consumeIf('F'))
3545 return nullptr;
3546 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003547 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003548 if (ReturnType == nullptr)
3549 return nullptr;
3550
3551 FunctionRefQual ReferenceQualifier = FrefQualNone;
3552 size_t ParamsBegin = Names.size();
3553 while (true) {
3554 if (consumeIf('E'))
3555 break;
3556 if (consumeIf('v'))
3557 continue;
3558 if (consumeIf("RE")) {
3559 ReferenceQualifier = FrefQualLValue;
3560 break;
3561 }
3562 if (consumeIf("OE")) {
3563 ReferenceQualifier = FrefQualRValue;
3564 break;
3565 }
Pavel Labathba825192018-10-16 14:29:14 +00003566 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003567 if (T == nullptr)
3568 return nullptr;
3569 Names.push_back(T);
3570 }
3571
3572 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3573 return make<FunctionType>(ReturnType, Params, CVQuals,
3574 ReferenceQualifier, ExceptionSpec);
3575}
3576
3577// extension:
3578// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3579// ::= Dv [<dimension expression>] _ <element type>
3580// <extended element type> ::= <element type>
3581// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003582template <typename Derived, typename Alloc>
3583Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003584 if (!consumeIf("Dv"))
3585 return nullptr;
3586 if (look() >= '1' && look() <= '9') {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003587 Node *DimensionNumber = make<NameType>(parseNumber());
3588 if (!DimensionNumber)
3589 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003590 if (!consumeIf('_'))
3591 return nullptr;
3592 if (consumeIf('p'))
3593 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003594 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003595 if (ElemType == nullptr)
3596 return nullptr;
3597 return make<VectorType>(ElemType, DimensionNumber);
3598 }
3599
3600 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003601 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003602 if (!DimExpr)
3603 return nullptr;
3604 if (!consumeIf('_'))
3605 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003606 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003607 if (!ElemType)
3608 return nullptr;
3609 return make<VectorType>(ElemType, DimExpr);
3610 }
Pavel Labathba825192018-10-16 14:29:14 +00003611 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003612 if (!ElemType)
3613 return nullptr;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003614 return make<VectorType>(ElemType, /*Dimension=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003615}
3616
3617// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3618// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003619template <typename Derived, typename Alloc>
3620Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003621 if (!consumeIf('D'))
3622 return nullptr;
3623 if (!consumeIf('t') && !consumeIf('T'))
3624 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003625 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003626 if (E == nullptr)
3627 return nullptr;
3628 if (!consumeIf('E'))
3629 return nullptr;
3630 return make<EnclosingExpr>("decltype(", E, ")");
3631}
3632
3633// <array-type> ::= A <positive dimension number> _ <element type>
3634// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003635template <typename Derived, typename Alloc>
3636Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003637 if (!consumeIf('A'))
3638 return nullptr;
3639
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003640 Node *Dimension = nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003641
Richard Smithc20d1442018-08-20 20:14:49 +00003642 if (std::isdigit(look())) {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003643 Dimension = make<NameType>(parseNumber());
3644 if (!Dimension)
3645 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003646 if (!consumeIf('_'))
3647 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003648 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003649 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003650 if (DimExpr == nullptr)
3651 return nullptr;
3652 if (!consumeIf('_'))
3653 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003654 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003655 }
3656
Pavel Labathba825192018-10-16 14:29:14 +00003657 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003658 if (Ty == nullptr)
3659 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003660 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003661}
3662
3663// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003664template <typename Derived, typename Alloc>
3665Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003666 if (!consumeIf('M'))
3667 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003668 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003669 if (ClassType == nullptr)
3670 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003671 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003672 if (MemberType == nullptr)
3673 return nullptr;
3674 return make<PointerToMemberType>(ClassType, MemberType);
3675}
3676
3677// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3678// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3679// ::= Tu <name> # dependent elaborated type specifier using 'union'
3680// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003681template <typename Derived, typename Alloc>
3682Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003683 StringView ElabSpef;
3684 if (consumeIf("Ts"))
3685 ElabSpef = "struct";
3686 else if (consumeIf("Tu"))
3687 ElabSpef = "union";
3688 else if (consumeIf("Te"))
3689 ElabSpef = "enum";
3690
Pavel Labathba825192018-10-16 14:29:14 +00003691 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003692 if (Name == nullptr)
3693 return nullptr;
3694
3695 if (!ElabSpef.empty())
3696 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3697
3698 return Name;
3699}
3700
3701// <qualified-type> ::= <qualifiers> <type>
3702// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3703// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003704template <typename Derived, typename Alloc>
3705Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003706 if (consumeIf('U')) {
3707 StringView Qual = parseBareSourceName();
3708 if (Qual.empty())
3709 return nullptr;
3710
Richard Smithc20d1442018-08-20 20:14:49 +00003711 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3712 if (Qual.startsWith("objcproto")) {
3713 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3714 StringView Proto;
3715 {
3716 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3717 SaveLast(Last, ProtoSourceName.end());
3718 Proto = parseBareSourceName();
3719 }
3720 if (Proto.empty())
3721 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003722 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003723 if (Child == nullptr)
3724 return nullptr;
3725 return make<ObjCProtoName>(Child, Proto);
3726 }
3727
Alex Orlovf50df922021-03-24 10:21:32 +04003728 Node *TA = nullptr;
3729 if (look() == 'I') {
3730 TA = getDerived().parseTemplateArgs();
3731 if (TA == nullptr)
3732 return nullptr;
3733 }
3734
Pavel Labathba825192018-10-16 14:29:14 +00003735 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003736 if (Child == nullptr)
3737 return nullptr;
Alex Orlovf50df922021-03-24 10:21:32 +04003738 return make<VendorExtQualType>(Child, Qual, TA);
Richard Smithc20d1442018-08-20 20:14:49 +00003739 }
3740
3741 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003742 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003743 if (Ty == nullptr)
3744 return nullptr;
3745 if (Quals != QualNone)
3746 Ty = make<QualType>(Ty, Quals);
3747 return Ty;
3748}
3749
3750// <type> ::= <builtin-type>
3751// ::= <qualified-type>
3752// ::= <function-type>
3753// ::= <class-enum-type>
3754// ::= <array-type>
3755// ::= <pointer-to-member-type>
3756// ::= <template-param>
3757// ::= <template-template-param> <template-args>
3758// ::= <decltype>
3759// ::= P <type> # pointer
3760// ::= R <type> # l-value reference
3761// ::= O <type> # r-value reference (C++11)
3762// ::= C <type> # complex pair (C99)
3763// ::= G <type> # imaginary (C99)
3764// ::= <substitution> # See Compression below
3765// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3766// extension ::= <vector-type> # <vector-type> starts with Dv
3767//
3768// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3769// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003770template <typename Derived, typename Alloc>
3771Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003772 Node *Result = nullptr;
3773
Richard Smithc20d1442018-08-20 20:14:49 +00003774 switch (look()) {
3775 // ::= <qualified-type>
3776 case 'r':
3777 case 'V':
3778 case 'K': {
3779 unsigned AfterQuals = 0;
3780 if (look(AfterQuals) == 'r') ++AfterQuals;
3781 if (look(AfterQuals) == 'V') ++AfterQuals;
3782 if (look(AfterQuals) == 'K') ++AfterQuals;
3783
3784 if (look(AfterQuals) == 'F' ||
3785 (look(AfterQuals) == 'D' &&
3786 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3787 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003788 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003789 break;
3790 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003791 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003792 }
3793 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003794 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003795 break;
3796 }
3797 // <builtin-type> ::= v # void
3798 case 'v':
3799 ++First;
3800 return make<NameType>("void");
3801 // ::= w # wchar_t
3802 case 'w':
3803 ++First;
3804 return make<NameType>("wchar_t");
3805 // ::= b # bool
3806 case 'b':
3807 ++First;
3808 return make<NameType>("bool");
3809 // ::= c # char
3810 case 'c':
3811 ++First;
3812 return make<NameType>("char");
3813 // ::= a # signed char
3814 case 'a':
3815 ++First;
3816 return make<NameType>("signed char");
3817 // ::= h # unsigned char
3818 case 'h':
3819 ++First;
3820 return make<NameType>("unsigned char");
3821 // ::= s # short
3822 case 's':
3823 ++First;
3824 return make<NameType>("short");
3825 // ::= t # unsigned short
3826 case 't':
3827 ++First;
3828 return make<NameType>("unsigned short");
3829 // ::= i # int
3830 case 'i':
3831 ++First;
3832 return make<NameType>("int");
3833 // ::= j # unsigned int
3834 case 'j':
3835 ++First;
3836 return make<NameType>("unsigned int");
3837 // ::= l # long
3838 case 'l':
3839 ++First;
3840 return make<NameType>("long");
3841 // ::= m # unsigned long
3842 case 'm':
3843 ++First;
3844 return make<NameType>("unsigned long");
3845 // ::= x # long long, __int64
3846 case 'x':
3847 ++First;
3848 return make<NameType>("long long");
3849 // ::= y # unsigned long long, __int64
3850 case 'y':
3851 ++First;
3852 return make<NameType>("unsigned long long");
3853 // ::= n # __int128
3854 case 'n':
3855 ++First;
3856 return make<NameType>("__int128");
3857 // ::= o # unsigned __int128
3858 case 'o':
3859 ++First;
3860 return make<NameType>("unsigned __int128");
3861 // ::= f # float
3862 case 'f':
3863 ++First;
3864 return make<NameType>("float");
3865 // ::= d # double
3866 case 'd':
3867 ++First;
3868 return make<NameType>("double");
3869 // ::= e # long double, __float80
3870 case 'e':
3871 ++First;
3872 return make<NameType>("long double");
3873 // ::= g # __float128
3874 case 'g':
3875 ++First;
3876 return make<NameType>("__float128");
3877 // ::= z # ellipsis
3878 case 'z':
3879 ++First;
3880 return make<NameType>("...");
3881
3882 // <builtin-type> ::= u <source-name> # vendor extended type
3883 case 'u': {
3884 ++First;
3885 StringView Res = parseBareSourceName();
3886 if (Res.empty())
3887 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003888 // Typically, <builtin-type>s are not considered substitution candidates,
3889 // but the exception to that exception is vendor extended types (Itanium C++
3890 // ABI 5.9.1).
3891 Result = make<NameType>(Res);
3892 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003893 }
3894 case 'D':
3895 switch (look(1)) {
3896 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3897 case 'd':
3898 First += 2;
3899 return make<NameType>("decimal64");
3900 // ::= De # IEEE 754r decimal floating point (128 bits)
3901 case 'e':
3902 First += 2;
3903 return make<NameType>("decimal128");
3904 // ::= Df # IEEE 754r decimal floating point (32 bits)
3905 case 'f':
3906 First += 2;
3907 return make<NameType>("decimal32");
3908 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3909 case 'h':
3910 First += 2;
Stuart Bradye8bf5772021-06-07 16:30:22 +01003911 return make<NameType>("half");
Pengfei Wang50e90b82021-09-23 11:02:25 +08003912 // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits)
3913 case 'F': {
3914 First += 2;
3915 Node *DimensionNumber = make<NameType>(parseNumber());
3916 if (!DimensionNumber)
3917 return nullptr;
3918 if (!consumeIf('_'))
3919 return nullptr;
3920 return make<BinaryFPType>(DimensionNumber);
3921 }
Richard Smithc20d1442018-08-20 20:14:49 +00003922 // ::= Di # char32_t
3923 case 'i':
3924 First += 2;
3925 return make<NameType>("char32_t");
3926 // ::= Ds # char16_t
3927 case 's':
3928 First += 2;
3929 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003930 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3931 case 'u':
3932 First += 2;
3933 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003934 // ::= Da # auto (in dependent new-expressions)
3935 case 'a':
3936 First += 2;
3937 return make<NameType>("auto");
3938 // ::= Dc # decltype(auto)
3939 case 'c':
3940 First += 2;
3941 return make<NameType>("decltype(auto)");
3942 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3943 case 'n':
3944 First += 2;
3945 return make<NameType>("std::nullptr_t");
3946
3947 // ::= <decltype>
3948 case 't':
3949 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003950 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003951 break;
3952 }
3953 // extension ::= <vector-type> # <vector-type> starts with Dv
3954 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003955 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003956 break;
3957 }
3958 // ::= Dp <type> # pack expansion (C++0x)
3959 case 'p': {
3960 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003961 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003962 if (!Child)
3963 return nullptr;
3964 Result = make<ParameterPackExpansion>(Child);
3965 break;
3966 }
3967 // Exception specifier on a function type.
3968 case 'o':
3969 case 'O':
3970 case 'w':
3971 // Transaction safe function type.
3972 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003973 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003974 break;
3975 }
3976 break;
3977 // ::= <function-type>
3978 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003979 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003980 break;
3981 }
3982 // ::= <array-type>
3983 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003984 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003985 break;
3986 }
3987 // ::= <pointer-to-member-type>
3988 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003989 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003990 break;
3991 }
3992 // ::= <template-param>
3993 case 'T': {
3994 // This could be an elaborate type specifier on a <class-enum-type>.
3995 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003996 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003997 break;
3998 }
3999
Pavel Labathba825192018-10-16 14:29:14 +00004000 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004001 if (Result == nullptr)
4002 return nullptr;
4003
4004 // Result could be either of:
4005 // <type> ::= <template-param>
4006 // <type> ::= <template-template-param> <template-args>
4007 //
4008 // <template-template-param> ::= <template-param>
4009 // ::= <substitution>
4010 //
4011 // If this is followed by some <template-args>, and we're permitted to
4012 // parse them, take the second production.
4013
4014 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004015 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004016 if (TA == nullptr)
4017 return nullptr;
4018 Result = make<NameWithTemplateArgs>(Result, TA);
4019 }
4020 break;
4021 }
4022 // ::= P <type> # pointer
4023 case 'P': {
4024 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004025 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004026 if (Ptr == nullptr)
4027 return nullptr;
4028 Result = make<PointerType>(Ptr);
4029 break;
4030 }
4031 // ::= R <type> # l-value reference
4032 case 'R': {
4033 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004034 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004035 if (Ref == nullptr)
4036 return nullptr;
4037 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
4038 break;
4039 }
4040 // ::= O <type> # r-value reference (C++11)
4041 case 'O': {
4042 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004043 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004044 if (Ref == nullptr)
4045 return nullptr;
4046 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
4047 break;
4048 }
4049 // ::= C <type> # complex pair (C99)
4050 case 'C': {
4051 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004052 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004053 if (P == nullptr)
4054 return nullptr;
4055 Result = make<PostfixQualifiedType>(P, " complex");
4056 break;
4057 }
4058 // ::= G <type> # imaginary (C99)
4059 case 'G': {
4060 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004061 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004062 if (P == nullptr)
4063 return P;
4064 Result = make<PostfixQualifiedType>(P, " imaginary");
4065 break;
4066 }
4067 // ::= <substitution> # See Compression below
4068 case 'S': {
4069 if (look(1) && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00004070 Node *Sub = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00004071 if (Sub == nullptr)
4072 return nullptr;
4073
4074 // Sub could be either of:
4075 // <type> ::= <substitution>
4076 // <type> ::= <template-template-param> <template-args>
4077 //
4078 // <template-template-param> ::= <template-param>
4079 // ::= <substitution>
4080 //
4081 // If this is followed by some <template-args>, and we're permitted to
4082 // parse them, take the second production.
4083
4084 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004085 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004086 if (TA == nullptr)
4087 return nullptr;
4088 Result = make<NameWithTemplateArgs>(Sub, TA);
4089 break;
4090 }
4091
4092 // If all we parsed was a substitution, don't re-insert into the
4093 // substitution table.
4094 return Sub;
4095 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00004096 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004097 }
4098 // ::= <class-enum-type>
4099 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004100 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004101 break;
4102 }
4103 }
4104
4105 // If we parsed a type, insert it into the substitution table. Note that all
4106 // <builtin-type>s and <substitution>s have already bailed out, because they
4107 // don't get substitutions.
4108 if (Result != nullptr)
4109 Subs.push_back(Result);
4110 return Result;
4111}
4112
Pavel Labathba825192018-10-16 14:29:14 +00004113template <typename Derived, typename Alloc>
4114Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4115 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004116 if (E == nullptr)
4117 return nullptr;
4118 return make<PrefixExpr>(Kind, E);
4119}
4120
Pavel Labathba825192018-10-16 14:29:14 +00004121template <typename Derived, typename Alloc>
4122Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4123 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004124 if (LHS == nullptr)
4125 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004126 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004127 if (RHS == nullptr)
4128 return nullptr;
4129 return make<BinaryExpr>(LHS, Kind, RHS);
4130}
4131
Pavel Labathba825192018-10-16 14:29:14 +00004132template <typename Derived, typename Alloc>
4133Node *
4134AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004135 StringView Tmp = parseNumber(true);
4136 if (!Tmp.empty() && consumeIf('E'))
4137 return make<IntegerLiteral>(Lit, Tmp);
4138 return nullptr;
4139}
4140
4141// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004142template <typename Alloc, typename Derived>
4143Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004144 Qualifiers CVR = QualNone;
4145 if (consumeIf('r'))
4146 CVR |= QualRestrict;
4147 if (consumeIf('V'))
4148 CVR |= QualVolatile;
4149 if (consumeIf('K'))
4150 CVR |= QualConst;
4151 return CVR;
4152}
4153
4154// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4155// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4156// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4157// ::= 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 -04004158// ::= fpT # 'this' expression (not part of standard?)
Pavel Labathba825192018-10-16 14:29:14 +00004159template <typename Derived, typename Alloc>
4160Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Erik Pilkington91c24af2020-05-13 22:19:45 -04004161 if (consumeIf("fpT"))
4162 return make<NameType>("this");
Richard Smithc20d1442018-08-20 20:14:49 +00004163 if (consumeIf("fp")) {
4164 parseCVQualifiers();
4165 StringView Num = parseNumber();
4166 if (!consumeIf('_'))
4167 return nullptr;
4168 return make<FunctionParam>(Num);
4169 }
4170 if (consumeIf("fL")) {
4171 if (parseNumber().empty())
4172 return nullptr;
4173 if (!consumeIf('p'))
4174 return nullptr;
4175 parseCVQualifiers();
4176 StringView Num = parseNumber();
4177 if (!consumeIf('_'))
4178 return nullptr;
4179 return make<FunctionParam>(Num);
4180 }
4181 return nullptr;
4182}
4183
4184// [gs] nw <expression>* _ <type> E # new (expr-list) type
4185// [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4186// [gs] na <expression>* _ <type> E # new[] (expr-list) type
4187// [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4188// <initializer> ::= pi <expression>* E # parenthesized initialization
Pavel Labathba825192018-10-16 14:29:14 +00004189template <typename Derived, typename Alloc>
4190Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004191 bool Global = consumeIf("gs");
4192 bool IsArray = look(1) == 'a';
4193 if (!consumeIf("nw") && !consumeIf("na"))
4194 return nullptr;
4195 size_t Exprs = Names.size();
4196 while (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00004197 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004198 if (Ex == nullptr)
4199 return nullptr;
4200 Names.push_back(Ex);
4201 }
4202 NodeArray ExprList = popTrailingNodeArray(Exprs);
Pavel Labathba825192018-10-16 14:29:14 +00004203 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004204 if (Ty == nullptr)
4205 return Ty;
4206 if (consumeIf("pi")) {
4207 size_t InitsBegin = Names.size();
4208 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004209 Node *Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004210 if (Init == nullptr)
4211 return Init;
4212 Names.push_back(Init);
4213 }
4214 NodeArray Inits = popTrailingNodeArray(InitsBegin);
4215 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
4216 } else if (!consumeIf('E'))
4217 return nullptr;
4218 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
4219}
4220
4221// cv <type> <expression> # conversion with one argument
4222// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004223template <typename Derived, typename Alloc>
4224Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004225 if (!consumeIf("cv"))
4226 return nullptr;
4227 Node *Ty;
4228 {
4229 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004230 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004231 }
4232
4233 if (Ty == nullptr)
4234 return nullptr;
4235
4236 if (consumeIf('_')) {
4237 size_t ExprsBegin = Names.size();
4238 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004239 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004240 if (E == nullptr)
4241 return E;
4242 Names.push_back(E);
4243 }
4244 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4245 return make<ConversionExpr>(Ty, Exprs);
4246 }
4247
Pavel Labathba825192018-10-16 14:29:14 +00004248 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004249 if (E[0] == nullptr)
4250 return nullptr;
4251 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4252}
4253
4254// <expr-primary> ::= L <type> <value number> E # integer literal
4255// ::= L <type> <value float> E # floating literal
4256// ::= L <string type> E # string literal
4257// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004258// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004259// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4260// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004261template <typename Derived, typename Alloc>
4262Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004263 if (!consumeIf('L'))
4264 return nullptr;
4265 switch (look()) {
4266 case 'w':
4267 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004268 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004269 case 'b':
4270 if (consumeIf("b0E"))
4271 return make<BoolExpr>(0);
4272 if (consumeIf("b1E"))
4273 return make<BoolExpr>(1);
4274 return nullptr;
4275 case 'c':
4276 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004277 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004278 case 'a':
4279 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004280 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004281 case 'h':
4282 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004283 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004284 case 's':
4285 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004286 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004287 case 't':
4288 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004289 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004290 case 'i':
4291 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004292 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004293 case 'j':
4294 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004295 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004296 case 'l':
4297 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004298 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004299 case 'm':
4300 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004301 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004302 case 'x':
4303 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004304 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004305 case 'y':
4306 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004307 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004308 case 'n':
4309 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004310 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004311 case 'o':
4312 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004313 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004314 case 'f':
4315 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004316 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004317 case 'd':
4318 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004319 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004320 case 'e':
4321 ++First;
Xing Xue3dc5e082020-04-15 09:59:06 -04004322#if defined(__powerpc__) || defined(__s390__)
4323 // Handle cases where long doubles encoded with e have the same size
4324 // and representation as doubles.
4325 return getDerived().template parseFloatingLiteral<double>();
4326#else
Pavel Labathba825192018-10-16 14:29:14 +00004327 return getDerived().template parseFloatingLiteral<long double>();
Xing Xue3dc5e082020-04-15 09:59:06 -04004328#endif
Richard Smithc20d1442018-08-20 20:14:49 +00004329 case '_':
4330 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004331 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004332 if (R != nullptr && consumeIf('E'))
4333 return R;
4334 }
4335 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004336 case 'A': {
4337 Node *T = getDerived().parseType();
4338 if (T == nullptr)
4339 return nullptr;
4340 // FIXME: We need to include the string contents in the mangling.
4341 if (consumeIf('E'))
4342 return make<StringLiteral>(T);
4343 return nullptr;
4344 }
4345 case 'D':
4346 if (consumeIf("DnE"))
4347 return make<NameType>("nullptr");
4348 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004349 case 'T':
4350 // Invalid mangled name per
4351 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4352 return nullptr;
Richard Smithfb917462019-09-09 22:26:04 +00004353 case 'U': {
4354 // FIXME: Should we support LUb... for block literals?
4355 if (look(1) != 'l')
4356 return nullptr;
4357 Node *T = parseUnnamedTypeName(nullptr);
4358 if (!T || !consumeIf('E'))
4359 return nullptr;
4360 return make<LambdaExpr>(T);
4361 }
Richard Smithc20d1442018-08-20 20:14:49 +00004362 default: {
4363 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004364 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004365 if (T == nullptr)
4366 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004367 StringView N = parseNumber(/*AllowNegative=*/true);
Richard Smithfb917462019-09-09 22:26:04 +00004368 if (N.empty())
4369 return nullptr;
4370 if (!consumeIf('E'))
4371 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004372 return make<EnumLiteral>(T, N);
Richard Smithc20d1442018-08-20 20:14:49 +00004373 }
4374 }
4375}
4376
4377// <braced-expression> ::= <expression>
4378// ::= di <field source-name> <braced-expression> # .name = expr
4379// ::= dx <index expression> <braced-expression> # [expr] = expr
4380// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004381template <typename Derived, typename Alloc>
4382Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004383 if (look() == 'd') {
4384 switch (look(1)) {
4385 case 'i': {
4386 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004387 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004388 if (Field == nullptr)
4389 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004390 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004391 if (Init == nullptr)
4392 return nullptr;
4393 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4394 }
4395 case 'x': {
4396 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004397 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004398 if (Index == nullptr)
4399 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004400 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004401 if (Init == nullptr)
4402 return nullptr;
4403 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4404 }
4405 case 'X': {
4406 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004407 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004408 if (RangeBegin == nullptr)
4409 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004410 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004411 if (RangeEnd == nullptr)
4412 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004413 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004414 if (Init == nullptr)
4415 return nullptr;
4416 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4417 }
4418 }
4419 }
Pavel Labathba825192018-10-16 14:29:14 +00004420 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004421}
4422
4423// (not yet in the spec)
4424// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4425// ::= fR <binary-operator-name> <expression> <expression>
4426// ::= fl <binary-operator-name> <expression>
4427// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004428template <typename Derived, typename Alloc>
4429Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004430 if (!consumeIf('f'))
4431 return nullptr;
4432
4433 char FoldKind = look();
4434 bool IsLeftFold, HasInitializer;
4435 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4436 if (FoldKind == 'l' || FoldKind == 'L')
4437 IsLeftFold = true;
4438 else if (FoldKind == 'r' || FoldKind == 'R')
4439 IsLeftFold = false;
4440 else
4441 return nullptr;
4442 ++First;
4443
4444 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4445 StringView OperatorName;
4446 if (consumeIf("aa")) OperatorName = "&&";
4447 else if (consumeIf("an")) OperatorName = "&";
4448 else if (consumeIf("aN")) OperatorName = "&=";
4449 else if (consumeIf("aS")) OperatorName = "=";
4450 else if (consumeIf("cm")) OperatorName = ",";
4451 else if (consumeIf("ds")) OperatorName = ".*";
4452 else if (consumeIf("dv")) OperatorName = "/";
4453 else if (consumeIf("dV")) OperatorName = "/=";
4454 else if (consumeIf("eo")) OperatorName = "^";
4455 else if (consumeIf("eO")) OperatorName = "^=";
4456 else if (consumeIf("eq")) OperatorName = "==";
4457 else if (consumeIf("ge")) OperatorName = ">=";
4458 else if (consumeIf("gt")) OperatorName = ">";
4459 else if (consumeIf("le")) OperatorName = "<=";
4460 else if (consumeIf("ls")) OperatorName = "<<";
4461 else if (consumeIf("lS")) OperatorName = "<<=";
4462 else if (consumeIf("lt")) OperatorName = "<";
4463 else if (consumeIf("mi")) OperatorName = "-";
4464 else if (consumeIf("mI")) OperatorName = "-=";
4465 else if (consumeIf("ml")) OperatorName = "*";
4466 else if (consumeIf("mL")) OperatorName = "*=";
4467 else if (consumeIf("ne")) OperatorName = "!=";
4468 else if (consumeIf("oo")) OperatorName = "||";
4469 else if (consumeIf("or")) OperatorName = "|";
4470 else if (consumeIf("oR")) OperatorName = "|=";
4471 else if (consumeIf("pl")) OperatorName = "+";
4472 else if (consumeIf("pL")) OperatorName = "+=";
4473 else if (consumeIf("rm")) OperatorName = "%";
4474 else if (consumeIf("rM")) OperatorName = "%=";
4475 else if (consumeIf("rs")) OperatorName = ">>";
4476 else if (consumeIf("rS")) OperatorName = ">>=";
4477 else return nullptr;
4478
Pavel Labathba825192018-10-16 14:29:14 +00004479 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004480 if (Pack == nullptr)
4481 return nullptr;
4482 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004483 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004484 if (Init == nullptr)
4485 return nullptr;
4486 }
4487
4488 if (IsLeftFold && Init)
4489 std::swap(Pack, Init);
4490
4491 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4492}
4493
Richard Smith1865d2f2020-10-22 19:29:36 -07004494// <expression> ::= mc <parameter type> <expr> [<offset number>] E
4495//
4496// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4497template <typename Derived, typename Alloc>
4498Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() {
4499 Node *Ty = getDerived().parseType();
4500 if (!Ty)
4501 return nullptr;
4502 Node *Expr = getDerived().parseExpr();
4503 if (!Expr)
4504 return nullptr;
4505 StringView Offset = getDerived().parseNumber(true);
4506 if (!consumeIf('E'))
4507 return nullptr;
4508 return make<PointerToMemberConversionExpr>(Ty, Expr, Offset);
4509}
4510
4511// <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
4512// <union-selector> ::= _ [<number>]
4513//
4514// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4515template <typename Derived, typename Alloc>
4516Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
4517 Node *Ty = getDerived().parseType();
4518 if (!Ty)
4519 return nullptr;
4520 Node *Expr = getDerived().parseExpr();
4521 if (!Expr)
4522 return nullptr;
4523 StringView Offset = getDerived().parseNumber(true);
4524 size_t SelectorsBegin = Names.size();
4525 while (consumeIf('_')) {
4526 Node *Selector = make<NameType>(parseNumber());
4527 if (!Selector)
4528 return nullptr;
4529 Names.push_back(Selector);
4530 }
4531 bool OnePastTheEnd = consumeIf('p');
4532 if (!consumeIf('E'))
4533 return nullptr;
4534 return make<SubobjectExpr>(
4535 Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
4536}
4537
Richard Smithc20d1442018-08-20 20:14:49 +00004538// <expression> ::= <unary operator-name> <expression>
4539// ::= <binary operator-name> <expression> <expression>
4540// ::= <ternary operator-name> <expression> <expression> <expression>
4541// ::= cl <expression>+ E # call
4542// ::= cv <type> <expression> # conversion with one argument
4543// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4544// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4545// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4546// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4547// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4548// ::= [gs] dl <expression> # delete expression
4549// ::= [gs] da <expression> # delete[] expression
4550// ::= pp_ <expression> # prefix ++
4551// ::= mm_ <expression> # prefix --
4552// ::= ti <type> # typeid (type)
4553// ::= te <expression> # typeid (expression)
4554// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4555// ::= sc <type> <expression> # static_cast<type> (expression)
4556// ::= cc <type> <expression> # const_cast<type> (expression)
4557// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4558// ::= st <type> # sizeof (a type)
4559// ::= sz <expression> # sizeof (an expression)
4560// ::= at <type> # alignof (a type)
4561// ::= az <expression> # alignof (an expression)
4562// ::= nx <expression> # noexcept (expression)
4563// ::= <template-param>
4564// ::= <function-param>
4565// ::= dt <expression> <unresolved-name> # expr.name
4566// ::= pt <expression> <unresolved-name> # expr->name
4567// ::= ds <expression> <expression> # expr.*expr
4568// ::= sZ <template-param> # size of a parameter pack
4569// ::= sZ <function-param> # size of a function parameter pack
4570// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4571// ::= sp <expression> # pack expansion
4572// ::= tw <expression> # throw expression
4573// ::= tr # throw with no operand (rethrow)
4574// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4575// # freestanding dependent name (e.g., T::x),
4576// # objectless nonstatic member reference
4577// ::= fL <binary-operator-name> <expression> <expression>
4578// ::= fR <binary-operator-name> <expression> <expression>
4579// ::= fl <binary-operator-name> <expression>
4580// ::= fr <binary-operator-name> <expression>
4581// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004582template <typename Derived, typename Alloc>
4583Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004584 bool Global = consumeIf("gs");
4585 if (numLeft() < 2)
4586 return nullptr;
4587
4588 switch (*First) {
4589 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004590 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004591 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004592 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004593 case 'f': {
4594 // Disambiguate a fold expression from a <function-param>.
4595 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004596 return getDerived().parseFunctionParam();
4597 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004598 }
4599 case 'a':
4600 switch (First[1]) {
4601 case 'a':
4602 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004603 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004604 case 'd':
4605 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004606 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004607 case 'n':
4608 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004609 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004610 case 'N':
4611 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004612 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004613 case 'S':
4614 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004615 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004616 case 't': {
4617 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004618 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004619 if (Ty == nullptr)
4620 return nullptr;
4621 return make<EnclosingExpr>("alignof (", Ty, ")");
4622 }
4623 case 'z': {
4624 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004625 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004626 if (Ty == nullptr)
4627 return nullptr;
4628 return make<EnclosingExpr>("alignof (", Ty, ")");
4629 }
4630 }
4631 return nullptr;
4632 case 'c':
4633 switch (First[1]) {
4634 // cc <type> <expression> # const_cast<type>(expression)
4635 case 'c': {
4636 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004637 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004638 if (Ty == nullptr)
4639 return Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004640 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004641 if (Ex == nullptr)
4642 return Ex;
4643 return make<CastExpr>("const_cast", Ty, Ex);
4644 }
4645 // cl <expression>+ E # call
4646 case 'l': {
4647 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004648 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004649 if (Callee == nullptr)
4650 return Callee;
4651 size_t ExprsBegin = Names.size();
4652 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004653 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004654 if (E == nullptr)
4655 return E;
4656 Names.push_back(E);
4657 }
4658 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4659 }
4660 case 'm':
4661 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004662 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004663 case 'o':
4664 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004665 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004666 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004667 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004668 }
4669 return nullptr;
4670 case 'd':
4671 switch (First[1]) {
4672 case 'a': {
4673 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004674 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004675 if (Ex == nullptr)
4676 return Ex;
4677 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4678 }
4679 case 'c': {
4680 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004681 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004682 if (T == nullptr)
4683 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004684 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004685 if (Ex == nullptr)
4686 return Ex;
4687 return make<CastExpr>("dynamic_cast", T, Ex);
4688 }
4689 case 'e':
4690 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004691 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004692 case 'l': {
4693 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004694 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004695 if (E == nullptr)
4696 return E;
4697 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4698 }
4699 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004700 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004701 case 's': {
4702 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004703 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004704 if (LHS == nullptr)
4705 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004706 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004707 if (RHS == nullptr)
4708 return nullptr;
4709 return make<MemberExpr>(LHS, ".*", RHS);
4710 }
4711 case 't': {
4712 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004713 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004714 if (LHS == nullptr)
4715 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004716 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004717 if (RHS == nullptr)
4718 return nullptr;
4719 return make<MemberExpr>(LHS, ".", RHS);
4720 }
4721 case 'v':
4722 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004723 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004724 case 'V':
4725 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004726 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004727 }
4728 return nullptr;
4729 case 'e':
4730 switch (First[1]) {
4731 case 'o':
4732 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004733 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004734 case 'O':
4735 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004736 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004737 case 'q':
4738 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004739 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004740 }
4741 return nullptr;
4742 case 'g':
4743 switch (First[1]) {
4744 case 'e':
4745 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004746 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004747 case 't':
4748 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004749 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004750 }
4751 return nullptr;
4752 case 'i':
4753 switch (First[1]) {
4754 case 'x': {
4755 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004756 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004757 if (Base == nullptr)
4758 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004759 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004760 if (Index == nullptr)
4761 return Index;
4762 return make<ArraySubscriptExpr>(Base, Index);
4763 }
4764 case 'l': {
4765 First += 2;
4766 size_t InitsBegin = Names.size();
4767 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004768 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004769 if (E == nullptr)
4770 return nullptr;
4771 Names.push_back(E);
4772 }
4773 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4774 }
4775 }
4776 return nullptr;
4777 case 'l':
4778 switch (First[1]) {
4779 case 'e':
4780 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004781 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004782 case 's':
4783 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004784 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004785 case 'S':
4786 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004787 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004788 case 't':
4789 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004790 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004791 }
4792 return nullptr;
4793 case 'm':
4794 switch (First[1]) {
Richard Smith1865d2f2020-10-22 19:29:36 -07004795 case 'c':
4796 First += 2;
4797 return parsePointerToMemberConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004798 case 'i':
4799 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004800 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004801 case 'I':
4802 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004803 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004804 case 'l':
4805 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004806 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004807 case 'L':
4808 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004809 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004810 case 'm':
4811 First += 2;
4812 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004813 return getDerived().parsePrefixExpr("--");
4814 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004815 if (Ex == nullptr)
4816 return nullptr;
4817 return make<PostfixExpr>(Ex, "--");
4818 }
4819 return nullptr;
4820 case 'n':
4821 switch (First[1]) {
4822 case 'a':
4823 case 'w':
Pavel Labathba825192018-10-16 14:29:14 +00004824 return getDerived().parseNewExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004825 case 'e':
4826 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004827 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004828 case 'g':
4829 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004830 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004831 case 't':
4832 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004833 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004834 case 'x':
4835 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004836 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004837 if (Ex == nullptr)
4838 return Ex;
4839 return make<EnclosingExpr>("noexcept (", Ex, ")");
4840 }
4841 return nullptr;
4842 case 'o':
4843 switch (First[1]) {
4844 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004845 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004846 case 'o':
4847 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004848 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004849 case 'r':
4850 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004851 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004852 case 'R':
4853 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004854 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004855 }
4856 return nullptr;
4857 case 'p':
4858 switch (First[1]) {
4859 case 'm':
4860 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004861 return getDerived().parseBinaryExpr("->*");
Richard Smithc20d1442018-08-20 20:14:49 +00004862 case 'l':
4863 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004864 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004865 case 'L':
4866 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004867 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004868 case 'p': {
4869 First += 2;
4870 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004871 return getDerived().parsePrefixExpr("++");
4872 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004873 if (Ex == nullptr)
4874 return Ex;
4875 return make<PostfixExpr>(Ex, "++");
4876 }
4877 case 's':
4878 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004879 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004880 case 't': {
4881 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004882 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004883 if (L == nullptr)
4884 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004885 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004886 if (R == nullptr)
4887 return nullptr;
4888 return make<MemberExpr>(L, "->", R);
4889 }
4890 }
4891 return nullptr;
4892 case 'q':
4893 if (First[1] == 'u') {
4894 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004895 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004896 if (Cond == nullptr)
4897 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004898 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004899 if (LHS == nullptr)
4900 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004901 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004902 if (RHS == nullptr)
4903 return nullptr;
4904 return make<ConditionalExpr>(Cond, LHS, RHS);
4905 }
4906 return nullptr;
4907 case 'r':
4908 switch (First[1]) {
4909 case 'c': {
4910 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004911 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004912 if (T == nullptr)
4913 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004914 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004915 if (Ex == nullptr)
4916 return Ex;
4917 return make<CastExpr>("reinterpret_cast", T, Ex);
4918 }
4919 case 'm':
4920 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004921 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004922 case 'M':
4923 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004924 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004925 case 's':
4926 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004927 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004928 case 'S':
4929 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004930 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004931 }
4932 return nullptr;
4933 case 's':
4934 switch (First[1]) {
4935 case 'c': {
4936 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004937 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004938 if (T == nullptr)
4939 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004940 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004941 if (Ex == nullptr)
4942 return Ex;
4943 return make<CastExpr>("static_cast", T, Ex);
4944 }
Richard Smith1865d2f2020-10-22 19:29:36 -07004945 case 'o':
4946 First += 2;
4947 return parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004948 case 'p': {
4949 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004950 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004951 if (Child == nullptr)
4952 return nullptr;
4953 return make<ParameterPackExpansion>(Child);
4954 }
4955 case 'r':
Pavel Labathba825192018-10-16 14:29:14 +00004956 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004957 case 't': {
4958 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004959 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004960 if (Ty == nullptr)
4961 return Ty;
4962 return make<EnclosingExpr>("sizeof (", Ty, ")");
4963 }
4964 case 'z': {
4965 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004966 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004967 if (Ex == nullptr)
4968 return Ex;
4969 return make<EnclosingExpr>("sizeof (", Ex, ")");
4970 }
4971 case 'Z':
4972 First += 2;
4973 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004974 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004975 if (R == nullptr)
4976 return nullptr;
4977 return make<SizeofParamPackExpr>(R);
4978 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004979 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004980 if (FP == nullptr)
4981 return nullptr;
4982 return make<EnclosingExpr>("sizeof... (", FP, ")");
4983 }
4984 return nullptr;
4985 case 'P': {
4986 First += 2;
4987 size_t ArgsBegin = Names.size();
4988 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004989 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004990 if (Arg == nullptr)
4991 return nullptr;
4992 Names.push_back(Arg);
4993 }
Richard Smithb485b352018-08-24 23:30:26 +00004994 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4995 if (!Pack)
4996 return nullptr;
4997 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00004998 }
4999 }
5000 return nullptr;
5001 case 't':
5002 switch (First[1]) {
5003 case 'e': {
5004 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005005 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005006 if (Ex == nullptr)
5007 return Ex;
5008 return make<EnclosingExpr>("typeid (", Ex, ")");
5009 }
5010 case 'i': {
5011 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005012 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005013 if (Ty == nullptr)
5014 return Ty;
5015 return make<EnclosingExpr>("typeid (", Ty, ")");
5016 }
5017 case 'l': {
5018 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005019 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005020 if (Ty == nullptr)
5021 return nullptr;
5022 size_t InitsBegin = Names.size();
5023 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005024 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005025 if (E == nullptr)
5026 return nullptr;
5027 Names.push_back(E);
5028 }
5029 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
5030 }
5031 case 'r':
5032 First += 2;
5033 return make<NameType>("throw");
5034 case 'w': {
5035 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005036 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005037 if (Ex == nullptr)
5038 return nullptr;
5039 return make<ThrowExpr>(Ex);
5040 }
5041 }
5042 return nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05005043 case 'u': {
5044 ++First;
5045 Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
5046 if (!Name)
5047 return nullptr;
5048 // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
5049 // standard encoding expects a <template-arg>, and would be otherwise be
5050 // interpreted as <type> node 'short' or 'ellipsis'. However, neither
5051 // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
5052 // actual conflict here.
5053 if (Name->getBaseName() == "__uuidof") {
5054 if (numLeft() < 2)
5055 return nullptr;
5056 if (*First == 't') {
5057 ++First;
5058 Node *Ty = getDerived().parseType();
5059 if (!Ty)
5060 return nullptr;
5061 return make<CallExpr>(Name, makeNodeArray(&Ty, &Ty + 1));
5062 }
5063 if (*First == 'z') {
5064 ++First;
5065 Node *Ex = getDerived().parseExpr();
5066 if (!Ex)
5067 return nullptr;
5068 return make<CallExpr>(Name, makeNodeArray(&Ex, &Ex + 1));
5069 }
5070 }
5071 size_t ExprsBegin = Names.size();
5072 while (!consumeIf('E')) {
5073 Node *E = getDerived().parseTemplateArg();
5074 if (E == nullptr)
5075 return E;
5076 Names.push_back(E);
5077 }
5078 return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin));
5079 }
Richard Smithc20d1442018-08-20 20:14:49 +00005080 case '1':
5081 case '2':
5082 case '3':
5083 case '4':
5084 case '5':
5085 case '6':
5086 case '7':
5087 case '8':
5088 case '9':
Pavel Labathba825192018-10-16 14:29:14 +00005089 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00005090 }
5091 return nullptr;
5092}
5093
5094// <call-offset> ::= h <nv-offset> _
5095// ::= v <v-offset> _
5096//
5097// <nv-offset> ::= <offset number>
5098// # non-virtual base override
5099//
5100// <v-offset> ::= <offset number> _ <virtual offset number>
5101// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00005102template <typename Alloc, typename Derived>
5103bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00005104 // Just scan through the call offset, we never add this information into the
5105 // output.
5106 if (consumeIf('h'))
5107 return parseNumber(true).empty() || !consumeIf('_');
5108 if (consumeIf('v'))
5109 return parseNumber(true).empty() || !consumeIf('_') ||
5110 parseNumber(true).empty() || !consumeIf('_');
5111 return true;
5112}
5113
5114// <special-name> ::= TV <type> # virtual table
5115// ::= TT <type> # VTT structure (construction vtable index)
5116// ::= TI <type> # typeinfo structure
5117// ::= TS <type> # typeinfo name (null-terminated byte string)
5118// ::= Tc <call-offset> <call-offset> <base encoding>
5119// # base is the nominal target function of thunk
5120// # first call-offset is 'this' adjustment
5121// # second call-offset is result adjustment
5122// ::= T <call-offset> <base encoding>
5123// # base is the nominal target function of thunk
5124// ::= GV <object name> # Guard variable for one-time initialization
5125// # No <type>
5126// ::= TW <object name> # Thread-local wrapper
5127// ::= TH <object name> # Thread-local initialization
5128// ::= GR <object name> _ # First temporary
5129// ::= GR <object name> <seq-id> _ # Subsequent temporaries
5130// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
5131// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00005132template <typename Derived, typename Alloc>
5133Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00005134 switch (look()) {
5135 case 'T':
5136 switch (look(1)) {
Richard Smith1865d2f2020-10-22 19:29:36 -07005137 // TA <template-arg> # template parameter object
5138 //
5139 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
5140 case 'A': {
5141 First += 2;
5142 Node *Arg = getDerived().parseTemplateArg();
5143 if (Arg == nullptr)
5144 return nullptr;
5145 return make<SpecialName>("template parameter object for ", Arg);
5146 }
Richard Smithc20d1442018-08-20 20:14:49 +00005147 // TV <type> # virtual table
5148 case 'V': {
5149 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005150 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005151 if (Ty == nullptr)
5152 return nullptr;
5153 return make<SpecialName>("vtable for ", Ty);
5154 }
5155 // TT <type> # VTT structure (construction vtable index)
5156 case 'T': {
5157 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005158 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005159 if (Ty == nullptr)
5160 return nullptr;
5161 return make<SpecialName>("VTT for ", Ty);
5162 }
5163 // TI <type> # typeinfo structure
5164 case 'I': {
5165 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005166 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005167 if (Ty == nullptr)
5168 return nullptr;
5169 return make<SpecialName>("typeinfo for ", Ty);
5170 }
5171 // TS <type> # typeinfo name (null-terminated byte string)
5172 case 'S': {
5173 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005174 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005175 if (Ty == nullptr)
5176 return nullptr;
5177 return make<SpecialName>("typeinfo name for ", Ty);
5178 }
5179 // Tc <call-offset> <call-offset> <base encoding>
5180 case 'c': {
5181 First += 2;
5182 if (parseCallOffset() || parseCallOffset())
5183 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005184 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005185 if (Encoding == nullptr)
5186 return nullptr;
5187 return make<SpecialName>("covariant return thunk to ", Encoding);
5188 }
5189 // extension ::= TC <first type> <number> _ <second type>
5190 // # construction vtable for second-in-first
5191 case 'C': {
5192 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005193 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005194 if (FirstType == nullptr)
5195 return nullptr;
5196 if (parseNumber(true).empty() || !consumeIf('_'))
5197 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005198 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005199 if (SecondType == nullptr)
5200 return nullptr;
5201 return make<CtorVtableSpecialName>(SecondType, FirstType);
5202 }
5203 // TW <object name> # Thread-local wrapper
5204 case 'W': {
5205 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005206 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005207 if (Name == nullptr)
5208 return nullptr;
5209 return make<SpecialName>("thread-local wrapper routine for ", Name);
5210 }
5211 // TH <object name> # Thread-local initialization
5212 case 'H': {
5213 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005214 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005215 if (Name == nullptr)
5216 return nullptr;
5217 return make<SpecialName>("thread-local initialization routine for ", Name);
5218 }
5219 // T <call-offset> <base encoding>
5220 default: {
5221 ++First;
5222 bool IsVirt = look() == 'v';
5223 if (parseCallOffset())
5224 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005225 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005226 if (BaseEncoding == nullptr)
5227 return nullptr;
5228 if (IsVirt)
5229 return make<SpecialName>("virtual thunk to ", BaseEncoding);
5230 else
5231 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
5232 }
5233 }
5234 case 'G':
5235 switch (look(1)) {
5236 // GV <object name> # Guard variable for one-time initialization
5237 case 'V': {
5238 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005239 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005240 if (Name == nullptr)
5241 return nullptr;
5242 return make<SpecialName>("guard variable for ", Name);
5243 }
5244 // GR <object name> # reference temporary for object
5245 // GR <object name> _ # First temporary
5246 // GR <object name> <seq-id> _ # Subsequent temporaries
5247 case 'R': {
5248 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005249 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005250 if (Name == nullptr)
5251 return nullptr;
5252 size_t Count;
5253 bool ParsedSeqId = !parseSeqId(&Count);
5254 if (!consumeIf('_') && ParsedSeqId)
5255 return nullptr;
5256 return make<SpecialName>("reference temporary for ", Name);
5257 }
5258 }
5259 }
5260 return nullptr;
5261}
5262
5263// <encoding> ::= <function name> <bare-function-type>
5264// ::= <data name>
5265// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00005266template <typename Derived, typename Alloc>
5267Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithfac39712020-07-09 21:08:39 -07005268 // The template parameters of an encoding are unrelated to those of the
5269 // enclosing context.
5270 class SaveTemplateParams {
5271 AbstractManglingParser *Parser;
5272 decltype(TemplateParams) OldParams;
Justin Lebar2c536232021-06-09 16:57:22 -07005273 decltype(OuterTemplateParams) OldOuterParams;
Richard Smithfac39712020-07-09 21:08:39 -07005274
5275 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04005276 SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
Richard Smithfac39712020-07-09 21:08:39 -07005277 OldParams = std::move(Parser->TemplateParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005278 OldOuterParams = std::move(Parser->OuterTemplateParams);
Richard Smithfac39712020-07-09 21:08:39 -07005279 Parser->TemplateParams.clear();
Justin Lebar2c536232021-06-09 16:57:22 -07005280 Parser->OuterTemplateParams.clear();
Richard Smithfac39712020-07-09 21:08:39 -07005281 }
5282 ~SaveTemplateParams() {
5283 Parser->TemplateParams = std::move(OldParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005284 Parser->OuterTemplateParams = std::move(OldOuterParams);
Richard Smithfac39712020-07-09 21:08:39 -07005285 }
5286 } SaveTemplateParams(this);
Richard Smithfd434322020-07-09 20:36:04 -07005287
Richard Smithc20d1442018-08-20 20:14:49 +00005288 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00005289 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00005290
5291 auto IsEndOfEncoding = [&] {
5292 // The set of chars that can potentially follow an <encoding> (none of which
5293 // can start a <type>). Enumerating these allows us to avoid speculative
5294 // parsing.
5295 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5296 };
5297
5298 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00005299 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00005300 if (Name == nullptr)
5301 return nullptr;
5302
5303 if (resolveForwardTemplateRefs(NameInfo))
5304 return nullptr;
5305
5306 if (IsEndOfEncoding())
5307 return Name;
5308
5309 Node *Attrs = nullptr;
5310 if (consumeIf("Ua9enable_ifI")) {
5311 size_t BeforeArgs = Names.size();
5312 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005313 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005314 if (Arg == nullptr)
5315 return nullptr;
5316 Names.push_back(Arg);
5317 }
5318 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00005319 if (!Attrs)
5320 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005321 }
5322
5323 Node *ReturnType = nullptr;
5324 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00005325 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005326 if (ReturnType == nullptr)
5327 return nullptr;
5328 }
5329
5330 if (consumeIf('v'))
5331 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5332 Attrs, NameInfo.CVQualifiers,
5333 NameInfo.ReferenceQualifier);
5334
5335 size_t ParamsBegin = Names.size();
5336 do {
Pavel Labathba825192018-10-16 14:29:14 +00005337 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005338 if (Ty == nullptr)
5339 return nullptr;
5340 Names.push_back(Ty);
5341 } while (!IsEndOfEncoding());
5342
5343 return make<FunctionEncoding>(ReturnType, Name,
5344 popTrailingNodeArray(ParamsBegin),
5345 Attrs, NameInfo.CVQualifiers,
5346 NameInfo.ReferenceQualifier);
5347}
5348
5349template <class Float>
5350struct FloatData;
5351
5352template <>
5353struct FloatData<float>
5354{
5355 static const size_t mangled_size = 8;
5356 static const size_t max_demangled_size = 24;
5357 static constexpr const char* spec = "%af";
5358};
5359
5360template <>
5361struct FloatData<double>
5362{
5363 static const size_t mangled_size = 16;
5364 static const size_t max_demangled_size = 32;
5365 static constexpr const char* spec = "%a";
5366};
5367
5368template <>
5369struct FloatData<long double>
5370{
5371#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5372 defined(__wasm__)
5373 static const size_t mangled_size = 32;
5374#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5375 static const size_t mangled_size = 16;
5376#else
5377 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5378#endif
Elliott Hughes5a360ea2020-04-10 17:42:00 -07005379 // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
5380 // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
5381 // Negatives are one character longer than positives.
5382 // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
5383 // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
5384 static const size_t max_demangled_size = 42;
Richard Smithc20d1442018-08-20 20:14:49 +00005385 static constexpr const char *spec = "%LaL";
5386};
5387
Pavel Labathba825192018-10-16 14:29:14 +00005388template <typename Alloc, typename Derived>
5389template <class Float>
5390Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005391 const size_t N = FloatData<Float>::mangled_size;
5392 if (numLeft() <= N)
5393 return nullptr;
5394 StringView Data(First, First + N);
5395 for (char C : Data)
5396 if (!std::isxdigit(C))
5397 return nullptr;
5398 First += N;
5399 if (!consumeIf('E'))
5400 return nullptr;
5401 return make<FloatLiteralImpl<Float>>(Data);
5402}
5403
5404// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005405template <typename Alloc, typename Derived>
5406bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005407 if (!(look() >= '0' && look() <= '9') &&
5408 !(look() >= 'A' && look() <= 'Z'))
5409 return true;
5410
5411 size_t Id = 0;
5412 while (true) {
5413 if (look() >= '0' && look() <= '9') {
5414 Id *= 36;
5415 Id += static_cast<size_t>(look() - '0');
5416 } else if (look() >= 'A' && look() <= 'Z') {
5417 Id *= 36;
5418 Id += static_cast<size_t>(look() - 'A') + 10;
5419 } else {
5420 *Out = Id;
5421 return false;
5422 }
5423 ++First;
5424 }
5425}
5426
5427// <substitution> ::= S <seq-id> _
5428// ::= S_
5429// <substitution> ::= Sa # ::std::allocator
5430// <substitution> ::= Sb # ::std::basic_string
5431// <substitution> ::= Ss # ::std::basic_string < char,
5432// ::std::char_traits<char>,
5433// ::std::allocator<char> >
5434// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5435// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5436// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Pavel Labathba825192018-10-16 14:29:14 +00005437template <typename Derived, typename Alloc>
5438Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005439 if (!consumeIf('S'))
5440 return nullptr;
5441
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08005442 if (look() >= 'a' && look() <= 'z') {
Richard Smithc20d1442018-08-20 20:14:49 +00005443 Node *SpecialSub;
5444 switch (look()) {
5445 case 'a':
5446 ++First;
5447 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::allocator);
5448 break;
5449 case 'b':
5450 ++First;
5451 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::basic_string);
5452 break;
5453 case 's':
5454 ++First;
5455 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::string);
5456 break;
5457 case 'i':
5458 ++First;
5459 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::istream);
5460 break;
5461 case 'o':
5462 ++First;
5463 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::ostream);
5464 break;
5465 case 'd':
5466 ++First;
5467 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::iostream);
5468 break;
5469 default:
5470 return nullptr;
5471 }
Richard Smithb485b352018-08-24 23:30:26 +00005472 if (!SpecialSub)
5473 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005474 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5475 // has ABI tags, the tags are appended to the substitution; the result is a
5476 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005477 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005478 if (WithTags != SpecialSub) {
5479 Subs.push_back(WithTags);
5480 SpecialSub = WithTags;
5481 }
5482 return SpecialSub;
5483 }
5484
5485 // ::= S_
5486 if (consumeIf('_')) {
5487 if (Subs.empty())
5488 return nullptr;
5489 return Subs[0];
5490 }
5491
5492 // ::= S <seq-id> _
5493 size_t Index = 0;
5494 if (parseSeqId(&Index))
5495 return nullptr;
5496 ++Index;
5497 if (!consumeIf('_') || Index >= Subs.size())
5498 return nullptr;
5499 return Subs[Index];
5500}
5501
5502// <template-param> ::= T_ # first template parameter
5503// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005504// ::= TL <level-1> __
5505// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005506template <typename Derived, typename Alloc>
5507Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005508 if (!consumeIf('T'))
5509 return nullptr;
5510
Richard Smithdf1c14c2019-09-06 23:53:21 +00005511 size_t Level = 0;
5512 if (consumeIf('L')) {
5513 if (parsePositiveInteger(&Level))
5514 return nullptr;
5515 ++Level;
5516 if (!consumeIf('_'))
5517 return nullptr;
5518 }
5519
Richard Smithc20d1442018-08-20 20:14:49 +00005520 size_t Index = 0;
5521 if (!consumeIf('_')) {
5522 if (parsePositiveInteger(&Index))
5523 return nullptr;
5524 ++Index;
5525 if (!consumeIf('_'))
5526 return nullptr;
5527 }
5528
Richard Smithc20d1442018-08-20 20:14:49 +00005529 // If we're in a context where this <template-param> refers to a
5530 // <template-arg> further ahead in the mangled name (currently just conversion
5531 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005532 // This can only happen at the outermost level.
5533 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005534 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5535 if (!ForwardRef)
5536 return nullptr;
5537 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5538 ForwardTemplateRefs.push_back(
5539 static_cast<ForwardTemplateReference *>(ForwardRef));
5540 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005541 }
5542
Richard Smithdf1c14c2019-09-06 23:53:21 +00005543 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5544 Index >= TemplateParams[Level]->size()) {
5545 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5546 // list are mangled as the corresponding artificial template type parameter.
5547 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5548 // This will be popped by the ScopedTemplateParamList in
5549 // parseUnnamedTypeName.
5550 if (Level == TemplateParams.size())
5551 TemplateParams.push_back(nullptr);
5552 return make<NameType>("auto");
5553 }
5554
Richard Smithc20d1442018-08-20 20:14:49 +00005555 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005556 }
5557
5558 return (*TemplateParams[Level])[Index];
5559}
5560
5561// <template-param-decl> ::= Ty # type parameter
5562// ::= Tn <type> # non-type parameter
5563// ::= Tt <template-param-decl>* E # template parameter
5564// ::= Tp <template-param-decl> # parameter pack
5565template <typename Derived, typename Alloc>
5566Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5567 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5568 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5569 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5570 if (N) TemplateParams.back()->push_back(N);
5571 return N;
5572 };
5573
5574 if (consumeIf("Ty")) {
5575 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5576 if (!Name)
5577 return nullptr;
5578 return make<TypeTemplateParamDecl>(Name);
5579 }
5580
5581 if (consumeIf("Tn")) {
5582 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5583 if (!Name)
5584 return nullptr;
5585 Node *Type = parseType();
5586 if (!Type)
5587 return nullptr;
5588 return make<NonTypeTemplateParamDecl>(Name, Type);
5589 }
5590
5591 if (consumeIf("Tt")) {
5592 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5593 if (!Name)
5594 return nullptr;
5595 size_t ParamsBegin = Names.size();
5596 ScopedTemplateParamList TemplateTemplateParamParams(this);
5597 while (!consumeIf("E")) {
5598 Node *P = parseTemplateParamDecl();
5599 if (!P)
5600 return nullptr;
5601 Names.push_back(P);
5602 }
5603 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5604 return make<TemplateTemplateParamDecl>(Name, Params);
5605 }
5606
5607 if (consumeIf("Tp")) {
5608 Node *P = parseTemplateParamDecl();
5609 if (!P)
5610 return nullptr;
5611 return make<TemplateParamPackDecl>(P);
5612 }
5613
5614 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005615}
5616
5617// <template-arg> ::= <type> # type or template
5618// ::= X <expression> E # expression
5619// ::= <expr-primary> # simple expressions
5620// ::= J <template-arg>* E # argument pack
5621// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005622template <typename Derived, typename Alloc>
5623Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005624 switch (look()) {
5625 case 'X': {
5626 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005627 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005628 if (Arg == nullptr || !consumeIf('E'))
5629 return nullptr;
5630 return Arg;
5631 }
5632 case 'J': {
5633 ++First;
5634 size_t ArgsBegin = Names.size();
5635 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005636 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005637 if (Arg == nullptr)
5638 return nullptr;
5639 Names.push_back(Arg);
5640 }
5641 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5642 return make<TemplateArgumentPack>(Args);
5643 }
5644 case 'L': {
5645 // ::= LZ <encoding> E # extension
5646 if (look(1) == 'Z') {
5647 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005648 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005649 if (Arg == nullptr || !consumeIf('E'))
5650 return nullptr;
5651 return Arg;
5652 }
5653 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005654 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005655 }
5656 default:
Pavel Labathba825192018-10-16 14:29:14 +00005657 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005658 }
5659}
5660
5661// <template-args> ::= I <template-arg>* E
5662// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005663template <typename Derived, typename Alloc>
5664Node *
5665AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005666 if (!consumeIf('I'))
5667 return nullptr;
5668
5669 // <template-params> refer to the innermost <template-args>. Clear out any
5670 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005671 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005672 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005673 TemplateParams.push_back(&OuterTemplateParams);
5674 OuterTemplateParams.clear();
5675 }
Richard Smithc20d1442018-08-20 20:14:49 +00005676
5677 size_t ArgsBegin = Names.size();
5678 while (!consumeIf('E')) {
5679 if (TagTemplates) {
5680 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005681 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005682 TemplateParams = std::move(OldParams);
5683 if (Arg == nullptr)
5684 return nullptr;
5685 Names.push_back(Arg);
5686 Node *TableEntry = Arg;
5687 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5688 TableEntry = make<ParameterPack>(
5689 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005690 if (!TableEntry)
5691 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005692 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005693 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005694 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005695 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005696 if (Arg == nullptr)
5697 return nullptr;
5698 Names.push_back(Arg);
5699 }
5700 }
5701 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5702}
5703
5704// <mangled-name> ::= _Z <encoding>
5705// ::= <type>
5706// extension ::= ___Z <encoding> _block_invoke
5707// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5708// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005709template <typename Derived, typename Alloc>
5710Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005711 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005712 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005713 if (Encoding == nullptr)
5714 return nullptr;
5715 if (look() == '.') {
5716 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5717 First = Last;
5718 }
5719 if (numLeft() != 0)
5720 return nullptr;
5721 return Encoding;
5722 }
5723
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005724 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005725 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005726 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5727 return nullptr;
5728 bool RequireNumber = consumeIf('_');
5729 if (parseNumber().empty() && RequireNumber)
5730 return nullptr;
5731 if (look() == '.')
5732 First = Last;
5733 if (numLeft() != 0)
5734 return nullptr;
5735 return make<SpecialName>("invocation function for block in ", Encoding);
5736 }
5737
Pavel Labathba825192018-10-16 14:29:14 +00005738 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005739 if (numLeft() != 0)
5740 return nullptr;
5741 return Ty;
5742}
5743
Pavel Labathba825192018-10-16 14:29:14 +00005744template <typename Alloc>
5745struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5746 using AbstractManglingParser<ManglingParser<Alloc>,
5747 Alloc>::AbstractManglingParser;
5748};
5749
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005750DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005751
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005752#endif // DEMANGLE_ITANIUMDEMANGLE_H