Charles Davis | 74ce859 | 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 | 9af2d4a | 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); |
| 48 | |
| 49 | private: |
| 50 | void mangleUnqualifiedName(const NamedDecl *ND) { |
| 51 | mangleUnqualifiedName(ND, ND->getDeclName()); |
| 52 | } |
| 53 | void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name); |
| 54 | void mangleSourceName(const IdentifierInfo *II); |
| 55 | void manglePostfix(const DeclContext *DC, bool NoFunction=false); |
| 56 | |
| 57 | void mangleObjCMethodName(const ObjCMethodDecl *MD); |
| 58 | |
| 59 | }; |
| 60 | |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 61 | /// MicrosoftMangleContext - Overrides the default MangleContext for the |
| 62 | /// Microsoft Visual C++ ABI. |
| 63 | class MicrosoftMangleContext : public MangleContext { |
| 64 | public: |
| 65 | MicrosoftMangleContext(ASTContext &Context, |
| 66 | Diagnostic &Diags) : MangleContext(Context, Diags) { } |
Charles Davis | b6a5a0d | 2010-06-11 04:25:47 +0000 | [diff] [blame^] | 67 | virtual bool shouldMangleDeclName(const NamedDecl *D); |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 68 | virtual void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &); |
| 69 | virtual void mangleThunk(const CXXMethodDecl *MD, |
| 70 | const ThunkInfo &Thunk, |
| 71 | llvm::SmallVectorImpl<char> &); |
| 72 | virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, |
| 73 | const ThisAdjustment &ThisAdjustment, |
| 74 | llvm::SmallVectorImpl<char> &); |
| 75 | virtual void mangleGuardVariable(const VarDecl *D, |
| 76 | llvm::SmallVectorImpl<char> &); |
| 77 | virtual void mangleCXXVTable(const CXXRecordDecl *RD, |
| 78 | llvm::SmallVectorImpl<char> &); |
| 79 | virtual void mangleCXXVTT(const CXXRecordDecl *RD, |
| 80 | llvm::SmallVectorImpl<char> &); |
| 81 | virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, |
| 82 | const CXXRecordDecl *Type, |
| 83 | llvm::SmallVectorImpl<char> &); |
| 84 | virtual void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &); |
| 85 | virtual void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &); |
| 86 | virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, |
| 87 | llvm::SmallVectorImpl<char> &); |
| 88 | virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, |
| 89 | llvm::SmallVectorImpl<char> &); |
| 90 | }; |
| 91 | |
| 92 | class MicrosoftCXXABI : public CXXABI { |
| 93 | MicrosoftMangleContext MangleCtx; |
| 94 | public: |
| 95 | MicrosoftCXXABI(CodeGenModule &CGM) |
| 96 | : MangleCtx(CGM.getContext(), CGM.getDiags()) {} |
| 97 | |
| 98 | MicrosoftMangleContext &getMangleContext() { |
| 99 | return MangleCtx; |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | } |
| 104 | |
Charles Davis | b6a5a0d | 2010-06-11 04:25:47 +0000 | [diff] [blame^] | 105 | static bool isInCLinkageSpecification(const Decl *D) { |
| 106 | D = D->getCanonicalDecl(); |
| 107 | for (const DeclContext *DC = D->getDeclContext(); |
| 108 | !DC->isTranslationUnit(); DC = DC->getParent()) { |
| 109 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) |
| 110 | return Linkage->getLanguage() == LinkageSpecDecl::lang_c; |
| 111 | } |
| 112 | |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) { |
| 117 | // In C, functions with no attributes never need to be mangled. Fastpath them. |
| 118 | if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs()) |
| 119 | return false; |
| 120 | |
| 121 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 122 | // over all other naming in the .o file. |
| 123 | if (D->hasAttr<AsmLabelAttr>()) |
| 124 | return true; |
| 125 | |
| 126 | // Clang's "overloadable" attribute extension to C/C++ implies name mangling |
| 127 | // (always) as does passing a C++ member function and a function |
| 128 | // whose name is not a simple identifier. |
| 129 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 130 | if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) || |
| 131 | !FD->getDeclName().isIdentifier())) |
| 132 | return true; |
| 133 | |
| 134 | // Otherwise, no mangling is done outside C++ mode. |
| 135 | if (!getASTContext().getLangOptions().CPlusPlus) |
| 136 | return false; |
| 137 | |
| 138 | // C functions and "main" are not mangled. |
| 139 | if ((FD && FD->isMain()) || isInCLinkageSpecification(D)) |
| 140 | return false; |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
Charles Davis | 9af2d4a | 2010-06-11 03:07:32 +0000 | [diff] [blame] | 145 | void MicrosoftCXXNameMangler::mangle(const NamedDecl *D, |
| 146 | llvm::StringRef Prefix) { |
| 147 | // MSVC doesn't mangle C++ names the same way it mangles extern "C" names. |
| 148 | // Therefore it's really important that we don't decorate the |
| 149 | // name with leading underscores or leading/trailing at signs. So, emit a |
| 150 | // asm marker at the start so we get the name right. |
| 151 | Out << '\01'; // LLVM IR Marker for __asm("foo") |
| 152 | |
| 153 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 154 | // over all other naming in the .o file. |
| 155 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
| 156 | // If we have an asm name, then we use it as the mangling. |
| 157 | Out << ALA->getLabel(); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | // <mangled-name> ::= ? <name> <type> |
| 162 | Out << Prefix; |
| 163 | mangleName(D); |
| 164 | // TODO: Mangle type. |
| 165 | } |
| 166 | |
| 167 | void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) { |
| 168 | // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @ |
| 169 | const DeclContext *DC = ND->getDeclContext(); |
| 170 | |
| 171 | // Always start with the unqualified name. |
| 172 | mangleUnqualifiedName(ND); |
| 173 | |
| 174 | // If this is an extern variable declared locally, the relevant DeclContext |
| 175 | // is that of the containing namespace, or the translation unit. |
| 176 | if (isa<FunctionDecl>(DC) && ND->hasLinkage()) |
| 177 | while (!DC->isNamespace() && !DC->isTranslationUnit()) |
| 178 | DC = DC->getParent(); |
| 179 | |
| 180 | manglePostfix(DC); |
| 181 | |
| 182 | // Terminate the whole name with an '@'. |
| 183 | Out << '@'; |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, |
| 188 | DeclarationName Name) { |
| 189 | // <unqualified-name> ::= <operator-name> |
| 190 | // ::= <ctor-dtor-name> |
| 191 | // ::= <source-name> |
| 192 | switch (Name.getNameKind()) { |
| 193 | case DeclarationName::Identifier: { |
| 194 | if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { |
| 195 | mangleSourceName(II); |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | // Otherwise, an anonymous entity. We must have a declaration. |
| 200 | assert(ND && "mangling empty name without declaration"); |
| 201 | |
| 202 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { |
| 203 | if (NS->isAnonymousNamespace()) { |
| 204 | Out << "?A"; |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // We must have an anonymous struct. |
| 210 | const TagDecl *TD = cast<TagDecl>(ND); |
| 211 | if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) { |
| 212 | assert(TD->getDeclContext() == D->getDeclContext() && |
| 213 | "Typedef should not be in another decl context!"); |
| 214 | assert(D->getDeclName().getAsIdentifierInfo() && |
| 215 | "Typedef was not named!"); |
| 216 | mangleSourceName(D->getDeclName().getAsIdentifierInfo()); |
| 217 | break; |
| 218 | } |
| 219 | |
| 220 | // TODO: How does VC mangle anonymous structs? |
| 221 | assert(false && "Don't know how to mangle anonymous types yet!"); |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | case DeclarationName::ObjCZeroArgSelector: |
| 226 | case DeclarationName::ObjCOneArgSelector: |
| 227 | case DeclarationName::ObjCMultiArgSelector: |
| 228 | assert(false && "Can't mangle Objective-C selector names here!"); |
| 229 | break; |
| 230 | |
| 231 | case DeclarationName::CXXConstructorName: |
| 232 | assert(false && "Can't mangle constructors yet!"); |
| 233 | break; |
| 234 | |
| 235 | case DeclarationName::CXXDestructorName: |
| 236 | assert(false && "Can't mangle destructors yet!"); |
| 237 | break; |
| 238 | |
| 239 | case DeclarationName::CXXConversionFunctionName: |
| 240 | // <operator-name> ::= ?B # (cast) |
| 241 | // The target type is encoded as the return type. |
| 242 | Out << "?B"; |
| 243 | break; |
| 244 | |
| 245 | case DeclarationName::CXXOperatorName: |
| 246 | assert(false && "Can't mangle operators yet!"); |
| 247 | |
| 248 | case DeclarationName::CXXLiteralOperatorName: |
| 249 | // FIXME: Was this added in VS2010? Does MS even know how to mangle this? |
| 250 | assert(false && "Don't know how to mangle literal operators yet!"); |
| 251 | break; |
| 252 | |
| 253 | case DeclarationName::CXXUsingDirective: |
| 254 | assert(false && "Can't mangle a using directive name!"); |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC, |
| 260 | bool NoFunction) { |
| 261 | // <postfix> ::= <unqualified-name> [<postfix>] |
| 262 | // ::= <template-postfix> <template-args> [<postfix>] |
| 263 | // ::= <template-param> |
| 264 | // ::= <substitution> [<postfix>] |
| 265 | |
| 266 | if (!DC) return; |
| 267 | |
| 268 | while (isa<LinkageSpecDecl>(DC)) |
| 269 | DC = DC->getParent(); |
| 270 | |
| 271 | if (DC->isTranslationUnit()) |
| 272 | return; |
| 273 | |
| 274 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) { |
| 275 | llvm::SmallString<64> Name; |
| 276 | Context.mangleBlock(BD, Name); |
| 277 | Out << Name << '@'; |
| 278 | return manglePostfix(DC->getParent(), NoFunction); |
| 279 | } |
| 280 | |
| 281 | if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC))) |
| 282 | return; |
| 283 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) |
| 284 | mangleObjCMethodName(Method); |
| 285 | else { |
| 286 | mangleUnqualifiedName(cast<NamedDecl>(DC)); |
| 287 | manglePostfix(DC->getParent(), NoFunction); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) { |
| 292 | // <source name> ::= <identifier> @ |
| 293 | Out << II->getName() << '@'; |
| 294 | } |
| 295 | |
| 296 | void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { |
| 297 | llvm::SmallString<64> Buffer; |
| 298 | MiscNameMangler(Context, Buffer).mangleObjCMethodName(MD); |
| 299 | Out << Buffer; |
| 300 | } |
| 301 | |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 302 | void MicrosoftMangleContext::mangleName(const NamedDecl *D, |
| 303 | llvm::SmallVectorImpl<char> &Name) { |
Charles Davis | 9af2d4a | 2010-06-11 03:07:32 +0000 | [diff] [blame] | 304 | assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && |
| 305 | "Invalid mangleName() call, argument is not a variable or function!"); |
| 306 | assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && |
| 307 | "Invalid mangleName() call on 'structor decl!"); |
| 308 | |
| 309 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 310 | getASTContext().getSourceManager(), |
| 311 | "Mangling declaration"); |
| 312 | |
| 313 | MicrosoftCXXNameMangler Mangler(*this, Name); |
| 314 | return Mangler.mangle(D); |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 315 | } |
| 316 | void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD, |
| 317 | const ThunkInfo &Thunk, |
| 318 | llvm::SmallVectorImpl<char> &) { |
| 319 | assert(false && "Can't yet mangle thunks!"); |
| 320 | } |
| 321 | void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, |
| 322 | CXXDtorType Type, |
| 323 | const ThisAdjustment &, |
| 324 | llvm::SmallVectorImpl<char> &) { |
| 325 | assert(false && "Can't yet mangle destructor thunks!"); |
| 326 | } |
| 327 | void MicrosoftMangleContext::mangleGuardVariable(const VarDecl *D, |
| 328 | llvm::SmallVectorImpl<char> &) { |
| 329 | assert(false && "Can't yet mangle guard variables!"); |
| 330 | } |
| 331 | void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD, |
| 332 | llvm::SmallVectorImpl<char> &) { |
| 333 | assert(false && "Can't yet mangle virtual tables!"); |
| 334 | } |
| 335 | void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD, |
| 336 | llvm::SmallVectorImpl<char> &) { |
| 337 | llvm_unreachable("The MS C++ ABI does not have virtual table tables!"); |
| 338 | } |
| 339 | void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, |
| 340 | int64_t Offset, |
| 341 | const CXXRecordDecl *Type, |
| 342 | llvm::SmallVectorImpl<char> &) { |
| 343 | llvm_unreachable("The MS C++ ABI does not have constructor vtables!"); |
| 344 | } |
| 345 | void MicrosoftMangleContext::mangleCXXRTTI(QualType T, |
| 346 | llvm::SmallVectorImpl<char> &) { |
| 347 | assert(false && "Can't yet mangle RTTI!"); |
| 348 | } |
| 349 | void MicrosoftMangleContext::mangleCXXRTTIName(QualType T, |
| 350 | llvm::SmallVectorImpl<char> &) { |
| 351 | assert(false && "Can't yet mangle RTTI names!"); |
| 352 | } |
| 353 | void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D, |
| 354 | CXXCtorType Type, |
| 355 | llvm::SmallVectorImpl<char> &) { |
| 356 | assert(false && "Can't yet mangle constructors!"); |
| 357 | } |
| 358 | void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D, |
| 359 | CXXDtorType Type, |
| 360 | llvm::SmallVectorImpl<char> &) { |
| 361 | assert(false && "Can't yet mangle destructors!"); |
| 362 | } |
| 363 | |
Charles Davis | 95a546e | 2010-06-11 01:06:47 +0000 | [diff] [blame] | 364 | CXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) { |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 365 | return new MicrosoftCXXABI(CGM); |
| 366 | } |
| 367 | |