blob: a3693b524f96c5c366ac33d56ea59a100413c401 [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
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08002559 struct OperatorInfo {
2560 enum OIKind : unsigned char {
2561 Prefix, // Prefix unary: @ expr
2562 Postfix, // Postfix unary: expr @
2563 Binary, // Binary: lhs @ rhs
2564 Array, // Array index: lhs [ rhs ]
2565 Member, // Member access: lhs @ rhs
2566 New, // New
2567 Del, // Delete
2568 Call, // Function call: expr (expr*)
2569 CCast, // C cast: (type)expr
2570 Conditional, // Conditional: expr ? expr : expr
Nathan Sidwell0dda3d42022-02-18 09:51:24 -08002571 NameOnly, // Overload only, not allowed in expression.
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08002572 // Below do not have operator names
2573 NamedCast, // Named cast, @<type>(expr)
2574 OfIdOp, // alignof, sizeof, typeid
2575
2576 Unnameable = NamedCast,
2577 };
2578 char Enc[2]; // Encoding
2579 OIKind Kind; // Kind of operator
2580 bool Flag : 1; // Entry-specific flag
2581 const char *Name; // Spelling
2582
2583 public:
2584 constexpr OperatorInfo(const char (&E)[3], OIKind K, bool F, const char *N)
2585 : Enc{E[0], E[1]}, Kind{K}, Flag{F}, Name{N} {}
2586
2587 public:
2588 bool operator<(const OperatorInfo &Other) const {
2589 return *this < Other.Enc;
2590 }
2591 bool operator<(const char *Peek) const {
2592 return Enc[0] < Peek[0] || (Enc[0] == Peek[0] && Enc[1] < Peek[1]);
2593 }
2594 bool operator==(const char *Peek) const {
2595 return Enc[0] == Peek[0] && Enc[1] == Peek[1];
2596 }
2597 bool operator!=(const char *Peek) const { return !this->operator==(Peek); }
2598
2599 public:
2600 StringView getSymbol() const {
2601 StringView Res = Name;
2602 if (Kind < Unnameable) {
2603 assert(Res.startsWith("operator") &&
2604 "operator name does not start with 'operator'");
2605 Res = Res.dropFront(sizeof("operator") - 1);
2606 Res.consumeFront(' ');
2607 }
2608 return Res;
2609 }
2610 StringView getName() const { return Name; }
2611 OIKind getKind() const { return Kind; }
2612 bool getFlag() const { return Flag; }
2613 };
2614 const OperatorInfo *parseOperatorEncoding();
2615
Richard Smithc20d1442018-08-20 20:14:49 +00002616 /// Parse the <unresolved-name> production.
Nathan Sidwell77c52e22022-01-28 11:59:03 -08002617 Node *parseUnresolvedName(bool Global);
Richard Smithc20d1442018-08-20 20:14:49 +00002618 Node *parseSimpleId();
2619 Node *parseBaseUnresolvedName();
2620 Node *parseUnresolvedType();
2621 Node *parseDestructorName();
2622
2623 /// Top-level entry point into the parser.
2624 Node *parse();
2625};
2626
2627const char* parse_discriminator(const char* first, const char* last);
2628
2629// <name> ::= <nested-name> // N
2630// ::= <local-name> # See Scope Encoding below // Z
2631// ::= <unscoped-template-name> <template-args>
2632// ::= <unscoped-name>
2633//
2634// <unscoped-template-name> ::= <unscoped-name>
2635// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002636template <typename Derived, typename Alloc>
2637Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002638 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002639 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002640 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002641 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002642
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002643 Node *Result = nullptr;
2644 bool IsSubst = look() == 'S' && look(1) != 't';
2645 if (IsSubst) {
2646 // A substitution must lead to:
2647 // ::= <unscoped-template-name> <template-args>
2648 Result = getDerived().parseSubstitution();
2649 } else {
2650 // An unscoped name can be one of:
2651 // ::= <unscoped-name>
2652 // ::= <unscoped-template-name> <template-args>
2653 Result = getDerived().parseUnscopedName(State);
2654 }
2655 if (Result == nullptr)
2656 return nullptr;
2657
2658 if (look() == 'I') {
2659 // ::= <unscoped-template-name> <template-args>
2660 if (!IsSubst)
2661 // An unscoped-template-name is substitutable.
2662 Subs.push_back(Result);
Pavel Labathba825192018-10-16 14:29:14 +00002663 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002664 if (TA == nullptr)
2665 return nullptr;
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002666 if (State)
2667 State->EndsWithTemplateArgs = true;
2668 Result = make<NameWithTemplateArgs>(Result, TA);
2669 } else if (IsSubst) {
2670 // The substitution case must be followed by <template-args>.
2671 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002672 }
2673
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002674 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00002675}
2676
2677// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2678// := Z <function encoding> E s [<discriminator>]
2679// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002680template <typename Derived, typename Alloc>
2681Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002682 if (!consumeIf('Z'))
2683 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002684 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002685 if (Encoding == nullptr || !consumeIf('E'))
2686 return nullptr;
2687
2688 if (consumeIf('s')) {
2689 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002690 auto *StringLitName = make<NameType>("string literal");
2691 if (!StringLitName)
2692 return nullptr;
2693 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002694 }
2695
2696 if (consumeIf('d')) {
2697 parseNumber(true);
2698 if (!consumeIf('_'))
2699 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002700 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002701 if (N == nullptr)
2702 return nullptr;
2703 return make<LocalName>(Encoding, N);
2704 }
2705
Pavel Labathba825192018-10-16 14:29:14 +00002706 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002707 if (Entity == nullptr)
2708 return nullptr;
2709 First = parse_discriminator(First, Last);
2710 return make<LocalName>(Encoding, Entity);
2711}
2712
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08002713// <unscoped-name> ::= [L]* <unqualified-name>
2714// ::= St [L]* <unqualified-name> # ::std::
2715// [*] extension
Pavel Labathba825192018-10-16 14:29:14 +00002716template <typename Derived, typename Alloc>
2717Node *
2718AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002719 Node *Std = nullptr;
2720 if (consumeIf("St")) {
2721 Std = make<NameType>("std");
2722 if (Std == nullptr)
Nathan Sidwell200e97c2022-01-21 11:37:01 -08002723 return nullptr;
2724 }
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002725 consumeIf('L');
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08002726
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002727 return getDerived().parseUnqualifiedName(State, Std);
Richard Smithc20d1442018-08-20 20:14:49 +00002728}
2729
2730// <unqualified-name> ::= <operator-name> [abi-tags]
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002731// ::= <ctor-dtor-name> [<abi-tags>]
2732// ::= <source-name> [<abi-tags>]
2733// ::= <unnamed-type-name> [<abi-tags>]
Richard Smithc20d1442018-08-20 20:14:49 +00002734// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002735template <typename Derived, typename Alloc>
2736Node *
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002737AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State,
2738 Node *Scope) {
Richard Smithc20d1442018-08-20 20:14:49 +00002739 Node *Result;
2740 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002741 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002742 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002743 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002744 else if (consumeIf("DC")) {
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002745 // Structured binding
Richard Smithc20d1442018-08-20 20:14:49 +00002746 size_t BindingsBegin = Names.size();
2747 do {
Pavel Labathba825192018-10-16 14:29:14 +00002748 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002749 if (Binding == nullptr)
2750 return nullptr;
2751 Names.push_back(Binding);
2752 } while (!consumeIf('E'));
2753 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002754 } else if (look() == 'C' || look() == 'D') {
2755 // A <ctor-dtor-name>.
2756 if (Scope == nullptr)
2757 return nullptr;
2758 Result = getDerived().parseCtorDtorName(Scope, State);
2759 } else {
Pavel Labathba825192018-10-16 14:29:14 +00002760 Result = getDerived().parseOperatorName(State);
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002761 }
Richard Smithc20d1442018-08-20 20:14:49 +00002762 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002763 Result = getDerived().parseAbiTags(Result);
Nathan Sidwell9a29c972022-01-25 12:23:31 -08002764 if (Result != nullptr && Scope != nullptr)
2765 Result = make<NestedName>(Scope, Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002766 return Result;
2767}
2768
2769// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2770// ::= <closure-type-name>
2771//
2772// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2773//
2774// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002775template <typename Derived, typename Alloc>
2776Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002777AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2778 // <template-params> refer to the innermost <template-args>. Clear out any
2779 // outer args that we may have inserted into TemplateParams.
2780 if (State != nullptr)
2781 TemplateParams.clear();
2782
Richard Smithc20d1442018-08-20 20:14:49 +00002783 if (consumeIf("Ut")) {
2784 StringView Count = parseNumber();
2785 if (!consumeIf('_'))
2786 return nullptr;
2787 return make<UnnamedTypeName>(Count);
2788 }
2789 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002790 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2791 TemplateParams.size());
2792 ScopedTemplateParamList LambdaTemplateParams(this);
2793
2794 size_t ParamsBegin = Names.size();
2795 while (look() == 'T' &&
2796 StringView("yptn").find(look(1)) != StringView::npos) {
2797 Node *T = parseTemplateParamDecl();
2798 if (!T)
2799 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002800 Names.push_back(T);
2801 }
2802 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2803
2804 // FIXME: If TempParams is empty and none of the function parameters
2805 // includes 'auto', we should remove LambdaTemplateParams from the
2806 // TemplateParams list. Unfortunately, we don't find out whether there are
2807 // any 'auto' parameters until too late in an example such as:
2808 //
2809 // template<typename T> void f(
2810 // decltype([](decltype([]<typename T>(T v) {}),
2811 // auto) {})) {}
2812 // template<typename T> void f(
2813 // decltype([](decltype([]<typename T>(T w) {}),
2814 // int) {})) {}
2815 //
2816 // Here, the type of v is at level 2 but the type of w is at level 1. We
2817 // don't find this out until we encounter the type of the next parameter.
2818 //
2819 // However, compilers can't actually cope with the former example in
2820 // practice, and it's likely to be made ill-formed in future, so we don't
2821 // need to support it here.
2822 //
2823 // If we encounter an 'auto' in the function parameter types, we will
2824 // recreate a template parameter scope for it, but any intervening lambdas
2825 // will be parsed in the 'wrong' template parameter depth.
2826 if (TempParams.empty())
2827 TemplateParams.pop_back();
2828
Richard Smithc20d1442018-08-20 20:14:49 +00002829 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002830 do {
Pavel Labathba825192018-10-16 14:29:14 +00002831 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002832 if (P == nullptr)
2833 return nullptr;
2834 Names.push_back(P);
2835 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002836 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002837 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2838
Richard Smithc20d1442018-08-20 20:14:49 +00002839 StringView Count = parseNumber();
2840 if (!consumeIf('_'))
2841 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002842 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002843 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002844 if (consumeIf("Ub")) {
2845 (void)parseNumber();
2846 if (!consumeIf('_'))
2847 return nullptr;
2848 return make<NameType>("'block-literal'");
2849 }
Richard Smithc20d1442018-08-20 20:14:49 +00002850 return nullptr;
2851}
2852
2853// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002854template <typename Derived, typename Alloc>
2855Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002856 size_t Length = 0;
2857 if (parsePositiveInteger(&Length))
2858 return nullptr;
2859 if (numLeft() < Length || Length == 0)
2860 return nullptr;
2861 StringView Name(First, First + Length);
2862 First += Length;
2863 if (Name.startsWith("_GLOBAL__N"))
2864 return make<NameType>("(anonymous namespace)");
2865 return make<NameType>(Name);
2866}
2867
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08002868// If the next 2 chars are an operator encoding, consume them and return their
2869// OperatorInfo. Otherwise return nullptr.
2870template <typename Derived, typename Alloc>
2871const typename AbstractManglingParser<Derived, Alloc>::OperatorInfo *
2872AbstractManglingParser<Derived, Alloc>::parseOperatorEncoding() {
2873 static const OperatorInfo Ops[] = {
2874 // Keep ordered by encoding
2875 {"aN", OperatorInfo::Binary, false, "operator&="},
2876 {"aS", OperatorInfo::Binary, false, "operator="},
2877 {"aa", OperatorInfo::Binary, false, "operator&&"},
2878 {"ad", OperatorInfo::Prefix, false, "operator&"},
2879 {"an", OperatorInfo::Binary, false, "operator&"},
2880 {"at", OperatorInfo::OfIdOp, /*Type*/ true, "alignof ("},
Nathan Sidwell0dda3d42022-02-18 09:51:24 -08002881 {"aw", OperatorInfo::NameOnly, false, "operator co_await"},
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08002882 {"az", OperatorInfo::OfIdOp, /*Type*/ false, "alignof ("},
2883 {"cc", OperatorInfo::NamedCast, false, "const_cast"},
2884 {"cl", OperatorInfo::Call, false, "operator()"},
2885 {"cm", OperatorInfo::Binary, false, "operator,"},
2886 {"co", OperatorInfo::Prefix, false, "operator~"},
2887 {"cv", OperatorInfo::CCast, false, "operator"}, // C Cast
2888 {"dV", OperatorInfo::Binary, false, "operator/="},
2889 {"da", OperatorInfo::Del, /*Ary*/ true, "operator delete[]"},
2890 {"dc", OperatorInfo::NamedCast, false, "dynamic_cast"},
2891 {"de", OperatorInfo::Prefix, false, "operator*"},
2892 {"dl", OperatorInfo::Del, /*Ary*/ false, "operator delete"},
2893 {"ds", OperatorInfo::Member, /*Named*/ false, "operator.*"},
2894 {"dt", OperatorInfo::Member, /*Named*/ false, "operator."},
2895 {"dv", OperatorInfo::Binary, false, "operator/"},
2896 {"eO", OperatorInfo::Binary, false, "operator^="},
2897 {"eo", OperatorInfo::Binary, false, "operator^"},
2898 {"eq", OperatorInfo::Binary, false, "operator=="},
2899 {"ge", OperatorInfo::Binary, false, "operator>="},
2900 {"gt", OperatorInfo::Binary, false, "operator>"},
2901 {"ix", OperatorInfo::Array, false, "operator[]"},
2902 {"lS", OperatorInfo::Binary, false, "operator<<="},
2903 {"le", OperatorInfo::Binary, false, "operator<="},
2904 {"ls", OperatorInfo::Binary, false, "operator<<"},
2905 {"lt", OperatorInfo::Binary, false, "operator<"},
2906 {"mI", OperatorInfo::Binary, false, "operator-="},
2907 {"mL", OperatorInfo::Binary, false, "operator*="},
2908 {"mi", OperatorInfo::Binary, false, "operator-"},
2909 {"ml", OperatorInfo::Binary, false, "operator*"},
2910 {"mm", OperatorInfo::Postfix, false, "operator--"},
2911 {"na", OperatorInfo::New, /*Ary*/ true, "operator new[]"},
2912 {"ne", OperatorInfo::Binary, false, "operator!="},
2913 {"ng", OperatorInfo::Prefix, false, "operator-"},
2914 {"nt", OperatorInfo::Prefix, false, "operator!"},
2915 {"nw", OperatorInfo::New, /*Ary*/ false, "operator new"},
2916 {"oR", OperatorInfo::Binary, false, "operator|="},
2917 {"oo", OperatorInfo::Binary, false, "operator||"},
2918 {"or", OperatorInfo::Binary, false, "operator|"},
2919 {"pL", OperatorInfo::Binary, false, "operator+="},
2920 {"pl", OperatorInfo::Binary, false, "operator+"},
2921 {"pm", OperatorInfo::Member, /*Named*/ false, "operator->*"},
2922 {"pp", OperatorInfo::Postfix, false, "operator++"},
2923 {"ps", OperatorInfo::Prefix, false, "operator+"},
2924 {"pt", OperatorInfo::Member, /*Named*/ true, "operator->"},
2925 {"qu", OperatorInfo::Conditional, false, "operator?"},
2926 {"rM", OperatorInfo::Binary, false, "operator%="},
2927 {"rS", OperatorInfo::Binary, false, "operator>>="},
2928 {"rc", OperatorInfo::NamedCast, false, "reinterpret_cast"},
2929 {"rm", OperatorInfo::Binary, false, "operator%"},
2930 {"rs", OperatorInfo::Binary, false, "operator>>"},
2931 {"sc", OperatorInfo::NamedCast, false, "static_cast"},
2932 {"ss", OperatorInfo::Binary, false, "operator<=>"},
2933 {"st", OperatorInfo::OfIdOp, /*Type*/ true, "sizeof ("},
2934 {"sz", OperatorInfo::OfIdOp, /*Type*/ false, "sizeof ("},
2935 {"te", OperatorInfo::OfIdOp, /*Type*/ false, "typeid ("},
2936 {"ti", OperatorInfo::OfIdOp, /*Type*/ true, "typeid ("},
2937 };
2938 const auto NumOps = sizeof(Ops) / sizeof(Ops[0]);
2939
2940#ifndef NDEBUG
2941 {
2942 // Verify table order.
2943 static bool Done;
2944 if (!Done) {
2945 Done = true;
2946 for (const auto *Op = &Ops[0]; Op != &Ops[NumOps - 1]; Op++)
2947 assert(Op[0] < Op[1] && "Operator table is not ordered");
2948 }
2949 }
2950#endif
2951
2952 if (numLeft() < 2)
2953 return nullptr;
2954
2955 auto Op = std::lower_bound(
2956 &Ops[0], &Ops[NumOps], First,
2957 [](const OperatorInfo &Op_, const char *Enc_) { return Op_ < Enc_; });
2958 if (Op == &Ops[NumOps] || *Op != First)
2959 return nullptr;
2960
2961 First += 2;
2962 return Op;
2963}
2964
2965// <operator-name> ::= See parseOperatorEncoding()
Richard Smithc20d1442018-08-20 20:14:49 +00002966// ::= li <source-name> # operator ""
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08002967// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002968template <typename Derived, typename Alloc>
2969Node *
2970AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08002971 if (const auto *Op = parseOperatorEncoding()) {
2972 if (Op->getKind() == OperatorInfo::CCast) {
2973 // ::= cv <type> # (cast)
Richard Smithc20d1442018-08-20 20:14:49 +00002974 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2975 // If we're parsing an encoding, State != nullptr and the conversion
2976 // operators' <type> could have a <template-param> that refers to some
2977 // <template-arg>s further ahead in the mangled name.
2978 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2979 PermitForwardTemplateReferences ||
2980 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002981 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002982 if (Ty == nullptr)
2983 return nullptr;
2984 if (State) State->CtorDtorConversion = true;
2985 return make<ConversionOperatorType>(Ty);
2986 }
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08002987
2988 if (Op->getKind() >= OperatorInfo::Unnameable)
2989 /* Not a nameable operator. */
2990 return nullptr;
2991 if (Op->getKind() == OperatorInfo::Member && !Op->getFlag())
2992 /* Not a nameable MemberExpr */
2993 return nullptr;
2994
2995 return make<NameType>(Op->getName());
2996 }
2997
2998 if (consumeIf("li")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002999 // ::= li <source-name> # operator ""
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08003000 Node *SN = getDerived().parseSourceName(State);
3001 if (SN == nullptr)
3002 return nullptr;
3003 return make<LiteralOperator>(SN);
3004 }
3005
3006 if (consumeIf('v')) {
3007 // ::= v <digit> <source-name> # vendor extended operator
3008 if (look() >= '0' && look() <= '9') {
3009 First++;
Pavel Labathba825192018-10-16 14:29:14 +00003010 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003011 if (SN == nullptr)
3012 return nullptr;
3013 return make<ConversionOperatorType>(SN);
3014 }
3015 return nullptr;
3016 }
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08003017
Richard Smithc20d1442018-08-20 20:14:49 +00003018 return nullptr;
3019}
3020
3021// <ctor-dtor-name> ::= C1 # complete object constructor
3022// ::= C2 # base object constructor
3023// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003024// extension ::= C4 # gcc old-style "[unified]" constructor
3025// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003026// ::= D0 # deleting destructor
3027// ::= D1 # complete object destructor
3028// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003029// extension ::= D4 # gcc old-style "[unified]" destructor
3030// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003031template <typename Derived, typename Alloc>
3032Node *
3033AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3034 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003035 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3036 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3037 switch (SSK) {
3038 case SpecialSubKind::string:
3039 case SpecialSubKind::istream:
3040 case SpecialSubKind::ostream:
3041 case SpecialSubKind::iostream:
3042 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003043 if (!SoFar)
3044 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003045 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003046 default:
3047 break;
3048 }
3049 }
3050
3051 if (consumeIf('C')) {
3052 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003053 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3054 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003055 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003056 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003057 ++First;
3058 if (State) State->CtorDtorConversion = true;
3059 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003060 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003061 return nullptr;
3062 }
Nico Weber29294792019-04-03 23:14:33 +00003063 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003064 }
3065
Nico Weber29294792019-04-03 23:14:33 +00003066 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3067 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003068 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003069 First += 2;
3070 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003071 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003072 }
3073
3074 return nullptr;
3075}
3076
3077// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3078// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3079//
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003080// <prefix> ::= <prefix> [L]* <unqualified-name>
Richard Smithc20d1442018-08-20 20:14:49 +00003081// ::= <template-prefix> <template-args>
3082// ::= <template-param>
3083// ::= <decltype>
3084// ::= # empty
3085// ::= <substitution>
3086// ::= <prefix> <data-member-prefix>
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003087// [*] extension
Richard Smithc20d1442018-08-20 20:14:49 +00003088//
3089// <data-member-prefix> := <member source-name> [<template-args>] M
3090//
3091// <template-prefix> ::= <prefix> <template unqualified-name>
3092// ::= <template-param>
3093// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003094template <typename Derived, typename Alloc>
3095Node *
3096AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003097 if (!consumeIf('N'))
3098 return nullptr;
3099
3100 Qualifiers CVTmp = parseCVQualifiers();
3101 if (State) State->CVQualifiers = CVTmp;
3102
3103 if (consumeIf('O')) {
3104 if (State) State->ReferenceQualifier = FrefQualRValue;
3105 } else if (consumeIf('R')) {
3106 if (State) State->ReferenceQualifier = FrefQualLValue;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003107 } else {
Richard Smithc20d1442018-08-20 20:14:49 +00003108 if (State) State->ReferenceQualifier = FrefQualNone;
Richard Smithb485b352018-08-24 23:30:26 +00003109 }
Richard Smithc20d1442018-08-20 20:14:49 +00003110
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003111 Node *SoFar = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003112 while (!consumeIf('E')) {
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003113 if (State)
3114 // Only set end-with-template on the case that does that.
3115 State->EndsWithTemplateArgs = false;
3116
Richard Smithc20d1442018-08-20 20:14:49 +00003117 if (look() == 'T') {
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003118 // ::= <template-param>
3119 if (SoFar != nullptr)
3120 return nullptr; // Cannot have a prefix.
3121 SoFar = getDerived().parseTemplateParam();
3122 } else if (look() == 'I') {
3123 // ::= <template-prefix> <template-args>
3124 if (SoFar == nullptr)
3125 return nullptr; // Must have a prefix.
Pavel Labathba825192018-10-16 14:29:14 +00003126 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003127 if (TA == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003128 return nullptr;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003129 if (SoFar->getKind() == Node::KNameWithTemplateArgs)
3130 // Semantically <template-args> <template-args> cannot be generated by a
3131 // C++ entity. There will always be [something like] a name between
3132 // them.
3133 return nullptr;
3134 if (State)
3135 State->EndsWithTemplateArgs = true;
Richard Smithc20d1442018-08-20 20:14:49 +00003136 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003137 } else if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
3138 // ::= <decltype>
3139 if (SoFar != nullptr)
3140 return nullptr; // Cannot have a prefix.
3141 SoFar = getDerived().parseDecltype();
3142 } else if (look() == 'S') {
3143 // ::= <substitution>
3144 if (SoFar != nullptr)
3145 return nullptr; // Cannot have a prefix.
3146 if (look(1) == 't') {
3147 // parseSubstition does not handle 'St'.
3148 First += 2;
3149 SoFar = make<NameType>("std");
3150 } else {
3151 SoFar = getDerived().parseSubstitution();
3152 }
Richard Smithc20d1442018-08-20 20:14:49 +00003153 if (SoFar == nullptr)
3154 return nullptr;
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003155 continue; // Do not push a new substitution.
3156 } else {
Nathan Sidwelle6545292022-01-25 12:31:01 -08003157 consumeIf('L'); // extension
Nathan Sidwell9a29c972022-01-25 12:23:31 -08003158 // ::= [<prefix>] <unqualified-name>
3159 SoFar = getDerived().parseUnqualifiedName(State, SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00003160 }
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08003161 if (SoFar == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003162 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003163 Subs.push_back(SoFar);
Nathan Sidwelle6545292022-01-25 12:31:01 -08003164
3165 // No longer used.
3166 // <data-member-prefix> := <member source-name> [<template-args>] M
3167 consumeIf('M');
Richard Smithc20d1442018-08-20 20:14:49 +00003168 }
3169
3170 if (SoFar == nullptr || Subs.empty())
3171 return nullptr;
3172
3173 Subs.pop_back();
3174 return SoFar;
3175}
3176
3177// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003178template <typename Derived, typename Alloc>
3179Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3180 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003181 if (SN == nullptr)
3182 return nullptr;
3183 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003184 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003185 if (TA == nullptr)
3186 return nullptr;
3187 return make<NameWithTemplateArgs>(SN, TA);
3188 }
3189 return SN;
3190}
3191
3192// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3193// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003194template <typename Derived, typename Alloc>
3195Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003196 Node *Result;
3197 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003198 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003199 else
Pavel Labathba825192018-10-16 14:29:14 +00003200 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003201 if (Result == nullptr)
3202 return nullptr;
3203 return make<DtorName>(Result);
3204}
3205
3206// <unresolved-type> ::= <template-param>
3207// ::= <decltype>
3208// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003209template <typename Derived, typename Alloc>
3210Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003211 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003212 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003213 if (TP == nullptr)
3214 return nullptr;
3215 Subs.push_back(TP);
3216 return TP;
3217 }
3218 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003219 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003220 if (DT == nullptr)
3221 return nullptr;
3222 Subs.push_back(DT);
3223 return DT;
3224 }
Pavel Labathba825192018-10-16 14:29:14 +00003225 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003226}
3227
3228// <base-unresolved-name> ::= <simple-id> # unresolved name
3229// extension ::= <operator-name> # unresolved operator-function-id
3230// extension ::= <operator-name> <template-args> # unresolved operator template-id
3231// ::= on <operator-name> # unresolved operator-function-id
3232// ::= on <operator-name> <template-args> # unresolved operator template-id
3233// ::= dn <destructor-name> # destructor or pseudo-destructor;
3234// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003235template <typename Derived, typename Alloc>
3236Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003237 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003238 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003239
3240 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003241 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003242
3243 consumeIf("on");
3244
Pavel Labathba825192018-10-16 14:29:14 +00003245 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003246 if (Oper == nullptr)
3247 return nullptr;
3248 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003249 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003250 if (TA == nullptr)
3251 return nullptr;
3252 return make<NameWithTemplateArgs>(Oper, TA);
3253 }
3254 return Oper;
3255}
3256
3257// <unresolved-name>
3258// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3259// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3260// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3261// # A::x, N::y, A<T>::z; "gs" means leading "::"
Nathan Sidwell77c52e22022-01-28 11:59:03 -08003262// [gs] has been parsed by caller.
Richard Smithc20d1442018-08-20 20:14:49 +00003263// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3264// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3265// # T::N::x /decltype(p)::N::x
3266// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3267//
3268// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003269template <typename Derived, typename Alloc>
Nathan Sidwell77c52e22022-01-28 11:59:03 -08003270Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName(bool Global) {
Richard Smithc20d1442018-08-20 20:14:49 +00003271 Node *SoFar = nullptr;
3272
3273 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3274 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3275 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003276 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003277 if (SoFar == nullptr)
3278 return nullptr;
3279
3280 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003281 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003282 if (TA == nullptr)
3283 return nullptr;
3284 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003285 if (!SoFar)
3286 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003287 }
3288
3289 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003290 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003291 if (Qual == nullptr)
3292 return nullptr;
3293 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003294 if (!SoFar)
3295 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003296 }
3297
Pavel Labathba825192018-10-16 14:29:14 +00003298 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003299 if (Base == nullptr)
3300 return nullptr;
3301 return make<QualifiedName>(SoFar, Base);
3302 }
3303
Richard Smithc20d1442018-08-20 20:14:49 +00003304 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3305 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003306 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003307 if (SoFar == nullptr)
3308 return nullptr;
3309 if (Global)
3310 SoFar = make<GlobalQualifiedName>(SoFar);
3311 return SoFar;
3312 }
3313
3314 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3315 if (std::isdigit(look())) {
3316 do {
Pavel Labathba825192018-10-16 14:29:14 +00003317 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003318 if (Qual == nullptr)
3319 return nullptr;
3320 if (SoFar)
3321 SoFar = make<QualifiedName>(SoFar, Qual);
3322 else if (Global)
3323 SoFar = make<GlobalQualifiedName>(Qual);
3324 else
3325 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003326 if (!SoFar)
3327 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003328 } while (!consumeIf('E'));
3329 }
3330 // sr <unresolved-type> <base-unresolved-name>
3331 // sr <unresolved-type> <template-args> <base-unresolved-name>
3332 else {
Pavel Labathba825192018-10-16 14:29:14 +00003333 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003334 if (SoFar == nullptr)
3335 return nullptr;
3336
3337 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003338 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003339 if (TA == nullptr)
3340 return nullptr;
3341 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003342 if (!SoFar)
3343 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003344 }
3345 }
3346
3347 assert(SoFar != nullptr);
3348
Pavel Labathba825192018-10-16 14:29:14 +00003349 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003350 if (Base == nullptr)
3351 return nullptr;
3352 return make<QualifiedName>(SoFar, Base);
3353}
3354
3355// <abi-tags> ::= <abi-tag> [<abi-tags>]
3356// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003357template <typename Derived, typename Alloc>
3358Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003359 while (consumeIf('B')) {
3360 StringView SN = parseBareSourceName();
3361 if (SN.empty())
3362 return nullptr;
3363 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003364 if (!N)
3365 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003366 }
3367 return N;
3368}
3369
3370// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003371template <typename Alloc, typename Derived>
3372StringView
3373AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003374 const char *Tmp = First;
3375 if (AllowNegative)
3376 consumeIf('n');
3377 if (numLeft() == 0 || !std::isdigit(*First))
3378 return StringView();
3379 while (numLeft() != 0 && std::isdigit(*First))
3380 ++First;
3381 return StringView(Tmp, First);
3382}
3383
3384// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003385template <typename Alloc, typename Derived>
3386bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003387 *Out = 0;
3388 if (look() < '0' || look() > '9')
3389 return true;
3390 while (look() >= '0' && look() <= '9') {
3391 *Out *= 10;
3392 *Out += static_cast<size_t>(consume() - '0');
3393 }
3394 return false;
3395}
3396
Pavel Labathba825192018-10-16 14:29:14 +00003397template <typename Alloc, typename Derived>
3398StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003399 size_t Int = 0;
3400 if (parsePositiveInteger(&Int) || numLeft() < Int)
3401 return StringView();
3402 StringView R(First, First + Int);
3403 First += Int;
3404 return R;
3405}
3406
3407// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3408//
3409// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3410// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3411// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3412//
3413// <ref-qualifier> ::= R # & ref-qualifier
3414// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003415template <typename Derived, typename Alloc>
3416Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003417 Qualifiers CVQuals = parseCVQualifiers();
3418
3419 Node *ExceptionSpec = nullptr;
3420 if (consumeIf("Do")) {
3421 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003422 if (!ExceptionSpec)
3423 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003424 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003425 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003426 if (E == nullptr || !consumeIf('E'))
3427 return nullptr;
3428 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003429 if (!ExceptionSpec)
3430 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003431 } else if (consumeIf("Dw")) {
3432 size_t SpecsBegin = Names.size();
3433 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003434 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003435 if (T == nullptr)
3436 return nullptr;
3437 Names.push_back(T);
3438 }
3439 ExceptionSpec =
3440 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003441 if (!ExceptionSpec)
3442 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003443 }
3444
3445 consumeIf("Dx"); // transaction safe
3446
3447 if (!consumeIf('F'))
3448 return nullptr;
3449 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003450 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003451 if (ReturnType == nullptr)
3452 return nullptr;
3453
3454 FunctionRefQual ReferenceQualifier = FrefQualNone;
3455 size_t ParamsBegin = Names.size();
3456 while (true) {
3457 if (consumeIf('E'))
3458 break;
3459 if (consumeIf('v'))
3460 continue;
3461 if (consumeIf("RE")) {
3462 ReferenceQualifier = FrefQualLValue;
3463 break;
3464 }
3465 if (consumeIf("OE")) {
3466 ReferenceQualifier = FrefQualRValue;
3467 break;
3468 }
Pavel Labathba825192018-10-16 14:29:14 +00003469 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003470 if (T == nullptr)
3471 return nullptr;
3472 Names.push_back(T);
3473 }
3474
3475 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3476 return make<FunctionType>(ReturnType, Params, CVQuals,
3477 ReferenceQualifier, ExceptionSpec);
3478}
3479
3480// extension:
3481// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3482// ::= Dv [<dimension expression>] _ <element type>
3483// <extended element type> ::= <element type>
3484// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003485template <typename Derived, typename Alloc>
3486Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003487 if (!consumeIf("Dv"))
3488 return nullptr;
3489 if (look() >= '1' && look() <= '9') {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003490 Node *DimensionNumber = make<NameType>(parseNumber());
3491 if (!DimensionNumber)
3492 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003493 if (!consumeIf('_'))
3494 return nullptr;
3495 if (consumeIf('p'))
3496 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003497 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003498 if (ElemType == nullptr)
3499 return nullptr;
3500 return make<VectorType>(ElemType, DimensionNumber);
3501 }
3502
3503 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003504 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003505 if (!DimExpr)
3506 return nullptr;
3507 if (!consumeIf('_'))
3508 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003509 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003510 if (!ElemType)
3511 return nullptr;
3512 return make<VectorType>(ElemType, DimExpr);
3513 }
Pavel Labathba825192018-10-16 14:29:14 +00003514 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003515 if (!ElemType)
3516 return nullptr;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003517 return make<VectorType>(ElemType, /*Dimension=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003518}
3519
3520// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3521// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003522template <typename Derived, typename Alloc>
3523Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003524 if (!consumeIf('D'))
3525 return nullptr;
3526 if (!consumeIf('t') && !consumeIf('T'))
3527 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003528 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003529 if (E == nullptr)
3530 return nullptr;
3531 if (!consumeIf('E'))
3532 return nullptr;
3533 return make<EnclosingExpr>("decltype(", E, ")");
3534}
3535
3536// <array-type> ::= A <positive dimension number> _ <element type>
3537// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003538template <typename Derived, typename Alloc>
3539Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003540 if (!consumeIf('A'))
3541 return nullptr;
3542
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003543 Node *Dimension = nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003544
Richard Smithc20d1442018-08-20 20:14:49 +00003545 if (std::isdigit(look())) {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003546 Dimension = make<NameType>(parseNumber());
3547 if (!Dimension)
3548 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003549 if (!consumeIf('_'))
3550 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003551 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003552 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003553 if (DimExpr == nullptr)
3554 return nullptr;
3555 if (!consumeIf('_'))
3556 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003557 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003558 }
3559
Pavel Labathba825192018-10-16 14:29:14 +00003560 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003561 if (Ty == nullptr)
3562 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003563 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003564}
3565
3566// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003567template <typename Derived, typename Alloc>
3568Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003569 if (!consumeIf('M'))
3570 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003571 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003572 if (ClassType == nullptr)
3573 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003574 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003575 if (MemberType == nullptr)
3576 return nullptr;
3577 return make<PointerToMemberType>(ClassType, MemberType);
3578}
3579
3580// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3581// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3582// ::= Tu <name> # dependent elaborated type specifier using 'union'
3583// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003584template <typename Derived, typename Alloc>
3585Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003586 StringView ElabSpef;
3587 if (consumeIf("Ts"))
3588 ElabSpef = "struct";
3589 else if (consumeIf("Tu"))
3590 ElabSpef = "union";
3591 else if (consumeIf("Te"))
3592 ElabSpef = "enum";
3593
Pavel Labathba825192018-10-16 14:29:14 +00003594 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003595 if (Name == nullptr)
3596 return nullptr;
3597
3598 if (!ElabSpef.empty())
3599 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3600
3601 return Name;
3602}
3603
3604// <qualified-type> ::= <qualifiers> <type>
3605// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3606// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003607template <typename Derived, typename Alloc>
3608Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003609 if (consumeIf('U')) {
3610 StringView Qual = parseBareSourceName();
3611 if (Qual.empty())
3612 return nullptr;
3613
Richard Smithc20d1442018-08-20 20:14:49 +00003614 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3615 if (Qual.startsWith("objcproto")) {
3616 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3617 StringView Proto;
3618 {
3619 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3620 SaveLast(Last, ProtoSourceName.end());
3621 Proto = parseBareSourceName();
3622 }
3623 if (Proto.empty())
3624 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003625 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003626 if (Child == nullptr)
3627 return nullptr;
3628 return make<ObjCProtoName>(Child, Proto);
3629 }
3630
Alex Orlovf50df922021-03-24 10:21:32 +04003631 Node *TA = nullptr;
3632 if (look() == 'I') {
3633 TA = getDerived().parseTemplateArgs();
3634 if (TA == nullptr)
3635 return nullptr;
3636 }
3637
Pavel Labathba825192018-10-16 14:29:14 +00003638 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003639 if (Child == nullptr)
3640 return nullptr;
Alex Orlovf50df922021-03-24 10:21:32 +04003641 return make<VendorExtQualType>(Child, Qual, TA);
Richard Smithc20d1442018-08-20 20:14:49 +00003642 }
3643
3644 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003645 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003646 if (Ty == nullptr)
3647 return nullptr;
3648 if (Quals != QualNone)
3649 Ty = make<QualType>(Ty, Quals);
3650 return Ty;
3651}
3652
3653// <type> ::= <builtin-type>
3654// ::= <qualified-type>
3655// ::= <function-type>
3656// ::= <class-enum-type>
3657// ::= <array-type>
3658// ::= <pointer-to-member-type>
3659// ::= <template-param>
3660// ::= <template-template-param> <template-args>
3661// ::= <decltype>
3662// ::= P <type> # pointer
3663// ::= R <type> # l-value reference
3664// ::= O <type> # r-value reference (C++11)
3665// ::= C <type> # complex pair (C99)
3666// ::= G <type> # imaginary (C99)
3667// ::= <substitution> # See Compression below
3668// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3669// extension ::= <vector-type> # <vector-type> starts with Dv
3670//
3671// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3672// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003673template <typename Derived, typename Alloc>
3674Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003675 Node *Result = nullptr;
3676
Richard Smithc20d1442018-08-20 20:14:49 +00003677 switch (look()) {
3678 // ::= <qualified-type>
3679 case 'r':
3680 case 'V':
3681 case 'K': {
3682 unsigned AfterQuals = 0;
3683 if (look(AfterQuals) == 'r') ++AfterQuals;
3684 if (look(AfterQuals) == 'V') ++AfterQuals;
3685 if (look(AfterQuals) == 'K') ++AfterQuals;
3686
3687 if (look(AfterQuals) == 'F' ||
3688 (look(AfterQuals) == 'D' &&
3689 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3690 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003691 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003692 break;
3693 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003694 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003695 }
3696 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003697 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003698 break;
3699 }
3700 // <builtin-type> ::= v # void
3701 case 'v':
3702 ++First;
3703 return make<NameType>("void");
3704 // ::= w # wchar_t
3705 case 'w':
3706 ++First;
3707 return make<NameType>("wchar_t");
3708 // ::= b # bool
3709 case 'b':
3710 ++First;
3711 return make<NameType>("bool");
3712 // ::= c # char
3713 case 'c':
3714 ++First;
3715 return make<NameType>("char");
3716 // ::= a # signed char
3717 case 'a':
3718 ++First;
3719 return make<NameType>("signed char");
3720 // ::= h # unsigned char
3721 case 'h':
3722 ++First;
3723 return make<NameType>("unsigned char");
3724 // ::= s # short
3725 case 's':
3726 ++First;
3727 return make<NameType>("short");
3728 // ::= t # unsigned short
3729 case 't':
3730 ++First;
3731 return make<NameType>("unsigned short");
3732 // ::= i # int
3733 case 'i':
3734 ++First;
3735 return make<NameType>("int");
3736 // ::= j # unsigned int
3737 case 'j':
3738 ++First;
3739 return make<NameType>("unsigned int");
3740 // ::= l # long
3741 case 'l':
3742 ++First;
3743 return make<NameType>("long");
3744 // ::= m # unsigned long
3745 case 'm':
3746 ++First;
3747 return make<NameType>("unsigned long");
3748 // ::= x # long long, __int64
3749 case 'x':
3750 ++First;
3751 return make<NameType>("long long");
3752 // ::= y # unsigned long long, __int64
3753 case 'y':
3754 ++First;
3755 return make<NameType>("unsigned long long");
3756 // ::= n # __int128
3757 case 'n':
3758 ++First;
3759 return make<NameType>("__int128");
3760 // ::= o # unsigned __int128
3761 case 'o':
3762 ++First;
3763 return make<NameType>("unsigned __int128");
3764 // ::= f # float
3765 case 'f':
3766 ++First;
3767 return make<NameType>("float");
3768 // ::= d # double
3769 case 'd':
3770 ++First;
3771 return make<NameType>("double");
3772 // ::= e # long double, __float80
3773 case 'e':
3774 ++First;
3775 return make<NameType>("long double");
3776 // ::= g # __float128
3777 case 'g':
3778 ++First;
3779 return make<NameType>("__float128");
3780 // ::= z # ellipsis
3781 case 'z':
3782 ++First;
3783 return make<NameType>("...");
3784
3785 // <builtin-type> ::= u <source-name> # vendor extended type
3786 case 'u': {
3787 ++First;
3788 StringView Res = parseBareSourceName();
3789 if (Res.empty())
3790 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003791 // Typically, <builtin-type>s are not considered substitution candidates,
3792 // but the exception to that exception is vendor extended types (Itanium C++
3793 // ABI 5.9.1).
3794 Result = make<NameType>(Res);
3795 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003796 }
3797 case 'D':
3798 switch (look(1)) {
3799 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3800 case 'd':
3801 First += 2;
3802 return make<NameType>("decimal64");
3803 // ::= De # IEEE 754r decimal floating point (128 bits)
3804 case 'e':
3805 First += 2;
3806 return make<NameType>("decimal128");
3807 // ::= Df # IEEE 754r decimal floating point (32 bits)
3808 case 'f':
3809 First += 2;
3810 return make<NameType>("decimal32");
3811 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3812 case 'h':
3813 First += 2;
Stuart Bradye8bf5772021-06-07 16:30:22 +01003814 return make<NameType>("half");
Pengfei Wang50e90b82021-09-23 11:02:25 +08003815 // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits)
3816 case 'F': {
3817 First += 2;
3818 Node *DimensionNumber = make<NameType>(parseNumber());
3819 if (!DimensionNumber)
3820 return nullptr;
3821 if (!consumeIf('_'))
3822 return nullptr;
3823 return make<BinaryFPType>(DimensionNumber);
3824 }
Richard Smithc20d1442018-08-20 20:14:49 +00003825 // ::= Di # char32_t
3826 case 'i':
3827 First += 2;
3828 return make<NameType>("char32_t");
3829 // ::= Ds # char16_t
3830 case 's':
3831 First += 2;
3832 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003833 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3834 case 'u':
3835 First += 2;
3836 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003837 // ::= Da # auto (in dependent new-expressions)
3838 case 'a':
3839 First += 2;
3840 return make<NameType>("auto");
3841 // ::= Dc # decltype(auto)
3842 case 'c':
3843 First += 2;
3844 return make<NameType>("decltype(auto)");
3845 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3846 case 'n':
3847 First += 2;
3848 return make<NameType>("std::nullptr_t");
3849
3850 // ::= <decltype>
3851 case 't':
3852 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003853 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003854 break;
3855 }
3856 // extension ::= <vector-type> # <vector-type> starts with Dv
3857 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003858 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003859 break;
3860 }
3861 // ::= Dp <type> # pack expansion (C++0x)
3862 case 'p': {
3863 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003864 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003865 if (!Child)
3866 return nullptr;
3867 Result = make<ParameterPackExpansion>(Child);
3868 break;
3869 }
3870 // Exception specifier on a function type.
3871 case 'o':
3872 case 'O':
3873 case 'w':
3874 // Transaction safe function type.
3875 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003876 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003877 break;
3878 }
3879 break;
3880 // ::= <function-type>
3881 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003882 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003883 break;
3884 }
3885 // ::= <array-type>
3886 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003887 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003888 break;
3889 }
3890 // ::= <pointer-to-member-type>
3891 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003892 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003893 break;
3894 }
3895 // ::= <template-param>
3896 case 'T': {
3897 // This could be an elaborate type specifier on a <class-enum-type>.
3898 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003899 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003900 break;
3901 }
3902
Pavel Labathba825192018-10-16 14:29:14 +00003903 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003904 if (Result == nullptr)
3905 return nullptr;
3906
3907 // Result could be either of:
3908 // <type> ::= <template-param>
3909 // <type> ::= <template-template-param> <template-args>
3910 //
3911 // <template-template-param> ::= <template-param>
3912 // ::= <substitution>
3913 //
3914 // If this is followed by some <template-args>, and we're permitted to
3915 // parse them, take the second production.
3916
3917 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003918 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003919 if (TA == nullptr)
3920 return nullptr;
3921 Result = make<NameWithTemplateArgs>(Result, TA);
3922 }
3923 break;
3924 }
3925 // ::= P <type> # pointer
3926 case 'P': {
3927 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003928 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003929 if (Ptr == nullptr)
3930 return nullptr;
3931 Result = make<PointerType>(Ptr);
3932 break;
3933 }
3934 // ::= R <type> # l-value reference
3935 case 'R': {
3936 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003937 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003938 if (Ref == nullptr)
3939 return nullptr;
3940 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
3941 break;
3942 }
3943 // ::= O <type> # r-value reference (C++11)
3944 case 'O': {
3945 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003946 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003947 if (Ref == nullptr)
3948 return nullptr;
3949 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
3950 break;
3951 }
3952 // ::= C <type> # complex pair (C99)
3953 case 'C': {
3954 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003955 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003956 if (P == nullptr)
3957 return nullptr;
3958 Result = make<PostfixQualifiedType>(P, " complex");
3959 break;
3960 }
3961 // ::= G <type> # imaginary (C99)
3962 case 'G': {
3963 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003964 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003965 if (P == nullptr)
3966 return P;
3967 Result = make<PostfixQualifiedType>(P, " imaginary");
3968 break;
3969 }
3970 // ::= <substitution> # See Compression below
3971 case 'S': {
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08003972 if (look(1) != 't') {
3973 Result = getDerived().parseSubstitution();
3974 if (Result == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003975 return nullptr;
3976
3977 // Sub could be either of:
3978 // <type> ::= <substitution>
3979 // <type> ::= <template-template-param> <template-args>
3980 //
3981 // <template-template-param> ::= <template-param>
3982 // ::= <substitution>
3983 //
3984 // If this is followed by some <template-args>, and we're permitted to
3985 // parse them, take the second production.
3986
3987 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003988 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003989 if (TA == nullptr)
3990 return nullptr;
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08003991 Result = make<NameWithTemplateArgs>(Result, TA);
3992 } else {
3993 // If all we parsed was a substitution, don't re-insert into the
3994 // substitution table.
3995 return Result;
Richard Smithc20d1442018-08-20 20:14:49 +00003996 }
Nathan Sidwelle4cc3532022-01-24 04:28:09 -08003997 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003998 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003999 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004000 }
4001 // ::= <class-enum-type>
4002 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004003 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004004 break;
4005 }
4006 }
4007
4008 // If we parsed a type, insert it into the substitution table. Note that all
4009 // <builtin-type>s and <substitution>s have already bailed out, because they
4010 // don't get substitutions.
4011 if (Result != nullptr)
4012 Subs.push_back(Result);
4013 return Result;
4014}
4015
Pavel Labathba825192018-10-16 14:29:14 +00004016template <typename Derived, typename Alloc>
4017Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4018 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004019 if (E == nullptr)
4020 return nullptr;
4021 return make<PrefixExpr>(Kind, E);
4022}
4023
Pavel Labathba825192018-10-16 14:29:14 +00004024template <typename Derived, typename Alloc>
4025Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4026 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004027 if (LHS == nullptr)
4028 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004029 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004030 if (RHS == nullptr)
4031 return nullptr;
4032 return make<BinaryExpr>(LHS, Kind, RHS);
4033}
4034
Pavel Labathba825192018-10-16 14:29:14 +00004035template <typename Derived, typename Alloc>
4036Node *
4037AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004038 StringView Tmp = parseNumber(true);
4039 if (!Tmp.empty() && consumeIf('E'))
4040 return make<IntegerLiteral>(Lit, Tmp);
4041 return nullptr;
4042}
4043
4044// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004045template <typename Alloc, typename Derived>
4046Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004047 Qualifiers CVR = QualNone;
4048 if (consumeIf('r'))
4049 CVR |= QualRestrict;
4050 if (consumeIf('V'))
4051 CVR |= QualVolatile;
4052 if (consumeIf('K'))
4053 CVR |= QualConst;
4054 return CVR;
4055}
4056
4057// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4058// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4059// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4060// ::= 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 -04004061// ::= fpT # 'this' expression (not part of standard?)
Pavel Labathba825192018-10-16 14:29:14 +00004062template <typename Derived, typename Alloc>
4063Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Erik Pilkington91c24af2020-05-13 22:19:45 -04004064 if (consumeIf("fpT"))
4065 return make<NameType>("this");
Richard Smithc20d1442018-08-20 20:14:49 +00004066 if (consumeIf("fp")) {
4067 parseCVQualifiers();
4068 StringView Num = parseNumber();
4069 if (!consumeIf('_'))
4070 return nullptr;
4071 return make<FunctionParam>(Num);
4072 }
4073 if (consumeIf("fL")) {
4074 if (parseNumber().empty())
4075 return nullptr;
4076 if (!consumeIf('p'))
4077 return nullptr;
4078 parseCVQualifiers();
4079 StringView Num = parseNumber();
4080 if (!consumeIf('_'))
4081 return nullptr;
4082 return make<FunctionParam>(Num);
4083 }
4084 return nullptr;
4085}
4086
Richard Smithc20d1442018-08-20 20:14:49 +00004087// cv <type> <expression> # conversion with one argument
4088// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004089template <typename Derived, typename Alloc>
4090Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004091 if (!consumeIf("cv"))
4092 return nullptr;
4093 Node *Ty;
4094 {
4095 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004096 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004097 }
4098
4099 if (Ty == nullptr)
4100 return nullptr;
4101
4102 if (consumeIf('_')) {
4103 size_t ExprsBegin = Names.size();
4104 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004105 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004106 if (E == nullptr)
4107 return E;
4108 Names.push_back(E);
4109 }
4110 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4111 return make<ConversionExpr>(Ty, Exprs);
4112 }
4113
Pavel Labathba825192018-10-16 14:29:14 +00004114 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004115 if (E[0] == nullptr)
4116 return nullptr;
4117 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4118}
4119
4120// <expr-primary> ::= L <type> <value number> E # integer literal
4121// ::= L <type> <value float> E # floating literal
4122// ::= L <string type> E # string literal
4123// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004124// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004125// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4126// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004127template <typename Derived, typename Alloc>
4128Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004129 if (!consumeIf('L'))
4130 return nullptr;
4131 switch (look()) {
4132 case 'w':
4133 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004134 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004135 case 'b':
4136 if (consumeIf("b0E"))
4137 return make<BoolExpr>(0);
4138 if (consumeIf("b1E"))
4139 return make<BoolExpr>(1);
4140 return nullptr;
4141 case 'c':
4142 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004143 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004144 case 'a':
4145 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004146 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004147 case 'h':
4148 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004149 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004150 case 's':
4151 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004152 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004153 case 't':
4154 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004155 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004156 case 'i':
4157 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004158 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004159 case 'j':
4160 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004161 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004162 case 'l':
4163 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004164 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004165 case 'm':
4166 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004167 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004168 case 'x':
4169 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004170 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004171 case 'y':
4172 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004173 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004174 case 'n':
4175 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004176 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004177 case 'o':
4178 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004179 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004180 case 'f':
4181 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004182 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004183 case 'd':
4184 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004185 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004186 case 'e':
4187 ++First;
Xing Xue3dc5e082020-04-15 09:59:06 -04004188#if defined(__powerpc__) || defined(__s390__)
4189 // Handle cases where long doubles encoded with e have the same size
4190 // and representation as doubles.
4191 return getDerived().template parseFloatingLiteral<double>();
4192#else
Pavel Labathba825192018-10-16 14:29:14 +00004193 return getDerived().template parseFloatingLiteral<long double>();
Xing Xue3dc5e082020-04-15 09:59:06 -04004194#endif
Richard Smithc20d1442018-08-20 20:14:49 +00004195 case '_':
4196 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004197 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004198 if (R != nullptr && consumeIf('E'))
4199 return R;
4200 }
4201 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004202 case 'A': {
4203 Node *T = getDerived().parseType();
4204 if (T == nullptr)
4205 return nullptr;
4206 // FIXME: We need to include the string contents in the mangling.
4207 if (consumeIf('E'))
4208 return make<StringLiteral>(T);
4209 return nullptr;
4210 }
4211 case 'D':
4212 if (consumeIf("DnE"))
4213 return make<NameType>("nullptr");
4214 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004215 case 'T':
4216 // Invalid mangled name per
4217 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4218 return nullptr;
Richard Smithfb917462019-09-09 22:26:04 +00004219 case 'U': {
4220 // FIXME: Should we support LUb... for block literals?
4221 if (look(1) != 'l')
4222 return nullptr;
4223 Node *T = parseUnnamedTypeName(nullptr);
4224 if (!T || !consumeIf('E'))
4225 return nullptr;
4226 return make<LambdaExpr>(T);
4227 }
Richard Smithc20d1442018-08-20 20:14:49 +00004228 default: {
4229 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004230 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004231 if (T == nullptr)
4232 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004233 StringView N = parseNumber(/*AllowNegative=*/true);
Richard Smithfb917462019-09-09 22:26:04 +00004234 if (N.empty())
4235 return nullptr;
4236 if (!consumeIf('E'))
4237 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004238 return make<EnumLiteral>(T, N);
Richard Smithc20d1442018-08-20 20:14:49 +00004239 }
4240 }
4241}
4242
4243// <braced-expression> ::= <expression>
4244// ::= di <field source-name> <braced-expression> # .name = expr
4245// ::= dx <index expression> <braced-expression> # [expr] = expr
4246// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004247template <typename Derived, typename Alloc>
4248Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004249 if (look() == 'd') {
4250 switch (look(1)) {
4251 case 'i': {
4252 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004253 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004254 if (Field == nullptr)
4255 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004256 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004257 if (Init == nullptr)
4258 return nullptr;
4259 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4260 }
4261 case 'x': {
4262 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004263 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004264 if (Index == nullptr)
4265 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004266 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004267 if (Init == nullptr)
4268 return nullptr;
4269 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4270 }
4271 case 'X': {
4272 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004273 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004274 if (RangeBegin == nullptr)
4275 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004276 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004277 if (RangeEnd == nullptr)
4278 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004279 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004280 if (Init == nullptr)
4281 return nullptr;
4282 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4283 }
4284 }
4285 }
Pavel Labathba825192018-10-16 14:29:14 +00004286 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004287}
4288
4289// (not yet in the spec)
4290// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4291// ::= fR <binary-operator-name> <expression> <expression>
4292// ::= fl <binary-operator-name> <expression>
4293// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004294template <typename Derived, typename Alloc>
4295Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004296 if (!consumeIf('f'))
4297 return nullptr;
4298
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004299 bool IsLeftFold = false, HasInitializer = false;
4300 switch (look()) {
4301 default:
Richard Smithc20d1442018-08-20 20:14:49 +00004302 return nullptr;
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004303 case 'L':
4304 IsLeftFold = true;
4305 HasInitializer = true;
4306 break;
4307 case 'R':
4308 HasInitializer = true;
4309 break;
4310 case 'l':
4311 IsLeftFold = true;
4312 break;
4313 case 'r':
4314 break;
4315 }
Richard Smithc20d1442018-08-20 20:14:49 +00004316 ++First;
4317
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004318 const auto *Op = parseOperatorEncoding();
4319 if (!Op || Op->getKind() != OperatorInfo::Binary)
4320 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004321
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004322 Node *Pack = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004323 if (Pack == nullptr)
4324 return nullptr;
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004325
4326 Node *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004327 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004328 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004329 if (Init == nullptr)
4330 return nullptr;
4331 }
4332
4333 if (IsLeftFold && Init)
4334 std::swap(Pack, Init);
4335
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004336 return make<FoldExpr>(IsLeftFold, Op->getSymbol(), Pack, Init);
Richard Smithc20d1442018-08-20 20:14:49 +00004337}
4338
Richard Smith1865d2f2020-10-22 19:29:36 -07004339// <expression> ::= mc <parameter type> <expr> [<offset number>] E
4340//
4341// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4342template <typename Derived, typename Alloc>
4343Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() {
4344 Node *Ty = getDerived().parseType();
4345 if (!Ty)
4346 return nullptr;
4347 Node *Expr = getDerived().parseExpr();
4348 if (!Expr)
4349 return nullptr;
4350 StringView Offset = getDerived().parseNumber(true);
4351 if (!consumeIf('E'))
4352 return nullptr;
4353 return make<PointerToMemberConversionExpr>(Ty, Expr, Offset);
4354}
4355
4356// <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
4357// <union-selector> ::= _ [<number>]
4358//
4359// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4360template <typename Derived, typename Alloc>
4361Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
4362 Node *Ty = getDerived().parseType();
4363 if (!Ty)
4364 return nullptr;
4365 Node *Expr = getDerived().parseExpr();
4366 if (!Expr)
4367 return nullptr;
4368 StringView Offset = getDerived().parseNumber(true);
4369 size_t SelectorsBegin = Names.size();
4370 while (consumeIf('_')) {
4371 Node *Selector = make<NameType>(parseNumber());
4372 if (!Selector)
4373 return nullptr;
4374 Names.push_back(Selector);
4375 }
4376 bool OnePastTheEnd = consumeIf('p');
4377 if (!consumeIf('E'))
4378 return nullptr;
4379 return make<SubobjectExpr>(
4380 Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
4381}
4382
Richard Smithc20d1442018-08-20 20:14:49 +00004383// <expression> ::= <unary operator-name> <expression>
4384// ::= <binary operator-name> <expression> <expression>
4385// ::= <ternary operator-name> <expression> <expression> <expression>
4386// ::= cl <expression>+ E # call
4387// ::= cv <type> <expression> # conversion with one argument
4388// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4389// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4390// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4391// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4392// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4393// ::= [gs] dl <expression> # delete expression
4394// ::= [gs] da <expression> # delete[] expression
4395// ::= pp_ <expression> # prefix ++
4396// ::= mm_ <expression> # prefix --
4397// ::= ti <type> # typeid (type)
4398// ::= te <expression> # typeid (expression)
4399// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4400// ::= sc <type> <expression> # static_cast<type> (expression)
4401// ::= cc <type> <expression> # const_cast<type> (expression)
4402// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4403// ::= st <type> # sizeof (a type)
4404// ::= sz <expression> # sizeof (an expression)
4405// ::= at <type> # alignof (a type)
4406// ::= az <expression> # alignof (an expression)
4407// ::= nx <expression> # noexcept (expression)
4408// ::= <template-param>
4409// ::= <function-param>
4410// ::= dt <expression> <unresolved-name> # expr.name
4411// ::= pt <expression> <unresolved-name> # expr->name
4412// ::= ds <expression> <expression> # expr.*expr
4413// ::= sZ <template-param> # size of a parameter pack
4414// ::= sZ <function-param> # size of a function parameter pack
4415// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4416// ::= sp <expression> # pack expansion
4417// ::= tw <expression> # throw expression
4418// ::= tr # throw with no operand (rethrow)
4419// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4420// # freestanding dependent name (e.g., T::x),
4421// # objectless nonstatic member reference
4422// ::= fL <binary-operator-name> <expression> <expression>
4423// ::= fR <binary-operator-name> <expression> <expression>
4424// ::= fl <binary-operator-name> <expression>
4425// ::= fr <binary-operator-name> <expression>
4426// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004427template <typename Derived, typename Alloc>
4428Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004429 bool Global = consumeIf("gs");
Richard Smithc20d1442018-08-20 20:14:49 +00004430
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004431 const auto *Op = parseOperatorEncoding();
4432 if (Op) {
4433 auto Sym = Op->getSymbol();
4434 switch (Op->getKind()) {
4435 case OperatorInfo::Binary:
4436 // Binary operator: lhs @ rhs
4437 return getDerived().parseBinaryExpr(Sym);
4438 case OperatorInfo::Prefix:
4439 // Prefix unary operator: @ expr
4440 return getDerived().parsePrefixExpr(Sym);
4441 case OperatorInfo::Postfix: {
4442 // Postfix unary operator: expr @
4443 if (consumeIf('_'))
4444 return getDerived().parsePrefixExpr(Sym);
Pavel Labathba825192018-10-16 14:29:14 +00004445 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004446 if (Ex == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00004447 return nullptr;
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004448 return make<PostfixExpr>(Ex, Sym);
Richard Smithc20d1442018-08-20 20:14:49 +00004449 }
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004450 case OperatorInfo::Array: {
4451 // Array Index: lhs [ rhs ]
Pavel Labathba825192018-10-16 14:29:14 +00004452 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004453 if (Base == nullptr)
4454 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004455 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004456 if (Index == nullptr)
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004457 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004458 return make<ArraySubscriptExpr>(Base, Index);
4459 }
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004460 case OperatorInfo::Member: {
4461 // Member access lhs @ rhs
4462 Node *LHS = getDerived().parseExpr();
4463 if (LHS == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00004464 return nullptr;
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004465 Node *RHS = getDerived().parseExpr();
4466 if (RHS == nullptr)
4467 return nullptr;
4468 return make<MemberExpr>(LHS, Sym, RHS);
Richard Smithc20d1442018-08-20 20:14:49 +00004469 }
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004470 case OperatorInfo::New: {
4471 // New
4472 // # new (expr-list) type [(init)]
4473 // [gs] nw <expression>* _ <type> [pi <expression>*] E
4474 // # new[] (expr-list) type [(init)]
4475 // [gs] na <expression>* _ <type> [pi <expression>*] E
Nathan Sidwellc69bde22022-01-28 07:09:38 -08004476 size_t Exprs = Names.size();
4477 while (!consumeIf('_')) {
4478 Node *Ex = getDerived().parseExpr();
4479 if (Ex == nullptr)
4480 return nullptr;
4481 Names.push_back(Ex);
4482 }
4483 NodeArray ExprList = popTrailingNodeArray(Exprs);
4484 Node *Ty = getDerived().parseType();
4485 if (Ty == nullptr)
4486 return nullptr;
4487 bool HaveInits = consumeIf("pi");
4488 size_t InitsBegin = Names.size();
4489 while (!consumeIf('E')) {
4490 if (!HaveInits)
4491 return nullptr;
4492 Node *Init = getDerived().parseExpr();
4493 if (Init == nullptr)
4494 return Init;
4495 Names.push_back(Init);
4496 }
4497 NodeArray Inits = popTrailingNodeArray(InitsBegin);
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004498 return make<NewExpr>(ExprList, Ty, Inits, Global,
4499 /*IsArray=*/Op->getFlag());
Nathan Sidwellc69bde22022-01-28 07:09:38 -08004500 }
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004501 case OperatorInfo::Del: {
4502 // Delete
Pavel Labathba825192018-10-16 14:29:14 +00004503 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004504 if (Ex == nullptr)
Nathan Sidwellc6483042022-01-28 09:27:28 -08004505 return nullptr;
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004506 return make<DeleteExpr>(Ex, Global, /*IsArray=*/Op->getFlag());
Nathan Sidwellc6483042022-01-28 09:27:28 -08004507 }
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004508 case OperatorInfo::Call: {
4509 // Function Call
4510 Node *Callee = getDerived().parseExpr();
4511 if (Callee == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00004512 return nullptr;
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004513 size_t ExprsBegin = Names.size();
4514 while (!consumeIf('E')) {
4515 Node *E = getDerived().parseExpr();
4516 if (E == nullptr)
4517 return nullptr;
4518 Names.push_back(E);
4519 }
4520 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4521 }
4522 case OperatorInfo::CCast: {
4523 // C Cast: (type)expr
4524 Node *Ty;
4525 {
4526 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
4527 Ty = getDerived().parseType();
4528 }
4529 if (Ty == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00004530 return nullptr;
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004531
4532 size_t ExprsBegin = Names.size();
4533 bool IsMany = consumeIf('_');
4534 while (!consumeIf('E')) {
4535 Node *E = getDerived().parseExpr();
4536 if (E == nullptr)
4537 return E;
4538 Names.push_back(E);
4539 if (!IsMany)
4540 break;
4541 }
4542 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4543 if (!IsMany && Exprs.size() != 1)
4544 return nullptr;
4545 return make<ConversionExpr>(Ty, Exprs);
Richard Smithc20d1442018-08-20 20:14:49 +00004546 }
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004547 case OperatorInfo::Conditional: {
4548 // Conditional operator: expr ? expr : expr
Pavel Labathba825192018-10-16 14:29:14 +00004549 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004550 if (Cond == nullptr)
4551 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004552 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004553 if (LHS == nullptr)
4554 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004555 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004556 if (RHS == nullptr)
4557 return nullptr;
4558 return make<ConditionalExpr>(Cond, LHS, RHS);
4559 }
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004560 case OperatorInfo::NamedCast: {
4561 // Named cast operation, @<type>(expr)
Pavel Labathba825192018-10-16 14:29:14 +00004562 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004563 if (Ty == nullptr)
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004564 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004565 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004566 if (Ex == nullptr)
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004567 return nullptr;
4568 return make<CastExpr>(Sym, Ty, Ex);
Richard Smithc20d1442018-08-20 20:14:49 +00004569 }
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004570 case OperatorInfo::OfIdOp: {
4571 // [sizeof/alignof/typeid] ( <type>|<expr> )
4572 Node *Arg =
4573 Op->getFlag() ? getDerived().parseType() : getDerived().parseExpr();
4574 if (!Arg)
4575 return nullptr;
4576 return make<EnclosingExpr>(Sym, Arg, ")");
4577 }
Nathan Sidwell0dda3d42022-02-18 09:51:24 -08004578 case OperatorInfo::NameOnly: {
4579 // Not valid as an expression operand.
4580 return nullptr;
4581 }
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004582 }
4583 DEMANGLE_UNREACHABLE;
4584 }
4585
4586 if (numLeft() < 2)
4587 return nullptr;
4588
4589 if (look() == 'L')
4590 return getDerived().parseExprPrimary();
4591 if (look() == 'T')
4592 return getDerived().parseTemplateParam();
4593 if (look() == 'f') {
4594 // Disambiguate a fold expression from a <function-param>.
4595 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
4596 return getDerived().parseFunctionParam();
4597 return getDerived().parseFoldExpr();
4598 }
4599 if (consumeIf("il")) {
4600 size_t InitsBegin = Names.size();
4601 while (!consumeIf('E')) {
4602 Node *E = getDerived().parseBracedExpr();
4603 if (E == nullptr)
4604 return nullptr;
4605 Names.push_back(E);
4606 }
4607 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4608 }
4609 if (consumeIf("mc"))
4610 return parsePointerToMemberConversionExpr();
4611 if (consumeIf("nx")) {
4612 Node *Ex = getDerived().parseExpr();
4613 if (Ex == nullptr)
4614 return Ex;
4615 return make<EnclosingExpr>("noexcept (", Ex, ")");
4616 }
4617 if (consumeIf("so"))
4618 return parseSubobjectExpr();
4619 if (consumeIf("sp")) {
4620 Node *Child = getDerived().parseExpr();
4621 if (Child == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00004622 return nullptr;
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004623 return make<ParameterPackExpansion>(Child);
4624 }
4625 if (consumeIf("sZ")) {
4626 if (look() == 'T') {
4627 Node *R = getDerived().parseTemplateParam();
4628 if (R == nullptr)
Richard Smithb485b352018-08-24 23:30:26 +00004629 return nullptr;
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004630 return make<SizeofParamPackExpr>(R);
Richard Smithc20d1442018-08-20 20:14:49 +00004631 }
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004632 Node *FP = getDerived().parseFunctionParam();
4633 if (FP == nullptr)
4634 return nullptr;
4635 return make<EnclosingExpr>("sizeof... (", FP, ")");
4636 }
4637 if (consumeIf("sP")) {
4638 size_t ArgsBegin = Names.size();
4639 while (!consumeIf('E')) {
4640 Node *Arg = getDerived().parseTemplateArg();
4641 if (Arg == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00004642 return nullptr;
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004643 Names.push_back(Arg);
Richard Smithc20d1442018-08-20 20:14:49 +00004644 }
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004645 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4646 if (!Pack)
4647 return nullptr;
4648 return make<EnclosingExpr>("sizeof... (", Pack, ")");
4649 }
4650 if (consumeIf("tl")) {
4651 Node *Ty = getDerived().parseType();
4652 if (Ty == nullptr)
4653 return nullptr;
4654 size_t InitsBegin = Names.size();
4655 while (!consumeIf('E')) {
4656 Node *E = getDerived().parseBracedExpr();
4657 if (E == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00004658 return nullptr;
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004659 Names.push_back(E);
Richard Smithc20d1442018-08-20 20:14:49 +00004660 }
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004661 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
4662 }
4663 if (consumeIf("tr"))
4664 return make<NameType>("throw");
4665 if (consumeIf("tw")) {
4666 Node *Ex = getDerived().parseExpr();
4667 if (Ex == nullptr)
4668 return nullptr;
4669 return make<ThrowExpr>(Ex);
4670 }
4671 if (consumeIf('u')) {
James Y Knight4a60efc2020-12-07 10:26:49 -05004672 Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
4673 if (!Name)
4674 return nullptr;
4675 // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
4676 // standard encoding expects a <template-arg>, and would be otherwise be
4677 // interpreted as <type> node 'short' or 'ellipsis'. However, neither
4678 // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
4679 // actual conflict here.
Nathan Sidwella3b59002022-02-11 05:54:40 -08004680 bool IsUUID = false;
4681 Node *UUID = nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05004682 if (Name->getBaseName() == "__uuidof") {
Nathan Sidwella3b59002022-02-11 05:54:40 -08004683 if (consumeIf('t')) {
4684 UUID = getDerived().parseType();
4685 IsUUID = true;
4686 } else if (consumeIf('z')) {
4687 UUID = getDerived().parseExpr();
4688 IsUUID = true;
James Y Knight4a60efc2020-12-07 10:26:49 -05004689 }
4690 }
4691 size_t ExprsBegin = Names.size();
Nathan Sidwella3b59002022-02-11 05:54:40 -08004692 if (IsUUID) {
4693 if (UUID == nullptr)
4694 return nullptr;
4695 Names.push_back(UUID);
4696 } else {
4697 while (!consumeIf('E')) {
4698 Node *E = getDerived().parseTemplateArg();
4699 if (E == nullptr)
4700 return E;
4701 Names.push_back(E);
4702 }
James Y Knight4a60efc2020-12-07 10:26:49 -05004703 }
4704 return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin));
4705 }
Nathan Sidwell12b2ce72022-01-27 13:23:16 -08004706
4707 // Only unresolved names remain.
4708 return getDerived().parseUnresolvedName(Global);
Richard Smithc20d1442018-08-20 20:14:49 +00004709}
4710
4711// <call-offset> ::= h <nv-offset> _
4712// ::= v <v-offset> _
4713//
4714// <nv-offset> ::= <offset number>
4715// # non-virtual base override
4716//
4717// <v-offset> ::= <offset number> _ <virtual offset number>
4718// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00004719template <typename Alloc, typename Derived>
4720bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00004721 // Just scan through the call offset, we never add this information into the
4722 // output.
4723 if (consumeIf('h'))
4724 return parseNumber(true).empty() || !consumeIf('_');
4725 if (consumeIf('v'))
4726 return parseNumber(true).empty() || !consumeIf('_') ||
4727 parseNumber(true).empty() || !consumeIf('_');
4728 return true;
4729}
4730
4731// <special-name> ::= TV <type> # virtual table
4732// ::= TT <type> # VTT structure (construction vtable index)
4733// ::= TI <type> # typeinfo structure
4734// ::= TS <type> # typeinfo name (null-terminated byte string)
4735// ::= Tc <call-offset> <call-offset> <base encoding>
4736// # base is the nominal target function of thunk
4737// # first call-offset is 'this' adjustment
4738// # second call-offset is result adjustment
4739// ::= T <call-offset> <base encoding>
4740// # base is the nominal target function of thunk
4741// ::= GV <object name> # Guard variable for one-time initialization
4742// # No <type>
4743// ::= TW <object name> # Thread-local wrapper
4744// ::= TH <object name> # Thread-local initialization
4745// ::= GR <object name> _ # First temporary
4746// ::= GR <object name> <seq-id> _ # Subsequent temporaries
4747// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
4748// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00004749template <typename Derived, typename Alloc>
4750Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00004751 switch (look()) {
4752 case 'T':
4753 switch (look(1)) {
Richard Smith1865d2f2020-10-22 19:29:36 -07004754 // TA <template-arg> # template parameter object
4755 //
4756 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
4757 case 'A': {
4758 First += 2;
4759 Node *Arg = getDerived().parseTemplateArg();
4760 if (Arg == nullptr)
4761 return nullptr;
4762 return make<SpecialName>("template parameter object for ", Arg);
4763 }
Richard Smithc20d1442018-08-20 20:14:49 +00004764 // TV <type> # virtual table
4765 case 'V': {
4766 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004767 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004768 if (Ty == nullptr)
4769 return nullptr;
4770 return make<SpecialName>("vtable for ", Ty);
4771 }
4772 // TT <type> # VTT structure (construction vtable index)
4773 case 'T': {
4774 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004775 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004776 if (Ty == nullptr)
4777 return nullptr;
4778 return make<SpecialName>("VTT for ", Ty);
4779 }
4780 // TI <type> # typeinfo structure
4781 case 'I': {
4782 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004783 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004784 if (Ty == nullptr)
4785 return nullptr;
4786 return make<SpecialName>("typeinfo for ", Ty);
4787 }
4788 // TS <type> # typeinfo name (null-terminated byte string)
4789 case 'S': {
4790 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004791 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004792 if (Ty == nullptr)
4793 return nullptr;
4794 return make<SpecialName>("typeinfo name for ", Ty);
4795 }
4796 // Tc <call-offset> <call-offset> <base encoding>
4797 case 'c': {
4798 First += 2;
4799 if (parseCallOffset() || parseCallOffset())
4800 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004801 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004802 if (Encoding == nullptr)
4803 return nullptr;
4804 return make<SpecialName>("covariant return thunk to ", Encoding);
4805 }
4806 // extension ::= TC <first type> <number> _ <second type>
4807 // # construction vtable for second-in-first
4808 case 'C': {
4809 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004810 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004811 if (FirstType == nullptr)
4812 return nullptr;
4813 if (parseNumber(true).empty() || !consumeIf('_'))
4814 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004815 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004816 if (SecondType == nullptr)
4817 return nullptr;
4818 return make<CtorVtableSpecialName>(SecondType, FirstType);
4819 }
4820 // TW <object name> # Thread-local wrapper
4821 case 'W': {
4822 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004823 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00004824 if (Name == nullptr)
4825 return nullptr;
4826 return make<SpecialName>("thread-local wrapper routine for ", Name);
4827 }
4828 // TH <object name> # Thread-local initialization
4829 case 'H': {
4830 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004831 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00004832 if (Name == nullptr)
4833 return nullptr;
4834 return make<SpecialName>("thread-local initialization routine for ", Name);
4835 }
4836 // T <call-offset> <base encoding>
4837 default: {
4838 ++First;
4839 bool IsVirt = look() == 'v';
4840 if (parseCallOffset())
4841 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004842 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004843 if (BaseEncoding == nullptr)
4844 return nullptr;
4845 if (IsVirt)
4846 return make<SpecialName>("virtual thunk to ", BaseEncoding);
4847 else
4848 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
4849 }
4850 }
4851 case 'G':
4852 switch (look(1)) {
4853 // GV <object name> # Guard variable for one-time initialization
4854 case 'V': {
4855 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004856 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00004857 if (Name == nullptr)
4858 return nullptr;
4859 return make<SpecialName>("guard variable for ", Name);
4860 }
4861 // GR <object name> # reference temporary for object
4862 // GR <object name> _ # First temporary
4863 // GR <object name> <seq-id> _ # Subsequent temporaries
4864 case 'R': {
4865 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004866 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00004867 if (Name == nullptr)
4868 return nullptr;
4869 size_t Count;
4870 bool ParsedSeqId = !parseSeqId(&Count);
4871 if (!consumeIf('_') && ParsedSeqId)
4872 return nullptr;
4873 return make<SpecialName>("reference temporary for ", Name);
4874 }
4875 }
4876 }
4877 return nullptr;
4878}
4879
4880// <encoding> ::= <function name> <bare-function-type>
4881// ::= <data name>
4882// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00004883template <typename Derived, typename Alloc>
4884Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithfac39712020-07-09 21:08:39 -07004885 // The template parameters of an encoding are unrelated to those of the
4886 // enclosing context.
4887 class SaveTemplateParams {
4888 AbstractManglingParser *Parser;
4889 decltype(TemplateParams) OldParams;
Justin Lebar2c536232021-06-09 16:57:22 -07004890 decltype(OuterTemplateParams) OldOuterParams;
Richard Smithfac39712020-07-09 21:08:39 -07004891
4892 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04004893 SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
Richard Smithfac39712020-07-09 21:08:39 -07004894 OldParams = std::move(Parser->TemplateParams);
Justin Lebar2c536232021-06-09 16:57:22 -07004895 OldOuterParams = std::move(Parser->OuterTemplateParams);
Richard Smithfac39712020-07-09 21:08:39 -07004896 Parser->TemplateParams.clear();
Justin Lebar2c536232021-06-09 16:57:22 -07004897 Parser->OuterTemplateParams.clear();
Richard Smithfac39712020-07-09 21:08:39 -07004898 }
4899 ~SaveTemplateParams() {
4900 Parser->TemplateParams = std::move(OldParams);
Justin Lebar2c536232021-06-09 16:57:22 -07004901 Parser->OuterTemplateParams = std::move(OldOuterParams);
Richard Smithfac39712020-07-09 21:08:39 -07004902 }
4903 } SaveTemplateParams(this);
Richard Smithfd434322020-07-09 20:36:04 -07004904
Richard Smithc20d1442018-08-20 20:14:49 +00004905 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00004906 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00004907
4908 auto IsEndOfEncoding = [&] {
4909 // The set of chars that can potentially follow an <encoding> (none of which
4910 // can start a <type>). Enumerating these allows us to avoid speculative
4911 // parsing.
4912 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
4913 };
4914
4915 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00004916 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00004917 if (Name == nullptr)
4918 return nullptr;
4919
4920 if (resolveForwardTemplateRefs(NameInfo))
4921 return nullptr;
4922
4923 if (IsEndOfEncoding())
4924 return Name;
4925
4926 Node *Attrs = nullptr;
4927 if (consumeIf("Ua9enable_ifI")) {
4928 size_t BeforeArgs = Names.size();
4929 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004930 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004931 if (Arg == nullptr)
4932 return nullptr;
4933 Names.push_back(Arg);
4934 }
4935 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00004936 if (!Attrs)
4937 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004938 }
4939
4940 Node *ReturnType = nullptr;
4941 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00004942 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004943 if (ReturnType == nullptr)
4944 return nullptr;
4945 }
4946
4947 if (consumeIf('v'))
4948 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
4949 Attrs, NameInfo.CVQualifiers,
4950 NameInfo.ReferenceQualifier);
4951
4952 size_t ParamsBegin = Names.size();
4953 do {
Pavel Labathba825192018-10-16 14:29:14 +00004954 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004955 if (Ty == nullptr)
4956 return nullptr;
4957 Names.push_back(Ty);
4958 } while (!IsEndOfEncoding());
4959
4960 return make<FunctionEncoding>(ReturnType, Name,
4961 popTrailingNodeArray(ParamsBegin),
4962 Attrs, NameInfo.CVQualifiers,
4963 NameInfo.ReferenceQualifier);
4964}
4965
4966template <class Float>
4967struct FloatData;
4968
4969template <>
4970struct FloatData<float>
4971{
4972 static const size_t mangled_size = 8;
4973 static const size_t max_demangled_size = 24;
4974 static constexpr const char* spec = "%af";
4975};
4976
4977template <>
4978struct FloatData<double>
4979{
4980 static const size_t mangled_size = 16;
4981 static const size_t max_demangled_size = 32;
4982 static constexpr const char* spec = "%a";
4983};
4984
4985template <>
4986struct FloatData<long double>
4987{
4988#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
4989 defined(__wasm__)
4990 static const size_t mangled_size = 32;
4991#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
4992 static const size_t mangled_size = 16;
4993#else
4994 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
4995#endif
Elliott Hughes5a360ea2020-04-10 17:42:00 -07004996 // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
4997 // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
4998 // Negatives are one character longer than positives.
4999 // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
5000 // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
5001 static const size_t max_demangled_size = 42;
Richard Smithc20d1442018-08-20 20:14:49 +00005002 static constexpr const char *spec = "%LaL";
5003};
5004
Pavel Labathba825192018-10-16 14:29:14 +00005005template <typename Alloc, typename Derived>
5006template <class Float>
5007Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005008 const size_t N = FloatData<Float>::mangled_size;
5009 if (numLeft() <= N)
5010 return nullptr;
5011 StringView Data(First, First + N);
5012 for (char C : Data)
5013 if (!std::isxdigit(C))
5014 return nullptr;
5015 First += N;
5016 if (!consumeIf('E'))
5017 return nullptr;
5018 return make<FloatLiteralImpl<Float>>(Data);
5019}
5020
5021// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005022template <typename Alloc, typename Derived>
5023bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005024 if (!(look() >= '0' && look() <= '9') &&
5025 !(look() >= 'A' && look() <= 'Z'))
5026 return true;
5027
5028 size_t Id = 0;
5029 while (true) {
5030 if (look() >= '0' && look() <= '9') {
5031 Id *= 36;
5032 Id += static_cast<size_t>(look() - '0');
5033 } else if (look() >= 'A' && look() <= 'Z') {
5034 Id *= 36;
5035 Id += static_cast<size_t>(look() - 'A') + 10;
5036 } else {
5037 *Out = Id;
5038 return false;
5039 }
5040 ++First;
5041 }
5042}
5043
5044// <substitution> ::= S <seq-id> _
5045// ::= S_
5046// <substitution> ::= Sa # ::std::allocator
5047// <substitution> ::= Sb # ::std::basic_string
5048// <substitution> ::= Ss # ::std::basic_string < char,
5049// ::std::char_traits<char>,
5050// ::std::allocator<char> >
5051// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5052// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5053// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Nathan Sidwelle12f7bd2022-01-21 11:00:56 -08005054// The St case is handled specially in parseNestedName.
Pavel Labathba825192018-10-16 14:29:14 +00005055template <typename Derived, typename Alloc>
5056Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005057 if (!consumeIf('S'))
5058 return nullptr;
5059
Nathan Sidwellfd0ef6d2022-01-20 07:40:12 -08005060 if (look() >= 'a' && look() <= 'z') {
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005061 SpecialSubKind Kind;
Richard Smithc20d1442018-08-20 20:14:49 +00005062 switch (look()) {
5063 case 'a':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005064 Kind = SpecialSubKind::allocator;
Richard Smithc20d1442018-08-20 20:14:49 +00005065 break;
5066 case 'b':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005067 Kind = SpecialSubKind::basic_string;
Richard Smithc20d1442018-08-20 20:14:49 +00005068 break;
5069 case 'd':
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005070 Kind = SpecialSubKind::iostream;
5071 break;
5072 case 'i':
5073 Kind = SpecialSubKind::istream;
5074 break;
5075 case 'o':
5076 Kind = SpecialSubKind::ostream;
5077 break;
5078 case 's':
5079 Kind = SpecialSubKind::string;
Richard Smithc20d1442018-08-20 20:14:49 +00005080 break;
5081 default:
5082 return nullptr;
5083 }
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005084 ++First;
5085 auto *SpecialSub = make<SpecialSubstitution>(Kind);
Richard Smithb485b352018-08-24 23:30:26 +00005086 if (!SpecialSub)
5087 return nullptr;
Nathan Sidwelldf43e1b2022-01-24 07:59:57 -08005088
Richard Smithc20d1442018-08-20 20:14:49 +00005089 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5090 // has ABI tags, the tags are appended to the substitution; the result is a
5091 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005092 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005093 if (WithTags != SpecialSub) {
5094 Subs.push_back(WithTags);
5095 SpecialSub = WithTags;
5096 }
5097 return SpecialSub;
5098 }
5099
5100 // ::= S_
5101 if (consumeIf('_')) {
5102 if (Subs.empty())
5103 return nullptr;
5104 return Subs[0];
5105 }
5106
5107 // ::= S <seq-id> _
5108 size_t Index = 0;
5109 if (parseSeqId(&Index))
5110 return nullptr;
5111 ++Index;
5112 if (!consumeIf('_') || Index >= Subs.size())
5113 return nullptr;
5114 return Subs[Index];
5115}
5116
5117// <template-param> ::= T_ # first template parameter
5118// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005119// ::= TL <level-1> __
5120// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005121template <typename Derived, typename Alloc>
5122Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005123 if (!consumeIf('T'))
5124 return nullptr;
5125
Richard Smithdf1c14c2019-09-06 23:53:21 +00005126 size_t Level = 0;
5127 if (consumeIf('L')) {
5128 if (parsePositiveInteger(&Level))
5129 return nullptr;
5130 ++Level;
5131 if (!consumeIf('_'))
5132 return nullptr;
5133 }
5134
Richard Smithc20d1442018-08-20 20:14:49 +00005135 size_t Index = 0;
5136 if (!consumeIf('_')) {
5137 if (parsePositiveInteger(&Index))
5138 return nullptr;
5139 ++Index;
5140 if (!consumeIf('_'))
5141 return nullptr;
5142 }
5143
Richard Smithc20d1442018-08-20 20:14:49 +00005144 // If we're in a context where this <template-param> refers to a
5145 // <template-arg> further ahead in the mangled name (currently just conversion
5146 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005147 // This can only happen at the outermost level.
5148 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005149 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5150 if (!ForwardRef)
5151 return nullptr;
5152 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5153 ForwardTemplateRefs.push_back(
5154 static_cast<ForwardTemplateReference *>(ForwardRef));
5155 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005156 }
5157
Richard Smithdf1c14c2019-09-06 23:53:21 +00005158 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5159 Index >= TemplateParams[Level]->size()) {
5160 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5161 // list are mangled as the corresponding artificial template type parameter.
5162 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5163 // This will be popped by the ScopedTemplateParamList in
5164 // parseUnnamedTypeName.
5165 if (Level == TemplateParams.size())
5166 TemplateParams.push_back(nullptr);
5167 return make<NameType>("auto");
5168 }
5169
Richard Smithc20d1442018-08-20 20:14:49 +00005170 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005171 }
5172
5173 return (*TemplateParams[Level])[Index];
5174}
5175
5176// <template-param-decl> ::= Ty # type parameter
5177// ::= Tn <type> # non-type parameter
5178// ::= Tt <template-param-decl>* E # template parameter
5179// ::= Tp <template-param-decl> # parameter pack
5180template <typename Derived, typename Alloc>
5181Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5182 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5183 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5184 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5185 if (N) TemplateParams.back()->push_back(N);
5186 return N;
5187 };
5188
5189 if (consumeIf("Ty")) {
5190 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5191 if (!Name)
5192 return nullptr;
5193 return make<TypeTemplateParamDecl>(Name);
5194 }
5195
5196 if (consumeIf("Tn")) {
5197 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5198 if (!Name)
5199 return nullptr;
5200 Node *Type = parseType();
5201 if (!Type)
5202 return nullptr;
5203 return make<NonTypeTemplateParamDecl>(Name, Type);
5204 }
5205
5206 if (consumeIf("Tt")) {
5207 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5208 if (!Name)
5209 return nullptr;
5210 size_t ParamsBegin = Names.size();
5211 ScopedTemplateParamList TemplateTemplateParamParams(this);
5212 while (!consumeIf("E")) {
5213 Node *P = parseTemplateParamDecl();
5214 if (!P)
5215 return nullptr;
5216 Names.push_back(P);
5217 }
5218 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5219 return make<TemplateTemplateParamDecl>(Name, Params);
5220 }
5221
5222 if (consumeIf("Tp")) {
5223 Node *P = parseTemplateParamDecl();
5224 if (!P)
5225 return nullptr;
5226 return make<TemplateParamPackDecl>(P);
5227 }
5228
5229 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005230}
5231
5232// <template-arg> ::= <type> # type or template
5233// ::= X <expression> E # expression
5234// ::= <expr-primary> # simple expressions
5235// ::= J <template-arg>* E # argument pack
5236// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005237template <typename Derived, typename Alloc>
5238Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005239 switch (look()) {
5240 case 'X': {
5241 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005242 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005243 if (Arg == nullptr || !consumeIf('E'))
5244 return nullptr;
5245 return Arg;
5246 }
5247 case 'J': {
5248 ++First;
5249 size_t ArgsBegin = Names.size();
5250 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005251 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005252 if (Arg == nullptr)
5253 return nullptr;
5254 Names.push_back(Arg);
5255 }
5256 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5257 return make<TemplateArgumentPack>(Args);
5258 }
5259 case 'L': {
5260 // ::= LZ <encoding> E # extension
5261 if (look(1) == 'Z') {
5262 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005263 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005264 if (Arg == nullptr || !consumeIf('E'))
5265 return nullptr;
5266 return Arg;
5267 }
5268 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005269 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005270 }
5271 default:
Pavel Labathba825192018-10-16 14:29:14 +00005272 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005273 }
5274}
5275
5276// <template-args> ::= I <template-arg>* E
5277// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005278template <typename Derived, typename Alloc>
5279Node *
5280AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005281 if (!consumeIf('I'))
5282 return nullptr;
5283
5284 // <template-params> refer to the innermost <template-args>. Clear out any
5285 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005286 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005287 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005288 TemplateParams.push_back(&OuterTemplateParams);
5289 OuterTemplateParams.clear();
5290 }
Richard Smithc20d1442018-08-20 20:14:49 +00005291
5292 size_t ArgsBegin = Names.size();
5293 while (!consumeIf('E')) {
5294 if (TagTemplates) {
5295 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005296 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005297 TemplateParams = std::move(OldParams);
5298 if (Arg == nullptr)
5299 return nullptr;
5300 Names.push_back(Arg);
5301 Node *TableEntry = Arg;
5302 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5303 TableEntry = make<ParameterPack>(
5304 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005305 if (!TableEntry)
5306 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005307 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005308 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005309 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005310 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005311 if (Arg == nullptr)
5312 return nullptr;
5313 Names.push_back(Arg);
5314 }
5315 }
5316 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5317}
5318
5319// <mangled-name> ::= _Z <encoding>
5320// ::= <type>
5321// extension ::= ___Z <encoding> _block_invoke
5322// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5323// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005324template <typename Derived, typename Alloc>
5325Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005326 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005327 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005328 if (Encoding == nullptr)
5329 return nullptr;
5330 if (look() == '.') {
5331 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5332 First = Last;
5333 }
5334 if (numLeft() != 0)
5335 return nullptr;
5336 return Encoding;
5337 }
5338
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005339 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005340 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005341 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5342 return nullptr;
5343 bool RequireNumber = consumeIf('_');
5344 if (parseNumber().empty() && RequireNumber)
5345 return nullptr;
5346 if (look() == '.')
5347 First = Last;
5348 if (numLeft() != 0)
5349 return nullptr;
5350 return make<SpecialName>("invocation function for block in ", Encoding);
5351 }
5352
Pavel Labathba825192018-10-16 14:29:14 +00005353 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005354 if (numLeft() != 0)
5355 return nullptr;
5356 return Ty;
5357}
5358
Pavel Labathba825192018-10-16 14:29:14 +00005359template <typename Alloc>
5360struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5361 using AbstractManglingParser<ManglingParser<Alloc>,
5362 Alloc>::AbstractManglingParser;
5363};
5364
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005365DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005366
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005367#endif // DEMANGLE_ITANIUMDEMANGLE_H