blob: 7784e842bfeb43b886457e883618ae985446e1e9 [file] [log] [blame]
Richard Smithc20d1442018-08-20 20:14:49 +00001//===------------------------- ItaniumDemangle.h ----------------*- C++ -*-===//
2//
Chandler Carruth8ee27c32019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Richard Smithc20d1442018-08-20 20:14:49 +00006//
7//===----------------------------------------------------------------------===//
8//
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00009// Generic itanium demangler library. This file has two byte-per-byte identical
10// copies in the source tree, one in libcxxabi, and the other in llvm.
Richard Smithc20d1442018-08-20 20:14:49 +000011//
12//===----------------------------------------------------------------------===//
13
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +000014#ifndef DEMANGLE_ITANIUMDEMANGLE_H
15#define DEMANGLE_ITANIUMDEMANGLE_H
Richard Smithc20d1442018-08-20 20:14:49 +000016
17// FIXME: (possibly) incomplete list of features that clang mangles that this
18// file does not yet support:
19// - C++ modules TS
20
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +000021#include "DemangleConfig.h"
Richard Smithc20d1442018-08-20 20:14:49 +000022#include "StringView.h"
23#include "Utility.h"
Richard Smithc20d1442018-08-20 20:14:49 +000024#include <cassert>
25#include <cctype>
26#include <cstdio>
27#include <cstdlib>
28#include <cstring>
29#include <numeric>
30#include <utility>
31
32#define FOR_EACH_NODE_KIND(X) \
33 X(NodeArrayNode) \
34 X(DotSuffix) \
35 X(VendorExtQualType) \
36 X(QualType) \
37 X(ConversionOperatorType) \
38 X(PostfixQualifiedType) \
39 X(ElaboratedTypeSpefType) \
40 X(NameType) \
41 X(AbiTagAttr) \
42 X(EnableIfAttr) \
43 X(ObjCProtoName) \
44 X(PointerType) \
45 X(ReferenceType) \
46 X(PointerToMemberType) \
47 X(ArrayType) \
48 X(FunctionType) \
49 X(NoexceptSpec) \
50 X(DynamicExceptionSpec) \
51 X(FunctionEncoding) \
52 X(LiteralOperator) \
53 X(SpecialName) \
54 X(CtorVtableSpecialName) \
55 X(QualifiedName) \
56 X(NestedName) \
57 X(LocalName) \
58 X(VectorType) \
59 X(PixelVectorType) \
Richard Smithdf1c14c2019-09-06 23:53:21 +000060 X(SyntheticTemplateParamName) \
61 X(TypeTemplateParamDecl) \
62 X(NonTypeTemplateParamDecl) \
63 X(TemplateTemplateParamDecl) \
64 X(TemplateParamPackDecl) \
Richard Smithc20d1442018-08-20 20:14:49 +000065 X(ParameterPack) \
66 X(TemplateArgumentPack) \
67 X(ParameterPackExpansion) \
68 X(TemplateArgs) \
69 X(ForwardTemplateReference) \
70 X(NameWithTemplateArgs) \
71 X(GlobalQualifiedName) \
72 X(StdQualifiedName) \
73 X(ExpandedSpecialSubstitution) \
74 X(SpecialSubstitution) \
75 X(CtorDtorName) \
76 X(DtorName) \
77 X(UnnamedTypeName) \
78 X(ClosureTypeName) \
79 X(StructuredBindingName) \
80 X(BinaryExpr) \
81 X(ArraySubscriptExpr) \
82 X(PostfixExpr) \
83 X(ConditionalExpr) \
84 X(MemberExpr) \
85 X(EnclosingExpr) \
86 X(CastExpr) \
87 X(SizeofParamPackExpr) \
88 X(CallExpr) \
89 X(NewExpr) \
90 X(DeleteExpr) \
91 X(PrefixExpr) \
92 X(FunctionParam) \
93 X(ConversionExpr) \
94 X(InitListExpr) \
95 X(FoldExpr) \
96 X(ThrowExpr) \
Erik Pilkingtone8457c62019-06-18 23:34:09 +000097 X(UUIDOfExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000098 X(BoolExpr) \
Richard Smithdf1c14c2019-09-06 23:53:21 +000099 X(StringLiteral) \
100 X(LambdaExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +0000101 X(IntegerCastExpr) \
102 X(IntegerLiteral) \
103 X(FloatLiteral) \
104 X(DoubleLiteral) \
105 X(LongDoubleLiteral) \
106 X(BracedExpr) \
107 X(BracedRangeExpr)
108
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000109DEMANGLE_NAMESPACE_BEGIN
110
Richard Smithc20d1442018-08-20 20:14:49 +0000111// Base class of all AST nodes. The AST is built by the parser, then is
112// traversed by the printLeft/Right functions to produce a demangled string.
113class Node {
114public:
115 enum Kind : unsigned char {
116#define ENUMERATOR(NodeKind) K ## NodeKind,
117 FOR_EACH_NODE_KIND(ENUMERATOR)
118#undef ENUMERATOR
119 };
120
121 /// Three-way bool to track a cached value. Unknown is possible if this node
122 /// has an unexpanded parameter pack below it that may affect this cache.
123 enum class Cache : unsigned char { Yes, No, Unknown, };
124
125private:
126 Kind K;
127
128 // FIXME: Make these protected.
129public:
130 /// Tracks if this node has a component on its right side, in which case we
131 /// need to call printRight.
132 Cache RHSComponentCache;
133
134 /// Track if this node is a (possibly qualified) array type. This can affect
135 /// how we format the output string.
136 Cache ArrayCache;
137
138 /// Track if this node is a (possibly qualified) function type. This can
139 /// affect how we format the output string.
140 Cache FunctionCache;
141
142public:
143 Node(Kind K_, Cache RHSComponentCache_ = Cache::No,
144 Cache ArrayCache_ = Cache::No, Cache FunctionCache_ = Cache::No)
145 : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_),
146 FunctionCache(FunctionCache_) {}
147
148 /// Visit the most-derived object corresponding to this object.
149 template<typename Fn> void visit(Fn F) const;
150
151 // The following function is provided by all derived classes:
152 //
153 // Call F with arguments that, when passed to the constructor of this node,
154 // would construct an equivalent node.
155 //template<typename Fn> void match(Fn F) const;
156
157 bool hasRHSComponent(OutputStream &S) const {
158 if (RHSComponentCache != Cache::Unknown)
159 return RHSComponentCache == Cache::Yes;
160 return hasRHSComponentSlow(S);
161 }
162
163 bool hasArray(OutputStream &S) const {
164 if (ArrayCache != Cache::Unknown)
165 return ArrayCache == Cache::Yes;
166 return hasArraySlow(S);
167 }
168
169 bool hasFunction(OutputStream &S) const {
170 if (FunctionCache != Cache::Unknown)
171 return FunctionCache == Cache::Yes;
172 return hasFunctionSlow(S);
173 }
174
175 Kind getKind() const { return K; }
176
177 virtual bool hasRHSComponentSlow(OutputStream &) const { return false; }
178 virtual bool hasArraySlow(OutputStream &) const { return false; }
179 virtual bool hasFunctionSlow(OutputStream &) const { return false; }
180
181 // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
182 // get at a node that actually represents some concrete syntax.
183 virtual const Node *getSyntaxNode(OutputStream &) const {
184 return this;
185 }
186
187 void print(OutputStream &S) const {
188 printLeft(S);
189 if (RHSComponentCache != Cache::No)
190 printRight(S);
191 }
192
193 // Print the "left" side of this Node into OutputStream.
194 virtual void printLeft(OutputStream &) const = 0;
195
196 // Print the "right". This distinction is necessary to represent C++ types
197 // that appear on the RHS of their subtype, such as arrays or functions.
198 // Since most types don't have such a component, provide a default
199 // implementation.
200 virtual void printRight(OutputStream &) const {}
201
202 virtual StringView getBaseName() const { return StringView(); }
203
204 // Silence compiler warnings, this dtor will never be called.
205 virtual ~Node() = default;
206
207#ifndef NDEBUG
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000208 DEMANGLE_DUMP_METHOD void dump() const;
Richard Smithc20d1442018-08-20 20:14:49 +0000209#endif
210};
211
212class NodeArray {
213 Node **Elements;
214 size_t NumElements;
215
216public:
217 NodeArray() : Elements(nullptr), NumElements(0) {}
218 NodeArray(Node **Elements_, size_t NumElements_)
219 : Elements(Elements_), NumElements(NumElements_) {}
220
221 bool empty() const { return NumElements == 0; }
222 size_t size() const { return NumElements; }
223
224 Node **begin() const { return Elements; }
225 Node **end() const { return Elements + NumElements; }
226
227 Node *operator[](size_t Idx) const { return Elements[Idx]; }
228
229 void printWithComma(OutputStream &S) const {
230 bool FirstElement = true;
231 for (size_t Idx = 0; Idx != NumElements; ++Idx) {
232 size_t BeforeComma = S.getCurrentPosition();
233 if (!FirstElement)
234 S += ", ";
235 size_t AfterComma = S.getCurrentPosition();
236 Elements[Idx]->print(S);
237
238 // Elements[Idx] is an empty parameter pack expansion, we should erase the
239 // comma we just printed.
240 if (AfterComma == S.getCurrentPosition()) {
241 S.setCurrentPosition(BeforeComma);
242 continue;
243 }
244
245 FirstElement = false;
246 }
247 }
248};
249
250struct NodeArrayNode : Node {
251 NodeArray Array;
252 NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
253
254 template<typename Fn> void match(Fn F) const { F(Array); }
255
256 void printLeft(OutputStream &S) const override {
257 Array.printWithComma(S);
258 }
259};
260
261class DotSuffix final : public Node {
262 const Node *Prefix;
263 const StringView Suffix;
264
265public:
266 DotSuffix(const Node *Prefix_, StringView Suffix_)
267 : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
268
269 template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
270
271 void printLeft(OutputStream &s) const override {
272 Prefix->print(s);
273 s += " (";
274 s += Suffix;
275 s += ")";
276 }
277};
278
279class VendorExtQualType final : public Node {
280 const Node *Ty;
281 StringView Ext;
282
283public:
284 VendorExtQualType(const Node *Ty_, StringView Ext_)
285 : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_) {}
286
287 template<typename Fn> void match(Fn F) const { F(Ty, Ext); }
288
289 void printLeft(OutputStream &S) const override {
290 Ty->print(S);
291 S += " ";
292 S += Ext;
293 }
294};
295
296enum FunctionRefQual : unsigned char {
297 FrefQualNone,
298 FrefQualLValue,
299 FrefQualRValue,
300};
301
302enum Qualifiers {
303 QualNone = 0,
304 QualConst = 0x1,
305 QualVolatile = 0x2,
306 QualRestrict = 0x4,
307};
308
309inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
310 return Q1 = static_cast<Qualifiers>(Q1 | Q2);
311}
312
Richard Smithdf1c14c2019-09-06 23:53:21 +0000313class QualType final : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +0000314protected:
315 const Qualifiers Quals;
316 const Node *Child;
317
318 void printQuals(OutputStream &S) const {
319 if (Quals & QualConst)
320 S += " const";
321 if (Quals & QualVolatile)
322 S += " volatile";
323 if (Quals & QualRestrict)
324 S += " restrict";
325 }
326
327public:
328 QualType(const Node *Child_, Qualifiers Quals_)
329 : Node(KQualType, Child_->RHSComponentCache,
330 Child_->ArrayCache, Child_->FunctionCache),
331 Quals(Quals_), Child(Child_) {}
332
333 template<typename Fn> void match(Fn F) const { F(Child, Quals); }
334
335 bool hasRHSComponentSlow(OutputStream &S) const override {
336 return Child->hasRHSComponent(S);
337 }
338 bool hasArraySlow(OutputStream &S) const override {
339 return Child->hasArray(S);
340 }
341 bool hasFunctionSlow(OutputStream &S) const override {
342 return Child->hasFunction(S);
343 }
344
345 void printLeft(OutputStream &S) const override {
346 Child->printLeft(S);
347 printQuals(S);
348 }
349
350 void printRight(OutputStream &S) const override { Child->printRight(S); }
351};
352
353class ConversionOperatorType final : public Node {
354 const Node *Ty;
355
356public:
357 ConversionOperatorType(const Node *Ty_)
358 : Node(KConversionOperatorType), Ty(Ty_) {}
359
360 template<typename Fn> void match(Fn F) const { F(Ty); }
361
362 void printLeft(OutputStream &S) const override {
363 S += "operator ";
364 Ty->print(S);
365 }
366};
367
368class PostfixQualifiedType final : public Node {
369 const Node *Ty;
370 const StringView Postfix;
371
372public:
373 PostfixQualifiedType(Node *Ty_, StringView Postfix_)
374 : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
375
376 template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
377
378 void printLeft(OutputStream &s) const override {
379 Ty->printLeft(s);
380 s += Postfix;
381 }
382};
383
384class NameType final : public Node {
385 const StringView Name;
386
387public:
388 NameType(StringView Name_) : Node(KNameType), Name(Name_) {}
389
390 template<typename Fn> void match(Fn F) const { F(Name); }
391
392 StringView getName() const { return Name; }
393 StringView getBaseName() const override { return Name; }
394
395 void printLeft(OutputStream &s) const override { s += Name; }
396};
397
398class ElaboratedTypeSpefType : public Node {
399 StringView Kind;
400 Node *Child;
401public:
402 ElaboratedTypeSpefType(StringView Kind_, Node *Child_)
403 : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
404
405 template<typename Fn> void match(Fn F) const { F(Kind, Child); }
406
407 void printLeft(OutputStream &S) const override {
408 S += Kind;
409 S += ' ';
410 Child->print(S);
411 }
412};
413
414struct AbiTagAttr : Node {
415 Node *Base;
416 StringView Tag;
417
418 AbiTagAttr(Node* Base_, StringView Tag_)
419 : Node(KAbiTagAttr, Base_->RHSComponentCache,
420 Base_->ArrayCache, Base_->FunctionCache),
421 Base(Base_), Tag(Tag_) {}
422
423 template<typename Fn> void match(Fn F) const { F(Base, Tag); }
424
425 void printLeft(OutputStream &S) const override {
426 Base->printLeft(S);
427 S += "[abi:";
428 S += Tag;
429 S += "]";
430 }
431};
432
433class EnableIfAttr : public Node {
434 NodeArray Conditions;
435public:
436 EnableIfAttr(NodeArray Conditions_)
437 : Node(KEnableIfAttr), Conditions(Conditions_) {}
438
439 template<typename Fn> void match(Fn F) const { F(Conditions); }
440
441 void printLeft(OutputStream &S) const override {
442 S += " [enable_if:";
443 Conditions.printWithComma(S);
444 S += ']';
445 }
446};
447
448class ObjCProtoName : public Node {
449 const Node *Ty;
450 StringView Protocol;
451
452 friend class PointerType;
453
454public:
455 ObjCProtoName(const Node *Ty_, StringView Protocol_)
456 : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
457
458 template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
459
460 bool isObjCObject() const {
461 return Ty->getKind() == KNameType &&
462 static_cast<const NameType *>(Ty)->getName() == "objc_object";
463 }
464
465 void printLeft(OutputStream &S) const override {
466 Ty->print(S);
467 S += "<";
468 S += Protocol;
469 S += ">";
470 }
471};
472
473class PointerType final : public Node {
474 const Node *Pointee;
475
476public:
477 PointerType(const Node *Pointee_)
478 : Node(KPointerType, Pointee_->RHSComponentCache),
479 Pointee(Pointee_) {}
480
481 template<typename Fn> void match(Fn F) const { F(Pointee); }
482
483 bool hasRHSComponentSlow(OutputStream &S) const override {
484 return Pointee->hasRHSComponent(S);
485 }
486
487 void printLeft(OutputStream &s) const override {
488 // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
489 if (Pointee->getKind() != KObjCProtoName ||
490 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
491 Pointee->printLeft(s);
492 if (Pointee->hasArray(s))
493 s += " ";
494 if (Pointee->hasArray(s) || Pointee->hasFunction(s))
495 s += "(";
496 s += "*";
497 } else {
498 const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
499 s += "id<";
500 s += objcProto->Protocol;
501 s += ">";
502 }
503 }
504
505 void printRight(OutputStream &s) const override {
506 if (Pointee->getKind() != KObjCProtoName ||
507 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
508 if (Pointee->hasArray(s) || Pointee->hasFunction(s))
509 s += ")";
510 Pointee->printRight(s);
511 }
512 }
513};
514
515enum class ReferenceKind {
516 LValue,
517 RValue,
518};
519
520// Represents either a LValue or an RValue reference type.
521class ReferenceType : public Node {
522 const Node *Pointee;
523 ReferenceKind RK;
524
525 mutable bool Printing = false;
526
527 // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
528 // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
529 // other combination collapses to a lvalue ref.
530 std::pair<ReferenceKind, const Node *> collapse(OutputStream &S) const {
531 auto SoFar = std::make_pair(RK, Pointee);
532 for (;;) {
533 const Node *SN = SoFar.second->getSyntaxNode(S);
534 if (SN->getKind() != KReferenceType)
535 break;
536 auto *RT = static_cast<const ReferenceType *>(SN);
537 SoFar.second = RT->Pointee;
538 SoFar.first = std::min(SoFar.first, RT->RK);
539 }
540 return SoFar;
541 }
542
543public:
544 ReferenceType(const Node *Pointee_, ReferenceKind RK_)
545 : Node(KReferenceType, Pointee_->RHSComponentCache),
546 Pointee(Pointee_), RK(RK_) {}
547
548 template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
549
550 bool hasRHSComponentSlow(OutputStream &S) const override {
551 return Pointee->hasRHSComponent(S);
552 }
553
554 void printLeft(OutputStream &s) const override {
555 if (Printing)
556 return;
557 SwapAndRestore<bool> SavePrinting(Printing, true);
558 std::pair<ReferenceKind, const Node *> Collapsed = collapse(s);
559 Collapsed.second->printLeft(s);
560 if (Collapsed.second->hasArray(s))
561 s += " ";
562 if (Collapsed.second->hasArray(s) || Collapsed.second->hasFunction(s))
563 s += "(";
564
565 s += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
566 }
567 void printRight(OutputStream &s) const override {
568 if (Printing)
569 return;
570 SwapAndRestore<bool> SavePrinting(Printing, true);
571 std::pair<ReferenceKind, const Node *> Collapsed = collapse(s);
572 if (Collapsed.second->hasArray(s) || Collapsed.second->hasFunction(s))
573 s += ")";
574 Collapsed.second->printRight(s);
575 }
576};
577
578class PointerToMemberType final : public Node {
579 const Node *ClassType;
580 const Node *MemberType;
581
582public:
583 PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
584 : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
585 ClassType(ClassType_), MemberType(MemberType_) {}
586
587 template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
588
589 bool hasRHSComponentSlow(OutputStream &S) const override {
590 return MemberType->hasRHSComponent(S);
591 }
592
593 void printLeft(OutputStream &s) const override {
594 MemberType->printLeft(s);
595 if (MemberType->hasArray(s) || MemberType->hasFunction(s))
596 s += "(";
597 else
598 s += " ";
599 ClassType->print(s);
600 s += "::*";
601 }
602
603 void printRight(OutputStream &s) const override {
604 if (MemberType->hasArray(s) || MemberType->hasFunction(s))
605 s += ")";
606 MemberType->printRight(s);
607 }
608};
609
610class NodeOrString {
611 const void *First;
612 const void *Second;
613
614public:
615 /* implicit */ NodeOrString(StringView Str) {
616 const char *FirstChar = Str.begin();
617 const char *SecondChar = Str.end();
618 if (SecondChar == nullptr) {
619 assert(FirstChar == SecondChar);
620 ++FirstChar, ++SecondChar;
621 }
622 First = static_cast<const void *>(FirstChar);
623 Second = static_cast<const void *>(SecondChar);
624 }
625
626 /* implicit */ NodeOrString(Node *N)
627 : First(static_cast<const void *>(N)), Second(nullptr) {}
628 NodeOrString() : First(nullptr), Second(nullptr) {}
629
630 bool isString() const { return Second && First; }
631 bool isNode() const { return First && !Second; }
632 bool isEmpty() const { return !First && !Second; }
633
634 StringView asString() const {
635 assert(isString());
636 return StringView(static_cast<const char *>(First),
637 static_cast<const char *>(Second));
638 }
639
640 const Node *asNode() const {
641 assert(isNode());
642 return static_cast<const Node *>(First);
643 }
644};
645
646class ArrayType final : public Node {
647 const Node *Base;
648 NodeOrString Dimension;
649
650public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +0000651 ArrayType(const Node *Base_, NodeOrString Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +0000652 : Node(KArrayType,
653 /*RHSComponentCache=*/Cache::Yes,
654 /*ArrayCache=*/Cache::Yes),
655 Base(Base_), Dimension(Dimension_) {}
656
657 template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
658
659 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
660 bool hasArraySlow(OutputStream &) const override { return true; }
661
662 void printLeft(OutputStream &S) const override { Base->printLeft(S); }
663
664 void printRight(OutputStream &S) const override {
665 if (S.back() != ']')
666 S += " ";
667 S += "[";
668 if (Dimension.isString())
669 S += Dimension.asString();
670 else if (Dimension.isNode())
671 Dimension.asNode()->print(S);
672 S += "]";
673 Base->printRight(S);
674 }
675};
676
677class FunctionType final : public Node {
678 const Node *Ret;
679 NodeArray Params;
680 Qualifiers CVQuals;
681 FunctionRefQual RefQual;
682 const Node *ExceptionSpec;
683
684public:
685 FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
686 FunctionRefQual RefQual_, const Node *ExceptionSpec_)
687 : Node(KFunctionType,
688 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
689 /*FunctionCache=*/Cache::Yes),
690 Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
691 ExceptionSpec(ExceptionSpec_) {}
692
693 template<typename Fn> void match(Fn F) const {
694 F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
695 }
696
697 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
698 bool hasFunctionSlow(OutputStream &) const override { return true; }
699
700 // Handle C++'s ... quirky decl grammar by using the left & right
701 // distinction. Consider:
702 // int (*f(float))(char) {}
703 // f is a function that takes a float and returns a pointer to a function
704 // that takes a char and returns an int. If we're trying to print f, start
705 // by printing out the return types's left, then print our parameters, then
706 // finally print right of the return type.
707 void printLeft(OutputStream &S) const override {
708 Ret->printLeft(S);
709 S += " ";
710 }
711
712 void printRight(OutputStream &S) const override {
713 S += "(";
714 Params.printWithComma(S);
715 S += ")";
716 Ret->printRight(S);
717
718 if (CVQuals & QualConst)
719 S += " const";
720 if (CVQuals & QualVolatile)
721 S += " volatile";
722 if (CVQuals & QualRestrict)
723 S += " restrict";
724
725 if (RefQual == FrefQualLValue)
726 S += " &";
727 else if (RefQual == FrefQualRValue)
728 S += " &&";
729
730 if (ExceptionSpec != nullptr) {
731 S += ' ';
732 ExceptionSpec->print(S);
733 }
734 }
735};
736
737class NoexceptSpec : public Node {
738 const Node *E;
739public:
740 NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
741
742 template<typename Fn> void match(Fn F) const { F(E); }
743
744 void printLeft(OutputStream &S) const override {
745 S += "noexcept(";
746 E->print(S);
747 S += ")";
748 }
749};
750
751class DynamicExceptionSpec : public Node {
752 NodeArray Types;
753public:
754 DynamicExceptionSpec(NodeArray Types_)
755 : Node(KDynamicExceptionSpec), Types(Types_) {}
756
757 template<typename Fn> void match(Fn F) const { F(Types); }
758
759 void printLeft(OutputStream &S) const override {
760 S += "throw(";
761 Types.printWithComma(S);
762 S += ')';
763 }
764};
765
766class FunctionEncoding final : public Node {
767 const Node *Ret;
768 const Node *Name;
769 NodeArray Params;
770 const Node *Attrs;
771 Qualifiers CVQuals;
772 FunctionRefQual RefQual;
773
774public:
775 FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
776 const Node *Attrs_, Qualifiers CVQuals_,
777 FunctionRefQual RefQual_)
778 : Node(KFunctionEncoding,
779 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
780 /*FunctionCache=*/Cache::Yes),
781 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
782 CVQuals(CVQuals_), RefQual(RefQual_) {}
783
784 template<typename Fn> void match(Fn F) const {
785 F(Ret, Name, Params, Attrs, CVQuals, RefQual);
786 }
787
788 Qualifiers getCVQuals() const { return CVQuals; }
789 FunctionRefQual getRefQual() const { return RefQual; }
790 NodeArray getParams() const { return Params; }
791 const Node *getReturnType() const { return Ret; }
792
793 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
794 bool hasFunctionSlow(OutputStream &) const override { return true; }
795
796 const Node *getName() const { return Name; }
797
798 void printLeft(OutputStream &S) const override {
799 if (Ret) {
800 Ret->printLeft(S);
801 if (!Ret->hasRHSComponent(S))
802 S += " ";
803 }
804 Name->print(S);
805 }
806
807 void printRight(OutputStream &S) const override {
808 S += "(";
809 Params.printWithComma(S);
810 S += ")";
811 if (Ret)
812 Ret->printRight(S);
813
814 if (CVQuals & QualConst)
815 S += " const";
816 if (CVQuals & QualVolatile)
817 S += " volatile";
818 if (CVQuals & QualRestrict)
819 S += " restrict";
820
821 if (RefQual == FrefQualLValue)
822 S += " &";
823 else if (RefQual == FrefQualRValue)
824 S += " &&";
825
826 if (Attrs != nullptr)
827 Attrs->print(S);
828 }
829};
830
831class LiteralOperator : public Node {
832 const Node *OpName;
833
834public:
835 LiteralOperator(const Node *OpName_)
836 : Node(KLiteralOperator), OpName(OpName_) {}
837
838 template<typename Fn> void match(Fn F) const { F(OpName); }
839
840 void printLeft(OutputStream &S) const override {
841 S += "operator\"\" ";
842 OpName->print(S);
843 }
844};
845
846class SpecialName final : public Node {
847 const StringView Special;
848 const Node *Child;
849
850public:
851 SpecialName(StringView Special_, const Node *Child_)
852 : Node(KSpecialName), Special(Special_), Child(Child_) {}
853
854 template<typename Fn> void match(Fn F) const { F(Special, Child); }
855
856 void printLeft(OutputStream &S) const override {
857 S += Special;
858 Child->print(S);
859 }
860};
861
862class CtorVtableSpecialName final : public Node {
863 const Node *FirstType;
864 const Node *SecondType;
865
866public:
867 CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
868 : Node(KCtorVtableSpecialName),
869 FirstType(FirstType_), SecondType(SecondType_) {}
870
871 template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
872
873 void printLeft(OutputStream &S) const override {
874 S += "construction vtable for ";
875 FirstType->print(S);
876 S += "-in-";
877 SecondType->print(S);
878 }
879};
880
881struct NestedName : Node {
882 Node *Qual;
883 Node *Name;
884
885 NestedName(Node *Qual_, Node *Name_)
886 : Node(KNestedName), Qual(Qual_), Name(Name_) {}
887
888 template<typename Fn> void match(Fn F) const { F(Qual, Name); }
889
890 StringView getBaseName() const override { return Name->getBaseName(); }
891
892 void printLeft(OutputStream &S) const override {
893 Qual->print(S);
894 S += "::";
895 Name->print(S);
896 }
897};
898
899struct LocalName : Node {
900 Node *Encoding;
901 Node *Entity;
902
903 LocalName(Node *Encoding_, Node *Entity_)
904 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
905
906 template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
907
908 void printLeft(OutputStream &S) const override {
909 Encoding->print(S);
910 S += "::";
911 Entity->print(S);
912 }
913};
914
915class QualifiedName final : public Node {
916 // qualifier::name
917 const Node *Qualifier;
918 const Node *Name;
919
920public:
921 QualifiedName(const Node *Qualifier_, const Node *Name_)
922 : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
923
924 template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
925
926 StringView getBaseName() const override { return Name->getBaseName(); }
927
928 void printLeft(OutputStream &S) const override {
929 Qualifier->print(S);
930 S += "::";
931 Name->print(S);
932 }
933};
934
935class VectorType final : public Node {
936 const Node *BaseType;
937 const NodeOrString Dimension;
938
939public:
940 VectorType(const Node *BaseType_, NodeOrString Dimension_)
941 : Node(KVectorType), BaseType(BaseType_),
942 Dimension(Dimension_) {}
943
944 template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
945
946 void printLeft(OutputStream &S) const override {
947 BaseType->print(S);
948 S += " vector[";
949 if (Dimension.isNode())
950 Dimension.asNode()->print(S);
951 else if (Dimension.isString())
952 S += Dimension.asString();
953 S += "]";
954 }
955};
956
957class PixelVectorType final : public Node {
958 const NodeOrString Dimension;
959
960public:
961 PixelVectorType(NodeOrString Dimension_)
962 : Node(KPixelVectorType), Dimension(Dimension_) {}
963
964 template<typename Fn> void match(Fn F) const { F(Dimension); }
965
966 void printLeft(OutputStream &S) const override {
967 // FIXME: This should demangle as "vector pixel".
968 S += "pixel vector[";
969 S += Dimension.asString();
970 S += "]";
971 }
972};
973
Richard Smithdf1c14c2019-09-06 23:53:21 +0000974enum class TemplateParamKind { Type, NonType, Template };
975
976/// An invented name for a template parameter for which we don't have a
977/// corresponding template argument.
978///
979/// This node is created when parsing the <lambda-sig> for a lambda with
980/// explicit template arguments, which might be referenced in the parameter
981/// types appearing later in the <lambda-sig>.
982class SyntheticTemplateParamName final : public Node {
983 TemplateParamKind Kind;
984 unsigned Index;
985
986public:
987 SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_)
988 : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {}
989
990 template<typename Fn> void match(Fn F) const { F(Kind, Index); }
991
992 void printLeft(OutputStream &S) const override {
993 switch (Kind) {
994 case TemplateParamKind::Type:
995 S += "$T";
996 break;
997 case TemplateParamKind::NonType:
998 S += "$N";
999 break;
1000 case TemplateParamKind::Template:
1001 S += "$TT";
1002 break;
1003 }
1004 if (Index > 0)
1005 S << Index - 1;
1006 }
1007};
1008
1009/// A template type parameter declaration, 'typename T'.
1010class TypeTemplateParamDecl final : public Node {
1011 Node *Name;
1012
1013public:
1014 TypeTemplateParamDecl(Node *Name_)
1015 : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {}
1016
1017 template<typename Fn> void match(Fn F) const { F(Name); }
1018
1019 void printLeft(OutputStream &S) const override {
1020 S += "typename ";
1021 }
1022
1023 void printRight(OutputStream &S) const override {
1024 Name->print(S);
1025 }
1026};
1027
1028/// A non-type template parameter declaration, 'int N'.
1029class NonTypeTemplateParamDecl final : public Node {
1030 Node *Name;
1031 Node *Type;
1032
1033public:
1034 NonTypeTemplateParamDecl(Node *Name_, Node *Type_)
1035 : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {}
1036
1037 template<typename Fn> void match(Fn F) const { F(Name, Type); }
1038
1039 void printLeft(OutputStream &S) const override {
1040 Type->printLeft(S);
1041 if (!Type->hasRHSComponent(S))
1042 S += " ";
1043 }
1044
1045 void printRight(OutputStream &S) const override {
1046 Name->print(S);
1047 Type->printRight(S);
1048 }
1049};
1050
1051/// A template template parameter declaration,
1052/// 'template<typename T> typename N'.
1053class TemplateTemplateParamDecl final : public Node {
1054 Node *Name;
1055 NodeArray Params;
1056
1057public:
1058 TemplateTemplateParamDecl(Node *Name_, NodeArray Params_)
1059 : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_),
1060 Params(Params_) {}
1061
1062 template<typename Fn> void match(Fn F) const { F(Name, Params); }
1063
1064 void printLeft(OutputStream &S) const override {
1065 S += "template<";
1066 Params.printWithComma(S);
1067 S += "> typename ";
1068 }
1069
1070 void printRight(OutputStream &S) const override {
1071 Name->print(S);
1072 }
1073};
1074
1075/// A template parameter pack declaration, 'typename ...T'.
1076class TemplateParamPackDecl final : public Node {
1077 Node *Param;
1078
1079public:
1080 TemplateParamPackDecl(Node *Param_)
1081 : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {}
1082
1083 template<typename Fn> void match(Fn F) const { F(Param); }
1084
1085 void printLeft(OutputStream &S) const override {
1086 Param->printLeft(S);
1087 S += "...";
1088 }
1089
1090 void printRight(OutputStream &S) const override {
1091 Param->printRight(S);
1092 }
1093};
1094
Richard Smithc20d1442018-08-20 20:14:49 +00001095/// An unexpanded parameter pack (either in the expression or type context). If
1096/// this AST is correct, this node will have a ParameterPackExpansion node above
1097/// it.
1098///
1099/// This node is created when some <template-args> are found that apply to an
1100/// <encoding>, and is stored in the TemplateParams table. In order for this to
1101/// appear in the final AST, it has to referenced via a <template-param> (ie,
1102/// T_).
1103class ParameterPack final : public Node {
1104 NodeArray Data;
1105
1106 // Setup OutputStream for a pack expansion unless we're already expanding one.
1107 void initializePackExpansion(OutputStream &S) const {
1108 if (S.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
1109 S.CurrentPackMax = static_cast<unsigned>(Data.size());
1110 S.CurrentPackIndex = 0;
1111 }
1112 }
1113
1114public:
1115 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
1116 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
1117 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1118 return P->ArrayCache == Cache::No;
1119 }))
1120 ArrayCache = Cache::No;
1121 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1122 return P->FunctionCache == Cache::No;
1123 }))
1124 FunctionCache = Cache::No;
1125 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1126 return P->RHSComponentCache == Cache::No;
1127 }))
1128 RHSComponentCache = Cache::No;
1129 }
1130
1131 template<typename Fn> void match(Fn F) const { F(Data); }
1132
1133 bool hasRHSComponentSlow(OutputStream &S) const override {
1134 initializePackExpansion(S);
1135 size_t Idx = S.CurrentPackIndex;
1136 return Idx < Data.size() && Data[Idx]->hasRHSComponent(S);
1137 }
1138 bool hasArraySlow(OutputStream &S) const override {
1139 initializePackExpansion(S);
1140 size_t Idx = S.CurrentPackIndex;
1141 return Idx < Data.size() && Data[Idx]->hasArray(S);
1142 }
1143 bool hasFunctionSlow(OutputStream &S) const override {
1144 initializePackExpansion(S);
1145 size_t Idx = S.CurrentPackIndex;
1146 return Idx < Data.size() && Data[Idx]->hasFunction(S);
1147 }
1148 const Node *getSyntaxNode(OutputStream &S) const override {
1149 initializePackExpansion(S);
1150 size_t Idx = S.CurrentPackIndex;
1151 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(S) : this;
1152 }
1153
1154 void printLeft(OutputStream &S) const override {
1155 initializePackExpansion(S);
1156 size_t Idx = S.CurrentPackIndex;
1157 if (Idx < Data.size())
1158 Data[Idx]->printLeft(S);
1159 }
1160 void printRight(OutputStream &S) const override {
1161 initializePackExpansion(S);
1162 size_t Idx = S.CurrentPackIndex;
1163 if (Idx < Data.size())
1164 Data[Idx]->printRight(S);
1165 }
1166};
1167
1168/// A variadic template argument. This node represents an occurrence of
1169/// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1170/// one of it's Elements is. The parser inserts a ParameterPack into the
1171/// TemplateParams table if the <template-args> this pack belongs to apply to an
1172/// <encoding>.
1173class TemplateArgumentPack final : public Node {
1174 NodeArray Elements;
1175public:
1176 TemplateArgumentPack(NodeArray Elements_)
1177 : Node(KTemplateArgumentPack), Elements(Elements_) {}
1178
1179 template<typename Fn> void match(Fn F) const { F(Elements); }
1180
1181 NodeArray getElements() const { return Elements; }
1182
1183 void printLeft(OutputStream &S) const override {
1184 Elements.printWithComma(S);
1185 }
1186};
1187
1188/// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1189/// which each have Child->ParameterPackSize elements.
1190class ParameterPackExpansion final : public Node {
1191 const Node *Child;
1192
1193public:
1194 ParameterPackExpansion(const Node *Child_)
1195 : Node(KParameterPackExpansion), Child(Child_) {}
1196
1197 template<typename Fn> void match(Fn F) const { F(Child); }
1198
1199 const Node *getChild() const { return Child; }
1200
1201 void printLeft(OutputStream &S) const override {
1202 constexpr unsigned Max = std::numeric_limits<unsigned>::max();
1203 SwapAndRestore<unsigned> SavePackIdx(S.CurrentPackIndex, Max);
1204 SwapAndRestore<unsigned> SavePackMax(S.CurrentPackMax, Max);
1205 size_t StreamPos = S.getCurrentPosition();
1206
1207 // Print the first element in the pack. If Child contains a ParameterPack,
1208 // it will set up S.CurrentPackMax and print the first element.
1209 Child->print(S);
1210
1211 // No ParameterPack was found in Child. This can occur if we've found a pack
1212 // expansion on a <function-param>.
1213 if (S.CurrentPackMax == Max) {
1214 S += "...";
1215 return;
1216 }
1217
1218 // We found a ParameterPack, but it has no elements. Erase whatever we may
1219 // of printed.
1220 if (S.CurrentPackMax == 0) {
1221 S.setCurrentPosition(StreamPos);
1222 return;
1223 }
1224
1225 // Else, iterate through the rest of the elements in the pack.
1226 for (unsigned I = 1, E = S.CurrentPackMax; I < E; ++I) {
1227 S += ", ";
1228 S.CurrentPackIndex = I;
1229 Child->print(S);
1230 }
1231 }
1232};
1233
1234class TemplateArgs final : public Node {
1235 NodeArray Params;
1236
1237public:
1238 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1239
1240 template<typename Fn> void match(Fn F) const { F(Params); }
1241
1242 NodeArray getParams() { return Params; }
1243
1244 void printLeft(OutputStream &S) const override {
1245 S += "<";
1246 Params.printWithComma(S);
1247 if (S.back() == '>')
1248 S += " ";
1249 S += ">";
1250 }
1251};
1252
Richard Smithb485b352018-08-24 23:30:26 +00001253/// A forward-reference to a template argument that was not known at the point
1254/// where the template parameter name was parsed in a mangling.
1255///
1256/// This is created when demangling the name of a specialization of a
1257/// conversion function template:
1258///
1259/// \code
1260/// struct A {
1261/// template<typename T> operator T*();
1262/// };
1263/// \endcode
1264///
1265/// When demangling a specialization of the conversion function template, we
1266/// encounter the name of the template (including the \c T) before we reach
1267/// the template argument list, so we cannot substitute the parameter name
1268/// for the corresponding argument while parsing. Instead, we create a
1269/// \c ForwardTemplateReference node that is resolved after we parse the
1270/// template arguments.
Richard Smithc20d1442018-08-20 20:14:49 +00001271struct ForwardTemplateReference : Node {
1272 size_t Index;
1273 Node *Ref = nullptr;
1274
1275 // If we're currently printing this node. It is possible (though invalid) for
1276 // a forward template reference to refer to itself via a substitution. This
1277 // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1278 // out if more than one print* function is active.
1279 mutable bool Printing = false;
1280
1281 ForwardTemplateReference(size_t Index_)
1282 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1283 Cache::Unknown),
1284 Index(Index_) {}
1285
1286 // We don't provide a matcher for these, because the value of the node is
1287 // not determined by its construction parameters, and it generally needs
1288 // special handling.
1289 template<typename Fn> void match(Fn F) const = delete;
1290
1291 bool hasRHSComponentSlow(OutputStream &S) const override {
1292 if (Printing)
1293 return false;
1294 SwapAndRestore<bool> SavePrinting(Printing, true);
1295 return Ref->hasRHSComponent(S);
1296 }
1297 bool hasArraySlow(OutputStream &S) const override {
1298 if (Printing)
1299 return false;
1300 SwapAndRestore<bool> SavePrinting(Printing, true);
1301 return Ref->hasArray(S);
1302 }
1303 bool hasFunctionSlow(OutputStream &S) const override {
1304 if (Printing)
1305 return false;
1306 SwapAndRestore<bool> SavePrinting(Printing, true);
1307 return Ref->hasFunction(S);
1308 }
1309 const Node *getSyntaxNode(OutputStream &S) const override {
1310 if (Printing)
1311 return this;
1312 SwapAndRestore<bool> SavePrinting(Printing, true);
1313 return Ref->getSyntaxNode(S);
1314 }
1315
1316 void printLeft(OutputStream &S) const override {
1317 if (Printing)
1318 return;
1319 SwapAndRestore<bool> SavePrinting(Printing, true);
1320 Ref->printLeft(S);
1321 }
1322 void printRight(OutputStream &S) const override {
1323 if (Printing)
1324 return;
1325 SwapAndRestore<bool> SavePrinting(Printing, true);
1326 Ref->printRight(S);
1327 }
1328};
1329
1330struct NameWithTemplateArgs : Node {
1331 // name<template_args>
1332 Node *Name;
1333 Node *TemplateArgs;
1334
1335 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1336 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1337
1338 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1339
1340 StringView getBaseName() const override { return Name->getBaseName(); }
1341
1342 void printLeft(OutputStream &S) const override {
1343 Name->print(S);
1344 TemplateArgs->print(S);
1345 }
1346};
1347
1348class GlobalQualifiedName final : public Node {
1349 Node *Child;
1350
1351public:
1352 GlobalQualifiedName(Node* Child_)
1353 : Node(KGlobalQualifiedName), Child(Child_) {}
1354
1355 template<typename Fn> void match(Fn F) const { F(Child); }
1356
1357 StringView getBaseName() const override { return Child->getBaseName(); }
1358
1359 void printLeft(OutputStream &S) const override {
1360 S += "::";
1361 Child->print(S);
1362 }
1363};
1364
1365struct StdQualifiedName : Node {
1366 Node *Child;
1367
1368 StdQualifiedName(Node *Child_) : Node(KStdQualifiedName), Child(Child_) {}
1369
1370 template<typename Fn> void match(Fn F) const { F(Child); }
1371
1372 StringView getBaseName() const override { return Child->getBaseName(); }
1373
1374 void printLeft(OutputStream &S) const override {
1375 S += "std::";
1376 Child->print(S);
1377 }
1378};
1379
1380enum class SpecialSubKind {
1381 allocator,
1382 basic_string,
1383 string,
1384 istream,
1385 ostream,
1386 iostream,
1387};
1388
1389class ExpandedSpecialSubstitution final : public Node {
1390 SpecialSubKind SSK;
1391
1392public:
1393 ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1394 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1395
1396 template<typename Fn> void match(Fn F) const { F(SSK); }
1397
1398 StringView getBaseName() const override {
1399 switch (SSK) {
1400 case SpecialSubKind::allocator:
1401 return StringView("allocator");
1402 case SpecialSubKind::basic_string:
1403 return StringView("basic_string");
1404 case SpecialSubKind::string:
1405 return StringView("basic_string");
1406 case SpecialSubKind::istream:
1407 return StringView("basic_istream");
1408 case SpecialSubKind::ostream:
1409 return StringView("basic_ostream");
1410 case SpecialSubKind::iostream:
1411 return StringView("basic_iostream");
1412 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001413 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001414 }
1415
1416 void printLeft(OutputStream &S) const override {
1417 switch (SSK) {
1418 case SpecialSubKind::allocator:
Richard Smithb485b352018-08-24 23:30:26 +00001419 S += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001420 break;
1421 case SpecialSubKind::basic_string:
Richard Smithb485b352018-08-24 23:30:26 +00001422 S += "std::basic_string";
1423 break;
Richard Smithc20d1442018-08-20 20:14:49 +00001424 case SpecialSubKind::string:
1425 S += "std::basic_string<char, std::char_traits<char>, "
1426 "std::allocator<char> >";
1427 break;
1428 case SpecialSubKind::istream:
1429 S += "std::basic_istream<char, std::char_traits<char> >";
1430 break;
1431 case SpecialSubKind::ostream:
1432 S += "std::basic_ostream<char, std::char_traits<char> >";
1433 break;
1434 case SpecialSubKind::iostream:
1435 S += "std::basic_iostream<char, std::char_traits<char> >";
1436 break;
1437 }
1438 }
1439};
1440
1441class SpecialSubstitution final : public Node {
1442public:
1443 SpecialSubKind SSK;
1444
1445 SpecialSubstitution(SpecialSubKind SSK_)
1446 : Node(KSpecialSubstitution), SSK(SSK_) {}
1447
1448 template<typename Fn> void match(Fn F) const { F(SSK); }
1449
1450 StringView getBaseName() const override {
1451 switch (SSK) {
1452 case SpecialSubKind::allocator:
1453 return StringView("allocator");
1454 case SpecialSubKind::basic_string:
1455 return StringView("basic_string");
1456 case SpecialSubKind::string:
1457 return StringView("string");
1458 case SpecialSubKind::istream:
1459 return StringView("istream");
1460 case SpecialSubKind::ostream:
1461 return StringView("ostream");
1462 case SpecialSubKind::iostream:
1463 return StringView("iostream");
1464 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001465 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001466 }
1467
1468 void printLeft(OutputStream &S) const override {
1469 switch (SSK) {
1470 case SpecialSubKind::allocator:
1471 S += "std::allocator";
1472 break;
1473 case SpecialSubKind::basic_string:
1474 S += "std::basic_string";
1475 break;
1476 case SpecialSubKind::string:
1477 S += "std::string";
1478 break;
1479 case SpecialSubKind::istream:
1480 S += "std::istream";
1481 break;
1482 case SpecialSubKind::ostream:
1483 S += "std::ostream";
1484 break;
1485 case SpecialSubKind::iostream:
1486 S += "std::iostream";
1487 break;
1488 }
1489 }
1490};
1491
1492class CtorDtorName final : public Node {
1493 const Node *Basename;
1494 const bool IsDtor;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001495 const int Variant;
Richard Smithc20d1442018-08-20 20:14:49 +00001496
1497public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001498 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1499 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1500 Variant(Variant_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001501
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001502 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
Richard Smithc20d1442018-08-20 20:14:49 +00001503
1504 void printLeft(OutputStream &S) const override {
1505 if (IsDtor)
1506 S += "~";
1507 S += Basename->getBaseName();
1508 }
1509};
1510
1511class DtorName : public Node {
1512 const Node *Base;
1513
1514public:
1515 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1516
1517 template<typename Fn> void match(Fn F) const { F(Base); }
1518
1519 void printLeft(OutputStream &S) const override {
1520 S += "~";
1521 Base->printLeft(S);
1522 }
1523};
1524
1525class UnnamedTypeName : public Node {
1526 const StringView Count;
1527
1528public:
1529 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1530
1531 template<typename Fn> void match(Fn F) const { F(Count); }
1532
1533 void printLeft(OutputStream &S) const override {
1534 S += "'unnamed";
1535 S += Count;
1536 S += "\'";
1537 }
1538};
1539
1540class ClosureTypeName : public Node {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001541 NodeArray TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00001542 NodeArray Params;
1543 StringView Count;
1544
1545public:
Richard Smithdf1c14c2019-09-06 23:53:21 +00001546 ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_,
1547 StringView Count_)
1548 : Node(KClosureTypeName), TemplateParams(TemplateParams_),
1549 Params(Params_), Count(Count_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001550
Richard Smithdf1c14c2019-09-06 23:53:21 +00001551 template<typename Fn> void match(Fn F) const {
1552 F(TemplateParams, Params, Count);
1553 }
1554
1555 void printDeclarator(OutputStream &S) const {
1556 if (!TemplateParams.empty()) {
1557 S += "<";
1558 TemplateParams.printWithComma(S);
1559 S += ">";
1560 }
1561 S += "(";
1562 Params.printWithComma(S);
1563 S += ")";
1564 }
Richard Smithc20d1442018-08-20 20:14:49 +00001565
1566 void printLeft(OutputStream &S) const override {
1567 S += "\'lambda";
1568 S += Count;
Richard Smithdf1c14c2019-09-06 23:53:21 +00001569 S += "\'";
1570 printDeclarator(S);
Richard Smithc20d1442018-08-20 20:14:49 +00001571 }
1572};
1573
1574class StructuredBindingName : public Node {
1575 NodeArray Bindings;
1576public:
1577 StructuredBindingName(NodeArray Bindings_)
1578 : Node(KStructuredBindingName), Bindings(Bindings_) {}
1579
1580 template<typename Fn> void match(Fn F) const { F(Bindings); }
1581
1582 void printLeft(OutputStream &S) const override {
1583 S += '[';
1584 Bindings.printWithComma(S);
1585 S += ']';
1586 }
1587};
1588
1589// -- Expression Nodes --
1590
1591class BinaryExpr : public Node {
1592 const Node *LHS;
1593 const StringView InfixOperator;
1594 const Node *RHS;
1595
1596public:
1597 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
1598 : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
1599 }
1600
1601 template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
1602
1603 void printLeft(OutputStream &S) const override {
1604 // might be a template argument expression, then we need to disambiguate
1605 // with parens.
1606 if (InfixOperator == ">")
1607 S += "(";
1608
1609 S += "(";
1610 LHS->print(S);
1611 S += ") ";
1612 S += InfixOperator;
1613 S += " (";
1614 RHS->print(S);
1615 S += ")";
1616
1617 if (InfixOperator == ">")
1618 S += ")";
1619 }
1620};
1621
1622class ArraySubscriptExpr : public Node {
1623 const Node *Op1;
1624 const Node *Op2;
1625
1626public:
1627 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
1628 : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
1629
1630 template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
1631
1632 void printLeft(OutputStream &S) const override {
1633 S += "(";
1634 Op1->print(S);
1635 S += ")[";
1636 Op2->print(S);
1637 S += "]";
1638 }
1639};
1640
1641class PostfixExpr : public Node {
1642 const Node *Child;
1643 const StringView Operator;
1644
1645public:
1646 PostfixExpr(const Node *Child_, StringView Operator_)
1647 : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
1648
1649 template<typename Fn> void match(Fn F) const { F(Child, Operator); }
1650
1651 void printLeft(OutputStream &S) const override {
1652 S += "(";
1653 Child->print(S);
1654 S += ")";
1655 S += Operator;
1656 }
1657};
1658
1659class ConditionalExpr : public Node {
1660 const Node *Cond;
1661 const Node *Then;
1662 const Node *Else;
1663
1664public:
1665 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
1666 : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
1667
1668 template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
1669
1670 void printLeft(OutputStream &S) const override {
1671 S += "(";
1672 Cond->print(S);
1673 S += ") ? (";
1674 Then->print(S);
1675 S += ") : (";
1676 Else->print(S);
1677 S += ")";
1678 }
1679};
1680
1681class MemberExpr : public Node {
1682 const Node *LHS;
1683 const StringView Kind;
1684 const Node *RHS;
1685
1686public:
1687 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
1688 : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1689
1690 template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
1691
1692 void printLeft(OutputStream &S) const override {
1693 LHS->print(S);
1694 S += Kind;
1695 RHS->print(S);
1696 }
1697};
1698
1699class EnclosingExpr : public Node {
1700 const StringView Prefix;
1701 const Node *Infix;
1702 const StringView Postfix;
1703
1704public:
1705 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1706 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1707 Postfix(Postfix_) {}
1708
1709 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1710
1711 void printLeft(OutputStream &S) const override {
1712 S += Prefix;
1713 Infix->print(S);
1714 S += Postfix;
1715 }
1716};
1717
1718class CastExpr : public Node {
1719 // cast_kind<to>(from)
1720 const StringView CastKind;
1721 const Node *To;
1722 const Node *From;
1723
1724public:
1725 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1726 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1727
1728 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1729
1730 void printLeft(OutputStream &S) const override {
1731 S += CastKind;
1732 S += "<";
1733 To->printLeft(S);
1734 S += ">(";
1735 From->printLeft(S);
1736 S += ")";
1737 }
1738};
1739
1740class SizeofParamPackExpr : public Node {
1741 const Node *Pack;
1742
1743public:
1744 SizeofParamPackExpr(const Node *Pack_)
1745 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1746
1747 template<typename Fn> void match(Fn F) const { F(Pack); }
1748
1749 void printLeft(OutputStream &S) const override {
1750 S += "sizeof...(";
1751 ParameterPackExpansion PPE(Pack);
1752 PPE.printLeft(S);
1753 S += ")";
1754 }
1755};
1756
1757class CallExpr : public Node {
1758 const Node *Callee;
1759 NodeArray Args;
1760
1761public:
1762 CallExpr(const Node *Callee_, NodeArray Args_)
1763 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1764
1765 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1766
1767 void printLeft(OutputStream &S) const override {
1768 Callee->print(S);
1769 S += "(";
1770 Args.printWithComma(S);
1771 S += ")";
1772 }
1773};
1774
1775class NewExpr : public Node {
1776 // new (expr_list) type(init_list)
1777 NodeArray ExprList;
1778 Node *Type;
1779 NodeArray InitList;
1780 bool IsGlobal; // ::operator new ?
1781 bool IsArray; // new[] ?
1782public:
1783 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1784 bool IsArray_)
1785 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1786 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1787
1788 template<typename Fn> void match(Fn F) const {
1789 F(ExprList, Type, InitList, IsGlobal, IsArray);
1790 }
1791
1792 void printLeft(OutputStream &S) const override {
1793 if (IsGlobal)
1794 S += "::operator ";
1795 S += "new";
1796 if (IsArray)
1797 S += "[]";
1798 S += ' ';
1799 if (!ExprList.empty()) {
1800 S += "(";
1801 ExprList.printWithComma(S);
1802 S += ")";
1803 }
1804 Type->print(S);
1805 if (!InitList.empty()) {
1806 S += "(";
1807 InitList.printWithComma(S);
1808 S += ")";
1809 }
1810
1811 }
1812};
1813
1814class DeleteExpr : public Node {
1815 Node *Op;
1816 bool IsGlobal;
1817 bool IsArray;
1818
1819public:
1820 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1821 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1822
1823 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1824
1825 void printLeft(OutputStream &S) const override {
1826 if (IsGlobal)
1827 S += "::";
1828 S += "delete";
1829 if (IsArray)
1830 S += "[] ";
1831 Op->print(S);
1832 }
1833};
1834
1835class PrefixExpr : public Node {
1836 StringView Prefix;
1837 Node *Child;
1838
1839public:
1840 PrefixExpr(StringView Prefix_, Node *Child_)
1841 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1842
1843 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1844
1845 void printLeft(OutputStream &S) const override {
1846 S += Prefix;
1847 S += "(";
1848 Child->print(S);
1849 S += ")";
1850 }
1851};
1852
1853class FunctionParam : public Node {
1854 StringView Number;
1855
1856public:
1857 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
1858
1859 template<typename Fn> void match(Fn F) const { F(Number); }
1860
1861 void printLeft(OutputStream &S) const override {
1862 S += "fp";
1863 S += Number;
1864 }
1865};
1866
1867class ConversionExpr : public Node {
1868 const Node *Type;
1869 NodeArray Expressions;
1870
1871public:
1872 ConversionExpr(const Node *Type_, NodeArray Expressions_)
1873 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
1874
1875 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
1876
1877 void printLeft(OutputStream &S) const override {
1878 S += "(";
1879 Type->print(S);
1880 S += ")(";
1881 Expressions.printWithComma(S);
1882 S += ")";
1883 }
1884};
1885
1886class InitListExpr : public Node {
1887 const Node *Ty;
1888 NodeArray Inits;
1889public:
1890 InitListExpr(const Node *Ty_, NodeArray Inits_)
1891 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
1892
1893 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
1894
1895 void printLeft(OutputStream &S) const override {
1896 if (Ty)
1897 Ty->print(S);
1898 S += '{';
1899 Inits.printWithComma(S);
1900 S += '}';
1901 }
1902};
1903
1904class BracedExpr : public Node {
1905 const Node *Elem;
1906 const Node *Init;
1907 bool IsArray;
1908public:
1909 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
1910 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
1911
1912 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
1913
1914 void printLeft(OutputStream &S) const override {
1915 if (IsArray) {
1916 S += '[';
1917 Elem->print(S);
1918 S += ']';
1919 } else {
1920 S += '.';
1921 Elem->print(S);
1922 }
1923 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
1924 S += " = ";
1925 Init->print(S);
1926 }
1927};
1928
1929class BracedRangeExpr : public Node {
1930 const Node *First;
1931 const Node *Last;
1932 const Node *Init;
1933public:
1934 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
1935 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
1936
1937 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
1938
1939 void printLeft(OutputStream &S) const override {
1940 S += '[';
1941 First->print(S);
1942 S += " ... ";
1943 Last->print(S);
1944 S += ']';
1945 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
1946 S += " = ";
1947 Init->print(S);
1948 }
1949};
1950
1951class FoldExpr : public Node {
1952 const Node *Pack, *Init;
1953 StringView OperatorName;
1954 bool IsLeftFold;
1955
1956public:
1957 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
1958 const Node *Init_)
1959 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
1960 IsLeftFold(IsLeftFold_) {}
1961
1962 template<typename Fn> void match(Fn F) const {
1963 F(IsLeftFold, OperatorName, Pack, Init);
1964 }
1965
1966 void printLeft(OutputStream &S) const override {
1967 auto PrintPack = [&] {
1968 S += '(';
1969 ParameterPackExpansion(Pack).print(S);
1970 S += ')';
1971 };
1972
1973 S += '(';
1974
1975 if (IsLeftFold) {
1976 // init op ... op pack
1977 if (Init != nullptr) {
1978 Init->print(S);
1979 S += ' ';
1980 S += OperatorName;
1981 S += ' ';
1982 }
1983 // ... op pack
1984 S += "... ";
1985 S += OperatorName;
1986 S += ' ';
1987 PrintPack();
1988 } else { // !IsLeftFold
1989 // pack op ...
1990 PrintPack();
1991 S += ' ';
1992 S += OperatorName;
1993 S += " ...";
1994 // pack op ... op init
1995 if (Init != nullptr) {
1996 S += ' ';
1997 S += OperatorName;
1998 S += ' ';
1999 Init->print(S);
2000 }
2001 }
2002 S += ')';
2003 }
2004};
2005
2006class ThrowExpr : public Node {
2007 const Node *Op;
2008
2009public:
2010 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
2011
2012 template<typename Fn> void match(Fn F) const { F(Op); }
2013
2014 void printLeft(OutputStream &S) const override {
2015 S += "throw ";
2016 Op->print(S);
2017 }
2018};
2019
Erik Pilkingtone8457c62019-06-18 23:34:09 +00002020// MSVC __uuidof extension, generated by clang in -fms-extensions mode.
2021class UUIDOfExpr : public Node {
2022 Node *Operand;
2023public:
2024 UUIDOfExpr(Node *Operand_) : Node(KUUIDOfExpr), Operand(Operand_) {}
2025
2026 template<typename Fn> void match(Fn F) const { F(Operand); }
2027
2028 void printLeft(OutputStream &S) const override {
2029 S << "__uuidof(";
2030 Operand->print(S);
2031 S << ")";
2032 }
2033};
2034
Richard Smithc20d1442018-08-20 20:14:49 +00002035class BoolExpr : public Node {
2036 bool Value;
2037
2038public:
2039 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
2040
2041 template<typename Fn> void match(Fn F) const { F(Value); }
2042
2043 void printLeft(OutputStream &S) const override {
2044 S += Value ? StringView("true") : StringView("false");
2045 }
2046};
2047
Richard Smithdf1c14c2019-09-06 23:53:21 +00002048class StringLiteral : public Node {
2049 const Node *Type;
2050
2051public:
2052 StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
2053
2054 template<typename Fn> void match(Fn F) const { F(Type); }
2055
2056 void printLeft(OutputStream &S) const override {
2057 S += "\"<";
2058 Type->print(S);
2059 S += ">\"";
2060 }
2061};
2062
2063class LambdaExpr : public Node {
2064 const Node *Type;
2065
Richard Smithdf1c14c2019-09-06 23:53:21 +00002066public:
2067 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2068
2069 template<typename Fn> void match(Fn F) const { F(Type); }
2070
2071 void printLeft(OutputStream &S) const override {
2072 S += "[]";
Richard Smithfb917462019-09-09 22:26:04 +00002073 if (Type->getKind() == KClosureTypeName)
2074 static_cast<const ClosureTypeName *>(Type)->printDeclarator(S);
Richard Smithdf1c14c2019-09-06 23:53:21 +00002075 S += "{...}";
2076 }
2077};
2078
Richard Smithc20d1442018-08-20 20:14:49 +00002079class IntegerCastExpr : public Node {
2080 // ty(integer)
2081 const Node *Ty;
2082 StringView Integer;
2083
2084public:
2085 IntegerCastExpr(const Node *Ty_, StringView Integer_)
2086 : Node(KIntegerCastExpr), Ty(Ty_), Integer(Integer_) {}
2087
2088 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2089
2090 void printLeft(OutputStream &S) const override {
2091 S += "(";
2092 Ty->print(S);
2093 S += ")";
2094 S += Integer;
2095 }
2096};
2097
2098class IntegerLiteral : public Node {
2099 StringView Type;
2100 StringView Value;
2101
2102public:
2103 IntegerLiteral(StringView Type_, StringView Value_)
2104 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2105
2106 template<typename Fn> void match(Fn F) const { F(Type, Value); }
2107
2108 void printLeft(OutputStream &S) const override {
2109 if (Type.size() > 3) {
2110 S += "(";
2111 S += Type;
2112 S += ")";
2113 }
2114
2115 if (Value[0] == 'n') {
2116 S += "-";
2117 S += Value.dropFront(1);
2118 } else
2119 S += Value;
2120
2121 if (Type.size() <= 3)
2122 S += Type;
2123 }
2124};
2125
2126template <class Float> struct FloatData;
2127
2128namespace float_literal_impl {
2129constexpr Node::Kind getFloatLiteralKind(float *) {
2130 return Node::KFloatLiteral;
2131}
2132constexpr Node::Kind getFloatLiteralKind(double *) {
2133 return Node::KDoubleLiteral;
2134}
2135constexpr Node::Kind getFloatLiteralKind(long double *) {
2136 return Node::KLongDoubleLiteral;
2137}
2138}
2139
2140template <class Float> class FloatLiteralImpl : public Node {
2141 const StringView Contents;
2142
2143 static constexpr Kind KindForClass =
2144 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2145
2146public:
2147 FloatLiteralImpl(StringView Contents_)
2148 : Node(KindForClass), Contents(Contents_) {}
2149
2150 template<typename Fn> void match(Fn F) const { F(Contents); }
2151
2152 void printLeft(OutputStream &s) const override {
2153 const char *first = Contents.begin();
2154 const char *last = Contents.end() + 1;
2155
2156 const size_t N = FloatData<Float>::mangled_size;
2157 if (static_cast<std::size_t>(last - first) > N) {
2158 last = first + N;
2159 union {
2160 Float value;
2161 char buf[sizeof(Float)];
2162 };
2163 const char *t = first;
2164 char *e = buf;
2165 for (; t != last; ++t, ++e) {
2166 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2167 : static_cast<unsigned>(*t - 'a' + 10);
2168 ++t;
2169 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2170 : static_cast<unsigned>(*t - 'a' + 10);
2171 *e = static_cast<char>((d1 << 4) + d0);
2172 }
2173#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2174 std::reverse(buf, e);
2175#endif
2176 char num[FloatData<Float>::max_demangled_size] = {0};
2177 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
2178 s += StringView(num, num + n);
2179 }
2180 }
2181};
2182
2183using FloatLiteral = FloatLiteralImpl<float>;
2184using DoubleLiteral = FloatLiteralImpl<double>;
2185using LongDoubleLiteral = FloatLiteralImpl<long double>;
2186
2187/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2188/// appropriate derived class.
2189template<typename Fn>
2190void Node::visit(Fn F) const {
2191 switch (K) {
2192#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2193 FOR_EACH_NODE_KIND(CASE)
2194#undef CASE
2195 }
2196 assert(0 && "unknown mangling node kind");
2197}
2198
2199/// Determine the kind of a node from its type.
2200template<typename NodeT> struct NodeKind;
2201#define SPECIALIZATION(X) \
2202 template<> struct NodeKind<X> { \
2203 static constexpr Node::Kind Kind = Node::K##X; \
2204 static constexpr const char *name() { return #X; } \
2205 };
2206FOR_EACH_NODE_KIND(SPECIALIZATION)
2207#undef SPECIALIZATION
2208
2209#undef FOR_EACH_NODE_KIND
2210
2211template <class T, size_t N>
2212class PODSmallVector {
2213 static_assert(std::is_pod<T>::value,
2214 "T is required to be a plain old data type");
2215
2216 T* First;
2217 T* Last;
2218 T* Cap;
2219 T Inline[N];
2220
2221 bool isInline() const { return First == Inline; }
2222
2223 void clearInline() {
2224 First = Inline;
2225 Last = Inline;
2226 Cap = Inline + N;
2227 }
2228
2229 void reserve(size_t NewCap) {
2230 size_t S = size();
2231 if (isInline()) {
2232 auto* Tmp = static_cast<T*>(std::malloc(NewCap * sizeof(T)));
2233 if (Tmp == nullptr)
2234 std::terminate();
2235 std::copy(First, Last, Tmp);
2236 First = Tmp;
2237 } else {
2238 First = static_cast<T*>(std::realloc(First, NewCap * sizeof(T)));
2239 if (First == nullptr)
2240 std::terminate();
2241 }
2242 Last = First + S;
2243 Cap = First + NewCap;
2244 }
2245
2246public:
2247 PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
2248
2249 PODSmallVector(const PODSmallVector&) = delete;
2250 PODSmallVector& operator=(const PODSmallVector&) = delete;
2251
2252 PODSmallVector(PODSmallVector&& Other) : PODSmallVector() {
2253 if (Other.isInline()) {
2254 std::copy(Other.begin(), Other.end(), First);
2255 Last = First + Other.size();
2256 Other.clear();
2257 return;
2258 }
2259
2260 First = Other.First;
2261 Last = Other.Last;
2262 Cap = Other.Cap;
2263 Other.clearInline();
2264 }
2265
2266 PODSmallVector& operator=(PODSmallVector&& Other) {
2267 if (Other.isInline()) {
2268 if (!isInline()) {
2269 std::free(First);
2270 clearInline();
2271 }
2272 std::copy(Other.begin(), Other.end(), First);
2273 Last = First + Other.size();
2274 Other.clear();
2275 return *this;
2276 }
2277
2278 if (isInline()) {
2279 First = Other.First;
2280 Last = Other.Last;
2281 Cap = Other.Cap;
2282 Other.clearInline();
2283 return *this;
2284 }
2285
2286 std::swap(First, Other.First);
2287 std::swap(Last, Other.Last);
2288 std::swap(Cap, Other.Cap);
2289 Other.clear();
2290 return *this;
2291 }
2292
2293 void push_back(const T& Elem) {
2294 if (Last == Cap)
2295 reserve(size() * 2);
2296 *Last++ = Elem;
2297 }
2298
2299 void pop_back() {
2300 assert(Last != First && "Popping empty vector!");
2301 --Last;
2302 }
2303
2304 void dropBack(size_t Index) {
2305 assert(Index <= size() && "dropBack() can't expand!");
2306 Last = First + Index;
2307 }
2308
2309 T* begin() { return First; }
2310 T* end() { return Last; }
2311
2312 bool empty() const { return First == Last; }
2313 size_t size() const { return static_cast<size_t>(Last - First); }
2314 T& back() {
2315 assert(Last != First && "Calling back() on empty vector!");
2316 return *(Last - 1);
2317 }
2318 T& operator[](size_t Index) {
2319 assert(Index < size() && "Invalid access!");
2320 return *(begin() + Index);
2321 }
2322 void clear() { Last = First; }
2323
2324 ~PODSmallVector() {
2325 if (!isInline())
2326 std::free(First);
2327 }
2328};
2329
Pavel Labathba825192018-10-16 14:29:14 +00002330template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002331 const char *First;
2332 const char *Last;
2333
2334 // Name stack, this is used by the parser to hold temporary names that were
2335 // parsed. The parser collapses multiple names into new nodes to construct
2336 // the AST. Once the parser is finished, names.size() == 1.
2337 PODSmallVector<Node *, 32> Names;
2338
2339 // Substitution table. Itanium supports name substitutions as a means of
2340 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2341 // table.
2342 PODSmallVector<Node *, 32> Subs;
2343
Richard Smithdf1c14c2019-09-06 23:53:21 +00002344 using TemplateParamList = PODSmallVector<Node *, 8>;
2345
2346 class ScopedTemplateParamList {
2347 AbstractManglingParser *Parser;
2348 size_t OldNumTemplateParamLists;
2349 TemplateParamList Params;
2350
2351 public:
2352 ScopedTemplateParamList(AbstractManglingParser *Parser)
2353 : Parser(Parser),
2354 OldNumTemplateParamLists(Parser->TemplateParams.size()) {
2355 Parser->TemplateParams.push_back(&Params);
2356 }
2357 ~ScopedTemplateParamList() {
2358 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2359 Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2360 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002361 };
2362
Richard Smithc20d1442018-08-20 20:14:49 +00002363 // Template parameter table. Like the above, but referenced like "T42_".
2364 // This has a smaller size compared to Subs and Names because it can be
2365 // stored on the stack.
Richard Smithdf1c14c2019-09-06 23:53:21 +00002366 TemplateParamList OuterTemplateParams;
2367
2368 // Lists of template parameters indexed by template parameter depth,
2369 // referenced like "TL2_4_". If nonempty, element 0 is always
2370 // OuterTemplateParams; inner elements are always template parameter lists of
2371 // lambda expressions. For a generic lambda with no explicit template
2372 // parameter list, the corresponding parameter list pointer will be null.
2373 PODSmallVector<TemplateParamList *, 4> TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00002374
2375 // Set of unresolved forward <template-param> references. These can occur in a
2376 // conversion operator's type, and are resolved in the enclosing <encoding>.
2377 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2378
Richard Smithc20d1442018-08-20 20:14:49 +00002379 bool TryToParseTemplateArgs = true;
2380 bool PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002381 size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2382
2383 unsigned NumSyntheticTemplateParameters[3] = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002384
2385 Alloc ASTAllocator;
2386
Pavel Labathba825192018-10-16 14:29:14 +00002387 AbstractManglingParser(const char *First_, const char *Last_)
2388 : First(First_), Last(Last_) {}
2389
2390 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002391
2392 void reset(const char *First_, const char *Last_) {
2393 First = First_;
2394 Last = Last_;
2395 Names.clear();
2396 Subs.clear();
2397 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002398 ParsingLambdaParamsAtLevel = (size_t)-1;
Richard Smithc20d1442018-08-20 20:14:49 +00002399 TryToParseTemplateArgs = true;
2400 PermitForwardTemplateReferences = false;
Richard Smith9a2307a2019-09-07 00:11:53 +00002401 for (int I = 0; I != 3; ++I)
2402 NumSyntheticTemplateParameters[I] = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00002403 ASTAllocator.reset();
2404 }
2405
Richard Smithb485b352018-08-24 23:30:26 +00002406 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002407 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2408 }
2409
2410 template <class It> NodeArray makeNodeArray(It begin, It end) {
2411 size_t sz = static_cast<size_t>(end - begin);
2412 void *mem = ASTAllocator.allocateNodeArray(sz);
2413 Node **data = new (mem) Node *[sz];
2414 std::copy(begin, end, data);
2415 return NodeArray(data, sz);
2416 }
2417
2418 NodeArray popTrailingNodeArray(size_t FromPosition) {
2419 assert(FromPosition <= Names.size());
2420 NodeArray res =
2421 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2422 Names.dropBack(FromPosition);
2423 return res;
2424 }
2425
2426 bool consumeIf(StringView S) {
2427 if (StringView(First, Last).startsWith(S)) {
2428 First += S.size();
2429 return true;
2430 }
2431 return false;
2432 }
2433
2434 bool consumeIf(char C) {
2435 if (First != Last && *First == C) {
2436 ++First;
2437 return true;
2438 }
2439 return false;
2440 }
2441
2442 char consume() { return First != Last ? *First++ : '\0'; }
2443
2444 char look(unsigned Lookahead = 0) {
2445 if (static_cast<size_t>(Last - First) <= Lookahead)
2446 return '\0';
2447 return First[Lookahead];
2448 }
2449
2450 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2451
2452 StringView parseNumber(bool AllowNegative = false);
2453 Qualifiers parseCVQualifiers();
2454 bool parsePositiveInteger(size_t *Out);
2455 StringView parseBareSourceName();
2456
2457 bool parseSeqId(size_t *Out);
2458 Node *parseSubstitution();
2459 Node *parseTemplateParam();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002460 Node *parseTemplateParamDecl();
Richard Smithc20d1442018-08-20 20:14:49 +00002461 Node *parseTemplateArgs(bool TagTemplates = false);
2462 Node *parseTemplateArg();
2463
2464 /// Parse the <expr> production.
2465 Node *parseExpr();
2466 Node *parsePrefixExpr(StringView Kind);
2467 Node *parseBinaryExpr(StringView Kind);
2468 Node *parseIntegerLiteral(StringView Lit);
2469 Node *parseExprPrimary();
2470 template <class Float> Node *parseFloatingLiteral();
2471 Node *parseFunctionParam();
2472 Node *parseNewExpr();
2473 Node *parseConversionExpr();
2474 Node *parseBracedExpr();
2475 Node *parseFoldExpr();
2476
2477 /// Parse the <type> production.
2478 Node *parseType();
2479 Node *parseFunctionType();
2480 Node *parseVectorType();
2481 Node *parseDecltype();
2482 Node *parseArrayType();
2483 Node *parsePointerToMemberType();
2484 Node *parseClassEnumType();
2485 Node *parseQualifiedType();
2486
2487 Node *parseEncoding();
2488 bool parseCallOffset();
2489 Node *parseSpecialName();
2490
2491 /// Holds some extra information about a <name> that is being parsed. This
2492 /// information is only pertinent if the <name> refers to an <encoding>.
2493 struct NameState {
2494 bool CtorDtorConversion = false;
2495 bool EndsWithTemplateArgs = false;
2496 Qualifiers CVQualifiers = QualNone;
2497 FunctionRefQual ReferenceQualifier = FrefQualNone;
2498 size_t ForwardTemplateRefsBegin;
2499
Pavel Labathba825192018-10-16 14:29:14 +00002500 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002501 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2502 };
2503
2504 bool resolveForwardTemplateRefs(NameState &State) {
2505 size_t I = State.ForwardTemplateRefsBegin;
2506 size_t E = ForwardTemplateRefs.size();
2507 for (; I < E; ++I) {
2508 size_t Idx = ForwardTemplateRefs[I]->Index;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002509 if (TemplateParams.empty() || !TemplateParams[0] ||
2510 Idx >= TemplateParams[0]->size())
Richard Smithc20d1442018-08-20 20:14:49 +00002511 return true;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002512 ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
Richard Smithc20d1442018-08-20 20:14:49 +00002513 }
2514 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2515 return false;
2516 }
2517
2518 /// Parse the <name> production>
2519 Node *parseName(NameState *State = nullptr);
2520 Node *parseLocalName(NameState *State);
2521 Node *parseOperatorName(NameState *State);
2522 Node *parseUnqualifiedName(NameState *State);
2523 Node *parseUnnamedTypeName(NameState *State);
2524 Node *parseSourceName(NameState *State);
2525 Node *parseUnscopedName(NameState *State);
2526 Node *parseNestedName(NameState *State);
2527 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2528
2529 Node *parseAbiTags(Node *N);
2530
2531 /// Parse the <unresolved-name> production.
2532 Node *parseUnresolvedName();
2533 Node *parseSimpleId();
2534 Node *parseBaseUnresolvedName();
2535 Node *parseUnresolvedType();
2536 Node *parseDestructorName();
2537
2538 /// Top-level entry point into the parser.
2539 Node *parse();
2540};
2541
2542const char* parse_discriminator(const char* first, const char* last);
2543
2544// <name> ::= <nested-name> // N
2545// ::= <local-name> # See Scope Encoding below // Z
2546// ::= <unscoped-template-name> <template-args>
2547// ::= <unscoped-name>
2548//
2549// <unscoped-template-name> ::= <unscoped-name>
2550// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002551template <typename Derived, typename Alloc>
2552Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002553 consumeIf('L'); // extension
2554
2555 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002556 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002557 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002558 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002559
2560 // ::= <unscoped-template-name> <template-args>
2561 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00002562 Node *S = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00002563 if (S == nullptr)
2564 return nullptr;
2565 if (look() != 'I')
2566 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002567 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002568 if (TA == nullptr)
2569 return nullptr;
2570 if (State) State->EndsWithTemplateArgs = true;
2571 return make<NameWithTemplateArgs>(S, TA);
2572 }
2573
Pavel Labathba825192018-10-16 14:29:14 +00002574 Node *N = getDerived().parseUnscopedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002575 if (N == nullptr)
2576 return nullptr;
2577 // ::= <unscoped-template-name> <template-args>
2578 if (look() == 'I') {
2579 Subs.push_back(N);
Pavel Labathba825192018-10-16 14:29:14 +00002580 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002581 if (TA == nullptr)
2582 return nullptr;
2583 if (State) State->EndsWithTemplateArgs = true;
2584 return make<NameWithTemplateArgs>(N, TA);
2585 }
2586 // ::= <unscoped-name>
2587 return N;
2588}
2589
2590// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2591// := Z <function encoding> E s [<discriminator>]
2592// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002593template <typename Derived, typename Alloc>
2594Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002595 if (!consumeIf('Z'))
2596 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002597 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002598 if (Encoding == nullptr || !consumeIf('E'))
2599 return nullptr;
2600
2601 if (consumeIf('s')) {
2602 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002603 auto *StringLitName = make<NameType>("string literal");
2604 if (!StringLitName)
2605 return nullptr;
2606 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002607 }
2608
2609 if (consumeIf('d')) {
2610 parseNumber(true);
2611 if (!consumeIf('_'))
2612 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002613 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002614 if (N == nullptr)
2615 return nullptr;
2616 return make<LocalName>(Encoding, N);
2617 }
2618
Pavel Labathba825192018-10-16 14:29:14 +00002619 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002620 if (Entity == nullptr)
2621 return nullptr;
2622 First = parse_discriminator(First, Last);
2623 return make<LocalName>(Encoding, Entity);
2624}
2625
2626// <unscoped-name> ::= <unqualified-name>
2627// ::= St <unqualified-name> # ::std::
2628// extension ::= StL<unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00002629template <typename Derived, typename Alloc>
2630Node *
2631AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
2632 if (consumeIf("StL") || consumeIf("St")) {
2633 Node *R = getDerived().parseUnqualifiedName(State);
2634 if (R == nullptr)
2635 return nullptr;
2636 return make<StdQualifiedName>(R);
2637 }
2638 return getDerived().parseUnqualifiedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002639}
2640
2641// <unqualified-name> ::= <operator-name> [abi-tags]
2642// ::= <ctor-dtor-name>
2643// ::= <source-name>
2644// ::= <unnamed-type-name>
2645// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002646template <typename Derived, typename Alloc>
2647Node *
2648AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002649 // <ctor-dtor-name>s are special-cased in parseNestedName().
2650 Node *Result;
2651 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002652 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002653 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002654 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002655 else if (consumeIf("DC")) {
2656 size_t BindingsBegin = Names.size();
2657 do {
Pavel Labathba825192018-10-16 14:29:14 +00002658 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002659 if (Binding == nullptr)
2660 return nullptr;
2661 Names.push_back(Binding);
2662 } while (!consumeIf('E'));
2663 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2664 } else
Pavel Labathba825192018-10-16 14:29:14 +00002665 Result = getDerived().parseOperatorName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002666 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002667 Result = getDerived().parseAbiTags(Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002668 return Result;
2669}
2670
2671// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2672// ::= <closure-type-name>
2673//
2674// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2675//
2676// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002677template <typename Derived, typename Alloc>
2678Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002679AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2680 // <template-params> refer to the innermost <template-args>. Clear out any
2681 // outer args that we may have inserted into TemplateParams.
2682 if (State != nullptr)
2683 TemplateParams.clear();
2684
Richard Smithc20d1442018-08-20 20:14:49 +00002685 if (consumeIf("Ut")) {
2686 StringView Count = parseNumber();
2687 if (!consumeIf('_'))
2688 return nullptr;
2689 return make<UnnamedTypeName>(Count);
2690 }
2691 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002692 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2693 TemplateParams.size());
2694 ScopedTemplateParamList LambdaTemplateParams(this);
2695
2696 size_t ParamsBegin = Names.size();
2697 while (look() == 'T' &&
2698 StringView("yptn").find(look(1)) != StringView::npos) {
2699 Node *T = parseTemplateParamDecl();
2700 if (!T)
2701 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002702 Names.push_back(T);
2703 }
2704 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2705
2706 // FIXME: If TempParams is empty and none of the function parameters
2707 // includes 'auto', we should remove LambdaTemplateParams from the
2708 // TemplateParams list. Unfortunately, we don't find out whether there are
2709 // any 'auto' parameters until too late in an example such as:
2710 //
2711 // template<typename T> void f(
2712 // decltype([](decltype([]<typename T>(T v) {}),
2713 // auto) {})) {}
2714 // template<typename T> void f(
2715 // decltype([](decltype([]<typename T>(T w) {}),
2716 // int) {})) {}
2717 //
2718 // Here, the type of v is at level 2 but the type of w is at level 1. We
2719 // don't find this out until we encounter the type of the next parameter.
2720 //
2721 // However, compilers can't actually cope with the former example in
2722 // practice, and it's likely to be made ill-formed in future, so we don't
2723 // need to support it here.
2724 //
2725 // If we encounter an 'auto' in the function parameter types, we will
2726 // recreate a template parameter scope for it, but any intervening lambdas
2727 // will be parsed in the 'wrong' template parameter depth.
2728 if (TempParams.empty())
2729 TemplateParams.pop_back();
2730
Richard Smithc20d1442018-08-20 20:14:49 +00002731 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002732 do {
Pavel Labathba825192018-10-16 14:29:14 +00002733 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002734 if (P == nullptr)
2735 return nullptr;
2736 Names.push_back(P);
2737 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002738 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002739 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2740
Richard Smithc20d1442018-08-20 20:14:49 +00002741 StringView Count = parseNumber();
2742 if (!consumeIf('_'))
2743 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002744 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002745 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002746 if (consumeIf("Ub")) {
2747 (void)parseNumber();
2748 if (!consumeIf('_'))
2749 return nullptr;
2750 return make<NameType>("'block-literal'");
2751 }
Richard Smithc20d1442018-08-20 20:14:49 +00002752 return nullptr;
2753}
2754
2755// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002756template <typename Derived, typename Alloc>
2757Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002758 size_t Length = 0;
2759 if (parsePositiveInteger(&Length))
2760 return nullptr;
2761 if (numLeft() < Length || Length == 0)
2762 return nullptr;
2763 StringView Name(First, First + Length);
2764 First += Length;
2765 if (Name.startsWith("_GLOBAL__N"))
2766 return make<NameType>("(anonymous namespace)");
2767 return make<NameType>(Name);
2768}
2769
2770// <operator-name> ::= aa # &&
2771// ::= ad # & (unary)
2772// ::= an # &
2773// ::= aN # &=
2774// ::= aS # =
2775// ::= cl # ()
2776// ::= cm # ,
2777// ::= co # ~
2778// ::= cv <type> # (cast)
2779// ::= da # delete[]
2780// ::= de # * (unary)
2781// ::= dl # delete
2782// ::= dv # /
2783// ::= dV # /=
2784// ::= eo # ^
2785// ::= eO # ^=
2786// ::= eq # ==
2787// ::= ge # >=
2788// ::= gt # >
2789// ::= ix # []
2790// ::= le # <=
2791// ::= li <source-name> # operator ""
2792// ::= ls # <<
2793// ::= lS # <<=
2794// ::= lt # <
2795// ::= mi # -
2796// ::= mI # -=
2797// ::= ml # *
2798// ::= mL # *=
2799// ::= mm # -- (postfix in <expression> context)
2800// ::= na # new[]
2801// ::= ne # !=
2802// ::= ng # - (unary)
2803// ::= nt # !
2804// ::= nw # new
2805// ::= oo # ||
2806// ::= or # |
2807// ::= oR # |=
2808// ::= pm # ->*
2809// ::= pl # +
2810// ::= pL # +=
2811// ::= pp # ++ (postfix in <expression> context)
2812// ::= ps # + (unary)
2813// ::= pt # ->
2814// ::= qu # ?
2815// ::= rm # %
2816// ::= rM # %=
2817// ::= rs # >>
2818// ::= rS # >>=
2819// ::= ss # <=> C++2a
2820// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002821template <typename Derived, typename Alloc>
2822Node *
2823AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002824 switch (look()) {
2825 case 'a':
2826 switch (look(1)) {
2827 case 'a':
2828 First += 2;
2829 return make<NameType>("operator&&");
2830 case 'd':
2831 case 'n':
2832 First += 2;
2833 return make<NameType>("operator&");
2834 case 'N':
2835 First += 2;
2836 return make<NameType>("operator&=");
2837 case 'S':
2838 First += 2;
2839 return make<NameType>("operator=");
2840 }
2841 return nullptr;
2842 case 'c':
2843 switch (look(1)) {
2844 case 'l':
2845 First += 2;
2846 return make<NameType>("operator()");
2847 case 'm':
2848 First += 2;
2849 return make<NameType>("operator,");
2850 case 'o':
2851 First += 2;
2852 return make<NameType>("operator~");
2853 // ::= cv <type> # (cast)
2854 case 'v': {
2855 First += 2;
2856 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2857 // If we're parsing an encoding, State != nullptr and the conversion
2858 // operators' <type> could have a <template-param> that refers to some
2859 // <template-arg>s further ahead in the mangled name.
2860 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2861 PermitForwardTemplateReferences ||
2862 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002863 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002864 if (Ty == nullptr)
2865 return nullptr;
2866 if (State) State->CtorDtorConversion = true;
2867 return make<ConversionOperatorType>(Ty);
2868 }
2869 }
2870 return nullptr;
2871 case 'd':
2872 switch (look(1)) {
2873 case 'a':
2874 First += 2;
2875 return make<NameType>("operator delete[]");
2876 case 'e':
2877 First += 2;
2878 return make<NameType>("operator*");
2879 case 'l':
2880 First += 2;
2881 return make<NameType>("operator delete");
2882 case 'v':
2883 First += 2;
2884 return make<NameType>("operator/");
2885 case 'V':
2886 First += 2;
2887 return make<NameType>("operator/=");
2888 }
2889 return nullptr;
2890 case 'e':
2891 switch (look(1)) {
2892 case 'o':
2893 First += 2;
2894 return make<NameType>("operator^");
2895 case 'O':
2896 First += 2;
2897 return make<NameType>("operator^=");
2898 case 'q':
2899 First += 2;
2900 return make<NameType>("operator==");
2901 }
2902 return nullptr;
2903 case 'g':
2904 switch (look(1)) {
2905 case 'e':
2906 First += 2;
2907 return make<NameType>("operator>=");
2908 case 't':
2909 First += 2;
2910 return make<NameType>("operator>");
2911 }
2912 return nullptr;
2913 case 'i':
2914 if (look(1) == 'x') {
2915 First += 2;
2916 return make<NameType>("operator[]");
2917 }
2918 return nullptr;
2919 case 'l':
2920 switch (look(1)) {
2921 case 'e':
2922 First += 2;
2923 return make<NameType>("operator<=");
2924 // ::= li <source-name> # operator ""
2925 case 'i': {
2926 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002927 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002928 if (SN == nullptr)
2929 return nullptr;
2930 return make<LiteralOperator>(SN);
2931 }
2932 case 's':
2933 First += 2;
2934 return make<NameType>("operator<<");
2935 case 'S':
2936 First += 2;
2937 return make<NameType>("operator<<=");
2938 case 't':
2939 First += 2;
2940 return make<NameType>("operator<");
2941 }
2942 return nullptr;
2943 case 'm':
2944 switch (look(1)) {
2945 case 'i':
2946 First += 2;
2947 return make<NameType>("operator-");
2948 case 'I':
2949 First += 2;
2950 return make<NameType>("operator-=");
2951 case 'l':
2952 First += 2;
2953 return make<NameType>("operator*");
2954 case 'L':
2955 First += 2;
2956 return make<NameType>("operator*=");
2957 case 'm':
2958 First += 2;
2959 return make<NameType>("operator--");
2960 }
2961 return nullptr;
2962 case 'n':
2963 switch (look(1)) {
2964 case 'a':
2965 First += 2;
2966 return make<NameType>("operator new[]");
2967 case 'e':
2968 First += 2;
2969 return make<NameType>("operator!=");
2970 case 'g':
2971 First += 2;
2972 return make<NameType>("operator-");
2973 case 't':
2974 First += 2;
2975 return make<NameType>("operator!");
2976 case 'w':
2977 First += 2;
2978 return make<NameType>("operator new");
2979 }
2980 return nullptr;
2981 case 'o':
2982 switch (look(1)) {
2983 case 'o':
2984 First += 2;
2985 return make<NameType>("operator||");
2986 case 'r':
2987 First += 2;
2988 return make<NameType>("operator|");
2989 case 'R':
2990 First += 2;
2991 return make<NameType>("operator|=");
2992 }
2993 return nullptr;
2994 case 'p':
2995 switch (look(1)) {
2996 case 'm':
2997 First += 2;
2998 return make<NameType>("operator->*");
2999 case 'l':
3000 First += 2;
3001 return make<NameType>("operator+");
3002 case 'L':
3003 First += 2;
3004 return make<NameType>("operator+=");
3005 case 'p':
3006 First += 2;
3007 return make<NameType>("operator++");
3008 case 's':
3009 First += 2;
3010 return make<NameType>("operator+");
3011 case 't':
3012 First += 2;
3013 return make<NameType>("operator->");
3014 }
3015 return nullptr;
3016 case 'q':
3017 if (look(1) == 'u') {
3018 First += 2;
3019 return make<NameType>("operator?");
3020 }
3021 return nullptr;
3022 case 'r':
3023 switch (look(1)) {
3024 case 'm':
3025 First += 2;
3026 return make<NameType>("operator%");
3027 case 'M':
3028 First += 2;
3029 return make<NameType>("operator%=");
3030 case 's':
3031 First += 2;
3032 return make<NameType>("operator>>");
3033 case 'S':
3034 First += 2;
3035 return make<NameType>("operator>>=");
3036 }
3037 return nullptr;
3038 case 's':
3039 if (look(1) == 's') {
3040 First += 2;
3041 return make<NameType>("operator<=>");
3042 }
3043 return nullptr;
3044 // ::= v <digit> <source-name> # vendor extended operator
3045 case 'v':
3046 if (std::isdigit(look(1))) {
3047 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003048 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003049 if (SN == nullptr)
3050 return nullptr;
3051 return make<ConversionOperatorType>(SN);
3052 }
3053 return nullptr;
3054 }
3055 return nullptr;
3056}
3057
3058// <ctor-dtor-name> ::= C1 # complete object constructor
3059// ::= C2 # base object constructor
3060// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003061// extension ::= C4 # gcc old-style "[unified]" constructor
3062// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003063// ::= D0 # deleting destructor
3064// ::= D1 # complete object destructor
3065// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003066// extension ::= D4 # gcc old-style "[unified]" destructor
3067// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003068template <typename Derived, typename Alloc>
3069Node *
3070AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3071 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003072 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3073 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3074 switch (SSK) {
3075 case SpecialSubKind::string:
3076 case SpecialSubKind::istream:
3077 case SpecialSubKind::ostream:
3078 case SpecialSubKind::iostream:
3079 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003080 if (!SoFar)
3081 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003082 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003083 default:
3084 break;
3085 }
3086 }
3087
3088 if (consumeIf('C')) {
3089 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003090 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3091 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003092 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003093 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003094 ++First;
3095 if (State) State->CtorDtorConversion = true;
3096 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003097 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003098 return nullptr;
3099 }
Nico Weber29294792019-04-03 23:14:33 +00003100 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003101 }
3102
Nico Weber29294792019-04-03 23:14:33 +00003103 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3104 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003105 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003106 First += 2;
3107 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003108 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003109 }
3110
3111 return nullptr;
3112}
3113
3114// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3115// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3116//
3117// <prefix> ::= <prefix> <unqualified-name>
3118// ::= <template-prefix> <template-args>
3119// ::= <template-param>
3120// ::= <decltype>
3121// ::= # empty
3122// ::= <substitution>
3123// ::= <prefix> <data-member-prefix>
3124// extension ::= L
3125//
3126// <data-member-prefix> := <member source-name> [<template-args>] M
3127//
3128// <template-prefix> ::= <prefix> <template unqualified-name>
3129// ::= <template-param>
3130// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003131template <typename Derived, typename Alloc>
3132Node *
3133AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003134 if (!consumeIf('N'))
3135 return nullptr;
3136
3137 Qualifiers CVTmp = parseCVQualifiers();
3138 if (State) State->CVQualifiers = CVTmp;
3139
3140 if (consumeIf('O')) {
3141 if (State) State->ReferenceQualifier = FrefQualRValue;
3142 } else if (consumeIf('R')) {
3143 if (State) State->ReferenceQualifier = FrefQualLValue;
3144 } else
3145 if (State) State->ReferenceQualifier = FrefQualNone;
3146
3147 Node *SoFar = nullptr;
3148 auto PushComponent = [&](Node *Comp) {
Richard Smithb485b352018-08-24 23:30:26 +00003149 if (!Comp) return false;
Richard Smithc20d1442018-08-20 20:14:49 +00003150 if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
3151 else SoFar = Comp;
3152 if (State) State->EndsWithTemplateArgs = false;
Richard Smithb485b352018-08-24 23:30:26 +00003153 return SoFar != nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003154 };
3155
Richard Smithb485b352018-08-24 23:30:26 +00003156 if (consumeIf("St")) {
Richard Smithc20d1442018-08-20 20:14:49 +00003157 SoFar = make<NameType>("std");
Richard Smithb485b352018-08-24 23:30:26 +00003158 if (!SoFar)
3159 return nullptr;
3160 }
Richard Smithc20d1442018-08-20 20:14:49 +00003161
3162 while (!consumeIf('E')) {
3163 consumeIf('L'); // extension
3164
3165 // <data-member-prefix> := <member source-name> [<template-args>] M
3166 if (consumeIf('M')) {
3167 if (SoFar == nullptr)
3168 return nullptr;
3169 continue;
3170 }
3171
3172 // ::= <template-param>
3173 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003174 if (!PushComponent(getDerived().parseTemplateParam()))
Richard Smithc20d1442018-08-20 20:14:49 +00003175 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003176 Subs.push_back(SoFar);
3177 continue;
3178 }
3179
3180 // ::= <template-prefix> <template-args>
3181 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003182 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003183 if (TA == nullptr || SoFar == nullptr)
3184 return nullptr;
3185 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003186 if (!SoFar)
3187 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003188 if (State) State->EndsWithTemplateArgs = true;
3189 Subs.push_back(SoFar);
3190 continue;
3191 }
3192
3193 // ::= <decltype>
3194 if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
Pavel Labathba825192018-10-16 14:29:14 +00003195 if (!PushComponent(getDerived().parseDecltype()))
Richard Smithc20d1442018-08-20 20:14:49 +00003196 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003197 Subs.push_back(SoFar);
3198 continue;
3199 }
3200
3201 // ::= <substitution>
3202 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00003203 Node *S = getDerived().parseSubstitution();
Richard Smithb485b352018-08-24 23:30:26 +00003204 if (!PushComponent(S))
Richard Smithc20d1442018-08-20 20:14:49 +00003205 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003206 if (SoFar != S)
3207 Subs.push_back(S);
3208 continue;
3209 }
3210
3211 // Parse an <unqualified-name> thats actually a <ctor-dtor-name>.
3212 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
3213 if (SoFar == nullptr)
3214 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003215 if (!PushComponent(getDerived().parseCtorDtorName(SoFar, State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003216 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003217 SoFar = getDerived().parseAbiTags(SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00003218 if (SoFar == nullptr)
3219 return nullptr;
3220 Subs.push_back(SoFar);
3221 continue;
3222 }
3223
3224 // ::= <prefix> <unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00003225 if (!PushComponent(getDerived().parseUnqualifiedName(State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003226 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003227 Subs.push_back(SoFar);
3228 }
3229
3230 if (SoFar == nullptr || Subs.empty())
3231 return nullptr;
3232
3233 Subs.pop_back();
3234 return SoFar;
3235}
3236
3237// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003238template <typename Derived, typename Alloc>
3239Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3240 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003241 if (SN == nullptr)
3242 return nullptr;
3243 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003244 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003245 if (TA == nullptr)
3246 return nullptr;
3247 return make<NameWithTemplateArgs>(SN, TA);
3248 }
3249 return SN;
3250}
3251
3252// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3253// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003254template <typename Derived, typename Alloc>
3255Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003256 Node *Result;
3257 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003258 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003259 else
Pavel Labathba825192018-10-16 14:29:14 +00003260 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003261 if (Result == nullptr)
3262 return nullptr;
3263 return make<DtorName>(Result);
3264}
3265
3266// <unresolved-type> ::= <template-param>
3267// ::= <decltype>
3268// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003269template <typename Derived, typename Alloc>
3270Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003271 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003272 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003273 if (TP == nullptr)
3274 return nullptr;
3275 Subs.push_back(TP);
3276 return TP;
3277 }
3278 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003279 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003280 if (DT == nullptr)
3281 return nullptr;
3282 Subs.push_back(DT);
3283 return DT;
3284 }
Pavel Labathba825192018-10-16 14:29:14 +00003285 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003286}
3287
3288// <base-unresolved-name> ::= <simple-id> # unresolved name
3289// extension ::= <operator-name> # unresolved operator-function-id
3290// extension ::= <operator-name> <template-args> # unresolved operator template-id
3291// ::= on <operator-name> # unresolved operator-function-id
3292// ::= on <operator-name> <template-args> # unresolved operator template-id
3293// ::= dn <destructor-name> # destructor or pseudo-destructor;
3294// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003295template <typename Derived, typename Alloc>
3296Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003297 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003298 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003299
3300 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003301 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003302
3303 consumeIf("on");
3304
Pavel Labathba825192018-10-16 14:29:14 +00003305 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003306 if (Oper == nullptr)
3307 return nullptr;
3308 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003309 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003310 if (TA == nullptr)
3311 return nullptr;
3312 return make<NameWithTemplateArgs>(Oper, TA);
3313 }
3314 return Oper;
3315}
3316
3317// <unresolved-name>
3318// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3319// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3320// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3321// # A::x, N::y, A<T>::z; "gs" means leading "::"
3322// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3323// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3324// # T::N::x /decltype(p)::N::x
3325// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3326//
3327// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003328template <typename Derived, typename Alloc>
3329Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003330 Node *SoFar = nullptr;
3331
3332 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3333 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3334 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003335 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003336 if (SoFar == nullptr)
3337 return nullptr;
3338
3339 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003340 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003341 if (TA == nullptr)
3342 return nullptr;
3343 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003344 if (!SoFar)
3345 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003346 }
3347
3348 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003349 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003350 if (Qual == nullptr)
3351 return nullptr;
3352 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003353 if (!SoFar)
3354 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003355 }
3356
Pavel Labathba825192018-10-16 14:29:14 +00003357 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003358 if (Base == nullptr)
3359 return nullptr;
3360 return make<QualifiedName>(SoFar, Base);
3361 }
3362
3363 bool Global = consumeIf("gs");
3364
3365 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3366 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003367 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003368 if (SoFar == nullptr)
3369 return nullptr;
3370 if (Global)
3371 SoFar = make<GlobalQualifiedName>(SoFar);
3372 return SoFar;
3373 }
3374
3375 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3376 if (std::isdigit(look())) {
3377 do {
Pavel Labathba825192018-10-16 14:29:14 +00003378 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003379 if (Qual == nullptr)
3380 return nullptr;
3381 if (SoFar)
3382 SoFar = make<QualifiedName>(SoFar, Qual);
3383 else if (Global)
3384 SoFar = make<GlobalQualifiedName>(Qual);
3385 else
3386 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003387 if (!SoFar)
3388 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003389 } while (!consumeIf('E'));
3390 }
3391 // sr <unresolved-type> <base-unresolved-name>
3392 // sr <unresolved-type> <template-args> <base-unresolved-name>
3393 else {
Pavel Labathba825192018-10-16 14:29:14 +00003394 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003395 if (SoFar == nullptr)
3396 return nullptr;
3397
3398 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003399 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003400 if (TA == nullptr)
3401 return nullptr;
3402 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003403 if (!SoFar)
3404 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003405 }
3406 }
3407
3408 assert(SoFar != nullptr);
3409
Pavel Labathba825192018-10-16 14:29:14 +00003410 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003411 if (Base == nullptr)
3412 return nullptr;
3413 return make<QualifiedName>(SoFar, Base);
3414}
3415
3416// <abi-tags> ::= <abi-tag> [<abi-tags>]
3417// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003418template <typename Derived, typename Alloc>
3419Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003420 while (consumeIf('B')) {
3421 StringView SN = parseBareSourceName();
3422 if (SN.empty())
3423 return nullptr;
3424 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003425 if (!N)
3426 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003427 }
3428 return N;
3429}
3430
3431// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003432template <typename Alloc, typename Derived>
3433StringView
3434AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003435 const char *Tmp = First;
3436 if (AllowNegative)
3437 consumeIf('n');
3438 if (numLeft() == 0 || !std::isdigit(*First))
3439 return StringView();
3440 while (numLeft() != 0 && std::isdigit(*First))
3441 ++First;
3442 return StringView(Tmp, First);
3443}
3444
3445// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003446template <typename Alloc, typename Derived>
3447bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003448 *Out = 0;
3449 if (look() < '0' || look() > '9')
3450 return true;
3451 while (look() >= '0' && look() <= '9') {
3452 *Out *= 10;
3453 *Out += static_cast<size_t>(consume() - '0');
3454 }
3455 return false;
3456}
3457
Pavel Labathba825192018-10-16 14:29:14 +00003458template <typename Alloc, typename Derived>
3459StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003460 size_t Int = 0;
3461 if (parsePositiveInteger(&Int) || numLeft() < Int)
3462 return StringView();
3463 StringView R(First, First + Int);
3464 First += Int;
3465 return R;
3466}
3467
3468// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3469//
3470// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3471// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3472// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3473//
3474// <ref-qualifier> ::= R # & ref-qualifier
3475// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003476template <typename Derived, typename Alloc>
3477Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003478 Qualifiers CVQuals = parseCVQualifiers();
3479
3480 Node *ExceptionSpec = nullptr;
3481 if (consumeIf("Do")) {
3482 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003483 if (!ExceptionSpec)
3484 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003485 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003486 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003487 if (E == nullptr || !consumeIf('E'))
3488 return nullptr;
3489 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003490 if (!ExceptionSpec)
3491 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003492 } else if (consumeIf("Dw")) {
3493 size_t SpecsBegin = Names.size();
3494 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003495 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003496 if (T == nullptr)
3497 return nullptr;
3498 Names.push_back(T);
3499 }
3500 ExceptionSpec =
3501 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003502 if (!ExceptionSpec)
3503 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003504 }
3505
3506 consumeIf("Dx"); // transaction safe
3507
3508 if (!consumeIf('F'))
3509 return nullptr;
3510 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003511 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003512 if (ReturnType == nullptr)
3513 return nullptr;
3514
3515 FunctionRefQual ReferenceQualifier = FrefQualNone;
3516 size_t ParamsBegin = Names.size();
3517 while (true) {
3518 if (consumeIf('E'))
3519 break;
3520 if (consumeIf('v'))
3521 continue;
3522 if (consumeIf("RE")) {
3523 ReferenceQualifier = FrefQualLValue;
3524 break;
3525 }
3526 if (consumeIf("OE")) {
3527 ReferenceQualifier = FrefQualRValue;
3528 break;
3529 }
Pavel Labathba825192018-10-16 14:29:14 +00003530 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003531 if (T == nullptr)
3532 return nullptr;
3533 Names.push_back(T);
3534 }
3535
3536 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3537 return make<FunctionType>(ReturnType, Params, CVQuals,
3538 ReferenceQualifier, ExceptionSpec);
3539}
3540
3541// extension:
3542// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3543// ::= Dv [<dimension expression>] _ <element type>
3544// <extended element type> ::= <element type>
3545// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003546template <typename Derived, typename Alloc>
3547Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003548 if (!consumeIf("Dv"))
3549 return nullptr;
3550 if (look() >= '1' && look() <= '9') {
3551 StringView DimensionNumber = parseNumber();
3552 if (!consumeIf('_'))
3553 return nullptr;
3554 if (consumeIf('p'))
3555 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003556 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003557 if (ElemType == nullptr)
3558 return nullptr;
3559 return make<VectorType>(ElemType, DimensionNumber);
3560 }
3561
3562 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003563 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003564 if (!DimExpr)
3565 return nullptr;
3566 if (!consumeIf('_'))
3567 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003568 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003569 if (!ElemType)
3570 return nullptr;
3571 return make<VectorType>(ElemType, DimExpr);
3572 }
Pavel Labathba825192018-10-16 14:29:14 +00003573 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003574 if (!ElemType)
3575 return nullptr;
3576 return make<VectorType>(ElemType, StringView());
3577}
3578
3579// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3580// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003581template <typename Derived, typename Alloc>
3582Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003583 if (!consumeIf('D'))
3584 return nullptr;
3585 if (!consumeIf('t') && !consumeIf('T'))
3586 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003587 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003588 if (E == nullptr)
3589 return nullptr;
3590 if (!consumeIf('E'))
3591 return nullptr;
3592 return make<EnclosingExpr>("decltype(", E, ")");
3593}
3594
3595// <array-type> ::= A <positive dimension number> _ <element type>
3596// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003597template <typename Derived, typename Alloc>
3598Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003599 if (!consumeIf('A'))
3600 return nullptr;
3601
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003602 NodeOrString Dimension;
3603
Richard Smithc20d1442018-08-20 20:14:49 +00003604 if (std::isdigit(look())) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003605 Dimension = parseNumber();
Richard Smithc20d1442018-08-20 20:14:49 +00003606 if (!consumeIf('_'))
3607 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003608 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003609 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003610 if (DimExpr == nullptr)
3611 return nullptr;
3612 if (!consumeIf('_'))
3613 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003614 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003615 }
3616
Pavel Labathba825192018-10-16 14:29:14 +00003617 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003618 if (Ty == nullptr)
3619 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003620 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003621}
3622
3623// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003624template <typename Derived, typename Alloc>
3625Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003626 if (!consumeIf('M'))
3627 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003628 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003629 if (ClassType == nullptr)
3630 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003631 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003632 if (MemberType == nullptr)
3633 return nullptr;
3634 return make<PointerToMemberType>(ClassType, MemberType);
3635}
3636
3637// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3638// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3639// ::= Tu <name> # dependent elaborated type specifier using 'union'
3640// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003641template <typename Derived, typename Alloc>
3642Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003643 StringView ElabSpef;
3644 if (consumeIf("Ts"))
3645 ElabSpef = "struct";
3646 else if (consumeIf("Tu"))
3647 ElabSpef = "union";
3648 else if (consumeIf("Te"))
3649 ElabSpef = "enum";
3650
Pavel Labathba825192018-10-16 14:29:14 +00003651 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003652 if (Name == nullptr)
3653 return nullptr;
3654
3655 if (!ElabSpef.empty())
3656 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3657
3658 return Name;
3659}
3660
3661// <qualified-type> ::= <qualifiers> <type>
3662// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3663// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003664template <typename Derived, typename Alloc>
3665Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003666 if (consumeIf('U')) {
3667 StringView Qual = parseBareSourceName();
3668 if (Qual.empty())
3669 return nullptr;
3670
3671 // FIXME parse the optional <template-args> here!
3672
3673 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3674 if (Qual.startsWith("objcproto")) {
3675 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3676 StringView Proto;
3677 {
3678 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3679 SaveLast(Last, ProtoSourceName.end());
3680 Proto = parseBareSourceName();
3681 }
3682 if (Proto.empty())
3683 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003684 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003685 if (Child == nullptr)
3686 return nullptr;
3687 return make<ObjCProtoName>(Child, Proto);
3688 }
3689
Pavel Labathba825192018-10-16 14:29:14 +00003690 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003691 if (Child == nullptr)
3692 return nullptr;
3693 return make<VendorExtQualType>(Child, Qual);
3694 }
3695
3696 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003697 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003698 if (Ty == nullptr)
3699 return nullptr;
3700 if (Quals != QualNone)
3701 Ty = make<QualType>(Ty, Quals);
3702 return Ty;
3703}
3704
3705// <type> ::= <builtin-type>
3706// ::= <qualified-type>
3707// ::= <function-type>
3708// ::= <class-enum-type>
3709// ::= <array-type>
3710// ::= <pointer-to-member-type>
3711// ::= <template-param>
3712// ::= <template-template-param> <template-args>
3713// ::= <decltype>
3714// ::= P <type> # pointer
3715// ::= R <type> # l-value reference
3716// ::= O <type> # r-value reference (C++11)
3717// ::= C <type> # complex pair (C99)
3718// ::= G <type> # imaginary (C99)
3719// ::= <substitution> # See Compression below
3720// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3721// extension ::= <vector-type> # <vector-type> starts with Dv
3722//
3723// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3724// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003725template <typename Derived, typename Alloc>
3726Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003727 Node *Result = nullptr;
3728
Richard Smithc20d1442018-08-20 20:14:49 +00003729 switch (look()) {
3730 // ::= <qualified-type>
3731 case 'r':
3732 case 'V':
3733 case 'K': {
3734 unsigned AfterQuals = 0;
3735 if (look(AfterQuals) == 'r') ++AfterQuals;
3736 if (look(AfterQuals) == 'V') ++AfterQuals;
3737 if (look(AfterQuals) == 'K') ++AfterQuals;
3738
3739 if (look(AfterQuals) == 'F' ||
3740 (look(AfterQuals) == 'D' &&
3741 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3742 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003743 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003744 break;
3745 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003746 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003747 }
3748 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003749 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003750 break;
3751 }
3752 // <builtin-type> ::= v # void
3753 case 'v':
3754 ++First;
3755 return make<NameType>("void");
3756 // ::= w # wchar_t
3757 case 'w':
3758 ++First;
3759 return make<NameType>("wchar_t");
3760 // ::= b # bool
3761 case 'b':
3762 ++First;
3763 return make<NameType>("bool");
3764 // ::= c # char
3765 case 'c':
3766 ++First;
3767 return make<NameType>("char");
3768 // ::= a # signed char
3769 case 'a':
3770 ++First;
3771 return make<NameType>("signed char");
3772 // ::= h # unsigned char
3773 case 'h':
3774 ++First;
3775 return make<NameType>("unsigned char");
3776 // ::= s # short
3777 case 's':
3778 ++First;
3779 return make<NameType>("short");
3780 // ::= t # unsigned short
3781 case 't':
3782 ++First;
3783 return make<NameType>("unsigned short");
3784 // ::= i # int
3785 case 'i':
3786 ++First;
3787 return make<NameType>("int");
3788 // ::= j # unsigned int
3789 case 'j':
3790 ++First;
3791 return make<NameType>("unsigned int");
3792 // ::= l # long
3793 case 'l':
3794 ++First;
3795 return make<NameType>("long");
3796 // ::= m # unsigned long
3797 case 'm':
3798 ++First;
3799 return make<NameType>("unsigned long");
3800 // ::= x # long long, __int64
3801 case 'x':
3802 ++First;
3803 return make<NameType>("long long");
3804 // ::= y # unsigned long long, __int64
3805 case 'y':
3806 ++First;
3807 return make<NameType>("unsigned long long");
3808 // ::= n # __int128
3809 case 'n':
3810 ++First;
3811 return make<NameType>("__int128");
3812 // ::= o # unsigned __int128
3813 case 'o':
3814 ++First;
3815 return make<NameType>("unsigned __int128");
3816 // ::= f # float
3817 case 'f':
3818 ++First;
3819 return make<NameType>("float");
3820 // ::= d # double
3821 case 'd':
3822 ++First;
3823 return make<NameType>("double");
3824 // ::= e # long double, __float80
3825 case 'e':
3826 ++First;
3827 return make<NameType>("long double");
3828 // ::= g # __float128
3829 case 'g':
3830 ++First;
3831 return make<NameType>("__float128");
3832 // ::= z # ellipsis
3833 case 'z':
3834 ++First;
3835 return make<NameType>("...");
3836
3837 // <builtin-type> ::= u <source-name> # vendor extended type
3838 case 'u': {
3839 ++First;
3840 StringView Res = parseBareSourceName();
3841 if (Res.empty())
3842 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003843 // Typically, <builtin-type>s are not considered substitution candidates,
3844 // but the exception to that exception is vendor extended types (Itanium C++
3845 // ABI 5.9.1).
3846 Result = make<NameType>(Res);
3847 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003848 }
3849 case 'D':
3850 switch (look(1)) {
3851 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3852 case 'd':
3853 First += 2;
3854 return make<NameType>("decimal64");
3855 // ::= De # IEEE 754r decimal floating point (128 bits)
3856 case 'e':
3857 First += 2;
3858 return make<NameType>("decimal128");
3859 // ::= Df # IEEE 754r decimal floating point (32 bits)
3860 case 'f':
3861 First += 2;
3862 return make<NameType>("decimal32");
3863 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3864 case 'h':
3865 First += 2;
3866 return make<NameType>("decimal16");
3867 // ::= Di # char32_t
3868 case 'i':
3869 First += 2;
3870 return make<NameType>("char32_t");
3871 // ::= Ds # char16_t
3872 case 's':
3873 First += 2;
3874 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003875 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3876 case 'u':
3877 First += 2;
3878 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003879 // ::= Da # auto (in dependent new-expressions)
3880 case 'a':
3881 First += 2;
3882 return make<NameType>("auto");
3883 // ::= Dc # decltype(auto)
3884 case 'c':
3885 First += 2;
3886 return make<NameType>("decltype(auto)");
3887 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3888 case 'n':
3889 First += 2;
3890 return make<NameType>("std::nullptr_t");
3891
3892 // ::= <decltype>
3893 case 't':
3894 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003895 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003896 break;
3897 }
3898 // extension ::= <vector-type> # <vector-type> starts with Dv
3899 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003900 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003901 break;
3902 }
3903 // ::= Dp <type> # pack expansion (C++0x)
3904 case 'p': {
3905 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003906 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003907 if (!Child)
3908 return nullptr;
3909 Result = make<ParameterPackExpansion>(Child);
3910 break;
3911 }
3912 // Exception specifier on a function type.
3913 case 'o':
3914 case 'O':
3915 case 'w':
3916 // Transaction safe function type.
3917 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003918 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003919 break;
3920 }
3921 break;
3922 // ::= <function-type>
3923 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003924 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003925 break;
3926 }
3927 // ::= <array-type>
3928 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003929 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003930 break;
3931 }
3932 // ::= <pointer-to-member-type>
3933 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003934 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003935 break;
3936 }
3937 // ::= <template-param>
3938 case 'T': {
3939 // This could be an elaborate type specifier on a <class-enum-type>.
3940 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003941 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003942 break;
3943 }
3944
Pavel Labathba825192018-10-16 14:29:14 +00003945 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003946 if (Result == nullptr)
3947 return nullptr;
3948
3949 // Result could be either of:
3950 // <type> ::= <template-param>
3951 // <type> ::= <template-template-param> <template-args>
3952 //
3953 // <template-template-param> ::= <template-param>
3954 // ::= <substitution>
3955 //
3956 // If this is followed by some <template-args>, and we're permitted to
3957 // parse them, take the second production.
3958
3959 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003960 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003961 if (TA == nullptr)
3962 return nullptr;
3963 Result = make<NameWithTemplateArgs>(Result, TA);
3964 }
3965 break;
3966 }
3967 // ::= P <type> # pointer
3968 case 'P': {
3969 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003970 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003971 if (Ptr == nullptr)
3972 return nullptr;
3973 Result = make<PointerType>(Ptr);
3974 break;
3975 }
3976 // ::= R <type> # l-value reference
3977 case 'R': {
3978 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003979 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003980 if (Ref == nullptr)
3981 return nullptr;
3982 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
3983 break;
3984 }
3985 // ::= O <type> # r-value reference (C++11)
3986 case 'O': {
3987 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003988 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003989 if (Ref == nullptr)
3990 return nullptr;
3991 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
3992 break;
3993 }
3994 // ::= C <type> # complex pair (C99)
3995 case 'C': {
3996 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003997 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003998 if (P == nullptr)
3999 return nullptr;
4000 Result = make<PostfixQualifiedType>(P, " complex");
4001 break;
4002 }
4003 // ::= G <type> # imaginary (C99)
4004 case 'G': {
4005 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004006 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004007 if (P == nullptr)
4008 return P;
4009 Result = make<PostfixQualifiedType>(P, " imaginary");
4010 break;
4011 }
4012 // ::= <substitution> # See Compression below
4013 case 'S': {
4014 if (look(1) && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00004015 Node *Sub = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00004016 if (Sub == nullptr)
4017 return nullptr;
4018
4019 // Sub could be either of:
4020 // <type> ::= <substitution>
4021 // <type> ::= <template-template-param> <template-args>
4022 //
4023 // <template-template-param> ::= <template-param>
4024 // ::= <substitution>
4025 //
4026 // If this is followed by some <template-args>, and we're permitted to
4027 // parse them, take the second production.
4028
4029 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004030 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004031 if (TA == nullptr)
4032 return nullptr;
4033 Result = make<NameWithTemplateArgs>(Sub, TA);
4034 break;
4035 }
4036
4037 // If all we parsed was a substitution, don't re-insert into the
4038 // substitution table.
4039 return Sub;
4040 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00004041 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004042 }
4043 // ::= <class-enum-type>
4044 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004045 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004046 break;
4047 }
4048 }
4049
4050 // If we parsed a type, insert it into the substitution table. Note that all
4051 // <builtin-type>s and <substitution>s have already bailed out, because they
4052 // don't get substitutions.
4053 if (Result != nullptr)
4054 Subs.push_back(Result);
4055 return Result;
4056}
4057
Pavel Labathba825192018-10-16 14:29:14 +00004058template <typename Derived, typename Alloc>
4059Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4060 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004061 if (E == nullptr)
4062 return nullptr;
4063 return make<PrefixExpr>(Kind, E);
4064}
4065
Pavel Labathba825192018-10-16 14:29:14 +00004066template <typename Derived, typename Alloc>
4067Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4068 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004069 if (LHS == nullptr)
4070 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004071 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004072 if (RHS == nullptr)
4073 return nullptr;
4074 return make<BinaryExpr>(LHS, Kind, RHS);
4075}
4076
Pavel Labathba825192018-10-16 14:29:14 +00004077template <typename Derived, typename Alloc>
4078Node *
4079AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004080 StringView Tmp = parseNumber(true);
4081 if (!Tmp.empty() && consumeIf('E'))
4082 return make<IntegerLiteral>(Lit, Tmp);
4083 return nullptr;
4084}
4085
4086// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004087template <typename Alloc, typename Derived>
4088Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004089 Qualifiers CVR = QualNone;
4090 if (consumeIf('r'))
4091 CVR |= QualRestrict;
4092 if (consumeIf('V'))
4093 CVR |= QualVolatile;
4094 if (consumeIf('K'))
4095 CVR |= QualConst;
4096 return CVR;
4097}
4098
4099// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4100// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4101// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4102// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L > 0, second and later parameters
Pavel Labathba825192018-10-16 14:29:14 +00004103template <typename Derived, typename Alloc>
4104Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00004105 if (consumeIf("fp")) {
4106 parseCVQualifiers();
4107 StringView Num = parseNumber();
4108 if (!consumeIf('_'))
4109 return nullptr;
4110 return make<FunctionParam>(Num);
4111 }
4112 if (consumeIf("fL")) {
4113 if (parseNumber().empty())
4114 return nullptr;
4115 if (!consumeIf('p'))
4116 return nullptr;
4117 parseCVQualifiers();
4118 StringView Num = parseNumber();
4119 if (!consumeIf('_'))
4120 return nullptr;
4121 return make<FunctionParam>(Num);
4122 }
4123 return nullptr;
4124}
4125
4126// [gs] nw <expression>* _ <type> E # new (expr-list) type
4127// [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4128// [gs] na <expression>* _ <type> E # new[] (expr-list) type
4129// [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4130// <initializer> ::= pi <expression>* E # parenthesized initialization
Pavel Labathba825192018-10-16 14:29:14 +00004131template <typename Derived, typename Alloc>
4132Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004133 bool Global = consumeIf("gs");
4134 bool IsArray = look(1) == 'a';
4135 if (!consumeIf("nw") && !consumeIf("na"))
4136 return nullptr;
4137 size_t Exprs = Names.size();
4138 while (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00004139 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004140 if (Ex == nullptr)
4141 return nullptr;
4142 Names.push_back(Ex);
4143 }
4144 NodeArray ExprList = popTrailingNodeArray(Exprs);
Pavel Labathba825192018-10-16 14:29:14 +00004145 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004146 if (Ty == nullptr)
4147 return Ty;
4148 if (consumeIf("pi")) {
4149 size_t InitsBegin = Names.size();
4150 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004151 Node *Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004152 if (Init == nullptr)
4153 return Init;
4154 Names.push_back(Init);
4155 }
4156 NodeArray Inits = popTrailingNodeArray(InitsBegin);
4157 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
4158 } else if (!consumeIf('E'))
4159 return nullptr;
4160 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
4161}
4162
4163// cv <type> <expression> # conversion with one argument
4164// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004165template <typename Derived, typename Alloc>
4166Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004167 if (!consumeIf("cv"))
4168 return nullptr;
4169 Node *Ty;
4170 {
4171 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004172 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004173 }
4174
4175 if (Ty == nullptr)
4176 return nullptr;
4177
4178 if (consumeIf('_')) {
4179 size_t ExprsBegin = Names.size();
4180 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004181 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004182 if (E == nullptr)
4183 return E;
4184 Names.push_back(E);
4185 }
4186 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4187 return make<ConversionExpr>(Ty, Exprs);
4188 }
4189
Pavel Labathba825192018-10-16 14:29:14 +00004190 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004191 if (E[0] == nullptr)
4192 return nullptr;
4193 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4194}
4195
4196// <expr-primary> ::= L <type> <value number> E # integer literal
4197// ::= L <type> <value float> E # floating literal
4198// ::= L <string type> E # string literal
4199// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004200// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004201// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4202// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004203template <typename Derived, typename Alloc>
4204Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004205 if (!consumeIf('L'))
4206 return nullptr;
4207 switch (look()) {
4208 case 'w':
4209 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004210 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004211 case 'b':
4212 if (consumeIf("b0E"))
4213 return make<BoolExpr>(0);
4214 if (consumeIf("b1E"))
4215 return make<BoolExpr>(1);
4216 return nullptr;
4217 case 'c':
4218 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004219 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004220 case 'a':
4221 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004222 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004223 case 'h':
4224 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004225 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004226 case 's':
4227 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004228 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004229 case 't':
4230 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004231 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004232 case 'i':
4233 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004234 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004235 case 'j':
4236 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004237 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004238 case 'l':
4239 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004240 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004241 case 'm':
4242 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004243 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004244 case 'x':
4245 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004246 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004247 case 'y':
4248 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004249 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004250 case 'n':
4251 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004252 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004253 case 'o':
4254 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004255 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004256 case 'f':
4257 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004258 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004259 case 'd':
4260 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004261 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004262 case 'e':
4263 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004264 return getDerived().template parseFloatingLiteral<long double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004265 case '_':
4266 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004267 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004268 if (R != nullptr && consumeIf('E'))
4269 return R;
4270 }
4271 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004272 case 'A': {
4273 Node *T = getDerived().parseType();
4274 if (T == nullptr)
4275 return nullptr;
4276 // FIXME: We need to include the string contents in the mangling.
4277 if (consumeIf('E'))
4278 return make<StringLiteral>(T);
4279 return nullptr;
4280 }
4281 case 'D':
4282 if (consumeIf("DnE"))
4283 return make<NameType>("nullptr");
4284 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004285 case 'T':
4286 // Invalid mangled name per
4287 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4288 return nullptr;
Richard Smithfb917462019-09-09 22:26:04 +00004289 case 'U': {
4290 // FIXME: Should we support LUb... for block literals?
4291 if (look(1) != 'l')
4292 return nullptr;
4293 Node *T = parseUnnamedTypeName(nullptr);
4294 if (!T || !consumeIf('E'))
4295 return nullptr;
4296 return make<LambdaExpr>(T);
4297 }
Richard Smithc20d1442018-08-20 20:14:49 +00004298 default: {
4299 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004300 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004301 if (T == nullptr)
4302 return nullptr;
4303 StringView N = parseNumber();
Richard Smithfb917462019-09-09 22:26:04 +00004304 if (N.empty())
4305 return nullptr;
4306 if (!consumeIf('E'))
4307 return nullptr;
4308 return make<IntegerCastExpr>(T, N);
Richard Smithc20d1442018-08-20 20:14:49 +00004309 }
4310 }
4311}
4312
4313// <braced-expression> ::= <expression>
4314// ::= di <field source-name> <braced-expression> # .name = expr
4315// ::= dx <index expression> <braced-expression> # [expr] = expr
4316// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004317template <typename Derived, typename Alloc>
4318Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004319 if (look() == 'd') {
4320 switch (look(1)) {
4321 case 'i': {
4322 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004323 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004324 if (Field == nullptr)
4325 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004326 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004327 if (Init == nullptr)
4328 return nullptr;
4329 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4330 }
4331 case 'x': {
4332 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004333 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004334 if (Index == nullptr)
4335 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004336 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004337 if (Init == nullptr)
4338 return nullptr;
4339 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4340 }
4341 case 'X': {
4342 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004343 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004344 if (RangeBegin == nullptr)
4345 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004346 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004347 if (RangeEnd == nullptr)
4348 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004349 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004350 if (Init == nullptr)
4351 return nullptr;
4352 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4353 }
4354 }
4355 }
Pavel Labathba825192018-10-16 14:29:14 +00004356 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004357}
4358
4359// (not yet in the spec)
4360// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4361// ::= fR <binary-operator-name> <expression> <expression>
4362// ::= fl <binary-operator-name> <expression>
4363// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004364template <typename Derived, typename Alloc>
4365Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004366 if (!consumeIf('f'))
4367 return nullptr;
4368
4369 char FoldKind = look();
4370 bool IsLeftFold, HasInitializer;
4371 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4372 if (FoldKind == 'l' || FoldKind == 'L')
4373 IsLeftFold = true;
4374 else if (FoldKind == 'r' || FoldKind == 'R')
4375 IsLeftFold = false;
4376 else
4377 return nullptr;
4378 ++First;
4379
4380 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4381 StringView OperatorName;
4382 if (consumeIf("aa")) OperatorName = "&&";
4383 else if (consumeIf("an")) OperatorName = "&";
4384 else if (consumeIf("aN")) OperatorName = "&=";
4385 else if (consumeIf("aS")) OperatorName = "=";
4386 else if (consumeIf("cm")) OperatorName = ",";
4387 else if (consumeIf("ds")) OperatorName = ".*";
4388 else if (consumeIf("dv")) OperatorName = "/";
4389 else if (consumeIf("dV")) OperatorName = "/=";
4390 else if (consumeIf("eo")) OperatorName = "^";
4391 else if (consumeIf("eO")) OperatorName = "^=";
4392 else if (consumeIf("eq")) OperatorName = "==";
4393 else if (consumeIf("ge")) OperatorName = ">=";
4394 else if (consumeIf("gt")) OperatorName = ">";
4395 else if (consumeIf("le")) OperatorName = "<=";
4396 else if (consumeIf("ls")) OperatorName = "<<";
4397 else if (consumeIf("lS")) OperatorName = "<<=";
4398 else if (consumeIf("lt")) OperatorName = "<";
4399 else if (consumeIf("mi")) OperatorName = "-";
4400 else if (consumeIf("mI")) OperatorName = "-=";
4401 else if (consumeIf("ml")) OperatorName = "*";
4402 else if (consumeIf("mL")) OperatorName = "*=";
4403 else if (consumeIf("ne")) OperatorName = "!=";
4404 else if (consumeIf("oo")) OperatorName = "||";
4405 else if (consumeIf("or")) OperatorName = "|";
4406 else if (consumeIf("oR")) OperatorName = "|=";
4407 else if (consumeIf("pl")) OperatorName = "+";
4408 else if (consumeIf("pL")) OperatorName = "+=";
4409 else if (consumeIf("rm")) OperatorName = "%";
4410 else if (consumeIf("rM")) OperatorName = "%=";
4411 else if (consumeIf("rs")) OperatorName = ">>";
4412 else if (consumeIf("rS")) OperatorName = ">>=";
4413 else return nullptr;
4414
Pavel Labathba825192018-10-16 14:29:14 +00004415 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004416 if (Pack == nullptr)
4417 return nullptr;
4418 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004419 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004420 if (Init == nullptr)
4421 return nullptr;
4422 }
4423
4424 if (IsLeftFold && Init)
4425 std::swap(Pack, Init);
4426
4427 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4428}
4429
4430// <expression> ::= <unary operator-name> <expression>
4431// ::= <binary operator-name> <expression> <expression>
4432// ::= <ternary operator-name> <expression> <expression> <expression>
4433// ::= cl <expression>+ E # call
4434// ::= cv <type> <expression> # conversion with one argument
4435// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4436// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4437// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4438// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4439// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4440// ::= [gs] dl <expression> # delete expression
4441// ::= [gs] da <expression> # delete[] expression
4442// ::= pp_ <expression> # prefix ++
4443// ::= mm_ <expression> # prefix --
4444// ::= ti <type> # typeid (type)
4445// ::= te <expression> # typeid (expression)
4446// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4447// ::= sc <type> <expression> # static_cast<type> (expression)
4448// ::= cc <type> <expression> # const_cast<type> (expression)
4449// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4450// ::= st <type> # sizeof (a type)
4451// ::= sz <expression> # sizeof (an expression)
4452// ::= at <type> # alignof (a type)
4453// ::= az <expression> # alignof (an expression)
4454// ::= nx <expression> # noexcept (expression)
4455// ::= <template-param>
4456// ::= <function-param>
4457// ::= dt <expression> <unresolved-name> # expr.name
4458// ::= pt <expression> <unresolved-name> # expr->name
4459// ::= ds <expression> <expression> # expr.*expr
4460// ::= sZ <template-param> # size of a parameter pack
4461// ::= sZ <function-param> # size of a function parameter pack
4462// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4463// ::= sp <expression> # pack expansion
4464// ::= tw <expression> # throw expression
4465// ::= tr # throw with no operand (rethrow)
4466// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4467// # freestanding dependent name (e.g., T::x),
4468// # objectless nonstatic member reference
4469// ::= fL <binary-operator-name> <expression> <expression>
4470// ::= fR <binary-operator-name> <expression> <expression>
4471// ::= fl <binary-operator-name> <expression>
4472// ::= fr <binary-operator-name> <expression>
4473// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004474template <typename Derived, typename Alloc>
4475Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004476 bool Global = consumeIf("gs");
4477 if (numLeft() < 2)
4478 return nullptr;
4479
4480 switch (*First) {
4481 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004482 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004483 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004484 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004485 case 'f': {
4486 // Disambiguate a fold expression from a <function-param>.
4487 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004488 return getDerived().parseFunctionParam();
4489 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004490 }
4491 case 'a':
4492 switch (First[1]) {
4493 case 'a':
4494 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004495 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004496 case 'd':
4497 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004498 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004499 case 'n':
4500 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004501 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004502 case 'N':
4503 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004504 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004505 case 'S':
4506 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004507 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004508 case 't': {
4509 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004510 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004511 if (Ty == nullptr)
4512 return nullptr;
4513 return make<EnclosingExpr>("alignof (", Ty, ")");
4514 }
4515 case 'z': {
4516 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004517 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004518 if (Ty == nullptr)
4519 return nullptr;
4520 return make<EnclosingExpr>("alignof (", Ty, ")");
4521 }
4522 }
4523 return nullptr;
4524 case 'c':
4525 switch (First[1]) {
4526 // cc <type> <expression> # const_cast<type>(expression)
4527 case 'c': {
4528 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004529 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004530 if (Ty == nullptr)
4531 return Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004532 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004533 if (Ex == nullptr)
4534 return Ex;
4535 return make<CastExpr>("const_cast", Ty, Ex);
4536 }
4537 // cl <expression>+ E # call
4538 case 'l': {
4539 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004540 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004541 if (Callee == nullptr)
4542 return Callee;
4543 size_t ExprsBegin = Names.size();
4544 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004545 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004546 if (E == nullptr)
4547 return E;
4548 Names.push_back(E);
4549 }
4550 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4551 }
4552 case 'm':
4553 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004554 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004555 case 'o':
4556 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004557 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004558 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004559 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004560 }
4561 return nullptr;
4562 case 'd':
4563 switch (First[1]) {
4564 case 'a': {
4565 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004566 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004567 if (Ex == nullptr)
4568 return Ex;
4569 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4570 }
4571 case 'c': {
4572 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004573 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004574 if (T == nullptr)
4575 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004576 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004577 if (Ex == nullptr)
4578 return Ex;
4579 return make<CastExpr>("dynamic_cast", T, Ex);
4580 }
4581 case 'e':
4582 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004583 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004584 case 'l': {
4585 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004586 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004587 if (E == nullptr)
4588 return E;
4589 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4590 }
4591 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004592 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004593 case 's': {
4594 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004595 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004596 if (LHS == nullptr)
4597 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004598 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004599 if (RHS == nullptr)
4600 return nullptr;
4601 return make<MemberExpr>(LHS, ".*", RHS);
4602 }
4603 case 't': {
4604 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004605 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004606 if (LHS == nullptr)
4607 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004608 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004609 if (RHS == nullptr)
4610 return nullptr;
4611 return make<MemberExpr>(LHS, ".", RHS);
4612 }
4613 case 'v':
4614 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004615 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004616 case 'V':
4617 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004618 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004619 }
4620 return nullptr;
4621 case 'e':
4622 switch (First[1]) {
4623 case 'o':
4624 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004625 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004626 case 'O':
4627 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004628 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004629 case 'q':
4630 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004631 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004632 }
4633 return nullptr;
4634 case 'g':
4635 switch (First[1]) {
4636 case 'e':
4637 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004638 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004639 case 't':
4640 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004641 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004642 }
4643 return nullptr;
4644 case 'i':
4645 switch (First[1]) {
4646 case 'x': {
4647 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004648 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004649 if (Base == nullptr)
4650 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004651 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004652 if (Index == nullptr)
4653 return Index;
4654 return make<ArraySubscriptExpr>(Base, Index);
4655 }
4656 case 'l': {
4657 First += 2;
4658 size_t InitsBegin = Names.size();
4659 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004660 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004661 if (E == nullptr)
4662 return nullptr;
4663 Names.push_back(E);
4664 }
4665 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4666 }
4667 }
4668 return nullptr;
4669 case 'l':
4670 switch (First[1]) {
4671 case 'e':
4672 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004673 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004674 case 's':
4675 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004676 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004677 case 'S':
4678 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004679 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004680 case 't':
4681 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004682 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004683 }
4684 return nullptr;
4685 case 'm':
4686 switch (First[1]) {
4687 case 'i':
4688 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004689 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004690 case 'I':
4691 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004692 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004693 case 'l':
4694 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004695 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004696 case 'L':
4697 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004698 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004699 case 'm':
4700 First += 2;
4701 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004702 return getDerived().parsePrefixExpr("--");
4703 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004704 if (Ex == nullptr)
4705 return nullptr;
4706 return make<PostfixExpr>(Ex, "--");
4707 }
4708 return nullptr;
4709 case 'n':
4710 switch (First[1]) {
4711 case 'a':
4712 case 'w':
Pavel Labathba825192018-10-16 14:29:14 +00004713 return getDerived().parseNewExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004714 case 'e':
4715 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004716 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004717 case 'g':
4718 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004719 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004720 case 't':
4721 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004722 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004723 case 'x':
4724 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004725 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004726 if (Ex == nullptr)
4727 return Ex;
4728 return make<EnclosingExpr>("noexcept (", Ex, ")");
4729 }
4730 return nullptr;
4731 case 'o':
4732 switch (First[1]) {
4733 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004734 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004735 case 'o':
4736 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004737 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004738 case 'r':
4739 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004740 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004741 case 'R':
4742 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004743 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004744 }
4745 return nullptr;
4746 case 'p':
4747 switch (First[1]) {
4748 case 'm':
4749 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004750 return getDerived().parseBinaryExpr("->*");
Richard Smithc20d1442018-08-20 20:14:49 +00004751 case 'l':
4752 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004753 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004754 case 'L':
4755 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004756 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004757 case 'p': {
4758 First += 2;
4759 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004760 return getDerived().parsePrefixExpr("++");
4761 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004762 if (Ex == nullptr)
4763 return Ex;
4764 return make<PostfixExpr>(Ex, "++");
4765 }
4766 case 's':
4767 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004768 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004769 case 't': {
4770 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004771 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004772 if (L == nullptr)
4773 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004774 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004775 if (R == nullptr)
4776 return nullptr;
4777 return make<MemberExpr>(L, "->", R);
4778 }
4779 }
4780 return nullptr;
4781 case 'q':
4782 if (First[1] == 'u') {
4783 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004784 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004785 if (Cond == nullptr)
4786 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004787 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004788 if (LHS == nullptr)
4789 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004790 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004791 if (RHS == nullptr)
4792 return nullptr;
4793 return make<ConditionalExpr>(Cond, LHS, RHS);
4794 }
4795 return nullptr;
4796 case 'r':
4797 switch (First[1]) {
4798 case 'c': {
4799 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004800 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004801 if (T == nullptr)
4802 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004803 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004804 if (Ex == nullptr)
4805 return Ex;
4806 return make<CastExpr>("reinterpret_cast", T, Ex);
4807 }
4808 case 'm':
4809 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004810 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004811 case 'M':
4812 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004813 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004814 case 's':
4815 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004816 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004817 case 'S':
4818 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004819 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004820 }
4821 return nullptr;
4822 case 's':
4823 switch (First[1]) {
4824 case 'c': {
4825 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004826 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004827 if (T == nullptr)
4828 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004829 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004830 if (Ex == nullptr)
4831 return Ex;
4832 return make<CastExpr>("static_cast", T, Ex);
4833 }
4834 case 'p': {
4835 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004836 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004837 if (Child == nullptr)
4838 return nullptr;
4839 return make<ParameterPackExpansion>(Child);
4840 }
4841 case 'r':
Pavel Labathba825192018-10-16 14:29:14 +00004842 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004843 case 't': {
4844 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004845 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004846 if (Ty == nullptr)
4847 return Ty;
4848 return make<EnclosingExpr>("sizeof (", Ty, ")");
4849 }
4850 case 'z': {
4851 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004852 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004853 if (Ex == nullptr)
4854 return Ex;
4855 return make<EnclosingExpr>("sizeof (", Ex, ")");
4856 }
4857 case 'Z':
4858 First += 2;
4859 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004860 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004861 if (R == nullptr)
4862 return nullptr;
4863 return make<SizeofParamPackExpr>(R);
4864 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004865 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004866 if (FP == nullptr)
4867 return nullptr;
4868 return make<EnclosingExpr>("sizeof... (", FP, ")");
4869 }
4870 return nullptr;
4871 case 'P': {
4872 First += 2;
4873 size_t ArgsBegin = Names.size();
4874 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004875 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004876 if (Arg == nullptr)
4877 return nullptr;
4878 Names.push_back(Arg);
4879 }
Richard Smithb485b352018-08-24 23:30:26 +00004880 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4881 if (!Pack)
4882 return nullptr;
4883 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00004884 }
4885 }
4886 return nullptr;
4887 case 't':
4888 switch (First[1]) {
4889 case 'e': {
4890 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004891 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004892 if (Ex == nullptr)
4893 return Ex;
4894 return make<EnclosingExpr>("typeid (", Ex, ")");
4895 }
4896 case 'i': {
4897 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004898 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004899 if (Ty == nullptr)
4900 return Ty;
4901 return make<EnclosingExpr>("typeid (", Ty, ")");
4902 }
4903 case 'l': {
4904 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004905 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004906 if (Ty == nullptr)
4907 return nullptr;
4908 size_t InitsBegin = Names.size();
4909 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004910 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004911 if (E == nullptr)
4912 return nullptr;
4913 Names.push_back(E);
4914 }
4915 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
4916 }
4917 case 'r':
4918 First += 2;
4919 return make<NameType>("throw");
4920 case 'w': {
4921 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004922 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004923 if (Ex == nullptr)
4924 return nullptr;
4925 return make<ThrowExpr>(Ex);
4926 }
4927 }
4928 return nullptr;
4929 case '1':
4930 case '2':
4931 case '3':
4932 case '4':
4933 case '5':
4934 case '6':
4935 case '7':
4936 case '8':
4937 case '9':
Pavel Labathba825192018-10-16 14:29:14 +00004938 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004939 }
Erik Pilkingtone8457c62019-06-18 23:34:09 +00004940
4941 if (consumeIf("u8__uuidoft")) {
4942 Node *Ty = getDerived().parseType();
4943 if (!Ty)
4944 return nullptr;
4945 return make<UUIDOfExpr>(Ty);
4946 }
4947
4948 if (consumeIf("u8__uuidofz")) {
4949 Node *Ex = getDerived().parseExpr();
4950 if (!Ex)
4951 return nullptr;
4952 return make<UUIDOfExpr>(Ex);
4953 }
4954
Richard Smithc20d1442018-08-20 20:14:49 +00004955 return nullptr;
4956}
4957
4958// <call-offset> ::= h <nv-offset> _
4959// ::= v <v-offset> _
4960//
4961// <nv-offset> ::= <offset number>
4962// # non-virtual base override
4963//
4964// <v-offset> ::= <offset number> _ <virtual offset number>
4965// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00004966template <typename Alloc, typename Derived>
4967bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00004968 // Just scan through the call offset, we never add this information into the
4969 // output.
4970 if (consumeIf('h'))
4971 return parseNumber(true).empty() || !consumeIf('_');
4972 if (consumeIf('v'))
4973 return parseNumber(true).empty() || !consumeIf('_') ||
4974 parseNumber(true).empty() || !consumeIf('_');
4975 return true;
4976}
4977
4978// <special-name> ::= TV <type> # virtual table
4979// ::= TT <type> # VTT structure (construction vtable index)
4980// ::= TI <type> # typeinfo structure
4981// ::= TS <type> # typeinfo name (null-terminated byte string)
4982// ::= Tc <call-offset> <call-offset> <base encoding>
4983// # base is the nominal target function of thunk
4984// # first call-offset is 'this' adjustment
4985// # second call-offset is result adjustment
4986// ::= T <call-offset> <base encoding>
4987// # base is the nominal target function of thunk
4988// ::= GV <object name> # Guard variable for one-time initialization
4989// # No <type>
4990// ::= TW <object name> # Thread-local wrapper
4991// ::= TH <object name> # Thread-local initialization
4992// ::= GR <object name> _ # First temporary
4993// ::= GR <object name> <seq-id> _ # Subsequent temporaries
4994// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
4995// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00004996template <typename Derived, typename Alloc>
4997Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00004998 switch (look()) {
4999 case 'T':
5000 switch (look(1)) {
5001 // TV <type> # virtual table
5002 case 'V': {
5003 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005004 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005005 if (Ty == nullptr)
5006 return nullptr;
5007 return make<SpecialName>("vtable for ", Ty);
5008 }
5009 // TT <type> # VTT structure (construction vtable index)
5010 case 'T': {
5011 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005012 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005013 if (Ty == nullptr)
5014 return nullptr;
5015 return make<SpecialName>("VTT for ", Ty);
5016 }
5017 // TI <type> # typeinfo structure
5018 case 'I': {
5019 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005020 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005021 if (Ty == nullptr)
5022 return nullptr;
5023 return make<SpecialName>("typeinfo for ", Ty);
5024 }
5025 // TS <type> # typeinfo name (null-terminated byte string)
5026 case 'S': {
5027 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005028 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005029 if (Ty == nullptr)
5030 return nullptr;
5031 return make<SpecialName>("typeinfo name for ", Ty);
5032 }
5033 // Tc <call-offset> <call-offset> <base encoding>
5034 case 'c': {
5035 First += 2;
5036 if (parseCallOffset() || parseCallOffset())
5037 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005038 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005039 if (Encoding == nullptr)
5040 return nullptr;
5041 return make<SpecialName>("covariant return thunk to ", Encoding);
5042 }
5043 // extension ::= TC <first type> <number> _ <second type>
5044 // # construction vtable for second-in-first
5045 case 'C': {
5046 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005047 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005048 if (FirstType == nullptr)
5049 return nullptr;
5050 if (parseNumber(true).empty() || !consumeIf('_'))
5051 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005052 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005053 if (SecondType == nullptr)
5054 return nullptr;
5055 return make<CtorVtableSpecialName>(SecondType, FirstType);
5056 }
5057 // TW <object name> # Thread-local wrapper
5058 case 'W': {
5059 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005060 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005061 if (Name == nullptr)
5062 return nullptr;
5063 return make<SpecialName>("thread-local wrapper routine for ", Name);
5064 }
5065 // TH <object name> # Thread-local initialization
5066 case 'H': {
5067 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005068 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005069 if (Name == nullptr)
5070 return nullptr;
5071 return make<SpecialName>("thread-local initialization routine for ", Name);
5072 }
5073 // T <call-offset> <base encoding>
5074 default: {
5075 ++First;
5076 bool IsVirt = look() == 'v';
5077 if (parseCallOffset())
5078 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005079 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005080 if (BaseEncoding == nullptr)
5081 return nullptr;
5082 if (IsVirt)
5083 return make<SpecialName>("virtual thunk to ", BaseEncoding);
5084 else
5085 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
5086 }
5087 }
5088 case 'G':
5089 switch (look(1)) {
5090 // GV <object name> # Guard variable for one-time initialization
5091 case 'V': {
5092 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005093 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005094 if (Name == nullptr)
5095 return nullptr;
5096 return make<SpecialName>("guard variable for ", Name);
5097 }
5098 // GR <object name> # reference temporary for object
5099 // GR <object name> _ # First temporary
5100 // GR <object name> <seq-id> _ # Subsequent temporaries
5101 case 'R': {
5102 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005103 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005104 if (Name == nullptr)
5105 return nullptr;
5106 size_t Count;
5107 bool ParsedSeqId = !parseSeqId(&Count);
5108 if (!consumeIf('_') && ParsedSeqId)
5109 return nullptr;
5110 return make<SpecialName>("reference temporary for ", Name);
5111 }
5112 }
5113 }
5114 return nullptr;
5115}
5116
5117// <encoding> ::= <function name> <bare-function-type>
5118// ::= <data name>
5119// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00005120template <typename Derived, typename Alloc>
5121Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithc20d1442018-08-20 20:14:49 +00005122 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00005123 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00005124
5125 auto IsEndOfEncoding = [&] {
5126 // The set of chars that can potentially follow an <encoding> (none of which
5127 // can start a <type>). Enumerating these allows us to avoid speculative
5128 // parsing.
5129 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5130 };
5131
5132 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00005133 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00005134 if (Name == nullptr)
5135 return nullptr;
5136
5137 if (resolveForwardTemplateRefs(NameInfo))
5138 return nullptr;
5139
5140 if (IsEndOfEncoding())
5141 return Name;
5142
5143 Node *Attrs = nullptr;
5144 if (consumeIf("Ua9enable_ifI")) {
5145 size_t BeforeArgs = Names.size();
5146 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005147 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005148 if (Arg == nullptr)
5149 return nullptr;
5150 Names.push_back(Arg);
5151 }
5152 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00005153 if (!Attrs)
5154 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005155 }
5156
5157 Node *ReturnType = nullptr;
5158 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00005159 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005160 if (ReturnType == nullptr)
5161 return nullptr;
5162 }
5163
5164 if (consumeIf('v'))
5165 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5166 Attrs, NameInfo.CVQualifiers,
5167 NameInfo.ReferenceQualifier);
5168
5169 size_t ParamsBegin = Names.size();
5170 do {
Pavel Labathba825192018-10-16 14:29:14 +00005171 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005172 if (Ty == nullptr)
5173 return nullptr;
5174 Names.push_back(Ty);
5175 } while (!IsEndOfEncoding());
5176
5177 return make<FunctionEncoding>(ReturnType, Name,
5178 popTrailingNodeArray(ParamsBegin),
5179 Attrs, NameInfo.CVQualifiers,
5180 NameInfo.ReferenceQualifier);
5181}
5182
5183template <class Float>
5184struct FloatData;
5185
5186template <>
5187struct FloatData<float>
5188{
5189 static const size_t mangled_size = 8;
5190 static const size_t max_demangled_size = 24;
5191 static constexpr const char* spec = "%af";
5192};
5193
5194template <>
5195struct FloatData<double>
5196{
5197 static const size_t mangled_size = 16;
5198 static const size_t max_demangled_size = 32;
5199 static constexpr const char* spec = "%a";
5200};
5201
5202template <>
5203struct FloatData<long double>
5204{
5205#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5206 defined(__wasm__)
5207 static const size_t mangled_size = 32;
5208#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5209 static const size_t mangled_size = 16;
5210#else
5211 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5212#endif
5213 static const size_t max_demangled_size = 40;
5214 static constexpr const char *spec = "%LaL";
5215};
5216
Pavel Labathba825192018-10-16 14:29:14 +00005217template <typename Alloc, typename Derived>
5218template <class Float>
5219Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005220 const size_t N = FloatData<Float>::mangled_size;
5221 if (numLeft() <= N)
5222 return nullptr;
5223 StringView Data(First, First + N);
5224 for (char C : Data)
5225 if (!std::isxdigit(C))
5226 return nullptr;
5227 First += N;
5228 if (!consumeIf('E'))
5229 return nullptr;
5230 return make<FloatLiteralImpl<Float>>(Data);
5231}
5232
5233// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005234template <typename Alloc, typename Derived>
5235bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005236 if (!(look() >= '0' && look() <= '9') &&
5237 !(look() >= 'A' && look() <= 'Z'))
5238 return true;
5239
5240 size_t Id = 0;
5241 while (true) {
5242 if (look() >= '0' && look() <= '9') {
5243 Id *= 36;
5244 Id += static_cast<size_t>(look() - '0');
5245 } else if (look() >= 'A' && look() <= 'Z') {
5246 Id *= 36;
5247 Id += static_cast<size_t>(look() - 'A') + 10;
5248 } else {
5249 *Out = Id;
5250 return false;
5251 }
5252 ++First;
5253 }
5254}
5255
5256// <substitution> ::= S <seq-id> _
5257// ::= S_
5258// <substitution> ::= Sa # ::std::allocator
5259// <substitution> ::= Sb # ::std::basic_string
5260// <substitution> ::= Ss # ::std::basic_string < char,
5261// ::std::char_traits<char>,
5262// ::std::allocator<char> >
5263// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5264// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5265// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Pavel Labathba825192018-10-16 14:29:14 +00005266template <typename Derived, typename Alloc>
5267Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005268 if (!consumeIf('S'))
5269 return nullptr;
5270
5271 if (std::islower(look())) {
5272 Node *SpecialSub;
5273 switch (look()) {
5274 case 'a':
5275 ++First;
5276 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::allocator);
5277 break;
5278 case 'b':
5279 ++First;
5280 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::basic_string);
5281 break;
5282 case 's':
5283 ++First;
5284 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::string);
5285 break;
5286 case 'i':
5287 ++First;
5288 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::istream);
5289 break;
5290 case 'o':
5291 ++First;
5292 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::ostream);
5293 break;
5294 case 'd':
5295 ++First;
5296 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::iostream);
5297 break;
5298 default:
5299 return nullptr;
5300 }
Richard Smithb485b352018-08-24 23:30:26 +00005301 if (!SpecialSub)
5302 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005303 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5304 // has ABI tags, the tags are appended to the substitution; the result is a
5305 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005306 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005307 if (WithTags != SpecialSub) {
5308 Subs.push_back(WithTags);
5309 SpecialSub = WithTags;
5310 }
5311 return SpecialSub;
5312 }
5313
5314 // ::= S_
5315 if (consumeIf('_')) {
5316 if (Subs.empty())
5317 return nullptr;
5318 return Subs[0];
5319 }
5320
5321 // ::= S <seq-id> _
5322 size_t Index = 0;
5323 if (parseSeqId(&Index))
5324 return nullptr;
5325 ++Index;
5326 if (!consumeIf('_') || Index >= Subs.size())
5327 return nullptr;
5328 return Subs[Index];
5329}
5330
5331// <template-param> ::= T_ # first template parameter
5332// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005333// ::= TL <level-1> __
5334// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005335template <typename Derived, typename Alloc>
5336Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005337 if (!consumeIf('T'))
5338 return nullptr;
5339
Richard Smithdf1c14c2019-09-06 23:53:21 +00005340 size_t Level = 0;
5341 if (consumeIf('L')) {
5342 if (parsePositiveInteger(&Level))
5343 return nullptr;
5344 ++Level;
5345 if (!consumeIf('_'))
5346 return nullptr;
5347 }
5348
Richard Smithc20d1442018-08-20 20:14:49 +00005349 size_t Index = 0;
5350 if (!consumeIf('_')) {
5351 if (parsePositiveInteger(&Index))
5352 return nullptr;
5353 ++Index;
5354 if (!consumeIf('_'))
5355 return nullptr;
5356 }
5357
Richard Smithc20d1442018-08-20 20:14:49 +00005358 // If we're in a context where this <template-param> refers to a
5359 // <template-arg> further ahead in the mangled name (currently just conversion
5360 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005361 // This can only happen at the outermost level.
5362 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005363 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5364 if (!ForwardRef)
5365 return nullptr;
5366 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5367 ForwardTemplateRefs.push_back(
5368 static_cast<ForwardTemplateReference *>(ForwardRef));
5369 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005370 }
5371
Richard Smithdf1c14c2019-09-06 23:53:21 +00005372 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5373 Index >= TemplateParams[Level]->size()) {
5374 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5375 // list are mangled as the corresponding artificial template type parameter.
5376 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5377 // This will be popped by the ScopedTemplateParamList in
5378 // parseUnnamedTypeName.
5379 if (Level == TemplateParams.size())
5380 TemplateParams.push_back(nullptr);
5381 return make<NameType>("auto");
5382 }
5383
Richard Smithc20d1442018-08-20 20:14:49 +00005384 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005385 }
5386
5387 return (*TemplateParams[Level])[Index];
5388}
5389
5390// <template-param-decl> ::= Ty # type parameter
5391// ::= Tn <type> # non-type parameter
5392// ::= Tt <template-param-decl>* E # template parameter
5393// ::= Tp <template-param-decl> # parameter pack
5394template <typename Derived, typename Alloc>
5395Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5396 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5397 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5398 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5399 if (N) TemplateParams.back()->push_back(N);
5400 return N;
5401 };
5402
5403 if (consumeIf("Ty")) {
5404 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5405 if (!Name)
5406 return nullptr;
5407 return make<TypeTemplateParamDecl>(Name);
5408 }
5409
5410 if (consumeIf("Tn")) {
5411 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5412 if (!Name)
5413 return nullptr;
5414 Node *Type = parseType();
5415 if (!Type)
5416 return nullptr;
5417 return make<NonTypeTemplateParamDecl>(Name, Type);
5418 }
5419
5420 if (consumeIf("Tt")) {
5421 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5422 if (!Name)
5423 return nullptr;
5424 size_t ParamsBegin = Names.size();
5425 ScopedTemplateParamList TemplateTemplateParamParams(this);
5426 while (!consumeIf("E")) {
5427 Node *P = parseTemplateParamDecl();
5428 if (!P)
5429 return nullptr;
5430 Names.push_back(P);
5431 }
5432 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5433 return make<TemplateTemplateParamDecl>(Name, Params);
5434 }
5435
5436 if (consumeIf("Tp")) {
5437 Node *P = parseTemplateParamDecl();
5438 if (!P)
5439 return nullptr;
5440 return make<TemplateParamPackDecl>(P);
5441 }
5442
5443 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005444}
5445
5446// <template-arg> ::= <type> # type or template
5447// ::= X <expression> E # expression
5448// ::= <expr-primary> # simple expressions
5449// ::= J <template-arg>* E # argument pack
5450// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005451template <typename Derived, typename Alloc>
5452Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005453 switch (look()) {
5454 case 'X': {
5455 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005456 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005457 if (Arg == nullptr || !consumeIf('E'))
5458 return nullptr;
5459 return Arg;
5460 }
5461 case 'J': {
5462 ++First;
5463 size_t ArgsBegin = Names.size();
5464 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005465 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005466 if (Arg == nullptr)
5467 return nullptr;
5468 Names.push_back(Arg);
5469 }
5470 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5471 return make<TemplateArgumentPack>(Args);
5472 }
5473 case 'L': {
5474 // ::= LZ <encoding> E # extension
5475 if (look(1) == 'Z') {
5476 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005477 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005478 if (Arg == nullptr || !consumeIf('E'))
5479 return nullptr;
5480 return Arg;
5481 }
5482 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005483 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005484 }
5485 default:
Pavel Labathba825192018-10-16 14:29:14 +00005486 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005487 }
5488}
5489
5490// <template-args> ::= I <template-arg>* E
5491// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005492template <typename Derived, typename Alloc>
5493Node *
5494AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005495 if (!consumeIf('I'))
5496 return nullptr;
5497
5498 // <template-params> refer to the innermost <template-args>. Clear out any
5499 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005500 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005501 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005502 TemplateParams.push_back(&OuterTemplateParams);
5503 OuterTemplateParams.clear();
5504 }
Richard Smithc20d1442018-08-20 20:14:49 +00005505
5506 size_t ArgsBegin = Names.size();
5507 while (!consumeIf('E')) {
5508 if (TagTemplates) {
5509 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005510 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005511 TemplateParams = std::move(OldParams);
5512 if (Arg == nullptr)
5513 return nullptr;
5514 Names.push_back(Arg);
5515 Node *TableEntry = Arg;
5516 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5517 TableEntry = make<ParameterPack>(
5518 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005519 if (!TableEntry)
5520 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005521 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005522 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005523 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005524 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005525 if (Arg == nullptr)
5526 return nullptr;
5527 Names.push_back(Arg);
5528 }
5529 }
5530 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5531}
5532
5533// <mangled-name> ::= _Z <encoding>
5534// ::= <type>
5535// extension ::= ___Z <encoding> _block_invoke
5536// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5537// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005538template <typename Derived, typename Alloc>
5539Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005540 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005541 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005542 if (Encoding == nullptr)
5543 return nullptr;
5544 if (look() == '.') {
5545 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5546 First = Last;
5547 }
5548 if (numLeft() != 0)
5549 return nullptr;
5550 return Encoding;
5551 }
5552
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005553 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005554 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005555 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5556 return nullptr;
5557 bool RequireNumber = consumeIf('_');
5558 if (parseNumber().empty() && RequireNumber)
5559 return nullptr;
5560 if (look() == '.')
5561 First = Last;
5562 if (numLeft() != 0)
5563 return nullptr;
5564 return make<SpecialName>("invocation function for block in ", Encoding);
5565 }
5566
Pavel Labathba825192018-10-16 14:29:14 +00005567 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005568 if (numLeft() != 0)
5569 return nullptr;
5570 return Ty;
5571}
5572
Pavel Labathba825192018-10-16 14:29:14 +00005573template <typename Alloc>
5574struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5575 using AbstractManglingParser<ManglingParser<Alloc>,
5576 Alloc>::AbstractManglingParser;
5577};
5578
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005579DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005580
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005581#endif // DEMANGLE_ITANIUMDEMANGLE_H