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