clang API Documentation
00001 //===--- MicrosoftMangle.cpp - Microsoft Visual C++ Name Mangling ---------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This provides C++ name mangling targeting the Microsoft Visual C++ ABI. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/AST/Mangle.h" 00015 #include "clang/AST/ASTContext.h" 00016 #include "clang/AST/Attr.h" 00017 #include "clang/AST/CXXInheritance.h" 00018 #include "clang/AST/CharUnits.h" 00019 #include "clang/AST/Decl.h" 00020 #include "clang/AST/DeclCXX.h" 00021 #include "clang/AST/DeclObjC.h" 00022 #include "clang/AST/DeclTemplate.h" 00023 #include "clang/AST/Expr.h" 00024 #include "clang/AST/ExprCXX.h" 00025 #include "clang/AST/VTableBuilder.h" 00026 #include "clang/Basic/ABI.h" 00027 #include "clang/Basic/DiagnosticOptions.h" 00028 #include "clang/Basic/TargetInfo.h" 00029 #include "llvm/ADT/StringExtras.h" 00030 #include "llvm/Support/MathExtras.h" 00031 00032 using namespace clang; 00033 00034 namespace { 00035 00036 /// \brief Retrieve the declaration context that should be used when mangling 00037 /// the given declaration. 00038 static const DeclContext *getEffectiveDeclContext(const Decl *D) { 00039 // The ABI assumes that lambda closure types that occur within 00040 // default arguments live in the context of the function. However, due to 00041 // the way in which Clang parses and creates function declarations, this is 00042 // not the case: the lambda closure type ends up living in the context 00043 // where the function itself resides, because the function declaration itself 00044 // had not yet been created. Fix the context here. 00045 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 00046 if (RD->isLambda()) 00047 if (ParmVarDecl *ContextParam = 00048 dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl())) 00049 return ContextParam->getDeclContext(); 00050 } 00051 00052 // Perform the same check for block literals. 00053 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) { 00054 if (ParmVarDecl *ContextParam = 00055 dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl())) 00056 return ContextParam->getDeclContext(); 00057 } 00058 00059 const DeclContext *DC = D->getDeclContext(); 00060 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(DC)) 00061 return getEffectiveDeclContext(CD); 00062 00063 return DC; 00064 } 00065 00066 static const DeclContext *getEffectiveParentContext(const DeclContext *DC) { 00067 return getEffectiveDeclContext(cast<Decl>(DC)); 00068 } 00069 00070 static const FunctionDecl *getStructor(const FunctionDecl *fn) { 00071 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate()) 00072 return ftd->getTemplatedDecl(); 00073 00074 return fn; 00075 } 00076 00077 static bool isLambda(const NamedDecl *ND) { 00078 const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(ND); 00079 if (!Record) 00080 return false; 00081 00082 return Record->isLambda(); 00083 } 00084 00085 /// MicrosoftMangleContextImpl - Overrides the default MangleContext for the 00086 /// Microsoft Visual C++ ABI. 00087 class MicrosoftMangleContextImpl : public MicrosoftMangleContext { 00088 typedef std::pair<const DeclContext *, IdentifierInfo *> DiscriminatorKeyTy; 00089 llvm::DenseMap<DiscriminatorKeyTy, unsigned> Discriminator; 00090 llvm::DenseMap<const NamedDecl *, unsigned> Uniquifier; 00091 llvm::DenseMap<const CXXRecordDecl *, unsigned> LambdaIds; 00092 00093 public: 00094 MicrosoftMangleContextImpl(ASTContext &Context, DiagnosticsEngine &Diags) 00095 : MicrosoftMangleContext(Context, Diags) {} 00096 bool shouldMangleCXXName(const NamedDecl *D) override; 00097 bool shouldMangleStringLiteral(const StringLiteral *SL) override; 00098 void mangleCXXName(const NamedDecl *D, raw_ostream &Out) override; 00099 void mangleVirtualMemPtrThunk(const CXXMethodDecl *MD, 00100 raw_ostream &) override; 00101 void mangleThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk, 00102 raw_ostream &) override; 00103 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, 00104 const ThisAdjustment &ThisAdjustment, 00105 raw_ostream &) override; 00106 void mangleCXXVFTable(const CXXRecordDecl *Derived, 00107 ArrayRef<const CXXRecordDecl *> BasePath, 00108 raw_ostream &Out) override; 00109 void mangleCXXVBTable(const CXXRecordDecl *Derived, 00110 ArrayRef<const CXXRecordDecl *> BasePath, 00111 raw_ostream &Out) override; 00112 void mangleCXXRTTI(QualType T, raw_ostream &Out) override; 00113 void mangleCXXRTTIName(QualType T, raw_ostream &Out) override; 00114 void mangleCXXRTTIBaseClassDescriptor(const CXXRecordDecl *Derived, 00115 uint32_t NVOffset, int32_t VBPtrOffset, 00116 uint32_t VBTableOffset, uint32_t Flags, 00117 raw_ostream &Out) override; 00118 void mangleCXXRTTIBaseClassArray(const CXXRecordDecl *Derived, 00119 raw_ostream &Out) override; 00120 void mangleCXXRTTIClassHierarchyDescriptor(const CXXRecordDecl *Derived, 00121 raw_ostream &Out) override; 00122 void 00123 mangleCXXRTTICompleteObjectLocator(const CXXRecordDecl *Derived, 00124 ArrayRef<const CXXRecordDecl *> BasePath, 00125 raw_ostream &Out) override; 00126 void mangleTypeName(QualType T, raw_ostream &) override; 00127 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, 00128 raw_ostream &) override; 00129 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, 00130 raw_ostream &) override; 00131 void mangleReferenceTemporary(const VarDecl *, unsigned ManglingNumber, 00132 raw_ostream &) override; 00133 void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out) override; 00134 void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out) override; 00135 void mangleDynamicAtExitDestructor(const VarDecl *D, 00136 raw_ostream &Out) override; 00137 void mangleStringLiteral(const StringLiteral *SL, raw_ostream &Out) override; 00138 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) { 00139 // Lambda closure types are already numbered. 00140 if (isLambda(ND)) 00141 return false; 00142 00143 const DeclContext *DC = getEffectiveDeclContext(ND); 00144 if (!DC->isFunctionOrMethod()) 00145 return false; 00146 00147 // Use the canonical number for externally visible decls. 00148 if (ND->isExternallyVisible()) { 00149 disc = getASTContext().getManglingNumber(ND); 00150 return true; 00151 } 00152 00153 // Anonymous tags are already numbered. 00154 if (const TagDecl *Tag = dyn_cast<TagDecl>(ND)) { 00155 if (Tag->getName().empty() && !Tag->getTypedefNameForAnonDecl()) 00156 return false; 00157 } 00158 00159 // Make up a reasonable number for internal decls. 00160 unsigned &discriminator = Uniquifier[ND]; 00161 if (!discriminator) 00162 discriminator = ++Discriminator[std::make_pair(DC, ND->getIdentifier())]; 00163 disc = discriminator + 1; 00164 return true; 00165 } 00166 00167 unsigned getLambdaId(const CXXRecordDecl *RD) { 00168 assert(RD->isLambda() && "RD must be a lambda!"); 00169 assert(!RD->isExternallyVisible() && "RD must not be visible!"); 00170 assert(RD->getLambdaManglingNumber() == 0 && 00171 "RD must not have a mangling number!"); 00172 std::pair<llvm::DenseMap<const CXXRecordDecl *, unsigned>::iterator, bool> 00173 Result = LambdaIds.insert(std::make_pair(RD, LambdaIds.size())); 00174 return Result.first->second; 00175 } 00176 00177 private: 00178 void mangleInitFiniStub(const VarDecl *D, raw_ostream &Out, char CharCode); 00179 }; 00180 00181 /// MicrosoftCXXNameMangler - Manage the mangling of a single name for the 00182 /// Microsoft Visual C++ ABI. 00183 class MicrosoftCXXNameMangler { 00184 MicrosoftMangleContextImpl &Context; 00185 raw_ostream &Out; 00186 00187 /// The "structor" is the top-level declaration being mangled, if 00188 /// that's not a template specialization; otherwise it's the pattern 00189 /// for that specialization. 00190 const NamedDecl *Structor; 00191 unsigned StructorType; 00192 00193 typedef llvm::SmallVector<std::string, 10> BackRefVec; 00194 BackRefVec NameBackReferences; 00195 00196 typedef llvm::DenseMap<void *, unsigned> ArgBackRefMap; 00197 ArgBackRefMap TypeBackReferences; 00198 00199 ASTContext &getASTContext() const { return Context.getASTContext(); } 00200 00201 // FIXME: If we add support for __ptr32/64 qualifiers, then we should push 00202 // this check into mangleQualifiers(). 00203 const bool PointersAre64Bit; 00204 00205 public: 00206 enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result }; 00207 00208 MicrosoftCXXNameMangler(MicrosoftMangleContextImpl &C, raw_ostream &Out_) 00209 : Context(C), Out(Out_), Structor(nullptr), StructorType(-1), 00210 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) == 00211 64) {} 00212 00213 MicrosoftCXXNameMangler(MicrosoftMangleContextImpl &C, raw_ostream &Out_, 00214 const CXXDestructorDecl *D, CXXDtorType Type) 00215 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type), 00216 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) == 00217 64) {} 00218 00219 raw_ostream &getStream() const { return Out; } 00220 00221 void mangle(const NamedDecl *D, StringRef Prefix = "\01?"); 00222 void mangleName(const NamedDecl *ND); 00223 void mangleFunctionEncoding(const FunctionDecl *FD); 00224 void mangleVariableEncoding(const VarDecl *VD); 00225 void mangleMemberDataPointer(const CXXRecordDecl *RD, const ValueDecl *VD); 00226 void mangleMemberFunctionPointer(const CXXRecordDecl *RD, 00227 const CXXMethodDecl *MD); 00228 void mangleVirtualMemPtrThunk( 00229 const CXXMethodDecl *MD, 00230 const MicrosoftVTableContext::MethodVFTableLocation &ML); 00231 void mangleNumber(int64_t Number); 00232 void mangleType(QualType T, SourceRange Range, 00233 QualifierMangleMode QMM = QMM_Mangle); 00234 void mangleFunctionType(const FunctionType *T, 00235 const FunctionDecl *D = nullptr, 00236 bool ForceThisQuals = false); 00237 void mangleNestedName(const NamedDecl *ND); 00238 00239 private: 00240 void mangleUnqualifiedName(const NamedDecl *ND) { 00241 mangleUnqualifiedName(ND, ND->getDeclName()); 00242 } 00243 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name); 00244 void mangleSourceName(StringRef Name); 00245 void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc); 00246 void mangleCXXDtorType(CXXDtorType T); 00247 void mangleQualifiers(Qualifiers Quals, bool IsMember); 00248 void mangleRefQualifier(RefQualifierKind RefQualifier); 00249 void manglePointerCVQualifiers(Qualifiers Quals); 00250 void manglePointerExtQualifiers(Qualifiers Quals, const Type *PointeeType); 00251 00252 void mangleUnscopedTemplateName(const TemplateDecl *ND); 00253 void 00254 mangleTemplateInstantiationName(const TemplateDecl *TD, 00255 const TemplateArgumentList &TemplateArgs); 00256 void mangleObjCMethodName(const ObjCMethodDecl *MD); 00257 00258 void mangleArgumentType(QualType T, SourceRange Range); 00259 00260 // Declare manglers for every type class. 00261 #define ABSTRACT_TYPE(CLASS, PARENT) 00262 #define NON_CANONICAL_TYPE(CLASS, PARENT) 00263 #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \ 00264 SourceRange Range); 00265 #include "clang/AST/TypeNodes.def" 00266 #undef ABSTRACT_TYPE 00267 #undef NON_CANONICAL_TYPE 00268 #undef TYPE 00269 00270 void mangleType(const TagDecl *TD); 00271 void mangleDecayedArrayType(const ArrayType *T); 00272 void mangleArrayType(const ArrayType *T); 00273 void mangleFunctionClass(const FunctionDecl *FD); 00274 void mangleCallingConvention(const FunctionType *T); 00275 void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean); 00276 void mangleExpression(const Expr *E); 00277 void mangleThrowSpecification(const FunctionProtoType *T); 00278 00279 void mangleTemplateArgs(const TemplateDecl *TD, 00280 const TemplateArgumentList &TemplateArgs); 00281 void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA, 00282 const NamedDecl *Parm); 00283 }; 00284 } 00285 00286 bool MicrosoftMangleContextImpl::shouldMangleCXXName(const NamedDecl *D) { 00287 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 00288 LanguageLinkage L = FD->getLanguageLinkage(); 00289 // Overloadable functions need mangling. 00290 if (FD->hasAttr<OverloadableAttr>()) 00291 return true; 00292 00293 // The ABI expects that we would never mangle "typical" user-defined entry 00294 // points regardless of visibility or freestanding-ness. 00295 // 00296 // N.B. This is distinct from asking about "main". "main" has a lot of 00297 // special rules associated with it in the standard while these 00298 // user-defined entry points are outside of the purview of the standard. 00299 // For example, there can be only one definition for "main" in a standards 00300 // compliant program; however nothing forbids the existence of wmain and 00301 // WinMain in the same translation unit. 00302 if (FD->isMSVCRTEntryPoint()) 00303 return false; 00304 00305 // C++ functions and those whose names are not a simple identifier need 00306 // mangling. 00307 if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage) 00308 return true; 00309 00310 // C functions are not mangled. 00311 if (L == CLanguageLinkage) 00312 return false; 00313 } 00314 00315 // Otherwise, no mangling is done outside C++ mode. 00316 if (!getASTContext().getLangOpts().CPlusPlus) 00317 return false; 00318 00319 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 00320 // C variables are not mangled. 00321 if (VD->isExternC()) 00322 return false; 00323 00324 // Variables at global scope with non-internal linkage are not mangled. 00325 const DeclContext *DC = getEffectiveDeclContext(D); 00326 // Check for extern variable declared locally. 00327 if (DC->isFunctionOrMethod() && D->hasLinkage()) 00328 while (!DC->isNamespace() && !DC->isTranslationUnit()) 00329 DC = getEffectiveParentContext(DC); 00330 00331 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage && 00332 !isa<VarTemplateSpecializationDecl>(D)) 00333 return false; 00334 } 00335 00336 return true; 00337 } 00338 00339 bool 00340 MicrosoftMangleContextImpl::shouldMangleStringLiteral(const StringLiteral *SL) { 00341 return SL->isAscii() || SL->isWide(); 00342 // TODO: This needs to be updated when MSVC gains support for Unicode 00343 // literals. 00344 } 00345 00346 void MicrosoftCXXNameMangler::mangle(const NamedDecl *D, StringRef Prefix) { 00347 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names. 00348 // Therefore it's really important that we don't decorate the 00349 // name with leading underscores or leading/trailing at signs. So, by 00350 // default, we emit an asm marker at the start so we get the name right. 00351 // Callers can override this with a custom prefix. 00352 00353 // <mangled-name> ::= ? <name> <type-encoding> 00354 Out << Prefix; 00355 mangleName(D); 00356 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 00357 mangleFunctionEncoding(FD); 00358 else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 00359 mangleVariableEncoding(VD); 00360 else { 00361 // TODO: Fields? Can MSVC even mangle them? 00362 // Issue a diagnostic for now. 00363 DiagnosticsEngine &Diags = Context.getDiags(); 00364 unsigned DiagID = Diags.getCustomDiagID( 00365 DiagnosticsEngine::Error, "cannot mangle this declaration yet"); 00366 Diags.Report(D->getLocation(), DiagID) << D->getSourceRange(); 00367 } 00368 } 00369 00370 void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { 00371 // <type-encoding> ::= <function-class> <function-type> 00372 00373 // Since MSVC operates on the type as written and not the canonical type, it 00374 // actually matters which decl we have here. MSVC appears to choose the 00375 // first, since it is most likely to be the declaration in a header file. 00376 FD = FD->getFirstDecl(); 00377 00378 // We should never ever see a FunctionNoProtoType at this point. 00379 // We don't even know how to mangle their types anyway :). 00380 const FunctionProtoType *FT = FD->getType()->castAs<FunctionProtoType>(); 00381 00382 // extern "C" functions can hold entities that must be mangled. 00383 // As it stands, these functions still need to get expressed in the full 00384 // external name. They have their class and type omitted, replaced with '9'. 00385 if (Context.shouldMangleDeclName(FD)) { 00386 // First, the function class. 00387 mangleFunctionClass(FD); 00388 00389 mangleFunctionType(FT, FD); 00390 } else 00391 Out << '9'; 00392 } 00393 00394 void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) { 00395 // <type-encoding> ::= <storage-class> <variable-type> 00396 // <storage-class> ::= 0 # private static member 00397 // ::= 1 # protected static member 00398 // ::= 2 # public static member 00399 // ::= 3 # global 00400 // ::= 4 # static local 00401 00402 // The first character in the encoding (after the name) is the storage class. 00403 if (VD->isStaticDataMember()) { 00404 // If it's a static member, it also encodes the access level. 00405 switch (VD->getAccess()) { 00406 default: 00407 case AS_private: Out << '0'; break; 00408 case AS_protected: Out << '1'; break; 00409 case AS_public: Out << '2'; break; 00410 } 00411 } 00412 else if (!VD->isStaticLocal()) 00413 Out << '3'; 00414 else 00415 Out << '4'; 00416 // Now mangle the type. 00417 // <variable-type> ::= <type> <cvr-qualifiers> 00418 // ::= <type> <pointee-cvr-qualifiers> # pointers, references 00419 // Pointers and references are odd. The type of 'int * const foo;' gets 00420 // mangled as 'QAHA' instead of 'PAHB', for example. 00421 SourceRange SR = VD->getSourceRange(); 00422 QualType Ty = VD->getType(); 00423 if (Ty->isPointerType() || Ty->isReferenceType() || 00424 Ty->isMemberPointerType()) { 00425 mangleType(Ty, SR, QMM_Drop); 00426 manglePointerExtQualifiers( 00427 Ty.getDesugaredType(getASTContext()).getLocalQualifiers(), nullptr); 00428 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) { 00429 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true); 00430 // Member pointers are suffixed with a back reference to the member 00431 // pointer's class name. 00432 mangleName(MPT->getClass()->getAsCXXRecordDecl()); 00433 } else 00434 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false); 00435 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) { 00436 // Global arrays are funny, too. 00437 mangleDecayedArrayType(AT); 00438 if (AT->getElementType()->isArrayType()) 00439 Out << 'A'; 00440 else 00441 mangleQualifiers(Ty.getQualifiers(), false); 00442 } else { 00443 mangleType(Ty, SR, QMM_Drop); 00444 mangleQualifiers(Ty.getQualifiers(), false); 00445 } 00446 } 00447 00448 void MicrosoftCXXNameMangler::mangleMemberDataPointer(const CXXRecordDecl *RD, 00449 const ValueDecl *VD) { 00450 // <member-data-pointer> ::= <integer-literal> 00451 // ::= $F <number> <number> 00452 // ::= $G <number> <number> <number> 00453 00454 int64_t FieldOffset; 00455 int64_t VBTableOffset; 00456 MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel(); 00457 if (VD) { 00458 FieldOffset = getASTContext().getFieldOffset(VD); 00459 assert(FieldOffset % getASTContext().getCharWidth() == 0 && 00460 "cannot take address of bitfield"); 00461 FieldOffset /= getASTContext().getCharWidth(); 00462 00463 VBTableOffset = 0; 00464 } else { 00465 FieldOffset = RD->nullFieldOffsetIsZero() ? 0 : -1; 00466 00467 VBTableOffset = -1; 00468 } 00469 00470 char Code = '\0'; 00471 switch (IM) { 00472 case MSInheritanceAttr::Keyword_single_inheritance: Code = '0'; break; 00473 case MSInheritanceAttr::Keyword_multiple_inheritance: Code = '0'; break; 00474 case MSInheritanceAttr::Keyword_virtual_inheritance: Code = 'F'; break; 00475 case MSInheritanceAttr::Keyword_unspecified_inheritance: Code = 'G'; break; 00476 } 00477 00478 Out << '$' << Code; 00479 00480 mangleNumber(FieldOffset); 00481 00482 // The C++ standard doesn't allow base-to-derived member pointer conversions 00483 // in template parameter contexts, so the vbptr offset of data member pointers 00484 // is always zero. 00485 if (MSInheritanceAttr::hasVBPtrOffsetField(IM)) 00486 mangleNumber(0); 00487 if (MSInheritanceAttr::hasVBTableOffsetField(IM)) 00488 mangleNumber(VBTableOffset); 00489 } 00490 00491 void 00492 MicrosoftCXXNameMangler::mangleMemberFunctionPointer(const CXXRecordDecl *RD, 00493 const CXXMethodDecl *MD) { 00494 // <member-function-pointer> ::= $1? <name> 00495 // ::= $H? <name> <number> 00496 // ::= $I? <name> <number> <number> 00497 // ::= $J? <name> <number> <number> <number> 00498 00499 MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel(); 00500 00501 char Code = '\0'; 00502 switch (IM) { 00503 case MSInheritanceAttr::Keyword_single_inheritance: Code = '1'; break; 00504 case MSInheritanceAttr::Keyword_multiple_inheritance: Code = 'H'; break; 00505 case MSInheritanceAttr::Keyword_virtual_inheritance: Code = 'I'; break; 00506 case MSInheritanceAttr::Keyword_unspecified_inheritance: Code = 'J'; break; 00507 } 00508 00509 // If non-virtual, mangle the name. If virtual, mangle as a virtual memptr 00510 // thunk. 00511 uint64_t NVOffset = 0; 00512 uint64_t VBTableOffset = 0; 00513 uint64_t VBPtrOffset = 0; 00514 if (MD) { 00515 Out << '$' << Code << '?'; 00516 if (MD->isVirtual()) { 00517 MicrosoftVTableContext *VTContext = 00518 cast<MicrosoftVTableContext>(getASTContext().getVTableContext()); 00519 const MicrosoftVTableContext::MethodVFTableLocation &ML = 00520 VTContext->getMethodVFTableLocation(GlobalDecl(MD)); 00521 mangleVirtualMemPtrThunk(MD, ML); 00522 NVOffset = ML.VFPtrOffset.getQuantity(); 00523 VBTableOffset = ML.VBTableIndex * 4; 00524 if (ML.VBase) { 00525 const ASTRecordLayout &Layout = getASTContext().getASTRecordLayout(RD); 00526 VBPtrOffset = Layout.getVBPtrOffset().getQuantity(); 00527 } 00528 } else { 00529 mangleName(MD); 00530 mangleFunctionEncoding(MD); 00531 } 00532 } else { 00533 // Null single inheritance member functions are encoded as a simple nullptr. 00534 if (IM == MSInheritanceAttr::Keyword_single_inheritance) { 00535 Out << "$0A@"; 00536 return; 00537 } 00538 if (IM == MSInheritanceAttr::Keyword_unspecified_inheritance) 00539 VBTableOffset = -1; 00540 Out << '$' << Code; 00541 } 00542 00543 if (MSInheritanceAttr::hasNVOffsetField(/*IsMemberFunction=*/true, IM)) 00544 mangleNumber(NVOffset); 00545 if (MSInheritanceAttr::hasVBPtrOffsetField(IM)) 00546 mangleNumber(VBPtrOffset); 00547 if (MSInheritanceAttr::hasVBTableOffsetField(IM)) 00548 mangleNumber(VBTableOffset); 00549 } 00550 00551 void MicrosoftCXXNameMangler::mangleVirtualMemPtrThunk( 00552 const CXXMethodDecl *MD, 00553 const MicrosoftVTableContext::MethodVFTableLocation &ML) { 00554 // Get the vftable offset. 00555 CharUnits PointerWidth = getASTContext().toCharUnitsFromBits( 00556 getASTContext().getTargetInfo().getPointerWidth(0)); 00557 uint64_t OffsetInVFTable = ML.Index * PointerWidth.getQuantity(); 00558 00559 Out << "?_9"; 00560 mangleName(MD->getParent()); 00561 Out << "$B"; 00562 mangleNumber(OffsetInVFTable); 00563 Out << 'A'; 00564 Out << (PointersAre64Bit ? 'A' : 'E'); 00565 } 00566 00567 void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) { 00568 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @ 00569 00570 // Always start with the unqualified name. 00571 mangleUnqualifiedName(ND); 00572 00573 mangleNestedName(ND); 00574 00575 // Terminate the whole name with an '@'. 00576 Out << '@'; 00577 } 00578 00579 void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) { 00580 // <non-negative integer> ::= A@ # when Number == 0 00581 // ::= <decimal digit> # when 1 <= Number <= 10 00582 // ::= <hex digit>+ @ # when Number >= 10 00583 // 00584 // <number> ::= [?] <non-negative integer> 00585 00586 uint64_t Value = static_cast<uint64_t>(Number); 00587 if (Number < 0) { 00588 Value = -Value; 00589 Out << '?'; 00590 } 00591 00592 if (Value == 0) 00593 Out << "A@"; 00594 else if (Value >= 1 && Value <= 10) 00595 Out << (Value - 1); 00596 else { 00597 // Numbers that are not encoded as decimal digits are represented as nibbles 00598 // in the range of ASCII characters 'A' to 'P'. 00599 // The number 0x123450 would be encoded as 'BCDEFA' 00600 char EncodedNumberBuffer[sizeof(uint64_t) * 2]; 00601 MutableArrayRef<char> BufferRef(EncodedNumberBuffer); 00602 MutableArrayRef<char>::reverse_iterator I = BufferRef.rbegin(); 00603 for (; Value != 0; Value >>= 4) 00604 *I++ = 'A' + (Value & 0xf); 00605 Out.write(I.base(), I - BufferRef.rbegin()); 00606 Out << '@'; 00607 } 00608 } 00609 00610 static const TemplateDecl * 00611 isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) { 00612 // Check if we have a function template. 00613 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { 00614 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) { 00615 TemplateArgs = FD->getTemplateSpecializationArgs(); 00616 return TD; 00617 } 00618 } 00619 00620 // Check if we have a class template. 00621 if (const ClassTemplateSpecializationDecl *Spec = 00622 dyn_cast<ClassTemplateSpecializationDecl>(ND)) { 00623 TemplateArgs = &Spec->getTemplateArgs(); 00624 return Spec->getSpecializedTemplate(); 00625 } 00626 00627 // Check if we have a variable template. 00628 if (const VarTemplateSpecializationDecl *Spec = 00629 dyn_cast<VarTemplateSpecializationDecl>(ND)) { 00630 TemplateArgs = &Spec->getTemplateArgs(); 00631 return Spec->getSpecializedTemplate(); 00632 } 00633 00634 return nullptr; 00635 } 00636 00637 void MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, 00638 DeclarationName Name) { 00639 // <unqualified-name> ::= <operator-name> 00640 // ::= <ctor-dtor-name> 00641 // ::= <source-name> 00642 // ::= <template-name> 00643 00644 // Check if we have a template. 00645 const TemplateArgumentList *TemplateArgs = nullptr; 00646 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { 00647 // Function templates aren't considered for name back referencing. This 00648 // makes sense since function templates aren't likely to occur multiple 00649 // times in a symbol. 00650 // FIXME: Test alias template mangling with MSVC 2013. 00651 if (!isa<ClassTemplateDecl>(TD)) { 00652 mangleTemplateInstantiationName(TD, *TemplateArgs); 00653 Out << '@'; 00654 return; 00655 } 00656 00657 // Here comes the tricky thing: if we need to mangle something like 00658 // void foo(A::X<Y>, B::X<Y>), 00659 // the X<Y> part is aliased. However, if you need to mangle 00660 // void foo(A::X<A::Y>, A::X<B::Y>), 00661 // the A::X<> part is not aliased. 00662 // That said, from the mangler's perspective we have a structure like this: 00663 // namespace[s] -> type[ -> template-parameters] 00664 // but from the Clang perspective we have 00665 // type [ -> template-parameters] 00666 // \-> namespace[s] 00667 // What we do is we create a new mangler, mangle the same type (without 00668 // a namespace suffix) to a string using the extra mangler and then use 00669 // the mangled type name as a key to check the mangling of different types 00670 // for aliasing. 00671 00672 llvm::SmallString<64> TemplateMangling; 00673 llvm::raw_svector_ostream Stream(TemplateMangling); 00674 MicrosoftCXXNameMangler Extra(Context, Stream); 00675 Extra.mangleTemplateInstantiationName(TD, *TemplateArgs); 00676 Stream.flush(); 00677 00678 mangleSourceName(TemplateMangling); 00679 return; 00680 } 00681 00682 switch (Name.getNameKind()) { 00683 case DeclarationName::Identifier: { 00684 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { 00685 mangleSourceName(II->getName()); 00686 break; 00687 } 00688 00689 // Otherwise, an anonymous entity. We must have a declaration. 00690 assert(ND && "mangling empty name without declaration"); 00691 00692 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { 00693 if (NS->isAnonymousNamespace()) { 00694 Out << "?A@"; 00695 break; 00696 } 00697 } 00698 00699 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) { 00700 // We must have an anonymous union or struct declaration. 00701 const CXXRecordDecl *RD = VD->getType()->getAsCXXRecordDecl(); 00702 assert(RD && "expected variable decl to have a record type"); 00703 // Anonymous types with no tag or typedef get the name of their 00704 // declarator mangled in. If they have no declarator, number them with 00705 // a $S prefix. 00706 llvm::SmallString<64> Name("$S"); 00707 // Get a unique id for the anonymous struct. 00708 Name += llvm::utostr(Context.getAnonymousStructId(RD) + 1); 00709 mangleSourceName(Name.str()); 00710 break; 00711 } 00712 00713 // We must have an anonymous struct. 00714 const TagDecl *TD = cast<TagDecl>(ND); 00715 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) { 00716 assert(TD->getDeclContext() == D->getDeclContext() && 00717 "Typedef should not be in another decl context!"); 00718 assert(D->getDeclName().getAsIdentifierInfo() && 00719 "Typedef was not named!"); 00720 mangleSourceName(D->getDeclName().getAsIdentifierInfo()->getName()); 00721 break; 00722 } 00723 00724 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) { 00725 if (Record->isLambda()) { 00726 llvm::SmallString<10> Name("<lambda_"); 00727 unsigned LambdaId; 00728 if (Record->getLambdaManglingNumber()) 00729 LambdaId = Record->getLambdaManglingNumber(); 00730 else 00731 LambdaId = Context.getLambdaId(Record); 00732 00733 Name += llvm::utostr(LambdaId); 00734 Name += ">"; 00735 00736 mangleSourceName(Name); 00737 break; 00738 } 00739 } 00740 00741 llvm::SmallString<64> Name("<unnamed-type-"); 00742 if (TD->hasDeclaratorForAnonDecl()) { 00743 // Anonymous types with no tag or typedef get the name of their 00744 // declarator mangled in if they have one. 00745 Name += TD->getDeclaratorForAnonDecl()->getName(); 00746 } else { 00747 // Otherwise, number the types using a $S prefix. 00748 Name += "$S"; 00749 Name += llvm::utostr(Context.getAnonymousStructId(TD)); 00750 } 00751 Name += ">"; 00752 mangleSourceName(Name.str()); 00753 break; 00754 } 00755 00756 case DeclarationName::ObjCZeroArgSelector: 00757 case DeclarationName::ObjCOneArgSelector: 00758 case DeclarationName::ObjCMultiArgSelector: 00759 llvm_unreachable("Can't mangle Objective-C selector names here!"); 00760 00761 case DeclarationName::CXXConstructorName: 00762 if (ND == Structor) { 00763 assert(StructorType == Ctor_Complete && 00764 "Should never be asked to mangle a ctor other than complete"); 00765 } 00766 Out << "?0"; 00767 break; 00768 00769 case DeclarationName::CXXDestructorName: 00770 if (ND == Structor) 00771 // If the named decl is the C++ destructor we're mangling, 00772 // use the type we were given. 00773 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType)); 00774 else 00775 // Otherwise, use the base destructor name. This is relevant if a 00776 // class with a destructor is declared within a destructor. 00777 mangleCXXDtorType(Dtor_Base); 00778 break; 00779 00780 case DeclarationName::CXXConversionFunctionName: 00781 // <operator-name> ::= ?B # (cast) 00782 // The target type is encoded as the return type. 00783 Out << "?B"; 00784 break; 00785 00786 case DeclarationName::CXXOperatorName: 00787 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation()); 00788 break; 00789 00790 case DeclarationName::CXXLiteralOperatorName: { 00791 Out << "?__K"; 00792 mangleSourceName(Name.getCXXLiteralIdentifier()->getName()); 00793 break; 00794 } 00795 00796 case DeclarationName::CXXUsingDirective: 00797 llvm_unreachable("Can't mangle a using directive name!"); 00798 } 00799 } 00800 00801 void MicrosoftCXXNameMangler::mangleNestedName(const NamedDecl *ND) { 00802 // <postfix> ::= <unqualified-name> [<postfix>] 00803 // ::= <substitution> [<postfix>] 00804 const DeclContext *DC = getEffectiveDeclContext(ND); 00805 00806 while (!DC->isTranslationUnit()) { 00807 if (isa<TagDecl>(ND) || isa<VarDecl>(ND)) { 00808 unsigned Disc; 00809 if (Context.getNextDiscriminator(ND, Disc)) { 00810 Out << '?'; 00811 mangleNumber(Disc); 00812 Out << '?'; 00813 } 00814 } 00815 00816 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) { 00817 DiagnosticsEngine &Diags = Context.getDiags(); 00818 unsigned DiagID = 00819 Diags.getCustomDiagID(DiagnosticsEngine::Error, 00820 "cannot mangle a local inside this block yet"); 00821 Diags.Report(BD->getLocation(), DiagID); 00822 00823 // FIXME: This is completely, utterly, wrong; see ItaniumMangle 00824 // for how this should be done. 00825 Out << "__block_invoke" << Context.getBlockId(BD, false); 00826 Out << '@'; 00827 continue; 00828 } else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) { 00829 mangleObjCMethodName(Method); 00830 } else if (isa<NamedDecl>(DC)) { 00831 ND = cast<NamedDecl>(DC); 00832 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { 00833 mangle(FD, "?"); 00834 break; 00835 } else 00836 mangleUnqualifiedName(ND); 00837 } 00838 DC = DC->getParent(); 00839 } 00840 } 00841 00842 void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) { 00843 // Microsoft uses the names on the case labels for these dtor variants. Clang 00844 // uses the Itanium terminology internally. Everything in this ABI delegates 00845 // towards the base dtor. 00846 switch (T) { 00847 // <operator-name> ::= ?1 # destructor 00848 case Dtor_Base: Out << "?1"; return; 00849 // <operator-name> ::= ?_D # vbase destructor 00850 case Dtor_Complete: Out << "?_D"; return; 00851 // <operator-name> ::= ?_G # scalar deleting destructor 00852 case Dtor_Deleting: Out << "?_G"; return; 00853 // <operator-name> ::= ?_E # vector deleting destructor 00854 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need 00855 // it. 00856 case Dtor_Comdat: 00857 llvm_unreachable("not expecting a COMDAT"); 00858 } 00859 llvm_unreachable("Unsupported dtor type?"); 00860 } 00861 00862 void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, 00863 SourceLocation Loc) { 00864 switch (OO) { 00865 // ?0 # constructor 00866 // ?1 # destructor 00867 // <operator-name> ::= ?2 # new 00868 case OO_New: Out << "?2"; break; 00869 // <operator-name> ::= ?3 # delete 00870 case OO_Delete: Out << "?3"; break; 00871 // <operator-name> ::= ?4 # = 00872 case OO_Equal: Out << "?4"; break; 00873 // <operator-name> ::= ?5 # >> 00874 case OO_GreaterGreater: Out << "?5"; break; 00875 // <operator-name> ::= ?6 # << 00876 case OO_LessLess: Out << "?6"; break; 00877 // <operator-name> ::= ?7 # ! 00878 case OO_Exclaim: Out << "?7"; break; 00879 // <operator-name> ::= ?8 # == 00880 case OO_EqualEqual: Out << "?8"; break; 00881 // <operator-name> ::= ?9 # != 00882 case OO_ExclaimEqual: Out << "?9"; break; 00883 // <operator-name> ::= ?A # [] 00884 case OO_Subscript: Out << "?A"; break; 00885 // ?B # conversion 00886 // <operator-name> ::= ?C # -> 00887 case OO_Arrow: Out << "?C"; break; 00888 // <operator-name> ::= ?D # * 00889 case OO_Star: Out << "?D"; break; 00890 // <operator-name> ::= ?E # ++ 00891 case OO_PlusPlus: Out << "?E"; break; 00892 // <operator-name> ::= ?F # -- 00893 case OO_MinusMinus: Out << "?F"; break; 00894 // <operator-name> ::= ?G # - 00895 case OO_Minus: Out << "?G"; break; 00896 // <operator-name> ::= ?H # + 00897 case OO_Plus: Out << "?H"; break; 00898 // <operator-name> ::= ?I # & 00899 case OO_Amp: Out << "?I"; break; 00900 // <operator-name> ::= ?J # ->* 00901 case OO_ArrowStar: Out << "?J"; break; 00902 // <operator-name> ::= ?K # / 00903 case OO_Slash: Out << "?K"; break; 00904 // <operator-name> ::= ?L # % 00905 case OO_Percent: Out << "?L"; break; 00906 // <operator-name> ::= ?M # < 00907 case OO_Less: Out << "?M"; break; 00908 // <operator-name> ::= ?N # <= 00909 case OO_LessEqual: Out << "?N"; break; 00910 // <operator-name> ::= ?O # > 00911 case OO_Greater: Out << "?O"; break; 00912 // <operator-name> ::= ?P # >= 00913 case OO_GreaterEqual: Out << "?P"; break; 00914 // <operator-name> ::= ?Q # , 00915 case OO_Comma: Out << "?Q"; break; 00916 // <operator-name> ::= ?R # () 00917 case OO_Call: Out << "?R"; break; 00918 // <operator-name> ::= ?S # ~ 00919 case OO_Tilde: Out << "?S"; break; 00920 // <operator-name> ::= ?T # ^ 00921 case OO_Caret: Out << "?T"; break; 00922 // <operator-name> ::= ?U # | 00923 case OO_Pipe: Out << "?U"; break; 00924 // <operator-name> ::= ?V # && 00925 case OO_AmpAmp: Out << "?V"; break; 00926 // <operator-name> ::= ?W # || 00927 case OO_PipePipe: Out << "?W"; break; 00928 // <operator-name> ::= ?X # *= 00929 case OO_StarEqual: Out << "?X"; break; 00930 // <operator-name> ::= ?Y # += 00931 case OO_PlusEqual: Out << "?Y"; break; 00932 // <operator-name> ::= ?Z # -= 00933 case OO_MinusEqual: Out << "?Z"; break; 00934 // <operator-name> ::= ?_0 # /= 00935 case OO_SlashEqual: Out << "?_0"; break; 00936 // <operator-name> ::= ?_1 # %= 00937 case OO_PercentEqual: Out << "?_1"; break; 00938 // <operator-name> ::= ?_2 # >>= 00939 case OO_GreaterGreaterEqual: Out << "?_2"; break; 00940 // <operator-name> ::= ?_3 # <<= 00941 case OO_LessLessEqual: Out << "?_3"; break; 00942 // <operator-name> ::= ?_4 # &= 00943 case OO_AmpEqual: Out << "?_4"; break; 00944 // <operator-name> ::= ?_5 # |= 00945 case OO_PipeEqual: Out << "?_5"; break; 00946 // <operator-name> ::= ?_6 # ^= 00947 case OO_CaretEqual: Out << "?_6"; break; 00948 // ?_7 # vftable 00949 // ?_8 # vbtable 00950 // ?_9 # vcall 00951 // ?_A # typeof 00952 // ?_B # local static guard 00953 // ?_C # string 00954 // ?_D # vbase destructor 00955 // ?_E # vector deleting destructor 00956 // ?_F # default constructor closure 00957 // ?_G # scalar deleting destructor 00958 // ?_H # vector constructor iterator 00959 // ?_I # vector destructor iterator 00960 // ?_J # vector vbase constructor iterator 00961 // ?_K # virtual displacement map 00962 // ?_L # eh vector constructor iterator 00963 // ?_M # eh vector destructor iterator 00964 // ?_N # eh vector vbase constructor iterator 00965 // ?_O # copy constructor closure 00966 // ?_P<name> # udt returning <name> 00967 // ?_Q # <unknown> 00968 // ?_R0 # RTTI Type Descriptor 00969 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d) 00970 // ?_R2 # RTTI Base Class Array 00971 // ?_R3 # RTTI Class Hierarchy Descriptor 00972 // ?_R4 # RTTI Complete Object Locator 00973 // ?_S # local vftable 00974 // ?_T # local vftable constructor closure 00975 // <operator-name> ::= ?_U # new[] 00976 case OO_Array_New: Out << "?_U"; break; 00977 // <operator-name> ::= ?_V # delete[] 00978 case OO_Array_Delete: Out << "?_V"; break; 00979 00980 case OO_Conditional: { 00981 DiagnosticsEngine &Diags = Context.getDiags(); 00982 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 00983 "cannot mangle this conditional operator yet"); 00984 Diags.Report(Loc, DiagID); 00985 break; 00986 } 00987 00988 case OO_None: 00989 case NUM_OVERLOADED_OPERATORS: 00990 llvm_unreachable("Not an overloaded operator"); 00991 } 00992 } 00993 00994 void MicrosoftCXXNameMangler::mangleSourceName(StringRef Name) { 00995 // <source name> ::= <identifier> @ 00996 BackRefVec::iterator Found = 00997 std::find(NameBackReferences.begin(), NameBackReferences.end(), Name); 00998 if (Found == NameBackReferences.end()) { 00999 if (NameBackReferences.size() < 10) 01000 NameBackReferences.push_back(Name); 01001 Out << Name << '@'; 01002 } else { 01003 Out << (Found - NameBackReferences.begin()); 01004 } 01005 } 01006 01007 void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { 01008 Context.mangleObjCMethodName(MD, Out); 01009 } 01010 01011 void MicrosoftCXXNameMangler::mangleTemplateInstantiationName( 01012 const TemplateDecl *TD, const TemplateArgumentList &TemplateArgs) { 01013 // <template-name> ::= <unscoped-template-name> <template-args> 01014 // ::= <substitution> 01015 // Always start with the unqualified name. 01016 01017 // Templates have their own context for back references. 01018 ArgBackRefMap OuterArgsContext; 01019 BackRefVec OuterTemplateContext; 01020 NameBackReferences.swap(OuterTemplateContext); 01021 TypeBackReferences.swap(OuterArgsContext); 01022 01023 mangleUnscopedTemplateName(TD); 01024 mangleTemplateArgs(TD, TemplateArgs); 01025 01026 // Restore the previous back reference contexts. 01027 NameBackReferences.swap(OuterTemplateContext); 01028 TypeBackReferences.swap(OuterArgsContext); 01029 } 01030 01031 void 01032 MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) { 01033 // <unscoped-template-name> ::= ?$ <unqualified-name> 01034 Out << "?$"; 01035 mangleUnqualifiedName(TD); 01036 } 01037 01038 void MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value, 01039 bool IsBoolean) { 01040 // <integer-literal> ::= $0 <number> 01041 Out << "$0"; 01042 // Make sure booleans are encoded as 0/1. 01043 if (IsBoolean && Value.getBoolValue()) 01044 mangleNumber(1); 01045 else 01046 mangleNumber(Value.getSExtValue()); 01047 } 01048 01049 void MicrosoftCXXNameMangler::mangleExpression(const Expr *E) { 01050 // See if this is a constant expression. 01051 llvm::APSInt Value; 01052 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) { 01053 mangleIntegerLiteral(Value, E->getType()->isBooleanType()); 01054 return; 01055 } 01056 01057 // Look through no-op casts like template parameter substitutions. 01058 E = E->IgnoreParenNoopCasts(Context.getASTContext()); 01059 01060 const CXXUuidofExpr *UE = nullptr; 01061 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { 01062 if (UO->getOpcode() == UO_AddrOf) 01063 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr()); 01064 } else 01065 UE = dyn_cast<CXXUuidofExpr>(E); 01066 01067 if (UE) { 01068 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from 01069 // const __s_GUID _GUID_{lower case UUID with underscores} 01070 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext()); 01071 std::string Name = "_GUID_" + Uuid.lower(); 01072 std::replace(Name.begin(), Name.end(), '-', '_'); 01073 01074 // If we had to peek through an address-of operator, treat this like we are 01075 // dealing with a pointer type. Otherwise, treat it like a const reference. 01076 // 01077 // N.B. This matches up with the handling of TemplateArgument::Declaration 01078 // in mangleTemplateArg 01079 if (UE == E) 01080 Out << "$E?"; 01081 else 01082 Out << "$1?"; 01083 Out << Name << "@@3U__s_GUID@@B"; 01084 return; 01085 } 01086 01087 // As bad as this diagnostic is, it's better than crashing. 01088 DiagnosticsEngine &Diags = Context.getDiags(); 01089 unsigned DiagID = Diags.getCustomDiagID( 01090 DiagnosticsEngine::Error, "cannot yet mangle expression type %0"); 01091 Diags.Report(E->getExprLoc(), DiagID) << E->getStmtClassName() 01092 << E->getSourceRange(); 01093 } 01094 01095 void MicrosoftCXXNameMangler::mangleTemplateArgs( 01096 const TemplateDecl *TD, const TemplateArgumentList &TemplateArgs) { 01097 // <template-args> ::= <template-arg>+ 01098 const TemplateParameterList *TPL = TD->getTemplateParameters(); 01099 assert(TPL->size() == TemplateArgs.size() && 01100 "size mismatch between args and parms!"); 01101 01102 unsigned Idx = 0; 01103 for (const TemplateArgument &TA : TemplateArgs.asArray()) 01104 mangleTemplateArg(TD, TA, TPL->getParam(Idx++)); 01105 } 01106 01107 void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD, 01108 const TemplateArgument &TA, 01109 const NamedDecl *Parm) { 01110 // <template-arg> ::= <type> 01111 // ::= <integer-literal> 01112 // ::= <member-data-pointer> 01113 // ::= <member-function-pointer> 01114 // ::= $E? <name> <type-encoding> 01115 // ::= $1? <name> <type-encoding> 01116 // ::= $0A@ 01117 // ::= <template-args> 01118 01119 switch (TA.getKind()) { 01120 case TemplateArgument::Null: 01121 llvm_unreachable("Can't mangle null template arguments!"); 01122 case TemplateArgument::TemplateExpansion: 01123 llvm_unreachable("Can't mangle template expansion arguments!"); 01124 case TemplateArgument::Type: { 01125 QualType T = TA.getAsType(); 01126 mangleType(T, SourceRange(), QMM_Escape); 01127 break; 01128 } 01129 case TemplateArgument::Declaration: { 01130 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl()); 01131 if (isa<FieldDecl>(ND) || isa<IndirectFieldDecl>(ND)) { 01132 mangleMemberDataPointer( 01133 cast<CXXRecordDecl>(ND->getDeclContext())->getMostRecentDecl(), 01134 cast<ValueDecl>(ND)); 01135 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { 01136 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); 01137 if (MD && MD->isInstance()) 01138 mangleMemberFunctionPointer(MD->getParent()->getMostRecentDecl(), MD); 01139 else 01140 mangle(FD, "$1?"); 01141 } else { 01142 mangle(ND, TA.getParamTypeForDecl()->isReferenceType() ? "$E?" : "$1?"); 01143 } 01144 break; 01145 } 01146 case TemplateArgument::Integral: 01147 mangleIntegerLiteral(TA.getAsIntegral(), 01148 TA.getIntegralType()->isBooleanType()); 01149 break; 01150 case TemplateArgument::NullPtr: { 01151 QualType T = TA.getNullPtrType(); 01152 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) { 01153 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 01154 if (MPT->isMemberFunctionPointerType() && isa<ClassTemplateDecl>(TD)) { 01155 mangleMemberFunctionPointer(RD, nullptr); 01156 return; 01157 } 01158 if (MPT->isMemberDataPointer()) { 01159 mangleMemberDataPointer(RD, nullptr); 01160 return; 01161 } 01162 } 01163 Out << "$0A@"; 01164 break; 01165 } 01166 case TemplateArgument::Expression: 01167 mangleExpression(TA.getAsExpr()); 01168 break; 01169 case TemplateArgument::Pack: { 01170 ArrayRef<TemplateArgument> TemplateArgs = TA.getPackAsArray(); 01171 if (TemplateArgs.empty()) { 01172 if (isa<TemplateTypeParmDecl>(Parm) || 01173 isa<TemplateTemplateParmDecl>(Parm)) 01174 Out << "$$V"; 01175 else if (isa<NonTypeTemplateParmDecl>(Parm)) 01176 Out << "$S"; 01177 else 01178 llvm_unreachable("unexpected template parameter decl!"); 01179 } else { 01180 for (const TemplateArgument &PA : TemplateArgs) 01181 mangleTemplateArg(TD, PA, Parm); 01182 } 01183 break; 01184 } 01185 case TemplateArgument::Template: { 01186 const NamedDecl *ND = 01187 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl(); 01188 if (const auto *TD = dyn_cast<TagDecl>(ND)) { 01189 mangleType(TD); 01190 } else if (isa<TypeAliasDecl>(ND)) { 01191 Out << "$$Y"; 01192 mangleName(ND); 01193 } else { 01194 llvm_unreachable("unexpected template template NamedDecl!"); 01195 } 01196 break; 01197 } 01198 } 01199 } 01200 01201 void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals, 01202 bool IsMember) { 01203 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers> 01204 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only); 01205 // 'I' means __restrict (32/64-bit). 01206 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict 01207 // keyword! 01208 // <base-cvr-qualifiers> ::= A # near 01209 // ::= B # near const 01210 // ::= C # near volatile 01211 // ::= D # near const volatile 01212 // ::= E # far (16-bit) 01213 // ::= F # far const (16-bit) 01214 // ::= G # far volatile (16-bit) 01215 // ::= H # far const volatile (16-bit) 01216 // ::= I # huge (16-bit) 01217 // ::= J # huge const (16-bit) 01218 // ::= K # huge volatile (16-bit) 01219 // ::= L # huge const volatile (16-bit) 01220 // ::= M <basis> # based 01221 // ::= N <basis> # based const 01222 // ::= O <basis> # based volatile 01223 // ::= P <basis> # based const volatile 01224 // ::= Q # near member 01225 // ::= R # near const member 01226 // ::= S # near volatile member 01227 // ::= T # near const volatile member 01228 // ::= U # far member (16-bit) 01229 // ::= V # far const member (16-bit) 01230 // ::= W # far volatile member (16-bit) 01231 // ::= X # far const volatile member (16-bit) 01232 // ::= Y # huge member (16-bit) 01233 // ::= Z # huge const member (16-bit) 01234 // ::= 0 # huge volatile member (16-bit) 01235 // ::= 1 # huge const volatile member (16-bit) 01236 // ::= 2 <basis> # based member 01237 // ::= 3 <basis> # based const member 01238 // ::= 4 <basis> # based volatile member 01239 // ::= 5 <basis> # based const volatile member 01240 // ::= 6 # near function (pointers only) 01241 // ::= 7 # far function (pointers only) 01242 // ::= 8 # near method (pointers only) 01243 // ::= 9 # far method (pointers only) 01244 // ::= _A <basis> # based function (pointers only) 01245 // ::= _B <basis> # based function (far?) (pointers only) 01246 // ::= _C <basis> # based method (pointers only) 01247 // ::= _D <basis> # based method (far?) (pointers only) 01248 // ::= _E # block (Clang) 01249 // <basis> ::= 0 # __based(void) 01250 // ::= 1 # __based(segment)? 01251 // ::= 2 <name> # __based(name) 01252 // ::= 3 # ? 01253 // ::= 4 # ? 01254 // ::= 5 # not really based 01255 bool HasConst = Quals.hasConst(), 01256 HasVolatile = Quals.hasVolatile(); 01257 01258 if (!IsMember) { 01259 if (HasConst && HasVolatile) { 01260 Out << 'D'; 01261 } else if (HasVolatile) { 01262 Out << 'C'; 01263 } else if (HasConst) { 01264 Out << 'B'; 01265 } else { 01266 Out << 'A'; 01267 } 01268 } else { 01269 if (HasConst && HasVolatile) { 01270 Out << 'T'; 01271 } else if (HasVolatile) { 01272 Out << 'S'; 01273 } else if (HasConst) { 01274 Out << 'R'; 01275 } else { 01276 Out << 'Q'; 01277 } 01278 } 01279 01280 // FIXME: For now, just drop all extension qualifiers on the floor. 01281 } 01282 01283 void 01284 MicrosoftCXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) { 01285 // <ref-qualifier> ::= G # lvalue reference 01286 // ::= H # rvalue-reference 01287 switch (RefQualifier) { 01288 case RQ_None: 01289 break; 01290 01291 case RQ_LValue: 01292 Out << 'G'; 01293 break; 01294 01295 case RQ_RValue: 01296 Out << 'H'; 01297 break; 01298 } 01299 } 01300 01301 void 01302 MicrosoftCXXNameMangler::manglePointerExtQualifiers(Qualifiers Quals, 01303 const Type *PointeeType) { 01304 bool HasRestrict = Quals.hasRestrict(); 01305 if (PointersAre64Bit && (!PointeeType || !PointeeType->isFunctionType())) 01306 Out << 'E'; 01307 01308 if (HasRestrict) 01309 Out << 'I'; 01310 } 01311 01312 void MicrosoftCXXNameMangler::manglePointerCVQualifiers(Qualifiers Quals) { 01313 // <pointer-cv-qualifiers> ::= P # no qualifiers 01314 // ::= Q # const 01315 // ::= R # volatile 01316 // ::= S # const volatile 01317 bool HasConst = Quals.hasConst(), 01318 HasVolatile = Quals.hasVolatile(); 01319 01320 if (HasConst && HasVolatile) { 01321 Out << 'S'; 01322 } else if (HasVolatile) { 01323 Out << 'R'; 01324 } else if (HasConst) { 01325 Out << 'Q'; 01326 } else { 01327 Out << 'P'; 01328 } 01329 } 01330 01331 void MicrosoftCXXNameMangler::mangleArgumentType(QualType T, 01332 SourceRange Range) { 01333 // MSVC will backreference two canonically equivalent types that have slightly 01334 // different manglings when mangled alone. 01335 01336 // Decayed types do not match up with non-decayed versions of the same type. 01337 // 01338 // e.g. 01339 // void (*x)(void) will not form a backreference with void x(void) 01340 void *TypePtr; 01341 if (const DecayedType *DT = T->getAs<DecayedType>()) { 01342 TypePtr = DT->getOriginalType().getCanonicalType().getAsOpaquePtr(); 01343 // If the original parameter was textually written as an array, 01344 // instead treat the decayed parameter like it's const. 01345 // 01346 // e.g. 01347 // int [] -> int * const 01348 if (DT->getOriginalType()->isArrayType()) 01349 T = T.withConst(); 01350 } else 01351 TypePtr = T.getCanonicalType().getAsOpaquePtr(); 01352 01353 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr); 01354 01355 if (Found == TypeBackReferences.end()) { 01356 size_t OutSizeBefore = Out.GetNumBytesInBuffer(); 01357 01358 mangleType(T, Range, QMM_Drop); 01359 01360 // See if it's worth creating a back reference. 01361 // Only types longer than 1 character are considered 01362 // and only 10 back references slots are available: 01363 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1); 01364 if (LongerThanOneChar && TypeBackReferences.size() < 10) { 01365 size_t Size = TypeBackReferences.size(); 01366 TypeBackReferences[TypePtr] = Size; 01367 } 01368 } else { 01369 Out << Found->second; 01370 } 01371 } 01372 01373 void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range, 01374 QualifierMangleMode QMM) { 01375 // Don't use the canonical types. MSVC includes things like 'const' on 01376 // pointer arguments to function pointers that canonicalization strips away. 01377 T = T.getDesugaredType(getASTContext()); 01378 Qualifiers Quals = T.getLocalQualifiers(); 01379 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) { 01380 // If there were any Quals, getAsArrayType() pushed them onto the array 01381 // element type. 01382 if (QMM == QMM_Mangle) 01383 Out << 'A'; 01384 else if (QMM == QMM_Escape || QMM == QMM_Result) 01385 Out << "$$B"; 01386 mangleArrayType(AT); 01387 return; 01388 } 01389 01390 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() || 01391 T->isBlockPointerType(); 01392 01393 switch (QMM) { 01394 case QMM_Drop: 01395 break; 01396 case QMM_Mangle: 01397 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) { 01398 Out << '6'; 01399 mangleFunctionType(FT); 01400 return; 01401 } 01402 mangleQualifiers(Quals, false); 01403 break; 01404 case QMM_Escape: 01405 if (!IsPointer && Quals) { 01406 Out << "$$C"; 01407 mangleQualifiers(Quals, false); 01408 } 01409 break; 01410 case QMM_Result: 01411 if ((!IsPointer && Quals) || isa<TagType>(T)) { 01412 Out << '?'; 01413 mangleQualifiers(Quals, false); 01414 } 01415 break; 01416 } 01417 01418 // We have to mangle these now, while we still have enough information. 01419 if (IsPointer) { 01420 manglePointerCVQualifiers(Quals); 01421 manglePointerExtQualifiers(Quals, T->getPointeeType().getTypePtr()); 01422 } 01423 const Type *ty = T.getTypePtr(); 01424 01425 switch (ty->getTypeClass()) { 01426 #define ABSTRACT_TYPE(CLASS, PARENT) 01427 #define NON_CANONICAL_TYPE(CLASS, PARENT) \ 01428 case Type::CLASS: \ 01429 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ 01430 return; 01431 #define TYPE(CLASS, PARENT) \ 01432 case Type::CLASS: \ 01433 mangleType(cast<CLASS##Type>(ty), Range); \ 01434 break; 01435 #include "clang/AST/TypeNodes.def" 01436 #undef ABSTRACT_TYPE 01437 #undef NON_CANONICAL_TYPE 01438 #undef TYPE 01439 } 01440 } 01441 01442 void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T, 01443 SourceRange Range) { 01444 // <type> ::= <builtin-type> 01445 // <builtin-type> ::= X # void 01446 // ::= C # signed char 01447 // ::= D # char 01448 // ::= E # unsigned char 01449 // ::= F # short 01450 // ::= G # unsigned short (or wchar_t if it's not a builtin) 01451 // ::= H # int 01452 // ::= I # unsigned int 01453 // ::= J # long 01454 // ::= K # unsigned long 01455 // L # <none> 01456 // ::= M # float 01457 // ::= N # double 01458 // ::= O # long double (__float80 is mangled differently) 01459 // ::= _J # long long, __int64 01460 // ::= _K # unsigned long long, __int64 01461 // ::= _L # __int128 01462 // ::= _M # unsigned __int128 01463 // ::= _N # bool 01464 // _O # <array in parameter> 01465 // ::= _T # __float80 (Intel) 01466 // ::= _W # wchar_t 01467 // ::= _Z # __float80 (Digital Mars) 01468 switch (T->getKind()) { 01469 case BuiltinType::Void: Out << 'X'; break; 01470 case BuiltinType::SChar: Out << 'C'; break; 01471 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break; 01472 case BuiltinType::UChar: Out << 'E'; break; 01473 case BuiltinType::Short: Out << 'F'; break; 01474 case BuiltinType::UShort: Out << 'G'; break; 01475 case BuiltinType::Int: Out << 'H'; break; 01476 case BuiltinType::UInt: Out << 'I'; break; 01477 case BuiltinType::Long: Out << 'J'; break; 01478 case BuiltinType::ULong: Out << 'K'; break; 01479 case BuiltinType::Float: Out << 'M'; break; 01480 case BuiltinType::Double: Out << 'N'; break; 01481 // TODO: Determine size and mangle accordingly 01482 case BuiltinType::LongDouble: Out << 'O'; break; 01483 case BuiltinType::LongLong: Out << "_J"; break; 01484 case BuiltinType::ULongLong: Out << "_K"; break; 01485 case BuiltinType::Int128: Out << "_L"; break; 01486 case BuiltinType::UInt128: Out << "_M"; break; 01487 case BuiltinType::Bool: Out << "_N"; break; 01488 case BuiltinType::WChar_S: 01489 case BuiltinType::WChar_U: Out << "_W"; break; 01490 01491 #define BUILTIN_TYPE(Id, SingletonId) 01492 #define PLACEHOLDER_TYPE(Id, SingletonId) \ 01493 case BuiltinType::Id: 01494 #include "clang/AST/BuiltinTypes.def" 01495 case BuiltinType::Dependent: 01496 llvm_unreachable("placeholder types shouldn't get to name mangling"); 01497 01498 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break; 01499 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break; 01500 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break; 01501 01502 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break; 01503 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break; 01504 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break; 01505 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break; 01506 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break; 01507 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break; 01508 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break; 01509 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break; 01510 01511 case BuiltinType::NullPtr: Out << "$$T"; break; 01512 01513 case BuiltinType::Char16: 01514 case BuiltinType::Char32: 01515 case BuiltinType::Half: { 01516 DiagnosticsEngine &Diags = Context.getDiags(); 01517 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 01518 "cannot mangle this built-in %0 type yet"); 01519 Diags.Report(Range.getBegin(), DiagID) 01520 << T->getName(Context.getASTContext().getPrintingPolicy()) 01521 << Range; 01522 break; 01523 } 01524 } 01525 } 01526 01527 // <type> ::= <function-type> 01528 void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T, 01529 SourceRange) { 01530 // Structors only appear in decls, so at this point we know it's not a 01531 // structor type. 01532 // FIXME: This may not be lambda-friendly. 01533 if (T->getTypeQuals() || T->getRefQualifier() != RQ_None) { 01534 Out << "$$A8@@"; 01535 mangleFunctionType(T, /*D=*/nullptr, /*ForceThisQuals=*/true); 01536 } else { 01537 Out << "$$A6"; 01538 mangleFunctionType(T); 01539 } 01540 } 01541 void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T, 01542 SourceRange) { 01543 llvm_unreachable("Can't mangle K&R function prototypes"); 01544 } 01545 01546 void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T, 01547 const FunctionDecl *D, 01548 bool ForceThisQuals) { 01549 // <function-type> ::= <this-cvr-qualifiers> <calling-convention> 01550 // <return-type> <argument-list> <throw-spec> 01551 const FunctionProtoType *Proto = cast<FunctionProtoType>(T); 01552 01553 SourceRange Range; 01554 if (D) Range = D->getSourceRange(); 01555 01556 bool IsStructor = false, HasThisQuals = ForceThisQuals; 01557 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(D)) { 01558 if (MD->isInstance()) 01559 HasThisQuals = true; 01560 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) 01561 IsStructor = true; 01562 } 01563 01564 // If this is a C++ instance method, mangle the CVR qualifiers for the 01565 // this pointer. 01566 if (HasThisQuals) { 01567 Qualifiers Quals = Qualifiers::fromCVRMask(Proto->getTypeQuals()); 01568 manglePointerExtQualifiers(Quals, /*PointeeType=*/nullptr); 01569 mangleRefQualifier(Proto->getRefQualifier()); 01570 mangleQualifiers(Quals, /*IsMember=*/false); 01571 } 01572 01573 mangleCallingConvention(T); 01574 01575 // <return-type> ::= <type> 01576 // ::= @ # structors (they have no declared return type) 01577 if (IsStructor) { 01578 if (isa<CXXDestructorDecl>(D) && D == Structor && 01579 StructorType == Dtor_Deleting) { 01580 // The scalar deleting destructor takes an extra int argument. 01581 // However, the FunctionType generated has 0 arguments. 01582 // FIXME: This is a temporary hack. 01583 // Maybe should fix the FunctionType creation instead? 01584 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z"); 01585 return; 01586 } 01587 Out << '@'; 01588 } else { 01589 QualType ResultType = Proto->getReturnType(); 01590 if (const auto *AT = 01591 dyn_cast_or_null<AutoType>(ResultType->getContainedAutoType())) { 01592 Out << '?'; 01593 mangleQualifiers(ResultType.getLocalQualifiers(), /*IsMember=*/false); 01594 Out << '?'; 01595 mangleSourceName(AT->isDecltypeAuto() ? "<decltype-auto>" : "<auto>"); 01596 Out << '@'; 01597 } else { 01598 if (ResultType->isVoidType()) 01599 ResultType = ResultType.getUnqualifiedType(); 01600 mangleType(ResultType, Range, QMM_Result); 01601 } 01602 } 01603 01604 // <argument-list> ::= X # void 01605 // ::= <type>+ @ 01606 // ::= <type>* Z # varargs 01607 if (Proto->getNumParams() == 0 && !Proto->isVariadic()) { 01608 Out << 'X'; 01609 } else { 01610 // Happens for function pointer type arguments for example. 01611 for (const QualType Arg : Proto->param_types()) 01612 mangleArgumentType(Arg, Range); 01613 // <builtin-type> ::= Z # ellipsis 01614 if (Proto->isVariadic()) 01615 Out << 'Z'; 01616 else 01617 Out << '@'; 01618 } 01619 01620 mangleThrowSpecification(Proto); 01621 } 01622 01623 void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) { 01624 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this' 01625 // # pointer. in 64-bit mode *all* 01626 // # 'this' pointers are 64-bit. 01627 // ::= <global-function> 01628 // <member-function> ::= A # private: near 01629 // ::= B # private: far 01630 // ::= C # private: static near 01631 // ::= D # private: static far 01632 // ::= E # private: virtual near 01633 // ::= F # private: virtual far 01634 // ::= I # protected: near 01635 // ::= J # protected: far 01636 // ::= K # protected: static near 01637 // ::= L # protected: static far 01638 // ::= M # protected: virtual near 01639 // ::= N # protected: virtual far 01640 // ::= Q # public: near 01641 // ::= R # public: far 01642 // ::= S # public: static near 01643 // ::= T # public: static far 01644 // ::= U # public: virtual near 01645 // ::= V # public: virtual far 01646 // <global-function> ::= Y # global near 01647 // ::= Z # global far 01648 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { 01649 switch (MD->getAccess()) { 01650 case AS_none: 01651 llvm_unreachable("Unsupported access specifier"); 01652 case AS_private: 01653 if (MD->isStatic()) 01654 Out << 'C'; 01655 else if (MD->isVirtual()) 01656 Out << 'E'; 01657 else 01658 Out << 'A'; 01659 break; 01660 case AS_protected: 01661 if (MD->isStatic()) 01662 Out << 'K'; 01663 else if (MD->isVirtual()) 01664 Out << 'M'; 01665 else 01666 Out << 'I'; 01667 break; 01668 case AS_public: 01669 if (MD->isStatic()) 01670 Out << 'S'; 01671 else if (MD->isVirtual()) 01672 Out << 'U'; 01673 else 01674 Out << 'Q'; 01675 } 01676 } else 01677 Out << 'Y'; 01678 } 01679 void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T) { 01680 // <calling-convention> ::= A # __cdecl 01681 // ::= B # __export __cdecl 01682 // ::= C # __pascal 01683 // ::= D # __export __pascal 01684 // ::= E # __thiscall 01685 // ::= F # __export __thiscall 01686 // ::= G # __stdcall 01687 // ::= H # __export __stdcall 01688 // ::= I # __fastcall 01689 // ::= J # __export __fastcall 01690 // ::= Q # __vectorcall 01691 // The 'export' calling conventions are from a bygone era 01692 // (*cough*Win16*cough*) when functions were declared for export with 01693 // that keyword. (It didn't actually export them, it just made them so 01694 // that they could be in a DLL and somebody from another module could call 01695 // them.) 01696 CallingConv CC = T->getCallConv(); 01697 switch (CC) { 01698 default: 01699 llvm_unreachable("Unsupported CC for mangling"); 01700 case CC_X86_64Win64: 01701 case CC_X86_64SysV: 01702 case CC_C: Out << 'A'; break; 01703 case CC_X86Pascal: Out << 'C'; break; 01704 case CC_X86ThisCall: Out << 'E'; break; 01705 case CC_X86StdCall: Out << 'G'; break; 01706 case CC_X86FastCall: Out << 'I'; break; 01707 case CC_X86VectorCall: Out << 'Q'; break; 01708 } 01709 } 01710 void MicrosoftCXXNameMangler::mangleThrowSpecification( 01711 const FunctionProtoType *FT) { 01712 // <throw-spec> ::= Z # throw(...) (default) 01713 // ::= @ # throw() or __declspec/__attribute__((nothrow)) 01714 // ::= <type>+ 01715 // NOTE: Since the Microsoft compiler ignores throw specifications, they are 01716 // all actually mangled as 'Z'. (They're ignored because their associated 01717 // functionality isn't implemented, and probably never will be.) 01718 Out << 'Z'; 01719 } 01720 01721 void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T, 01722 SourceRange Range) { 01723 // Probably should be mangled as a template instantiation; need to see what 01724 // VC does first. 01725 DiagnosticsEngine &Diags = Context.getDiags(); 01726 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 01727 "cannot mangle this unresolved dependent type yet"); 01728 Diags.Report(Range.getBegin(), DiagID) 01729 << Range; 01730 } 01731 01732 // <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type> 01733 // <union-type> ::= T <name> 01734 // <struct-type> ::= U <name> 01735 // <class-type> ::= V <name> 01736 // <enum-type> ::= W4 <name> 01737 void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) { 01738 mangleType(cast<TagType>(T)->getDecl()); 01739 } 01740 void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) { 01741 mangleType(cast<TagType>(T)->getDecl()); 01742 } 01743 void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) { 01744 switch (TD->getTagKind()) { 01745 case TTK_Union: 01746 Out << 'T'; 01747 break; 01748 case TTK_Struct: 01749 case TTK_Interface: 01750 Out << 'U'; 01751 break; 01752 case TTK_Class: 01753 Out << 'V'; 01754 break; 01755 case TTK_Enum: 01756 Out << "W4"; 01757 break; 01758 } 01759 mangleName(TD); 01760 } 01761 01762 // <type> ::= <array-type> 01763 // <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> 01764 // [Y <dimension-count> <dimension>+] 01765 // <element-type> # as global, E is never required 01766 // It's supposed to be the other way around, but for some strange reason, it 01767 // isn't. Today this behavior is retained for the sole purpose of backwards 01768 // compatibility. 01769 void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) { 01770 // This isn't a recursive mangling, so now we have to do it all in this 01771 // one call. 01772 manglePointerCVQualifiers(T->getElementType().getQualifiers()); 01773 mangleType(T->getElementType(), SourceRange()); 01774 } 01775 void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T, 01776 SourceRange) { 01777 llvm_unreachable("Should have been special cased"); 01778 } 01779 void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T, 01780 SourceRange) { 01781 llvm_unreachable("Should have been special cased"); 01782 } 01783 void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T, 01784 SourceRange) { 01785 llvm_unreachable("Should have been special cased"); 01786 } 01787 void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T, 01788 SourceRange) { 01789 llvm_unreachable("Should have been special cased"); 01790 } 01791 void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) { 01792 QualType ElementTy(T, 0); 01793 SmallVector<llvm::APInt, 3> Dimensions; 01794 for (;;) { 01795 if (const ConstantArrayType *CAT = 01796 getASTContext().getAsConstantArrayType(ElementTy)) { 01797 Dimensions.push_back(CAT->getSize()); 01798 ElementTy = CAT->getElementType(); 01799 } else if (ElementTy->isVariableArrayType()) { 01800 const VariableArrayType *VAT = 01801 getASTContext().getAsVariableArrayType(ElementTy); 01802 DiagnosticsEngine &Diags = Context.getDiags(); 01803 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 01804 "cannot mangle this variable-length array yet"); 01805 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID) 01806 << VAT->getBracketsRange(); 01807 return; 01808 } else if (ElementTy->isDependentSizedArrayType()) { 01809 // The dependent expression has to be folded into a constant (TODO). 01810 const DependentSizedArrayType *DSAT = 01811 getASTContext().getAsDependentSizedArrayType(ElementTy); 01812 DiagnosticsEngine &Diags = Context.getDiags(); 01813 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 01814 "cannot mangle this dependent-length array yet"); 01815 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID) 01816 << DSAT->getBracketsRange(); 01817 return; 01818 } else if (const IncompleteArrayType *IAT = 01819 getASTContext().getAsIncompleteArrayType(ElementTy)) { 01820 Dimensions.push_back(llvm::APInt(32, 0)); 01821 ElementTy = IAT->getElementType(); 01822 } 01823 else break; 01824 } 01825 Out << 'Y'; 01826 // <dimension-count> ::= <number> # number of extra dimensions 01827 mangleNumber(Dimensions.size()); 01828 for (const llvm::APInt &Dimension : Dimensions) 01829 mangleNumber(Dimension.getLimitedValue()); 01830 mangleType(ElementTy, SourceRange(), QMM_Escape); 01831 } 01832 01833 // <type> ::= <pointer-to-member-type> 01834 // <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> 01835 // <class name> <type> 01836 void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T, 01837 SourceRange Range) { 01838 QualType PointeeType = T->getPointeeType(); 01839 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) { 01840 Out << '8'; 01841 mangleName(T->getClass()->castAs<RecordType>()->getDecl()); 01842 mangleFunctionType(FPT, nullptr, true); 01843 } else { 01844 mangleQualifiers(PointeeType.getQualifiers(), true); 01845 mangleName(T->getClass()->castAs<RecordType>()->getDecl()); 01846 mangleType(PointeeType, Range, QMM_Drop); 01847 } 01848 } 01849 01850 void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T, 01851 SourceRange Range) { 01852 DiagnosticsEngine &Diags = Context.getDiags(); 01853 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 01854 "cannot mangle this template type parameter type yet"); 01855 Diags.Report(Range.getBegin(), DiagID) 01856 << Range; 01857 } 01858 01859 void MicrosoftCXXNameMangler::mangleType( 01860 const SubstTemplateTypeParmPackType *T, 01861 SourceRange Range) { 01862 DiagnosticsEngine &Diags = Context.getDiags(); 01863 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 01864 "cannot mangle this substituted parameter pack yet"); 01865 Diags.Report(Range.getBegin(), DiagID) 01866 << Range; 01867 } 01868 01869 // <type> ::= <pointer-type> 01870 // <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type> 01871 // # the E is required for 64-bit non-static pointers 01872 void MicrosoftCXXNameMangler::mangleType(const PointerType *T, 01873 SourceRange Range) { 01874 QualType PointeeTy = T->getPointeeType(); 01875 mangleType(PointeeTy, Range); 01876 } 01877 void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T, 01878 SourceRange Range) { 01879 // Object pointers never have qualifiers. 01880 Out << 'A'; 01881 manglePointerExtQualifiers(Qualifiers(), T->getPointeeType().getTypePtr()); 01882 mangleType(T->getPointeeType(), Range); 01883 } 01884 01885 // <type> ::= <reference-type> 01886 // <reference-type> ::= A E? <cvr-qualifiers> <type> 01887 // # the E is required for 64-bit non-static lvalue references 01888 void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T, 01889 SourceRange Range) { 01890 Out << 'A'; 01891 manglePointerExtQualifiers(Qualifiers(), T->getPointeeType().getTypePtr()); 01892 mangleType(T->getPointeeType(), Range); 01893 } 01894 01895 // <type> ::= <r-value-reference-type> 01896 // <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type> 01897 // # the E is required for 64-bit non-static rvalue references 01898 void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T, 01899 SourceRange Range) { 01900 Out << "$$Q"; 01901 manglePointerExtQualifiers(Qualifiers(), T->getPointeeType().getTypePtr()); 01902 mangleType(T->getPointeeType(), Range); 01903 } 01904 01905 void MicrosoftCXXNameMangler::mangleType(const ComplexType *T, 01906 SourceRange Range) { 01907 DiagnosticsEngine &Diags = Context.getDiags(); 01908 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 01909 "cannot mangle this complex number type yet"); 01910 Diags.Report(Range.getBegin(), DiagID) 01911 << Range; 01912 } 01913 01914 void MicrosoftCXXNameMangler::mangleType(const VectorType *T, 01915 SourceRange Range) { 01916 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>(); 01917 assert(ET && "vectors with non-builtin elements are unsupported"); 01918 uint64_t Width = getASTContext().getTypeSize(T); 01919 // Pattern match exactly the typedefs in our intrinsic headers. Anything that 01920 // doesn't match the Intel types uses a custom mangling below. 01921 bool IntelVector = true; 01922 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) { 01923 Out << "T__m64"; 01924 } else if (Width == 128 || Width == 256) { 01925 if (ET->getKind() == BuiltinType::Float) 01926 Out << "T__m" << Width; 01927 else if (ET->getKind() == BuiltinType::LongLong) 01928 Out << "T__m" << Width << 'i'; 01929 else if (ET->getKind() == BuiltinType::Double) 01930 Out << "U__m" << Width << 'd'; 01931 else 01932 IntelVector = false; 01933 } else { 01934 IntelVector = false; 01935 } 01936 01937 if (!IntelVector) { 01938 // The MS ABI doesn't have a special mangling for vector types, so we define 01939 // our own mangling to handle uses of __vector_size__ on user-specified 01940 // types, and for extensions like __v4sf. 01941 Out << "T__clang_vec" << T->getNumElements() << '_'; 01942 mangleType(ET, Range); 01943 } 01944 01945 Out << "@@"; 01946 } 01947 01948 void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T, 01949 SourceRange Range) { 01950 DiagnosticsEngine &Diags = Context.getDiags(); 01951 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 01952 "cannot mangle this extended vector type yet"); 01953 Diags.Report(Range.getBegin(), DiagID) 01954 << Range; 01955 } 01956 void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T, 01957 SourceRange Range) { 01958 DiagnosticsEngine &Diags = Context.getDiags(); 01959 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 01960 "cannot mangle this dependent-sized extended vector type yet"); 01961 Diags.Report(Range.getBegin(), DiagID) 01962 << Range; 01963 } 01964 01965 void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T, 01966 SourceRange) { 01967 // ObjC interfaces have structs underlying them. 01968 Out << 'U'; 01969 mangleName(T->getDecl()); 01970 } 01971 01972 void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T, 01973 SourceRange Range) { 01974 // We don't allow overloading by different protocol qualification, 01975 // so mangling them isn't necessary. 01976 mangleType(T->getBaseType(), Range); 01977 } 01978 01979 void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T, 01980 SourceRange Range) { 01981 Out << "_E"; 01982 01983 QualType pointee = T->getPointeeType(); 01984 mangleFunctionType(pointee->castAs<FunctionProtoType>()); 01985 } 01986 01987 void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *, 01988 SourceRange) { 01989 llvm_unreachable("Cannot mangle injected class name type."); 01990 } 01991 01992 void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T, 01993 SourceRange Range) { 01994 DiagnosticsEngine &Diags = Context.getDiags(); 01995 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 01996 "cannot mangle this template specialization type yet"); 01997 Diags.Report(Range.getBegin(), DiagID) 01998 << Range; 01999 } 02000 02001 void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T, 02002 SourceRange Range) { 02003 DiagnosticsEngine &Diags = Context.getDiags(); 02004 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 02005 "cannot mangle this dependent name type yet"); 02006 Diags.Report(Range.getBegin(), DiagID) 02007 << Range; 02008 } 02009 02010 void MicrosoftCXXNameMangler::mangleType( 02011 const DependentTemplateSpecializationType *T, 02012 SourceRange Range) { 02013 DiagnosticsEngine &Diags = Context.getDiags(); 02014 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 02015 "cannot mangle this dependent template specialization type yet"); 02016 Diags.Report(Range.getBegin(), DiagID) 02017 << Range; 02018 } 02019 02020 void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T, 02021 SourceRange Range) { 02022 DiagnosticsEngine &Diags = Context.getDiags(); 02023 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 02024 "cannot mangle this pack expansion yet"); 02025 Diags.Report(Range.getBegin(), DiagID) 02026 << Range; 02027 } 02028 02029 void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T, 02030 SourceRange Range) { 02031 DiagnosticsEngine &Diags = Context.getDiags(); 02032 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 02033 "cannot mangle this typeof(type) yet"); 02034 Diags.Report(Range.getBegin(), DiagID) 02035 << Range; 02036 } 02037 02038 void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T, 02039 SourceRange Range) { 02040 DiagnosticsEngine &Diags = Context.getDiags(); 02041 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 02042 "cannot mangle this typeof(expression) yet"); 02043 Diags.Report(Range.getBegin(), DiagID) 02044 << Range; 02045 } 02046 02047 void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T, 02048 SourceRange Range) { 02049 DiagnosticsEngine &Diags = Context.getDiags(); 02050 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 02051 "cannot mangle this decltype() yet"); 02052 Diags.Report(Range.getBegin(), DiagID) 02053 << Range; 02054 } 02055 02056 void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T, 02057 SourceRange Range) { 02058 DiagnosticsEngine &Diags = Context.getDiags(); 02059 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 02060 "cannot mangle this unary transform type yet"); 02061 Diags.Report(Range.getBegin(), DiagID) 02062 << Range; 02063 } 02064 02065 void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) { 02066 assert(T->getDeducedType().isNull() && "expecting a dependent type!"); 02067 02068 DiagnosticsEngine &Diags = Context.getDiags(); 02069 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 02070 "cannot mangle this 'auto' type yet"); 02071 Diags.Report(Range.getBegin(), DiagID) 02072 << Range; 02073 } 02074 02075 void MicrosoftCXXNameMangler::mangleType(const AtomicType *T, 02076 SourceRange Range) { 02077 DiagnosticsEngine &Diags = Context.getDiags(); 02078 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 02079 "cannot mangle this C11 atomic type yet"); 02080 Diags.Report(Range.getBegin(), DiagID) 02081 << Range; 02082 } 02083 02084 void MicrosoftMangleContextImpl::mangleCXXName(const NamedDecl *D, 02085 raw_ostream &Out) { 02086 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && 02087 "Invalid mangleName() call, argument is not a variable or function!"); 02088 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && 02089 "Invalid mangleName() call on 'structor decl!"); 02090 02091 PrettyStackTraceDecl CrashInfo(D, SourceLocation(), 02092 getASTContext().getSourceManager(), 02093 "Mangling declaration"); 02094 02095 MicrosoftCXXNameMangler Mangler(*this, Out); 02096 return Mangler.mangle(D); 02097 } 02098 02099 // <this-adjustment> ::= <no-adjustment> | <static-adjustment> | 02100 // <virtual-adjustment> 02101 // <no-adjustment> ::= A # private near 02102 // ::= B # private far 02103 // ::= I # protected near 02104 // ::= J # protected far 02105 // ::= Q # public near 02106 // ::= R # public far 02107 // <static-adjustment> ::= G <static-offset> # private near 02108 // ::= H <static-offset> # private far 02109 // ::= O <static-offset> # protected near 02110 // ::= P <static-offset> # protected far 02111 // ::= W <static-offset> # public near 02112 // ::= X <static-offset> # public far 02113 // <virtual-adjustment> ::= $0 <virtual-shift> <static-offset> # private near 02114 // ::= $1 <virtual-shift> <static-offset> # private far 02115 // ::= $2 <virtual-shift> <static-offset> # protected near 02116 // ::= $3 <virtual-shift> <static-offset> # protected far 02117 // ::= $4 <virtual-shift> <static-offset> # public near 02118 // ::= $5 <virtual-shift> <static-offset> # public far 02119 // <virtual-shift> ::= <vtordisp-shift> | <vtordispex-shift> 02120 // <vtordisp-shift> ::= <offset-to-vtordisp> 02121 // <vtordispex-shift> ::= <offset-to-vbptr> <vbase-offset-offset> 02122 // <offset-to-vtordisp> 02123 static void mangleThunkThisAdjustment(const CXXMethodDecl *MD, 02124 const ThisAdjustment &Adjustment, 02125 MicrosoftCXXNameMangler &Mangler, 02126 raw_ostream &Out) { 02127 if (!Adjustment.Virtual.isEmpty()) { 02128 Out << '$'; 02129 char AccessSpec; 02130 switch (MD->getAccess()) { 02131 case AS_none: 02132 llvm_unreachable("Unsupported access specifier"); 02133 case AS_private: 02134 AccessSpec = '0'; 02135 break; 02136 case AS_protected: 02137 AccessSpec = '2'; 02138 break; 02139 case AS_public: 02140 AccessSpec = '4'; 02141 } 02142 if (Adjustment.Virtual.Microsoft.VBPtrOffset) { 02143 Out << 'R' << AccessSpec; 02144 Mangler.mangleNumber( 02145 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBPtrOffset)); 02146 Mangler.mangleNumber( 02147 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBOffsetOffset)); 02148 Mangler.mangleNumber( 02149 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset)); 02150 Mangler.mangleNumber(static_cast<uint32_t>(Adjustment.NonVirtual)); 02151 } else { 02152 Out << AccessSpec; 02153 Mangler.mangleNumber( 02154 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset)); 02155 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual)); 02156 } 02157 } else if (Adjustment.NonVirtual != 0) { 02158 switch (MD->getAccess()) { 02159 case AS_none: 02160 llvm_unreachable("Unsupported access specifier"); 02161 case AS_private: 02162 Out << 'G'; 02163 break; 02164 case AS_protected: 02165 Out << 'O'; 02166 break; 02167 case AS_public: 02168 Out << 'W'; 02169 } 02170 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual)); 02171 } else { 02172 switch (MD->getAccess()) { 02173 case AS_none: 02174 llvm_unreachable("Unsupported access specifier"); 02175 case AS_private: 02176 Out << 'A'; 02177 break; 02178 case AS_protected: 02179 Out << 'I'; 02180 break; 02181 case AS_public: 02182 Out << 'Q'; 02183 } 02184 } 02185 } 02186 02187 void 02188 MicrosoftMangleContextImpl::mangleVirtualMemPtrThunk(const CXXMethodDecl *MD, 02189 raw_ostream &Out) { 02190 MicrosoftVTableContext *VTContext = 02191 cast<MicrosoftVTableContext>(getASTContext().getVTableContext()); 02192 const MicrosoftVTableContext::MethodVFTableLocation &ML = 02193 VTContext->getMethodVFTableLocation(GlobalDecl(MD)); 02194 02195 MicrosoftCXXNameMangler Mangler(*this, Out); 02196 Mangler.getStream() << "\01?"; 02197 Mangler.mangleVirtualMemPtrThunk(MD, ML); 02198 } 02199 02200 void MicrosoftMangleContextImpl::mangleThunk(const CXXMethodDecl *MD, 02201 const ThunkInfo &Thunk, 02202 raw_ostream &Out) { 02203 MicrosoftCXXNameMangler Mangler(*this, Out); 02204 Out << "\01?"; 02205 Mangler.mangleName(MD); 02206 mangleThunkThisAdjustment(MD, Thunk.This, Mangler, Out); 02207 if (!Thunk.Return.isEmpty()) 02208 assert(Thunk.Method != nullptr && 02209 "Thunk info should hold the overridee decl"); 02210 02211 const CXXMethodDecl *DeclForFPT = Thunk.Method ? Thunk.Method : MD; 02212 Mangler.mangleFunctionType( 02213 DeclForFPT->getType()->castAs<FunctionProtoType>(), MD); 02214 } 02215 02216 void MicrosoftMangleContextImpl::mangleCXXDtorThunk( 02217 const CXXDestructorDecl *DD, CXXDtorType Type, 02218 const ThisAdjustment &Adjustment, raw_ostream &Out) { 02219 // FIXME: Actually, the dtor thunk should be emitted for vector deleting 02220 // dtors rather than scalar deleting dtors. Just use the vector deleting dtor 02221 // mangling manually until we support both deleting dtor types. 02222 assert(Type == Dtor_Deleting); 02223 MicrosoftCXXNameMangler Mangler(*this, Out, DD, Type); 02224 Out << "\01??_E"; 02225 Mangler.mangleName(DD->getParent()); 02226 mangleThunkThisAdjustment(DD, Adjustment, Mangler, Out); 02227 Mangler.mangleFunctionType(DD->getType()->castAs<FunctionProtoType>(), DD); 02228 } 02229 02230 void MicrosoftMangleContextImpl::mangleCXXVFTable( 02231 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath, 02232 raw_ostream &Out) { 02233 // <mangled-name> ::= ?_7 <class-name> <storage-class> 02234 // <cvr-qualifiers> [<name>] @ 02235 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class> 02236 // is always '6' for vftables. 02237 MicrosoftCXXNameMangler Mangler(*this, Out); 02238 Mangler.getStream() << "\01??_7"; 02239 Mangler.mangleName(Derived); 02240 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const. 02241 for (const CXXRecordDecl *RD : BasePath) 02242 Mangler.mangleName(RD); 02243 Mangler.getStream() << '@'; 02244 } 02245 02246 void MicrosoftMangleContextImpl::mangleCXXVBTable( 02247 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath, 02248 raw_ostream &Out) { 02249 // <mangled-name> ::= ?_8 <class-name> <storage-class> 02250 // <cvr-qualifiers> [<name>] @ 02251 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class> 02252 // is always '7' for vbtables. 02253 MicrosoftCXXNameMangler Mangler(*this, Out); 02254 Mangler.getStream() << "\01??_8"; 02255 Mangler.mangleName(Derived); 02256 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const. 02257 for (const CXXRecordDecl *RD : BasePath) 02258 Mangler.mangleName(RD); 02259 Mangler.getStream() << '@'; 02260 } 02261 02262 void MicrosoftMangleContextImpl::mangleCXXRTTI(QualType T, raw_ostream &Out) { 02263 MicrosoftCXXNameMangler Mangler(*this, Out); 02264 Mangler.getStream() << "\01??_R0"; 02265 Mangler.mangleType(T, SourceRange(), MicrosoftCXXNameMangler::QMM_Result); 02266 Mangler.getStream() << "@8"; 02267 } 02268 02269 void MicrosoftMangleContextImpl::mangleCXXRTTIName(QualType T, 02270 raw_ostream &Out) { 02271 MicrosoftCXXNameMangler Mangler(*this, Out); 02272 Mangler.getStream() << '.'; 02273 Mangler.mangleType(T, SourceRange(), MicrosoftCXXNameMangler::QMM_Result); 02274 } 02275 02276 void MicrosoftMangleContextImpl::mangleCXXRTTIBaseClassDescriptor( 02277 const CXXRecordDecl *Derived, uint32_t NVOffset, int32_t VBPtrOffset, 02278 uint32_t VBTableOffset, uint32_t Flags, raw_ostream &Out) { 02279 MicrosoftCXXNameMangler Mangler(*this, Out); 02280 Mangler.getStream() << "\01??_R1"; 02281 Mangler.mangleNumber(NVOffset); 02282 Mangler.mangleNumber(VBPtrOffset); 02283 Mangler.mangleNumber(VBTableOffset); 02284 Mangler.mangleNumber(Flags); 02285 Mangler.mangleName(Derived); 02286 Mangler.getStream() << "8"; 02287 } 02288 02289 void MicrosoftMangleContextImpl::mangleCXXRTTIBaseClassArray( 02290 const CXXRecordDecl *Derived, raw_ostream &Out) { 02291 MicrosoftCXXNameMangler Mangler(*this, Out); 02292 Mangler.getStream() << "\01??_R2"; 02293 Mangler.mangleName(Derived); 02294 Mangler.getStream() << "8"; 02295 } 02296 02297 void MicrosoftMangleContextImpl::mangleCXXRTTIClassHierarchyDescriptor( 02298 const CXXRecordDecl *Derived, raw_ostream &Out) { 02299 MicrosoftCXXNameMangler Mangler(*this, Out); 02300 Mangler.getStream() << "\01??_R3"; 02301 Mangler.mangleName(Derived); 02302 Mangler.getStream() << "8"; 02303 } 02304 02305 void MicrosoftMangleContextImpl::mangleCXXRTTICompleteObjectLocator( 02306 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath, 02307 raw_ostream &Out) { 02308 // <mangled-name> ::= ?_R4 <class-name> <storage-class> 02309 // <cvr-qualifiers> [<name>] @ 02310 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class> 02311 // is always '6' for vftables. 02312 MicrosoftCXXNameMangler Mangler(*this, Out); 02313 Mangler.getStream() << "\01??_R4"; 02314 Mangler.mangleName(Derived); 02315 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const. 02316 for (const CXXRecordDecl *RD : BasePath) 02317 Mangler.mangleName(RD); 02318 Mangler.getStream() << '@'; 02319 } 02320 02321 void MicrosoftMangleContextImpl::mangleTypeName(QualType T, raw_ostream &Out) { 02322 // This is just a made up unique string for the purposes of tbaa. undname 02323 // does *not* know how to demangle it. 02324 MicrosoftCXXNameMangler Mangler(*this, Out); 02325 Mangler.getStream() << '?'; 02326 Mangler.mangleType(T, SourceRange()); 02327 } 02328 02329 void MicrosoftMangleContextImpl::mangleCXXCtor(const CXXConstructorDecl *D, 02330 CXXCtorType Type, 02331 raw_ostream &Out) { 02332 MicrosoftCXXNameMangler mangler(*this, Out); 02333 mangler.mangle(D); 02334 } 02335 02336 void MicrosoftMangleContextImpl::mangleCXXDtor(const CXXDestructorDecl *D, 02337 CXXDtorType Type, 02338 raw_ostream &Out) { 02339 MicrosoftCXXNameMangler mangler(*this, Out, D, Type); 02340 mangler.mangle(D); 02341 } 02342 02343 void MicrosoftMangleContextImpl::mangleReferenceTemporary(const VarDecl *VD, 02344 unsigned, 02345 raw_ostream &) { 02346 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, 02347 "cannot mangle this reference temporary yet"); 02348 getDiags().Report(VD->getLocation(), DiagID); 02349 } 02350 02351 void MicrosoftMangleContextImpl::mangleStaticGuardVariable(const VarDecl *VD, 02352 raw_ostream &Out) { 02353 // TODO: This is not correct, especially with respect to VS "14". VS "14" 02354 // utilizes thread local variables to implement thread safe, re-entrant 02355 // initialization for statics. They no longer differentiate between an 02356 // externally visible and non-externally visible static with respect to 02357 // mangling, they all get $TSS <number>. 02358 // 02359 // N.B. This means that they can get more than 32 static variable guards in a 02360 // scope. It also means that they broke compatibility with their own ABI. 02361 02362 // <guard-name> ::= ?_B <postfix> @5 <scope-depth> 02363 // ::= ?$S <guard-num> @ <postfix> @4IA 02364 02365 // The first mangling is what MSVC uses to guard static locals in inline 02366 // functions. It uses a different mangling in external functions to support 02367 // guarding more than 32 variables. MSVC rejects inline functions with more 02368 // than 32 static locals. We don't fully implement the second mangling 02369 // because those guards are not externally visible, and instead use LLVM's 02370 // default renaming when creating a new guard variable. 02371 MicrosoftCXXNameMangler Mangler(*this, Out); 02372 02373 bool Visible = VD->isExternallyVisible(); 02374 // <operator-name> ::= ?_B # local static guard 02375 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@"); 02376 unsigned ScopeDepth = 0; 02377 if (Visible && !getNextDiscriminator(VD, ScopeDepth)) 02378 // If we do not have a discriminator and are emitting a guard variable for 02379 // use at global scope, then mangling the nested name will not be enough to 02380 // remove ambiguities. 02381 Mangler.mangle(VD, ""); 02382 else 02383 Mangler.mangleNestedName(VD); 02384 Mangler.getStream() << (Visible ? "@5" : "@4IA"); 02385 if (ScopeDepth) 02386 Mangler.mangleNumber(ScopeDepth); 02387 } 02388 02389 void MicrosoftMangleContextImpl::mangleInitFiniStub(const VarDecl *D, 02390 raw_ostream &Out, 02391 char CharCode) { 02392 MicrosoftCXXNameMangler Mangler(*this, Out); 02393 Mangler.getStream() << "\01??__" << CharCode; 02394 Mangler.mangleName(D); 02395 if (D->isStaticDataMember()) { 02396 Mangler.mangleVariableEncoding(D); 02397 Mangler.getStream() << '@'; 02398 } 02399 // This is the function class mangling. These stubs are global, non-variadic, 02400 // cdecl functions that return void and take no args. 02401 Mangler.getStream() << "YAXXZ"; 02402 } 02403 02404 void MicrosoftMangleContextImpl::mangleDynamicInitializer(const VarDecl *D, 02405 raw_ostream &Out) { 02406 // <initializer-name> ::= ?__E <name> YAXXZ 02407 mangleInitFiniStub(D, Out, 'E'); 02408 } 02409 02410 void 02411 MicrosoftMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D, 02412 raw_ostream &Out) { 02413 // <destructor-name> ::= ?__F <name> YAXXZ 02414 mangleInitFiniStub(D, Out, 'F'); 02415 } 02416 02417 void MicrosoftMangleContextImpl::mangleStringLiteral(const StringLiteral *SL, 02418 raw_ostream &Out) { 02419 // <char-type> ::= 0 # char 02420 // ::= 1 # wchar_t 02421 // ::= ??? # char16_t/char32_t will need a mangling too... 02422 // 02423 // <literal-length> ::= <non-negative integer> # the length of the literal 02424 // 02425 // <encoded-crc> ::= <hex digit>+ @ # crc of the literal including 02426 // # null-terminator 02427 // 02428 // <encoded-string> ::= <simple character> # uninteresting character 02429 // ::= '?$' <hex digit> <hex digit> # these two nibbles 02430 // # encode the byte for the 02431 // # character 02432 // ::= '?' [a-z] # \xe1 - \xfa 02433 // ::= '?' [A-Z] # \xc1 - \xda 02434 // ::= '?' [0-9] # [,/\:. \n\t'-] 02435 // 02436 // <literal> ::= '??_C@_' <char-type> <literal-length> <encoded-crc> 02437 // <encoded-string> '@' 02438 MicrosoftCXXNameMangler Mangler(*this, Out); 02439 Mangler.getStream() << "\01??_C@_"; 02440 02441 // <char-type>: The "kind" of string literal is encoded into the mangled name. 02442 // TODO: This needs to be updated when MSVC gains support for unicode 02443 // literals. 02444 if (SL->isAscii()) 02445 Mangler.getStream() << '0'; 02446 else if (SL->isWide()) 02447 Mangler.getStream() << '1'; 02448 else 02449 llvm_unreachable("unexpected string literal kind!"); 02450 02451 // <literal-length>: The next part of the mangled name consists of the length 02452 // of the string. 02453 // The StringLiteral does not consider the NUL terminator byte(s) but the 02454 // mangling does. 02455 // N.B. The length is in terms of bytes, not characters. 02456 Mangler.mangleNumber(SL->getByteLength() + SL->getCharByteWidth()); 02457 02458 // We will use the "Rocksoft^tm Model CRC Algorithm" to describe the 02459 // properties of our CRC: 02460 // Width : 32 02461 // Poly : 04C11DB7 02462 // Init : FFFFFFFF 02463 // RefIn : True 02464 // RefOut : True 02465 // XorOut : 00000000 02466 // Check : 340BC6D9 02467 uint32_t CRC = 0xFFFFFFFFU; 02468 02469 auto UpdateCRC = [&CRC](char Byte) { 02470 for (unsigned i = 0; i < 8; ++i) { 02471 bool Bit = CRC & 0x80000000U; 02472 if (Byte & (1U << i)) 02473 Bit = !Bit; 02474 CRC <<= 1; 02475 if (Bit) 02476 CRC ^= 0x04C11DB7U; 02477 } 02478 }; 02479 02480 auto GetLittleEndianByte = [&Mangler, &SL](unsigned Index) { 02481 unsigned CharByteWidth = SL->getCharByteWidth(); 02482 uint32_t CodeUnit = SL->getCodeUnit(Index / CharByteWidth); 02483 unsigned OffsetInCodeUnit = Index % CharByteWidth; 02484 return static_cast<char>((CodeUnit >> (8 * OffsetInCodeUnit)) & 0xff); 02485 }; 02486 02487 auto GetBigEndianByte = [&Mangler, &SL](unsigned Index) { 02488 unsigned CharByteWidth = SL->getCharByteWidth(); 02489 uint32_t CodeUnit = SL->getCodeUnit(Index / CharByteWidth); 02490 unsigned OffsetInCodeUnit = (CharByteWidth - 1) - (Index % CharByteWidth); 02491 return static_cast<char>((CodeUnit >> (8 * OffsetInCodeUnit)) & 0xff); 02492 }; 02493 02494 // CRC all the bytes of the StringLiteral. 02495 for (unsigned I = 0, E = SL->getByteLength(); I != E; ++I) 02496 UpdateCRC(GetLittleEndianByte(I)); 02497 02498 // The NUL terminator byte(s) were not present earlier, 02499 // we need to manually process those bytes into the CRC. 02500 for (unsigned NullTerminator = 0; NullTerminator < SL->getCharByteWidth(); 02501 ++NullTerminator) 02502 UpdateCRC('\x00'); 02503 02504 // The literature refers to the process of reversing the bits in the final CRC 02505 // output as "reflection". 02506 CRC = llvm::reverseBits(CRC); 02507 02508 // <encoded-crc>: The CRC is encoded utilizing the standard number mangling 02509 // scheme. 02510 Mangler.mangleNumber(CRC); 02511 02512 // <encoded-string>: The mangled name also contains the first 32 _characters_ 02513 // (including null-terminator bytes) of the StringLiteral. 02514 // Each character is encoded by splitting them into bytes and then encoding 02515 // the constituent bytes. 02516 auto MangleByte = [&Mangler](char Byte) { 02517 // There are five different manglings for characters: 02518 // - [a-zA-Z0-9_$]: A one-to-one mapping. 02519 // - ?[a-z]: The range from \xe1 to \xfa. 02520 // - ?[A-Z]: The range from \xc1 to \xda. 02521 // - ?[0-9]: The set of [,/\:. \n\t'-]. 02522 // - ?$XX: A fallback which maps nibbles. 02523 if (isIdentifierBody(Byte, /*AllowDollar=*/true)) { 02524 Mangler.getStream() << Byte; 02525 } else if (isLetter(Byte & 0x7f)) { 02526 Mangler.getStream() << '?' << static_cast<char>(Byte & 0x7f); 02527 } else { 02528 switch (Byte) { 02529 case ',': 02530 Mangler.getStream() << "?0"; 02531 break; 02532 case '/': 02533 Mangler.getStream() << "?1"; 02534 break; 02535 case '\\': 02536 Mangler.getStream() << "?2"; 02537 break; 02538 case ':': 02539 Mangler.getStream() << "?3"; 02540 break; 02541 case '.': 02542 Mangler.getStream() << "?4"; 02543 break; 02544 case ' ': 02545 Mangler.getStream() << "?5"; 02546 break; 02547 case '\n': 02548 Mangler.getStream() << "?6"; 02549 break; 02550 case '\t': 02551 Mangler.getStream() << "?7"; 02552 break; 02553 case '\'': 02554 Mangler.getStream() << "?8"; 02555 break; 02556 case '-': 02557 Mangler.getStream() << "?9"; 02558 break; 02559 default: 02560 Mangler.getStream() << "?$"; 02561 Mangler.getStream() << static_cast<char>('A' + ((Byte >> 4) & 0xf)); 02562 Mangler.getStream() << static_cast<char>('A' + (Byte & 0xf)); 02563 break; 02564 } 02565 } 02566 }; 02567 02568 // Enforce our 32 character max. 02569 unsigned NumCharsToMangle = std::min(32U, SL->getLength()); 02570 for (unsigned I = 0, E = NumCharsToMangle * SL->getCharByteWidth(); I != E; 02571 ++I) 02572 MangleByte(GetBigEndianByte(I)); 02573 02574 // Encode the NUL terminator if there is room. 02575 if (NumCharsToMangle < 32) 02576 for (unsigned NullTerminator = 0; NullTerminator < SL->getCharByteWidth(); 02577 ++NullTerminator) 02578 MangleByte(0); 02579 02580 Mangler.getStream() << '@'; 02581 } 02582 02583 MicrosoftMangleContext * 02584 MicrosoftMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags) { 02585 return new MicrosoftMangleContextImpl(Context, Diags); 02586 }