blob: 5a3f2283733b5b6dc3930a32271630a369c818b8 [file] [log] [blame]
Charles Davis74ce8592010-06-09 23:25:41 +00001//===--- 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
27using namespace clang;
28using namespace CodeGen;
29
30namespace {
31
Charles Davis9af2d4a2010-06-11 03:07:32 +000032/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
33/// Microsoft Visual C++ ABI.
34class MicrosoftCXXNameMangler {
35 MangleContext &Context;
36 llvm::raw_svector_ostream Out;
37
38 ASTContext &getASTContext() const { return Context.getASTContext(); }
39
40public:
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 Davis7dacc952010-06-12 08:11:16 +000048 void mangleType(QualType T);
Charles Davis9af2d4a2010-06-11 03:07:32 +000049
50private:
51 void mangleUnqualifiedName(const NamedDecl *ND) {
52 mangleUnqualifiedName(ND, ND->getDeclName());
53 }
54 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
55 void mangleSourceName(const IdentifierInfo *II);
56 void manglePostfix(const DeclContext *DC, bool NoFunction=false);
57
58 void mangleObjCMethodName(const ObjCMethodDecl *MD);
59
Charles Davis7dacc952010-06-12 08:11:16 +000060 // Declare manglers for every type class.
61#define ABSTRACT_TYPE(CLASS, PARENT)
62#define NON_CANONICAL_TYPE(CLASS, PARENT)
63#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
64#include "clang/AST/TypeNodes.def"
65
Charles Davis9af2d4a2010-06-11 03:07:32 +000066};
67
Charles Davis74ce8592010-06-09 23:25:41 +000068/// MicrosoftMangleContext - Overrides the default MangleContext for the
69/// Microsoft Visual C++ ABI.
70class MicrosoftMangleContext : public MangleContext {
71public:
72 MicrosoftMangleContext(ASTContext &Context,
73 Diagnostic &Diags) : MangleContext(Context, Diags) { }
Charles Davisb6a5a0d2010-06-11 04:25:47 +000074 virtual bool shouldMangleDeclName(const NamedDecl *D);
Charles Davis74ce8592010-06-09 23:25:41 +000075 virtual void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &);
76 virtual void mangleThunk(const CXXMethodDecl *MD,
77 const ThunkInfo &Thunk,
78 llvm::SmallVectorImpl<char> &);
79 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
80 const ThisAdjustment &ThisAdjustment,
81 llvm::SmallVectorImpl<char> &);
82 virtual void mangleGuardVariable(const VarDecl *D,
83 llvm::SmallVectorImpl<char> &);
84 virtual void mangleCXXVTable(const CXXRecordDecl *RD,
85 llvm::SmallVectorImpl<char> &);
86 virtual void mangleCXXVTT(const CXXRecordDecl *RD,
87 llvm::SmallVectorImpl<char> &);
88 virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
89 const CXXRecordDecl *Type,
90 llvm::SmallVectorImpl<char> &);
91 virtual void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &);
92 virtual void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &);
93 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
94 llvm::SmallVectorImpl<char> &);
95 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
96 llvm::SmallVectorImpl<char> &);
97};
98
99class MicrosoftCXXABI : public CXXABI {
100 MicrosoftMangleContext MangleCtx;
101public:
102 MicrosoftCXXABI(CodeGenModule &CGM)
103 : MangleCtx(CGM.getContext(), CGM.getDiags()) {}
104
105 MicrosoftMangleContext &getMangleContext() {
106 return MangleCtx;
107 }
108};
109
110}
111
Charles Davisb6a5a0d2010-06-11 04:25:47 +0000112static bool isInCLinkageSpecification(const Decl *D) {
113 D = D->getCanonicalDecl();
114 for (const DeclContext *DC = D->getDeclContext();
115 !DC->isTranslationUnit(); DC = DC->getParent()) {
116 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
117 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
118 }
Charles Davis7dacc952010-06-12 08:11:16 +0000119
Charles Davisb6a5a0d2010-06-11 04:25:47 +0000120 return false;
121}
122
123bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
124 // In C, functions with no attributes never need to be mangled. Fastpath them.
125 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
126 return false;
127
128 // Any decl can be declared with __asm("foo") on it, and this takes precedence
129 // over all other naming in the .o file.
130 if (D->hasAttr<AsmLabelAttr>())
131 return true;
132
133 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
134 // (always) as does passing a C++ member function and a function
135 // whose name is not a simple identifier.
136 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
137 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
138 !FD->getDeclName().isIdentifier()))
139 return true;
140
141 // Otherwise, no mangling is done outside C++ mode.
142 if (!getASTContext().getLangOptions().CPlusPlus)
143 return false;
144
Charles Davis7dacc952010-06-12 08:11:16 +0000145 // Variables at global scope with internal linkage are not mangled.
146 if (!FD) {
147 const DeclContext *DC = D->getDeclContext();
148 if (DC->isTranslationUnit() && D->getLinkage() == InternalLinkage)
149 return false;
150 }
151
Charles Davisb6a5a0d2010-06-11 04:25:47 +0000152 // C functions and "main" are not mangled.
153 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
154 return false;
155
156 return true;
157}
158
Charles Davis9af2d4a2010-06-11 03:07:32 +0000159void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
160 llvm::StringRef Prefix) {
161 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
162 // Therefore it's really important that we don't decorate the
163 // name with leading underscores or leading/trailing at signs. So, emit a
164 // asm marker at the start so we get the name right.
165 Out << '\01'; // LLVM IR Marker for __asm("foo")
166
167 // Any decl can be declared with __asm("foo") on it, and this takes precedence
168 // over all other naming in the .o file.
169 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
170 // If we have an asm name, then we use it as the mangling.
171 Out << ALA->getLabel();
172 return;
173 }
174
175 // <mangled-name> ::= ? <name> <type>
176 Out << Prefix;
177 mangleName(D);
178 // TODO: Mangle type.
179}
180
181void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
182 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
183 const DeclContext *DC = ND->getDeclContext();
184
185 // Always start with the unqualified name.
186 mangleUnqualifiedName(ND);
187
188 // If this is an extern variable declared locally, the relevant DeclContext
189 // is that of the containing namespace, or the translation unit.
190 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
191 while (!DC->isNamespace() && !DC->isTranslationUnit())
192 DC = DC->getParent();
193
194 manglePostfix(DC);
195
196 // Terminate the whole name with an '@'.
197 Out << '@';
198}
199
200void
201MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
202 DeclarationName Name) {
203 // <unqualified-name> ::= <operator-name>
204 // ::= <ctor-dtor-name>
205 // ::= <source-name>
206 switch (Name.getNameKind()) {
207 case DeclarationName::Identifier: {
208 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
209 mangleSourceName(II);
210 break;
211 }
212
213 // Otherwise, an anonymous entity. We must have a declaration.
214 assert(ND && "mangling empty name without declaration");
215
216 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
217 if (NS->isAnonymousNamespace()) {
218 Out << "?A";
219 break;
220 }
221 }
222
223 // We must have an anonymous struct.
224 const TagDecl *TD = cast<TagDecl>(ND);
225 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
226 assert(TD->getDeclContext() == D->getDeclContext() &&
227 "Typedef should not be in another decl context!");
228 assert(D->getDeclName().getAsIdentifierInfo() &&
229 "Typedef was not named!");
230 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
231 break;
232 }
233
234 // TODO: How does VC mangle anonymous structs?
235 assert(false && "Don't know how to mangle anonymous types yet!");
236 break;
237 }
238
239 case DeclarationName::ObjCZeroArgSelector:
240 case DeclarationName::ObjCOneArgSelector:
241 case DeclarationName::ObjCMultiArgSelector:
242 assert(false && "Can't mangle Objective-C selector names here!");
243 break;
244
245 case DeclarationName::CXXConstructorName:
246 assert(false && "Can't mangle constructors yet!");
247 break;
248
249 case DeclarationName::CXXDestructorName:
250 assert(false && "Can't mangle destructors yet!");
251 break;
252
253 case DeclarationName::CXXConversionFunctionName:
254 // <operator-name> ::= ?B # (cast)
255 // The target type is encoded as the return type.
256 Out << "?B";
257 break;
258
259 case DeclarationName::CXXOperatorName:
260 assert(false && "Can't mangle operators yet!");
261
262 case DeclarationName::CXXLiteralOperatorName:
263 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
264 assert(false && "Don't know how to mangle literal operators yet!");
265 break;
266
267 case DeclarationName::CXXUsingDirective:
268 assert(false && "Can't mangle a using directive name!");
269 break;
270 }
271}
272
273void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
274 bool NoFunction) {
275 // <postfix> ::= <unqualified-name> [<postfix>]
276 // ::= <template-postfix> <template-args> [<postfix>]
277 // ::= <template-param>
278 // ::= <substitution> [<postfix>]
279
280 if (!DC) return;
281
282 while (isa<LinkageSpecDecl>(DC))
283 DC = DC->getParent();
284
285 if (DC->isTranslationUnit())
286 return;
287
288 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
289 llvm::SmallString<64> Name;
290 Context.mangleBlock(BD, Name);
291 Out << Name << '@';
292 return manglePostfix(DC->getParent(), NoFunction);
293 }
294
295 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
296 return;
297 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
298 mangleObjCMethodName(Method);
299 else {
300 mangleUnqualifiedName(cast<NamedDecl>(DC));
301 manglePostfix(DC->getParent(), NoFunction);
302 }
303}
304
305void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
306 // <source name> ::= <identifier> @
307 Out << II->getName() << '@';
308}
309
310void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
311 llvm::SmallString<64> Buffer;
312 MiscNameMangler(Context, Buffer).mangleObjCMethodName(MD);
313 Out << Buffer;
314}
315
Charles Davis7dacc952010-06-12 08:11:16 +0000316void MicrosoftCXXNameMangler::mangleType(QualType T) {
317 // Only operate on the canonical type!
318 T = getASTContext().getCanonicalType(T);
319
320 switch (T->getTypeClass()) {
321#define ABSTRACT_TYPE(CLASS, PARENT)
322#define NON_CANONICAL_TYPE(CLASS, PARENT) \
323case Type::CLASS: \
324llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
325return;
326#define TYPE(CLASS, PARENT)
327#include "clang/AST/TypeNodes.def"
328 case Type::Builtin:
329 mangleType(static_cast<BuiltinType *>(T.getTypePtr()));
330 break;
331 default:
332 assert(false && "Don't know how to mangle this type!");
333 break;
334 }
335}
336
337void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T) {
338 // <type> ::= <builtin-type>
339 // <builtin-type> ::= X # void
340 // ::= C # signed char
341 // ::= D # char
342 // ::= E # unsigned char
343 // ::= F # short
344 // ::= G # unsigned short (or wchar_t if it's not a builtin)
345 // ::= H # int
346 // ::= I # unsigned int
347 // ::= J # long
348 // ::= K # unsigned long
349 // L # <none>
350 // ::= M # float
351 // ::= N # double
352 // ::= O # long double (__float80 is mangled differently)
353 // ::= _D # __int8 (yup, it's a distinct type in MSVC)
354 // ::= _E # unsigned __int8
355 // ::= _F # __int16
356 // ::= _G # unsigned __int16
357 // ::= _H # __int32
358 // ::= _I # unsigned __int32
359 // ::= _J # long long, __int64
360 // ::= _K # unsigned long long, __int64
361 // ::= _L # __int128
362 // ::= _M # unsigned __int128
363 // ::= _N # bool
364 // _O # <array in parameter>
365 // ::= _T # __float80 (Intel)
366 // ::= _W # wchar_t
367 // ::= _Z # __float80 (Digital Mars)
368 switch (T->getKind()) {
369 case BuiltinType::Void: Out << 'X'; break;
370 case BuiltinType::SChar: Out << 'C'; break;
371 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
372 case BuiltinType::UChar: Out << 'E'; break;
373 case BuiltinType::Short: Out << 'F'; break;
374 case BuiltinType::UShort: Out << 'G'; break;
375 case BuiltinType::Int: Out << 'H'; break;
376 case BuiltinType::UInt: Out << 'I'; break;
377 case BuiltinType::Long: Out << 'J'; break;
378 case BuiltinType::ULong: Out << 'K'; break;
379 case BuiltinType::Float: Out << 'M'; break;
380 case BuiltinType::Double: Out << 'N'; break;
381 // TODO: Determine size and mangle accordingly
382 case BuiltinType::LongDouble: Out << 'O'; break;
383 // TODO: __int8 and friends
384 case BuiltinType::LongLong: Out << "_J"; break;
385 case BuiltinType::ULongLong: Out << "_K"; break;
386 case BuiltinType::Int128: Out << "_L"; break;
387 case BuiltinType::UInt128: Out << "_M"; break;
388 case BuiltinType::Bool: Out << "_N"; break;
389 case BuiltinType::WChar: Out << "_W"; break;
390
391 case BuiltinType::Overload:
392 case BuiltinType::Dependent:
393 assert(false &&
394 "Overloaded and dependent types shouldn't get to name mangling");
395 break;
396 case BuiltinType::UndeducedAuto:
397 assert(0 && "Should not see undeduced auto here");
398 break;
399 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
400 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
401 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
402
403 case BuiltinType::Char16:
404 case BuiltinType::Char32:
405 case BuiltinType::NullPtr:
406 assert(false && "Don't know how to mangle this type");
407 break;
408 }
409}
410
Charles Davis74ce8592010-06-09 23:25:41 +0000411void MicrosoftMangleContext::mangleName(const NamedDecl *D,
412 llvm::SmallVectorImpl<char> &Name) {
Charles Davis9af2d4a2010-06-11 03:07:32 +0000413 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
414 "Invalid mangleName() call, argument is not a variable or function!");
415 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
416 "Invalid mangleName() call on 'structor decl!");
417
418 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
419 getASTContext().getSourceManager(),
420 "Mangling declaration");
421
422 MicrosoftCXXNameMangler Mangler(*this, Name);
423 return Mangler.mangle(D);
Charles Davis74ce8592010-06-09 23:25:41 +0000424}
425void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
426 const ThunkInfo &Thunk,
427 llvm::SmallVectorImpl<char> &) {
428 assert(false && "Can't yet mangle thunks!");
429}
430void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
431 CXXDtorType Type,
432 const ThisAdjustment &,
433 llvm::SmallVectorImpl<char> &) {
434 assert(false && "Can't yet mangle destructor thunks!");
435}
436void MicrosoftMangleContext::mangleGuardVariable(const VarDecl *D,
437 llvm::SmallVectorImpl<char> &) {
438 assert(false && "Can't yet mangle guard variables!");
439}
440void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
441 llvm::SmallVectorImpl<char> &) {
442 assert(false && "Can't yet mangle virtual tables!");
443}
444void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
445 llvm::SmallVectorImpl<char> &) {
446 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
447}
448void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
449 int64_t Offset,
450 const CXXRecordDecl *Type,
451 llvm::SmallVectorImpl<char> &) {
452 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
453}
454void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
455 llvm::SmallVectorImpl<char> &) {
456 assert(false && "Can't yet mangle RTTI!");
457}
458void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
459 llvm::SmallVectorImpl<char> &) {
460 assert(false && "Can't yet mangle RTTI names!");
461}
462void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
463 CXXCtorType Type,
464 llvm::SmallVectorImpl<char> &) {
465 assert(false && "Can't yet mangle constructors!");
466}
467void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
468 CXXDtorType Type,
469 llvm::SmallVectorImpl<char> &) {
470 assert(false && "Can't yet mangle destructors!");
471}
472
Charles Davis95a546e2010-06-11 01:06:47 +0000473CXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) {
Charles Davis74ce8592010-06-09 23:25:41 +0000474 return new MicrosoftCXXABI(CGM);
475}
476