blob: d005db5476cd60c406080ceb6b790bf4d1e1a643 [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
Nathan Sidwellf19ccea2022-02-18 04:05:06 -080035#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) \
63 X(BinaryFPType) \
64 X(SyntheticTemplateParamName) \
65 X(TypeTemplateParamDecl) \
66 X(NonTypeTemplateParamDecl) \
67 X(TemplateTemplateParamDecl) \
68 X(TemplateParamPackDecl) \
69 X(ParameterPack) \
70 X(TemplateArgumentPack) \
71 X(ParameterPackExpansion) \
72 X(TemplateArgs) \
73 X(ForwardTemplateReference) \
74 X(NameWithTemplateArgs) \
75 X(GlobalQualifiedName) \
76 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) \
88 X(SubobjectExpr) \
89 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) \
98 X(PointerToMemberConversionExpr) \
99 X(InitListExpr) \
100 X(FoldExpr) \
101 X(ThrowExpr) \
102 X(BoolExpr) \
103 X(StringLiteral) \
104 X(LambdaExpr) \
105 X(EnumLiteral) \
106 X(IntegerLiteral) \
107 X(FloatLiteral) \
108 X(DoubleLiteral) \
109 X(LongDoubleLiteral) \
110 X(BracedExpr) \
111 X(BracedRangeExpr)
Richard Smithc20d1442018-08-20 20:14:49 +0000112
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000113DEMANGLE_NAMESPACE_BEGIN
114
Mikhail Borisov8452f062021-08-17 18:06:53 -0400115template <class T, size_t N> class PODSmallVector {
116 static_assert(std::is_pod<T>::value,
117 "T is required to be a plain old data type");
118
119 T *First = nullptr;
120 T *Last = nullptr;
121 T *Cap = nullptr;
122 T Inline[N] = {0};
123
124 bool isInline() const { return First == Inline; }
125
126 void clearInline() {
127 First = Inline;
128 Last = Inline;
129 Cap = Inline + N;
130 }
131
132 void reserve(size_t NewCap) {
133 size_t S = size();
134 if (isInline()) {
135 auto *Tmp = static_cast<T *>(std::malloc(NewCap * sizeof(T)));
136 if (Tmp == nullptr)
137 std::terminate();
138 std::copy(First, Last, Tmp);
139 First = Tmp;
140 } else {
141 First = static_cast<T *>(std::realloc(First, NewCap * sizeof(T)));
142 if (First == nullptr)
143 std::terminate();
144 }
145 Last = First + S;
146 Cap = First + NewCap;
147 }
148
149public:
150 PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
151
152 PODSmallVector(const PODSmallVector &) = delete;
153 PODSmallVector &operator=(const PODSmallVector &) = delete;
154
155 PODSmallVector(PODSmallVector &&Other) : PODSmallVector() {
156 if (Other.isInline()) {
157 std::copy(Other.begin(), Other.end(), First);
158 Last = First + Other.size();
159 Other.clear();
160 return;
161 }
162
163 First = Other.First;
164 Last = Other.Last;
165 Cap = Other.Cap;
166 Other.clearInline();
167 }
168
169 PODSmallVector &operator=(PODSmallVector &&Other) {
170 if (Other.isInline()) {
171 if (!isInline()) {
172 std::free(First);
173 clearInline();
174 }
175 std::copy(Other.begin(), Other.end(), First);
176 Last = First + Other.size();
177 Other.clear();
178 return *this;
179 }
180
181 if (isInline()) {
182 First = Other.First;
183 Last = Other.Last;
184 Cap = Other.Cap;
185 Other.clearInline();
186 return *this;
187 }
188
189 std::swap(First, Other.First);
190 std::swap(Last, Other.Last);
191 std::swap(Cap, Other.Cap);
192 Other.clear();
193 return *this;
194 }
195
196 // NOLINTNEXTLINE(readability-identifier-naming)
197 void push_back(const T &Elem) {
198 if (Last == Cap)
199 reserve(size() * 2);
200 *Last++ = Elem;
201 }
202
203 // NOLINTNEXTLINE(readability-identifier-naming)
204 void pop_back() {
205 assert(Last != First && "Popping empty vector!");
206 --Last;
207 }
208
209 void dropBack(size_t Index) {
210 assert(Index <= size() && "dropBack() can't expand!");
211 Last = First + Index;
212 }
213
214 T *begin() { return First; }
215 T *end() { return Last; }
216
217 bool empty() const { return First == Last; }
218 size_t size() const { return static_cast<size_t>(Last - First); }
219 T &back() {
220 assert(Last != First && "Calling back() on empty vector!");
221 return *(Last - 1);
222 }
223 T &operator[](size_t Index) {
224 assert(Index < size() && "Invalid access!");
225 return *(begin() + Index);
226 }
227 void clear() { Last = First; }
228
229 ~PODSmallVector() {
230 if (!isInline())
231 std::free(First);
232 }
233};
234
Richard Smithc20d1442018-08-20 20:14:49 +0000235// Base class of all AST nodes. The AST is built by the parser, then is
236// traversed by the printLeft/Right functions to produce a demangled string.
237class Node {
238public:
239 enum Kind : unsigned char {
240#define ENUMERATOR(NodeKind) K ## NodeKind,
241 FOR_EACH_NODE_KIND(ENUMERATOR)
242#undef ENUMERATOR
243 };
244
245 /// Three-way bool to track a cached value. Unknown is possible if this node
246 /// has an unexpanded parameter pack below it that may affect this cache.
247 enum class Cache : unsigned char { Yes, No, Unknown, };
248
249private:
250 Kind K;
251
252 // FIXME: Make these protected.
253public:
254 /// Tracks if this node has a component on its right side, in which case we
255 /// need to call printRight.
256 Cache RHSComponentCache;
257
258 /// Track if this node is a (possibly qualified) array type. This can affect
259 /// how we format the output string.
260 Cache ArrayCache;
261
262 /// Track if this node is a (possibly qualified) function type. This can
263 /// affect how we format the output string.
264 Cache FunctionCache;
265
266public:
267 Node(Kind K_, Cache RHSComponentCache_ = Cache::No,
268 Cache ArrayCache_ = Cache::No, Cache FunctionCache_ = Cache::No)
269 : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_),
270 FunctionCache(FunctionCache_) {}
271
272 /// Visit the most-derived object corresponding to this object.
273 template<typename Fn> void visit(Fn F) const;
274
275 // The following function is provided by all derived classes:
276 //
277 // Call F with arguments that, when passed to the constructor of this node,
278 // would construct an equivalent node.
279 //template<typename Fn> void match(Fn F) const;
280
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700281 bool hasRHSComponent(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000282 if (RHSComponentCache != Cache::Unknown)
283 return RHSComponentCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700284 return hasRHSComponentSlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000285 }
286
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700287 bool hasArray(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000288 if (ArrayCache != Cache::Unknown)
289 return ArrayCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700290 return hasArraySlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000291 }
292
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700293 bool hasFunction(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000294 if (FunctionCache != Cache::Unknown)
295 return FunctionCache == Cache::Yes;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700296 return hasFunctionSlow(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000297 }
298
299 Kind getKind() const { return K; }
300
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700301 virtual bool hasRHSComponentSlow(OutputBuffer &) const { return false; }
302 virtual bool hasArraySlow(OutputBuffer &) const { return false; }
303 virtual bool hasFunctionSlow(OutputBuffer &) const { return false; }
Richard Smithc20d1442018-08-20 20:14:49 +0000304
305 // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
306 // get at a node that actually represents some concrete syntax.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700307 virtual const Node *getSyntaxNode(OutputBuffer &) const { return this; }
Richard Smithc20d1442018-08-20 20:14:49 +0000308
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700309 void print(OutputBuffer &OB) const {
310 printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000311 if (RHSComponentCache != Cache::No)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700312 printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000313 }
314
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -0800315 // Print the "left" side of this Node into OutputBuffer.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700316 virtual void printLeft(OutputBuffer &) const = 0;
Richard Smithc20d1442018-08-20 20:14:49 +0000317
318 // Print the "right". This distinction is necessary to represent C++ types
319 // that appear on the RHS of their subtype, such as arrays or functions.
320 // Since most types don't have such a component, provide a default
321 // implementation.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700322 virtual void printRight(OutputBuffer &) const {}
Richard Smithc20d1442018-08-20 20:14:49 +0000323
324 virtual StringView getBaseName() const { return StringView(); }
325
326 // Silence compiler warnings, this dtor will never be called.
327 virtual ~Node() = default;
328
329#ifndef NDEBUG
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000330 DEMANGLE_DUMP_METHOD void dump() const;
Richard Smithc20d1442018-08-20 20:14:49 +0000331#endif
332};
333
334class NodeArray {
335 Node **Elements;
336 size_t NumElements;
337
338public:
339 NodeArray() : Elements(nullptr), NumElements(0) {}
340 NodeArray(Node **Elements_, size_t NumElements_)
341 : Elements(Elements_), NumElements(NumElements_) {}
342
343 bool empty() const { return NumElements == 0; }
344 size_t size() const { return NumElements; }
345
346 Node **begin() const { return Elements; }
347 Node **end() const { return Elements + NumElements; }
348
349 Node *operator[](size_t Idx) const { return Elements[Idx]; }
350
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700351 void printWithComma(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000352 bool FirstElement = true;
353 for (size_t Idx = 0; Idx != NumElements; ++Idx) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700354 size_t BeforeComma = OB.getCurrentPosition();
Richard Smithc20d1442018-08-20 20:14:49 +0000355 if (!FirstElement)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700356 OB += ", ";
357 size_t AfterComma = OB.getCurrentPosition();
358 Elements[Idx]->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000359
360 // Elements[Idx] is an empty parameter pack expansion, we should erase the
361 // comma we just printed.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700362 if (AfterComma == OB.getCurrentPosition()) {
363 OB.setCurrentPosition(BeforeComma);
Richard Smithc20d1442018-08-20 20:14:49 +0000364 continue;
365 }
366
367 FirstElement = false;
368 }
369 }
370};
371
372struct NodeArrayNode : Node {
373 NodeArray Array;
374 NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
375
376 template<typename Fn> void match(Fn F) const { F(Array); }
377
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700378 void printLeft(OutputBuffer &OB) const override { Array.printWithComma(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000379};
380
381class DotSuffix final : public Node {
382 const Node *Prefix;
383 const StringView Suffix;
384
385public:
386 DotSuffix(const Node *Prefix_, StringView Suffix_)
387 : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
388
389 template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
390
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700391 void printLeft(OutputBuffer &OB) const override {
392 Prefix->print(OB);
393 OB += " (";
394 OB += Suffix;
395 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000396 }
397};
398
399class VendorExtQualType final : public Node {
400 const Node *Ty;
401 StringView Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400402 const Node *TA;
Richard Smithc20d1442018-08-20 20:14:49 +0000403
404public:
Alex Orlovf50df922021-03-24 10:21:32 +0400405 VendorExtQualType(const Node *Ty_, StringView Ext_, const Node *TA_)
406 : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_), TA(TA_) {}
Richard Smithc20d1442018-08-20 20:14:49 +0000407
Alex Orlovf50df922021-03-24 10:21:32 +0400408 template <typename Fn> void match(Fn F) const { F(Ty, Ext, TA); }
Richard Smithc20d1442018-08-20 20:14:49 +0000409
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700410 void printLeft(OutputBuffer &OB) const override {
411 Ty->print(OB);
412 OB += " ";
413 OB += Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400414 if (TA != nullptr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700415 TA->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000416 }
417};
418
419enum FunctionRefQual : unsigned char {
420 FrefQualNone,
421 FrefQualLValue,
422 FrefQualRValue,
423};
424
425enum Qualifiers {
426 QualNone = 0,
427 QualConst = 0x1,
428 QualVolatile = 0x2,
429 QualRestrict = 0x4,
430};
431
432inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
433 return Q1 = static_cast<Qualifiers>(Q1 | Q2);
434}
435
Richard Smithdf1c14c2019-09-06 23:53:21 +0000436class QualType final : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +0000437protected:
438 const Qualifiers Quals;
439 const Node *Child;
440
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700441 void printQuals(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000442 if (Quals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700443 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000444 if (Quals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700445 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000446 if (Quals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700447 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000448 }
449
450public:
451 QualType(const Node *Child_, Qualifiers Quals_)
452 : Node(KQualType, Child_->RHSComponentCache,
453 Child_->ArrayCache, Child_->FunctionCache),
454 Quals(Quals_), Child(Child_) {}
455
456 template<typename Fn> void match(Fn F) const { F(Child, Quals); }
457
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700458 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
459 return Child->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000460 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700461 bool hasArraySlow(OutputBuffer &OB) const override {
462 return Child->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000463 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700464 bool hasFunctionSlow(OutputBuffer &OB) const override {
465 return Child->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000466 }
467
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700468 void printLeft(OutputBuffer &OB) const override {
469 Child->printLeft(OB);
470 printQuals(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000471 }
472
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700473 void printRight(OutputBuffer &OB) const override { Child->printRight(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000474};
475
476class ConversionOperatorType final : public Node {
477 const Node *Ty;
478
479public:
480 ConversionOperatorType(const Node *Ty_)
481 : Node(KConversionOperatorType), Ty(Ty_) {}
482
483 template<typename Fn> void match(Fn F) const { F(Ty); }
484
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700485 void printLeft(OutputBuffer &OB) const override {
486 OB += "operator ";
487 Ty->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000488 }
489};
490
491class PostfixQualifiedType final : public Node {
492 const Node *Ty;
493 const StringView Postfix;
494
495public:
496 PostfixQualifiedType(Node *Ty_, StringView Postfix_)
497 : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
498
499 template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
500
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700501 void printLeft(OutputBuffer &OB) const override {
502 Ty->printLeft(OB);
503 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +0000504 }
505};
506
507class NameType final : public Node {
508 const StringView Name;
509
510public:
511 NameType(StringView Name_) : Node(KNameType), Name(Name_) {}
512
513 template<typename Fn> void match(Fn F) const { F(Name); }
514
515 StringView getName() const { return Name; }
516 StringView getBaseName() const override { return Name; }
517
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700518 void printLeft(OutputBuffer &OB) const override { OB += Name; }
Richard Smithc20d1442018-08-20 20:14:49 +0000519};
520
521class ElaboratedTypeSpefType : public Node {
522 StringView Kind;
523 Node *Child;
524public:
525 ElaboratedTypeSpefType(StringView Kind_, Node *Child_)
526 : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
527
528 template<typename Fn> void match(Fn F) const { F(Kind, Child); }
529
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700530 void printLeft(OutputBuffer &OB) const override {
531 OB += Kind;
532 OB += ' ';
533 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000534 }
535};
536
537struct AbiTagAttr : Node {
538 Node *Base;
539 StringView Tag;
540
541 AbiTagAttr(Node* Base_, StringView Tag_)
542 : Node(KAbiTagAttr, Base_->RHSComponentCache,
543 Base_->ArrayCache, Base_->FunctionCache),
544 Base(Base_), Tag(Tag_) {}
545
546 template<typename Fn> void match(Fn F) const { F(Base, Tag); }
547
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700548 void printLeft(OutputBuffer &OB) const override {
549 Base->printLeft(OB);
550 OB += "[abi:";
551 OB += Tag;
552 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +0000553 }
554};
555
556class EnableIfAttr : public Node {
557 NodeArray Conditions;
558public:
559 EnableIfAttr(NodeArray Conditions_)
560 : Node(KEnableIfAttr), Conditions(Conditions_) {}
561
562 template<typename Fn> void match(Fn F) const { F(Conditions); }
563
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700564 void printLeft(OutputBuffer &OB) const override {
565 OB += " [enable_if:";
566 Conditions.printWithComma(OB);
567 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +0000568 }
569};
570
571class ObjCProtoName : public Node {
572 const Node *Ty;
573 StringView Protocol;
574
575 friend class PointerType;
576
577public:
578 ObjCProtoName(const Node *Ty_, StringView Protocol_)
579 : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
580
581 template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
582
583 bool isObjCObject() const {
584 return Ty->getKind() == KNameType &&
585 static_cast<const NameType *>(Ty)->getName() == "objc_object";
586 }
587
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700588 void printLeft(OutputBuffer &OB) const override {
589 Ty->print(OB);
590 OB += "<";
591 OB += Protocol;
592 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +0000593 }
594};
595
596class PointerType final : public Node {
597 const Node *Pointee;
598
599public:
600 PointerType(const Node *Pointee_)
601 : Node(KPointerType, Pointee_->RHSComponentCache),
602 Pointee(Pointee_) {}
603
604 template<typename Fn> void match(Fn F) const { F(Pointee); }
605
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700606 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
607 return Pointee->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000608 }
609
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700610 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000611 // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
612 if (Pointee->getKind() != KObjCProtoName ||
613 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700614 Pointee->printLeft(OB);
615 if (Pointee->hasArray(OB))
616 OB += " ";
617 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
618 OB += "(";
619 OB += "*";
Richard Smithc20d1442018-08-20 20:14:49 +0000620 } else {
621 const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700622 OB += "id<";
623 OB += objcProto->Protocol;
624 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +0000625 }
626 }
627
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700628 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000629 if (Pointee->getKind() != KObjCProtoName ||
630 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700631 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
632 OB += ")";
633 Pointee->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000634 }
635 }
636};
637
638enum class ReferenceKind {
639 LValue,
640 RValue,
641};
642
643// Represents either a LValue or an RValue reference type.
644class ReferenceType : public Node {
645 const Node *Pointee;
646 ReferenceKind RK;
647
648 mutable bool Printing = false;
649
650 // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
651 // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
652 // other combination collapses to a lvalue ref.
Mikhail Borisov05f77222021-08-17 18:10:57 -0400653 //
654 // A combination of a TemplateForwardReference and a back-ref Substitution
655 // from an ill-formed string may have created a cycle; use cycle detection to
656 // avoid looping forever.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700657 std::pair<ReferenceKind, const Node *> collapse(OutputBuffer &OB) const {
Richard Smithc20d1442018-08-20 20:14:49 +0000658 auto SoFar = std::make_pair(RK, Pointee);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400659 // Track the chain of nodes for the Floyd's 'tortoise and hare'
660 // cycle-detection algorithm, since getSyntaxNode(S) is impure
661 PODSmallVector<const Node *, 8> Prev;
Richard Smithc20d1442018-08-20 20:14:49 +0000662 for (;;) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700663 const Node *SN = SoFar.second->getSyntaxNode(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000664 if (SN->getKind() != KReferenceType)
665 break;
666 auto *RT = static_cast<const ReferenceType *>(SN);
667 SoFar.second = RT->Pointee;
668 SoFar.first = std::min(SoFar.first, RT->RK);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400669
670 // The middle of Prev is the 'slow' pointer moving at half speed
671 Prev.push_back(SoFar.second);
672 if (Prev.size() > 1 && SoFar.second == Prev[(Prev.size() - 1) / 2]) {
673 // Cycle detected
674 SoFar.second = nullptr;
675 break;
676 }
Richard Smithc20d1442018-08-20 20:14:49 +0000677 }
678 return SoFar;
679 }
680
681public:
682 ReferenceType(const Node *Pointee_, ReferenceKind RK_)
683 : Node(KReferenceType, Pointee_->RHSComponentCache),
684 Pointee(Pointee_), RK(RK_) {}
685
686 template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
687
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700688 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
689 return Pointee->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000690 }
691
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700692 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000693 if (Printing)
694 return;
695 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700696 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400697 if (!Collapsed.second)
698 return;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700699 Collapsed.second->printLeft(OB);
700 if (Collapsed.second->hasArray(OB))
701 OB += " ";
702 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
703 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +0000704
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700705 OB += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
Richard Smithc20d1442018-08-20 20:14:49 +0000706 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700707 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000708 if (Printing)
709 return;
710 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700711 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400712 if (!Collapsed.second)
713 return;
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700714 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
715 OB += ")";
716 Collapsed.second->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000717 }
718};
719
720class PointerToMemberType final : public Node {
721 const Node *ClassType;
722 const Node *MemberType;
723
724public:
725 PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
726 : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
727 ClassType(ClassType_), MemberType(MemberType_) {}
728
729 template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
730
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700731 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
732 return MemberType->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000733 }
734
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700735 void printLeft(OutputBuffer &OB) const override {
736 MemberType->printLeft(OB);
737 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
738 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +0000739 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700740 OB += " ";
741 ClassType->print(OB);
742 OB += "::*";
Richard Smithc20d1442018-08-20 20:14:49 +0000743 }
744
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700745 void printRight(OutputBuffer &OB) const override {
746 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
747 OB += ")";
748 MemberType->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000749 }
750};
751
Richard Smithc20d1442018-08-20 20:14:49 +0000752class ArrayType final : public Node {
753 const Node *Base;
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800754 Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +0000755
756public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800757 ArrayType(const Node *Base_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +0000758 : Node(KArrayType,
759 /*RHSComponentCache=*/Cache::Yes,
760 /*ArrayCache=*/Cache::Yes),
761 Base(Base_), Dimension(Dimension_) {}
762
763 template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
764
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700765 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
766 bool hasArraySlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000767
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700768 void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); }
Richard Smithc20d1442018-08-20 20:14:49 +0000769
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700770 void printRight(OutputBuffer &OB) const override {
771 if (OB.back() != ']')
772 OB += " ";
773 OB += "[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800774 if (Dimension)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700775 Dimension->print(OB);
776 OB += "]";
777 Base->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000778 }
779};
780
781class FunctionType final : public Node {
782 const Node *Ret;
783 NodeArray Params;
784 Qualifiers CVQuals;
785 FunctionRefQual RefQual;
786 const Node *ExceptionSpec;
787
788public:
789 FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
790 FunctionRefQual RefQual_, const Node *ExceptionSpec_)
791 : Node(KFunctionType,
792 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
793 /*FunctionCache=*/Cache::Yes),
794 Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
795 ExceptionSpec(ExceptionSpec_) {}
796
797 template<typename Fn> void match(Fn F) const {
798 F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
799 }
800
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700801 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
802 bool hasFunctionSlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000803
804 // Handle C++'s ... quirky decl grammar by using the left & right
805 // distinction. Consider:
806 // int (*f(float))(char) {}
807 // f is a function that takes a float and returns a pointer to a function
808 // that takes a char and returns an int. If we're trying to print f, start
809 // by printing out the return types's left, then print our parameters, then
810 // finally print right of the return type.
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700811 void printLeft(OutputBuffer &OB) const override {
812 Ret->printLeft(OB);
813 OB += " ";
Richard Smithc20d1442018-08-20 20:14:49 +0000814 }
815
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700816 void printRight(OutputBuffer &OB) const override {
817 OB += "(";
818 Params.printWithComma(OB);
819 OB += ")";
820 Ret->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000821
822 if (CVQuals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700823 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000824 if (CVQuals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700825 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000826 if (CVQuals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700827 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000828
829 if (RefQual == FrefQualLValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700830 OB += " &";
Richard Smithc20d1442018-08-20 20:14:49 +0000831 else if (RefQual == FrefQualRValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700832 OB += " &&";
Richard Smithc20d1442018-08-20 20:14:49 +0000833
834 if (ExceptionSpec != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700835 OB += ' ';
836 ExceptionSpec->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000837 }
838 }
839};
840
841class NoexceptSpec : public Node {
842 const Node *E;
843public:
844 NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
845
846 template<typename Fn> void match(Fn F) const { F(E); }
847
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700848 void printLeft(OutputBuffer &OB) const override {
849 OB += "noexcept(";
850 E->print(OB);
851 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000852 }
853};
854
855class DynamicExceptionSpec : public Node {
856 NodeArray Types;
857public:
858 DynamicExceptionSpec(NodeArray Types_)
859 : Node(KDynamicExceptionSpec), Types(Types_) {}
860
861 template<typename Fn> void match(Fn F) const { F(Types); }
862
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700863 void printLeft(OutputBuffer &OB) const override {
864 OB += "throw(";
865 Types.printWithComma(OB);
866 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +0000867 }
868};
869
870class FunctionEncoding final : public Node {
871 const Node *Ret;
872 const Node *Name;
873 NodeArray Params;
874 const Node *Attrs;
875 Qualifiers CVQuals;
876 FunctionRefQual RefQual;
877
878public:
879 FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
880 const Node *Attrs_, Qualifiers CVQuals_,
881 FunctionRefQual RefQual_)
882 : Node(KFunctionEncoding,
883 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
884 /*FunctionCache=*/Cache::Yes),
885 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
886 CVQuals(CVQuals_), RefQual(RefQual_) {}
887
888 template<typename Fn> void match(Fn F) const {
889 F(Ret, Name, Params, Attrs, CVQuals, RefQual);
890 }
891
892 Qualifiers getCVQuals() const { return CVQuals; }
893 FunctionRefQual getRefQual() const { return RefQual; }
894 NodeArray getParams() const { return Params; }
895 const Node *getReturnType() const { return Ret; }
896
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700897 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
898 bool hasFunctionSlow(OutputBuffer &) const override { return true; }
Richard Smithc20d1442018-08-20 20:14:49 +0000899
900 const Node *getName() const { return Name; }
901
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700902 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +0000903 if (Ret) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700904 Ret->printLeft(OB);
905 if (!Ret->hasRHSComponent(OB))
906 OB += " ";
Richard Smithc20d1442018-08-20 20:14:49 +0000907 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700908 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000909 }
910
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700911 void printRight(OutputBuffer &OB) const override {
912 OB += "(";
913 Params.printWithComma(OB);
914 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +0000915 if (Ret)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700916 Ret->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000917
918 if (CVQuals & QualConst)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700919 OB += " const";
Richard Smithc20d1442018-08-20 20:14:49 +0000920 if (CVQuals & QualVolatile)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700921 OB += " volatile";
Richard Smithc20d1442018-08-20 20:14:49 +0000922 if (CVQuals & QualRestrict)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700923 OB += " restrict";
Richard Smithc20d1442018-08-20 20:14:49 +0000924
925 if (RefQual == FrefQualLValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700926 OB += " &";
Richard Smithc20d1442018-08-20 20:14:49 +0000927 else if (RefQual == FrefQualRValue)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700928 OB += " &&";
Richard Smithc20d1442018-08-20 20:14:49 +0000929
930 if (Attrs != nullptr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700931 Attrs->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000932 }
933};
934
935class LiteralOperator : public Node {
936 const Node *OpName;
937
938public:
939 LiteralOperator(const Node *OpName_)
940 : Node(KLiteralOperator), OpName(OpName_) {}
941
942 template<typename Fn> void match(Fn F) const { F(OpName); }
943
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700944 void printLeft(OutputBuffer &OB) const override {
945 OB += "operator\"\" ";
946 OpName->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000947 }
948};
949
950class SpecialName final : public Node {
951 const StringView Special;
952 const Node *Child;
953
954public:
955 SpecialName(StringView Special_, const Node *Child_)
956 : Node(KSpecialName), Special(Special_), Child(Child_) {}
957
958 template<typename Fn> void match(Fn F) const { F(Special, Child); }
959
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700960 void printLeft(OutputBuffer &OB) const override {
961 OB += Special;
962 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000963 }
964};
965
966class CtorVtableSpecialName final : public Node {
967 const Node *FirstType;
968 const Node *SecondType;
969
970public:
971 CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
972 : Node(KCtorVtableSpecialName),
973 FirstType(FirstType_), SecondType(SecondType_) {}
974
975 template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
976
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700977 void printLeft(OutputBuffer &OB) const override {
978 OB += "construction vtable for ";
979 FirstType->print(OB);
980 OB += "-in-";
981 SecondType->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +0000982 }
983};
984
985struct NestedName : Node {
986 Node *Qual;
987 Node *Name;
988
989 NestedName(Node *Qual_, Node *Name_)
990 : Node(KNestedName), Qual(Qual_), Name(Name_) {}
991
992 template<typename Fn> void match(Fn F) const { F(Qual, Name); }
993
994 StringView getBaseName() const override { return Name->getBaseName(); }
995
Luís Ferreiraa4380e22021-10-21 17:31:53 -0700996 void printLeft(OutputBuffer &OB) const override {
997 Qual->print(OB);
998 OB += "::";
999 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001000 }
1001};
1002
1003struct LocalName : Node {
1004 Node *Encoding;
1005 Node *Entity;
1006
1007 LocalName(Node *Encoding_, Node *Entity_)
1008 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
1009
1010 template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
1011
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001012 void printLeft(OutputBuffer &OB) const override {
1013 Encoding->print(OB);
1014 OB += "::";
1015 Entity->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001016 }
1017};
1018
1019class QualifiedName final : public Node {
1020 // qualifier::name
1021 const Node *Qualifier;
1022 const Node *Name;
1023
1024public:
1025 QualifiedName(const Node *Qualifier_, const Node *Name_)
1026 : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
1027
1028 template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
1029
1030 StringView getBaseName() const override { return Name->getBaseName(); }
1031
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001032 void printLeft(OutputBuffer &OB) const override {
1033 Qualifier->print(OB);
1034 OB += "::";
1035 Name->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001036 }
1037};
1038
1039class VectorType final : public Node {
1040 const Node *BaseType;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001041 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001042
1043public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001044 VectorType(const Node *BaseType_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001045 : Node(KVectorType), BaseType(BaseType_),
1046 Dimension(Dimension_) {}
1047
1048 template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
1049
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001050 void printLeft(OutputBuffer &OB) const override {
1051 BaseType->print(OB);
1052 OB += " vector[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001053 if (Dimension)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001054 Dimension->print(OB);
1055 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001056 }
1057};
1058
1059class PixelVectorType final : public Node {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001060 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001061
1062public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001063 PixelVectorType(const Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001064 : Node(KPixelVectorType), Dimension(Dimension_) {}
1065
1066 template<typename Fn> void match(Fn F) const { F(Dimension); }
1067
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001068 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001069 // FIXME: This should demangle as "vector pixel".
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001070 OB += "pixel vector[";
1071 Dimension->print(OB);
1072 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001073 }
1074};
1075
Pengfei Wang50e90b82021-09-23 11:02:25 +08001076class BinaryFPType final : public Node {
1077 const Node *Dimension;
1078
1079public:
1080 BinaryFPType(const Node *Dimension_)
1081 : Node(KBinaryFPType), Dimension(Dimension_) {}
1082
1083 template<typename Fn> void match(Fn F) const { F(Dimension); }
1084
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001085 void printLeft(OutputBuffer &OB) const override {
1086 OB += "_Float";
1087 Dimension->print(OB);
Pengfei Wang50e90b82021-09-23 11:02:25 +08001088 }
1089};
1090
Richard Smithdf1c14c2019-09-06 23:53:21 +00001091enum class TemplateParamKind { Type, NonType, Template };
1092
1093/// An invented name for a template parameter for which we don't have a
1094/// corresponding template argument.
1095///
1096/// This node is created when parsing the <lambda-sig> for a lambda with
1097/// explicit template arguments, which might be referenced in the parameter
1098/// types appearing later in the <lambda-sig>.
1099class SyntheticTemplateParamName final : public Node {
1100 TemplateParamKind Kind;
1101 unsigned Index;
1102
1103public:
1104 SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_)
1105 : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {}
1106
1107 template<typename Fn> void match(Fn F) const { F(Kind, Index); }
1108
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001109 void printLeft(OutputBuffer &OB) const override {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001110 switch (Kind) {
1111 case TemplateParamKind::Type:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001112 OB += "$T";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001113 break;
1114 case TemplateParamKind::NonType:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001115 OB += "$N";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001116 break;
1117 case TemplateParamKind::Template:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001118 OB += "$TT";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001119 break;
1120 }
1121 if (Index > 0)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001122 OB << Index - 1;
Richard Smithdf1c14c2019-09-06 23:53:21 +00001123 }
1124};
1125
1126/// A template type parameter declaration, 'typename T'.
1127class TypeTemplateParamDecl final : public Node {
1128 Node *Name;
1129
1130public:
1131 TypeTemplateParamDecl(Node *Name_)
1132 : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {}
1133
1134 template<typename Fn> void match(Fn F) const { F(Name); }
1135
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001136 void printLeft(OutputBuffer &OB) const override { OB += "typename "; }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001137
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001138 void printRight(OutputBuffer &OB) const override { Name->print(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001139};
1140
1141/// A non-type template parameter declaration, 'int N'.
1142class NonTypeTemplateParamDecl final : public Node {
1143 Node *Name;
1144 Node *Type;
1145
1146public:
1147 NonTypeTemplateParamDecl(Node *Name_, Node *Type_)
1148 : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {}
1149
1150 template<typename Fn> void match(Fn F) const { F(Name, Type); }
1151
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001152 void printLeft(OutputBuffer &OB) const override {
1153 Type->printLeft(OB);
1154 if (!Type->hasRHSComponent(OB))
1155 OB += " ";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001156 }
1157
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001158 void printRight(OutputBuffer &OB) const override {
1159 Name->print(OB);
1160 Type->printRight(OB);
Richard Smithdf1c14c2019-09-06 23:53:21 +00001161 }
1162};
1163
1164/// A template template parameter declaration,
1165/// 'template<typename T> typename N'.
1166class TemplateTemplateParamDecl final : public Node {
1167 Node *Name;
1168 NodeArray Params;
1169
1170public:
1171 TemplateTemplateParamDecl(Node *Name_, NodeArray Params_)
1172 : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_),
1173 Params(Params_) {}
1174
1175 template<typename Fn> void match(Fn F) const { F(Name, Params); }
1176
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001177 void printLeft(OutputBuffer &OB) const override {
1178 OB += "template<";
1179 Params.printWithComma(OB);
1180 OB += "> typename ";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001181 }
1182
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001183 void printRight(OutputBuffer &OB) const override { Name->print(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001184};
1185
1186/// A template parameter pack declaration, 'typename ...T'.
1187class TemplateParamPackDecl final : public Node {
1188 Node *Param;
1189
1190public:
1191 TemplateParamPackDecl(Node *Param_)
1192 : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {}
1193
1194 template<typename Fn> void match(Fn F) const { F(Param); }
1195
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001196 void printLeft(OutputBuffer &OB) const override {
1197 Param->printLeft(OB);
1198 OB += "...";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001199 }
1200
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001201 void printRight(OutputBuffer &OB) const override { Param->printRight(OB); }
Richard Smithdf1c14c2019-09-06 23:53:21 +00001202};
1203
Richard Smithc20d1442018-08-20 20:14:49 +00001204/// An unexpanded parameter pack (either in the expression or type context). If
1205/// this AST is correct, this node will have a ParameterPackExpansion node above
1206/// it.
1207///
1208/// This node is created when some <template-args> are found that apply to an
1209/// <encoding>, and is stored in the TemplateParams table. In order for this to
1210/// appear in the final AST, it has to referenced via a <template-param> (ie,
1211/// T_).
1212class ParameterPack final : public Node {
1213 NodeArray Data;
1214
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08001215 // Setup OutputBuffer for a pack expansion, unless we're already expanding
1216 // one.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001217 void initializePackExpansion(OutputBuffer &OB) const {
1218 if (OB.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
1219 OB.CurrentPackMax = static_cast<unsigned>(Data.size());
1220 OB.CurrentPackIndex = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00001221 }
1222 }
1223
1224public:
1225 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
1226 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
1227 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1228 return P->ArrayCache == Cache::No;
1229 }))
1230 ArrayCache = Cache::No;
1231 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1232 return P->FunctionCache == Cache::No;
1233 }))
1234 FunctionCache = Cache::No;
1235 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1236 return P->RHSComponentCache == Cache::No;
1237 }))
1238 RHSComponentCache = Cache::No;
1239 }
1240
1241 template<typename Fn> void match(Fn F) const { F(Data); }
1242
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001243 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
1244 initializePackExpansion(OB);
1245 size_t Idx = OB.CurrentPackIndex;
1246 return Idx < Data.size() && Data[Idx]->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001247 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001248 bool hasArraySlow(OutputBuffer &OB) const override {
1249 initializePackExpansion(OB);
1250 size_t Idx = OB.CurrentPackIndex;
1251 return Idx < Data.size() && Data[Idx]->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001252 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001253 bool hasFunctionSlow(OutputBuffer &OB) const override {
1254 initializePackExpansion(OB);
1255 size_t Idx = OB.CurrentPackIndex;
1256 return Idx < Data.size() && Data[Idx]->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001257 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001258 const Node *getSyntaxNode(OutputBuffer &OB) const override {
1259 initializePackExpansion(OB);
1260 size_t Idx = OB.CurrentPackIndex;
1261 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(OB) : this;
Richard Smithc20d1442018-08-20 20:14:49 +00001262 }
1263
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001264 void printLeft(OutputBuffer &OB) const override {
1265 initializePackExpansion(OB);
1266 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001267 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001268 Data[Idx]->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001269 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001270 void printRight(OutputBuffer &OB) const override {
1271 initializePackExpansion(OB);
1272 size_t Idx = OB.CurrentPackIndex;
Richard Smithc20d1442018-08-20 20:14:49 +00001273 if (Idx < Data.size())
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001274 Data[Idx]->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001275 }
1276};
1277
1278/// A variadic template argument. This node represents an occurrence of
1279/// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1280/// one of it's Elements is. The parser inserts a ParameterPack into the
1281/// TemplateParams table if the <template-args> this pack belongs to apply to an
1282/// <encoding>.
1283class TemplateArgumentPack final : public Node {
1284 NodeArray Elements;
1285public:
1286 TemplateArgumentPack(NodeArray Elements_)
1287 : Node(KTemplateArgumentPack), Elements(Elements_) {}
1288
1289 template<typename Fn> void match(Fn F) const { F(Elements); }
1290
1291 NodeArray getElements() const { return Elements; }
1292
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001293 void printLeft(OutputBuffer &OB) const override {
1294 Elements.printWithComma(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001295 }
1296};
1297
1298/// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1299/// which each have Child->ParameterPackSize elements.
1300class ParameterPackExpansion final : public Node {
1301 const Node *Child;
1302
1303public:
1304 ParameterPackExpansion(const Node *Child_)
1305 : Node(KParameterPackExpansion), Child(Child_) {}
1306
1307 template<typename Fn> void match(Fn F) const { F(Child); }
1308
1309 const Node *getChild() const { return Child; }
1310
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001311 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001312 constexpr unsigned Max = std::numeric_limits<unsigned>::max();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001313 SwapAndRestore<unsigned> SavePackIdx(OB.CurrentPackIndex, Max);
1314 SwapAndRestore<unsigned> SavePackMax(OB.CurrentPackMax, Max);
1315 size_t StreamPos = OB.getCurrentPosition();
Richard Smithc20d1442018-08-20 20:14:49 +00001316
1317 // Print the first element in the pack. If Child contains a ParameterPack,
1318 // it will set up S.CurrentPackMax and print the first element.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001319 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001320
1321 // No ParameterPack was found in Child. This can occur if we've found a pack
1322 // expansion on a <function-param>.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001323 if (OB.CurrentPackMax == Max) {
1324 OB += "...";
Richard Smithc20d1442018-08-20 20:14:49 +00001325 return;
1326 }
1327
1328 // We found a ParameterPack, but it has no elements. Erase whatever we may
1329 // of printed.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001330 if (OB.CurrentPackMax == 0) {
1331 OB.setCurrentPosition(StreamPos);
Richard Smithc20d1442018-08-20 20:14:49 +00001332 return;
1333 }
1334
1335 // Else, iterate through the rest of the elements in the pack.
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001336 for (unsigned I = 1, E = OB.CurrentPackMax; I < E; ++I) {
1337 OB += ", ";
1338 OB.CurrentPackIndex = I;
1339 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001340 }
1341 }
1342};
1343
1344class TemplateArgs final : public Node {
1345 NodeArray Params;
1346
1347public:
1348 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1349
1350 template<typename Fn> void match(Fn F) const { F(Params); }
1351
1352 NodeArray getParams() { return Params; }
1353
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001354 void printLeft(OutputBuffer &OB) const override {
1355 OB += "<";
1356 Params.printWithComma(OB);
1357 if (OB.back() == '>')
1358 OB += " ";
1359 OB += ">";
Richard Smithc20d1442018-08-20 20:14:49 +00001360 }
1361};
1362
Richard Smithb485b352018-08-24 23:30:26 +00001363/// A forward-reference to a template argument that was not known at the point
1364/// where the template parameter name was parsed in a mangling.
1365///
1366/// This is created when demangling the name of a specialization of a
1367/// conversion function template:
1368///
1369/// \code
1370/// struct A {
1371/// template<typename T> operator T*();
1372/// };
1373/// \endcode
1374///
1375/// When demangling a specialization of the conversion function template, we
1376/// encounter the name of the template (including the \c T) before we reach
1377/// the template argument list, so we cannot substitute the parameter name
1378/// for the corresponding argument while parsing. Instead, we create a
1379/// \c ForwardTemplateReference node that is resolved after we parse the
1380/// template arguments.
Richard Smithc20d1442018-08-20 20:14:49 +00001381struct ForwardTemplateReference : Node {
1382 size_t Index;
1383 Node *Ref = nullptr;
1384
1385 // If we're currently printing this node. It is possible (though invalid) for
1386 // a forward template reference to refer to itself via a substitution. This
1387 // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1388 // out if more than one print* function is active.
1389 mutable bool Printing = false;
1390
1391 ForwardTemplateReference(size_t Index_)
1392 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1393 Cache::Unknown),
1394 Index(Index_) {}
1395
1396 // We don't provide a matcher for these, because the value of the node is
1397 // not determined by its construction parameters, and it generally needs
1398 // special handling.
1399 template<typename Fn> void match(Fn F) const = delete;
1400
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001401 bool hasRHSComponentSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001402 if (Printing)
1403 return false;
1404 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001405 return Ref->hasRHSComponent(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001406 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001407 bool hasArraySlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001408 if (Printing)
1409 return false;
1410 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001411 return Ref->hasArray(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001412 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001413 bool hasFunctionSlow(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001414 if (Printing)
1415 return false;
1416 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001417 return Ref->hasFunction(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001418 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001419 const Node *getSyntaxNode(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001420 if (Printing)
1421 return this;
1422 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001423 return Ref->getSyntaxNode(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001424 }
1425
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001426 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001427 if (Printing)
1428 return;
1429 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001430 Ref->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001431 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001432 void printRight(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001433 if (Printing)
1434 return;
1435 SwapAndRestore<bool> SavePrinting(Printing, true);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001436 Ref->printRight(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001437 }
1438};
1439
1440struct NameWithTemplateArgs : Node {
1441 // name<template_args>
1442 Node *Name;
1443 Node *TemplateArgs;
1444
1445 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1446 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1447
1448 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1449
1450 StringView getBaseName() const override { return Name->getBaseName(); }
1451
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001452 void printLeft(OutputBuffer &OB) const override {
1453 Name->print(OB);
1454 TemplateArgs->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001455 }
1456};
1457
1458class GlobalQualifiedName final : public Node {
1459 Node *Child;
1460
1461public:
1462 GlobalQualifiedName(Node* Child_)
1463 : Node(KGlobalQualifiedName), Child(Child_) {}
1464
1465 template<typename Fn> void match(Fn F) const { F(Child); }
1466
1467 StringView getBaseName() const override { return Child->getBaseName(); }
1468
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001469 void printLeft(OutputBuffer &OB) const override {
1470 OB += "::";
1471 Child->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001472 }
1473};
1474
Richard Smithc20d1442018-08-20 20:14:49 +00001475enum class SpecialSubKind {
1476 allocator,
1477 basic_string,
1478 string,
1479 istream,
1480 ostream,
1481 iostream,
1482};
1483
1484class ExpandedSpecialSubstitution final : public Node {
1485 SpecialSubKind SSK;
1486
1487public:
1488 ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1489 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1490
1491 template<typename Fn> void match(Fn F) const { F(SSK); }
1492
1493 StringView getBaseName() const override {
1494 switch (SSK) {
1495 case SpecialSubKind::allocator:
1496 return StringView("allocator");
1497 case SpecialSubKind::basic_string:
1498 return StringView("basic_string");
1499 case SpecialSubKind::string:
1500 return StringView("basic_string");
1501 case SpecialSubKind::istream:
1502 return StringView("basic_istream");
1503 case SpecialSubKind::ostream:
1504 return StringView("basic_ostream");
1505 case SpecialSubKind::iostream:
1506 return StringView("basic_iostream");
1507 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001508 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001509 }
1510
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001511 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001512 switch (SSK) {
1513 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001514 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001515 break;
1516 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001517 OB += "std::basic_string";
Richard Smithb485b352018-08-24 23:30:26 +00001518 break;
Richard Smithc20d1442018-08-20 20:14:49 +00001519 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001520 OB += "std::basic_string<char, std::char_traits<char>, "
1521 "std::allocator<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001522 break;
1523 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001524 OB += "std::basic_istream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001525 break;
1526 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001527 OB += "std::basic_ostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001528 break;
1529 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001530 OB += "std::basic_iostream<char, std::char_traits<char> >";
Richard Smithc20d1442018-08-20 20:14:49 +00001531 break;
1532 }
1533 }
1534};
1535
1536class SpecialSubstitution final : public Node {
1537public:
1538 SpecialSubKind SSK;
1539
1540 SpecialSubstitution(SpecialSubKind SSK_)
1541 : Node(KSpecialSubstitution), SSK(SSK_) {}
1542
1543 template<typename Fn> void match(Fn F) const { F(SSK); }
1544
1545 StringView getBaseName() const override {
1546 switch (SSK) {
1547 case SpecialSubKind::allocator:
1548 return StringView("allocator");
1549 case SpecialSubKind::basic_string:
1550 return StringView("basic_string");
1551 case SpecialSubKind::string:
1552 return StringView("string");
1553 case SpecialSubKind::istream:
1554 return StringView("istream");
1555 case SpecialSubKind::ostream:
1556 return StringView("ostream");
1557 case SpecialSubKind::iostream:
1558 return StringView("iostream");
1559 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001560 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001561 }
1562
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001563 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001564 switch (SSK) {
1565 case SpecialSubKind::allocator:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001566 OB += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001567 break;
1568 case SpecialSubKind::basic_string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001569 OB += "std::basic_string";
Richard Smithc20d1442018-08-20 20:14:49 +00001570 break;
1571 case SpecialSubKind::string:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001572 OB += "std::string";
Richard Smithc20d1442018-08-20 20:14:49 +00001573 break;
1574 case SpecialSubKind::istream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001575 OB += "std::istream";
Richard Smithc20d1442018-08-20 20:14:49 +00001576 break;
1577 case SpecialSubKind::ostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001578 OB += "std::ostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001579 break;
1580 case SpecialSubKind::iostream:
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001581 OB += "std::iostream";
Richard Smithc20d1442018-08-20 20:14:49 +00001582 break;
1583 }
1584 }
1585};
1586
1587class CtorDtorName final : public Node {
1588 const Node *Basename;
1589 const bool IsDtor;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001590 const int Variant;
Richard Smithc20d1442018-08-20 20:14:49 +00001591
1592public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001593 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1594 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1595 Variant(Variant_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001596
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001597 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
Richard Smithc20d1442018-08-20 20:14:49 +00001598
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001599 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001600 if (IsDtor)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001601 OB += "~";
1602 OB += Basename->getBaseName();
Richard Smithc20d1442018-08-20 20:14:49 +00001603 }
1604};
1605
1606class DtorName : public Node {
1607 const Node *Base;
1608
1609public:
1610 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1611
1612 template<typename Fn> void match(Fn F) const { F(Base); }
1613
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001614 void printLeft(OutputBuffer &OB) const override {
1615 OB += "~";
1616 Base->printLeft(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001617 }
1618};
1619
1620class UnnamedTypeName : public Node {
1621 const StringView Count;
1622
1623public:
1624 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1625
1626 template<typename Fn> void match(Fn F) const { F(Count); }
1627
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001628 void printLeft(OutputBuffer &OB) const override {
1629 OB += "'unnamed";
1630 OB += Count;
1631 OB += "\'";
Richard Smithc20d1442018-08-20 20:14:49 +00001632 }
1633};
1634
1635class ClosureTypeName : public Node {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001636 NodeArray TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00001637 NodeArray Params;
1638 StringView Count;
1639
1640public:
Richard Smithdf1c14c2019-09-06 23:53:21 +00001641 ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_,
1642 StringView Count_)
1643 : Node(KClosureTypeName), TemplateParams(TemplateParams_),
1644 Params(Params_), Count(Count_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001645
Richard Smithdf1c14c2019-09-06 23:53:21 +00001646 template<typename Fn> void match(Fn F) const {
1647 F(TemplateParams, Params, Count);
1648 }
1649
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001650 void printDeclarator(OutputBuffer &OB) const {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001651 if (!TemplateParams.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001652 OB += "<";
1653 TemplateParams.printWithComma(OB);
1654 OB += ">";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001655 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001656 OB += "(";
1657 Params.printWithComma(OB);
1658 OB += ")";
Richard Smithdf1c14c2019-09-06 23:53:21 +00001659 }
Richard Smithc20d1442018-08-20 20:14:49 +00001660
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001661 void printLeft(OutputBuffer &OB) const override {
1662 OB += "\'lambda";
1663 OB += Count;
1664 OB += "\'";
1665 printDeclarator(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001666 }
1667};
1668
1669class StructuredBindingName : public Node {
1670 NodeArray Bindings;
1671public:
1672 StructuredBindingName(NodeArray Bindings_)
1673 : Node(KStructuredBindingName), Bindings(Bindings_) {}
1674
1675 template<typename Fn> void match(Fn F) const { F(Bindings); }
1676
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001677 void printLeft(OutputBuffer &OB) const override {
1678 OB += '[';
1679 Bindings.printWithComma(OB);
1680 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00001681 }
1682};
1683
1684// -- Expression Nodes --
1685
1686class BinaryExpr : public Node {
1687 const Node *LHS;
1688 const StringView InfixOperator;
1689 const Node *RHS;
1690
1691public:
1692 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
1693 : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
1694 }
1695
1696 template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
1697
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001698 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001699 // might be a template argument expression, then we need to disambiguate
1700 // with parens.
1701 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001702 OB += "(";
Richard Smithc20d1442018-08-20 20:14:49 +00001703
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001704 OB += "(";
1705 LHS->print(OB);
1706 OB += ") ";
1707 OB += InfixOperator;
1708 OB += " (";
1709 RHS->print(OB);
1710 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001711
1712 if (InfixOperator == ">")
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001713 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001714 }
1715};
1716
1717class ArraySubscriptExpr : public Node {
1718 const Node *Op1;
1719 const Node *Op2;
1720
1721public:
1722 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
1723 : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
1724
1725 template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
1726
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001727 void printLeft(OutputBuffer &OB) const override {
1728 OB += "(";
1729 Op1->print(OB);
1730 OB += ")[";
1731 Op2->print(OB);
1732 OB += "]";
Richard Smithc20d1442018-08-20 20:14:49 +00001733 }
1734};
1735
1736class PostfixExpr : public Node {
1737 const Node *Child;
1738 const StringView Operator;
1739
1740public:
1741 PostfixExpr(const Node *Child_, StringView Operator_)
1742 : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
1743
1744 template<typename Fn> void match(Fn F) const { F(Child, Operator); }
1745
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001746 void printLeft(OutputBuffer &OB) const override {
1747 OB += "(";
1748 Child->print(OB);
1749 OB += ")";
1750 OB += Operator;
Richard Smithc20d1442018-08-20 20:14:49 +00001751 }
1752};
1753
1754class ConditionalExpr : public Node {
1755 const Node *Cond;
1756 const Node *Then;
1757 const Node *Else;
1758
1759public:
1760 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
1761 : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
1762
1763 template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
1764
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001765 void printLeft(OutputBuffer &OB) const override {
1766 OB += "(";
1767 Cond->print(OB);
1768 OB += ") ? (";
1769 Then->print(OB);
1770 OB += ") : (";
1771 Else->print(OB);
1772 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001773 }
1774};
1775
1776class MemberExpr : public Node {
1777 const Node *LHS;
1778 const StringView Kind;
1779 const Node *RHS;
1780
1781public:
1782 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
1783 : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1784
1785 template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
1786
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001787 void printLeft(OutputBuffer &OB) const override {
1788 LHS->print(OB);
1789 OB += Kind;
Nathan Sidwellc6483042022-01-28 09:27:28 -08001790 // Parenthesize pointer-to-member deference argument.
1791 bool IsPtr = Kind.back() == '*';
1792 if (IsPtr)
1793 OB += '(';
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001794 RHS->print(OB);
Nathan Sidwellc6483042022-01-28 09:27:28 -08001795 if (IsPtr)
1796 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00001797 }
1798};
1799
Richard Smith1865d2f2020-10-22 19:29:36 -07001800class SubobjectExpr : public Node {
1801 const Node *Type;
1802 const Node *SubExpr;
1803 StringView Offset;
1804 NodeArray UnionSelectors;
1805 bool OnePastTheEnd;
1806
1807public:
1808 SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_,
1809 NodeArray UnionSelectors_, bool OnePastTheEnd_)
1810 : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_),
1811 UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {}
1812
1813 template<typename Fn> void match(Fn F) const {
1814 F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd);
1815 }
1816
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001817 void printLeft(OutputBuffer &OB) const override {
1818 SubExpr->print(OB);
1819 OB += ".<";
1820 Type->print(OB);
1821 OB += " at offset ";
Richard Smith1865d2f2020-10-22 19:29:36 -07001822 if (Offset.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001823 OB += "0";
Richard Smith1865d2f2020-10-22 19:29:36 -07001824 } else if (Offset[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001825 OB += "-";
1826 OB += Offset.dropFront();
Richard Smith1865d2f2020-10-22 19:29:36 -07001827 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001828 OB += Offset;
Richard Smith1865d2f2020-10-22 19:29:36 -07001829 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001830 OB += ">";
Richard Smith1865d2f2020-10-22 19:29:36 -07001831 }
1832};
1833
Richard Smithc20d1442018-08-20 20:14:49 +00001834class EnclosingExpr : public Node {
1835 const StringView Prefix;
1836 const Node *Infix;
1837 const StringView Postfix;
1838
1839public:
1840 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1841 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1842 Postfix(Postfix_) {}
1843
1844 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1845
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001846 void printLeft(OutputBuffer &OB) const override {
1847 OB += Prefix;
1848 Infix->print(OB);
1849 OB += Postfix;
Richard Smithc20d1442018-08-20 20:14:49 +00001850 }
1851};
1852
1853class CastExpr : public Node {
1854 // cast_kind<to>(from)
1855 const StringView CastKind;
1856 const Node *To;
1857 const Node *From;
1858
1859public:
1860 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1861 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1862
1863 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1864
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001865 void printLeft(OutputBuffer &OB) const override {
1866 OB += CastKind;
1867 OB += "<";
1868 To->printLeft(OB);
1869 OB += ">(";
1870 From->printLeft(OB);
1871 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001872 }
1873};
1874
1875class SizeofParamPackExpr : public Node {
1876 const Node *Pack;
1877
1878public:
1879 SizeofParamPackExpr(const Node *Pack_)
1880 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1881
1882 template<typename Fn> void match(Fn F) const { F(Pack); }
1883
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001884 void printLeft(OutputBuffer &OB) const override {
1885 OB += "sizeof...(";
Richard Smithc20d1442018-08-20 20:14:49 +00001886 ParameterPackExpansion PPE(Pack);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001887 PPE.printLeft(OB);
1888 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001889 }
1890};
1891
1892class CallExpr : public Node {
1893 const Node *Callee;
1894 NodeArray Args;
1895
1896public:
1897 CallExpr(const Node *Callee_, NodeArray Args_)
1898 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1899
1900 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1901
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001902 void printLeft(OutputBuffer &OB) const override {
1903 Callee->print(OB);
1904 OB += "(";
1905 Args.printWithComma(OB);
1906 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001907 }
1908};
1909
1910class NewExpr : public Node {
1911 // new (expr_list) type(init_list)
1912 NodeArray ExprList;
1913 Node *Type;
1914 NodeArray InitList;
1915 bool IsGlobal; // ::operator new ?
1916 bool IsArray; // new[] ?
1917public:
1918 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1919 bool IsArray_)
1920 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1921 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1922
1923 template<typename Fn> void match(Fn F) const {
1924 F(ExprList, Type, InitList, IsGlobal, IsArray);
1925 }
1926
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001927 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001928 if (IsGlobal)
Nathan Sidwellc69bde22022-01-28 07:09:38 -08001929 OB += "::";
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001930 OB += "new";
Richard Smithc20d1442018-08-20 20:14:49 +00001931 if (IsArray)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001932 OB += "[]";
Richard Smithc20d1442018-08-20 20:14:49 +00001933 if (!ExprList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001934 OB += "(";
1935 ExprList.printWithComma(OB);
1936 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001937 }
Nathan Sidwellc69bde22022-01-28 07:09:38 -08001938 OB += ' ';
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001939 Type->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001940 if (!InitList.empty()) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001941 OB += "(";
1942 InitList.printWithComma(OB);
1943 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001944 }
Richard Smithc20d1442018-08-20 20:14:49 +00001945 }
1946};
1947
1948class DeleteExpr : public Node {
1949 Node *Op;
1950 bool IsGlobal;
1951 bool IsArray;
1952
1953public:
1954 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1955 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1956
1957 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1958
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001959 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00001960 if (IsGlobal)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001961 OB += "::";
1962 OB += "delete";
Richard Smithc20d1442018-08-20 20:14:49 +00001963 if (IsArray)
Nathan Sidwellc69bde22022-01-28 07:09:38 -08001964 OB += "[]";
1965 OB += ' ';
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001966 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00001967 }
1968};
1969
1970class PrefixExpr : public Node {
1971 StringView Prefix;
1972 Node *Child;
1973
1974public:
1975 PrefixExpr(StringView Prefix_, Node *Child_)
1976 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1977
1978 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1979
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001980 void printLeft(OutputBuffer &OB) const override {
1981 OB += Prefix;
1982 OB += "(";
1983 Child->print(OB);
1984 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00001985 }
1986};
1987
1988class FunctionParam : public Node {
1989 StringView Number;
1990
1991public:
1992 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
1993
1994 template<typename Fn> void match(Fn F) const { F(Number); }
1995
Luís Ferreiraa4380e22021-10-21 17:31:53 -07001996 void printLeft(OutputBuffer &OB) const override {
1997 OB += "fp";
1998 OB += Number;
Richard Smithc20d1442018-08-20 20:14:49 +00001999 }
2000};
2001
2002class ConversionExpr : public Node {
2003 const Node *Type;
2004 NodeArray Expressions;
2005
2006public:
2007 ConversionExpr(const Node *Type_, NodeArray Expressions_)
2008 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
2009
2010 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
2011
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002012 void printLeft(OutputBuffer &OB) const override {
2013 OB += "(";
2014 Type->print(OB);
2015 OB += ")(";
2016 Expressions.printWithComma(OB);
2017 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002018 }
2019};
2020
Richard Smith1865d2f2020-10-22 19:29:36 -07002021class PointerToMemberConversionExpr : public Node {
2022 const Node *Type;
2023 const Node *SubExpr;
2024 StringView Offset;
2025
2026public:
2027 PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_,
2028 StringView Offset_)
2029 : Node(KPointerToMemberConversionExpr), Type(Type_), SubExpr(SubExpr_),
2030 Offset(Offset_) {}
2031
2032 template<typename Fn> void match(Fn F) const { F(Type, SubExpr, Offset); }
2033
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002034 void printLeft(OutputBuffer &OB) const override {
2035 OB += "(";
2036 Type->print(OB);
2037 OB += ")(";
2038 SubExpr->print(OB);
2039 OB += ")";
Richard Smith1865d2f2020-10-22 19:29:36 -07002040 }
2041};
2042
Richard Smithc20d1442018-08-20 20:14:49 +00002043class InitListExpr : public Node {
2044 const Node *Ty;
2045 NodeArray Inits;
2046public:
2047 InitListExpr(const Node *Ty_, NodeArray Inits_)
2048 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
2049
2050 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
2051
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002052 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002053 if (Ty)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002054 Ty->print(OB);
2055 OB += '{';
2056 Inits.printWithComma(OB);
2057 OB += '}';
Richard Smithc20d1442018-08-20 20:14:49 +00002058 }
2059};
2060
2061class BracedExpr : public Node {
2062 const Node *Elem;
2063 const Node *Init;
2064 bool IsArray;
2065public:
2066 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
2067 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
2068
2069 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
2070
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002071 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002072 if (IsArray) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002073 OB += '[';
2074 Elem->print(OB);
2075 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002076 } else {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002077 OB += '.';
2078 Elem->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002079 }
2080 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002081 OB += " = ";
2082 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002083 }
2084};
2085
2086class BracedRangeExpr : public Node {
2087 const Node *First;
2088 const Node *Last;
2089 const Node *Init;
2090public:
2091 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
2092 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
2093
2094 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
2095
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002096 void printLeft(OutputBuffer &OB) const override {
2097 OB += '[';
2098 First->print(OB);
2099 OB += " ... ";
2100 Last->print(OB);
2101 OB += ']';
Richard Smithc20d1442018-08-20 20:14:49 +00002102 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002103 OB += " = ";
2104 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002105 }
2106};
2107
2108class FoldExpr : public Node {
2109 const Node *Pack, *Init;
2110 StringView OperatorName;
2111 bool IsLeftFold;
2112
2113public:
2114 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
2115 const Node *Init_)
2116 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
2117 IsLeftFold(IsLeftFold_) {}
2118
2119 template<typename Fn> void match(Fn F) const {
2120 F(IsLeftFold, OperatorName, Pack, Init);
2121 }
2122
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002123 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002124 auto PrintPack = [&] {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002125 OB += '(';
2126 ParameterPackExpansion(Pack).print(OB);
2127 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002128 };
2129
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002130 OB += '(';
Richard Smithc20d1442018-08-20 20:14:49 +00002131
2132 if (IsLeftFold) {
2133 // init op ... op pack
2134 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002135 Init->print(OB);
2136 OB += ' ';
2137 OB += OperatorName;
2138 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002139 }
2140 // ... op pack
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002141 OB += "... ";
2142 OB += OperatorName;
2143 OB += ' ';
Richard Smithc20d1442018-08-20 20:14:49 +00002144 PrintPack();
2145 } else { // !IsLeftFold
2146 // pack op ...
2147 PrintPack();
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002148 OB += ' ';
2149 OB += OperatorName;
2150 OB += " ...";
Richard Smithc20d1442018-08-20 20:14:49 +00002151 // pack op ... op init
2152 if (Init != nullptr) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002153 OB += ' ';
2154 OB += OperatorName;
2155 OB += ' ';
2156 Init->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002157 }
2158 }
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002159 OB += ')';
Richard Smithc20d1442018-08-20 20:14:49 +00002160 }
2161};
2162
2163class ThrowExpr : public Node {
2164 const Node *Op;
2165
2166public:
2167 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
2168
2169 template<typename Fn> void match(Fn F) const { F(Op); }
2170
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002171 void printLeft(OutputBuffer &OB) const override {
2172 OB += "throw ";
2173 Op->print(OB);
Richard Smithc20d1442018-08-20 20:14:49 +00002174 }
2175};
2176
2177class BoolExpr : public Node {
2178 bool Value;
2179
2180public:
2181 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
2182
2183 template<typename Fn> void match(Fn F) const { F(Value); }
2184
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002185 void printLeft(OutputBuffer &OB) const override {
2186 OB += Value ? StringView("true") : StringView("false");
Richard Smithc20d1442018-08-20 20:14:49 +00002187 }
2188};
2189
Richard Smithdf1c14c2019-09-06 23:53:21 +00002190class StringLiteral : public Node {
2191 const Node *Type;
2192
2193public:
2194 StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
2195
2196 template<typename Fn> void match(Fn F) const { F(Type); }
2197
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002198 void printLeft(OutputBuffer &OB) const override {
2199 OB += "\"<";
2200 Type->print(OB);
2201 OB += ">\"";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002202 }
2203};
2204
2205class LambdaExpr : public Node {
2206 const Node *Type;
2207
Richard Smithdf1c14c2019-09-06 23:53:21 +00002208public:
2209 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2210
2211 template<typename Fn> void match(Fn F) const { F(Type); }
2212
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002213 void printLeft(OutputBuffer &OB) const override {
2214 OB += "[]";
Richard Smithfb917462019-09-09 22:26:04 +00002215 if (Type->getKind() == KClosureTypeName)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002216 static_cast<const ClosureTypeName *>(Type)->printDeclarator(OB);
2217 OB += "{...}";
Richard Smithdf1c14c2019-09-06 23:53:21 +00002218 }
2219};
2220
Erik Pilkington0a170f12020-05-13 14:13:37 -04002221class EnumLiteral : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +00002222 // ty(integer)
2223 const Node *Ty;
2224 StringView Integer;
2225
2226public:
Erik Pilkington0a170f12020-05-13 14:13:37 -04002227 EnumLiteral(const Node *Ty_, StringView Integer_)
2228 : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00002229
2230 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2231
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002232 void printLeft(OutputBuffer &OB) const override {
2233 OB << "(";
2234 Ty->print(OB);
2235 OB << ")";
Erik Pilkington0a170f12020-05-13 14:13:37 -04002236
2237 if (Integer[0] == 'n')
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002238 OB << "-" << Integer.dropFront(1);
Erik Pilkington0a170f12020-05-13 14:13:37 -04002239 else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002240 OB << Integer;
Richard Smithc20d1442018-08-20 20:14:49 +00002241 }
2242};
2243
2244class IntegerLiteral : public Node {
2245 StringView Type;
2246 StringView Value;
2247
2248public:
2249 IntegerLiteral(StringView Type_, StringView Value_)
2250 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2251
2252 template<typename Fn> void match(Fn F) const { F(Type, Value); }
2253
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002254 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002255 if (Type.size() > 3) {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002256 OB += "(";
2257 OB += Type;
2258 OB += ")";
Richard Smithc20d1442018-08-20 20:14:49 +00002259 }
2260
2261 if (Value[0] == 'n') {
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002262 OB += "-";
2263 OB += Value.dropFront(1);
Richard Smithc20d1442018-08-20 20:14:49 +00002264 } else
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002265 OB += Value;
Richard Smithc20d1442018-08-20 20:14:49 +00002266
2267 if (Type.size() <= 3)
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002268 OB += Type;
Richard Smithc20d1442018-08-20 20:14:49 +00002269 }
2270};
2271
2272template <class Float> struct FloatData;
2273
2274namespace float_literal_impl {
2275constexpr Node::Kind getFloatLiteralKind(float *) {
2276 return Node::KFloatLiteral;
2277}
2278constexpr Node::Kind getFloatLiteralKind(double *) {
2279 return Node::KDoubleLiteral;
2280}
2281constexpr Node::Kind getFloatLiteralKind(long double *) {
2282 return Node::KLongDoubleLiteral;
2283}
2284}
2285
2286template <class Float> class FloatLiteralImpl : public Node {
2287 const StringView Contents;
2288
2289 static constexpr Kind KindForClass =
2290 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2291
2292public:
2293 FloatLiteralImpl(StringView Contents_)
2294 : Node(KindForClass), Contents(Contents_) {}
2295
2296 template<typename Fn> void match(Fn F) const { F(Contents); }
2297
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002298 void printLeft(OutputBuffer &OB) const override {
Richard Smithc20d1442018-08-20 20:14:49 +00002299 const char *first = Contents.begin();
2300 const char *last = Contents.end() + 1;
2301
2302 const size_t N = FloatData<Float>::mangled_size;
2303 if (static_cast<std::size_t>(last - first) > N) {
2304 last = first + N;
2305 union {
2306 Float value;
2307 char buf[sizeof(Float)];
2308 };
2309 const char *t = first;
2310 char *e = buf;
2311 for (; t != last; ++t, ++e) {
2312 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2313 : static_cast<unsigned>(*t - 'a' + 10);
2314 ++t;
2315 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2316 : static_cast<unsigned>(*t - 'a' + 10);
2317 *e = static_cast<char>((d1 << 4) + d0);
2318 }
2319#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2320 std::reverse(buf, e);
2321#endif
2322 char num[FloatData<Float>::max_demangled_size] = {0};
2323 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
Luís Ferreiraa4380e22021-10-21 17:31:53 -07002324 OB += StringView(num, num + n);
Richard Smithc20d1442018-08-20 20:14:49 +00002325 }
2326 }
2327};
2328
2329using FloatLiteral = FloatLiteralImpl<float>;
2330using DoubleLiteral = FloatLiteralImpl<double>;
2331using LongDoubleLiteral = FloatLiteralImpl<long double>;
2332
2333/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2334/// appropriate derived class.
2335template<typename Fn>
2336void Node::visit(Fn F) const {
2337 switch (K) {
2338#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2339 FOR_EACH_NODE_KIND(CASE)
2340#undef CASE
2341 }
2342 assert(0 && "unknown mangling node kind");
2343}
2344
2345/// Determine the kind of a node from its type.
2346template<typename NodeT> struct NodeKind;
2347#define SPECIALIZATION(X) \
2348 template<> struct NodeKind<X> { \
2349 static constexpr Node::Kind Kind = Node::K##X; \
2350 static constexpr const char *name() { return #X; } \
2351 };
2352FOR_EACH_NODE_KIND(SPECIALIZATION)
2353#undef SPECIALIZATION
2354
2355#undef FOR_EACH_NODE_KIND
2356
Pavel Labathba825192018-10-16 14:29:14 +00002357template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002358 const char *First;
2359 const char *Last;
2360
2361 // Name stack, this is used by the parser to hold temporary names that were
2362 // parsed. The parser collapses multiple names into new nodes to construct
2363 // the AST. Once the parser is finished, names.size() == 1.
2364 PODSmallVector<Node *, 32> Names;
2365
2366 // Substitution table. Itanium supports name substitutions as a means of
2367 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2368 // table.
2369 PODSmallVector<Node *, 32> Subs;
2370
Richard Smithdf1c14c2019-09-06 23:53:21 +00002371 using TemplateParamList = PODSmallVector<Node *, 8>;
2372
2373 class ScopedTemplateParamList {
2374 AbstractManglingParser *Parser;
2375 size_t OldNumTemplateParamLists;
2376 TemplateParamList Params;
2377
2378 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04002379 ScopedTemplateParamList(AbstractManglingParser *TheParser)
2380 : Parser(TheParser),
2381 OldNumTemplateParamLists(TheParser->TemplateParams.size()) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002382 Parser->TemplateParams.push_back(&Params);
2383 }
2384 ~ScopedTemplateParamList() {
2385 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2386 Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2387 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002388 };
2389
Richard Smithc20d1442018-08-20 20:14:49 +00002390 // Template parameter table. Like the above, but referenced like "T42_".
2391 // This has a smaller size compared to Subs and Names because it can be
2392 // stored on the stack.
Richard Smithdf1c14c2019-09-06 23:53:21 +00002393 TemplateParamList OuterTemplateParams;
2394
2395 // Lists of template parameters indexed by template parameter depth,
2396 // referenced like "TL2_4_". If nonempty, element 0 is always
2397 // OuterTemplateParams; inner elements are always template parameter lists of
2398 // lambda expressions. For a generic lambda with no explicit template
2399 // parameter list, the corresponding parameter list pointer will be null.
2400 PODSmallVector<TemplateParamList *, 4> TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00002401
2402 // Set of unresolved forward <template-param> references. These can occur in a
2403 // conversion operator's type, and are resolved in the enclosing <encoding>.
2404 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2405
Richard Smithc20d1442018-08-20 20:14:49 +00002406 bool TryToParseTemplateArgs = true;
2407 bool PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002408 size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2409
2410 unsigned NumSyntheticTemplateParameters[3] = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002411
2412 Alloc ASTAllocator;
2413
Pavel Labathba825192018-10-16 14:29:14 +00002414 AbstractManglingParser(const char *First_, const char *Last_)
2415 : First(First_), Last(Last_) {}
2416
2417 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002418
2419 void reset(const char *First_, const char *Last_) {
2420 First = First_;
2421 Last = Last_;
2422 Names.clear();
2423 Subs.clear();
2424 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002425 ParsingLambdaParamsAtLevel = (size_t)-1;
Richard Smithc20d1442018-08-20 20:14:49 +00002426 TryToParseTemplateArgs = true;
2427 PermitForwardTemplateReferences = false;
Richard Smith9a2307a2019-09-07 00:11:53 +00002428 for (int I = 0; I != 3; ++I)
2429 NumSyntheticTemplateParameters[I] = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00002430 ASTAllocator.reset();
2431 }
2432
Richard Smithb485b352018-08-24 23:30:26 +00002433 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002434 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2435 }
2436
2437 template <class It> NodeArray makeNodeArray(It begin, It end) {
2438 size_t sz = static_cast<size_t>(end - begin);
2439 void *mem = ASTAllocator.allocateNodeArray(sz);
2440 Node **data = new (mem) Node *[sz];
2441 std::copy(begin, end, data);
2442 return NodeArray(data, sz);
2443 }
2444
2445 NodeArray popTrailingNodeArray(size_t FromPosition) {
2446 assert(FromPosition <= Names.size());
2447 NodeArray res =
2448 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2449 Names.dropBack(FromPosition);
2450 return res;
2451 }
2452
2453 bool consumeIf(StringView S) {
2454 if (StringView(First, Last).startsWith(S)) {
2455 First += S.size();
2456 return true;
2457 }
2458 return false;
2459 }
2460
2461 bool consumeIf(char C) {
2462 if (First != Last && *First == C) {
2463 ++First;
2464 return true;
2465 }
2466 return false;
2467 }
2468
2469 char consume() { return First != Last ? *First++ : '\0'; }
2470
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08002471 char look(unsigned Lookahead = 0) const {
Richard Smithc20d1442018-08-20 20:14:49 +00002472 if (static_cast<size_t>(Last - First) <= Lookahead)
2473 return '\0';
2474 return First[Lookahead];
2475 }
2476
2477 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2478
2479 StringView parseNumber(bool AllowNegative = false);
2480 Qualifiers parseCVQualifiers();
2481 bool parsePositiveInteger(size_t *Out);
2482 StringView parseBareSourceName();
2483
2484 bool parseSeqId(size_t *Out);
2485 Node *parseSubstitution();
2486 Node *parseTemplateParam();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002487 Node *parseTemplateParamDecl();
Richard Smithc20d1442018-08-20 20:14:49 +00002488 Node *parseTemplateArgs(bool TagTemplates = false);
2489 Node *parseTemplateArg();
2490
2491 /// Parse the <expr> production.
2492 Node *parseExpr();
2493 Node *parsePrefixExpr(StringView Kind);
2494 Node *parseBinaryExpr(StringView Kind);
2495 Node *parseIntegerLiteral(StringView Lit);
2496 Node *parseExprPrimary();
2497 template <class Float> Node *parseFloatingLiteral();
2498 Node *parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00002499 Node *parseConversionExpr();
2500 Node *parseBracedExpr();
2501 Node *parseFoldExpr();
Richard Smith1865d2f2020-10-22 19:29:36 -07002502 Node *parsePointerToMemberConversionExpr();
2503 Node *parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00002504
2505 /// Parse the <type> production.
2506 Node *parseType();
2507 Node *parseFunctionType();
2508 Node *parseVectorType();
2509 Node *parseDecltype();
2510 Node *parseArrayType();
2511 Node *parsePointerToMemberType();
2512 Node *parseClassEnumType();
2513 Node *parseQualifiedType();
2514
2515 Node *parseEncoding();
2516 bool parseCallOffset();
2517 Node *parseSpecialName();
2518
2519 /// Holds some extra information about a <name> that is being parsed. This
2520 /// information is only pertinent if the <name> refers to an <encoding>.
2521 struct NameState {
2522 bool CtorDtorConversion = false;
2523 bool EndsWithTemplateArgs = false;
2524 Qualifiers CVQualifiers = QualNone;
2525 FunctionRefQual ReferenceQualifier = FrefQualNone;
2526 size_t ForwardTemplateRefsBegin;
2527
Pavel Labathba825192018-10-16 14:29:14 +00002528 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002529 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2530 };
2531
2532 bool resolveForwardTemplateRefs(NameState &State) {
2533 size_t I = State.ForwardTemplateRefsBegin;
2534 size_t E = ForwardTemplateRefs.size();
2535 for (; I < E; ++I) {
2536 size_t Idx = ForwardTemplateRefs[I]->Index;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002537 if (TemplateParams.empty() || !TemplateParams[0] ||
2538 Idx >= TemplateParams[0]->size())
Richard Smithc20d1442018-08-20 20:14:49 +00002539 return true;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002540 ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
Richard Smithc20d1442018-08-20 20:14:49 +00002541 }
2542 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2543 return false;
2544 }
2545
2546 /// Parse the <name> production>
2547 Node *parseName(NameState *State = nullptr);
2548 Node *parseLocalName(NameState *State);
2549 Node *parseOperatorName(NameState *State);
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002550 Node *parseUnqualifiedName(NameState *State, Node *Scope);
Richard Smithc20d1442018-08-20 20:14:49 +00002551 Node *parseUnnamedTypeName(NameState *State);
2552 Node *parseSourceName(NameState *State);
2553 Node *parseUnscopedName(NameState *State);
2554 Node *parseNestedName(NameState *State);
2555 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2556
2557 Node *parseAbiTags(Node *N);
2558
2559 /// Parse the <unresolved-name> production.
Nathan Sidwell77c52e22022-01-28 11:59:03 -08002560 Node *parseUnresolvedName(bool Global);
Richard Smithc20d1442018-08-20 20:14:49 +00002561 Node *parseSimpleId();
2562 Node *parseBaseUnresolvedName();
2563 Node *parseUnresolvedType();
2564 Node *parseDestructorName();
2565
2566 /// Top-level entry point into the parser.
2567 Node *parse();
2568};
2569
2570const char* parse_discriminator(const char* first, const char* last);
2571
2572// <name> ::= <nested-name> // N
2573// ::= <local-name> # See Scope Encoding below // Z
2574// ::= <unscoped-template-name> <template-args>
2575// ::= <unscoped-name>
2576//
2577// <unscoped-template-name> ::= <unscoped-name>
2578// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002579template <typename Derived, typename Alloc>
2580Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002581 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002582 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002583 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002584 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002585
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002586 Node *Result = nullptr;
2587 bool IsSubst = look() == 'S' && look(1) != 't';
2588 if (IsSubst) {
2589 // A substitution must lead to:
2590 // ::= <unscoped-template-name> <template-args>
2591 Result = getDerived().parseSubstitution();
2592 } else {
2593 // An unscoped name can be one of:
2594 // ::= <unscoped-name>
2595 // ::= <unscoped-template-name> <template-args>
2596 Result = getDerived().parseUnscopedName(State);
2597 }
2598 if (Result == nullptr)
2599 return nullptr;
2600
2601 if (look() == 'I') {
2602 // ::= <unscoped-template-name> <template-args>
2603 if (!IsSubst)
2604 // An unscoped-template-name is substitutable.
2605 Subs.push_back(Result);
Pavel Labathba825192018-10-16 14:29:14 +00002606 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002607 if (TA == nullptr)
2608 return nullptr;
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002609 if (State)
2610 State->EndsWithTemplateArgs = true;
2611 Result = make<NameWithTemplateArgs>(Result, TA);
2612 } else if (IsSubst) {
2613 // The substitution case must be followed by <template-args>.
2614 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002615 }
2616
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002617 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00002618}
2619
2620// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2621// := Z <function encoding> E s [<discriminator>]
2622// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002623template <typename Derived, typename Alloc>
2624Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002625 if (!consumeIf('Z'))
2626 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002627 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002628 if (Encoding == nullptr || !consumeIf('E'))
2629 return nullptr;
2630
2631 if (consumeIf('s')) {
2632 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002633 auto *StringLitName = make<NameType>("string literal");
2634 if (!StringLitName)
2635 return nullptr;
2636 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002637 }
2638
2639 if (consumeIf('d')) {
2640 parseNumber(true);
2641 if (!consumeIf('_'))
2642 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002643 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002644 if (N == nullptr)
2645 return nullptr;
2646 return make<LocalName>(Encoding, N);
2647 }
2648
Pavel Labathba825192018-10-16 14:29:14 +00002649 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002650 if (Entity == nullptr)
2651 return nullptr;
2652 First = parse_discriminator(First, Last);
2653 return make<LocalName>(Encoding, Entity);
2654}
2655
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08002656// <unscoped-name> ::= [L]* <unqualified-name>
2657// ::= St [L]* <unqualified-name> # ::std::
2658// [*] extension
Pavel Labathba825192018-10-16 14:29:14 +00002659template <typename Derived, typename Alloc>
2660Node *
2661AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002662 Node *Std = nullptr;
2663 if (consumeIf("St")) {
2664 Std = make<NameType>("std");
2665 if (Std == nullptr)
Nathan Sidwell200e97c2022-01-21 11:37:01 -08002666 return nullptr;
2667 }
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002668 consumeIf('L');
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002669
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002670 return getDerived().parseUnqualifiedName(State, Std);
Richard Smithc20d1442018-08-20 20:14:49 +00002671}
2672
2673// <unqualified-name> ::= <operator-name> [abi-tags]
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002674// ::= <ctor-dtor-name> [<abi-tags>]
2675// ::= <source-name> [<abi-tags>]
2676// ::= <unnamed-type-name> [<abi-tags>]
Richard Smithc20d1442018-08-20 20:14:49 +00002677// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002678template <typename Derived, typename Alloc>
2679Node *
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002680AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State,
2681 Node *Scope) {
Richard Smithc20d1442018-08-20 20:14:49 +00002682 Node *Result;
2683 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002684 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002685 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002686 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002687 else if (consumeIf("DC")) {
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002688 // Structured binding
Richard Smithc20d1442018-08-20 20:14:49 +00002689 size_t BindingsBegin = Names.size();
2690 do {
Pavel Labathba825192018-10-16 14:29:14 +00002691 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002692 if (Binding == nullptr)
2693 return nullptr;
2694 Names.push_back(Binding);
2695 } while (!consumeIf('E'));
2696 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002697 } else if (look() == 'C' || look() == 'D') {
2698 // A <ctor-dtor-name>.
2699 if (Scope == nullptr)
2700 return nullptr;
2701 Result = getDerived().parseCtorDtorName(Scope, State);
2702 } else {
Pavel Labathba825192018-10-16 14:29:14 +00002703 Result = getDerived().parseOperatorName(State);
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002704 }
Richard Smithc20d1442018-08-20 20:14:49 +00002705 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002706 Result = getDerived().parseAbiTags(Result);
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002707 if (Result != nullptr && Scope != nullptr)
2708 Result = make<NestedName>(Scope, Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002709 return Result;
2710}
2711
2712// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2713// ::= <closure-type-name>
2714//
2715// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2716//
2717// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002718template <typename Derived, typename Alloc>
2719Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002720AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2721 // <template-params> refer to the innermost <template-args>. Clear out any
2722 // outer args that we may have inserted into TemplateParams.
2723 if (State != nullptr)
2724 TemplateParams.clear();
2725
Richard Smithc20d1442018-08-20 20:14:49 +00002726 if (consumeIf("Ut")) {
2727 StringView Count = parseNumber();
2728 if (!consumeIf('_'))
2729 return nullptr;
2730 return make<UnnamedTypeName>(Count);
2731 }
2732 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002733 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2734 TemplateParams.size());
2735 ScopedTemplateParamList LambdaTemplateParams(this);
2736
2737 size_t ParamsBegin = Names.size();
2738 while (look() == 'T' &&
2739 StringView("yptn").find(look(1)) != StringView::npos) {
2740 Node *T = parseTemplateParamDecl();
2741 if (!T)
2742 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002743 Names.push_back(T);
2744 }
2745 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2746
2747 // FIXME: If TempParams is empty and none of the function parameters
2748 // includes 'auto', we should remove LambdaTemplateParams from the
2749 // TemplateParams list. Unfortunately, we don't find out whether there are
2750 // any 'auto' parameters until too late in an example such as:
2751 //
2752 // template<typename T> void f(
2753 // decltype([](decltype([]<typename T>(T v) {}),
2754 // auto) {})) {}
2755 // template<typename T> void f(
2756 // decltype([](decltype([]<typename T>(T w) {}),
2757 // int) {})) {}
2758 //
2759 // Here, the type of v is at level 2 but the type of w is at level 1. We
2760 // don't find this out until we encounter the type of the next parameter.
2761 //
2762 // However, compilers can't actually cope with the former example in
2763 // practice, and it's likely to be made ill-formed in future, so we don't
2764 // need to support it here.
2765 //
2766 // If we encounter an 'auto' in the function parameter types, we will
2767 // recreate a template parameter scope for it, but any intervening lambdas
2768 // will be parsed in the 'wrong' template parameter depth.
2769 if (TempParams.empty())
2770 TemplateParams.pop_back();
2771
Richard Smithc20d1442018-08-20 20:14:49 +00002772 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002773 do {
Pavel Labathba825192018-10-16 14:29:14 +00002774 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002775 if (P == nullptr)
2776 return nullptr;
2777 Names.push_back(P);
2778 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002779 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002780 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2781
Richard Smithc20d1442018-08-20 20:14:49 +00002782 StringView Count = parseNumber();
2783 if (!consumeIf('_'))
2784 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002785 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002786 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002787 if (consumeIf("Ub")) {
2788 (void)parseNumber();
2789 if (!consumeIf('_'))
2790 return nullptr;
2791 return make<NameType>("'block-literal'");
2792 }
Richard Smithc20d1442018-08-20 20:14:49 +00002793 return nullptr;
2794}
2795
2796// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002797template <typename Derived, typename Alloc>
2798Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002799 size_t Length = 0;
2800 if (parsePositiveInteger(&Length))
2801 return nullptr;
2802 if (numLeft() < Length || Length == 0)
2803 return nullptr;
2804 StringView Name(First, First + Length);
2805 First += Length;
2806 if (Name.startsWith("_GLOBAL__N"))
2807 return make<NameType>("(anonymous namespace)");
2808 return make<NameType>(Name);
2809}
2810
2811// <operator-name> ::= aa # &&
2812// ::= ad # & (unary)
2813// ::= an # &
2814// ::= aN # &=
2815// ::= aS # =
2816// ::= cl # ()
2817// ::= cm # ,
2818// ::= co # ~
2819// ::= cv <type> # (cast)
2820// ::= da # delete[]
2821// ::= de # * (unary)
2822// ::= dl # delete
2823// ::= dv # /
2824// ::= dV # /=
2825// ::= eo # ^
2826// ::= eO # ^=
2827// ::= eq # ==
2828// ::= ge # >=
2829// ::= gt # >
2830// ::= ix # []
2831// ::= le # <=
2832// ::= li <source-name> # operator ""
2833// ::= ls # <<
2834// ::= lS # <<=
2835// ::= lt # <
2836// ::= mi # -
2837// ::= mI # -=
2838// ::= ml # *
2839// ::= mL # *=
2840// ::= mm # -- (postfix in <expression> context)
2841// ::= na # new[]
2842// ::= ne # !=
2843// ::= ng # - (unary)
2844// ::= nt # !
2845// ::= nw # new
2846// ::= oo # ||
2847// ::= or # |
2848// ::= oR # |=
2849// ::= pm # ->*
2850// ::= pl # +
2851// ::= pL # +=
2852// ::= pp # ++ (postfix in <expression> context)
2853// ::= ps # + (unary)
2854// ::= pt # ->
2855// ::= qu # ?
2856// ::= rm # %
2857// ::= rM # %=
2858// ::= rs # >>
2859// ::= rS # >>=
2860// ::= ss # <=> C++2a
2861// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002862template <typename Derived, typename Alloc>
2863Node *
2864AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002865 switch (look()) {
2866 case 'a':
2867 switch (look(1)) {
2868 case 'a':
2869 First += 2;
2870 return make<NameType>("operator&&");
2871 case 'd':
2872 case 'n':
2873 First += 2;
2874 return make<NameType>("operator&");
2875 case 'N':
2876 First += 2;
2877 return make<NameType>("operator&=");
2878 case 'S':
2879 First += 2;
2880 return make<NameType>("operator=");
2881 }
2882 return nullptr;
2883 case 'c':
2884 switch (look(1)) {
2885 case 'l':
2886 First += 2;
2887 return make<NameType>("operator()");
2888 case 'm':
2889 First += 2;
2890 return make<NameType>("operator,");
2891 case 'o':
2892 First += 2;
2893 return make<NameType>("operator~");
2894 // ::= cv <type> # (cast)
2895 case 'v': {
2896 First += 2;
2897 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2898 // If we're parsing an encoding, State != nullptr and the conversion
2899 // operators' <type> could have a <template-param> that refers to some
2900 // <template-arg>s further ahead in the mangled name.
2901 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2902 PermitForwardTemplateReferences ||
2903 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002904 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002905 if (Ty == nullptr)
2906 return nullptr;
2907 if (State) State->CtorDtorConversion = true;
2908 return make<ConversionOperatorType>(Ty);
2909 }
2910 }
2911 return nullptr;
2912 case 'd':
2913 switch (look(1)) {
2914 case 'a':
2915 First += 2;
2916 return make<NameType>("operator delete[]");
2917 case 'e':
2918 First += 2;
2919 return make<NameType>("operator*");
2920 case 'l':
2921 First += 2;
2922 return make<NameType>("operator delete");
2923 case 'v':
2924 First += 2;
2925 return make<NameType>("operator/");
2926 case 'V':
2927 First += 2;
2928 return make<NameType>("operator/=");
2929 }
2930 return nullptr;
2931 case 'e':
2932 switch (look(1)) {
2933 case 'o':
2934 First += 2;
2935 return make<NameType>("operator^");
2936 case 'O':
2937 First += 2;
2938 return make<NameType>("operator^=");
2939 case 'q':
2940 First += 2;
2941 return make<NameType>("operator==");
2942 }
2943 return nullptr;
2944 case 'g':
2945 switch (look(1)) {
2946 case 'e':
2947 First += 2;
2948 return make<NameType>("operator>=");
2949 case 't':
2950 First += 2;
2951 return make<NameType>("operator>");
2952 }
2953 return nullptr;
2954 case 'i':
2955 if (look(1) == 'x') {
2956 First += 2;
2957 return make<NameType>("operator[]");
2958 }
2959 return nullptr;
2960 case 'l':
2961 switch (look(1)) {
2962 case 'e':
2963 First += 2;
2964 return make<NameType>("operator<=");
2965 // ::= li <source-name> # operator ""
2966 case 'i': {
2967 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002968 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002969 if (SN == nullptr)
2970 return nullptr;
2971 return make<LiteralOperator>(SN);
2972 }
2973 case 's':
2974 First += 2;
2975 return make<NameType>("operator<<");
2976 case 'S':
2977 First += 2;
2978 return make<NameType>("operator<<=");
2979 case 't':
2980 First += 2;
2981 return make<NameType>("operator<");
2982 }
2983 return nullptr;
2984 case 'm':
2985 switch (look(1)) {
2986 case 'i':
2987 First += 2;
2988 return make<NameType>("operator-");
2989 case 'I':
2990 First += 2;
2991 return make<NameType>("operator-=");
2992 case 'l':
2993 First += 2;
2994 return make<NameType>("operator*");
2995 case 'L':
2996 First += 2;
2997 return make<NameType>("operator*=");
2998 case 'm':
2999 First += 2;
3000 return make<NameType>("operator--");
3001 }
3002 return nullptr;
3003 case 'n':
3004 switch (look(1)) {
3005 case 'a':
3006 First += 2;
3007 return make<NameType>("operator new[]");
3008 case 'e':
3009 First += 2;
3010 return make<NameType>("operator!=");
3011 case 'g':
3012 First += 2;
3013 return make<NameType>("operator-");
3014 case 't':
3015 First += 2;
3016 return make<NameType>("operator!");
3017 case 'w':
3018 First += 2;
3019 return make<NameType>("operator new");
3020 }
3021 return nullptr;
3022 case 'o':
3023 switch (look(1)) {
3024 case 'o':
3025 First += 2;
3026 return make<NameType>("operator||");
3027 case 'r':
3028 First += 2;
3029 return make<NameType>("operator|");
3030 case 'R':
3031 First += 2;
3032 return make<NameType>("operator|=");
3033 }
3034 return nullptr;
3035 case 'p':
3036 switch (look(1)) {
3037 case 'm':
3038 First += 2;
3039 return make<NameType>("operator->*");
3040 case 'l':
3041 First += 2;
3042 return make<NameType>("operator+");
3043 case 'L':
3044 First += 2;
3045 return make<NameType>("operator+=");
3046 case 'p':
3047 First += 2;
3048 return make<NameType>("operator++");
3049 case 's':
3050 First += 2;
3051 return make<NameType>("operator+");
3052 case 't':
3053 First += 2;
3054 return make<NameType>("operator->");
3055 }
3056 return nullptr;
3057 case 'q':
3058 if (look(1) == 'u') {
3059 First += 2;
3060 return make<NameType>("operator?");
3061 }
3062 return nullptr;
3063 case 'r':
3064 switch (look(1)) {
3065 case 'm':
3066 First += 2;
3067 return make<NameType>("operator%");
3068 case 'M':
3069 First += 2;
3070 return make<NameType>("operator%=");
3071 case 's':
3072 First += 2;
3073 return make<NameType>("operator>>");
3074 case 'S':
3075 First += 2;
3076 return make<NameType>("operator>>=");
3077 }
3078 return nullptr;
3079 case 's':
3080 if (look(1) == 's') {
3081 First += 2;
3082 return make<NameType>("operator<=>");
3083 }
3084 return nullptr;
3085 // ::= v <digit> <source-name> # vendor extended operator
3086 case 'v':
3087 if (std::isdigit(look(1))) {
3088 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003089 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003090 if (SN == nullptr)
3091 return nullptr;
3092 return make<ConversionOperatorType>(SN);
3093 }
3094 return nullptr;
3095 }
3096 return nullptr;
3097}
3098
3099// <ctor-dtor-name> ::= C1 # complete object constructor
3100// ::= C2 # base object constructor
3101// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003102// extension ::= C4 # gcc old-style "[unified]" constructor
3103// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003104// ::= D0 # deleting destructor
3105// ::= D1 # complete object destructor
3106// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003107// extension ::= D4 # gcc old-style "[unified]" destructor
3108// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003109template <typename Derived, typename Alloc>
3110Node *
3111AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3112 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003113 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3114 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3115 switch (SSK) {
3116 case SpecialSubKind::string:
3117 case SpecialSubKind::istream:
3118 case SpecialSubKind::ostream:
3119 case SpecialSubKind::iostream:
3120 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003121 if (!SoFar)
3122 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003123 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003124 default:
3125 break;
3126 }
3127 }
3128
3129 if (consumeIf('C')) {
3130 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003131 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3132 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003133 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003134 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003135 ++First;
3136 if (State) State->CtorDtorConversion = true;
3137 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003138 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003139 return nullptr;
3140 }
Nico Weber29294792019-04-03 23:14:33 +00003141 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003142 }
3143
Nico Weber29294792019-04-03 23:14:33 +00003144 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3145 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003146 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003147 First += 2;
3148 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003149 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003150 }
3151
3152 return nullptr;
3153}
3154
3155// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3156// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3157//
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003158// <prefix> ::= <prefix> [L]* <unqualified-name>
Richard Smithc20d1442018-08-20 20:14:49 +00003159// ::= <template-prefix> <template-args>
3160// ::= <template-param>
3161// ::= <decltype>
3162// ::= # empty
3163// ::= <substitution>
3164// ::= <prefix> <data-member-prefix>
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003165// [*] extension
Richard Smithc20d1442018-08-20 20:14:49 +00003166//
3167// <data-member-prefix> := <member source-name> [<template-args>] M
3168//
3169// <template-prefix> ::= <prefix> <template unqualified-name>
3170// ::= <template-param>
3171// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003172template <typename Derived, typename Alloc>
3173Node *
3174AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003175 if (!consumeIf('N'))
3176 return nullptr;
3177
3178 Qualifiers CVTmp = parseCVQualifiers();
3179 if (State) State->CVQualifiers = CVTmp;
3180
3181 if (consumeIf('O')) {
3182 if (State) State->ReferenceQualifier = FrefQualRValue;
3183 } else if (consumeIf('R')) {
3184 if (State) State->ReferenceQualifier = FrefQualLValue;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003185 } else {
Richard Smithc20d1442018-08-20 20:14:49 +00003186 if (State) State->ReferenceQualifier = FrefQualNone;
Richard Smithb485b352018-08-24 23:30:26 +00003187 }
Richard Smithc20d1442018-08-20 20:14:49 +00003188
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003189 Node *SoFar = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003190 while (!consumeIf('E')) {
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003191 if (State)
3192 // Only set end-with-template on the case that does that.
3193 State->EndsWithTemplateArgs = false;
3194
Richard Smithc20d1442018-08-20 20:14:49 +00003195 if (look() == 'T') {
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003196 // ::= <template-param>
3197 if (SoFar != nullptr)
3198 return nullptr; // Cannot have a prefix.
3199 SoFar = getDerived().parseTemplateParam();
3200 } else if (look() == 'I') {
3201 // ::= <template-prefix> <template-args>
3202 if (SoFar == nullptr)
3203 return nullptr; // Must have a prefix.
Pavel Labathba825192018-10-16 14:29:14 +00003204 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003205 if (TA == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003206 return nullptr;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003207 if (SoFar->getKind() == Node::KNameWithTemplateArgs)
3208 // Semantically <template-args> <template-args> cannot be generated by a
3209 // C++ entity. There will always be [something like] a name between
3210 // them.
3211 return nullptr;
3212 if (State)
3213 State->EndsWithTemplateArgs = true;
Richard Smithc20d1442018-08-20 20:14:49 +00003214 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003215 } else if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
3216 // ::= <decltype>
3217 if (SoFar != nullptr)
3218 return nullptr; // Cannot have a prefix.
3219 SoFar = getDerived().parseDecltype();
3220 } else if (look() == 'S') {
3221 // ::= <substitution>
3222 if (SoFar != nullptr)
3223 return nullptr; // Cannot have a prefix.
3224 if (look(1) == 't') {
3225 // parseSubstition does not handle 'St'.
3226 First += 2;
3227 SoFar = make<NameType>("std");
3228 } else {
3229 SoFar = getDerived().parseSubstitution();
3230 }
Richard Smithc20d1442018-08-20 20:14:49 +00003231 if (SoFar == nullptr)
3232 return nullptr;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003233 continue; // Do not push a new substitution.
3234 } else {
Nathan Sidwelle6545292022-01-25 12:31:01 -08003235 consumeIf('L'); // extension
Nathan Sidwell9a29c972022-01-25 12:23:31 -08003236 // ::= [<prefix>] <unqualified-name>
3237 SoFar = getDerived().parseUnqualifiedName(State, SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00003238 }
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003239 if (SoFar == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003240 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003241 Subs.push_back(SoFar);
Nathan Sidwelle6545292022-01-25 12:31:01 -08003242
3243 // No longer used.
3244 // <data-member-prefix> := <member source-name> [<template-args>] M
3245 consumeIf('M');
Richard Smithc20d1442018-08-20 20:14:49 +00003246 }
3247
3248 if (SoFar == nullptr || Subs.empty())
3249 return nullptr;
3250
3251 Subs.pop_back();
3252 return SoFar;
3253}
3254
3255// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003256template <typename Derived, typename Alloc>
3257Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3258 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003259 if (SN == nullptr)
3260 return nullptr;
3261 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003262 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003263 if (TA == nullptr)
3264 return nullptr;
3265 return make<NameWithTemplateArgs>(SN, TA);
3266 }
3267 return SN;
3268}
3269
3270// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3271// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003272template <typename Derived, typename Alloc>
3273Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003274 Node *Result;
3275 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003276 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003277 else
Pavel Labathba825192018-10-16 14:29:14 +00003278 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003279 if (Result == nullptr)
3280 return nullptr;
3281 return make<DtorName>(Result);
3282}
3283
3284// <unresolved-type> ::= <template-param>
3285// ::= <decltype>
3286// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003287template <typename Derived, typename Alloc>
3288Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003289 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003290 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003291 if (TP == nullptr)
3292 return nullptr;
3293 Subs.push_back(TP);
3294 return TP;
3295 }
3296 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003297 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003298 if (DT == nullptr)
3299 return nullptr;
3300 Subs.push_back(DT);
3301 return DT;
3302 }
Pavel Labathba825192018-10-16 14:29:14 +00003303 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003304}
3305
3306// <base-unresolved-name> ::= <simple-id> # unresolved name
3307// extension ::= <operator-name> # unresolved operator-function-id
3308// extension ::= <operator-name> <template-args> # unresolved operator template-id
3309// ::= on <operator-name> # unresolved operator-function-id
3310// ::= on <operator-name> <template-args> # unresolved operator template-id
3311// ::= dn <destructor-name> # destructor or pseudo-destructor;
3312// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003313template <typename Derived, typename Alloc>
3314Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003315 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003316 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003317
3318 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003319 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003320
3321 consumeIf("on");
3322
Pavel Labathba825192018-10-16 14:29:14 +00003323 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003324 if (Oper == nullptr)
3325 return nullptr;
3326 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003327 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003328 if (TA == nullptr)
3329 return nullptr;
3330 return make<NameWithTemplateArgs>(Oper, TA);
3331 }
3332 return Oper;
3333}
3334
3335// <unresolved-name>
3336// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3337// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3338// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3339// # A::x, N::y, A<T>::z; "gs" means leading "::"
Nathan Sidwell77c52e22022-01-28 11:59:03 -08003340// [gs] has been parsed by caller.
Richard Smithc20d1442018-08-20 20:14:49 +00003341// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3342// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3343// # T::N::x /decltype(p)::N::x
3344// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3345//
3346// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003347template <typename Derived, typename Alloc>
Nathan Sidwell77c52e22022-01-28 11:59:03 -08003348Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName(bool Global) {
Richard Smithc20d1442018-08-20 20:14:49 +00003349 Node *SoFar = nullptr;
3350
3351 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3352 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3353 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003354 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003355 if (SoFar == nullptr)
3356 return nullptr;
3357
3358 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003359 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003360 if (TA == nullptr)
3361 return nullptr;
3362 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003363 if (!SoFar)
3364 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003365 }
3366
3367 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003368 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003369 if (Qual == nullptr)
3370 return nullptr;
3371 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003372 if (!SoFar)
3373 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003374 }
3375
Pavel Labathba825192018-10-16 14:29:14 +00003376 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003377 if (Base == nullptr)
3378 return nullptr;
3379 return make<QualifiedName>(SoFar, Base);
3380 }
3381
Richard Smithc20d1442018-08-20 20:14:49 +00003382 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3383 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003384 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003385 if (SoFar == nullptr)
3386 return nullptr;
3387 if (Global)
3388 SoFar = make<GlobalQualifiedName>(SoFar);
3389 return SoFar;
3390 }
3391
3392 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3393 if (std::isdigit(look())) {
3394 do {
Pavel Labathba825192018-10-16 14:29:14 +00003395 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003396 if (Qual == nullptr)
3397 return nullptr;
3398 if (SoFar)
3399 SoFar = make<QualifiedName>(SoFar, Qual);
3400 else if (Global)
3401 SoFar = make<GlobalQualifiedName>(Qual);
3402 else
3403 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003404 if (!SoFar)
3405 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003406 } while (!consumeIf('E'));
3407 }
3408 // sr <unresolved-type> <base-unresolved-name>
3409 // sr <unresolved-type> <template-args> <base-unresolved-name>
3410 else {
Pavel Labathba825192018-10-16 14:29:14 +00003411 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003412 if (SoFar == nullptr)
3413 return nullptr;
3414
3415 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003416 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003417 if (TA == nullptr)
3418 return nullptr;
3419 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003420 if (!SoFar)
3421 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003422 }
3423 }
3424
3425 assert(SoFar != nullptr);
3426
Pavel Labathba825192018-10-16 14:29:14 +00003427 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003428 if (Base == nullptr)
3429 return nullptr;
3430 return make<QualifiedName>(SoFar, Base);
3431}
3432
3433// <abi-tags> ::= <abi-tag> [<abi-tags>]
3434// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003435template <typename Derived, typename Alloc>
3436Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003437 while (consumeIf('B')) {
3438 StringView SN = parseBareSourceName();
3439 if (SN.empty())
3440 return nullptr;
3441 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003442 if (!N)
3443 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003444 }
3445 return N;
3446}
3447
3448// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003449template <typename Alloc, typename Derived>
3450StringView
3451AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003452 const char *Tmp = First;
3453 if (AllowNegative)
3454 consumeIf('n');
3455 if (numLeft() == 0 || !std::isdigit(*First))
3456 return StringView();
3457 while (numLeft() != 0 && std::isdigit(*First))
3458 ++First;
3459 return StringView(Tmp, First);
3460}
3461
3462// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003463template <typename Alloc, typename Derived>
3464bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003465 *Out = 0;
3466 if (look() < '0' || look() > '9')
3467 return true;
3468 while (look() >= '0' && look() <= '9') {
3469 *Out *= 10;
3470 *Out += static_cast<size_t>(consume() - '0');
3471 }
3472 return false;
3473}
3474
Pavel Labathba825192018-10-16 14:29:14 +00003475template <typename Alloc, typename Derived>
3476StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003477 size_t Int = 0;
3478 if (parsePositiveInteger(&Int) || numLeft() < Int)
3479 return StringView();
3480 StringView R(First, First + Int);
3481 First += Int;
3482 return R;
3483}
3484
3485// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3486//
3487// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3488// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3489// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3490//
3491// <ref-qualifier> ::= R # & ref-qualifier
3492// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003493template <typename Derived, typename Alloc>
3494Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003495 Qualifiers CVQuals = parseCVQualifiers();
3496
3497 Node *ExceptionSpec = nullptr;
3498 if (consumeIf("Do")) {
3499 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003500 if (!ExceptionSpec)
3501 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003502 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003503 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003504 if (E == nullptr || !consumeIf('E'))
3505 return nullptr;
3506 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003507 if (!ExceptionSpec)
3508 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003509 } else if (consumeIf("Dw")) {
3510 size_t SpecsBegin = Names.size();
3511 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003512 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003513 if (T == nullptr)
3514 return nullptr;
3515 Names.push_back(T);
3516 }
3517 ExceptionSpec =
3518 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003519 if (!ExceptionSpec)
3520 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003521 }
3522
3523 consumeIf("Dx"); // transaction safe
3524
3525 if (!consumeIf('F'))
3526 return nullptr;
3527 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003528 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003529 if (ReturnType == nullptr)
3530 return nullptr;
3531
3532 FunctionRefQual ReferenceQualifier = FrefQualNone;
3533 size_t ParamsBegin = Names.size();
3534 while (true) {
3535 if (consumeIf('E'))
3536 break;
3537 if (consumeIf('v'))
3538 continue;
3539 if (consumeIf("RE")) {
3540 ReferenceQualifier = FrefQualLValue;
3541 break;
3542 }
3543 if (consumeIf("OE")) {
3544 ReferenceQualifier = FrefQualRValue;
3545 break;
3546 }
Pavel Labathba825192018-10-16 14:29:14 +00003547 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003548 if (T == nullptr)
3549 return nullptr;
3550 Names.push_back(T);
3551 }
3552
3553 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3554 return make<FunctionType>(ReturnType, Params, CVQuals,
3555 ReferenceQualifier, ExceptionSpec);
3556}
3557
3558// extension:
3559// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3560// ::= Dv [<dimension expression>] _ <element type>
3561// <extended element type> ::= <element type>
3562// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003563template <typename Derived, typename Alloc>
3564Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003565 if (!consumeIf("Dv"))
3566 return nullptr;
3567 if (look() >= '1' && look() <= '9') {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003568 Node *DimensionNumber = make<NameType>(parseNumber());
3569 if (!DimensionNumber)
3570 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003571 if (!consumeIf('_'))
3572 return nullptr;
3573 if (consumeIf('p'))
3574 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003575 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003576 if (ElemType == nullptr)
3577 return nullptr;
3578 return make<VectorType>(ElemType, DimensionNumber);
3579 }
3580
3581 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003582 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003583 if (!DimExpr)
3584 return nullptr;
3585 if (!consumeIf('_'))
3586 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003587 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003588 if (!ElemType)
3589 return nullptr;
3590 return make<VectorType>(ElemType, DimExpr);
3591 }
Pavel Labathba825192018-10-16 14:29:14 +00003592 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003593 if (!ElemType)
3594 return nullptr;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003595 return make<VectorType>(ElemType, /*Dimension=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003596}
3597
3598// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3599// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003600template <typename Derived, typename Alloc>
3601Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003602 if (!consumeIf('D'))
3603 return nullptr;
3604 if (!consumeIf('t') && !consumeIf('T'))
3605 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003606 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003607 if (E == nullptr)
3608 return nullptr;
3609 if (!consumeIf('E'))
3610 return nullptr;
3611 return make<EnclosingExpr>("decltype(", E, ")");
3612}
3613
3614// <array-type> ::= A <positive dimension number> _ <element type>
3615// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003616template <typename Derived, typename Alloc>
3617Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003618 if (!consumeIf('A'))
3619 return nullptr;
3620
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003621 Node *Dimension = nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003622
Richard Smithc20d1442018-08-20 20:14:49 +00003623 if (std::isdigit(look())) {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003624 Dimension = make<NameType>(parseNumber());
3625 if (!Dimension)
3626 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003627 if (!consumeIf('_'))
3628 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003629 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003630 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003631 if (DimExpr == nullptr)
3632 return nullptr;
3633 if (!consumeIf('_'))
3634 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003635 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003636 }
3637
Pavel Labathba825192018-10-16 14:29:14 +00003638 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003639 if (Ty == nullptr)
3640 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003641 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003642}
3643
3644// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003645template <typename Derived, typename Alloc>
3646Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003647 if (!consumeIf('M'))
3648 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003649 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003650 if (ClassType == nullptr)
3651 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003652 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003653 if (MemberType == nullptr)
3654 return nullptr;
3655 return make<PointerToMemberType>(ClassType, MemberType);
3656}
3657
3658// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3659// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3660// ::= Tu <name> # dependent elaborated type specifier using 'union'
3661// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003662template <typename Derived, typename Alloc>
3663Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003664 StringView ElabSpef;
3665 if (consumeIf("Ts"))
3666 ElabSpef = "struct";
3667 else if (consumeIf("Tu"))
3668 ElabSpef = "union";
3669 else if (consumeIf("Te"))
3670 ElabSpef = "enum";
3671
Pavel Labathba825192018-10-16 14:29:14 +00003672 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003673 if (Name == nullptr)
3674 return nullptr;
3675
3676 if (!ElabSpef.empty())
3677 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3678
3679 return Name;
3680}
3681
3682// <qualified-type> ::= <qualifiers> <type>
3683// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3684// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003685template <typename Derived, typename Alloc>
3686Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003687 if (consumeIf('U')) {
3688 StringView Qual = parseBareSourceName();
3689 if (Qual.empty())
3690 return nullptr;
3691
Richard Smithc20d1442018-08-20 20:14:49 +00003692 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3693 if (Qual.startsWith("objcproto")) {
3694 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3695 StringView Proto;
3696 {
3697 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3698 SaveLast(Last, ProtoSourceName.end());
3699 Proto = parseBareSourceName();
3700 }
3701 if (Proto.empty())
3702 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003703 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003704 if (Child == nullptr)
3705 return nullptr;
3706 return make<ObjCProtoName>(Child, Proto);
3707 }
3708
Alex Orlovf50df922021-03-24 10:21:32 +04003709 Node *TA = nullptr;
3710 if (look() == 'I') {
3711 TA = getDerived().parseTemplateArgs();
3712 if (TA == nullptr)
3713 return nullptr;
3714 }
3715
Pavel Labathba825192018-10-16 14:29:14 +00003716 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003717 if (Child == nullptr)
3718 return nullptr;
Alex Orlovf50df922021-03-24 10:21:32 +04003719 return make<VendorExtQualType>(Child, Qual, TA);
Richard Smithc20d1442018-08-20 20:14:49 +00003720 }
3721
3722 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003723 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003724 if (Ty == nullptr)
3725 return nullptr;
3726 if (Quals != QualNone)
3727 Ty = make<QualType>(Ty, Quals);
3728 return Ty;
3729}
3730
3731// <type> ::= <builtin-type>
3732// ::= <qualified-type>
3733// ::= <function-type>
3734// ::= <class-enum-type>
3735// ::= <array-type>
3736// ::= <pointer-to-member-type>
3737// ::= <template-param>
3738// ::= <template-template-param> <template-args>
3739// ::= <decltype>
3740// ::= P <type> # pointer
3741// ::= R <type> # l-value reference
3742// ::= O <type> # r-value reference (C++11)
3743// ::= C <type> # complex pair (C99)
3744// ::= G <type> # imaginary (C99)
3745// ::= <substitution> # See Compression below
3746// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3747// extension ::= <vector-type> # <vector-type> starts with Dv
3748//
3749// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3750// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003751template <typename Derived, typename Alloc>
3752Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003753 Node *Result = nullptr;
3754
Richard Smithc20d1442018-08-20 20:14:49 +00003755 switch (look()) {
3756 // ::= <qualified-type>
3757 case 'r':
3758 case 'V':
3759 case 'K': {
3760 unsigned AfterQuals = 0;
3761 if (look(AfterQuals) == 'r') ++AfterQuals;
3762 if (look(AfterQuals) == 'V') ++AfterQuals;
3763 if (look(AfterQuals) == 'K') ++AfterQuals;
3764
3765 if (look(AfterQuals) == 'F' ||
3766 (look(AfterQuals) == 'D' &&
3767 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3768 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003769 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003770 break;
3771 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003772 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003773 }
3774 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003775 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003776 break;
3777 }
3778 // <builtin-type> ::= v # void
3779 case 'v':
3780 ++First;
3781 return make<NameType>("void");
3782 // ::= w # wchar_t
3783 case 'w':
3784 ++First;
3785 return make<NameType>("wchar_t");
3786 // ::= b # bool
3787 case 'b':
3788 ++First;
3789 return make<NameType>("bool");
3790 // ::= c # char
3791 case 'c':
3792 ++First;
3793 return make<NameType>("char");
3794 // ::= a # signed char
3795 case 'a':
3796 ++First;
3797 return make<NameType>("signed char");
3798 // ::= h # unsigned char
3799 case 'h':
3800 ++First;
3801 return make<NameType>("unsigned char");
3802 // ::= s # short
3803 case 's':
3804 ++First;
3805 return make<NameType>("short");
3806 // ::= t # unsigned short
3807 case 't':
3808 ++First;
3809 return make<NameType>("unsigned short");
3810 // ::= i # int
3811 case 'i':
3812 ++First;
3813 return make<NameType>("int");
3814 // ::= j # unsigned int
3815 case 'j':
3816 ++First;
3817 return make<NameType>("unsigned int");
3818 // ::= l # long
3819 case 'l':
3820 ++First;
3821 return make<NameType>("long");
3822 // ::= m # unsigned long
3823 case 'm':
3824 ++First;
3825 return make<NameType>("unsigned long");
3826 // ::= x # long long, __int64
3827 case 'x':
3828 ++First;
3829 return make<NameType>("long long");
3830 // ::= y # unsigned long long, __int64
3831 case 'y':
3832 ++First;
3833 return make<NameType>("unsigned long long");
3834 // ::= n # __int128
3835 case 'n':
3836 ++First;
3837 return make<NameType>("__int128");
3838 // ::= o # unsigned __int128
3839 case 'o':
3840 ++First;
3841 return make<NameType>("unsigned __int128");
3842 // ::= f # float
3843 case 'f':
3844 ++First;
3845 return make<NameType>("float");
3846 // ::= d # double
3847 case 'd':
3848 ++First;
3849 return make<NameType>("double");
3850 // ::= e # long double, __float80
3851 case 'e':
3852 ++First;
3853 return make<NameType>("long double");
3854 // ::= g # __float128
3855 case 'g':
3856 ++First;
3857 return make<NameType>("__float128");
3858 // ::= z # ellipsis
3859 case 'z':
3860 ++First;
3861 return make<NameType>("...");
3862
3863 // <builtin-type> ::= u <source-name> # vendor extended type
3864 case 'u': {
3865 ++First;
3866 StringView Res = parseBareSourceName();
3867 if (Res.empty())
3868 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003869 // Typically, <builtin-type>s are not considered substitution candidates,
3870 // but the exception to that exception is vendor extended types (Itanium C++
3871 // ABI 5.9.1).
3872 Result = make<NameType>(Res);
3873 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003874 }
3875 case 'D':
3876 switch (look(1)) {
3877 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3878 case 'd':
3879 First += 2;
3880 return make<NameType>("decimal64");
3881 // ::= De # IEEE 754r decimal floating point (128 bits)
3882 case 'e':
3883 First += 2;
3884 return make<NameType>("decimal128");
3885 // ::= Df # IEEE 754r decimal floating point (32 bits)
3886 case 'f':
3887 First += 2;
3888 return make<NameType>("decimal32");
3889 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3890 case 'h':
3891 First += 2;
Stuart Bradye8bf5772021-06-07 16:30:22 +01003892 return make<NameType>("half");
Pengfei Wang50e90b82021-09-23 11:02:25 +08003893 // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits)
3894 case 'F': {
3895 First += 2;
3896 Node *DimensionNumber = make<NameType>(parseNumber());
3897 if (!DimensionNumber)
3898 return nullptr;
3899 if (!consumeIf('_'))
3900 return nullptr;
3901 return make<BinaryFPType>(DimensionNumber);
3902 }
Richard Smithc20d1442018-08-20 20:14:49 +00003903 // ::= Di # char32_t
3904 case 'i':
3905 First += 2;
3906 return make<NameType>("char32_t");
3907 // ::= Ds # char16_t
3908 case 's':
3909 First += 2;
3910 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003911 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3912 case 'u':
3913 First += 2;
3914 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003915 // ::= Da # auto (in dependent new-expressions)
3916 case 'a':
3917 First += 2;
3918 return make<NameType>("auto");
3919 // ::= Dc # decltype(auto)
3920 case 'c':
3921 First += 2;
3922 return make<NameType>("decltype(auto)");
3923 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3924 case 'n':
3925 First += 2;
3926 return make<NameType>("std::nullptr_t");
3927
3928 // ::= <decltype>
3929 case 't':
3930 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003931 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003932 break;
3933 }
3934 // extension ::= <vector-type> # <vector-type> starts with Dv
3935 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003936 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003937 break;
3938 }
3939 // ::= Dp <type> # pack expansion (C++0x)
3940 case 'p': {
3941 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003942 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003943 if (!Child)
3944 return nullptr;
3945 Result = make<ParameterPackExpansion>(Child);
3946 break;
3947 }
3948 // Exception specifier on a function type.
3949 case 'o':
3950 case 'O':
3951 case 'w':
3952 // Transaction safe function type.
3953 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003954 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003955 break;
3956 }
3957 break;
3958 // ::= <function-type>
3959 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003960 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003961 break;
3962 }
3963 // ::= <array-type>
3964 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003965 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003966 break;
3967 }
3968 // ::= <pointer-to-member-type>
3969 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003970 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003971 break;
3972 }
3973 // ::= <template-param>
3974 case 'T': {
3975 // This could be an elaborate type specifier on a <class-enum-type>.
3976 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003977 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003978 break;
3979 }
3980
Pavel Labathba825192018-10-16 14:29:14 +00003981 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003982 if (Result == nullptr)
3983 return nullptr;
3984
3985 // Result could be either of:
3986 // <type> ::= <template-param>
3987 // <type> ::= <template-template-param> <template-args>
3988 //
3989 // <template-template-param> ::= <template-param>
3990 // ::= <substitution>
3991 //
3992 // If this is followed by some <template-args>, and we're permitted to
3993 // parse them, take the second production.
3994
3995 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003996 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003997 if (TA == nullptr)
3998 return nullptr;
3999 Result = make<NameWithTemplateArgs>(Result, TA);
4000 }
4001 break;
4002 }
4003 // ::= P <type> # pointer
4004 case 'P': {
4005 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004006 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004007 if (Ptr == nullptr)
4008 return nullptr;
4009 Result = make<PointerType>(Ptr);
4010 break;
4011 }
4012 // ::= R <type> # l-value reference
4013 case 'R': {
4014 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004015 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004016 if (Ref == nullptr)
4017 return nullptr;
4018 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
4019 break;
4020 }
4021 // ::= O <type> # r-value reference (C++11)
4022 case 'O': {
4023 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004024 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004025 if (Ref == nullptr)
4026 return nullptr;
4027 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
4028 break;
4029 }
4030 // ::= C <type> # complex pair (C99)
4031 case 'C': {
4032 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004033 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004034 if (P == nullptr)
4035 return nullptr;
4036 Result = make<PostfixQualifiedType>(P, " complex");
4037 break;
4038 }
4039 // ::= G <type> # imaginary (C99)
4040 case 'G': {
4041 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004042 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004043 if (P == nullptr)
4044 return P;
4045 Result = make<PostfixQualifiedType>(P, " imaginary");
4046 break;
4047 }
4048 // ::= <substitution> # See Compression below
4049 case 'S': {
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004050 if (look(1) != 't') {
4051 Result = getDerived().parseSubstitution();
4052 if (Result == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00004053 return nullptr;
4054
4055 // Sub could be either of:
4056 // <type> ::= <substitution>
4057 // <type> ::= <template-template-param> <template-args>
4058 //
4059 // <template-template-param> ::= <template-param>
4060 // ::= <substitution>
4061 //
4062 // If this is followed by some <template-args>, and we're permitted to
4063 // parse them, take the second production.
4064
4065 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004066 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004067 if (TA == nullptr)
4068 return nullptr;
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004069 Result = make<NameWithTemplateArgs>(Result, TA);
4070 } else {
4071 // If all we parsed was a substitution, don't re-insert into the
4072 // substitution table.
4073 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00004074 }
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08004075 break;
Richard Smithc20d1442018-08-20 20:14:49 +00004076 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00004077 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004078 }
4079 // ::= <class-enum-type>
4080 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004081 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004082 break;
4083 }
4084 }
4085
4086 // If we parsed a type, insert it into the substitution table. Note that all
4087 // <builtin-type>s and <substitution>s have already bailed out, because they
4088 // don't get substitutions.
4089 if (Result != nullptr)
4090 Subs.push_back(Result);
4091 return Result;
4092}
4093
Pavel Labathba825192018-10-16 14:29:14 +00004094template <typename Derived, typename Alloc>
4095Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4096 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004097 if (E == nullptr)
4098 return nullptr;
4099 return make<PrefixExpr>(Kind, E);
4100}
4101
Pavel Labathba825192018-10-16 14:29:14 +00004102template <typename Derived, typename Alloc>
4103Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4104 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004105 if (LHS == nullptr)
4106 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004107 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004108 if (RHS == nullptr)
4109 return nullptr;
4110 return make<BinaryExpr>(LHS, Kind, RHS);
4111}
4112
Pavel Labathba825192018-10-16 14:29:14 +00004113template <typename Derived, typename Alloc>
4114Node *
4115AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004116 StringView Tmp = parseNumber(true);
4117 if (!Tmp.empty() && consumeIf('E'))
4118 return make<IntegerLiteral>(Lit, Tmp);
4119 return nullptr;
4120}
4121
4122// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004123template <typename Alloc, typename Derived>
4124Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004125 Qualifiers CVR = QualNone;
4126 if (consumeIf('r'))
4127 CVR |= QualRestrict;
4128 if (consumeIf('V'))
4129 CVR |= QualVolatile;
4130 if (consumeIf('K'))
4131 CVR |= QualConst;
4132 return CVR;
4133}
4134
4135// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4136// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4137// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4138// ::= 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 -04004139// ::= fpT # 'this' expression (not part of standard?)
Pavel Labathba825192018-10-16 14:29:14 +00004140template <typename Derived, typename Alloc>
4141Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Erik Pilkington91c24af2020-05-13 22:19:45 -04004142 if (consumeIf("fpT"))
4143 return make<NameType>("this");
Richard Smithc20d1442018-08-20 20:14:49 +00004144 if (consumeIf("fp")) {
4145 parseCVQualifiers();
4146 StringView Num = parseNumber();
4147 if (!consumeIf('_'))
4148 return nullptr;
4149 return make<FunctionParam>(Num);
4150 }
4151 if (consumeIf("fL")) {
4152 if (parseNumber().empty())
4153 return nullptr;
4154 if (!consumeIf('p'))
4155 return nullptr;
4156 parseCVQualifiers();
4157 StringView Num = parseNumber();
4158 if (!consumeIf('_'))
4159 return nullptr;
4160 return make<FunctionParam>(Num);
4161 }
4162 return nullptr;
4163}
4164
Richard Smithc20d1442018-08-20 20:14:49 +00004165// cv <type> <expression> # conversion with one argument
4166// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004167template <typename Derived, typename Alloc>
4168Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004169 if (!consumeIf("cv"))
4170 return nullptr;
4171 Node *Ty;
4172 {
4173 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004174 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004175 }
4176
4177 if (Ty == nullptr)
4178 return nullptr;
4179
4180 if (consumeIf('_')) {
4181 size_t ExprsBegin = Names.size();
4182 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004183 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004184 if (E == nullptr)
4185 return E;
4186 Names.push_back(E);
4187 }
4188 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4189 return make<ConversionExpr>(Ty, Exprs);
4190 }
4191
Pavel Labathba825192018-10-16 14:29:14 +00004192 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004193 if (E[0] == nullptr)
4194 return nullptr;
4195 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4196}
4197
4198// <expr-primary> ::= L <type> <value number> E # integer literal
4199// ::= L <type> <value float> E # floating literal
4200// ::= L <string type> E # string literal
4201// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004202// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004203// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4204// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004205template <typename Derived, typename Alloc>
4206Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004207 if (!consumeIf('L'))
4208 return nullptr;
4209 switch (look()) {
4210 case 'w':
4211 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004212 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004213 case 'b':
4214 if (consumeIf("b0E"))
4215 return make<BoolExpr>(0);
4216 if (consumeIf("b1E"))
4217 return make<BoolExpr>(1);
4218 return nullptr;
4219 case 'c':
4220 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004221 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004222 case 'a':
4223 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004224 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004225 case 'h':
4226 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004227 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004228 case 's':
4229 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004230 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004231 case 't':
4232 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004233 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004234 case 'i':
4235 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004236 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004237 case 'j':
4238 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004239 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004240 case 'l':
4241 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004242 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004243 case 'm':
4244 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004245 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004246 case 'x':
4247 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004248 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004249 case 'y':
4250 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004251 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004252 case 'n':
4253 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004254 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004255 case 'o':
4256 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004257 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004258 case 'f':
4259 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004260 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004261 case 'd':
4262 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004263 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004264 case 'e':
4265 ++First;
Xing Xue3dc5e082020-04-15 09:59:06 -04004266#if defined(__powerpc__) || defined(__s390__)
4267 // Handle cases where long doubles encoded with e have the same size
4268 // and representation as doubles.
4269 return getDerived().template parseFloatingLiteral<double>();
4270#else
Pavel Labathba825192018-10-16 14:29:14 +00004271 return getDerived().template parseFloatingLiteral<long double>();
Xing Xue3dc5e082020-04-15 09:59:06 -04004272#endif
Richard Smithc20d1442018-08-20 20:14:49 +00004273 case '_':
4274 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004275 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004276 if (R != nullptr && consumeIf('E'))
4277 return R;
4278 }
4279 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004280 case 'A': {
4281 Node *T = getDerived().parseType();
4282 if (T == nullptr)
4283 return nullptr;
4284 // FIXME: We need to include the string contents in the mangling.
4285 if (consumeIf('E'))
4286 return make<StringLiteral>(T);
4287 return nullptr;
4288 }
4289 case 'D':
4290 if (consumeIf("DnE"))
4291 return make<NameType>("nullptr");
4292 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004293 case 'T':
4294 // Invalid mangled name per
4295 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4296 return nullptr;
Richard Smithfb917462019-09-09 22:26:04 +00004297 case 'U': {
4298 // FIXME: Should we support LUb... for block literals?
4299 if (look(1) != 'l')
4300 return nullptr;
4301 Node *T = parseUnnamedTypeName(nullptr);
4302 if (!T || !consumeIf('E'))
4303 return nullptr;
4304 return make<LambdaExpr>(T);
4305 }
Richard Smithc20d1442018-08-20 20:14:49 +00004306 default: {
4307 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004308 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004309 if (T == nullptr)
4310 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004311 StringView N = parseNumber(/*AllowNegative=*/true);
Richard Smithfb917462019-09-09 22:26:04 +00004312 if (N.empty())
4313 return nullptr;
4314 if (!consumeIf('E'))
4315 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004316 return make<EnumLiteral>(T, N);
Richard Smithc20d1442018-08-20 20:14:49 +00004317 }
4318 }
4319}
4320
4321// <braced-expression> ::= <expression>
4322// ::= di <field source-name> <braced-expression> # .name = expr
4323// ::= dx <index expression> <braced-expression> # [expr] = expr
4324// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004325template <typename Derived, typename Alloc>
4326Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004327 if (look() == 'd') {
4328 switch (look(1)) {
4329 case 'i': {
4330 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004331 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004332 if (Field == nullptr)
4333 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004334 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004335 if (Init == nullptr)
4336 return nullptr;
4337 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4338 }
4339 case 'x': {
4340 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004341 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004342 if (Index == nullptr)
4343 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004344 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004345 if (Init == nullptr)
4346 return nullptr;
4347 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4348 }
4349 case 'X': {
4350 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004351 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004352 if (RangeBegin == nullptr)
4353 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004354 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004355 if (RangeEnd == nullptr)
4356 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004357 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004358 if (Init == nullptr)
4359 return nullptr;
4360 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4361 }
4362 }
4363 }
Pavel Labathba825192018-10-16 14:29:14 +00004364 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004365}
4366
4367// (not yet in the spec)
4368// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4369// ::= fR <binary-operator-name> <expression> <expression>
4370// ::= fl <binary-operator-name> <expression>
4371// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004372template <typename Derived, typename Alloc>
4373Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004374 if (!consumeIf('f'))
4375 return nullptr;
4376
4377 char FoldKind = look();
4378 bool IsLeftFold, HasInitializer;
4379 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4380 if (FoldKind == 'l' || FoldKind == 'L')
4381 IsLeftFold = true;
4382 else if (FoldKind == 'r' || FoldKind == 'R')
4383 IsLeftFold = false;
4384 else
4385 return nullptr;
4386 ++First;
4387
4388 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4389 StringView OperatorName;
4390 if (consumeIf("aa")) OperatorName = "&&";
4391 else if (consumeIf("an")) OperatorName = "&";
4392 else if (consumeIf("aN")) OperatorName = "&=";
4393 else if (consumeIf("aS")) OperatorName = "=";
4394 else if (consumeIf("cm")) OperatorName = ",";
4395 else if (consumeIf("ds")) OperatorName = ".*";
4396 else if (consumeIf("dv")) OperatorName = "/";
4397 else if (consumeIf("dV")) OperatorName = "/=";
4398 else if (consumeIf("eo")) OperatorName = "^";
4399 else if (consumeIf("eO")) OperatorName = "^=";
4400 else if (consumeIf("eq")) OperatorName = "==";
4401 else if (consumeIf("ge")) OperatorName = ">=";
4402 else if (consumeIf("gt")) OperatorName = ">";
4403 else if (consumeIf("le")) OperatorName = "<=";
4404 else if (consumeIf("ls")) OperatorName = "<<";
4405 else if (consumeIf("lS")) OperatorName = "<<=";
4406 else if (consumeIf("lt")) OperatorName = "<";
4407 else if (consumeIf("mi")) OperatorName = "-";
4408 else if (consumeIf("mI")) OperatorName = "-=";
4409 else if (consumeIf("ml")) OperatorName = "*";
4410 else if (consumeIf("mL")) OperatorName = "*=";
4411 else if (consumeIf("ne")) OperatorName = "!=";
4412 else if (consumeIf("oo")) OperatorName = "||";
4413 else if (consumeIf("or")) OperatorName = "|";
4414 else if (consumeIf("oR")) OperatorName = "|=";
4415 else if (consumeIf("pl")) OperatorName = "+";
4416 else if (consumeIf("pL")) OperatorName = "+=";
4417 else if (consumeIf("rm")) OperatorName = "%";
4418 else if (consumeIf("rM")) OperatorName = "%=";
4419 else if (consumeIf("rs")) OperatorName = ">>";
4420 else if (consumeIf("rS")) OperatorName = ">>=";
4421 else return nullptr;
4422
Pavel Labathba825192018-10-16 14:29:14 +00004423 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004424 if (Pack == nullptr)
4425 return nullptr;
4426 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004427 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004428 if (Init == nullptr)
4429 return nullptr;
4430 }
4431
4432 if (IsLeftFold && Init)
4433 std::swap(Pack, Init);
4434
4435 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4436}
4437
Richard Smith1865d2f2020-10-22 19:29:36 -07004438// <expression> ::= mc <parameter type> <expr> [<offset number>] E
4439//
4440// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4441template <typename Derived, typename Alloc>
4442Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() {
4443 Node *Ty = getDerived().parseType();
4444 if (!Ty)
4445 return nullptr;
4446 Node *Expr = getDerived().parseExpr();
4447 if (!Expr)
4448 return nullptr;
4449 StringView Offset = getDerived().parseNumber(true);
4450 if (!consumeIf('E'))
4451 return nullptr;
4452 return make<PointerToMemberConversionExpr>(Ty, Expr, Offset);
4453}
4454
4455// <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
4456// <union-selector> ::= _ [<number>]
4457//
4458// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4459template <typename Derived, typename Alloc>
4460Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
4461 Node *Ty = getDerived().parseType();
4462 if (!Ty)
4463 return nullptr;
4464 Node *Expr = getDerived().parseExpr();
4465 if (!Expr)
4466 return nullptr;
4467 StringView Offset = getDerived().parseNumber(true);
4468 size_t SelectorsBegin = Names.size();
4469 while (consumeIf('_')) {
4470 Node *Selector = make<NameType>(parseNumber());
4471 if (!Selector)
4472 return nullptr;
4473 Names.push_back(Selector);
4474 }
4475 bool OnePastTheEnd = consumeIf('p');
4476 if (!consumeIf('E'))
4477 return nullptr;
4478 return make<SubobjectExpr>(
4479 Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
4480}
4481
Richard Smithc20d1442018-08-20 20:14:49 +00004482// <expression> ::= <unary operator-name> <expression>
4483// ::= <binary operator-name> <expression> <expression>
4484// ::= <ternary operator-name> <expression> <expression> <expression>
4485// ::= cl <expression>+ E # call
4486// ::= cv <type> <expression> # conversion with one argument
4487// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4488// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4489// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4490// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4491// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4492// ::= [gs] dl <expression> # delete expression
4493// ::= [gs] da <expression> # delete[] expression
4494// ::= pp_ <expression> # prefix ++
4495// ::= mm_ <expression> # prefix --
4496// ::= ti <type> # typeid (type)
4497// ::= te <expression> # typeid (expression)
4498// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4499// ::= sc <type> <expression> # static_cast<type> (expression)
4500// ::= cc <type> <expression> # const_cast<type> (expression)
4501// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4502// ::= st <type> # sizeof (a type)
4503// ::= sz <expression> # sizeof (an expression)
4504// ::= at <type> # alignof (a type)
4505// ::= az <expression> # alignof (an expression)
4506// ::= nx <expression> # noexcept (expression)
4507// ::= <template-param>
4508// ::= <function-param>
4509// ::= dt <expression> <unresolved-name> # expr.name
4510// ::= pt <expression> <unresolved-name> # expr->name
4511// ::= ds <expression> <expression> # expr.*expr
4512// ::= sZ <template-param> # size of a parameter pack
4513// ::= sZ <function-param> # size of a function parameter pack
4514// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4515// ::= sp <expression> # pack expansion
4516// ::= tw <expression> # throw expression
4517// ::= tr # throw with no operand (rethrow)
4518// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4519// # freestanding dependent name (e.g., T::x),
4520// # objectless nonstatic member reference
4521// ::= fL <binary-operator-name> <expression> <expression>
4522// ::= fR <binary-operator-name> <expression> <expression>
4523// ::= fl <binary-operator-name> <expression>
4524// ::= fr <binary-operator-name> <expression>
4525// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004526template <typename Derived, typename Alloc>
4527Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004528 bool Global = consumeIf("gs");
4529 if (numLeft() < 2)
4530 return nullptr;
4531
4532 switch (*First) {
4533 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004534 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004535 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004536 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004537 case 'f': {
4538 // Disambiguate a fold expression from a <function-param>.
4539 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004540 return getDerived().parseFunctionParam();
4541 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004542 }
4543 case 'a':
4544 switch (First[1]) {
4545 case 'a':
4546 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004547 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004548 case 'd':
4549 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004550 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004551 case 'n':
4552 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004553 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004554 case 'N':
4555 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004556 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004557 case 'S':
4558 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004559 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004560 case 't': {
4561 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004562 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004563 if (Ty == nullptr)
4564 return nullptr;
4565 return make<EnclosingExpr>("alignof (", Ty, ")");
4566 }
4567 case 'z': {
4568 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004569 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004570 if (Ty == nullptr)
4571 return nullptr;
4572 return make<EnclosingExpr>("alignof (", Ty, ")");
4573 }
4574 }
4575 return nullptr;
4576 case 'c':
4577 switch (First[1]) {
4578 // cc <type> <expression> # const_cast<type>(expression)
4579 case 'c': {
4580 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004581 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004582 if (Ty == nullptr)
4583 return Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004584 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004585 if (Ex == nullptr)
4586 return Ex;
4587 return make<CastExpr>("const_cast", Ty, Ex);
4588 }
4589 // cl <expression>+ E # call
4590 case 'l': {
4591 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004592 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004593 if (Callee == nullptr)
4594 return Callee;
4595 size_t ExprsBegin = Names.size();
4596 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004597 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004598 if (E == nullptr)
4599 return E;
4600 Names.push_back(E);
4601 }
4602 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4603 }
4604 case 'm':
4605 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004606 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004607 case 'o':
4608 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004609 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004610 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004611 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004612 }
4613 return nullptr;
4614 case 'd':
4615 switch (First[1]) {
4616 case 'a': {
4617 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004618 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004619 if (Ex == nullptr)
4620 return Ex;
4621 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4622 }
4623 case 'c': {
4624 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004625 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004626 if (T == nullptr)
4627 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004628 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004629 if (Ex == nullptr)
4630 return Ex;
4631 return make<CastExpr>("dynamic_cast", T, Ex);
4632 }
4633 case 'e':
4634 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004635 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004636 case 'l': {
4637 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004638 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004639 if (E == nullptr)
4640 return E;
4641 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4642 }
4643 case 'n':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08004644 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004645 case 's': {
4646 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004647 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004648 if (LHS == nullptr)
4649 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004650 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004651 if (RHS == nullptr)
4652 return nullptr;
4653 return make<MemberExpr>(LHS, ".*", RHS);
4654 }
4655 case 't': {
4656 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004657 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004658 if (LHS == nullptr)
4659 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004660 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004661 if (RHS == nullptr)
4662 return nullptr;
4663 return make<MemberExpr>(LHS, ".", RHS);
4664 }
4665 case 'v':
4666 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004667 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004668 case 'V':
4669 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004670 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004671 }
4672 return nullptr;
4673 case 'e':
4674 switch (First[1]) {
4675 case 'o':
4676 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004677 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004678 case 'O':
4679 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004680 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004681 case 'q':
4682 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004683 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004684 }
4685 return nullptr;
4686 case 'g':
4687 switch (First[1]) {
4688 case 'e':
4689 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004690 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004691 case 't':
4692 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004693 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004694 }
4695 return nullptr;
4696 case 'i':
4697 switch (First[1]) {
4698 case 'x': {
4699 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004700 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004701 if (Base == nullptr)
4702 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004703 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004704 if (Index == nullptr)
4705 return Index;
4706 return make<ArraySubscriptExpr>(Base, Index);
4707 }
4708 case 'l': {
4709 First += 2;
4710 size_t InitsBegin = Names.size();
4711 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004712 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004713 if (E == nullptr)
4714 return nullptr;
4715 Names.push_back(E);
4716 }
4717 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4718 }
4719 }
4720 return nullptr;
4721 case 'l':
4722 switch (First[1]) {
4723 case 'e':
4724 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004725 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004726 case 's':
4727 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004728 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004729 case 'S':
4730 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004731 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004732 case 't':
4733 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004734 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004735 }
4736 return nullptr;
4737 case 'm':
4738 switch (First[1]) {
Richard Smith1865d2f2020-10-22 19:29:36 -07004739 case 'c':
4740 First += 2;
4741 return parsePointerToMemberConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004742 case 'i':
4743 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004744 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004745 case 'I':
4746 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004747 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004748 case 'l':
4749 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004750 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004751 case 'L':
4752 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004753 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004754 case 'm':
4755 First += 2;
4756 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004757 return getDerived().parsePrefixExpr("--");
4758 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004759 if (Ex == nullptr)
4760 return nullptr;
4761 return make<PostfixExpr>(Ex, "--");
4762 }
4763 return nullptr;
4764 case 'n':
4765 switch (First[1]) {
4766 case 'a':
Nathan Sidwellc69bde22022-01-28 07:09:38 -08004767 case 'w': {
4768 // [gs] nw <expression>* _ <type> [pi <expression>*] E # new (expr-list) type [(init)]
4769 // [gs] na <expression>* _ <type> [pi <expression>*] E # new[] (expr-list) type [(init)]
4770 bool IsArray = First[1] == 'a';
4771 First += 2;
4772 size_t Exprs = Names.size();
4773 while (!consumeIf('_')) {
4774 Node *Ex = getDerived().parseExpr();
4775 if (Ex == nullptr)
4776 return nullptr;
4777 Names.push_back(Ex);
4778 }
4779 NodeArray ExprList = popTrailingNodeArray(Exprs);
4780 Node *Ty = getDerived().parseType();
4781 if (Ty == nullptr)
4782 return nullptr;
4783 bool HaveInits = consumeIf("pi");
4784 size_t InitsBegin = Names.size();
4785 while (!consumeIf('E')) {
4786 if (!HaveInits)
4787 return nullptr;
4788 Node *Init = getDerived().parseExpr();
4789 if (Init == nullptr)
4790 return Init;
4791 Names.push_back(Init);
4792 }
4793 NodeArray Inits = popTrailingNodeArray(InitsBegin);
4794 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
4795 }
Richard Smithc20d1442018-08-20 20:14:49 +00004796 case 'e':
4797 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004798 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004799 case 'g':
4800 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004801 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004802 case 't':
4803 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004804 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004805 case 'x':
4806 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004807 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004808 if (Ex == nullptr)
4809 return Ex;
4810 return make<EnclosingExpr>("noexcept (", Ex, ")");
4811 }
4812 return nullptr;
4813 case 'o':
4814 switch (First[1]) {
4815 case 'n':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08004816 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004817 case 'o':
4818 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004819 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004820 case 'r':
4821 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004822 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004823 case 'R':
4824 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004825 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004826 }
4827 return nullptr;
4828 case 'p':
4829 switch (First[1]) {
Nathan Sidwellc6483042022-01-28 09:27:28 -08004830 case 'm': {
Richard Smithc20d1442018-08-20 20:14:49 +00004831 First += 2;
Nathan Sidwellc6483042022-01-28 09:27:28 -08004832 Node *LHS = getDerived().parseExpr();
4833 if (LHS == nullptr)
4834 return LHS;
4835 Node *RHS = getDerived().parseExpr();
4836 if (RHS == nullptr)
4837 return nullptr;
4838 return make<MemberExpr>(LHS, "->*", RHS);
4839 }
Richard Smithc20d1442018-08-20 20:14:49 +00004840 case 'l':
4841 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004842 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004843 case 'L':
4844 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004845 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004846 case 'p': {
4847 First += 2;
4848 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004849 return getDerived().parsePrefixExpr("++");
4850 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004851 if (Ex == nullptr)
4852 return Ex;
4853 return make<PostfixExpr>(Ex, "++");
4854 }
4855 case 's':
4856 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004857 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004858 case 't': {
4859 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004860 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004861 if (L == nullptr)
4862 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004863 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004864 if (R == nullptr)
4865 return nullptr;
4866 return make<MemberExpr>(L, "->", R);
4867 }
4868 }
4869 return nullptr;
4870 case 'q':
4871 if (First[1] == 'u') {
4872 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004873 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004874 if (Cond == nullptr)
4875 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004876 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004877 if (LHS == nullptr)
4878 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004879 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004880 if (RHS == nullptr)
4881 return nullptr;
4882 return make<ConditionalExpr>(Cond, LHS, RHS);
4883 }
4884 return nullptr;
4885 case 'r':
4886 switch (First[1]) {
4887 case 'c': {
4888 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004889 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004890 if (T == nullptr)
4891 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004892 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004893 if (Ex == nullptr)
4894 return Ex;
4895 return make<CastExpr>("reinterpret_cast", T, Ex);
4896 }
4897 case 'm':
4898 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004899 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004900 case 'M':
4901 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004902 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004903 case 's':
4904 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004905 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004906 case 'S':
4907 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004908 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004909 }
4910 return nullptr;
4911 case 's':
4912 switch (First[1]) {
4913 case 'c': {
4914 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004915 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004916 if (T == nullptr)
4917 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004918 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004919 if (Ex == nullptr)
4920 return Ex;
4921 return make<CastExpr>("static_cast", T, Ex);
4922 }
Richard Smith1865d2f2020-10-22 19:29:36 -07004923 case 'o':
4924 First += 2;
4925 return parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004926 case 'p': {
4927 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004928 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004929 if (Child == nullptr)
4930 return nullptr;
4931 return make<ParameterPackExpansion>(Child);
4932 }
4933 case 'r':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08004934 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004935 case 't': {
4936 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004937 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004938 if (Ty == nullptr)
4939 return Ty;
4940 return make<EnclosingExpr>("sizeof (", Ty, ")");
4941 }
4942 case 'z': {
4943 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004944 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004945 if (Ex == nullptr)
4946 return Ex;
4947 return make<EnclosingExpr>("sizeof (", Ex, ")");
4948 }
4949 case 'Z':
4950 First += 2;
4951 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004952 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004953 if (R == nullptr)
4954 return nullptr;
4955 return make<SizeofParamPackExpr>(R);
4956 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004957 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004958 if (FP == nullptr)
4959 return nullptr;
4960 return make<EnclosingExpr>("sizeof... (", FP, ")");
4961 }
4962 return nullptr;
4963 case 'P': {
4964 First += 2;
4965 size_t ArgsBegin = Names.size();
4966 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004967 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004968 if (Arg == nullptr)
4969 return nullptr;
4970 Names.push_back(Arg);
4971 }
Richard Smithb485b352018-08-24 23:30:26 +00004972 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4973 if (!Pack)
4974 return nullptr;
4975 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00004976 }
4977 }
4978 return nullptr;
4979 case 't':
4980 switch (First[1]) {
4981 case 'e': {
4982 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004983 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004984 if (Ex == nullptr)
4985 return Ex;
4986 return make<EnclosingExpr>("typeid (", Ex, ")");
4987 }
4988 case 'i': {
4989 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004990 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004991 if (Ty == nullptr)
4992 return Ty;
4993 return make<EnclosingExpr>("typeid (", Ty, ")");
4994 }
4995 case 'l': {
4996 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004997 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004998 if (Ty == nullptr)
4999 return nullptr;
5000 size_t InitsBegin = Names.size();
5001 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005002 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005003 if (E == nullptr)
5004 return nullptr;
5005 Names.push_back(E);
5006 }
5007 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
5008 }
5009 case 'r':
5010 First += 2;
5011 return make<NameType>("throw");
5012 case 'w': {
5013 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005014 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005015 if (Ex == nullptr)
5016 return nullptr;
5017 return make<ThrowExpr>(Ex);
5018 }
5019 }
5020 return nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05005021 case 'u': {
5022 ++First;
5023 Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
5024 if (!Name)
5025 return nullptr;
5026 // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
5027 // standard encoding expects a <template-arg>, and would be otherwise be
5028 // interpreted as <type> node 'short' or 'ellipsis'. However, neither
5029 // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
5030 // actual conflict here.
Nathan Sidwella3b59002022-02-11 05:54:40 -08005031 bool IsUUID = false;
5032 Node *UUID = nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05005033 if (Name->getBaseName() == "__uuidof") {
Nathan Sidwella3b59002022-02-11 05:54:40 -08005034 if (consumeIf('t')) {
5035 UUID = getDerived().parseType();
5036 IsUUID = true;
5037 } else if (consumeIf('z')) {
5038 UUID = getDerived().parseExpr();
5039 IsUUID = true;
James Y Knight4a60efc2020-12-07 10:26:49 -05005040 }
5041 }
5042 size_t ExprsBegin = Names.size();
Nathan Sidwella3b59002022-02-11 05:54:40 -08005043 if (IsUUID) {
5044 if (UUID == nullptr)
5045 return nullptr;
5046 Names.push_back(UUID);
5047 } else {
5048 while (!consumeIf('E')) {
5049 Node *E = getDerived().parseTemplateArg();
5050 if (E == nullptr)
5051 return E;
5052 Names.push_back(E);
5053 }
James Y Knight4a60efc2020-12-07 10:26:49 -05005054 }
5055 return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin));
5056 }
Richard Smithc20d1442018-08-20 20:14:49 +00005057 case '1':
5058 case '2':
5059 case '3':
5060 case '4':
5061 case '5':
5062 case '6':
5063 case '7':
5064 case '8':
5065 case '9':
Nathan Sidwell77c52e22022-01-28 11:59:03 -08005066 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00005067 }
5068 return nullptr;
5069}
5070
5071// <call-offset> ::= h <nv-offset> _
5072// ::= v <v-offset> _
5073//
5074// <nv-offset> ::= <offset number>
5075// # non-virtual base override
5076//
5077// <v-offset> ::= <offset number> _ <virtual offset number>
5078// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00005079template <typename Alloc, typename Derived>
5080bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00005081 // Just scan through the call offset, we never add this information into the
5082 // output.
5083 if (consumeIf('h'))
5084 return parseNumber(true).empty() || !consumeIf('_');
5085 if (consumeIf('v'))
5086 return parseNumber(true).empty() || !consumeIf('_') ||
5087 parseNumber(true).empty() || !consumeIf('_');
5088 return true;
5089}
5090
5091// <special-name> ::= TV <type> # virtual table
5092// ::= TT <type> # VTT structure (construction vtable index)
5093// ::= TI <type> # typeinfo structure
5094// ::= TS <type> # typeinfo name (null-terminated byte string)
5095// ::= Tc <call-offset> <call-offset> <base encoding>
5096// # base is the nominal target function of thunk
5097// # first call-offset is 'this' adjustment
5098// # second call-offset is result adjustment
5099// ::= T <call-offset> <base encoding>
5100// # base is the nominal target function of thunk
5101// ::= GV <object name> # Guard variable for one-time initialization
5102// # No <type>
5103// ::= TW <object name> # Thread-local wrapper
5104// ::= TH <object name> # Thread-local initialization
5105// ::= GR <object name> _ # First temporary
5106// ::= GR <object name> <seq-id> _ # Subsequent temporaries
5107// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
5108// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00005109template <typename Derived, typename Alloc>
5110Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00005111 switch (look()) {
5112 case 'T':
5113 switch (look(1)) {
Richard Smith1865d2f2020-10-22 19:29:36 -07005114 // TA <template-arg> # template parameter object
5115 //
5116 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
5117 case 'A': {
5118 First += 2;
5119 Node *Arg = getDerived().parseTemplateArg();
5120 if (Arg == nullptr)
5121 return nullptr;
5122 return make<SpecialName>("template parameter object for ", Arg);
5123 }
Richard Smithc20d1442018-08-20 20:14:49 +00005124 // TV <type> # virtual table
5125 case 'V': {
5126 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005127 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005128 if (Ty == nullptr)
5129 return nullptr;
5130 return make<SpecialName>("vtable for ", Ty);
5131 }
5132 // TT <type> # VTT structure (construction vtable index)
5133 case 'T': {
5134 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005135 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005136 if (Ty == nullptr)
5137 return nullptr;
5138 return make<SpecialName>("VTT for ", Ty);
5139 }
5140 // TI <type> # typeinfo structure
5141 case 'I': {
5142 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005143 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005144 if (Ty == nullptr)
5145 return nullptr;
5146 return make<SpecialName>("typeinfo for ", Ty);
5147 }
5148 // TS <type> # typeinfo name (null-terminated byte string)
5149 case 'S': {
5150 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005151 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005152 if (Ty == nullptr)
5153 return nullptr;
5154 return make<SpecialName>("typeinfo name for ", Ty);
5155 }
5156 // Tc <call-offset> <call-offset> <base encoding>
5157 case 'c': {
5158 First += 2;
5159 if (parseCallOffset() || parseCallOffset())
5160 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005161 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005162 if (Encoding == nullptr)
5163 return nullptr;
5164 return make<SpecialName>("covariant return thunk to ", Encoding);
5165 }
5166 // extension ::= TC <first type> <number> _ <second type>
5167 // # construction vtable for second-in-first
5168 case 'C': {
5169 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005170 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005171 if (FirstType == nullptr)
5172 return nullptr;
5173 if (parseNumber(true).empty() || !consumeIf('_'))
5174 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005175 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005176 if (SecondType == nullptr)
5177 return nullptr;
5178 return make<CtorVtableSpecialName>(SecondType, FirstType);
5179 }
5180 // TW <object name> # Thread-local wrapper
5181 case 'W': {
5182 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005183 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005184 if (Name == nullptr)
5185 return nullptr;
5186 return make<SpecialName>("thread-local wrapper routine for ", Name);
5187 }
5188 // TH <object name> # Thread-local initialization
5189 case 'H': {
5190 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005191 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005192 if (Name == nullptr)
5193 return nullptr;
5194 return make<SpecialName>("thread-local initialization routine for ", Name);
5195 }
5196 // T <call-offset> <base encoding>
5197 default: {
5198 ++First;
5199 bool IsVirt = look() == 'v';
5200 if (parseCallOffset())
5201 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005202 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005203 if (BaseEncoding == nullptr)
5204 return nullptr;
5205 if (IsVirt)
5206 return make<SpecialName>("virtual thunk to ", BaseEncoding);
5207 else
5208 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
5209 }
5210 }
5211 case 'G':
5212 switch (look(1)) {
5213 // GV <object name> # Guard variable for one-time initialization
5214 case 'V': {
5215 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005216 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005217 if (Name == nullptr)
5218 return nullptr;
5219 return make<SpecialName>("guard variable for ", Name);
5220 }
5221 // GR <object name> # reference temporary for object
5222 // GR <object name> _ # First temporary
5223 // GR <object name> <seq-id> _ # Subsequent temporaries
5224 case 'R': {
5225 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005226 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005227 if (Name == nullptr)
5228 return nullptr;
5229 size_t Count;
5230 bool ParsedSeqId = !parseSeqId(&Count);
5231 if (!consumeIf('_') && ParsedSeqId)
5232 return nullptr;
5233 return make<SpecialName>("reference temporary for ", Name);
5234 }
5235 }
5236 }
5237 return nullptr;
5238}
5239
5240// <encoding> ::= <function name> <bare-function-type>
5241// ::= <data name>
5242// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00005243template <typename Derived, typename Alloc>
5244Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithfac39712020-07-09 21:08:39 -07005245 // The template parameters of an encoding are unrelated to those of the
5246 // enclosing context.
5247 class SaveTemplateParams {
5248 AbstractManglingParser *Parser;
5249 decltype(TemplateParams) OldParams;
Justin Lebar2c536232021-06-09 16:57:22 -07005250 decltype(OuterTemplateParams) OldOuterParams;
Richard Smithfac39712020-07-09 21:08:39 -07005251
5252 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04005253 SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
Richard Smithfac39712020-07-09 21:08:39 -07005254 OldParams = std::move(Parser->TemplateParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005255 OldOuterParams = std::move(Parser->OuterTemplateParams);
Richard Smithfac39712020-07-09 21:08:39 -07005256 Parser->TemplateParams.clear();
Justin Lebar2c536232021-06-09 16:57:22 -07005257 Parser->OuterTemplateParams.clear();
Richard Smithfac39712020-07-09 21:08:39 -07005258 }
5259 ~SaveTemplateParams() {
5260 Parser->TemplateParams = std::move(OldParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005261 Parser->OuterTemplateParams = std::move(OldOuterParams);
Richard Smithfac39712020-07-09 21:08:39 -07005262 }
5263 } SaveTemplateParams(this);
Richard Smithfd434322020-07-09 20:36:04 -07005264
Richard Smithc20d1442018-08-20 20:14:49 +00005265 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00005266 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00005267
5268 auto IsEndOfEncoding = [&] {
5269 // The set of chars that can potentially follow an <encoding> (none of which
5270 // can start a <type>). Enumerating these allows us to avoid speculative
5271 // parsing.
5272 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5273 };
5274
5275 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00005276 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00005277 if (Name == nullptr)
5278 return nullptr;
5279
5280 if (resolveForwardTemplateRefs(NameInfo))
5281 return nullptr;
5282
5283 if (IsEndOfEncoding())
5284 return Name;
5285
5286 Node *Attrs = nullptr;
5287 if (consumeIf("Ua9enable_ifI")) {
5288 size_t BeforeArgs = Names.size();
5289 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005290 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005291 if (Arg == nullptr)
5292 return nullptr;
5293 Names.push_back(Arg);
5294 }
5295 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00005296 if (!Attrs)
5297 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005298 }
5299
5300 Node *ReturnType = nullptr;
5301 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00005302 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005303 if (ReturnType == nullptr)
5304 return nullptr;
5305 }
5306
5307 if (consumeIf('v'))
5308 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5309 Attrs, NameInfo.CVQualifiers,
5310 NameInfo.ReferenceQualifier);
5311
5312 size_t ParamsBegin = Names.size();
5313 do {
Pavel Labathba825192018-10-16 14:29:14 +00005314 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005315 if (Ty == nullptr)
5316 return nullptr;
5317 Names.push_back(Ty);
5318 } while (!IsEndOfEncoding());
5319
5320 return make<FunctionEncoding>(ReturnType, Name,
5321 popTrailingNodeArray(ParamsBegin),
5322 Attrs, NameInfo.CVQualifiers,
5323 NameInfo.ReferenceQualifier);
5324}
5325
5326template <class Float>
5327struct FloatData;
5328
5329template <>
5330struct FloatData<float>
5331{
5332 static const size_t mangled_size = 8;
5333 static const size_t max_demangled_size = 24;
5334 static constexpr const char* spec = "%af";
5335};
5336
5337template <>
5338struct FloatData<double>
5339{
5340 static const size_t mangled_size = 16;
5341 static const size_t max_demangled_size = 32;
5342 static constexpr const char* spec = "%a";
5343};
5344
5345template <>
5346struct FloatData<long double>
5347{
5348#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5349 defined(__wasm__)
5350 static const size_t mangled_size = 32;
5351#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5352 static const size_t mangled_size = 16;
5353#else
5354 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5355#endif
Elliott Hughes5a360ea2020-04-10 17:42:00 -07005356 // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
5357 // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
5358 // Negatives are one character longer than positives.
5359 // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
5360 // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
5361 static const size_t max_demangled_size = 42;
Richard Smithc20d1442018-08-20 20:14:49 +00005362 static constexpr const char *spec = "%LaL";
5363};
5364
Pavel Labathba825192018-10-16 14:29:14 +00005365template <typename Alloc, typename Derived>
5366template <class Float>
5367Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005368 const size_t N = FloatData<Float>::mangled_size;
5369 if (numLeft() <= N)
5370 return nullptr;
5371 StringView Data(First, First + N);
5372 for (char C : Data)
5373 if (!std::isxdigit(C))
5374 return nullptr;
5375 First += N;
5376 if (!consumeIf('E'))
5377 return nullptr;
5378 return make<FloatLiteralImpl<Float>>(Data);
5379}
5380
5381// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005382template <typename Alloc, typename Derived>
5383bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005384 if (!(look() >= '0' && look() <= '9') &&
5385 !(look() >= 'A' && look() <= 'Z'))
5386 return true;
5387
5388 size_t Id = 0;
5389 while (true) {
5390 if (look() >= '0' && look() <= '9') {
5391 Id *= 36;
5392 Id += static_cast<size_t>(look() - '0');
5393 } else if (look() >= 'A' && look() <= 'Z') {
5394 Id *= 36;
5395 Id += static_cast<size_t>(look() - 'A') + 10;
5396 } else {
5397 *Out = Id;
5398 return false;
5399 }
5400 ++First;
5401 }
5402}
5403
5404// <substitution> ::= S <seq-id> _
5405// ::= S_
5406// <substitution> ::= Sa # ::std::allocator
5407// <substitution> ::= Sb # ::std::basic_string
5408// <substitution> ::= Ss # ::std::basic_string < char,
5409// ::std::char_traits<char>,
5410// ::std::allocator<char> >
5411// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5412// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5413// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08005414// The St case is handled specially in parseNestedName.
Pavel Labathba825192018-10-16 14:29:14 +00005415template <typename Derived, typename Alloc>
5416Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005417 if (!consumeIf('S'))
5418 return nullptr;
5419
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08005420 if (look() >= 'a' && look() <= 'z') {
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005421 SpecialSubKind Kind;
Richard Smithc20d1442018-08-20 20:14:49 +00005422 switch (look()) {
5423 case 'a':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005424 Kind = SpecialSubKind::allocator;
Richard Smithc20d1442018-08-20 20:14:49 +00005425 break;
5426 case 'b':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005427 Kind = SpecialSubKind::basic_string;
Richard Smithc20d1442018-08-20 20:14:49 +00005428 break;
5429 case 'd':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005430 Kind = SpecialSubKind::iostream;
5431 break;
5432 case 'i':
5433 Kind = SpecialSubKind::istream;
5434 break;
5435 case 'o':
5436 Kind = SpecialSubKind::ostream;
5437 break;
5438 case 's':
5439 Kind = SpecialSubKind::string;
Richard Smithc20d1442018-08-20 20:14:49 +00005440 break;
5441 default:
5442 return nullptr;
5443 }
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005444 ++First;
5445 auto *SpecialSub = make<SpecialSubstitution>(Kind);
Richard Smithb485b352018-08-24 23:30:26 +00005446 if (!SpecialSub)
5447 return nullptr;
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005448
Richard Smithc20d1442018-08-20 20:14:49 +00005449 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5450 // has ABI tags, the tags are appended to the substitution; the result is a
5451 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005452 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005453 if (WithTags != SpecialSub) {
5454 Subs.push_back(WithTags);
5455 SpecialSub = WithTags;
5456 }
5457 return SpecialSub;
5458 }
5459
5460 // ::= S_
5461 if (consumeIf('_')) {
5462 if (Subs.empty())
5463 return nullptr;
5464 return Subs[0];
5465 }
5466
5467 // ::= S <seq-id> _
5468 size_t Index = 0;
5469 if (parseSeqId(&Index))
5470 return nullptr;
5471 ++Index;
5472 if (!consumeIf('_') || Index >= Subs.size())
5473 return nullptr;
5474 return Subs[Index];
5475}
5476
5477// <template-param> ::= T_ # first template parameter
5478// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005479// ::= TL <level-1> __
5480// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005481template <typename Derived, typename Alloc>
5482Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005483 if (!consumeIf('T'))
5484 return nullptr;
5485
Richard Smithdf1c14c2019-09-06 23:53:21 +00005486 size_t Level = 0;
5487 if (consumeIf('L')) {
5488 if (parsePositiveInteger(&Level))
5489 return nullptr;
5490 ++Level;
5491 if (!consumeIf('_'))
5492 return nullptr;
5493 }
5494
Richard Smithc20d1442018-08-20 20:14:49 +00005495 size_t Index = 0;
5496 if (!consumeIf('_')) {
5497 if (parsePositiveInteger(&Index))
5498 return nullptr;
5499 ++Index;
5500 if (!consumeIf('_'))
5501 return nullptr;
5502 }
5503
Richard Smithc20d1442018-08-20 20:14:49 +00005504 // If we're in a context where this <template-param> refers to a
5505 // <template-arg> further ahead in the mangled name (currently just conversion
5506 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005507 // This can only happen at the outermost level.
5508 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005509 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5510 if (!ForwardRef)
5511 return nullptr;
5512 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5513 ForwardTemplateRefs.push_back(
5514 static_cast<ForwardTemplateReference *>(ForwardRef));
5515 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005516 }
5517
Richard Smithdf1c14c2019-09-06 23:53:21 +00005518 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5519 Index >= TemplateParams[Level]->size()) {
5520 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5521 // list are mangled as the corresponding artificial template type parameter.
5522 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5523 // This will be popped by the ScopedTemplateParamList in
5524 // parseUnnamedTypeName.
5525 if (Level == TemplateParams.size())
5526 TemplateParams.push_back(nullptr);
5527 return make<NameType>("auto");
5528 }
5529
Richard Smithc20d1442018-08-20 20:14:49 +00005530 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005531 }
5532
5533 return (*TemplateParams[Level])[Index];
5534}
5535
5536// <template-param-decl> ::= Ty # type parameter
5537// ::= Tn <type> # non-type parameter
5538// ::= Tt <template-param-decl>* E # template parameter
5539// ::= Tp <template-param-decl> # parameter pack
5540template <typename Derived, typename Alloc>
5541Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5542 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5543 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5544 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5545 if (N) TemplateParams.back()->push_back(N);
5546 return N;
5547 };
5548
5549 if (consumeIf("Ty")) {
5550 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5551 if (!Name)
5552 return nullptr;
5553 return make<TypeTemplateParamDecl>(Name);
5554 }
5555
5556 if (consumeIf("Tn")) {
5557 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5558 if (!Name)
5559 return nullptr;
5560 Node *Type = parseType();
5561 if (!Type)
5562 return nullptr;
5563 return make<NonTypeTemplateParamDecl>(Name, Type);
5564 }
5565
5566 if (consumeIf("Tt")) {
5567 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5568 if (!Name)
5569 return nullptr;
5570 size_t ParamsBegin = Names.size();
5571 ScopedTemplateParamList TemplateTemplateParamParams(this);
5572 while (!consumeIf("E")) {
5573 Node *P = parseTemplateParamDecl();
5574 if (!P)
5575 return nullptr;
5576 Names.push_back(P);
5577 }
5578 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5579 return make<TemplateTemplateParamDecl>(Name, Params);
5580 }
5581
5582 if (consumeIf("Tp")) {
5583 Node *P = parseTemplateParamDecl();
5584 if (!P)
5585 return nullptr;
5586 return make<TemplateParamPackDecl>(P);
5587 }
5588
5589 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005590}
5591
5592// <template-arg> ::= <type> # type or template
5593// ::= X <expression> E # expression
5594// ::= <expr-primary> # simple expressions
5595// ::= J <template-arg>* E # argument pack
5596// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005597template <typename Derived, typename Alloc>
5598Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005599 switch (look()) {
5600 case 'X': {
5601 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005602 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005603 if (Arg == nullptr || !consumeIf('E'))
5604 return nullptr;
5605 return Arg;
5606 }
5607 case 'J': {
5608 ++First;
5609 size_t ArgsBegin = Names.size();
5610 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005611 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005612 if (Arg == nullptr)
5613 return nullptr;
5614 Names.push_back(Arg);
5615 }
5616 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5617 return make<TemplateArgumentPack>(Args);
5618 }
5619 case 'L': {
5620 // ::= LZ <encoding> E # extension
5621 if (look(1) == 'Z') {
5622 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005623 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005624 if (Arg == nullptr || !consumeIf('E'))
5625 return nullptr;
5626 return Arg;
5627 }
5628 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005629 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005630 }
5631 default:
Pavel Labathba825192018-10-16 14:29:14 +00005632 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005633 }
5634}
5635
5636// <template-args> ::= I <template-arg>* E
5637// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005638template <typename Derived, typename Alloc>
5639Node *
5640AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005641 if (!consumeIf('I'))
5642 return nullptr;
5643
5644 // <template-params> refer to the innermost <template-args>. Clear out any
5645 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005646 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005647 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005648 TemplateParams.push_back(&OuterTemplateParams);
5649 OuterTemplateParams.clear();
5650 }
Richard Smithc20d1442018-08-20 20:14:49 +00005651
5652 size_t ArgsBegin = Names.size();
5653 while (!consumeIf('E')) {
5654 if (TagTemplates) {
5655 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005656 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005657 TemplateParams = std::move(OldParams);
5658 if (Arg == nullptr)
5659 return nullptr;
5660 Names.push_back(Arg);
5661 Node *TableEntry = Arg;
5662 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5663 TableEntry = make<ParameterPack>(
5664 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005665 if (!TableEntry)
5666 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005667 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005668 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005669 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005670 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005671 if (Arg == nullptr)
5672 return nullptr;
5673 Names.push_back(Arg);
5674 }
5675 }
5676 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5677}
5678
5679// <mangled-name> ::= _Z <encoding>
5680// ::= <type>
5681// extension ::= ___Z <encoding> _block_invoke
5682// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5683// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005684template <typename Derived, typename Alloc>
5685Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005686 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005687 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005688 if (Encoding == nullptr)
5689 return nullptr;
5690 if (look() == '.') {
5691 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5692 First = Last;
5693 }
5694 if (numLeft() != 0)
5695 return nullptr;
5696 return Encoding;
5697 }
5698
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005699 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005700 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005701 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5702 return nullptr;
5703 bool RequireNumber = consumeIf('_');
5704 if (parseNumber().empty() && RequireNumber)
5705 return nullptr;
5706 if (look() == '.')
5707 First = Last;
5708 if (numLeft() != 0)
5709 return nullptr;
5710 return make<SpecialName>("invocation function for block in ", Encoding);
5711 }
5712
Pavel Labathba825192018-10-16 14:29:14 +00005713 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005714 if (numLeft() != 0)
5715 return nullptr;
5716 return Ty;
5717}
5718
Pavel Labathba825192018-10-16 14:29:14 +00005719template <typename Alloc>
5720struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5721 using AbstractManglingParser<ManglingParser<Alloc>,
5722 Alloc>::AbstractManglingParser;
5723};
5724
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005725DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005726
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005727#endif // DEMANGLE_ITANIUMDEMANGLE_H