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