MicrosoftCXXABI.cpp revision b021f8bd445959e2a5c958624a464ff621253ae4
1//===--- MicrosoftCXXABI.cpp - Emit LLVM Code from ASTs for a Module ------===// 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++ code generation targetting the Microsoft Visual C++ ABI. 11// The class in this file generates structures that follow the Microsoft 12// Visual C++ ABI, which is actually not very well documented at all outside 13// of Microsoft. 14// 15//===----------------------------------------------------------------------===// 16 17#include "CGCXXABI.h" 18#include "CodeGenModule.h" 19#include "Mangle.h" 20#include "clang/AST/ASTContext.h" 21#include "clang/AST/Decl.h" 22#include "clang/AST/DeclCXX.h" 23#include "clang/AST/DeclTemplate.h" 24#include "clang/AST/ExprCXX.h" 25#include "CGVTables.h" 26 27using namespace clang; 28using namespace CodeGen; 29 30namespace { 31 32/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the 33/// Microsoft Visual C++ ABI. 34class MicrosoftCXXNameMangler { 35 MangleContext &Context; 36 llvm::raw_svector_ostream Out; 37 38 ASTContext &getASTContext() const { return Context.getASTContext(); } 39 40public: 41 MicrosoftCXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res) 42 : Context(C), Out(Res) { } 43 44 llvm::raw_svector_ostream &getStream() { return Out; } 45 46 void mangle(const NamedDecl *D, llvm::StringRef Prefix = "?"); 47 void mangleName(const NamedDecl *ND); 48 void mangleVariableEncoding(const VarDecl *VD); 49 void mangleType(QualType T); 50 51private: 52 void mangleUnqualifiedName(const NamedDecl *ND) { 53 mangleUnqualifiedName(ND, ND->getDeclName()); 54 } 55 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name); 56 void mangleSourceName(const IdentifierInfo *II); 57 void manglePostfix(const DeclContext *DC, bool NoFunction=false); 58 void mangleQualifiers(Qualifiers Quals, bool IsMember); 59 60 void mangleObjCMethodName(const ObjCMethodDecl *MD); 61 62 // Declare manglers for every type class. 63#define ABSTRACT_TYPE(CLASS, PARENT) 64#define NON_CANONICAL_TYPE(CLASS, PARENT) 65#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T); 66#include "clang/AST/TypeNodes.def" 67 68}; 69 70/// MicrosoftMangleContext - Overrides the default MangleContext for the 71/// Microsoft Visual C++ ABI. 72class MicrosoftMangleContext : public MangleContext { 73public: 74 MicrosoftMangleContext(ASTContext &Context, 75 Diagnostic &Diags) : MangleContext(Context, Diags) { } 76 virtual bool shouldMangleDeclName(const NamedDecl *D); 77 virtual void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &); 78 virtual void mangleThunk(const CXXMethodDecl *MD, 79 const ThunkInfo &Thunk, 80 llvm::SmallVectorImpl<char> &); 81 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, 82 const ThisAdjustment &ThisAdjustment, 83 llvm::SmallVectorImpl<char> &); 84 virtual void mangleGuardVariable(const VarDecl *D, 85 llvm::SmallVectorImpl<char> &); 86 virtual void mangleCXXVTable(const CXXRecordDecl *RD, 87 llvm::SmallVectorImpl<char> &); 88 virtual void mangleCXXVTT(const CXXRecordDecl *RD, 89 llvm::SmallVectorImpl<char> &); 90 virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, 91 const CXXRecordDecl *Type, 92 llvm::SmallVectorImpl<char> &); 93 virtual void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &); 94 virtual void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &); 95 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, 96 llvm::SmallVectorImpl<char> &); 97 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, 98 llvm::SmallVectorImpl<char> &); 99}; 100 101class MicrosoftCXXABI : public CXXABI { 102 MicrosoftMangleContext MangleCtx; 103public: 104 MicrosoftCXXABI(CodeGenModule &CGM) 105 : MangleCtx(CGM.getContext(), CGM.getDiags()) {} 106 107 MicrosoftMangleContext &getMangleContext() { 108 return MangleCtx; 109 } 110}; 111 112} 113 114static bool isInCLinkageSpecification(const Decl *D) { 115 D = D->getCanonicalDecl(); 116 for (const DeclContext *DC = D->getDeclContext(); 117 !DC->isTranslationUnit(); DC = DC->getParent()) { 118 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) 119 return Linkage->getLanguage() == LinkageSpecDecl::lang_c; 120 } 121 122 return false; 123} 124 125bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) { 126 // In C, functions with no attributes never need to be mangled. Fastpath them. 127 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs()) 128 return false; 129 130 // Any decl can be declared with __asm("foo") on it, and this takes precedence 131 // over all other naming in the .o file. 132 if (D->hasAttr<AsmLabelAttr>()) 133 return true; 134 135 // Clang's "overloadable" attribute extension to C/C++ implies name mangling 136 // (always) as does passing a C++ member function and a function 137 // whose name is not a simple identifier. 138 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 139 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) || 140 !FD->getDeclName().isIdentifier())) 141 return true; 142 143 // Otherwise, no mangling is done outside C++ mode. 144 if (!getASTContext().getLangOptions().CPlusPlus) 145 return false; 146 147 // Variables at global scope with internal linkage are not mangled. 148 if (!FD) { 149 const DeclContext *DC = D->getDeclContext(); 150 if (DC->isTranslationUnit() && D->getLinkage() == InternalLinkage) 151 return false; 152 } 153 154 // C functions and "main" are not mangled. 155 if ((FD && FD->isMain()) || isInCLinkageSpecification(D)) 156 return false; 157 158 return true; 159} 160 161void MicrosoftCXXNameMangler::mangle(const NamedDecl *D, 162 llvm::StringRef Prefix) { 163 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names. 164 // Therefore it's really important that we don't decorate the 165 // name with leading underscores or leading/trailing at signs. So, emit a 166 // asm marker at the start so we get the name right. 167 Out << '\01'; // LLVM IR Marker for __asm("foo") 168 169 // Any decl can be declared with __asm("foo") on it, and this takes precedence 170 // over all other naming in the .o file. 171 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { 172 // If we have an asm name, then we use it as the mangling. 173 Out << ALA->getLabel(); 174 return; 175 } 176 177 // <mangled-name> ::= ? <name> <type> 178 Out << Prefix; 179 mangleName(D); 180 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 181 mangleVariableEncoding(VD); 182 // TODO: Function types. 183} 184 185void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) { 186 // <encoding> ::= <variable name> <storage-class> <variable-type> 187 // <storage-class> ::= 0 # private static member 188 // ::= 1 # protected static member 189 // ::= 2 # public static member 190 // ::= 3 # global 191 // ::= 4 # static local 192 193 // The first character in the encoding (after the name) is the storage class. 194 if (VD->isStaticDataMember()) { 195 // If it's a static member, it also encodes the access level. 196 switch (VD->getAccess()) { 197 default: 198 case AS_private: Out << '0'; break; 199 case AS_protected: Out << '1'; break; 200 case AS_public: Out << '2'; break; 201 } 202 } 203 else if (!VD->isStaticLocal()) 204 Out << '3'; 205 else 206 Out << '4'; 207 // Now mangle the type. 208 // <variable-type> ::= <type> <cvr-qualifiers> 209 QualType Ty = VD->getType(); 210 mangleType(Ty.getLocalUnqualifiedType()); 211 mangleQualifiers(Ty.getLocalQualifiers(), false); 212} 213 214void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) { 215 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @ 216 const DeclContext *DC = ND->getDeclContext(); 217 218 // Always start with the unqualified name. 219 mangleUnqualifiedName(ND); 220 221 // If this is an extern variable declared locally, the relevant DeclContext 222 // is that of the containing namespace, or the translation unit. 223 if (isa<FunctionDecl>(DC) && ND->hasLinkage()) 224 while (!DC->isNamespace() && !DC->isTranslationUnit()) 225 DC = DC->getParent(); 226 227 manglePostfix(DC); 228 229 // Terminate the whole name with an '@'. 230 Out << '@'; 231} 232 233void 234MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, 235 DeclarationName Name) { 236 // <unqualified-name> ::= <operator-name> 237 // ::= <ctor-dtor-name> 238 // ::= <source-name> 239 switch (Name.getNameKind()) { 240 case DeclarationName::Identifier: { 241 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { 242 mangleSourceName(II); 243 break; 244 } 245 246 // Otherwise, an anonymous entity. We must have a declaration. 247 assert(ND && "mangling empty name without declaration"); 248 249 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { 250 if (NS->isAnonymousNamespace()) { 251 Out << "?A"; 252 break; 253 } 254 } 255 256 // We must have an anonymous struct. 257 const TagDecl *TD = cast<TagDecl>(ND); 258 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) { 259 assert(TD->getDeclContext() == D->getDeclContext() && 260 "Typedef should not be in another decl context!"); 261 assert(D->getDeclName().getAsIdentifierInfo() && 262 "Typedef was not named!"); 263 mangleSourceName(D->getDeclName().getAsIdentifierInfo()); 264 break; 265 } 266 267 // TODO: How does VC mangle anonymous structs? 268 assert(false && "Don't know how to mangle anonymous types yet!"); 269 break; 270 } 271 272 case DeclarationName::ObjCZeroArgSelector: 273 case DeclarationName::ObjCOneArgSelector: 274 case DeclarationName::ObjCMultiArgSelector: 275 assert(false && "Can't mangle Objective-C selector names here!"); 276 break; 277 278 case DeclarationName::CXXConstructorName: 279 assert(false && "Can't mangle constructors yet!"); 280 break; 281 282 case DeclarationName::CXXDestructorName: 283 assert(false && "Can't mangle destructors yet!"); 284 break; 285 286 case DeclarationName::CXXConversionFunctionName: 287 // <operator-name> ::= ?B # (cast) 288 // The target type is encoded as the return type. 289 Out << "?B"; 290 break; 291 292 case DeclarationName::CXXOperatorName: 293 assert(false && "Can't mangle operators yet!"); 294 295 case DeclarationName::CXXLiteralOperatorName: 296 // FIXME: Was this added in VS2010? Does MS even know how to mangle this? 297 assert(false && "Don't know how to mangle literal operators yet!"); 298 break; 299 300 case DeclarationName::CXXUsingDirective: 301 assert(false && "Can't mangle a using directive name!"); 302 break; 303 } 304} 305 306void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC, 307 bool NoFunction) { 308 // <postfix> ::= <unqualified-name> [<postfix>] 309 // ::= <template-postfix> <template-args> [<postfix>] 310 // ::= <template-param> 311 // ::= <substitution> [<postfix>] 312 313 if (!DC) return; 314 315 while (isa<LinkageSpecDecl>(DC)) 316 DC = DC->getParent(); 317 318 if (DC->isTranslationUnit()) 319 return; 320 321 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) { 322 llvm::SmallString<64> Name; 323 Context.mangleBlock(BD, Name); 324 Out << Name << '@'; 325 return manglePostfix(DC->getParent(), NoFunction); 326 } 327 328 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC))) 329 return; 330 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) 331 mangleObjCMethodName(Method); 332 else { 333 mangleUnqualifiedName(cast<NamedDecl>(DC)); 334 manglePostfix(DC->getParent(), NoFunction); 335 } 336} 337 338void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) { 339 // <source name> ::= <identifier> @ 340 Out << II->getName() << '@'; 341} 342 343void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { 344 llvm::SmallString<64> Buffer; 345 MiscNameMangler(Context, Buffer).mangleObjCMethodName(MD); 346 Out << Buffer; 347} 348 349void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals, 350 bool IsMember) { 351 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers> 352 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only); 353 // 'I' means __restrict (32/64-bit). 354 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict 355 // keyword! 356 // <base-cvr-qualifiers> ::= A # near 357 // ::= B # near const 358 // ::= C # near volatile 359 // ::= D # near const volatile 360 // ::= E # far (16-bit) 361 // ::= F # far const (16-bit) 362 // ::= G # far volatile (16-bit) 363 // ::= H # far const volatile (16-bit) 364 // ::= I # huge (16-bit) 365 // ::= J # huge const (16-bit) 366 // ::= K # huge volatile (16-bit) 367 // ::= L # huge const volatile (16-bit) 368 // ::= M <basis> # based 369 // ::= N <basis> # based const 370 // ::= O <basis> # based volatile 371 // ::= P <basis> # based const volatile 372 // ::= Q # near member 373 // ::= R # near const member 374 // ::= S # near volatile member 375 // ::= T # near const volatile member 376 // ::= U # far member (16-bit) 377 // ::= V # far const member (16-bit) 378 // ::= W # far volatile member (16-bit) 379 // ::= X # far const volatile member (16-bit) 380 // ::= Y # huge member (16-bit) 381 // ::= Z # huge const member (16-bit) 382 // ::= 0 # huge volatile member (16-bit) 383 // ::= 1 # huge const volatile member (16-bit) 384 // ::= 2 <basis> # based member 385 // ::= 3 <basis> # based const member 386 // ::= 4 <basis> # based volatile member 387 // ::= 5 <basis> # based const volatile member 388 // ::= 6 # near function (pointers only) 389 // ::= 7 # far function (pointers only) 390 // ::= 8 # near method (pointers only) 391 // ::= 9 # far method (pointers only) 392 // ::= _A <basis> # based function (pointers only) 393 // ::= _B <basis> # based function (far?) (pointers only) 394 // ::= _C <basis> # based method (pointers only) 395 // ::= _D <basis> # based method (far?) (pointers only) 396 // <basis> ::= 0 # __based(void) 397 // ::= 1 # __based(segment)? 398 // ::= 2 <name> # __based(name) 399 // ::= 3 # ? 400 // ::= 4 # ? 401 // ::= 5 # not really based 402 if (!IsMember) { 403 if (!Quals.hasVolatile()) { 404 if (!Quals.hasConst()) 405 Out << 'A'; 406 else 407 Out << 'B'; 408 } else { 409 if (!Quals.hasConst()) 410 Out << 'C'; 411 else 412 Out << 'D'; 413 } 414 } else { 415 if (!Quals.hasVolatile()) { 416 if (!Quals.hasConst()) 417 Out << 'Q'; 418 else 419 Out << 'R'; 420 } else { 421 if (!Quals.hasConst()) 422 Out << 'S'; 423 else 424 Out << 'T'; 425 } 426 } 427 428 // FIXME: For now, just drop all extension qualifiers on the floor. 429} 430 431void MicrosoftCXXNameMangler::mangleType(QualType T) { 432 // Only operate on the canonical type! 433 T = getASTContext().getCanonicalType(T); 434 435 switch (T->getTypeClass()) { 436#define ABSTRACT_TYPE(CLASS, PARENT) 437#define NON_CANONICAL_TYPE(CLASS, PARENT) \ 438case Type::CLASS: \ 439llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ 440return; 441#define TYPE(CLASS, PARENT) 442#include "clang/AST/TypeNodes.def" 443 case Type::Builtin: 444 mangleType(static_cast<BuiltinType *>(T.getTypePtr())); 445 break; 446 default: 447 assert(false && "Don't know how to mangle this type!"); 448 break; 449 } 450} 451 452void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T) { 453 // <type> ::= <builtin-type> 454 // <builtin-type> ::= X # void 455 // ::= C # signed char 456 // ::= D # char 457 // ::= E # unsigned char 458 // ::= F # short 459 // ::= G # unsigned short (or wchar_t if it's not a builtin) 460 // ::= H # int 461 // ::= I # unsigned int 462 // ::= J # long 463 // ::= K # unsigned long 464 // L # <none> 465 // ::= M # float 466 // ::= N # double 467 // ::= O # long double (__float80 is mangled differently) 468 // ::= _D # __int8 (yup, it's a distinct type in MSVC) 469 // ::= _E # unsigned __int8 470 // ::= _F # __int16 471 // ::= _G # unsigned __int16 472 // ::= _H # __int32 473 // ::= _I # unsigned __int32 474 // ::= _J # long long, __int64 475 // ::= _K # unsigned long long, __int64 476 // ::= _L # __int128 477 // ::= _M # unsigned __int128 478 // ::= _N # bool 479 // _O # <array in parameter> 480 // ::= _T # __float80 (Intel) 481 // ::= _W # wchar_t 482 // ::= _Z # __float80 (Digital Mars) 483 switch (T->getKind()) { 484 case BuiltinType::Void: Out << 'X'; break; 485 case BuiltinType::SChar: Out << 'C'; break; 486 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break; 487 case BuiltinType::UChar: Out << 'E'; break; 488 case BuiltinType::Short: Out << 'F'; break; 489 case BuiltinType::UShort: Out << 'G'; break; 490 case BuiltinType::Int: Out << 'H'; break; 491 case BuiltinType::UInt: Out << 'I'; break; 492 case BuiltinType::Long: Out << 'J'; break; 493 case BuiltinType::ULong: Out << 'K'; break; 494 case BuiltinType::Float: Out << 'M'; break; 495 case BuiltinType::Double: Out << 'N'; break; 496 // TODO: Determine size and mangle accordingly 497 case BuiltinType::LongDouble: Out << 'O'; break; 498 // TODO: __int8 and friends 499 case BuiltinType::LongLong: Out << "_J"; break; 500 case BuiltinType::ULongLong: Out << "_K"; break; 501 case BuiltinType::Int128: Out << "_L"; break; 502 case BuiltinType::UInt128: Out << "_M"; break; 503 case BuiltinType::Bool: Out << "_N"; break; 504 case BuiltinType::WChar: Out << "_W"; break; 505 506 case BuiltinType::Overload: 507 case BuiltinType::Dependent: 508 assert(false && 509 "Overloaded and dependent types shouldn't get to name mangling"); 510 break; 511 case BuiltinType::UndeducedAuto: 512 assert(0 && "Should not see undeduced auto here"); 513 break; 514 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break; 515 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break; 516 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break; 517 518 case BuiltinType::Char16: 519 case BuiltinType::Char32: 520 case BuiltinType::NullPtr: 521 assert(false && "Don't know how to mangle this type"); 522 break; 523 } 524} 525 526void MicrosoftMangleContext::mangleName(const NamedDecl *D, 527 llvm::SmallVectorImpl<char> &Name) { 528 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && 529 "Invalid mangleName() call, argument is not a variable or function!"); 530 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && 531 "Invalid mangleName() call on 'structor decl!"); 532 533 PrettyStackTraceDecl CrashInfo(D, SourceLocation(), 534 getASTContext().getSourceManager(), 535 "Mangling declaration"); 536 537 MicrosoftCXXNameMangler Mangler(*this, Name); 538 return Mangler.mangle(D); 539} 540void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD, 541 const ThunkInfo &Thunk, 542 llvm::SmallVectorImpl<char> &) { 543 assert(false && "Can't yet mangle thunks!"); 544} 545void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, 546 CXXDtorType Type, 547 const ThisAdjustment &, 548 llvm::SmallVectorImpl<char> &) { 549 assert(false && "Can't yet mangle destructor thunks!"); 550} 551void MicrosoftMangleContext::mangleGuardVariable(const VarDecl *D, 552 llvm::SmallVectorImpl<char> &) { 553 assert(false && "Can't yet mangle guard variables!"); 554} 555void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD, 556 llvm::SmallVectorImpl<char> &) { 557 assert(false && "Can't yet mangle virtual tables!"); 558} 559void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD, 560 llvm::SmallVectorImpl<char> &) { 561 llvm_unreachable("The MS C++ ABI does not have virtual table tables!"); 562} 563void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, 564 int64_t Offset, 565 const CXXRecordDecl *Type, 566 llvm::SmallVectorImpl<char> &) { 567 llvm_unreachable("The MS C++ ABI does not have constructor vtables!"); 568} 569void MicrosoftMangleContext::mangleCXXRTTI(QualType T, 570 llvm::SmallVectorImpl<char> &) { 571 assert(false && "Can't yet mangle RTTI!"); 572} 573void MicrosoftMangleContext::mangleCXXRTTIName(QualType T, 574 llvm::SmallVectorImpl<char> &) { 575 assert(false && "Can't yet mangle RTTI names!"); 576} 577void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D, 578 CXXCtorType Type, 579 llvm::SmallVectorImpl<char> &) { 580 assert(false && "Can't yet mangle constructors!"); 581} 582void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D, 583 CXXDtorType Type, 584 llvm::SmallVectorImpl<char> &) { 585 assert(false && "Can't yet mangle destructors!"); 586} 587 588CXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) { 589 return new MicrosoftCXXABI(CGM); 590} 591 592