1 //===--- MicrosoftMangle.cpp - Microsoft Visual C++ Name Mangling ---------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This provides C++ name mangling targeting the Microsoft Visual C++ ABI. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/AST/Mangle.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/Attr.h" 17 #include "clang/AST/CXXInheritance.h" 18 #include "clang/AST/CharUnits.h" 19 #include "clang/AST/Decl.h" 20 #include "clang/AST/DeclCXX.h" 21 #include "clang/AST/DeclObjC.h" 22 #include "clang/AST/DeclTemplate.h" 23 #include "clang/AST/ExprCXX.h" 24 #include "clang/AST/VTableBuilder.h" 25 #include "clang/Basic/ABI.h" 26 #include "clang/Basic/DiagnosticOptions.h" 27 #include "clang/Basic/TargetInfo.h" 28 #include "llvm/ADT/StringMap.h" 29 30 using namespace clang; 31 32 namespace { 33 34 /// \brief Retrieve the declaration context that should be used when mangling 35 /// the given declaration. 36 static const DeclContext *getEffectiveDeclContext(const Decl *D) { 37 // The ABI assumes that lambda closure types that occur within 38 // default arguments live in the context of the function. However, due to 39 // the way in which Clang parses and creates function declarations, this is 40 // not the case: the lambda closure type ends up living in the context 41 // where the function itself resides, because the function declaration itself 42 // had not yet been created. Fix the context here. 43 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 44 if (RD->isLambda()) 45 if (ParmVarDecl *ContextParam = 46 dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl())) 47 return ContextParam->getDeclContext(); 48 } 49 50 // Perform the same check for block literals. 51 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) { 52 if (ParmVarDecl *ContextParam = 53 dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl())) 54 return ContextParam->getDeclContext(); 55 } 56 57 const DeclContext *DC = D->getDeclContext(); 58 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(DC)) 59 return getEffectiveDeclContext(CD); 60 61 return DC; 62 } 63 64 static const DeclContext *getEffectiveParentContext(const DeclContext *DC) { 65 return getEffectiveDeclContext(cast<Decl>(DC)); 66 } 67 68 static const FunctionDecl *getStructor(const FunctionDecl *fn) { 69 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate()) 70 return ftd->getTemplatedDecl(); 71 72 return fn; 73 } 74 75 /// MicrosoftCXXNameMangler - Manage the mangling of a single name for the 76 /// Microsoft Visual C++ ABI. 77 class MicrosoftCXXNameMangler { 78 MangleContext &Context; 79 raw_ostream &Out; 80 81 /// The "structor" is the top-level declaration being mangled, if 82 /// that's not a template specialization; otherwise it's the pattern 83 /// for that specialization. 84 const NamedDecl *Structor; 85 unsigned StructorType; 86 87 typedef llvm::StringMap<unsigned> BackRefMap; 88 BackRefMap NameBackReferences; 89 bool UseNameBackReferences; 90 91 typedef llvm::DenseMap<void*, unsigned> ArgBackRefMap; 92 ArgBackRefMap TypeBackReferences; 93 94 ASTContext &getASTContext() const { return Context.getASTContext(); } 95 96 // FIXME: If we add support for __ptr32/64 qualifiers, then we should push 97 // this check into mangleQualifiers(). 98 const bool PointersAre64Bit; 99 100 public: 101 enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result }; 102 103 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_) 104 : Context(C), Out(Out_), 105 Structor(0), StructorType(-1), 106 UseNameBackReferences(true), 107 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) == 108 64) { } 109 110 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_, 111 const CXXDestructorDecl *D, CXXDtorType Type) 112 : Context(C), Out(Out_), 113 Structor(getStructor(D)), StructorType(Type), 114 UseNameBackReferences(true), 115 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) == 116 64) { } 117 118 raw_ostream &getStream() const { return Out; } 119 120 void mangle(const NamedDecl *D, StringRef Prefix = "\01?"); 121 void mangleName(const NamedDecl *ND); 122 void mangleDeclaration(const NamedDecl *ND); 123 void mangleFunctionEncoding(const FunctionDecl *FD); 124 void mangleVariableEncoding(const VarDecl *VD); 125 void mangleMemberDataPointer(const CXXRecordDecl *RD, const ValueDecl *VD); 126 void mangleMemberFunctionPointer(const CXXRecordDecl *RD, 127 const CXXMethodDecl *MD); 128 void mangleVirtualMemPtrThunk( 129 const CXXMethodDecl *MD, 130 const MicrosoftVTableContext::MethodVFTableLocation &ML); 131 void mangleNumber(int64_t Number); 132 void mangleType(QualType T, SourceRange Range, 133 QualifierMangleMode QMM = QMM_Mangle); 134 void mangleFunctionType(const FunctionType *T, const FunctionDecl *D = 0, 135 bool ForceInstMethod = false); 136 void manglePostfix(const DeclContext *DC, bool NoFunction = false); 137 138 private: 139 void disableBackReferences() { UseNameBackReferences = false; } 140 void mangleUnqualifiedName(const NamedDecl *ND) { 141 mangleUnqualifiedName(ND, ND->getDeclName()); 142 } 143 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name); 144 void mangleSourceName(StringRef Name); 145 void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc); 146 void mangleCXXDtorType(CXXDtorType T); 147 void mangleQualifiers(Qualifiers Quals, bool IsMember); 148 void manglePointerQualifiers(Qualifiers Quals); 149 150 void mangleUnscopedTemplateName(const TemplateDecl *ND); 151 void mangleTemplateInstantiationName(const TemplateDecl *TD, 152 const TemplateArgumentList &TemplateArgs); 153 void mangleObjCMethodName(const ObjCMethodDecl *MD); 154 void mangleLocalName(const FunctionDecl *FD); 155 156 void mangleArgumentType(QualType T, SourceRange Range); 157 158 // Declare manglers for every type class. 159 #define ABSTRACT_TYPE(CLASS, PARENT) 160 #define NON_CANONICAL_TYPE(CLASS, PARENT) 161 #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \ 162 SourceRange Range); 163 #include "clang/AST/TypeNodes.def" 164 #undef ABSTRACT_TYPE 165 #undef NON_CANONICAL_TYPE 166 #undef TYPE 167 168 void mangleType(const TagDecl *TD); 169 void mangleDecayedArrayType(const ArrayType *T); 170 void mangleArrayType(const ArrayType *T); 171 void mangleFunctionClass(const FunctionDecl *FD); 172 void mangleCallingConvention(const FunctionType *T); 173 void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean); 174 void mangleExpression(const Expr *E); 175 void mangleThrowSpecification(const FunctionProtoType *T); 176 177 void mangleTemplateArgs(const TemplateDecl *TD, 178 const TemplateArgumentList &TemplateArgs); 179 void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA); 180 }; 181 182 /// MicrosoftMangleContextImpl - Overrides the default MangleContext for the 183 /// Microsoft Visual C++ ABI. 184 class MicrosoftMangleContextImpl : public MicrosoftMangleContext { 185 public: 186 MicrosoftMangleContextImpl(ASTContext &Context, DiagnosticsEngine &Diags) 187 : MicrosoftMangleContext(Context, Diags) {} 188 virtual bool shouldMangleCXXName(const NamedDecl *D); 189 virtual void mangleCXXName(const NamedDecl *D, raw_ostream &Out); 190 virtual void mangleVirtualMemPtrThunk(const CXXMethodDecl *MD, 191 raw_ostream &); 192 virtual void mangleThunk(const CXXMethodDecl *MD, 193 const ThunkInfo &Thunk, 194 raw_ostream &); 195 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, 196 const ThisAdjustment &ThisAdjustment, 197 raw_ostream &); 198 virtual void mangleCXXVFTable(const CXXRecordDecl *Derived, 199 ArrayRef<const CXXRecordDecl *> BasePath, 200 raw_ostream &Out); 201 virtual void mangleCXXVBTable(const CXXRecordDecl *Derived, 202 ArrayRef<const CXXRecordDecl *> BasePath, 203 raw_ostream &Out); 204 virtual void mangleCXXRTTI(QualType T, raw_ostream &); 205 virtual void mangleCXXRTTIName(QualType T, raw_ostream &); 206 virtual void mangleTypeName(QualType T, raw_ostream &); 207 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, 208 raw_ostream &); 209 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, 210 raw_ostream &); 211 virtual void mangleReferenceTemporary(const VarDecl *, raw_ostream &); 212 virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out); 213 virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out); 214 virtual void mangleDynamicAtExitDestructor(const VarDecl *D, 215 raw_ostream &Out); 216 217 private: 218 void mangleInitFiniStub(const VarDecl *D, raw_ostream &Out, char CharCode); 219 }; 220 221 } 222 223 bool MicrosoftMangleContextImpl::shouldMangleCXXName(const NamedDecl *D) { 224 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 225 LanguageLinkage L = FD->getLanguageLinkage(); 226 // Overloadable functions need mangling. 227 if (FD->hasAttr<OverloadableAttr>()) 228 return true; 229 230 // The ABI expects that we would never mangle "typical" user-defined entry 231 // points regardless of visibility or freestanding-ness. 232 // 233 // N.B. This is distinct from asking about "main". "main" has a lot of 234 // special rules associated with it in the standard while these 235 // user-defined entry points are outside of the purview of the standard. 236 // For example, there can be only one definition for "main" in a standards 237 // compliant program; however nothing forbids the existence of wmain and 238 // WinMain in the same translation unit. 239 if (FD->isMSVCRTEntryPoint()) 240 return false; 241 242 // C++ functions and those whose names are not a simple identifier need 243 // mangling. 244 if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage) 245 return true; 246 247 // C functions are not mangled. 248 if (L == CLanguageLinkage) 249 return false; 250 } 251 252 // Otherwise, no mangling is done outside C++ mode. 253 if (!getASTContext().getLangOpts().CPlusPlus) 254 return false; 255 256 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 257 // C variables are not mangled. 258 if (VD->isExternC()) 259 return false; 260 261 // Variables at global scope with non-internal linkage are not mangled. 262 const DeclContext *DC = getEffectiveDeclContext(D); 263 // Check for extern variable declared locally. 264 if (DC->isFunctionOrMethod() && D->hasLinkage()) 265 while (!DC->isNamespace() && !DC->isTranslationUnit()) 266 DC = getEffectiveParentContext(DC); 267 268 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage && 269 !isa<VarTemplateSpecializationDecl>(D)) 270 return false; 271 } 272 273 return true; 274 } 275 276 void MicrosoftCXXNameMangler::mangle(const NamedDecl *D, 277 StringRef Prefix) { 278 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names. 279 // Therefore it's really important that we don't decorate the 280 // name with leading underscores or leading/trailing at signs. So, by 281 // default, we emit an asm marker at the start so we get the name right. 282 // Callers can override this with a custom prefix. 283 284 // <mangled-name> ::= ? <name> <type-encoding> 285 Out << Prefix; 286 mangleName(D); 287 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 288 mangleFunctionEncoding(FD); 289 else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 290 mangleVariableEncoding(VD); 291 else { 292 // TODO: Fields? Can MSVC even mangle them? 293 // Issue a diagnostic for now. 294 DiagnosticsEngine &Diags = Context.getDiags(); 295 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 296 "cannot mangle this declaration yet"); 297 Diags.Report(D->getLocation(), DiagID) 298 << D->getSourceRange(); 299 } 300 } 301 302 void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { 303 // <type-encoding> ::= <function-class> <function-type> 304 305 // Since MSVC operates on the type as written and not the canonical type, it 306 // actually matters which decl we have here. MSVC appears to choose the 307 // first, since it is most likely to be the declaration in a header file. 308 FD = FD->getFirstDecl(); 309 310 // We should never ever see a FunctionNoProtoType at this point. 311 // We don't even know how to mangle their types anyway :). 312 const FunctionProtoType *FT = FD->getType()->castAs<FunctionProtoType>(); 313 314 // extern "C" functions can hold entities that must be mangled. 315 // As it stands, these functions still need to get expressed in the full 316 // external name. They have their class and type omitted, replaced with '9'. 317 if (Context.shouldMangleDeclName(FD)) { 318 // First, the function class. 319 mangleFunctionClass(FD); 320 321 mangleFunctionType(FT, FD); 322 } else 323 Out << '9'; 324 } 325 326 void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) { 327 // <type-encoding> ::= <storage-class> <variable-type> 328 // <storage-class> ::= 0 # private static member 329 // ::= 1 # protected static member 330 // ::= 2 # public static member 331 // ::= 3 # global 332 // ::= 4 # static local 333 334 // The first character in the encoding (after the name) is the storage class. 335 if (VD->isStaticDataMember()) { 336 // If it's a static member, it also encodes the access level. 337 switch (VD->getAccess()) { 338 default: 339 case AS_private: Out << '0'; break; 340 case AS_protected: Out << '1'; break; 341 case AS_public: Out << '2'; break; 342 } 343 } 344 else if (!VD->isStaticLocal()) 345 Out << '3'; 346 else 347 Out << '4'; 348 // Now mangle the type. 349 // <variable-type> ::= <type> <cvr-qualifiers> 350 // ::= <type> <pointee-cvr-qualifiers> # pointers, references 351 // Pointers and references are odd. The type of 'int * const foo;' gets 352 // mangled as 'QAHA' instead of 'PAHB', for example. 353 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc(); 354 QualType Ty = VD->getType(); 355 if (Ty->isPointerType() || Ty->isReferenceType() || 356 Ty->isMemberPointerType()) { 357 mangleType(Ty, TL.getSourceRange(), QMM_Drop); 358 if (PointersAre64Bit) 359 Out << 'E'; 360 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) { 361 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true); 362 // Member pointers are suffixed with a back reference to the member 363 // pointer's class name. 364 mangleName(MPT->getClass()->getAsCXXRecordDecl()); 365 } else 366 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false); 367 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) { 368 // Global arrays are funny, too. 369 mangleDecayedArrayType(AT); 370 if (AT->getElementType()->isArrayType()) 371 Out << 'A'; 372 else 373 mangleQualifiers(Ty.getQualifiers(), false); 374 } else { 375 mangleType(Ty, TL.getSourceRange(), QMM_Drop); 376 mangleQualifiers(Ty.getLocalQualifiers(), false); 377 } 378 } 379 380 void MicrosoftCXXNameMangler::mangleMemberDataPointer(const CXXRecordDecl *RD, 381 const ValueDecl *VD) { 382 // <member-data-pointer> ::= <integer-literal> 383 // ::= $F <number> <number> 384 // ::= $G <number> <number> <number> 385 386 int64_t FieldOffset; 387 int64_t VBTableOffset; 388 MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel(); 389 if (VD) { 390 FieldOffset = getASTContext().getFieldOffset(VD); 391 assert(FieldOffset % getASTContext().getCharWidth() == 0 && 392 "cannot take address of bitfield"); 393 FieldOffset /= getASTContext().getCharWidth(); 394 395 VBTableOffset = 0; 396 } else { 397 FieldOffset = RD->nullFieldOffsetIsZero() ? 0 : -1; 398 399 VBTableOffset = -1; 400 } 401 402 char Code = '\0'; 403 switch (IM) { 404 case MSInheritanceAttr::Keyword_single_inheritance: Code = '0'; break; 405 case MSInheritanceAttr::Keyword_multiple_inheritance: Code = '0'; break; 406 case MSInheritanceAttr::Keyword_virtual_inheritance: Code = 'F'; break; 407 case MSInheritanceAttr::Keyword_unspecified_inheritance: Code = 'G'; break; 408 } 409 410 Out << '$' << Code; 411 412 mangleNumber(FieldOffset); 413 414 if (MSInheritanceAttr::hasVBPtrOffsetField(IM)) 415 mangleNumber(0); 416 if (MSInheritanceAttr::hasVBTableOffsetField(IM)) 417 mangleNumber(VBTableOffset); 418 } 419 420 void 421 MicrosoftCXXNameMangler::mangleMemberFunctionPointer(const CXXRecordDecl *RD, 422 const CXXMethodDecl *MD) { 423 // <member-function-pointer> ::= $1? <name> 424 // ::= $H? <name> <number> 425 // ::= $I? <name> <number> <number> 426 // ::= $J? <name> <number> <number> <number> 427 // ::= $0A@ 428 429 MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel(); 430 431 // The null member function pointer is $0A@ in function templates and crashes 432 // MSVC when used in class templates, so we don't know what they really look 433 // like. 434 if (!MD) { 435 Out << "$0A@"; 436 return; 437 } 438 439 char Code = '\0'; 440 switch (IM) { 441 case MSInheritanceAttr::Keyword_single_inheritance: Code = '1'; break; 442 case MSInheritanceAttr::Keyword_multiple_inheritance: Code = 'H'; break; 443 case MSInheritanceAttr::Keyword_virtual_inheritance: Code = 'I'; break; 444 case MSInheritanceAttr::Keyword_unspecified_inheritance: Code = 'J'; break; 445 } 446 447 Out << '$' << Code << '?'; 448 449 // If non-virtual, mangle the name. If virtual, mangle as a virtual memptr 450 // thunk. 451 uint64_t NVOffset = 0; 452 uint64_t VBTableOffset = 0; 453 if (MD->isVirtual()) { 454 MicrosoftVTableContext *VTContext = 455 cast<MicrosoftVTableContext>(getASTContext().getVTableContext()); 456 const MicrosoftVTableContext::MethodVFTableLocation &ML = 457 VTContext->getMethodVFTableLocation(GlobalDecl(MD)); 458 mangleVirtualMemPtrThunk(MD, ML); 459 NVOffset = ML.VFPtrOffset.getQuantity(); 460 VBTableOffset = ML.VBTableIndex * 4; 461 if (ML.VBase) { 462 DiagnosticsEngine &Diags = Context.getDiags(); 463 unsigned DiagID = Diags.getCustomDiagID( 464 DiagnosticsEngine::Error, 465 "cannot mangle pointers to member functions from virtual bases"); 466 Diags.Report(MD->getLocation(), DiagID); 467 } 468 } else { 469 mangleName(MD); 470 mangleFunctionEncoding(MD); 471 } 472 473 if (MSInheritanceAttr::hasNVOffsetField(/*IsMemberFunction=*/true, IM)) 474 mangleNumber(NVOffset); 475 if (MSInheritanceAttr::hasVBPtrOffsetField(IM)) 476 mangleNumber(0); 477 if (MSInheritanceAttr::hasVBTableOffsetField(IM)) 478 mangleNumber(VBTableOffset); 479 } 480 481 void MicrosoftCXXNameMangler::mangleVirtualMemPtrThunk( 482 const CXXMethodDecl *MD, 483 const MicrosoftVTableContext::MethodVFTableLocation &ML) { 484 // Get the vftable offset. 485 CharUnits PointerWidth = getASTContext().toCharUnitsFromBits( 486 getASTContext().getTargetInfo().getPointerWidth(0)); 487 uint64_t OffsetInVFTable = ML.Index * PointerWidth.getQuantity(); 488 489 Out << "?_9"; 490 mangleName(MD->getParent()); 491 Out << "$B"; 492 mangleNumber(OffsetInVFTable); 493 Out << 'A'; 494 Out << (PointersAre64Bit ? 'A' : 'E'); 495 } 496 497 void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) { 498 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @ 499 const DeclContext *DC = ND->getDeclContext(); 500 501 // Always start with the unqualified name. 502 mangleUnqualifiedName(ND); 503 504 // If this is an extern variable declared locally, the relevant DeclContext 505 // is that of the containing namespace, or the translation unit. 506 if (isa<FunctionDecl>(DC) && ND->hasLinkage()) 507 while (!DC->isNamespace() && !DC->isTranslationUnit()) 508 DC = DC->getParent(); 509 510 manglePostfix(DC); 511 512 // Terminate the whole name with an '@'. 513 Out << '@'; 514 } 515 516 void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) { 517 // <non-negative integer> ::= A@ # when Number == 0 518 // ::= <decimal digit> # when 1 <= Number <= 10 519 // ::= <hex digit>+ @ # when Number >= 10 520 // 521 // <number> ::= [?] <non-negative integer> 522 523 uint64_t Value = static_cast<uint64_t>(Number); 524 if (Number < 0) { 525 Value = -Value; 526 Out << '?'; 527 } 528 529 if (Value == 0) 530 Out << "A@"; 531 else if (Value >= 1 && Value <= 10) 532 Out << (Value - 1); 533 else { 534 // Numbers that are not encoded as decimal digits are represented as nibbles 535 // in the range of ASCII characters 'A' to 'P'. 536 // The number 0x123450 would be encoded as 'BCDEFA' 537 char EncodedNumberBuffer[sizeof(uint64_t) * 2]; 538 llvm::MutableArrayRef<char> BufferRef(EncodedNumberBuffer); 539 llvm::MutableArrayRef<char>::reverse_iterator I = BufferRef.rbegin(); 540 for (; Value != 0; Value >>= 4) 541 *I++ = 'A' + (Value & 0xf); 542 Out.write(I.base(), I - BufferRef.rbegin()); 543 Out << '@'; 544 } 545 } 546 547 static const TemplateDecl * 548 isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) { 549 // Check if we have a function template. 550 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){ 551 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) { 552 TemplateArgs = FD->getTemplateSpecializationArgs(); 553 return TD; 554 } 555 } 556 557 // Check if we have a class template. 558 if (const ClassTemplateSpecializationDecl *Spec = 559 dyn_cast<ClassTemplateSpecializationDecl>(ND)) { 560 TemplateArgs = &Spec->getTemplateArgs(); 561 return Spec->getSpecializedTemplate(); 562 } 563 564 return 0; 565 } 566 567 void 568 MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, 569 DeclarationName Name) { 570 // <unqualified-name> ::= <operator-name> 571 // ::= <ctor-dtor-name> 572 // ::= <source-name> 573 // ::= <template-name> 574 575 // Check if we have a template. 576 const TemplateArgumentList *TemplateArgs = 0; 577 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { 578 // Function templates aren't considered for name back referencing. This 579 // makes sense since function templates aren't likely to occur multiple 580 // times in a symbol. 581 // FIXME: Test alias template mangling with MSVC 2013. 582 if (!isa<ClassTemplateDecl>(TD)) { 583 mangleTemplateInstantiationName(TD, *TemplateArgs); 584 return; 585 } 586 587 // We have a class template. 588 // Here comes the tricky thing: if we need to mangle something like 589 // void foo(A::X<Y>, B::X<Y>), 590 // the X<Y> part is aliased. However, if you need to mangle 591 // void foo(A::X<A::Y>, A::X<B::Y>), 592 // the A::X<> part is not aliased. 593 // That said, from the mangler's perspective we have a structure like this: 594 // namespace[s] -> type[ -> template-parameters] 595 // but from the Clang perspective we have 596 // type [ -> template-parameters] 597 // \-> namespace[s] 598 // What we do is we create a new mangler, mangle the same type (without 599 // a namespace suffix) using the extra mangler with back references 600 // disabled (to avoid infinite recursion) and then use the mangled type 601 // name as a key to check the mangling of different types for aliasing. 602 603 std::string BackReferenceKey; 604 BackRefMap::iterator Found; 605 if (UseNameBackReferences) { 606 llvm::raw_string_ostream Stream(BackReferenceKey); 607 MicrosoftCXXNameMangler Extra(Context, Stream); 608 Extra.disableBackReferences(); 609 Extra.mangleUnqualifiedName(ND, Name); 610 Stream.flush(); 611 612 Found = NameBackReferences.find(BackReferenceKey); 613 } 614 if (!UseNameBackReferences || Found == NameBackReferences.end()) { 615 mangleTemplateInstantiationName(TD, *TemplateArgs); 616 if (UseNameBackReferences && NameBackReferences.size() < 10) { 617 size_t Size = NameBackReferences.size(); 618 NameBackReferences[BackReferenceKey] = Size; 619 } 620 } else { 621 Out << Found->second; 622 } 623 return; 624 } 625 626 switch (Name.getNameKind()) { 627 case DeclarationName::Identifier: { 628 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { 629 mangleSourceName(II->getName()); 630 break; 631 } 632 633 // Otherwise, an anonymous entity. We must have a declaration. 634 assert(ND && "mangling empty name without declaration"); 635 636 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { 637 if (NS->isAnonymousNamespace()) { 638 Out << "?A@"; 639 break; 640 } 641 } 642 643 // We must have an anonymous struct. 644 const TagDecl *TD = cast<TagDecl>(ND); 645 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) { 646 assert(TD->getDeclContext() == D->getDeclContext() && 647 "Typedef should not be in another decl context!"); 648 assert(D->getDeclName().getAsIdentifierInfo() && 649 "Typedef was not named!"); 650 mangleSourceName(D->getDeclName().getAsIdentifierInfo()->getName()); 651 break; 652 } 653 654 if (TD->hasDeclaratorForAnonDecl()) { 655 // Anonymous types with no tag or typedef get the name of their 656 // declarator mangled in. 657 llvm::SmallString<64> Name("<unnamed-type-"); 658 Name += TD->getDeclaratorForAnonDecl()->getName(); 659 Name += ">"; 660 mangleSourceName(Name.str()); 661 } else { 662 // Anonymous types with no tag, no typedef, or declarator get 663 // '<unnamed-tag>'. 664 mangleSourceName("<unnamed-tag>"); 665 } 666 break; 667 } 668 669 case DeclarationName::ObjCZeroArgSelector: 670 case DeclarationName::ObjCOneArgSelector: 671 case DeclarationName::ObjCMultiArgSelector: 672 llvm_unreachable("Can't mangle Objective-C selector names here!"); 673 674 case DeclarationName::CXXConstructorName: 675 if (ND == Structor) { 676 assert(StructorType == Ctor_Complete && 677 "Should never be asked to mangle a ctor other than complete"); 678 } 679 Out << "?0"; 680 break; 681 682 case DeclarationName::CXXDestructorName: 683 if (ND == Structor) 684 // If the named decl is the C++ destructor we're mangling, 685 // use the type we were given. 686 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType)); 687 else 688 // Otherwise, use the base destructor name. This is relevant if a 689 // class with a destructor is declared within a destructor. 690 mangleCXXDtorType(Dtor_Base); 691 break; 692 693 case DeclarationName::CXXConversionFunctionName: 694 // <operator-name> ::= ?B # (cast) 695 // The target type is encoded as the return type. 696 Out << "?B"; 697 break; 698 699 case DeclarationName::CXXOperatorName: 700 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation()); 701 break; 702 703 case DeclarationName::CXXLiteralOperatorName: { 704 // FIXME: Was this added in VS2010? Does MS even know how to mangle this? 705 DiagnosticsEngine Diags = Context.getDiags(); 706 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 707 "cannot mangle this literal operator yet"); 708 Diags.Report(ND->getLocation(), DiagID); 709 break; 710 } 711 712 case DeclarationName::CXXUsingDirective: 713 llvm_unreachable("Can't mangle a using directive name!"); 714 } 715 } 716 717 void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC, 718 bool NoFunction) { 719 // <postfix> ::= <unqualified-name> [<postfix>] 720 // ::= <substitution> [<postfix>] 721 722 if (!DC) return; 723 724 while (isa<LinkageSpecDecl>(DC)) 725 DC = DC->getParent(); 726 727 if (DC->isTranslationUnit()) 728 return; 729 730 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) { 731 DiagnosticsEngine Diags = Context.getDiags(); 732 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 733 "cannot mangle a local inside this block yet"); 734 Diags.Report(BD->getLocation(), DiagID); 735 736 // FIXME: This is completely, utterly, wrong; see ItaniumMangle 737 // for how this should be done. 738 Out << "__block_invoke" << Context.getBlockId(BD, false); 739 Out << '@'; 740 return manglePostfix(DC->getParent(), NoFunction); 741 } else if (isa<CapturedDecl>(DC)) { 742 // Skip CapturedDecl context. 743 manglePostfix(DC->getParent(), NoFunction); 744 return; 745 } 746 747 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC))) 748 return; 749 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) 750 mangleObjCMethodName(Method); 751 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC)) 752 mangleLocalName(Func); 753 else { 754 mangleUnqualifiedName(cast<NamedDecl>(DC)); 755 manglePostfix(DC->getParent(), NoFunction); 756 } 757 } 758 759 void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) { 760 // Microsoft uses the names on the case labels for these dtor variants. Clang 761 // uses the Itanium terminology internally. Everything in this ABI delegates 762 // towards the base dtor. 763 switch (T) { 764 // <operator-name> ::= ?1 # destructor 765 case Dtor_Base: Out << "?1"; return; 766 // <operator-name> ::= ?_D # vbase destructor 767 case Dtor_Complete: Out << "?_D"; return; 768 // <operator-name> ::= ?_G # scalar deleting destructor 769 case Dtor_Deleting: Out << "?_G"; return; 770 // <operator-name> ::= ?_E # vector deleting destructor 771 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need 772 // it. 773 } 774 llvm_unreachable("Unsupported dtor type?"); 775 } 776 777 void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, 778 SourceLocation Loc) { 779 switch (OO) { 780 // ?0 # constructor 781 // ?1 # destructor 782 // <operator-name> ::= ?2 # new 783 case OO_New: Out << "?2"; break; 784 // <operator-name> ::= ?3 # delete 785 case OO_Delete: Out << "?3"; break; 786 // <operator-name> ::= ?4 # = 787 case OO_Equal: Out << "?4"; break; 788 // <operator-name> ::= ?5 # >> 789 case OO_GreaterGreater: Out << "?5"; break; 790 // <operator-name> ::= ?6 # << 791 case OO_LessLess: Out << "?6"; break; 792 // <operator-name> ::= ?7 # ! 793 case OO_Exclaim: Out << "?7"; break; 794 // <operator-name> ::= ?8 # == 795 case OO_EqualEqual: Out << "?8"; break; 796 // <operator-name> ::= ?9 # != 797 case OO_ExclaimEqual: Out << "?9"; break; 798 // <operator-name> ::= ?A # [] 799 case OO_Subscript: Out << "?A"; break; 800 // ?B # conversion 801 // <operator-name> ::= ?C # -> 802 case OO_Arrow: Out << "?C"; break; 803 // <operator-name> ::= ?D # * 804 case OO_Star: Out << "?D"; break; 805 // <operator-name> ::= ?E # ++ 806 case OO_PlusPlus: Out << "?E"; break; 807 // <operator-name> ::= ?F # -- 808 case OO_MinusMinus: Out << "?F"; break; 809 // <operator-name> ::= ?G # - 810 case OO_Minus: Out << "?G"; break; 811 // <operator-name> ::= ?H # + 812 case OO_Plus: Out << "?H"; break; 813 // <operator-name> ::= ?I # & 814 case OO_Amp: Out << "?I"; break; 815 // <operator-name> ::= ?J # ->* 816 case OO_ArrowStar: Out << "?J"; break; 817 // <operator-name> ::= ?K # / 818 case OO_Slash: Out << "?K"; break; 819 // <operator-name> ::= ?L # % 820 case OO_Percent: Out << "?L"; break; 821 // <operator-name> ::= ?M # < 822 case OO_Less: Out << "?M"; break; 823 // <operator-name> ::= ?N # <= 824 case OO_LessEqual: Out << "?N"; break; 825 // <operator-name> ::= ?O # > 826 case OO_Greater: Out << "?O"; break; 827 // <operator-name> ::= ?P # >= 828 case OO_GreaterEqual: Out << "?P"; break; 829 // <operator-name> ::= ?Q # , 830 case OO_Comma: Out << "?Q"; break; 831 // <operator-name> ::= ?R # () 832 case OO_Call: Out << "?R"; break; 833 // <operator-name> ::= ?S # ~ 834 case OO_Tilde: Out << "?S"; break; 835 // <operator-name> ::= ?T # ^ 836 case OO_Caret: Out << "?T"; break; 837 // <operator-name> ::= ?U # | 838 case OO_Pipe: Out << "?U"; break; 839 // <operator-name> ::= ?V # && 840 case OO_AmpAmp: Out << "?V"; break; 841 // <operator-name> ::= ?W # || 842 case OO_PipePipe: Out << "?W"; break; 843 // <operator-name> ::= ?X # *= 844 case OO_StarEqual: Out << "?X"; break; 845 // <operator-name> ::= ?Y # += 846 case OO_PlusEqual: Out << "?Y"; break; 847 // <operator-name> ::= ?Z # -= 848 case OO_MinusEqual: Out << "?Z"; break; 849 // <operator-name> ::= ?_0 # /= 850 case OO_SlashEqual: Out << "?_0"; break; 851 // <operator-name> ::= ?_1 # %= 852 case OO_PercentEqual: Out << "?_1"; break; 853 // <operator-name> ::= ?_2 # >>= 854 case OO_GreaterGreaterEqual: Out << "?_2"; break; 855 // <operator-name> ::= ?_3 # <<= 856 case OO_LessLessEqual: Out << "?_3"; break; 857 // <operator-name> ::= ?_4 # &= 858 case OO_AmpEqual: Out << "?_4"; break; 859 // <operator-name> ::= ?_5 # |= 860 case OO_PipeEqual: Out << "?_5"; break; 861 // <operator-name> ::= ?_6 # ^= 862 case OO_CaretEqual: Out << "?_6"; break; 863 // ?_7 # vftable 864 // ?_8 # vbtable 865 // ?_9 # vcall 866 // ?_A # typeof 867 // ?_B # local static guard 868 // ?_C # string 869 // ?_D # vbase destructor 870 // ?_E # vector deleting destructor 871 // ?_F # default constructor closure 872 // ?_G # scalar deleting destructor 873 // ?_H # vector constructor iterator 874 // ?_I # vector destructor iterator 875 // ?_J # vector vbase constructor iterator 876 // ?_K # virtual displacement map 877 // ?_L # eh vector constructor iterator 878 // ?_M # eh vector destructor iterator 879 // ?_N # eh vector vbase constructor iterator 880 // ?_O # copy constructor closure 881 // ?_P<name> # udt returning <name> 882 // ?_Q # <unknown> 883 // ?_R0 # RTTI Type Descriptor 884 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d) 885 // ?_R2 # RTTI Base Class Array 886 // ?_R3 # RTTI Class Hierarchy Descriptor 887 // ?_R4 # RTTI Complete Object Locator 888 // ?_S # local vftable 889 // ?_T # local vftable constructor closure 890 // <operator-name> ::= ?_U # new[] 891 case OO_Array_New: Out << "?_U"; break; 892 // <operator-name> ::= ?_V # delete[] 893 case OO_Array_Delete: Out << "?_V"; break; 894 895 case OO_Conditional: { 896 DiagnosticsEngine &Diags = Context.getDiags(); 897 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 898 "cannot mangle this conditional operator yet"); 899 Diags.Report(Loc, DiagID); 900 break; 901 } 902 903 case OO_None: 904 case NUM_OVERLOADED_OPERATORS: 905 llvm_unreachable("Not an overloaded operator"); 906 } 907 } 908 909 void MicrosoftCXXNameMangler::mangleSourceName(StringRef Name) { 910 // <source name> ::= <identifier> @ 911 BackRefMap::iterator Found; 912 if (UseNameBackReferences) 913 Found = NameBackReferences.find(Name); 914 if (!UseNameBackReferences || Found == NameBackReferences.end()) { 915 Out << Name << '@'; 916 if (UseNameBackReferences && NameBackReferences.size() < 10) { 917 size_t Size = NameBackReferences.size(); 918 NameBackReferences[Name] = Size; 919 } 920 } else { 921 Out << Found->second; 922 } 923 } 924 925 void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { 926 Context.mangleObjCMethodName(MD, Out); 927 } 928 929 // Find out how many function decls live above this one and return an integer 930 // suitable for use as the number in a numbered anonymous scope. 931 // TODO: Memoize. 932 static unsigned getLocalNestingLevel(const FunctionDecl *FD) { 933 const DeclContext *DC = FD->getParent(); 934 int level = 1; 935 936 while (DC && !DC->isTranslationUnit()) { 937 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++; 938 DC = DC->getParent(); 939 } 940 941 return 2*level; 942 } 943 944 void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) { 945 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name> 946 // <numbered-anonymous-scope> ::= ? <number> 947 // Even though the name is rendered in reverse order (e.g. 948 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to 949 // innermost. So a method bar in class C local to function foo gets mangled 950 // as something like: 951 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ 952 // This is more apparent when you have a type nested inside a method of a 953 // type nested inside a function. A method baz in class D local to method 954 // bar of class C local to function foo gets mangled as: 955 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ 956 // This scheme is general enough to support GCC-style nested 957 // functions. You could have a method baz of class C inside a function bar 958 // inside a function foo, like so: 959 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ 960 unsigned NestLevel = getLocalNestingLevel(FD); 961 Out << '?'; 962 mangleNumber(NestLevel); 963 Out << '?'; 964 mangle(FD, "?"); 965 } 966 967 void MicrosoftCXXNameMangler::mangleTemplateInstantiationName( 968 const TemplateDecl *TD, 969 const TemplateArgumentList &TemplateArgs) { 970 // <template-name> ::= <unscoped-template-name> <template-args> 971 // ::= <substitution> 972 // Always start with the unqualified name. 973 974 // Templates have their own context for back references. 975 ArgBackRefMap OuterArgsContext; 976 BackRefMap OuterTemplateContext; 977 NameBackReferences.swap(OuterTemplateContext); 978 TypeBackReferences.swap(OuterArgsContext); 979 980 mangleUnscopedTemplateName(TD); 981 mangleTemplateArgs(TD, TemplateArgs); 982 983 // Restore the previous back reference contexts. 984 NameBackReferences.swap(OuterTemplateContext); 985 TypeBackReferences.swap(OuterArgsContext); 986 } 987 988 void 989 MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) { 990 // <unscoped-template-name> ::= ?$ <unqualified-name> 991 Out << "?$"; 992 mangleUnqualifiedName(TD); 993 } 994 995 void 996 MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value, 997 bool IsBoolean) { 998 // <integer-literal> ::= $0 <number> 999 Out << "$0"; 1000 // Make sure booleans are encoded as 0/1. 1001 if (IsBoolean && Value.getBoolValue()) 1002 mangleNumber(1); 1003 else 1004 mangleNumber(Value.getSExtValue()); 1005 } 1006 1007 void 1008 MicrosoftCXXNameMangler::mangleExpression(const Expr *E) { 1009 // See if this is a constant expression. 1010 llvm::APSInt Value; 1011 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) { 1012 mangleIntegerLiteral(Value, E->getType()->isBooleanType()); 1013 return; 1014 } 1015 1016 const CXXUuidofExpr *UE = 0; 1017 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { 1018 if (UO->getOpcode() == UO_AddrOf) 1019 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr()); 1020 } else 1021 UE = dyn_cast<CXXUuidofExpr>(E); 1022 1023 if (UE) { 1024 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from 1025 // const __s_GUID _GUID_{lower case UUID with underscores} 1026 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext()); 1027 std::string Name = "_GUID_" + Uuid.lower(); 1028 std::replace(Name.begin(), Name.end(), '-', '_'); 1029 1030 // If we had to peek through an address-of operator, treat this like we are 1031 // dealing with a pointer type. Otherwise, treat it like a const reference. 1032 // 1033 // N.B. This matches up with the handling of TemplateArgument::Declaration 1034 // in mangleTemplateArg 1035 if (UE == E) 1036 Out << "$E?"; 1037 else 1038 Out << "$1?"; 1039 Out << Name << "@@3U__s_GUID@@B"; 1040 return; 1041 } 1042 1043 // As bad as this diagnostic is, it's better than crashing. 1044 DiagnosticsEngine &Diags = Context.getDiags(); 1045 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1046 "cannot yet mangle expression type %0"); 1047 Diags.Report(E->getExprLoc(), DiagID) 1048 << E->getStmtClassName() << E->getSourceRange(); 1049 } 1050 1051 void 1052 MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD, 1053 const TemplateArgumentList &TemplateArgs) { 1054 // <template-args> ::= <template-arg>+ @ 1055 unsigned NumTemplateArgs = TemplateArgs.size(); 1056 for (unsigned i = 0; i < NumTemplateArgs; ++i) { 1057 const TemplateArgument &TA = TemplateArgs[i]; 1058 mangleTemplateArg(TD, TA); 1059 } 1060 Out << '@'; 1061 } 1062 1063 void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD, 1064 const TemplateArgument &TA) { 1065 // <template-arg> ::= <type> 1066 // ::= <integer-literal> 1067 // ::= <member-data-pointer> 1068 // ::= <member-function-pointer> 1069 // ::= $E? <name> <type-encoding> 1070 // ::= $1? <name> <type-encoding> 1071 // ::= $0A@ 1072 // ::= <template-args> 1073 1074 switch (TA.getKind()) { 1075 case TemplateArgument::Null: 1076 llvm_unreachable("Can't mangle null template arguments!"); 1077 case TemplateArgument::TemplateExpansion: 1078 llvm_unreachable("Can't mangle template expansion arguments!"); 1079 case TemplateArgument::Type: { 1080 QualType T = TA.getAsType(); 1081 mangleType(T, SourceRange(), QMM_Escape); 1082 break; 1083 } 1084 case TemplateArgument::Declaration: { 1085 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl()); 1086 if (isa<FieldDecl>(ND) || isa<IndirectFieldDecl>(ND)) { 1087 mangleMemberDataPointer( 1088 cast<CXXRecordDecl>(ND->getDeclContext())->getMostRecentDecl(), 1089 cast<ValueDecl>(ND)); 1090 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { 1091 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); 1092 if (MD && MD->isInstance()) 1093 mangleMemberFunctionPointer(MD->getParent()->getMostRecentDecl(), MD); 1094 else 1095 mangle(FD, "$1?"); 1096 } else { 1097 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?"); 1098 } 1099 break; 1100 } 1101 case TemplateArgument::Integral: 1102 mangleIntegerLiteral(TA.getAsIntegral(), 1103 TA.getIntegralType()->isBooleanType()); 1104 break; 1105 case TemplateArgument::NullPtr: { 1106 QualType T = TA.getNullPtrType(); 1107 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) { 1108 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 1109 if (MPT->isMemberFunctionPointerType()) 1110 mangleMemberFunctionPointer(RD, 0); 1111 else 1112 mangleMemberDataPointer(RD, 0); 1113 } else { 1114 Out << "$0A@"; 1115 } 1116 break; 1117 } 1118 case TemplateArgument::Expression: 1119 mangleExpression(TA.getAsExpr()); 1120 break; 1121 case TemplateArgument::Pack: 1122 // Unlike Itanium, there is no character code to indicate an argument pack. 1123 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end(); 1124 I != E; ++I) 1125 mangleTemplateArg(TD, *I); 1126 break; 1127 case TemplateArgument::Template: 1128 mangleType(cast<TagDecl>( 1129 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl())); 1130 break; 1131 } 1132 } 1133 1134 void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals, 1135 bool IsMember) { 1136 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers> 1137 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only); 1138 // 'I' means __restrict (32/64-bit). 1139 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict 1140 // keyword! 1141 // <base-cvr-qualifiers> ::= A # near 1142 // ::= B # near const 1143 // ::= C # near volatile 1144 // ::= D # near const volatile 1145 // ::= E # far (16-bit) 1146 // ::= F # far const (16-bit) 1147 // ::= G # far volatile (16-bit) 1148 // ::= H # far const volatile (16-bit) 1149 // ::= I # huge (16-bit) 1150 // ::= J # huge const (16-bit) 1151 // ::= K # huge volatile (16-bit) 1152 // ::= L # huge const volatile (16-bit) 1153 // ::= M <basis> # based 1154 // ::= N <basis> # based const 1155 // ::= O <basis> # based volatile 1156 // ::= P <basis> # based const volatile 1157 // ::= Q # near member 1158 // ::= R # near const member 1159 // ::= S # near volatile member 1160 // ::= T # near const volatile member 1161 // ::= U # far member (16-bit) 1162 // ::= V # far const member (16-bit) 1163 // ::= W # far volatile member (16-bit) 1164 // ::= X # far const volatile member (16-bit) 1165 // ::= Y # huge member (16-bit) 1166 // ::= Z # huge const member (16-bit) 1167 // ::= 0 # huge volatile member (16-bit) 1168 // ::= 1 # huge const volatile member (16-bit) 1169 // ::= 2 <basis> # based member 1170 // ::= 3 <basis> # based const member 1171 // ::= 4 <basis> # based volatile member 1172 // ::= 5 <basis> # based const volatile member 1173 // ::= 6 # near function (pointers only) 1174 // ::= 7 # far function (pointers only) 1175 // ::= 8 # near method (pointers only) 1176 // ::= 9 # far method (pointers only) 1177 // ::= _A <basis> # based function (pointers only) 1178 // ::= _B <basis> # based function (far?) (pointers only) 1179 // ::= _C <basis> # based method (pointers only) 1180 // ::= _D <basis> # based method (far?) (pointers only) 1181 // ::= _E # block (Clang) 1182 // <basis> ::= 0 # __based(void) 1183 // ::= 1 # __based(segment)? 1184 // ::= 2 <name> # __based(name) 1185 // ::= 3 # ? 1186 // ::= 4 # ? 1187 // ::= 5 # not really based 1188 bool HasConst = Quals.hasConst(), 1189 HasVolatile = Quals.hasVolatile(); 1190 1191 if (!IsMember) { 1192 if (HasConst && HasVolatile) { 1193 Out << 'D'; 1194 } else if (HasVolatile) { 1195 Out << 'C'; 1196 } else if (HasConst) { 1197 Out << 'B'; 1198 } else { 1199 Out << 'A'; 1200 } 1201 } else { 1202 if (HasConst && HasVolatile) { 1203 Out << 'T'; 1204 } else if (HasVolatile) { 1205 Out << 'S'; 1206 } else if (HasConst) { 1207 Out << 'R'; 1208 } else { 1209 Out << 'Q'; 1210 } 1211 } 1212 1213 // FIXME: For now, just drop all extension qualifiers on the floor. 1214 } 1215 1216 void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) { 1217 // <pointer-cvr-qualifiers> ::= P # no qualifiers 1218 // ::= Q # const 1219 // ::= R # volatile 1220 // ::= S # const volatile 1221 bool HasConst = Quals.hasConst(), 1222 HasVolatile = Quals.hasVolatile(); 1223 if (HasConst && HasVolatile) { 1224 Out << 'S'; 1225 } else if (HasVolatile) { 1226 Out << 'R'; 1227 } else if (HasConst) { 1228 Out << 'Q'; 1229 } else { 1230 Out << 'P'; 1231 } 1232 } 1233 1234 void MicrosoftCXXNameMangler::mangleArgumentType(QualType T, 1235 SourceRange Range) { 1236 // MSVC will backreference two canonically equivalent types that have slightly 1237 // different manglings when mangled alone. 1238 1239 // Decayed types do not match up with non-decayed versions of the same type. 1240 // 1241 // e.g. 1242 // void (*x)(void) will not form a backreference with void x(void) 1243 void *TypePtr; 1244 if (const DecayedType *DT = T->getAs<DecayedType>()) { 1245 TypePtr = DT->getOriginalType().getCanonicalType().getAsOpaquePtr(); 1246 // If the original parameter was textually written as an array, 1247 // instead treat the decayed parameter like it's const. 1248 // 1249 // e.g. 1250 // int [] -> int * const 1251 if (DT->getOriginalType()->isArrayType()) 1252 T = T.withConst(); 1253 } else 1254 TypePtr = T.getCanonicalType().getAsOpaquePtr(); 1255 1256 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr); 1257 1258 if (Found == TypeBackReferences.end()) { 1259 size_t OutSizeBefore = Out.GetNumBytesInBuffer(); 1260 1261 mangleType(T, Range, QMM_Drop); 1262 1263 // See if it's worth creating a back reference. 1264 // Only types longer than 1 character are considered 1265 // and only 10 back references slots are available: 1266 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1); 1267 if (LongerThanOneChar && TypeBackReferences.size() < 10) { 1268 size_t Size = TypeBackReferences.size(); 1269 TypeBackReferences[TypePtr] = Size; 1270 } 1271 } else { 1272 Out << Found->second; 1273 } 1274 } 1275 1276 void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range, 1277 QualifierMangleMode QMM) { 1278 // Don't use the canonical types. MSVC includes things like 'const' on 1279 // pointer arguments to function pointers that canonicalization strips away. 1280 T = T.getDesugaredType(getASTContext()); 1281 Qualifiers Quals = T.getLocalQualifiers(); 1282 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) { 1283 // If there were any Quals, getAsArrayType() pushed them onto the array 1284 // element type. 1285 if (QMM == QMM_Mangle) 1286 Out << 'A'; 1287 else if (QMM == QMM_Escape || QMM == QMM_Result) 1288 Out << "$$B"; 1289 mangleArrayType(AT); 1290 return; 1291 } 1292 1293 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() || 1294 T->isBlockPointerType(); 1295 1296 switch (QMM) { 1297 case QMM_Drop: 1298 break; 1299 case QMM_Mangle: 1300 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) { 1301 Out << '6'; 1302 mangleFunctionType(FT); 1303 return; 1304 } 1305 mangleQualifiers(Quals, false); 1306 break; 1307 case QMM_Escape: 1308 if (!IsPointer && Quals) { 1309 Out << "$$C"; 1310 mangleQualifiers(Quals, false); 1311 } 1312 break; 1313 case QMM_Result: 1314 if ((!IsPointer && Quals) || isa<TagType>(T)) { 1315 Out << '?'; 1316 mangleQualifiers(Quals, false); 1317 } 1318 break; 1319 } 1320 1321 // We have to mangle these now, while we still have enough information. 1322 if (IsPointer) 1323 manglePointerQualifiers(Quals); 1324 const Type *ty = T.getTypePtr(); 1325 1326 switch (ty->getTypeClass()) { 1327 #define ABSTRACT_TYPE(CLASS, PARENT) 1328 #define NON_CANONICAL_TYPE(CLASS, PARENT) \ 1329 case Type::CLASS: \ 1330 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ 1331 return; 1332 #define TYPE(CLASS, PARENT) \ 1333 case Type::CLASS: \ 1334 mangleType(cast<CLASS##Type>(ty), Range); \ 1335 break; 1336 #include "clang/AST/TypeNodes.def" 1337 #undef ABSTRACT_TYPE 1338 #undef NON_CANONICAL_TYPE 1339 #undef TYPE 1340 } 1341 } 1342 1343 void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T, 1344 SourceRange Range) { 1345 // <type> ::= <builtin-type> 1346 // <builtin-type> ::= X # void 1347 // ::= C # signed char 1348 // ::= D # char 1349 // ::= E # unsigned char 1350 // ::= F # short 1351 // ::= G # unsigned short (or wchar_t if it's not a builtin) 1352 // ::= H # int 1353 // ::= I # unsigned int 1354 // ::= J # long 1355 // ::= K # unsigned long 1356 // L # <none> 1357 // ::= M # float 1358 // ::= N # double 1359 // ::= O # long double (__float80 is mangled differently) 1360 // ::= _J # long long, __int64 1361 // ::= _K # unsigned long long, __int64 1362 // ::= _L # __int128 1363 // ::= _M # unsigned __int128 1364 // ::= _N # bool 1365 // _O # <array in parameter> 1366 // ::= _T # __float80 (Intel) 1367 // ::= _W # wchar_t 1368 // ::= _Z # __float80 (Digital Mars) 1369 switch (T->getKind()) { 1370 case BuiltinType::Void: Out << 'X'; break; 1371 case BuiltinType::SChar: Out << 'C'; break; 1372 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break; 1373 case BuiltinType::UChar: Out << 'E'; break; 1374 case BuiltinType::Short: Out << 'F'; break; 1375 case BuiltinType::UShort: Out << 'G'; break; 1376 case BuiltinType::Int: Out << 'H'; break; 1377 case BuiltinType::UInt: Out << 'I'; break; 1378 case BuiltinType::Long: Out << 'J'; break; 1379 case BuiltinType::ULong: Out << 'K'; break; 1380 case BuiltinType::Float: Out << 'M'; break; 1381 case BuiltinType::Double: Out << 'N'; break; 1382 // TODO: Determine size and mangle accordingly 1383 case BuiltinType::LongDouble: Out << 'O'; break; 1384 case BuiltinType::LongLong: Out << "_J"; break; 1385 case BuiltinType::ULongLong: Out << "_K"; break; 1386 case BuiltinType::Int128: Out << "_L"; break; 1387 case BuiltinType::UInt128: Out << "_M"; break; 1388 case BuiltinType::Bool: Out << "_N"; break; 1389 case BuiltinType::WChar_S: 1390 case BuiltinType::WChar_U: Out << "_W"; break; 1391 1392 #define BUILTIN_TYPE(Id, SingletonId) 1393 #define PLACEHOLDER_TYPE(Id, SingletonId) \ 1394 case BuiltinType::Id: 1395 #include "clang/AST/BuiltinTypes.def" 1396 case BuiltinType::Dependent: 1397 llvm_unreachable("placeholder types shouldn't get to name mangling"); 1398 1399 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break; 1400 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break; 1401 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break; 1402 1403 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break; 1404 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break; 1405 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break; 1406 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break; 1407 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break; 1408 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break; 1409 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break; 1410 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break; 1411 1412 case BuiltinType::NullPtr: Out << "$$T"; break; 1413 1414 case BuiltinType::Char16: 1415 case BuiltinType::Char32: 1416 case BuiltinType::Half: { 1417 DiagnosticsEngine &Diags = Context.getDiags(); 1418 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1419 "cannot mangle this built-in %0 type yet"); 1420 Diags.Report(Range.getBegin(), DiagID) 1421 << T->getName(Context.getASTContext().getPrintingPolicy()) 1422 << Range; 1423 break; 1424 } 1425 } 1426 } 1427 1428 // <type> ::= <function-type> 1429 void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T, 1430 SourceRange) { 1431 // Structors only appear in decls, so at this point we know it's not a 1432 // structor type. 1433 // FIXME: This may not be lambda-friendly. 1434 Out << "$$A6"; 1435 mangleFunctionType(T); 1436 } 1437 void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T, 1438 SourceRange) { 1439 llvm_unreachable("Can't mangle K&R function prototypes"); 1440 } 1441 1442 void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T, 1443 const FunctionDecl *D, 1444 bool ForceInstMethod) { 1445 // <function-type> ::= <this-cvr-qualifiers> <calling-convention> 1446 // <return-type> <argument-list> <throw-spec> 1447 const FunctionProtoType *Proto = cast<FunctionProtoType>(T); 1448 1449 SourceRange Range; 1450 if (D) Range = D->getSourceRange(); 1451 1452 bool IsStructor = false, IsInstMethod = ForceInstMethod; 1453 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(D)) { 1454 if (MD->isInstance()) 1455 IsInstMethod = true; 1456 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) 1457 IsStructor = true; 1458 } 1459 1460 // If this is a C++ instance method, mangle the CVR qualifiers for the 1461 // this pointer. 1462 if (IsInstMethod) { 1463 if (PointersAre64Bit) 1464 Out << 'E'; 1465 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false); 1466 } 1467 1468 mangleCallingConvention(T); 1469 1470 // <return-type> ::= <type> 1471 // ::= @ # structors (they have no declared return type) 1472 if (IsStructor) { 1473 if (isa<CXXDestructorDecl>(D) && D == Structor && 1474 StructorType == Dtor_Deleting) { 1475 // The scalar deleting destructor takes an extra int argument. 1476 // However, the FunctionType generated has 0 arguments. 1477 // FIXME: This is a temporary hack. 1478 // Maybe should fix the FunctionType creation instead? 1479 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z"); 1480 return; 1481 } 1482 Out << '@'; 1483 } else { 1484 QualType ResultType = Proto->getReturnType(); 1485 if (ResultType->isVoidType()) 1486 ResultType = ResultType.getUnqualifiedType(); 1487 mangleType(ResultType, Range, QMM_Result); 1488 } 1489 1490 // <argument-list> ::= X # void 1491 // ::= <type>+ @ 1492 // ::= <type>* Z # varargs 1493 if (Proto->getNumParams() == 0 && !Proto->isVariadic()) { 1494 Out << 'X'; 1495 } else { 1496 // Happens for function pointer type arguments for example. 1497 for (FunctionProtoType::param_type_iterator 1498 Arg = Proto->param_type_begin(), 1499 ArgEnd = Proto->param_type_end(); 1500 Arg != ArgEnd; ++Arg) 1501 mangleArgumentType(*Arg, Range); 1502 // <builtin-type> ::= Z # ellipsis 1503 if (Proto->isVariadic()) 1504 Out << 'Z'; 1505 else 1506 Out << '@'; 1507 } 1508 1509 mangleThrowSpecification(Proto); 1510 } 1511 1512 void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) { 1513 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this' 1514 // # pointer. in 64-bit mode *all* 1515 // # 'this' pointers are 64-bit. 1516 // ::= <global-function> 1517 // <member-function> ::= A # private: near 1518 // ::= B # private: far 1519 // ::= C # private: static near 1520 // ::= D # private: static far 1521 // ::= E # private: virtual near 1522 // ::= F # private: virtual far 1523 // ::= I # protected: near 1524 // ::= J # protected: far 1525 // ::= K # protected: static near 1526 // ::= L # protected: static far 1527 // ::= M # protected: virtual near 1528 // ::= N # protected: virtual far 1529 // ::= Q # public: near 1530 // ::= R # public: far 1531 // ::= S # public: static near 1532 // ::= T # public: static far 1533 // ::= U # public: virtual near 1534 // ::= V # public: virtual far 1535 // <global-function> ::= Y # global near 1536 // ::= Z # global far 1537 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { 1538 switch (MD->getAccess()) { 1539 case AS_none: 1540 llvm_unreachable("Unsupported access specifier"); 1541 case AS_private: 1542 if (MD->isStatic()) 1543 Out << 'C'; 1544 else if (MD->isVirtual()) 1545 Out << 'E'; 1546 else 1547 Out << 'A'; 1548 break; 1549 case AS_protected: 1550 if (MD->isStatic()) 1551 Out << 'K'; 1552 else if (MD->isVirtual()) 1553 Out << 'M'; 1554 else 1555 Out << 'I'; 1556 break; 1557 case AS_public: 1558 if (MD->isStatic()) 1559 Out << 'S'; 1560 else if (MD->isVirtual()) 1561 Out << 'U'; 1562 else 1563 Out << 'Q'; 1564 } 1565 } else 1566 Out << 'Y'; 1567 } 1568 void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T) { 1569 // <calling-convention> ::= A # __cdecl 1570 // ::= B # __export __cdecl 1571 // ::= C # __pascal 1572 // ::= D # __export __pascal 1573 // ::= E # __thiscall 1574 // ::= F # __export __thiscall 1575 // ::= G # __stdcall 1576 // ::= H # __export __stdcall 1577 // ::= I # __fastcall 1578 // ::= J # __export __fastcall 1579 // The 'export' calling conventions are from a bygone era 1580 // (*cough*Win16*cough*) when functions were declared for export with 1581 // that keyword. (It didn't actually export them, it just made them so 1582 // that they could be in a DLL and somebody from another module could call 1583 // them.) 1584 CallingConv CC = T->getCallConv(); 1585 switch (CC) { 1586 default: 1587 llvm_unreachable("Unsupported CC for mangling"); 1588 case CC_X86_64Win64: 1589 case CC_X86_64SysV: 1590 case CC_C: Out << 'A'; break; 1591 case CC_X86Pascal: Out << 'C'; break; 1592 case CC_X86ThisCall: Out << 'E'; break; 1593 case CC_X86StdCall: Out << 'G'; break; 1594 case CC_X86FastCall: Out << 'I'; break; 1595 } 1596 } 1597 void MicrosoftCXXNameMangler::mangleThrowSpecification( 1598 const FunctionProtoType *FT) { 1599 // <throw-spec> ::= Z # throw(...) (default) 1600 // ::= @ # throw() or __declspec/__attribute__((nothrow)) 1601 // ::= <type>+ 1602 // NOTE: Since the Microsoft compiler ignores throw specifications, they are 1603 // all actually mangled as 'Z'. (They're ignored because their associated 1604 // functionality isn't implemented, and probably never will be.) 1605 Out << 'Z'; 1606 } 1607 1608 void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T, 1609 SourceRange Range) { 1610 // Probably should be mangled as a template instantiation; need to see what 1611 // VC does first. 1612 DiagnosticsEngine &Diags = Context.getDiags(); 1613 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1614 "cannot mangle this unresolved dependent type yet"); 1615 Diags.Report(Range.getBegin(), DiagID) 1616 << Range; 1617 } 1618 1619 // <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type> 1620 // <union-type> ::= T <name> 1621 // <struct-type> ::= U <name> 1622 // <class-type> ::= V <name> 1623 // <enum-type> ::= W4 <name> 1624 void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) { 1625 mangleType(cast<TagType>(T)->getDecl()); 1626 } 1627 void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) { 1628 mangleType(cast<TagType>(T)->getDecl()); 1629 } 1630 void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) { 1631 switch (TD->getTagKind()) { 1632 case TTK_Union: 1633 Out << 'T'; 1634 break; 1635 case TTK_Struct: 1636 case TTK_Interface: 1637 Out << 'U'; 1638 break; 1639 case TTK_Class: 1640 Out << 'V'; 1641 break; 1642 case TTK_Enum: 1643 Out << "W4"; 1644 break; 1645 } 1646 mangleName(TD); 1647 } 1648 1649 // <type> ::= <array-type> 1650 // <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> 1651 // [Y <dimension-count> <dimension>+] 1652 // <element-type> # as global, E is never required 1653 // It's supposed to be the other way around, but for some strange reason, it 1654 // isn't. Today this behavior is retained for the sole purpose of backwards 1655 // compatibility. 1656 void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) { 1657 // This isn't a recursive mangling, so now we have to do it all in this 1658 // one call. 1659 manglePointerQualifiers(T->getElementType().getQualifiers()); 1660 mangleType(T->getElementType(), SourceRange()); 1661 } 1662 void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T, 1663 SourceRange) { 1664 llvm_unreachable("Should have been special cased"); 1665 } 1666 void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T, 1667 SourceRange) { 1668 llvm_unreachable("Should have been special cased"); 1669 } 1670 void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T, 1671 SourceRange) { 1672 llvm_unreachable("Should have been special cased"); 1673 } 1674 void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T, 1675 SourceRange) { 1676 llvm_unreachable("Should have been special cased"); 1677 } 1678 void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) { 1679 QualType ElementTy(T, 0); 1680 SmallVector<llvm::APInt, 3> Dimensions; 1681 for (;;) { 1682 if (const ConstantArrayType *CAT = 1683 getASTContext().getAsConstantArrayType(ElementTy)) { 1684 Dimensions.push_back(CAT->getSize()); 1685 ElementTy = CAT->getElementType(); 1686 } else if (ElementTy->isVariableArrayType()) { 1687 const VariableArrayType *VAT = 1688 getASTContext().getAsVariableArrayType(ElementTy); 1689 DiagnosticsEngine &Diags = Context.getDiags(); 1690 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1691 "cannot mangle this variable-length array yet"); 1692 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID) 1693 << VAT->getBracketsRange(); 1694 return; 1695 } else if (ElementTy->isDependentSizedArrayType()) { 1696 // The dependent expression has to be folded into a constant (TODO). 1697 const DependentSizedArrayType *DSAT = 1698 getASTContext().getAsDependentSizedArrayType(ElementTy); 1699 DiagnosticsEngine &Diags = Context.getDiags(); 1700 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1701 "cannot mangle this dependent-length array yet"); 1702 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID) 1703 << DSAT->getBracketsRange(); 1704 return; 1705 } else if (const IncompleteArrayType *IAT = 1706 getASTContext().getAsIncompleteArrayType(ElementTy)) { 1707 Dimensions.push_back(llvm::APInt(32, 0)); 1708 ElementTy = IAT->getElementType(); 1709 } 1710 else break; 1711 } 1712 Out << 'Y'; 1713 // <dimension-count> ::= <number> # number of extra dimensions 1714 mangleNumber(Dimensions.size()); 1715 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim) 1716 mangleNumber(Dimensions[Dim].getLimitedValue()); 1717 mangleType(ElementTy, SourceRange(), QMM_Escape); 1718 } 1719 1720 // <type> ::= <pointer-to-member-type> 1721 // <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> 1722 // <class name> <type> 1723 void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T, 1724 SourceRange Range) { 1725 QualType PointeeType = T->getPointeeType(); 1726 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) { 1727 Out << '8'; 1728 mangleName(T->getClass()->castAs<RecordType>()->getDecl()); 1729 mangleFunctionType(FPT, 0, true); 1730 } else { 1731 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType()) 1732 Out << 'E'; 1733 mangleQualifiers(PointeeType.getQualifiers(), true); 1734 mangleName(T->getClass()->castAs<RecordType>()->getDecl()); 1735 mangleType(PointeeType, Range, QMM_Drop); 1736 } 1737 } 1738 1739 void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T, 1740 SourceRange Range) { 1741 DiagnosticsEngine &Diags = Context.getDiags(); 1742 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1743 "cannot mangle this template type parameter type yet"); 1744 Diags.Report(Range.getBegin(), DiagID) 1745 << Range; 1746 } 1747 1748 void MicrosoftCXXNameMangler::mangleType( 1749 const SubstTemplateTypeParmPackType *T, 1750 SourceRange Range) { 1751 DiagnosticsEngine &Diags = Context.getDiags(); 1752 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1753 "cannot mangle this substituted parameter pack yet"); 1754 Diags.Report(Range.getBegin(), DiagID) 1755 << Range; 1756 } 1757 1758 // <type> ::= <pointer-type> 1759 // <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type> 1760 // # the E is required for 64-bit non-static pointers 1761 void MicrosoftCXXNameMangler::mangleType(const PointerType *T, 1762 SourceRange Range) { 1763 QualType PointeeTy = T->getPointeeType(); 1764 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType()) 1765 Out << 'E'; 1766 mangleType(PointeeTy, Range); 1767 } 1768 void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T, 1769 SourceRange Range) { 1770 // Object pointers never have qualifiers. 1771 Out << 'A'; 1772 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType()) 1773 Out << 'E'; 1774 mangleType(T->getPointeeType(), Range); 1775 } 1776 1777 // <type> ::= <reference-type> 1778 // <reference-type> ::= A E? <cvr-qualifiers> <type> 1779 // # the E is required for 64-bit non-static lvalue references 1780 void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T, 1781 SourceRange Range) { 1782 Out << 'A'; 1783 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType()) 1784 Out << 'E'; 1785 mangleType(T->getPointeeType(), Range); 1786 } 1787 1788 // <type> ::= <r-value-reference-type> 1789 // <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type> 1790 // # the E is required for 64-bit non-static rvalue references 1791 void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T, 1792 SourceRange Range) { 1793 Out << "$$Q"; 1794 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType()) 1795 Out << 'E'; 1796 mangleType(T->getPointeeType(), Range); 1797 } 1798 1799 void MicrosoftCXXNameMangler::mangleType(const ComplexType *T, 1800 SourceRange Range) { 1801 DiagnosticsEngine &Diags = Context.getDiags(); 1802 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1803 "cannot mangle this complex number type yet"); 1804 Diags.Report(Range.getBegin(), DiagID) 1805 << Range; 1806 } 1807 1808 void MicrosoftCXXNameMangler::mangleType(const VectorType *T, 1809 SourceRange Range) { 1810 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>(); 1811 assert(ET && "vectors with non-builtin elements are unsupported"); 1812 uint64_t Width = getASTContext().getTypeSize(T); 1813 // Pattern match exactly the typedefs in our intrinsic headers. Anything that 1814 // doesn't match the Intel types uses a custom mangling below. 1815 bool IntelVector = true; 1816 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) { 1817 Out << "T__m64"; 1818 } else if (Width == 128 || Width == 256) { 1819 if (ET->getKind() == BuiltinType::Float) 1820 Out << "T__m" << Width; 1821 else if (ET->getKind() == BuiltinType::LongLong) 1822 Out << "T__m" << Width << 'i'; 1823 else if (ET->getKind() == BuiltinType::Double) 1824 Out << "U__m" << Width << 'd'; 1825 else 1826 IntelVector = false; 1827 } else { 1828 IntelVector = false; 1829 } 1830 1831 if (!IntelVector) { 1832 // The MS ABI doesn't have a special mangling for vector types, so we define 1833 // our own mangling to handle uses of __vector_size__ on user-specified 1834 // types, and for extensions like __v4sf. 1835 Out << "T__clang_vec" << T->getNumElements() << '_'; 1836 mangleType(ET, Range); 1837 } 1838 1839 Out << "@@"; 1840 } 1841 1842 void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T, 1843 SourceRange Range) { 1844 DiagnosticsEngine &Diags = Context.getDiags(); 1845 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1846 "cannot mangle this extended vector type yet"); 1847 Diags.Report(Range.getBegin(), DiagID) 1848 << Range; 1849 } 1850 void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T, 1851 SourceRange Range) { 1852 DiagnosticsEngine &Diags = Context.getDiags(); 1853 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1854 "cannot mangle this dependent-sized extended vector type yet"); 1855 Diags.Report(Range.getBegin(), DiagID) 1856 << Range; 1857 } 1858 1859 void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T, 1860 SourceRange) { 1861 // ObjC interfaces have structs underlying them. 1862 Out << 'U'; 1863 mangleName(T->getDecl()); 1864 } 1865 1866 void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T, 1867 SourceRange Range) { 1868 // We don't allow overloading by different protocol qualification, 1869 // so mangling them isn't necessary. 1870 mangleType(T->getBaseType(), Range); 1871 } 1872 1873 void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T, 1874 SourceRange Range) { 1875 Out << "_E"; 1876 1877 QualType pointee = T->getPointeeType(); 1878 mangleFunctionType(pointee->castAs<FunctionProtoType>()); 1879 } 1880 1881 void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *, 1882 SourceRange) { 1883 llvm_unreachable("Cannot mangle injected class name type."); 1884 } 1885 1886 void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T, 1887 SourceRange Range) { 1888 DiagnosticsEngine &Diags = Context.getDiags(); 1889 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1890 "cannot mangle this template specialization type yet"); 1891 Diags.Report(Range.getBegin(), DiagID) 1892 << Range; 1893 } 1894 1895 void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T, 1896 SourceRange Range) { 1897 DiagnosticsEngine &Diags = Context.getDiags(); 1898 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1899 "cannot mangle this dependent name type yet"); 1900 Diags.Report(Range.getBegin(), DiagID) 1901 << Range; 1902 } 1903 1904 void MicrosoftCXXNameMangler::mangleType( 1905 const DependentTemplateSpecializationType *T, 1906 SourceRange Range) { 1907 DiagnosticsEngine &Diags = Context.getDiags(); 1908 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1909 "cannot mangle this dependent template specialization type yet"); 1910 Diags.Report(Range.getBegin(), DiagID) 1911 << Range; 1912 } 1913 1914 void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T, 1915 SourceRange Range) { 1916 DiagnosticsEngine &Diags = Context.getDiags(); 1917 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1918 "cannot mangle this pack expansion yet"); 1919 Diags.Report(Range.getBegin(), DiagID) 1920 << Range; 1921 } 1922 1923 void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T, 1924 SourceRange Range) { 1925 DiagnosticsEngine &Diags = Context.getDiags(); 1926 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1927 "cannot mangle this typeof(type) yet"); 1928 Diags.Report(Range.getBegin(), DiagID) 1929 << Range; 1930 } 1931 1932 void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T, 1933 SourceRange Range) { 1934 DiagnosticsEngine &Diags = Context.getDiags(); 1935 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1936 "cannot mangle this typeof(expression) yet"); 1937 Diags.Report(Range.getBegin(), DiagID) 1938 << Range; 1939 } 1940 1941 void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T, 1942 SourceRange Range) { 1943 DiagnosticsEngine &Diags = Context.getDiags(); 1944 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1945 "cannot mangle this decltype() yet"); 1946 Diags.Report(Range.getBegin(), DiagID) 1947 << Range; 1948 } 1949 1950 void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T, 1951 SourceRange Range) { 1952 DiagnosticsEngine &Diags = Context.getDiags(); 1953 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1954 "cannot mangle this unary transform type yet"); 1955 Diags.Report(Range.getBegin(), DiagID) 1956 << Range; 1957 } 1958 1959 void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) { 1960 assert(T->getDeducedType().isNull() && "expecting a dependent type!"); 1961 1962 DiagnosticsEngine &Diags = Context.getDiags(); 1963 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1964 "cannot mangle this 'auto' type yet"); 1965 Diags.Report(Range.getBegin(), DiagID) 1966 << Range; 1967 } 1968 1969 void MicrosoftCXXNameMangler::mangleType(const AtomicType *T, 1970 SourceRange Range) { 1971 DiagnosticsEngine &Diags = Context.getDiags(); 1972 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 1973 "cannot mangle this C11 atomic type yet"); 1974 Diags.Report(Range.getBegin(), DiagID) 1975 << Range; 1976 } 1977 1978 void MicrosoftMangleContextImpl::mangleCXXName(const NamedDecl *D, 1979 raw_ostream &Out) { 1980 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && 1981 "Invalid mangleName() call, argument is not a variable or function!"); 1982 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && 1983 "Invalid mangleName() call on 'structor decl!"); 1984 1985 PrettyStackTraceDecl CrashInfo(D, SourceLocation(), 1986 getASTContext().getSourceManager(), 1987 "Mangling declaration"); 1988 1989 MicrosoftCXXNameMangler Mangler(*this, Out); 1990 return Mangler.mangle(D); 1991 } 1992 1993 // <this-adjustment> ::= <no-adjustment> | <static-adjustment> | 1994 // <virtual-adjustment> 1995 // <no-adjustment> ::= A # private near 1996 // ::= B # private far 1997 // ::= I # protected near 1998 // ::= J # protected far 1999 // ::= Q # public near 2000 // ::= R # public far 2001 // <static-adjustment> ::= G <static-offset> # private near 2002 // ::= H <static-offset> # private far 2003 // ::= O <static-offset> # protected near 2004 // ::= P <static-offset> # protected far 2005 // ::= W <static-offset> # public near 2006 // ::= X <static-offset> # public far 2007 // <virtual-adjustment> ::= $0 <virtual-shift> <static-offset> # private near 2008 // ::= $1 <virtual-shift> <static-offset> # private far 2009 // ::= $2 <virtual-shift> <static-offset> # protected near 2010 // ::= $3 <virtual-shift> <static-offset> # protected far 2011 // ::= $4 <virtual-shift> <static-offset> # public near 2012 // ::= $5 <virtual-shift> <static-offset> # public far 2013 // <virtual-shift> ::= <vtordisp-shift> | <vtordispex-shift> 2014 // <vtordisp-shift> ::= <offset-to-vtordisp> 2015 // <vtordispex-shift> ::= <offset-to-vbptr> <vbase-offset-offset> 2016 // <offset-to-vtordisp> 2017 static void mangleThunkThisAdjustment(const CXXMethodDecl *MD, 2018 const ThisAdjustment &Adjustment, 2019 MicrosoftCXXNameMangler &Mangler, 2020 raw_ostream &Out) { 2021 if (!Adjustment.Virtual.isEmpty()) { 2022 Out << '$'; 2023 char AccessSpec; 2024 switch (MD->getAccess()) { 2025 case AS_none: 2026 llvm_unreachable("Unsupported access specifier"); 2027 case AS_private: 2028 AccessSpec = '0'; 2029 break; 2030 case AS_protected: 2031 AccessSpec = '2'; 2032 break; 2033 case AS_public: 2034 AccessSpec = '4'; 2035 } 2036 if (Adjustment.Virtual.Microsoft.VBPtrOffset) { 2037 Out << 'R' << AccessSpec; 2038 Mangler.mangleNumber( 2039 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBPtrOffset)); 2040 Mangler.mangleNumber( 2041 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBOffsetOffset)); 2042 Mangler.mangleNumber( 2043 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset)); 2044 Mangler.mangleNumber(static_cast<uint32_t>(Adjustment.NonVirtual)); 2045 } else { 2046 Out << AccessSpec; 2047 Mangler.mangleNumber( 2048 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset)); 2049 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual)); 2050 } 2051 } else if (Adjustment.NonVirtual != 0) { 2052 switch (MD->getAccess()) { 2053 case AS_none: 2054 llvm_unreachable("Unsupported access specifier"); 2055 case AS_private: 2056 Out << 'G'; 2057 break; 2058 case AS_protected: 2059 Out << 'O'; 2060 break; 2061 case AS_public: 2062 Out << 'W'; 2063 } 2064 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual)); 2065 } else { 2066 switch (MD->getAccess()) { 2067 case AS_none: 2068 llvm_unreachable("Unsupported access specifier"); 2069 case AS_private: 2070 Out << 'A'; 2071 break; 2072 case AS_protected: 2073 Out << 'I'; 2074 break; 2075 case AS_public: 2076 Out << 'Q'; 2077 } 2078 } 2079 } 2080 2081 void 2082 MicrosoftMangleContextImpl::mangleVirtualMemPtrThunk(const CXXMethodDecl *MD, 2083 raw_ostream &Out) { 2084 MicrosoftVTableContext *VTContext = 2085 cast<MicrosoftVTableContext>(getASTContext().getVTableContext()); 2086 const MicrosoftVTableContext::MethodVFTableLocation &ML = 2087 VTContext->getMethodVFTableLocation(GlobalDecl(MD)); 2088 2089 MicrosoftCXXNameMangler Mangler(*this, Out); 2090 Mangler.getStream() << "\01?"; 2091 Mangler.mangleVirtualMemPtrThunk(MD, ML); 2092 } 2093 2094 void MicrosoftMangleContextImpl::mangleThunk(const CXXMethodDecl *MD, 2095 const ThunkInfo &Thunk, 2096 raw_ostream &Out) { 2097 MicrosoftCXXNameMangler Mangler(*this, Out); 2098 Out << "\01?"; 2099 Mangler.mangleName(MD); 2100 mangleThunkThisAdjustment(MD, Thunk.This, Mangler, Out); 2101 if (!Thunk.Return.isEmpty()) 2102 assert(Thunk.Method != 0 && "Thunk info should hold the overridee decl"); 2103 2104 const CXXMethodDecl *DeclForFPT = Thunk.Method ? Thunk.Method : MD; 2105 Mangler.mangleFunctionType( 2106 DeclForFPT->getType()->castAs<FunctionProtoType>(), MD); 2107 } 2108 2109 void MicrosoftMangleContextImpl::mangleCXXDtorThunk( 2110 const CXXDestructorDecl *DD, CXXDtorType Type, 2111 const ThisAdjustment &Adjustment, raw_ostream &Out) { 2112 // FIXME: Actually, the dtor thunk should be emitted for vector deleting 2113 // dtors rather than scalar deleting dtors. Just use the vector deleting dtor 2114 // mangling manually until we support both deleting dtor types. 2115 assert(Type == Dtor_Deleting); 2116 MicrosoftCXXNameMangler Mangler(*this, Out, DD, Type); 2117 Out << "\01??_E"; 2118 Mangler.mangleName(DD->getParent()); 2119 mangleThunkThisAdjustment(DD, Adjustment, Mangler, Out); 2120 Mangler.mangleFunctionType(DD->getType()->castAs<FunctionProtoType>(), DD); 2121 } 2122 2123 void MicrosoftMangleContextImpl::mangleCXXVFTable( 2124 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath, 2125 raw_ostream &Out) { 2126 // <mangled-name> ::= ?_7 <class-name> <storage-class> 2127 // <cvr-qualifiers> [<name>] @ 2128 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class> 2129 // is always '6' for vftables. 2130 MicrosoftCXXNameMangler Mangler(*this, Out); 2131 Mangler.getStream() << "\01??_7"; 2132 Mangler.mangleName(Derived); 2133 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const. 2134 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(), 2135 E = BasePath.end(); 2136 I != E; ++I) { 2137 Mangler.mangleName(*I); 2138 } 2139 Mangler.getStream() << '@'; 2140 } 2141 2142 void MicrosoftMangleContextImpl::mangleCXXVBTable( 2143 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath, 2144 raw_ostream &Out) { 2145 // <mangled-name> ::= ?_8 <class-name> <storage-class> 2146 // <cvr-qualifiers> [<name>] @ 2147 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class> 2148 // is always '7' for vbtables. 2149 MicrosoftCXXNameMangler Mangler(*this, Out); 2150 Mangler.getStream() << "\01??_8"; 2151 Mangler.mangleName(Derived); 2152 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const. 2153 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(), 2154 E = BasePath.end(); 2155 I != E; ++I) { 2156 Mangler.mangleName(*I); 2157 } 2158 Mangler.getStream() << '@'; 2159 } 2160 2161 void MicrosoftMangleContextImpl::mangleCXXRTTI(QualType T, raw_ostream &) { 2162 // FIXME: Give a location... 2163 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, 2164 "cannot mangle RTTI descriptors for type %0 yet"); 2165 getDiags().Report(DiagID) 2166 << T.getBaseTypeIdentifier(); 2167 } 2168 2169 void MicrosoftMangleContextImpl::mangleCXXRTTIName(QualType T, raw_ostream &) { 2170 // FIXME: Give a location... 2171 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, 2172 "cannot mangle the name of type %0 into RTTI descriptors yet"); 2173 getDiags().Report(DiagID) 2174 << T.getBaseTypeIdentifier(); 2175 } 2176 2177 void MicrosoftMangleContextImpl::mangleTypeName(QualType T, raw_ostream &Out) { 2178 // This is just a made up unique string for the purposes of tbaa. undname 2179 // does *not* know how to demangle it. 2180 MicrosoftCXXNameMangler Mangler(*this, Out); 2181 Mangler.getStream() << '?'; 2182 Mangler.mangleType(T, SourceRange()); 2183 } 2184 2185 void MicrosoftMangleContextImpl::mangleCXXCtor(const CXXConstructorDecl *D, 2186 CXXCtorType Type, 2187 raw_ostream &Out) { 2188 MicrosoftCXXNameMangler mangler(*this, Out); 2189 mangler.mangle(D); 2190 } 2191 2192 void MicrosoftMangleContextImpl::mangleCXXDtor(const CXXDestructorDecl *D, 2193 CXXDtorType Type, 2194 raw_ostream &Out) { 2195 MicrosoftCXXNameMangler mangler(*this, Out, D, Type); 2196 mangler.mangle(D); 2197 } 2198 2199 void MicrosoftMangleContextImpl::mangleReferenceTemporary(const VarDecl *VD, 2200 raw_ostream &) { 2201 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, 2202 "cannot mangle this reference temporary yet"); 2203 getDiags().Report(VD->getLocation(), DiagID); 2204 } 2205 2206 void MicrosoftMangleContextImpl::mangleStaticGuardVariable(const VarDecl *VD, 2207 raw_ostream &Out) { 2208 // TODO: This is not correct, especially with respect to MSVC2013. MSVC2013 2209 // utilizes thread local variables to implement thread safe, re-entrant 2210 // initialization for statics. They no longer differentiate between an 2211 // externally visible and non-externally visible static with respect to 2212 // mangling, they all get $TSS <number>. 2213 // 2214 // N.B. This means that they can get more than 32 static variable guards in a 2215 // scope. It also means that they broke compatibility with their own ABI. 2216 2217 // <guard-name> ::= ?_B <postfix> @51 2218 // ::= ?$S <guard-num> @ <postfix> @4IA 2219 2220 // The first mangling is what MSVC uses to guard static locals in inline 2221 // functions. It uses a different mangling in external functions to support 2222 // guarding more than 32 variables. MSVC rejects inline functions with more 2223 // than 32 static locals. We don't fully implement the second mangling 2224 // because those guards are not externally visible, and instead use LLVM's 2225 // default renaming when creating a new guard variable. 2226 MicrosoftCXXNameMangler Mangler(*this, Out); 2227 2228 bool Visible = VD->isExternallyVisible(); 2229 // <operator-name> ::= ?_B # local static guard 2230 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@"); 2231 Mangler.manglePostfix(VD->getDeclContext()); 2232 Mangler.getStream() << (Visible ? "@51" : "@4IA"); 2233 } 2234 2235 void MicrosoftMangleContextImpl::mangleInitFiniStub(const VarDecl *D, 2236 raw_ostream &Out, 2237 char CharCode) { 2238 MicrosoftCXXNameMangler Mangler(*this, Out); 2239 Mangler.getStream() << "\01??__" << CharCode; 2240 Mangler.mangleName(D); 2241 // This is the function class mangling. These stubs are global, non-variadic, 2242 // cdecl functions that return void and take no args. 2243 Mangler.getStream() << "YAXXZ"; 2244 } 2245 2246 void MicrosoftMangleContextImpl::mangleDynamicInitializer(const VarDecl *D, 2247 raw_ostream &Out) { 2248 // <initializer-name> ::= ?__E <name> YAXXZ 2249 mangleInitFiniStub(D, Out, 'E'); 2250 } 2251 2252 void 2253 MicrosoftMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D, 2254 raw_ostream &Out) { 2255 // <destructor-name> ::= ?__F <name> YAXXZ 2256 mangleInitFiniStub(D, Out, 'F'); 2257 } 2258 2259 MicrosoftMangleContext * 2260 MicrosoftMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags) { 2261 return new MicrosoftMangleContextImpl(Context, Diags); 2262 } 2263