blob: b835345bed220749843288b343fc61189ad2c11e [file] [log] [blame]
Richard Smithc20d1442018-08-20 20:14:49 +00001//===------------------------- ItaniumDemangle.h ----------------*- C++ -*-===//
2//
Chandler Carruth8ee27c32019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Richard Smithc20d1442018-08-20 20:14:49 +00006//
7//===----------------------------------------------------------------------===//
8//
Nathan Sidwell5b0a8cf2022-01-24 06:38:47 -08009// Generic itanium demangler library.
10// There are two copies of this file in the source tree. The one under
11// libcxxabi is the original and the one under llvm is the copy. Use
12// cp-to-llvm.sh to update the copy. See README.txt for more details.
Richard Smithc20d1442018-08-20 20:14:49 +000013//
14//===----------------------------------------------------------------------===//
15
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +000016#ifndef DEMANGLE_ITANIUMDEMANGLE_H
17#define DEMANGLE_ITANIUMDEMANGLE_H
Richard Smithc20d1442018-08-20 20:14:49 +000018
19// FIXME: (possibly) incomplete list of features that clang mangles that this
20// file does not yet support:
21// - C++ modules TS
22
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +000023#include "DemangleConfig.h"
Richard Smithc20d1442018-08-20 20:14:49 +000024#include "StringView.h"
25#include "Utility.h"
Nathan Sidwellbbc632f2022-01-24 04:11:59 -080026#include <algorithm>
Richard Smithc20d1442018-08-20 20:14:49 +000027#include <cassert>
28#include <cctype>
29#include <cstdio>
30#include <cstdlib>
31#include <cstring>
Nathan Sidwellbbc632f2022-01-24 04:11:59 -080032#include <limits>
Richard Smithc20d1442018-08-20 20:14:49 +000033#include <utility>
34
35#define FOR_EACH_NODE_KIND(X) \
36 X(NodeArrayNode) \
37 X(DotSuffix) \
38 X(VendorExtQualType) \
39 X(QualType) \
40 X(ConversionOperatorType) \
41 X(PostfixQualifiedType) \
42 X(ElaboratedTypeSpefType) \
43 X(NameType) \
44 X(AbiTagAttr) \
45 X(EnableIfAttr) \
46 X(ObjCProtoName) \
47 X(PointerType) \
48 X(ReferenceType) \
49 X(PointerToMemberType) \
50 X(ArrayType) \
51 X(FunctionType) \
52 X(NoexceptSpec) \
53 X(DynamicExceptionSpec) \
54 X(FunctionEncoding) \
55 X(LiteralOperator) \
56 X(SpecialName) \
57 X(CtorVtableSpecialName) \
58 X(QualifiedName) \
59 X(NestedName) \
60 X(LocalName) \
61 X(VectorType) \
62 X(PixelVectorType) \
Pengfei Wang50e90b82021-09-23 11:02:25 +080063 X(BinaryFPType) \
Richard Smithdf1c14c2019-09-06 23:53:21 +000064 X(SyntheticTemplateParamName) \
65 X(TypeTemplateParamDecl) \
66 X(NonTypeTemplateParamDecl) \
67 X(TemplateTemplateParamDecl) \
68 X(TemplateParamPackDecl) \
Richard Smithc20d1442018-08-20 20:14:49 +000069 X(ParameterPack) \
70 X(TemplateArgumentPack) \
71 X(ParameterPackExpansion) \
72 X(TemplateArgs) \
73 X(ForwardTemplateReference) \
74 X(NameWithTemplateArgs) \
75 X(GlobalQualifiedName) \
Richard Smithc20d1442018-08-20 20:14:49 +000076 X(ExpandedSpecialSubstitution) \
77 X(SpecialSubstitution) \
78 X(CtorDtorName) \
79 X(DtorName) \
80 X(UnnamedTypeName) \
81 X(ClosureTypeName) \
82 X(StructuredBindingName) \
83 X(BinaryExpr) \
84 X(ArraySubscriptExpr) \
85 X(PostfixExpr) \
86 X(ConditionalExpr) \
87 X(MemberExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070088 X(SubobjectExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000089 X(EnclosingExpr) \
90 X(CastExpr) \
91 X(SizeofParamPackExpr) \
92 X(CallExpr) \
93 X(NewExpr) \
94 X(DeleteExpr) \
95 X(PrefixExpr) \
96 X(FunctionParam) \
97 X(ConversionExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070098 X(PointerToMemberConversionExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000099 X(InitListExpr) \
100 X(FoldExpr) \
101 X(ThrowExpr) \
102 X(BoolExpr) \
Richard Smithdf1c14c2019-09-06 23:53:21 +0000103 X(StringLiteral) \
104 X(LambdaExpr) \
Erik Pilkington0a170f12020-05-13 14:13:37 -0400105 X(EnumLiteral) \
Richard Smithc20d1442018-08-20 20:14:49 +0000106 X(IntegerLiteral) \
107 X(FloatLiteral) \
108 X(DoubleLiteral) \
109 X(LongDoubleLiteral) \
110 X(BracedExpr) \
111 X(BracedRangeExpr)
112
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000113DEMANGLE_NAMESPACE_BEGIN
114
Mikhail Borisov8452f062021-08-17 18:06:53 -0400115template <class T, size_t N> class PODSmallVector {
116 static_assert(std::is_pod<T>::value,
117 "T is required to be a plain old data type");
118
119 T *First = nullptr;
120 T *Last = nullptr;
121 T *Cap = nullptr;
122 T Inline[N] = {0};
123
124 bool isInline() const { return First == Inline; }
125
126 void clearInline() {
127 First = Inline;
128 Last = Inline;
129 Cap = Inline + N;
130 }
131
132 void reserve(size_t NewCap) {
133 size_t S = size();
134 if (isInline()) {
135 auto *Tmp = static_cast<T *>(std::malloc(NewCap * sizeof(T)));
136 if (Tmp == nullptr)
137 std::terminate();
138 std::copy(First, Last, Tmp);
139 First = Tmp;
140 } else {
141 First = static_cast<T *>(std::realloc(First, NewCap * sizeof(T)));
142 if (First == nullptr)
143 std::terminate();
144 }
145 Last = First + S;
146 Cap = First + NewCap;
147 }
148
149public:
150 PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
151
152 PODSmallVector(const PODSmallVector &) = delete;
153 PODSmallVector &operator=(const PODSmallVector &) = delete;
154
155 PODSmallVector(PODSmallVector &&Other) : PODSmallVector() {
156 if (Other.isInline()) {
157 std::copy(Other.begin(), Other.end(), First);
158 Last = First + Other.size();
159 Other.clear();
160 return;
161 }
162
163 First = Other.First;
164 Last = Other.Last;
165 Cap = Other.Cap;
166 Other.clearInline();
167 }
168
169 PODSmallVector &operator=(PODSmallVector &&Other) {
170 if (Other.isInline()) {
171 if (!isInline()) {
172 std::free(First);
173 clearInline();
174 }
175 std::copy(Other.begin(), Other.end(), First);
176 Last = First + Other.size();
177 Other.clear();
178 return *this;
179 }
180
181 if (isInline()) {
182 First = Other.First;
183 Last = Other.Last;
184 Cap = Other.Cap;
185 Other.clearInline();
186 return *this;
187 }
188
189 std::swap(First, Other.First);
190 std::swap(Last, Other.Last);
191 std::swap(Cap, Other.Cap);
192 Other.clear();
193 return *this;
194 }
195
196 // NOLINTNEXTLINE(readability-identifier-naming)
197 void push_back(const T &Elem) {
198 if (Last == Cap)
199 reserve(size() * 2);
200 *Last++ = Elem;
201 }
202
203 // NOLINTNEXTLINE(readability-identifier-naming)
204 void pop_back() {
205 assert(Last != First && "Popping empty vector!");
206 --Last;
207 }
208
209 void dropBack(size_t Index) {
210 assert(Index <= size() && "dropBack() can't expand!");
211 Last = First + Index;
212 }
213
214 T *begin() { return First; }
215 T *end() { return Last; }
216
217 bool empty() const { return First == Last; }
218 size_t size() const { return static_cast<size_t>(Last - First); }
219 T &back() {
220 assert(Last != First && "Calling back() on empty vector!");
221 return *(Last - 1);
222 }
223 T &operator[](size_t Index) {
224 assert(Index < size() && "Invalid access!");
225 return *(begin() + Index);
226 }
227 void clear() { Last = First; }
228
229 ~PODSmallVector() {
230 if (!isInline())
231 std::free(First);
232 }
233};
234
Richard Smithc20d1442018-08-20 20:14:49 +0000235// Base class of all AST nodes. The AST is built by the parser, then is
236// traversed by the printLeft/Right functions to produce a demangled string.
237class Node {
238public:
239 enum Kind : unsigned char {
240#define ENUMERATOR(NodeKind) K ## NodeKind,
241 FOR_EACH_NODE_KIND(ENUMERATOR)
242#undef ENUMERATOR
243 };
244
245 /// Three-way bool to track a cached value. Unknown is possible if this node
246 /// has an unexpanded parameter pack below it that may affect this cache.
247 enum class Cache : unsigned char { Yes, No, Unknown, };
248
249private:
250 Kind K;
251
252 // FIXME: Make these protected.
253public:
254 /// Tracks if this node has a component on its right side, in which case we
255 /// need to call printRight.
256 Cache RHSComponentCache;
257
258 /// Track if this node is a (possibly qualified) array type. This can affect
259 /// how we format the output string.
260 Cache ArrayCache;
261
262 /// Track if this node is a (possibly qualified) function type. This can
263 /// affect how we format the output string.
264 Cache FunctionCache;
265
266public:
267 Node(Kind K_, Cache RHSComponentCache_ = Cache::No,
268 Cache ArrayCache_ = Cache::No, Cache FunctionCache_ = Cache::No)
269 : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_),
270 FunctionCache(FunctionCache_) {}
271
272 /// Visit the most-derived object corresponding to this object.
273 template<typename Fn> void visit(Fn F) const;
274
275 // The following function is provided by all derived classes:
276 //
277 // Call F with arguments that, when passed to the constructor of this node,
278 // would construct an equivalent node.
279 //template<typename Fn> void match(Fn F) const;
280
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700281 bool hasRHSComponent(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000282 if (RHSComponentCache != Cache::Unknown)
283 return RHSComponentCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700284 return hasRHSComponentSlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000285 }
286
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700287 bool hasArray(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000288 if (ArrayCache != Cache::Unknown)
289 return ArrayCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700290 return hasArraySlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000291 }
292
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700293 bool hasFunction(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000294 if (FunctionCache != Cache::Unknown)
295 return FunctionCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700296 return hasFunctionSlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000297 }
298
299 Kind getKind() const { return K; }
300
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700301 virtual bool hasRHSComponentSlow(OutputBuffer &) const { return false; }
302 virtual bool hasArraySlow(OutputBuffer &) const { return false; }
303 virtual bool hasFunctionSlow(OutputBuffer &) const { return false; }
Richard Smithc20d1442018-08-20 20:14:49 +0000304
305 // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
306 // get at a node that actually represents some concrete syntax.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700307 virtual const Node *getSyntaxNode(OutputBuffer &) const { return this; }
Richard Smithc20d1442018-08-20 20:14:49 +0000308
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700309 void print(OutputBuffer &OB) const {
310 printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000311 if (RHSComponentCache != Cache::No)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700312 printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000313 }
314
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -0800315 // Print the "left" side of this Node into OutputBuffer.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700316 virtual void printLeft(OutputBuffer &) const = 0;
Richard Smithc20d1442018-08-20 20:14:49 +0000317
318 // Print the "right". This distinction is necessary to represent C++ types
319 // that appear on the RHS of their subtype, such as arrays or functions.
320 // Since most types don't have such a component, provide a default
321 // implementation.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700322 virtual void printRight(OutputBuffer &) const {}
Richard Smithc20d1442018-08-20 20:14:49 +0000323
324 virtual StringView getBaseName() const { return StringView(); }
325
326 // Silence compiler warnings, this dtor will never be called.
327 virtual ~Node() = default;
328
329#ifndef NDEBUG
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000330 DEMANGLE_DUMP_METHOD void dump() const;
Richard Smithc20d1442018-08-20 20:14:49 +0000331#endif
332};
333
334class NodeArray {
335 Node **Elements;
336 size_t NumElements;
337
338public:
339 NodeArray() : Elements(nullptr), NumElements(0) {}
340 NodeArray(Node **Elements_, size_t NumElements_)
341 : Elements(Elements_), NumElements(NumElements_) {}
342
343 bool empty() const { return NumElements == 0; }
344 size_t size() const { return NumElements; }
345
346 Node **begin() const { return Elements; }
347 Node **end() const { return Elements + NumElements; }
348
349 Node *operator[](size_t Idx) const { return Elements[Idx]; }
350
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700351 void printWithComma(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000352 bool FirstElement = true;
353 for (size_t Idx = 0; Idx != NumElements; ++Idx) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700354 size_t BeforeComma = OB.getCurrentPosition();
Richard Smithc20d1442018-08-20 20:14:49 +0000355 if (!FirstElement)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700356 OB += ", ";
357 size_t AfterComma = OB.getCurrentPosition();
358 Elements[Idx]->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000359
360 // Elements[Idx] is an empty parameter pack expansion, we should erase the
361 // comma we just printed.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700362 if (AfterComma == OB.getCurrentPosition()) {
363 OB.setCurrentPosition(BeforeComma);
Richard Smithc20d1442018-08-20 20:14:49 +0000364 continue;
365 }
366
367 FirstElement = false;
368 }
369 }
370};
371
372struct NodeArrayNode : Node {
373 NodeArray Array;
374 NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
375
376 template<typename Fn> void match(Fn F) const { F(Array); }
377
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700378 void printLeft(OutputBuffer &OB) const override { Array.printWithComma(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000379};
380
381class DotSuffix final : public Node {
382 const Node *Prefix;
383 const StringView Suffix;
384
385public:
386 DotSuffix(const Node *Prefix_, StringView Suffix_)
387 : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
388
389 template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
390
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700391 void printLeft(OutputBuffer &OB) const override {
392 Prefix->print(OB);
393 OB += " (";
394 OB += Suffix;
395 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000396 }
397};
398
399class VendorExtQualType final : public Node {
400 const Node *Ty;
401 StringView Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400402 const Node *TA;
Richard Smithc20d1442018-08-20 20:14:49 +0000403
404public:
Alex Orlovf50df922021-03-24 10:21:32 +0400405 VendorExtQualType(const Node *Ty_, StringView Ext_, const Node *TA_)
406 : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_), TA(TA_) {}
Richard Smithc20d1442018-08-20 20:14:49 +0000407
Alex Orlovf50df922021-03-24 10:21:32 +0400408 template <typename Fn> void match(Fn F) const { F(Ty, Ext, TA); }
Richard Smithc20d1442018-08-20 20:14:49 +0000409
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700410 void printLeft(OutputBuffer &OB) const override {
411 Ty->print(OB);
412 OB += " ";
413 OB += Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400414 if (TA != nullptr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700415 TA->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000416 }
417};
418
419enum FunctionRefQual : unsigned char {
420 FrefQualNone,
421 FrefQualLValue,
422 FrefQualRValue,
423};
424
425enum Qualifiers {
426 QualNone = 0,
427 QualConst = 0x1,
428 QualVolatile = 0x2,
429 QualRestrict = 0x4,
430};
431
432inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
433 return Q1 = static_cast<Qualifiers>(Q1 | Q2);
434}
435
Richard Smithdf1c14c2019-09-06 23:53:21 +0000436class QualType final : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +0000437protected:
438 const Qualifiers Quals;
439 const Node *Child;
440
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700441 void printQuals(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000442 if (Quals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700443 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000444 if (Quals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700445 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000446 if (Quals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700447 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000448 }
449
450public:
451 QualType(const Node *Child_, Qualifiers Quals_)
452 : Node(KQualType, Child_->RHSComponentCache,
453 Child_->ArrayCache, Child_->FunctionCache),
454 Quals(Quals_), Child(Child_) {}
455
456 template<typename Fn> void match(Fn F) const { F(Child, Quals); }
457
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700458 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
459 return Child->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000460 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700461 bool hasArraySlow(OutputBuffer &OB) const override {
462 return Child->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000463 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700464 bool hasFunctionSlow(OutputBuffer &OB) const override {
465 return Child->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000466 }
467
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700468 void printLeft(OutputBuffer &OB) const override {
469 Child->printLeft(OB);
470 printQuals(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000471 }
472
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700473 void printRight(OutputBuffer &OB) const override { Child->printRight(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000474};
475
476class ConversionOperatorType final : public Node {
477 const Node *Ty;
478
479public:
480 ConversionOperatorType(const Node *Ty_)
481 : Node(KConversionOperatorType), Ty(Ty_) {}
482
483 template<typename Fn> void match(Fn F) const { F(Ty); }
484
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700485 void printLeft(OutputBuffer &OB) const override {
486 OB += "operator ";
487 Ty->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000488 }
489};
490
491class PostfixQualifiedType final : public Node {
492 const Node *Ty;
493 const StringView Postfix;
494
495public:
496 PostfixQualifiedType(Node *Ty_, StringView Postfix_)
497 : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
498
499 template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
500
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700501 void printLeft(OutputBuffer &OB) const override {
502 Ty->printLeft(OB);
503 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +0000504 }
505};
506
507class NameType final : public Node {
508 const StringView Name;
509
510public:
511 NameType(StringView Name_) : Node(KNameType), Name(Name_) {}
512
513 template<typename Fn> void match(Fn F) const { F(Name); }
514
515 StringView getName() const { return Name; }
516 StringView getBaseName() const override { return Name; }
517
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700518 void printLeft(OutputBuffer &OB) const override { OB += Name; }
Richard Smithc20d1442018-08-20 20:14:49 +0000519};
520
521class ElaboratedTypeSpefType : public Node {
522 StringView Kind;
523 Node *Child;
524public:
525 ElaboratedTypeSpefType(StringView Kind_, Node *Child_)
526 : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
527
528 template<typename Fn> void match(Fn F) const { F(Kind, Child); }
529
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700530 void printLeft(OutputBuffer &OB) const override {
531 OB += Kind;
532 OB += ' ';
533 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000534 }
535};
536
537struct AbiTagAttr : Node {
538 Node *Base;
539 StringView Tag;
540
541 AbiTagAttr(Node* Base_, StringView Tag_)
542 : Node(KAbiTagAttr, Base_->RHSComponentCache,
543 Base_->ArrayCache, Base_->FunctionCache),
544 Base(Base_), Tag(Tag_) {}
545
546 template<typename Fn> void match(Fn F) const { F(Base, Tag); }
547
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700548 void printLeft(OutputBuffer &OB) const override {
549 Base->printLeft(OB);
550 OB += "[abi:";
551 OB += Tag;
552 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +0000553 }
554};
555
556class EnableIfAttr : public Node {
557 NodeArray Conditions;
558public:
559 EnableIfAttr(NodeArray Conditions_)
560 : Node(KEnableIfAttr), Conditions(Conditions_) {}
561
562 template<typename Fn> void match(Fn F) const { F(Conditions); }
563
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700564 void printLeft(OutputBuffer &OB) const override {
565 OB += " [enable_if:";
566 Conditions.printWithComma(OB);
567 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +0000568 }
569};
570
571class ObjCProtoName : public Node {
572 const Node *Ty;
573 StringView Protocol;
574
575 friend class PointerType;
576
577public:
578 ObjCProtoName(const Node *Ty_, StringView Protocol_)
579 : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
580
581 template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
582
583 bool isObjCObject() const {
584 return Ty->getKind() == KNameType &&
585 static_cast<const NameType *>(Ty)->getName() == "objc_object";
586 }
587
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700588 void printLeft(OutputBuffer &OB) const override {
589 Ty->print(OB);
590 OB += "<";
591 OB += Protocol;
592 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +0000593 }
594};
595
596class PointerType final : public Node {
597 const Node *Pointee;
598
599public:
600 PointerType(const Node *Pointee_)
601 : Node(KPointerType, Pointee_->RHSComponentCache),
602 Pointee(Pointee_) {}
603
604 template<typename Fn> void match(Fn F) const { F(Pointee); }
605
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700606 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
607 return Pointee->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000608 }
609
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700610 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000611 // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
612 if (Pointee->getKind() != KObjCProtoName ||
613 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700614 Pointee->printLeft(OB);
615 if (Pointee->hasArray(OB))
616 OB += " ";
617 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
618 OB += "(";
619 OB += "*";
Richard Smithc20d1442018-08-20 20:14:49 +0000620 } else {
621 const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700622 OB += "id<";
623 OB += objcProto->Protocol;
624 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +0000625 }
626 }
627
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700628 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000629 if (Pointee->getKind() != KObjCProtoName ||
630 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700631 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
632 OB += ")";
633 Pointee->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000634 }
635 }
636};
637
638enum class ReferenceKind {
639 LValue,
640 RValue,
641};
642
643// Represents either a LValue or an RValue reference type.
644class ReferenceType : public Node {
645 const Node *Pointee;
646 ReferenceKind RK;
647
648 mutable bool Printing = false;
649
650 // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
651 // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
652 // other combination collapses to a lvalue ref.
Mikhail Borisov05f77222021-08-17 18:10:57 -0400653 //
654 // A combination of a TemplateForwardReference and a back-ref Substitution
655 // from an ill-formed string may have created a cycle; use cycle detection to
656 // avoid looping forever.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700657 std::pair<ReferenceKind, const Node *> collapse(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000658 auto SoFar = std::make_pair(RK, Pointee);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400659 // Track the chain of nodes for the Floyd's 'tortoise and hare'
660 // cycle-detection algorithm, since getSyntaxNode(S) is impure
661 PODSmallVector<const Node *, 8> Prev;
Richard Smithc20d1442018-08-20 20:14:49 +0000662 for (;;) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700663 const Node *SN = SoFar.second->getSyntaxNode(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000664 if (SN->getKind() != KReferenceType)
665 break;
666 auto *RT = static_cast<const ReferenceType *>(SN);
667 SoFar.second = RT->Pointee;
668 SoFar.first = std::min(SoFar.first, RT->RK);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400669
670 // The middle of Prev is the 'slow' pointer moving at half speed
671 Prev.push_back(SoFar.second);
672 if (Prev.size() > 1 && SoFar.second == Prev[(Prev.size() - 1) / 2]) {
673 // Cycle detected
674 SoFar.second = nullptr;
675 break;
676 }
Richard Smithc20d1442018-08-20 20:14:49 +0000677 }
678 return SoFar;
679 }
680
681public:
682 ReferenceType(const Node *Pointee_, ReferenceKind RK_)
683 : Node(KReferenceType, Pointee_->RHSComponentCache),
684 Pointee(Pointee_), RK(RK_) {}
685
686 template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
687
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700688 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
689 return Pointee->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000690 }
691
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700692 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000693 if (Printing)
694 return;
695 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700696 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400697 if (!Collapsed.second)
698 return;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700699 Collapsed.second->printLeft(OB);
700 if (Collapsed.second->hasArray(OB))
701 OB += " ";
702 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
703 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +0000704
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700705 OB += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
Richard Smithc20d1442018-08-20 20:14:49 +0000706 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700707 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000708 if (Printing)
709 return;
710 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700711 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400712 if (!Collapsed.second)
713 return;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700714 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
715 OB += ")";
716 Collapsed.second->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000717 }
718};
719
720class PointerToMemberType final : public Node {
721 const Node *ClassType;
722 const Node *MemberType;
723
724public:
725 PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
726 : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
727 ClassType(ClassType_), MemberType(MemberType_) {}
728
729 template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
730
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700731 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
732 return MemberType->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000733 }
734
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700735 void printLeft(OutputBuffer &OB) const override {
736 MemberType->printLeft(OB);
737 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
738 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +0000739 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700740 OB += " ";
741 ClassType->print(OB);
742 OB += "::*";
Richard Smithc20d1442018-08-20 20:14:49 +0000743 }
744
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700745 void printRight(OutputBuffer &OB) const override {
746 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
747 OB += ")";
748 MemberType->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000749 }
750};
751
Richard Smithc20d1442018-08-20 20:14:49 +0000752class ArrayType final : public Node {
753 const Node *Base;
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800754 Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +0000755
756public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800757 ArrayType(const Node *Base_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +0000758 : Node(KArrayType,
759 /*RHSComponentCache=*/Cache::Yes,
760 /*ArrayCache=*/Cache::Yes),
761 Base(Base_), Dimension(Dimension_) {}
762
763 template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
764
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700765 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
766 bool hasArraySlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000767
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700768 void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000769
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700770 void printRight(OutputBuffer &OB) const override {
771 if (OB.back() != ']')
772 OB += " ";
773 OB += "[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800774 if (Dimension)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700775 Dimension->print(OB);
776 OB += "]";
777 Base->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000778 }
779};
780
781class FunctionType final : public Node {
782 const Node *Ret;
783 NodeArray Params;
784 Qualifiers CVQuals;
785 FunctionRefQual RefQual;
786 const Node *ExceptionSpec;
787
788public:
789 FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
790 FunctionRefQual RefQual_, const Node *ExceptionSpec_)
791 : Node(KFunctionType,
792 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
793 /*FunctionCache=*/Cache::Yes),
794 Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
795 ExceptionSpec(ExceptionSpec_) {}
796
797 template<typename Fn> void match(Fn F) const {
798 F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
799 }
800
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700801 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
802 bool hasFunctionSlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000803
804 // Handle C++'s ... quirky decl grammar by using the left & right
805 // distinction. Consider:
806 // int (*f(float))(char) {}
807 // f is a function that takes a float and returns a pointer to a function
808 // that takes a char and returns an int. If we're trying to print f, start
809 // by printing out the return types's left, then print our parameters, then
810 // finally print right of the return type.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700811 void printLeft(OutputBuffer &OB) const override {
812 Ret->printLeft(OB);
813 OB += " ";
Richard Smithc20d1442018-08-20 20:14:49 +0000814 }
815
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700816 void printRight(OutputBuffer &OB) const override {
817 OB += "(";
818 Params.printWithComma(OB);
819 OB += ")";
820 Ret->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000821
822 if (CVQuals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700823 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000824 if (CVQuals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700825 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000826 if (CVQuals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700827 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000828
829 if (RefQual == FrefQualLValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700830 OB += " &";
Richard Smithc20d1442018-08-20 20:14:49 +0000831 else if (RefQual == FrefQualRValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700832 OB += " &&";
Richard Smithc20d1442018-08-20 20:14:49 +0000833
834 if (ExceptionSpec != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700835 OB += ' ';
836 ExceptionSpec->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000837 }
838 }
839};
840
841class NoexceptSpec : public Node {
842 const Node *E;
843public:
844 NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
845
846 template<typename Fn> void match(Fn F) const { F(E); }
847
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700848 void printLeft(OutputBuffer &OB) const override {
849 OB += "noexcept(";
850 E->print(OB);
851 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000852 }
853};
854
855class DynamicExceptionSpec : public Node {
856 NodeArray Types;
857public:
858 DynamicExceptionSpec(NodeArray Types_)
859 : Node(KDynamicExceptionSpec), Types(Types_) {}
860
861 template<typename Fn> void match(Fn F) const { F(Types); }
862
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700863 void printLeft(OutputBuffer &OB) const override {
864 OB += "throw(";
865 Types.printWithComma(OB);
866 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +0000867 }
868};
869
870class FunctionEncoding final : public Node {
871 const Node *Ret;
872 const Node *Name;
873 NodeArray Params;
874 const Node *Attrs;
875 Qualifiers CVQuals;
876 FunctionRefQual RefQual;
877
878public:
879 FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
880 const Node *Attrs_, Qualifiers CVQuals_,
881 FunctionRefQual RefQual_)
882 : Node(KFunctionEncoding,
883 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
884 /*FunctionCache=*/Cache::Yes),
885 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
886 CVQuals(CVQuals_), RefQual(RefQual_) {}
887
888 template<typename Fn> void match(Fn F) const {
889 F(Ret, Name, Params, Attrs, CVQuals, RefQual);
890 }
891
892 Qualifiers getCVQuals() const { return CVQuals; }
893 FunctionRefQual getRefQual() const { return RefQual; }
894 NodeArray getParams() const { return Params; }
895 const Node *getReturnType() const { return Ret; }
896
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700897 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
898 bool hasFunctionSlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000899
900 const Node *getName() const { return Name; }
901
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700902 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000903 if (Ret) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700904 Ret->printLeft(OB);
905 if (!Ret->hasRHSComponent(OB))
906 OB += " ";
Richard Smithc20d1442018-08-20 20:14:49 +0000907 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700908 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000909 }
910
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700911 void printRight(OutputBuffer &OB) const override {
912 OB += "(";
913 Params.printWithComma(OB);
914 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000915 if (Ret)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700916 Ret->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000917
918 if (CVQuals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700919 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000920 if (CVQuals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700921 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000922 if (CVQuals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700923 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000924
925 if (RefQual == FrefQualLValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700926 OB += " &";
Richard Smithc20d1442018-08-20 20:14:49 +0000927 else if (RefQual == FrefQualRValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700928 OB += " &&";
Richard Smithc20d1442018-08-20 20:14:49 +0000929
930 if (Attrs != nullptr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700931 Attrs->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000932 }
933};
934
935class LiteralOperator : public Node {
936 const Node *OpName;
937
938public:
939 LiteralOperator(const Node *OpName_)
940 : Node(KLiteralOperator), OpName(OpName_) {}
941
942 template<typename Fn> void match(Fn F) const { F(OpName); }
943
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700944 void printLeft(OutputBuffer &OB) const override {
945 OB += "operator\"\" ";
946 OpName->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000947 }
948};
949
950class SpecialName final : public Node {
951 const StringView Special;
952 const Node *Child;
953
954public:
955 SpecialName(StringView Special_, const Node *Child_)
956 : Node(KSpecialName), Special(Special_), Child(Child_) {}
957
958 template<typename Fn> void match(Fn F) const { F(Special, Child); }
959
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700960 void printLeft(OutputBuffer &OB) const override {
961 OB += Special;
962 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000963 }
964};
965
966class CtorVtableSpecialName final : public Node {
967 const Node *FirstType;
968 const Node *SecondType;
969
970public:
971 CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
972 : Node(KCtorVtableSpecialName),
973 FirstType(FirstType_), SecondType(SecondType_) {}
974
975 template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
976
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700977 void printLeft(OutputBuffer &OB) const override {
978 OB += "construction vtable for ";
979 FirstType->print(OB);
980 OB += "-in-";
981 SecondType->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000982 }
983};
984
985struct NestedName : Node {
986 Node *Qual;
987 Node *Name;
988
989 NestedName(Node *Qual_, Node *Name_)
990 : Node(KNestedName), Qual(Qual_), Name(Name_) {}
991
992 template<typename Fn> void match(Fn F) const { F(Qual, Name); }
993
994 StringView getBaseName() const override { return Name->getBaseName(); }
995
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700996 void printLeft(OutputBuffer &OB) const override {
997 Qual->print(OB);
998 OB += "::";
999 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001000 }
1001};
1002
1003struct LocalName : Node {
1004 Node *Encoding;
1005 Node *Entity;
1006
1007 LocalName(Node *Encoding_, Node *Entity_)
1008 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
1009
1010 template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
1011
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001012 void printLeft(OutputBuffer &OB) const override {
1013 Encoding->print(OB);
1014 OB += "::";
1015 Entity->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001016 }
1017};
1018
1019class QualifiedName final : public Node {
1020 // qualifier::name
1021 const Node *Qualifier;
1022 const Node *Name;
1023
1024public:
1025 QualifiedName(const Node *Qualifier_, const Node *Name_)
1026 : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
1027
1028 template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
1029
1030 StringView getBaseName() const override { return Name->getBaseName(); }
1031
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001032 void printLeft(OutputBuffer &OB) const override {
1033 Qualifier->print(OB);
1034 OB += "::";
1035 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001036 }
1037};
1038
1039class VectorType final : public Node {
1040 const Node *BaseType;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001041 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001042
1043public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001044 VectorType(const Node *BaseType_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001045 : Node(KVectorType), BaseType(BaseType_),
1046 Dimension(Dimension_) {}
1047
1048 template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
1049
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001050 void printLeft(OutputBuffer &OB) const override {
1051 BaseType->print(OB);
1052 OB += " vector[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001053 if (Dimension)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001054 Dimension->print(OB);
1055 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001056 }
1057};
1058
1059class PixelVectorType final : public Node {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001060 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001061
1062public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001063 PixelVectorType(const Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001064 : Node(KPixelVectorType), Dimension(Dimension_) {}
1065
1066 template<typename Fn> void match(Fn F) const { F(Dimension); }
1067
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001068 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001069 // FIXME: This should demangle as "vector pixel".
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001070 OB += "pixel vector[";
1071 Dimension->print(OB);
1072 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001073 }
1074};
1075
Pengfei Wang50e90b82021-09-23 11:02:25 +08001076class BinaryFPType final : public Node {
1077 const Node *Dimension;
1078
1079public:
1080 BinaryFPType(const Node *Dimension_)
1081 : Node(KBinaryFPType), Dimension(Dimension_) {}
1082
1083 template<typename Fn> void match(Fn F) const { F(Dimension); }
1084
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001085 void printLeft(OutputBuffer &OB) const override {
1086 OB += "_Float";
1087 Dimension->print(OB);
Pengfei Wang50e90b82021-09-23 11:02:25 +08001088 }
1089};
1090
Richard Smithdf1c14c2019-09-06 23:53:21 +00001091enum class TemplateParamKind { Type, NonType, Template };
1092
1093/// An invented name for a template parameter for which we don't have a
1094/// corresponding template argument.
1095///
1096/// This node is created when parsing the <lambda-sig> for a lambda with
1097/// explicit template arguments, which might be referenced in the parameter
1098/// types appearing later in the <lambda-sig>.
1099class SyntheticTemplateParamName final : public Node {
1100 TemplateParamKind Kind;
1101 unsigned Index;
1102
1103public:
1104 SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_)
1105 : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {}
1106
1107 template<typename Fn> void match(Fn F) const { F(Kind, Index); }
1108
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001109 void printLeft(OutputBuffer &OB) const override {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001110 switch (Kind) {
1111 case TemplateParamKind::Type:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001112 OB += "$T";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001113 break;
1114 case TemplateParamKind::NonType:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001115 OB += "$N";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001116 break;
1117 case TemplateParamKind::Template:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001118 OB += "$TT";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001119 break;
1120 }
1121 if (Index > 0)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001122 OB << Index - 1;
Richard Smithdf1c14c2019-09-06 23:53:21 +00001123 }
1124};
1125
1126/// A template type parameter declaration, 'typename T'.
1127class TypeTemplateParamDecl final : public Node {
1128 Node *Name;
1129
1130public:
1131 TypeTemplateParamDecl(Node *Name_)
1132 : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {}
1133
1134 template<typename Fn> void match(Fn F) const { F(Name); }
1135
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001136 void printLeft(OutputBuffer &OB) const override { OB += "typename "; }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001137
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001138 void printRight(OutputBuffer &OB) const override { Name->print(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001139};
1140
1141/// A non-type template parameter declaration, 'int N'.
1142class NonTypeTemplateParamDecl final : public Node {
1143 Node *Name;
1144 Node *Type;
1145
1146public:
1147 NonTypeTemplateParamDecl(Node *Name_, Node *Type_)
1148 : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {}
1149
1150 template<typename Fn> void match(Fn F) const { F(Name, Type); }
1151
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001152 void printLeft(OutputBuffer &OB) const override {
1153 Type->printLeft(OB);
1154 if (!Type->hasRHSComponent(OB))
1155 OB += " ";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001156 }
1157
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001158 void printRight(OutputBuffer &OB) const override {
1159 Name->print(OB);
1160 Type->printRight(OB);
Richard Smithdf1c14c2019-09-06 23:53:21 +00001161 }
1162};
1163
1164/// A template template parameter declaration,
1165/// 'template<typename T> typename N'.
1166class TemplateTemplateParamDecl final : public Node {
1167 Node *Name;
1168 NodeArray Params;
1169
1170public:
1171 TemplateTemplateParamDecl(Node *Name_, NodeArray Params_)
1172 : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_),
1173 Params(Params_) {}
1174
1175 template<typename Fn> void match(Fn F) const { F(Name, Params); }
1176
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001177 void printLeft(OutputBuffer &OB) const override {
1178 OB += "template<";
1179 Params.printWithComma(OB);
1180 OB += "> typename ";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001181 }
1182
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001183 void printRight(OutputBuffer &OB) const override { Name->print(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001184};
1185
1186/// A template parameter pack declaration, 'typename ...T'.
1187class TemplateParamPackDecl final : public Node {
1188 Node *Param;
1189
1190public:
1191 TemplateParamPackDecl(Node *Param_)
1192 : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {}
1193
1194 template<typename Fn> void match(Fn F) const { F(Param); }
1195
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001196 void printLeft(OutputBuffer &OB) const override {
1197 Param->printLeft(OB);
1198 OB += "...";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001199 }
1200
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001201 void printRight(OutputBuffer &OB) const override { Param->printRight(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001202};
1203
Richard Smithc20d1442018-08-20 20:14:49 +00001204/// An unexpanded parameter pack (either in the expression or type context). If
1205/// this AST is correct, this node will have a ParameterPackExpansion node above
1206/// it.
1207///
1208/// This node is created when some <template-args> are found that apply to an
1209/// <encoding>, and is stored in the TemplateParams table. In order for this to
1210/// appear in the final AST, it has to referenced via a <template-param> (ie,
1211/// T_).
1212class ParameterPack final : public Node {
1213 NodeArray Data;
1214
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08001215 // Setup OutputBuffer for a pack expansion, unless we're already expanding
1216 // one.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001217 void initializePackExpansion(OutputBuffer &OB) const {
1218 if (OB.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
1219 OB.CurrentPackMax = static_cast<unsigned>(Data.size());
1220 OB.CurrentPackIndex = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00001221 }
1222 }
1223
1224public:
1225 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
1226 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
1227 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1228 return P->ArrayCache == Cache::No;
1229 }))
1230 ArrayCache = Cache::No;
1231 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1232 return P->FunctionCache == Cache::No;
1233 }))
1234 FunctionCache = Cache::No;
1235 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1236 return P->RHSComponentCache == Cache::No;
1237 }))
1238 RHSComponentCache = Cache::No;
1239 }
1240
1241 template<typename Fn> void match(Fn F) const { F(Data); }
1242
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001243 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
1244 initializePackExpansion(OB);
1245 size_t Idx = OB.CurrentPackIndex;
1246 return Idx < Data.size() && Data[Idx]->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001247 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001248 bool hasArraySlow(OutputBuffer &OB) const override {
1249 initializePackExpansion(OB);
1250 size_t Idx = OB.CurrentPackIndex;
1251 return Idx < Data.size() && Data[Idx]->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001252 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001253 bool hasFunctionSlow(OutputBuffer &OB) const override {
1254 initializePackExpansion(OB);
1255 size_t Idx = OB.CurrentPackIndex;
1256 return Idx < Data.size() && Data[Idx]->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001257 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001258 const Node *getSyntaxNode(OutputBuffer &OB) const override {
1259 initializePackExpansion(OB);
1260 size_t Idx = OB.CurrentPackIndex;
1261 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(OB) : this;
Richard Smithc20d1442018-08-20 20:14:49 +00001262 }
1263
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001264 void printLeft(OutputBuffer &OB) const override {
1265 initializePackExpansion(OB);
1266 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001267 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001268 Data[Idx]->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001269 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001270 void printRight(OutputBuffer &OB) const override {
1271 initializePackExpansion(OB);
1272 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001273 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001274 Data[Idx]->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001275 }
1276};
1277
1278/// A variadic template argument. This node represents an occurrence of
1279/// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1280/// one of it's Elements is. The parser inserts a ParameterPack into the
1281/// TemplateParams table if the <template-args> this pack belongs to apply to an
1282/// <encoding>.
1283class TemplateArgumentPack final : public Node {
1284 NodeArray Elements;
1285public:
1286 TemplateArgumentPack(NodeArray Elements_)
1287 : Node(KTemplateArgumentPack), Elements(Elements_) {}
1288
1289 template<typename Fn> void match(Fn F) const { F(Elements); }
1290
1291 NodeArray getElements() const { return Elements; }
1292
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001293 void printLeft(OutputBuffer &OB) const override {
1294 Elements.printWithComma(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001295 }
1296};
1297
1298/// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1299/// which each have Child->ParameterPackSize elements.
1300class ParameterPackExpansion final : public Node {
1301 const Node *Child;
1302
1303public:
1304 ParameterPackExpansion(const Node *Child_)
1305 : Node(KParameterPackExpansion), Child(Child_) {}
1306
1307 template<typename Fn> void match(Fn F) const { F(Child); }
1308
1309 const Node *getChild() const { return Child; }
1310
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001311 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001312 constexpr unsigned Max = std::numeric_limits<unsigned>::max();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001313 SwapAndRestore<unsigned> SavePackIdx(OB.CurrentPackIndex, Max);
1314 SwapAndRestore<unsigned> SavePackMax(OB.CurrentPackMax, Max);
1315 size_t StreamPos = OB.getCurrentPosition();
Richard Smithc20d1442018-08-20 20:14:49 +00001316
1317 // Print the first element in the pack. If Child contains a ParameterPack,
1318 // it will set up S.CurrentPackMax and print the first element.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001319 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001320
1321 // No ParameterPack was found in Child. This can occur if we've found a pack
1322 // expansion on a <function-param>.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001323 if (OB.CurrentPackMax == Max) {
1324 OB += "...";
Richard Smithc20d1442018-08-20 20:14:49 +00001325 return;
1326 }
1327
1328 // We found a ParameterPack, but it has no elements. Erase whatever we may
1329 // of printed.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001330 if (OB.CurrentPackMax == 0) {
1331 OB.setCurrentPosition(StreamPos);
Richard Smithc20d1442018-08-20 20:14:49 +00001332 return;
1333 }
1334
1335 // Else, iterate through the rest of the elements in the pack.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001336 for (unsigned I = 1, E = OB.CurrentPackMax; I < E; ++I) {
1337 OB += ", ";
1338 OB.CurrentPackIndex = I;
1339 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001340 }
1341 }
1342};
1343
1344class TemplateArgs final : public Node {
1345 NodeArray Params;
1346
1347public:
1348 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1349
1350 template<typename Fn> void match(Fn F) const { F(Params); }
1351
1352 NodeArray getParams() { return Params; }
1353
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001354 void printLeft(OutputBuffer &OB) const override {
1355 OB += "<";
1356 Params.printWithComma(OB);
1357 if (OB.back() == '>')
1358 OB += " ";
1359 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +00001360 }
1361};
1362
Richard Smithb485b352018-08-24 23:30:26 +00001363/// A forward-reference to a template argument that was not known at the point
1364/// where the template parameter name was parsed in a mangling.
1365///
1366/// This is created when demangling the name of a specialization of a
1367/// conversion function template:
1368///
1369/// \code
1370/// struct A {
1371/// template<typename T> operator T*();
1372/// };
1373/// \endcode
1374///
1375/// When demangling a specialization of the conversion function template, we
1376/// encounter the name of the template (including the \c T) before we reach
1377/// the template argument list, so we cannot substitute the parameter name
1378/// for the corresponding argument while parsing. Instead, we create a
1379/// \c ForwardTemplateReference node that is resolved after we parse the
1380/// template arguments.
Richard Smithc20d1442018-08-20 20:14:49 +00001381struct ForwardTemplateReference : Node {
1382 size_t Index;
1383 Node *Ref = nullptr;
1384
1385 // If we're currently printing this node. It is possible (though invalid) for
1386 // a forward template reference to refer to itself via a substitution. This
1387 // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1388 // out if more than one print* function is active.
1389 mutable bool Printing = false;
1390
1391 ForwardTemplateReference(size_t Index_)
1392 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1393 Cache::Unknown),
1394 Index(Index_) {}
1395
1396 // We don't provide a matcher for these, because the value of the node is
1397 // not determined by its construction parameters, and it generally needs
1398 // special handling.
1399 template<typename Fn> void match(Fn F) const = delete;
1400
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001401 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001402 if (Printing)
1403 return false;
1404 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001405 return Ref->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001406 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001407 bool hasArraySlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001408 if (Printing)
1409 return false;
1410 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001411 return Ref->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001412 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001413 bool hasFunctionSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001414 if (Printing)
1415 return false;
1416 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001417 return Ref->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001418 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001419 const Node *getSyntaxNode(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001420 if (Printing)
1421 return this;
1422 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001423 return Ref->getSyntaxNode(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001424 }
1425
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001426 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001427 if (Printing)
1428 return;
1429 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001430 Ref->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001431 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001432 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001433 if (Printing)
1434 return;
1435 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001436 Ref->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001437 }
1438};
1439
1440struct NameWithTemplateArgs : Node {
1441 // name<template_args>
1442 Node *Name;
1443 Node *TemplateArgs;
1444
1445 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1446 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1447
1448 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1449
1450 StringView getBaseName() const override { return Name->getBaseName(); }
1451
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001452 void printLeft(OutputBuffer &OB) const override {
1453 Name->print(OB);
1454 TemplateArgs->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001455 }
1456};
1457
1458class GlobalQualifiedName final : public Node {
1459 Node *Child;
1460
1461public:
1462 GlobalQualifiedName(Node* Child_)
1463 : Node(KGlobalQualifiedName), Child(Child_) {}
1464
1465 template<typename Fn> void match(Fn F) const { F(Child); }
1466
1467 StringView getBaseName() const override { return Child->getBaseName(); }
1468
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001469 void printLeft(OutputBuffer &OB) const override {
1470 OB += "::";
1471 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001472 }
1473};
1474
Richard Smithc20d1442018-08-20 20:14:49 +00001475enum class SpecialSubKind {
1476 allocator,
1477 basic_string,
1478 string,
1479 istream,
1480 ostream,
1481 iostream,
1482};
1483
1484class ExpandedSpecialSubstitution final : public Node {
1485 SpecialSubKind SSK;
1486
1487public:
1488 ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1489 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1490
1491 template<typename Fn> void match(Fn F) const { F(SSK); }
1492
1493 StringView getBaseName() const override {
1494 switch (SSK) {
1495 case SpecialSubKind::allocator:
1496 return StringView("allocator");
1497 case SpecialSubKind::basic_string:
1498 return StringView("basic_string");
1499 case SpecialSubKind::string:
1500 return StringView("basic_string");
1501 case SpecialSubKind::istream:
1502 return StringView("basic_istream");
1503 case SpecialSubKind::ostream:
1504 return StringView("basic_ostream");
1505 case SpecialSubKind::iostream:
1506 return StringView("basic_iostream");
1507 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001508 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001509 }
1510
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001511 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001512 switch (SSK) {
1513 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001514 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001515 break;
1516 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001517 OB += "std::basic_string";
Richard Smithb485b352018-08-24 23:30:26 +00001518 break;
Richard Smithc20d1442018-08-20 20:14:49 +00001519 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001520 OB += "std::basic_string<char, std::char_traits<char>, "
1521 "std::allocator<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001522 break;
1523 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001524 OB += "std::basic_istream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001525 break;
1526 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001527 OB += "std::basic_ostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001528 break;
1529 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001530 OB += "std::basic_iostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001531 break;
1532 }
1533 }
1534};
1535
1536class SpecialSubstitution final : public Node {
1537public:
1538 SpecialSubKind SSK;
1539
1540 SpecialSubstitution(SpecialSubKind SSK_)
1541 : Node(KSpecialSubstitution), SSK(SSK_) {}
1542
1543 template<typename Fn> void match(Fn F) const { F(SSK); }
1544
1545 StringView getBaseName() const override {
1546 switch (SSK) {
1547 case SpecialSubKind::allocator:
1548 return StringView("allocator");
1549 case SpecialSubKind::basic_string:
1550 return StringView("basic_string");
1551 case SpecialSubKind::string:
1552 return StringView("string");
1553 case SpecialSubKind::istream:
1554 return StringView("istream");
1555 case SpecialSubKind::ostream:
1556 return StringView("ostream");
1557 case SpecialSubKind::iostream:
1558 return StringView("iostream");
1559 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001560 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001561 }
1562
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001563 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001564 switch (SSK) {
1565 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001566 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001567 break;
1568 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001569 OB += "std::basic_string";
Richard Smithc20d1442018-08-20 20:14:49 +00001570 break;
1571 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001572 OB += "std::string";
Richard Smithc20d1442018-08-20 20:14:49 +00001573 break;
1574 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001575 OB += "std::istream";
Richard Smithc20d1442018-08-20 20:14:49 +00001576 break;
1577 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001578 OB += "std::ostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001579 break;
1580 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001581 OB += "std::iostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001582 break;
1583 }
1584 }
1585};
1586
1587class CtorDtorName final : public Node {
1588 const Node *Basename;
1589 const bool IsDtor;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001590 const int Variant;
Richard Smithc20d1442018-08-20 20:14:49 +00001591
1592public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001593 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1594 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1595 Variant(Variant_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001596
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001597 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
Richard Smithc20d1442018-08-20 20:14:49 +00001598
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001599 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001600 if (IsDtor)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001601 OB += "~";
1602 OB += Basename->getBaseName();
Richard Smithc20d1442018-08-20 20:14:49 +00001603 }
1604};
1605
1606class DtorName : public Node {
1607 const Node *Base;
1608
1609public:
1610 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1611
1612 template<typename Fn> void match(Fn F) const { F(Base); }
1613
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001614 void printLeft(OutputBuffer &OB) const override {
1615 OB += "~";
1616 Base->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001617 }
1618};
1619
1620class UnnamedTypeName : public Node {
1621 const StringView Count;
1622
1623public:
1624 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1625
1626 template<typename Fn> void match(Fn F) const { F(Count); }
1627
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001628 void printLeft(OutputBuffer &OB) const override {
1629 OB += "'unnamed";
1630 OB += Count;
1631 OB += "\'";
Richard Smithc20d1442018-08-20 20:14:49 +00001632 }
1633};
1634
1635class ClosureTypeName : public Node {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001636 NodeArray TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00001637 NodeArray Params;
1638 StringView Count;
1639
1640public:
Richard Smithdf1c14c2019-09-06 23:53:21 +00001641 ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_,
1642 StringView Count_)
1643 : Node(KClosureTypeName), TemplateParams(TemplateParams_),
1644 Params(Params_), Count(Count_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001645
Richard Smithdf1c14c2019-09-06 23:53:21 +00001646 template<typename Fn> void match(Fn F) const {
1647 F(TemplateParams, Params, Count);
1648 }
1649
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001650 void printDeclarator(OutputBuffer &OB) const {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001651 if (!TemplateParams.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001652 OB += "<";
1653 TemplateParams.printWithComma(OB);
1654 OB += ">";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001655 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001656 OB += "(";
1657 Params.printWithComma(OB);
1658 OB += ")";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001659 }
Richard Smithc20d1442018-08-20 20:14:49 +00001660
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001661 void printLeft(OutputBuffer &OB) const override {
1662 OB += "\'lambda";
1663 OB += Count;
1664 OB += "\'";
1665 printDeclarator(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001666 }
1667};
1668
1669class StructuredBindingName : public Node {
1670 NodeArray Bindings;
1671public:
1672 StructuredBindingName(NodeArray Bindings_)
1673 : Node(KStructuredBindingName), Bindings(Bindings_) {}
1674
1675 template<typename Fn> void match(Fn F) const { F(Bindings); }
1676
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001677 void printLeft(OutputBuffer &OB) const override {
1678 OB += '[';
1679 Bindings.printWithComma(OB);
1680 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00001681 }
1682};
1683
1684// -- Expression Nodes --
1685
1686class BinaryExpr : public Node {
1687 const Node *LHS;
1688 const StringView InfixOperator;
1689 const Node *RHS;
1690
1691public:
1692 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
1693 : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
1694 }
1695
1696 template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
1697
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001698 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001699 // might be a template argument expression, then we need to disambiguate
1700 // with parens.
1701 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001702 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +00001703
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001704 OB += "(";
1705 LHS->print(OB);
1706 OB += ") ";
1707 OB += InfixOperator;
1708 OB += " (";
1709 RHS->print(OB);
1710 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001711
1712 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001713 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001714 }
1715};
1716
1717class ArraySubscriptExpr : public Node {
1718 const Node *Op1;
1719 const Node *Op2;
1720
1721public:
1722 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
1723 : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
1724
1725 template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
1726
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001727 void printLeft(OutputBuffer &OB) const override {
1728 OB += "(";
1729 Op1->print(OB);
1730 OB += ")[";
1731 Op2->print(OB);
1732 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001733 }
1734};
1735
1736class PostfixExpr : public Node {
1737 const Node *Child;
1738 const StringView Operator;
1739
1740public:
1741 PostfixExpr(const Node *Child_, StringView Operator_)
1742 : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
1743
1744 template<typename Fn> void match(Fn F) const { F(Child, Operator); }
1745
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001746 void printLeft(OutputBuffer &OB) const override {
1747 OB += "(";
1748 Child->print(OB);
1749 OB += ")";
1750 OB += Operator;
Richard Smithc20d1442018-08-20 20:14:49 +00001751 }
1752};
1753
1754class ConditionalExpr : public Node {
1755 const Node *Cond;
1756 const Node *Then;
1757 const Node *Else;
1758
1759public:
1760 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
1761 : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
1762
1763 template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
1764
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001765 void printLeft(OutputBuffer &OB) const override {
1766 OB += "(";
1767 Cond->print(OB);
1768 OB += ") ? (";
1769 Then->print(OB);
1770 OB += ") : (";
1771 Else->print(OB);
1772 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001773 }
1774};
1775
1776class MemberExpr : public Node {
1777 const Node *LHS;
1778 const StringView Kind;
1779 const Node *RHS;
1780
1781public:
1782 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
1783 : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1784
1785 template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
1786
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001787 void printLeft(OutputBuffer &OB) const override {
1788 LHS->print(OB);
1789 OB += Kind;
1790 RHS->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001791 }
1792};
1793
Richard Smith1865d2f2020-10-22 19:29:36 -07001794class SubobjectExpr : public Node {
1795 const Node *Type;
1796 const Node *SubExpr;
1797 StringView Offset;
1798 NodeArray UnionSelectors;
1799 bool OnePastTheEnd;
1800
1801public:
1802 SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_,
1803 NodeArray UnionSelectors_, bool OnePastTheEnd_)
1804 : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_),
1805 UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {}
1806
1807 template<typename Fn> void match(Fn F) const {
1808 F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd);
1809 }
1810
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001811 void printLeft(OutputBuffer &OB) const override {
1812 SubExpr->print(OB);
1813 OB += ".<";
1814 Type->print(OB);
1815 OB += " at offset ";
Richard Smith1865d2f2020-10-22 19:29:36 -07001816 if (Offset.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001817 OB += "0";
Richard Smith1865d2f2020-10-22 19:29:36 -07001818 } else if (Offset[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001819 OB += "-";
1820 OB += Offset.dropFront();
Richard Smith1865d2f2020-10-22 19:29:36 -07001821 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001822 OB += Offset;
Richard Smith1865d2f2020-10-22 19:29:36 -07001823 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001824 OB += ">";
Richard Smith1865d2f2020-10-22 19:29:36 -07001825 }
1826};
1827
Richard Smithc20d1442018-08-20 20:14:49 +00001828class EnclosingExpr : public Node {
1829 const StringView Prefix;
1830 const Node *Infix;
1831 const StringView Postfix;
1832
1833public:
1834 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1835 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1836 Postfix(Postfix_) {}
1837
1838 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1839
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001840 void printLeft(OutputBuffer &OB) const override {
1841 OB += Prefix;
1842 Infix->print(OB);
1843 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +00001844 }
1845};
1846
1847class CastExpr : public Node {
1848 // cast_kind<to>(from)
1849 const StringView CastKind;
1850 const Node *To;
1851 const Node *From;
1852
1853public:
1854 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1855 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1856
1857 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1858
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001859 void printLeft(OutputBuffer &OB) const override {
1860 OB += CastKind;
1861 OB += "<";
1862 To->printLeft(OB);
1863 OB += ">(";
1864 From->printLeft(OB);
1865 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001866 }
1867};
1868
1869class SizeofParamPackExpr : public Node {
1870 const Node *Pack;
1871
1872public:
1873 SizeofParamPackExpr(const Node *Pack_)
1874 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1875
1876 template<typename Fn> void match(Fn F) const { F(Pack); }
1877
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001878 void printLeft(OutputBuffer &OB) const override {
1879 OB += "sizeof...(";
Richard Smithc20d1442018-08-20 20:14:49 +00001880 ParameterPackExpansion PPE(Pack);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001881 PPE.printLeft(OB);
1882 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001883 }
1884};
1885
1886class CallExpr : public Node {
1887 const Node *Callee;
1888 NodeArray Args;
1889
1890public:
1891 CallExpr(const Node *Callee_, NodeArray Args_)
1892 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1893
1894 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1895
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001896 void printLeft(OutputBuffer &OB) const override {
1897 Callee->print(OB);
1898 OB += "(";
1899 Args.printWithComma(OB);
1900 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001901 }
1902};
1903
1904class NewExpr : public Node {
1905 // new (expr_list) type(init_list)
1906 NodeArray ExprList;
1907 Node *Type;
1908 NodeArray InitList;
1909 bool IsGlobal; // ::operator new ?
1910 bool IsArray; // new[] ?
1911public:
1912 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1913 bool IsArray_)
1914 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1915 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1916
1917 template<typename Fn> void match(Fn F) const {
1918 F(ExprList, Type, InitList, IsGlobal, IsArray);
1919 }
1920
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001921 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001922 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001923 OB += "::operator ";
1924 OB += "new";
Richard Smithc20d1442018-08-20 20:14:49 +00001925 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001926 OB += "[]";
1927 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00001928 if (!ExprList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001929 OB += "(";
1930 ExprList.printWithComma(OB);
1931 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001932 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001933 Type->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001934 if (!InitList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001935 OB += "(";
1936 InitList.printWithComma(OB);
1937 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001938 }
Richard Smithc20d1442018-08-20 20:14:49 +00001939 }
1940};
1941
1942class DeleteExpr : public Node {
1943 Node *Op;
1944 bool IsGlobal;
1945 bool IsArray;
1946
1947public:
1948 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1949 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1950
1951 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1952
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001953 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001954 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001955 OB += "::";
1956 OB += "delete";
Richard Smithc20d1442018-08-20 20:14:49 +00001957 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001958 OB += "[] ";
1959 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001960 }
1961};
1962
1963class PrefixExpr : public Node {
1964 StringView Prefix;
1965 Node *Child;
1966
1967public:
1968 PrefixExpr(StringView Prefix_, Node *Child_)
1969 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1970
1971 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1972
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001973 void printLeft(OutputBuffer &OB) const override {
1974 OB += Prefix;
1975 OB += "(";
1976 Child->print(OB);
1977 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001978 }
1979};
1980
1981class FunctionParam : public Node {
1982 StringView Number;
1983
1984public:
1985 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
1986
1987 template<typename Fn> void match(Fn F) const { F(Number); }
1988
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001989 void printLeft(OutputBuffer &OB) const override {
1990 OB += "fp";
1991 OB += Number;
Richard Smithc20d1442018-08-20 20:14:49 +00001992 }
1993};
1994
1995class ConversionExpr : public Node {
1996 const Node *Type;
1997 NodeArray Expressions;
1998
1999public:
2000 ConversionExpr(const Node *Type_, NodeArray Expressions_)
2001 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
2002
2003 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
2004
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002005 void printLeft(OutputBuffer &OB) const override {
2006 OB += "(";
2007 Type->print(OB);
2008 OB += ")(";
2009 Expressions.printWithComma(OB);
2010 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002011 }
2012};
2013
Richard Smith1865d2f2020-10-22 19:29:36 -07002014class PointerToMemberConversionExpr : public Node {
2015 const Node *Type;
2016 const Node *SubExpr;
2017 StringView Offset;
2018
2019public:
2020 PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_,
2021 StringView Offset_)
2022 : Node(KPointerToMemberConversionExpr), Type(Type_), SubExpr(SubExpr_),
2023 Offset(Offset_) {}
2024
2025 template<typename Fn> void match(Fn F) const { F(Type, SubExpr, Offset); }
2026
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002027 void printLeft(OutputBuffer &OB) const override {
2028 OB += "(";
2029 Type->print(OB);
2030 OB += ")(";
2031 SubExpr->print(OB);
2032 OB += ")";
Richard Smith1865d2f2020-10-22 19:29:36 -07002033 }
2034};
2035
Richard Smithc20d1442018-08-20 20:14:49 +00002036class InitListExpr : public Node {
2037 const Node *Ty;
2038 NodeArray Inits;
2039public:
2040 InitListExpr(const Node *Ty_, NodeArray Inits_)
2041 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
2042
2043 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
2044
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002045 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002046 if (Ty)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002047 Ty->print(OB);
2048 OB += '{';
2049 Inits.printWithComma(OB);
2050 OB += '}';
Richard Smithc20d1442018-08-20 20:14:49 +00002051 }
2052};
2053
2054class BracedExpr : public Node {
2055 const Node *Elem;
2056 const Node *Init;
2057 bool IsArray;
2058public:
2059 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
2060 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
2061
2062 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
2063
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002064 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002065 if (IsArray) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002066 OB += '[';
2067 Elem->print(OB);
2068 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002069 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002070 OB += '.';
2071 Elem->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002072 }
2073 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002074 OB += " = ";
2075 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002076 }
2077};
2078
2079class BracedRangeExpr : public Node {
2080 const Node *First;
2081 const Node *Last;
2082 const Node *Init;
2083public:
2084 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
2085 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
2086
2087 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
2088
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002089 void printLeft(OutputBuffer &OB) const override {
2090 OB += '[';
2091 First->print(OB);
2092 OB += " ... ";
2093 Last->print(OB);
2094 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002095 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002096 OB += " = ";
2097 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002098 }
2099};
2100
2101class FoldExpr : public Node {
2102 const Node *Pack, *Init;
2103 StringView OperatorName;
2104 bool IsLeftFold;
2105
2106public:
2107 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
2108 const Node *Init_)
2109 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
2110 IsLeftFold(IsLeftFold_) {}
2111
2112 template<typename Fn> void match(Fn F) const {
2113 F(IsLeftFold, OperatorName, Pack, Init);
2114 }
2115
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002116 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002117 auto PrintPack = [&] {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002118 OB += '(';
2119 ParameterPackExpansion(Pack).print(OB);
2120 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002121 };
2122
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002123 OB += '(';
Richard Smithc20d1442018-08-20 20:14:49 +00002124
2125 if (IsLeftFold) {
2126 // init op ... op pack
2127 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002128 Init->print(OB);
2129 OB += ' ';
2130 OB += OperatorName;
2131 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002132 }
2133 // ... op pack
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002134 OB += "... ";
2135 OB += OperatorName;
2136 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002137 PrintPack();
2138 } else { // !IsLeftFold
2139 // pack op ...
2140 PrintPack();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002141 OB += ' ';
2142 OB += OperatorName;
2143 OB += " ...";
Richard Smithc20d1442018-08-20 20:14:49 +00002144 // pack op ... op init
2145 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002146 OB += ' ';
2147 OB += OperatorName;
2148 OB += ' ';
2149 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002150 }
2151 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002152 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002153 }
2154};
2155
2156class ThrowExpr : public Node {
2157 const Node *Op;
2158
2159public:
2160 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
2161
2162 template<typename Fn> void match(Fn F) const { F(Op); }
2163
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002164 void printLeft(OutputBuffer &OB) const override {
2165 OB += "throw ";
2166 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002167 }
2168};
2169
2170class BoolExpr : public Node {
2171 bool Value;
2172
2173public:
2174 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
2175
2176 template<typename Fn> void match(Fn F) const { F(Value); }
2177
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002178 void printLeft(OutputBuffer &OB) const override {
2179 OB += Value ? StringView("true") : StringView("false");
Richard Smithc20d1442018-08-20 20:14:49 +00002180 }
2181};
2182
Richard Smithdf1c14c2019-09-06 23:53:21 +00002183class StringLiteral : public Node {
2184 const Node *Type;
2185
2186public:
2187 StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
2188
2189 template<typename Fn> void match(Fn F) const { F(Type); }
2190
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002191 void printLeft(OutputBuffer &OB) const override {
2192 OB += "\"<";
2193 Type->print(OB);
2194 OB += ">\"";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002195 }
2196};
2197
2198class LambdaExpr : public Node {
2199 const Node *Type;
2200
Richard Smithdf1c14c2019-09-06 23:53:21 +00002201public:
2202 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2203
2204 template<typename Fn> void match(Fn F) const { F(Type); }
2205
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002206 void printLeft(OutputBuffer &OB) const override {
2207 OB += "[]";
Richard Smithfb917462019-09-09 22:26:04 +00002208 if (Type->getKind() == KClosureTypeName)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002209 static_cast<const ClosureTypeName *>(Type)->printDeclarator(OB);
2210 OB += "{...}";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002211 }
2212};
2213
Erik Pilkington0a170f12020-05-13 14:13:37 -04002214class EnumLiteral : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +00002215 // ty(integer)
2216 const Node *Ty;
2217 StringView Integer;
2218
2219public:
Erik Pilkington0a170f12020-05-13 14:13:37 -04002220 EnumLiteral(const Node *Ty_, StringView Integer_)
2221 : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00002222
2223 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2224
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002225 void printLeft(OutputBuffer &OB) const override {
2226 OB << "(";
2227 Ty->print(OB);
2228 OB << ")";
Erik Pilkington0a170f12020-05-13 14:13:37 -04002229
2230 if (Integer[0] == 'n')
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002231 OB << "-" << Integer.dropFront(1);
Erik Pilkington0a170f12020-05-13 14:13:37 -04002232 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002233 OB << Integer;
Richard Smithc20d1442018-08-20 20:14:49 +00002234 }
2235};
2236
2237class IntegerLiteral : public Node {
2238 StringView Type;
2239 StringView Value;
2240
2241public:
2242 IntegerLiteral(StringView Type_, StringView Value_)
2243 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2244
2245 template<typename Fn> void match(Fn F) const { F(Type, Value); }
2246
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002247 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002248 if (Type.size() > 3) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002249 OB += "(";
2250 OB += Type;
2251 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002252 }
2253
2254 if (Value[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002255 OB += "-";
2256 OB += Value.dropFront(1);
Richard Smithc20d1442018-08-20 20:14:49 +00002257 } else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002258 OB += Value;
Richard Smithc20d1442018-08-20 20:14:49 +00002259
2260 if (Type.size() <= 3)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002261 OB += Type;
Richard Smithc20d1442018-08-20 20:14:49 +00002262 }
2263};
2264
2265template <class Float> struct FloatData;
2266
2267namespace float_literal_impl {
2268constexpr Node::Kind getFloatLiteralKind(float *) {
2269 return Node::KFloatLiteral;
2270}
2271constexpr Node::Kind getFloatLiteralKind(double *) {
2272 return Node::KDoubleLiteral;
2273}
2274constexpr Node::Kind getFloatLiteralKind(long double *) {
2275 return Node::KLongDoubleLiteral;
2276}
2277}
2278
2279template <class Float> class FloatLiteralImpl : public Node {
2280 const StringView Contents;
2281
2282 static constexpr Kind KindForClass =
2283 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2284
2285public:
2286 FloatLiteralImpl(StringView Contents_)
2287 : Node(KindForClass), Contents(Contents_) {}
2288
2289 template<typename Fn> void match(Fn F) const { F(Contents); }
2290
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002291 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002292 const char *first = Contents.begin();
2293 const char *last = Contents.end() + 1;
2294
2295 const size_t N = FloatData<Float>::mangled_size;
2296 if (static_cast<std::size_t>(last - first) > N) {
2297 last = first + N;
2298 union {
2299 Float value;
2300 char buf[sizeof(Float)];
2301 };
2302 const char *t = first;
2303 char *e = buf;
2304 for (; t != last; ++t, ++e) {
2305 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2306 : static_cast<unsigned>(*t - 'a' + 10);
2307 ++t;
2308 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2309 : static_cast<unsigned>(*t - 'a' + 10);
2310 *e = static_cast<char>((d1 << 4) + d0);
2311 }
2312#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2313 std::reverse(buf, e);
2314#endif
2315 char num[FloatData<Float>::max_demangled_size] = {0};
2316 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002317 OB += StringView(num, num + n);
Richard Smithc20d1442018-08-20 20:14:49 +00002318 }
2319 }
2320};
2321
2322using FloatLiteral = FloatLiteralImpl<float>;
2323using DoubleLiteral = FloatLiteralImpl<double>;
2324using LongDoubleLiteral = FloatLiteralImpl<long double>;
2325
2326/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2327/// appropriate derived class.
2328template<typename Fn>
2329void Node::visit(Fn F) const {
2330 switch (K) {
2331#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2332 FOR_EACH_NODE_KIND(CASE)
2333#undef CASE
2334 }
2335 assert(0 && "unknown mangling node kind");
2336}
2337
2338/// Determine the kind of a node from its type.
2339template<typename NodeT> struct NodeKind;
2340#define SPECIALIZATION(X) \
2341 template<> struct NodeKind<X> { \
2342 static constexpr Node::Kind Kind = Node::K##X; \
2343 static constexpr const char *name() { return #X; } \
2344 };
2345FOR_EACH_NODE_KIND(SPECIALIZATION)
2346#undef SPECIALIZATION
2347
2348#undef FOR_EACH_NODE_KIND
2349
Pavel Labathba825192018-10-16 14:29:14 +00002350template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002351 const char *First;
2352 const char *Last;
2353
2354 // Name stack, this is used by the parser to hold temporary names that were
2355 // parsed. The parser collapses multiple names into new nodes to construct
2356 // the AST. Once the parser is finished, names.size() == 1.
2357 PODSmallVector<Node *, 32> Names;
2358
2359 // Substitution table. Itanium supports name substitutions as a means of
2360 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2361 // table.
2362 PODSmallVector<Node *, 32> Subs;
2363
Richard Smithdf1c14c2019-09-06 23:53:21 +00002364 using TemplateParamList = PODSmallVector<Node *, 8>;
2365
2366 class ScopedTemplateParamList {
2367 AbstractManglingParser *Parser;
2368 size_t OldNumTemplateParamLists;
2369 TemplateParamList Params;
2370
2371 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04002372 ScopedTemplateParamList(AbstractManglingParser *TheParser)
2373 : Parser(TheParser),
2374 OldNumTemplateParamLists(TheParser->TemplateParams.size()) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002375 Parser->TemplateParams.push_back(&Params);
2376 }
2377 ~ScopedTemplateParamList() {
2378 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2379 Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2380 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002381 };
2382
Richard Smithc20d1442018-08-20 20:14:49 +00002383 // Template parameter table. Like the above, but referenced like "T42_".
2384 // This has a smaller size compared to Subs and Names because it can be
2385 // stored on the stack.
Richard Smithdf1c14c2019-09-06 23:53:21 +00002386 TemplateParamList OuterTemplateParams;
2387
2388 // Lists of template parameters indexed by template parameter depth,
2389 // referenced like "TL2_4_". If nonempty, element 0 is always
2390 // OuterTemplateParams; inner elements are always template parameter lists of
2391 // lambda expressions. For a generic lambda with no explicit template
2392 // parameter list, the corresponding parameter list pointer will be null.
2393 PODSmallVector<TemplateParamList *, 4> TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00002394
2395 // Set of unresolved forward <template-param> references. These can occur in a
2396 // conversion operator's type, and are resolved in the enclosing <encoding>.
2397 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2398
Richard Smithc20d1442018-08-20 20:14:49 +00002399 bool TryToParseTemplateArgs = true;
2400 bool PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002401 size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2402
2403 unsigned NumSyntheticTemplateParameters[3] = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002404
2405 Alloc ASTAllocator;
2406
Pavel Labathba825192018-10-16 14:29:14 +00002407 AbstractManglingParser(const char *First_, const char *Last_)
2408 : First(First_), Last(Last_) {}
2409
2410 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002411
2412 void reset(const char *First_, const char *Last_) {
2413 First = First_;
2414 Last = Last_;
2415 Names.clear();
2416 Subs.clear();
2417 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002418 ParsingLambdaParamsAtLevel = (size_t)-1;
Richard Smithc20d1442018-08-20 20:14:49 +00002419 TryToParseTemplateArgs = true;
2420 PermitForwardTemplateReferences = false;
Richard Smith9a2307a2019-09-07 00:11:53 +00002421 for (int I = 0; I != 3; ++I)
2422 NumSyntheticTemplateParameters[I] = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00002423 ASTAllocator.reset();
2424 }
2425
Richard Smithb485b352018-08-24 23:30:26 +00002426 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002427 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2428 }
2429
2430 template <class It> NodeArray makeNodeArray(It begin, It end) {
2431 size_t sz = static_cast<size_t>(end - begin);
2432 void *mem = ASTAllocator.allocateNodeArray(sz);
2433 Node **data = new (mem) Node *[sz];
2434 std::copy(begin, end, data);
2435 return NodeArray(data, sz);
2436 }
2437
2438 NodeArray popTrailingNodeArray(size_t FromPosition) {
2439 assert(FromPosition <= Names.size());
2440 NodeArray res =
2441 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2442 Names.dropBack(FromPosition);
2443 return res;
2444 }
2445
2446 bool consumeIf(StringView S) {
2447 if (StringView(First, Last).startsWith(S)) {
2448 First += S.size();
2449 return true;
2450 }
2451 return false;
2452 }
2453
2454 bool consumeIf(char C) {
2455 if (First != Last && *First == C) {
2456 ++First;
2457 return true;
2458 }
2459 return false;
2460 }
2461
2462 char consume() { return First != Last ? *First++ : '\0'; }
2463
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08002464 char look(unsigned Lookahead = 0) const {
Richard Smithc20d1442018-08-20 20:14:49 +00002465 if (static_cast<size_t>(Last - First) <= Lookahead)
2466 return '\0';
2467 return First[Lookahead];
2468 }
2469
2470 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2471
2472 StringView parseNumber(bool AllowNegative = false);
2473 Qualifiers parseCVQualifiers();
2474 bool parsePositiveInteger(size_t *Out);
2475 StringView parseBareSourceName();
2476
2477 bool parseSeqId(size_t *Out);
2478 Node *parseSubstitution();
2479 Node *parseTemplateParam();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002480 Node *parseTemplateParamDecl();
Richard Smithc20d1442018-08-20 20:14:49 +00002481 Node *parseTemplateArgs(bool TagTemplates = false);
2482 Node *parseTemplateArg();
2483
2484 /// Parse the <expr> production.
2485 Node *parseExpr();
2486 Node *parsePrefixExpr(StringView Kind);
2487 Node *parseBinaryExpr(StringView Kind);
2488 Node *parseIntegerLiteral(StringView Lit);
2489 Node *parseExprPrimary();
2490 template <class Float> Node *parseFloatingLiteral();
2491 Node *parseFunctionParam();
2492 Node *parseNewExpr();
2493 Node *parseConversionExpr();
2494 Node *parseBracedExpr();
2495 Node *parseFoldExpr();
Richard Smith1865d2f2020-10-22 19:29:36 -07002496 Node *parsePointerToMemberConversionExpr();
2497 Node *parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00002498
2499 /// Parse the <type> production.
2500 Node *parseType();
2501 Node *parseFunctionType();
2502 Node *parseVectorType();
2503 Node *parseDecltype();
2504 Node *parseArrayType();
2505 Node *parsePointerToMemberType();
2506 Node *parseClassEnumType();
2507 Node *parseQualifiedType();
2508
2509 Node *parseEncoding();
2510 bool parseCallOffset();
2511 Node *parseSpecialName();
2512
2513 /// Holds some extra information about a <name> that is being parsed. This
2514 /// information is only pertinent if the <name> refers to an <encoding>.
2515 struct NameState {
2516 bool CtorDtorConversion = false;
2517 bool EndsWithTemplateArgs = false;
2518 Qualifiers CVQualifiers = QualNone;
2519 FunctionRefQual ReferenceQualifier = FrefQualNone;
2520 size_t ForwardTemplateRefsBegin;
2521
Pavel Labathba825192018-10-16 14:29:14 +00002522 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002523 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2524 };
2525
2526 bool resolveForwardTemplateRefs(NameState &State) {
2527 size_t I = State.ForwardTemplateRefsBegin;
2528 size_t E = ForwardTemplateRefs.size();
2529 for (; I < E; ++I) {
2530 size_t Idx = ForwardTemplateRefs[I]->Index;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002531 if (TemplateParams.empty() || !TemplateParams[0] ||
2532 Idx >= TemplateParams[0]->size())
Richard Smithc20d1442018-08-20 20:14:49 +00002533 return true;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002534 ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
Richard Smithc20d1442018-08-20 20:14:49 +00002535 }
2536 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2537 return false;
2538 }
2539
2540 /// Parse the <name> production>
2541 Node *parseName(NameState *State = nullptr);
2542 Node *parseLocalName(NameState *State);
2543 Node *parseOperatorName(NameState *State);
2544 Node *parseUnqualifiedName(NameState *State);
2545 Node *parseUnnamedTypeName(NameState *State);
2546 Node *parseSourceName(NameState *State);
2547 Node *parseUnscopedName(NameState *State);
2548 Node *parseNestedName(NameState *State);
2549 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2550
2551 Node *parseAbiTags(Node *N);
2552
2553 /// Parse the <unresolved-name> production.
2554 Node *parseUnresolvedName();
2555 Node *parseSimpleId();
2556 Node *parseBaseUnresolvedName();
2557 Node *parseUnresolvedType();
2558 Node *parseDestructorName();
2559
2560 /// Top-level entry point into the parser.
2561 Node *parse();
2562};
2563
2564const char* parse_discriminator(const char* first, const char* last);
2565
2566// <name> ::= <nested-name> // N
2567// ::= <local-name> # See Scope Encoding below // Z
2568// ::= <unscoped-template-name> <template-args>
2569// ::= <unscoped-name>
2570//
2571// <unscoped-template-name> ::= <unscoped-name>
2572// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002573template <typename Derived, typename Alloc>
2574Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002575 consumeIf('L'); // extension
2576
2577 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002578 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002579 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002580 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002581
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002582 Node *Result = nullptr;
2583 bool IsSubst = look() == 'S' && look(1) != 't';
2584 if (IsSubst) {
2585 // A substitution must lead to:
2586 // ::= <unscoped-template-name> <template-args>
2587 Result = getDerived().parseSubstitution();
2588 } else {
2589 // An unscoped name can be one of:
2590 // ::= <unscoped-name>
2591 // ::= <unscoped-template-name> <template-args>
2592 Result = getDerived().parseUnscopedName(State);
2593 }
2594 if (Result == nullptr)
2595 return nullptr;
2596
2597 if (look() == 'I') {
2598 // ::= <unscoped-template-name> <template-args>
2599 if (!IsSubst)
2600 // An unscoped-template-name is substitutable.
2601 Subs.push_back(Result);
Pavel Labathba825192018-10-16 14:29:14 +00002602 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002603 if (TA == nullptr)
2604 return nullptr;
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002605 if (State)
2606 State->EndsWithTemplateArgs = true;
2607 Result = make<NameWithTemplateArgs>(Result, TA);
2608 } else if (IsSubst) {
2609 // The substitution case must be followed by <template-args>.
2610 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002611 }
2612
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002613 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00002614}
2615
2616// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2617// := Z <function encoding> E s [<discriminator>]
2618// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002619template <typename Derived, typename Alloc>
2620Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002621 if (!consumeIf('Z'))
2622 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002623 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002624 if (Encoding == nullptr || !consumeIf('E'))
2625 return nullptr;
2626
2627 if (consumeIf('s')) {
2628 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002629 auto *StringLitName = make<NameType>("string literal");
2630 if (!StringLitName)
2631 return nullptr;
2632 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002633 }
2634
2635 if (consumeIf('d')) {
2636 parseNumber(true);
2637 if (!consumeIf('_'))
2638 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002639 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002640 if (N == nullptr)
2641 return nullptr;
2642 return make<LocalName>(Encoding, N);
2643 }
2644
Pavel Labathba825192018-10-16 14:29:14 +00002645 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002646 if (Entity == nullptr)
2647 return nullptr;
2648 First = parse_discriminator(First, Last);
2649 return make<LocalName>(Encoding, Entity);
2650}
2651
2652// <unscoped-name> ::= <unqualified-name>
2653// ::= St <unqualified-name> # ::std::
2654// extension ::= StL<unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00002655template <typename Derived, typename Alloc>
2656Node *
2657AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002658 bool IsStd = consumeIf("St");
2659 if (IsStd)
2660 consumeIf('L');
2661
2662 Node *Result = getDerived().parseUnqualifiedName(State);
2663 if (Result == nullptr)
2664 return nullptr;
Nathan Sidwell200e97c2022-01-21 11:37:01 -08002665 if (IsStd) {
2666 if (auto *Std = make<NameType>("std"))
2667 Result = make<NestedName>(Std, Result);
2668 else
2669 return nullptr;
2670 }
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002671
2672 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00002673}
2674
2675// <unqualified-name> ::= <operator-name> [abi-tags]
2676// ::= <ctor-dtor-name>
2677// ::= <source-name>
2678// ::= <unnamed-type-name>
2679// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002680template <typename Derived, typename Alloc>
2681Node *
2682AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002683 // <ctor-dtor-name>s are special-cased in parseNestedName().
2684 Node *Result;
2685 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002686 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002687 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002688 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002689 else if (consumeIf("DC")) {
2690 size_t BindingsBegin = Names.size();
2691 do {
Pavel Labathba825192018-10-16 14:29:14 +00002692 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002693 if (Binding == nullptr)
2694 return nullptr;
2695 Names.push_back(Binding);
2696 } while (!consumeIf('E'));
2697 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2698 } else
Pavel Labathba825192018-10-16 14:29:14 +00002699 Result = getDerived().parseOperatorName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002700 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002701 Result = getDerived().parseAbiTags(Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002702 return Result;
2703}
2704
2705// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2706// ::= <closure-type-name>
2707//
2708// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2709//
2710// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002711template <typename Derived, typename Alloc>
2712Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002713AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2714 // <template-params> refer to the innermost <template-args>. Clear out any
2715 // outer args that we may have inserted into TemplateParams.
2716 if (State != nullptr)
2717 TemplateParams.clear();
2718
Richard Smithc20d1442018-08-20 20:14:49 +00002719 if (consumeIf("Ut")) {
2720 StringView Count = parseNumber();
2721 if (!consumeIf('_'))
2722 return nullptr;
2723 return make<UnnamedTypeName>(Count);
2724 }
2725 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002726 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2727 TemplateParams.size());
2728 ScopedTemplateParamList LambdaTemplateParams(this);
2729
2730 size_t ParamsBegin = Names.size();
2731 while (look() == 'T' &&
2732 StringView("yptn").find(look(1)) != StringView::npos) {
2733 Node *T = parseTemplateParamDecl();
2734 if (!T)
2735 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002736 Names.push_back(T);
2737 }
2738 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2739
2740 // FIXME: If TempParams is empty and none of the function parameters
2741 // includes 'auto', we should remove LambdaTemplateParams from the
2742 // TemplateParams list. Unfortunately, we don't find out whether there are
2743 // any 'auto' parameters until too late in an example such as:
2744 //
2745 // template<typename T> void f(
2746 // decltype([](decltype([]<typename T>(T v) {}),
2747 // auto) {})) {}
2748 // template<typename T> void f(
2749 // decltype([](decltype([]<typename T>(T w) {}),
2750 // int) {})) {}
2751 //
2752 // Here, the type of v is at level 2 but the type of w is at level 1. We
2753 // don't find this out until we encounter the type of the next parameter.
2754 //
2755 // However, compilers can't actually cope with the former example in
2756 // practice, and it's likely to be made ill-formed in future, so we don't
2757 // need to support it here.
2758 //
2759 // If we encounter an 'auto' in the function parameter types, we will
2760 // recreate a template parameter scope for it, but any intervening lambdas
2761 // will be parsed in the 'wrong' template parameter depth.
2762 if (TempParams.empty())
2763 TemplateParams.pop_back();
2764
Richard Smithc20d1442018-08-20 20:14:49 +00002765 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002766 do {
Pavel Labathba825192018-10-16 14:29:14 +00002767 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002768 if (P == nullptr)
2769 return nullptr;
2770 Names.push_back(P);
2771 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002772 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002773 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2774
Richard Smithc20d1442018-08-20 20:14:49 +00002775 StringView Count = parseNumber();
2776 if (!consumeIf('_'))
2777 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002778 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002779 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002780 if (consumeIf("Ub")) {
2781 (void)parseNumber();
2782 if (!consumeIf('_'))
2783 return nullptr;
2784 return make<NameType>("'block-literal'");
2785 }
Richard Smithc20d1442018-08-20 20:14:49 +00002786 return nullptr;
2787}
2788
2789// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002790template <typename Derived, typename Alloc>
2791Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002792 size_t Length = 0;
2793 if (parsePositiveInteger(&Length))
2794 return nullptr;
2795 if (numLeft() < Length || Length == 0)
2796 return nullptr;
2797 StringView Name(First, First + Length);
2798 First += Length;
2799 if (Name.startsWith("_GLOBAL__N"))
2800 return make<NameType>("(anonymous namespace)");
2801 return make<NameType>(Name);
2802}
2803
2804// <operator-name> ::= aa # &&
2805// ::= ad # & (unary)
2806// ::= an # &
2807// ::= aN # &=
2808// ::= aS # =
2809// ::= cl # ()
2810// ::= cm # ,
2811// ::= co # ~
2812// ::= cv <type> # (cast)
2813// ::= da # delete[]
2814// ::= de # * (unary)
2815// ::= dl # delete
2816// ::= dv # /
2817// ::= dV # /=
2818// ::= eo # ^
2819// ::= eO # ^=
2820// ::= eq # ==
2821// ::= ge # >=
2822// ::= gt # >
2823// ::= ix # []
2824// ::= le # <=
2825// ::= li <source-name> # operator ""
2826// ::= ls # <<
2827// ::= lS # <<=
2828// ::= lt # <
2829// ::= mi # -
2830// ::= mI # -=
2831// ::= ml # *
2832// ::= mL # *=
2833// ::= mm # -- (postfix in <expression> context)
2834// ::= na # new[]
2835// ::= ne # !=
2836// ::= ng # - (unary)
2837// ::= nt # !
2838// ::= nw # new
2839// ::= oo # ||
2840// ::= or # |
2841// ::= oR # |=
2842// ::= pm # ->*
2843// ::= pl # +
2844// ::= pL # +=
2845// ::= pp # ++ (postfix in <expression> context)
2846// ::= ps # + (unary)
2847// ::= pt # ->
2848// ::= qu # ?
2849// ::= rm # %
2850// ::= rM # %=
2851// ::= rs # >>
2852// ::= rS # >>=
2853// ::= ss # <=> C++2a
2854// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002855template <typename Derived, typename Alloc>
2856Node *
2857AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002858 switch (look()) {
2859 case 'a':
2860 switch (look(1)) {
2861 case 'a':
2862 First += 2;
2863 return make<NameType>("operator&&");
2864 case 'd':
2865 case 'n':
2866 First += 2;
2867 return make<NameType>("operator&");
2868 case 'N':
2869 First += 2;
2870 return make<NameType>("operator&=");
2871 case 'S':
2872 First += 2;
2873 return make<NameType>("operator=");
2874 }
2875 return nullptr;
2876 case 'c':
2877 switch (look(1)) {
2878 case 'l':
2879 First += 2;
2880 return make<NameType>("operator()");
2881 case 'm':
2882 First += 2;
2883 return make<NameType>("operator,");
2884 case 'o':
2885 First += 2;
2886 return make<NameType>("operator~");
2887 // ::= cv <type> # (cast)
2888 case 'v': {
2889 First += 2;
2890 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2891 // If we're parsing an encoding, State != nullptr and the conversion
2892 // operators' <type> could have a <template-param> that refers to some
2893 // <template-arg>s further ahead in the mangled name.
2894 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2895 PermitForwardTemplateReferences ||
2896 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002897 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002898 if (Ty == nullptr)
2899 return nullptr;
2900 if (State) State->CtorDtorConversion = true;
2901 return make<ConversionOperatorType>(Ty);
2902 }
2903 }
2904 return nullptr;
2905 case 'd':
2906 switch (look(1)) {
2907 case 'a':
2908 First += 2;
2909 return make<NameType>("operator delete[]");
2910 case 'e':
2911 First += 2;
2912 return make<NameType>("operator*");
2913 case 'l':
2914 First += 2;
2915 return make<NameType>("operator delete");
2916 case 'v':
2917 First += 2;
2918 return make<NameType>("operator/");
2919 case 'V':
2920 First += 2;
2921 return make<NameType>("operator/=");
2922 }
2923 return nullptr;
2924 case 'e':
2925 switch (look(1)) {
2926 case 'o':
2927 First += 2;
2928 return make<NameType>("operator^");
2929 case 'O':
2930 First += 2;
2931 return make<NameType>("operator^=");
2932 case 'q':
2933 First += 2;
2934 return make<NameType>("operator==");
2935 }
2936 return nullptr;
2937 case 'g':
2938 switch (look(1)) {
2939 case 'e':
2940 First += 2;
2941 return make<NameType>("operator>=");
2942 case 't':
2943 First += 2;
2944 return make<NameType>("operator>");
2945 }
2946 return nullptr;
2947 case 'i':
2948 if (look(1) == 'x') {
2949 First += 2;
2950 return make<NameType>("operator[]");
2951 }
2952 return nullptr;
2953 case 'l':
2954 switch (look(1)) {
2955 case 'e':
2956 First += 2;
2957 return make<NameType>("operator<=");
2958 // ::= li <source-name> # operator ""
2959 case 'i': {
2960 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002961 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002962 if (SN == nullptr)
2963 return nullptr;
2964 return make<LiteralOperator>(SN);
2965 }
2966 case 's':
2967 First += 2;
2968 return make<NameType>("operator<<");
2969 case 'S':
2970 First += 2;
2971 return make<NameType>("operator<<=");
2972 case 't':
2973 First += 2;
2974 return make<NameType>("operator<");
2975 }
2976 return nullptr;
2977 case 'm':
2978 switch (look(1)) {
2979 case 'i':
2980 First += 2;
2981 return make<NameType>("operator-");
2982 case 'I':
2983 First += 2;
2984 return make<NameType>("operator-=");
2985 case 'l':
2986 First += 2;
2987 return make<NameType>("operator*");
2988 case 'L':
2989 First += 2;
2990 return make<NameType>("operator*=");
2991 case 'm':
2992 First += 2;
2993 return make<NameType>("operator--");
2994 }
2995 return nullptr;
2996 case 'n':
2997 switch (look(1)) {
2998 case 'a':
2999 First += 2;
3000 return make<NameType>("operator new[]");
3001 case 'e':
3002 First += 2;
3003 return make<NameType>("operator!=");
3004 case 'g':
3005 First += 2;
3006 return make<NameType>("operator-");
3007 case 't':
3008 First += 2;
3009 return make<NameType>("operator!");
3010 case 'w':
3011 First += 2;
3012 return make<NameType>("operator new");
3013 }
3014 return nullptr;
3015 case 'o':
3016 switch (look(1)) {
3017 case 'o':
3018 First += 2;
3019 return make<NameType>("operator||");
3020 case 'r':
3021 First += 2;
3022 return make<NameType>("operator|");
3023 case 'R':
3024 First += 2;
3025 return make<NameType>("operator|=");
3026 }
3027 return nullptr;
3028 case 'p':
3029 switch (look(1)) {
3030 case 'm':
3031 First += 2;
3032 return make<NameType>("operator->*");
3033 case 'l':
3034 First += 2;
3035 return make<NameType>("operator+");
3036 case 'L':
3037 First += 2;
3038 return make<NameType>("operator+=");
3039 case 'p':
3040 First += 2;
3041 return make<NameType>("operator++");
3042 case 's':
3043 First += 2;
3044 return make<NameType>("operator+");
3045 case 't':
3046 First += 2;
3047 return make<NameType>("operator->");
3048 }
3049 return nullptr;
3050 case 'q':
3051 if (look(1) == 'u') {
3052 First += 2;
3053 return make<NameType>("operator?");
3054 }
3055 return nullptr;
3056 case 'r':
3057 switch (look(1)) {
3058 case 'm':
3059 First += 2;
3060 return make<NameType>("operator%");
3061 case 'M':
3062 First += 2;
3063 return make<NameType>("operator%=");
3064 case 's':
3065 First += 2;
3066 return make<NameType>("operator>>");
3067 case 'S':
3068 First += 2;
3069 return make<NameType>("operator>>=");
3070 }
3071 return nullptr;
3072 case 's':
3073 if (look(1) == 's') {
3074 First += 2;
3075 return make<NameType>("operator<=>");
3076 }
3077 return nullptr;
3078 // ::= v <digit> <source-name> # vendor extended operator
3079 case 'v':
3080 if (std::isdigit(look(1))) {
3081 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003082 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003083 if (SN == nullptr)
3084 return nullptr;
3085 return make<ConversionOperatorType>(SN);
3086 }
3087 return nullptr;
3088 }
3089 return nullptr;
3090}
3091
3092// <ctor-dtor-name> ::= C1 # complete object constructor
3093// ::= C2 # base object constructor
3094// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003095// extension ::= C4 # gcc old-style "[unified]" constructor
3096// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003097// ::= D0 # deleting destructor
3098// ::= D1 # complete object destructor
3099// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003100// extension ::= D4 # gcc old-style "[unified]" destructor
3101// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003102template <typename Derived, typename Alloc>
3103Node *
3104AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3105 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003106 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3107 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3108 switch (SSK) {
3109 case SpecialSubKind::string:
3110 case SpecialSubKind::istream:
3111 case SpecialSubKind::ostream:
3112 case SpecialSubKind::iostream:
3113 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003114 if (!SoFar)
3115 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003116 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003117 default:
3118 break;
3119 }
3120 }
3121
3122 if (consumeIf('C')) {
3123 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003124 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3125 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003126 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003127 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003128 ++First;
3129 if (State) State->CtorDtorConversion = true;
3130 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003131 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003132 return nullptr;
3133 }
Nico Weber29294792019-04-03 23:14:33 +00003134 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003135 }
3136
Nico Weber29294792019-04-03 23:14:33 +00003137 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3138 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003139 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003140 First += 2;
3141 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003142 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003143 }
3144
3145 return nullptr;
3146}
3147
3148// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3149// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3150//
3151// <prefix> ::= <prefix> <unqualified-name>
3152// ::= <template-prefix> <template-args>
3153// ::= <template-param>
3154// ::= <decltype>
3155// ::= # empty
3156// ::= <substitution>
3157// ::= <prefix> <data-member-prefix>
3158// extension ::= L
3159//
3160// <data-member-prefix> := <member source-name> [<template-args>] M
3161//
3162// <template-prefix> ::= <prefix> <template unqualified-name>
3163// ::= <template-param>
3164// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003165template <typename Derived, typename Alloc>
3166Node *
3167AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003168 if (!consumeIf('N'))
3169 return nullptr;
3170
3171 Qualifiers CVTmp = parseCVQualifiers();
3172 if (State) State->CVQualifiers = CVTmp;
3173
3174 if (consumeIf('O')) {
3175 if (State) State->ReferenceQualifier = FrefQualRValue;
3176 } else if (consumeIf('R')) {
3177 if (State) State->ReferenceQualifier = FrefQualLValue;
3178 } else
3179 if (State) State->ReferenceQualifier = FrefQualNone;
3180
3181 Node *SoFar = nullptr;
3182 auto PushComponent = [&](Node *Comp) {
Richard Smithb485b352018-08-24 23:30:26 +00003183 if (!Comp) return false;
Richard Smithc20d1442018-08-20 20:14:49 +00003184 if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
3185 else SoFar = Comp;
3186 if (State) State->EndsWithTemplateArgs = false;
Richard Smithb485b352018-08-24 23:30:26 +00003187 return SoFar != nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003188 };
3189
Richard Smithb485b352018-08-24 23:30:26 +00003190 if (consumeIf("St")) {
Richard Smithc20d1442018-08-20 20:14:49 +00003191 SoFar = make<NameType>("std");
Richard Smithb485b352018-08-24 23:30:26 +00003192 if (!SoFar)
3193 return nullptr;
3194 }
Richard Smithc20d1442018-08-20 20:14:49 +00003195
3196 while (!consumeIf('E')) {
3197 consumeIf('L'); // extension
3198
3199 // <data-member-prefix> := <member source-name> [<template-args>] M
3200 if (consumeIf('M')) {
3201 if (SoFar == nullptr)
3202 return nullptr;
3203 continue;
3204 }
3205
3206 // ::= <template-param>
3207 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003208 if (!PushComponent(getDerived().parseTemplateParam()))
Richard Smithc20d1442018-08-20 20:14:49 +00003209 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003210 Subs.push_back(SoFar);
3211 continue;
3212 }
3213
3214 // ::= <template-prefix> <template-args>
3215 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003216 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003217 if (TA == nullptr || SoFar == nullptr)
3218 return nullptr;
3219 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003220 if (!SoFar)
3221 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003222 if (State) State->EndsWithTemplateArgs = true;
3223 Subs.push_back(SoFar);
3224 continue;
3225 }
3226
3227 // ::= <decltype>
3228 if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
Pavel Labathba825192018-10-16 14:29:14 +00003229 if (!PushComponent(getDerived().parseDecltype()))
Richard Smithc20d1442018-08-20 20:14:49 +00003230 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003231 Subs.push_back(SoFar);
3232 continue;
3233 }
3234
3235 // ::= <substitution>
3236 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00003237 Node *S = getDerived().parseSubstitution();
Richard Smithb485b352018-08-24 23:30:26 +00003238 if (!PushComponent(S))
Richard Smithc20d1442018-08-20 20:14:49 +00003239 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003240 if (SoFar != S)
3241 Subs.push_back(S);
3242 continue;
3243 }
3244
3245 // Parse an <unqualified-name> thats actually a <ctor-dtor-name>.
3246 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
3247 if (SoFar == nullptr)
3248 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003249 if (!PushComponent(getDerived().parseCtorDtorName(SoFar, State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003250 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003251 SoFar = getDerived().parseAbiTags(SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00003252 if (SoFar == nullptr)
3253 return nullptr;
3254 Subs.push_back(SoFar);
3255 continue;
3256 }
3257
3258 // ::= <prefix> <unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00003259 if (!PushComponent(getDerived().parseUnqualifiedName(State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003260 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003261 Subs.push_back(SoFar);
3262 }
3263
3264 if (SoFar == nullptr || Subs.empty())
3265 return nullptr;
3266
3267 Subs.pop_back();
3268 return SoFar;
3269}
3270
3271// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003272template <typename Derived, typename Alloc>
3273Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3274 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003275 if (SN == nullptr)
3276 return nullptr;
3277 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003278 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003279 if (TA == nullptr)
3280 return nullptr;
3281 return make<NameWithTemplateArgs>(SN, TA);
3282 }
3283 return SN;
3284}
3285
3286// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3287// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003288template <typename Derived, typename Alloc>
3289Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003290 Node *Result;
3291 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003292 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003293 else
Pavel Labathba825192018-10-16 14:29:14 +00003294 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003295 if (Result == nullptr)
3296 return nullptr;
3297 return make<DtorName>(Result);
3298}
3299
3300// <unresolved-type> ::= <template-param>
3301// ::= <decltype>
3302// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003303template <typename Derived, typename Alloc>
3304Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003305 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003306 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003307 if (TP == nullptr)
3308 return nullptr;
3309 Subs.push_back(TP);
3310 return TP;
3311 }
3312 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003313 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003314 if (DT == nullptr)
3315 return nullptr;
3316 Subs.push_back(DT);
3317 return DT;
3318 }
Pavel Labathba825192018-10-16 14:29:14 +00003319 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003320}
3321
3322// <base-unresolved-name> ::= <simple-id> # unresolved name
3323// extension ::= <operator-name> # unresolved operator-function-id
3324// extension ::= <operator-name> <template-args> # unresolved operator template-id
3325// ::= on <operator-name> # unresolved operator-function-id
3326// ::= on <operator-name> <template-args> # unresolved operator template-id
3327// ::= dn <destructor-name> # destructor or pseudo-destructor;
3328// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003329template <typename Derived, typename Alloc>
3330Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003331 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003332 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003333
3334 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003335 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003336
3337 consumeIf("on");
3338
Pavel Labathba825192018-10-16 14:29:14 +00003339 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003340 if (Oper == nullptr)
3341 return nullptr;
3342 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003343 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003344 if (TA == nullptr)
3345 return nullptr;
3346 return make<NameWithTemplateArgs>(Oper, TA);
3347 }
3348 return Oper;
3349}
3350
3351// <unresolved-name>
3352// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3353// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3354// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3355// # A::x, N::y, A<T>::z; "gs" means leading "::"
3356// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3357// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3358// # T::N::x /decltype(p)::N::x
3359// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3360//
3361// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003362template <typename Derived, typename Alloc>
3363Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003364 Node *SoFar = nullptr;
3365
3366 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3367 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3368 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003369 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003370 if (SoFar == nullptr)
3371 return nullptr;
3372
3373 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003374 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003375 if (TA == nullptr)
3376 return nullptr;
3377 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003378 if (!SoFar)
3379 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003380 }
3381
3382 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003383 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003384 if (Qual == nullptr)
3385 return nullptr;
3386 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003387 if (!SoFar)
3388 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003389 }
3390
Pavel Labathba825192018-10-16 14:29:14 +00003391 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003392 if (Base == nullptr)
3393 return nullptr;
3394 return make<QualifiedName>(SoFar, Base);
3395 }
3396
3397 bool Global = consumeIf("gs");
3398
3399 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3400 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003401 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003402 if (SoFar == nullptr)
3403 return nullptr;
3404 if (Global)
3405 SoFar = make<GlobalQualifiedName>(SoFar);
3406 return SoFar;
3407 }
3408
3409 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3410 if (std::isdigit(look())) {
3411 do {
Pavel Labathba825192018-10-16 14:29:14 +00003412 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003413 if (Qual == nullptr)
3414 return nullptr;
3415 if (SoFar)
3416 SoFar = make<QualifiedName>(SoFar, Qual);
3417 else if (Global)
3418 SoFar = make<GlobalQualifiedName>(Qual);
3419 else
3420 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003421 if (!SoFar)
3422 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003423 } while (!consumeIf('E'));
3424 }
3425 // sr <unresolved-type> <base-unresolved-name>
3426 // sr <unresolved-type> <template-args> <base-unresolved-name>
3427 else {
Pavel Labathba825192018-10-16 14:29:14 +00003428 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003429 if (SoFar == nullptr)
3430 return nullptr;
3431
3432 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003433 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003434 if (TA == nullptr)
3435 return nullptr;
3436 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003437 if (!SoFar)
3438 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003439 }
3440 }
3441
3442 assert(SoFar != nullptr);
3443
Pavel Labathba825192018-10-16 14:29:14 +00003444 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003445 if (Base == nullptr)
3446 return nullptr;
3447 return make<QualifiedName>(SoFar, Base);
3448}
3449
3450// <abi-tags> ::= <abi-tag> [<abi-tags>]
3451// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003452template <typename Derived, typename Alloc>
3453Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003454 while (consumeIf('B')) {
3455 StringView SN = parseBareSourceName();
3456 if (SN.empty())
3457 return nullptr;
3458 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003459 if (!N)
3460 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003461 }
3462 return N;
3463}
3464
3465// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003466template <typename Alloc, typename Derived>
3467StringView
3468AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003469 const char *Tmp = First;
3470 if (AllowNegative)
3471 consumeIf('n');
3472 if (numLeft() == 0 || !std::isdigit(*First))
3473 return StringView();
3474 while (numLeft() != 0 && std::isdigit(*First))
3475 ++First;
3476 return StringView(Tmp, First);
3477}
3478
3479// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003480template <typename Alloc, typename Derived>
3481bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003482 *Out = 0;
3483 if (look() < '0' || look() > '9')
3484 return true;
3485 while (look() >= '0' && look() <= '9') {
3486 *Out *= 10;
3487 *Out += static_cast<size_t>(consume() - '0');
3488 }
3489 return false;
3490}
3491
Pavel Labathba825192018-10-16 14:29:14 +00003492template <typename Alloc, typename Derived>
3493StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003494 size_t Int = 0;
3495 if (parsePositiveInteger(&Int) || numLeft() < Int)
3496 return StringView();
3497 StringView R(First, First + Int);
3498 First += Int;
3499 return R;
3500}
3501
3502// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3503//
3504// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3505// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3506// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3507//
3508// <ref-qualifier> ::= R # & ref-qualifier
3509// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003510template <typename Derived, typename Alloc>
3511Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003512 Qualifiers CVQuals = parseCVQualifiers();
3513
3514 Node *ExceptionSpec = nullptr;
3515 if (consumeIf("Do")) {
3516 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003517 if (!ExceptionSpec)
3518 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003519 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003520 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003521 if (E == nullptr || !consumeIf('E'))
3522 return nullptr;
3523 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003524 if (!ExceptionSpec)
3525 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003526 } else if (consumeIf("Dw")) {
3527 size_t SpecsBegin = Names.size();
3528 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003529 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003530 if (T == nullptr)
3531 return nullptr;
3532 Names.push_back(T);
3533 }
3534 ExceptionSpec =
3535 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003536 if (!ExceptionSpec)
3537 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003538 }
3539
3540 consumeIf("Dx"); // transaction safe
3541
3542 if (!consumeIf('F'))
3543 return nullptr;
3544 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003545 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003546 if (ReturnType == nullptr)
3547 return nullptr;
3548
3549 FunctionRefQual ReferenceQualifier = FrefQualNone;
3550 size_t ParamsBegin = Names.size();
3551 while (true) {
3552 if (consumeIf('E'))
3553 break;
3554 if (consumeIf('v'))
3555 continue;
3556 if (consumeIf("RE")) {
3557 ReferenceQualifier = FrefQualLValue;
3558 break;
3559 }
3560 if (consumeIf("OE")) {
3561 ReferenceQualifier = FrefQualRValue;
3562 break;
3563 }
Pavel Labathba825192018-10-16 14:29:14 +00003564 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003565 if (T == nullptr)
3566 return nullptr;
3567 Names.push_back(T);
3568 }
3569
3570 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3571 return make<FunctionType>(ReturnType, Params, CVQuals,
3572 ReferenceQualifier, ExceptionSpec);
3573}
3574
3575// extension:
3576// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3577// ::= Dv [<dimension expression>] _ <element type>
3578// <extended element type> ::= <element type>
3579// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003580template <typename Derived, typename Alloc>
3581Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003582 if (!consumeIf("Dv"))
3583 return nullptr;
3584 if (look() >= '1' && look() <= '9') {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003585 Node *DimensionNumber = make<NameType>(parseNumber());
3586 if (!DimensionNumber)
3587 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003588 if (!consumeIf('_'))
3589 return nullptr;
3590 if (consumeIf('p'))
3591 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003592 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003593 if (ElemType == nullptr)
3594 return nullptr;
3595 return make<VectorType>(ElemType, DimensionNumber);
3596 }
3597
3598 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003599 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003600 if (!DimExpr)
3601 return nullptr;
3602 if (!consumeIf('_'))
3603 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003604 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003605 if (!ElemType)
3606 return nullptr;
3607 return make<VectorType>(ElemType, DimExpr);
3608 }
Pavel Labathba825192018-10-16 14:29:14 +00003609 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003610 if (!ElemType)
3611 return nullptr;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003612 return make<VectorType>(ElemType, /*Dimension=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003613}
3614
3615// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3616// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003617template <typename Derived, typename Alloc>
3618Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003619 if (!consumeIf('D'))
3620 return nullptr;
3621 if (!consumeIf('t') && !consumeIf('T'))
3622 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003623 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003624 if (E == nullptr)
3625 return nullptr;
3626 if (!consumeIf('E'))
3627 return nullptr;
3628 return make<EnclosingExpr>("decltype(", E, ")");
3629}
3630
3631// <array-type> ::= A <positive dimension number> _ <element type>
3632// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003633template <typename Derived, typename Alloc>
3634Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003635 if (!consumeIf('A'))
3636 return nullptr;
3637
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003638 Node *Dimension = nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003639
Richard Smithc20d1442018-08-20 20:14:49 +00003640 if (std::isdigit(look())) {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003641 Dimension = make<NameType>(parseNumber());
3642 if (!Dimension)
3643 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003644 if (!consumeIf('_'))
3645 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003646 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003647 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003648 if (DimExpr == nullptr)
3649 return nullptr;
3650 if (!consumeIf('_'))
3651 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003652 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003653 }
3654
Pavel Labathba825192018-10-16 14:29:14 +00003655 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003656 if (Ty == nullptr)
3657 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003658 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003659}
3660
3661// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003662template <typename Derived, typename Alloc>
3663Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003664 if (!consumeIf('M'))
3665 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003666 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003667 if (ClassType == nullptr)
3668 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003669 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003670 if (MemberType == nullptr)
3671 return nullptr;
3672 return make<PointerToMemberType>(ClassType, MemberType);
3673}
3674
3675// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3676// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3677// ::= Tu <name> # dependent elaborated type specifier using 'union'
3678// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003679template <typename Derived, typename Alloc>
3680Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003681 StringView ElabSpef;
3682 if (consumeIf("Ts"))
3683 ElabSpef = "struct";
3684 else if (consumeIf("Tu"))
3685 ElabSpef = "union";
3686 else if (consumeIf("Te"))
3687 ElabSpef = "enum";
3688
Pavel Labathba825192018-10-16 14:29:14 +00003689 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003690 if (Name == nullptr)
3691 return nullptr;
3692
3693 if (!ElabSpef.empty())
3694 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3695
3696 return Name;
3697}
3698
3699// <qualified-type> ::= <qualifiers> <type>
3700// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3701// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003702template <typename Derived, typename Alloc>
3703Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003704 if (consumeIf('U')) {
3705 StringView Qual = parseBareSourceName();
3706 if (Qual.empty())
3707 return nullptr;
3708
Richard Smithc20d1442018-08-20 20:14:49 +00003709 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3710 if (Qual.startsWith("objcproto")) {
3711 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3712 StringView Proto;
3713 {
3714 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3715 SaveLast(Last, ProtoSourceName.end());
3716 Proto = parseBareSourceName();
3717 }
3718 if (Proto.empty())
3719 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003720 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003721 if (Child == nullptr)
3722 return nullptr;
3723 return make<ObjCProtoName>(Child, Proto);
3724 }
3725
Alex Orlovf50df922021-03-24 10:21:32 +04003726 Node *TA = nullptr;
3727 if (look() == 'I') {
3728 TA = getDerived().parseTemplateArgs();
3729 if (TA == nullptr)
3730 return nullptr;
3731 }
3732
Pavel Labathba825192018-10-16 14:29:14 +00003733 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003734 if (Child == nullptr)
3735 return nullptr;
Alex Orlovf50df922021-03-24 10:21:32 +04003736 return make<VendorExtQualType>(Child, Qual, TA);
Richard Smithc20d1442018-08-20 20:14:49 +00003737 }
3738
3739 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003740 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003741 if (Ty == nullptr)
3742 return nullptr;
3743 if (Quals != QualNone)
3744 Ty = make<QualType>(Ty, Quals);
3745 return Ty;
3746}
3747
3748// <type> ::= <builtin-type>
3749// ::= <qualified-type>
3750// ::= <function-type>
3751// ::= <class-enum-type>
3752// ::= <array-type>
3753// ::= <pointer-to-member-type>
3754// ::= <template-param>
3755// ::= <template-template-param> <template-args>
3756// ::= <decltype>
3757// ::= P <type> # pointer
3758// ::= R <type> # l-value reference
3759// ::= O <type> # r-value reference (C++11)
3760// ::= C <type> # complex pair (C99)
3761// ::= G <type> # imaginary (C99)
3762// ::= <substitution> # See Compression below
3763// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3764// extension ::= <vector-type> # <vector-type> starts with Dv
3765//
3766// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3767// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003768template <typename Derived, typename Alloc>
3769Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003770 Node *Result = nullptr;
3771
Richard Smithc20d1442018-08-20 20:14:49 +00003772 switch (look()) {
3773 // ::= <qualified-type>
3774 case 'r':
3775 case 'V':
3776 case 'K': {
3777 unsigned AfterQuals = 0;
3778 if (look(AfterQuals) == 'r') ++AfterQuals;
3779 if (look(AfterQuals) == 'V') ++AfterQuals;
3780 if (look(AfterQuals) == 'K') ++AfterQuals;
3781
3782 if (look(AfterQuals) == 'F' ||
3783 (look(AfterQuals) == 'D' &&
3784 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3785 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003786 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003787 break;
3788 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003789 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003790 }
3791 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003792 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003793 break;
3794 }
3795 // <builtin-type> ::= v # void
3796 case 'v':
3797 ++First;
3798 return make<NameType>("void");
3799 // ::= w # wchar_t
3800 case 'w':
3801 ++First;
3802 return make<NameType>("wchar_t");
3803 // ::= b # bool
3804 case 'b':
3805 ++First;
3806 return make<NameType>("bool");
3807 // ::= c # char
3808 case 'c':
3809 ++First;
3810 return make<NameType>("char");
3811 // ::= a # signed char
3812 case 'a':
3813 ++First;
3814 return make<NameType>("signed char");
3815 // ::= h # unsigned char
3816 case 'h':
3817 ++First;
3818 return make<NameType>("unsigned char");
3819 // ::= s # short
3820 case 's':
3821 ++First;
3822 return make<NameType>("short");
3823 // ::= t # unsigned short
3824 case 't':
3825 ++First;
3826 return make<NameType>("unsigned short");
3827 // ::= i # int
3828 case 'i':
3829 ++First;
3830 return make<NameType>("int");
3831 // ::= j # unsigned int
3832 case 'j':
3833 ++First;
3834 return make<NameType>("unsigned int");
3835 // ::= l # long
3836 case 'l':
3837 ++First;
3838 return make<NameType>("long");
3839 // ::= m # unsigned long
3840 case 'm':
3841 ++First;
3842 return make<NameType>("unsigned long");
3843 // ::= x # long long, __int64
3844 case 'x':
3845 ++First;
3846 return make<NameType>("long long");
3847 // ::= y # unsigned long long, __int64
3848 case 'y':
3849 ++First;
3850 return make<NameType>("unsigned long long");
3851 // ::= n # __int128
3852 case 'n':
3853 ++First;
3854 return make<NameType>("__int128");
3855 // ::= o # unsigned __int128
3856 case 'o':
3857 ++First;
3858 return make<NameType>("unsigned __int128");
3859 // ::= f # float
3860 case 'f':
3861 ++First;
3862 return make<NameType>("float");
3863 // ::= d # double
3864 case 'd':
3865 ++First;
3866 return make<NameType>("double");
3867 // ::= e # long double, __float80
3868 case 'e':
3869 ++First;
3870 return make<NameType>("long double");
3871 // ::= g # __float128
3872 case 'g':
3873 ++First;
3874 return make<NameType>("__float128");
3875 // ::= z # ellipsis
3876 case 'z':
3877 ++First;
3878 return make<NameType>("...");
3879
3880 // <builtin-type> ::= u <source-name> # vendor extended type
3881 case 'u': {
3882 ++First;
3883 StringView Res = parseBareSourceName();
3884 if (Res.empty())
3885 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003886 // Typically, <builtin-type>s are not considered substitution candidates,
3887 // but the exception to that exception is vendor extended types (Itanium C++
3888 // ABI 5.9.1).
3889 Result = make<NameType>(Res);
3890 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003891 }
3892 case 'D':
3893 switch (look(1)) {
3894 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3895 case 'd':
3896 First += 2;
3897 return make<NameType>("decimal64");
3898 // ::= De # IEEE 754r decimal floating point (128 bits)
3899 case 'e':
3900 First += 2;
3901 return make<NameType>("decimal128");
3902 // ::= Df # IEEE 754r decimal floating point (32 bits)
3903 case 'f':
3904 First += 2;
3905 return make<NameType>("decimal32");
3906 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3907 case 'h':
3908 First += 2;
Stuart Bradye8bf5772021-06-07 16:30:22 +01003909 return make<NameType>("half");
Pengfei Wang50e90b82021-09-23 11:02:25 +08003910 // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits)
3911 case 'F': {
3912 First += 2;
3913 Node *DimensionNumber = make<NameType>(parseNumber());
3914 if (!DimensionNumber)
3915 return nullptr;
3916 if (!consumeIf('_'))
3917 return nullptr;
3918 return make<BinaryFPType>(DimensionNumber);
3919 }
Richard Smithc20d1442018-08-20 20:14:49 +00003920 // ::= Di # char32_t
3921 case 'i':
3922 First += 2;
3923 return make<NameType>("char32_t");
3924 // ::= Ds # char16_t
3925 case 's':
3926 First += 2;
3927 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003928 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3929 case 'u':
3930 First += 2;
3931 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003932 // ::= Da # auto (in dependent new-expressions)
3933 case 'a':
3934 First += 2;
3935 return make<NameType>("auto");
3936 // ::= Dc # decltype(auto)
3937 case 'c':
3938 First += 2;
3939 return make<NameType>("decltype(auto)");
3940 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3941 case 'n':
3942 First += 2;
3943 return make<NameType>("std::nullptr_t");
3944
3945 // ::= <decltype>
3946 case 't':
3947 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003948 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003949 break;
3950 }
3951 // extension ::= <vector-type> # <vector-type> starts with Dv
3952 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003953 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003954 break;
3955 }
3956 // ::= Dp <type> # pack expansion (C++0x)
3957 case 'p': {
3958 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003959 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003960 if (!Child)
3961 return nullptr;
3962 Result = make<ParameterPackExpansion>(Child);
3963 break;
3964 }
3965 // Exception specifier on a function type.
3966 case 'o':
3967 case 'O':
3968 case 'w':
3969 // Transaction safe function type.
3970 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003971 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003972 break;
3973 }
3974 break;
3975 // ::= <function-type>
3976 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003977 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003978 break;
3979 }
3980 // ::= <array-type>
3981 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003982 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003983 break;
3984 }
3985 // ::= <pointer-to-member-type>
3986 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003987 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003988 break;
3989 }
3990 // ::= <template-param>
3991 case 'T': {
3992 // This could be an elaborate type specifier on a <class-enum-type>.
3993 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003994 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003995 break;
3996 }
3997
Pavel Labathba825192018-10-16 14:29:14 +00003998 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003999 if (Result == nullptr)
4000 return nullptr;
4001
4002 // Result could be either of:
4003 // <type> ::= <template-param>
4004 // <type> ::= <template-template-param> <template-args>
4005 //
4006 // <template-template-param> ::= <template-param>
4007 // ::= <substitution>
4008 //
4009 // If this is followed by some <template-args>, and we're permitted to
4010 // parse them, take the second production.
4011
4012 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004013 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004014 if (TA == nullptr)
4015 return nullptr;
4016 Result = make<NameWithTemplateArgs>(Result, TA);
4017 }
4018 break;
4019 }
4020 // ::= P <type> # pointer
4021 case 'P': {
4022 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004023 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004024 if (Ptr == nullptr)
4025 return nullptr;
4026 Result = make<PointerType>(Ptr);
4027 break;
4028 }
4029 // ::= R <type> # l-value reference
4030 case 'R': {
4031 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004032 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004033 if (Ref == nullptr)
4034 return nullptr;
4035 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
4036 break;
4037 }
4038 // ::= O <type> # r-value reference (C++11)
4039 case 'O': {
4040 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004041 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004042 if (Ref == nullptr)
4043 return nullptr;
4044 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
4045 break;
4046 }
4047 // ::= C <type> # complex pair (C99)
4048 case 'C': {
4049 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004050 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004051 if (P == nullptr)
4052 return nullptr;
4053 Result = make<PostfixQualifiedType>(P, " complex");
4054 break;
4055 }
4056 // ::= G <type> # imaginary (C99)
4057 case 'G': {
4058 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004059 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004060 if (P == nullptr)
4061 return P;
4062 Result = make<PostfixQualifiedType>(P, " imaginary");
4063 break;
4064 }
4065 // ::= <substitution> # See Compression below
4066 case 'S': {
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004067 if (look(1) != 't') {
4068 Result = getDerived().parseSubstitution();
4069 if (Result == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00004070 return nullptr;
4071
4072 // Sub could be either of:
4073 // <type> ::= <substitution>
4074 // <type> ::= <template-template-param> <template-args>
4075 //
4076 // <template-template-param> ::= <template-param>
4077 // ::= <substitution>
4078 //
4079 // If this is followed by some <template-args>, and we're permitted to
4080 // parse them, take the second production.
4081
4082 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004083 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004084 if (TA == nullptr)
4085 return nullptr;
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004086 Result = make<NameWithTemplateArgs>(Result, TA);
4087 } else {
4088 // If all we parsed was a substitution, don't re-insert into the
4089 // substitution table.
4090 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00004091 }
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004092 break;
Richard Smithc20d1442018-08-20 20:14:49 +00004093 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00004094 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004095 }
4096 // ::= <class-enum-type>
4097 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004098 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004099 break;
4100 }
4101 }
4102
4103 // If we parsed a type, insert it into the substitution table. Note that all
4104 // <builtin-type>s and <substitution>s have already bailed out, because they
4105 // don't get substitutions.
4106 if (Result != nullptr)
4107 Subs.push_back(Result);
4108 return Result;
4109}
4110
Pavel Labathba825192018-10-16 14:29:14 +00004111template <typename Derived, typename Alloc>
4112Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4113 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004114 if (E == nullptr)
4115 return nullptr;
4116 return make<PrefixExpr>(Kind, E);
4117}
4118
Pavel Labathba825192018-10-16 14:29:14 +00004119template <typename Derived, typename Alloc>
4120Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4121 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004122 if (LHS == nullptr)
4123 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004124 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004125 if (RHS == nullptr)
4126 return nullptr;
4127 return make<BinaryExpr>(LHS, Kind, RHS);
4128}
4129
Pavel Labathba825192018-10-16 14:29:14 +00004130template <typename Derived, typename Alloc>
4131Node *
4132AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004133 StringView Tmp = parseNumber(true);
4134 if (!Tmp.empty() && consumeIf('E'))
4135 return make<IntegerLiteral>(Lit, Tmp);
4136 return nullptr;
4137}
4138
4139// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004140template <typename Alloc, typename Derived>
4141Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004142 Qualifiers CVR = QualNone;
4143 if (consumeIf('r'))
4144 CVR |= QualRestrict;
4145 if (consumeIf('V'))
4146 CVR |= QualVolatile;
4147 if (consumeIf('K'))
4148 CVR |= QualConst;
4149 return CVR;
4150}
4151
4152// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4153// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4154// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4155// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L > 0, second and later parameters
Erik Pilkington91c24af2020-05-13 22:19:45 -04004156// ::= fpT # 'this' expression (not part of standard?)
Pavel Labathba825192018-10-16 14:29:14 +00004157template <typename Derived, typename Alloc>
4158Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Erik Pilkington91c24af2020-05-13 22:19:45 -04004159 if (consumeIf("fpT"))
4160 return make<NameType>("this");
Richard Smithc20d1442018-08-20 20:14:49 +00004161 if (consumeIf("fp")) {
4162 parseCVQualifiers();
4163 StringView Num = parseNumber();
4164 if (!consumeIf('_'))
4165 return nullptr;
4166 return make<FunctionParam>(Num);
4167 }
4168 if (consumeIf("fL")) {
4169 if (parseNumber().empty())
4170 return nullptr;
4171 if (!consumeIf('p'))
4172 return nullptr;
4173 parseCVQualifiers();
4174 StringView Num = parseNumber();
4175 if (!consumeIf('_'))
4176 return nullptr;
4177 return make<FunctionParam>(Num);
4178 }
4179 return nullptr;
4180}
4181
4182// [gs] nw <expression>* _ <type> E # new (expr-list) type
4183// [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4184// [gs] na <expression>* _ <type> E # new[] (expr-list) type
4185// [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4186// <initializer> ::= pi <expression>* E # parenthesized initialization
Pavel Labathba825192018-10-16 14:29:14 +00004187template <typename Derived, typename Alloc>
4188Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004189 bool Global = consumeIf("gs");
4190 bool IsArray = look(1) == 'a';
4191 if (!consumeIf("nw") && !consumeIf("na"))
4192 return nullptr;
4193 size_t Exprs = Names.size();
4194 while (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00004195 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004196 if (Ex == nullptr)
4197 return nullptr;
4198 Names.push_back(Ex);
4199 }
4200 NodeArray ExprList = popTrailingNodeArray(Exprs);
Pavel Labathba825192018-10-16 14:29:14 +00004201 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004202 if (Ty == nullptr)
4203 return Ty;
4204 if (consumeIf("pi")) {
4205 size_t InitsBegin = Names.size();
4206 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004207 Node *Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004208 if (Init == nullptr)
4209 return Init;
4210 Names.push_back(Init);
4211 }
4212 NodeArray Inits = popTrailingNodeArray(InitsBegin);
4213 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
4214 } else if (!consumeIf('E'))
4215 return nullptr;
4216 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
4217}
4218
4219// cv <type> <expression> # conversion with one argument
4220// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004221template <typename Derived, typename Alloc>
4222Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004223 if (!consumeIf("cv"))
4224 return nullptr;
4225 Node *Ty;
4226 {
4227 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004228 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004229 }
4230
4231 if (Ty == nullptr)
4232 return nullptr;
4233
4234 if (consumeIf('_')) {
4235 size_t ExprsBegin = Names.size();
4236 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004237 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004238 if (E == nullptr)
4239 return E;
4240 Names.push_back(E);
4241 }
4242 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4243 return make<ConversionExpr>(Ty, Exprs);
4244 }
4245
Pavel Labathba825192018-10-16 14:29:14 +00004246 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004247 if (E[0] == nullptr)
4248 return nullptr;
4249 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4250}
4251
4252// <expr-primary> ::= L <type> <value number> E # integer literal
4253// ::= L <type> <value float> E # floating literal
4254// ::= L <string type> E # string literal
4255// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004256// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004257// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4258// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004259template <typename Derived, typename Alloc>
4260Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004261 if (!consumeIf('L'))
4262 return nullptr;
4263 switch (look()) {
4264 case 'w':
4265 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004266 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004267 case 'b':
4268 if (consumeIf("b0E"))
4269 return make<BoolExpr>(0);
4270 if (consumeIf("b1E"))
4271 return make<BoolExpr>(1);
4272 return nullptr;
4273 case 'c':
4274 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004275 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004276 case 'a':
4277 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004278 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004279 case 'h':
4280 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004281 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004282 case 's':
4283 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004284 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004285 case 't':
4286 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004287 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004288 case 'i':
4289 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004290 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004291 case 'j':
4292 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004293 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004294 case 'l':
4295 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004296 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004297 case 'm':
4298 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004299 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004300 case 'x':
4301 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004302 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004303 case 'y':
4304 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004305 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004306 case 'n':
4307 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004308 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004309 case 'o':
4310 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004311 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004312 case 'f':
4313 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004314 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004315 case 'd':
4316 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004317 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004318 case 'e':
4319 ++First;
Xing Xue3dc5e082020-04-15 09:59:06 -04004320#if defined(__powerpc__) || defined(__s390__)
4321 // Handle cases where long doubles encoded with e have the same size
4322 // and representation as doubles.
4323 return getDerived().template parseFloatingLiteral<double>();
4324#else
Pavel Labathba825192018-10-16 14:29:14 +00004325 return getDerived().template parseFloatingLiteral<long double>();
Xing Xue3dc5e082020-04-15 09:59:06 -04004326#endif
Richard Smithc20d1442018-08-20 20:14:49 +00004327 case '_':
4328 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004329 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004330 if (R != nullptr && consumeIf('E'))
4331 return R;
4332 }
4333 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004334 case 'A': {
4335 Node *T = getDerived().parseType();
4336 if (T == nullptr)
4337 return nullptr;
4338 // FIXME: We need to include the string contents in the mangling.
4339 if (consumeIf('E'))
4340 return make<StringLiteral>(T);
4341 return nullptr;
4342 }
4343 case 'D':
4344 if (consumeIf("DnE"))
4345 return make<NameType>("nullptr");
4346 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004347 case 'T':
4348 // Invalid mangled name per
4349 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4350 return nullptr;
Richard Smithfb917462019-09-09 22:26:04 +00004351 case 'U': {
4352 // FIXME: Should we support LUb... for block literals?
4353 if (look(1) != 'l')
4354 return nullptr;
4355 Node *T = parseUnnamedTypeName(nullptr);
4356 if (!T || !consumeIf('E'))
4357 return nullptr;
4358 return make<LambdaExpr>(T);
4359 }
Richard Smithc20d1442018-08-20 20:14:49 +00004360 default: {
4361 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004362 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004363 if (T == nullptr)
4364 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004365 StringView N = parseNumber(/*AllowNegative=*/true);
Richard Smithfb917462019-09-09 22:26:04 +00004366 if (N.empty())
4367 return nullptr;
4368 if (!consumeIf('E'))
4369 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004370 return make<EnumLiteral>(T, N);
Richard Smithc20d1442018-08-20 20:14:49 +00004371 }
4372 }
4373}
4374
4375// <braced-expression> ::= <expression>
4376// ::= di <field source-name> <braced-expression> # .name = expr
4377// ::= dx <index expression> <braced-expression> # [expr] = expr
4378// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004379template <typename Derived, typename Alloc>
4380Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004381 if (look() == 'd') {
4382 switch (look(1)) {
4383 case 'i': {
4384 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004385 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004386 if (Field == nullptr)
4387 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004388 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004389 if (Init == nullptr)
4390 return nullptr;
4391 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4392 }
4393 case 'x': {
4394 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004395 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004396 if (Index == nullptr)
4397 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004398 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004399 if (Init == nullptr)
4400 return nullptr;
4401 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4402 }
4403 case 'X': {
4404 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004405 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004406 if (RangeBegin == nullptr)
4407 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004408 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004409 if (RangeEnd == nullptr)
4410 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004411 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004412 if (Init == nullptr)
4413 return nullptr;
4414 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4415 }
4416 }
4417 }
Pavel Labathba825192018-10-16 14:29:14 +00004418 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004419}
4420
4421// (not yet in the spec)
4422// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4423// ::= fR <binary-operator-name> <expression> <expression>
4424// ::= fl <binary-operator-name> <expression>
4425// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004426template <typename Derived, typename Alloc>
4427Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004428 if (!consumeIf('f'))
4429 return nullptr;
4430
4431 char FoldKind = look();
4432 bool IsLeftFold, HasInitializer;
4433 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4434 if (FoldKind == 'l' || FoldKind == 'L')
4435 IsLeftFold = true;
4436 else if (FoldKind == 'r' || FoldKind == 'R')
4437 IsLeftFold = false;
4438 else
4439 return nullptr;
4440 ++First;
4441
4442 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4443 StringView OperatorName;
4444 if (consumeIf("aa")) OperatorName = "&&";
4445 else if (consumeIf("an")) OperatorName = "&";
4446 else if (consumeIf("aN")) OperatorName = "&=";
4447 else if (consumeIf("aS")) OperatorName = "=";
4448 else if (consumeIf("cm")) OperatorName = ",";
4449 else if (consumeIf("ds")) OperatorName = ".*";
4450 else if (consumeIf("dv")) OperatorName = "/";
4451 else if (consumeIf("dV")) OperatorName = "/=";
4452 else if (consumeIf("eo")) OperatorName = "^";
4453 else if (consumeIf("eO")) OperatorName = "^=";
4454 else if (consumeIf("eq")) OperatorName = "==";
4455 else if (consumeIf("ge")) OperatorName = ">=";
4456 else if (consumeIf("gt")) OperatorName = ">";
4457 else if (consumeIf("le")) OperatorName = "<=";
4458 else if (consumeIf("ls")) OperatorName = "<<";
4459 else if (consumeIf("lS")) OperatorName = "<<=";
4460 else if (consumeIf("lt")) OperatorName = "<";
4461 else if (consumeIf("mi")) OperatorName = "-";
4462 else if (consumeIf("mI")) OperatorName = "-=";
4463 else if (consumeIf("ml")) OperatorName = "*";
4464 else if (consumeIf("mL")) OperatorName = "*=";
4465 else if (consumeIf("ne")) OperatorName = "!=";
4466 else if (consumeIf("oo")) OperatorName = "||";
4467 else if (consumeIf("or")) OperatorName = "|";
4468 else if (consumeIf("oR")) OperatorName = "|=";
4469 else if (consumeIf("pl")) OperatorName = "+";
4470 else if (consumeIf("pL")) OperatorName = "+=";
4471 else if (consumeIf("rm")) OperatorName = "%";
4472 else if (consumeIf("rM")) OperatorName = "%=";
4473 else if (consumeIf("rs")) OperatorName = ">>";
4474 else if (consumeIf("rS")) OperatorName = ">>=";
4475 else return nullptr;
4476
Pavel Labathba825192018-10-16 14:29:14 +00004477 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004478 if (Pack == nullptr)
4479 return nullptr;
4480 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004481 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004482 if (Init == nullptr)
4483 return nullptr;
4484 }
4485
4486 if (IsLeftFold && Init)
4487 std::swap(Pack, Init);
4488
4489 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4490}
4491
Richard Smith1865d2f2020-10-22 19:29:36 -07004492// <expression> ::= mc <parameter type> <expr> [<offset number>] E
4493//
4494// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4495template <typename Derived, typename Alloc>
4496Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() {
4497 Node *Ty = getDerived().parseType();
4498 if (!Ty)
4499 return nullptr;
4500 Node *Expr = getDerived().parseExpr();
4501 if (!Expr)
4502 return nullptr;
4503 StringView Offset = getDerived().parseNumber(true);
4504 if (!consumeIf('E'))
4505 return nullptr;
4506 return make<PointerToMemberConversionExpr>(Ty, Expr, Offset);
4507}
4508
4509// <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
4510// <union-selector> ::= _ [<number>]
4511//
4512// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4513template <typename Derived, typename Alloc>
4514Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
4515 Node *Ty = getDerived().parseType();
4516 if (!Ty)
4517 return nullptr;
4518 Node *Expr = getDerived().parseExpr();
4519 if (!Expr)
4520 return nullptr;
4521 StringView Offset = getDerived().parseNumber(true);
4522 size_t SelectorsBegin = Names.size();
4523 while (consumeIf('_')) {
4524 Node *Selector = make<NameType>(parseNumber());
4525 if (!Selector)
4526 return nullptr;
4527 Names.push_back(Selector);
4528 }
4529 bool OnePastTheEnd = consumeIf('p');
4530 if (!consumeIf('E'))
4531 return nullptr;
4532 return make<SubobjectExpr>(
4533 Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
4534}
4535
Richard Smithc20d1442018-08-20 20:14:49 +00004536// <expression> ::= <unary operator-name> <expression>
4537// ::= <binary operator-name> <expression> <expression>
4538// ::= <ternary operator-name> <expression> <expression> <expression>
4539// ::= cl <expression>+ E # call
4540// ::= cv <type> <expression> # conversion with one argument
4541// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4542// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4543// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4544// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4545// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4546// ::= [gs] dl <expression> # delete expression
4547// ::= [gs] da <expression> # delete[] expression
4548// ::= pp_ <expression> # prefix ++
4549// ::= mm_ <expression> # prefix --
4550// ::= ti <type> # typeid (type)
4551// ::= te <expression> # typeid (expression)
4552// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4553// ::= sc <type> <expression> # static_cast<type> (expression)
4554// ::= cc <type> <expression> # const_cast<type> (expression)
4555// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4556// ::= st <type> # sizeof (a type)
4557// ::= sz <expression> # sizeof (an expression)
4558// ::= at <type> # alignof (a type)
4559// ::= az <expression> # alignof (an expression)
4560// ::= nx <expression> # noexcept (expression)
4561// ::= <template-param>
4562// ::= <function-param>
4563// ::= dt <expression> <unresolved-name> # expr.name
4564// ::= pt <expression> <unresolved-name> # expr->name
4565// ::= ds <expression> <expression> # expr.*expr
4566// ::= sZ <template-param> # size of a parameter pack
4567// ::= sZ <function-param> # size of a function parameter pack
4568// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4569// ::= sp <expression> # pack expansion
4570// ::= tw <expression> # throw expression
4571// ::= tr # throw with no operand (rethrow)
4572// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4573// # freestanding dependent name (e.g., T::x),
4574// # objectless nonstatic member reference
4575// ::= fL <binary-operator-name> <expression> <expression>
4576// ::= fR <binary-operator-name> <expression> <expression>
4577// ::= fl <binary-operator-name> <expression>
4578// ::= fr <binary-operator-name> <expression>
4579// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004580template <typename Derived, typename Alloc>
4581Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004582 bool Global = consumeIf("gs");
4583 if (numLeft() < 2)
4584 return nullptr;
4585
4586 switch (*First) {
4587 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004588 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004589 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004590 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004591 case 'f': {
4592 // Disambiguate a fold expression from a <function-param>.
4593 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004594 return getDerived().parseFunctionParam();
4595 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004596 }
4597 case 'a':
4598 switch (First[1]) {
4599 case 'a':
4600 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004601 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004602 case 'd':
4603 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004604 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004605 case 'n':
4606 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004607 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004608 case 'N':
4609 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004610 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004611 case 'S':
4612 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004613 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004614 case 't': {
4615 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004616 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004617 if (Ty == nullptr)
4618 return nullptr;
4619 return make<EnclosingExpr>("alignof (", Ty, ")");
4620 }
4621 case 'z': {
4622 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004623 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004624 if (Ty == nullptr)
4625 return nullptr;
4626 return make<EnclosingExpr>("alignof (", Ty, ")");
4627 }
4628 }
4629 return nullptr;
4630 case 'c':
4631 switch (First[1]) {
4632 // cc <type> <expression> # const_cast<type>(expression)
4633 case 'c': {
4634 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004635 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004636 if (Ty == nullptr)
4637 return Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004638 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004639 if (Ex == nullptr)
4640 return Ex;
4641 return make<CastExpr>("const_cast", Ty, Ex);
4642 }
4643 // cl <expression>+ E # call
4644 case 'l': {
4645 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004646 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004647 if (Callee == nullptr)
4648 return Callee;
4649 size_t ExprsBegin = Names.size();
4650 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004651 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004652 if (E == nullptr)
4653 return E;
4654 Names.push_back(E);
4655 }
4656 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4657 }
4658 case 'm':
4659 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004660 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004661 case 'o':
4662 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004663 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004664 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004665 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004666 }
4667 return nullptr;
4668 case 'd':
4669 switch (First[1]) {
4670 case 'a': {
4671 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004672 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004673 if (Ex == nullptr)
4674 return Ex;
4675 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4676 }
4677 case 'c': {
4678 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004679 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004680 if (T == nullptr)
4681 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004682 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004683 if (Ex == nullptr)
4684 return Ex;
4685 return make<CastExpr>("dynamic_cast", T, Ex);
4686 }
4687 case 'e':
4688 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004689 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004690 case 'l': {
4691 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004692 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004693 if (E == nullptr)
4694 return E;
4695 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4696 }
4697 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004698 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004699 case 's': {
4700 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004701 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004702 if (LHS == nullptr)
4703 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004704 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004705 if (RHS == nullptr)
4706 return nullptr;
4707 return make<MemberExpr>(LHS, ".*", RHS);
4708 }
4709 case 't': {
4710 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004711 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004712 if (LHS == nullptr)
4713 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004714 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004715 if (RHS == nullptr)
4716 return nullptr;
4717 return make<MemberExpr>(LHS, ".", RHS);
4718 }
4719 case 'v':
4720 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004721 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004722 case 'V':
4723 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004724 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004725 }
4726 return nullptr;
4727 case 'e':
4728 switch (First[1]) {
4729 case 'o':
4730 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004731 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004732 case 'O':
4733 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004734 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004735 case 'q':
4736 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004737 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004738 }
4739 return nullptr;
4740 case 'g':
4741 switch (First[1]) {
4742 case 'e':
4743 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004744 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004745 case 't':
4746 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004747 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004748 }
4749 return nullptr;
4750 case 'i':
4751 switch (First[1]) {
4752 case 'x': {
4753 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004754 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004755 if (Base == nullptr)
4756 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004757 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004758 if (Index == nullptr)
4759 return Index;
4760 return make<ArraySubscriptExpr>(Base, Index);
4761 }
4762 case 'l': {
4763 First += 2;
4764 size_t InitsBegin = Names.size();
4765 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004766 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004767 if (E == nullptr)
4768 return nullptr;
4769 Names.push_back(E);
4770 }
4771 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4772 }
4773 }
4774 return nullptr;
4775 case 'l':
4776 switch (First[1]) {
4777 case 'e':
4778 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004779 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004780 case 's':
4781 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004782 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004783 case 'S':
4784 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004785 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004786 case 't':
4787 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004788 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004789 }
4790 return nullptr;
4791 case 'm':
4792 switch (First[1]) {
Richard Smith1865d2f2020-10-22 19:29:36 -07004793 case 'c':
4794 First += 2;
4795 return parsePointerToMemberConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004796 case 'i':
4797 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004798 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004799 case 'I':
4800 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004801 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004802 case 'l':
4803 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004804 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004805 case 'L':
4806 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004807 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004808 case 'm':
4809 First += 2;
4810 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004811 return getDerived().parsePrefixExpr("--");
4812 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004813 if (Ex == nullptr)
4814 return nullptr;
4815 return make<PostfixExpr>(Ex, "--");
4816 }
4817 return nullptr;
4818 case 'n':
4819 switch (First[1]) {
4820 case 'a':
4821 case 'w':
Pavel Labathba825192018-10-16 14:29:14 +00004822 return getDerived().parseNewExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004823 case 'e':
4824 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004825 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004826 case 'g':
4827 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004828 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004829 case 't':
4830 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004831 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004832 case 'x':
4833 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004834 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004835 if (Ex == nullptr)
4836 return Ex;
4837 return make<EnclosingExpr>("noexcept (", Ex, ")");
4838 }
4839 return nullptr;
4840 case 'o':
4841 switch (First[1]) {
4842 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004843 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004844 case 'o':
4845 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004846 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004847 case 'r':
4848 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004849 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004850 case 'R':
4851 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004852 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004853 }
4854 return nullptr;
4855 case 'p':
4856 switch (First[1]) {
4857 case 'm':
4858 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004859 return getDerived().parseBinaryExpr("->*");
Richard Smithc20d1442018-08-20 20:14:49 +00004860 case 'l':
4861 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004862 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004863 case 'L':
4864 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004865 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004866 case 'p': {
4867 First += 2;
4868 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004869 return getDerived().parsePrefixExpr("++");
4870 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004871 if (Ex == nullptr)
4872 return Ex;
4873 return make<PostfixExpr>(Ex, "++");
4874 }
4875 case 's':
4876 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004877 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004878 case 't': {
4879 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004880 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004881 if (L == nullptr)
4882 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004883 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004884 if (R == nullptr)
4885 return nullptr;
4886 return make<MemberExpr>(L, "->", R);
4887 }
4888 }
4889 return nullptr;
4890 case 'q':
4891 if (First[1] == 'u') {
4892 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004893 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004894 if (Cond == nullptr)
4895 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004896 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004897 if (LHS == nullptr)
4898 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004899 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004900 if (RHS == nullptr)
4901 return nullptr;
4902 return make<ConditionalExpr>(Cond, LHS, RHS);
4903 }
4904 return nullptr;
4905 case 'r':
4906 switch (First[1]) {
4907 case 'c': {
4908 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004909 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004910 if (T == nullptr)
4911 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004912 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004913 if (Ex == nullptr)
4914 return Ex;
4915 return make<CastExpr>("reinterpret_cast", T, Ex);
4916 }
4917 case 'm':
4918 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004919 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004920 case 'M':
4921 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004922 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004923 case 's':
4924 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004925 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004926 case 'S':
4927 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004928 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004929 }
4930 return nullptr;
4931 case 's':
4932 switch (First[1]) {
4933 case 'c': {
4934 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004935 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004936 if (T == nullptr)
4937 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004938 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004939 if (Ex == nullptr)
4940 return Ex;
4941 return make<CastExpr>("static_cast", T, Ex);
4942 }
Richard Smith1865d2f2020-10-22 19:29:36 -07004943 case 'o':
4944 First += 2;
4945 return parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004946 case 'p': {
4947 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004948 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004949 if (Child == nullptr)
4950 return nullptr;
4951 return make<ParameterPackExpansion>(Child);
4952 }
4953 case 'r':
Pavel Labathba825192018-10-16 14:29:14 +00004954 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004955 case 't': {
4956 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004957 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004958 if (Ty == nullptr)
4959 return Ty;
4960 return make<EnclosingExpr>("sizeof (", Ty, ")");
4961 }
4962 case 'z': {
4963 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004964 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004965 if (Ex == nullptr)
4966 return Ex;
4967 return make<EnclosingExpr>("sizeof (", Ex, ")");
4968 }
4969 case 'Z':
4970 First += 2;
4971 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004972 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004973 if (R == nullptr)
4974 return nullptr;
4975 return make<SizeofParamPackExpr>(R);
4976 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004977 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004978 if (FP == nullptr)
4979 return nullptr;
4980 return make<EnclosingExpr>("sizeof... (", FP, ")");
4981 }
4982 return nullptr;
4983 case 'P': {
4984 First += 2;
4985 size_t ArgsBegin = Names.size();
4986 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004987 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004988 if (Arg == nullptr)
4989 return nullptr;
4990 Names.push_back(Arg);
4991 }
Richard Smithb485b352018-08-24 23:30:26 +00004992 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4993 if (!Pack)
4994 return nullptr;
4995 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00004996 }
4997 }
4998 return nullptr;
4999 case 't':
5000 switch (First[1]) {
5001 case 'e': {
5002 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005003 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005004 if (Ex == nullptr)
5005 return Ex;
5006 return make<EnclosingExpr>("typeid (", Ex, ")");
5007 }
5008 case 'i': {
5009 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005010 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005011 if (Ty == nullptr)
5012 return Ty;
5013 return make<EnclosingExpr>("typeid (", Ty, ")");
5014 }
5015 case 'l': {
5016 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005017 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005018 if (Ty == nullptr)
5019 return nullptr;
5020 size_t InitsBegin = Names.size();
5021 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005022 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005023 if (E == nullptr)
5024 return nullptr;
5025 Names.push_back(E);
5026 }
5027 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
5028 }
5029 case 'r':
5030 First += 2;
5031 return make<NameType>("throw");
5032 case 'w': {
5033 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005034 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005035 if (Ex == nullptr)
5036 return nullptr;
5037 return make<ThrowExpr>(Ex);
5038 }
5039 }
5040 return nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05005041 case 'u': {
5042 ++First;
5043 Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
5044 if (!Name)
5045 return nullptr;
5046 // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
5047 // standard encoding expects a <template-arg>, and would be otherwise be
5048 // interpreted as <type> node 'short' or 'ellipsis'. However, neither
5049 // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
5050 // actual conflict here.
5051 if (Name->getBaseName() == "__uuidof") {
5052 if (numLeft() < 2)
5053 return nullptr;
5054 if (*First == 't') {
5055 ++First;
5056 Node *Ty = getDerived().parseType();
5057 if (!Ty)
5058 return nullptr;
5059 return make<CallExpr>(Name, makeNodeArray(&Ty, &Ty + 1));
5060 }
5061 if (*First == 'z') {
5062 ++First;
5063 Node *Ex = getDerived().parseExpr();
5064 if (!Ex)
5065 return nullptr;
5066 return make<CallExpr>(Name, makeNodeArray(&Ex, &Ex + 1));
5067 }
5068 }
5069 size_t ExprsBegin = Names.size();
5070 while (!consumeIf('E')) {
5071 Node *E = getDerived().parseTemplateArg();
5072 if (E == nullptr)
5073 return E;
5074 Names.push_back(E);
5075 }
5076 return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin));
5077 }
Richard Smithc20d1442018-08-20 20:14:49 +00005078 case '1':
5079 case '2':
5080 case '3':
5081 case '4':
5082 case '5':
5083 case '6':
5084 case '7':
5085 case '8':
5086 case '9':
Pavel Labathba825192018-10-16 14:29:14 +00005087 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00005088 }
5089 return nullptr;
5090}
5091
5092// <call-offset> ::= h <nv-offset> _
5093// ::= v <v-offset> _
5094//
5095// <nv-offset> ::= <offset number>
5096// # non-virtual base override
5097//
5098// <v-offset> ::= <offset number> _ <virtual offset number>
5099// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00005100template <typename Alloc, typename Derived>
5101bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00005102 // Just scan through the call offset, we never add this information into the
5103 // output.
5104 if (consumeIf('h'))
5105 return parseNumber(true).empty() || !consumeIf('_');
5106 if (consumeIf('v'))
5107 return parseNumber(true).empty() || !consumeIf('_') ||
5108 parseNumber(true).empty() || !consumeIf('_');
5109 return true;
5110}
5111
5112// <special-name> ::= TV <type> # virtual table
5113// ::= TT <type> # VTT structure (construction vtable index)
5114// ::= TI <type> # typeinfo structure
5115// ::= TS <type> # typeinfo name (null-terminated byte string)
5116// ::= Tc <call-offset> <call-offset> <base encoding>
5117// # base is the nominal target function of thunk
5118// # first call-offset is 'this' adjustment
5119// # second call-offset is result adjustment
5120// ::= T <call-offset> <base encoding>
5121// # base is the nominal target function of thunk
5122// ::= GV <object name> # Guard variable for one-time initialization
5123// # No <type>
5124// ::= TW <object name> # Thread-local wrapper
5125// ::= TH <object name> # Thread-local initialization
5126// ::= GR <object name> _ # First temporary
5127// ::= GR <object name> <seq-id> _ # Subsequent temporaries
5128// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
5129// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00005130template <typename Derived, typename Alloc>
5131Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00005132 switch (look()) {
5133 case 'T':
5134 switch (look(1)) {
Richard Smith1865d2f2020-10-22 19:29:36 -07005135 // TA <template-arg> # template parameter object
5136 //
5137 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
5138 case 'A': {
5139 First += 2;
5140 Node *Arg = getDerived().parseTemplateArg();
5141 if (Arg == nullptr)
5142 return nullptr;
5143 return make<SpecialName>("template parameter object for ", Arg);
5144 }
Richard Smithc20d1442018-08-20 20:14:49 +00005145 // TV <type> # virtual table
5146 case 'V': {
5147 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005148 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005149 if (Ty == nullptr)
5150 return nullptr;
5151 return make<SpecialName>("vtable for ", Ty);
5152 }
5153 // TT <type> # VTT structure (construction vtable index)
5154 case 'T': {
5155 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005156 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005157 if (Ty == nullptr)
5158 return nullptr;
5159 return make<SpecialName>("VTT for ", Ty);
5160 }
5161 // TI <type> # typeinfo structure
5162 case 'I': {
5163 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005164 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005165 if (Ty == nullptr)
5166 return nullptr;
5167 return make<SpecialName>("typeinfo for ", Ty);
5168 }
5169 // TS <type> # typeinfo name (null-terminated byte string)
5170 case 'S': {
5171 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005172 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005173 if (Ty == nullptr)
5174 return nullptr;
5175 return make<SpecialName>("typeinfo name for ", Ty);
5176 }
5177 // Tc <call-offset> <call-offset> <base encoding>
5178 case 'c': {
5179 First += 2;
5180 if (parseCallOffset() || parseCallOffset())
5181 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005182 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005183 if (Encoding == nullptr)
5184 return nullptr;
5185 return make<SpecialName>("covariant return thunk to ", Encoding);
5186 }
5187 // extension ::= TC <first type> <number> _ <second type>
5188 // # construction vtable for second-in-first
5189 case 'C': {
5190 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005191 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005192 if (FirstType == nullptr)
5193 return nullptr;
5194 if (parseNumber(true).empty() || !consumeIf('_'))
5195 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005196 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005197 if (SecondType == nullptr)
5198 return nullptr;
5199 return make<CtorVtableSpecialName>(SecondType, FirstType);
5200 }
5201 // TW <object name> # Thread-local wrapper
5202 case 'W': {
5203 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005204 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005205 if (Name == nullptr)
5206 return nullptr;
5207 return make<SpecialName>("thread-local wrapper routine for ", Name);
5208 }
5209 // TH <object name> # Thread-local initialization
5210 case 'H': {
5211 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005212 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005213 if (Name == nullptr)
5214 return nullptr;
5215 return make<SpecialName>("thread-local initialization routine for ", Name);
5216 }
5217 // T <call-offset> <base encoding>
5218 default: {
5219 ++First;
5220 bool IsVirt = look() == 'v';
5221 if (parseCallOffset())
5222 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005223 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005224 if (BaseEncoding == nullptr)
5225 return nullptr;
5226 if (IsVirt)
5227 return make<SpecialName>("virtual thunk to ", BaseEncoding);
5228 else
5229 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
5230 }
5231 }
5232 case 'G':
5233 switch (look(1)) {
5234 // GV <object name> # Guard variable for one-time initialization
5235 case 'V': {
5236 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005237 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005238 if (Name == nullptr)
5239 return nullptr;
5240 return make<SpecialName>("guard variable for ", Name);
5241 }
5242 // GR <object name> # reference temporary for object
5243 // GR <object name> _ # First temporary
5244 // GR <object name> <seq-id> _ # Subsequent temporaries
5245 case 'R': {
5246 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005247 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005248 if (Name == nullptr)
5249 return nullptr;
5250 size_t Count;
5251 bool ParsedSeqId = !parseSeqId(&Count);
5252 if (!consumeIf('_') && ParsedSeqId)
5253 return nullptr;
5254 return make<SpecialName>("reference temporary for ", Name);
5255 }
5256 }
5257 }
5258 return nullptr;
5259}
5260
5261// <encoding> ::= <function name> <bare-function-type>
5262// ::= <data name>
5263// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00005264template <typename Derived, typename Alloc>
5265Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithfac39712020-07-09 21:08:39 -07005266 // The template parameters of an encoding are unrelated to those of the
5267 // enclosing context.
5268 class SaveTemplateParams {
5269 AbstractManglingParser *Parser;
5270 decltype(TemplateParams) OldParams;
Justin Lebar2c536232021-06-09 16:57:22 -07005271 decltype(OuterTemplateParams) OldOuterParams;
Richard Smithfac39712020-07-09 21:08:39 -07005272
5273 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04005274 SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
Richard Smithfac39712020-07-09 21:08:39 -07005275 OldParams = std::move(Parser->TemplateParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005276 OldOuterParams = std::move(Parser->OuterTemplateParams);
Richard Smithfac39712020-07-09 21:08:39 -07005277 Parser->TemplateParams.clear();
Justin Lebar2c536232021-06-09 16:57:22 -07005278 Parser->OuterTemplateParams.clear();
Richard Smithfac39712020-07-09 21:08:39 -07005279 }
5280 ~SaveTemplateParams() {
5281 Parser->TemplateParams = std::move(OldParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005282 Parser->OuterTemplateParams = std::move(OldOuterParams);
Richard Smithfac39712020-07-09 21:08:39 -07005283 }
5284 } SaveTemplateParams(this);
Richard Smithfd434322020-07-09 20:36:04 -07005285
Richard Smithc20d1442018-08-20 20:14:49 +00005286 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00005287 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00005288
5289 auto IsEndOfEncoding = [&] {
5290 // The set of chars that can potentially follow an <encoding> (none of which
5291 // can start a <type>). Enumerating these allows us to avoid speculative
5292 // parsing.
5293 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5294 };
5295
5296 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00005297 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00005298 if (Name == nullptr)
5299 return nullptr;
5300
5301 if (resolveForwardTemplateRefs(NameInfo))
5302 return nullptr;
5303
5304 if (IsEndOfEncoding())
5305 return Name;
5306
5307 Node *Attrs = nullptr;
5308 if (consumeIf("Ua9enable_ifI")) {
5309 size_t BeforeArgs = Names.size();
5310 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005311 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005312 if (Arg == nullptr)
5313 return nullptr;
5314 Names.push_back(Arg);
5315 }
5316 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00005317 if (!Attrs)
5318 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005319 }
5320
5321 Node *ReturnType = nullptr;
5322 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00005323 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005324 if (ReturnType == nullptr)
5325 return nullptr;
5326 }
5327
5328 if (consumeIf('v'))
5329 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5330 Attrs, NameInfo.CVQualifiers,
5331 NameInfo.ReferenceQualifier);
5332
5333 size_t ParamsBegin = Names.size();
5334 do {
Pavel Labathba825192018-10-16 14:29:14 +00005335 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005336 if (Ty == nullptr)
5337 return nullptr;
5338 Names.push_back(Ty);
5339 } while (!IsEndOfEncoding());
5340
5341 return make<FunctionEncoding>(ReturnType, Name,
5342 popTrailingNodeArray(ParamsBegin),
5343 Attrs, NameInfo.CVQualifiers,
5344 NameInfo.ReferenceQualifier);
5345}
5346
5347template <class Float>
5348struct FloatData;
5349
5350template <>
5351struct FloatData<float>
5352{
5353 static const size_t mangled_size = 8;
5354 static const size_t max_demangled_size = 24;
5355 static constexpr const char* spec = "%af";
5356};
5357
5358template <>
5359struct FloatData<double>
5360{
5361 static const size_t mangled_size = 16;
5362 static const size_t max_demangled_size = 32;
5363 static constexpr const char* spec = "%a";
5364};
5365
5366template <>
5367struct FloatData<long double>
5368{
5369#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5370 defined(__wasm__)
5371 static const size_t mangled_size = 32;
5372#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5373 static const size_t mangled_size = 16;
5374#else
5375 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5376#endif
Elliott Hughes5a360ea2020-04-10 17:42:00 -07005377 // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
5378 // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
5379 // Negatives are one character longer than positives.
5380 // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
5381 // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
5382 static const size_t max_demangled_size = 42;
Richard Smithc20d1442018-08-20 20:14:49 +00005383 static constexpr const char *spec = "%LaL";
5384};
5385
Pavel Labathba825192018-10-16 14:29:14 +00005386template <typename Alloc, typename Derived>
5387template <class Float>
5388Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005389 const size_t N = FloatData<Float>::mangled_size;
5390 if (numLeft() <= N)
5391 return nullptr;
5392 StringView Data(First, First + N);
5393 for (char C : Data)
5394 if (!std::isxdigit(C))
5395 return nullptr;
5396 First += N;
5397 if (!consumeIf('E'))
5398 return nullptr;
5399 return make<FloatLiteralImpl<Float>>(Data);
5400}
5401
5402// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005403template <typename Alloc, typename Derived>
5404bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005405 if (!(look() >= '0' && look() <= '9') &&
5406 !(look() >= 'A' && look() <= 'Z'))
5407 return true;
5408
5409 size_t Id = 0;
5410 while (true) {
5411 if (look() >= '0' && look() <= '9') {
5412 Id *= 36;
5413 Id += static_cast<size_t>(look() - '0');
5414 } else if (look() >= 'A' && look() <= 'Z') {
5415 Id *= 36;
5416 Id += static_cast<size_t>(look() - 'A') + 10;
5417 } else {
5418 *Out = Id;
5419 return false;
5420 }
5421 ++First;
5422 }
5423}
5424
5425// <substitution> ::= S <seq-id> _
5426// ::= S_
5427// <substitution> ::= Sa # ::std::allocator
5428// <substitution> ::= Sb # ::std::basic_string
5429// <substitution> ::= Ss # ::std::basic_string < char,
5430// ::std::char_traits<char>,
5431// ::std::allocator<char> >
5432// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5433// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5434// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Pavel Labathba825192018-10-16 14:29:14 +00005435template <typename Derived, typename Alloc>
5436Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005437 if (!consumeIf('S'))
5438 return nullptr;
5439
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08005440 if (look() >= 'a' && look() <= 'z') {
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005441 SpecialSubKind Kind;
Richard Smithc20d1442018-08-20 20:14:49 +00005442 switch (look()) {
5443 case 'a':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005444 Kind = SpecialSubKind::allocator;
Richard Smithc20d1442018-08-20 20:14:49 +00005445 break;
5446 case 'b':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005447 Kind = SpecialSubKind::basic_string;
Richard Smithc20d1442018-08-20 20:14:49 +00005448 break;
5449 case 'd':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005450 Kind = SpecialSubKind::iostream;
5451 break;
5452 case 'i':
5453 Kind = SpecialSubKind::istream;
5454 break;
5455 case 'o':
5456 Kind = SpecialSubKind::ostream;
5457 break;
5458 case 's':
5459 Kind = SpecialSubKind::string;
Richard Smithc20d1442018-08-20 20:14:49 +00005460 break;
5461 default:
5462 return nullptr;
5463 }
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005464 ++First;
5465 auto *SpecialSub = make<SpecialSubstitution>(Kind);
Richard Smithb485b352018-08-24 23:30:26 +00005466 if (!SpecialSub)
5467 return nullptr;
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005468
Richard Smithc20d1442018-08-20 20:14:49 +00005469 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5470 // has ABI tags, the tags are appended to the substitution; the result is a
5471 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005472 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005473 if (WithTags != SpecialSub) {
5474 Subs.push_back(WithTags);
5475 SpecialSub = WithTags;
5476 }
5477 return SpecialSub;
5478 }
5479
5480 // ::= S_
5481 if (consumeIf('_')) {
5482 if (Subs.empty())
5483 return nullptr;
5484 return Subs[0];
5485 }
5486
5487 // ::= S <seq-id> _
5488 size_t Index = 0;
5489 if (parseSeqId(&Index))
5490 return nullptr;
5491 ++Index;
5492 if (!consumeIf('_') || Index >= Subs.size())
5493 return nullptr;
5494 return Subs[Index];
5495}
5496
5497// <template-param> ::= T_ # first template parameter
5498// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005499// ::= TL <level-1> __
5500// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005501template <typename Derived, typename Alloc>
5502Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005503 if (!consumeIf('T'))
5504 return nullptr;
5505
Richard Smithdf1c14c2019-09-06 23:53:21 +00005506 size_t Level = 0;
5507 if (consumeIf('L')) {
5508 if (parsePositiveInteger(&Level))
5509 return nullptr;
5510 ++Level;
5511 if (!consumeIf('_'))
5512 return nullptr;
5513 }
5514
Richard Smithc20d1442018-08-20 20:14:49 +00005515 size_t Index = 0;
5516 if (!consumeIf('_')) {
5517 if (parsePositiveInteger(&Index))
5518 return nullptr;
5519 ++Index;
5520 if (!consumeIf('_'))
5521 return nullptr;
5522 }
5523
Richard Smithc20d1442018-08-20 20:14:49 +00005524 // If we're in a context where this <template-param> refers to a
5525 // <template-arg> further ahead in the mangled name (currently just conversion
5526 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005527 // This can only happen at the outermost level.
5528 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005529 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5530 if (!ForwardRef)
5531 return nullptr;
5532 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5533 ForwardTemplateRefs.push_back(
5534 static_cast<ForwardTemplateReference *>(ForwardRef));
5535 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005536 }
5537
Richard Smithdf1c14c2019-09-06 23:53:21 +00005538 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5539 Index >= TemplateParams[Level]->size()) {
5540 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5541 // list are mangled as the corresponding artificial template type parameter.
5542 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5543 // This will be popped by the ScopedTemplateParamList in
5544 // parseUnnamedTypeName.
5545 if (Level == TemplateParams.size())
5546 TemplateParams.push_back(nullptr);
5547 return make<NameType>("auto");
5548 }
5549
Richard Smithc20d1442018-08-20 20:14:49 +00005550 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005551 }
5552
5553 return (*TemplateParams[Level])[Index];
5554}
5555
5556// <template-param-decl> ::= Ty # type parameter
5557// ::= Tn <type> # non-type parameter
5558// ::= Tt <template-param-decl>* E # template parameter
5559// ::= Tp <template-param-decl> # parameter pack
5560template <typename Derived, typename Alloc>
5561Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5562 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5563 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5564 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5565 if (N) TemplateParams.back()->push_back(N);
5566 return N;
5567 };
5568
5569 if (consumeIf("Ty")) {
5570 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5571 if (!Name)
5572 return nullptr;
5573 return make<TypeTemplateParamDecl>(Name);
5574 }
5575
5576 if (consumeIf("Tn")) {
5577 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5578 if (!Name)
5579 return nullptr;
5580 Node *Type = parseType();
5581 if (!Type)
5582 return nullptr;
5583 return make<NonTypeTemplateParamDecl>(Name, Type);
5584 }
5585
5586 if (consumeIf("Tt")) {
5587 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5588 if (!Name)
5589 return nullptr;
5590 size_t ParamsBegin = Names.size();
5591 ScopedTemplateParamList TemplateTemplateParamParams(this);
5592 while (!consumeIf("E")) {
5593 Node *P = parseTemplateParamDecl();
5594 if (!P)
5595 return nullptr;
5596 Names.push_back(P);
5597 }
5598 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5599 return make<TemplateTemplateParamDecl>(Name, Params);
5600 }
5601
5602 if (consumeIf("Tp")) {
5603 Node *P = parseTemplateParamDecl();
5604 if (!P)
5605 return nullptr;
5606 return make<TemplateParamPackDecl>(P);
5607 }
5608
5609 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005610}
5611
5612// <template-arg> ::= <type> # type or template
5613// ::= X <expression> E # expression
5614// ::= <expr-primary> # simple expressions
5615// ::= J <template-arg>* E # argument pack
5616// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005617template <typename Derived, typename Alloc>
5618Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005619 switch (look()) {
5620 case 'X': {
5621 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005622 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005623 if (Arg == nullptr || !consumeIf('E'))
5624 return nullptr;
5625 return Arg;
5626 }
5627 case 'J': {
5628 ++First;
5629 size_t ArgsBegin = Names.size();
5630 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005631 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005632 if (Arg == nullptr)
5633 return nullptr;
5634 Names.push_back(Arg);
5635 }
5636 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5637 return make<TemplateArgumentPack>(Args);
5638 }
5639 case 'L': {
5640 // ::= LZ <encoding> E # extension
5641 if (look(1) == 'Z') {
5642 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005643 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005644 if (Arg == nullptr || !consumeIf('E'))
5645 return nullptr;
5646 return Arg;
5647 }
5648 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005649 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005650 }
5651 default:
Pavel Labathba825192018-10-16 14:29:14 +00005652 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005653 }
5654}
5655
5656// <template-args> ::= I <template-arg>* E
5657// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005658template <typename Derived, typename Alloc>
5659Node *
5660AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005661 if (!consumeIf('I'))
5662 return nullptr;
5663
5664 // <template-params> refer to the innermost <template-args>. Clear out any
5665 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005666 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005667 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005668 TemplateParams.push_back(&OuterTemplateParams);
5669 OuterTemplateParams.clear();
5670 }
Richard Smithc20d1442018-08-20 20:14:49 +00005671
5672 size_t ArgsBegin = Names.size();
5673 while (!consumeIf('E')) {
5674 if (TagTemplates) {
5675 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005676 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005677 TemplateParams = std::move(OldParams);
5678 if (Arg == nullptr)
5679 return nullptr;
5680 Names.push_back(Arg);
5681 Node *TableEntry = Arg;
5682 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5683 TableEntry = make<ParameterPack>(
5684 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005685 if (!TableEntry)
5686 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005687 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005688 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005689 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005690 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005691 if (Arg == nullptr)
5692 return nullptr;
5693 Names.push_back(Arg);
5694 }
5695 }
5696 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5697}
5698
5699// <mangled-name> ::= _Z <encoding>
5700// ::= <type>
5701// extension ::= ___Z <encoding> _block_invoke
5702// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5703// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005704template <typename Derived, typename Alloc>
5705Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005706 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005707 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005708 if (Encoding == nullptr)
5709 return nullptr;
5710 if (look() == '.') {
5711 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5712 First = Last;
5713 }
5714 if (numLeft() != 0)
5715 return nullptr;
5716 return Encoding;
5717 }
5718
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005719 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005720 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005721 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5722 return nullptr;
5723 bool RequireNumber = consumeIf('_');
5724 if (parseNumber().empty() && RequireNumber)
5725 return nullptr;
5726 if (look() == '.')
5727 First = Last;
5728 if (numLeft() != 0)
5729 return nullptr;
5730 return make<SpecialName>("invocation function for block in ", Encoding);
5731 }
5732
Pavel Labathba825192018-10-16 14:29:14 +00005733 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005734 if (numLeft() != 0)
5735 return nullptr;
5736 return Ty;
5737}
5738
Pavel Labathba825192018-10-16 14:29:14 +00005739template <typename Alloc>
5740struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5741 using AbstractManglingParser<ManglingParser<Alloc>,
5742 Alloc>::AbstractManglingParser;
5743};
5744
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005745DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005746
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005747#endif // DEMANGLE_ITANIUMDEMANGLE_H