1 //===--- ItaniumMangle.cpp - Itanium C++ Name Mangling ----------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Implements C++ name mangling according to the Itanium C++ ABI, 11 // which is used in GCC 3.2 and newer (and many compilers that are 12 // ABI-compatible with GCC): 13 // 14 // http://mentorembedded.github.io/cxx-abi/abi.html#mangling 15 // 16 //===----------------------------------------------------------------------===// 17 #include "clang/AST/Mangle.h" 18 #include "clang/AST/ASTContext.h" 19 #include "clang/AST/Attr.h" 20 #include "clang/AST/Decl.h" 21 #include "clang/AST/DeclCXX.h" 22 #include "clang/AST/DeclObjC.h" 23 #include "clang/AST/DeclOpenMP.h" 24 #include "clang/AST/DeclTemplate.h" 25 #include "clang/AST/Expr.h" 26 #include "clang/AST/ExprCXX.h" 27 #include "clang/AST/ExprObjC.h" 28 #include "clang/AST/TypeLoc.h" 29 #include "clang/Basic/ABI.h" 30 #include "clang/Basic/SourceManager.h" 31 #include "clang/Basic/TargetInfo.h" 32 #include "llvm/ADT/StringExtras.h" 33 #include "llvm/Support/ErrorHandling.h" 34 #include "llvm/Support/raw_ostream.h" 35 36 #define MANGLE_CHECKER 0 37 38 #if MANGLE_CHECKER 39 #include <cxxabi.h> 40 #endif 41 42 using namespace clang; 43 44 namespace { 45 46 /// Retrieve the declaration context that should be used when mangling the given 47 /// declaration. 48 static const DeclContext *getEffectiveDeclContext(const Decl *D) { 49 // The ABI assumes that lambda closure types that occur within 50 // default arguments live in the context of the function. However, due to 51 // the way in which Clang parses and creates function declarations, this is 52 // not the case: the lambda closure type ends up living in the context 53 // where the function itself resides, because the function declaration itself 54 // had not yet been created. Fix the context here. 55 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 56 if (RD->isLambda()) 57 if (ParmVarDecl *ContextParam 58 = dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl())) 59 return ContextParam->getDeclContext(); 60 } 61 62 // Perform the same check for block literals. 63 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) { 64 if (ParmVarDecl *ContextParam 65 = dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl())) 66 return ContextParam->getDeclContext(); 67 } 68 69 const DeclContext *DC = D->getDeclContext(); 70 if (isa<CapturedDecl>(DC) || isa<OMPDeclareReductionDecl>(DC)) { 71 return getEffectiveDeclContext(cast<Decl>(DC)); 72 } 73 74 if (const auto *VD = dyn_cast<VarDecl>(D)) 75 if (VD->isExternC()) 76 return VD->getASTContext().getTranslationUnitDecl(); 77 78 if (const auto *FD = dyn_cast<FunctionDecl>(D)) 79 if (FD->isExternC()) 80 return FD->getASTContext().getTranslationUnitDecl(); 81 82 return DC->getRedeclContext(); 83 } 84 85 static const DeclContext *getEffectiveParentContext(const DeclContext *DC) { 86 return getEffectiveDeclContext(cast<Decl>(DC)); 87 } 88 89 static bool isLocalContainerContext(const DeclContext *DC) { 90 return isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC) || isa<BlockDecl>(DC); 91 } 92 93 static const RecordDecl *GetLocalClassDecl(const Decl *D) { 94 const DeclContext *DC = getEffectiveDeclContext(D); 95 while (!DC->isNamespace() && !DC->isTranslationUnit()) { 96 if (isLocalContainerContext(DC)) 97 return dyn_cast<RecordDecl>(D); 98 D = cast<Decl>(DC); 99 DC = getEffectiveDeclContext(D); 100 } 101 return nullptr; 102 } 103 104 static const FunctionDecl *getStructor(const FunctionDecl *fn) { 105 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate()) 106 return ftd->getTemplatedDecl(); 107 108 return fn; 109 } 110 111 static const NamedDecl *getStructor(const NamedDecl *decl) { 112 const FunctionDecl *fn = dyn_cast_or_null<FunctionDecl>(decl); 113 return (fn ? getStructor(fn) : decl); 114 } 115 116 static bool isLambda(const NamedDecl *ND) { 117 const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(ND); 118 if (!Record) 119 return false; 120 121 return Record->isLambda(); 122 } 123 124 static const unsigned UnknownArity = ~0U; 125 126 class ItaniumMangleContextImpl : public ItaniumMangleContext { 127 typedef std::pair<const DeclContext*, IdentifierInfo*> DiscriminatorKeyTy; 128 llvm::DenseMap<DiscriminatorKeyTy, unsigned> Discriminator; 129 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier; 130 131 public: 132 explicit ItaniumMangleContextImpl(ASTContext &Context, 133 DiagnosticsEngine &Diags) 134 : ItaniumMangleContext(Context, Diags) {} 135 136 /// @name Mangler Entry Points 137 /// @{ 138 139 bool shouldMangleCXXName(const NamedDecl *D) override; 140 bool shouldMangleStringLiteral(const StringLiteral *) override { 141 return false; 142 } 143 void mangleCXXName(const NamedDecl *D, raw_ostream &) override; 144 void mangleThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk, 145 raw_ostream &) override; 146 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, 147 const ThisAdjustment &ThisAdjustment, 148 raw_ostream &) override; 149 void mangleReferenceTemporary(const VarDecl *D, unsigned ManglingNumber, 150 raw_ostream &) override; 151 void mangleCXXVTable(const CXXRecordDecl *RD, raw_ostream &) override; 152 void mangleCXXVTT(const CXXRecordDecl *RD, raw_ostream &) override; 153 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, 154 const CXXRecordDecl *Type, raw_ostream &) override; 155 void mangleCXXRTTI(QualType T, raw_ostream &) override; 156 void mangleCXXRTTIName(QualType T, raw_ostream &) override; 157 void mangleTypeName(QualType T, raw_ostream &) override; 158 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, 159 raw_ostream &) override; 160 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, 161 raw_ostream &) override; 162 163 void mangleCXXCtorComdat(const CXXConstructorDecl *D, raw_ostream &) override; 164 void mangleCXXDtorComdat(const CXXDestructorDecl *D, raw_ostream &) override; 165 void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &) override; 166 void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out) override; 167 void mangleDynamicAtExitDestructor(const VarDecl *D, 168 raw_ostream &Out) override; 169 void mangleSEHFilterExpression(const NamedDecl *EnclosingDecl, 170 raw_ostream &Out) override; 171 void mangleSEHFinallyBlock(const NamedDecl *EnclosingDecl, 172 raw_ostream &Out) override; 173 void mangleItaniumThreadLocalInit(const VarDecl *D, raw_ostream &) override; 174 void mangleItaniumThreadLocalWrapper(const VarDecl *D, 175 raw_ostream &) override; 176 177 void mangleStringLiteral(const StringLiteral *, raw_ostream &) override; 178 179 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) { 180 // Lambda closure types are already numbered. 181 if (isLambda(ND)) 182 return false; 183 184 // Anonymous tags are already numbered. 185 if (const TagDecl *Tag = dyn_cast<TagDecl>(ND)) { 186 if (Tag->getName().empty() && !Tag->getTypedefNameForAnonDecl()) 187 return false; 188 } 189 190 // Use the canonical number for externally visible decls. 191 if (ND->isExternallyVisible()) { 192 unsigned discriminator = getASTContext().getManglingNumber(ND); 193 if (discriminator == 1) 194 return false; 195 disc = discriminator - 2; 196 return true; 197 } 198 199 // Make up a reasonable number for internal decls. 200 unsigned &discriminator = Uniquifier[ND]; 201 if (!discriminator) { 202 const DeclContext *DC = getEffectiveDeclContext(ND); 203 discriminator = ++Discriminator[std::make_pair(DC, ND->getIdentifier())]; 204 } 205 if (discriminator == 1) 206 return false; 207 disc = discriminator-2; 208 return true; 209 } 210 /// @} 211 }; 212 213 /// Manage the mangling of a single name. 214 class CXXNameMangler { 215 ItaniumMangleContextImpl &Context; 216 raw_ostream &Out; 217 bool NullOut = false; 218 /// In the "DisableDerivedAbiTags" mode derived ABI tags are not calculated. 219 /// This mode is used when mangler creates another mangler recursively to 220 /// calculate ABI tags for the function return value or the variable type. 221 /// Also it is required to avoid infinite recursion in some cases. 222 bool DisableDerivedAbiTags = false; 223 224 /// The "structor" is the top-level declaration being mangled, if 225 /// that's not a template specialization; otherwise it's the pattern 226 /// for that specialization. 227 const NamedDecl *Structor; 228 unsigned StructorType; 229 230 /// The next substitution sequence number. 231 unsigned SeqID; 232 233 class FunctionTypeDepthState { 234 unsigned Bits; 235 236 enum { InResultTypeMask = 1 }; 237 238 public: 239 FunctionTypeDepthState() : Bits(0) {} 240 241 /// The number of function types we're inside. 242 unsigned getDepth() const { 243 return Bits >> 1; 244 } 245 246 /// True if we're in the return type of the innermost function type. 247 bool isInResultType() const { 248 return Bits & InResultTypeMask; 249 } 250 251 FunctionTypeDepthState push() { 252 FunctionTypeDepthState tmp = *this; 253 Bits = (Bits & ~InResultTypeMask) + 2; 254 return tmp; 255 } 256 257 void enterResultType() { 258 Bits |= InResultTypeMask; 259 } 260 261 void leaveResultType() { 262 Bits &= ~InResultTypeMask; 263 } 264 265 void pop(FunctionTypeDepthState saved) { 266 assert(getDepth() == saved.getDepth() + 1); 267 Bits = saved.Bits; 268 } 269 270 } FunctionTypeDepth; 271 272 // abi_tag is a gcc attribute, taking one or more strings called "tags". 273 // The goal is to annotate against which version of a library an object was 274 // built and to be able to provide backwards compatibility ("dual abi"). 275 // For more information see docs/ItaniumMangleAbiTags.rst. 276 typedef SmallVector<StringRef, 4> AbiTagList; 277 278 // State to gather all implicit and explicit tags used in a mangled name. 279 // Must always have an instance of this while emitting any name to keep 280 // track. 281 class AbiTagState final { 282 public: 283 explicit AbiTagState(AbiTagState *&Head) : LinkHead(Head) { 284 Parent = LinkHead; 285 LinkHead = this; 286 } 287 288 // No copy, no move. 289 AbiTagState(const AbiTagState &) = delete; 290 AbiTagState &operator=(const AbiTagState &) = delete; 291 292 ~AbiTagState() { pop(); } 293 294 void write(raw_ostream &Out, const NamedDecl *ND, 295 const AbiTagList *AdditionalAbiTags) { 296 ND = cast<NamedDecl>(ND->getCanonicalDecl()); 297 if (!isa<FunctionDecl>(ND) && !isa<VarDecl>(ND)) { 298 assert( 299 !AdditionalAbiTags && 300 "only function and variables need a list of additional abi tags"); 301 if (const auto *NS = dyn_cast<NamespaceDecl>(ND)) { 302 if (const auto *AbiTag = NS->getAttr<AbiTagAttr>()) { 303 UsedAbiTags.insert(UsedAbiTags.end(), AbiTag->tags().begin(), 304 AbiTag->tags().end()); 305 } 306 // Don't emit abi tags for namespaces. 307 return; 308 } 309 } 310 311 AbiTagList TagList; 312 if (const auto *AbiTag = ND->getAttr<AbiTagAttr>()) { 313 UsedAbiTags.insert(UsedAbiTags.end(), AbiTag->tags().begin(), 314 AbiTag->tags().end()); 315 TagList.insert(TagList.end(), AbiTag->tags().begin(), 316 AbiTag->tags().end()); 317 } 318 319 if (AdditionalAbiTags) { 320 UsedAbiTags.insert(UsedAbiTags.end(), AdditionalAbiTags->begin(), 321 AdditionalAbiTags->end()); 322 TagList.insert(TagList.end(), AdditionalAbiTags->begin(), 323 AdditionalAbiTags->end()); 324 } 325 326 std::sort(TagList.begin(), TagList.end()); 327 TagList.erase(std::unique(TagList.begin(), TagList.end()), TagList.end()); 328 329 writeSortedUniqueAbiTags(Out, TagList); 330 } 331 332 const AbiTagList &getUsedAbiTags() const { return UsedAbiTags; } 333 void setUsedAbiTags(const AbiTagList &AbiTags) { 334 UsedAbiTags = AbiTags; 335 } 336 337 const AbiTagList &getEmittedAbiTags() const { 338 return EmittedAbiTags; 339 } 340 341 const AbiTagList &getSortedUniqueUsedAbiTags() { 342 std::sort(UsedAbiTags.begin(), UsedAbiTags.end()); 343 UsedAbiTags.erase(std::unique(UsedAbiTags.begin(), UsedAbiTags.end()), 344 UsedAbiTags.end()); 345 return UsedAbiTags; 346 } 347 348 private: 349 //! All abi tags used implicitly or explicitly. 350 AbiTagList UsedAbiTags; 351 //! All explicit abi tags (i.e. not from namespace). 352 AbiTagList EmittedAbiTags; 353 354 AbiTagState *&LinkHead; 355 AbiTagState *Parent = nullptr; 356 357 void pop() { 358 assert(LinkHead == this && 359 "abi tag link head must point to us on destruction"); 360 if (Parent) { 361 Parent->UsedAbiTags.insert(Parent->UsedAbiTags.end(), 362 UsedAbiTags.begin(), UsedAbiTags.end()); 363 Parent->EmittedAbiTags.insert(Parent->EmittedAbiTags.end(), 364 EmittedAbiTags.begin(), 365 EmittedAbiTags.end()); 366 } 367 LinkHead = Parent; 368 } 369 370 void writeSortedUniqueAbiTags(raw_ostream &Out, const AbiTagList &AbiTags) { 371 for (const auto &Tag : AbiTags) { 372 EmittedAbiTags.push_back(Tag); 373 Out << "B"; 374 Out << Tag.size(); 375 Out << Tag; 376 } 377 } 378 }; 379 380 AbiTagState *AbiTags = nullptr; 381 AbiTagState AbiTagsRoot; 382 383 llvm::DenseMap<uintptr_t, unsigned> Substitutions; 384 385 ASTContext &getASTContext() const { return Context.getASTContext(); } 386 387 public: 388 CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_, 389 const NamedDecl *D = nullptr, bool NullOut_ = false) 390 : Context(C), Out(Out_), NullOut(NullOut_), Structor(getStructor(D)), 391 StructorType(0), SeqID(0), AbiTagsRoot(AbiTags) { 392 // These can't be mangled without a ctor type or dtor type. 393 assert(!D || (!isa<CXXDestructorDecl>(D) && 394 !isa<CXXConstructorDecl>(D))); 395 } 396 CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_, 397 const CXXConstructorDecl *D, CXXCtorType Type) 398 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type), 399 SeqID(0), AbiTagsRoot(AbiTags) { } 400 CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_, 401 const CXXDestructorDecl *D, CXXDtorType Type) 402 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type), 403 SeqID(0), AbiTagsRoot(AbiTags) { } 404 405 CXXNameMangler(CXXNameMangler &Outer, raw_ostream &Out_) 406 : Context(Outer.Context), Out(Out_), NullOut(false), 407 Structor(Outer.Structor), StructorType(Outer.StructorType), 408 SeqID(Outer.SeqID), FunctionTypeDepth(Outer.FunctionTypeDepth), 409 AbiTagsRoot(AbiTags), Substitutions(Outer.Substitutions) {} 410 411 CXXNameMangler(CXXNameMangler &Outer, llvm::raw_null_ostream &Out_) 412 : Context(Outer.Context), Out(Out_), NullOut(true), 413 Structor(Outer.Structor), StructorType(Outer.StructorType), 414 SeqID(Outer.SeqID), FunctionTypeDepth(Outer.FunctionTypeDepth), 415 AbiTagsRoot(AbiTags), Substitutions(Outer.Substitutions) {} 416 417 #if MANGLE_CHECKER 418 ~CXXNameMangler() { 419 if (Out.str()[0] == '\01') 420 return; 421 422 int status = 0; 423 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status); 424 assert(status == 0 && "Could not demangle mangled name!"); 425 free(result); 426 } 427 #endif 428 raw_ostream &getStream() { return Out; } 429 430 void disableDerivedAbiTags() { DisableDerivedAbiTags = true; } 431 static bool shouldHaveAbiTags(ItaniumMangleContextImpl &C, const VarDecl *VD); 432 433 void mangle(const NamedDecl *D); 434 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual); 435 void mangleNumber(const llvm::APSInt &I); 436 void mangleNumber(int64_t Number); 437 void mangleFloat(const llvm::APFloat &F); 438 void mangleFunctionEncoding(const FunctionDecl *FD); 439 void mangleSeqID(unsigned SeqID); 440 void mangleName(const NamedDecl *ND); 441 void mangleType(QualType T); 442 void mangleNameOrStandardSubstitution(const NamedDecl *ND); 443 444 private: 445 446 bool mangleSubstitution(const NamedDecl *ND); 447 bool mangleSubstitution(QualType T); 448 bool mangleSubstitution(TemplateName Template); 449 bool mangleSubstitution(uintptr_t Ptr); 450 451 void mangleExistingSubstitution(TemplateName name); 452 453 bool mangleStandardSubstitution(const NamedDecl *ND); 454 455 void addSubstitution(const NamedDecl *ND) { 456 ND = cast<NamedDecl>(ND->getCanonicalDecl()); 457 458 addSubstitution(reinterpret_cast<uintptr_t>(ND)); 459 } 460 void addSubstitution(QualType T); 461 void addSubstitution(TemplateName Template); 462 void addSubstitution(uintptr_t Ptr); 463 // Destructive copy substitutions from other mangler. 464 void extendSubstitutions(CXXNameMangler* Other); 465 466 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier, 467 bool recursive = false); 468 void mangleUnresolvedName(NestedNameSpecifier *qualifier, 469 DeclarationName name, 470 const TemplateArgumentLoc *TemplateArgs, 471 unsigned NumTemplateArgs, 472 unsigned KnownArity = UnknownArity); 473 474 void mangleFunctionEncodingBareType(const FunctionDecl *FD); 475 476 void mangleNameWithAbiTags(const NamedDecl *ND, 477 const AbiTagList *AdditionalAbiTags); 478 void mangleTemplateName(const TemplateDecl *TD, 479 const TemplateArgument *TemplateArgs, 480 unsigned NumTemplateArgs); 481 void mangleUnqualifiedName(const NamedDecl *ND, 482 const AbiTagList *AdditionalAbiTags) { 483 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity, 484 AdditionalAbiTags); 485 } 486 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name, 487 unsigned KnownArity, 488 const AbiTagList *AdditionalAbiTags); 489 void mangleUnscopedName(const NamedDecl *ND, 490 const AbiTagList *AdditionalAbiTags); 491 void mangleUnscopedTemplateName(const TemplateDecl *ND, 492 const AbiTagList *AdditionalAbiTags); 493 void mangleUnscopedTemplateName(TemplateName, 494 const AbiTagList *AdditionalAbiTags); 495 void mangleSourceName(const IdentifierInfo *II); 496 void mangleRegCallName(const IdentifierInfo *II); 497 void mangleSourceNameWithAbiTags( 498 const NamedDecl *ND, const AbiTagList *AdditionalAbiTags = nullptr); 499 void mangleLocalName(const Decl *D, 500 const AbiTagList *AdditionalAbiTags); 501 void mangleBlockForPrefix(const BlockDecl *Block); 502 void mangleUnqualifiedBlock(const BlockDecl *Block); 503 void mangleLambda(const CXXRecordDecl *Lambda); 504 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC, 505 const AbiTagList *AdditionalAbiTags, 506 bool NoFunction=false); 507 void mangleNestedName(const TemplateDecl *TD, 508 const TemplateArgument *TemplateArgs, 509 unsigned NumTemplateArgs); 510 void manglePrefix(NestedNameSpecifier *qualifier); 511 void manglePrefix(const DeclContext *DC, bool NoFunction=false); 512 void manglePrefix(QualType type); 513 void mangleTemplatePrefix(const TemplateDecl *ND, bool NoFunction=false); 514 void mangleTemplatePrefix(TemplateName Template); 515 bool mangleUnresolvedTypeOrSimpleId(QualType DestroyedType, 516 StringRef Prefix = ""); 517 void mangleOperatorName(DeclarationName Name, unsigned Arity); 518 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity); 519 void mangleVendorQualifier(StringRef qualifier); 520 void mangleQualifiers(Qualifiers Quals); 521 void mangleRefQualifier(RefQualifierKind RefQualifier); 522 523 void mangleObjCMethodName(const ObjCMethodDecl *MD); 524 525 // Declare manglers for every type class. 526 #define ABSTRACT_TYPE(CLASS, PARENT) 527 #define NON_CANONICAL_TYPE(CLASS, PARENT) 528 #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T); 529 #include "clang/AST/TypeNodes.def" 530 531 void mangleType(const TagType*); 532 void mangleType(TemplateName); 533 static StringRef getCallingConvQualifierName(CallingConv CC); 534 void mangleExtParameterInfo(FunctionProtoType::ExtParameterInfo info); 535 void mangleExtFunctionInfo(const FunctionType *T); 536 void mangleBareFunctionType(const FunctionProtoType *T, bool MangleReturnType, 537 const FunctionDecl *FD = nullptr); 538 void mangleNeonVectorType(const VectorType *T); 539 void mangleAArch64NeonVectorType(const VectorType *T); 540 541 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value); 542 void mangleMemberExprBase(const Expr *base, bool isArrow); 543 void mangleMemberExpr(const Expr *base, bool isArrow, 544 NestedNameSpecifier *qualifier, 545 NamedDecl *firstQualifierLookup, 546 DeclarationName name, 547 const TemplateArgumentLoc *TemplateArgs, 548 unsigned NumTemplateArgs, 549 unsigned knownArity); 550 void mangleCastExpression(const Expr *E, StringRef CastEncoding); 551 void mangleInitListElements(const InitListExpr *InitList); 552 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity); 553 void mangleCXXCtorType(CXXCtorType T, const CXXRecordDecl *InheritedFrom); 554 void mangleCXXDtorType(CXXDtorType T); 555 556 void mangleTemplateArgs(const TemplateArgumentLoc *TemplateArgs, 557 unsigned NumTemplateArgs); 558 void mangleTemplateArgs(const TemplateArgument *TemplateArgs, 559 unsigned NumTemplateArgs); 560 void mangleTemplateArgs(const TemplateArgumentList &AL); 561 void mangleTemplateArg(TemplateArgument A); 562 563 void mangleTemplateParameter(unsigned Index); 564 565 void mangleFunctionParam(const ParmVarDecl *parm); 566 567 void writeAbiTags(const NamedDecl *ND, 568 const AbiTagList *AdditionalAbiTags); 569 570 // Returns sorted unique list of ABI tags. 571 AbiTagList makeFunctionReturnTypeTags(const FunctionDecl *FD); 572 // Returns sorted unique list of ABI tags. 573 AbiTagList makeVariableTypeTags(const VarDecl *VD); 574 }; 575 576 } 577 578 bool ItaniumMangleContextImpl::shouldMangleCXXName(const NamedDecl *D) { 579 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 580 if (FD) { 581 LanguageLinkage L = FD->getLanguageLinkage(); 582 // Overloadable functions need mangling. 583 if (FD->hasAttr<OverloadableAttr>()) 584 return true; 585 586 // "main" is not mangled. 587 if (FD->isMain()) 588 return false; 589 590 // C++ functions and those whose names are not a simple identifier need 591 // mangling. 592 if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage) 593 return true; 594 595 // C functions are not mangled. 596 if (L == CLanguageLinkage) 597 return false; 598 } 599 600 // Otherwise, no mangling is done outside C++ mode. 601 if (!getASTContext().getLangOpts().CPlusPlus) 602 return false; 603 604 const VarDecl *VD = dyn_cast<VarDecl>(D); 605 if (VD && !isa<DecompositionDecl>(D)) { 606 // C variables are not mangled. 607 if (VD->isExternC()) 608 return false; 609 610 // Variables at global scope with non-internal linkage are not mangled 611 const DeclContext *DC = getEffectiveDeclContext(D); 612 // Check for extern variable declared locally. 613 if (DC->isFunctionOrMethod() && D->hasLinkage()) 614 while (!DC->isNamespace() && !DC->isTranslationUnit()) 615 DC = getEffectiveParentContext(DC); 616 if (DC->isTranslationUnit() && D->getFormalLinkage() != InternalLinkage && 617 !CXXNameMangler::shouldHaveAbiTags(*this, VD) && 618 !isa<VarTemplateSpecializationDecl>(D)) 619 return false; 620 } 621 622 return true; 623 } 624 625 void CXXNameMangler::writeAbiTags(const NamedDecl *ND, 626 const AbiTagList *AdditionalAbiTags) { 627 assert(AbiTags && "require AbiTagState"); 628 AbiTags->write(Out, ND, DisableDerivedAbiTags ? nullptr : AdditionalAbiTags); 629 } 630 631 void CXXNameMangler::mangleSourceNameWithAbiTags( 632 const NamedDecl *ND, const AbiTagList *AdditionalAbiTags) { 633 mangleSourceName(ND->getIdentifier()); 634 writeAbiTags(ND, AdditionalAbiTags); 635 } 636 637 void CXXNameMangler::mangle(const NamedDecl *D) { 638 // <mangled-name> ::= _Z <encoding> 639 // ::= <data name> 640 // ::= <special-name> 641 Out << "_Z"; 642 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 643 mangleFunctionEncoding(FD); 644 else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 645 mangleName(VD); 646 else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(D)) 647 mangleName(IFD->getAnonField()); 648 else 649 mangleName(cast<FieldDecl>(D)); 650 } 651 652 void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { 653 // <encoding> ::= <function name> <bare-function-type> 654 655 // Don't mangle in the type if this isn't a decl we should typically mangle. 656 if (!Context.shouldMangleDeclName(FD)) { 657 mangleName(FD); 658 return; 659 } 660 661 AbiTagList ReturnTypeAbiTags = makeFunctionReturnTypeTags(FD); 662 if (ReturnTypeAbiTags.empty()) { 663 // There are no tags for return type, the simplest case. 664 mangleName(FD); 665 mangleFunctionEncodingBareType(FD); 666 return; 667 } 668 669 // Mangle function name and encoding to temporary buffer. 670 // We have to output name and encoding to the same mangler to get the same 671 // substitution as it will be in final mangling. 672 SmallString<256> FunctionEncodingBuf; 673 llvm::raw_svector_ostream FunctionEncodingStream(FunctionEncodingBuf); 674 CXXNameMangler FunctionEncodingMangler(*this, FunctionEncodingStream); 675 // Output name of the function. 676 FunctionEncodingMangler.disableDerivedAbiTags(); 677 FunctionEncodingMangler.mangleNameWithAbiTags(FD, nullptr); 678 679 // Remember length of the function name in the buffer. 680 size_t EncodingPositionStart = FunctionEncodingStream.str().size(); 681 FunctionEncodingMangler.mangleFunctionEncodingBareType(FD); 682 683 // Get tags from return type that are not present in function name or 684 // encoding. 685 const AbiTagList &UsedAbiTags = 686 FunctionEncodingMangler.AbiTagsRoot.getSortedUniqueUsedAbiTags(); 687 AbiTagList AdditionalAbiTags(ReturnTypeAbiTags.size()); 688 AdditionalAbiTags.erase( 689 std::set_difference(ReturnTypeAbiTags.begin(), ReturnTypeAbiTags.end(), 690 UsedAbiTags.begin(), UsedAbiTags.end(), 691 AdditionalAbiTags.begin()), 692 AdditionalAbiTags.end()); 693 694 // Output name with implicit tags and function encoding from temporary buffer. 695 mangleNameWithAbiTags(FD, &AdditionalAbiTags); 696 Out << FunctionEncodingStream.str().substr(EncodingPositionStart); 697 698 // Function encoding could create new substitutions so we have to add 699 // temp mangled substitutions to main mangler. 700 extendSubstitutions(&FunctionEncodingMangler); 701 } 702 703 void CXXNameMangler::mangleFunctionEncodingBareType(const FunctionDecl *FD) { 704 if (FD->hasAttr<EnableIfAttr>()) { 705 FunctionTypeDepthState Saved = FunctionTypeDepth.push(); 706 Out << "Ua9enable_ifI"; 707 // FIXME: specific_attr_iterator iterates in reverse order. Fix that and use 708 // it here. 709 for (AttrVec::const_reverse_iterator I = FD->getAttrs().rbegin(), 710 E = FD->getAttrs().rend(); 711 I != E; ++I) { 712 EnableIfAttr *EIA = dyn_cast<EnableIfAttr>(*I); 713 if (!EIA) 714 continue; 715 Out << 'X'; 716 mangleExpression(EIA->getCond()); 717 Out << 'E'; 718 } 719 Out << 'E'; 720 FunctionTypeDepth.pop(Saved); 721 } 722 723 // When mangling an inheriting constructor, the bare function type used is 724 // that of the inherited constructor. 725 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) 726 if (auto Inherited = CD->getInheritedConstructor()) 727 FD = Inherited.getConstructor(); 728 729 // Whether the mangling of a function type includes the return type depends on 730 // the context and the nature of the function. The rules for deciding whether 731 // the return type is included are: 732 // 733 // 1. Template functions (names or types) have return types encoded, with 734 // the exceptions listed below. 735 // 2. Function types not appearing as part of a function name mangling, 736 // e.g. parameters, pointer types, etc., have return type encoded, with the 737 // exceptions listed below. 738 // 3. Non-template function names do not have return types encoded. 739 // 740 // The exceptions mentioned in (1) and (2) above, for which the return type is 741 // never included, are 742 // 1. Constructors. 743 // 2. Destructors. 744 // 3. Conversion operator functions, e.g. operator int. 745 bool MangleReturnType = false; 746 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) { 747 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) || 748 isa<CXXConversionDecl>(FD))) 749 MangleReturnType = true; 750 751 // Mangle the type of the primary template. 752 FD = PrimaryTemplate->getTemplatedDecl(); 753 } 754 755 mangleBareFunctionType(FD->getType()->castAs<FunctionProtoType>(), 756 MangleReturnType, FD); 757 } 758 759 static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) { 760 while (isa<LinkageSpecDecl>(DC)) { 761 DC = getEffectiveParentContext(DC); 762 } 763 764 return DC; 765 } 766 767 /// Return whether a given namespace is the 'std' namespace. 768 static bool isStd(const NamespaceDecl *NS) { 769 if (!IgnoreLinkageSpecDecls(getEffectiveParentContext(NS)) 770 ->isTranslationUnit()) 771 return false; 772 773 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier(); 774 return II && II->isStr("std"); 775 } 776 777 // isStdNamespace - Return whether a given decl context is a toplevel 'std' 778 // namespace. 779 static bool isStdNamespace(const DeclContext *DC) { 780 if (!DC->isNamespace()) 781 return false; 782 783 return isStd(cast<NamespaceDecl>(DC)); 784 } 785 786 static const TemplateDecl * 787 isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) { 788 // Check if we have a function template. 789 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { 790 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) { 791 TemplateArgs = FD->getTemplateSpecializationArgs(); 792 return TD; 793 } 794 } 795 796 // Check if we have a class template. 797 if (const ClassTemplateSpecializationDecl *Spec = 798 dyn_cast<ClassTemplateSpecializationDecl>(ND)) { 799 TemplateArgs = &Spec->getTemplateArgs(); 800 return Spec->getSpecializedTemplate(); 801 } 802 803 // Check if we have a variable template. 804 if (const VarTemplateSpecializationDecl *Spec = 805 dyn_cast<VarTemplateSpecializationDecl>(ND)) { 806 TemplateArgs = &Spec->getTemplateArgs(); 807 return Spec->getSpecializedTemplate(); 808 } 809 810 return nullptr; 811 } 812 813 void CXXNameMangler::mangleName(const NamedDecl *ND) { 814 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) { 815 // Variables should have implicit tags from its type. 816 AbiTagList VariableTypeAbiTags = makeVariableTypeTags(VD); 817 if (VariableTypeAbiTags.empty()) { 818 // Simple case no variable type tags. 819 mangleNameWithAbiTags(VD, nullptr); 820 return; 821 } 822 823 // Mangle variable name to null stream to collect tags. 824 llvm::raw_null_ostream NullOutStream; 825 CXXNameMangler VariableNameMangler(*this, NullOutStream); 826 VariableNameMangler.disableDerivedAbiTags(); 827 VariableNameMangler.mangleNameWithAbiTags(VD, nullptr); 828 829 // Get tags from variable type that are not present in its name. 830 const AbiTagList &UsedAbiTags = 831 VariableNameMangler.AbiTagsRoot.getSortedUniqueUsedAbiTags(); 832 AbiTagList AdditionalAbiTags(VariableTypeAbiTags.size()); 833 AdditionalAbiTags.erase( 834 std::set_difference(VariableTypeAbiTags.begin(), 835 VariableTypeAbiTags.end(), UsedAbiTags.begin(), 836 UsedAbiTags.end(), AdditionalAbiTags.begin()), 837 AdditionalAbiTags.end()); 838 839 // Output name with implicit tags. 840 mangleNameWithAbiTags(VD, &AdditionalAbiTags); 841 } else { 842 mangleNameWithAbiTags(ND, nullptr); 843 } 844 } 845 846 void CXXNameMangler::mangleNameWithAbiTags(const NamedDecl *ND, 847 const AbiTagList *AdditionalAbiTags) { 848 // <name> ::= <nested-name> 849 // ::= <unscoped-name> 850 // ::= <unscoped-template-name> <template-args> 851 // ::= <local-name> 852 // 853 const DeclContext *DC = getEffectiveDeclContext(ND); 854 855 // If this is an extern variable declared locally, the relevant DeclContext 856 // is that of the containing namespace, or the translation unit. 857 // FIXME: This is a hack; extern variables declared locally should have 858 // a proper semantic declaration context! 859 if (isLocalContainerContext(DC) && ND->hasLinkage() && !isLambda(ND)) 860 while (!DC->isNamespace() && !DC->isTranslationUnit()) 861 DC = getEffectiveParentContext(DC); 862 else if (GetLocalClassDecl(ND)) { 863 mangleLocalName(ND, AdditionalAbiTags); 864 return; 865 } 866 867 DC = IgnoreLinkageSpecDecls(DC); 868 869 if (DC->isTranslationUnit() || isStdNamespace(DC)) { 870 // Check if we have a template. 871 const TemplateArgumentList *TemplateArgs = nullptr; 872 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { 873 mangleUnscopedTemplateName(TD, AdditionalAbiTags); 874 mangleTemplateArgs(*TemplateArgs); 875 return; 876 } 877 878 mangleUnscopedName(ND, AdditionalAbiTags); 879 return; 880 } 881 882 if (isLocalContainerContext(DC)) { 883 mangleLocalName(ND, AdditionalAbiTags); 884 return; 885 } 886 887 mangleNestedName(ND, DC, AdditionalAbiTags); 888 } 889 890 void CXXNameMangler::mangleTemplateName(const TemplateDecl *TD, 891 const TemplateArgument *TemplateArgs, 892 unsigned NumTemplateArgs) { 893 const DeclContext *DC = IgnoreLinkageSpecDecls(getEffectiveDeclContext(TD)); 894 895 if (DC->isTranslationUnit() || isStdNamespace(DC)) { 896 mangleUnscopedTemplateName(TD, nullptr); 897 mangleTemplateArgs(TemplateArgs, NumTemplateArgs); 898 } else { 899 mangleNestedName(TD, TemplateArgs, NumTemplateArgs); 900 } 901 } 902 903 void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND, 904 const AbiTagList *AdditionalAbiTags) { 905 // <unscoped-name> ::= <unqualified-name> 906 // ::= St <unqualified-name> # ::std:: 907 908 if (isStdNamespace(IgnoreLinkageSpecDecls(getEffectiveDeclContext(ND)))) 909 Out << "St"; 910 911 mangleUnqualifiedName(ND, AdditionalAbiTags); 912 } 913 914 void CXXNameMangler::mangleUnscopedTemplateName( 915 const TemplateDecl *ND, const AbiTagList *AdditionalAbiTags) { 916 // <unscoped-template-name> ::= <unscoped-name> 917 // ::= <substitution> 918 if (mangleSubstitution(ND)) 919 return; 920 921 // <template-template-param> ::= <template-param> 922 if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(ND)) { 923 assert(!AdditionalAbiTags && 924 "template template param cannot have abi tags"); 925 mangleTemplateParameter(TTP->getIndex()); 926 } else if (isa<BuiltinTemplateDecl>(ND)) { 927 mangleUnscopedName(ND, AdditionalAbiTags); 928 } else { 929 mangleUnscopedName(ND->getTemplatedDecl(), AdditionalAbiTags); 930 } 931 932 addSubstitution(ND); 933 } 934 935 void CXXNameMangler::mangleUnscopedTemplateName( 936 TemplateName Template, const AbiTagList *AdditionalAbiTags) { 937 // <unscoped-template-name> ::= <unscoped-name> 938 // ::= <substitution> 939 if (TemplateDecl *TD = Template.getAsTemplateDecl()) 940 return mangleUnscopedTemplateName(TD, AdditionalAbiTags); 941 942 if (mangleSubstitution(Template)) 943 return; 944 945 assert(!AdditionalAbiTags && 946 "dependent template name cannot have abi tags"); 947 948 DependentTemplateName *Dependent = Template.getAsDependentTemplateName(); 949 assert(Dependent && "Not a dependent template name?"); 950 if (const IdentifierInfo *Id = Dependent->getIdentifier()) 951 mangleSourceName(Id); 952 else 953 mangleOperatorName(Dependent->getOperator(), UnknownArity); 954 955 addSubstitution(Template); 956 } 957 958 void CXXNameMangler::mangleFloat(const llvm::APFloat &f) { 959 // ABI: 960 // Floating-point literals are encoded using a fixed-length 961 // lowercase hexadecimal string corresponding to the internal 962 // representation (IEEE on Itanium), high-order bytes first, 963 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f 964 // on Itanium. 965 // The 'without leading zeroes' thing seems to be an editorial 966 // mistake; see the discussion on cxx-abi-dev beginning on 967 // 2012-01-16. 968 969 // Our requirements here are just barely weird enough to justify 970 // using a custom algorithm instead of post-processing APInt::toString(). 971 972 llvm::APInt valueBits = f.bitcastToAPInt(); 973 unsigned numCharacters = (valueBits.getBitWidth() + 3) / 4; 974 assert(numCharacters != 0); 975 976 // Allocate a buffer of the right number of characters. 977 SmallVector<char, 20> buffer(numCharacters); 978 979 // Fill the buffer left-to-right. 980 for (unsigned stringIndex = 0; stringIndex != numCharacters; ++stringIndex) { 981 // The bit-index of the next hex digit. 982 unsigned digitBitIndex = 4 * (numCharacters - stringIndex - 1); 983 984 // Project out 4 bits starting at 'digitIndex'. 985 uint64_t hexDigit = valueBits.getRawData()[digitBitIndex / 64]; 986 hexDigit >>= (digitBitIndex % 64); 987 hexDigit &= 0xF; 988 989 // Map that over to a lowercase hex digit. 990 static const char charForHex[16] = { 991 '0', '1', '2', '3', '4', '5', '6', '7', 992 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 993 }; 994 buffer[stringIndex] = charForHex[hexDigit]; 995 } 996 997 Out.write(buffer.data(), numCharacters); 998 } 999 1000 void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) { 1001 if (Value.isSigned() && Value.isNegative()) { 1002 Out << 'n'; 1003 Value.abs().print(Out, /*signed*/ false); 1004 } else { 1005 Value.print(Out, /*signed*/ false); 1006 } 1007 } 1008 1009 void CXXNameMangler::mangleNumber(int64_t Number) { 1010 // <number> ::= [n] <non-negative decimal integer> 1011 if (Number < 0) { 1012 Out << 'n'; 1013 Number = -Number; 1014 } 1015 1016 Out << Number; 1017 } 1018 1019 void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) { 1020 // <call-offset> ::= h <nv-offset> _ 1021 // ::= v <v-offset> _ 1022 // <nv-offset> ::= <offset number> # non-virtual base override 1023 // <v-offset> ::= <offset number> _ <virtual offset number> 1024 // # virtual base override, with vcall offset 1025 if (!Virtual) { 1026 Out << 'h'; 1027 mangleNumber(NonVirtual); 1028 Out << '_'; 1029 return; 1030 } 1031 1032 Out << 'v'; 1033 mangleNumber(NonVirtual); 1034 Out << '_'; 1035 mangleNumber(Virtual); 1036 Out << '_'; 1037 } 1038 1039 void CXXNameMangler::manglePrefix(QualType type) { 1040 if (const auto *TST = type->getAs<TemplateSpecializationType>()) { 1041 if (!mangleSubstitution(QualType(TST, 0))) { 1042 mangleTemplatePrefix(TST->getTemplateName()); 1043 1044 // FIXME: GCC does not appear to mangle the template arguments when 1045 // the template in question is a dependent template name. Should we 1046 // emulate that badness? 1047 mangleTemplateArgs(TST->getArgs(), TST->getNumArgs()); 1048 addSubstitution(QualType(TST, 0)); 1049 } 1050 } else if (const auto *DTST = 1051 type->getAs<DependentTemplateSpecializationType>()) { 1052 if (!mangleSubstitution(QualType(DTST, 0))) { 1053 TemplateName Template = getASTContext().getDependentTemplateName( 1054 DTST->getQualifier(), DTST->getIdentifier()); 1055 mangleTemplatePrefix(Template); 1056 1057 // FIXME: GCC does not appear to mangle the template arguments when 1058 // the template in question is a dependent template name. Should we 1059 // emulate that badness? 1060 mangleTemplateArgs(DTST->getArgs(), DTST->getNumArgs()); 1061 addSubstitution(QualType(DTST, 0)); 1062 } 1063 } else { 1064 // We use the QualType mangle type variant here because it handles 1065 // substitutions. 1066 mangleType(type); 1067 } 1068 } 1069 1070 /// Mangle everything prior to the base-unresolved-name in an unresolved-name. 1071 /// 1072 /// \param recursive - true if this is being called recursively, 1073 /// i.e. if there is more prefix "to the right". 1074 void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier, 1075 bool recursive) { 1076 1077 // x, ::x 1078 // <unresolved-name> ::= [gs] <base-unresolved-name> 1079 1080 // T::x / decltype(p)::x 1081 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name> 1082 1083 // T::N::x /decltype(p)::N::x 1084 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E 1085 // <base-unresolved-name> 1086 1087 // A::x, N::y, A<T>::z; "gs" means leading "::" 1088 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E 1089 // <base-unresolved-name> 1090 1091 switch (qualifier->getKind()) { 1092 case NestedNameSpecifier::Global: 1093 Out << "gs"; 1094 1095 // We want an 'sr' unless this is the entire NNS. 1096 if (recursive) 1097 Out << "sr"; 1098 1099 // We never want an 'E' here. 1100 return; 1101 1102 case NestedNameSpecifier::Super: 1103 llvm_unreachable("Can't mangle __super specifier"); 1104 1105 case NestedNameSpecifier::Namespace: 1106 if (qualifier->getPrefix()) 1107 mangleUnresolvedPrefix(qualifier->getPrefix(), 1108 /*recursive*/ true); 1109 else 1110 Out << "sr"; 1111 mangleSourceNameWithAbiTags(qualifier->getAsNamespace()); 1112 break; 1113 case NestedNameSpecifier::NamespaceAlias: 1114 if (qualifier->getPrefix()) 1115 mangleUnresolvedPrefix(qualifier->getPrefix(), 1116 /*recursive*/ true); 1117 else 1118 Out << "sr"; 1119 mangleSourceNameWithAbiTags(qualifier->getAsNamespaceAlias()); 1120 break; 1121 1122 case NestedNameSpecifier::TypeSpec: 1123 case NestedNameSpecifier::TypeSpecWithTemplate: { 1124 const Type *type = qualifier->getAsType(); 1125 1126 // We only want to use an unresolved-type encoding if this is one of: 1127 // - a decltype 1128 // - a template type parameter 1129 // - a template template parameter with arguments 1130 // In all of these cases, we should have no prefix. 1131 if (qualifier->getPrefix()) { 1132 mangleUnresolvedPrefix(qualifier->getPrefix(), 1133 /*recursive*/ true); 1134 } else { 1135 // Otherwise, all the cases want this. 1136 Out << "sr"; 1137 } 1138 1139 if (mangleUnresolvedTypeOrSimpleId(QualType(type, 0), recursive ? "N" : "")) 1140 return; 1141 1142 break; 1143 } 1144 1145 case NestedNameSpecifier::Identifier: 1146 // Member expressions can have these without prefixes. 1147 if (qualifier->getPrefix()) 1148 mangleUnresolvedPrefix(qualifier->getPrefix(), 1149 /*recursive*/ true); 1150 else 1151 Out << "sr"; 1152 1153 mangleSourceName(qualifier->getAsIdentifier()); 1154 // An Identifier has no type information, so we can't emit abi tags for it. 1155 break; 1156 } 1157 1158 // If this was the innermost part of the NNS, and we fell out to 1159 // here, append an 'E'. 1160 if (!recursive) 1161 Out << 'E'; 1162 } 1163 1164 /// Mangle an unresolved-name, which is generally used for names which 1165 /// weren't resolved to specific entities. 1166 void CXXNameMangler::mangleUnresolvedName( 1167 NestedNameSpecifier *qualifier, DeclarationName name, 1168 const TemplateArgumentLoc *TemplateArgs, unsigned NumTemplateArgs, 1169 unsigned knownArity) { 1170 if (qualifier) mangleUnresolvedPrefix(qualifier); 1171 switch (name.getNameKind()) { 1172 // <base-unresolved-name> ::= <simple-id> 1173 case DeclarationName::Identifier: 1174 mangleSourceName(name.getAsIdentifierInfo()); 1175 break; 1176 // <base-unresolved-name> ::= dn <destructor-name> 1177 case DeclarationName::CXXDestructorName: 1178 Out << "dn"; 1179 mangleUnresolvedTypeOrSimpleId(name.getCXXNameType()); 1180 break; 1181 // <base-unresolved-name> ::= on <operator-name> 1182 case DeclarationName::CXXConversionFunctionName: 1183 case DeclarationName::CXXLiteralOperatorName: 1184 case DeclarationName::CXXOperatorName: 1185 Out << "on"; 1186 mangleOperatorName(name, knownArity); 1187 break; 1188 case DeclarationName::CXXConstructorName: 1189 llvm_unreachable("Can't mangle a constructor name!"); 1190 case DeclarationName::CXXUsingDirective: 1191 llvm_unreachable("Can't mangle a using directive name!"); 1192 case DeclarationName::CXXDeductionGuideName: 1193 llvm_unreachable("Can't mangle a deduction guide name!"); 1194 case DeclarationName::ObjCMultiArgSelector: 1195 case DeclarationName::ObjCOneArgSelector: 1196 case DeclarationName::ObjCZeroArgSelector: 1197 llvm_unreachable("Can't mangle Objective-C selector names here!"); 1198 } 1199 1200 // The <simple-id> and on <operator-name> productions end in an optional 1201 // <template-args>. 1202 if (TemplateArgs) 1203 mangleTemplateArgs(TemplateArgs, NumTemplateArgs); 1204 } 1205 1206 void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, 1207 DeclarationName Name, 1208 unsigned KnownArity, 1209 const AbiTagList *AdditionalAbiTags) { 1210 unsigned Arity = KnownArity; 1211 // <unqualified-name> ::= <operator-name> 1212 // ::= <ctor-dtor-name> 1213 // ::= <source-name> 1214 switch (Name.getNameKind()) { 1215 case DeclarationName::Identifier: { 1216 const IdentifierInfo *II = Name.getAsIdentifierInfo(); 1217 1218 // We mangle decomposition declarations as the names of their bindings. 1219 if (auto *DD = dyn_cast<DecompositionDecl>(ND)) { 1220 // FIXME: Non-standard mangling for decomposition declarations: 1221 // 1222 // <unqualified-name> ::= DC <source-name>* E 1223 // 1224 // These can never be referenced across translation units, so we do 1225 // not need a cross-vendor mangling for anything other than demanglers. 1226 // Proposed on cxx-abi-dev on 2016-08-12 1227 Out << "DC"; 1228 for (auto *BD : DD->bindings()) 1229 mangleSourceName(BD->getDeclName().getAsIdentifierInfo()); 1230 Out << 'E'; 1231 writeAbiTags(ND, AdditionalAbiTags); 1232 break; 1233 } 1234 1235 if (II) { 1236 // We must avoid conflicts between internally- and externally- 1237 // linked variable and function declaration names in the same TU: 1238 // void test() { extern void foo(); } 1239 // static void foo(); 1240 // This naming convention is the same as that followed by GCC, 1241 // though it shouldn't actually matter. 1242 if (ND && ND->getFormalLinkage() == InternalLinkage && 1243 getEffectiveDeclContext(ND)->isFileContext()) 1244 Out << 'L'; 1245 1246 auto *FD = dyn_cast<FunctionDecl>(ND); 1247 bool IsRegCall = FD && 1248 FD->getType()->castAs<FunctionType>()->getCallConv() == 1249 clang::CC_X86RegCall; 1250 if (IsRegCall) 1251 mangleRegCallName(II); 1252 else 1253 mangleSourceName(II); 1254 1255 writeAbiTags(ND, AdditionalAbiTags); 1256 break; 1257 } 1258 1259 // Otherwise, an anonymous entity. We must have a declaration. 1260 assert(ND && "mangling empty name without declaration"); 1261 1262 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { 1263 if (NS->isAnonymousNamespace()) { 1264 // This is how gcc mangles these names. 1265 Out << "12_GLOBAL__N_1"; 1266 break; 1267 } 1268 } 1269 1270 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) { 1271 // We must have an anonymous union or struct declaration. 1272 const RecordDecl *RD = 1273 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl()); 1274 1275 // Itanium C++ ABI 5.1.2: 1276 // 1277 // For the purposes of mangling, the name of an anonymous union is 1278 // considered to be the name of the first named data member found by a 1279 // pre-order, depth-first, declaration-order walk of the data members of 1280 // the anonymous union. If there is no such data member (i.e., if all of 1281 // the data members in the union are unnamed), then there is no way for 1282 // a program to refer to the anonymous union, and there is therefore no 1283 // need to mangle its name. 1284 assert(RD->isAnonymousStructOrUnion() 1285 && "Expected anonymous struct or union!"); 1286 const FieldDecl *FD = RD->findFirstNamedDataMember(); 1287 1288 // It's actually possible for various reasons for us to get here 1289 // with an empty anonymous struct / union. Fortunately, it 1290 // doesn't really matter what name we generate. 1291 if (!FD) break; 1292 assert(FD->getIdentifier() && "Data member name isn't an identifier!"); 1293 1294 mangleSourceName(FD->getIdentifier()); 1295 // Not emitting abi tags: internal name anyway. 1296 break; 1297 } 1298 1299 // Class extensions have no name as a category, and it's possible 1300 // for them to be the semantic parent of certain declarations 1301 // (primarily, tag decls defined within declarations). Such 1302 // declarations will always have internal linkage, so the name 1303 // doesn't really matter, but we shouldn't crash on them. For 1304 // safety, just handle all ObjC containers here. 1305 if (isa<ObjCContainerDecl>(ND)) 1306 break; 1307 1308 // We must have an anonymous struct. 1309 const TagDecl *TD = cast<TagDecl>(ND); 1310 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) { 1311 assert(TD->getDeclContext() == D->getDeclContext() && 1312 "Typedef should not be in another decl context!"); 1313 assert(D->getDeclName().getAsIdentifierInfo() && 1314 "Typedef was not named!"); 1315 mangleSourceName(D->getDeclName().getAsIdentifierInfo()); 1316 assert(!AdditionalAbiTags && "Type cannot have additional abi tags"); 1317 // Explicit abi tags are still possible; take from underlying type, not 1318 // from typedef. 1319 writeAbiTags(TD, nullptr); 1320 break; 1321 } 1322 1323 // <unnamed-type-name> ::= <closure-type-name> 1324 // 1325 // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ 1326 // <lambda-sig> ::= <parameter-type>+ # Parameter types or 'v' for 'void'. 1327 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) { 1328 if (Record->isLambda() && Record->getLambdaManglingNumber()) { 1329 assert(!AdditionalAbiTags && 1330 "Lambda type cannot have additional abi tags"); 1331 mangleLambda(Record); 1332 break; 1333 } 1334 } 1335 1336 if (TD->isExternallyVisible()) { 1337 unsigned UnnamedMangle = getASTContext().getManglingNumber(TD); 1338 Out << "Ut"; 1339 if (UnnamedMangle > 1) 1340 Out << UnnamedMangle - 2; 1341 Out << '_'; 1342 writeAbiTags(TD, AdditionalAbiTags); 1343 break; 1344 } 1345 1346 // Get a unique id for the anonymous struct. If it is not a real output 1347 // ID doesn't matter so use fake one. 1348 unsigned AnonStructId = NullOut ? 0 : Context.getAnonymousStructId(TD); 1349 1350 // Mangle it as a source name in the form 1351 // [n] $_<id> 1352 // where n is the length of the string. 1353 SmallString<8> Str; 1354 Str += "$_"; 1355 Str += llvm::utostr(AnonStructId); 1356 1357 Out << Str.size(); 1358 Out << Str; 1359 break; 1360 } 1361 1362 case DeclarationName::ObjCZeroArgSelector: 1363 case DeclarationName::ObjCOneArgSelector: 1364 case DeclarationName::ObjCMultiArgSelector: 1365 llvm_unreachable("Can't mangle Objective-C selector names here!"); 1366 1367 case DeclarationName::CXXConstructorName: { 1368 const CXXRecordDecl *InheritedFrom = nullptr; 1369 const TemplateArgumentList *InheritedTemplateArgs = nullptr; 1370 if (auto Inherited = 1371 cast<CXXConstructorDecl>(ND)->getInheritedConstructor()) { 1372 InheritedFrom = Inherited.getConstructor()->getParent(); 1373 InheritedTemplateArgs = 1374 Inherited.getConstructor()->getTemplateSpecializationArgs(); 1375 } 1376 1377 if (ND == Structor) 1378 // If the named decl is the C++ constructor we're mangling, use the type 1379 // we were given. 1380 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType), InheritedFrom); 1381 else 1382 // Otherwise, use the complete constructor name. This is relevant if a 1383 // class with a constructor is declared within a constructor. 1384 mangleCXXCtorType(Ctor_Complete, InheritedFrom); 1385 1386 // FIXME: The template arguments are part of the enclosing prefix or 1387 // nested-name, but it's more convenient to mangle them here. 1388 if (InheritedTemplateArgs) 1389 mangleTemplateArgs(*InheritedTemplateArgs); 1390 1391 writeAbiTags(ND, AdditionalAbiTags); 1392 break; 1393 } 1394 1395 case DeclarationName::CXXDestructorName: 1396 if (ND == Structor) 1397 // If the named decl is the C++ destructor we're mangling, use the type we 1398 // were given. 1399 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType)); 1400 else 1401 // Otherwise, use the complete destructor name. This is relevant if a 1402 // class with a destructor is declared within a destructor. 1403 mangleCXXDtorType(Dtor_Complete); 1404 writeAbiTags(ND, AdditionalAbiTags); 1405 break; 1406 1407 case DeclarationName::CXXOperatorName: 1408 if (ND && Arity == UnknownArity) { 1409 Arity = cast<FunctionDecl>(ND)->getNumParams(); 1410 1411 // If we have a member function, we need to include the 'this' pointer. 1412 if (const auto *MD = dyn_cast<CXXMethodDecl>(ND)) 1413 if (!MD->isStatic()) 1414 Arity++; 1415 } 1416 // FALLTHROUGH 1417 case DeclarationName::CXXConversionFunctionName: 1418 case DeclarationName::CXXLiteralOperatorName: 1419 mangleOperatorName(Name, Arity); 1420 writeAbiTags(ND, AdditionalAbiTags); 1421 break; 1422 1423 case DeclarationName::CXXDeductionGuideName: 1424 llvm_unreachable("Can't mangle a deduction guide name!"); 1425 1426 case DeclarationName::CXXUsingDirective: 1427 llvm_unreachable("Can't mangle a using directive name!"); 1428 } 1429 } 1430 1431 void CXXNameMangler::mangleRegCallName(const IdentifierInfo *II) { 1432 // <source-name> ::= <positive length number> __regcall3__ <identifier> 1433 // <number> ::= [n] <non-negative decimal integer> 1434 // <identifier> ::= <unqualified source code identifier> 1435 Out << II->getLength() + sizeof("__regcall3__") - 1 << "__regcall3__" 1436 << II->getName(); 1437 } 1438 1439 void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) { 1440 // <source-name> ::= <positive length number> <identifier> 1441 // <number> ::= [n] <non-negative decimal integer> 1442 // <identifier> ::= <unqualified source code identifier> 1443 Out << II->getLength() << II->getName(); 1444 } 1445 1446 void CXXNameMangler::mangleNestedName(const NamedDecl *ND, 1447 const DeclContext *DC, 1448 const AbiTagList *AdditionalAbiTags, 1449 bool NoFunction) { 1450 // <nested-name> 1451 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E 1452 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> 1453 // <template-args> E 1454 1455 Out << 'N'; 1456 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) { 1457 Qualifiers MethodQuals = 1458 Qualifiers::fromCVRUMask(Method->getTypeQualifiers()); 1459 // We do not consider restrict a distinguishing attribute for overloading 1460 // purposes so we must not mangle it. 1461 MethodQuals.removeRestrict(); 1462 // __unaligned is not currently mangled in any way, so remove it. 1463 MethodQuals.removeUnaligned(); 1464 mangleQualifiers(MethodQuals); 1465 mangleRefQualifier(Method->getRefQualifier()); 1466 } 1467 1468 // Check if we have a template. 1469 const TemplateArgumentList *TemplateArgs = nullptr; 1470 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { 1471 mangleTemplatePrefix(TD, NoFunction); 1472 mangleTemplateArgs(*TemplateArgs); 1473 } 1474 else { 1475 manglePrefix(DC, NoFunction); 1476 mangleUnqualifiedName(ND, AdditionalAbiTags); 1477 } 1478 1479 Out << 'E'; 1480 } 1481 void CXXNameMangler::mangleNestedName(const TemplateDecl *TD, 1482 const TemplateArgument *TemplateArgs, 1483 unsigned NumTemplateArgs) { 1484 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E 1485 1486 Out << 'N'; 1487 1488 mangleTemplatePrefix(TD); 1489 mangleTemplateArgs(TemplateArgs, NumTemplateArgs); 1490 1491 Out << 'E'; 1492 } 1493 1494 void CXXNameMangler::mangleLocalName(const Decl *D, 1495 const AbiTagList *AdditionalAbiTags) { 1496 // <local-name> := Z <function encoding> E <entity name> [<discriminator>] 1497 // := Z <function encoding> E s [<discriminator>] 1498 // <local-name> := Z <function encoding> E d [ <parameter number> ] 1499 // _ <entity name> 1500 // <discriminator> := _ <non-negative number> 1501 assert(isa<NamedDecl>(D) || isa<BlockDecl>(D)); 1502 const RecordDecl *RD = GetLocalClassDecl(D); 1503 const DeclContext *DC = getEffectiveDeclContext(RD ? RD : D); 1504 1505 Out << 'Z'; 1506 1507 { 1508 AbiTagState LocalAbiTags(AbiTags); 1509 1510 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) 1511 mangleObjCMethodName(MD); 1512 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) 1513 mangleBlockForPrefix(BD); 1514 else 1515 mangleFunctionEncoding(cast<FunctionDecl>(DC)); 1516 1517 // Implicit ABI tags (from namespace) are not available in the following 1518 // entity; reset to actually emitted tags, which are available. 1519 LocalAbiTags.setUsedAbiTags(LocalAbiTags.getEmittedAbiTags()); 1520 } 1521 1522 Out << 'E'; 1523 1524 // GCC 5.3.0 doesn't emit derived ABI tags for local names but that seems to 1525 // be a bug that is fixed in trunk. 1526 1527 if (RD) { 1528 // The parameter number is omitted for the last parameter, 0 for the 1529 // second-to-last parameter, 1 for the third-to-last parameter, etc. The 1530 // <entity name> will of course contain a <closure-type-name>: Its 1531 // numbering will be local to the particular argument in which it appears 1532 // -- other default arguments do not affect its encoding. 1533 const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD); 1534 if (CXXRD && CXXRD->isLambda()) { 1535 if (const ParmVarDecl *Parm 1536 = dyn_cast_or_null<ParmVarDecl>(CXXRD->getLambdaContextDecl())) { 1537 if (const FunctionDecl *Func 1538 = dyn_cast<FunctionDecl>(Parm->getDeclContext())) { 1539 Out << 'd'; 1540 unsigned Num = Func->getNumParams() - Parm->getFunctionScopeIndex(); 1541 if (Num > 1) 1542 mangleNumber(Num - 2); 1543 Out << '_'; 1544 } 1545 } 1546 } 1547 1548 // Mangle the name relative to the closest enclosing function. 1549 // equality ok because RD derived from ND above 1550 if (D == RD) { 1551 mangleUnqualifiedName(RD, AdditionalAbiTags); 1552 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) { 1553 manglePrefix(getEffectiveDeclContext(BD), true /*NoFunction*/); 1554 assert(!AdditionalAbiTags && "Block cannot have additional abi tags"); 1555 mangleUnqualifiedBlock(BD); 1556 } else { 1557 const NamedDecl *ND = cast<NamedDecl>(D); 1558 mangleNestedName(ND, getEffectiveDeclContext(ND), AdditionalAbiTags, 1559 true /*NoFunction*/); 1560 } 1561 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) { 1562 // Mangle a block in a default parameter; see above explanation for 1563 // lambdas. 1564 if (const ParmVarDecl *Parm 1565 = dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl())) { 1566 if (const FunctionDecl *Func 1567 = dyn_cast<FunctionDecl>(Parm->getDeclContext())) { 1568 Out << 'd'; 1569 unsigned Num = Func->getNumParams() - Parm->getFunctionScopeIndex(); 1570 if (Num > 1) 1571 mangleNumber(Num - 2); 1572 Out << '_'; 1573 } 1574 } 1575 1576 assert(!AdditionalAbiTags && "Block cannot have additional abi tags"); 1577 mangleUnqualifiedBlock(BD); 1578 } else { 1579 mangleUnqualifiedName(cast<NamedDecl>(D), AdditionalAbiTags); 1580 } 1581 1582 if (const NamedDecl *ND = dyn_cast<NamedDecl>(RD ? RD : D)) { 1583 unsigned disc; 1584 if (Context.getNextDiscriminator(ND, disc)) { 1585 if (disc < 10) 1586 Out << '_' << disc; 1587 else 1588 Out << "__" << disc << '_'; 1589 } 1590 } 1591 } 1592 1593 void CXXNameMangler::mangleBlockForPrefix(const BlockDecl *Block) { 1594 if (GetLocalClassDecl(Block)) { 1595 mangleLocalName(Block, /* AdditionalAbiTags */ nullptr); 1596 return; 1597 } 1598 const DeclContext *DC = getEffectiveDeclContext(Block); 1599 if (isLocalContainerContext(DC)) { 1600 mangleLocalName(Block, /* AdditionalAbiTags */ nullptr); 1601 return; 1602 } 1603 manglePrefix(getEffectiveDeclContext(Block)); 1604 mangleUnqualifiedBlock(Block); 1605 } 1606 1607 void CXXNameMangler::mangleUnqualifiedBlock(const BlockDecl *Block) { 1608 if (Decl *Context = Block->getBlockManglingContextDecl()) { 1609 if ((isa<VarDecl>(Context) || isa<FieldDecl>(Context)) && 1610 Context->getDeclContext()->isRecord()) { 1611 const auto *ND = cast<NamedDecl>(Context); 1612 if (ND->getIdentifier()) { 1613 mangleSourceNameWithAbiTags(ND); 1614 Out << 'M'; 1615 } 1616 } 1617 } 1618 1619 // If we have a block mangling number, use it. 1620 unsigned Number = Block->getBlockManglingNumber(); 1621 // Otherwise, just make up a number. It doesn't matter what it is because 1622 // the symbol in question isn't externally visible. 1623 if (!Number) 1624 Number = Context.getBlockId(Block, false); 1625 Out << "Ub"; 1626 if (Number > 0) 1627 Out << Number - 1; 1628 Out << '_'; 1629 } 1630 1631 void CXXNameMangler::mangleLambda(const CXXRecordDecl *Lambda) { 1632 // If the context of a closure type is an initializer for a class member 1633 // (static or nonstatic), it is encoded in a qualified name with a final 1634 // <prefix> of the form: 1635 // 1636 // <data-member-prefix> := <member source-name> M 1637 // 1638 // Technically, the data-member-prefix is part of the <prefix>. However, 1639 // since a closure type will always be mangled with a prefix, it's easier 1640 // to emit that last part of the prefix here. 1641 if (Decl *Context = Lambda->getLambdaContextDecl()) { 1642 if ((isa<VarDecl>(Context) || isa<FieldDecl>(Context)) && 1643 Context->getDeclContext()->isRecord()) { 1644 if (const IdentifierInfo *Name 1645 = cast<NamedDecl>(Context)->getIdentifier()) { 1646 mangleSourceName(Name); 1647 Out << 'M'; 1648 } 1649 } 1650 } 1651 1652 Out << "Ul"; 1653 const FunctionProtoType *Proto = Lambda->getLambdaTypeInfo()->getType()-> 1654 getAs<FunctionProtoType>(); 1655 mangleBareFunctionType(Proto, /*MangleReturnType=*/false, 1656 Lambda->getLambdaStaticInvoker()); 1657 Out << "E"; 1658 1659 // The number is omitted for the first closure type with a given 1660 // <lambda-sig> in a given context; it is n-2 for the nth closure type 1661 // (in lexical order) with that same <lambda-sig> and context. 1662 // 1663 // The AST keeps track of the number for us. 1664 unsigned Number = Lambda->getLambdaManglingNumber(); 1665 assert(Number > 0 && "Lambda should be mangled as an unnamed class"); 1666 if (Number > 1) 1667 mangleNumber(Number - 2); 1668 Out << '_'; 1669 } 1670 1671 void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) { 1672 switch (qualifier->getKind()) { 1673 case NestedNameSpecifier::Global: 1674 // nothing 1675 return; 1676 1677 case NestedNameSpecifier::Super: 1678 llvm_unreachable("Can't mangle __super specifier"); 1679 1680 case NestedNameSpecifier::Namespace: 1681 mangleName(qualifier->getAsNamespace()); 1682 return; 1683 1684 case NestedNameSpecifier::NamespaceAlias: 1685 mangleName(qualifier->getAsNamespaceAlias()->getNamespace()); 1686 return; 1687 1688 case NestedNameSpecifier::TypeSpec: 1689 case NestedNameSpecifier::TypeSpecWithTemplate: 1690 manglePrefix(QualType(qualifier->getAsType(), 0)); 1691 return; 1692 1693 case NestedNameSpecifier::Identifier: 1694 // Member expressions can have these without prefixes, but that 1695 // should end up in mangleUnresolvedPrefix instead. 1696 assert(qualifier->getPrefix()); 1697 manglePrefix(qualifier->getPrefix()); 1698 1699 mangleSourceName(qualifier->getAsIdentifier()); 1700 return; 1701 } 1702 1703 llvm_unreachable("unexpected nested name specifier"); 1704 } 1705 1706 void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) { 1707 // <prefix> ::= <prefix> <unqualified-name> 1708 // ::= <template-prefix> <template-args> 1709 // ::= <template-param> 1710 // ::= # empty 1711 // ::= <substitution> 1712 1713 DC = IgnoreLinkageSpecDecls(DC); 1714 1715 if (DC->isTranslationUnit()) 1716 return; 1717 1718 if (NoFunction && isLocalContainerContext(DC)) 1719 return; 1720 1721 assert(!isLocalContainerContext(DC)); 1722 1723 const NamedDecl *ND = cast<NamedDecl>(DC); 1724 if (mangleSubstitution(ND)) 1725 return; 1726 1727 // Check if we have a template. 1728 const TemplateArgumentList *TemplateArgs = nullptr; 1729 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { 1730 mangleTemplatePrefix(TD); 1731 mangleTemplateArgs(*TemplateArgs); 1732 } else { 1733 manglePrefix(getEffectiveDeclContext(ND), NoFunction); 1734 mangleUnqualifiedName(ND, nullptr); 1735 } 1736 1737 addSubstitution(ND); 1738 } 1739 1740 void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) { 1741 // <template-prefix> ::= <prefix> <template unqualified-name> 1742 // ::= <template-param> 1743 // ::= <substitution> 1744 if (TemplateDecl *TD = Template.getAsTemplateDecl()) 1745 return mangleTemplatePrefix(TD); 1746 1747 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName()) 1748 manglePrefix(Qualified->getQualifier()); 1749 1750 if (OverloadedTemplateStorage *Overloaded 1751 = Template.getAsOverloadedTemplate()) { 1752 mangleUnqualifiedName(nullptr, (*Overloaded->begin())->getDeclName(), 1753 UnknownArity, nullptr); 1754 return; 1755 } 1756 1757 DependentTemplateName *Dependent = Template.getAsDependentTemplateName(); 1758 assert(Dependent && "Unknown template name kind?"); 1759 if (NestedNameSpecifier *Qualifier = Dependent->getQualifier()) 1760 manglePrefix(Qualifier); 1761 mangleUnscopedTemplateName(Template, /* AdditionalAbiTags */ nullptr); 1762 } 1763 1764 void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND, 1765 bool NoFunction) { 1766 // <template-prefix> ::= <prefix> <template unqualified-name> 1767 // ::= <template-param> 1768 // ::= <substitution> 1769 // <template-template-param> ::= <template-param> 1770 // <substitution> 1771 1772 if (mangleSubstitution(ND)) 1773 return; 1774 1775 // <template-template-param> ::= <template-param> 1776 if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(ND)) { 1777 mangleTemplateParameter(TTP->getIndex()); 1778 } else { 1779 manglePrefix(getEffectiveDeclContext(ND), NoFunction); 1780 if (isa<BuiltinTemplateDecl>(ND)) 1781 mangleUnqualifiedName(ND, nullptr); 1782 else 1783 mangleUnqualifiedName(ND->getTemplatedDecl(), nullptr); 1784 } 1785 1786 addSubstitution(ND); 1787 } 1788 1789 /// Mangles a template name under the production <type>. Required for 1790 /// template template arguments. 1791 /// <type> ::= <class-enum-type> 1792 /// ::= <template-param> 1793 /// ::= <substitution> 1794 void CXXNameMangler::mangleType(TemplateName TN) { 1795 if (mangleSubstitution(TN)) 1796 return; 1797 1798 TemplateDecl *TD = nullptr; 1799 1800 switch (TN.getKind()) { 1801 case TemplateName::QualifiedTemplate: 1802 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl(); 1803 goto HaveDecl; 1804 1805 case TemplateName::Template: 1806 TD = TN.getAsTemplateDecl(); 1807 goto HaveDecl; 1808 1809 HaveDecl: 1810 if (isa<TemplateTemplateParmDecl>(TD)) 1811 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex()); 1812 else 1813 mangleName(TD); 1814 break; 1815 1816 case TemplateName::OverloadedTemplate: 1817 llvm_unreachable("can't mangle an overloaded template name as a <type>"); 1818 1819 case TemplateName::DependentTemplate: { 1820 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName(); 1821 assert(Dependent->isIdentifier()); 1822 1823 // <class-enum-type> ::= <name> 1824 // <name> ::= <nested-name> 1825 mangleUnresolvedPrefix(Dependent->getQualifier()); 1826 mangleSourceName(Dependent->getIdentifier()); 1827 break; 1828 } 1829 1830 case TemplateName::SubstTemplateTemplateParm: { 1831 // Substituted template parameters are mangled as the substituted 1832 // template. This will check for the substitution twice, which is 1833 // fine, but we have to return early so that we don't try to *add* 1834 // the substitution twice. 1835 SubstTemplateTemplateParmStorage *subst 1836 = TN.getAsSubstTemplateTemplateParm(); 1837 mangleType(subst->getReplacement()); 1838 return; 1839 } 1840 1841 case TemplateName::SubstTemplateTemplateParmPack: { 1842 // FIXME: not clear how to mangle this! 1843 // template <template <class> class T...> class A { 1844 // template <template <class> class U...> void foo(B<T,U> x...); 1845 // }; 1846 Out << "_SUBSTPACK_"; 1847 break; 1848 } 1849 } 1850 1851 addSubstitution(TN); 1852 } 1853 1854 bool CXXNameMangler::mangleUnresolvedTypeOrSimpleId(QualType Ty, 1855 StringRef Prefix) { 1856 // Only certain other types are valid as prefixes; enumerate them. 1857 switch (Ty->getTypeClass()) { 1858 case Type::Builtin: 1859 case Type::Complex: 1860 case Type::Adjusted: 1861 case Type::Decayed: 1862 case Type::Pointer: 1863 case Type::BlockPointer: 1864 case Type::LValueReference: 1865 case Type::RValueReference: 1866 case Type::MemberPointer: 1867 case Type::ConstantArray: 1868 case Type::IncompleteArray: 1869 case Type::VariableArray: 1870 case Type::DependentSizedArray: 1871 case Type::DependentSizedExtVector: 1872 case Type::Vector: 1873 case Type::ExtVector: 1874 case Type::FunctionProto: 1875 case Type::FunctionNoProto: 1876 case Type::Paren: 1877 case Type::Attributed: 1878 case Type::Auto: 1879 case Type::DeducedTemplateSpecialization: 1880 case Type::PackExpansion: 1881 case Type::ObjCObject: 1882 case Type::ObjCInterface: 1883 case Type::ObjCObjectPointer: 1884 case Type::ObjCTypeParam: 1885 case Type::Atomic: 1886 case Type::Pipe: 1887 llvm_unreachable("type is illegal as a nested name specifier"); 1888 1889 case Type::SubstTemplateTypeParmPack: 1890 // FIXME: not clear how to mangle this! 1891 // template <class T...> class A { 1892 // template <class U...> void foo(decltype(T::foo(U())) x...); 1893 // }; 1894 Out << "_SUBSTPACK_"; 1895 break; 1896 1897 // <unresolved-type> ::= <template-param> 1898 // ::= <decltype> 1899 // ::= <template-template-param> <template-args> 1900 // (this last is not official yet) 1901 case Type::TypeOfExpr: 1902 case Type::TypeOf: 1903 case Type::Decltype: 1904 case Type::TemplateTypeParm: 1905 case Type::UnaryTransform: 1906 case Type::SubstTemplateTypeParm: 1907 unresolvedType: 1908 // Some callers want a prefix before the mangled type. 1909 Out << Prefix; 1910 1911 // This seems to do everything we want. It's not really 1912 // sanctioned for a substituted template parameter, though. 1913 mangleType(Ty); 1914 1915 // We never want to print 'E' directly after an unresolved-type, 1916 // so we return directly. 1917 return true; 1918 1919 case Type::Typedef: 1920 mangleSourceNameWithAbiTags(cast<TypedefType>(Ty)->getDecl()); 1921 break; 1922 1923 case Type::UnresolvedUsing: 1924 mangleSourceNameWithAbiTags( 1925 cast<UnresolvedUsingType>(Ty)->getDecl()); 1926 break; 1927 1928 case Type::Enum: 1929 case Type::Record: 1930 mangleSourceNameWithAbiTags(cast<TagType>(Ty)->getDecl()); 1931 break; 1932 1933 case Type::TemplateSpecialization: { 1934 const TemplateSpecializationType *TST = 1935 cast<TemplateSpecializationType>(Ty); 1936 TemplateName TN = TST->getTemplateName(); 1937 switch (TN.getKind()) { 1938 case TemplateName::Template: 1939 case TemplateName::QualifiedTemplate: { 1940 TemplateDecl *TD = TN.getAsTemplateDecl(); 1941 1942 // If the base is a template template parameter, this is an 1943 // unresolved type. 1944 assert(TD && "no template for template specialization type"); 1945 if (isa<TemplateTemplateParmDecl>(TD)) 1946 goto unresolvedType; 1947 1948 mangleSourceNameWithAbiTags(TD); 1949 break; 1950 } 1951 1952 case TemplateName::OverloadedTemplate: 1953 case TemplateName::DependentTemplate: 1954 llvm_unreachable("invalid base for a template specialization type"); 1955 1956 case TemplateName::SubstTemplateTemplateParm: { 1957 SubstTemplateTemplateParmStorage *subst = 1958 TN.getAsSubstTemplateTemplateParm(); 1959 mangleExistingSubstitution(subst->getReplacement()); 1960 break; 1961 } 1962 1963 case TemplateName::SubstTemplateTemplateParmPack: { 1964 // FIXME: not clear how to mangle this! 1965 // template <template <class U> class T...> class A { 1966 // template <class U...> void foo(decltype(T<U>::foo) x...); 1967 // }; 1968 Out << "_SUBSTPACK_"; 1969 break; 1970 } 1971 } 1972 1973 mangleTemplateArgs(TST->getArgs(), TST->getNumArgs()); 1974 break; 1975 } 1976 1977 case Type::InjectedClassName: 1978 mangleSourceNameWithAbiTags( 1979 cast<InjectedClassNameType>(Ty)->getDecl()); 1980 break; 1981 1982 case Type::DependentName: 1983 mangleSourceName(cast<DependentNameType>(Ty)->getIdentifier()); 1984 break; 1985 1986 case Type::DependentTemplateSpecialization: { 1987 const DependentTemplateSpecializationType *DTST = 1988 cast<DependentTemplateSpecializationType>(Ty); 1989 mangleSourceName(DTST->getIdentifier()); 1990 mangleTemplateArgs(DTST->getArgs(), DTST->getNumArgs()); 1991 break; 1992 } 1993 1994 case Type::Elaborated: 1995 return mangleUnresolvedTypeOrSimpleId( 1996 cast<ElaboratedType>(Ty)->getNamedType(), Prefix); 1997 } 1998 1999 return false; 2000 } 2001 2002 void CXXNameMangler::mangleOperatorName(DeclarationName Name, unsigned Arity) { 2003 switch (Name.getNameKind()) { 2004 case DeclarationName::CXXConstructorName: 2005 case DeclarationName::CXXDestructorName: 2006 case DeclarationName::CXXDeductionGuideName: 2007 case DeclarationName::CXXUsingDirective: 2008 case DeclarationName::Identifier: 2009 case DeclarationName::ObjCMultiArgSelector: 2010 case DeclarationName::ObjCOneArgSelector: 2011 case DeclarationName::ObjCZeroArgSelector: 2012 llvm_unreachable("Not an operator name"); 2013 2014 case DeclarationName::CXXConversionFunctionName: 2015 // <operator-name> ::= cv <type> # (cast) 2016 Out << "cv"; 2017 mangleType(Name.getCXXNameType()); 2018 break; 2019 2020 case DeclarationName::CXXLiteralOperatorName: 2021 Out << "li"; 2022 mangleSourceName(Name.getCXXLiteralIdentifier()); 2023 return; 2024 2025 case DeclarationName::CXXOperatorName: 2026 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity); 2027 break; 2028 } 2029 } 2030 2031 void 2032 CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) { 2033 switch (OO) { 2034 // <operator-name> ::= nw # new 2035 case OO_New: Out << "nw"; break; 2036 // ::= na # new[] 2037 case OO_Array_New: Out << "na"; break; 2038 // ::= dl # delete 2039 case OO_Delete: Out << "dl"; break; 2040 // ::= da # delete[] 2041 case OO_Array_Delete: Out << "da"; break; 2042 // ::= ps # + (unary) 2043 // ::= pl # + (binary or unknown) 2044 case OO_Plus: 2045 Out << (Arity == 1? "ps" : "pl"); break; 2046 // ::= ng # - (unary) 2047 // ::= mi # - (binary or unknown) 2048 case OO_Minus: 2049 Out << (Arity == 1? "ng" : "mi"); break; 2050 // ::= ad # & (unary) 2051 // ::= an # & (binary or unknown) 2052 case OO_Amp: 2053 Out << (Arity == 1? "ad" : "an"); break; 2054 // ::= de # * (unary) 2055 // ::= ml # * (binary or unknown) 2056 case OO_Star: 2057 // Use binary when unknown. 2058 Out << (Arity == 1? "de" : "ml"); break; 2059 // ::= co # ~ 2060 case OO_Tilde: Out << "co"; break; 2061 // ::= dv # / 2062 case OO_Slash: Out << "dv"; break; 2063 // ::= rm # % 2064 case OO_Percent: Out << "rm"; break; 2065 // ::= or # | 2066 case OO_Pipe: Out << "or"; break; 2067 // ::= eo # ^ 2068 case OO_Caret: Out << "eo"; break; 2069 // ::= aS # = 2070 case OO_Equal: Out << "aS"; break; 2071 // ::= pL # += 2072 case OO_PlusEqual: Out << "pL"; break; 2073 // ::= mI # -= 2074 case OO_MinusEqual: Out << "mI"; break; 2075 // ::= mL # *= 2076 case OO_StarEqual: Out << "mL"; break; 2077 // ::= dV # /= 2078 case OO_SlashEqual: Out << "dV"; break; 2079 // ::= rM # %= 2080 case OO_PercentEqual: Out << "rM"; break; 2081 // ::= aN # &= 2082 case OO_AmpEqual: Out << "aN"; break; 2083 // ::= oR # |= 2084 case OO_PipeEqual: Out << "oR"; break; 2085 // ::= eO # ^= 2086 case OO_CaretEqual: Out << "eO"; break; 2087 // ::= ls # << 2088 case OO_LessLess: Out << "ls"; break; 2089 // ::= rs # >> 2090 case OO_GreaterGreater: Out << "rs"; break; 2091 // ::= lS # <<= 2092 case OO_LessLessEqual: Out << "lS"; break; 2093 // ::= rS # >>= 2094 case OO_GreaterGreaterEqual: Out << "rS"; break; 2095 // ::= eq # == 2096 case OO_EqualEqual: Out << "eq"; break; 2097 // ::= ne # != 2098 case OO_ExclaimEqual: Out << "ne"; break; 2099 // ::= lt # < 2100 case OO_Less: Out << "lt"; break; 2101 // ::= gt # > 2102 case OO_Greater: Out << "gt"; break; 2103 // ::= le # <= 2104 case OO_LessEqual: Out << "le"; break; 2105 // ::= ge # >= 2106 case OO_GreaterEqual: Out << "ge"; break; 2107 // ::= nt # ! 2108 case OO_Exclaim: Out << "nt"; break; 2109 // ::= aa # && 2110 case OO_AmpAmp: Out << "aa"; break; 2111 // ::= oo # || 2112 case OO_PipePipe: Out << "oo"; break; 2113 // ::= pp # ++ 2114 case OO_PlusPlus: Out << "pp"; break; 2115 // ::= mm # -- 2116 case OO_MinusMinus: Out << "mm"; break; 2117 // ::= cm # , 2118 case OO_Comma: Out << "cm"; break; 2119 // ::= pm # ->* 2120 case OO_ArrowStar: Out << "pm"; break; 2121 // ::= pt # -> 2122 case OO_Arrow: Out << "pt"; break; 2123 // ::= cl # () 2124 case OO_Call: Out << "cl"; break; 2125 // ::= ix # [] 2126 case OO_Subscript: Out << "ix"; break; 2127 2128 // ::= qu # ? 2129 // The conditional operator can't be overloaded, but we still handle it when 2130 // mangling expressions. 2131 case OO_Conditional: Out << "qu"; break; 2132 // Proposal on cxx-abi-dev, 2015-10-21. 2133 // ::= aw # co_await 2134 case OO_Coawait: Out << "aw"; break; 2135 2136 case OO_None: 2137 case NUM_OVERLOADED_OPERATORS: 2138 llvm_unreachable("Not an overloaded operator"); 2139 } 2140 } 2141 2142 void CXXNameMangler::mangleQualifiers(Qualifiers Quals) { 2143 // Vendor qualifiers come first. 2144 2145 // Address space qualifiers start with an ordinary letter. 2146 if (Quals.hasAddressSpace()) { 2147 // Address space extension: 2148 // 2149 // <type> ::= U <target-addrspace> 2150 // <type> ::= U <OpenCL-addrspace> 2151 // <type> ::= U <CUDA-addrspace> 2152 2153 SmallString<64> ASString; 2154 unsigned AS = Quals.getAddressSpace(); 2155 2156 if (Context.getASTContext().addressSpaceMapManglingFor(AS)) { 2157 // <target-addrspace> ::= "AS" <address-space-number> 2158 unsigned TargetAS = Context.getASTContext().getTargetAddressSpace(AS); 2159 ASString = "AS" + llvm::utostr(TargetAS); 2160 } else { 2161 switch (AS) { 2162 default: llvm_unreachable("Not a language specific address space"); 2163 // <OpenCL-addrspace> ::= "CL" [ "global" | "local" | "constant | 2164 // "generic" ] 2165 case LangAS::opencl_global: ASString = "CLglobal"; break; 2166 case LangAS::opencl_local: ASString = "CLlocal"; break; 2167 case LangAS::opencl_constant: ASString = "CLconstant"; break; 2168 case LangAS::opencl_generic: ASString = "CLgeneric"; break; 2169 // <CUDA-addrspace> ::= "CU" [ "device" | "constant" | "shared" ] 2170 case LangAS::cuda_device: ASString = "CUdevice"; break; 2171 case LangAS::cuda_constant: ASString = "CUconstant"; break; 2172 case LangAS::cuda_shared: ASString = "CUshared"; break; 2173 } 2174 } 2175 mangleVendorQualifier(ASString); 2176 } 2177 2178 // The ARC ownership qualifiers start with underscores. 2179 switch (Quals.getObjCLifetime()) { 2180 // Objective-C ARC Extension: 2181 // 2182 // <type> ::= U "__strong" 2183 // <type> ::= U "__weak" 2184 // <type> ::= U "__autoreleasing" 2185 case Qualifiers::OCL_None: 2186 break; 2187 2188 case Qualifiers::OCL_Weak: 2189 mangleVendorQualifier("__weak"); 2190 break; 2191 2192 case Qualifiers::OCL_Strong: 2193 mangleVendorQualifier("__strong"); 2194 break; 2195 2196 case Qualifiers::OCL_Autoreleasing: 2197 mangleVendorQualifier("__autoreleasing"); 2198 break; 2199 2200 case Qualifiers::OCL_ExplicitNone: 2201 // The __unsafe_unretained qualifier is *not* mangled, so that 2202 // __unsafe_unretained types in ARC produce the same manglings as the 2203 // equivalent (but, naturally, unqualified) types in non-ARC, providing 2204 // better ABI compatibility. 2205 // 2206 // It's safe to do this because unqualified 'id' won't show up 2207 // in any type signatures that need to be mangled. 2208 break; 2209 } 2210 2211 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const 2212 if (Quals.hasRestrict()) 2213 Out << 'r'; 2214 if (Quals.hasVolatile()) 2215 Out << 'V'; 2216 if (Quals.hasConst()) 2217 Out << 'K'; 2218 } 2219 2220 void CXXNameMangler::mangleVendorQualifier(StringRef name) { 2221 Out << 'U' << name.size() << name; 2222 } 2223 2224 void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) { 2225 // <ref-qualifier> ::= R # lvalue reference 2226 // ::= O # rvalue-reference 2227 switch (RefQualifier) { 2228 case RQ_None: 2229 break; 2230 2231 case RQ_LValue: 2232 Out << 'R'; 2233 break; 2234 2235 case RQ_RValue: 2236 Out << 'O'; 2237 break; 2238 } 2239 } 2240 2241 void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { 2242 Context.mangleObjCMethodName(MD, Out); 2243 } 2244 2245 static bool isTypeSubstitutable(Qualifiers Quals, const Type *Ty) { 2246 if (Quals) 2247 return true; 2248 if (Ty->isSpecificBuiltinType(BuiltinType::ObjCSel)) 2249 return true; 2250 if (Ty->isOpenCLSpecificType()) 2251 return true; 2252 if (Ty->isBuiltinType()) 2253 return false; 2254 2255 return true; 2256 } 2257 2258 void CXXNameMangler::mangleType(QualType T) { 2259 // If our type is instantiation-dependent but not dependent, we mangle 2260 // it as it was written in the source, removing any top-level sugar. 2261 // Otherwise, use the canonical type. 2262 // 2263 // FIXME: This is an approximation of the instantiation-dependent name 2264 // mangling rules, since we should really be using the type as written and 2265 // augmented via semantic analysis (i.e., with implicit conversions and 2266 // default template arguments) for any instantiation-dependent type. 2267 // Unfortunately, that requires several changes to our AST: 2268 // - Instantiation-dependent TemplateSpecializationTypes will need to be 2269 // uniqued, so that we can handle substitutions properly 2270 // - Default template arguments will need to be represented in the 2271 // TemplateSpecializationType, since they need to be mangled even though 2272 // they aren't written. 2273 // - Conversions on non-type template arguments need to be expressed, since 2274 // they can affect the mangling of sizeof/alignof. 2275 // 2276 // FIXME: This is wrong when mapping to the canonical type for a dependent 2277 // type discards instantiation-dependent portions of the type, such as for: 2278 // 2279 // template<typename T, int N> void f(T (&)[sizeof(N)]); 2280 // template<typename T> void f(T() throw(typename T::type)); (pre-C++17) 2281 // 2282 // It's also wrong in the opposite direction when instantiation-dependent, 2283 // canonically-equivalent types differ in some irrelevant portion of inner 2284 // type sugar. In such cases, we fail to form correct substitutions, eg: 2285 // 2286 // template<int N> void f(A<sizeof(N)> *, A<sizeof(N)> (*)); 2287 // 2288 // We should instead canonicalize the non-instantiation-dependent parts, 2289 // regardless of whether the type as a whole is dependent or instantiation 2290 // dependent. 2291 if (!T->isInstantiationDependentType() || T->isDependentType()) 2292 T = T.getCanonicalType(); 2293 else { 2294 // Desugar any types that are purely sugar. 2295 do { 2296 // Don't desugar through template specialization types that aren't 2297 // type aliases. We need to mangle the template arguments as written. 2298 if (const TemplateSpecializationType *TST 2299 = dyn_cast<TemplateSpecializationType>(T)) 2300 if (!TST->isTypeAlias()) 2301 break; 2302 2303 QualType Desugared 2304 = T.getSingleStepDesugaredType(Context.getASTContext()); 2305 if (Desugared == T) 2306 break; 2307 2308 T = Desugared; 2309 } while (true); 2310 } 2311 SplitQualType split = T.split(); 2312 Qualifiers quals = split.Quals; 2313 const Type *ty = split.Ty; 2314 2315 bool isSubstitutable = isTypeSubstitutable(quals, ty); 2316 if (isSubstitutable && mangleSubstitution(T)) 2317 return; 2318 2319 // If we're mangling a qualified array type, push the qualifiers to 2320 // the element type. 2321 if (quals && isa<ArrayType>(T)) { 2322 ty = Context.getASTContext().getAsArrayType(T); 2323 quals = Qualifiers(); 2324 2325 // Note that we don't update T: we want to add the 2326 // substitution at the original type. 2327 } 2328 2329 if (quals) { 2330 mangleQualifiers(quals); 2331 // Recurse: even if the qualified type isn't yet substitutable, 2332 // the unqualified type might be. 2333 mangleType(QualType(ty, 0)); 2334 } else { 2335 switch (ty->getTypeClass()) { 2336 #define ABSTRACT_TYPE(CLASS, PARENT) 2337 #define NON_CANONICAL_TYPE(CLASS, PARENT) \ 2338 case Type::CLASS: \ 2339 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ 2340 return; 2341 #define TYPE(CLASS, PARENT) \ 2342 case Type::CLASS: \ 2343 mangleType(static_cast<const CLASS##Type*>(ty)); \ 2344 break; 2345 #include "clang/AST/TypeNodes.def" 2346 } 2347 } 2348 2349 // Add the substitution. 2350 if (isSubstitutable) 2351 addSubstitution(T); 2352 } 2353 2354 void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) { 2355 if (!mangleStandardSubstitution(ND)) 2356 mangleName(ND); 2357 } 2358 2359 void CXXNameMangler::mangleType(const BuiltinType *T) { 2360 // <type> ::= <builtin-type> 2361 // <builtin-type> ::= v # void 2362 // ::= w # wchar_t 2363 // ::= b # bool 2364 // ::= c # char 2365 // ::= a # signed char 2366 // ::= h # unsigned char 2367 // ::= s # short 2368 // ::= t # unsigned short 2369 // ::= i # int 2370 // ::= j # unsigned int 2371 // ::= l # long 2372 // ::= m # unsigned long 2373 // ::= x # long long, __int64 2374 // ::= y # unsigned long long, __int64 2375 // ::= n # __int128 2376 // ::= o # unsigned __int128 2377 // ::= f # float 2378 // ::= d # double 2379 // ::= e # long double, __float80 2380 // ::= g # __float128 2381 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits) 2382 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits) 2383 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits) 2384 // ::= Dh # IEEE 754r half-precision floating point (16 bits) 2385 // ::= Di # char32_t 2386 // ::= Ds # char16_t 2387 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr)) 2388 // ::= u <source-name> # vendor extended type 2389 std::string type_name; 2390 switch (T->getKind()) { 2391 case BuiltinType::Void: 2392 Out << 'v'; 2393 break; 2394 case BuiltinType::Bool: 2395 Out << 'b'; 2396 break; 2397 case BuiltinType::Char_U: 2398 case BuiltinType::Char_S: 2399 Out << 'c'; 2400 break; 2401 case BuiltinType::UChar: 2402 Out << 'h'; 2403 break; 2404 case BuiltinType::UShort: 2405 Out << 't'; 2406 break; 2407 case BuiltinType::UInt: 2408 Out << 'j'; 2409 break; 2410 case BuiltinType::ULong: 2411 Out << 'm'; 2412 break; 2413 case BuiltinType::ULongLong: 2414 Out << 'y'; 2415 break; 2416 case BuiltinType::UInt128: 2417 Out << 'o'; 2418 break; 2419 case BuiltinType::SChar: 2420 Out << 'a'; 2421 break; 2422 case BuiltinType::WChar_S: 2423 case BuiltinType::WChar_U: 2424 Out << 'w'; 2425 break; 2426 case BuiltinType::Char16: 2427 Out << "Ds"; 2428 break; 2429 case BuiltinType::Char32: 2430 Out << "Di"; 2431 break; 2432 case BuiltinType::Short: 2433 Out << 's'; 2434 break; 2435 case BuiltinType::Int: 2436 Out << 'i'; 2437 break; 2438 case BuiltinType::Long: 2439 Out << 'l'; 2440 break; 2441 case BuiltinType::LongLong: 2442 Out << 'x'; 2443 break; 2444 case BuiltinType::Int128: 2445 Out << 'n'; 2446 break; 2447 case BuiltinType::Half: 2448 Out << "Dh"; 2449 break; 2450 case BuiltinType::Float: 2451 Out << 'f'; 2452 break; 2453 case BuiltinType::Double: 2454 Out << 'd'; 2455 break; 2456 case BuiltinType::LongDouble: 2457 Out << (getASTContext().getTargetInfo().useFloat128ManglingForLongDouble() 2458 ? 'g' 2459 : 'e'); 2460 break; 2461 case BuiltinType::Float128: 2462 if (getASTContext().getTargetInfo().useFloat128ManglingForLongDouble()) 2463 Out << "U10__float128"; // Match the GCC mangling 2464 else 2465 Out << 'g'; 2466 break; 2467 case BuiltinType::NullPtr: 2468 Out << "Dn"; 2469 break; 2470 2471 #define BUILTIN_TYPE(Id, SingletonId) 2472 #define PLACEHOLDER_TYPE(Id, SingletonId) \ 2473 case BuiltinType::Id: 2474 #include "clang/AST/BuiltinTypes.def" 2475 case BuiltinType::Dependent: 2476 if (!NullOut) 2477 llvm_unreachable("mangling a placeholder type"); 2478 break; 2479 case BuiltinType::ObjCId: 2480 Out << "11objc_object"; 2481 break; 2482 case BuiltinType::ObjCClass: 2483 Out << "10objc_class"; 2484 break; 2485 case BuiltinType::ObjCSel: 2486 Out << "13objc_selector"; 2487 break; 2488 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 2489 case BuiltinType::Id: \ 2490 type_name = "ocl_" #ImgType "_" #Suffix; \ 2491 Out << type_name.size() << type_name; \ 2492 break; 2493 #include "clang/Basic/OpenCLImageTypes.def" 2494 case BuiltinType::OCLSampler: 2495 Out << "11ocl_sampler"; 2496 break; 2497 case BuiltinType::OCLEvent: 2498 Out << "9ocl_event"; 2499 break; 2500 case BuiltinType::OCLClkEvent: 2501 Out << "12ocl_clkevent"; 2502 break; 2503 case BuiltinType::OCLQueue: 2504 Out << "9ocl_queue"; 2505 break; 2506 case BuiltinType::OCLReserveID: 2507 Out << "13ocl_reserveid"; 2508 break; 2509 } 2510 } 2511 2512 StringRef CXXNameMangler::getCallingConvQualifierName(CallingConv CC) { 2513 switch (CC) { 2514 case CC_C: 2515 return ""; 2516 2517 case CC_X86StdCall: 2518 case CC_X86FastCall: 2519 case CC_X86ThisCall: 2520 case CC_X86VectorCall: 2521 case CC_X86Pascal: 2522 case CC_X86_64Win64: 2523 case CC_X86_64SysV: 2524 case CC_X86RegCall: 2525 case CC_AAPCS: 2526 case CC_AAPCS_VFP: 2527 case CC_IntelOclBicc: 2528 case CC_SpirFunction: 2529 case CC_OpenCLKernel: 2530 case CC_PreserveMost: 2531 case CC_PreserveAll: 2532 // FIXME: we should be mangling all of the above. 2533 return ""; 2534 2535 case CC_Swift: 2536 return "swiftcall"; 2537 } 2538 llvm_unreachable("bad calling convention"); 2539 } 2540 2541 void CXXNameMangler::mangleExtFunctionInfo(const FunctionType *T) { 2542 // Fast path. 2543 if (T->getExtInfo() == FunctionType::ExtInfo()) 2544 return; 2545 2546 // Vendor-specific qualifiers are emitted in reverse alphabetical order. 2547 // This will get more complicated in the future if we mangle other 2548 // things here; but for now, since we mangle ns_returns_retained as 2549 // a qualifier on the result type, we can get away with this: 2550 StringRef CCQualifier = getCallingConvQualifierName(T->getExtInfo().getCC()); 2551 if (!CCQualifier.empty()) 2552 mangleVendorQualifier(CCQualifier); 2553 2554 // FIXME: regparm 2555 // FIXME: noreturn 2556 } 2557 2558 void 2559 CXXNameMangler::mangleExtParameterInfo(FunctionProtoType::ExtParameterInfo PI) { 2560 // Vendor-specific qualifiers are emitted in reverse alphabetical order. 2561 2562 // Note that these are *not* substitution candidates. Demanglers might 2563 // have trouble with this if the parameter type is fully substituted. 2564 2565 switch (PI.getABI()) { 2566 case ParameterABI::Ordinary: 2567 break; 2568 2569 // All of these start with "swift", so they come before "ns_consumed". 2570 case ParameterABI::SwiftContext: 2571 case ParameterABI::SwiftErrorResult: 2572 case ParameterABI::SwiftIndirectResult: 2573 mangleVendorQualifier(getParameterABISpelling(PI.getABI())); 2574 break; 2575 } 2576 2577 if (PI.isConsumed()) 2578 mangleVendorQualifier("ns_consumed"); 2579 } 2580 2581 // <type> ::= <function-type> 2582 // <function-type> ::= [<CV-qualifiers>] F [Y] 2583 // <bare-function-type> [<ref-qualifier>] E 2584 void CXXNameMangler::mangleType(const FunctionProtoType *T) { 2585 mangleExtFunctionInfo(T); 2586 2587 // Mangle CV-qualifiers, if present. These are 'this' qualifiers, 2588 // e.g. "const" in "int (A::*)() const". 2589 mangleQualifiers(Qualifiers::fromCVRMask(T->getTypeQuals())); 2590 2591 // Mangle instantiation-dependent exception-specification, if present, 2592 // per cxx-abi-dev proposal on 2016-10-11. 2593 if (T->hasInstantiationDependentExceptionSpec()) { 2594 if (T->getExceptionSpecType() == EST_ComputedNoexcept) { 2595 Out << "DO"; 2596 mangleExpression(T->getNoexceptExpr()); 2597 Out << "E"; 2598 } else { 2599 assert(T->getExceptionSpecType() == EST_Dynamic); 2600 Out << "Dw"; 2601 for (auto ExceptTy : T->exceptions()) 2602 mangleType(ExceptTy); 2603 Out << "E"; 2604 } 2605 } else if (T->isNothrow(getASTContext())) { 2606 Out << "Do"; 2607 } 2608 2609 Out << 'F'; 2610 2611 // FIXME: We don't have enough information in the AST to produce the 'Y' 2612 // encoding for extern "C" function types. 2613 mangleBareFunctionType(T, /*MangleReturnType=*/true); 2614 2615 // Mangle the ref-qualifier, if present. 2616 mangleRefQualifier(T->getRefQualifier()); 2617 2618 Out << 'E'; 2619 } 2620 2621 void CXXNameMangler::mangleType(const FunctionNoProtoType *T) { 2622 // Function types without prototypes can arise when mangling a function type 2623 // within an overloadable function in C. We mangle these as the absence of any 2624 // parameter types (not even an empty parameter list). 2625 Out << 'F'; 2626 2627 FunctionTypeDepthState saved = FunctionTypeDepth.push(); 2628 2629 FunctionTypeDepth.enterResultType(); 2630 mangleType(T->getReturnType()); 2631 FunctionTypeDepth.leaveResultType(); 2632 2633 FunctionTypeDepth.pop(saved); 2634 Out << 'E'; 2635 } 2636 2637 void CXXNameMangler::mangleBareFunctionType(const FunctionProtoType *Proto, 2638 bool MangleReturnType, 2639 const FunctionDecl *FD) { 2640 // Record that we're in a function type. See mangleFunctionParam 2641 // for details on what we're trying to achieve here. 2642 FunctionTypeDepthState saved = FunctionTypeDepth.push(); 2643 2644 // <bare-function-type> ::= <signature type>+ 2645 if (MangleReturnType) { 2646 FunctionTypeDepth.enterResultType(); 2647 2648 // Mangle ns_returns_retained as an order-sensitive qualifier here. 2649 if (Proto->getExtInfo().getProducesResult() && FD == nullptr) 2650 mangleVendorQualifier("ns_returns_retained"); 2651 2652 // Mangle the return type without any direct ARC ownership qualifiers. 2653 QualType ReturnTy = Proto->getReturnType(); 2654 if (ReturnTy.getObjCLifetime()) { 2655 auto SplitReturnTy = ReturnTy.split(); 2656 SplitReturnTy.Quals.removeObjCLifetime(); 2657 ReturnTy = getASTContext().getQualifiedType(SplitReturnTy); 2658 } 2659 mangleType(ReturnTy); 2660 2661 FunctionTypeDepth.leaveResultType(); 2662 } 2663 2664 if (Proto->getNumParams() == 0 && !Proto->isVariadic()) { 2665 // <builtin-type> ::= v # void 2666 Out << 'v'; 2667 2668 FunctionTypeDepth.pop(saved); 2669 return; 2670 } 2671 2672 assert(!FD || FD->getNumParams() == Proto->getNumParams()); 2673 for (unsigned I = 0, E = Proto->getNumParams(); I != E; ++I) { 2674 // Mangle extended parameter info as order-sensitive qualifiers here. 2675 if (Proto->hasExtParameterInfos() && FD == nullptr) { 2676 mangleExtParameterInfo(Proto->getExtParameterInfo(I)); 2677 } 2678 2679 // Mangle the type. 2680 QualType ParamTy = Proto->getParamType(I); 2681 mangleType(Context.getASTContext().getSignatureParameterType(ParamTy)); 2682 2683 if (FD) { 2684 if (auto *Attr = FD->getParamDecl(I)->getAttr<PassObjectSizeAttr>()) { 2685 // Attr can only take 1 character, so we can hardcode the length below. 2686 assert(Attr->getType() <= 9 && Attr->getType() >= 0); 2687 Out << "U17pass_object_size" << Attr->getType(); 2688 } 2689 } 2690 } 2691 2692 FunctionTypeDepth.pop(saved); 2693 2694 // <builtin-type> ::= z # ellipsis 2695 if (Proto->isVariadic()) 2696 Out << 'z'; 2697 } 2698 2699 // <type> ::= <class-enum-type> 2700 // <class-enum-type> ::= <name> 2701 void CXXNameMangler::mangleType(const UnresolvedUsingType *T) { 2702 mangleName(T->getDecl()); 2703 } 2704 2705 // <type> ::= <class-enum-type> 2706 // <class-enum-type> ::= <name> 2707 void CXXNameMangler::mangleType(const EnumType *T) { 2708 mangleType(static_cast<const TagType*>(T)); 2709 } 2710 void CXXNameMangler::mangleType(const RecordType *T) { 2711 mangleType(static_cast<const TagType*>(T)); 2712 } 2713 void CXXNameMangler::mangleType(const TagType *T) { 2714 mangleName(T->getDecl()); 2715 } 2716 2717 // <type> ::= <array-type> 2718 // <array-type> ::= A <positive dimension number> _ <element type> 2719 // ::= A [<dimension expression>] _ <element type> 2720 void CXXNameMangler::mangleType(const ConstantArrayType *T) { 2721 Out << 'A' << T->getSize() << '_'; 2722 mangleType(T->getElementType()); 2723 } 2724 void CXXNameMangler::mangleType(const VariableArrayType *T) { 2725 Out << 'A'; 2726 // decayed vla types (size 0) will just be skipped. 2727 if (T->getSizeExpr()) 2728 mangleExpression(T->getSizeExpr()); 2729 Out << '_'; 2730 mangleType(T->getElementType()); 2731 } 2732 void CXXNameMangler::mangleType(const DependentSizedArrayType *T) { 2733 Out << 'A'; 2734 mangleExpression(T->getSizeExpr()); 2735 Out << '_'; 2736 mangleType(T->getElementType()); 2737 } 2738 void CXXNameMangler::mangleType(const IncompleteArrayType *T) { 2739 Out << "A_"; 2740 mangleType(T->getElementType()); 2741 } 2742 2743 // <type> ::= <pointer-to-member-type> 2744 // <pointer-to-member-type> ::= M <class type> <member type> 2745 void CXXNameMangler::mangleType(const MemberPointerType *T) { 2746 Out << 'M'; 2747 mangleType(QualType(T->getClass(), 0)); 2748 QualType PointeeType = T->getPointeeType(); 2749 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) { 2750 mangleType(FPT); 2751 2752 // Itanium C++ ABI 5.1.8: 2753 // 2754 // The type of a non-static member function is considered to be different, 2755 // for the purposes of substitution, from the type of a namespace-scope or 2756 // static member function whose type appears similar. The types of two 2757 // non-static member functions are considered to be different, for the 2758 // purposes of substitution, if the functions are members of different 2759 // classes. In other words, for the purposes of substitution, the class of 2760 // which the function is a member is considered part of the type of 2761 // function. 2762 2763 // Given that we already substitute member function pointers as a 2764 // whole, the net effect of this rule is just to unconditionally 2765 // suppress substitution on the function type in a member pointer. 2766 // We increment the SeqID here to emulate adding an entry to the 2767 // substitution table. 2768 ++SeqID; 2769 } else 2770 mangleType(PointeeType); 2771 } 2772 2773 // <type> ::= <template-param> 2774 void CXXNameMangler::mangleType(const TemplateTypeParmType *T) { 2775 mangleTemplateParameter(T->getIndex()); 2776 } 2777 2778 // <type> ::= <template-param> 2779 void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) { 2780 // FIXME: not clear how to mangle this! 2781 // template <class T...> class A { 2782 // template <class U...> void foo(T(*)(U) x...); 2783 // }; 2784 Out << "_SUBSTPACK_"; 2785 } 2786 2787 // <type> ::= P <type> # pointer-to 2788 void CXXNameMangler::mangleType(const PointerType *T) { 2789 Out << 'P'; 2790 mangleType(T->getPointeeType()); 2791 } 2792 void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) { 2793 Out << 'P'; 2794 mangleType(T->getPointeeType()); 2795 } 2796 2797 // <type> ::= R <type> # reference-to 2798 void CXXNameMangler::mangleType(const LValueReferenceType *T) { 2799 Out << 'R'; 2800 mangleType(T->getPointeeType()); 2801 } 2802 2803 // <type> ::= O <type> # rvalue reference-to (C++0x) 2804 void CXXNameMangler::mangleType(const RValueReferenceType *T) { 2805 Out << 'O'; 2806 mangleType(T->getPointeeType()); 2807 } 2808 2809 // <type> ::= C <type> # complex pair (C 2000) 2810 void CXXNameMangler::mangleType(const ComplexType *T) { 2811 Out << 'C'; 2812 mangleType(T->getElementType()); 2813 } 2814 2815 // ARM's ABI for Neon vector types specifies that they should be mangled as 2816 // if they are structs (to match ARM's initial implementation). The 2817 // vector type must be one of the special types predefined by ARM. 2818 void CXXNameMangler::mangleNeonVectorType(const VectorType *T) { 2819 QualType EltType = T->getElementType(); 2820 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType"); 2821 const char *EltName = nullptr; 2822 if (T->getVectorKind() == VectorType::NeonPolyVector) { 2823 switch (cast<BuiltinType>(EltType)->getKind()) { 2824 case BuiltinType::SChar: 2825 case BuiltinType::UChar: 2826 EltName = "poly8_t"; 2827 break; 2828 case BuiltinType::Short: 2829 case BuiltinType::UShort: 2830 EltName = "poly16_t"; 2831 break; 2832 case BuiltinType::ULongLong: 2833 EltName = "poly64_t"; 2834 break; 2835 default: llvm_unreachable("unexpected Neon polynomial vector element type"); 2836 } 2837 } else { 2838 switch (cast<BuiltinType>(EltType)->getKind()) { 2839 case BuiltinType::SChar: EltName = "int8_t"; break; 2840 case BuiltinType::UChar: EltName = "uint8_t"; break; 2841 case BuiltinType::Short: EltName = "int16_t"; break; 2842 case BuiltinType::UShort: EltName = "uint16_t"; break; 2843 case BuiltinType::Int: EltName = "int32_t"; break; 2844 case BuiltinType::UInt: EltName = "uint32_t"; break; 2845 case BuiltinType::LongLong: EltName = "int64_t"; break; 2846 case BuiltinType::ULongLong: EltName = "uint64_t"; break; 2847 case BuiltinType::Double: EltName = "float64_t"; break; 2848 case BuiltinType::Float: EltName = "float32_t"; break; 2849 case BuiltinType::Half: EltName = "float16_t";break; 2850 default: 2851 llvm_unreachable("unexpected Neon vector element type"); 2852 } 2853 } 2854 const char *BaseName = nullptr; 2855 unsigned BitSize = (T->getNumElements() * 2856 getASTContext().getTypeSize(EltType)); 2857 if (BitSize == 64) 2858 BaseName = "__simd64_"; 2859 else { 2860 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits"); 2861 BaseName = "__simd128_"; 2862 } 2863 Out << strlen(BaseName) + strlen(EltName); 2864 Out << BaseName << EltName; 2865 } 2866 2867 static StringRef mangleAArch64VectorBase(const BuiltinType *EltType) { 2868 switch (EltType->getKind()) { 2869 case BuiltinType::SChar: 2870 return "Int8"; 2871 case BuiltinType::Short: 2872 return "Int16"; 2873 case BuiltinType::Int: 2874 return "Int32"; 2875 case BuiltinType::Long: 2876 case BuiltinType::LongLong: 2877 return "Int64"; 2878 case BuiltinType::UChar: 2879 return "Uint8"; 2880 case BuiltinType::UShort: 2881 return "Uint16"; 2882 case BuiltinType::UInt: 2883 return "Uint32"; 2884 case BuiltinType::ULong: 2885 case BuiltinType::ULongLong: 2886 return "Uint64"; 2887 case BuiltinType::Half: 2888 return "Float16"; 2889 case BuiltinType::Float: 2890 return "Float32"; 2891 case BuiltinType::Double: 2892 return "Float64"; 2893 default: 2894 llvm_unreachable("Unexpected vector element base type"); 2895 } 2896 } 2897 2898 // AArch64's ABI for Neon vector types specifies that they should be mangled as 2899 // the equivalent internal name. The vector type must be one of the special 2900 // types predefined by ARM. 2901 void CXXNameMangler::mangleAArch64NeonVectorType(const VectorType *T) { 2902 QualType EltType = T->getElementType(); 2903 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType"); 2904 unsigned BitSize = 2905 (T->getNumElements() * getASTContext().getTypeSize(EltType)); 2906 (void)BitSize; // Silence warning. 2907 2908 assert((BitSize == 64 || BitSize == 128) && 2909 "Neon vector type not 64 or 128 bits"); 2910 2911 StringRef EltName; 2912 if (T->getVectorKind() == VectorType::NeonPolyVector) { 2913 switch (cast<BuiltinType>(EltType)->getKind()) { 2914 case BuiltinType::UChar: 2915 EltName = "Poly8"; 2916 break; 2917 case BuiltinType::UShort: 2918 EltName = "Poly16"; 2919 break; 2920 case BuiltinType::ULong: 2921 case BuiltinType::ULongLong: 2922 EltName = "Poly64"; 2923 break; 2924 default: 2925 llvm_unreachable("unexpected Neon polynomial vector element type"); 2926 } 2927 } else 2928 EltName = mangleAArch64VectorBase(cast<BuiltinType>(EltType)); 2929 2930 std::string TypeName = 2931 ("__" + EltName + "x" + Twine(T->getNumElements()) + "_t").str(); 2932 Out << TypeName.length() << TypeName; 2933 } 2934 2935 // GNU extension: vector types 2936 // <type> ::= <vector-type> 2937 // <vector-type> ::= Dv <positive dimension number> _ 2938 // <extended element type> 2939 // ::= Dv [<dimension expression>] _ <element type> 2940 // <extended element type> ::= <element type> 2941 // ::= p # AltiVec vector pixel 2942 // ::= b # Altivec vector bool 2943 void CXXNameMangler::mangleType(const VectorType *T) { 2944 if ((T->getVectorKind() == VectorType::NeonVector || 2945 T->getVectorKind() == VectorType::NeonPolyVector)) { 2946 llvm::Triple Target = getASTContext().getTargetInfo().getTriple(); 2947 llvm::Triple::ArchType Arch = 2948 getASTContext().getTargetInfo().getTriple().getArch(); 2949 if ((Arch == llvm::Triple::aarch64 || 2950 Arch == llvm::Triple::aarch64_be) && !Target.isOSDarwin()) 2951 mangleAArch64NeonVectorType(T); 2952 else 2953 mangleNeonVectorType(T); 2954 return; 2955 } 2956 Out << "Dv" << T->getNumElements() << '_'; 2957 if (T->getVectorKind() == VectorType::AltiVecPixel) 2958 Out << 'p'; 2959 else if (T->getVectorKind() == VectorType::AltiVecBool) 2960 Out << 'b'; 2961 else 2962 mangleType(T->getElementType()); 2963 } 2964 void CXXNameMangler::mangleType(const ExtVectorType *T) { 2965 mangleType(static_cast<const VectorType*>(T)); 2966 } 2967 void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) { 2968 Out << "Dv"; 2969 mangleExpression(T->getSizeExpr()); 2970 Out << '_'; 2971 mangleType(T->getElementType()); 2972 } 2973 2974 void CXXNameMangler::mangleType(const PackExpansionType *T) { 2975 // <type> ::= Dp <type> # pack expansion (C++0x) 2976 Out << "Dp"; 2977 mangleType(T->getPattern()); 2978 } 2979 2980 void CXXNameMangler::mangleType(const ObjCInterfaceType *T) { 2981 mangleSourceName(T->getDecl()->getIdentifier()); 2982 } 2983 2984 void CXXNameMangler::mangleType(const ObjCObjectType *T) { 2985 // Treat __kindof as a vendor extended type qualifier. 2986 if (T->isKindOfType()) 2987 Out << "U8__kindof"; 2988 2989 if (!T->qual_empty()) { 2990 // Mangle protocol qualifiers. 2991 SmallString<64> QualStr; 2992 llvm::raw_svector_ostream QualOS(QualStr); 2993 QualOS << "objcproto"; 2994 for (const auto *I : T->quals()) { 2995 StringRef name = I->getName(); 2996 QualOS << name.size() << name; 2997 } 2998 Out << 'U' << QualStr.size() << QualStr; 2999 } 3000 3001 mangleType(T->getBaseType()); 3002 3003 if (T->isSpecialized()) { 3004 // Mangle type arguments as I <type>+ E 3005 Out << 'I'; 3006 for (auto typeArg : T->getTypeArgs()) 3007 mangleType(typeArg); 3008 Out << 'E'; 3009 } 3010 } 3011 3012 void CXXNameMangler::mangleType(const BlockPointerType *T) { 3013 Out << "U13block_pointer"; 3014 mangleType(T->getPointeeType()); 3015 } 3016 3017 void CXXNameMangler::mangleType(const InjectedClassNameType *T) { 3018 // Mangle injected class name types as if the user had written the 3019 // specialization out fully. It may not actually be possible to see 3020 // this mangling, though. 3021 mangleType(T->getInjectedSpecializationType()); 3022 } 3023 3024 void CXXNameMangler::mangleType(const TemplateSpecializationType *T) { 3025 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) { 3026 mangleTemplateName(TD, T->getArgs(), T->getNumArgs()); 3027 } else { 3028 if (mangleSubstitution(QualType(T, 0))) 3029 return; 3030 3031 mangleTemplatePrefix(T->getTemplateName()); 3032 3033 // FIXME: GCC does not appear to mangle the template arguments when 3034 // the template in question is a dependent template name. Should we 3035 // emulate that badness? 3036 mangleTemplateArgs(T->getArgs(), T->getNumArgs()); 3037 addSubstitution(QualType(T, 0)); 3038 } 3039 } 3040 3041 void CXXNameMangler::mangleType(const DependentNameType *T) { 3042 // Proposal by cxx-abi-dev, 2014-03-26 3043 // <class-enum-type> ::= <name> # non-dependent or dependent type name or 3044 // # dependent elaborated type specifier using 3045 // # 'typename' 3046 // ::= Ts <name> # dependent elaborated type specifier using 3047 // # 'struct' or 'class' 3048 // ::= Tu <name> # dependent elaborated type specifier using 3049 // # 'union' 3050 // ::= Te <name> # dependent elaborated type specifier using 3051 // # 'enum' 3052 switch (T->getKeyword()) { 3053 case ETK_None: 3054 case ETK_Typename: 3055 break; 3056 case ETK_Struct: 3057 case ETK_Class: 3058 case ETK_Interface: 3059 Out << "Ts"; 3060 break; 3061 case ETK_Union: 3062 Out << "Tu"; 3063 break; 3064 case ETK_Enum: 3065 Out << "Te"; 3066 break; 3067 } 3068 // Typename types are always nested 3069 Out << 'N'; 3070 manglePrefix(T->getQualifier()); 3071 mangleSourceName(T->getIdentifier()); 3072 Out << 'E'; 3073 } 3074 3075 void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) { 3076 // Dependently-scoped template types are nested if they have a prefix. 3077 Out << 'N'; 3078 3079 // TODO: avoid making this TemplateName. 3080 TemplateName Prefix = 3081 getASTContext().getDependentTemplateName(T->getQualifier(), 3082 T->getIdentifier()); 3083 mangleTemplatePrefix(Prefix); 3084 3085 // FIXME: GCC does not appear to mangle the template arguments when 3086 // the template in question is a dependent template name. Should we 3087 // emulate that badness? 3088 mangleTemplateArgs(T->getArgs(), T->getNumArgs()); 3089 Out << 'E'; 3090 } 3091 3092 void CXXNameMangler::mangleType(const TypeOfType *T) { 3093 // FIXME: this is pretty unsatisfactory, but there isn't an obvious 3094 // "extension with parameters" mangling. 3095 Out << "u6typeof"; 3096 } 3097 3098 void CXXNameMangler::mangleType(const TypeOfExprType *T) { 3099 // FIXME: this is pretty unsatisfactory, but there isn't an obvious 3100 // "extension with parameters" mangling. 3101 Out << "u6typeof"; 3102 } 3103 3104 void CXXNameMangler::mangleType(const DecltypeType *T) { 3105 Expr *E = T->getUnderlyingExpr(); 3106 3107 // type ::= Dt <expression> E # decltype of an id-expression 3108 // # or class member access 3109 // ::= DT <expression> E # decltype of an expression 3110 3111 // This purports to be an exhaustive list of id-expressions and 3112 // class member accesses. Note that we do not ignore parentheses; 3113 // parentheses change the semantics of decltype for these 3114 // expressions (and cause the mangler to use the other form). 3115 if (isa<DeclRefExpr>(E) || 3116 isa<MemberExpr>(E) || 3117 isa<UnresolvedLookupExpr>(E) || 3118 isa<DependentScopeDeclRefExpr>(E) || 3119 isa<CXXDependentScopeMemberExpr>(E) || 3120 isa<UnresolvedMemberExpr>(E)) 3121 Out << "Dt"; 3122 else 3123 Out << "DT"; 3124 mangleExpression(E); 3125 Out << 'E'; 3126 } 3127 3128 void CXXNameMangler::mangleType(const UnaryTransformType *T) { 3129 // If this is dependent, we need to record that. If not, we simply 3130 // mangle it as the underlying type since they are equivalent. 3131 if (T->isDependentType()) { 3132 Out << 'U'; 3133 3134 switch (T->getUTTKind()) { 3135 case UnaryTransformType::EnumUnderlyingType: 3136 Out << "3eut"; 3137 break; 3138 } 3139 } 3140 3141 mangleType(T->getBaseType()); 3142 } 3143 3144 void CXXNameMangler::mangleType(const AutoType *T) { 3145 QualType D = T->getDeducedType(); 3146 // <builtin-type> ::= Da # dependent auto 3147 if (D.isNull()) { 3148 assert(T->getKeyword() != AutoTypeKeyword::GNUAutoType && 3149 "shouldn't need to mangle __auto_type!"); 3150 Out << (T->isDecltypeAuto() ? "Dc" : "Da"); 3151 } else 3152 mangleType(D); 3153 } 3154 3155 void CXXNameMangler::mangleType(const DeducedTemplateSpecializationType *T) { 3156 // FIXME: This is not the right mangling. We also need to include a scope 3157 // here in some cases. 3158 QualType D = T->getDeducedType(); 3159 if (D.isNull()) 3160 mangleUnscopedTemplateName(T->getTemplateName(), nullptr); 3161 else 3162 mangleType(D); 3163 } 3164 3165 void CXXNameMangler::mangleType(const AtomicType *T) { 3166 // <type> ::= U <source-name> <type> # vendor extended type qualifier 3167 // (Until there's a standardized mangling...) 3168 Out << "U7_Atomic"; 3169 mangleType(T->getValueType()); 3170 } 3171 3172 void CXXNameMangler::mangleType(const PipeType *T) { 3173 // Pipe type mangling rules are described in SPIR 2.0 specification 3174 // A.1 Data types and A.3 Summary of changes 3175 // <type> ::= 8ocl_pipe 3176 Out << "8ocl_pipe"; 3177 } 3178 3179 void CXXNameMangler::mangleIntegerLiteral(QualType T, 3180 const llvm::APSInt &Value) { 3181 // <expr-primary> ::= L <type> <value number> E # integer literal 3182 Out << 'L'; 3183 3184 mangleType(T); 3185 if (T->isBooleanType()) { 3186 // Boolean values are encoded as 0/1. 3187 Out << (Value.getBoolValue() ? '1' : '0'); 3188 } else { 3189 mangleNumber(Value); 3190 } 3191 Out << 'E'; 3192 3193 } 3194 3195 void CXXNameMangler::mangleMemberExprBase(const Expr *Base, bool IsArrow) { 3196 // Ignore member expressions involving anonymous unions. 3197 while (const auto *RT = Base->getType()->getAs<RecordType>()) { 3198 if (!RT->getDecl()->isAnonymousStructOrUnion()) 3199 break; 3200 const auto *ME = dyn_cast<MemberExpr>(Base); 3201 if (!ME) 3202 break; 3203 Base = ME->getBase(); 3204 IsArrow = ME->isArrow(); 3205 } 3206 3207 if (Base->isImplicitCXXThis()) { 3208 // Note: GCC mangles member expressions to the implicit 'this' as 3209 // *this., whereas we represent them as this->. The Itanium C++ ABI 3210 // does not specify anything here, so we follow GCC. 3211 Out << "dtdefpT"; 3212 } else { 3213 Out << (IsArrow ? "pt" : "dt"); 3214 mangleExpression(Base); 3215 } 3216 } 3217 3218 /// Mangles a member expression. 3219 void CXXNameMangler::mangleMemberExpr(const Expr *base, 3220 bool isArrow, 3221 NestedNameSpecifier *qualifier, 3222 NamedDecl *firstQualifierLookup, 3223 DeclarationName member, 3224 const TemplateArgumentLoc *TemplateArgs, 3225 unsigned NumTemplateArgs, 3226 unsigned arity) { 3227 // <expression> ::= dt <expression> <unresolved-name> 3228 // ::= pt <expression> <unresolved-name> 3229 if (base) 3230 mangleMemberExprBase(base, isArrow); 3231 mangleUnresolvedName(qualifier, member, TemplateArgs, NumTemplateArgs, arity); 3232 } 3233 3234 /// Look at the callee of the given call expression and determine if 3235 /// it's a parenthesized id-expression which would have triggered ADL 3236 /// otherwise. 3237 static bool isParenthesizedADLCallee(const CallExpr *call) { 3238 const Expr *callee = call->getCallee(); 3239 const Expr *fn = callee->IgnoreParens(); 3240 3241 // Must be parenthesized. IgnoreParens() skips __extension__ nodes, 3242 // too, but for those to appear in the callee, it would have to be 3243 // parenthesized. 3244 if (callee == fn) return false; 3245 3246 // Must be an unresolved lookup. 3247 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn); 3248 if (!lookup) return false; 3249 3250 assert(!lookup->requiresADL()); 3251 3252 // Must be an unqualified lookup. 3253 if (lookup->getQualifier()) return false; 3254 3255 // Must not have found a class member. Note that if one is a class 3256 // member, they're all class members. 3257 if (lookup->getNumDecls() > 0 && 3258 (*lookup->decls_begin())->isCXXClassMember()) 3259 return false; 3260 3261 // Otherwise, ADL would have been triggered. 3262 return true; 3263 } 3264 3265 void CXXNameMangler::mangleCastExpression(const Expr *E, StringRef CastEncoding) { 3266 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E); 3267 Out << CastEncoding; 3268 mangleType(ECE->getType()); 3269 mangleExpression(ECE->getSubExpr()); 3270 } 3271 3272 void CXXNameMangler::mangleInitListElements(const InitListExpr *InitList) { 3273 if (auto *Syntactic = InitList->getSyntacticForm()) 3274 InitList = Syntactic; 3275 for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i) 3276 mangleExpression(InitList->getInit(i)); 3277 } 3278 3279 void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) { 3280 // <expression> ::= <unary operator-name> <expression> 3281 // ::= <binary operator-name> <expression> <expression> 3282 // ::= <trinary operator-name> <expression> <expression> <expression> 3283 // ::= cv <type> expression # conversion with one argument 3284 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments 3285 // ::= dc <type> <expression> # dynamic_cast<type> (expression) 3286 // ::= sc <type> <expression> # static_cast<type> (expression) 3287 // ::= cc <type> <expression> # const_cast<type> (expression) 3288 // ::= rc <type> <expression> # reinterpret_cast<type> (expression) 3289 // ::= st <type> # sizeof (a type) 3290 // ::= at <type> # alignof (a type) 3291 // ::= <template-param> 3292 // ::= <function-param> 3293 // ::= sr <type> <unqualified-name> # dependent name 3294 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id 3295 // ::= ds <expression> <expression> # expr.*expr 3296 // ::= sZ <template-param> # size of a parameter pack 3297 // ::= sZ <function-param> # size of a function parameter pack 3298 // ::= <expr-primary> 3299 // <expr-primary> ::= L <type> <value number> E # integer literal 3300 // ::= L <type <value float> E # floating literal 3301 // ::= L <mangled-name> E # external name 3302 // ::= fpT # 'this' expression 3303 QualType ImplicitlyConvertedToType; 3304 3305 recurse: 3306 switch (E->getStmtClass()) { 3307 case Expr::NoStmtClass: 3308 #define ABSTRACT_STMT(Type) 3309 #define EXPR(Type, Base) 3310 #define STMT(Type, Base) \ 3311 case Expr::Type##Class: 3312 #include "clang/AST/StmtNodes.inc" 3313 // fallthrough 3314 3315 // These all can only appear in local or variable-initialization 3316 // contexts and so should never appear in a mangling. 3317 case Expr::AddrLabelExprClass: 3318 case Expr::DesignatedInitUpdateExprClass: 3319 case Expr::ImplicitValueInitExprClass: 3320 case Expr::ArrayInitLoopExprClass: 3321 case Expr::ArrayInitIndexExprClass: 3322 case Expr::NoInitExprClass: 3323 case Expr::ParenListExprClass: 3324 case Expr::LambdaExprClass: 3325 case Expr::MSPropertyRefExprClass: 3326 case Expr::MSPropertySubscriptExprClass: 3327 case Expr::TypoExprClass: // This should no longer exist in the AST by now. 3328 case Expr::OMPArraySectionExprClass: 3329 case Expr::CXXInheritedCtorInitExprClass: 3330 llvm_unreachable("unexpected statement kind"); 3331 3332 // FIXME: invent manglings for all these. 3333 case Expr::BlockExprClass: 3334 case Expr::ChooseExprClass: 3335 case Expr::CompoundLiteralExprClass: 3336 case Expr::DesignatedInitExprClass: 3337 case Expr::ExtVectorElementExprClass: 3338 case Expr::GenericSelectionExprClass: 3339 case Expr::ObjCEncodeExprClass: 3340 case Expr::ObjCIsaExprClass: 3341 case Expr::ObjCIvarRefExprClass: 3342 case Expr::ObjCMessageExprClass: 3343 case Expr::ObjCPropertyRefExprClass: 3344 case Expr::ObjCProtocolExprClass: 3345 case Expr::ObjCSelectorExprClass: 3346 case Expr::ObjCStringLiteralClass: 3347 case Expr::ObjCBoxedExprClass: 3348 case Expr::ObjCArrayLiteralClass: 3349 case Expr::ObjCDictionaryLiteralClass: 3350 case Expr::ObjCSubscriptRefExprClass: 3351 case Expr::ObjCIndirectCopyRestoreExprClass: 3352 case Expr::ObjCAvailabilityCheckExprClass: 3353 case Expr::OffsetOfExprClass: 3354 case Expr::PredefinedExprClass: 3355 case Expr::ShuffleVectorExprClass: 3356 case Expr::ConvertVectorExprClass: 3357 case Expr::StmtExprClass: 3358 case Expr::TypeTraitExprClass: 3359 case Expr::ArrayTypeTraitExprClass: 3360 case Expr::ExpressionTraitExprClass: 3361 case Expr::VAArgExprClass: 3362 case Expr::CUDAKernelCallExprClass: 3363 case Expr::AsTypeExprClass: 3364 case Expr::PseudoObjectExprClass: 3365 case Expr::AtomicExprClass: 3366 { 3367 if (!NullOut) { 3368 // As bad as this diagnostic is, it's better than crashing. 3369 DiagnosticsEngine &Diags = Context.getDiags(); 3370 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 3371 "cannot yet mangle expression type %0"); 3372 Diags.Report(E->getExprLoc(), DiagID) 3373 << E->getStmtClassName() << E->getSourceRange(); 3374 } 3375 break; 3376 } 3377 3378 case Expr::CXXUuidofExprClass: { 3379 const CXXUuidofExpr *UE = cast<CXXUuidofExpr>(E); 3380 if (UE->isTypeOperand()) { 3381 QualType UuidT = UE->getTypeOperand(Context.getASTContext()); 3382 Out << "u8__uuidoft"; 3383 mangleType(UuidT); 3384 } else { 3385 Expr *UuidExp = UE->getExprOperand(); 3386 Out << "u8__uuidofz"; 3387 mangleExpression(UuidExp, Arity); 3388 } 3389 break; 3390 } 3391 3392 // Even gcc-4.5 doesn't mangle this. 3393 case Expr::BinaryConditionalOperatorClass: { 3394 DiagnosticsEngine &Diags = Context.getDiags(); 3395 unsigned DiagID = 3396 Diags.getCustomDiagID(DiagnosticsEngine::Error, 3397 "?: operator with omitted middle operand cannot be mangled"); 3398 Diags.Report(E->getExprLoc(), DiagID) 3399 << E->getStmtClassName() << E->getSourceRange(); 3400 break; 3401 } 3402 3403 // These are used for internal purposes and cannot be meaningfully mangled. 3404 case Expr::OpaqueValueExprClass: 3405 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?"); 3406 3407 case Expr::InitListExprClass: { 3408 Out << "il"; 3409 mangleInitListElements(cast<InitListExpr>(E)); 3410 Out << "E"; 3411 break; 3412 } 3413 3414 case Expr::CXXDefaultArgExprClass: 3415 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity); 3416 break; 3417 3418 case Expr::CXXDefaultInitExprClass: 3419 mangleExpression(cast<CXXDefaultInitExpr>(E)->getExpr(), Arity); 3420 break; 3421 3422 case Expr::CXXStdInitializerListExprClass: 3423 mangleExpression(cast<CXXStdInitializerListExpr>(E)->getSubExpr(), Arity); 3424 break; 3425 3426 case Expr::SubstNonTypeTemplateParmExprClass: 3427 mangleExpression(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), 3428 Arity); 3429 break; 3430 3431 case Expr::UserDefinedLiteralClass: 3432 // We follow g++'s approach of mangling a UDL as a call to the literal 3433 // operator. 3434 case Expr::CXXMemberCallExprClass: // fallthrough 3435 case Expr::CallExprClass: { 3436 const CallExpr *CE = cast<CallExpr>(E); 3437 3438 // <expression> ::= cp <simple-id> <expression>* E 3439 // We use this mangling only when the call would use ADL except 3440 // for being parenthesized. Per discussion with David 3441 // Vandervoorde, 2011.04.25. 3442 if (isParenthesizedADLCallee(CE)) { 3443 Out << "cp"; 3444 // The callee here is a parenthesized UnresolvedLookupExpr with 3445 // no qualifier and should always get mangled as a <simple-id> 3446 // anyway. 3447 3448 // <expression> ::= cl <expression>* E 3449 } else { 3450 Out << "cl"; 3451 } 3452 3453 unsigned CallArity = CE->getNumArgs(); 3454 for (const Expr *Arg : CE->arguments()) 3455 if (isa<PackExpansionExpr>(Arg)) 3456 CallArity = UnknownArity; 3457 3458 mangleExpression(CE->getCallee(), CallArity); 3459 for (const Expr *Arg : CE->arguments()) 3460 mangleExpression(Arg); 3461 Out << 'E'; 3462 break; 3463 } 3464 3465 case Expr::CXXNewExprClass: { 3466 const CXXNewExpr *New = cast<CXXNewExpr>(E); 3467 if (New->isGlobalNew()) Out << "gs"; 3468 Out << (New->isArray() ? "na" : "nw"); 3469 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(), 3470 E = New->placement_arg_end(); I != E; ++I) 3471 mangleExpression(*I); 3472 Out << '_'; 3473 mangleType(New->getAllocatedType()); 3474 if (New->hasInitializer()) { 3475 if (New->getInitializationStyle() == CXXNewExpr::ListInit) 3476 Out << "il"; 3477 else 3478 Out << "pi"; 3479 const Expr *Init = New->getInitializer(); 3480 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) { 3481 // Directly inline the initializers. 3482 for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(), 3483 E = CCE->arg_end(); 3484 I != E; ++I) 3485 mangleExpression(*I); 3486 } else if (const ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) { 3487 for (unsigned i = 0, e = PLE->getNumExprs(); i != e; ++i) 3488 mangleExpression(PLE->getExpr(i)); 3489 } else if (New->getInitializationStyle() == CXXNewExpr::ListInit && 3490 isa<InitListExpr>(Init)) { 3491 // Only take InitListExprs apart for list-initialization. 3492 mangleInitListElements(cast<InitListExpr>(Init)); 3493 } else 3494 mangleExpression(Init); 3495 } 3496 Out << 'E'; 3497 break; 3498 } 3499 3500 case Expr::CXXPseudoDestructorExprClass: { 3501 const auto *PDE = cast<CXXPseudoDestructorExpr>(E); 3502 if (const Expr *Base = PDE->getBase()) 3503 mangleMemberExprBase(Base, PDE->isArrow()); 3504 NestedNameSpecifier *Qualifier = PDE->getQualifier(); 3505 QualType ScopeType; 3506 if (TypeSourceInfo *ScopeInfo = PDE->getScopeTypeInfo()) { 3507 if (Qualifier) { 3508 mangleUnresolvedPrefix(Qualifier, 3509 /*Recursive=*/true); 3510 mangleUnresolvedTypeOrSimpleId(ScopeInfo->getType()); 3511 Out << 'E'; 3512 } else { 3513 Out << "sr"; 3514 if (!mangleUnresolvedTypeOrSimpleId(ScopeInfo->getType())) 3515 Out << 'E'; 3516 } 3517 } else if (Qualifier) { 3518 mangleUnresolvedPrefix(Qualifier); 3519 } 3520 // <base-unresolved-name> ::= dn <destructor-name> 3521 Out << "dn"; 3522 QualType DestroyedType = PDE->getDestroyedType(); 3523 mangleUnresolvedTypeOrSimpleId(DestroyedType); 3524 break; 3525 } 3526 3527 case Expr::MemberExprClass: { 3528 const MemberExpr *ME = cast<MemberExpr>(E); 3529 mangleMemberExpr(ME->getBase(), ME->isArrow(), 3530 ME->getQualifier(), nullptr, 3531 ME->getMemberDecl()->getDeclName(), 3532 ME->getTemplateArgs(), ME->getNumTemplateArgs(), 3533 Arity); 3534 break; 3535 } 3536 3537 case Expr::UnresolvedMemberExprClass: { 3538 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E); 3539 mangleMemberExpr(ME->isImplicitAccess() ? nullptr : ME->getBase(), 3540 ME->isArrow(), ME->getQualifier(), nullptr, 3541 ME->getMemberName(), 3542 ME->getTemplateArgs(), ME->getNumTemplateArgs(), 3543 Arity); 3544 break; 3545 } 3546 3547 case Expr::CXXDependentScopeMemberExprClass: { 3548 const CXXDependentScopeMemberExpr *ME 3549 = cast<CXXDependentScopeMemberExpr>(E); 3550 mangleMemberExpr(ME->isImplicitAccess() ? nullptr : ME->getBase(), 3551 ME->isArrow(), ME->getQualifier(), 3552 ME->getFirstQualifierFoundInScope(), 3553 ME->getMember(), 3554 ME->getTemplateArgs(), ME->getNumTemplateArgs(), 3555 Arity); 3556 break; 3557 } 3558 3559 case Expr::UnresolvedLookupExprClass: { 3560 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E); 3561 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), 3562 ULE->getTemplateArgs(), ULE->getNumTemplateArgs(), 3563 Arity); 3564 break; 3565 } 3566 3567 case Expr::CXXUnresolvedConstructExprClass: { 3568 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E); 3569 unsigned N = CE->arg_size(); 3570 3571 Out << "cv"; 3572 mangleType(CE->getType()); 3573 if (N != 1) Out << '_'; 3574 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I)); 3575 if (N != 1) Out << 'E'; 3576 break; 3577 } 3578 3579 case Expr::CXXConstructExprClass: { 3580 const auto *CE = cast<CXXConstructExpr>(E); 3581 if (!CE->isListInitialization() || CE->isStdInitListInitialization()) { 3582 assert( 3583 CE->getNumArgs() >= 1 && 3584 (CE->getNumArgs() == 1 || isa<CXXDefaultArgExpr>(CE->getArg(1))) && 3585 "implicit CXXConstructExpr must have one argument"); 3586 return mangleExpression(cast<CXXConstructExpr>(E)->getArg(0)); 3587 } 3588 Out << "il"; 3589 for (auto *E : CE->arguments()) 3590 mangleExpression(E); 3591 Out << "E"; 3592 break; 3593 } 3594 3595 case Expr::CXXTemporaryObjectExprClass: { 3596 const auto *CE = cast<CXXTemporaryObjectExpr>(E); 3597 unsigned N = CE->getNumArgs(); 3598 bool List = CE->isListInitialization(); 3599 3600 if (List) 3601 Out << "tl"; 3602 else 3603 Out << "cv"; 3604 mangleType(CE->getType()); 3605 if (!List && N != 1) 3606 Out << '_'; 3607 if (CE->isStdInitListInitialization()) { 3608 // We implicitly created a std::initializer_list<T> for the first argument 3609 // of a constructor of type U in an expression of the form U{a, b, c}. 3610 // Strip all the semantic gunk off the initializer list. 3611 auto *SILE = 3612 cast<CXXStdInitializerListExpr>(CE->getArg(0)->IgnoreImplicit()); 3613 auto *ILE = cast<InitListExpr>(SILE->getSubExpr()->IgnoreImplicit()); 3614 mangleInitListElements(ILE); 3615 } else { 3616 for (auto *E : CE->arguments()) 3617 mangleExpression(E); 3618 } 3619 if (List || N != 1) 3620 Out << 'E'; 3621 break; 3622 } 3623 3624 case Expr::CXXScalarValueInitExprClass: 3625 Out << "cv"; 3626 mangleType(E->getType()); 3627 Out << "_E"; 3628 break; 3629 3630 case Expr::CXXNoexceptExprClass: 3631 Out << "nx"; 3632 mangleExpression(cast<CXXNoexceptExpr>(E)->getOperand()); 3633 break; 3634 3635 case Expr::UnaryExprOrTypeTraitExprClass: { 3636 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E); 3637 3638 if (!SAE->isInstantiationDependent()) { 3639 // Itanium C++ ABI: 3640 // If the operand of a sizeof or alignof operator is not 3641 // instantiation-dependent it is encoded as an integer literal 3642 // reflecting the result of the operator. 3643 // 3644 // If the result of the operator is implicitly converted to a known 3645 // integer type, that type is used for the literal; otherwise, the type 3646 // of std::size_t or std::ptrdiff_t is used. 3647 QualType T = (ImplicitlyConvertedToType.isNull() || 3648 !ImplicitlyConvertedToType->isIntegerType())? SAE->getType() 3649 : ImplicitlyConvertedToType; 3650 llvm::APSInt V = SAE->EvaluateKnownConstInt(Context.getASTContext()); 3651 mangleIntegerLiteral(T, V); 3652 break; 3653 } 3654 3655 switch(SAE->getKind()) { 3656 case UETT_SizeOf: 3657 Out << 's'; 3658 break; 3659 case UETT_AlignOf: 3660 Out << 'a'; 3661 break; 3662 case UETT_VecStep: { 3663 DiagnosticsEngine &Diags = Context.getDiags(); 3664 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 3665 "cannot yet mangle vec_step expression"); 3666 Diags.Report(DiagID); 3667 return; 3668 } 3669 case UETT_OpenMPRequiredSimdAlign: 3670 DiagnosticsEngine &Diags = Context.getDiags(); 3671 unsigned DiagID = Diags.getCustomDiagID( 3672 DiagnosticsEngine::Error, 3673 "cannot yet mangle __builtin_omp_required_simd_align expression"); 3674 Diags.Report(DiagID); 3675 return; 3676 } 3677 if (SAE->isArgumentType()) { 3678 Out << 't'; 3679 mangleType(SAE->getArgumentType()); 3680 } else { 3681 Out << 'z'; 3682 mangleExpression(SAE->getArgumentExpr()); 3683 } 3684 break; 3685 } 3686 3687 case Expr::CXXThrowExprClass: { 3688 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E); 3689 // <expression> ::= tw <expression> # throw expression 3690 // ::= tr # rethrow 3691 if (TE->getSubExpr()) { 3692 Out << "tw"; 3693 mangleExpression(TE->getSubExpr()); 3694 } else { 3695 Out << "tr"; 3696 } 3697 break; 3698 } 3699 3700 case Expr::CXXTypeidExprClass: { 3701 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E); 3702 // <expression> ::= ti <type> # typeid (type) 3703 // ::= te <expression> # typeid (expression) 3704 if (TIE->isTypeOperand()) { 3705 Out << "ti"; 3706 mangleType(TIE->getTypeOperand(Context.getASTContext())); 3707 } else { 3708 Out << "te"; 3709 mangleExpression(TIE->getExprOperand()); 3710 } 3711 break; 3712 } 3713 3714 case Expr::CXXDeleteExprClass: { 3715 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E); 3716 // <expression> ::= [gs] dl <expression> # [::] delete expr 3717 // ::= [gs] da <expression> # [::] delete [] expr 3718 if (DE->isGlobalDelete()) Out << "gs"; 3719 Out << (DE->isArrayForm() ? "da" : "dl"); 3720 mangleExpression(DE->getArgument()); 3721 break; 3722 } 3723 3724 case Expr::UnaryOperatorClass: { 3725 const UnaryOperator *UO = cast<UnaryOperator>(E); 3726 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()), 3727 /*Arity=*/1); 3728 mangleExpression(UO->getSubExpr()); 3729 break; 3730 } 3731 3732 case Expr::ArraySubscriptExprClass: { 3733 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E); 3734 3735 // Array subscript is treated as a syntactically weird form of 3736 // binary operator. 3737 Out << "ix"; 3738 mangleExpression(AE->getLHS()); 3739 mangleExpression(AE->getRHS()); 3740 break; 3741 } 3742 3743 case Expr::CompoundAssignOperatorClass: // fallthrough 3744 case Expr::BinaryOperatorClass: { 3745 const BinaryOperator *BO = cast<BinaryOperator>(E); 3746 if (BO->getOpcode() == BO_PtrMemD) 3747 Out << "ds"; 3748 else 3749 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()), 3750 /*Arity=*/2); 3751 mangleExpression(BO->getLHS()); 3752 mangleExpression(BO->getRHS()); 3753 break; 3754 } 3755 3756 case Expr::ConditionalOperatorClass: { 3757 const ConditionalOperator *CO = cast<ConditionalOperator>(E); 3758 mangleOperatorName(OO_Conditional, /*Arity=*/3); 3759 mangleExpression(CO->getCond()); 3760 mangleExpression(CO->getLHS(), Arity); 3761 mangleExpression(CO->getRHS(), Arity); 3762 break; 3763 } 3764 3765 case Expr::ImplicitCastExprClass: { 3766 ImplicitlyConvertedToType = E->getType(); 3767 E = cast<ImplicitCastExpr>(E)->getSubExpr(); 3768 goto recurse; 3769 } 3770 3771 case Expr::ObjCBridgedCastExprClass: { 3772 // Mangle ownership casts as a vendor extended operator __bridge, 3773 // __bridge_transfer, or __bridge_retain. 3774 StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName(); 3775 Out << "v1U" << Kind.size() << Kind; 3776 } 3777 // Fall through to mangle the cast itself. 3778 3779 case Expr::CStyleCastExprClass: 3780 mangleCastExpression(E, "cv"); 3781 break; 3782 3783 case Expr::CXXFunctionalCastExprClass: { 3784 auto *Sub = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreImplicit(); 3785 // FIXME: Add isImplicit to CXXConstructExpr. 3786 if (auto *CCE = dyn_cast<CXXConstructExpr>(Sub)) 3787 if (CCE->getParenOrBraceRange().isInvalid()) 3788 Sub = CCE->getArg(0)->IgnoreImplicit(); 3789 if (auto *StdInitList = dyn_cast<CXXStdInitializerListExpr>(Sub)) 3790 Sub = StdInitList->getSubExpr()->IgnoreImplicit(); 3791 if (auto *IL = dyn_cast<InitListExpr>(Sub)) { 3792 Out << "tl"; 3793 mangleType(E->getType()); 3794 mangleInitListElements(IL); 3795 Out << "E"; 3796 } else { 3797 mangleCastExpression(E, "cv"); 3798 } 3799 break; 3800 } 3801 3802 case Expr::CXXStaticCastExprClass: 3803 mangleCastExpression(E, "sc"); 3804 break; 3805 case Expr::CXXDynamicCastExprClass: 3806 mangleCastExpression(E, "dc"); 3807 break; 3808 case Expr::CXXReinterpretCastExprClass: 3809 mangleCastExpression(E, "rc"); 3810 break; 3811 case Expr::CXXConstCastExprClass: 3812 mangleCastExpression(E, "cc"); 3813 break; 3814 3815 case Expr::CXXOperatorCallExprClass: { 3816 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E); 3817 unsigned NumArgs = CE->getNumArgs(); 3818 // A CXXOperatorCallExpr for OO_Arrow models only semantics, not syntax 3819 // (the enclosing MemberExpr covers the syntactic portion). 3820 if (CE->getOperator() != OO_Arrow) 3821 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs); 3822 // Mangle the arguments. 3823 for (unsigned i = 0; i != NumArgs; ++i) 3824 mangleExpression(CE->getArg(i)); 3825 break; 3826 } 3827 3828 case Expr::ParenExprClass: 3829 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity); 3830 break; 3831 3832 case Expr::DeclRefExprClass: { 3833 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); 3834 3835 switch (D->getKind()) { 3836 default: 3837 // <expr-primary> ::= L <mangled-name> E # external name 3838 Out << 'L'; 3839 mangle(D); 3840 Out << 'E'; 3841 break; 3842 3843 case Decl::ParmVar: 3844 mangleFunctionParam(cast<ParmVarDecl>(D)); 3845 break; 3846 3847 case Decl::EnumConstant: { 3848 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D); 3849 mangleIntegerLiteral(ED->getType(), ED->getInitVal()); 3850 break; 3851 } 3852 3853 case Decl::NonTypeTemplateParm: { 3854 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D); 3855 mangleTemplateParameter(PD->getIndex()); 3856 break; 3857 } 3858 3859 } 3860 3861 break; 3862 } 3863 3864 case Expr::SubstNonTypeTemplateParmPackExprClass: 3865 // FIXME: not clear how to mangle this! 3866 // template <unsigned N...> class A { 3867 // template <class U...> void foo(U (&x)[N]...); 3868 // }; 3869 Out << "_SUBSTPACK_"; 3870 break; 3871 3872 case Expr::FunctionParmPackExprClass: { 3873 // FIXME: not clear how to mangle this! 3874 const FunctionParmPackExpr *FPPE = cast<FunctionParmPackExpr>(E); 3875 Out << "v110_SUBSTPACK"; 3876 mangleFunctionParam(FPPE->getParameterPack()); 3877 break; 3878 } 3879 3880 case Expr::DependentScopeDeclRefExprClass: { 3881 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E); 3882 mangleUnresolvedName(DRE->getQualifier(), DRE->getDeclName(), 3883 DRE->getTemplateArgs(), DRE->getNumTemplateArgs(), 3884 Arity); 3885 break; 3886 } 3887 3888 case Expr::CXXBindTemporaryExprClass: 3889 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr()); 3890 break; 3891 3892 case Expr::ExprWithCleanupsClass: 3893 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity); 3894 break; 3895 3896 case Expr::FloatingLiteralClass: { 3897 const FloatingLiteral *FL = cast<FloatingLiteral>(E); 3898 Out << 'L'; 3899 mangleType(FL->getType()); 3900 mangleFloat(FL->getValue()); 3901 Out << 'E'; 3902 break; 3903 } 3904 3905 case Expr::CharacterLiteralClass: 3906 Out << 'L'; 3907 mangleType(E->getType()); 3908 Out << cast<CharacterLiteral>(E)->getValue(); 3909 Out << 'E'; 3910 break; 3911 3912 // FIXME. __objc_yes/__objc_no are mangled same as true/false 3913 case Expr::ObjCBoolLiteralExprClass: 3914 Out << "Lb"; 3915 Out << (cast<ObjCBoolLiteralExpr>(E)->getValue() ? '1' : '0'); 3916 Out << 'E'; 3917 break; 3918 3919 case Expr::CXXBoolLiteralExprClass: 3920 Out << "Lb"; 3921 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0'); 3922 Out << 'E'; 3923 break; 3924 3925 case Expr::IntegerLiteralClass: { 3926 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue()); 3927 if (E->getType()->isSignedIntegerType()) 3928 Value.setIsSigned(true); 3929 mangleIntegerLiteral(E->getType(), Value); 3930 break; 3931 } 3932 3933 case Expr::ImaginaryLiteralClass: { 3934 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E); 3935 // Mangle as if a complex literal. 3936 // Proposal from David Vandevoorde, 2010.06.30. 3937 Out << 'L'; 3938 mangleType(E->getType()); 3939 if (const FloatingLiteral *Imag = 3940 dyn_cast<FloatingLiteral>(IE->getSubExpr())) { 3941 // Mangle a floating-point zero of the appropriate type. 3942 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics())); 3943 Out << '_'; 3944 mangleFloat(Imag->getValue()); 3945 } else { 3946 Out << "0_"; 3947 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue()); 3948 if (IE->getSubExpr()->getType()->isSignedIntegerType()) 3949 Value.setIsSigned(true); 3950 mangleNumber(Value); 3951 } 3952 Out << 'E'; 3953 break; 3954 } 3955 3956 case Expr::StringLiteralClass: { 3957 // Revised proposal from David Vandervoorde, 2010.07.15. 3958 Out << 'L'; 3959 assert(isa<ConstantArrayType>(E->getType())); 3960 mangleType(E->getType()); 3961 Out << 'E'; 3962 break; 3963 } 3964 3965 case Expr::GNUNullExprClass: 3966 // FIXME: should this really be mangled the same as nullptr? 3967 // fallthrough 3968 3969 case Expr::CXXNullPtrLiteralExprClass: { 3970 Out << "LDnE"; 3971 break; 3972 } 3973 3974 case Expr::PackExpansionExprClass: 3975 Out << "sp"; 3976 mangleExpression(cast<PackExpansionExpr>(E)->getPattern()); 3977 break; 3978 3979 case Expr::SizeOfPackExprClass: { 3980 auto *SPE = cast<SizeOfPackExpr>(E); 3981 if (SPE->isPartiallySubstituted()) { 3982 Out << "sP"; 3983 for (const auto &A : SPE->getPartialArguments()) 3984 mangleTemplateArg(A); 3985 Out << "E"; 3986 break; 3987 } 3988 3989 Out << "sZ"; 3990 const NamedDecl *Pack = SPE->getPack(); 3991 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack)) 3992 mangleTemplateParameter(TTP->getIndex()); 3993 else if (const NonTypeTemplateParmDecl *NTTP 3994 = dyn_cast<NonTypeTemplateParmDecl>(Pack)) 3995 mangleTemplateParameter(NTTP->getIndex()); 3996 else if (const TemplateTemplateParmDecl *TempTP 3997 = dyn_cast<TemplateTemplateParmDecl>(Pack)) 3998 mangleTemplateParameter(TempTP->getIndex()); 3999 else 4000 mangleFunctionParam(cast<ParmVarDecl>(Pack)); 4001 break; 4002 } 4003 4004 case Expr::MaterializeTemporaryExprClass: { 4005 mangleExpression(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()); 4006 break; 4007 } 4008 4009 case Expr::CXXFoldExprClass: { 4010 auto *FE = cast<CXXFoldExpr>(E); 4011 if (FE->isLeftFold()) 4012 Out << (FE->getInit() ? "fL" : "fl"); 4013 else 4014 Out << (FE->getInit() ? "fR" : "fr"); 4015 4016 if (FE->getOperator() == BO_PtrMemD) 4017 Out << "ds"; 4018 else 4019 mangleOperatorName( 4020 BinaryOperator::getOverloadedOperator(FE->getOperator()), 4021 /*Arity=*/2); 4022 4023 if (FE->getLHS()) 4024 mangleExpression(FE->getLHS()); 4025 if (FE->getRHS()) 4026 mangleExpression(FE->getRHS()); 4027 break; 4028 } 4029 4030 case Expr::CXXThisExprClass: 4031 Out << "fpT"; 4032 break; 4033 4034 case Expr::CoawaitExprClass: 4035 // FIXME: Propose a non-vendor mangling. 4036 Out << "v18co_await"; 4037 mangleExpression(cast<CoawaitExpr>(E)->getOperand()); 4038 break; 4039 4040 case Expr::DependentCoawaitExprClass: 4041 // FIXME: Propose a non-vendor mangling. 4042 Out << "v18co_await"; 4043 mangleExpression(cast<DependentCoawaitExpr>(E)->getOperand()); 4044 break; 4045 4046 case Expr::CoyieldExprClass: 4047 // FIXME: Propose a non-vendor mangling. 4048 Out << "v18co_yield"; 4049 mangleExpression(cast<CoawaitExpr>(E)->getOperand()); 4050 break; 4051 } 4052 } 4053 4054 /// Mangle an expression which refers to a parameter variable. 4055 /// 4056 /// <expression> ::= <function-param> 4057 /// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0 4058 /// <function-param> ::= fp <top-level CV-qualifiers> 4059 /// <parameter-2 non-negative number> _ # L == 0, I > 0 4060 /// <function-param> ::= fL <L-1 non-negative number> 4061 /// p <top-level CV-qualifiers> _ # L > 0, I == 0 4062 /// <function-param> ::= fL <L-1 non-negative number> 4063 /// p <top-level CV-qualifiers> 4064 /// <I-1 non-negative number> _ # L > 0, I > 0 4065 /// 4066 /// L is the nesting depth of the parameter, defined as 1 if the 4067 /// parameter comes from the innermost function prototype scope 4068 /// enclosing the current context, 2 if from the next enclosing 4069 /// function prototype scope, and so on, with one special case: if 4070 /// we've processed the full parameter clause for the innermost 4071 /// function type, then L is one less. This definition conveniently 4072 /// makes it irrelevant whether a function's result type was written 4073 /// trailing or leading, but is otherwise overly complicated; the 4074 /// numbering was first designed without considering references to 4075 /// parameter in locations other than return types, and then the 4076 /// mangling had to be generalized without changing the existing 4077 /// manglings. 4078 /// 4079 /// I is the zero-based index of the parameter within its parameter 4080 /// declaration clause. Note that the original ABI document describes 4081 /// this using 1-based ordinals. 4082 void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) { 4083 unsigned parmDepth = parm->getFunctionScopeDepth(); 4084 unsigned parmIndex = parm->getFunctionScopeIndex(); 4085 4086 // Compute 'L'. 4087 // parmDepth does not include the declaring function prototype. 4088 // FunctionTypeDepth does account for that. 4089 assert(parmDepth < FunctionTypeDepth.getDepth()); 4090 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth; 4091 if (FunctionTypeDepth.isInResultType()) 4092 nestingDepth--; 4093 4094 if (nestingDepth == 0) { 4095 Out << "fp"; 4096 } else { 4097 Out << "fL" << (nestingDepth - 1) << 'p'; 4098 } 4099 4100 // Top-level qualifiers. We don't have to worry about arrays here, 4101 // because parameters declared as arrays should already have been 4102 // transformed to have pointer type. FIXME: apparently these don't 4103 // get mangled if used as an rvalue of a known non-class type? 4104 assert(!parm->getType()->isArrayType() 4105 && "parameter's type is still an array type?"); 4106 mangleQualifiers(parm->getType().getQualifiers()); 4107 4108 // Parameter index. 4109 if (parmIndex != 0) { 4110 Out << (parmIndex - 1); 4111 } 4112 Out << '_'; 4113 } 4114 4115 void CXXNameMangler::mangleCXXCtorType(CXXCtorType T, 4116 const CXXRecordDecl *InheritedFrom) { 4117 // <ctor-dtor-name> ::= C1 # complete object constructor 4118 // ::= C2 # base object constructor 4119 // ::= CI1 <type> # complete inheriting constructor 4120 // ::= CI2 <type> # base inheriting constructor 4121 // 4122 // In addition, C5 is a comdat name with C1 and C2 in it. 4123 Out << 'C'; 4124 if (InheritedFrom) 4125 Out << 'I'; 4126 switch (T) { 4127 case Ctor_Complete: 4128 Out << '1'; 4129 break; 4130 case Ctor_Base: 4131 Out << '2'; 4132 break; 4133 case Ctor_Comdat: 4134 Out << '5'; 4135 break; 4136 case Ctor_DefaultClosure: 4137 case Ctor_CopyingClosure: 4138 llvm_unreachable("closure constructors don't exist for the Itanium ABI!"); 4139 } 4140 if (InheritedFrom) 4141 mangleName(InheritedFrom); 4142 } 4143 4144 void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) { 4145 // <ctor-dtor-name> ::= D0 # deleting destructor 4146 // ::= D1 # complete object destructor 4147 // ::= D2 # base object destructor 4148 // 4149 // In addition, D5 is a comdat name with D1, D2 and, if virtual, D0 in it. 4150 switch (T) { 4151 case Dtor_Deleting: 4152 Out << "D0"; 4153 break; 4154 case Dtor_Complete: 4155 Out << "D1"; 4156 break; 4157 case Dtor_Base: 4158 Out << "D2"; 4159 break; 4160 case Dtor_Comdat: 4161 Out << "D5"; 4162 break; 4163 } 4164 } 4165 4166 void CXXNameMangler::mangleTemplateArgs(const TemplateArgumentLoc *TemplateArgs, 4167 unsigned NumTemplateArgs) { 4168 // <template-args> ::= I <template-arg>+ E 4169 Out << 'I'; 4170 for (unsigned i = 0; i != NumTemplateArgs; ++i) 4171 mangleTemplateArg(TemplateArgs[i].getArgument()); 4172 Out << 'E'; 4173 } 4174 4175 void CXXNameMangler::mangleTemplateArgs(const TemplateArgumentList &AL) { 4176 // <template-args> ::= I <template-arg>+ E 4177 Out << 'I'; 4178 for (unsigned i = 0, e = AL.size(); i != e; ++i) 4179 mangleTemplateArg(AL[i]); 4180 Out << 'E'; 4181 } 4182 4183 void CXXNameMangler::mangleTemplateArgs(const TemplateArgument *TemplateArgs, 4184 unsigned NumTemplateArgs) { 4185 // <template-args> ::= I <template-arg>+ E 4186 Out << 'I'; 4187 for (unsigned i = 0; i != NumTemplateArgs; ++i) 4188 mangleTemplateArg(TemplateArgs[i]); 4189 Out << 'E'; 4190 } 4191 4192 void CXXNameMangler::mangleTemplateArg(TemplateArgument A) { 4193 // <template-arg> ::= <type> # type or template 4194 // ::= X <expression> E # expression 4195 // ::= <expr-primary> # simple expressions 4196 // ::= J <template-arg>* E # argument pack 4197 if (!A.isInstantiationDependent() || A.isDependent()) 4198 A = Context.getASTContext().getCanonicalTemplateArgument(A); 4199 4200 switch (A.getKind()) { 4201 case TemplateArgument::Null: 4202 llvm_unreachable("Cannot mangle NULL template argument"); 4203 4204 case TemplateArgument::Type: 4205 mangleType(A.getAsType()); 4206 break; 4207 case TemplateArgument::Template: 4208 // This is mangled as <type>. 4209 mangleType(A.getAsTemplate()); 4210 break; 4211 case TemplateArgument::TemplateExpansion: 4212 // <type> ::= Dp <type> # pack expansion (C++0x) 4213 Out << "Dp"; 4214 mangleType(A.getAsTemplateOrTemplatePattern()); 4215 break; 4216 case TemplateArgument::Expression: { 4217 // It's possible to end up with a DeclRefExpr here in certain 4218 // dependent cases, in which case we should mangle as a 4219 // declaration. 4220 const Expr *E = A.getAsExpr()->IgnoreParens(); 4221 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { 4222 const ValueDecl *D = DRE->getDecl(); 4223 if (isa<VarDecl>(D) || isa<FunctionDecl>(D)) { 4224 Out << 'L'; 4225 mangle(D); 4226 Out << 'E'; 4227 break; 4228 } 4229 } 4230 4231 Out << 'X'; 4232 mangleExpression(E); 4233 Out << 'E'; 4234 break; 4235 } 4236 case TemplateArgument::Integral: 4237 mangleIntegerLiteral(A.getIntegralType(), A.getAsIntegral()); 4238 break; 4239 case TemplateArgument::Declaration: { 4240 // <expr-primary> ::= L <mangled-name> E # external name 4241 // Clang produces AST's where pointer-to-member-function expressions 4242 // and pointer-to-function expressions are represented as a declaration not 4243 // an expression. We compensate for it here to produce the correct mangling. 4244 ValueDecl *D = A.getAsDecl(); 4245 bool compensateMangling = !A.getParamTypeForDecl()->isReferenceType(); 4246 if (compensateMangling) { 4247 Out << 'X'; 4248 mangleOperatorName(OO_Amp, 1); 4249 } 4250 4251 Out << 'L'; 4252 // References to external entities use the mangled name; if the name would 4253 // not normally be mangled then mangle it as unqualified. 4254 mangle(D); 4255 Out << 'E'; 4256 4257 if (compensateMangling) 4258 Out << 'E'; 4259 4260 break; 4261 } 4262 case TemplateArgument::NullPtr: { 4263 // <expr-primary> ::= L <type> 0 E 4264 Out << 'L'; 4265 mangleType(A.getNullPtrType()); 4266 Out << "0E"; 4267 break; 4268 } 4269 case TemplateArgument::Pack: { 4270 // <template-arg> ::= J <template-arg>* E 4271 Out << 'J'; 4272 for (const auto &P : A.pack_elements()) 4273 mangleTemplateArg(P); 4274 Out << 'E'; 4275 } 4276 } 4277 } 4278 4279 void CXXNameMangler::mangleTemplateParameter(unsigned Index) { 4280 // <template-param> ::= T_ # first template parameter 4281 // ::= T <parameter-2 non-negative number> _ 4282 if (Index == 0) 4283 Out << "T_"; 4284 else 4285 Out << 'T' << (Index - 1) << '_'; 4286 } 4287 4288 void CXXNameMangler::mangleSeqID(unsigned SeqID) { 4289 if (SeqID == 1) 4290 Out << '0'; 4291 else if (SeqID > 1) { 4292 SeqID--; 4293 4294 // <seq-id> is encoded in base-36, using digits and upper case letters. 4295 char Buffer[7]; // log(2**32) / log(36) ~= 7 4296 MutableArrayRef<char> BufferRef(Buffer); 4297 MutableArrayRef<char>::reverse_iterator I = BufferRef.rbegin(); 4298 4299 for (; SeqID != 0; SeqID /= 36) { 4300 unsigned C = SeqID % 36; 4301 *I++ = (C < 10 ? '0' + C : 'A' + C - 10); 4302 } 4303 4304 Out.write(I.base(), I - BufferRef.rbegin()); 4305 } 4306 Out << '_'; 4307 } 4308 4309 void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) { 4310 bool result = mangleSubstitution(tname); 4311 assert(result && "no existing substitution for template name"); 4312 (void) result; 4313 } 4314 4315 // <substitution> ::= S <seq-id> _ 4316 // ::= S_ 4317 bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) { 4318 // Try one of the standard substitutions first. 4319 if (mangleStandardSubstitution(ND)) 4320 return true; 4321 4322 ND = cast<NamedDecl>(ND->getCanonicalDecl()); 4323 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND)); 4324 } 4325 4326 /// Determine whether the given type has any qualifiers that are relevant for 4327 /// substitutions. 4328 static bool hasMangledSubstitutionQualifiers(QualType T) { 4329 Qualifiers Qs = T.getQualifiers(); 4330 return Qs.getCVRQualifiers() || Qs.hasAddressSpace(); 4331 } 4332 4333 bool CXXNameMangler::mangleSubstitution(QualType T) { 4334 if (!hasMangledSubstitutionQualifiers(T)) { 4335 if (const RecordType *RT = T->getAs<RecordType>()) 4336 return mangleSubstitution(RT->getDecl()); 4337 } 4338 4339 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); 4340 4341 return mangleSubstitution(TypePtr); 4342 } 4343 4344 bool CXXNameMangler::mangleSubstitution(TemplateName Template) { 4345 if (TemplateDecl *TD = Template.getAsTemplateDecl()) 4346 return mangleSubstitution(TD); 4347 4348 Template = Context.getASTContext().getCanonicalTemplateName(Template); 4349 return mangleSubstitution( 4350 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer())); 4351 } 4352 4353 bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) { 4354 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr); 4355 if (I == Substitutions.end()) 4356 return false; 4357 4358 unsigned SeqID = I->second; 4359 Out << 'S'; 4360 mangleSeqID(SeqID); 4361 4362 return true; 4363 } 4364 4365 static bool isCharType(QualType T) { 4366 if (T.isNull()) 4367 return false; 4368 4369 return T->isSpecificBuiltinType(BuiltinType::Char_S) || 4370 T->isSpecificBuiltinType(BuiltinType::Char_U); 4371 } 4372 4373 /// Returns whether a given type is a template specialization of a given name 4374 /// with a single argument of type char. 4375 static bool isCharSpecialization(QualType T, const char *Name) { 4376 if (T.isNull()) 4377 return false; 4378 4379 const RecordType *RT = T->getAs<RecordType>(); 4380 if (!RT) 4381 return false; 4382 4383 const ClassTemplateSpecializationDecl *SD = 4384 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); 4385 if (!SD) 4386 return false; 4387 4388 if (!isStdNamespace(getEffectiveDeclContext(SD))) 4389 return false; 4390 4391 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); 4392 if (TemplateArgs.size() != 1) 4393 return false; 4394 4395 if (!isCharType(TemplateArgs[0].getAsType())) 4396 return false; 4397 4398 return SD->getIdentifier()->getName() == Name; 4399 } 4400 4401 template <std::size_t StrLen> 4402 static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD, 4403 const char (&Str)[StrLen]) { 4404 if (!SD->getIdentifier()->isStr(Str)) 4405 return false; 4406 4407 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); 4408 if (TemplateArgs.size() != 2) 4409 return false; 4410 4411 if (!isCharType(TemplateArgs[0].getAsType())) 4412 return false; 4413 4414 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits")) 4415 return false; 4416 4417 return true; 4418 } 4419 4420 bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) { 4421 // <substitution> ::= St # ::std:: 4422 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { 4423 if (isStd(NS)) { 4424 Out << "St"; 4425 return true; 4426 } 4427 } 4428 4429 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) { 4430 if (!isStdNamespace(getEffectiveDeclContext(TD))) 4431 return false; 4432 4433 // <substitution> ::= Sa # ::std::allocator 4434 if (TD->getIdentifier()->isStr("allocator")) { 4435 Out << "Sa"; 4436 return true; 4437 } 4438 4439 // <<substitution> ::= Sb # ::std::basic_string 4440 if (TD->getIdentifier()->isStr("basic_string")) { 4441 Out << "Sb"; 4442 return true; 4443 } 4444 } 4445 4446 if (const ClassTemplateSpecializationDecl *SD = 4447 dyn_cast<ClassTemplateSpecializationDecl>(ND)) { 4448 if (!isStdNamespace(getEffectiveDeclContext(SD))) 4449 return false; 4450 4451 // <substitution> ::= Ss # ::std::basic_string<char, 4452 // ::std::char_traits<char>, 4453 // ::std::allocator<char> > 4454 if (SD->getIdentifier()->isStr("basic_string")) { 4455 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); 4456 4457 if (TemplateArgs.size() != 3) 4458 return false; 4459 4460 if (!isCharType(TemplateArgs[0].getAsType())) 4461 return false; 4462 4463 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits")) 4464 return false; 4465 4466 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator")) 4467 return false; 4468 4469 Out << "Ss"; 4470 return true; 4471 } 4472 4473 // <substitution> ::= Si # ::std::basic_istream<char, 4474 // ::std::char_traits<char> > 4475 if (isStreamCharSpecialization(SD, "basic_istream")) { 4476 Out << "Si"; 4477 return true; 4478 } 4479 4480 // <substitution> ::= So # ::std::basic_ostream<char, 4481 // ::std::char_traits<char> > 4482 if (isStreamCharSpecialization(SD, "basic_ostream")) { 4483 Out << "So"; 4484 return true; 4485 } 4486 4487 // <substitution> ::= Sd # ::std::basic_iostream<char, 4488 // ::std::char_traits<char> > 4489 if (isStreamCharSpecialization(SD, "basic_iostream")) { 4490 Out << "Sd"; 4491 return true; 4492 } 4493 } 4494 return false; 4495 } 4496 4497 void CXXNameMangler::addSubstitution(QualType T) { 4498 if (!hasMangledSubstitutionQualifiers(T)) { 4499 if (const RecordType *RT = T->getAs<RecordType>()) { 4500 addSubstitution(RT->getDecl()); 4501 return; 4502 } 4503 } 4504 4505 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); 4506 addSubstitution(TypePtr); 4507 } 4508 4509 void CXXNameMangler::addSubstitution(TemplateName Template) { 4510 if (TemplateDecl *TD = Template.getAsTemplateDecl()) 4511 return addSubstitution(TD); 4512 4513 Template = Context.getASTContext().getCanonicalTemplateName(Template); 4514 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer())); 4515 } 4516 4517 void CXXNameMangler::addSubstitution(uintptr_t Ptr) { 4518 assert(!Substitutions.count(Ptr) && "Substitution already exists!"); 4519 Substitutions[Ptr] = SeqID++; 4520 } 4521 4522 void CXXNameMangler::extendSubstitutions(CXXNameMangler* Other) { 4523 assert(Other->SeqID >= SeqID && "Must be superset of substitutions!"); 4524 if (Other->SeqID > SeqID) { 4525 Substitutions.swap(Other->Substitutions); 4526 SeqID = Other->SeqID; 4527 } 4528 } 4529 4530 CXXNameMangler::AbiTagList 4531 CXXNameMangler::makeFunctionReturnTypeTags(const FunctionDecl *FD) { 4532 // When derived abi tags are disabled there is no need to make any list. 4533 if (DisableDerivedAbiTags) 4534 return AbiTagList(); 4535 4536 llvm::raw_null_ostream NullOutStream; 4537 CXXNameMangler TrackReturnTypeTags(*this, NullOutStream); 4538 TrackReturnTypeTags.disableDerivedAbiTags(); 4539 4540 const FunctionProtoType *Proto = 4541 cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>()); 4542 TrackReturnTypeTags.FunctionTypeDepth.enterResultType(); 4543 TrackReturnTypeTags.mangleType(Proto->getReturnType()); 4544 TrackReturnTypeTags.FunctionTypeDepth.leaveResultType(); 4545 4546 return TrackReturnTypeTags.AbiTagsRoot.getSortedUniqueUsedAbiTags(); 4547 } 4548 4549 CXXNameMangler::AbiTagList 4550 CXXNameMangler::makeVariableTypeTags(const VarDecl *VD) { 4551 // When derived abi tags are disabled there is no need to make any list. 4552 if (DisableDerivedAbiTags) 4553 return AbiTagList(); 4554 4555 llvm::raw_null_ostream NullOutStream; 4556 CXXNameMangler TrackVariableType(*this, NullOutStream); 4557 TrackVariableType.disableDerivedAbiTags(); 4558 4559 TrackVariableType.mangleType(VD->getType()); 4560 4561 return TrackVariableType.AbiTagsRoot.getSortedUniqueUsedAbiTags(); 4562 } 4563 4564 bool CXXNameMangler::shouldHaveAbiTags(ItaniumMangleContextImpl &C, 4565 const VarDecl *VD) { 4566 llvm::raw_null_ostream NullOutStream; 4567 CXXNameMangler TrackAbiTags(C, NullOutStream, nullptr, true); 4568 TrackAbiTags.mangle(VD); 4569 return TrackAbiTags.AbiTagsRoot.getUsedAbiTags().size(); 4570 } 4571 4572 // 4573 4574 /// Mangles the name of the declaration D and emits that name to the given 4575 /// output stream. 4576 /// 4577 /// If the declaration D requires a mangled name, this routine will emit that 4578 /// mangled name to \p os and return true. Otherwise, \p os will be unchanged 4579 /// and this routine will return false. In this case, the caller should just 4580 /// emit the identifier of the declaration (\c D->getIdentifier()) as its 4581 /// name. 4582 void ItaniumMangleContextImpl::mangleCXXName(const NamedDecl *D, 4583 raw_ostream &Out) { 4584 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && 4585 "Invalid mangleName() call, argument is not a variable or function!"); 4586 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && 4587 "Invalid mangleName() call on 'structor decl!"); 4588 4589 PrettyStackTraceDecl CrashInfo(D, SourceLocation(), 4590 getASTContext().getSourceManager(), 4591 "Mangling declaration"); 4592 4593 CXXNameMangler Mangler(*this, Out, D); 4594 Mangler.mangle(D); 4595 } 4596 4597 void ItaniumMangleContextImpl::mangleCXXCtor(const CXXConstructorDecl *D, 4598 CXXCtorType Type, 4599 raw_ostream &Out) { 4600 CXXNameMangler Mangler(*this, Out, D, Type); 4601 Mangler.mangle(D); 4602 } 4603 4604 void ItaniumMangleContextImpl::mangleCXXDtor(const CXXDestructorDecl *D, 4605 CXXDtorType Type, 4606 raw_ostream &Out) { 4607 CXXNameMangler Mangler(*this, Out, D, Type); 4608 Mangler.mangle(D); 4609 } 4610 4611 void ItaniumMangleContextImpl::mangleCXXCtorComdat(const CXXConstructorDecl *D, 4612 raw_ostream &Out) { 4613 CXXNameMangler Mangler(*this, Out, D, Ctor_Comdat); 4614 Mangler.mangle(D); 4615 } 4616 4617 void ItaniumMangleContextImpl::mangleCXXDtorComdat(const CXXDestructorDecl *D, 4618 raw_ostream &Out) { 4619 CXXNameMangler Mangler(*this, Out, D, Dtor_Comdat); 4620 Mangler.mangle(D); 4621 } 4622 4623 void ItaniumMangleContextImpl::mangleThunk(const CXXMethodDecl *MD, 4624 const ThunkInfo &Thunk, 4625 raw_ostream &Out) { 4626 // <special-name> ::= T <call-offset> <base encoding> 4627 // # base is the nominal target function of thunk 4628 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding> 4629 // # base is the nominal target function of thunk 4630 // # first call-offset is 'this' adjustment 4631 // # second call-offset is result adjustment 4632 4633 assert(!isa<CXXDestructorDecl>(MD) && 4634 "Use mangleCXXDtor for destructor decls!"); 4635 CXXNameMangler Mangler(*this, Out); 4636 Mangler.getStream() << "_ZT"; 4637 if (!Thunk.Return.isEmpty()) 4638 Mangler.getStream() << 'c'; 4639 4640 // Mangle the 'this' pointer adjustment. 4641 Mangler.mangleCallOffset(Thunk.This.NonVirtual, 4642 Thunk.This.Virtual.Itanium.VCallOffsetOffset); 4643 4644 // Mangle the return pointer adjustment if there is one. 4645 if (!Thunk.Return.isEmpty()) 4646 Mangler.mangleCallOffset(Thunk.Return.NonVirtual, 4647 Thunk.Return.Virtual.Itanium.VBaseOffsetOffset); 4648 4649 Mangler.mangleFunctionEncoding(MD); 4650 } 4651 4652 void ItaniumMangleContextImpl::mangleCXXDtorThunk( 4653 const CXXDestructorDecl *DD, CXXDtorType Type, 4654 const ThisAdjustment &ThisAdjustment, raw_ostream &Out) { 4655 // <special-name> ::= T <call-offset> <base encoding> 4656 // # base is the nominal target function of thunk 4657 CXXNameMangler Mangler(*this, Out, DD, Type); 4658 Mangler.getStream() << "_ZT"; 4659 4660 // Mangle the 'this' pointer adjustment. 4661 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual, 4662 ThisAdjustment.Virtual.Itanium.VCallOffsetOffset); 4663 4664 Mangler.mangleFunctionEncoding(DD); 4665 } 4666 4667 /// Returns the mangled name for a guard variable for the passed in VarDecl. 4668 void ItaniumMangleContextImpl::mangleStaticGuardVariable(const VarDecl *D, 4669 raw_ostream &Out) { 4670 // <special-name> ::= GV <object name> # Guard variable for one-time 4671 // # initialization 4672 CXXNameMangler Mangler(*this, Out); 4673 // GCC 5.3.0 doesn't emit derived ABI tags for local names but that seems to 4674 // be a bug that is fixed in trunk. 4675 Mangler.getStream() << "_ZGV"; 4676 Mangler.mangleName(D); 4677 } 4678 4679 void ItaniumMangleContextImpl::mangleDynamicInitializer(const VarDecl *MD, 4680 raw_ostream &Out) { 4681 // These symbols are internal in the Itanium ABI, so the names don't matter. 4682 // Clang has traditionally used this symbol and allowed LLVM to adjust it to 4683 // avoid duplicate symbols. 4684 Out << "__cxx_global_var_init"; 4685 } 4686 4687 void ItaniumMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D, 4688 raw_ostream &Out) { 4689 // Prefix the mangling of D with __dtor_. 4690 CXXNameMangler Mangler(*this, Out); 4691 Mangler.getStream() << "__dtor_"; 4692 if (shouldMangleDeclName(D)) 4693 Mangler.mangle(D); 4694 else 4695 Mangler.getStream() << D->getName(); 4696 } 4697 4698 void ItaniumMangleContextImpl::mangleSEHFilterExpression( 4699 const NamedDecl *EnclosingDecl, raw_ostream &Out) { 4700 CXXNameMangler Mangler(*this, Out); 4701 Mangler.getStream() << "__filt_"; 4702 if (shouldMangleDeclName(EnclosingDecl)) 4703 Mangler.mangle(EnclosingDecl); 4704 else 4705 Mangler.getStream() << EnclosingDecl->getName(); 4706 } 4707 4708 void ItaniumMangleContextImpl::mangleSEHFinallyBlock( 4709 const NamedDecl *EnclosingDecl, raw_ostream &Out) { 4710 CXXNameMangler Mangler(*this, Out); 4711 Mangler.getStream() << "__fin_"; 4712 if (shouldMangleDeclName(EnclosingDecl)) 4713 Mangler.mangle(EnclosingDecl); 4714 else 4715 Mangler.getStream() << EnclosingDecl->getName(); 4716 } 4717 4718 void ItaniumMangleContextImpl::mangleItaniumThreadLocalInit(const VarDecl *D, 4719 raw_ostream &Out) { 4720 // <special-name> ::= TH <object name> 4721 CXXNameMangler Mangler(*this, Out); 4722 Mangler.getStream() << "_ZTH"; 4723 Mangler.mangleName(D); 4724 } 4725 4726 void 4727 ItaniumMangleContextImpl::mangleItaniumThreadLocalWrapper(const VarDecl *D, 4728 raw_ostream &Out) { 4729 // <special-name> ::= TW <object name> 4730 CXXNameMangler Mangler(*this, Out); 4731 Mangler.getStream() << "_ZTW"; 4732 Mangler.mangleName(D); 4733 } 4734 4735 void ItaniumMangleContextImpl::mangleReferenceTemporary(const VarDecl *D, 4736 unsigned ManglingNumber, 4737 raw_ostream &Out) { 4738 // We match the GCC mangling here. 4739 // <special-name> ::= GR <object name> 4740 CXXNameMangler Mangler(*this, Out); 4741 Mangler.getStream() << "_ZGR"; 4742 Mangler.mangleName(D); 4743 assert(ManglingNumber > 0 && "Reference temporary mangling number is zero!"); 4744 Mangler.mangleSeqID(ManglingNumber - 1); 4745 } 4746 4747 void ItaniumMangleContextImpl::mangleCXXVTable(const CXXRecordDecl *RD, 4748 raw_ostream &Out) { 4749 // <special-name> ::= TV <type> # virtual table 4750 CXXNameMangler Mangler(*this, Out); 4751 Mangler.getStream() << "_ZTV"; 4752 Mangler.mangleNameOrStandardSubstitution(RD); 4753 } 4754 4755 void ItaniumMangleContextImpl::mangleCXXVTT(const CXXRecordDecl *RD, 4756 raw_ostream &Out) { 4757 // <special-name> ::= TT <type> # VTT structure 4758 CXXNameMangler Mangler(*this, Out); 4759 Mangler.getStream() << "_ZTT"; 4760 Mangler.mangleNameOrStandardSubstitution(RD); 4761 } 4762 4763 void ItaniumMangleContextImpl::mangleCXXCtorVTable(const CXXRecordDecl *RD, 4764 int64_t Offset, 4765 const CXXRecordDecl *Type, 4766 raw_ostream &Out) { 4767 // <special-name> ::= TC <type> <offset number> _ <base type> 4768 CXXNameMangler Mangler(*this, Out); 4769 Mangler.getStream() << "_ZTC"; 4770 Mangler.mangleNameOrStandardSubstitution(RD); 4771 Mangler.getStream() << Offset; 4772 Mangler.getStream() << '_'; 4773 Mangler.mangleNameOrStandardSubstitution(Type); 4774 } 4775 4776 void ItaniumMangleContextImpl::mangleCXXRTTI(QualType Ty, raw_ostream &Out) { 4777 // <special-name> ::= TI <type> # typeinfo structure 4778 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers"); 4779 CXXNameMangler Mangler(*this, Out); 4780 Mangler.getStream() << "_ZTI"; 4781 Mangler.mangleType(Ty); 4782 } 4783 4784 void ItaniumMangleContextImpl::mangleCXXRTTIName(QualType Ty, 4785 raw_ostream &Out) { 4786 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string) 4787 CXXNameMangler Mangler(*this, Out); 4788 Mangler.getStream() << "_ZTS"; 4789 Mangler.mangleType(Ty); 4790 } 4791 4792 void ItaniumMangleContextImpl::mangleTypeName(QualType Ty, raw_ostream &Out) { 4793 mangleCXXRTTIName(Ty, Out); 4794 } 4795 4796 void ItaniumMangleContextImpl::mangleStringLiteral(const StringLiteral *, raw_ostream &) { 4797 llvm_unreachable("Can't mangle string literals"); 4798 } 4799 4800 ItaniumMangleContext * 4801 ItaniumMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags) { 4802 return new ItaniumMangleContextImpl(Context, Diags); 4803 } 4804