1 //===------------------------- ItaniumDemangle.h ----------------*- C++ -*-===// 2 // 3 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Generic itanium demangler library. 10 // There are two copies of this file in the source tree. The one under 11 // libcxxabi is the original and the one under llvm is the copy. Use 12 // cp-to-llvm.sh to update the copy. See README.txt for more details. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef DEMANGLE_ITANIUMDEMANGLE_H 17 #define DEMANGLE_ITANIUMDEMANGLE_H 18 19 #include "DemangleConfig.h" 20 #include "StringView.h" 21 #include "Utility.h" 22 #include <algorithm> 23 #include <cassert> 24 #include <cctype> 25 #include <cstdio> 26 #include <cstdlib> 27 #include <cstring> 28 #include <limits> 29 #include <utility> 30 31 DEMANGLE_NAMESPACE_BEGIN 32 33 template <class T, size_t N> class PODSmallVector { 34 static_assert(std::is_pod<T>::value, 35 "T is required to be a plain old data type"); 36 37 T *First = nullptr; 38 T *Last = nullptr; 39 T *Cap = nullptr; 40 T Inline[N] = {0}; 41 42 bool isInline() const { return First == Inline; } 43 44 void clearInline() { 45 First = Inline; 46 Last = Inline; 47 Cap = Inline + N; 48 } 49 50 void reserve(size_t NewCap) { 51 size_t S = size(); 52 if (isInline()) { 53 auto *Tmp = static_cast<T *>(std::malloc(NewCap * sizeof(T))); 54 if (Tmp == nullptr) 55 std::terminate(); 56 std::copy(First, Last, Tmp); 57 First = Tmp; 58 } else { 59 First = static_cast<T *>(std::realloc(First, NewCap * sizeof(T))); 60 if (First == nullptr) 61 std::terminate(); 62 } 63 Last = First + S; 64 Cap = First + NewCap; 65 } 66 67 public: 68 PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {} 69 70 PODSmallVector(const PODSmallVector &) = delete; 71 PODSmallVector &operator=(const PODSmallVector &) = delete; 72 73 PODSmallVector(PODSmallVector &&Other) : PODSmallVector() { 74 if (Other.isInline()) { 75 std::copy(Other.begin(), Other.end(), First); 76 Last = First + Other.size(); 77 Other.clear(); 78 return; 79 } 80 81 First = Other.First; 82 Last = Other.Last; 83 Cap = Other.Cap; 84 Other.clearInline(); 85 } 86 87 PODSmallVector &operator=(PODSmallVector &&Other) { 88 if (Other.isInline()) { 89 if (!isInline()) { 90 std::free(First); 91 clearInline(); 92 } 93 std::copy(Other.begin(), Other.end(), First); 94 Last = First + Other.size(); 95 Other.clear(); 96 return *this; 97 } 98 99 if (isInline()) { 100 First = Other.First; 101 Last = Other.Last; 102 Cap = Other.Cap; 103 Other.clearInline(); 104 return *this; 105 } 106 107 std::swap(First, Other.First); 108 std::swap(Last, Other.Last); 109 std::swap(Cap, Other.Cap); 110 Other.clear(); 111 return *this; 112 } 113 114 // NOLINTNEXTLINE(readability-identifier-naming) 115 void push_back(const T &Elem) { 116 if (Last == Cap) 117 reserve(size() * 2); 118 *Last++ = Elem; 119 } 120 121 // NOLINTNEXTLINE(readability-identifier-naming) 122 void pop_back() { 123 assert(Last != First && "Popping empty vector!"); 124 --Last; 125 } 126 127 void dropBack(size_t Index) { 128 assert(Index <= size() && "dropBack() can't expand!"); 129 Last = First + Index; 130 } 131 132 T *begin() { return First; } 133 T *end() { return Last; } 134 135 bool empty() const { return First == Last; } 136 size_t size() const { return static_cast<size_t>(Last - First); } 137 T &back() { 138 assert(Last != First && "Calling back() on empty vector!"); 139 return *(Last - 1); 140 } 141 T &operator[](size_t Index) { 142 assert(Index < size() && "Invalid access!"); 143 return *(begin() + Index); 144 } 145 void clear() { Last = First; } 146 147 ~PODSmallVector() { 148 if (!isInline()) 149 std::free(First); 150 } 151 }; 152 153 // Base class of all AST nodes. The AST is built by the parser, then is 154 // traversed by the printLeft/Right functions to produce a demangled string. 155 class Node { 156 public: 157 enum Kind : unsigned char { 158 #define NODE(NodeKind) K##NodeKind, 159 #include "ItaniumNodes.def" 160 }; 161 162 /// Three-way bool to track a cached value. Unknown is possible if this node 163 /// has an unexpanded parameter pack below it that may affect this cache. 164 enum class Cache : unsigned char { Yes, No, Unknown, }; 165 166 /// Operator precedence for expression nodes. Used to determine required 167 /// parens in expression emission. 168 enum class Prec { 169 Primary, 170 Postfix, 171 Unary, 172 Cast, 173 PtrMem, 174 Multiplicative, 175 Additive, 176 Shift, 177 Spaceship, 178 Relational, 179 Equality, 180 And, 181 Xor, 182 Ior, 183 AndIf, 184 OrIf, 185 Conditional, 186 Assign, 187 Comma, 188 Default, 189 }; 190 191 private: 192 Kind K; 193 194 Prec Precedence : 6; 195 196 // FIXME: Make these protected. 197 public: 198 /// Tracks if this node has a component on its right side, in which case we 199 /// need to call printRight. 200 Cache RHSComponentCache : 2; 201 202 /// Track if this node is a (possibly qualified) array type. This can affect 203 /// how we format the output string. 204 Cache ArrayCache : 2; 205 206 /// Track if this node is a (possibly qualified) function type. This can 207 /// affect how we format the output string. 208 Cache FunctionCache : 2; 209 210 public: 211 Node(Kind K_, Prec Precedence_ = Prec::Primary, 212 Cache RHSComponentCache_ = Cache::No, Cache ArrayCache_ = Cache::No, 213 Cache FunctionCache_ = Cache::No) 214 : K(K_), Precedence(Precedence_), RHSComponentCache(RHSComponentCache_), 215 ArrayCache(ArrayCache_), FunctionCache(FunctionCache_) {} 216 Node(Kind K_, Cache RHSComponentCache_, Cache ArrayCache_ = Cache::No, 217 Cache FunctionCache_ = Cache::No) 218 : Node(K_, Prec::Primary, RHSComponentCache_, ArrayCache_, 219 FunctionCache_) {} 220 221 /// Visit the most-derived object corresponding to this object. 222 template<typename Fn> void visit(Fn F) const; 223 224 // The following function is provided by all derived classes: 225 // 226 // Call F with arguments that, when passed to the constructor of this node, 227 // would construct an equivalent node. 228 //template<typename Fn> void match(Fn F) const; 229 230 bool hasRHSComponent(OutputBuffer &OB) const { 231 if (RHSComponentCache != Cache::Unknown) 232 return RHSComponentCache == Cache::Yes; 233 return hasRHSComponentSlow(OB); 234 } 235 236 bool hasArray(OutputBuffer &OB) const { 237 if (ArrayCache != Cache::Unknown) 238 return ArrayCache == Cache::Yes; 239 return hasArraySlow(OB); 240 } 241 242 bool hasFunction(OutputBuffer &OB) const { 243 if (FunctionCache != Cache::Unknown) 244 return FunctionCache == Cache::Yes; 245 return hasFunctionSlow(OB); 246 } 247 248 Kind getKind() const { return K; } 249 250 Prec getPrecedence() const { return Precedence; } 251 252 virtual bool hasRHSComponentSlow(OutputBuffer &) const { return false; } 253 virtual bool hasArraySlow(OutputBuffer &) const { return false; } 254 virtual bool hasFunctionSlow(OutputBuffer &) const { return false; } 255 256 // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to 257 // get at a node that actually represents some concrete syntax. 258 virtual const Node *getSyntaxNode(OutputBuffer &) const { return this; } 259 260 // Print this node as an expression operand, surrounding it in parentheses if 261 // its precedence is [Strictly] weaker than P. 262 void printAsOperand(OutputBuffer &OB, Prec P = Prec::Default, 263 bool StrictlyWorse = false) const { 264 bool Paren = 265 unsigned(getPrecedence()) >= unsigned(P) + unsigned(StrictlyWorse); 266 if (Paren) 267 OB.printOpen(); 268 print(OB); 269 if (Paren) 270 OB.printClose(); 271 } 272 273 void print(OutputBuffer &OB) const { 274 printLeft(OB); 275 if (RHSComponentCache != Cache::No) 276 printRight(OB); 277 } 278 279 // Print the "left" side of this Node into OutputBuffer. 280 virtual void printLeft(OutputBuffer &) const = 0; 281 282 // Print the "right". This distinction is necessary to represent C++ types 283 // that appear on the RHS of their subtype, such as arrays or functions. 284 // Since most types don't have such a component, provide a default 285 // implementation. 286 virtual void printRight(OutputBuffer &) const {} 287 288 virtual StringView getBaseName() const { return StringView(); } 289 290 // Silence compiler warnings, this dtor will never be called. 291 virtual ~Node() = default; 292 293 #ifndef NDEBUG 294 DEMANGLE_DUMP_METHOD void dump() const; 295 #endif 296 }; 297 298 class NodeArray { 299 Node **Elements; 300 size_t NumElements; 301 302 public: 303 NodeArray() : Elements(nullptr), NumElements(0) {} 304 NodeArray(Node **Elements_, size_t NumElements_) 305 : Elements(Elements_), NumElements(NumElements_) {} 306 307 bool empty() const { return NumElements == 0; } 308 size_t size() const { return NumElements; } 309 310 Node **begin() const { return Elements; } 311 Node **end() const { return Elements + NumElements; } 312 313 Node *operator[](size_t Idx) const { return Elements[Idx]; } 314 315 void printWithComma(OutputBuffer &OB) const { 316 bool FirstElement = true; 317 for (size_t Idx = 0; Idx != NumElements; ++Idx) { 318 size_t BeforeComma = OB.getCurrentPosition(); 319 if (!FirstElement) 320 OB += ", "; 321 size_t AfterComma = OB.getCurrentPosition(); 322 Elements[Idx]->printAsOperand(OB, Node::Prec::Comma); 323 324 // Elements[Idx] is an empty parameter pack expansion, we should erase the 325 // comma we just printed. 326 if (AfterComma == OB.getCurrentPosition()) { 327 OB.setCurrentPosition(BeforeComma); 328 continue; 329 } 330 331 FirstElement = false; 332 } 333 } 334 }; 335 336 struct NodeArrayNode : Node { 337 NodeArray Array; 338 NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {} 339 340 template<typename Fn> void match(Fn F) const { F(Array); } 341 342 void printLeft(OutputBuffer &OB) const override { Array.printWithComma(OB); } 343 }; 344 345 class DotSuffix final : public Node { 346 const Node *Prefix; 347 const StringView Suffix; 348 349 public: 350 DotSuffix(const Node *Prefix_, StringView Suffix_) 351 : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {} 352 353 template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); } 354 355 void printLeft(OutputBuffer &OB) const override { 356 Prefix->print(OB); 357 OB += " ("; 358 OB += Suffix; 359 OB += ")"; 360 } 361 }; 362 363 class VendorExtQualType final : public Node { 364 const Node *Ty; 365 StringView Ext; 366 const Node *TA; 367 368 public: 369 VendorExtQualType(const Node *Ty_, StringView Ext_, const Node *TA_) 370 : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_), TA(TA_) {} 371 372 template <typename Fn> void match(Fn F) const { F(Ty, Ext, TA); } 373 374 void printLeft(OutputBuffer &OB) const override { 375 Ty->print(OB); 376 OB += " "; 377 OB += Ext; 378 if (TA != nullptr) 379 TA->print(OB); 380 } 381 }; 382 383 enum FunctionRefQual : unsigned char { 384 FrefQualNone, 385 FrefQualLValue, 386 FrefQualRValue, 387 }; 388 389 enum Qualifiers { 390 QualNone = 0, 391 QualConst = 0x1, 392 QualVolatile = 0x2, 393 QualRestrict = 0x4, 394 }; 395 396 inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) { 397 return Q1 = static_cast<Qualifiers>(Q1 | Q2); 398 } 399 400 class QualType final : public Node { 401 protected: 402 const Qualifiers Quals; 403 const Node *Child; 404 405 void printQuals(OutputBuffer &OB) const { 406 if (Quals & QualConst) 407 OB += " const"; 408 if (Quals & QualVolatile) 409 OB += " volatile"; 410 if (Quals & QualRestrict) 411 OB += " restrict"; 412 } 413 414 public: 415 QualType(const Node *Child_, Qualifiers Quals_) 416 : Node(KQualType, Child_->RHSComponentCache, 417 Child_->ArrayCache, Child_->FunctionCache), 418 Quals(Quals_), Child(Child_) {} 419 420 template<typename Fn> void match(Fn F) const { F(Child, Quals); } 421 422 bool hasRHSComponentSlow(OutputBuffer &OB) const override { 423 return Child->hasRHSComponent(OB); 424 } 425 bool hasArraySlow(OutputBuffer &OB) const override { 426 return Child->hasArray(OB); 427 } 428 bool hasFunctionSlow(OutputBuffer &OB) const override { 429 return Child->hasFunction(OB); 430 } 431 432 void printLeft(OutputBuffer &OB) const override { 433 Child->printLeft(OB); 434 printQuals(OB); 435 } 436 437 void printRight(OutputBuffer &OB) const override { Child->printRight(OB); } 438 }; 439 440 class ConversionOperatorType final : public Node { 441 const Node *Ty; 442 443 public: 444 ConversionOperatorType(const Node *Ty_) 445 : Node(KConversionOperatorType), Ty(Ty_) {} 446 447 template<typename Fn> void match(Fn F) const { F(Ty); } 448 449 void printLeft(OutputBuffer &OB) const override { 450 OB += "operator "; 451 Ty->print(OB); 452 } 453 }; 454 455 class PostfixQualifiedType final : public Node { 456 const Node *Ty; 457 const StringView Postfix; 458 459 public: 460 PostfixQualifiedType(const Node *Ty_, StringView Postfix_) 461 : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {} 462 463 template<typename Fn> void match(Fn F) const { F(Ty, Postfix); } 464 465 void printLeft(OutputBuffer &OB) const override { 466 Ty->printLeft(OB); 467 OB += Postfix; 468 } 469 }; 470 471 class NameType final : public Node { 472 const StringView Name; 473 474 public: 475 NameType(StringView Name_) : Node(KNameType), Name(Name_) {} 476 477 template<typename Fn> void match(Fn F) const { F(Name); } 478 479 StringView getName() const { return Name; } 480 StringView getBaseName() const override { return Name; } 481 482 void printLeft(OutputBuffer &OB) const override { OB += Name; } 483 }; 484 485 class BitIntType final : public Node { 486 const Node *Size; 487 bool Signed; 488 489 public: 490 BitIntType(const Node *Size_, bool Signed_) 491 : Node(KBitIntType), Size(Size_), Signed(Signed_) {} 492 493 template <typename Fn> void match(Fn F) const { F(Size, Signed); } 494 495 void printLeft(OutputBuffer &OB) const override { 496 if (!Signed) 497 OB += "unsigned "; 498 OB += "_BitInt"; 499 OB.printOpen(); 500 Size->printAsOperand(OB); 501 OB.printClose(); 502 } 503 }; 504 505 class ElaboratedTypeSpefType : public Node { 506 StringView Kind; 507 Node *Child; 508 public: 509 ElaboratedTypeSpefType(StringView Kind_, Node *Child_) 510 : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {} 511 512 template<typename Fn> void match(Fn F) const { F(Kind, Child); } 513 514 void printLeft(OutputBuffer &OB) const override { 515 OB += Kind; 516 OB += ' '; 517 Child->print(OB); 518 } 519 }; 520 521 struct AbiTagAttr : Node { 522 Node *Base; 523 StringView Tag; 524 525 AbiTagAttr(Node* Base_, StringView Tag_) 526 : Node(KAbiTagAttr, Base_->RHSComponentCache, 527 Base_->ArrayCache, Base_->FunctionCache), 528 Base(Base_), Tag(Tag_) {} 529 530 template<typename Fn> void match(Fn F) const { F(Base, Tag); } 531 532 void printLeft(OutputBuffer &OB) const override { 533 Base->printLeft(OB); 534 OB += "[abi:"; 535 OB += Tag; 536 OB += "]"; 537 } 538 }; 539 540 class EnableIfAttr : public Node { 541 NodeArray Conditions; 542 public: 543 EnableIfAttr(NodeArray Conditions_) 544 : Node(KEnableIfAttr), Conditions(Conditions_) {} 545 546 template<typename Fn> void match(Fn F) const { F(Conditions); } 547 548 void printLeft(OutputBuffer &OB) const override { 549 OB += " [enable_if:"; 550 Conditions.printWithComma(OB); 551 OB += ']'; 552 } 553 }; 554 555 class ObjCProtoName : public Node { 556 const Node *Ty; 557 StringView Protocol; 558 559 friend class PointerType; 560 561 public: 562 ObjCProtoName(const Node *Ty_, StringView Protocol_) 563 : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {} 564 565 template<typename Fn> void match(Fn F) const { F(Ty, Protocol); } 566 567 bool isObjCObject() const { 568 return Ty->getKind() == KNameType && 569 static_cast<const NameType *>(Ty)->getName() == "objc_object"; 570 } 571 572 void printLeft(OutputBuffer &OB) const override { 573 Ty->print(OB); 574 OB += "<"; 575 OB += Protocol; 576 OB += ">"; 577 } 578 }; 579 580 class PointerType final : public Node { 581 const Node *Pointee; 582 583 public: 584 PointerType(const Node *Pointee_) 585 : Node(KPointerType, Pointee_->RHSComponentCache), 586 Pointee(Pointee_) {} 587 588 template<typename Fn> void match(Fn F) const { F(Pointee); } 589 590 bool hasRHSComponentSlow(OutputBuffer &OB) const override { 591 return Pointee->hasRHSComponent(OB); 592 } 593 594 void printLeft(OutputBuffer &OB) const override { 595 // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>. 596 if (Pointee->getKind() != KObjCProtoName || 597 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) { 598 Pointee->printLeft(OB); 599 if (Pointee->hasArray(OB)) 600 OB += " "; 601 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB)) 602 OB += "("; 603 OB += "*"; 604 } else { 605 const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee); 606 OB += "id<"; 607 OB += objcProto->Protocol; 608 OB += ">"; 609 } 610 } 611 612 void printRight(OutputBuffer &OB) const override { 613 if (Pointee->getKind() != KObjCProtoName || 614 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) { 615 if (Pointee->hasArray(OB) || Pointee->hasFunction(OB)) 616 OB += ")"; 617 Pointee->printRight(OB); 618 } 619 } 620 }; 621 622 enum class ReferenceKind { 623 LValue, 624 RValue, 625 }; 626 627 // Represents either a LValue or an RValue reference type. 628 class ReferenceType : public Node { 629 const Node *Pointee; 630 ReferenceKind RK; 631 632 mutable bool Printing = false; 633 634 // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The 635 // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any 636 // other combination collapses to a lvalue ref. 637 // 638 // A combination of a TemplateForwardReference and a back-ref Substitution 639 // from an ill-formed string may have created a cycle; use cycle detection to 640 // avoid looping forever. 641 std::pair<ReferenceKind, const Node *> collapse(OutputBuffer &OB) const { 642 auto SoFar = std::make_pair(RK, Pointee); 643 // Track the chain of nodes for the Floyd's 'tortoise and hare' 644 // cycle-detection algorithm, since getSyntaxNode(S) is impure 645 PODSmallVector<const Node *, 8> Prev; 646 for (;;) { 647 const Node *SN = SoFar.second->getSyntaxNode(OB); 648 if (SN->getKind() != KReferenceType) 649 break; 650 auto *RT = static_cast<const ReferenceType *>(SN); 651 SoFar.second = RT->Pointee; 652 SoFar.first = std::min(SoFar.first, RT->RK); 653 654 // The middle of Prev is the 'slow' pointer moving at half speed 655 Prev.push_back(SoFar.second); 656 if (Prev.size() > 1 && SoFar.second == Prev[(Prev.size() - 1) / 2]) { 657 // Cycle detected 658 SoFar.second = nullptr; 659 break; 660 } 661 } 662 return SoFar; 663 } 664 665 public: 666 ReferenceType(const Node *Pointee_, ReferenceKind RK_) 667 : Node(KReferenceType, Pointee_->RHSComponentCache), 668 Pointee(Pointee_), RK(RK_) {} 669 670 template<typename Fn> void match(Fn F) const { F(Pointee, RK); } 671 672 bool hasRHSComponentSlow(OutputBuffer &OB) const override { 673 return Pointee->hasRHSComponent(OB); 674 } 675 676 void printLeft(OutputBuffer &OB) const override { 677 if (Printing) 678 return; 679 ScopedOverride<bool> SavePrinting(Printing, true); 680 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB); 681 if (!Collapsed.second) 682 return; 683 Collapsed.second->printLeft(OB); 684 if (Collapsed.second->hasArray(OB)) 685 OB += " "; 686 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB)) 687 OB += "("; 688 689 OB += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&"); 690 } 691 void printRight(OutputBuffer &OB) const override { 692 if (Printing) 693 return; 694 ScopedOverride<bool> SavePrinting(Printing, true); 695 std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB); 696 if (!Collapsed.second) 697 return; 698 if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB)) 699 OB += ")"; 700 Collapsed.second->printRight(OB); 701 } 702 }; 703 704 class PointerToMemberType final : public Node { 705 const Node *ClassType; 706 const Node *MemberType; 707 708 public: 709 PointerToMemberType(const Node *ClassType_, const Node *MemberType_) 710 : Node(KPointerToMemberType, MemberType_->RHSComponentCache), 711 ClassType(ClassType_), MemberType(MemberType_) {} 712 713 template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); } 714 715 bool hasRHSComponentSlow(OutputBuffer &OB) const override { 716 return MemberType->hasRHSComponent(OB); 717 } 718 719 void printLeft(OutputBuffer &OB) const override { 720 MemberType->printLeft(OB); 721 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB)) 722 OB += "("; 723 else 724 OB += " "; 725 ClassType->print(OB); 726 OB += "::*"; 727 } 728 729 void printRight(OutputBuffer &OB) const override { 730 if (MemberType->hasArray(OB) || MemberType->hasFunction(OB)) 731 OB += ")"; 732 MemberType->printRight(OB); 733 } 734 }; 735 736 class ArrayType final : public Node { 737 const Node *Base; 738 Node *Dimension; 739 740 public: 741 ArrayType(const Node *Base_, Node *Dimension_) 742 : Node(KArrayType, 743 /*RHSComponentCache=*/Cache::Yes, 744 /*ArrayCache=*/Cache::Yes), 745 Base(Base_), Dimension(Dimension_) {} 746 747 template<typename Fn> void match(Fn F) const { F(Base, Dimension); } 748 749 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; } 750 bool hasArraySlow(OutputBuffer &) const override { return true; } 751 752 void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); } 753 754 void printRight(OutputBuffer &OB) const override { 755 if (OB.back() != ']') 756 OB += " "; 757 OB += "["; 758 if (Dimension) 759 Dimension->print(OB); 760 OB += "]"; 761 Base->printRight(OB); 762 } 763 }; 764 765 class FunctionType final : public Node { 766 const Node *Ret; 767 NodeArray Params; 768 Qualifiers CVQuals; 769 FunctionRefQual RefQual; 770 const Node *ExceptionSpec; 771 772 public: 773 FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_, 774 FunctionRefQual RefQual_, const Node *ExceptionSpec_) 775 : Node(KFunctionType, 776 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No, 777 /*FunctionCache=*/Cache::Yes), 778 Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_), 779 ExceptionSpec(ExceptionSpec_) {} 780 781 template<typename Fn> void match(Fn F) const { 782 F(Ret, Params, CVQuals, RefQual, ExceptionSpec); 783 } 784 785 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; } 786 bool hasFunctionSlow(OutputBuffer &) const override { return true; } 787 788 // Handle C++'s ... quirky decl grammar by using the left & right 789 // distinction. Consider: 790 // int (*f(float))(char) {} 791 // f is a function that takes a float and returns a pointer to a function 792 // that takes a char and returns an int. If we're trying to print f, start 793 // by printing out the return types's left, then print our parameters, then 794 // finally print right of the return type. 795 void printLeft(OutputBuffer &OB) const override { 796 Ret->printLeft(OB); 797 OB += " "; 798 } 799 800 void printRight(OutputBuffer &OB) const override { 801 OB.printOpen(); 802 Params.printWithComma(OB); 803 OB.printClose(); 804 Ret->printRight(OB); 805 806 if (CVQuals & QualConst) 807 OB += " const"; 808 if (CVQuals & QualVolatile) 809 OB += " volatile"; 810 if (CVQuals & QualRestrict) 811 OB += " restrict"; 812 813 if (RefQual == FrefQualLValue) 814 OB += " &"; 815 else if (RefQual == FrefQualRValue) 816 OB += " &&"; 817 818 if (ExceptionSpec != nullptr) { 819 OB += ' '; 820 ExceptionSpec->print(OB); 821 } 822 } 823 }; 824 825 class NoexceptSpec : public Node { 826 const Node *E; 827 public: 828 NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {} 829 830 template<typename Fn> void match(Fn F) const { F(E); } 831 832 void printLeft(OutputBuffer &OB) const override { 833 OB += "noexcept"; 834 OB.printOpen(); 835 E->printAsOperand(OB); 836 OB.printClose(); 837 } 838 }; 839 840 class DynamicExceptionSpec : public Node { 841 NodeArray Types; 842 public: 843 DynamicExceptionSpec(NodeArray Types_) 844 : Node(KDynamicExceptionSpec), Types(Types_) {} 845 846 template<typename Fn> void match(Fn F) const { F(Types); } 847 848 void printLeft(OutputBuffer &OB) const override { 849 OB += "throw"; 850 OB.printOpen(); 851 Types.printWithComma(OB); 852 OB.printClose(); 853 } 854 }; 855 856 class FunctionEncoding final : public Node { 857 const Node *Ret; 858 const Node *Name; 859 NodeArray Params; 860 const Node *Attrs; 861 Qualifiers CVQuals; 862 FunctionRefQual RefQual; 863 864 public: 865 FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_, 866 const Node *Attrs_, Qualifiers CVQuals_, 867 FunctionRefQual RefQual_) 868 : Node(KFunctionEncoding, 869 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No, 870 /*FunctionCache=*/Cache::Yes), 871 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_), 872 CVQuals(CVQuals_), RefQual(RefQual_) {} 873 874 template<typename Fn> void match(Fn F) const { 875 F(Ret, Name, Params, Attrs, CVQuals, RefQual); 876 } 877 878 Qualifiers getCVQuals() const { return CVQuals; } 879 FunctionRefQual getRefQual() const { return RefQual; } 880 NodeArray getParams() const { return Params; } 881 const Node *getReturnType() const { return Ret; } 882 883 bool hasRHSComponentSlow(OutputBuffer &) const override { return true; } 884 bool hasFunctionSlow(OutputBuffer &) const override { return true; } 885 886 const Node *getName() const { return Name; } 887 888 void printLeft(OutputBuffer &OB) const override { 889 if (Ret) { 890 Ret->printLeft(OB); 891 if (!Ret->hasRHSComponent(OB)) 892 OB += " "; 893 } 894 Name->print(OB); 895 } 896 897 void printRight(OutputBuffer &OB) const override { 898 OB.printOpen(); 899 Params.printWithComma(OB); 900 OB.printClose(); 901 if (Ret) 902 Ret->printRight(OB); 903 904 if (CVQuals & QualConst) 905 OB += " const"; 906 if (CVQuals & QualVolatile) 907 OB += " volatile"; 908 if (CVQuals & QualRestrict) 909 OB += " restrict"; 910 911 if (RefQual == FrefQualLValue) 912 OB += " &"; 913 else if (RefQual == FrefQualRValue) 914 OB += " &&"; 915 916 if (Attrs != nullptr) 917 Attrs->print(OB); 918 } 919 }; 920 921 class LiteralOperator : public Node { 922 const Node *OpName; 923 924 public: 925 LiteralOperator(const Node *OpName_) 926 : Node(KLiteralOperator), OpName(OpName_) {} 927 928 template<typename Fn> void match(Fn F) const { F(OpName); } 929 930 void printLeft(OutputBuffer &OB) const override { 931 OB += "operator\"\" "; 932 OpName->print(OB); 933 } 934 }; 935 936 class SpecialName final : public Node { 937 const StringView Special; 938 const Node *Child; 939 940 public: 941 SpecialName(StringView Special_, const Node *Child_) 942 : Node(KSpecialName), Special(Special_), Child(Child_) {} 943 944 template<typename Fn> void match(Fn F) const { F(Special, Child); } 945 946 void printLeft(OutputBuffer &OB) const override { 947 OB += Special; 948 Child->print(OB); 949 } 950 }; 951 952 class CtorVtableSpecialName final : public Node { 953 const Node *FirstType; 954 const Node *SecondType; 955 956 public: 957 CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_) 958 : Node(KCtorVtableSpecialName), 959 FirstType(FirstType_), SecondType(SecondType_) {} 960 961 template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); } 962 963 void printLeft(OutputBuffer &OB) const override { 964 OB += "construction vtable for "; 965 FirstType->print(OB); 966 OB += "-in-"; 967 SecondType->print(OB); 968 } 969 }; 970 971 struct NestedName : Node { 972 Node *Qual; 973 Node *Name; 974 975 NestedName(Node *Qual_, Node *Name_) 976 : Node(KNestedName), Qual(Qual_), Name(Name_) {} 977 978 template<typename Fn> void match(Fn F) const { F(Qual, Name); } 979 980 StringView getBaseName() const override { return Name->getBaseName(); } 981 982 void printLeft(OutputBuffer &OB) const override { 983 Qual->print(OB); 984 OB += "::"; 985 Name->print(OB); 986 } 987 }; 988 989 struct ModuleName : Node { 990 ModuleName *Parent; 991 Node *Name; 992 bool IsPartition; 993 994 ModuleName(ModuleName *Parent_, Node *Name_, bool IsPartition_ = false) 995 : Node(KModuleName), Parent(Parent_), Name(Name_), 996 IsPartition(IsPartition_) {} 997 998 template <typename Fn> void match(Fn F) const { 999 F(Parent, Name, IsPartition); 1000 } 1001 1002 void printLeft(OutputBuffer &OB) const override { 1003 if (Parent) 1004 Parent->print(OB); 1005 if (Parent || IsPartition) 1006 OB += IsPartition ? ':' : '.'; 1007 Name->print(OB); 1008 } 1009 }; 1010 1011 struct ModuleEntity : Node { 1012 ModuleName *Module; 1013 Node *Name; 1014 1015 ModuleEntity(ModuleName *Module_, Node *Name_) 1016 : Node(KModuleEntity), Module(Module_), Name(Name_) {} 1017 1018 template <typename Fn> void match(Fn F) const { F(Module, Name); } 1019 1020 StringView getBaseName() const override { return Name->getBaseName(); } 1021 1022 void printLeft(OutputBuffer &OB) const override { 1023 Name->print(OB); 1024 OB += '@'; 1025 Module->print(OB); 1026 } 1027 }; 1028 1029 struct LocalName : Node { 1030 Node *Encoding; 1031 Node *Entity; 1032 1033 LocalName(Node *Encoding_, Node *Entity_) 1034 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {} 1035 1036 template<typename Fn> void match(Fn F) const { F(Encoding, Entity); } 1037 1038 void printLeft(OutputBuffer &OB) const override { 1039 Encoding->print(OB); 1040 OB += "::"; 1041 Entity->print(OB); 1042 } 1043 }; 1044 1045 class QualifiedName final : public Node { 1046 // qualifier::name 1047 const Node *Qualifier; 1048 const Node *Name; 1049 1050 public: 1051 QualifiedName(const Node *Qualifier_, const Node *Name_) 1052 : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {} 1053 1054 template<typename Fn> void match(Fn F) const { F(Qualifier, Name); } 1055 1056 StringView getBaseName() const override { return Name->getBaseName(); } 1057 1058 void printLeft(OutputBuffer &OB) const override { 1059 Qualifier->print(OB); 1060 OB += "::"; 1061 Name->print(OB); 1062 } 1063 }; 1064 1065 class VectorType final : public Node { 1066 const Node *BaseType; 1067 const Node *Dimension; 1068 1069 public: 1070 VectorType(const Node *BaseType_, const Node *Dimension_) 1071 : Node(KVectorType), BaseType(BaseType_), Dimension(Dimension_) {} 1072 1073 template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); } 1074 1075 void printLeft(OutputBuffer &OB) const override { 1076 BaseType->print(OB); 1077 OB += " vector["; 1078 if (Dimension) 1079 Dimension->print(OB); 1080 OB += "]"; 1081 } 1082 }; 1083 1084 class PixelVectorType final : public Node { 1085 const Node *Dimension; 1086 1087 public: 1088 PixelVectorType(const Node *Dimension_) 1089 : Node(KPixelVectorType), Dimension(Dimension_) {} 1090 1091 template<typename Fn> void match(Fn F) const { F(Dimension); } 1092 1093 void printLeft(OutputBuffer &OB) const override { 1094 // FIXME: This should demangle as "vector pixel". 1095 OB += "pixel vector["; 1096 Dimension->print(OB); 1097 OB += "]"; 1098 } 1099 }; 1100 1101 class BinaryFPType final : public Node { 1102 const Node *Dimension; 1103 1104 public: 1105 BinaryFPType(const Node *Dimension_) 1106 : Node(KBinaryFPType), Dimension(Dimension_) {} 1107 1108 template<typename Fn> void match(Fn F) const { F(Dimension); } 1109 1110 void printLeft(OutputBuffer &OB) const override { 1111 OB += "_Float"; 1112 Dimension->print(OB); 1113 } 1114 }; 1115 1116 enum class TemplateParamKind { Type, NonType, Template }; 1117 1118 /// An invented name for a template parameter for which we don't have a 1119 /// corresponding template argument. 1120 /// 1121 /// This node is created when parsing the <lambda-sig> for a lambda with 1122 /// explicit template arguments, which might be referenced in the parameter 1123 /// types appearing later in the <lambda-sig>. 1124 class SyntheticTemplateParamName final : public Node { 1125 TemplateParamKind Kind; 1126 unsigned Index; 1127 1128 public: 1129 SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_) 1130 : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {} 1131 1132 template<typename Fn> void match(Fn F) const { F(Kind, Index); } 1133 1134 void printLeft(OutputBuffer &OB) const override { 1135 switch (Kind) { 1136 case TemplateParamKind::Type: 1137 OB += "$T"; 1138 break; 1139 case TemplateParamKind::NonType: 1140 OB += "$N"; 1141 break; 1142 case TemplateParamKind::Template: 1143 OB += "$TT"; 1144 break; 1145 } 1146 if (Index > 0) 1147 OB << Index - 1; 1148 } 1149 }; 1150 1151 /// A template type parameter declaration, 'typename T'. 1152 class TypeTemplateParamDecl final : public Node { 1153 Node *Name; 1154 1155 public: 1156 TypeTemplateParamDecl(Node *Name_) 1157 : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {} 1158 1159 template<typename Fn> void match(Fn F) const { F(Name); } 1160 1161 void printLeft(OutputBuffer &OB) const override { OB += "typename "; } 1162 1163 void printRight(OutputBuffer &OB) const override { Name->print(OB); } 1164 }; 1165 1166 /// A non-type template parameter declaration, 'int N'. 1167 class NonTypeTemplateParamDecl final : public Node { 1168 Node *Name; 1169 Node *Type; 1170 1171 public: 1172 NonTypeTemplateParamDecl(Node *Name_, Node *Type_) 1173 : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {} 1174 1175 template<typename Fn> void match(Fn F) const { F(Name, Type); } 1176 1177 void printLeft(OutputBuffer &OB) const override { 1178 Type->printLeft(OB); 1179 if (!Type->hasRHSComponent(OB)) 1180 OB += " "; 1181 } 1182 1183 void printRight(OutputBuffer &OB) const override { 1184 Name->print(OB); 1185 Type->printRight(OB); 1186 } 1187 }; 1188 1189 /// A template template parameter declaration, 1190 /// 'template<typename T> typename N'. 1191 class TemplateTemplateParamDecl final : public Node { 1192 Node *Name; 1193 NodeArray Params; 1194 1195 public: 1196 TemplateTemplateParamDecl(Node *Name_, NodeArray Params_) 1197 : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_), 1198 Params(Params_) {} 1199 1200 template<typename Fn> void match(Fn F) const { F(Name, Params); } 1201 1202 void printLeft(OutputBuffer &OB) const override { 1203 ScopedOverride<unsigned> LT(OB.GtIsGt, 0); 1204 OB += "template<"; 1205 Params.printWithComma(OB); 1206 OB += "> typename "; 1207 } 1208 1209 void printRight(OutputBuffer &OB) const override { Name->print(OB); } 1210 }; 1211 1212 /// A template parameter pack declaration, 'typename ...T'. 1213 class TemplateParamPackDecl final : public Node { 1214 Node *Param; 1215 1216 public: 1217 TemplateParamPackDecl(Node *Param_) 1218 : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {} 1219 1220 template<typename Fn> void match(Fn F) const { F(Param); } 1221 1222 void printLeft(OutputBuffer &OB) const override { 1223 Param->printLeft(OB); 1224 OB += "..."; 1225 } 1226 1227 void printRight(OutputBuffer &OB) const override { Param->printRight(OB); } 1228 }; 1229 1230 /// An unexpanded parameter pack (either in the expression or type context). If 1231 /// this AST is correct, this node will have a ParameterPackExpansion node above 1232 /// it. 1233 /// 1234 /// This node is created when some <template-args> are found that apply to an 1235 /// <encoding>, and is stored in the TemplateParams table. In order for this to 1236 /// appear in the final AST, it has to referenced via a <template-param> (ie, 1237 /// T_). 1238 class ParameterPack final : public Node { 1239 NodeArray Data; 1240 1241 // Setup OutputBuffer for a pack expansion, unless we're already expanding 1242 // one. 1243 void initializePackExpansion(OutputBuffer &OB) const { 1244 if (OB.CurrentPackMax == std::numeric_limits<unsigned>::max()) { 1245 OB.CurrentPackMax = static_cast<unsigned>(Data.size()); 1246 OB.CurrentPackIndex = 0; 1247 } 1248 } 1249 1250 public: 1251 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) { 1252 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown; 1253 if (std::all_of(Data.begin(), Data.end(), [](Node* P) { 1254 return P->ArrayCache == Cache::No; 1255 })) 1256 ArrayCache = Cache::No; 1257 if (std::all_of(Data.begin(), Data.end(), [](Node* P) { 1258 return P->FunctionCache == Cache::No; 1259 })) 1260 FunctionCache = Cache::No; 1261 if (std::all_of(Data.begin(), Data.end(), [](Node* P) { 1262 return P->RHSComponentCache == Cache::No; 1263 })) 1264 RHSComponentCache = Cache::No; 1265 } 1266 1267 template<typename Fn> void match(Fn F) const { F(Data); } 1268 1269 bool hasRHSComponentSlow(OutputBuffer &OB) const override { 1270 initializePackExpansion(OB); 1271 size_t Idx = OB.CurrentPackIndex; 1272 return Idx < Data.size() && Data[Idx]->hasRHSComponent(OB); 1273 } 1274 bool hasArraySlow(OutputBuffer &OB) const override { 1275 initializePackExpansion(OB); 1276 size_t Idx = OB.CurrentPackIndex; 1277 return Idx < Data.size() && Data[Idx]->hasArray(OB); 1278 } 1279 bool hasFunctionSlow(OutputBuffer &OB) const override { 1280 initializePackExpansion(OB); 1281 size_t Idx = OB.CurrentPackIndex; 1282 return Idx < Data.size() && Data[Idx]->hasFunction(OB); 1283 } 1284 const Node *getSyntaxNode(OutputBuffer &OB) const override { 1285 initializePackExpansion(OB); 1286 size_t Idx = OB.CurrentPackIndex; 1287 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(OB) : this; 1288 } 1289 1290 void printLeft(OutputBuffer &OB) const override { 1291 initializePackExpansion(OB); 1292 size_t Idx = OB.CurrentPackIndex; 1293 if (Idx < Data.size()) 1294 Data[Idx]->printLeft(OB); 1295 } 1296 void printRight(OutputBuffer &OB) const override { 1297 initializePackExpansion(OB); 1298 size_t Idx = OB.CurrentPackIndex; 1299 if (Idx < Data.size()) 1300 Data[Idx]->printRight(OB); 1301 } 1302 }; 1303 1304 /// A variadic template argument. This node represents an occurrence of 1305 /// J<something>E in some <template-args>. It isn't itself unexpanded, unless 1306 /// one of it's Elements is. The parser inserts a ParameterPack into the 1307 /// TemplateParams table if the <template-args> this pack belongs to apply to an 1308 /// <encoding>. 1309 class TemplateArgumentPack final : public Node { 1310 NodeArray Elements; 1311 public: 1312 TemplateArgumentPack(NodeArray Elements_) 1313 : Node(KTemplateArgumentPack), Elements(Elements_) {} 1314 1315 template<typename Fn> void match(Fn F) const { F(Elements); } 1316 1317 NodeArray getElements() const { return Elements; } 1318 1319 void printLeft(OutputBuffer &OB) const override { 1320 Elements.printWithComma(OB); 1321 } 1322 }; 1323 1324 /// A pack expansion. Below this node, there are some unexpanded ParameterPacks 1325 /// which each have Child->ParameterPackSize elements. 1326 class ParameterPackExpansion final : public Node { 1327 const Node *Child; 1328 1329 public: 1330 ParameterPackExpansion(const Node *Child_) 1331 : Node(KParameterPackExpansion), Child(Child_) {} 1332 1333 template<typename Fn> void match(Fn F) const { F(Child); } 1334 1335 const Node *getChild() const { return Child; } 1336 1337 void printLeft(OutputBuffer &OB) const override { 1338 constexpr unsigned Max = std::numeric_limits<unsigned>::max(); 1339 ScopedOverride<unsigned> SavePackIdx(OB.CurrentPackIndex, Max); 1340 ScopedOverride<unsigned> SavePackMax(OB.CurrentPackMax, Max); 1341 size_t StreamPos = OB.getCurrentPosition(); 1342 1343 // Print the first element in the pack. If Child contains a ParameterPack, 1344 // it will set up S.CurrentPackMax and print the first element. 1345 Child->print(OB); 1346 1347 // No ParameterPack was found in Child. This can occur if we've found a pack 1348 // expansion on a <function-param>. 1349 if (OB.CurrentPackMax == Max) { 1350 OB += "..."; 1351 return; 1352 } 1353 1354 // We found a ParameterPack, but it has no elements. Erase whatever we may 1355 // of printed. 1356 if (OB.CurrentPackMax == 0) { 1357 OB.setCurrentPosition(StreamPos); 1358 return; 1359 } 1360 1361 // Else, iterate through the rest of the elements in the pack. 1362 for (unsigned I = 1, E = OB.CurrentPackMax; I < E; ++I) { 1363 OB += ", "; 1364 OB.CurrentPackIndex = I; 1365 Child->print(OB); 1366 } 1367 } 1368 }; 1369 1370 class TemplateArgs final : public Node { 1371 NodeArray Params; 1372 1373 public: 1374 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {} 1375 1376 template<typename Fn> void match(Fn F) const { F(Params); } 1377 1378 NodeArray getParams() { return Params; } 1379 1380 void printLeft(OutputBuffer &OB) const override { 1381 ScopedOverride<unsigned> LT(OB.GtIsGt, 0); 1382 OB += "<"; 1383 Params.printWithComma(OB); 1384 if (OB.back() == '>') 1385 OB += " "; 1386 OB += ">"; 1387 } 1388 }; 1389 1390 /// A forward-reference to a template argument that was not known at the point 1391 /// where the template parameter name was parsed in a mangling. 1392 /// 1393 /// This is created when demangling the name of a specialization of a 1394 /// conversion function template: 1395 /// 1396 /// \code 1397 /// struct A { 1398 /// template<typename T> operator T*(); 1399 /// }; 1400 /// \endcode 1401 /// 1402 /// When demangling a specialization of the conversion function template, we 1403 /// encounter the name of the template (including the \c T) before we reach 1404 /// the template argument list, so we cannot substitute the parameter name 1405 /// for the corresponding argument while parsing. Instead, we create a 1406 /// \c ForwardTemplateReference node that is resolved after we parse the 1407 /// template arguments. 1408 struct ForwardTemplateReference : Node { 1409 size_t Index; 1410 Node *Ref = nullptr; 1411 1412 // If we're currently printing this node. It is possible (though invalid) for 1413 // a forward template reference to refer to itself via a substitution. This 1414 // creates a cyclic AST, which will stack overflow printing. To fix this, bail 1415 // out if more than one print* function is active. 1416 mutable bool Printing = false; 1417 1418 ForwardTemplateReference(size_t Index_) 1419 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown, 1420 Cache::Unknown), 1421 Index(Index_) {} 1422 1423 // We don't provide a matcher for these, because the value of the node is 1424 // not determined by its construction parameters, and it generally needs 1425 // special handling. 1426 template<typename Fn> void match(Fn F) const = delete; 1427 1428 bool hasRHSComponentSlow(OutputBuffer &OB) const override { 1429 if (Printing) 1430 return false; 1431 ScopedOverride<bool> SavePrinting(Printing, true); 1432 return Ref->hasRHSComponent(OB); 1433 } 1434 bool hasArraySlow(OutputBuffer &OB) const override { 1435 if (Printing) 1436 return false; 1437 ScopedOverride<bool> SavePrinting(Printing, true); 1438 return Ref->hasArray(OB); 1439 } 1440 bool hasFunctionSlow(OutputBuffer &OB) const override { 1441 if (Printing) 1442 return false; 1443 ScopedOverride<bool> SavePrinting(Printing, true); 1444 return Ref->hasFunction(OB); 1445 } 1446 const Node *getSyntaxNode(OutputBuffer &OB) const override { 1447 if (Printing) 1448 return this; 1449 ScopedOverride<bool> SavePrinting(Printing, true); 1450 return Ref->getSyntaxNode(OB); 1451 } 1452 1453 void printLeft(OutputBuffer &OB) const override { 1454 if (Printing) 1455 return; 1456 ScopedOverride<bool> SavePrinting(Printing, true); 1457 Ref->printLeft(OB); 1458 } 1459 void printRight(OutputBuffer &OB) const override { 1460 if (Printing) 1461 return; 1462 ScopedOverride<bool> SavePrinting(Printing, true); 1463 Ref->printRight(OB); 1464 } 1465 }; 1466 1467 struct NameWithTemplateArgs : Node { 1468 // name<template_args> 1469 Node *Name; 1470 Node *TemplateArgs; 1471 1472 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_) 1473 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {} 1474 1475 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); } 1476 1477 StringView getBaseName() const override { return Name->getBaseName(); } 1478 1479 void printLeft(OutputBuffer &OB) const override { 1480 Name->print(OB); 1481 TemplateArgs->print(OB); 1482 } 1483 }; 1484 1485 class GlobalQualifiedName final : public Node { 1486 Node *Child; 1487 1488 public: 1489 GlobalQualifiedName(Node* Child_) 1490 : Node(KGlobalQualifiedName), Child(Child_) {} 1491 1492 template<typename Fn> void match(Fn F) const { F(Child); } 1493 1494 StringView getBaseName() const override { return Child->getBaseName(); } 1495 1496 void printLeft(OutputBuffer &OB) const override { 1497 OB += "::"; 1498 Child->print(OB); 1499 } 1500 }; 1501 1502 enum class SpecialSubKind { 1503 allocator, 1504 basic_string, 1505 string, 1506 istream, 1507 ostream, 1508 iostream, 1509 }; 1510 1511 class ExpandedSpecialSubstitution final : public Node { 1512 SpecialSubKind SSK; 1513 1514 public: 1515 ExpandedSpecialSubstitution(SpecialSubKind SSK_) 1516 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {} 1517 1518 template<typename Fn> void match(Fn F) const { F(SSK); } 1519 1520 StringView getBaseName() const override { 1521 switch (SSK) { 1522 case SpecialSubKind::allocator: 1523 return StringView("allocator"); 1524 case SpecialSubKind::basic_string: 1525 return StringView("basic_string"); 1526 case SpecialSubKind::string: 1527 return StringView("basic_string"); 1528 case SpecialSubKind::istream: 1529 return StringView("basic_istream"); 1530 case SpecialSubKind::ostream: 1531 return StringView("basic_ostream"); 1532 case SpecialSubKind::iostream: 1533 return StringView("basic_iostream"); 1534 } 1535 DEMANGLE_UNREACHABLE; 1536 } 1537 1538 void printLeft(OutputBuffer &OB) const override { 1539 switch (SSK) { 1540 case SpecialSubKind::allocator: 1541 OB += "std::allocator"; 1542 break; 1543 case SpecialSubKind::basic_string: 1544 OB += "std::basic_string"; 1545 break; 1546 case SpecialSubKind::string: 1547 OB += "std::basic_string<char, std::char_traits<char>, " 1548 "std::allocator<char> >"; 1549 break; 1550 case SpecialSubKind::istream: 1551 OB += "std::basic_istream<char, std::char_traits<char> >"; 1552 break; 1553 case SpecialSubKind::ostream: 1554 OB += "std::basic_ostream<char, std::char_traits<char> >"; 1555 break; 1556 case SpecialSubKind::iostream: 1557 OB += "std::basic_iostream<char, std::char_traits<char> >"; 1558 break; 1559 } 1560 } 1561 }; 1562 1563 class SpecialSubstitution final : public Node { 1564 public: 1565 SpecialSubKind SSK; 1566 1567 SpecialSubstitution(SpecialSubKind SSK_) 1568 : Node(KSpecialSubstitution), SSK(SSK_) {} 1569 1570 template<typename Fn> void match(Fn F) const { F(SSK); } 1571 1572 StringView getBaseName() const override { 1573 switch (SSK) { 1574 case SpecialSubKind::allocator: 1575 return StringView("allocator"); 1576 case SpecialSubKind::basic_string: 1577 return StringView("basic_string"); 1578 case SpecialSubKind::string: 1579 return StringView("string"); 1580 case SpecialSubKind::istream: 1581 return StringView("istream"); 1582 case SpecialSubKind::ostream: 1583 return StringView("ostream"); 1584 case SpecialSubKind::iostream: 1585 return StringView("iostream"); 1586 } 1587 DEMANGLE_UNREACHABLE; 1588 } 1589 1590 void printLeft(OutputBuffer &OB) const override { 1591 switch (SSK) { 1592 case SpecialSubKind::allocator: 1593 OB += "std::allocator"; 1594 break; 1595 case SpecialSubKind::basic_string: 1596 OB += "std::basic_string"; 1597 break; 1598 case SpecialSubKind::string: 1599 OB += "std::string"; 1600 break; 1601 case SpecialSubKind::istream: 1602 OB += "std::istream"; 1603 break; 1604 case SpecialSubKind::ostream: 1605 OB += "std::ostream"; 1606 break; 1607 case SpecialSubKind::iostream: 1608 OB += "std::iostream"; 1609 break; 1610 } 1611 } 1612 }; 1613 1614 class CtorDtorName final : public Node { 1615 const Node *Basename; 1616 const bool IsDtor; 1617 const int Variant; 1618 1619 public: 1620 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_) 1621 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_), 1622 Variant(Variant_) {} 1623 1624 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); } 1625 1626 void printLeft(OutputBuffer &OB) const override { 1627 if (IsDtor) 1628 OB += "~"; 1629 OB += Basename->getBaseName(); 1630 } 1631 }; 1632 1633 class DtorName : public Node { 1634 const Node *Base; 1635 1636 public: 1637 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {} 1638 1639 template<typename Fn> void match(Fn F) const { F(Base); } 1640 1641 void printLeft(OutputBuffer &OB) const override { 1642 OB += "~"; 1643 Base->printLeft(OB); 1644 } 1645 }; 1646 1647 class UnnamedTypeName : public Node { 1648 const StringView Count; 1649 1650 public: 1651 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {} 1652 1653 template<typename Fn> void match(Fn F) const { F(Count); } 1654 1655 void printLeft(OutputBuffer &OB) const override { 1656 OB += "'unnamed"; 1657 OB += Count; 1658 OB += "\'"; 1659 } 1660 }; 1661 1662 class ClosureTypeName : public Node { 1663 NodeArray TemplateParams; 1664 NodeArray Params; 1665 StringView Count; 1666 1667 public: 1668 ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_, 1669 StringView Count_) 1670 : Node(KClosureTypeName), TemplateParams(TemplateParams_), 1671 Params(Params_), Count(Count_) {} 1672 1673 template<typename Fn> void match(Fn F) const { 1674 F(TemplateParams, Params, Count); 1675 } 1676 1677 void printDeclarator(OutputBuffer &OB) const { 1678 if (!TemplateParams.empty()) { 1679 ScopedOverride<unsigned> LT(OB.GtIsGt, 0); 1680 OB += "<"; 1681 TemplateParams.printWithComma(OB); 1682 OB += ">"; 1683 } 1684 OB.printOpen(); 1685 Params.printWithComma(OB); 1686 OB.printClose(); 1687 } 1688 1689 void printLeft(OutputBuffer &OB) const override { 1690 OB += "\'lambda"; 1691 OB += Count; 1692 OB += "\'"; 1693 printDeclarator(OB); 1694 } 1695 }; 1696 1697 class StructuredBindingName : public Node { 1698 NodeArray Bindings; 1699 public: 1700 StructuredBindingName(NodeArray Bindings_) 1701 : Node(KStructuredBindingName), Bindings(Bindings_) {} 1702 1703 template<typename Fn> void match(Fn F) const { F(Bindings); } 1704 1705 void printLeft(OutputBuffer &OB) const override { 1706 OB.printOpen('['); 1707 Bindings.printWithComma(OB); 1708 OB.printClose(']'); 1709 } 1710 }; 1711 1712 // -- Expression Nodes -- 1713 1714 class BinaryExpr : public Node { 1715 const Node *LHS; 1716 const StringView InfixOperator; 1717 const Node *RHS; 1718 1719 public: 1720 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_, 1721 Prec Prec_) 1722 : Node(KBinaryExpr, Prec_), LHS(LHS_), InfixOperator(InfixOperator_), 1723 RHS(RHS_) {} 1724 1725 template <typename Fn> void match(Fn F) const { 1726 F(LHS, InfixOperator, RHS, getPrecedence()); 1727 } 1728 1729 void printLeft(OutputBuffer &OB) const override { 1730 bool ParenAll = OB.isGtInsideTemplateArgs() && 1731 (InfixOperator == ">" || InfixOperator == ">>"); 1732 if (ParenAll) 1733 OB.printOpen(); 1734 // Assignment is right associative, with special LHS precedence. 1735 bool IsAssign = getPrecedence() == Prec::Assign; 1736 LHS->printAsOperand(OB, IsAssign ? Prec::OrIf : getPrecedence(), !IsAssign); 1737 // No space before comma operator 1738 if (!(InfixOperator == ",")) 1739 OB += " "; 1740 OB += InfixOperator; 1741 OB += " "; 1742 RHS->printAsOperand(OB, getPrecedence(), IsAssign); 1743 if (ParenAll) 1744 OB.printClose(); 1745 } 1746 }; 1747 1748 class ArraySubscriptExpr : public Node { 1749 const Node *Op1; 1750 const Node *Op2; 1751 1752 public: 1753 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_, Prec Prec_) 1754 : Node(KArraySubscriptExpr, Prec_), Op1(Op1_), Op2(Op2_) {} 1755 1756 template <typename Fn> void match(Fn F) const { 1757 F(Op1, Op2, getPrecedence()); 1758 } 1759 1760 void printLeft(OutputBuffer &OB) const override { 1761 Op1->printAsOperand(OB, getPrecedence()); 1762 OB.printOpen('['); 1763 Op2->printAsOperand(OB); 1764 OB.printClose(']'); 1765 } 1766 }; 1767 1768 class PostfixExpr : public Node { 1769 const Node *Child; 1770 const StringView Operator; 1771 1772 public: 1773 PostfixExpr(const Node *Child_, StringView Operator_, Prec Prec_) 1774 : Node(KPostfixExpr, Prec_), Child(Child_), Operator(Operator_) {} 1775 1776 template <typename Fn> void match(Fn F) const { 1777 F(Child, Operator, getPrecedence()); 1778 } 1779 1780 void printLeft(OutputBuffer &OB) const override { 1781 Child->printAsOperand(OB, getPrecedence(), true); 1782 OB += Operator; 1783 } 1784 }; 1785 1786 class ConditionalExpr : public Node { 1787 const Node *Cond; 1788 const Node *Then; 1789 const Node *Else; 1790 1791 public: 1792 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_, 1793 Prec Prec_) 1794 : Node(KConditionalExpr, Prec_), Cond(Cond_), Then(Then_), Else(Else_) {} 1795 1796 template <typename Fn> void match(Fn F) const { 1797 F(Cond, Then, Else, getPrecedence()); 1798 } 1799 1800 void printLeft(OutputBuffer &OB) const override { 1801 Cond->printAsOperand(OB, getPrecedence()); 1802 OB += " ? "; 1803 Then->printAsOperand(OB); 1804 OB += " : "; 1805 Else->printAsOperand(OB, Prec::Assign, true); 1806 } 1807 }; 1808 1809 class MemberExpr : public Node { 1810 const Node *LHS; 1811 const StringView Kind; 1812 const Node *RHS; 1813 1814 public: 1815 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_, Prec Prec_) 1816 : Node(KMemberExpr, Prec_), LHS(LHS_), Kind(Kind_), RHS(RHS_) {} 1817 1818 template <typename Fn> void match(Fn F) const { 1819 F(LHS, Kind, RHS, getPrecedence()); 1820 } 1821 1822 void printLeft(OutputBuffer &OB) const override { 1823 LHS->printAsOperand(OB, getPrecedence(), true); 1824 OB += Kind; 1825 RHS->printAsOperand(OB, getPrecedence(), false); 1826 } 1827 }; 1828 1829 class SubobjectExpr : public Node { 1830 const Node *Type; 1831 const Node *SubExpr; 1832 StringView Offset; 1833 NodeArray UnionSelectors; 1834 bool OnePastTheEnd; 1835 1836 public: 1837 SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_, 1838 NodeArray UnionSelectors_, bool OnePastTheEnd_) 1839 : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_), 1840 UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {} 1841 1842 template<typename Fn> void match(Fn F) const { 1843 F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd); 1844 } 1845 1846 void printLeft(OutputBuffer &OB) const override { 1847 SubExpr->print(OB); 1848 OB += ".<"; 1849 Type->print(OB); 1850 OB += " at offset "; 1851 if (Offset.empty()) { 1852 OB += "0"; 1853 } else if (Offset[0] == 'n') { 1854 OB += "-"; 1855 OB += Offset.dropFront(); 1856 } else { 1857 OB += Offset; 1858 } 1859 OB += ">"; 1860 } 1861 }; 1862 1863 class EnclosingExpr : public Node { 1864 const StringView Prefix; 1865 const Node *Infix; 1866 const StringView Postfix; 1867 1868 public: 1869 EnclosingExpr(StringView Prefix_, const Node *Infix_, 1870 Prec Prec_ = Prec::Primary) 1871 : Node(KEnclosingExpr, Prec_), Prefix(Prefix_), Infix(Infix_) {} 1872 1873 template <typename Fn> void match(Fn F) const { 1874 F(Prefix, Infix, getPrecedence()); 1875 } 1876 1877 void printLeft(OutputBuffer &OB) const override { 1878 OB += Prefix; 1879 OB.printOpen(); 1880 Infix->print(OB); 1881 OB.printClose(); 1882 OB += Postfix; 1883 } 1884 }; 1885 1886 class CastExpr : public Node { 1887 // cast_kind<to>(from) 1888 const StringView CastKind; 1889 const Node *To; 1890 const Node *From; 1891 1892 public: 1893 CastExpr(StringView CastKind_, const Node *To_, const Node *From_, Prec Prec_) 1894 : Node(KCastExpr, Prec_), CastKind(CastKind_), To(To_), From(From_) {} 1895 1896 template <typename Fn> void match(Fn F) const { 1897 F(CastKind, To, From, getPrecedence()); 1898 } 1899 1900 void printLeft(OutputBuffer &OB) const override { 1901 OB += CastKind; 1902 { 1903 ScopedOverride<unsigned> LT(OB.GtIsGt, 0); 1904 OB += "<"; 1905 To->printLeft(OB); 1906 if (OB.back() == '>') 1907 OB += " "; 1908 OB += ">"; 1909 } 1910 OB.printOpen(); 1911 From->printAsOperand(OB); 1912 OB.printClose(); 1913 } 1914 }; 1915 1916 class SizeofParamPackExpr : public Node { 1917 const Node *Pack; 1918 1919 public: 1920 SizeofParamPackExpr(const Node *Pack_) 1921 : Node(KSizeofParamPackExpr), Pack(Pack_) {} 1922 1923 template<typename Fn> void match(Fn F) const { F(Pack); } 1924 1925 void printLeft(OutputBuffer &OB) const override { 1926 OB += "sizeof..."; 1927 OB.printOpen(); 1928 ParameterPackExpansion PPE(Pack); 1929 PPE.printLeft(OB); 1930 OB.printClose(); 1931 } 1932 }; 1933 1934 class CallExpr : public Node { 1935 const Node *Callee; 1936 NodeArray Args; 1937 1938 public: 1939 CallExpr(const Node *Callee_, NodeArray Args_, Prec Prec_) 1940 : Node(KCallExpr, Prec_), Callee(Callee_), Args(Args_) {} 1941 1942 template <typename Fn> void match(Fn F) const { 1943 F(Callee, Args, getPrecedence()); 1944 } 1945 1946 void printLeft(OutputBuffer &OB) const override { 1947 Callee->print(OB); 1948 OB.printOpen(); 1949 Args.printWithComma(OB); 1950 OB.printClose(); 1951 } 1952 }; 1953 1954 class NewExpr : public Node { 1955 // new (expr_list) type(init_list) 1956 NodeArray ExprList; 1957 Node *Type; 1958 NodeArray InitList; 1959 bool IsGlobal; // ::operator new ? 1960 bool IsArray; // new[] ? 1961 public: 1962 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_, 1963 bool IsArray_, Prec Prec_) 1964 : Node(KNewExpr, Prec_), ExprList(ExprList_), Type(Type_), 1965 InitList(InitList_), IsGlobal(IsGlobal_), IsArray(IsArray_) {} 1966 1967 template<typename Fn> void match(Fn F) const { 1968 F(ExprList, Type, InitList, IsGlobal, IsArray, getPrecedence()); 1969 } 1970 1971 void printLeft(OutputBuffer &OB) const override { 1972 if (IsGlobal) 1973 OB += "::"; 1974 OB += "new"; 1975 if (IsArray) 1976 OB += "[]"; 1977 if (!ExprList.empty()) { 1978 OB.printOpen(); 1979 ExprList.printWithComma(OB); 1980 OB.printClose(); 1981 } 1982 OB += " "; 1983 Type->print(OB); 1984 if (!InitList.empty()) { 1985 OB.printOpen(); 1986 InitList.printWithComma(OB); 1987 OB.printClose(); 1988 } 1989 } 1990 }; 1991 1992 class DeleteExpr : public Node { 1993 Node *Op; 1994 bool IsGlobal; 1995 bool IsArray; 1996 1997 public: 1998 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_, Prec Prec_) 1999 : Node(KDeleteExpr, Prec_), Op(Op_), IsGlobal(IsGlobal_), 2000 IsArray(IsArray_) {} 2001 2002 template <typename Fn> void match(Fn F) const { 2003 F(Op, IsGlobal, IsArray, getPrecedence()); 2004 } 2005 2006 void printLeft(OutputBuffer &OB) const override { 2007 if (IsGlobal) 2008 OB += "::"; 2009 OB += "delete"; 2010 if (IsArray) 2011 OB += "[]"; 2012 OB += ' '; 2013 Op->print(OB); 2014 } 2015 }; 2016 2017 class PrefixExpr : public Node { 2018 StringView Prefix; 2019 Node *Child; 2020 2021 public: 2022 PrefixExpr(StringView Prefix_, Node *Child_, Prec Prec_) 2023 : Node(KPrefixExpr, Prec_), Prefix(Prefix_), Child(Child_) {} 2024 2025 template <typename Fn> void match(Fn F) const { 2026 F(Prefix, Child, getPrecedence()); 2027 } 2028 2029 void printLeft(OutputBuffer &OB) const override { 2030 OB += Prefix; 2031 Child->printAsOperand(OB, getPrecedence()); 2032 } 2033 }; 2034 2035 class FunctionParam : public Node { 2036 StringView Number; 2037 2038 public: 2039 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {} 2040 2041 template<typename Fn> void match(Fn F) const { F(Number); } 2042 2043 void printLeft(OutputBuffer &OB) const override { 2044 OB += "fp"; 2045 OB += Number; 2046 } 2047 }; 2048 2049 class ConversionExpr : public Node { 2050 const Node *Type; 2051 NodeArray Expressions; 2052 2053 public: 2054 ConversionExpr(const Node *Type_, NodeArray Expressions_, Prec Prec_) 2055 : Node(KConversionExpr, Prec_), Type(Type_), Expressions(Expressions_) {} 2056 2057 template <typename Fn> void match(Fn F) const { 2058 F(Type, Expressions, getPrecedence()); 2059 } 2060 2061 void printLeft(OutputBuffer &OB) const override { 2062 OB.printOpen(); 2063 Type->print(OB); 2064 OB.printClose(); 2065 OB.printOpen(); 2066 Expressions.printWithComma(OB); 2067 OB.printClose(); 2068 } 2069 }; 2070 2071 class PointerToMemberConversionExpr : public Node { 2072 const Node *Type; 2073 const Node *SubExpr; 2074 StringView Offset; 2075 2076 public: 2077 PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_, 2078 StringView Offset_, Prec Prec_) 2079 : Node(KPointerToMemberConversionExpr, Prec_), Type(Type_), 2080 SubExpr(SubExpr_), Offset(Offset_) {} 2081 2082 template <typename Fn> void match(Fn F) const { 2083 F(Type, SubExpr, Offset, getPrecedence()); 2084 } 2085 2086 void printLeft(OutputBuffer &OB) const override { 2087 OB.printOpen(); 2088 Type->print(OB); 2089 OB.printClose(); 2090 OB.printOpen(); 2091 SubExpr->print(OB); 2092 OB.printClose(); 2093 } 2094 }; 2095 2096 class InitListExpr : public Node { 2097 const Node *Ty; 2098 NodeArray Inits; 2099 public: 2100 InitListExpr(const Node *Ty_, NodeArray Inits_) 2101 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {} 2102 2103 template<typename Fn> void match(Fn F) const { F(Ty, Inits); } 2104 2105 void printLeft(OutputBuffer &OB) const override { 2106 if (Ty) 2107 Ty->print(OB); 2108 OB += '{'; 2109 Inits.printWithComma(OB); 2110 OB += '}'; 2111 } 2112 }; 2113 2114 class BracedExpr : public Node { 2115 const Node *Elem; 2116 const Node *Init; 2117 bool IsArray; 2118 public: 2119 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_) 2120 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {} 2121 2122 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); } 2123 2124 void printLeft(OutputBuffer &OB) const override { 2125 if (IsArray) { 2126 OB += '['; 2127 Elem->print(OB); 2128 OB += ']'; 2129 } else { 2130 OB += '.'; 2131 Elem->print(OB); 2132 } 2133 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr) 2134 OB += " = "; 2135 Init->print(OB); 2136 } 2137 }; 2138 2139 class BracedRangeExpr : public Node { 2140 const Node *First; 2141 const Node *Last; 2142 const Node *Init; 2143 public: 2144 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_) 2145 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {} 2146 2147 template<typename Fn> void match(Fn F) const { F(First, Last, Init); } 2148 2149 void printLeft(OutputBuffer &OB) const override { 2150 OB += '['; 2151 First->print(OB); 2152 OB += " ... "; 2153 Last->print(OB); 2154 OB += ']'; 2155 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr) 2156 OB += " = "; 2157 Init->print(OB); 2158 } 2159 }; 2160 2161 class FoldExpr : public Node { 2162 const Node *Pack, *Init; 2163 StringView OperatorName; 2164 bool IsLeftFold; 2165 2166 public: 2167 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_, 2168 const Node *Init_) 2169 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_), 2170 IsLeftFold(IsLeftFold_) {} 2171 2172 template<typename Fn> void match(Fn F) const { 2173 F(IsLeftFold, OperatorName, Pack, Init); 2174 } 2175 2176 void printLeft(OutputBuffer &OB) const override { 2177 auto PrintPack = [&] { 2178 OB.printOpen(); 2179 ParameterPackExpansion(Pack).print(OB); 2180 OB.printClose(); 2181 }; 2182 2183 OB.printOpen(); 2184 // Either '[init op ]... op pack' or 'pack op ...[ op init]' 2185 // Refactored to '[(init|pack) op ]...[ op (pack|init)]' 2186 // Fold expr operands are cast-expressions 2187 if (!IsLeftFold || Init != nullptr) { 2188 // '(init|pack) op ' 2189 if (IsLeftFold) 2190 Init->printAsOperand(OB, Prec::Cast, true); 2191 else 2192 PrintPack(); 2193 OB << " " << OperatorName << " "; 2194 } 2195 OB << "..."; 2196 if (IsLeftFold || Init != nullptr) { 2197 // ' op (init|pack)' 2198 OB << " " << OperatorName << " "; 2199 if (IsLeftFold) 2200 PrintPack(); 2201 else 2202 Init->printAsOperand(OB, Prec::Cast, true); 2203 } 2204 OB.printClose(); 2205 } 2206 }; 2207 2208 class ThrowExpr : public Node { 2209 const Node *Op; 2210 2211 public: 2212 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {} 2213 2214 template<typename Fn> void match(Fn F) const { F(Op); } 2215 2216 void printLeft(OutputBuffer &OB) const override { 2217 OB += "throw "; 2218 Op->print(OB); 2219 } 2220 }; 2221 2222 class BoolExpr : public Node { 2223 bool Value; 2224 2225 public: 2226 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {} 2227 2228 template<typename Fn> void match(Fn F) const { F(Value); } 2229 2230 void printLeft(OutputBuffer &OB) const override { 2231 OB += Value ? StringView("true") : StringView("false"); 2232 } 2233 }; 2234 2235 class StringLiteral : public Node { 2236 const Node *Type; 2237 2238 public: 2239 StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {} 2240 2241 template<typename Fn> void match(Fn F) const { F(Type); } 2242 2243 void printLeft(OutputBuffer &OB) const override { 2244 OB += "\"<"; 2245 Type->print(OB); 2246 OB += ">\""; 2247 } 2248 }; 2249 2250 class LambdaExpr : public Node { 2251 const Node *Type; 2252 2253 public: 2254 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {} 2255 2256 template<typename Fn> void match(Fn F) const { F(Type); } 2257 2258 void printLeft(OutputBuffer &OB) const override { 2259 OB += "[]"; 2260 if (Type->getKind() == KClosureTypeName) 2261 static_cast<const ClosureTypeName *>(Type)->printDeclarator(OB); 2262 OB += "{...}"; 2263 } 2264 }; 2265 2266 class EnumLiteral : public Node { 2267 // ty(integer) 2268 const Node *Ty; 2269 StringView Integer; 2270 2271 public: 2272 EnumLiteral(const Node *Ty_, StringView Integer_) 2273 : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {} 2274 2275 template<typename Fn> void match(Fn F) const { F(Ty, Integer); } 2276 2277 void printLeft(OutputBuffer &OB) const override { 2278 OB.printOpen(); 2279 Ty->print(OB); 2280 OB.printClose(); 2281 2282 if (Integer[0] == 'n') 2283 OB << "-" << Integer.dropFront(1); 2284 else 2285 OB << Integer; 2286 } 2287 }; 2288 2289 class IntegerLiteral : public Node { 2290 StringView Type; 2291 StringView Value; 2292 2293 public: 2294 IntegerLiteral(StringView Type_, StringView Value_) 2295 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {} 2296 2297 template<typename Fn> void match(Fn F) const { F(Type, Value); } 2298 2299 void printLeft(OutputBuffer &OB) const override { 2300 if (Type.size() > 3) { 2301 OB.printOpen(); 2302 OB += Type; 2303 OB.printClose(); 2304 } 2305 2306 if (Value[0] == 'n') { 2307 OB += '-'; 2308 OB += Value.dropFront(1); 2309 } else 2310 OB += Value; 2311 2312 if (Type.size() <= 3) 2313 OB += Type; 2314 } 2315 }; 2316 2317 template <class Float> struct FloatData; 2318 2319 namespace float_literal_impl { 2320 constexpr Node::Kind getFloatLiteralKind(float *) { 2321 return Node::KFloatLiteral; 2322 } 2323 constexpr Node::Kind getFloatLiteralKind(double *) { 2324 return Node::KDoubleLiteral; 2325 } 2326 constexpr Node::Kind getFloatLiteralKind(long double *) { 2327 return Node::KLongDoubleLiteral; 2328 } 2329 } 2330 2331 template <class Float> class FloatLiteralImpl : public Node { 2332 const StringView Contents; 2333 2334 static constexpr Kind KindForClass = 2335 float_literal_impl::getFloatLiteralKind((Float *)nullptr); 2336 2337 public: 2338 FloatLiteralImpl(StringView Contents_) 2339 : Node(KindForClass), Contents(Contents_) {} 2340 2341 template<typename Fn> void match(Fn F) const { F(Contents); } 2342 2343 void printLeft(OutputBuffer &OB) const override { 2344 const char *first = Contents.begin(); 2345 const char *last = Contents.end() + 1; 2346 2347 const size_t N = FloatData<Float>::mangled_size; 2348 if (static_cast<std::size_t>(last - first) > N) { 2349 last = first + N; 2350 union { 2351 Float value; 2352 char buf[sizeof(Float)]; 2353 }; 2354 const char *t = first; 2355 char *e = buf; 2356 for (; t != last; ++t, ++e) { 2357 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0') 2358 : static_cast<unsigned>(*t - 'a' + 10); 2359 ++t; 2360 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0') 2361 : static_cast<unsigned>(*t - 'a' + 10); 2362 *e = static_cast<char>((d1 << 4) + d0); 2363 } 2364 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 2365 std::reverse(buf, e); 2366 #endif 2367 char num[FloatData<Float>::max_demangled_size] = {0}; 2368 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value); 2369 OB += StringView(num, num + n); 2370 } 2371 } 2372 }; 2373 2374 using FloatLiteral = FloatLiteralImpl<float>; 2375 using DoubleLiteral = FloatLiteralImpl<double>; 2376 using LongDoubleLiteral = FloatLiteralImpl<long double>; 2377 2378 /// Visit the node. Calls \c F(P), where \c P is the node cast to the 2379 /// appropriate derived class. 2380 template<typename Fn> 2381 void Node::visit(Fn F) const { 2382 switch (K) { 2383 #define NODE(X) \ 2384 case K##X: \ 2385 return F(static_cast<const X *>(this)); 2386 #include "ItaniumNodes.def" 2387 } 2388 assert(0 && "unknown mangling node kind"); 2389 } 2390 2391 /// Determine the kind of a node from its type. 2392 template<typename NodeT> struct NodeKind; 2393 #define NODE(X) \ 2394 template <> struct NodeKind<X> { \ 2395 static constexpr Node::Kind Kind = Node::K##X; \ 2396 static constexpr const char *name() { return #X; } \ 2397 }; 2398 #include "ItaniumNodes.def" 2399 2400 template <typename Derived, typename Alloc> struct AbstractManglingParser { 2401 const char *First; 2402 const char *Last; 2403 2404 // Name stack, this is used by the parser to hold temporary names that were 2405 // parsed. The parser collapses multiple names into new nodes to construct 2406 // the AST. Once the parser is finished, names.size() == 1. 2407 PODSmallVector<Node *, 32> Names; 2408 2409 // Substitution table. Itanium supports name substitutions as a means of 2410 // compression. The string "S42_" refers to the 44nd entry (base-36) in this 2411 // table. 2412 PODSmallVector<Node *, 32> Subs; 2413 2414 using TemplateParamList = PODSmallVector<Node *, 8>; 2415 2416 class ScopedTemplateParamList { 2417 AbstractManglingParser *Parser; 2418 size_t OldNumTemplateParamLists; 2419 TemplateParamList Params; 2420 2421 public: 2422 ScopedTemplateParamList(AbstractManglingParser *TheParser) 2423 : Parser(TheParser), 2424 OldNumTemplateParamLists(TheParser->TemplateParams.size()) { 2425 Parser->TemplateParams.push_back(&Params); 2426 } 2427 ~ScopedTemplateParamList() { 2428 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists); 2429 Parser->TemplateParams.dropBack(OldNumTemplateParamLists); 2430 } 2431 }; 2432 2433 // Template parameter table. Like the above, but referenced like "T42_". 2434 // This has a smaller size compared to Subs and Names because it can be 2435 // stored on the stack. 2436 TemplateParamList OuterTemplateParams; 2437 2438 // Lists of template parameters indexed by template parameter depth, 2439 // referenced like "TL2_4_". If nonempty, element 0 is always 2440 // OuterTemplateParams; inner elements are always template parameter lists of 2441 // lambda expressions. For a generic lambda with no explicit template 2442 // parameter list, the corresponding parameter list pointer will be null. 2443 PODSmallVector<TemplateParamList *, 4> TemplateParams; 2444 2445 // Set of unresolved forward <template-param> references. These can occur in a 2446 // conversion operator's type, and are resolved in the enclosing <encoding>. 2447 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs; 2448 2449 bool TryToParseTemplateArgs = true; 2450 bool PermitForwardTemplateReferences = false; 2451 size_t ParsingLambdaParamsAtLevel = (size_t)-1; 2452 2453 unsigned NumSyntheticTemplateParameters[3] = {}; 2454 2455 Alloc ASTAllocator; 2456 2457 AbstractManglingParser(const char *First_, const char *Last_) 2458 : First(First_), Last(Last_) {} 2459 2460 Derived &getDerived() { return static_cast<Derived &>(*this); } 2461 2462 void reset(const char *First_, const char *Last_) { 2463 First = First_; 2464 Last = Last_; 2465 Names.clear(); 2466 Subs.clear(); 2467 TemplateParams.clear(); 2468 ParsingLambdaParamsAtLevel = (size_t)-1; 2469 TryToParseTemplateArgs = true; 2470 PermitForwardTemplateReferences = false; 2471 for (int I = 0; I != 3; ++I) 2472 NumSyntheticTemplateParameters[I] = 0; 2473 ASTAllocator.reset(); 2474 } 2475 2476 template <class T, class... Args> Node *make(Args &&... args) { 2477 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...); 2478 } 2479 2480 template <class It> NodeArray makeNodeArray(It begin, It end) { 2481 size_t sz = static_cast<size_t>(end - begin); 2482 void *mem = ASTAllocator.allocateNodeArray(sz); 2483 Node **data = new (mem) Node *[sz]; 2484 std::copy(begin, end, data); 2485 return NodeArray(data, sz); 2486 } 2487 2488 NodeArray popTrailingNodeArray(size_t FromPosition) { 2489 assert(FromPosition <= Names.size()); 2490 NodeArray res = 2491 makeNodeArray(Names.begin() + (long)FromPosition, Names.end()); 2492 Names.dropBack(FromPosition); 2493 return res; 2494 } 2495 2496 bool consumeIf(StringView S) { 2497 if (StringView(First, Last).startsWith(S)) { 2498 First += S.size(); 2499 return true; 2500 } 2501 return false; 2502 } 2503 2504 bool consumeIf(char C) { 2505 if (First != Last && *First == C) { 2506 ++First; 2507 return true; 2508 } 2509 return false; 2510 } 2511 2512 char consume() { return First != Last ? *First++ : '\0'; } 2513 2514 char look(unsigned Lookahead = 0) const { 2515 if (static_cast<size_t>(Last - First) <= Lookahead) 2516 return '\0'; 2517 return First[Lookahead]; 2518 } 2519 2520 size_t numLeft() const { return static_cast<size_t>(Last - First); } 2521 2522 StringView parseNumber(bool AllowNegative = false); 2523 Qualifiers parseCVQualifiers(); 2524 bool parsePositiveInteger(size_t *Out); 2525 StringView parseBareSourceName(); 2526 2527 bool parseSeqId(size_t *Out); 2528 Node *parseSubstitution(); 2529 Node *parseTemplateParam(); 2530 Node *parseTemplateParamDecl(); 2531 Node *parseTemplateArgs(bool TagTemplates = false); 2532 Node *parseTemplateArg(); 2533 2534 /// Parse the <expr> production. 2535 Node *parseExpr(); 2536 Node *parsePrefixExpr(StringView Kind, Node::Prec Prec); 2537 Node *parseBinaryExpr(StringView Kind, Node::Prec Prec); 2538 Node *parseIntegerLiteral(StringView Lit); 2539 Node *parseExprPrimary(); 2540 template <class Float> Node *parseFloatingLiteral(); 2541 Node *parseFunctionParam(); 2542 Node *parseConversionExpr(); 2543 Node *parseBracedExpr(); 2544 Node *parseFoldExpr(); 2545 Node *parsePointerToMemberConversionExpr(Node::Prec Prec); 2546 Node *parseSubobjectExpr(); 2547 2548 /// Parse the <type> production. 2549 Node *parseType(); 2550 Node *parseFunctionType(); 2551 Node *parseVectorType(); 2552 Node *parseDecltype(); 2553 Node *parseArrayType(); 2554 Node *parsePointerToMemberType(); 2555 Node *parseClassEnumType(); 2556 Node *parseQualifiedType(); 2557 2558 Node *parseEncoding(); 2559 bool parseCallOffset(); 2560 Node *parseSpecialName(); 2561 2562 /// Holds some extra information about a <name> that is being parsed. This 2563 /// information is only pertinent if the <name> refers to an <encoding>. 2564 struct NameState { 2565 bool CtorDtorConversion = false; 2566 bool EndsWithTemplateArgs = false; 2567 Qualifiers CVQualifiers = QualNone; 2568 FunctionRefQual ReferenceQualifier = FrefQualNone; 2569 size_t ForwardTemplateRefsBegin; 2570 2571 NameState(AbstractManglingParser *Enclosing) 2572 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {} 2573 }; 2574 2575 bool resolveForwardTemplateRefs(NameState &State) { 2576 size_t I = State.ForwardTemplateRefsBegin; 2577 size_t E = ForwardTemplateRefs.size(); 2578 for (; I < E; ++I) { 2579 size_t Idx = ForwardTemplateRefs[I]->Index; 2580 if (TemplateParams.empty() || !TemplateParams[0] || 2581 Idx >= TemplateParams[0]->size()) 2582 return true; 2583 ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx]; 2584 } 2585 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin); 2586 return false; 2587 } 2588 2589 /// Parse the <name> production> 2590 Node *parseName(NameState *State = nullptr); 2591 Node *parseLocalName(NameState *State); 2592 Node *parseOperatorName(NameState *State); 2593 bool parseModuleNameOpt(ModuleName *&Module); 2594 Node *parseUnqualifiedName(NameState *State, Node *Scope, ModuleName *Module); 2595 Node *parseUnnamedTypeName(NameState *State); 2596 Node *parseSourceName(NameState *State); 2597 Node *parseUnscopedName(NameState *State, bool *isSubstName); 2598 Node *parseNestedName(NameState *State); 2599 Node *parseCtorDtorName(Node *&SoFar, NameState *State); 2600 2601 Node *parseAbiTags(Node *N); 2602 2603 struct OperatorInfo { 2604 enum OIKind : unsigned char { 2605 Prefix, // Prefix unary: @ expr 2606 Postfix, // Postfix unary: expr @ 2607 Binary, // Binary: lhs @ rhs 2608 Array, // Array index: lhs [ rhs ] 2609 Member, // Member access: lhs @ rhs 2610 New, // New 2611 Del, // Delete 2612 Call, // Function call: expr (expr*) 2613 CCast, // C cast: (type)expr 2614 Conditional, // Conditional: expr ? expr : expr 2615 NameOnly, // Overload only, not allowed in expression. 2616 // Below do not have operator names 2617 NamedCast, // Named cast, @<type>(expr) 2618 OfIdOp, // alignof, sizeof, typeid 2619 2620 Unnameable = NamedCast, 2621 }; 2622 char Enc[2]; // Encoding 2623 OIKind Kind; // Kind of operator 2624 bool Flag : 1; // Entry-specific flag 2625 Node::Prec Prec : 7; // Precedence 2626 const char *Name; // Spelling 2627 2628 public: 2629 constexpr OperatorInfo(const char (&E)[3], OIKind K, bool F, Node::Prec P, 2630 const char *N) 2631 : Enc{E[0], E[1]}, Kind{K}, Flag{F}, Prec{P}, Name{N} {} 2632 2633 public: 2634 bool operator<(const OperatorInfo &Other) const { 2635 return *this < Other.Enc; 2636 } 2637 bool operator<(const char *Peek) const { 2638 return Enc[0] < Peek[0] || (Enc[0] == Peek[0] && Enc[1] < Peek[1]); 2639 } 2640 bool operator==(const char *Peek) const { 2641 return Enc[0] == Peek[0] && Enc[1] == Peek[1]; 2642 } 2643 bool operator!=(const char *Peek) const { return !this->operator==(Peek); } 2644 2645 public: 2646 StringView getSymbol() const { 2647 StringView Res = Name; 2648 if (Kind < Unnameable) { 2649 assert(Res.startsWith("operator") && 2650 "operator name does not start with 'operator'"); 2651 Res = Res.dropFront(sizeof("operator") - 1); 2652 Res.consumeFront(' '); 2653 } 2654 return Res; 2655 } 2656 StringView getName() const { return Name; } 2657 OIKind getKind() const { return Kind; } 2658 bool getFlag() const { return Flag; } 2659 Node::Prec getPrecedence() const { return Prec; } 2660 }; 2661 const OperatorInfo *parseOperatorEncoding(); 2662 2663 /// Parse the <unresolved-name> production. 2664 Node *parseUnresolvedName(bool Global); 2665 Node *parseSimpleId(); 2666 Node *parseBaseUnresolvedName(); 2667 Node *parseUnresolvedType(); 2668 Node *parseDestructorName(); 2669 2670 /// Top-level entry point into the parser. 2671 Node *parse(); 2672 }; 2673 2674 const char* parse_discriminator(const char* first, const char* last); 2675 2676 // <name> ::= <nested-name> // N 2677 // ::= <local-name> # See Scope Encoding below // Z 2678 // ::= <unscoped-template-name> <template-args> 2679 // ::= <unscoped-name> 2680 // 2681 // <unscoped-template-name> ::= <unscoped-name> 2682 // ::= <substitution> 2683 template <typename Derived, typename Alloc> 2684 Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) { 2685 if (look() == 'N') 2686 return getDerived().parseNestedName(State); 2687 if (look() == 'Z') 2688 return getDerived().parseLocalName(State); 2689 2690 Node *Result = nullptr; 2691 bool IsSubst = false; 2692 2693 Result = getDerived().parseUnscopedName(State, &IsSubst); 2694 if (!Result) 2695 return nullptr; 2696 2697 if (look() == 'I') { 2698 // ::= <unscoped-template-name> <template-args> 2699 if (!IsSubst) 2700 // An unscoped-template-name is substitutable. 2701 Subs.push_back(Result); 2702 Node *TA = getDerived().parseTemplateArgs(State != nullptr); 2703 if (TA == nullptr) 2704 return nullptr; 2705 if (State) 2706 State->EndsWithTemplateArgs = true; 2707 Result = make<NameWithTemplateArgs>(Result, TA); 2708 } else if (IsSubst) { 2709 // The substitution case must be followed by <template-args>. 2710 return nullptr; 2711 } 2712 2713 return Result; 2714 } 2715 2716 // <local-name> := Z <function encoding> E <entity name> [<discriminator>] 2717 // := Z <function encoding> E s [<discriminator>] 2718 // := Z <function encoding> Ed [ <parameter number> ] _ <entity name> 2719 template <typename Derived, typename Alloc> 2720 Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) { 2721 if (!consumeIf('Z')) 2722 return nullptr; 2723 Node *Encoding = getDerived().parseEncoding(); 2724 if (Encoding == nullptr || !consumeIf('E')) 2725 return nullptr; 2726 2727 if (consumeIf('s')) { 2728 First = parse_discriminator(First, Last); 2729 auto *StringLitName = make<NameType>("string literal"); 2730 if (!StringLitName) 2731 return nullptr; 2732 return make<LocalName>(Encoding, StringLitName); 2733 } 2734 2735 if (consumeIf('d')) { 2736 parseNumber(true); 2737 if (!consumeIf('_')) 2738 return nullptr; 2739 Node *N = getDerived().parseName(State); 2740 if (N == nullptr) 2741 return nullptr; 2742 return make<LocalName>(Encoding, N); 2743 } 2744 2745 Node *Entity = getDerived().parseName(State); 2746 if (Entity == nullptr) 2747 return nullptr; 2748 First = parse_discriminator(First, Last); 2749 return make<LocalName>(Encoding, Entity); 2750 } 2751 2752 // <unscoped-name> ::= <unqualified-name> 2753 // ::= St <unqualified-name> # ::std:: 2754 // [*] extension 2755 template <typename Derived, typename Alloc> 2756 Node * 2757 AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State, 2758 bool *IsSubst) { 2759 2760 Node *Std = nullptr; 2761 if (consumeIf("St")) { 2762 Std = make<NameType>("std"); 2763 if (Std == nullptr) 2764 return nullptr; 2765 } 2766 2767 Node *Res = nullptr; 2768 ModuleName *Module = nullptr; 2769 if (look() == 'S') { 2770 Node *S = getDerived().parseSubstitution(); 2771 if (!S) 2772 return nullptr; 2773 if (S->getKind() == Node::KModuleName) 2774 Module = static_cast<ModuleName *>(S); 2775 else if (IsSubst && Std == nullptr) { 2776 Res = S; 2777 *IsSubst = true; 2778 } else { 2779 return nullptr; 2780 } 2781 } 2782 2783 if (Res == nullptr || Std != nullptr) { 2784 Res = getDerived().parseUnqualifiedName(State, Std, Module); 2785 } 2786 2787 return Res; 2788 } 2789 2790 // <unqualified-name> ::= [<module-name>] L? <operator-name> [<abi-tags>] 2791 // ::= [<module-name>] <ctor-dtor-name> [<abi-tags>] 2792 // ::= [<module-name>] L? <source-name> [<abi-tags>] 2793 // ::= [<module-name>] L? <unnamed-type-name> [<abi-tags>] 2794 // # structured binding declaration 2795 // ::= [<module-name>] L? DC <source-name>+ E 2796 template <typename Derived, typename Alloc> 2797 Node *AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName( 2798 NameState *State, Node *Scope, ModuleName *Module) { 2799 if (getDerived().parseModuleNameOpt(Module)) 2800 return nullptr; 2801 2802 consumeIf('L'); 2803 2804 Node *Result; 2805 if (look() >= '1' && look() <= '9') { 2806 Result = getDerived().parseSourceName(State); 2807 } else if (look() == 'U') { 2808 Result = getDerived().parseUnnamedTypeName(State); 2809 } else if (consumeIf("DC")) { 2810 // Structured binding 2811 size_t BindingsBegin = Names.size(); 2812 do { 2813 Node *Binding = getDerived().parseSourceName(State); 2814 if (Binding == nullptr) 2815 return nullptr; 2816 Names.push_back(Binding); 2817 } while (!consumeIf('E')); 2818 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin)); 2819 } else if (look() == 'C' || look() == 'D') { 2820 // A <ctor-dtor-name>. 2821 if (Scope == nullptr || Module != nullptr) 2822 return nullptr; 2823 Result = getDerived().parseCtorDtorName(Scope, State); 2824 } else { 2825 Result = getDerived().parseOperatorName(State); 2826 } 2827 2828 if (Result != nullptr && Module != nullptr) 2829 Result = make<ModuleEntity>(Module, Result); 2830 if (Result != nullptr) 2831 Result = getDerived().parseAbiTags(Result); 2832 if (Result != nullptr && Scope != nullptr) 2833 Result = make<NestedName>(Scope, Result); 2834 2835 return Result; 2836 } 2837 2838 // <module-name> ::= <module-subname> 2839 // ::= <module-name> <module-subname> 2840 // ::= <substitution> # passed in by caller 2841 // <module-subname> ::= W <source-name> 2842 // ::= W P <source-name> 2843 template <typename Derived, typename Alloc> 2844 bool AbstractManglingParser<Derived, Alloc>::parseModuleNameOpt( 2845 ModuleName *&Module) { 2846 while (consumeIf('W')) { 2847 bool IsPartition = consumeIf('P'); 2848 Node *Sub = getDerived().parseSourceName(nullptr); 2849 if (!Sub) 2850 return true; 2851 Module = 2852 static_cast<ModuleName *>(make<ModuleName>(Module, Sub, IsPartition)); 2853 Subs.push_back(Module); 2854 } 2855 2856 return false; 2857 } 2858 2859 // <unnamed-type-name> ::= Ut [<nonnegative number>] _ 2860 // ::= <closure-type-name> 2861 // 2862 // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ 2863 // 2864 // <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters 2865 template <typename Derived, typename Alloc> 2866 Node * 2867 AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) { 2868 // <template-params> refer to the innermost <template-args>. Clear out any 2869 // outer args that we may have inserted into TemplateParams. 2870 if (State != nullptr) 2871 TemplateParams.clear(); 2872 2873 if (consumeIf("Ut")) { 2874 StringView Count = parseNumber(); 2875 if (!consumeIf('_')) 2876 return nullptr; 2877 return make<UnnamedTypeName>(Count); 2878 } 2879 if (consumeIf("Ul")) { 2880 ScopedOverride<size_t> SwapParams(ParsingLambdaParamsAtLevel, 2881 TemplateParams.size()); 2882 ScopedTemplateParamList LambdaTemplateParams(this); 2883 2884 size_t ParamsBegin = Names.size(); 2885 while (look() == 'T' && 2886 StringView("yptn").find(look(1)) != StringView::npos) { 2887 Node *T = parseTemplateParamDecl(); 2888 if (!T) 2889 return nullptr; 2890 Names.push_back(T); 2891 } 2892 NodeArray TempParams = popTrailingNodeArray(ParamsBegin); 2893 2894 // FIXME: If TempParams is empty and none of the function parameters 2895 // includes 'auto', we should remove LambdaTemplateParams from the 2896 // TemplateParams list. Unfortunately, we don't find out whether there are 2897 // any 'auto' parameters until too late in an example such as: 2898 // 2899 // template<typename T> void f( 2900 // decltype([](decltype([]<typename T>(T v) {}), 2901 // auto) {})) {} 2902 // template<typename T> void f( 2903 // decltype([](decltype([]<typename T>(T w) {}), 2904 // int) {})) {} 2905 // 2906 // Here, the type of v is at level 2 but the type of w is at level 1. We 2907 // don't find this out until we encounter the type of the next parameter. 2908 // 2909 // However, compilers can't actually cope with the former example in 2910 // practice, and it's likely to be made ill-formed in future, so we don't 2911 // need to support it here. 2912 // 2913 // If we encounter an 'auto' in the function parameter types, we will 2914 // recreate a template parameter scope for it, but any intervening lambdas 2915 // will be parsed in the 'wrong' template parameter depth. 2916 if (TempParams.empty()) 2917 TemplateParams.pop_back(); 2918 2919 if (!consumeIf("vE")) { 2920 do { 2921 Node *P = getDerived().parseType(); 2922 if (P == nullptr) 2923 return nullptr; 2924 Names.push_back(P); 2925 } while (!consumeIf('E')); 2926 } 2927 NodeArray Params = popTrailingNodeArray(ParamsBegin); 2928 2929 StringView Count = parseNumber(); 2930 if (!consumeIf('_')) 2931 return nullptr; 2932 return make<ClosureTypeName>(TempParams, Params, Count); 2933 } 2934 if (consumeIf("Ub")) { 2935 (void)parseNumber(); 2936 if (!consumeIf('_')) 2937 return nullptr; 2938 return make<NameType>("'block-literal'"); 2939 } 2940 return nullptr; 2941 } 2942 2943 // <source-name> ::= <positive length number> <identifier> 2944 template <typename Derived, typename Alloc> 2945 Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) { 2946 size_t Length = 0; 2947 if (parsePositiveInteger(&Length)) 2948 return nullptr; 2949 if (numLeft() < Length || Length == 0) 2950 return nullptr; 2951 StringView Name(First, First + Length); 2952 First += Length; 2953 if (Name.startsWith("_GLOBAL__N")) 2954 return make<NameType>("(anonymous namespace)"); 2955 return make<NameType>(Name); 2956 } 2957 2958 // If the next 2 chars are an operator encoding, consume them and return their 2959 // OperatorInfo. Otherwise return nullptr. 2960 template <typename Derived, typename Alloc> 2961 const typename AbstractManglingParser<Derived, Alloc>::OperatorInfo * 2962 AbstractManglingParser<Derived, Alloc>::parseOperatorEncoding() { 2963 static const OperatorInfo Ops[] = { 2964 // Keep ordered by encoding 2965 {"aN", OperatorInfo::Binary, false, Node::Prec::Assign, "operator&="}, 2966 {"aS", OperatorInfo::Binary, false, Node::Prec::Assign, "operator="}, 2967 {"aa", OperatorInfo::Binary, false, Node::Prec::AndIf, "operator&&"}, 2968 {"ad", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator&"}, 2969 {"an", OperatorInfo::Binary, false, Node::Prec::And, "operator&"}, 2970 {"at", OperatorInfo::OfIdOp, /*Type*/ true, Node::Prec::Unary, 2971 "alignof "}, 2972 {"aw", OperatorInfo::NameOnly, false, Node::Prec::Primary, 2973 "operator co_await"}, 2974 {"az", OperatorInfo::OfIdOp, /*Type*/ false, Node::Prec::Unary, 2975 "alignof "}, 2976 {"cc", OperatorInfo::NamedCast, false, Node::Prec::Postfix, "const_cast"}, 2977 {"cl", OperatorInfo::Call, false, Node::Prec::Postfix, "operator()"}, 2978 {"cm", OperatorInfo::Binary, false, Node::Prec::Comma, "operator,"}, 2979 {"co", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator~"}, 2980 {"cv", OperatorInfo::CCast, false, Node::Prec::Cast, 2981 "operator"}, // C Cast 2982 {"dV", OperatorInfo::Binary, false, Node::Prec::Assign, "operator/="}, 2983 {"da", OperatorInfo::Del, /*Ary*/ true, Node::Prec::Unary, 2984 "operator delete[]"}, 2985 {"dc", OperatorInfo::NamedCast, false, Node::Prec::Postfix, 2986 "dynamic_cast"}, 2987 {"de", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator*"}, 2988 {"dl", OperatorInfo::Del, /*Ary*/ false, Node::Prec::Unary, 2989 "operator delete"}, 2990 {"ds", OperatorInfo::Member, /*Named*/ false, Node::Prec::PtrMem, 2991 "operator.*"}, 2992 {"dt", OperatorInfo::Member, /*Named*/ false, Node::Prec::Postfix, 2993 "operator."}, 2994 {"dv", OperatorInfo::Binary, false, Node::Prec::Assign, "operator/"}, 2995 {"eO", OperatorInfo::Binary, false, Node::Prec::Assign, "operator^="}, 2996 {"eo", OperatorInfo::Binary, false, Node::Prec::Xor, "operator^"}, 2997 {"eq", OperatorInfo::Binary, false, Node::Prec::Equality, "operator=="}, 2998 {"ge", OperatorInfo::Binary, false, Node::Prec::Relational, "operator>="}, 2999 {"gt", OperatorInfo::Binary, false, Node::Prec::Relational, "operator>"}, 3000 {"ix", OperatorInfo::Array, false, Node::Prec::Postfix, "operator[]"}, 3001 {"lS", OperatorInfo::Binary, false, Node::Prec::Assign, "operator<<="}, 3002 {"le", OperatorInfo::Binary, false, Node::Prec::Relational, "operator<="}, 3003 {"ls", OperatorInfo::Binary, false, Node::Prec::Shift, "operator<<"}, 3004 {"lt", OperatorInfo::Binary, false, Node::Prec::Relational, "operator<"}, 3005 {"mI", OperatorInfo::Binary, false, Node::Prec::Assign, "operator-="}, 3006 {"mL", OperatorInfo::Binary, false, Node::Prec::Assign, "operator*="}, 3007 {"mi", OperatorInfo::Binary, false, Node::Prec::Additive, "operator-"}, 3008 {"ml", OperatorInfo::Binary, false, Node::Prec::Multiplicative, 3009 "operator*"}, 3010 {"mm", OperatorInfo::Postfix, false, Node::Prec::Postfix, "operator--"}, 3011 {"na", OperatorInfo::New, /*Ary*/ true, Node::Prec::Unary, 3012 "operator new[]"}, 3013 {"ne", OperatorInfo::Binary, false, Node::Prec::Equality, "operator!="}, 3014 {"ng", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator-"}, 3015 {"nt", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator!"}, 3016 {"nw", OperatorInfo::New, /*Ary*/ false, Node::Prec::Unary, 3017 "operator new"}, 3018 {"oR", OperatorInfo::Binary, false, Node::Prec::Assign, "operator|="}, 3019 {"oo", OperatorInfo::Binary, false, Node::Prec::OrIf, "operator||"}, 3020 {"or", OperatorInfo::Binary, false, Node::Prec::Ior, "operator|"}, 3021 {"pL", OperatorInfo::Binary, false, Node::Prec::Assign, "operator+="}, 3022 {"pl", OperatorInfo::Binary, false, Node::Prec::Additive, "operator+"}, 3023 {"pm", OperatorInfo::Member, /*Named*/ false, Node::Prec::PtrMem, 3024 "operator->*"}, 3025 {"pp", OperatorInfo::Postfix, false, Node::Prec::Postfix, "operator++"}, 3026 {"ps", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator+"}, 3027 {"pt", OperatorInfo::Member, /*Named*/ true, Node::Prec::Postfix, 3028 "operator->"}, 3029 {"qu", OperatorInfo::Conditional, false, Node::Prec::Conditional, 3030 "operator?"}, 3031 {"rM", OperatorInfo::Binary, false, Node::Prec::Assign, "operator%="}, 3032 {"rS", OperatorInfo::Binary, false, Node::Prec::Assign, "operator>>="}, 3033 {"rc", OperatorInfo::NamedCast, false, Node::Prec::Postfix, 3034 "reinterpret_cast"}, 3035 {"rm", OperatorInfo::Binary, false, Node::Prec::Multiplicative, 3036 "operator%"}, 3037 {"rs", OperatorInfo::Binary, false, Node::Prec::Shift, "operator>>"}, 3038 {"sc", OperatorInfo::NamedCast, false, Node::Prec::Postfix, 3039 "static_cast"}, 3040 {"ss", OperatorInfo::Binary, false, Node::Prec::Spaceship, "operator<=>"}, 3041 {"st", OperatorInfo::OfIdOp, /*Type*/ true, Node::Prec::Unary, "sizeof "}, 3042 {"sz", OperatorInfo::OfIdOp, /*Type*/ false, Node::Prec::Unary, 3043 "sizeof "}, 3044 {"te", OperatorInfo::OfIdOp, /*Type*/ false, Node::Prec::Postfix, 3045 "typeid "}, 3046 {"ti", OperatorInfo::OfIdOp, /*Type*/ true, Node::Prec::Postfix, 3047 "typeid "}, 3048 }; 3049 const auto NumOps = sizeof(Ops) / sizeof(Ops[0]); 3050 3051 #ifndef NDEBUG 3052 { 3053 // Verify table order. 3054 static bool Done; 3055 if (!Done) { 3056 Done = true; 3057 for (const auto *Op = &Ops[0]; Op != &Ops[NumOps - 1]; Op++) 3058 assert(Op[0] < Op[1] && "Operator table is not ordered"); 3059 } 3060 } 3061 #endif 3062 3063 if (numLeft() < 2) 3064 return nullptr; 3065 3066 auto Op = std::lower_bound( 3067 &Ops[0], &Ops[NumOps], First, 3068 [](const OperatorInfo &Op_, const char *Enc_) { return Op_ < Enc_; }); 3069 if (Op == &Ops[NumOps] || *Op != First) 3070 return nullptr; 3071 3072 First += 2; 3073 return Op; 3074 } 3075 3076 // <operator-name> ::= See parseOperatorEncoding() 3077 // ::= li <source-name> # operator "" 3078 // ::= v <digit> <source-name> # vendor extended operator 3079 template <typename Derived, typename Alloc> 3080 Node * 3081 AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) { 3082 if (const auto *Op = parseOperatorEncoding()) { 3083 if (Op->getKind() == OperatorInfo::CCast) { 3084 // ::= cv <type> # (cast) 3085 ScopedOverride<bool> SaveTemplate(TryToParseTemplateArgs, false); 3086 // If we're parsing an encoding, State != nullptr and the conversion 3087 // operators' <type> could have a <template-param> that refers to some 3088 // <template-arg>s further ahead in the mangled name. 3089 ScopedOverride<bool> SavePermit(PermitForwardTemplateReferences, 3090 PermitForwardTemplateReferences || 3091 State != nullptr); 3092 Node *Ty = getDerived().parseType(); 3093 if (Ty == nullptr) 3094 return nullptr; 3095 if (State) State->CtorDtorConversion = true; 3096 return make<ConversionOperatorType>(Ty); 3097 } 3098 3099 if (Op->getKind() >= OperatorInfo::Unnameable) 3100 /* Not a nameable operator. */ 3101 return nullptr; 3102 if (Op->getKind() == OperatorInfo::Member && !Op->getFlag()) 3103 /* Not a nameable MemberExpr */ 3104 return nullptr; 3105 3106 return make<NameType>(Op->getName()); 3107 } 3108 3109 if (consumeIf("li")) { 3110 // ::= li <source-name> # operator "" 3111 Node *SN = getDerived().parseSourceName(State); 3112 if (SN == nullptr) 3113 return nullptr; 3114 return make<LiteralOperator>(SN); 3115 } 3116 3117 if (consumeIf('v')) { 3118 // ::= v <digit> <source-name> # vendor extended operator 3119 if (look() >= '0' && look() <= '9') { 3120 First++; 3121 Node *SN = getDerived().parseSourceName(State); 3122 if (SN == nullptr) 3123 return nullptr; 3124 return make<ConversionOperatorType>(SN); 3125 } 3126 return nullptr; 3127 } 3128 3129 return nullptr; 3130 } 3131 3132 // <ctor-dtor-name> ::= C1 # complete object constructor 3133 // ::= C2 # base object constructor 3134 // ::= C3 # complete object allocating constructor 3135 // extension ::= C4 # gcc old-style "[unified]" constructor 3136 // extension ::= C5 # the COMDAT used for ctors 3137 // ::= D0 # deleting destructor 3138 // ::= D1 # complete object destructor 3139 // ::= D2 # base object destructor 3140 // extension ::= D4 # gcc old-style "[unified]" destructor 3141 // extension ::= D5 # the COMDAT used for dtors 3142 template <typename Derived, typename Alloc> 3143 Node * 3144 AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar, 3145 NameState *State) { 3146 if (SoFar->getKind() == Node::KSpecialSubstitution) { 3147 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK; 3148 switch (SSK) { 3149 case SpecialSubKind::string: 3150 case SpecialSubKind::istream: 3151 case SpecialSubKind::ostream: 3152 case SpecialSubKind::iostream: 3153 SoFar = make<ExpandedSpecialSubstitution>(SSK); 3154 if (!SoFar) 3155 return nullptr; 3156 break; 3157 default: 3158 break; 3159 } 3160 } 3161 3162 if (consumeIf('C')) { 3163 bool IsInherited = consumeIf('I'); 3164 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' && 3165 look() != '5') 3166 return nullptr; 3167 int Variant = look() - '0'; 3168 ++First; 3169 if (State) State->CtorDtorConversion = true; 3170 if (IsInherited) { 3171 if (getDerived().parseName(State) == nullptr) 3172 return nullptr; 3173 } 3174 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant); 3175 } 3176 3177 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' || 3178 look(1) == '4' || look(1) == '5')) { 3179 int Variant = look(1) - '0'; 3180 First += 2; 3181 if (State) State->CtorDtorConversion = true; 3182 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant); 3183 } 3184 3185 return nullptr; 3186 } 3187 3188 // <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> 3189 // <unqualified-name> E 3190 // ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> 3191 // <template-args> E 3192 // 3193 // <prefix> ::= <prefix> <unqualified-name> 3194 // ::= <template-prefix> <template-args> 3195 // ::= <template-param> 3196 // ::= <decltype> 3197 // ::= # empty 3198 // ::= <substitution> 3199 // ::= <prefix> <data-member-prefix> 3200 // [*] extension 3201 // 3202 // <data-member-prefix> := <member source-name> [<template-args>] M 3203 // 3204 // <template-prefix> ::= <prefix> <template unqualified-name> 3205 // ::= <template-param> 3206 // ::= <substitution> 3207 template <typename Derived, typename Alloc> 3208 Node * 3209 AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) { 3210 if (!consumeIf('N')) 3211 return nullptr; 3212 3213 Qualifiers CVTmp = parseCVQualifiers(); 3214 if (State) State->CVQualifiers = CVTmp; 3215 3216 if (consumeIf('O')) { 3217 if (State) State->ReferenceQualifier = FrefQualRValue; 3218 } else if (consumeIf('R')) { 3219 if (State) State->ReferenceQualifier = FrefQualLValue; 3220 } else { 3221 if (State) State->ReferenceQualifier = FrefQualNone; 3222 } 3223 3224 Node *SoFar = nullptr; 3225 while (!consumeIf('E')) { 3226 if (State) 3227 // Only set end-with-template on the case that does that. 3228 State->EndsWithTemplateArgs = false; 3229 3230 if (look() == 'T') { 3231 // ::= <template-param> 3232 if (SoFar != nullptr) 3233 return nullptr; // Cannot have a prefix. 3234 SoFar = getDerived().parseTemplateParam(); 3235 } else if (look() == 'I') { 3236 // ::= <template-prefix> <template-args> 3237 if (SoFar == nullptr) 3238 return nullptr; // Must have a prefix. 3239 Node *TA = getDerived().parseTemplateArgs(State != nullptr); 3240 if (TA == nullptr) 3241 return nullptr; 3242 if (SoFar->getKind() == Node::KNameWithTemplateArgs) 3243 // Semantically <template-args> <template-args> cannot be generated by a 3244 // C++ entity. There will always be [something like] a name between 3245 // them. 3246 return nullptr; 3247 if (State) 3248 State->EndsWithTemplateArgs = true; 3249 SoFar = make<NameWithTemplateArgs>(SoFar, TA); 3250 } else if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) { 3251 // ::= <decltype> 3252 if (SoFar != nullptr) 3253 return nullptr; // Cannot have a prefix. 3254 SoFar = getDerived().parseDecltype(); 3255 } else { 3256 ModuleName *Module = nullptr; 3257 3258 if (look() == 'S') { 3259 // ::= <substitution> 3260 Node *S = nullptr; 3261 if (look(1) == 't') { 3262 First += 2; 3263 S = make<NameType>("std"); 3264 } else { 3265 S = getDerived().parseSubstitution(); 3266 } 3267 if (!S) 3268 return nullptr; 3269 if (S->getKind() == Node::KModuleName) { 3270 Module = static_cast<ModuleName *>(S); 3271 } else if (SoFar != nullptr) { 3272 return nullptr; // Cannot have a prefix. 3273 } else { 3274 SoFar = S; 3275 continue; // Do not push a new substitution. 3276 } 3277 } 3278 3279 // ::= [<prefix>] <unqualified-name> 3280 SoFar = getDerived().parseUnqualifiedName(State, SoFar, Module); 3281 } 3282 3283 if (SoFar == nullptr) 3284 return nullptr; 3285 Subs.push_back(SoFar); 3286 3287 // No longer used. 3288 // <data-member-prefix> := <member source-name> [<template-args>] M 3289 consumeIf('M'); 3290 } 3291 3292 if (SoFar == nullptr || Subs.empty()) 3293 return nullptr; 3294 3295 Subs.pop_back(); 3296 return SoFar; 3297 } 3298 3299 // <simple-id> ::= <source-name> [ <template-args> ] 3300 template <typename Derived, typename Alloc> 3301 Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() { 3302 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr); 3303 if (SN == nullptr) 3304 return nullptr; 3305 if (look() == 'I') { 3306 Node *TA = getDerived().parseTemplateArgs(); 3307 if (TA == nullptr) 3308 return nullptr; 3309 return make<NameWithTemplateArgs>(SN, TA); 3310 } 3311 return SN; 3312 } 3313 3314 // <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f()) 3315 // ::= <simple-id> # e.g., ~A<2*N> 3316 template <typename Derived, typename Alloc> 3317 Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() { 3318 Node *Result; 3319 if (std::isdigit(look())) 3320 Result = getDerived().parseSimpleId(); 3321 else 3322 Result = getDerived().parseUnresolvedType(); 3323 if (Result == nullptr) 3324 return nullptr; 3325 return make<DtorName>(Result); 3326 } 3327 3328 // <unresolved-type> ::= <template-param> 3329 // ::= <decltype> 3330 // ::= <substitution> 3331 template <typename Derived, typename Alloc> 3332 Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() { 3333 if (look() == 'T') { 3334 Node *TP = getDerived().parseTemplateParam(); 3335 if (TP == nullptr) 3336 return nullptr; 3337 Subs.push_back(TP); 3338 return TP; 3339 } 3340 if (look() == 'D') { 3341 Node *DT = getDerived().parseDecltype(); 3342 if (DT == nullptr) 3343 return nullptr; 3344 Subs.push_back(DT); 3345 return DT; 3346 } 3347 return getDerived().parseSubstitution(); 3348 } 3349 3350 // <base-unresolved-name> ::= <simple-id> # unresolved name 3351 // extension ::= <operator-name> # unresolved operator-function-id 3352 // extension ::= <operator-name> <template-args> # unresolved operator template-id 3353 // ::= on <operator-name> # unresolved operator-function-id 3354 // ::= on <operator-name> <template-args> # unresolved operator template-id 3355 // ::= dn <destructor-name> # destructor or pseudo-destructor; 3356 // # e.g. ~X or ~X<N-1> 3357 template <typename Derived, typename Alloc> 3358 Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() { 3359 if (std::isdigit(look())) 3360 return getDerived().parseSimpleId(); 3361 3362 if (consumeIf("dn")) 3363 return getDerived().parseDestructorName(); 3364 3365 consumeIf("on"); 3366 3367 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr); 3368 if (Oper == nullptr) 3369 return nullptr; 3370 if (look() == 'I') { 3371 Node *TA = getDerived().parseTemplateArgs(); 3372 if (TA == nullptr) 3373 return nullptr; 3374 return make<NameWithTemplateArgs>(Oper, TA); 3375 } 3376 return Oper; 3377 } 3378 3379 // <unresolved-name> 3380 // extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name> 3381 // ::= [gs] <base-unresolved-name> # x or (with "gs") ::x 3382 // ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name> 3383 // # A::x, N::y, A<T>::z; "gs" means leading "::" 3384 // [gs] has been parsed by caller. 3385 // ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x 3386 // extension ::= sr <unresolved-type> <template-args> <base-unresolved-name> 3387 // # T::N::x /decltype(p)::N::x 3388 // (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name> 3389 // 3390 // <unresolved-qualifier-level> ::= <simple-id> 3391 template <typename Derived, typename Alloc> 3392 Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName(bool Global) { 3393 Node *SoFar = nullptr; 3394 3395 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name> 3396 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name> 3397 if (consumeIf("srN")) { 3398 SoFar = getDerived().parseUnresolvedType(); 3399 if (SoFar == nullptr) 3400 return nullptr; 3401 3402 if (look() == 'I') { 3403 Node *TA = getDerived().parseTemplateArgs(); 3404 if (TA == nullptr) 3405 return nullptr; 3406 SoFar = make<NameWithTemplateArgs>(SoFar, TA); 3407 if (!SoFar) 3408 return nullptr; 3409 } 3410 3411 while (!consumeIf('E')) { 3412 Node *Qual = getDerived().parseSimpleId(); 3413 if (Qual == nullptr) 3414 return nullptr; 3415 SoFar = make<QualifiedName>(SoFar, Qual); 3416 if (!SoFar) 3417 return nullptr; 3418 } 3419 3420 Node *Base = getDerived().parseBaseUnresolvedName(); 3421 if (Base == nullptr) 3422 return nullptr; 3423 return make<QualifiedName>(SoFar, Base); 3424 } 3425 3426 // [gs] <base-unresolved-name> # x or (with "gs") ::x 3427 if (!consumeIf("sr")) { 3428 SoFar = getDerived().parseBaseUnresolvedName(); 3429 if (SoFar == nullptr) 3430 return nullptr; 3431 if (Global) 3432 SoFar = make<GlobalQualifiedName>(SoFar); 3433 return SoFar; 3434 } 3435 3436 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name> 3437 if (std::isdigit(look())) { 3438 do { 3439 Node *Qual = getDerived().parseSimpleId(); 3440 if (Qual == nullptr) 3441 return nullptr; 3442 if (SoFar) 3443 SoFar = make<QualifiedName>(SoFar, Qual); 3444 else if (Global) 3445 SoFar = make<GlobalQualifiedName>(Qual); 3446 else 3447 SoFar = Qual; 3448 if (!SoFar) 3449 return nullptr; 3450 } while (!consumeIf('E')); 3451 } 3452 // sr <unresolved-type> <base-unresolved-name> 3453 // sr <unresolved-type> <template-args> <base-unresolved-name> 3454 else { 3455 SoFar = getDerived().parseUnresolvedType(); 3456 if (SoFar == nullptr) 3457 return nullptr; 3458 3459 if (look() == 'I') { 3460 Node *TA = getDerived().parseTemplateArgs(); 3461 if (TA == nullptr) 3462 return nullptr; 3463 SoFar = make<NameWithTemplateArgs>(SoFar, TA); 3464 if (!SoFar) 3465 return nullptr; 3466 } 3467 } 3468 3469 assert(SoFar != nullptr); 3470 3471 Node *Base = getDerived().parseBaseUnresolvedName(); 3472 if (Base == nullptr) 3473 return nullptr; 3474 return make<QualifiedName>(SoFar, Base); 3475 } 3476 3477 // <abi-tags> ::= <abi-tag> [<abi-tags>] 3478 // <abi-tag> ::= B <source-name> 3479 template <typename Derived, typename Alloc> 3480 Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) { 3481 while (consumeIf('B')) { 3482 StringView SN = parseBareSourceName(); 3483 if (SN.empty()) 3484 return nullptr; 3485 N = make<AbiTagAttr>(N, SN); 3486 if (!N) 3487 return nullptr; 3488 } 3489 return N; 3490 } 3491 3492 // <number> ::= [n] <non-negative decimal integer> 3493 template <typename Alloc, typename Derived> 3494 StringView 3495 AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) { 3496 const char *Tmp = First; 3497 if (AllowNegative) 3498 consumeIf('n'); 3499 if (numLeft() == 0 || !std::isdigit(*First)) 3500 return StringView(); 3501 while (numLeft() != 0 && std::isdigit(*First)) 3502 ++First; 3503 return StringView(Tmp, First); 3504 } 3505 3506 // <positive length number> ::= [0-9]* 3507 template <typename Alloc, typename Derived> 3508 bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) { 3509 *Out = 0; 3510 if (look() < '0' || look() > '9') 3511 return true; 3512 while (look() >= '0' && look() <= '9') { 3513 *Out *= 10; 3514 *Out += static_cast<size_t>(consume() - '0'); 3515 } 3516 return false; 3517 } 3518 3519 template <typename Alloc, typename Derived> 3520 StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() { 3521 size_t Int = 0; 3522 if (parsePositiveInteger(&Int) || numLeft() < Int) 3523 return StringView(); 3524 StringView R(First, First + Int); 3525 First += Int; 3526 return R; 3527 } 3528 3529 // <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E 3530 // 3531 // <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw()) 3532 // ::= DO <expression> E # computed (instantiation-dependent) noexcept 3533 // ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types 3534 // 3535 // <ref-qualifier> ::= R # & ref-qualifier 3536 // <ref-qualifier> ::= O # && ref-qualifier 3537 template <typename Derived, typename Alloc> 3538 Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() { 3539 Qualifiers CVQuals = parseCVQualifiers(); 3540 3541 Node *ExceptionSpec = nullptr; 3542 if (consumeIf("Do")) { 3543 ExceptionSpec = make<NameType>("noexcept"); 3544 if (!ExceptionSpec) 3545 return nullptr; 3546 } else if (consumeIf("DO")) { 3547 Node *E = getDerived().parseExpr(); 3548 if (E == nullptr || !consumeIf('E')) 3549 return nullptr; 3550 ExceptionSpec = make<NoexceptSpec>(E); 3551 if (!ExceptionSpec) 3552 return nullptr; 3553 } else if (consumeIf("Dw")) { 3554 size_t SpecsBegin = Names.size(); 3555 while (!consumeIf('E')) { 3556 Node *T = getDerived().parseType(); 3557 if (T == nullptr) 3558 return nullptr; 3559 Names.push_back(T); 3560 } 3561 ExceptionSpec = 3562 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin)); 3563 if (!ExceptionSpec) 3564 return nullptr; 3565 } 3566 3567 consumeIf("Dx"); // transaction safe 3568 3569 if (!consumeIf('F')) 3570 return nullptr; 3571 consumeIf('Y'); // extern "C" 3572 Node *ReturnType = getDerived().parseType(); 3573 if (ReturnType == nullptr) 3574 return nullptr; 3575 3576 FunctionRefQual ReferenceQualifier = FrefQualNone; 3577 size_t ParamsBegin = Names.size(); 3578 while (true) { 3579 if (consumeIf('E')) 3580 break; 3581 if (consumeIf('v')) 3582 continue; 3583 if (consumeIf("RE")) { 3584 ReferenceQualifier = FrefQualLValue; 3585 break; 3586 } 3587 if (consumeIf("OE")) { 3588 ReferenceQualifier = FrefQualRValue; 3589 break; 3590 } 3591 Node *T = getDerived().parseType(); 3592 if (T == nullptr) 3593 return nullptr; 3594 Names.push_back(T); 3595 } 3596 3597 NodeArray Params = popTrailingNodeArray(ParamsBegin); 3598 return make<FunctionType>(ReturnType, Params, CVQuals, 3599 ReferenceQualifier, ExceptionSpec); 3600 } 3601 3602 // extension: 3603 // <vector-type> ::= Dv <positive dimension number> _ <extended element type> 3604 // ::= Dv [<dimension expression>] _ <element type> 3605 // <extended element type> ::= <element type> 3606 // ::= p # AltiVec vector pixel 3607 template <typename Derived, typename Alloc> 3608 Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() { 3609 if (!consumeIf("Dv")) 3610 return nullptr; 3611 if (look() >= '1' && look() <= '9') { 3612 Node *DimensionNumber = make<NameType>(parseNumber()); 3613 if (!DimensionNumber) 3614 return nullptr; 3615 if (!consumeIf('_')) 3616 return nullptr; 3617 if (consumeIf('p')) 3618 return make<PixelVectorType>(DimensionNumber); 3619 Node *ElemType = getDerived().parseType(); 3620 if (ElemType == nullptr) 3621 return nullptr; 3622 return make<VectorType>(ElemType, DimensionNumber); 3623 } 3624 3625 if (!consumeIf('_')) { 3626 Node *DimExpr = getDerived().parseExpr(); 3627 if (!DimExpr) 3628 return nullptr; 3629 if (!consumeIf('_')) 3630 return nullptr; 3631 Node *ElemType = getDerived().parseType(); 3632 if (!ElemType) 3633 return nullptr; 3634 return make<VectorType>(ElemType, DimExpr); 3635 } 3636 Node *ElemType = getDerived().parseType(); 3637 if (!ElemType) 3638 return nullptr; 3639 return make<VectorType>(ElemType, /*Dimension=*/nullptr); 3640 } 3641 3642 // <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x) 3643 // ::= DT <expression> E # decltype of an expression (C++0x) 3644 template <typename Derived, typename Alloc> 3645 Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() { 3646 if (!consumeIf('D')) 3647 return nullptr; 3648 if (!consumeIf('t') && !consumeIf('T')) 3649 return nullptr; 3650 Node *E = getDerived().parseExpr(); 3651 if (E == nullptr) 3652 return nullptr; 3653 if (!consumeIf('E')) 3654 return nullptr; 3655 return make<EnclosingExpr>("decltype", E); 3656 } 3657 3658 // <array-type> ::= A <positive dimension number> _ <element type> 3659 // ::= A [<dimension expression>] _ <element type> 3660 template <typename Derived, typename Alloc> 3661 Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() { 3662 if (!consumeIf('A')) 3663 return nullptr; 3664 3665 Node *Dimension = nullptr; 3666 3667 if (std::isdigit(look())) { 3668 Dimension = make<NameType>(parseNumber()); 3669 if (!Dimension) 3670 return nullptr; 3671 if (!consumeIf('_')) 3672 return nullptr; 3673 } else if (!consumeIf('_')) { 3674 Node *DimExpr = getDerived().parseExpr(); 3675 if (DimExpr == nullptr) 3676 return nullptr; 3677 if (!consumeIf('_')) 3678 return nullptr; 3679 Dimension = DimExpr; 3680 } 3681 3682 Node *Ty = getDerived().parseType(); 3683 if (Ty == nullptr) 3684 return nullptr; 3685 return make<ArrayType>(Ty, Dimension); 3686 } 3687 3688 // <pointer-to-member-type> ::= M <class type> <member type> 3689 template <typename Derived, typename Alloc> 3690 Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() { 3691 if (!consumeIf('M')) 3692 return nullptr; 3693 Node *ClassType = getDerived().parseType(); 3694 if (ClassType == nullptr) 3695 return nullptr; 3696 Node *MemberType = getDerived().parseType(); 3697 if (MemberType == nullptr) 3698 return nullptr; 3699 return make<PointerToMemberType>(ClassType, MemberType); 3700 } 3701 3702 // <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier 3703 // ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class' 3704 // ::= Tu <name> # dependent elaborated type specifier using 'union' 3705 // ::= Te <name> # dependent elaborated type specifier using 'enum' 3706 template <typename Derived, typename Alloc> 3707 Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() { 3708 StringView ElabSpef; 3709 if (consumeIf("Ts")) 3710 ElabSpef = "struct"; 3711 else if (consumeIf("Tu")) 3712 ElabSpef = "union"; 3713 else if (consumeIf("Te")) 3714 ElabSpef = "enum"; 3715 3716 Node *Name = getDerived().parseName(); 3717 if (Name == nullptr) 3718 return nullptr; 3719 3720 if (!ElabSpef.empty()) 3721 return make<ElaboratedTypeSpefType>(ElabSpef, Name); 3722 3723 return Name; 3724 } 3725 3726 // <qualified-type> ::= <qualifiers> <type> 3727 // <qualifiers> ::= <extended-qualifier>* <CV-qualifiers> 3728 // <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier 3729 template <typename Derived, typename Alloc> 3730 Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() { 3731 if (consumeIf('U')) { 3732 StringView Qual = parseBareSourceName(); 3733 if (Qual.empty()) 3734 return nullptr; 3735 3736 // extension ::= U <objc-name> <objc-type> # objc-type<identifier> 3737 if (Qual.startsWith("objcproto")) { 3738 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto")); 3739 StringView Proto; 3740 { 3741 ScopedOverride<const char *> SaveFirst(First, ProtoSourceName.begin()), 3742 SaveLast(Last, ProtoSourceName.end()); 3743 Proto = parseBareSourceName(); 3744 } 3745 if (Proto.empty()) 3746 return nullptr; 3747 Node *Child = getDerived().parseQualifiedType(); 3748 if (Child == nullptr) 3749 return nullptr; 3750 return make<ObjCProtoName>(Child, Proto); 3751 } 3752 3753 Node *TA = nullptr; 3754 if (look() == 'I') { 3755 TA = getDerived().parseTemplateArgs(); 3756 if (TA == nullptr) 3757 return nullptr; 3758 } 3759 3760 Node *Child = getDerived().parseQualifiedType(); 3761 if (Child == nullptr) 3762 return nullptr; 3763 return make<VendorExtQualType>(Child, Qual, TA); 3764 } 3765 3766 Qualifiers Quals = parseCVQualifiers(); 3767 Node *Ty = getDerived().parseType(); 3768 if (Ty == nullptr) 3769 return nullptr; 3770 if (Quals != QualNone) 3771 Ty = make<QualType>(Ty, Quals); 3772 return Ty; 3773 } 3774 3775 // <type> ::= <builtin-type> 3776 // ::= <qualified-type> 3777 // ::= <function-type> 3778 // ::= <class-enum-type> 3779 // ::= <array-type> 3780 // ::= <pointer-to-member-type> 3781 // ::= <template-param> 3782 // ::= <template-template-param> <template-args> 3783 // ::= <decltype> 3784 // ::= P <type> # pointer 3785 // ::= R <type> # l-value reference 3786 // ::= O <type> # r-value reference (C++11) 3787 // ::= C <type> # complex pair (C99) 3788 // ::= G <type> # imaginary (C99) 3789 // ::= <substitution> # See Compression below 3790 // extension ::= U <objc-name> <objc-type> # objc-type<identifier> 3791 // extension ::= <vector-type> # <vector-type> starts with Dv 3792 // 3793 // <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1 3794 // <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name> 3795 template <typename Derived, typename Alloc> 3796 Node *AbstractManglingParser<Derived, Alloc>::parseType() { 3797 Node *Result = nullptr; 3798 3799 switch (look()) { 3800 // ::= <qualified-type> 3801 case 'r': 3802 case 'V': 3803 case 'K': { 3804 unsigned AfterQuals = 0; 3805 if (look(AfterQuals) == 'r') ++AfterQuals; 3806 if (look(AfterQuals) == 'V') ++AfterQuals; 3807 if (look(AfterQuals) == 'K') ++AfterQuals; 3808 3809 if (look(AfterQuals) == 'F' || 3810 (look(AfterQuals) == 'D' && 3811 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' || 3812 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) { 3813 Result = getDerived().parseFunctionType(); 3814 break; 3815 } 3816 DEMANGLE_FALLTHROUGH; 3817 } 3818 case 'U': { 3819 Result = getDerived().parseQualifiedType(); 3820 break; 3821 } 3822 // <builtin-type> ::= v # void 3823 case 'v': 3824 ++First; 3825 return make<NameType>("void"); 3826 // ::= w # wchar_t 3827 case 'w': 3828 ++First; 3829 return make<NameType>("wchar_t"); 3830 // ::= b # bool 3831 case 'b': 3832 ++First; 3833 return make<NameType>("bool"); 3834 // ::= c # char 3835 case 'c': 3836 ++First; 3837 return make<NameType>("char"); 3838 // ::= a # signed char 3839 case 'a': 3840 ++First; 3841 return make<NameType>("signed char"); 3842 // ::= h # unsigned char 3843 case 'h': 3844 ++First; 3845 return make<NameType>("unsigned char"); 3846 // ::= s # short 3847 case 's': 3848 ++First; 3849 return make<NameType>("short"); 3850 // ::= t # unsigned short 3851 case 't': 3852 ++First; 3853 return make<NameType>("unsigned short"); 3854 // ::= i # int 3855 case 'i': 3856 ++First; 3857 return make<NameType>("int"); 3858 // ::= j # unsigned int 3859 case 'j': 3860 ++First; 3861 return make<NameType>("unsigned int"); 3862 // ::= l # long 3863 case 'l': 3864 ++First; 3865 return make<NameType>("long"); 3866 // ::= m # unsigned long 3867 case 'm': 3868 ++First; 3869 return make<NameType>("unsigned long"); 3870 // ::= x # long long, __int64 3871 case 'x': 3872 ++First; 3873 return make<NameType>("long long"); 3874 // ::= y # unsigned long long, __int64 3875 case 'y': 3876 ++First; 3877 return make<NameType>("unsigned long long"); 3878 // ::= n # __int128 3879 case 'n': 3880 ++First; 3881 return make<NameType>("__int128"); 3882 // ::= o # unsigned __int128 3883 case 'o': 3884 ++First; 3885 return make<NameType>("unsigned __int128"); 3886 // ::= f # float 3887 case 'f': 3888 ++First; 3889 return make<NameType>("float"); 3890 // ::= d # double 3891 case 'd': 3892 ++First; 3893 return make<NameType>("double"); 3894 // ::= e # long double, __float80 3895 case 'e': 3896 ++First; 3897 return make<NameType>("long double"); 3898 // ::= g # __float128 3899 case 'g': 3900 ++First; 3901 return make<NameType>("__float128"); 3902 // ::= z # ellipsis 3903 case 'z': 3904 ++First; 3905 return make<NameType>("..."); 3906 3907 // <builtin-type> ::= u <source-name> # vendor extended type 3908 case 'u': { 3909 ++First; 3910 StringView Res = parseBareSourceName(); 3911 if (Res.empty()) 3912 return nullptr; 3913 // Typically, <builtin-type>s are not considered substitution candidates, 3914 // but the exception to that exception is vendor extended types (Itanium C++ 3915 // ABI 5.9.1). 3916 Result = make<NameType>(Res); 3917 break; 3918 } 3919 case 'D': 3920 switch (look(1)) { 3921 // ::= Dd # IEEE 754r decimal floating point (64 bits) 3922 case 'd': 3923 First += 2; 3924 return make<NameType>("decimal64"); 3925 // ::= De # IEEE 754r decimal floating point (128 bits) 3926 case 'e': 3927 First += 2; 3928 return make<NameType>("decimal128"); 3929 // ::= Df # IEEE 754r decimal floating point (32 bits) 3930 case 'f': 3931 First += 2; 3932 return make<NameType>("decimal32"); 3933 // ::= Dh # IEEE 754r half-precision floating point (16 bits) 3934 case 'h': 3935 First += 2; 3936 return make<NameType>("half"); 3937 // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits) 3938 case 'F': { 3939 First += 2; 3940 Node *DimensionNumber = make<NameType>(parseNumber()); 3941 if (!DimensionNumber) 3942 return nullptr; 3943 if (!consumeIf('_')) 3944 return nullptr; 3945 return make<BinaryFPType>(DimensionNumber); 3946 } 3947 // ::= DB <number> _ # C23 signed _BitInt(N) 3948 // ::= DB <instantiation-dependent expression> _ # C23 signed _BitInt(N) 3949 // ::= DU <number> _ # C23 unsigned _BitInt(N) 3950 // ::= DU <instantiation-dependent expression> _ # C23 unsigned _BitInt(N) 3951 case 'B': 3952 case 'U': { 3953 bool Signed = look(1) == 'B'; 3954 First += 2; 3955 Node *Size = std::isdigit(look()) ? make<NameType>(parseNumber()) 3956 : getDerived().parseExpr(); 3957 if (!Size) 3958 return nullptr; 3959 if (!consumeIf('_')) 3960 return nullptr; 3961 return make<BitIntType>(Size, Signed); 3962 } 3963 // ::= Di # char32_t 3964 case 'i': 3965 First += 2; 3966 return make<NameType>("char32_t"); 3967 // ::= Ds # char16_t 3968 case 's': 3969 First += 2; 3970 return make<NameType>("char16_t"); 3971 // ::= Du # char8_t (C++2a, not yet in the Itanium spec) 3972 case 'u': 3973 First += 2; 3974 return make<NameType>("char8_t"); 3975 // ::= Da # auto (in dependent new-expressions) 3976 case 'a': 3977 First += 2; 3978 return make<NameType>("auto"); 3979 // ::= Dc # decltype(auto) 3980 case 'c': 3981 First += 2; 3982 return make<NameType>("decltype(auto)"); 3983 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr)) 3984 case 'n': 3985 First += 2; 3986 return make<NameType>("std::nullptr_t"); 3987 3988 // ::= <decltype> 3989 case 't': 3990 case 'T': { 3991 Result = getDerived().parseDecltype(); 3992 break; 3993 } 3994 // extension ::= <vector-type> # <vector-type> starts with Dv 3995 case 'v': { 3996 Result = getDerived().parseVectorType(); 3997 break; 3998 } 3999 // ::= Dp <type> # pack expansion (C++0x) 4000 case 'p': { 4001 First += 2; 4002 Node *Child = getDerived().parseType(); 4003 if (!Child) 4004 return nullptr; 4005 Result = make<ParameterPackExpansion>(Child); 4006 break; 4007 } 4008 // Exception specifier on a function type. 4009 case 'o': 4010 case 'O': 4011 case 'w': 4012 // Transaction safe function type. 4013 case 'x': 4014 Result = getDerived().parseFunctionType(); 4015 break; 4016 } 4017 break; 4018 // ::= <function-type> 4019 case 'F': { 4020 Result = getDerived().parseFunctionType(); 4021 break; 4022 } 4023 // ::= <array-type> 4024 case 'A': { 4025 Result = getDerived().parseArrayType(); 4026 break; 4027 } 4028 // ::= <pointer-to-member-type> 4029 case 'M': { 4030 Result = getDerived().parsePointerToMemberType(); 4031 break; 4032 } 4033 // ::= <template-param> 4034 case 'T': { 4035 // This could be an elaborate type specifier on a <class-enum-type>. 4036 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') { 4037 Result = getDerived().parseClassEnumType(); 4038 break; 4039 } 4040 4041 Result = getDerived().parseTemplateParam(); 4042 if (Result == nullptr) 4043 return nullptr; 4044 4045 // Result could be either of: 4046 // <type> ::= <template-param> 4047 // <type> ::= <template-template-param> <template-args> 4048 // 4049 // <template-template-param> ::= <template-param> 4050 // ::= <substitution> 4051 // 4052 // If this is followed by some <template-args>, and we're permitted to 4053 // parse them, take the second production. 4054 4055 if (TryToParseTemplateArgs && look() == 'I') { 4056 Node *TA = getDerived().parseTemplateArgs(); 4057 if (TA == nullptr) 4058 return nullptr; 4059 Result = make<NameWithTemplateArgs>(Result, TA); 4060 } 4061 break; 4062 } 4063 // ::= P <type> # pointer 4064 case 'P': { 4065 ++First; 4066 Node *Ptr = getDerived().parseType(); 4067 if (Ptr == nullptr) 4068 return nullptr; 4069 Result = make<PointerType>(Ptr); 4070 break; 4071 } 4072 // ::= R <type> # l-value reference 4073 case 'R': { 4074 ++First; 4075 Node *Ref = getDerived().parseType(); 4076 if (Ref == nullptr) 4077 return nullptr; 4078 Result = make<ReferenceType>(Ref, ReferenceKind::LValue); 4079 break; 4080 } 4081 // ::= O <type> # r-value reference (C++11) 4082 case 'O': { 4083 ++First; 4084 Node *Ref = getDerived().parseType(); 4085 if (Ref == nullptr) 4086 return nullptr; 4087 Result = make<ReferenceType>(Ref, ReferenceKind::RValue); 4088 break; 4089 } 4090 // ::= C <type> # complex pair (C99) 4091 case 'C': { 4092 ++First; 4093 Node *P = getDerived().parseType(); 4094 if (P == nullptr) 4095 return nullptr; 4096 Result = make<PostfixQualifiedType>(P, " complex"); 4097 break; 4098 } 4099 // ::= G <type> # imaginary (C99) 4100 case 'G': { 4101 ++First; 4102 Node *P = getDerived().parseType(); 4103 if (P == nullptr) 4104 return P; 4105 Result = make<PostfixQualifiedType>(P, " imaginary"); 4106 break; 4107 } 4108 // ::= <substitution> # See Compression below 4109 case 'S': { 4110 if (look(1) != 't') { 4111 bool IsSubst = false; 4112 Result = getDerived().parseUnscopedName(nullptr, &IsSubst); 4113 if (!Result) 4114 return nullptr; 4115 4116 // Sub could be either of: 4117 // <type> ::= <substitution> 4118 // <type> ::= <template-template-param> <template-args> 4119 // 4120 // <template-template-param> ::= <template-param> 4121 // ::= <substitution> 4122 // 4123 // If this is followed by some <template-args>, and we're permitted to 4124 // parse them, take the second production. 4125 4126 if (look() == 'I' && (!IsSubst || TryToParseTemplateArgs)) { 4127 if (!IsSubst) 4128 Subs.push_back(Result); 4129 Node *TA = getDerived().parseTemplateArgs(); 4130 if (TA == nullptr) 4131 return nullptr; 4132 Result = make<NameWithTemplateArgs>(Result, TA); 4133 } else if (IsSubst) { 4134 // If all we parsed was a substitution, don't re-insert into the 4135 // substitution table. 4136 return Result; 4137 } 4138 break; 4139 } 4140 DEMANGLE_FALLTHROUGH; 4141 } 4142 // ::= <class-enum-type> 4143 default: { 4144 Result = getDerived().parseClassEnumType(); 4145 break; 4146 } 4147 } 4148 4149 // If we parsed a type, insert it into the substitution table. Note that all 4150 // <builtin-type>s and <substitution>s have already bailed out, because they 4151 // don't get substitutions. 4152 if (Result != nullptr) 4153 Subs.push_back(Result); 4154 return Result; 4155 } 4156 4157 template <typename Derived, typename Alloc> 4158 Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind, 4159 Node::Prec Prec) { 4160 Node *E = getDerived().parseExpr(); 4161 if (E == nullptr) 4162 return nullptr; 4163 return make<PrefixExpr>(Kind, E, Prec); 4164 } 4165 4166 template <typename Derived, typename Alloc> 4167 Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind, 4168 Node::Prec Prec) { 4169 Node *LHS = getDerived().parseExpr(); 4170 if (LHS == nullptr) 4171 return nullptr; 4172 Node *RHS = getDerived().parseExpr(); 4173 if (RHS == nullptr) 4174 return nullptr; 4175 return make<BinaryExpr>(LHS, Kind, RHS, Prec); 4176 } 4177 4178 template <typename Derived, typename Alloc> 4179 Node * 4180 AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) { 4181 StringView Tmp = parseNumber(true); 4182 if (!Tmp.empty() && consumeIf('E')) 4183 return make<IntegerLiteral>(Lit, Tmp); 4184 return nullptr; 4185 } 4186 4187 // <CV-Qualifiers> ::= [r] [V] [K] 4188 template <typename Alloc, typename Derived> 4189 Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() { 4190 Qualifiers CVR = QualNone; 4191 if (consumeIf('r')) 4192 CVR |= QualRestrict; 4193 if (consumeIf('V')) 4194 CVR |= QualVolatile; 4195 if (consumeIf('K')) 4196 CVR |= QualConst; 4197 return CVR; 4198 } 4199 4200 // <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter 4201 // ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters 4202 // ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter 4203 // ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L > 0, second and later parameters 4204 // ::= fpT # 'this' expression (not part of standard?) 4205 template <typename Derived, typename Alloc> 4206 Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() { 4207 if (consumeIf("fpT")) 4208 return make<NameType>("this"); 4209 if (consumeIf("fp")) { 4210 parseCVQualifiers(); 4211 StringView Num = parseNumber(); 4212 if (!consumeIf('_')) 4213 return nullptr; 4214 return make<FunctionParam>(Num); 4215 } 4216 if (consumeIf("fL")) { 4217 if (parseNumber().empty()) 4218 return nullptr; 4219 if (!consumeIf('p')) 4220 return nullptr; 4221 parseCVQualifiers(); 4222 StringView Num = parseNumber(); 4223 if (!consumeIf('_')) 4224 return nullptr; 4225 return make<FunctionParam>(Num); 4226 } 4227 return nullptr; 4228 } 4229 4230 // cv <type> <expression> # conversion with one argument 4231 // cv <type> _ <expression>* E # conversion with a different number of arguments 4232 template <typename Derived, typename Alloc> 4233 Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() { 4234 if (!consumeIf("cv")) 4235 return nullptr; 4236 Node *Ty; 4237 { 4238 ScopedOverride<bool> SaveTemp(TryToParseTemplateArgs, false); 4239 Ty = getDerived().parseType(); 4240 } 4241 4242 if (Ty == nullptr) 4243 return nullptr; 4244 4245 if (consumeIf('_')) { 4246 size_t ExprsBegin = Names.size(); 4247 while (!consumeIf('E')) { 4248 Node *E = getDerived().parseExpr(); 4249 if (E == nullptr) 4250 return E; 4251 Names.push_back(E); 4252 } 4253 NodeArray Exprs = popTrailingNodeArray(ExprsBegin); 4254 return make<ConversionExpr>(Ty, Exprs); 4255 } 4256 4257 Node *E[1] = {getDerived().parseExpr()}; 4258 if (E[0] == nullptr) 4259 return nullptr; 4260 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1)); 4261 } 4262 4263 // <expr-primary> ::= L <type> <value number> E # integer literal 4264 // ::= L <type> <value float> E # floating literal 4265 // ::= L <string type> E # string literal 4266 // ::= L <nullptr type> E # nullptr literal (i.e., "LDnE") 4267 // ::= L <lambda type> E # lambda expression 4268 // FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000) 4269 // ::= L <mangled-name> E # external name 4270 template <typename Derived, typename Alloc> 4271 Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() { 4272 if (!consumeIf('L')) 4273 return nullptr; 4274 switch (look()) { 4275 case 'w': 4276 ++First; 4277 return getDerived().parseIntegerLiteral("wchar_t"); 4278 case 'b': 4279 if (consumeIf("b0E")) 4280 return make<BoolExpr>(0); 4281 if (consumeIf("b1E")) 4282 return make<BoolExpr>(1); 4283 return nullptr; 4284 case 'c': 4285 ++First; 4286 return getDerived().parseIntegerLiteral("char"); 4287 case 'a': 4288 ++First; 4289 return getDerived().parseIntegerLiteral("signed char"); 4290 case 'h': 4291 ++First; 4292 return getDerived().parseIntegerLiteral("unsigned char"); 4293 case 's': 4294 ++First; 4295 return getDerived().parseIntegerLiteral("short"); 4296 case 't': 4297 ++First; 4298 return getDerived().parseIntegerLiteral("unsigned short"); 4299 case 'i': 4300 ++First; 4301 return getDerived().parseIntegerLiteral(""); 4302 case 'j': 4303 ++First; 4304 return getDerived().parseIntegerLiteral("u"); 4305 case 'l': 4306 ++First; 4307 return getDerived().parseIntegerLiteral("l"); 4308 case 'm': 4309 ++First; 4310 return getDerived().parseIntegerLiteral("ul"); 4311 case 'x': 4312 ++First; 4313 return getDerived().parseIntegerLiteral("ll"); 4314 case 'y': 4315 ++First; 4316 return getDerived().parseIntegerLiteral("ull"); 4317 case 'n': 4318 ++First; 4319 return getDerived().parseIntegerLiteral("__int128"); 4320 case 'o': 4321 ++First; 4322 return getDerived().parseIntegerLiteral("unsigned __int128"); 4323 case 'f': 4324 ++First; 4325 return getDerived().template parseFloatingLiteral<float>(); 4326 case 'd': 4327 ++First; 4328 return getDerived().template parseFloatingLiteral<double>(); 4329 case 'e': 4330 ++First; 4331 #if defined(__powerpc__) || defined(__s390__) 4332 // Handle cases where long doubles encoded with e have the same size 4333 // and representation as doubles. 4334 return getDerived().template parseFloatingLiteral<double>(); 4335 #else 4336 return getDerived().template parseFloatingLiteral<long double>(); 4337 #endif 4338 case '_': 4339 if (consumeIf("_Z")) { 4340 Node *R = getDerived().parseEncoding(); 4341 if (R != nullptr && consumeIf('E')) 4342 return R; 4343 } 4344 return nullptr; 4345 case 'A': { 4346 Node *T = getDerived().parseType(); 4347 if (T == nullptr) 4348 return nullptr; 4349 // FIXME: We need to include the string contents in the mangling. 4350 if (consumeIf('E')) 4351 return make<StringLiteral>(T); 4352 return nullptr; 4353 } 4354 case 'D': 4355 if (consumeIf("DnE")) 4356 return make<NameType>("nullptr"); 4357 return nullptr; 4358 case 'T': 4359 // Invalid mangled name per 4360 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html 4361 return nullptr; 4362 case 'U': { 4363 // FIXME: Should we support LUb... for block literals? 4364 if (look(1) != 'l') 4365 return nullptr; 4366 Node *T = parseUnnamedTypeName(nullptr); 4367 if (!T || !consumeIf('E')) 4368 return nullptr; 4369 return make<LambdaExpr>(T); 4370 } 4371 default: { 4372 // might be named type 4373 Node *T = getDerived().parseType(); 4374 if (T == nullptr) 4375 return nullptr; 4376 StringView N = parseNumber(/*AllowNegative=*/true); 4377 if (N.empty()) 4378 return nullptr; 4379 if (!consumeIf('E')) 4380 return nullptr; 4381 return make<EnumLiteral>(T, N); 4382 } 4383 } 4384 } 4385 4386 // <braced-expression> ::= <expression> 4387 // ::= di <field source-name> <braced-expression> # .name = expr 4388 // ::= dx <index expression> <braced-expression> # [expr] = expr 4389 // ::= dX <range begin expression> <range end expression> <braced-expression> 4390 template <typename Derived, typename Alloc> 4391 Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() { 4392 if (look() == 'd') { 4393 switch (look(1)) { 4394 case 'i': { 4395 First += 2; 4396 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr); 4397 if (Field == nullptr) 4398 return nullptr; 4399 Node *Init = getDerived().parseBracedExpr(); 4400 if (Init == nullptr) 4401 return nullptr; 4402 return make<BracedExpr>(Field, Init, /*isArray=*/false); 4403 } 4404 case 'x': { 4405 First += 2; 4406 Node *Index = getDerived().parseExpr(); 4407 if (Index == nullptr) 4408 return nullptr; 4409 Node *Init = getDerived().parseBracedExpr(); 4410 if (Init == nullptr) 4411 return nullptr; 4412 return make<BracedExpr>(Index, Init, /*isArray=*/true); 4413 } 4414 case 'X': { 4415 First += 2; 4416 Node *RangeBegin = getDerived().parseExpr(); 4417 if (RangeBegin == nullptr) 4418 return nullptr; 4419 Node *RangeEnd = getDerived().parseExpr(); 4420 if (RangeEnd == nullptr) 4421 return nullptr; 4422 Node *Init = getDerived().parseBracedExpr(); 4423 if (Init == nullptr) 4424 return nullptr; 4425 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init); 4426 } 4427 } 4428 } 4429 return getDerived().parseExpr(); 4430 } 4431 4432 // (not yet in the spec) 4433 // <fold-expr> ::= fL <binary-operator-name> <expression> <expression> 4434 // ::= fR <binary-operator-name> <expression> <expression> 4435 // ::= fl <binary-operator-name> <expression> 4436 // ::= fr <binary-operator-name> <expression> 4437 template <typename Derived, typename Alloc> 4438 Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() { 4439 if (!consumeIf('f')) 4440 return nullptr; 4441 4442 bool IsLeftFold = false, HasInitializer = false; 4443 switch (look()) { 4444 default: 4445 return nullptr; 4446 case 'L': 4447 IsLeftFold = true; 4448 HasInitializer = true; 4449 break; 4450 case 'R': 4451 HasInitializer = true; 4452 break; 4453 case 'l': 4454 IsLeftFold = true; 4455 break; 4456 case 'r': 4457 break; 4458 } 4459 ++First; 4460 4461 const auto *Op = parseOperatorEncoding(); 4462 if (!Op || Op->getKind() != OperatorInfo::Binary) 4463 return nullptr; 4464 4465 Node *Pack = getDerived().parseExpr(); 4466 if (Pack == nullptr) 4467 return nullptr; 4468 4469 Node *Init = nullptr; 4470 if (HasInitializer) { 4471 Init = getDerived().parseExpr(); 4472 if (Init == nullptr) 4473 return nullptr; 4474 } 4475 4476 if (IsLeftFold && Init) 4477 std::swap(Pack, Init); 4478 4479 return make<FoldExpr>(IsLeftFold, Op->getSymbol(), Pack, Init); 4480 } 4481 4482 // <expression> ::= mc <parameter type> <expr> [<offset number>] E 4483 // 4484 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47 4485 template <typename Derived, typename Alloc> 4486 Node * 4487 AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr( 4488 Node::Prec Prec) { 4489 Node *Ty = getDerived().parseType(); 4490 if (!Ty) 4491 return nullptr; 4492 Node *Expr = getDerived().parseExpr(); 4493 if (!Expr) 4494 return nullptr; 4495 StringView Offset = getDerived().parseNumber(true); 4496 if (!consumeIf('E')) 4497 return nullptr; 4498 return make<PointerToMemberConversionExpr>(Ty, Expr, Offset, Prec); 4499 } 4500 4501 // <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E 4502 // <union-selector> ::= _ [<number>] 4503 // 4504 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47 4505 template <typename Derived, typename Alloc> 4506 Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() { 4507 Node *Ty = getDerived().parseType(); 4508 if (!Ty) 4509 return nullptr; 4510 Node *Expr = getDerived().parseExpr(); 4511 if (!Expr) 4512 return nullptr; 4513 StringView Offset = getDerived().parseNumber(true); 4514 size_t SelectorsBegin = Names.size(); 4515 while (consumeIf('_')) { 4516 Node *Selector = make<NameType>(parseNumber()); 4517 if (!Selector) 4518 return nullptr; 4519 Names.push_back(Selector); 4520 } 4521 bool OnePastTheEnd = consumeIf('p'); 4522 if (!consumeIf('E')) 4523 return nullptr; 4524 return make<SubobjectExpr>( 4525 Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd); 4526 } 4527 4528 // <expression> ::= <unary operator-name> <expression> 4529 // ::= <binary operator-name> <expression> <expression> 4530 // ::= <ternary operator-name> <expression> <expression> <expression> 4531 // ::= cl <expression>+ E # call 4532 // ::= cv <type> <expression> # conversion with one argument 4533 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments 4534 // ::= [gs] nw <expression>* _ <type> E # new (expr-list) type 4535 // ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init) 4536 // ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type 4537 // ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init) 4538 // ::= [gs] dl <expression> # delete expression 4539 // ::= [gs] da <expression> # delete[] expression 4540 // ::= pp_ <expression> # prefix ++ 4541 // ::= mm_ <expression> # prefix -- 4542 // ::= ti <type> # typeid (type) 4543 // ::= te <expression> # typeid (expression) 4544 // ::= dc <type> <expression> # dynamic_cast<type> (expression) 4545 // ::= sc <type> <expression> # static_cast<type> (expression) 4546 // ::= cc <type> <expression> # const_cast<type> (expression) 4547 // ::= rc <type> <expression> # reinterpret_cast<type> (expression) 4548 // ::= st <type> # sizeof (a type) 4549 // ::= sz <expression> # sizeof (an expression) 4550 // ::= at <type> # alignof (a type) 4551 // ::= az <expression> # alignof (an expression) 4552 // ::= nx <expression> # noexcept (expression) 4553 // ::= <template-param> 4554 // ::= <function-param> 4555 // ::= dt <expression> <unresolved-name> # expr.name 4556 // ::= pt <expression> <unresolved-name> # expr->name 4557 // ::= ds <expression> <expression> # expr.*expr 4558 // ::= sZ <template-param> # size of a parameter pack 4559 // ::= sZ <function-param> # size of a function parameter pack 4560 // ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template 4561 // ::= sp <expression> # pack expansion 4562 // ::= tw <expression> # throw expression 4563 // ::= tr # throw with no operand (rethrow) 4564 // ::= <unresolved-name> # f(p), N::f(p), ::f(p), 4565 // # freestanding dependent name (e.g., T::x), 4566 // # objectless nonstatic member reference 4567 // ::= fL <binary-operator-name> <expression> <expression> 4568 // ::= fR <binary-operator-name> <expression> <expression> 4569 // ::= fl <binary-operator-name> <expression> 4570 // ::= fr <binary-operator-name> <expression> 4571 // ::= <expr-primary> 4572 template <typename Derived, typename Alloc> 4573 Node *AbstractManglingParser<Derived, Alloc>::parseExpr() { 4574 bool Global = consumeIf("gs"); 4575 4576 const auto *Op = parseOperatorEncoding(); 4577 if (Op) { 4578 auto Sym = Op->getSymbol(); 4579 switch (Op->getKind()) { 4580 case OperatorInfo::Binary: 4581 // Binary operator: lhs @ rhs 4582 return getDerived().parseBinaryExpr(Sym, Op->getPrecedence()); 4583 case OperatorInfo::Prefix: 4584 // Prefix unary operator: @ expr 4585 return getDerived().parsePrefixExpr(Sym, Op->getPrecedence()); 4586 case OperatorInfo::Postfix: { 4587 // Postfix unary operator: expr @ 4588 if (consumeIf('_')) 4589 return getDerived().parsePrefixExpr(Sym, Op->getPrecedence()); 4590 Node *Ex = getDerived().parseExpr(); 4591 if (Ex == nullptr) 4592 return nullptr; 4593 return make<PostfixExpr>(Ex, Sym, Op->getPrecedence()); 4594 } 4595 case OperatorInfo::Array: { 4596 // Array Index: lhs [ rhs ] 4597 Node *Base = getDerived().parseExpr(); 4598 if (Base == nullptr) 4599 return nullptr; 4600 Node *Index = getDerived().parseExpr(); 4601 if (Index == nullptr) 4602 return nullptr; 4603 return make<ArraySubscriptExpr>(Base, Index, Op->getPrecedence()); 4604 } 4605 case OperatorInfo::Member: { 4606 // Member access lhs @ rhs 4607 Node *LHS = getDerived().parseExpr(); 4608 if (LHS == nullptr) 4609 return nullptr; 4610 Node *RHS = getDerived().parseExpr(); 4611 if (RHS == nullptr) 4612 return nullptr; 4613 return make<MemberExpr>(LHS, Sym, RHS, Op->getPrecedence()); 4614 } 4615 case OperatorInfo::New: { 4616 // New 4617 // # new (expr-list) type [(init)] 4618 // [gs] nw <expression>* _ <type> [pi <expression>*] E 4619 // # new[] (expr-list) type [(init)] 4620 // [gs] na <expression>* _ <type> [pi <expression>*] E 4621 size_t Exprs = Names.size(); 4622 while (!consumeIf('_')) { 4623 Node *Ex = getDerived().parseExpr(); 4624 if (Ex == nullptr) 4625 return nullptr; 4626 Names.push_back(Ex); 4627 } 4628 NodeArray ExprList = popTrailingNodeArray(Exprs); 4629 Node *Ty = getDerived().parseType(); 4630 if (Ty == nullptr) 4631 return nullptr; 4632 bool HaveInits = consumeIf("pi"); 4633 size_t InitsBegin = Names.size(); 4634 while (!consumeIf('E')) { 4635 if (!HaveInits) 4636 return nullptr; 4637 Node *Init = getDerived().parseExpr(); 4638 if (Init == nullptr) 4639 return Init; 4640 Names.push_back(Init); 4641 } 4642 NodeArray Inits = popTrailingNodeArray(InitsBegin); 4643 return make<NewExpr>(ExprList, Ty, Inits, Global, 4644 /*IsArray=*/Op->getFlag(), Op->getPrecedence()); 4645 } 4646 case OperatorInfo::Del: { 4647 // Delete 4648 Node *Ex = getDerived().parseExpr(); 4649 if (Ex == nullptr) 4650 return nullptr; 4651 return make<DeleteExpr>(Ex, Global, /*IsArray=*/Op->getFlag(), 4652 Op->getPrecedence()); 4653 } 4654 case OperatorInfo::Call: { 4655 // Function Call 4656 Node *Callee = getDerived().parseExpr(); 4657 if (Callee == nullptr) 4658 return nullptr; 4659 size_t ExprsBegin = Names.size(); 4660 while (!consumeIf('E')) { 4661 Node *E = getDerived().parseExpr(); 4662 if (E == nullptr) 4663 return nullptr; 4664 Names.push_back(E); 4665 } 4666 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin), 4667 Op->getPrecedence()); 4668 } 4669 case OperatorInfo::CCast: { 4670 // C Cast: (type)expr 4671 Node *Ty; 4672 { 4673 ScopedOverride<bool> SaveTemp(TryToParseTemplateArgs, false); 4674 Ty = getDerived().parseType(); 4675 } 4676 if (Ty == nullptr) 4677 return nullptr; 4678 4679 size_t ExprsBegin = Names.size(); 4680 bool IsMany = consumeIf('_'); 4681 while (!consumeIf('E')) { 4682 Node *E = getDerived().parseExpr(); 4683 if (E == nullptr) 4684 return E; 4685 Names.push_back(E); 4686 if (!IsMany) 4687 break; 4688 } 4689 NodeArray Exprs = popTrailingNodeArray(ExprsBegin); 4690 if (!IsMany && Exprs.size() != 1) 4691 return nullptr; 4692 return make<ConversionExpr>(Ty, Exprs, Op->getPrecedence()); 4693 } 4694 case OperatorInfo::Conditional: { 4695 // Conditional operator: expr ? expr : expr 4696 Node *Cond = getDerived().parseExpr(); 4697 if (Cond == nullptr) 4698 return nullptr; 4699 Node *LHS = getDerived().parseExpr(); 4700 if (LHS == nullptr) 4701 return nullptr; 4702 Node *RHS = getDerived().parseExpr(); 4703 if (RHS == nullptr) 4704 return nullptr; 4705 return make<ConditionalExpr>(Cond, LHS, RHS, Op->getPrecedence()); 4706 } 4707 case OperatorInfo::NamedCast: { 4708 // Named cast operation, @<type>(expr) 4709 Node *Ty = getDerived().parseType(); 4710 if (Ty == nullptr) 4711 return nullptr; 4712 Node *Ex = getDerived().parseExpr(); 4713 if (Ex == nullptr) 4714 return nullptr; 4715 return make<CastExpr>(Sym, Ty, Ex, Op->getPrecedence()); 4716 } 4717 case OperatorInfo::OfIdOp: { 4718 // [sizeof/alignof/typeid] ( <type>|<expr> ) 4719 Node *Arg = 4720 Op->getFlag() ? getDerived().parseType() : getDerived().parseExpr(); 4721 if (!Arg) 4722 return nullptr; 4723 return make<EnclosingExpr>(Sym, Arg, Op->getPrecedence()); 4724 } 4725 case OperatorInfo::NameOnly: { 4726 // Not valid as an expression operand. 4727 return nullptr; 4728 } 4729 } 4730 DEMANGLE_UNREACHABLE; 4731 } 4732 4733 if (numLeft() < 2) 4734 return nullptr; 4735 4736 if (look() == 'L') 4737 return getDerived().parseExprPrimary(); 4738 if (look() == 'T') 4739 return getDerived().parseTemplateParam(); 4740 if (look() == 'f') { 4741 // Disambiguate a fold expression from a <function-param>. 4742 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2)))) 4743 return getDerived().parseFunctionParam(); 4744 return getDerived().parseFoldExpr(); 4745 } 4746 if (consumeIf("il")) { 4747 size_t InitsBegin = Names.size(); 4748 while (!consumeIf('E')) { 4749 Node *E = getDerived().parseBracedExpr(); 4750 if (E == nullptr) 4751 return nullptr; 4752 Names.push_back(E); 4753 } 4754 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin)); 4755 } 4756 if (consumeIf("mc")) 4757 return parsePointerToMemberConversionExpr(Node::Prec::Unary); 4758 if (consumeIf("nx")) { 4759 Node *Ex = getDerived().parseExpr(); 4760 if (Ex == nullptr) 4761 return Ex; 4762 return make<EnclosingExpr>("noexcept ", Ex, Node::Prec::Unary); 4763 } 4764 if (consumeIf("so")) 4765 return parseSubobjectExpr(); 4766 if (consumeIf("sp")) { 4767 Node *Child = getDerived().parseExpr(); 4768 if (Child == nullptr) 4769 return nullptr; 4770 return make<ParameterPackExpansion>(Child); 4771 } 4772 if (consumeIf("sZ")) { 4773 if (look() == 'T') { 4774 Node *R = getDerived().parseTemplateParam(); 4775 if (R == nullptr) 4776 return nullptr; 4777 return make<SizeofParamPackExpr>(R); 4778 } 4779 Node *FP = getDerived().parseFunctionParam(); 4780 if (FP == nullptr) 4781 return nullptr; 4782 return make<EnclosingExpr>("sizeof... ", FP); 4783 } 4784 if (consumeIf("sP")) { 4785 size_t ArgsBegin = Names.size(); 4786 while (!consumeIf('E')) { 4787 Node *Arg = getDerived().parseTemplateArg(); 4788 if (Arg == nullptr) 4789 return nullptr; 4790 Names.push_back(Arg); 4791 } 4792 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin)); 4793 if (!Pack) 4794 return nullptr; 4795 return make<EnclosingExpr>("sizeof... ", Pack); 4796 } 4797 if (consumeIf("tl")) { 4798 Node *Ty = getDerived().parseType(); 4799 if (Ty == nullptr) 4800 return nullptr; 4801 size_t InitsBegin = Names.size(); 4802 while (!consumeIf('E')) { 4803 Node *E = getDerived().parseBracedExpr(); 4804 if (E == nullptr) 4805 return nullptr; 4806 Names.push_back(E); 4807 } 4808 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin)); 4809 } 4810 if (consumeIf("tr")) 4811 return make<NameType>("throw"); 4812 if (consumeIf("tw")) { 4813 Node *Ex = getDerived().parseExpr(); 4814 if (Ex == nullptr) 4815 return nullptr; 4816 return make<ThrowExpr>(Ex); 4817 } 4818 if (consumeIf('u')) { 4819 Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr); 4820 if (!Name) 4821 return nullptr; 4822 // Special case legacy __uuidof mangling. The 't' and 'z' appear where the 4823 // standard encoding expects a <template-arg>, and would be otherwise be 4824 // interpreted as <type> node 'short' or 'ellipsis'. However, neither 4825 // __uuidof(short) nor __uuidof(...) can actually appear, so there is no 4826 // actual conflict here. 4827 bool IsUUID = false; 4828 Node *UUID = nullptr; 4829 if (Name->getBaseName() == "__uuidof") { 4830 if (consumeIf('t')) { 4831 UUID = getDerived().parseType(); 4832 IsUUID = true; 4833 } else if (consumeIf('z')) { 4834 UUID = getDerived().parseExpr(); 4835 IsUUID = true; 4836 } 4837 } 4838 size_t ExprsBegin = Names.size(); 4839 if (IsUUID) { 4840 if (UUID == nullptr) 4841 return nullptr; 4842 Names.push_back(UUID); 4843 } else { 4844 while (!consumeIf('E')) { 4845 Node *E = getDerived().parseTemplateArg(); 4846 if (E == nullptr) 4847 return E; 4848 Names.push_back(E); 4849 } 4850 } 4851 return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin), 4852 Node::Prec::Postfix); 4853 } 4854 4855 // Only unresolved names remain. 4856 return getDerived().parseUnresolvedName(Global); 4857 } 4858 4859 // <call-offset> ::= h <nv-offset> _ 4860 // ::= v <v-offset> _ 4861 // 4862 // <nv-offset> ::= <offset number> 4863 // # non-virtual base override 4864 // 4865 // <v-offset> ::= <offset number> _ <virtual offset number> 4866 // # virtual base override, with vcall offset 4867 template <typename Alloc, typename Derived> 4868 bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() { 4869 // Just scan through the call offset, we never add this information into the 4870 // output. 4871 if (consumeIf('h')) 4872 return parseNumber(true).empty() || !consumeIf('_'); 4873 if (consumeIf('v')) 4874 return parseNumber(true).empty() || !consumeIf('_') || 4875 parseNumber(true).empty() || !consumeIf('_'); 4876 return true; 4877 } 4878 4879 // <special-name> ::= TV <type> # virtual table 4880 // ::= TT <type> # VTT structure (construction vtable index) 4881 // ::= TI <type> # typeinfo structure 4882 // ::= TS <type> # typeinfo name (null-terminated byte string) 4883 // ::= Tc <call-offset> <call-offset> <base encoding> 4884 // # base is the nominal target function of thunk 4885 // # first call-offset is 'this' adjustment 4886 // # second call-offset is result adjustment 4887 // ::= T <call-offset> <base encoding> 4888 // # base is the nominal target function of thunk 4889 // # Guard variable for one-time initialization 4890 // ::= GV <object name> 4891 // # No <type> 4892 // ::= TW <object name> # Thread-local wrapper 4893 // ::= TH <object name> # Thread-local initialization 4894 // ::= GR <object name> _ # First temporary 4895 // ::= GR <object name> <seq-id> _ # Subsequent temporaries 4896 // # construction vtable for second-in-first 4897 // extension ::= TC <first type> <number> _ <second type> 4898 // extension ::= GR <object name> # reference temporary for object 4899 // extension ::= GI <module name> # module global initializer 4900 template <typename Derived, typename Alloc> 4901 Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() { 4902 switch (look()) { 4903 case 'T': 4904 switch (look(1)) { 4905 // TA <template-arg> # template parameter object 4906 // 4907 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63 4908 case 'A': { 4909 First += 2; 4910 Node *Arg = getDerived().parseTemplateArg(); 4911 if (Arg == nullptr) 4912 return nullptr; 4913 return make<SpecialName>("template parameter object for ", Arg); 4914 } 4915 // TV <type> # virtual table 4916 case 'V': { 4917 First += 2; 4918 Node *Ty = getDerived().parseType(); 4919 if (Ty == nullptr) 4920 return nullptr; 4921 return make<SpecialName>("vtable for ", Ty); 4922 } 4923 // TT <type> # VTT structure (construction vtable index) 4924 case 'T': { 4925 First += 2; 4926 Node *Ty = getDerived().parseType(); 4927 if (Ty == nullptr) 4928 return nullptr; 4929 return make<SpecialName>("VTT for ", Ty); 4930 } 4931 // TI <type> # typeinfo structure 4932 case 'I': { 4933 First += 2; 4934 Node *Ty = getDerived().parseType(); 4935 if (Ty == nullptr) 4936 return nullptr; 4937 return make<SpecialName>("typeinfo for ", Ty); 4938 } 4939 // TS <type> # typeinfo name (null-terminated byte string) 4940 case 'S': { 4941 First += 2; 4942 Node *Ty = getDerived().parseType(); 4943 if (Ty == nullptr) 4944 return nullptr; 4945 return make<SpecialName>("typeinfo name for ", Ty); 4946 } 4947 // Tc <call-offset> <call-offset> <base encoding> 4948 case 'c': { 4949 First += 2; 4950 if (parseCallOffset() || parseCallOffset()) 4951 return nullptr; 4952 Node *Encoding = getDerived().parseEncoding(); 4953 if (Encoding == nullptr) 4954 return nullptr; 4955 return make<SpecialName>("covariant return thunk to ", Encoding); 4956 } 4957 // extension ::= TC <first type> <number> _ <second type> 4958 // # construction vtable for second-in-first 4959 case 'C': { 4960 First += 2; 4961 Node *FirstType = getDerived().parseType(); 4962 if (FirstType == nullptr) 4963 return nullptr; 4964 if (parseNumber(true).empty() || !consumeIf('_')) 4965 return nullptr; 4966 Node *SecondType = getDerived().parseType(); 4967 if (SecondType == nullptr) 4968 return nullptr; 4969 return make<CtorVtableSpecialName>(SecondType, FirstType); 4970 } 4971 // TW <object name> # Thread-local wrapper 4972 case 'W': { 4973 First += 2; 4974 Node *Name = getDerived().parseName(); 4975 if (Name == nullptr) 4976 return nullptr; 4977 return make<SpecialName>("thread-local wrapper routine for ", Name); 4978 } 4979 // TH <object name> # Thread-local initialization 4980 case 'H': { 4981 First += 2; 4982 Node *Name = getDerived().parseName(); 4983 if (Name == nullptr) 4984 return nullptr; 4985 return make<SpecialName>("thread-local initialization routine for ", Name); 4986 } 4987 // T <call-offset> <base encoding> 4988 default: { 4989 ++First; 4990 bool IsVirt = look() == 'v'; 4991 if (parseCallOffset()) 4992 return nullptr; 4993 Node *BaseEncoding = getDerived().parseEncoding(); 4994 if (BaseEncoding == nullptr) 4995 return nullptr; 4996 if (IsVirt) 4997 return make<SpecialName>("virtual thunk to ", BaseEncoding); 4998 else 4999 return make<SpecialName>("non-virtual thunk to ", BaseEncoding); 5000 } 5001 } 5002 case 'G': 5003 switch (look(1)) { 5004 // GV <object name> # Guard variable for one-time initialization 5005 case 'V': { 5006 First += 2; 5007 Node *Name = getDerived().parseName(); 5008 if (Name == nullptr) 5009 return nullptr; 5010 return make<SpecialName>("guard variable for ", Name); 5011 } 5012 // GR <object name> # reference temporary for object 5013 // GR <object name> _ # First temporary 5014 // GR <object name> <seq-id> _ # Subsequent temporaries 5015 case 'R': { 5016 First += 2; 5017 Node *Name = getDerived().parseName(); 5018 if (Name == nullptr) 5019 return nullptr; 5020 size_t Count; 5021 bool ParsedSeqId = !parseSeqId(&Count); 5022 if (!consumeIf('_') && ParsedSeqId) 5023 return nullptr; 5024 return make<SpecialName>("reference temporary for ", Name); 5025 } 5026 // GI <module-name> v 5027 case 'I': { 5028 First += 2; 5029 ModuleName *Module = nullptr; 5030 if (getDerived().parseModuleNameOpt(Module)) 5031 return nullptr; 5032 if (Module == nullptr) 5033 return nullptr; 5034 return make<SpecialName>("initializer for module ", Module); 5035 } 5036 } 5037 } 5038 return nullptr; 5039 } 5040 5041 // <encoding> ::= <function name> <bare-function-type> 5042 // ::= <data name> 5043 // ::= <special-name> 5044 template <typename Derived, typename Alloc> 5045 Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() { 5046 // The template parameters of an encoding are unrelated to those of the 5047 // enclosing context. 5048 class SaveTemplateParams { 5049 AbstractManglingParser *Parser; 5050 decltype(TemplateParams) OldParams; 5051 decltype(OuterTemplateParams) OldOuterParams; 5052 5053 public: 5054 SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) { 5055 OldParams = std::move(Parser->TemplateParams); 5056 OldOuterParams = std::move(Parser->OuterTemplateParams); 5057 Parser->TemplateParams.clear(); 5058 Parser->OuterTemplateParams.clear(); 5059 } 5060 ~SaveTemplateParams() { 5061 Parser->TemplateParams = std::move(OldParams); 5062 Parser->OuterTemplateParams = std::move(OldOuterParams); 5063 } 5064 } SaveTemplateParams(this); 5065 5066 if (look() == 'G' || look() == 'T') 5067 return getDerived().parseSpecialName(); 5068 5069 auto IsEndOfEncoding = [&] { 5070 // The set of chars that can potentially follow an <encoding> (none of which 5071 // can start a <type>). Enumerating these allows us to avoid speculative 5072 // parsing. 5073 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_'; 5074 }; 5075 5076 NameState NameInfo(this); 5077 Node *Name = getDerived().parseName(&NameInfo); 5078 if (Name == nullptr) 5079 return nullptr; 5080 5081 if (resolveForwardTemplateRefs(NameInfo)) 5082 return nullptr; 5083 5084 if (IsEndOfEncoding()) 5085 return Name; 5086 5087 Node *Attrs = nullptr; 5088 if (consumeIf("Ua9enable_ifI")) { 5089 size_t BeforeArgs = Names.size(); 5090 while (!consumeIf('E')) { 5091 Node *Arg = getDerived().parseTemplateArg(); 5092 if (Arg == nullptr) 5093 return nullptr; 5094 Names.push_back(Arg); 5095 } 5096 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs)); 5097 if (!Attrs) 5098 return nullptr; 5099 } 5100 5101 Node *ReturnType = nullptr; 5102 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) { 5103 ReturnType = getDerived().parseType(); 5104 if (ReturnType == nullptr) 5105 return nullptr; 5106 } 5107 5108 if (consumeIf('v')) 5109 return make<FunctionEncoding>(ReturnType, Name, NodeArray(), 5110 Attrs, NameInfo.CVQualifiers, 5111 NameInfo.ReferenceQualifier); 5112 5113 size_t ParamsBegin = Names.size(); 5114 do { 5115 Node *Ty = getDerived().parseType(); 5116 if (Ty == nullptr) 5117 return nullptr; 5118 Names.push_back(Ty); 5119 } while (!IsEndOfEncoding()); 5120 5121 return make<FunctionEncoding>(ReturnType, Name, 5122 popTrailingNodeArray(ParamsBegin), 5123 Attrs, NameInfo.CVQualifiers, 5124 NameInfo.ReferenceQualifier); 5125 } 5126 5127 template <class Float> 5128 struct FloatData; 5129 5130 template <> 5131 struct FloatData<float> 5132 { 5133 static const size_t mangled_size = 8; 5134 static const size_t max_demangled_size = 24; 5135 static constexpr const char* spec = "%af"; 5136 }; 5137 5138 template <> 5139 struct FloatData<double> 5140 { 5141 static const size_t mangled_size = 16; 5142 static const size_t max_demangled_size = 32; 5143 static constexpr const char* spec = "%a"; 5144 }; 5145 5146 template <> 5147 struct FloatData<long double> 5148 { 5149 #if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \ 5150 defined(__wasm__) 5151 static const size_t mangled_size = 32; 5152 #elif defined(__arm__) || defined(__mips__) || defined(__hexagon__) 5153 static const size_t mangled_size = 16; 5154 #else 5155 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms 5156 #endif 5157 // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes. 5158 // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits. 5159 // Negatives are one character longer than positives. 5160 // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the 5161 // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128. 5162 static const size_t max_demangled_size = 42; 5163 static constexpr const char *spec = "%LaL"; 5164 }; 5165 5166 template <typename Alloc, typename Derived> 5167 template <class Float> 5168 Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() { 5169 const size_t N = FloatData<Float>::mangled_size; 5170 if (numLeft() <= N) 5171 return nullptr; 5172 StringView Data(First, First + N); 5173 for (char C : Data) 5174 if (!std::isxdigit(C)) 5175 return nullptr; 5176 First += N; 5177 if (!consumeIf('E')) 5178 return nullptr; 5179 return make<FloatLiteralImpl<Float>>(Data); 5180 } 5181 5182 // <seq-id> ::= <0-9A-Z>+ 5183 template <typename Alloc, typename Derived> 5184 bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) { 5185 if (!(look() >= '0' && look() <= '9') && 5186 !(look() >= 'A' && look() <= 'Z')) 5187 return true; 5188 5189 size_t Id = 0; 5190 while (true) { 5191 if (look() >= '0' && look() <= '9') { 5192 Id *= 36; 5193 Id += static_cast<size_t>(look() - '0'); 5194 } else if (look() >= 'A' && look() <= 'Z') { 5195 Id *= 36; 5196 Id += static_cast<size_t>(look() - 'A') + 10; 5197 } else { 5198 *Out = Id; 5199 return false; 5200 } 5201 ++First; 5202 } 5203 } 5204 5205 // <substitution> ::= S <seq-id> _ 5206 // ::= S_ 5207 // <substitution> ::= Sa # ::std::allocator 5208 // <substitution> ::= Sb # ::std::basic_string 5209 // <substitution> ::= Ss # ::std::basic_string < char, 5210 // ::std::char_traits<char>, 5211 // ::std::allocator<char> > 5212 // <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> > 5213 // <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> > 5214 // <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> > 5215 // The St case is handled specially in parseNestedName. 5216 template <typename Derived, typename Alloc> 5217 Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() { 5218 if (!consumeIf('S')) 5219 return nullptr; 5220 5221 if (look() >= 'a' && look() <= 'z') { 5222 SpecialSubKind Kind; 5223 switch (look()) { 5224 case 'a': 5225 Kind = SpecialSubKind::allocator; 5226 break; 5227 case 'b': 5228 Kind = SpecialSubKind::basic_string; 5229 break; 5230 case 'd': 5231 Kind = SpecialSubKind::iostream; 5232 break; 5233 case 'i': 5234 Kind = SpecialSubKind::istream; 5235 break; 5236 case 'o': 5237 Kind = SpecialSubKind::ostream; 5238 break; 5239 case 's': 5240 Kind = SpecialSubKind::string; 5241 break; 5242 default: 5243 return nullptr; 5244 } 5245 ++First; 5246 auto *SpecialSub = make<SpecialSubstitution>(Kind); 5247 if (!SpecialSub) 5248 return nullptr; 5249 5250 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution> 5251 // has ABI tags, the tags are appended to the substitution; the result is a 5252 // substitutable component. 5253 Node *WithTags = getDerived().parseAbiTags(SpecialSub); 5254 if (WithTags != SpecialSub) { 5255 Subs.push_back(WithTags); 5256 SpecialSub = WithTags; 5257 } 5258 return SpecialSub; 5259 } 5260 5261 // ::= S_ 5262 if (consumeIf('_')) { 5263 if (Subs.empty()) 5264 return nullptr; 5265 return Subs[0]; 5266 } 5267 5268 // ::= S <seq-id> _ 5269 size_t Index = 0; 5270 if (parseSeqId(&Index)) 5271 return nullptr; 5272 ++Index; 5273 if (!consumeIf('_') || Index >= Subs.size()) 5274 return nullptr; 5275 return Subs[Index]; 5276 } 5277 5278 // <template-param> ::= T_ # first template parameter 5279 // ::= T <parameter-2 non-negative number> _ 5280 // ::= TL <level-1> __ 5281 // ::= TL <level-1> _ <parameter-2 non-negative number> _ 5282 template <typename Derived, typename Alloc> 5283 Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() { 5284 if (!consumeIf('T')) 5285 return nullptr; 5286 5287 size_t Level = 0; 5288 if (consumeIf('L')) { 5289 if (parsePositiveInteger(&Level)) 5290 return nullptr; 5291 ++Level; 5292 if (!consumeIf('_')) 5293 return nullptr; 5294 } 5295 5296 size_t Index = 0; 5297 if (!consumeIf('_')) { 5298 if (parsePositiveInteger(&Index)) 5299 return nullptr; 5300 ++Index; 5301 if (!consumeIf('_')) 5302 return nullptr; 5303 } 5304 5305 // If we're in a context where this <template-param> refers to a 5306 // <template-arg> further ahead in the mangled name (currently just conversion 5307 // operator types), then we should only look it up in the right context. 5308 // This can only happen at the outermost level. 5309 if (PermitForwardTemplateReferences && Level == 0) { 5310 Node *ForwardRef = make<ForwardTemplateReference>(Index); 5311 if (!ForwardRef) 5312 return nullptr; 5313 assert(ForwardRef->getKind() == Node::KForwardTemplateReference); 5314 ForwardTemplateRefs.push_back( 5315 static_cast<ForwardTemplateReference *>(ForwardRef)); 5316 return ForwardRef; 5317 } 5318 5319 if (Level >= TemplateParams.size() || !TemplateParams[Level] || 5320 Index >= TemplateParams[Level]->size()) { 5321 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter 5322 // list are mangled as the corresponding artificial template type parameter. 5323 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) { 5324 // This will be popped by the ScopedTemplateParamList in 5325 // parseUnnamedTypeName. 5326 if (Level == TemplateParams.size()) 5327 TemplateParams.push_back(nullptr); 5328 return make<NameType>("auto"); 5329 } 5330 5331 return nullptr; 5332 } 5333 5334 return (*TemplateParams[Level])[Index]; 5335 } 5336 5337 // <template-param-decl> ::= Ty # type parameter 5338 // ::= Tn <type> # non-type parameter 5339 // ::= Tt <template-param-decl>* E # template parameter 5340 // ::= Tp <template-param-decl> # parameter pack 5341 template <typename Derived, typename Alloc> 5342 Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() { 5343 auto InventTemplateParamName = [&](TemplateParamKind Kind) { 5344 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++; 5345 Node *N = make<SyntheticTemplateParamName>(Kind, Index); 5346 if (N) TemplateParams.back()->push_back(N); 5347 return N; 5348 }; 5349 5350 if (consumeIf("Ty")) { 5351 Node *Name = InventTemplateParamName(TemplateParamKind::Type); 5352 if (!Name) 5353 return nullptr; 5354 return make<TypeTemplateParamDecl>(Name); 5355 } 5356 5357 if (consumeIf("Tn")) { 5358 Node *Name = InventTemplateParamName(TemplateParamKind::NonType); 5359 if (!Name) 5360 return nullptr; 5361 Node *Type = parseType(); 5362 if (!Type) 5363 return nullptr; 5364 return make<NonTypeTemplateParamDecl>(Name, Type); 5365 } 5366 5367 if (consumeIf("Tt")) { 5368 Node *Name = InventTemplateParamName(TemplateParamKind::Template); 5369 if (!Name) 5370 return nullptr; 5371 size_t ParamsBegin = Names.size(); 5372 ScopedTemplateParamList TemplateTemplateParamParams(this); 5373 while (!consumeIf("E")) { 5374 Node *P = parseTemplateParamDecl(); 5375 if (!P) 5376 return nullptr; 5377 Names.push_back(P); 5378 } 5379 NodeArray Params = popTrailingNodeArray(ParamsBegin); 5380 return make<TemplateTemplateParamDecl>(Name, Params); 5381 } 5382 5383 if (consumeIf("Tp")) { 5384 Node *P = parseTemplateParamDecl(); 5385 if (!P) 5386 return nullptr; 5387 return make<TemplateParamPackDecl>(P); 5388 } 5389 5390 return nullptr; 5391 } 5392 5393 // <template-arg> ::= <type> # type or template 5394 // ::= X <expression> E # expression 5395 // ::= <expr-primary> # simple expressions 5396 // ::= J <template-arg>* E # argument pack 5397 // ::= LZ <encoding> E # extension 5398 template <typename Derived, typename Alloc> 5399 Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() { 5400 switch (look()) { 5401 case 'X': { 5402 ++First; 5403 Node *Arg = getDerived().parseExpr(); 5404 if (Arg == nullptr || !consumeIf('E')) 5405 return nullptr; 5406 return Arg; 5407 } 5408 case 'J': { 5409 ++First; 5410 size_t ArgsBegin = Names.size(); 5411 while (!consumeIf('E')) { 5412 Node *Arg = getDerived().parseTemplateArg(); 5413 if (Arg == nullptr) 5414 return nullptr; 5415 Names.push_back(Arg); 5416 } 5417 NodeArray Args = popTrailingNodeArray(ArgsBegin); 5418 return make<TemplateArgumentPack>(Args); 5419 } 5420 case 'L': { 5421 // ::= LZ <encoding> E # extension 5422 if (look(1) == 'Z') { 5423 First += 2; 5424 Node *Arg = getDerived().parseEncoding(); 5425 if (Arg == nullptr || !consumeIf('E')) 5426 return nullptr; 5427 return Arg; 5428 } 5429 // ::= <expr-primary> # simple expressions 5430 return getDerived().parseExprPrimary(); 5431 } 5432 default: 5433 return getDerived().parseType(); 5434 } 5435 } 5436 5437 // <template-args> ::= I <template-arg>* E 5438 // extension, the abi says <template-arg>+ 5439 template <typename Derived, typename Alloc> 5440 Node * 5441 AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) { 5442 if (!consumeIf('I')) 5443 return nullptr; 5444 5445 // <template-params> refer to the innermost <template-args>. Clear out any 5446 // outer args that we may have inserted into TemplateParams. 5447 if (TagTemplates) { 5448 TemplateParams.clear(); 5449 TemplateParams.push_back(&OuterTemplateParams); 5450 OuterTemplateParams.clear(); 5451 } 5452 5453 size_t ArgsBegin = Names.size(); 5454 while (!consumeIf('E')) { 5455 if (TagTemplates) { 5456 auto OldParams = std::move(TemplateParams); 5457 Node *Arg = getDerived().parseTemplateArg(); 5458 TemplateParams = std::move(OldParams); 5459 if (Arg == nullptr) 5460 return nullptr; 5461 Names.push_back(Arg); 5462 Node *TableEntry = Arg; 5463 if (Arg->getKind() == Node::KTemplateArgumentPack) { 5464 TableEntry = make<ParameterPack>( 5465 static_cast<TemplateArgumentPack*>(TableEntry)->getElements()); 5466 if (!TableEntry) 5467 return nullptr; 5468 } 5469 TemplateParams.back()->push_back(TableEntry); 5470 } else { 5471 Node *Arg = getDerived().parseTemplateArg(); 5472 if (Arg == nullptr) 5473 return nullptr; 5474 Names.push_back(Arg); 5475 } 5476 } 5477 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin)); 5478 } 5479 5480 // <mangled-name> ::= _Z <encoding> 5481 // ::= <type> 5482 // extension ::= ___Z <encoding> _block_invoke 5483 // extension ::= ___Z <encoding> _block_invoke<decimal-digit>+ 5484 // extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+ 5485 template <typename Derived, typename Alloc> 5486 Node *AbstractManglingParser<Derived, Alloc>::parse() { 5487 if (consumeIf("_Z") || consumeIf("__Z")) { 5488 Node *Encoding = getDerived().parseEncoding(); 5489 if (Encoding == nullptr) 5490 return nullptr; 5491 if (look() == '.') { 5492 Encoding = make<DotSuffix>(Encoding, StringView(First, Last)); 5493 First = Last; 5494 } 5495 if (numLeft() != 0) 5496 return nullptr; 5497 return Encoding; 5498 } 5499 5500 if (consumeIf("___Z") || consumeIf("____Z")) { 5501 Node *Encoding = getDerived().parseEncoding(); 5502 if (Encoding == nullptr || !consumeIf("_block_invoke")) 5503 return nullptr; 5504 bool RequireNumber = consumeIf('_'); 5505 if (parseNumber().empty() && RequireNumber) 5506 return nullptr; 5507 if (look() == '.') 5508 First = Last; 5509 if (numLeft() != 0) 5510 return nullptr; 5511 return make<SpecialName>("invocation function for block in ", Encoding); 5512 } 5513 5514 Node *Ty = getDerived().parseType(); 5515 if (numLeft() != 0) 5516 return nullptr; 5517 return Ty; 5518 } 5519 5520 template <typename Alloc> 5521 struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> { 5522 using AbstractManglingParser<ManglingParser<Alloc>, 5523 Alloc>::AbstractManglingParser; 5524 }; 5525 5526 DEMANGLE_NAMESPACE_END 5527 5528 #endif // DEMANGLE_ITANIUMDEMANGLE_H 5529