blob: 4d26a58d90eada8d8511f9a7a76845533ba9487f [file] [log] [blame]
Charles Davisc3926642010-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 Davise60cea82010-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 Davis65161d12010-06-16 05:33:16 +000048 void mangleFunctionEncoding(const FunctionDecl *FD);
Charles Davisb021f8b2010-06-14 05:29:01 +000049 void mangleVariableEncoding(const VarDecl *VD);
Charles Davis570d2762010-06-12 08:11:16 +000050 void mangleType(QualType T);
Charles Davise60cea82010-06-11 03:07:32 +000051
52private:
53 void mangleUnqualifiedName(const NamedDecl *ND) {
54 mangleUnqualifiedName(ND, ND->getDeclName());
55 }
56 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
57 void mangleSourceName(const IdentifierInfo *II);
58 void manglePostfix(const DeclContext *DC, bool NoFunction=false);
Charles Davisb021f8b2010-06-14 05:29:01 +000059 void mangleQualifiers(Qualifiers Quals, bool IsMember);
Charles Davise60cea82010-06-11 03:07:32 +000060
61 void mangleObjCMethodName(const ObjCMethodDecl *MD);
62
Charles Davis570d2762010-06-12 08:11:16 +000063 // Declare manglers for every type class.
64#define ABSTRACT_TYPE(CLASS, PARENT)
65#define NON_CANONICAL_TYPE(CLASS, PARENT)
66#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
67#include "clang/AST/TypeNodes.def"
68
Charles Davis65161d12010-06-16 05:33:16 +000069 void mangleType(const FunctionType *T, bool IsStructor);
70 void mangleFunctionClass(const FunctionDecl *FD);
71 void mangleCallingConvention(const FunctionType *T);
72 void mangleThrowSpecification(const FunctionProtoType *T);
73
Charles Davise60cea82010-06-11 03:07:32 +000074};
75
Charles Davisc3926642010-06-09 23:25:41 +000076/// MicrosoftMangleContext - Overrides the default MangleContext for the
77/// Microsoft Visual C++ ABI.
78class MicrosoftMangleContext : public MangleContext {
79public:
80 MicrosoftMangleContext(ASTContext &Context,
81 Diagnostic &Diags) : MangleContext(Context, Diags) { }
Charles Davis971154d2010-06-11 04:25:47 +000082 virtual bool shouldMangleDeclName(const NamedDecl *D);
Charles Davisc3926642010-06-09 23:25:41 +000083 virtual void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &);
84 virtual void mangleThunk(const CXXMethodDecl *MD,
85 const ThunkInfo &Thunk,
86 llvm::SmallVectorImpl<char> &);
87 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
88 const ThisAdjustment &ThisAdjustment,
89 llvm::SmallVectorImpl<char> &);
90 virtual void mangleGuardVariable(const VarDecl *D,
91 llvm::SmallVectorImpl<char> &);
92 virtual void mangleCXXVTable(const CXXRecordDecl *RD,
93 llvm::SmallVectorImpl<char> &);
94 virtual void mangleCXXVTT(const CXXRecordDecl *RD,
95 llvm::SmallVectorImpl<char> &);
96 virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
97 const CXXRecordDecl *Type,
98 llvm::SmallVectorImpl<char> &);
99 virtual void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &);
100 virtual void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &);
101 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
102 llvm::SmallVectorImpl<char> &);
103 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
104 llvm::SmallVectorImpl<char> &);
105};
106
107class MicrosoftCXXABI : public CXXABI {
108 MicrosoftMangleContext MangleCtx;
109public:
110 MicrosoftCXXABI(CodeGenModule &CGM)
111 : MangleCtx(CGM.getContext(), CGM.getDiags()) {}
112
113 MicrosoftMangleContext &getMangleContext() {
114 return MangleCtx;
115 }
116};
117
118}
119
Charles Davis971154d2010-06-11 04:25:47 +0000120static bool isInCLinkageSpecification(const Decl *D) {
121 D = D->getCanonicalDecl();
122 for (const DeclContext *DC = D->getDeclContext();
123 !DC->isTranslationUnit(); DC = DC->getParent()) {
124 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
125 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
126 }
Charles Davis570d2762010-06-12 08:11:16 +0000127
Charles Davis971154d2010-06-11 04:25:47 +0000128 return false;
129}
130
131bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
132 // In C, functions with no attributes never need to be mangled. Fastpath them.
133 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
134 return false;
135
136 // Any decl can be declared with __asm("foo") on it, and this takes precedence
137 // over all other naming in the .o file.
138 if (D->hasAttr<AsmLabelAttr>())
139 return true;
140
141 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
142 // (always) as does passing a C++ member function and a function
143 // whose name is not a simple identifier.
144 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
145 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
146 !FD->getDeclName().isIdentifier()))
147 return true;
148
149 // Otherwise, no mangling is done outside C++ mode.
150 if (!getASTContext().getLangOptions().CPlusPlus)
151 return false;
152
Charles Davis570d2762010-06-12 08:11:16 +0000153 // Variables at global scope with internal linkage are not mangled.
154 if (!FD) {
155 const DeclContext *DC = D->getDeclContext();
156 if (DC->isTranslationUnit() && D->getLinkage() == InternalLinkage)
157 return false;
158 }
159
Charles Davis971154d2010-06-11 04:25:47 +0000160 // C functions and "main" are not mangled.
161 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
162 return false;
163
164 return true;
165}
166
Charles Davise60cea82010-06-11 03:07:32 +0000167void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
168 llvm::StringRef Prefix) {
169 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
170 // Therefore it's really important that we don't decorate the
171 // name with leading underscores or leading/trailing at signs. So, emit a
172 // asm marker at the start so we get the name right.
173 Out << '\01'; // LLVM IR Marker for __asm("foo")
174
175 // Any decl can be declared with __asm("foo") on it, and this takes precedence
176 // over all other naming in the .o file.
177 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
178 // If we have an asm name, then we use it as the mangling.
179 Out << ALA->getLabel();
180 return;
181 }
182
Charles Davis65161d12010-06-16 05:33:16 +0000183 // <mangled-name> ::= ? <name> <type-encoding>
Charles Davise60cea82010-06-11 03:07:32 +0000184 Out << Prefix;
185 mangleName(D);
Charles Davis65161d12010-06-16 05:33:16 +0000186 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
187 mangleFunctionEncoding(FD);
188 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Charles Davisb021f8b2010-06-14 05:29:01 +0000189 mangleVariableEncoding(VD);
Charles Davis65161d12010-06-16 05:33:16 +0000190 // TODO: Fields? Can MSVC even mangle them?
191}
192
193void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
194 // <type-encoding> ::= <function-class> <function-type>
195
196 // Don't mangle in the type if this isn't a decl we should typically mangle.
197 if (!Context.shouldMangleDeclName(FD))
198 return;
199
200 // We should never ever see a FunctionNoProtoType at this point.
201 // We don't even know how to mangle their types anyway :).
202 FunctionProtoType *OldType = cast<FunctionProtoType>(FD->getType());
203
204 bool InStructor = false;
205 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
206 if (MD) {
207 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
208 InStructor = true;
209 }
210
211 // First, the function class.
212 mangleFunctionClass(FD);
213
214 // If this is a C++ instance method, mangle the CVR qualifiers for the
215 // this pointer.
216 if (MD && MD->isInstance())
217 mangleQualifiers(Qualifiers::fromCVRMask(OldType->getTypeQuals()), false);
218
219 // Do the canonicalization out here because parameter types can
220 // undergo additional canonicalization (e.g. array decay).
221 const FunctionProtoType *FT = cast<FunctionProtoType>(getASTContext()
222 .getCanonicalType(OldType));
223 // If the function's type had a throw spec, canonicalization removed it.
224 // Get it back.
225 FT = cast<FunctionProtoType>(getASTContext().getFunctionType(
226 FT->getResultType(),
227 FT->arg_type_begin(),
228 FT->getNumArgs(),
229 FT->isVariadic(),
230 FT->getTypeQuals(),
231 OldType->hasExceptionSpec(),
232 OldType->hasAnyExceptionSpec(),
233 OldType->getNumExceptions(),
234 OldType->exception_begin(),
235 FT->getExtInfo()).getTypePtr());
236 mangleType(FT, InStructor);
Charles Davisb021f8b2010-06-14 05:29:01 +0000237}
238
239void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
Charles Davis65161d12010-06-16 05:33:16 +0000240 // <type-encoding> ::= <storage-class> <variable-type>
Charles Davisb021f8b2010-06-14 05:29:01 +0000241 // <storage-class> ::= 0 # private static member
242 // ::= 1 # protected static member
243 // ::= 2 # public static member
244 // ::= 3 # global
245 // ::= 4 # static local
246
247 // The first character in the encoding (after the name) is the storage class.
248 if (VD->isStaticDataMember()) {
249 // If it's a static member, it also encodes the access level.
250 switch (VD->getAccess()) {
251 default:
252 case AS_private: Out << '0'; break;
253 case AS_protected: Out << '1'; break;
254 case AS_public: Out << '2'; break;
255 }
256 }
257 else if (!VD->isStaticLocal())
258 Out << '3';
259 else
260 Out << '4';
261 // Now mangle the type.
262 // <variable-type> ::= <type> <cvr-qualifiers>
263 QualType Ty = VD->getType();
264 mangleType(Ty.getLocalUnqualifiedType());
265 mangleQualifiers(Ty.getLocalQualifiers(), false);
Charles Davise60cea82010-06-11 03:07:32 +0000266}
267
268void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
269 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
270 const DeclContext *DC = ND->getDeclContext();
271
272 // Always start with the unqualified name.
273 mangleUnqualifiedName(ND);
274
275 // If this is an extern variable declared locally, the relevant DeclContext
276 // is that of the containing namespace, or the translation unit.
277 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
278 while (!DC->isNamespace() && !DC->isTranslationUnit())
279 DC = DC->getParent();
280
281 manglePostfix(DC);
282
283 // Terminate the whole name with an '@'.
284 Out << '@';
285}
286
287void
288MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
289 DeclarationName Name) {
290 // <unqualified-name> ::= <operator-name>
291 // ::= <ctor-dtor-name>
292 // ::= <source-name>
293 switch (Name.getNameKind()) {
294 case DeclarationName::Identifier: {
295 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
296 mangleSourceName(II);
297 break;
298 }
299
300 // Otherwise, an anonymous entity. We must have a declaration.
301 assert(ND && "mangling empty name without declaration");
302
303 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
304 if (NS->isAnonymousNamespace()) {
305 Out << "?A";
306 break;
307 }
308 }
309
310 // We must have an anonymous struct.
311 const TagDecl *TD = cast<TagDecl>(ND);
312 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
313 assert(TD->getDeclContext() == D->getDeclContext() &&
314 "Typedef should not be in another decl context!");
315 assert(D->getDeclName().getAsIdentifierInfo() &&
316 "Typedef was not named!");
317 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
318 break;
319 }
320
321 // TODO: How does VC mangle anonymous structs?
322 assert(false && "Don't know how to mangle anonymous types yet!");
323 break;
324 }
325
326 case DeclarationName::ObjCZeroArgSelector:
327 case DeclarationName::ObjCOneArgSelector:
328 case DeclarationName::ObjCMultiArgSelector:
329 assert(false && "Can't mangle Objective-C selector names here!");
330 break;
331
332 case DeclarationName::CXXConstructorName:
333 assert(false && "Can't mangle constructors yet!");
334 break;
335
336 case DeclarationName::CXXDestructorName:
337 assert(false && "Can't mangle destructors yet!");
338 break;
339
340 case DeclarationName::CXXConversionFunctionName:
341 // <operator-name> ::= ?B # (cast)
342 // The target type is encoded as the return type.
343 Out << "?B";
344 break;
345
346 case DeclarationName::CXXOperatorName:
347 assert(false && "Can't mangle operators yet!");
348
349 case DeclarationName::CXXLiteralOperatorName:
350 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
351 assert(false && "Don't know how to mangle literal operators yet!");
352 break;
353
354 case DeclarationName::CXXUsingDirective:
355 assert(false && "Can't mangle a using directive name!");
356 break;
357 }
358}
359
360void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
361 bool NoFunction) {
362 // <postfix> ::= <unqualified-name> [<postfix>]
363 // ::= <template-postfix> <template-args> [<postfix>]
364 // ::= <template-param>
365 // ::= <substitution> [<postfix>]
366
367 if (!DC) return;
368
369 while (isa<LinkageSpecDecl>(DC))
370 DC = DC->getParent();
371
372 if (DC->isTranslationUnit())
373 return;
374
375 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
376 llvm::SmallString<64> Name;
377 Context.mangleBlock(BD, Name);
378 Out << Name << '@';
379 return manglePostfix(DC->getParent(), NoFunction);
380 }
381
382 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
383 return;
384 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
385 mangleObjCMethodName(Method);
386 else {
387 mangleUnqualifiedName(cast<NamedDecl>(DC));
388 manglePostfix(DC->getParent(), NoFunction);
389 }
390}
391
392void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
393 // <source name> ::= <identifier> @
394 Out << II->getName() << '@';
395}
396
397void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
398 llvm::SmallString<64> Buffer;
399 MiscNameMangler(Context, Buffer).mangleObjCMethodName(MD);
400 Out << Buffer;
401}
402
Charles Davisb021f8b2010-06-14 05:29:01 +0000403void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
404 bool IsMember) {
405 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
406 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
407 // 'I' means __restrict (32/64-bit).
408 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
409 // keyword!
410 // <base-cvr-qualifiers> ::= A # near
411 // ::= B # near const
412 // ::= C # near volatile
413 // ::= D # near const volatile
414 // ::= E # far (16-bit)
415 // ::= F # far const (16-bit)
416 // ::= G # far volatile (16-bit)
417 // ::= H # far const volatile (16-bit)
418 // ::= I # huge (16-bit)
419 // ::= J # huge const (16-bit)
420 // ::= K # huge volatile (16-bit)
421 // ::= L # huge const volatile (16-bit)
422 // ::= M <basis> # based
423 // ::= N <basis> # based const
424 // ::= O <basis> # based volatile
425 // ::= P <basis> # based const volatile
426 // ::= Q # near member
427 // ::= R # near const member
428 // ::= S # near volatile member
429 // ::= T # near const volatile member
430 // ::= U # far member (16-bit)
431 // ::= V # far const member (16-bit)
432 // ::= W # far volatile member (16-bit)
433 // ::= X # far const volatile member (16-bit)
434 // ::= Y # huge member (16-bit)
435 // ::= Z # huge const member (16-bit)
436 // ::= 0 # huge volatile member (16-bit)
437 // ::= 1 # huge const volatile member (16-bit)
438 // ::= 2 <basis> # based member
439 // ::= 3 <basis> # based const member
440 // ::= 4 <basis> # based volatile member
441 // ::= 5 <basis> # based const volatile member
442 // ::= 6 # near function (pointers only)
443 // ::= 7 # far function (pointers only)
444 // ::= 8 # near method (pointers only)
445 // ::= 9 # far method (pointers only)
446 // ::= _A <basis> # based function (pointers only)
447 // ::= _B <basis> # based function (far?) (pointers only)
448 // ::= _C <basis> # based method (pointers only)
449 // ::= _D <basis> # based method (far?) (pointers only)
450 // <basis> ::= 0 # __based(void)
451 // ::= 1 # __based(segment)?
452 // ::= 2 <name> # __based(name)
453 // ::= 3 # ?
454 // ::= 4 # ?
455 // ::= 5 # not really based
456 if (!IsMember) {
457 if (!Quals.hasVolatile()) {
458 if (!Quals.hasConst())
459 Out << 'A';
460 else
461 Out << 'B';
462 } else {
463 if (!Quals.hasConst())
464 Out << 'C';
465 else
466 Out << 'D';
467 }
468 } else {
469 if (!Quals.hasVolatile()) {
470 if (!Quals.hasConst())
471 Out << 'Q';
472 else
473 Out << 'R';
474 } else {
475 if (!Quals.hasConst())
476 Out << 'S';
477 else
478 Out << 'T';
479 }
480 }
481
482 // FIXME: For now, just drop all extension qualifiers on the floor.
483}
484
Charles Davis570d2762010-06-12 08:11:16 +0000485void MicrosoftCXXNameMangler::mangleType(QualType T) {
486 // Only operate on the canonical type!
487 T = getASTContext().getCanonicalType(T);
488
489 switch (T->getTypeClass()) {
490#define ABSTRACT_TYPE(CLASS, PARENT)
491#define NON_CANONICAL_TYPE(CLASS, PARENT) \
492case Type::CLASS: \
493llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
494return;
495#define TYPE(CLASS, PARENT)
496#include "clang/AST/TypeNodes.def"
497 case Type::Builtin:
498 mangleType(static_cast<BuiltinType *>(T.getTypePtr()));
499 break;
500 default:
501 assert(false && "Don't know how to mangle this type!");
502 break;
503 }
504}
505
506void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T) {
507 // <type> ::= <builtin-type>
508 // <builtin-type> ::= X # void
509 // ::= C # signed char
510 // ::= D # char
511 // ::= E # unsigned char
512 // ::= F # short
513 // ::= G # unsigned short (or wchar_t if it's not a builtin)
514 // ::= H # int
515 // ::= I # unsigned int
516 // ::= J # long
517 // ::= K # unsigned long
518 // L # <none>
519 // ::= M # float
520 // ::= N # double
521 // ::= O # long double (__float80 is mangled differently)
522 // ::= _D # __int8 (yup, it's a distinct type in MSVC)
523 // ::= _E # unsigned __int8
524 // ::= _F # __int16
525 // ::= _G # unsigned __int16
526 // ::= _H # __int32
527 // ::= _I # unsigned __int32
528 // ::= _J # long long, __int64
529 // ::= _K # unsigned long long, __int64
530 // ::= _L # __int128
531 // ::= _M # unsigned __int128
532 // ::= _N # bool
533 // _O # <array in parameter>
534 // ::= _T # __float80 (Intel)
535 // ::= _W # wchar_t
536 // ::= _Z # __float80 (Digital Mars)
537 switch (T->getKind()) {
538 case BuiltinType::Void: Out << 'X'; break;
539 case BuiltinType::SChar: Out << 'C'; break;
540 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
541 case BuiltinType::UChar: Out << 'E'; break;
542 case BuiltinType::Short: Out << 'F'; break;
543 case BuiltinType::UShort: Out << 'G'; break;
544 case BuiltinType::Int: Out << 'H'; break;
545 case BuiltinType::UInt: Out << 'I'; break;
546 case BuiltinType::Long: Out << 'J'; break;
547 case BuiltinType::ULong: Out << 'K'; break;
548 case BuiltinType::Float: Out << 'M'; break;
549 case BuiltinType::Double: Out << 'N'; break;
550 // TODO: Determine size and mangle accordingly
551 case BuiltinType::LongDouble: Out << 'O'; break;
552 // TODO: __int8 and friends
553 case BuiltinType::LongLong: Out << "_J"; break;
554 case BuiltinType::ULongLong: Out << "_K"; break;
555 case BuiltinType::Int128: Out << "_L"; break;
556 case BuiltinType::UInt128: Out << "_M"; break;
557 case BuiltinType::Bool: Out << "_N"; break;
558 case BuiltinType::WChar: Out << "_W"; break;
559
560 case BuiltinType::Overload:
561 case BuiltinType::Dependent:
562 assert(false &&
563 "Overloaded and dependent types shouldn't get to name mangling");
564 break;
565 case BuiltinType::UndeducedAuto:
566 assert(0 && "Should not see undeduced auto here");
567 break;
568 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
569 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
570 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
571
572 case BuiltinType::Char16:
573 case BuiltinType::Char32:
574 case BuiltinType::NullPtr:
575 assert(false && "Don't know how to mangle this type");
576 break;
577 }
578}
579
Charles Davis65161d12010-06-16 05:33:16 +0000580// <type> ::= <function-type>
581void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T) {
582 // Structors only appear in decls, so at this point we know it's not a
583 // structor type.
584 mangleType(T, false);
585}
586void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T) {
587 llvm_unreachable("Can't mangle K&R function prototypes");
588}
589
590void MicrosoftCXXNameMangler::mangleType(const FunctionType *T,
591 bool IsStructor) {
592 // <function-type> ::= <calling-convention> <return-type> <argument-list>
593 // <throw-spec>
594 mangleCallingConvention(T);
595
596 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
597
598 // Structors always have a 'void' return type, but MSVC mangles them as an
599 // '@' (because they have no declared return type).
600 if (IsStructor)
601 Out << '@';
602 else
603 mangleType(Proto->getResultType());
604
605 // <argument-list> ::= X # void
606 // ::= <type>+ @
607 // ::= <type>* Z # varargs
608 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
609 Out << 'X';
610 } else {
611 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
612 ArgEnd = Proto->arg_type_end();
613 Arg != ArgEnd; ++Arg)
614 mangleType(*Arg);
615
616 // <builtin-type> ::= Z # ellipsis
617 if (Proto->isVariadic())
618 Out << 'Z';
619 else
620 Out << '@';
621 }
622
623 mangleThrowSpecification(Proto);
624}
625
626void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
627 // <function-class> ::= A # private: near
628 // ::= B # private: far
629 // ::= C # private: static near
630 // ::= D # private: static far
631 // ::= E # private: virtual near
632 // ::= F # private: virtual far
633 // ::= G # private: thunk near
634 // ::= H # private: thunk far
635 // ::= I # protected: near
636 // ::= J # protected: far
637 // ::= K # protected: static near
638 // ::= L # protected: static far
639 // ::= M # protected: virtual near
640 // ::= N # protected: virtual far
641 // ::= O # protected: thunk near
642 // ::= P # protected: thunk far
643 // ::= Q # public: near
644 // ::= R # public: far
645 // ::= S # public: static near
646 // ::= T # public: static far
647 // ::= U # public: virtual near
648 // ::= V # public: virtual far
649 // ::= W # public: thunk near
650 // ::= X # public: thunk far
651 // ::= Y # global near
652 // ::= Z # global far
653 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
654 switch (MD->getAccess()) {
655 default:
656 case AS_private:
657 if (MD->isStatic())
658 Out << 'C';
659 else if (MD->isVirtual())
660 Out << 'E';
661 else
662 Out << 'A';
663 break;
664 case AS_protected:
665 if (MD->isStatic())
666 Out << 'K';
667 else if (MD->isVirtual())
668 Out << 'M';
669 else
670 Out << 'I';
671 break;
672 case AS_public:
673 if (MD->isStatic())
674 Out << 'S';
675 else if (MD->isVirtual())
676 Out << 'U';
677 else
678 Out << 'Q';
679 }
680 } else
681 Out << 'Y';
682}
683void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T) {
684 // <calling-convention> ::= A # __cdecl
685 // ::= B # __export __cdecl
686 // ::= C # __pascal
687 // ::= D # __export __pascal
688 // ::= E # __thiscall
689 // ::= F # __export __thiscall
690 // ::= G # __stdcall
691 // ::= H # __export __stdcall
692 // ::= I # __fastcall
693 // ::= J # __export __fastcall
694 // The 'export' calling conventions are from a bygone era
695 // (*cough*Win16*cough*) when functions were declared for export with
696 // that keyword. (It didn't actually export them, it just made them so
697 // that they could be in a DLL and somebody from another module could call
698 // them.)
699 switch (T->getCallConv()) {
700 case CC_Default:
701 case CC_C: Out << 'A'; break;
702 case CC_X86ThisCall: Out << 'E'; break;
703 case CC_X86StdCall: Out << 'G'; break;
704 case CC_X86FastCall: Out << 'I'; break;
705 }
706}
707void MicrosoftCXXNameMangler::mangleThrowSpecification(
708 const FunctionProtoType *FT) {
709 // <throw-spec> ::= Z # throw(...) (default)
710 // ::= @ # throw() or __declspec/__attribute__((nothrow))
711 // ::= <type>+
712 if (!FT->hasExceptionSpec() || FT->hasAnyExceptionSpec())
713 Out << 'Z';
714 else {
715 for (unsigned Exception = 0, NumExceptions = FT->getNumExceptions();
716 Exception < NumExceptions;
717 ++Exception)
718 mangleType(FT->getExceptionType(Exception).getLocalUnqualifiedType());
719 Out << '@';
720 }
721}
722
Charles Davisc3926642010-06-09 23:25:41 +0000723void MicrosoftMangleContext::mangleName(const NamedDecl *D,
724 llvm::SmallVectorImpl<char> &Name) {
Charles Davise60cea82010-06-11 03:07:32 +0000725 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
726 "Invalid mangleName() call, argument is not a variable or function!");
727 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
728 "Invalid mangleName() call on 'structor decl!");
729
730 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
731 getASTContext().getSourceManager(),
732 "Mangling declaration");
733
734 MicrosoftCXXNameMangler Mangler(*this, Name);
735 return Mangler.mangle(D);
Charles Davisc3926642010-06-09 23:25:41 +0000736}
737void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
738 const ThunkInfo &Thunk,
739 llvm::SmallVectorImpl<char> &) {
740 assert(false && "Can't yet mangle thunks!");
741}
742void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
743 CXXDtorType Type,
744 const ThisAdjustment &,
745 llvm::SmallVectorImpl<char> &) {
746 assert(false && "Can't yet mangle destructor thunks!");
747}
748void MicrosoftMangleContext::mangleGuardVariable(const VarDecl *D,
749 llvm::SmallVectorImpl<char> &) {
750 assert(false && "Can't yet mangle guard variables!");
751}
752void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
753 llvm::SmallVectorImpl<char> &) {
754 assert(false && "Can't yet mangle virtual tables!");
755}
756void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
757 llvm::SmallVectorImpl<char> &) {
758 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
759}
760void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
761 int64_t Offset,
762 const CXXRecordDecl *Type,
763 llvm::SmallVectorImpl<char> &) {
764 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
765}
766void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
767 llvm::SmallVectorImpl<char> &) {
768 assert(false && "Can't yet mangle RTTI!");
769}
770void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
771 llvm::SmallVectorImpl<char> &) {
772 assert(false && "Can't yet mangle RTTI names!");
773}
774void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
775 CXXCtorType Type,
776 llvm::SmallVectorImpl<char> &) {
777 assert(false && "Can't yet mangle constructors!");
778}
779void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
780 CXXDtorType Type,
781 llvm::SmallVectorImpl<char> &) {
782 assert(false && "Can't yet mangle destructors!");
783}
784
Charles Davis98b7c5c2010-06-11 01:06:47 +0000785CXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) {
Charles Davisc3926642010-06-09 23:25:41 +0000786 return new MicrosoftCXXABI(CGM);
787}
788