blob: f5bb0d678c268b5e4e52f3e746548d9c9431ec48 [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
2066 void printLambdaDeclarator(OutputStream &S) const;
2067
2068public:
2069 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2070
2071 template<typename Fn> void match(Fn F) const { F(Type); }
2072
2073 void printLeft(OutputStream &S) const override {
2074 S += "[]";
2075 printLambdaDeclarator(S);
2076 S += "{...}";
2077 }
2078};
2079
Richard Smithc20d1442018-08-20 20:14:49 +00002080class IntegerCastExpr : public Node {
2081 // ty(integer)
2082 const Node *Ty;
2083 StringView Integer;
2084
2085public:
2086 IntegerCastExpr(const Node *Ty_, StringView Integer_)
2087 : Node(KIntegerCastExpr), Ty(Ty_), Integer(Integer_) {}
2088
2089 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2090
2091 void printLeft(OutputStream &S) const override {
2092 S += "(";
2093 Ty->print(S);
2094 S += ")";
2095 S += Integer;
2096 }
2097};
2098
2099class IntegerLiteral : public Node {
2100 StringView Type;
2101 StringView Value;
2102
2103public:
2104 IntegerLiteral(StringView Type_, StringView Value_)
2105 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2106
2107 template<typename Fn> void match(Fn F) const { F(Type, Value); }
2108
2109 void printLeft(OutputStream &S) const override {
2110 if (Type.size() > 3) {
2111 S += "(";
2112 S += Type;
2113 S += ")";
2114 }
2115
2116 if (Value[0] == 'n') {
2117 S += "-";
2118 S += Value.dropFront(1);
2119 } else
2120 S += Value;
2121
2122 if (Type.size() <= 3)
2123 S += Type;
2124 }
2125};
2126
2127template <class Float> struct FloatData;
2128
2129namespace float_literal_impl {
2130constexpr Node::Kind getFloatLiteralKind(float *) {
2131 return Node::KFloatLiteral;
2132}
2133constexpr Node::Kind getFloatLiteralKind(double *) {
2134 return Node::KDoubleLiteral;
2135}
2136constexpr Node::Kind getFloatLiteralKind(long double *) {
2137 return Node::KLongDoubleLiteral;
2138}
2139}
2140
2141template <class Float> class FloatLiteralImpl : public Node {
2142 const StringView Contents;
2143
2144 static constexpr Kind KindForClass =
2145 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2146
2147public:
2148 FloatLiteralImpl(StringView Contents_)
2149 : Node(KindForClass), Contents(Contents_) {}
2150
2151 template<typename Fn> void match(Fn F) const { F(Contents); }
2152
2153 void printLeft(OutputStream &s) const override {
2154 const char *first = Contents.begin();
2155 const char *last = Contents.end() + 1;
2156
2157 const size_t N = FloatData<Float>::mangled_size;
2158 if (static_cast<std::size_t>(last - first) > N) {
2159 last = first + N;
2160 union {
2161 Float value;
2162 char buf[sizeof(Float)];
2163 };
2164 const char *t = first;
2165 char *e = buf;
2166 for (; t != last; ++t, ++e) {
2167 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2168 : static_cast<unsigned>(*t - 'a' + 10);
2169 ++t;
2170 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2171 : static_cast<unsigned>(*t - 'a' + 10);
2172 *e = static_cast<char>((d1 << 4) + d0);
2173 }
2174#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2175 std::reverse(buf, e);
2176#endif
2177 char num[FloatData<Float>::max_demangled_size] = {0};
2178 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
2179 s += StringView(num, num + n);
2180 }
2181 }
2182};
2183
2184using FloatLiteral = FloatLiteralImpl<float>;
2185using DoubleLiteral = FloatLiteralImpl<double>;
2186using LongDoubleLiteral = FloatLiteralImpl<long double>;
2187
2188/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2189/// appropriate derived class.
2190template<typename Fn>
2191void Node::visit(Fn F) const {
2192 switch (K) {
2193#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2194 FOR_EACH_NODE_KIND(CASE)
2195#undef CASE
2196 }
2197 assert(0 && "unknown mangling node kind");
2198}
2199
2200/// Determine the kind of a node from its type.
2201template<typename NodeT> struct NodeKind;
2202#define SPECIALIZATION(X) \
2203 template<> struct NodeKind<X> { \
2204 static constexpr Node::Kind Kind = Node::K##X; \
2205 static constexpr const char *name() { return #X; } \
2206 };
2207FOR_EACH_NODE_KIND(SPECIALIZATION)
2208#undef SPECIALIZATION
2209
2210#undef FOR_EACH_NODE_KIND
2211
Richard Smithdf1c14c2019-09-06 23:53:21 +00002212inline void LambdaExpr::printLambdaDeclarator(OutputStream &S) const {
2213 struct LambdaDeclaratorPrinter {
2214 OutputStream &S;
2215 void operator()(const ClosureTypeName *LambdaType) {
2216 LambdaType->printDeclarator(S);
2217 }
2218
2219 // Walk through any qualifiers to find the lambda-expression.
2220 void operator()(const SpecialName *Name) {
2221 Name->match([&](StringView, const Node *Name) { Name->visit(*this); });
2222 }
2223 void operator()(const NestedName *Name) {
2224 Name->match([&](const Node *, const Node *Name) { Name->visit(*this); });
2225 }
2226 void operator()(const LocalName *Name) {
2227 Name->match([&](const Node *, const Node *Name) { Name->visit(*this); });
2228 }
2229 void operator()(const QualifiedName *Name) {
2230 Name->match([&](const Node *, const Node *Name) { Name->visit(*this); });
2231 }
2232 void operator()(const GlobalQualifiedName *Name) {
2233 Name->match([&](const Node *Child) { Child->visit(*this); });
2234 }
2235 void operator()(const StdQualifiedName *Name) {
2236 Name->match([&](const Node *Child) { Child->visit(*this); });
2237 }
2238 void operator()(const Node *) {
2239 // If we can't find the lambda type, just print '[]{...}'.
2240 }
2241 };
2242 return Type->visit(LambdaDeclaratorPrinter{S});
2243}
2244
Richard Smithc20d1442018-08-20 20:14:49 +00002245template <class T, size_t N>
2246class PODSmallVector {
2247 static_assert(std::is_pod<T>::value,
2248 "T is required to be a plain old data type");
2249
2250 T* First;
2251 T* Last;
2252 T* Cap;
2253 T Inline[N];
2254
2255 bool isInline() const { return First == Inline; }
2256
2257 void clearInline() {
2258 First = Inline;
2259 Last = Inline;
2260 Cap = Inline + N;
2261 }
2262
2263 void reserve(size_t NewCap) {
2264 size_t S = size();
2265 if (isInline()) {
2266 auto* Tmp = static_cast<T*>(std::malloc(NewCap * sizeof(T)));
2267 if (Tmp == nullptr)
2268 std::terminate();
2269 std::copy(First, Last, Tmp);
2270 First = Tmp;
2271 } else {
2272 First = static_cast<T*>(std::realloc(First, NewCap * sizeof(T)));
2273 if (First == nullptr)
2274 std::terminate();
2275 }
2276 Last = First + S;
2277 Cap = First + NewCap;
2278 }
2279
2280public:
2281 PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
2282
2283 PODSmallVector(const PODSmallVector&) = delete;
2284 PODSmallVector& operator=(const PODSmallVector&) = delete;
2285
2286 PODSmallVector(PODSmallVector&& Other) : PODSmallVector() {
2287 if (Other.isInline()) {
2288 std::copy(Other.begin(), Other.end(), First);
2289 Last = First + Other.size();
2290 Other.clear();
2291 return;
2292 }
2293
2294 First = Other.First;
2295 Last = Other.Last;
2296 Cap = Other.Cap;
2297 Other.clearInline();
2298 }
2299
2300 PODSmallVector& operator=(PODSmallVector&& Other) {
2301 if (Other.isInline()) {
2302 if (!isInline()) {
2303 std::free(First);
2304 clearInline();
2305 }
2306 std::copy(Other.begin(), Other.end(), First);
2307 Last = First + Other.size();
2308 Other.clear();
2309 return *this;
2310 }
2311
2312 if (isInline()) {
2313 First = Other.First;
2314 Last = Other.Last;
2315 Cap = Other.Cap;
2316 Other.clearInline();
2317 return *this;
2318 }
2319
2320 std::swap(First, Other.First);
2321 std::swap(Last, Other.Last);
2322 std::swap(Cap, Other.Cap);
2323 Other.clear();
2324 return *this;
2325 }
2326
2327 void push_back(const T& Elem) {
2328 if (Last == Cap)
2329 reserve(size() * 2);
2330 *Last++ = Elem;
2331 }
2332
2333 void pop_back() {
2334 assert(Last != First && "Popping empty vector!");
2335 --Last;
2336 }
2337
2338 void dropBack(size_t Index) {
2339 assert(Index <= size() && "dropBack() can't expand!");
2340 Last = First + Index;
2341 }
2342
2343 T* begin() { return First; }
2344 T* end() { return Last; }
2345
2346 bool empty() const { return First == Last; }
2347 size_t size() const { return static_cast<size_t>(Last - First); }
2348 T& back() {
2349 assert(Last != First && "Calling back() on empty vector!");
2350 return *(Last - 1);
2351 }
2352 T& operator[](size_t Index) {
2353 assert(Index < size() && "Invalid access!");
2354 return *(begin() + Index);
2355 }
2356 void clear() { Last = First; }
2357
2358 ~PODSmallVector() {
2359 if (!isInline())
2360 std::free(First);
2361 }
2362};
2363
Pavel Labathba825192018-10-16 14:29:14 +00002364template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002365 const char *First;
2366 const char *Last;
2367
2368 // Name stack, this is used by the parser to hold temporary names that were
2369 // parsed. The parser collapses multiple names into new nodes to construct
2370 // the AST. Once the parser is finished, names.size() == 1.
2371 PODSmallVector<Node *, 32> Names;
2372
2373 // Substitution table. Itanium supports name substitutions as a means of
2374 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2375 // table.
2376 PODSmallVector<Node *, 32> Subs;
2377
Richard Smithdf1c14c2019-09-06 23:53:21 +00002378 using TemplateParamList = PODSmallVector<Node *, 8>;
2379
2380 class ScopedTemplateParamList {
2381 AbstractManglingParser *Parser;
2382 size_t OldNumTemplateParamLists;
2383 TemplateParamList Params;
2384
2385 public:
2386 ScopedTemplateParamList(AbstractManglingParser *Parser)
2387 : Parser(Parser),
2388 OldNumTemplateParamLists(Parser->TemplateParams.size()) {
2389 Parser->TemplateParams.push_back(&Params);
2390 }
2391 ~ScopedTemplateParamList() {
2392 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2393 Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2394 }
2395 void push_back(Node *Param) {
2396 Params.push_back(Param);
2397 }
2398 };
2399
Richard Smithc20d1442018-08-20 20:14:49 +00002400 // Template parameter table. Like the above, but referenced like "T42_".
2401 // This has a smaller size compared to Subs and Names because it can be
2402 // stored on the stack.
Richard Smithdf1c14c2019-09-06 23:53:21 +00002403 TemplateParamList OuterTemplateParams;
2404
2405 // Lists of template parameters indexed by template parameter depth,
2406 // referenced like "TL2_4_". If nonempty, element 0 is always
2407 // OuterTemplateParams; inner elements are always template parameter lists of
2408 // lambda expressions. For a generic lambda with no explicit template
2409 // parameter list, the corresponding parameter list pointer will be null.
2410 PODSmallVector<TemplateParamList *, 4> TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00002411
2412 // Set of unresolved forward <template-param> references. These can occur in a
2413 // conversion operator's type, and are resolved in the enclosing <encoding>.
2414 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2415
Richard Smithc20d1442018-08-20 20:14:49 +00002416 bool TryToParseTemplateArgs = true;
2417 bool PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002418 size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2419
2420 unsigned NumSyntheticTemplateParameters[3] = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002421
2422 Alloc ASTAllocator;
2423
Pavel Labathba825192018-10-16 14:29:14 +00002424 AbstractManglingParser(const char *First_, const char *Last_)
2425 : First(First_), Last(Last_) {}
2426
2427 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002428
2429 void reset(const char *First_, const char *Last_) {
2430 First = First_;
2431 Last = Last_;
2432 Names.clear();
2433 Subs.clear();
2434 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002435 ParsingLambdaParamsAtLevel = (size_t)-1;
Richard Smithc20d1442018-08-20 20:14:49 +00002436 TryToParseTemplateArgs = true;
2437 PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002438 NumSyntheticTemplateParameters = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002439 ASTAllocator.reset();
2440 }
2441
Richard Smithb485b352018-08-24 23:30:26 +00002442 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002443 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2444 }
2445
2446 template <class It> NodeArray makeNodeArray(It begin, It end) {
2447 size_t sz = static_cast<size_t>(end - begin);
2448 void *mem = ASTAllocator.allocateNodeArray(sz);
2449 Node **data = new (mem) Node *[sz];
2450 std::copy(begin, end, data);
2451 return NodeArray(data, sz);
2452 }
2453
2454 NodeArray popTrailingNodeArray(size_t FromPosition) {
2455 assert(FromPosition <= Names.size());
2456 NodeArray res =
2457 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2458 Names.dropBack(FromPosition);
2459 return res;
2460 }
2461
2462 bool consumeIf(StringView S) {
2463 if (StringView(First, Last).startsWith(S)) {
2464 First += S.size();
2465 return true;
2466 }
2467 return false;
2468 }
2469
2470 bool consumeIf(char C) {
2471 if (First != Last && *First == C) {
2472 ++First;
2473 return true;
2474 }
2475 return false;
2476 }
2477
2478 char consume() { return First != Last ? *First++ : '\0'; }
2479
2480 char look(unsigned Lookahead = 0) {
2481 if (static_cast<size_t>(Last - First) <= Lookahead)
2482 return '\0';
2483 return First[Lookahead];
2484 }
2485
2486 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2487
2488 StringView parseNumber(bool AllowNegative = false);
2489 Qualifiers parseCVQualifiers();
2490 bool parsePositiveInteger(size_t *Out);
2491 StringView parseBareSourceName();
2492
2493 bool parseSeqId(size_t *Out);
2494 Node *parseSubstitution();
2495 Node *parseTemplateParam();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002496 Node *parseTemplateParamDecl();
Richard Smithc20d1442018-08-20 20:14:49 +00002497 Node *parseTemplateArgs(bool TagTemplates = false);
2498 Node *parseTemplateArg();
2499
2500 /// Parse the <expr> production.
2501 Node *parseExpr();
2502 Node *parsePrefixExpr(StringView Kind);
2503 Node *parseBinaryExpr(StringView Kind);
2504 Node *parseIntegerLiteral(StringView Lit);
2505 Node *parseExprPrimary();
2506 template <class Float> Node *parseFloatingLiteral();
2507 Node *parseFunctionParam();
2508 Node *parseNewExpr();
2509 Node *parseConversionExpr();
2510 Node *parseBracedExpr();
2511 Node *parseFoldExpr();
2512
2513 /// Parse the <type> production.
2514 Node *parseType();
2515 Node *parseFunctionType();
2516 Node *parseVectorType();
2517 Node *parseDecltype();
2518 Node *parseArrayType();
2519 Node *parsePointerToMemberType();
2520 Node *parseClassEnumType();
2521 Node *parseQualifiedType();
2522
2523 Node *parseEncoding();
2524 bool parseCallOffset();
2525 Node *parseSpecialName();
2526
2527 /// Holds some extra information about a <name> that is being parsed. This
2528 /// information is only pertinent if the <name> refers to an <encoding>.
2529 struct NameState {
2530 bool CtorDtorConversion = false;
2531 bool EndsWithTemplateArgs = false;
2532 Qualifiers CVQualifiers = QualNone;
2533 FunctionRefQual ReferenceQualifier = FrefQualNone;
2534 size_t ForwardTemplateRefsBegin;
2535
Pavel Labathba825192018-10-16 14:29:14 +00002536 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002537 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2538 };
2539
2540 bool resolveForwardTemplateRefs(NameState &State) {
2541 size_t I = State.ForwardTemplateRefsBegin;
2542 size_t E = ForwardTemplateRefs.size();
2543 for (; I < E; ++I) {
2544 size_t Idx = ForwardTemplateRefs[I]->Index;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002545 if (TemplateParams.empty() || !TemplateParams[0] ||
2546 Idx >= TemplateParams[0]->size())
Richard Smithc20d1442018-08-20 20:14:49 +00002547 return true;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002548 ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
Richard Smithc20d1442018-08-20 20:14:49 +00002549 }
2550 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2551 return false;
2552 }
2553
2554 /// Parse the <name> production>
2555 Node *parseName(NameState *State = nullptr);
2556 Node *parseLocalName(NameState *State);
2557 Node *parseOperatorName(NameState *State);
2558 Node *parseUnqualifiedName(NameState *State);
2559 Node *parseUnnamedTypeName(NameState *State);
2560 Node *parseSourceName(NameState *State);
2561 Node *parseUnscopedName(NameState *State);
2562 Node *parseNestedName(NameState *State);
2563 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2564
2565 Node *parseAbiTags(Node *N);
2566
2567 /// Parse the <unresolved-name> production.
2568 Node *parseUnresolvedName();
2569 Node *parseSimpleId();
2570 Node *parseBaseUnresolvedName();
2571 Node *parseUnresolvedType();
2572 Node *parseDestructorName();
2573
2574 /// Top-level entry point into the parser.
2575 Node *parse();
2576};
2577
2578const char* parse_discriminator(const char* first, const char* last);
2579
2580// <name> ::= <nested-name> // N
2581// ::= <local-name> # See Scope Encoding below // Z
2582// ::= <unscoped-template-name> <template-args>
2583// ::= <unscoped-name>
2584//
2585// <unscoped-template-name> ::= <unscoped-name>
2586// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002587template <typename Derived, typename Alloc>
2588Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002589 consumeIf('L'); // extension
2590
2591 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002592 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002593 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002594 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002595
2596 // ::= <unscoped-template-name> <template-args>
2597 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00002598 Node *S = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00002599 if (S == nullptr)
2600 return nullptr;
2601 if (look() != 'I')
2602 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002603 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002604 if (TA == nullptr)
2605 return nullptr;
2606 if (State) State->EndsWithTemplateArgs = true;
2607 return make<NameWithTemplateArgs>(S, TA);
2608 }
2609
Pavel Labathba825192018-10-16 14:29:14 +00002610 Node *N = getDerived().parseUnscopedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002611 if (N == nullptr)
2612 return nullptr;
2613 // ::= <unscoped-template-name> <template-args>
2614 if (look() == 'I') {
2615 Subs.push_back(N);
Pavel Labathba825192018-10-16 14:29:14 +00002616 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002617 if (TA == nullptr)
2618 return nullptr;
2619 if (State) State->EndsWithTemplateArgs = true;
2620 return make<NameWithTemplateArgs>(N, TA);
2621 }
2622 // ::= <unscoped-name>
2623 return N;
2624}
2625
2626// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2627// := Z <function encoding> E s [<discriminator>]
2628// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002629template <typename Derived, typename Alloc>
2630Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002631 if (!consumeIf('Z'))
2632 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002633 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002634 if (Encoding == nullptr || !consumeIf('E'))
2635 return nullptr;
2636
2637 if (consumeIf('s')) {
2638 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002639 auto *StringLitName = make<NameType>("string literal");
2640 if (!StringLitName)
2641 return nullptr;
2642 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002643 }
2644
2645 if (consumeIf('d')) {
2646 parseNumber(true);
2647 if (!consumeIf('_'))
2648 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002649 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002650 if (N == nullptr)
2651 return nullptr;
2652 return make<LocalName>(Encoding, N);
2653 }
2654
Pavel Labathba825192018-10-16 14:29:14 +00002655 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002656 if (Entity == nullptr)
2657 return nullptr;
2658 First = parse_discriminator(First, Last);
2659 return make<LocalName>(Encoding, Entity);
2660}
2661
2662// <unscoped-name> ::= <unqualified-name>
2663// ::= St <unqualified-name> # ::std::
2664// extension ::= StL<unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00002665template <typename Derived, typename Alloc>
2666Node *
2667AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
2668 if (consumeIf("StL") || consumeIf("St")) {
2669 Node *R = getDerived().parseUnqualifiedName(State);
2670 if (R == nullptr)
2671 return nullptr;
2672 return make<StdQualifiedName>(R);
2673 }
2674 return getDerived().parseUnqualifiedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002675}
2676
2677// <unqualified-name> ::= <operator-name> [abi-tags]
2678// ::= <ctor-dtor-name>
2679// ::= <source-name>
2680// ::= <unnamed-type-name>
2681// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002682template <typename Derived, typename Alloc>
2683Node *
2684AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002685 // <ctor-dtor-name>s are special-cased in parseNestedName().
2686 Node *Result;
2687 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002688 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002689 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002690 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002691 else if (consumeIf("DC")) {
2692 size_t BindingsBegin = Names.size();
2693 do {
Pavel Labathba825192018-10-16 14:29:14 +00002694 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002695 if (Binding == nullptr)
2696 return nullptr;
2697 Names.push_back(Binding);
2698 } while (!consumeIf('E'));
2699 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2700 } else
Pavel Labathba825192018-10-16 14:29:14 +00002701 Result = getDerived().parseOperatorName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002702 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002703 Result = getDerived().parseAbiTags(Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002704 return Result;
2705}
2706
2707// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2708// ::= <closure-type-name>
2709//
2710// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2711//
2712// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002713template <typename Derived, typename Alloc>
2714Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002715AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2716 // <template-params> refer to the innermost <template-args>. Clear out any
2717 // outer args that we may have inserted into TemplateParams.
2718 if (State != nullptr)
2719 TemplateParams.clear();
2720
Richard Smithc20d1442018-08-20 20:14:49 +00002721 if (consumeIf("Ut")) {
2722 StringView Count = parseNumber();
2723 if (!consumeIf('_'))
2724 return nullptr;
2725 return make<UnnamedTypeName>(Count);
2726 }
2727 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002728 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2729 TemplateParams.size());
2730 ScopedTemplateParamList LambdaTemplateParams(this);
2731
2732 size_t ParamsBegin = Names.size();
2733 while (look() == 'T' &&
2734 StringView("yptn").find(look(1)) != StringView::npos) {
2735 Node *T = parseTemplateParamDecl();
2736 if (!T)
2737 return nullptr;
2738 LambdaTemplateParams.push_back(T);
2739 Names.push_back(T);
2740 }
2741 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2742
2743 // FIXME: If TempParams is empty and none of the function parameters
2744 // includes 'auto', we should remove LambdaTemplateParams from the
2745 // TemplateParams list. Unfortunately, we don't find out whether there are
2746 // any 'auto' parameters until too late in an example such as:
2747 //
2748 // template<typename T> void f(
2749 // decltype([](decltype([]<typename T>(T v) {}),
2750 // auto) {})) {}
2751 // template<typename T> void f(
2752 // decltype([](decltype([]<typename T>(T w) {}),
2753 // int) {})) {}
2754 //
2755 // Here, the type of v is at level 2 but the type of w is at level 1. We
2756 // don't find this out until we encounter the type of the next parameter.
2757 //
2758 // However, compilers can't actually cope with the former example in
2759 // practice, and it's likely to be made ill-formed in future, so we don't
2760 // need to support it here.
2761 //
2762 // If we encounter an 'auto' in the function parameter types, we will
2763 // recreate a template parameter scope for it, but any intervening lambdas
2764 // will be parsed in the 'wrong' template parameter depth.
2765 if (TempParams.empty())
2766 TemplateParams.pop_back();
2767
Richard Smithc20d1442018-08-20 20:14:49 +00002768 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002769 do {
Pavel Labathba825192018-10-16 14:29:14 +00002770 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002771 if (P == nullptr)
2772 return nullptr;
2773 Names.push_back(P);
2774 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002775 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002776 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2777
Richard Smithc20d1442018-08-20 20:14:49 +00002778 StringView Count = parseNumber();
2779 if (!consumeIf('_'))
2780 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002781 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002782 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002783 if (consumeIf("Ub")) {
2784 (void)parseNumber();
2785 if (!consumeIf('_'))
2786 return nullptr;
2787 return make<NameType>("'block-literal'");
2788 }
Richard Smithc20d1442018-08-20 20:14:49 +00002789 return nullptr;
2790}
2791
2792// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002793template <typename Derived, typename Alloc>
2794Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002795 size_t Length = 0;
2796 if (parsePositiveInteger(&Length))
2797 return nullptr;
2798 if (numLeft() < Length || Length == 0)
2799 return nullptr;
2800 StringView Name(First, First + Length);
2801 First += Length;
2802 if (Name.startsWith("_GLOBAL__N"))
2803 return make<NameType>("(anonymous namespace)");
2804 return make<NameType>(Name);
2805}
2806
2807// <operator-name> ::= aa # &&
2808// ::= ad # & (unary)
2809// ::= an # &
2810// ::= aN # &=
2811// ::= aS # =
2812// ::= cl # ()
2813// ::= cm # ,
2814// ::= co # ~
2815// ::= cv <type> # (cast)
2816// ::= da # delete[]
2817// ::= de # * (unary)
2818// ::= dl # delete
2819// ::= dv # /
2820// ::= dV # /=
2821// ::= eo # ^
2822// ::= eO # ^=
2823// ::= eq # ==
2824// ::= ge # >=
2825// ::= gt # >
2826// ::= ix # []
2827// ::= le # <=
2828// ::= li <source-name> # operator ""
2829// ::= ls # <<
2830// ::= lS # <<=
2831// ::= lt # <
2832// ::= mi # -
2833// ::= mI # -=
2834// ::= ml # *
2835// ::= mL # *=
2836// ::= mm # -- (postfix in <expression> context)
2837// ::= na # new[]
2838// ::= ne # !=
2839// ::= ng # - (unary)
2840// ::= nt # !
2841// ::= nw # new
2842// ::= oo # ||
2843// ::= or # |
2844// ::= oR # |=
2845// ::= pm # ->*
2846// ::= pl # +
2847// ::= pL # +=
2848// ::= pp # ++ (postfix in <expression> context)
2849// ::= ps # + (unary)
2850// ::= pt # ->
2851// ::= qu # ?
2852// ::= rm # %
2853// ::= rM # %=
2854// ::= rs # >>
2855// ::= rS # >>=
2856// ::= ss # <=> C++2a
2857// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002858template <typename Derived, typename Alloc>
2859Node *
2860AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002861 switch (look()) {
2862 case 'a':
2863 switch (look(1)) {
2864 case 'a':
2865 First += 2;
2866 return make<NameType>("operator&&");
2867 case 'd':
2868 case 'n':
2869 First += 2;
2870 return make<NameType>("operator&");
2871 case 'N':
2872 First += 2;
2873 return make<NameType>("operator&=");
2874 case 'S':
2875 First += 2;
2876 return make<NameType>("operator=");
2877 }
2878 return nullptr;
2879 case 'c':
2880 switch (look(1)) {
2881 case 'l':
2882 First += 2;
2883 return make<NameType>("operator()");
2884 case 'm':
2885 First += 2;
2886 return make<NameType>("operator,");
2887 case 'o':
2888 First += 2;
2889 return make<NameType>("operator~");
2890 // ::= cv <type> # (cast)
2891 case 'v': {
2892 First += 2;
2893 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2894 // If we're parsing an encoding, State != nullptr and the conversion
2895 // operators' <type> could have a <template-param> that refers to some
2896 // <template-arg>s further ahead in the mangled name.
2897 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2898 PermitForwardTemplateReferences ||
2899 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002900 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002901 if (Ty == nullptr)
2902 return nullptr;
2903 if (State) State->CtorDtorConversion = true;
2904 return make<ConversionOperatorType>(Ty);
2905 }
2906 }
2907 return nullptr;
2908 case 'd':
2909 switch (look(1)) {
2910 case 'a':
2911 First += 2;
2912 return make<NameType>("operator delete[]");
2913 case 'e':
2914 First += 2;
2915 return make<NameType>("operator*");
2916 case 'l':
2917 First += 2;
2918 return make<NameType>("operator delete");
2919 case 'v':
2920 First += 2;
2921 return make<NameType>("operator/");
2922 case 'V':
2923 First += 2;
2924 return make<NameType>("operator/=");
2925 }
2926 return nullptr;
2927 case 'e':
2928 switch (look(1)) {
2929 case 'o':
2930 First += 2;
2931 return make<NameType>("operator^");
2932 case 'O':
2933 First += 2;
2934 return make<NameType>("operator^=");
2935 case 'q':
2936 First += 2;
2937 return make<NameType>("operator==");
2938 }
2939 return nullptr;
2940 case 'g':
2941 switch (look(1)) {
2942 case 'e':
2943 First += 2;
2944 return make<NameType>("operator>=");
2945 case 't':
2946 First += 2;
2947 return make<NameType>("operator>");
2948 }
2949 return nullptr;
2950 case 'i':
2951 if (look(1) == 'x') {
2952 First += 2;
2953 return make<NameType>("operator[]");
2954 }
2955 return nullptr;
2956 case 'l':
2957 switch (look(1)) {
2958 case 'e':
2959 First += 2;
2960 return make<NameType>("operator<=");
2961 // ::= li <source-name> # operator ""
2962 case 'i': {
2963 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002964 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002965 if (SN == nullptr)
2966 return nullptr;
2967 return make<LiteralOperator>(SN);
2968 }
2969 case 's':
2970 First += 2;
2971 return make<NameType>("operator<<");
2972 case 'S':
2973 First += 2;
2974 return make<NameType>("operator<<=");
2975 case 't':
2976 First += 2;
2977 return make<NameType>("operator<");
2978 }
2979 return nullptr;
2980 case 'm':
2981 switch (look(1)) {
2982 case 'i':
2983 First += 2;
2984 return make<NameType>("operator-");
2985 case 'I':
2986 First += 2;
2987 return make<NameType>("operator-=");
2988 case 'l':
2989 First += 2;
2990 return make<NameType>("operator*");
2991 case 'L':
2992 First += 2;
2993 return make<NameType>("operator*=");
2994 case 'm':
2995 First += 2;
2996 return make<NameType>("operator--");
2997 }
2998 return nullptr;
2999 case 'n':
3000 switch (look(1)) {
3001 case 'a':
3002 First += 2;
3003 return make<NameType>("operator new[]");
3004 case 'e':
3005 First += 2;
3006 return make<NameType>("operator!=");
3007 case 'g':
3008 First += 2;
3009 return make<NameType>("operator-");
3010 case 't':
3011 First += 2;
3012 return make<NameType>("operator!");
3013 case 'w':
3014 First += 2;
3015 return make<NameType>("operator new");
3016 }
3017 return nullptr;
3018 case 'o':
3019 switch (look(1)) {
3020 case 'o':
3021 First += 2;
3022 return make<NameType>("operator||");
3023 case 'r':
3024 First += 2;
3025 return make<NameType>("operator|");
3026 case 'R':
3027 First += 2;
3028 return make<NameType>("operator|=");
3029 }
3030 return nullptr;
3031 case 'p':
3032 switch (look(1)) {
3033 case 'm':
3034 First += 2;
3035 return make<NameType>("operator->*");
3036 case 'l':
3037 First += 2;
3038 return make<NameType>("operator+");
3039 case 'L':
3040 First += 2;
3041 return make<NameType>("operator+=");
3042 case 'p':
3043 First += 2;
3044 return make<NameType>("operator++");
3045 case 's':
3046 First += 2;
3047 return make<NameType>("operator+");
3048 case 't':
3049 First += 2;
3050 return make<NameType>("operator->");
3051 }
3052 return nullptr;
3053 case 'q':
3054 if (look(1) == 'u') {
3055 First += 2;
3056 return make<NameType>("operator?");
3057 }
3058 return nullptr;
3059 case 'r':
3060 switch (look(1)) {
3061 case 'm':
3062 First += 2;
3063 return make<NameType>("operator%");
3064 case 'M':
3065 First += 2;
3066 return make<NameType>("operator%=");
3067 case 's':
3068 First += 2;
3069 return make<NameType>("operator>>");
3070 case 'S':
3071 First += 2;
3072 return make<NameType>("operator>>=");
3073 }
3074 return nullptr;
3075 case 's':
3076 if (look(1) == 's') {
3077 First += 2;
3078 return make<NameType>("operator<=>");
3079 }
3080 return nullptr;
3081 // ::= v <digit> <source-name> # vendor extended operator
3082 case 'v':
3083 if (std::isdigit(look(1))) {
3084 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003085 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003086 if (SN == nullptr)
3087 return nullptr;
3088 return make<ConversionOperatorType>(SN);
3089 }
3090 return nullptr;
3091 }
3092 return nullptr;
3093}
3094
3095// <ctor-dtor-name> ::= C1 # complete object constructor
3096// ::= C2 # base object constructor
3097// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003098// extension ::= C4 # gcc old-style "[unified]" constructor
3099// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003100// ::= D0 # deleting destructor
3101// ::= D1 # complete object destructor
3102// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003103// extension ::= D4 # gcc old-style "[unified]" destructor
3104// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003105template <typename Derived, typename Alloc>
3106Node *
3107AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3108 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003109 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3110 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3111 switch (SSK) {
3112 case SpecialSubKind::string:
3113 case SpecialSubKind::istream:
3114 case SpecialSubKind::ostream:
3115 case SpecialSubKind::iostream:
3116 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003117 if (!SoFar)
3118 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003119 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003120 default:
3121 break;
3122 }
3123 }
3124
3125 if (consumeIf('C')) {
3126 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003127 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3128 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003129 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003130 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003131 ++First;
3132 if (State) State->CtorDtorConversion = true;
3133 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003134 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003135 return nullptr;
3136 }
Nico Weber29294792019-04-03 23:14:33 +00003137 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003138 }
3139
Nico Weber29294792019-04-03 23:14:33 +00003140 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3141 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003142 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003143 First += 2;
3144 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003145 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003146 }
3147
3148 return nullptr;
3149}
3150
3151// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3152// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3153//
3154// <prefix> ::= <prefix> <unqualified-name>
3155// ::= <template-prefix> <template-args>
3156// ::= <template-param>
3157// ::= <decltype>
3158// ::= # empty
3159// ::= <substitution>
3160// ::= <prefix> <data-member-prefix>
3161// extension ::= L
3162//
3163// <data-member-prefix> := <member source-name> [<template-args>] M
3164//
3165// <template-prefix> ::= <prefix> <template unqualified-name>
3166// ::= <template-param>
3167// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003168template <typename Derived, typename Alloc>
3169Node *
3170AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003171 if (!consumeIf('N'))
3172 return nullptr;
3173
3174 Qualifiers CVTmp = parseCVQualifiers();
3175 if (State) State->CVQualifiers = CVTmp;
3176
3177 if (consumeIf('O')) {
3178 if (State) State->ReferenceQualifier = FrefQualRValue;
3179 } else if (consumeIf('R')) {
3180 if (State) State->ReferenceQualifier = FrefQualLValue;
3181 } else
3182 if (State) State->ReferenceQualifier = FrefQualNone;
3183
3184 Node *SoFar = nullptr;
3185 auto PushComponent = [&](Node *Comp) {
Richard Smithb485b352018-08-24 23:30:26 +00003186 if (!Comp) return false;
Richard Smithc20d1442018-08-20 20:14:49 +00003187 if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
3188 else SoFar = Comp;
3189 if (State) State->EndsWithTemplateArgs = false;
Richard Smithb485b352018-08-24 23:30:26 +00003190 return SoFar != nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003191 };
3192
Richard Smithb485b352018-08-24 23:30:26 +00003193 if (consumeIf("St")) {
Richard Smithc20d1442018-08-20 20:14:49 +00003194 SoFar = make<NameType>("std");
Richard Smithb485b352018-08-24 23:30:26 +00003195 if (!SoFar)
3196 return nullptr;
3197 }
Richard Smithc20d1442018-08-20 20:14:49 +00003198
3199 while (!consumeIf('E')) {
3200 consumeIf('L'); // extension
3201
3202 // <data-member-prefix> := <member source-name> [<template-args>] M
3203 if (consumeIf('M')) {
3204 if (SoFar == nullptr)
3205 return nullptr;
3206 continue;
3207 }
3208
3209 // ::= <template-param>
3210 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003211 if (!PushComponent(getDerived().parseTemplateParam()))
Richard Smithc20d1442018-08-20 20:14:49 +00003212 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003213 Subs.push_back(SoFar);
3214 continue;
3215 }
3216
3217 // ::= <template-prefix> <template-args>
3218 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003219 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003220 if (TA == nullptr || SoFar == nullptr)
3221 return nullptr;
3222 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003223 if (!SoFar)
3224 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003225 if (State) State->EndsWithTemplateArgs = true;
3226 Subs.push_back(SoFar);
3227 continue;
3228 }
3229
3230 // ::= <decltype>
3231 if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
Pavel Labathba825192018-10-16 14:29:14 +00003232 if (!PushComponent(getDerived().parseDecltype()))
Richard Smithc20d1442018-08-20 20:14:49 +00003233 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003234 Subs.push_back(SoFar);
3235 continue;
3236 }
3237
3238 // ::= <substitution>
3239 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00003240 Node *S = getDerived().parseSubstitution();
Richard Smithb485b352018-08-24 23:30:26 +00003241 if (!PushComponent(S))
Richard Smithc20d1442018-08-20 20:14:49 +00003242 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003243 if (SoFar != S)
3244 Subs.push_back(S);
3245 continue;
3246 }
3247
3248 // Parse an <unqualified-name> thats actually a <ctor-dtor-name>.
3249 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
3250 if (SoFar == nullptr)
3251 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003252 if (!PushComponent(getDerived().parseCtorDtorName(SoFar, State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003253 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003254 SoFar = getDerived().parseAbiTags(SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00003255 if (SoFar == nullptr)
3256 return nullptr;
3257 Subs.push_back(SoFar);
3258 continue;
3259 }
3260
3261 // ::= <prefix> <unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00003262 if (!PushComponent(getDerived().parseUnqualifiedName(State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003263 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003264 Subs.push_back(SoFar);
3265 }
3266
3267 if (SoFar == nullptr || Subs.empty())
3268 return nullptr;
3269
3270 Subs.pop_back();
3271 return SoFar;
3272}
3273
3274// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003275template <typename Derived, typename Alloc>
3276Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3277 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003278 if (SN == nullptr)
3279 return nullptr;
3280 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003281 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003282 if (TA == nullptr)
3283 return nullptr;
3284 return make<NameWithTemplateArgs>(SN, TA);
3285 }
3286 return SN;
3287}
3288
3289// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3290// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003291template <typename Derived, typename Alloc>
3292Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003293 Node *Result;
3294 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003295 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003296 else
Pavel Labathba825192018-10-16 14:29:14 +00003297 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003298 if (Result == nullptr)
3299 return nullptr;
3300 return make<DtorName>(Result);
3301}
3302
3303// <unresolved-type> ::= <template-param>
3304// ::= <decltype>
3305// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003306template <typename Derived, typename Alloc>
3307Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003308 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003309 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003310 if (TP == nullptr)
3311 return nullptr;
3312 Subs.push_back(TP);
3313 return TP;
3314 }
3315 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003316 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003317 if (DT == nullptr)
3318 return nullptr;
3319 Subs.push_back(DT);
3320 return DT;
3321 }
Pavel Labathba825192018-10-16 14:29:14 +00003322 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003323}
3324
3325// <base-unresolved-name> ::= <simple-id> # unresolved name
3326// extension ::= <operator-name> # unresolved operator-function-id
3327// extension ::= <operator-name> <template-args> # unresolved operator template-id
3328// ::= on <operator-name> # unresolved operator-function-id
3329// ::= on <operator-name> <template-args> # unresolved operator template-id
3330// ::= dn <destructor-name> # destructor or pseudo-destructor;
3331// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003332template <typename Derived, typename Alloc>
3333Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003334 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003335 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003336
3337 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003338 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003339
3340 consumeIf("on");
3341
Pavel Labathba825192018-10-16 14:29:14 +00003342 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003343 if (Oper == nullptr)
3344 return nullptr;
3345 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003346 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003347 if (TA == nullptr)
3348 return nullptr;
3349 return make<NameWithTemplateArgs>(Oper, TA);
3350 }
3351 return Oper;
3352}
3353
3354// <unresolved-name>
3355// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3356// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3357// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3358// # A::x, N::y, A<T>::z; "gs" means leading "::"
3359// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3360// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3361// # T::N::x /decltype(p)::N::x
3362// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3363//
3364// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003365template <typename Derived, typename Alloc>
3366Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003367 Node *SoFar = nullptr;
3368
3369 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3370 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3371 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003372 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003373 if (SoFar == nullptr)
3374 return nullptr;
3375
3376 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003377 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003378 if (TA == nullptr)
3379 return nullptr;
3380 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003381 if (!SoFar)
3382 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003383 }
3384
3385 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003386 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003387 if (Qual == nullptr)
3388 return nullptr;
3389 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003390 if (!SoFar)
3391 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003392 }
3393
Pavel Labathba825192018-10-16 14:29:14 +00003394 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003395 if (Base == nullptr)
3396 return nullptr;
3397 return make<QualifiedName>(SoFar, Base);
3398 }
3399
3400 bool Global = consumeIf("gs");
3401
3402 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3403 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003404 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003405 if (SoFar == nullptr)
3406 return nullptr;
3407 if (Global)
3408 SoFar = make<GlobalQualifiedName>(SoFar);
3409 return SoFar;
3410 }
3411
3412 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3413 if (std::isdigit(look())) {
3414 do {
Pavel Labathba825192018-10-16 14:29:14 +00003415 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003416 if (Qual == nullptr)
3417 return nullptr;
3418 if (SoFar)
3419 SoFar = make<QualifiedName>(SoFar, Qual);
3420 else if (Global)
3421 SoFar = make<GlobalQualifiedName>(Qual);
3422 else
3423 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003424 if (!SoFar)
3425 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003426 } while (!consumeIf('E'));
3427 }
3428 // sr <unresolved-type> <base-unresolved-name>
3429 // sr <unresolved-type> <template-args> <base-unresolved-name>
3430 else {
Pavel Labathba825192018-10-16 14:29:14 +00003431 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003432 if (SoFar == nullptr)
3433 return nullptr;
3434
3435 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003436 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003437 if (TA == nullptr)
3438 return nullptr;
3439 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003440 if (!SoFar)
3441 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003442 }
3443 }
3444
3445 assert(SoFar != nullptr);
3446
Pavel Labathba825192018-10-16 14:29:14 +00003447 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003448 if (Base == nullptr)
3449 return nullptr;
3450 return make<QualifiedName>(SoFar, Base);
3451}
3452
3453// <abi-tags> ::= <abi-tag> [<abi-tags>]
3454// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003455template <typename Derived, typename Alloc>
3456Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003457 while (consumeIf('B')) {
3458 StringView SN = parseBareSourceName();
3459 if (SN.empty())
3460 return nullptr;
3461 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003462 if (!N)
3463 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003464 }
3465 return N;
3466}
3467
3468// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003469template <typename Alloc, typename Derived>
3470StringView
3471AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003472 const char *Tmp = First;
3473 if (AllowNegative)
3474 consumeIf('n');
3475 if (numLeft() == 0 || !std::isdigit(*First))
3476 return StringView();
3477 while (numLeft() != 0 && std::isdigit(*First))
3478 ++First;
3479 return StringView(Tmp, First);
3480}
3481
3482// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003483template <typename Alloc, typename Derived>
3484bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003485 *Out = 0;
3486 if (look() < '0' || look() > '9')
3487 return true;
3488 while (look() >= '0' && look() <= '9') {
3489 *Out *= 10;
3490 *Out += static_cast<size_t>(consume() - '0');
3491 }
3492 return false;
3493}
3494
Pavel Labathba825192018-10-16 14:29:14 +00003495template <typename Alloc, typename Derived>
3496StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003497 size_t Int = 0;
3498 if (parsePositiveInteger(&Int) || numLeft() < Int)
3499 return StringView();
3500 StringView R(First, First + Int);
3501 First += Int;
3502 return R;
3503}
3504
3505// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3506//
3507// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3508// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3509// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3510//
3511// <ref-qualifier> ::= R # & ref-qualifier
3512// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003513template <typename Derived, typename Alloc>
3514Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003515 Qualifiers CVQuals = parseCVQualifiers();
3516
3517 Node *ExceptionSpec = nullptr;
3518 if (consumeIf("Do")) {
3519 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003520 if (!ExceptionSpec)
3521 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003522 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003523 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003524 if (E == nullptr || !consumeIf('E'))
3525 return nullptr;
3526 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003527 if (!ExceptionSpec)
3528 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003529 } else if (consumeIf("Dw")) {
3530 size_t SpecsBegin = Names.size();
3531 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003532 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003533 if (T == nullptr)
3534 return nullptr;
3535 Names.push_back(T);
3536 }
3537 ExceptionSpec =
3538 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003539 if (!ExceptionSpec)
3540 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003541 }
3542
3543 consumeIf("Dx"); // transaction safe
3544
3545 if (!consumeIf('F'))
3546 return nullptr;
3547 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003548 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003549 if (ReturnType == nullptr)
3550 return nullptr;
3551
3552 FunctionRefQual ReferenceQualifier = FrefQualNone;
3553 size_t ParamsBegin = Names.size();
3554 while (true) {
3555 if (consumeIf('E'))
3556 break;
3557 if (consumeIf('v'))
3558 continue;
3559 if (consumeIf("RE")) {
3560 ReferenceQualifier = FrefQualLValue;
3561 break;
3562 }
3563 if (consumeIf("OE")) {
3564 ReferenceQualifier = FrefQualRValue;
3565 break;
3566 }
Pavel Labathba825192018-10-16 14:29:14 +00003567 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003568 if (T == nullptr)
3569 return nullptr;
3570 Names.push_back(T);
3571 }
3572
3573 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3574 return make<FunctionType>(ReturnType, Params, CVQuals,
3575 ReferenceQualifier, ExceptionSpec);
3576}
3577
3578// extension:
3579// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3580// ::= Dv [<dimension expression>] _ <element type>
3581// <extended element type> ::= <element type>
3582// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003583template <typename Derived, typename Alloc>
3584Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003585 if (!consumeIf("Dv"))
3586 return nullptr;
3587 if (look() >= '1' && look() <= '9') {
3588 StringView DimensionNumber = parseNumber();
3589 if (!consumeIf('_'))
3590 return nullptr;
3591 if (consumeIf('p'))
3592 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003593 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003594 if (ElemType == nullptr)
3595 return nullptr;
3596 return make<VectorType>(ElemType, DimensionNumber);
3597 }
3598
3599 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003600 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003601 if (!DimExpr)
3602 return nullptr;
3603 if (!consumeIf('_'))
3604 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003605 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003606 if (!ElemType)
3607 return nullptr;
3608 return make<VectorType>(ElemType, DimExpr);
3609 }
Pavel Labathba825192018-10-16 14:29:14 +00003610 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003611 if (!ElemType)
3612 return nullptr;
3613 return make<VectorType>(ElemType, StringView());
3614}
3615
3616// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3617// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003618template <typename Derived, typename Alloc>
3619Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003620 if (!consumeIf('D'))
3621 return nullptr;
3622 if (!consumeIf('t') && !consumeIf('T'))
3623 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003624 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003625 if (E == nullptr)
3626 return nullptr;
3627 if (!consumeIf('E'))
3628 return nullptr;
3629 return make<EnclosingExpr>("decltype(", E, ")");
3630}
3631
3632// <array-type> ::= A <positive dimension number> _ <element type>
3633// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003634template <typename Derived, typename Alloc>
3635Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003636 if (!consumeIf('A'))
3637 return nullptr;
3638
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003639 NodeOrString Dimension;
3640
Richard Smithc20d1442018-08-20 20:14:49 +00003641 if (std::isdigit(look())) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003642 Dimension = parseNumber();
Richard Smithc20d1442018-08-20 20:14:49 +00003643 if (!consumeIf('_'))
3644 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003645 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003646 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003647 if (DimExpr == nullptr)
3648 return nullptr;
3649 if (!consumeIf('_'))
3650 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003651 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003652 }
3653
Pavel Labathba825192018-10-16 14:29:14 +00003654 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003655 if (Ty == nullptr)
3656 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003657 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003658}
3659
3660// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003661template <typename Derived, typename Alloc>
3662Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003663 if (!consumeIf('M'))
3664 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003665 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003666 if (ClassType == nullptr)
3667 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003668 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003669 if (MemberType == nullptr)
3670 return nullptr;
3671 return make<PointerToMemberType>(ClassType, MemberType);
3672}
3673
3674// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3675// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3676// ::= Tu <name> # dependent elaborated type specifier using 'union'
3677// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003678template <typename Derived, typename Alloc>
3679Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003680 StringView ElabSpef;
3681 if (consumeIf("Ts"))
3682 ElabSpef = "struct";
3683 else if (consumeIf("Tu"))
3684 ElabSpef = "union";
3685 else if (consumeIf("Te"))
3686 ElabSpef = "enum";
3687
Pavel Labathba825192018-10-16 14:29:14 +00003688 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003689 if (Name == nullptr)
3690 return nullptr;
3691
3692 if (!ElabSpef.empty())
3693 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3694
3695 return Name;
3696}
3697
3698// <qualified-type> ::= <qualifiers> <type>
3699// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3700// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003701template <typename Derived, typename Alloc>
3702Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003703 if (consumeIf('U')) {
3704 StringView Qual = parseBareSourceName();
3705 if (Qual.empty())
3706 return nullptr;
3707
3708 // FIXME parse the optional <template-args> here!
3709
3710 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3711 if (Qual.startsWith("objcproto")) {
3712 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3713 StringView Proto;
3714 {
3715 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3716 SaveLast(Last, ProtoSourceName.end());
3717 Proto = parseBareSourceName();
3718 }
3719 if (Proto.empty())
3720 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003721 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003722 if (Child == nullptr)
3723 return nullptr;
3724 return make<ObjCProtoName>(Child, Proto);
3725 }
3726
Pavel Labathba825192018-10-16 14:29:14 +00003727 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003728 if (Child == nullptr)
3729 return nullptr;
3730 return make<VendorExtQualType>(Child, Qual);
3731 }
3732
3733 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003734 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003735 if (Ty == nullptr)
3736 return nullptr;
3737 if (Quals != QualNone)
3738 Ty = make<QualType>(Ty, Quals);
3739 return Ty;
3740}
3741
3742// <type> ::= <builtin-type>
3743// ::= <qualified-type>
3744// ::= <function-type>
3745// ::= <class-enum-type>
3746// ::= <array-type>
3747// ::= <pointer-to-member-type>
3748// ::= <template-param>
3749// ::= <template-template-param> <template-args>
3750// ::= <decltype>
3751// ::= P <type> # pointer
3752// ::= R <type> # l-value reference
3753// ::= O <type> # r-value reference (C++11)
3754// ::= C <type> # complex pair (C99)
3755// ::= G <type> # imaginary (C99)
3756// ::= <substitution> # See Compression below
3757// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3758// extension ::= <vector-type> # <vector-type> starts with Dv
3759//
3760// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3761// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003762template <typename Derived, typename Alloc>
3763Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003764 Node *Result = nullptr;
3765
Richard Smithc20d1442018-08-20 20:14:49 +00003766 switch (look()) {
3767 // ::= <qualified-type>
3768 case 'r':
3769 case 'V':
3770 case 'K': {
3771 unsigned AfterQuals = 0;
3772 if (look(AfterQuals) == 'r') ++AfterQuals;
3773 if (look(AfterQuals) == 'V') ++AfterQuals;
3774 if (look(AfterQuals) == 'K') ++AfterQuals;
3775
3776 if (look(AfterQuals) == 'F' ||
3777 (look(AfterQuals) == 'D' &&
3778 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3779 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003780 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003781 break;
3782 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003783 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003784 }
3785 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003786 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003787 break;
3788 }
3789 // <builtin-type> ::= v # void
3790 case 'v':
3791 ++First;
3792 return make<NameType>("void");
3793 // ::= w # wchar_t
3794 case 'w':
3795 ++First;
3796 return make<NameType>("wchar_t");
3797 // ::= b # bool
3798 case 'b':
3799 ++First;
3800 return make<NameType>("bool");
3801 // ::= c # char
3802 case 'c':
3803 ++First;
3804 return make<NameType>("char");
3805 // ::= a # signed char
3806 case 'a':
3807 ++First;
3808 return make<NameType>("signed char");
3809 // ::= h # unsigned char
3810 case 'h':
3811 ++First;
3812 return make<NameType>("unsigned char");
3813 // ::= s # short
3814 case 's':
3815 ++First;
3816 return make<NameType>("short");
3817 // ::= t # unsigned short
3818 case 't':
3819 ++First;
3820 return make<NameType>("unsigned short");
3821 // ::= i # int
3822 case 'i':
3823 ++First;
3824 return make<NameType>("int");
3825 // ::= j # unsigned int
3826 case 'j':
3827 ++First;
3828 return make<NameType>("unsigned int");
3829 // ::= l # long
3830 case 'l':
3831 ++First;
3832 return make<NameType>("long");
3833 // ::= m # unsigned long
3834 case 'm':
3835 ++First;
3836 return make<NameType>("unsigned long");
3837 // ::= x # long long, __int64
3838 case 'x':
3839 ++First;
3840 return make<NameType>("long long");
3841 // ::= y # unsigned long long, __int64
3842 case 'y':
3843 ++First;
3844 return make<NameType>("unsigned long long");
3845 // ::= n # __int128
3846 case 'n':
3847 ++First;
3848 return make<NameType>("__int128");
3849 // ::= o # unsigned __int128
3850 case 'o':
3851 ++First;
3852 return make<NameType>("unsigned __int128");
3853 // ::= f # float
3854 case 'f':
3855 ++First;
3856 return make<NameType>("float");
3857 // ::= d # double
3858 case 'd':
3859 ++First;
3860 return make<NameType>("double");
3861 // ::= e # long double, __float80
3862 case 'e':
3863 ++First;
3864 return make<NameType>("long double");
3865 // ::= g # __float128
3866 case 'g':
3867 ++First;
3868 return make<NameType>("__float128");
3869 // ::= z # ellipsis
3870 case 'z':
3871 ++First;
3872 return make<NameType>("...");
3873
3874 // <builtin-type> ::= u <source-name> # vendor extended type
3875 case 'u': {
3876 ++First;
3877 StringView Res = parseBareSourceName();
3878 if (Res.empty())
3879 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003880 // Typically, <builtin-type>s are not considered substitution candidates,
3881 // but the exception to that exception is vendor extended types (Itanium C++
3882 // ABI 5.9.1).
3883 Result = make<NameType>(Res);
3884 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003885 }
3886 case 'D':
3887 switch (look(1)) {
3888 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3889 case 'd':
3890 First += 2;
3891 return make<NameType>("decimal64");
3892 // ::= De # IEEE 754r decimal floating point (128 bits)
3893 case 'e':
3894 First += 2;
3895 return make<NameType>("decimal128");
3896 // ::= Df # IEEE 754r decimal floating point (32 bits)
3897 case 'f':
3898 First += 2;
3899 return make<NameType>("decimal32");
3900 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3901 case 'h':
3902 First += 2;
3903 return make<NameType>("decimal16");
3904 // ::= Di # char32_t
3905 case 'i':
3906 First += 2;
3907 return make<NameType>("char32_t");
3908 // ::= Ds # char16_t
3909 case 's':
3910 First += 2;
3911 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003912 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3913 case 'u':
3914 First += 2;
3915 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003916 // ::= Da # auto (in dependent new-expressions)
3917 case 'a':
3918 First += 2;
3919 return make<NameType>("auto");
3920 // ::= Dc # decltype(auto)
3921 case 'c':
3922 First += 2;
3923 return make<NameType>("decltype(auto)");
3924 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3925 case 'n':
3926 First += 2;
3927 return make<NameType>("std::nullptr_t");
3928
3929 // ::= <decltype>
3930 case 't':
3931 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003932 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003933 break;
3934 }
3935 // extension ::= <vector-type> # <vector-type> starts with Dv
3936 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003937 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003938 break;
3939 }
3940 // ::= Dp <type> # pack expansion (C++0x)
3941 case 'p': {
3942 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003943 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003944 if (!Child)
3945 return nullptr;
3946 Result = make<ParameterPackExpansion>(Child);
3947 break;
3948 }
3949 // Exception specifier on a function type.
3950 case 'o':
3951 case 'O':
3952 case 'w':
3953 // Transaction safe function type.
3954 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003955 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003956 break;
3957 }
3958 break;
3959 // ::= <function-type>
3960 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003961 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003962 break;
3963 }
3964 // ::= <array-type>
3965 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003966 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003967 break;
3968 }
3969 // ::= <pointer-to-member-type>
3970 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003971 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003972 break;
3973 }
3974 // ::= <template-param>
3975 case 'T': {
3976 // This could be an elaborate type specifier on a <class-enum-type>.
3977 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003978 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003979 break;
3980 }
3981
Pavel Labathba825192018-10-16 14:29:14 +00003982 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003983 if (Result == nullptr)
3984 return nullptr;
3985
3986 // Result could be either of:
3987 // <type> ::= <template-param>
3988 // <type> ::= <template-template-param> <template-args>
3989 //
3990 // <template-template-param> ::= <template-param>
3991 // ::= <substitution>
3992 //
3993 // If this is followed by some <template-args>, and we're permitted to
3994 // parse them, take the second production.
3995
3996 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003997 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003998 if (TA == nullptr)
3999 return nullptr;
4000 Result = make<NameWithTemplateArgs>(Result, TA);
4001 }
4002 break;
4003 }
4004 // ::= P <type> # pointer
4005 case 'P': {
4006 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004007 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004008 if (Ptr == nullptr)
4009 return nullptr;
4010 Result = make<PointerType>(Ptr);
4011 break;
4012 }
4013 // ::= R <type> # l-value reference
4014 case 'R': {
4015 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004016 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004017 if (Ref == nullptr)
4018 return nullptr;
4019 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
4020 break;
4021 }
4022 // ::= O <type> # r-value reference (C++11)
4023 case 'O': {
4024 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004025 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004026 if (Ref == nullptr)
4027 return nullptr;
4028 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
4029 break;
4030 }
4031 // ::= C <type> # complex pair (C99)
4032 case 'C': {
4033 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004034 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004035 if (P == nullptr)
4036 return nullptr;
4037 Result = make<PostfixQualifiedType>(P, " complex");
4038 break;
4039 }
4040 // ::= G <type> # imaginary (C99)
4041 case 'G': {
4042 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004043 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004044 if (P == nullptr)
4045 return P;
4046 Result = make<PostfixQualifiedType>(P, " imaginary");
4047 break;
4048 }
4049 // ::= <substitution> # See Compression below
4050 case 'S': {
4051 if (look(1) && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00004052 Node *Sub = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00004053 if (Sub == nullptr)
4054 return nullptr;
4055
4056 // Sub could be either of:
4057 // <type> ::= <substitution>
4058 // <type> ::= <template-template-param> <template-args>
4059 //
4060 // <template-template-param> ::= <template-param>
4061 // ::= <substitution>
4062 //
4063 // If this is followed by some <template-args>, and we're permitted to
4064 // parse them, take the second production.
4065
4066 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004067 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004068 if (TA == nullptr)
4069 return nullptr;
4070 Result = make<NameWithTemplateArgs>(Sub, TA);
4071 break;
4072 }
4073
4074 // If all we parsed was a substitution, don't re-insert into the
4075 // substitution table.
4076 return Sub;
4077 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00004078 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004079 }
4080 // ::= <class-enum-type>
4081 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004082 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004083 break;
4084 }
4085 }
4086
4087 // If we parsed a type, insert it into the substitution table. Note that all
4088 // <builtin-type>s and <substitution>s have already bailed out, because they
4089 // don't get substitutions.
4090 if (Result != nullptr)
4091 Subs.push_back(Result);
4092 return Result;
4093}
4094
Pavel Labathba825192018-10-16 14:29:14 +00004095template <typename Derived, typename Alloc>
4096Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4097 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004098 if (E == nullptr)
4099 return nullptr;
4100 return make<PrefixExpr>(Kind, E);
4101}
4102
Pavel Labathba825192018-10-16 14:29:14 +00004103template <typename Derived, typename Alloc>
4104Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4105 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004106 if (LHS == nullptr)
4107 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004108 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004109 if (RHS == nullptr)
4110 return nullptr;
4111 return make<BinaryExpr>(LHS, Kind, RHS);
4112}
4113
Pavel Labathba825192018-10-16 14:29:14 +00004114template <typename Derived, typename Alloc>
4115Node *
4116AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004117 StringView Tmp = parseNumber(true);
4118 if (!Tmp.empty() && consumeIf('E'))
4119 return make<IntegerLiteral>(Lit, Tmp);
4120 return nullptr;
4121}
4122
4123// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004124template <typename Alloc, typename Derived>
4125Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004126 Qualifiers CVR = QualNone;
4127 if (consumeIf('r'))
4128 CVR |= QualRestrict;
4129 if (consumeIf('V'))
4130 CVR |= QualVolatile;
4131 if (consumeIf('K'))
4132 CVR |= QualConst;
4133 return CVR;
4134}
4135
4136// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4137// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4138// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4139// ::= 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 +00004140template <typename Derived, typename Alloc>
4141Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00004142 if (consumeIf("fp")) {
4143 parseCVQualifiers();
4144 StringView Num = parseNumber();
4145 if (!consumeIf('_'))
4146 return nullptr;
4147 return make<FunctionParam>(Num);
4148 }
4149 if (consumeIf("fL")) {
4150 if (parseNumber().empty())
4151 return nullptr;
4152 if (!consumeIf('p'))
4153 return nullptr;
4154 parseCVQualifiers();
4155 StringView Num = parseNumber();
4156 if (!consumeIf('_'))
4157 return nullptr;
4158 return make<FunctionParam>(Num);
4159 }
4160 return nullptr;
4161}
4162
4163// [gs] nw <expression>* _ <type> E # new (expr-list) type
4164// [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4165// [gs] na <expression>* _ <type> E # new[] (expr-list) type
4166// [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4167// <initializer> ::= pi <expression>* E # parenthesized initialization
Pavel Labathba825192018-10-16 14:29:14 +00004168template <typename Derived, typename Alloc>
4169Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004170 bool Global = consumeIf("gs");
4171 bool IsArray = look(1) == 'a';
4172 if (!consumeIf("nw") && !consumeIf("na"))
4173 return nullptr;
4174 size_t Exprs = Names.size();
4175 while (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00004176 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004177 if (Ex == nullptr)
4178 return nullptr;
4179 Names.push_back(Ex);
4180 }
4181 NodeArray ExprList = popTrailingNodeArray(Exprs);
Pavel Labathba825192018-10-16 14:29:14 +00004182 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004183 if (Ty == nullptr)
4184 return Ty;
4185 if (consumeIf("pi")) {
4186 size_t InitsBegin = Names.size();
4187 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004188 Node *Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004189 if (Init == nullptr)
4190 return Init;
4191 Names.push_back(Init);
4192 }
4193 NodeArray Inits = popTrailingNodeArray(InitsBegin);
4194 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
4195 } else if (!consumeIf('E'))
4196 return nullptr;
4197 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
4198}
4199
4200// cv <type> <expression> # conversion with one argument
4201// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004202template <typename Derived, typename Alloc>
4203Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004204 if (!consumeIf("cv"))
4205 return nullptr;
4206 Node *Ty;
4207 {
4208 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004209 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004210 }
4211
4212 if (Ty == nullptr)
4213 return nullptr;
4214
4215 if (consumeIf('_')) {
4216 size_t ExprsBegin = Names.size();
4217 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004218 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004219 if (E == nullptr)
4220 return E;
4221 Names.push_back(E);
4222 }
4223 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4224 return make<ConversionExpr>(Ty, Exprs);
4225 }
4226
Pavel Labathba825192018-10-16 14:29:14 +00004227 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004228 if (E[0] == nullptr)
4229 return nullptr;
4230 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4231}
4232
4233// <expr-primary> ::= L <type> <value number> E # integer literal
4234// ::= L <type> <value float> E # floating literal
4235// ::= L <string type> E # string literal
4236// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004237// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004238// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4239// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004240template <typename Derived, typename Alloc>
4241Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004242 if (!consumeIf('L'))
4243 return nullptr;
4244 switch (look()) {
4245 case 'w':
4246 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004247 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004248 case 'b':
4249 if (consumeIf("b0E"))
4250 return make<BoolExpr>(0);
4251 if (consumeIf("b1E"))
4252 return make<BoolExpr>(1);
4253 return nullptr;
4254 case 'c':
4255 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004256 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004257 case 'a':
4258 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004259 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004260 case 'h':
4261 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004262 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004263 case 's':
4264 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004265 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004266 case 't':
4267 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004268 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004269 case 'i':
4270 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004271 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004272 case 'j':
4273 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004274 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004275 case 'l':
4276 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004277 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004278 case 'm':
4279 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004280 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004281 case 'x':
4282 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004283 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004284 case 'y':
4285 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004286 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004287 case 'n':
4288 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004289 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004290 case 'o':
4291 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004292 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004293 case 'f':
4294 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004295 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004296 case 'd':
4297 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004298 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004299 case 'e':
4300 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004301 return getDerived().template parseFloatingLiteral<long double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004302 case '_':
4303 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004304 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004305 if (R != nullptr && consumeIf('E'))
4306 return R;
4307 }
4308 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004309 case 'A': {
4310 Node *T = getDerived().parseType();
4311 if (T == nullptr)
4312 return nullptr;
4313 // FIXME: We need to include the string contents in the mangling.
4314 if (consumeIf('E'))
4315 return make<StringLiteral>(T);
4316 return nullptr;
4317 }
4318 case 'D':
4319 if (consumeIf("DnE"))
4320 return make<NameType>("nullptr");
4321 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004322 case 'T':
4323 // Invalid mangled name per
4324 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4325 return nullptr;
4326 default: {
4327 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004328 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004329 if (T == nullptr)
4330 return nullptr;
4331 StringView N = parseNumber();
4332 if (!N.empty()) {
4333 if (!consumeIf('E'))
4334 return nullptr;
4335 return make<IntegerCastExpr>(T, N);
4336 }
4337 if (consumeIf('E'))
Richard Smithdf1c14c2019-09-06 23:53:21 +00004338 return make<LambdaExpr>(T);
Richard Smithc20d1442018-08-20 20:14:49 +00004339 return nullptr;
4340 }
4341 }
4342}
4343
4344// <braced-expression> ::= <expression>
4345// ::= di <field source-name> <braced-expression> # .name = expr
4346// ::= dx <index expression> <braced-expression> # [expr] = expr
4347// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004348template <typename Derived, typename Alloc>
4349Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004350 if (look() == 'd') {
4351 switch (look(1)) {
4352 case 'i': {
4353 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004354 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004355 if (Field == nullptr)
4356 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004357 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004358 if (Init == nullptr)
4359 return nullptr;
4360 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4361 }
4362 case 'x': {
4363 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004364 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004365 if (Index == nullptr)
4366 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004367 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004368 if (Init == nullptr)
4369 return nullptr;
4370 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4371 }
4372 case 'X': {
4373 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004374 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004375 if (RangeBegin == nullptr)
4376 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004377 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004378 if (RangeEnd == nullptr)
4379 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004380 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004381 if (Init == nullptr)
4382 return nullptr;
4383 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4384 }
4385 }
4386 }
Pavel Labathba825192018-10-16 14:29:14 +00004387 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004388}
4389
4390// (not yet in the spec)
4391// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4392// ::= fR <binary-operator-name> <expression> <expression>
4393// ::= fl <binary-operator-name> <expression>
4394// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004395template <typename Derived, typename Alloc>
4396Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004397 if (!consumeIf('f'))
4398 return nullptr;
4399
4400 char FoldKind = look();
4401 bool IsLeftFold, HasInitializer;
4402 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4403 if (FoldKind == 'l' || FoldKind == 'L')
4404 IsLeftFold = true;
4405 else if (FoldKind == 'r' || FoldKind == 'R')
4406 IsLeftFold = false;
4407 else
4408 return nullptr;
4409 ++First;
4410
4411 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4412 StringView OperatorName;
4413 if (consumeIf("aa")) OperatorName = "&&";
4414 else if (consumeIf("an")) OperatorName = "&";
4415 else if (consumeIf("aN")) OperatorName = "&=";
4416 else if (consumeIf("aS")) OperatorName = "=";
4417 else if (consumeIf("cm")) OperatorName = ",";
4418 else if (consumeIf("ds")) OperatorName = ".*";
4419 else if (consumeIf("dv")) OperatorName = "/";
4420 else if (consumeIf("dV")) OperatorName = "/=";
4421 else if (consumeIf("eo")) OperatorName = "^";
4422 else if (consumeIf("eO")) OperatorName = "^=";
4423 else if (consumeIf("eq")) OperatorName = "==";
4424 else if (consumeIf("ge")) OperatorName = ">=";
4425 else if (consumeIf("gt")) OperatorName = ">";
4426 else if (consumeIf("le")) OperatorName = "<=";
4427 else if (consumeIf("ls")) OperatorName = "<<";
4428 else if (consumeIf("lS")) OperatorName = "<<=";
4429 else if (consumeIf("lt")) OperatorName = "<";
4430 else if (consumeIf("mi")) OperatorName = "-";
4431 else if (consumeIf("mI")) OperatorName = "-=";
4432 else if (consumeIf("ml")) OperatorName = "*";
4433 else if (consumeIf("mL")) OperatorName = "*=";
4434 else if (consumeIf("ne")) OperatorName = "!=";
4435 else if (consumeIf("oo")) OperatorName = "||";
4436 else if (consumeIf("or")) OperatorName = "|";
4437 else if (consumeIf("oR")) OperatorName = "|=";
4438 else if (consumeIf("pl")) OperatorName = "+";
4439 else if (consumeIf("pL")) OperatorName = "+=";
4440 else if (consumeIf("rm")) OperatorName = "%";
4441 else if (consumeIf("rM")) OperatorName = "%=";
4442 else if (consumeIf("rs")) OperatorName = ">>";
4443 else if (consumeIf("rS")) OperatorName = ">>=";
4444 else return nullptr;
4445
Pavel Labathba825192018-10-16 14:29:14 +00004446 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004447 if (Pack == nullptr)
4448 return nullptr;
4449 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004450 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004451 if (Init == nullptr)
4452 return nullptr;
4453 }
4454
4455 if (IsLeftFold && Init)
4456 std::swap(Pack, Init);
4457
4458 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4459}
4460
4461// <expression> ::= <unary operator-name> <expression>
4462// ::= <binary operator-name> <expression> <expression>
4463// ::= <ternary operator-name> <expression> <expression> <expression>
4464// ::= cl <expression>+ E # call
4465// ::= cv <type> <expression> # conversion with one argument
4466// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4467// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4468// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4469// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4470// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4471// ::= [gs] dl <expression> # delete expression
4472// ::= [gs] da <expression> # delete[] expression
4473// ::= pp_ <expression> # prefix ++
4474// ::= mm_ <expression> # prefix --
4475// ::= ti <type> # typeid (type)
4476// ::= te <expression> # typeid (expression)
4477// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4478// ::= sc <type> <expression> # static_cast<type> (expression)
4479// ::= cc <type> <expression> # const_cast<type> (expression)
4480// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4481// ::= st <type> # sizeof (a type)
4482// ::= sz <expression> # sizeof (an expression)
4483// ::= at <type> # alignof (a type)
4484// ::= az <expression> # alignof (an expression)
4485// ::= nx <expression> # noexcept (expression)
4486// ::= <template-param>
4487// ::= <function-param>
4488// ::= dt <expression> <unresolved-name> # expr.name
4489// ::= pt <expression> <unresolved-name> # expr->name
4490// ::= ds <expression> <expression> # expr.*expr
4491// ::= sZ <template-param> # size of a parameter pack
4492// ::= sZ <function-param> # size of a function parameter pack
4493// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4494// ::= sp <expression> # pack expansion
4495// ::= tw <expression> # throw expression
4496// ::= tr # throw with no operand (rethrow)
4497// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4498// # freestanding dependent name (e.g., T::x),
4499// # objectless nonstatic member reference
4500// ::= fL <binary-operator-name> <expression> <expression>
4501// ::= fR <binary-operator-name> <expression> <expression>
4502// ::= fl <binary-operator-name> <expression>
4503// ::= fr <binary-operator-name> <expression>
4504// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004505template <typename Derived, typename Alloc>
4506Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004507 bool Global = consumeIf("gs");
4508 if (numLeft() < 2)
4509 return nullptr;
4510
4511 switch (*First) {
4512 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004513 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004514 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004515 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004516 case 'f': {
4517 // Disambiguate a fold expression from a <function-param>.
4518 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004519 return getDerived().parseFunctionParam();
4520 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004521 }
4522 case 'a':
4523 switch (First[1]) {
4524 case 'a':
4525 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004526 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004527 case 'd':
4528 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004529 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004530 case 'n':
4531 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004532 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004533 case 'N':
4534 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004535 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004536 case 'S':
4537 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004538 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004539 case 't': {
4540 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004541 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004542 if (Ty == nullptr)
4543 return nullptr;
4544 return make<EnclosingExpr>("alignof (", Ty, ")");
4545 }
4546 case 'z': {
4547 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004548 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004549 if (Ty == nullptr)
4550 return nullptr;
4551 return make<EnclosingExpr>("alignof (", Ty, ")");
4552 }
4553 }
4554 return nullptr;
4555 case 'c':
4556 switch (First[1]) {
4557 // cc <type> <expression> # const_cast<type>(expression)
4558 case 'c': {
4559 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004560 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004561 if (Ty == nullptr)
4562 return Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004563 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004564 if (Ex == nullptr)
4565 return Ex;
4566 return make<CastExpr>("const_cast", Ty, Ex);
4567 }
4568 // cl <expression>+ E # call
4569 case 'l': {
4570 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004571 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004572 if (Callee == nullptr)
4573 return Callee;
4574 size_t ExprsBegin = Names.size();
4575 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004576 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004577 if (E == nullptr)
4578 return E;
4579 Names.push_back(E);
4580 }
4581 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4582 }
4583 case 'm':
4584 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004585 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004586 case 'o':
4587 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004588 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004589 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004590 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004591 }
4592 return nullptr;
4593 case 'd':
4594 switch (First[1]) {
4595 case 'a': {
4596 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004597 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004598 if (Ex == nullptr)
4599 return Ex;
4600 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4601 }
4602 case 'c': {
4603 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004604 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004605 if (T == nullptr)
4606 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004607 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004608 if (Ex == nullptr)
4609 return Ex;
4610 return make<CastExpr>("dynamic_cast", T, Ex);
4611 }
4612 case 'e':
4613 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004614 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004615 case 'l': {
4616 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004617 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004618 if (E == nullptr)
4619 return E;
4620 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4621 }
4622 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004623 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004624 case 's': {
4625 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004626 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004627 if (LHS == nullptr)
4628 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004629 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004630 if (RHS == nullptr)
4631 return nullptr;
4632 return make<MemberExpr>(LHS, ".*", RHS);
4633 }
4634 case 't': {
4635 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004636 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004637 if (LHS == nullptr)
4638 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004639 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004640 if (RHS == nullptr)
4641 return nullptr;
4642 return make<MemberExpr>(LHS, ".", RHS);
4643 }
4644 case 'v':
4645 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004646 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004647 case 'V':
4648 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004649 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004650 }
4651 return nullptr;
4652 case 'e':
4653 switch (First[1]) {
4654 case 'o':
4655 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004656 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004657 case 'O':
4658 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004659 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004660 case 'q':
4661 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004662 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004663 }
4664 return nullptr;
4665 case 'g':
4666 switch (First[1]) {
4667 case 'e':
4668 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004669 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004670 case 't':
4671 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004672 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004673 }
4674 return nullptr;
4675 case 'i':
4676 switch (First[1]) {
4677 case 'x': {
4678 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004679 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004680 if (Base == nullptr)
4681 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004682 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004683 if (Index == nullptr)
4684 return Index;
4685 return make<ArraySubscriptExpr>(Base, Index);
4686 }
4687 case 'l': {
4688 First += 2;
4689 size_t InitsBegin = Names.size();
4690 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004691 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004692 if (E == nullptr)
4693 return nullptr;
4694 Names.push_back(E);
4695 }
4696 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4697 }
4698 }
4699 return nullptr;
4700 case 'l':
4701 switch (First[1]) {
4702 case 'e':
4703 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004704 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004705 case 's':
4706 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004707 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004708 case 'S':
4709 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004710 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004711 case 't':
4712 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004713 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004714 }
4715 return nullptr;
4716 case 'm':
4717 switch (First[1]) {
4718 case 'i':
4719 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004720 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004721 case 'I':
4722 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004723 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004724 case 'l':
4725 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004726 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004727 case 'L':
4728 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004729 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004730 case 'm':
4731 First += 2;
4732 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004733 return getDerived().parsePrefixExpr("--");
4734 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004735 if (Ex == nullptr)
4736 return nullptr;
4737 return make<PostfixExpr>(Ex, "--");
4738 }
4739 return nullptr;
4740 case 'n':
4741 switch (First[1]) {
4742 case 'a':
4743 case 'w':
Pavel Labathba825192018-10-16 14:29:14 +00004744 return getDerived().parseNewExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004745 case 'e':
4746 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004747 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004748 case 'g':
4749 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004750 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004751 case 't':
4752 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004753 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004754 case 'x':
4755 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004756 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004757 if (Ex == nullptr)
4758 return Ex;
4759 return make<EnclosingExpr>("noexcept (", Ex, ")");
4760 }
4761 return nullptr;
4762 case 'o':
4763 switch (First[1]) {
4764 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004765 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004766 case 'o':
4767 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004768 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004769 case 'r':
4770 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004771 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004772 case 'R':
4773 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004774 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004775 }
4776 return nullptr;
4777 case 'p':
4778 switch (First[1]) {
4779 case 'm':
4780 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004781 return getDerived().parseBinaryExpr("->*");
Richard Smithc20d1442018-08-20 20:14:49 +00004782 case 'l':
4783 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004784 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004785 case 'L':
4786 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004787 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004788 case 'p': {
4789 First += 2;
4790 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004791 return getDerived().parsePrefixExpr("++");
4792 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004793 if (Ex == nullptr)
4794 return Ex;
4795 return make<PostfixExpr>(Ex, "++");
4796 }
4797 case 's':
4798 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004799 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004800 case 't': {
4801 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004802 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004803 if (L == nullptr)
4804 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004805 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004806 if (R == nullptr)
4807 return nullptr;
4808 return make<MemberExpr>(L, "->", R);
4809 }
4810 }
4811 return nullptr;
4812 case 'q':
4813 if (First[1] == 'u') {
4814 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004815 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004816 if (Cond == nullptr)
4817 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004818 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004819 if (LHS == nullptr)
4820 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004821 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004822 if (RHS == nullptr)
4823 return nullptr;
4824 return make<ConditionalExpr>(Cond, LHS, RHS);
4825 }
4826 return nullptr;
4827 case 'r':
4828 switch (First[1]) {
4829 case 'c': {
4830 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004831 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004832 if (T == nullptr)
4833 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004834 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004835 if (Ex == nullptr)
4836 return Ex;
4837 return make<CastExpr>("reinterpret_cast", T, Ex);
4838 }
4839 case 'm':
4840 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004841 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004842 case 'M':
4843 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004844 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004845 case 's':
4846 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004847 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004848 case 'S':
4849 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004850 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004851 }
4852 return nullptr;
4853 case 's':
4854 switch (First[1]) {
4855 case 'c': {
4856 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004857 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004858 if (T == nullptr)
4859 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004860 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004861 if (Ex == nullptr)
4862 return Ex;
4863 return make<CastExpr>("static_cast", T, Ex);
4864 }
4865 case 'p': {
4866 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004867 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004868 if (Child == nullptr)
4869 return nullptr;
4870 return make<ParameterPackExpansion>(Child);
4871 }
4872 case 'r':
Pavel Labathba825192018-10-16 14:29:14 +00004873 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004874 case 't': {
4875 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004876 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004877 if (Ty == nullptr)
4878 return Ty;
4879 return make<EnclosingExpr>("sizeof (", Ty, ")");
4880 }
4881 case 'z': {
4882 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004883 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004884 if (Ex == nullptr)
4885 return Ex;
4886 return make<EnclosingExpr>("sizeof (", Ex, ")");
4887 }
4888 case 'Z':
4889 First += 2;
4890 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004891 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004892 if (R == nullptr)
4893 return nullptr;
4894 return make<SizeofParamPackExpr>(R);
4895 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004896 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004897 if (FP == nullptr)
4898 return nullptr;
4899 return make<EnclosingExpr>("sizeof... (", FP, ")");
4900 }
4901 return nullptr;
4902 case 'P': {
4903 First += 2;
4904 size_t ArgsBegin = Names.size();
4905 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004906 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004907 if (Arg == nullptr)
4908 return nullptr;
4909 Names.push_back(Arg);
4910 }
Richard Smithb485b352018-08-24 23:30:26 +00004911 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4912 if (!Pack)
4913 return nullptr;
4914 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00004915 }
4916 }
4917 return nullptr;
4918 case 't':
4919 switch (First[1]) {
4920 case 'e': {
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 Ex;
4925 return make<EnclosingExpr>("typeid (", Ex, ")");
4926 }
4927 case 'i': {
4928 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004929 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004930 if (Ty == nullptr)
4931 return Ty;
4932 return make<EnclosingExpr>("typeid (", Ty, ")");
4933 }
4934 case 'l': {
4935 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004936 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004937 if (Ty == nullptr)
4938 return nullptr;
4939 size_t InitsBegin = Names.size();
4940 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004941 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004942 if (E == nullptr)
4943 return nullptr;
4944 Names.push_back(E);
4945 }
4946 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
4947 }
4948 case 'r':
4949 First += 2;
4950 return make<NameType>("throw");
4951 case 'w': {
4952 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004953 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004954 if (Ex == nullptr)
4955 return nullptr;
4956 return make<ThrowExpr>(Ex);
4957 }
4958 }
4959 return nullptr;
4960 case '1':
4961 case '2':
4962 case '3':
4963 case '4':
4964 case '5':
4965 case '6':
4966 case '7':
4967 case '8':
4968 case '9':
Pavel Labathba825192018-10-16 14:29:14 +00004969 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004970 }
Erik Pilkingtone8457c62019-06-18 23:34:09 +00004971
4972 if (consumeIf("u8__uuidoft")) {
4973 Node *Ty = getDerived().parseType();
4974 if (!Ty)
4975 return nullptr;
4976 return make<UUIDOfExpr>(Ty);
4977 }
4978
4979 if (consumeIf("u8__uuidofz")) {
4980 Node *Ex = getDerived().parseExpr();
4981 if (!Ex)
4982 return nullptr;
4983 return make<UUIDOfExpr>(Ex);
4984 }
4985
Richard Smithc20d1442018-08-20 20:14:49 +00004986 return nullptr;
4987}
4988
4989// <call-offset> ::= h <nv-offset> _
4990// ::= v <v-offset> _
4991//
4992// <nv-offset> ::= <offset number>
4993// # non-virtual base override
4994//
4995// <v-offset> ::= <offset number> _ <virtual offset number>
4996// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00004997template <typename Alloc, typename Derived>
4998bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00004999 // Just scan through the call offset, we never add this information into the
5000 // output.
5001 if (consumeIf('h'))
5002 return parseNumber(true).empty() || !consumeIf('_');
5003 if (consumeIf('v'))
5004 return parseNumber(true).empty() || !consumeIf('_') ||
5005 parseNumber(true).empty() || !consumeIf('_');
5006 return true;
5007}
5008
5009// <special-name> ::= TV <type> # virtual table
5010// ::= TT <type> # VTT structure (construction vtable index)
5011// ::= TI <type> # typeinfo structure
5012// ::= TS <type> # typeinfo name (null-terminated byte string)
5013// ::= Tc <call-offset> <call-offset> <base encoding>
5014// # base is the nominal target function of thunk
5015// # first call-offset is 'this' adjustment
5016// # second call-offset is result adjustment
5017// ::= T <call-offset> <base encoding>
5018// # base is the nominal target function of thunk
5019// ::= GV <object name> # Guard variable for one-time initialization
5020// # No <type>
5021// ::= TW <object name> # Thread-local wrapper
5022// ::= TH <object name> # Thread-local initialization
5023// ::= GR <object name> _ # First temporary
5024// ::= GR <object name> <seq-id> _ # Subsequent temporaries
5025// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
5026// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00005027template <typename Derived, typename Alloc>
5028Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00005029 switch (look()) {
5030 case 'T':
5031 switch (look(1)) {
5032 // TV <type> # virtual table
5033 case 'V': {
5034 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005035 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005036 if (Ty == nullptr)
5037 return nullptr;
5038 return make<SpecialName>("vtable for ", Ty);
5039 }
5040 // TT <type> # VTT structure (construction vtable index)
5041 case 'T': {
5042 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005043 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005044 if (Ty == nullptr)
5045 return nullptr;
5046 return make<SpecialName>("VTT for ", Ty);
5047 }
5048 // TI <type> # typeinfo structure
5049 case 'I': {
5050 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005051 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005052 if (Ty == nullptr)
5053 return nullptr;
5054 return make<SpecialName>("typeinfo for ", Ty);
5055 }
5056 // TS <type> # typeinfo name (null-terminated byte string)
5057 case 'S': {
5058 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005059 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005060 if (Ty == nullptr)
5061 return nullptr;
5062 return make<SpecialName>("typeinfo name for ", Ty);
5063 }
5064 // Tc <call-offset> <call-offset> <base encoding>
5065 case 'c': {
5066 First += 2;
5067 if (parseCallOffset() || parseCallOffset())
5068 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005069 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005070 if (Encoding == nullptr)
5071 return nullptr;
5072 return make<SpecialName>("covariant return thunk to ", Encoding);
5073 }
5074 // extension ::= TC <first type> <number> _ <second type>
5075 // # construction vtable for second-in-first
5076 case 'C': {
5077 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005078 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005079 if (FirstType == nullptr)
5080 return nullptr;
5081 if (parseNumber(true).empty() || !consumeIf('_'))
5082 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005083 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005084 if (SecondType == nullptr)
5085 return nullptr;
5086 return make<CtorVtableSpecialName>(SecondType, FirstType);
5087 }
5088 // TW <object name> # Thread-local wrapper
5089 case 'W': {
5090 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005091 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005092 if (Name == nullptr)
5093 return nullptr;
5094 return make<SpecialName>("thread-local wrapper routine for ", Name);
5095 }
5096 // TH <object name> # Thread-local initialization
5097 case 'H': {
5098 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005099 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005100 if (Name == nullptr)
5101 return nullptr;
5102 return make<SpecialName>("thread-local initialization routine for ", Name);
5103 }
5104 // T <call-offset> <base encoding>
5105 default: {
5106 ++First;
5107 bool IsVirt = look() == 'v';
5108 if (parseCallOffset())
5109 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005110 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005111 if (BaseEncoding == nullptr)
5112 return nullptr;
5113 if (IsVirt)
5114 return make<SpecialName>("virtual thunk to ", BaseEncoding);
5115 else
5116 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
5117 }
5118 }
5119 case 'G':
5120 switch (look(1)) {
5121 // GV <object name> # Guard variable for one-time initialization
5122 case 'V': {
5123 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005124 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005125 if (Name == nullptr)
5126 return nullptr;
5127 return make<SpecialName>("guard variable for ", Name);
5128 }
5129 // GR <object name> # reference temporary for object
5130 // GR <object name> _ # First temporary
5131 // GR <object name> <seq-id> _ # Subsequent temporaries
5132 case 'R': {
5133 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005134 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005135 if (Name == nullptr)
5136 return nullptr;
5137 size_t Count;
5138 bool ParsedSeqId = !parseSeqId(&Count);
5139 if (!consumeIf('_') && ParsedSeqId)
5140 return nullptr;
5141 return make<SpecialName>("reference temporary for ", Name);
5142 }
5143 }
5144 }
5145 return nullptr;
5146}
5147
5148// <encoding> ::= <function name> <bare-function-type>
5149// ::= <data name>
5150// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00005151template <typename Derived, typename Alloc>
5152Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithc20d1442018-08-20 20:14:49 +00005153 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00005154 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00005155
5156 auto IsEndOfEncoding = [&] {
5157 // The set of chars that can potentially follow an <encoding> (none of which
5158 // can start a <type>). Enumerating these allows us to avoid speculative
5159 // parsing.
5160 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5161 };
5162
5163 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00005164 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00005165 if (Name == nullptr)
5166 return nullptr;
5167
5168 if (resolveForwardTemplateRefs(NameInfo))
5169 return nullptr;
5170
5171 if (IsEndOfEncoding())
5172 return Name;
5173
5174 Node *Attrs = nullptr;
5175 if (consumeIf("Ua9enable_ifI")) {
5176 size_t BeforeArgs = Names.size();
5177 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005178 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005179 if (Arg == nullptr)
5180 return nullptr;
5181 Names.push_back(Arg);
5182 }
5183 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00005184 if (!Attrs)
5185 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005186 }
5187
5188 Node *ReturnType = nullptr;
5189 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00005190 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005191 if (ReturnType == nullptr)
5192 return nullptr;
5193 }
5194
5195 if (consumeIf('v'))
5196 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5197 Attrs, NameInfo.CVQualifiers,
5198 NameInfo.ReferenceQualifier);
5199
5200 size_t ParamsBegin = Names.size();
5201 do {
Pavel Labathba825192018-10-16 14:29:14 +00005202 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005203 if (Ty == nullptr)
5204 return nullptr;
5205 Names.push_back(Ty);
5206 } while (!IsEndOfEncoding());
5207
5208 return make<FunctionEncoding>(ReturnType, Name,
5209 popTrailingNodeArray(ParamsBegin),
5210 Attrs, NameInfo.CVQualifiers,
5211 NameInfo.ReferenceQualifier);
5212}
5213
5214template <class Float>
5215struct FloatData;
5216
5217template <>
5218struct FloatData<float>
5219{
5220 static const size_t mangled_size = 8;
5221 static const size_t max_demangled_size = 24;
5222 static constexpr const char* spec = "%af";
5223};
5224
5225template <>
5226struct FloatData<double>
5227{
5228 static const size_t mangled_size = 16;
5229 static const size_t max_demangled_size = 32;
5230 static constexpr const char* spec = "%a";
5231};
5232
5233template <>
5234struct FloatData<long double>
5235{
5236#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5237 defined(__wasm__)
5238 static const size_t mangled_size = 32;
5239#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5240 static const size_t mangled_size = 16;
5241#else
5242 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5243#endif
5244 static const size_t max_demangled_size = 40;
5245 static constexpr const char *spec = "%LaL";
5246};
5247
Pavel Labathba825192018-10-16 14:29:14 +00005248template <typename Alloc, typename Derived>
5249template <class Float>
5250Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005251 const size_t N = FloatData<Float>::mangled_size;
5252 if (numLeft() <= N)
5253 return nullptr;
5254 StringView Data(First, First + N);
5255 for (char C : Data)
5256 if (!std::isxdigit(C))
5257 return nullptr;
5258 First += N;
5259 if (!consumeIf('E'))
5260 return nullptr;
5261 return make<FloatLiteralImpl<Float>>(Data);
5262}
5263
5264// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005265template <typename Alloc, typename Derived>
5266bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005267 if (!(look() >= '0' && look() <= '9') &&
5268 !(look() >= 'A' && look() <= 'Z'))
5269 return true;
5270
5271 size_t Id = 0;
5272 while (true) {
5273 if (look() >= '0' && look() <= '9') {
5274 Id *= 36;
5275 Id += static_cast<size_t>(look() - '0');
5276 } else if (look() >= 'A' && look() <= 'Z') {
5277 Id *= 36;
5278 Id += static_cast<size_t>(look() - 'A') + 10;
5279 } else {
5280 *Out = Id;
5281 return false;
5282 }
5283 ++First;
5284 }
5285}
5286
5287// <substitution> ::= S <seq-id> _
5288// ::= S_
5289// <substitution> ::= Sa # ::std::allocator
5290// <substitution> ::= Sb # ::std::basic_string
5291// <substitution> ::= Ss # ::std::basic_string < char,
5292// ::std::char_traits<char>,
5293// ::std::allocator<char> >
5294// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5295// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5296// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Pavel Labathba825192018-10-16 14:29:14 +00005297template <typename Derived, typename Alloc>
5298Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005299 if (!consumeIf('S'))
5300 return nullptr;
5301
5302 if (std::islower(look())) {
5303 Node *SpecialSub;
5304 switch (look()) {
5305 case 'a':
5306 ++First;
5307 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::allocator);
5308 break;
5309 case 'b':
5310 ++First;
5311 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::basic_string);
5312 break;
5313 case 's':
5314 ++First;
5315 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::string);
5316 break;
5317 case 'i':
5318 ++First;
5319 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::istream);
5320 break;
5321 case 'o':
5322 ++First;
5323 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::ostream);
5324 break;
5325 case 'd':
5326 ++First;
5327 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::iostream);
5328 break;
5329 default:
5330 return nullptr;
5331 }
Richard Smithb485b352018-08-24 23:30:26 +00005332 if (!SpecialSub)
5333 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005334 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5335 // has ABI tags, the tags are appended to the substitution; the result is a
5336 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005337 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005338 if (WithTags != SpecialSub) {
5339 Subs.push_back(WithTags);
5340 SpecialSub = WithTags;
5341 }
5342 return SpecialSub;
5343 }
5344
5345 // ::= S_
5346 if (consumeIf('_')) {
5347 if (Subs.empty())
5348 return nullptr;
5349 return Subs[0];
5350 }
5351
5352 // ::= S <seq-id> _
5353 size_t Index = 0;
5354 if (parseSeqId(&Index))
5355 return nullptr;
5356 ++Index;
5357 if (!consumeIf('_') || Index >= Subs.size())
5358 return nullptr;
5359 return Subs[Index];
5360}
5361
5362// <template-param> ::= T_ # first template parameter
5363// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005364// ::= TL <level-1> __
5365// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005366template <typename Derived, typename Alloc>
5367Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005368 if (!consumeIf('T'))
5369 return nullptr;
5370
Richard Smithdf1c14c2019-09-06 23:53:21 +00005371 size_t Level = 0;
5372 if (consumeIf('L')) {
5373 if (parsePositiveInteger(&Level))
5374 return nullptr;
5375 ++Level;
5376 if (!consumeIf('_'))
5377 return nullptr;
5378 }
5379
Richard Smithc20d1442018-08-20 20:14:49 +00005380 size_t Index = 0;
5381 if (!consumeIf('_')) {
5382 if (parsePositiveInteger(&Index))
5383 return nullptr;
5384 ++Index;
5385 if (!consumeIf('_'))
5386 return nullptr;
5387 }
5388
Richard Smithc20d1442018-08-20 20:14:49 +00005389 // If we're in a context where this <template-param> refers to a
5390 // <template-arg> further ahead in the mangled name (currently just conversion
5391 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005392 // This can only happen at the outermost level.
5393 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005394 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5395 if (!ForwardRef)
5396 return nullptr;
5397 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5398 ForwardTemplateRefs.push_back(
5399 static_cast<ForwardTemplateReference *>(ForwardRef));
5400 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005401 }
5402
Richard Smithdf1c14c2019-09-06 23:53:21 +00005403 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5404 Index >= TemplateParams[Level]->size()) {
5405 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5406 // list are mangled as the corresponding artificial template type parameter.
5407 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5408 // This will be popped by the ScopedTemplateParamList in
5409 // parseUnnamedTypeName.
5410 if (Level == TemplateParams.size())
5411 TemplateParams.push_back(nullptr);
5412 return make<NameType>("auto");
5413 }
5414
Richard Smithc20d1442018-08-20 20:14:49 +00005415 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005416 }
5417
5418 return (*TemplateParams[Level])[Index];
5419}
5420
5421// <template-param-decl> ::= Ty # type parameter
5422// ::= Tn <type> # non-type parameter
5423// ::= Tt <template-param-decl>* E # template parameter
5424// ::= Tp <template-param-decl> # parameter pack
5425template <typename Derived, typename Alloc>
5426Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5427 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5428 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5429 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5430 if (N) TemplateParams.back()->push_back(N);
5431 return N;
5432 };
5433
5434 if (consumeIf("Ty")) {
5435 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5436 if (!Name)
5437 return nullptr;
5438 return make<TypeTemplateParamDecl>(Name);
5439 }
5440
5441 if (consumeIf("Tn")) {
5442 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5443 if (!Name)
5444 return nullptr;
5445 Node *Type = parseType();
5446 if (!Type)
5447 return nullptr;
5448 return make<NonTypeTemplateParamDecl>(Name, Type);
5449 }
5450
5451 if (consumeIf("Tt")) {
5452 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5453 if (!Name)
5454 return nullptr;
5455 size_t ParamsBegin = Names.size();
5456 ScopedTemplateParamList TemplateTemplateParamParams(this);
5457 while (!consumeIf("E")) {
5458 Node *P = parseTemplateParamDecl();
5459 if (!P)
5460 return nullptr;
5461 Names.push_back(P);
5462 }
5463 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5464 return make<TemplateTemplateParamDecl>(Name, Params);
5465 }
5466
5467 if (consumeIf("Tp")) {
5468 Node *P = parseTemplateParamDecl();
5469 if (!P)
5470 return nullptr;
5471 return make<TemplateParamPackDecl>(P);
5472 }
5473
5474 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005475}
5476
5477// <template-arg> ::= <type> # type or template
5478// ::= X <expression> E # expression
5479// ::= <expr-primary> # simple expressions
5480// ::= J <template-arg>* E # argument pack
5481// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005482template <typename Derived, typename Alloc>
5483Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005484 switch (look()) {
5485 case 'X': {
5486 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005487 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005488 if (Arg == nullptr || !consumeIf('E'))
5489 return nullptr;
5490 return Arg;
5491 }
5492 case 'J': {
5493 ++First;
5494 size_t ArgsBegin = Names.size();
5495 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005496 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005497 if (Arg == nullptr)
5498 return nullptr;
5499 Names.push_back(Arg);
5500 }
5501 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5502 return make<TemplateArgumentPack>(Args);
5503 }
5504 case 'L': {
5505 // ::= LZ <encoding> E # extension
5506 if (look(1) == 'Z') {
5507 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005508 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005509 if (Arg == nullptr || !consumeIf('E'))
5510 return nullptr;
5511 return Arg;
5512 }
5513 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005514 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005515 }
5516 default:
Pavel Labathba825192018-10-16 14:29:14 +00005517 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005518 }
5519}
5520
5521// <template-args> ::= I <template-arg>* E
5522// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005523template <typename Derived, typename Alloc>
5524Node *
5525AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005526 if (!consumeIf('I'))
5527 return nullptr;
5528
5529 // <template-params> refer to the innermost <template-args>. Clear out any
5530 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005531 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005532 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005533 TemplateParams.push_back(&OuterTemplateParams);
5534 OuterTemplateParams.clear();
5535 }
Richard Smithc20d1442018-08-20 20:14:49 +00005536
5537 size_t ArgsBegin = Names.size();
5538 while (!consumeIf('E')) {
5539 if (TagTemplates) {
5540 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005541 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005542 TemplateParams = std::move(OldParams);
5543 if (Arg == nullptr)
5544 return nullptr;
5545 Names.push_back(Arg);
5546 Node *TableEntry = Arg;
5547 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5548 TableEntry = make<ParameterPack>(
5549 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005550 if (!TableEntry)
5551 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005552 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005553 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005554 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005555 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005556 if (Arg == nullptr)
5557 return nullptr;
5558 Names.push_back(Arg);
5559 }
5560 }
5561 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5562}
5563
5564// <mangled-name> ::= _Z <encoding>
5565// ::= <type>
5566// extension ::= ___Z <encoding> _block_invoke
5567// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5568// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005569template <typename Derived, typename Alloc>
5570Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005571 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005572 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005573 if (Encoding == nullptr)
5574 return nullptr;
5575 if (look() == '.') {
5576 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5577 First = Last;
5578 }
5579 if (numLeft() != 0)
5580 return nullptr;
5581 return Encoding;
5582 }
5583
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005584 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005585 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005586 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5587 return nullptr;
5588 bool RequireNumber = consumeIf('_');
5589 if (parseNumber().empty() && RequireNumber)
5590 return nullptr;
5591 if (look() == '.')
5592 First = Last;
5593 if (numLeft() != 0)
5594 return nullptr;
5595 return make<SpecialName>("invocation function for block in ", Encoding);
5596 }
5597
Pavel Labathba825192018-10-16 14:29:14 +00005598 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005599 if (numLeft() != 0)
5600 return nullptr;
5601 return Ty;
5602}
5603
Pavel Labathba825192018-10-16 14:29:14 +00005604template <typename Alloc>
5605struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5606 using AbstractManglingParser<ManglingParser<Alloc>,
5607 Alloc>::AbstractManglingParser;
5608};
5609
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005610DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005611
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005612#endif // DEMANGLE_ITANIUMDEMANGLE_H