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