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