Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 1 | //===--- MicrosoftCXXABI.cpp - Emit LLVM Code from ASTs for a Module ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This provides C++ code generation targetting the Microsoft Visual C++ ABI. |
| 11 | // The class in this file generates structures that follow the Microsoft |
| 12 | // Visual C++ ABI, which is actually not very well documented at all outside |
| 13 | // of Microsoft. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "CGCXXABI.h" |
| 18 | #include "CodeGenModule.h" |
| 19 | #include "Mangle.h" |
| 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "clang/AST/Decl.h" |
| 22 | #include "clang/AST/DeclCXX.h" |
| 23 | #include "clang/AST/DeclTemplate.h" |
| 24 | #include "clang/AST/ExprCXX.h" |
| 25 | #include "CGVTables.h" |
| 26 | |
| 27 | using namespace clang; |
| 28 | using namespace CodeGen; |
| 29 | |
| 30 | namespace { |
| 31 | |
Charles Davis | e60cea8 | 2010-06-11 03:07:32 +0000 | [diff] [blame] | 32 | /// MicrosoftCXXNameMangler - Manage the mangling of a single name for the |
| 33 | /// Microsoft Visual C++ ABI. |
| 34 | class MicrosoftCXXNameMangler { |
| 35 | MangleContext &Context; |
| 36 | llvm::raw_svector_ostream Out; |
| 37 | |
| 38 | ASTContext &getASTContext() const { return Context.getASTContext(); } |
| 39 | |
| 40 | public: |
| 41 | MicrosoftCXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res) |
| 42 | : Context(C), Out(Res) { } |
| 43 | |
| 44 | llvm::raw_svector_ostream &getStream() { return Out; } |
| 45 | |
| 46 | void mangle(const NamedDecl *D, llvm::StringRef Prefix = "?"); |
| 47 | void mangleName(const NamedDecl *ND); |
Charles Davis | 65161d1 | 2010-06-16 05:33:16 +0000 | [diff] [blame] | 48 | void mangleFunctionEncoding(const FunctionDecl *FD); |
Charles Davis | b021f8b | 2010-06-14 05:29:01 +0000 | [diff] [blame] | 49 | void mangleVariableEncoding(const VarDecl *VD); |
Charles Davis | c62458f | 2010-06-18 07:51:00 +0000 | [diff] [blame] | 50 | void mangleNumber(int64_t Number); |
Charles Davis | 570d276 | 2010-06-12 08:11:16 +0000 | [diff] [blame] | 51 | void mangleType(QualType T); |
Charles Davis | e60cea8 | 2010-06-11 03:07:32 +0000 | [diff] [blame] | 52 | |
| 53 | private: |
| 54 | void mangleUnqualifiedName(const NamedDecl *ND) { |
| 55 | mangleUnqualifiedName(ND, ND->getDeclName()); |
| 56 | } |
| 57 | void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name); |
| 58 | void mangleSourceName(const IdentifierInfo *II); |
| 59 | void manglePostfix(const DeclContext *DC, bool NoFunction=false); |
Charles Davis | 21e2a7e | 2010-06-17 06:47:31 +0000 | [diff] [blame] | 60 | void mangleOperatorName(OverloadedOperatorKind OO); |
Charles Davis | b021f8b | 2010-06-14 05:29:01 +0000 | [diff] [blame] | 61 | void mangleQualifiers(Qualifiers Quals, bool IsMember); |
Charles Davis | e60cea8 | 2010-06-11 03:07:32 +0000 | [diff] [blame] | 62 | |
| 63 | void mangleObjCMethodName(const ObjCMethodDecl *MD); |
| 64 | |
Charles Davis | 570d276 | 2010-06-12 08:11:16 +0000 | [diff] [blame] | 65 | // Declare manglers for every type class. |
| 66 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 67 | #define NON_CANONICAL_TYPE(CLASS, PARENT) |
| 68 | #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T); |
| 69 | #include "clang/AST/TypeNodes.def" |
| 70 | |
Charles Davis | c62458f | 2010-06-18 07:51:00 +0000 | [diff] [blame] | 71 | void mangleType(const TagType*); |
Charles Davis | 3a0d41d | 2010-06-26 03:50:05 +0000 | [diff] [blame] | 72 | void mangleType(const FunctionType *T, bool IsStructor, bool IsInstMethod); |
Charles Davis | 65161d1 | 2010-06-16 05:33:16 +0000 | [diff] [blame] | 73 | void mangleFunctionClass(const FunctionDecl *FD); |
| 74 | void mangleCallingConvention(const FunctionType *T); |
| 75 | void mangleThrowSpecification(const FunctionProtoType *T); |
| 76 | |
Charles Davis | e60cea8 | 2010-06-11 03:07:32 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 79 | /// MicrosoftMangleContext - Overrides the default MangleContext for the |
| 80 | /// Microsoft Visual C++ ABI. |
| 81 | class MicrosoftMangleContext : public MangleContext { |
| 82 | public: |
| 83 | MicrosoftMangleContext(ASTContext &Context, |
| 84 | Diagnostic &Diags) : MangleContext(Context, Diags) { } |
Charles Davis | 971154d | 2010-06-11 04:25:47 +0000 | [diff] [blame] | 85 | virtual bool shouldMangleDeclName(const NamedDecl *D); |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 86 | virtual void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &); |
| 87 | virtual void mangleThunk(const CXXMethodDecl *MD, |
| 88 | const ThunkInfo &Thunk, |
| 89 | llvm::SmallVectorImpl<char> &); |
| 90 | virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, |
| 91 | const ThisAdjustment &ThisAdjustment, |
| 92 | llvm::SmallVectorImpl<char> &); |
| 93 | virtual void mangleGuardVariable(const VarDecl *D, |
| 94 | llvm::SmallVectorImpl<char> &); |
| 95 | virtual void mangleCXXVTable(const CXXRecordDecl *RD, |
| 96 | llvm::SmallVectorImpl<char> &); |
| 97 | virtual void mangleCXXVTT(const CXXRecordDecl *RD, |
| 98 | llvm::SmallVectorImpl<char> &); |
| 99 | virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, |
| 100 | const CXXRecordDecl *Type, |
| 101 | llvm::SmallVectorImpl<char> &); |
| 102 | virtual void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &); |
| 103 | virtual void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &); |
| 104 | virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, |
| 105 | llvm::SmallVectorImpl<char> &); |
| 106 | virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, |
| 107 | llvm::SmallVectorImpl<char> &); |
| 108 | }; |
| 109 | |
| 110 | class MicrosoftCXXABI : public CXXABI { |
| 111 | MicrosoftMangleContext MangleCtx; |
| 112 | public: |
| 113 | MicrosoftCXXABI(CodeGenModule &CGM) |
| 114 | : MangleCtx(CGM.getContext(), CGM.getDiags()) {} |
| 115 | |
| 116 | MicrosoftMangleContext &getMangleContext() { |
| 117 | return MangleCtx; |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | } |
| 122 | |
Charles Davis | 971154d | 2010-06-11 04:25:47 +0000 | [diff] [blame] | 123 | static bool isInCLinkageSpecification(const Decl *D) { |
| 124 | D = D->getCanonicalDecl(); |
| 125 | for (const DeclContext *DC = D->getDeclContext(); |
| 126 | !DC->isTranslationUnit(); DC = DC->getParent()) { |
| 127 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) |
| 128 | return Linkage->getLanguage() == LinkageSpecDecl::lang_c; |
| 129 | } |
Charles Davis | 570d276 | 2010-06-12 08:11:16 +0000 | [diff] [blame] | 130 | |
Charles Davis | 971154d | 2010-06-11 04:25:47 +0000 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | |
| 134 | bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) { |
| 135 | // In C, functions with no attributes never need to be mangled. Fastpath them. |
| 136 | if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs()) |
| 137 | return false; |
| 138 | |
| 139 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 140 | // over all other naming in the .o file. |
| 141 | if (D->hasAttr<AsmLabelAttr>()) |
| 142 | return true; |
| 143 | |
| 144 | // Clang's "overloadable" attribute extension to C/C++ implies name mangling |
| 145 | // (always) as does passing a C++ member function and a function |
| 146 | // whose name is not a simple identifier. |
| 147 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 148 | if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) || |
| 149 | !FD->getDeclName().isIdentifier())) |
| 150 | return true; |
| 151 | |
| 152 | // Otherwise, no mangling is done outside C++ mode. |
| 153 | if (!getASTContext().getLangOptions().CPlusPlus) |
| 154 | return false; |
| 155 | |
Charles Davis | 570d276 | 2010-06-12 08:11:16 +0000 | [diff] [blame] | 156 | // Variables at global scope with internal linkage are not mangled. |
| 157 | if (!FD) { |
| 158 | const DeclContext *DC = D->getDeclContext(); |
| 159 | if (DC->isTranslationUnit() && D->getLinkage() == InternalLinkage) |
| 160 | return false; |
| 161 | } |
| 162 | |
Charles Davis | 971154d | 2010-06-11 04:25:47 +0000 | [diff] [blame] | 163 | // C functions and "main" are not mangled. |
| 164 | if ((FD && FD->isMain()) || isInCLinkageSpecification(D)) |
| 165 | return false; |
| 166 | |
| 167 | return true; |
| 168 | } |
| 169 | |
Charles Davis | e60cea8 | 2010-06-11 03:07:32 +0000 | [diff] [blame] | 170 | void MicrosoftCXXNameMangler::mangle(const NamedDecl *D, |
| 171 | llvm::StringRef Prefix) { |
| 172 | // MSVC doesn't mangle C++ names the same way it mangles extern "C" names. |
| 173 | // Therefore it's really important that we don't decorate the |
| 174 | // name with leading underscores or leading/trailing at signs. So, emit a |
| 175 | // asm marker at the start so we get the name right. |
| 176 | Out << '\01'; // LLVM IR Marker for __asm("foo") |
| 177 | |
| 178 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 179 | // over all other naming in the .o file. |
| 180 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
| 181 | // If we have an asm name, then we use it as the mangling. |
| 182 | Out << ALA->getLabel(); |
| 183 | return; |
| 184 | } |
| 185 | |
Charles Davis | 65161d1 | 2010-06-16 05:33:16 +0000 | [diff] [blame] | 186 | // <mangled-name> ::= ? <name> <type-encoding> |
Charles Davis | e60cea8 | 2010-06-11 03:07:32 +0000 | [diff] [blame] | 187 | Out << Prefix; |
| 188 | mangleName(D); |
Charles Davis | 65161d1 | 2010-06-16 05:33:16 +0000 | [diff] [blame] | 189 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 190 | mangleFunctionEncoding(FD); |
| 191 | else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
Charles Davis | b021f8b | 2010-06-14 05:29:01 +0000 | [diff] [blame] | 192 | mangleVariableEncoding(VD); |
Charles Davis | 65161d1 | 2010-06-16 05:33:16 +0000 | [diff] [blame] | 193 | // TODO: Fields? Can MSVC even mangle them? |
| 194 | } |
| 195 | |
| 196 | void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { |
| 197 | // <type-encoding> ::= <function-class> <function-type> |
| 198 | |
| 199 | // Don't mangle in the type if this isn't a decl we should typically mangle. |
| 200 | if (!Context.shouldMangleDeclName(FD)) |
| 201 | return; |
| 202 | |
| 203 | // We should never ever see a FunctionNoProtoType at this point. |
| 204 | // We don't even know how to mangle their types anyway :). |
Charles Davis | 3a0d41d | 2010-06-26 03:50:05 +0000 | [diff] [blame] | 205 | const FunctionProtoType *FT = cast<FunctionProtoType>(FD->getType()); |
Charles Davis | 65161d1 | 2010-06-16 05:33:16 +0000 | [diff] [blame] | 206 | |
Charles Davis | 3a0d41d | 2010-06-26 03:50:05 +0000 | [diff] [blame] | 207 | bool InStructor = false, InInstMethod = false; |
Charles Davis | 65161d1 | 2010-06-16 05:33:16 +0000 | [diff] [blame] | 208 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 209 | if (MD) { |
Charles Davis | 3a0d41d | 2010-06-26 03:50:05 +0000 | [diff] [blame] | 210 | if (MD->isInstance()) |
| 211 | InInstMethod = true; |
Charles Davis | 65161d1 | 2010-06-16 05:33:16 +0000 | [diff] [blame] | 212 | if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) |
| 213 | InStructor = true; |
| 214 | } |
| 215 | |
| 216 | // First, the function class. |
| 217 | mangleFunctionClass(FD); |
| 218 | |
Charles Davis | 3a0d41d | 2010-06-26 03:50:05 +0000 | [diff] [blame] | 219 | mangleType(FT, InStructor, InInstMethod); |
Charles Davis | b021f8b | 2010-06-14 05:29:01 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) { |
Charles Davis | 65161d1 | 2010-06-16 05:33:16 +0000 | [diff] [blame] | 223 | // <type-encoding> ::= <storage-class> <variable-type> |
Charles Davis | b021f8b | 2010-06-14 05:29:01 +0000 | [diff] [blame] | 224 | // <storage-class> ::= 0 # private static member |
| 225 | // ::= 1 # protected static member |
| 226 | // ::= 2 # public static member |
| 227 | // ::= 3 # global |
| 228 | // ::= 4 # static local |
| 229 | |
| 230 | // The first character in the encoding (after the name) is the storage class. |
| 231 | if (VD->isStaticDataMember()) { |
| 232 | // If it's a static member, it also encodes the access level. |
| 233 | switch (VD->getAccess()) { |
| 234 | default: |
| 235 | case AS_private: Out << '0'; break; |
| 236 | case AS_protected: Out << '1'; break; |
| 237 | case AS_public: Out << '2'; break; |
| 238 | } |
| 239 | } |
| 240 | else if (!VD->isStaticLocal()) |
| 241 | Out << '3'; |
| 242 | else |
| 243 | Out << '4'; |
| 244 | // Now mangle the type. |
| 245 | // <variable-type> ::= <type> <cvr-qualifiers> |
Charles Davis | 3a0d41d | 2010-06-26 03:50:05 +0000 | [diff] [blame] | 246 | // ::= <type> A # pointers and references |
| 247 | // Pointers and references are odd. The type of 'int * const foo;' gets |
| 248 | // mangled as 'QAHA' instead of 'PAHB', for example. |
Charles Davis | b021f8b | 2010-06-14 05:29:01 +0000 | [diff] [blame] | 249 | QualType Ty = VD->getType(); |
Charles Davis | 3a0d41d | 2010-06-26 03:50:05 +0000 | [diff] [blame] | 250 | if (Ty->isPointerType() || Ty->isReferenceType()) { |
| 251 | mangleType(Ty); |
| 252 | Out << 'A'; |
| 253 | } else { |
| 254 | mangleType(Ty.getLocalUnqualifiedType()); |
| 255 | mangleQualifiers(Ty.getLocalQualifiers(), false); |
| 256 | } |
Charles Davis | e60cea8 | 2010-06-11 03:07:32 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) { |
| 260 | // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @ |
| 261 | const DeclContext *DC = ND->getDeclContext(); |
| 262 | |
| 263 | // Always start with the unqualified name. |
| 264 | mangleUnqualifiedName(ND); |
| 265 | |
| 266 | // If this is an extern variable declared locally, the relevant DeclContext |
| 267 | // is that of the containing namespace, or the translation unit. |
| 268 | if (isa<FunctionDecl>(DC) && ND->hasLinkage()) |
| 269 | while (!DC->isNamespace() && !DC->isTranslationUnit()) |
| 270 | DC = DC->getParent(); |
| 271 | |
| 272 | manglePostfix(DC); |
| 273 | |
| 274 | // Terminate the whole name with an '@'. |
| 275 | Out << '@'; |
| 276 | } |
| 277 | |
Charles Davis | c62458f | 2010-06-18 07:51:00 +0000 | [diff] [blame] | 278 | void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) { |
| 279 | // <number> ::= [?] <decimal digit> # <= 9 |
| 280 | // ::= [?] <hex digit>+ @ # > 9; A = 0, B = 1, etc... |
| 281 | if (Number < 0) { |
| 282 | Out << '?'; |
| 283 | Number = -Number; |
| 284 | } |
| 285 | if (Number <= 9) { |
| 286 | Out << Number; |
| 287 | } else { |
| 288 | // We have to build up the encoding in reverse order, so it will come |
| 289 | // out right when we write it out. |
| 290 | char Encoding[16]; |
| 291 | char *EndPtr = Encoding+sizeof(Encoding); |
| 292 | char *CurPtr = EndPtr; |
| 293 | while (Number) { |
| 294 | *--CurPtr = 'A' + (Number % 16); |
| 295 | Number /= 16; |
| 296 | } |
| 297 | Out.write(CurPtr, EndPtr-CurPtr); |
| 298 | Out << '@'; |
| 299 | } |
| 300 | } |
| 301 | |
Charles Davis | e60cea8 | 2010-06-11 03:07:32 +0000 | [diff] [blame] | 302 | void |
| 303 | MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, |
| 304 | DeclarationName Name) { |
| 305 | // <unqualified-name> ::= <operator-name> |
| 306 | // ::= <ctor-dtor-name> |
| 307 | // ::= <source-name> |
| 308 | switch (Name.getNameKind()) { |
| 309 | case DeclarationName::Identifier: { |
| 310 | if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { |
| 311 | mangleSourceName(II); |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | // Otherwise, an anonymous entity. We must have a declaration. |
| 316 | assert(ND && "mangling empty name without declaration"); |
| 317 | |
| 318 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { |
| 319 | if (NS->isAnonymousNamespace()) { |
| 320 | Out << "?A"; |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // We must have an anonymous struct. |
| 326 | const TagDecl *TD = cast<TagDecl>(ND); |
| 327 | if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) { |
| 328 | assert(TD->getDeclContext() == D->getDeclContext() && |
| 329 | "Typedef should not be in another decl context!"); |
| 330 | assert(D->getDeclName().getAsIdentifierInfo() && |
| 331 | "Typedef was not named!"); |
| 332 | mangleSourceName(D->getDeclName().getAsIdentifierInfo()); |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | // TODO: How does VC mangle anonymous structs? |
| 337 | assert(false && "Don't know how to mangle anonymous types yet!"); |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | case DeclarationName::ObjCZeroArgSelector: |
| 342 | case DeclarationName::ObjCOneArgSelector: |
| 343 | case DeclarationName::ObjCMultiArgSelector: |
| 344 | assert(false && "Can't mangle Objective-C selector names here!"); |
| 345 | break; |
| 346 | |
| 347 | case DeclarationName::CXXConstructorName: |
| 348 | assert(false && "Can't mangle constructors yet!"); |
| 349 | break; |
| 350 | |
| 351 | case DeclarationName::CXXDestructorName: |
| 352 | assert(false && "Can't mangle destructors yet!"); |
| 353 | break; |
| 354 | |
| 355 | case DeclarationName::CXXConversionFunctionName: |
| 356 | // <operator-name> ::= ?B # (cast) |
| 357 | // The target type is encoded as the return type. |
| 358 | Out << "?B"; |
| 359 | break; |
| 360 | |
| 361 | case DeclarationName::CXXOperatorName: |
Charles Davis | 21e2a7e | 2010-06-17 06:47:31 +0000 | [diff] [blame] | 362 | mangleOperatorName(Name.getCXXOverloadedOperator()); |
| 363 | break; |
Charles Davis | e60cea8 | 2010-06-11 03:07:32 +0000 | [diff] [blame] | 364 | |
| 365 | case DeclarationName::CXXLiteralOperatorName: |
| 366 | // FIXME: Was this added in VS2010? Does MS even know how to mangle this? |
| 367 | assert(false && "Don't know how to mangle literal operators yet!"); |
| 368 | break; |
| 369 | |
| 370 | case DeclarationName::CXXUsingDirective: |
| 371 | assert(false && "Can't mangle a using directive name!"); |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC, |
| 377 | bool NoFunction) { |
| 378 | // <postfix> ::= <unqualified-name> [<postfix>] |
| 379 | // ::= <template-postfix> <template-args> [<postfix>] |
| 380 | // ::= <template-param> |
| 381 | // ::= <substitution> [<postfix>] |
| 382 | |
| 383 | if (!DC) return; |
| 384 | |
| 385 | while (isa<LinkageSpecDecl>(DC)) |
| 386 | DC = DC->getParent(); |
| 387 | |
| 388 | if (DC->isTranslationUnit()) |
| 389 | return; |
| 390 | |
| 391 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) { |
| 392 | llvm::SmallString<64> Name; |
Fariborz Jahanian | 564360b | 2010-06-24 00:08:06 +0000 | [diff] [blame] | 393 | Context.mangleBlock(GlobalDecl(), BD, Name); |
Charles Davis | e60cea8 | 2010-06-11 03:07:32 +0000 | [diff] [blame] | 394 | Out << Name << '@'; |
| 395 | return manglePostfix(DC->getParent(), NoFunction); |
| 396 | } |
| 397 | |
| 398 | if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC))) |
| 399 | return; |
| 400 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) |
| 401 | mangleObjCMethodName(Method); |
| 402 | else { |
| 403 | mangleUnqualifiedName(cast<NamedDecl>(DC)); |
| 404 | manglePostfix(DC->getParent(), NoFunction); |
| 405 | } |
| 406 | } |
| 407 | |
Charles Davis | 21e2a7e | 2010-06-17 06:47:31 +0000 | [diff] [blame] | 408 | void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO) { |
| 409 | switch (OO) { |
| 410 | // ?0 # constructor |
| 411 | // ?1 # destructor |
| 412 | // <operator-name> ::= ?2 # new |
| 413 | case OO_New: Out << "?2"; break; |
| 414 | // <operator-name> ::= ?3 # delete |
| 415 | case OO_Delete: Out << "?3"; break; |
| 416 | // <operator-name> ::= ?4 # = |
| 417 | case OO_Equal: Out << "?4"; break; |
| 418 | // <operator-name> ::= ?5 # >> |
| 419 | case OO_GreaterGreater: Out << "?5"; break; |
| 420 | // <operator-name> ::= ?6 # << |
| 421 | case OO_LessLess: Out << "?6"; break; |
| 422 | // <operator-name> ::= ?7 # ! |
| 423 | case OO_Exclaim: Out << "?7"; break; |
| 424 | // <operator-name> ::= ?8 # == |
| 425 | case OO_EqualEqual: Out << "?8"; break; |
| 426 | // <operator-name> ::= ?9 # != |
| 427 | case OO_ExclaimEqual: Out << "?9"; break; |
| 428 | // <operator-name> ::= ?A # [] |
| 429 | case OO_Subscript: Out << "?A"; break; |
| 430 | // ?B # conversion |
| 431 | // <operator-name> ::= ?C # -> |
| 432 | case OO_Arrow: Out << "?C"; break; |
| 433 | // <operator-name> ::= ?D # * |
| 434 | case OO_Star: Out << "?D"; break; |
| 435 | // <operator-name> ::= ?E # ++ |
| 436 | case OO_PlusPlus: Out << "?E"; break; |
| 437 | // <operator-name> ::= ?F # -- |
| 438 | case OO_MinusMinus: Out << "?F"; break; |
| 439 | // <operator-name> ::= ?G # - |
| 440 | case OO_Minus: Out << "?G"; break; |
| 441 | // <operator-name> ::= ?H # + |
| 442 | case OO_Plus: Out << "?H"; break; |
| 443 | // <operator-name> ::= ?I # & |
| 444 | case OO_Amp: Out << "?I"; break; |
| 445 | // <operator-name> ::= ?J # ->* |
| 446 | case OO_ArrowStar: Out << "?J"; break; |
| 447 | // <operator-name> ::= ?K # / |
| 448 | case OO_Slash: Out << "?K"; break; |
| 449 | // <operator-name> ::= ?L # % |
| 450 | case OO_Percent: Out << "?L"; break; |
| 451 | // <operator-name> ::= ?M # < |
| 452 | case OO_Less: Out << "?M"; break; |
| 453 | // <operator-name> ::= ?N # <= |
| 454 | case OO_LessEqual: Out << "?N"; break; |
| 455 | // <operator-name> ::= ?O # > |
| 456 | case OO_Greater: Out << "?O"; break; |
| 457 | // <operator-name> ::= ?P # >= |
| 458 | case OO_GreaterEqual: Out << "?P"; break; |
| 459 | // <operator-name> ::= ?Q # , |
| 460 | case OO_Comma: Out << "?Q"; break; |
| 461 | // <operator-name> ::= ?R # () |
| 462 | case OO_Call: Out << "?R"; break; |
| 463 | // <operator-name> ::= ?S # ~ |
| 464 | case OO_Tilde: Out << "?S"; break; |
| 465 | // <operator-name> ::= ?T # ^ |
| 466 | case OO_Caret: Out << "?T"; break; |
| 467 | // <operator-name> ::= ?U # | |
| 468 | case OO_Pipe: Out << "?U"; break; |
| 469 | // <operator-name> ::= ?V # && |
| 470 | case OO_AmpAmp: Out << "?V"; break; |
| 471 | // <operator-name> ::= ?W # || |
| 472 | case OO_PipePipe: Out << "?W"; break; |
| 473 | // <operator-name> ::= ?X # *= |
| 474 | case OO_StarEqual: Out << "?X"; break; |
| 475 | // <operator-name> ::= ?Y # += |
| 476 | case OO_PlusEqual: Out << "?Y"; break; |
| 477 | // <operator-name> ::= ?Z # -= |
| 478 | case OO_MinusEqual: Out << "?Z"; break; |
| 479 | // <operator-name> ::= ?_0 # /= |
| 480 | case OO_SlashEqual: Out << "?_0"; break; |
| 481 | // <operator-name> ::= ?_1 # %= |
| 482 | case OO_PercentEqual: Out << "?_1"; break; |
| 483 | // <operator-name> ::= ?_2 # >>= |
| 484 | case OO_GreaterGreaterEqual: Out << "?_2"; break; |
| 485 | // <operator-name> ::= ?_3 # <<= |
| 486 | case OO_LessLessEqual: Out << "?_3"; break; |
| 487 | // <operator-name> ::= ?_4 # &= |
| 488 | case OO_AmpEqual: Out << "?_4"; break; |
| 489 | // <operator-name> ::= ?_5 # |= |
| 490 | case OO_PipeEqual: Out << "?_5"; break; |
| 491 | // <operator-name> ::= ?_6 # ^= |
| 492 | case OO_CaretEqual: Out << "?_6"; break; |
| 493 | // ?_7 # vftable |
| 494 | // ?_8 # vbtable |
| 495 | // ?_9 # vcall |
| 496 | // ?_A # typeof |
| 497 | // ?_B # local static guard |
| 498 | // ?_C # string |
| 499 | // ?_D # vbase destructor |
| 500 | // ?_E # vector deleting destructor |
| 501 | // ?_F # default constructor closure |
| 502 | // ?_G # scalar deleting destructor |
| 503 | // ?_H # vector constructor iterator |
| 504 | // ?_I # vector destructor iterator |
| 505 | // ?_J # vector vbase constructor iterator |
| 506 | // ?_K # virtual displacement map |
| 507 | // ?_L # eh vector constructor iterator |
| 508 | // ?_M # eh vector destructor iterator |
| 509 | // ?_N # eh vector vbase constructor iterator |
| 510 | // ?_O # copy constructor closure |
| 511 | // ?_P<name> # udt returning <name> |
| 512 | // ?_Q # <unknown> |
| 513 | // ?_R0 # RTTI Type Descriptor |
| 514 | // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d) |
| 515 | // ?_R2 # RTTI Base Class Array |
| 516 | // ?_R3 # RTTI Class Hierarchy Descriptor |
| 517 | // ?_R4 # RTTI Complete Object Locator |
| 518 | // ?_S # local vftable |
| 519 | // ?_T # local vftable constructor closure |
| 520 | // <operator-name> ::= ?_U # new[] |
| 521 | case OO_Array_New: Out << "?_U"; break; |
| 522 | // <operator-name> ::= ?_V # delete[] |
| 523 | case OO_Array_Delete: Out << "?_V"; break; |
| 524 | |
| 525 | case OO_Conditional: |
| 526 | assert(false && "Don't know how to mangle ?:"); |
| 527 | break; |
| 528 | |
| 529 | case OO_None: |
| 530 | case NUM_OVERLOADED_OPERATORS: |
| 531 | assert(false && "Not an overloaded operator"); |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | |
Charles Davis | e60cea8 | 2010-06-11 03:07:32 +0000 | [diff] [blame] | 536 | void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) { |
| 537 | // <source name> ::= <identifier> @ |
| 538 | Out << II->getName() << '@'; |
| 539 | } |
| 540 | |
| 541 | void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { |
| 542 | llvm::SmallString<64> Buffer; |
| 543 | MiscNameMangler(Context, Buffer).mangleObjCMethodName(MD); |
| 544 | Out << Buffer; |
| 545 | } |
| 546 | |
Charles Davis | b021f8b | 2010-06-14 05:29:01 +0000 | [diff] [blame] | 547 | void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals, |
| 548 | bool IsMember) { |
| 549 | // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers> |
| 550 | // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only); |
| 551 | // 'I' means __restrict (32/64-bit). |
| 552 | // Note that the MSVC __restrict keyword isn't the same as the C99 restrict |
| 553 | // keyword! |
| 554 | // <base-cvr-qualifiers> ::= A # near |
| 555 | // ::= B # near const |
| 556 | // ::= C # near volatile |
| 557 | // ::= D # near const volatile |
| 558 | // ::= E # far (16-bit) |
| 559 | // ::= F # far const (16-bit) |
| 560 | // ::= G # far volatile (16-bit) |
| 561 | // ::= H # far const volatile (16-bit) |
| 562 | // ::= I # huge (16-bit) |
| 563 | // ::= J # huge const (16-bit) |
| 564 | // ::= K # huge volatile (16-bit) |
| 565 | // ::= L # huge const volatile (16-bit) |
| 566 | // ::= M <basis> # based |
| 567 | // ::= N <basis> # based const |
| 568 | // ::= O <basis> # based volatile |
| 569 | // ::= P <basis> # based const volatile |
| 570 | // ::= Q # near member |
| 571 | // ::= R # near const member |
| 572 | // ::= S # near volatile member |
| 573 | // ::= T # near const volatile member |
| 574 | // ::= U # far member (16-bit) |
| 575 | // ::= V # far const member (16-bit) |
| 576 | // ::= W # far volatile member (16-bit) |
| 577 | // ::= X # far const volatile member (16-bit) |
| 578 | // ::= Y # huge member (16-bit) |
| 579 | // ::= Z # huge const member (16-bit) |
| 580 | // ::= 0 # huge volatile member (16-bit) |
| 581 | // ::= 1 # huge const volatile member (16-bit) |
| 582 | // ::= 2 <basis> # based member |
| 583 | // ::= 3 <basis> # based const member |
| 584 | // ::= 4 <basis> # based volatile member |
| 585 | // ::= 5 <basis> # based const volatile member |
| 586 | // ::= 6 # near function (pointers only) |
| 587 | // ::= 7 # far function (pointers only) |
| 588 | // ::= 8 # near method (pointers only) |
| 589 | // ::= 9 # far method (pointers only) |
| 590 | // ::= _A <basis> # based function (pointers only) |
| 591 | // ::= _B <basis> # based function (far?) (pointers only) |
| 592 | // ::= _C <basis> # based method (pointers only) |
| 593 | // ::= _D <basis> # based method (far?) (pointers only) |
| 594 | // <basis> ::= 0 # __based(void) |
| 595 | // ::= 1 # __based(segment)? |
| 596 | // ::= 2 <name> # __based(name) |
| 597 | // ::= 3 # ? |
| 598 | // ::= 4 # ? |
| 599 | // ::= 5 # not really based |
| 600 | if (!IsMember) { |
| 601 | if (!Quals.hasVolatile()) { |
| 602 | if (!Quals.hasConst()) |
| 603 | Out << 'A'; |
| 604 | else |
| 605 | Out << 'B'; |
| 606 | } else { |
| 607 | if (!Quals.hasConst()) |
| 608 | Out << 'C'; |
| 609 | else |
| 610 | Out << 'D'; |
| 611 | } |
| 612 | } else { |
| 613 | if (!Quals.hasVolatile()) { |
| 614 | if (!Quals.hasConst()) |
| 615 | Out << 'Q'; |
| 616 | else |
| 617 | Out << 'R'; |
| 618 | } else { |
| 619 | if (!Quals.hasConst()) |
| 620 | Out << 'S'; |
| 621 | else |
| 622 | Out << 'T'; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | // FIXME: For now, just drop all extension qualifiers on the floor. |
| 627 | } |
| 628 | |
Charles Davis | 570d276 | 2010-06-12 08:11:16 +0000 | [diff] [blame] | 629 | void MicrosoftCXXNameMangler::mangleType(QualType T) { |
| 630 | // Only operate on the canonical type! |
| 631 | T = getASTContext().getCanonicalType(T); |
| 632 | |
Charles Davis | 3a0d41d | 2010-06-26 03:50:05 +0000 | [diff] [blame] | 633 | Qualifiers Quals = T.getLocalQualifiers(); |
| 634 | if (Quals) { |
| 635 | // We have to mangle these now, while we still have enough information. |
| 636 | // <pointer-cvr-qualifiers> ::= P # pointer |
| 637 | // ::= Q # const pointer |
| 638 | // ::= R # volatile pointer |
| 639 | // ::= S # const volatile pointer |
| 640 | if (T->isPointerType()) { |
| 641 | if (!Quals.hasVolatile()) { |
| 642 | Out << 'Q'; |
| 643 | } else { |
| 644 | if (!Quals.hasConst()) |
| 645 | Out << 'R'; |
| 646 | else |
| 647 | Out << 'S'; |
| 648 | } |
| 649 | } else |
| 650 | // Just emit qualifiers like normal. |
| 651 | // NB: When we mangle a pointer/reference type, and the pointee |
| 652 | // type has no qualifiers, the lack of qualifier gets mangled |
| 653 | // in there. |
| 654 | mangleQualifiers(Quals, false); |
| 655 | } |
| 656 | else if (T->isPointerType()) { |
| 657 | Out << 'P'; |
| 658 | } |
Charles Davis | 570d276 | 2010-06-12 08:11:16 +0000 | [diff] [blame] | 659 | switch (T->getTypeClass()) { |
| 660 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 661 | #define NON_CANONICAL_TYPE(CLASS, PARENT) \ |
| 662 | case Type::CLASS: \ |
| 663 | llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ |
| 664 | return; |
| 665 | #define TYPE(CLASS, PARENT) |
| 666 | #include "clang/AST/TypeNodes.def" |
| 667 | case Type::Builtin: |
| 668 | mangleType(static_cast<BuiltinType *>(T.getTypePtr())); |
| 669 | break; |
Charles Davis | c62458f | 2010-06-18 07:51:00 +0000 | [diff] [blame] | 670 | case Type::Enum: |
| 671 | mangleType(static_cast<EnumType *>(T.getTypePtr())); |
| 672 | break; |
| 673 | case Type::Record: |
| 674 | mangleType(static_cast<RecordType *>(T.getTypePtr())); |
| 675 | break; |
Charles Davis | 3a0d41d | 2010-06-26 03:50:05 +0000 | [diff] [blame] | 676 | case Type::Pointer: |
| 677 | mangleType(static_cast<PointerType *>(T.getTypePtr())); |
| 678 | break; |
| 679 | case Type::LValueReference: |
| 680 | mangleType(static_cast<LValueReferenceType *>(T.getTypePtr())); |
| 681 | break; |
Charles Davis | 570d276 | 2010-06-12 08:11:16 +0000 | [diff] [blame] | 682 | default: |
| 683 | assert(false && "Don't know how to mangle this type!"); |
| 684 | break; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T) { |
| 689 | // <type> ::= <builtin-type> |
| 690 | // <builtin-type> ::= X # void |
| 691 | // ::= C # signed char |
| 692 | // ::= D # char |
| 693 | // ::= E # unsigned char |
| 694 | // ::= F # short |
| 695 | // ::= G # unsigned short (or wchar_t if it's not a builtin) |
| 696 | // ::= H # int |
| 697 | // ::= I # unsigned int |
| 698 | // ::= J # long |
| 699 | // ::= K # unsigned long |
| 700 | // L # <none> |
| 701 | // ::= M # float |
| 702 | // ::= N # double |
| 703 | // ::= O # long double (__float80 is mangled differently) |
| 704 | // ::= _D # __int8 (yup, it's a distinct type in MSVC) |
| 705 | // ::= _E # unsigned __int8 |
| 706 | // ::= _F # __int16 |
| 707 | // ::= _G # unsigned __int16 |
| 708 | // ::= _H # __int32 |
| 709 | // ::= _I # unsigned __int32 |
| 710 | // ::= _J # long long, __int64 |
| 711 | // ::= _K # unsigned long long, __int64 |
| 712 | // ::= _L # __int128 |
| 713 | // ::= _M # unsigned __int128 |
| 714 | // ::= _N # bool |
| 715 | // _O # <array in parameter> |
| 716 | // ::= _T # __float80 (Intel) |
| 717 | // ::= _W # wchar_t |
| 718 | // ::= _Z # __float80 (Digital Mars) |
| 719 | switch (T->getKind()) { |
| 720 | case BuiltinType::Void: Out << 'X'; break; |
| 721 | case BuiltinType::SChar: Out << 'C'; break; |
| 722 | case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break; |
| 723 | case BuiltinType::UChar: Out << 'E'; break; |
| 724 | case BuiltinType::Short: Out << 'F'; break; |
| 725 | case BuiltinType::UShort: Out << 'G'; break; |
| 726 | case BuiltinType::Int: Out << 'H'; break; |
| 727 | case BuiltinType::UInt: Out << 'I'; break; |
| 728 | case BuiltinType::Long: Out << 'J'; break; |
| 729 | case BuiltinType::ULong: Out << 'K'; break; |
| 730 | case BuiltinType::Float: Out << 'M'; break; |
| 731 | case BuiltinType::Double: Out << 'N'; break; |
| 732 | // TODO: Determine size and mangle accordingly |
| 733 | case BuiltinType::LongDouble: Out << 'O'; break; |
| 734 | // TODO: __int8 and friends |
| 735 | case BuiltinType::LongLong: Out << "_J"; break; |
| 736 | case BuiltinType::ULongLong: Out << "_K"; break; |
| 737 | case BuiltinType::Int128: Out << "_L"; break; |
| 738 | case BuiltinType::UInt128: Out << "_M"; break; |
| 739 | case BuiltinType::Bool: Out << "_N"; break; |
| 740 | case BuiltinType::WChar: Out << "_W"; break; |
| 741 | |
| 742 | case BuiltinType::Overload: |
| 743 | case BuiltinType::Dependent: |
| 744 | assert(false && |
| 745 | "Overloaded and dependent types shouldn't get to name mangling"); |
| 746 | break; |
| 747 | case BuiltinType::UndeducedAuto: |
| 748 | assert(0 && "Should not see undeduced auto here"); |
| 749 | break; |
| 750 | case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break; |
| 751 | case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break; |
| 752 | case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break; |
| 753 | |
| 754 | case BuiltinType::Char16: |
| 755 | case BuiltinType::Char32: |
| 756 | case BuiltinType::NullPtr: |
| 757 | assert(false && "Don't know how to mangle this type"); |
| 758 | break; |
| 759 | } |
| 760 | } |
| 761 | |
Charles Davis | 65161d1 | 2010-06-16 05:33:16 +0000 | [diff] [blame] | 762 | // <type> ::= <function-type> |
| 763 | void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T) { |
| 764 | // Structors only appear in decls, so at this point we know it's not a |
| 765 | // structor type. |
Charles Davis | 3a0d41d | 2010-06-26 03:50:05 +0000 | [diff] [blame] | 766 | // I'll probably have mangleType(MemberPointerType) call |
| 767 | mangleType(T, false, false); |
Charles Davis | 65161d1 | 2010-06-16 05:33:16 +0000 | [diff] [blame] | 768 | } |
| 769 | void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T) { |
| 770 | llvm_unreachable("Can't mangle K&R function prototypes"); |
| 771 | } |
| 772 | |
| 773 | void MicrosoftCXXNameMangler::mangleType(const FunctionType *T, |
Charles Davis | 3a0d41d | 2010-06-26 03:50:05 +0000 | [diff] [blame] | 774 | bool IsStructor, |
| 775 | bool IsInstMethod) { |
| 776 | // <function-type> ::= <this-cvr-qualifiers> <calling-convention> |
| 777 | // <return-type> <argument-list> <throw-spec> |
Charles Davis | 65161d1 | 2010-06-16 05:33:16 +0000 | [diff] [blame] | 778 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
| 779 | |
Charles Davis | 3a0d41d | 2010-06-26 03:50:05 +0000 | [diff] [blame] | 780 | // If this is a C++ instance method, mangle the CVR qualifiers for the |
| 781 | // this pointer. |
| 782 | if (IsInstMethod) |
| 783 | mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false); |
| 784 | |
| 785 | mangleCallingConvention(T); |
| 786 | |
| 787 | // <return-type> ::= <type> |
| 788 | // ::= @ # structors (they have no declared return type) |
Charles Davis | 65161d1 | 2010-06-16 05:33:16 +0000 | [diff] [blame] | 789 | if (IsStructor) |
| 790 | Out << '@'; |
| 791 | else |
| 792 | mangleType(Proto->getResultType()); |
| 793 | |
| 794 | // <argument-list> ::= X # void |
| 795 | // ::= <type>+ @ |
| 796 | // ::= <type>* Z # varargs |
| 797 | if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) { |
| 798 | Out << 'X'; |
| 799 | } else { |
| 800 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
| 801 | ArgEnd = Proto->arg_type_end(); |
| 802 | Arg != ArgEnd; ++Arg) |
| 803 | mangleType(*Arg); |
| 804 | |
| 805 | // <builtin-type> ::= Z # ellipsis |
| 806 | if (Proto->isVariadic()) |
| 807 | Out << 'Z'; |
| 808 | else |
| 809 | Out << '@'; |
| 810 | } |
| 811 | |
| 812 | mangleThrowSpecification(Proto); |
| 813 | } |
| 814 | |
| 815 | void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) { |
| 816 | // <function-class> ::= A # private: near |
| 817 | // ::= B # private: far |
| 818 | // ::= C # private: static near |
| 819 | // ::= D # private: static far |
| 820 | // ::= E # private: virtual near |
| 821 | // ::= F # private: virtual far |
| 822 | // ::= G # private: thunk near |
| 823 | // ::= H # private: thunk far |
| 824 | // ::= I # protected: near |
| 825 | // ::= J # protected: far |
| 826 | // ::= K # protected: static near |
| 827 | // ::= L # protected: static far |
| 828 | // ::= M # protected: virtual near |
| 829 | // ::= N # protected: virtual far |
| 830 | // ::= O # protected: thunk near |
| 831 | // ::= P # protected: thunk far |
| 832 | // ::= Q # public: near |
| 833 | // ::= R # public: far |
| 834 | // ::= S # public: static near |
| 835 | // ::= T # public: static far |
| 836 | // ::= U # public: virtual near |
| 837 | // ::= V # public: virtual far |
| 838 | // ::= W # public: thunk near |
| 839 | // ::= X # public: thunk far |
| 840 | // ::= Y # global near |
| 841 | // ::= Z # global far |
| 842 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 843 | switch (MD->getAccess()) { |
| 844 | default: |
| 845 | case AS_private: |
| 846 | if (MD->isStatic()) |
| 847 | Out << 'C'; |
| 848 | else if (MD->isVirtual()) |
| 849 | Out << 'E'; |
| 850 | else |
| 851 | Out << 'A'; |
| 852 | break; |
| 853 | case AS_protected: |
| 854 | if (MD->isStatic()) |
| 855 | Out << 'K'; |
| 856 | else if (MD->isVirtual()) |
| 857 | Out << 'M'; |
| 858 | else |
| 859 | Out << 'I'; |
| 860 | break; |
| 861 | case AS_public: |
| 862 | if (MD->isStatic()) |
| 863 | Out << 'S'; |
| 864 | else if (MD->isVirtual()) |
| 865 | Out << 'U'; |
| 866 | else |
| 867 | Out << 'Q'; |
| 868 | } |
| 869 | } else |
| 870 | Out << 'Y'; |
| 871 | } |
| 872 | void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T) { |
| 873 | // <calling-convention> ::= A # __cdecl |
| 874 | // ::= B # __export __cdecl |
| 875 | // ::= C # __pascal |
| 876 | // ::= D # __export __pascal |
| 877 | // ::= E # __thiscall |
| 878 | // ::= F # __export __thiscall |
| 879 | // ::= G # __stdcall |
| 880 | // ::= H # __export __stdcall |
| 881 | // ::= I # __fastcall |
| 882 | // ::= J # __export __fastcall |
| 883 | // The 'export' calling conventions are from a bygone era |
| 884 | // (*cough*Win16*cough*) when functions were declared for export with |
| 885 | // that keyword. (It didn't actually export them, it just made them so |
| 886 | // that they could be in a DLL and somebody from another module could call |
| 887 | // them.) |
| 888 | switch (T->getCallConv()) { |
| 889 | case CC_Default: |
| 890 | case CC_C: Out << 'A'; break; |
| 891 | case CC_X86ThisCall: Out << 'E'; break; |
| 892 | case CC_X86StdCall: Out << 'G'; break; |
| 893 | case CC_X86FastCall: Out << 'I'; break; |
| 894 | } |
| 895 | } |
| 896 | void MicrosoftCXXNameMangler::mangleThrowSpecification( |
| 897 | const FunctionProtoType *FT) { |
| 898 | // <throw-spec> ::= Z # throw(...) (default) |
| 899 | // ::= @ # throw() or __declspec/__attribute__((nothrow)) |
| 900 | // ::= <type>+ |
Charles Davis | 3a0d41d | 2010-06-26 03:50:05 +0000 | [diff] [blame] | 901 | // NOTE: Since the Microsoft compiler ignores throw specifications, they are |
| 902 | // all actually mangled as 'Z'. (They're ignored because their associated |
| 903 | // functionality isn't implemented, and probably never will be.) |
| 904 | Out << 'Z'; |
Charles Davis | 65161d1 | 2010-06-16 05:33:16 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Charles Davis | c62458f | 2010-06-18 07:51:00 +0000 | [diff] [blame] | 907 | // <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type> |
| 908 | // <union-type> ::= T <name> |
| 909 | // <struct-type> ::= U <name> |
| 910 | // <class-type> ::= V <name> |
| 911 | // <enum-type> ::= W <size> <name> |
| 912 | void MicrosoftCXXNameMangler::mangleType(const EnumType *T) { |
| 913 | mangleType(static_cast<const TagType*>(T)); |
| 914 | } |
| 915 | void MicrosoftCXXNameMangler::mangleType(const RecordType *T) { |
| 916 | mangleType(static_cast<const TagType*>(T)); |
| 917 | } |
| 918 | void MicrosoftCXXNameMangler::mangleType(const TagType *T) { |
| 919 | switch (T->getDecl()->getTagKind()) { |
| 920 | case TTK_Union: |
| 921 | Out << 'T'; |
| 922 | break; |
| 923 | case TTK_Struct: |
| 924 | Out << 'U'; |
| 925 | break; |
| 926 | case TTK_Class: |
| 927 | Out << 'V'; |
| 928 | break; |
| 929 | case TTK_Enum: |
| 930 | Out << 'W'; |
| 931 | mangleNumber(getASTContext().getTypeSizeInChars( |
| 932 | cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity()); |
| 933 | break; |
| 934 | } |
| 935 | mangleName(T->getDecl()); |
| 936 | } |
| 937 | |
Charles Davis | 3a0d41d | 2010-06-26 03:50:05 +0000 | [diff] [blame] | 938 | // <type> ::= <pointer-type> |
| 939 | // <pointer-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> <type> |
| 940 | void MicrosoftCXXNameMangler::mangleType(const PointerType *T) { |
| 941 | QualType PointeeTy = T->getPointeeType(); |
| 942 | if (!PointeeTy.hasLocalQualifiers()) |
| 943 | // Lack of qualifiers is mangled as 'A'. |
| 944 | Out << 'A'; |
| 945 | mangleType(PointeeTy); |
| 946 | } |
| 947 | |
| 948 | // <type> ::= <reference-type> |
| 949 | // <reference-type> ::= A <cvr-qualifiers> <type> |
| 950 | void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T) { |
| 951 | Out << 'A'; |
| 952 | QualType PointeeTy = T->getPointeeType(); |
| 953 | if (!PointeeTy.hasLocalQualifiers()) |
| 954 | // Lack of qualifiers is mangled as 'A'. |
| 955 | Out << 'A'; |
| 956 | mangleType(PointeeTy); |
| 957 | } |
| 958 | |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 959 | void MicrosoftMangleContext::mangleName(const NamedDecl *D, |
| 960 | llvm::SmallVectorImpl<char> &Name) { |
Charles Davis | e60cea8 | 2010-06-11 03:07:32 +0000 | [diff] [blame] | 961 | assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && |
| 962 | "Invalid mangleName() call, argument is not a variable or function!"); |
| 963 | assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && |
| 964 | "Invalid mangleName() call on 'structor decl!"); |
| 965 | |
| 966 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 967 | getASTContext().getSourceManager(), |
| 968 | "Mangling declaration"); |
| 969 | |
| 970 | MicrosoftCXXNameMangler Mangler(*this, Name); |
| 971 | return Mangler.mangle(D); |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 972 | } |
| 973 | void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD, |
| 974 | const ThunkInfo &Thunk, |
| 975 | llvm::SmallVectorImpl<char> &) { |
| 976 | assert(false && "Can't yet mangle thunks!"); |
| 977 | } |
| 978 | void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, |
| 979 | CXXDtorType Type, |
| 980 | const ThisAdjustment &, |
| 981 | llvm::SmallVectorImpl<char> &) { |
| 982 | assert(false && "Can't yet mangle destructor thunks!"); |
| 983 | } |
| 984 | void MicrosoftMangleContext::mangleGuardVariable(const VarDecl *D, |
| 985 | llvm::SmallVectorImpl<char> &) { |
| 986 | assert(false && "Can't yet mangle guard variables!"); |
| 987 | } |
| 988 | void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD, |
| 989 | llvm::SmallVectorImpl<char> &) { |
| 990 | assert(false && "Can't yet mangle virtual tables!"); |
| 991 | } |
| 992 | void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD, |
| 993 | llvm::SmallVectorImpl<char> &) { |
| 994 | llvm_unreachable("The MS C++ ABI does not have virtual table tables!"); |
| 995 | } |
| 996 | void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, |
| 997 | int64_t Offset, |
| 998 | const CXXRecordDecl *Type, |
| 999 | llvm::SmallVectorImpl<char> &) { |
| 1000 | llvm_unreachable("The MS C++ ABI does not have constructor vtables!"); |
| 1001 | } |
| 1002 | void MicrosoftMangleContext::mangleCXXRTTI(QualType T, |
| 1003 | llvm::SmallVectorImpl<char> &) { |
| 1004 | assert(false && "Can't yet mangle RTTI!"); |
| 1005 | } |
| 1006 | void MicrosoftMangleContext::mangleCXXRTTIName(QualType T, |
| 1007 | llvm::SmallVectorImpl<char> &) { |
| 1008 | assert(false && "Can't yet mangle RTTI names!"); |
| 1009 | } |
| 1010 | void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D, |
| 1011 | CXXCtorType Type, |
| 1012 | llvm::SmallVectorImpl<char> &) { |
| 1013 | assert(false && "Can't yet mangle constructors!"); |
| 1014 | } |
| 1015 | void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D, |
| 1016 | CXXDtorType Type, |
| 1017 | llvm::SmallVectorImpl<char> &) { |
| 1018 | assert(false && "Can't yet mangle destructors!"); |
| 1019 | } |
| 1020 | |
Charles Davis | 98b7c5c | 2010-06-11 01:06:47 +0000 | [diff] [blame] | 1021 | CXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) { |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 1022 | return new MicrosoftCXXABI(CGM); |
| 1023 | } |
| 1024 | |