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