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