1 //===--- MicrosoftCXXABI.cpp - Emit LLVM Code from ASTs for a Module ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
15 //===----------------------------------------------------------------------===//
18 #include "CodeGenModule.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"
27 using namespace clang;
28 using namespace CodeGen;
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;
38 ASTContext &getASTContext() const { return Context.getASTContext(); }
41 MicrosoftCXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res)
42 : Context(C), Out(Res) { }
44 void mangle(const NamedDecl *D, llvm::StringRef Prefix = "?");
45 void mangleName(const NamedDecl *ND);
46 void mangleFunctionEncoding(const FunctionDecl *FD);
47 void mangleVariableEncoding(const VarDecl *VD);
48 void mangleNumber(int64_t Number);
49 void mangleType(QualType T);
52 void mangleUnqualifiedName(const NamedDecl *ND) {
53 mangleUnqualifiedName(ND, ND->getDeclName());
55 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
56 void mangleSourceName(const IdentifierInfo *II);
57 void manglePostfix(const DeclContext *DC, bool NoFunction=false);
58 void mangleOperatorName(OverloadedOperatorKind OO);
59 void mangleQualifiers(Qualifiers Quals, bool IsMember);
61 void mangleObjCMethodName(const ObjCMethodDecl *MD);
63 // 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"
69 void mangleType(const TagType*);
70 void mangleType(const FunctionType *T, const FunctionDecl *D,
71 bool IsStructor, bool IsInstMethod);
72 void mangleType(const ArrayType *T, bool IsGlobal);
73 void mangleExtraDimensions(QualType T);
74 void mangleFunctionClass(const FunctionDecl *FD);
75 void mangleCallingConvention(const FunctionType *T);
76 void mangleThrowSpecification(const FunctionProtoType *T);
80 /// MicrosoftMangleContext - Overrides the default MangleContext for the
81 /// Microsoft Visual C++ ABI.
82 class MicrosoftMangleContext : public MangleContext {
84 MicrosoftMangleContext(ASTContext &Context,
85 Diagnostic &Diags) : MangleContext(Context, Diags) { }
86 virtual bool shouldMangleDeclName(const NamedDecl *D);
87 virtual void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &);
88 virtual void mangleThunk(const CXXMethodDecl *MD,
89 const ThunkInfo &Thunk,
90 llvm::SmallVectorImpl<char> &);
91 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
92 const ThisAdjustment &ThisAdjustment,
93 llvm::SmallVectorImpl<char> &);
94 virtual void mangleGuardVariable(const VarDecl *D,
95 llvm::SmallVectorImpl<char> &);
96 virtual void mangleCXXVTable(const CXXRecordDecl *RD,
97 llvm::SmallVectorImpl<char> &);
98 virtual void mangleCXXVTT(const CXXRecordDecl *RD,
99 llvm::SmallVectorImpl<char> &);
100 virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
101 const CXXRecordDecl *Type,
102 llvm::SmallVectorImpl<char> &);
103 virtual void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &);
104 virtual void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &);
105 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
106 llvm::SmallVectorImpl<char> &);
107 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
108 llvm::SmallVectorImpl<char> &);
111 class MicrosoftCXXABI : public CGCXXABI {
112 MicrosoftMangleContext MangleCtx;
114 MicrosoftCXXABI(CodeGenModule &CGM)
115 : CGCXXABI(CGM), MangleCtx(CGM.getContext(), CGM.getDiags()) {}
117 MicrosoftMangleContext &getMangleContext() {
121 void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
124 llvm::SmallVectorImpl<CanQualType> &ArgTys) {
125 // 'this' is already in place
126 // TODO: 'for base' flag
129 void BuildDestructorSignature(const CXXDestructorDecl *Ctor,
132 llvm::SmallVectorImpl<CanQualType> &ArgTys) {
133 // 'this' is already in place
134 // TODO: 'for base' flag
137 void BuildInstanceFunctionParams(CodeGenFunction &CGF,
139 FunctionArgList &Params) {
140 BuildThisParam(CGF, Params);
141 // TODO: 'for base' flag
144 void EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
146 // TODO: 'for base' flag
152 static bool isInCLinkageSpecification(const Decl *D) {
153 D = D->getCanonicalDecl();
154 for (const DeclContext *DC = D->getDeclContext();
155 !DC->isTranslationUnit(); DC = DC->getParent()) {
156 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
157 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
163 bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
164 // In C, functions with no attributes never need to be mangled. Fastpath them.
165 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
168 // Any decl can be declared with __asm("foo") on it, and this takes precedence
169 // over all other naming in the .o file.
170 if (D->hasAttr<AsmLabelAttr>())
173 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
174 // (always) as does passing a C++ member function and a function
175 // whose name is not a simple identifier.
176 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
177 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
178 !FD->getDeclName().isIdentifier()))
181 // Otherwise, no mangling is done outside C++ mode.
182 if (!getASTContext().getLangOptions().CPlusPlus)
185 // Variables at global scope with internal linkage are not mangled.
187 const DeclContext *DC = D->getDeclContext();
188 if (DC->isTranslationUnit() && D->getLinkage() == InternalLinkage)
192 // C functions and "main" are not mangled.
193 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
199 void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
200 llvm::StringRef Prefix) {
201 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
202 // Therefore it's really important that we don't decorate the
203 // name with leading underscores or leading/trailing at signs. So, emit a
204 // asm marker at the start so we get the name right.
205 Out << '\01'; // LLVM IR Marker for __asm("foo")
207 // Any decl can be declared with __asm("foo") on it, and this takes precedence
208 // over all other naming in the .o file.
209 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
210 // If we have an asm name, then we use it as the mangling.
211 Out << ALA->getLabel();
215 // <mangled-name> ::= ? <name> <type-encoding>
218 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
219 mangleFunctionEncoding(FD);
220 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
221 mangleVariableEncoding(VD);
222 // TODO: Fields? Can MSVC even mangle them?
225 void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
226 // <type-encoding> ::= <function-class> <function-type>
228 // Don't mangle in the type if this isn't a decl we should typically mangle.
229 if (!Context.shouldMangleDeclName(FD))
232 // We should never ever see a FunctionNoProtoType at this point.
233 // We don't even know how to mangle their types anyway :).
234 const FunctionProtoType *FT = cast<FunctionProtoType>(FD->getType());
236 bool InStructor = false, InInstMethod = false;
237 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
239 if (MD->isInstance())
241 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
245 // First, the function class.
246 mangleFunctionClass(FD);
248 mangleType(FT, FD, InStructor, InInstMethod);
251 void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
252 // <type-encoding> ::= <storage-class> <variable-type>
253 // <storage-class> ::= 0 # private static member
254 // ::= 1 # protected static member
255 // ::= 2 # public static member
257 // ::= 4 # static local
259 // The first character in the encoding (after the name) is the storage class.
260 if (VD->isStaticDataMember()) {
261 // If it's a static member, it also encodes the access level.
262 switch (VD->getAccess()) {
264 case AS_private: Out << '0'; break;
265 case AS_protected: Out << '1'; break;
266 case AS_public: Out << '2'; break;
269 else if (!VD->isStaticLocal())
273 // Now mangle the type.
274 // <variable-type> ::= <type> <cvr-qualifiers>
275 // ::= <type> A # pointers, references, arrays
276 // Pointers and references are odd. The type of 'int * const foo;' gets
277 // mangled as 'QAHA' instead of 'PAHB', for example.
278 QualType Ty = VD->getType();
279 if (Ty->isPointerType() || Ty->isReferenceType()) {
282 } else if (Ty->isArrayType()) {
283 // Global arrays are funny, too.
284 mangleType(static_cast<ArrayType *>(Ty.getTypePtr()), true);
287 mangleType(Ty.getLocalUnqualifiedType());
288 mangleQualifiers(Ty.getLocalQualifiers(), false);
292 void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
293 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
294 const DeclContext *DC = ND->getDeclContext();
296 // Always start with the unqualified name.
297 mangleUnqualifiedName(ND);
299 // If this is an extern variable declared locally, the relevant DeclContext
300 // is that of the containing namespace, or the translation unit.
301 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
302 while (!DC->isNamespace() && !DC->isTranslationUnit())
303 DC = DC->getParent();
307 // Terminate the whole name with an '@'.
311 void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
312 // <number> ::= [?] <decimal digit> # <= 9
313 // ::= [?] <hex digit>+ @ # > 9; A = 0, B = 1, etc...
318 if (Number >= 1 && Number <= 10) {
321 // We have to build up the encoding in reverse order, so it will come
322 // out right when we write it out.
324 char *EndPtr = Encoding+sizeof(Encoding);
325 char *CurPtr = EndPtr;
327 *--CurPtr = 'A' + (Number % 16);
330 Out.write(CurPtr, EndPtr-CurPtr);
336 MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
337 DeclarationName Name) {
338 // <unqualified-name> ::= <operator-name>
339 // ::= <ctor-dtor-name>
341 switch (Name.getNameKind()) {
342 case DeclarationName::Identifier: {
343 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
344 mangleSourceName(II);
348 // Otherwise, an anonymous entity. We must have a declaration.
349 assert(ND && "mangling empty name without declaration");
351 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
352 if (NS->isAnonymousNamespace()) {
358 // We must have an anonymous struct.
359 const TagDecl *TD = cast<TagDecl>(ND);
360 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
361 assert(TD->getDeclContext() == D->getDeclContext() &&
362 "Typedef should not be in another decl context!");
363 assert(D->getDeclName().getAsIdentifierInfo() &&
364 "Typedef was not named!");
365 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
369 // When VC encounters an anonymous type with no tag and no typedef,
370 // it literally emits '<unnamed-tag>'.
371 Out << "<unnamed-tag>";
375 case DeclarationName::ObjCZeroArgSelector:
376 case DeclarationName::ObjCOneArgSelector:
377 case DeclarationName::ObjCMultiArgSelector:
378 assert(false && "Can't mangle Objective-C selector names here!");
381 case DeclarationName::CXXConstructorName:
382 assert(false && "Can't mangle constructors yet!");
385 case DeclarationName::CXXDestructorName:
386 assert(false && "Can't mangle destructors yet!");
389 case DeclarationName::CXXConversionFunctionName:
390 // <operator-name> ::= ?B # (cast)
391 // The target type is encoded as the return type.
395 case DeclarationName::CXXOperatorName:
396 mangleOperatorName(Name.getCXXOverloadedOperator());
399 case DeclarationName::CXXLiteralOperatorName:
400 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
401 assert(false && "Don't know how to mangle literal operators yet!");
404 case DeclarationName::CXXUsingDirective:
405 assert(false && "Can't mangle a using directive name!");
410 void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
412 // <postfix> ::= <unqualified-name> [<postfix>]
413 // ::= <template-postfix> <template-args> [<postfix>]
414 // ::= <template-param>
415 // ::= <substitution> [<postfix>]
419 while (isa<LinkageSpecDecl>(DC))
420 DC = DC->getParent();
422 if (DC->isTranslationUnit())
425 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
426 llvm::SmallString<64> Name;
427 Context.mangleBlock(GlobalDecl(), BD, Name);
429 return manglePostfix(DC->getParent(), NoFunction);
432 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
434 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
435 mangleObjCMethodName(Method);
437 mangleUnqualifiedName(cast<NamedDecl>(DC));
438 manglePostfix(DC->getParent(), NoFunction);
442 void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO) {
446 // <operator-name> ::= ?2 # new
447 case OO_New: Out << "?2"; break;
448 // <operator-name> ::= ?3 # delete
449 case OO_Delete: Out << "?3"; break;
450 // <operator-name> ::= ?4 # =
451 case OO_Equal: Out << "?4"; break;
452 // <operator-name> ::= ?5 # >>
453 case OO_GreaterGreater: Out << "?5"; break;
454 // <operator-name> ::= ?6 # <<
455 case OO_LessLess: Out << "?6"; break;
456 // <operator-name> ::= ?7 # !
457 case OO_Exclaim: Out << "?7"; break;
458 // <operator-name> ::= ?8 # ==
459 case OO_EqualEqual: Out << "?8"; break;
460 // <operator-name> ::= ?9 # !=
461 case OO_ExclaimEqual: Out << "?9"; break;
462 // <operator-name> ::= ?A # []
463 case OO_Subscript: Out << "?A"; break;
465 // <operator-name> ::= ?C # ->
466 case OO_Arrow: Out << "?C"; break;
467 // <operator-name> ::= ?D # *
468 case OO_Star: Out << "?D"; break;
469 // <operator-name> ::= ?E # ++
470 case OO_PlusPlus: Out << "?E"; break;
471 // <operator-name> ::= ?F # --
472 case OO_MinusMinus: Out << "?F"; break;
473 // <operator-name> ::= ?G # -
474 case OO_Minus: Out << "?G"; break;
475 // <operator-name> ::= ?H # +
476 case OO_Plus: Out << "?H"; break;
477 // <operator-name> ::= ?I # &
478 case OO_Amp: Out << "?I"; break;
479 // <operator-name> ::= ?J # ->*
480 case OO_ArrowStar: Out << "?J"; break;
481 // <operator-name> ::= ?K # /
482 case OO_Slash: Out << "?K"; break;
483 // <operator-name> ::= ?L # %
484 case OO_Percent: Out << "?L"; break;
485 // <operator-name> ::= ?M # <
486 case OO_Less: Out << "?M"; break;
487 // <operator-name> ::= ?N # <=
488 case OO_LessEqual: Out << "?N"; break;
489 // <operator-name> ::= ?O # >
490 case OO_Greater: Out << "?O"; break;
491 // <operator-name> ::= ?P # >=
492 case OO_GreaterEqual: Out << "?P"; break;
493 // <operator-name> ::= ?Q # ,
494 case OO_Comma: Out << "?Q"; break;
495 // <operator-name> ::= ?R # ()
496 case OO_Call: Out << "?R"; break;
497 // <operator-name> ::= ?S # ~
498 case OO_Tilde: Out << "?S"; break;
499 // <operator-name> ::= ?T # ^
500 case OO_Caret: Out << "?T"; break;
501 // <operator-name> ::= ?U # |
502 case OO_Pipe: Out << "?U"; break;
503 // <operator-name> ::= ?V # &&
504 case OO_AmpAmp: Out << "?V"; break;
505 // <operator-name> ::= ?W # ||
506 case OO_PipePipe: Out << "?W"; break;
507 // <operator-name> ::= ?X # *=
508 case OO_StarEqual: Out << "?X"; break;
509 // <operator-name> ::= ?Y # +=
510 case OO_PlusEqual: Out << "?Y"; break;
511 // <operator-name> ::= ?Z # -=
512 case OO_MinusEqual: Out << "?Z"; break;
513 // <operator-name> ::= ?_0 # /=
514 case OO_SlashEqual: Out << "?_0"; break;
515 // <operator-name> ::= ?_1 # %=
516 case OO_PercentEqual: Out << "?_1"; break;
517 // <operator-name> ::= ?_2 # >>=
518 case OO_GreaterGreaterEqual: Out << "?_2"; break;
519 // <operator-name> ::= ?_3 # <<=
520 case OO_LessLessEqual: Out << "?_3"; break;
521 // <operator-name> ::= ?_4 # &=
522 case OO_AmpEqual: Out << "?_4"; break;
523 // <operator-name> ::= ?_5 # |=
524 case OO_PipeEqual: Out << "?_5"; break;
525 // <operator-name> ::= ?_6 # ^=
526 case OO_CaretEqual: Out << "?_6"; break;
531 // ?_B # local static guard
533 // ?_D # vbase destructor
534 // ?_E # vector deleting destructor
535 // ?_F # default constructor closure
536 // ?_G # scalar deleting destructor
537 // ?_H # vector constructor iterator
538 // ?_I # vector destructor iterator
539 // ?_J # vector vbase constructor iterator
540 // ?_K # virtual displacement map
541 // ?_L # eh vector constructor iterator
542 // ?_M # eh vector destructor iterator
543 // ?_N # eh vector vbase constructor iterator
544 // ?_O # copy constructor closure
545 // ?_P<name> # udt returning <name>
547 // ?_R0 # RTTI Type Descriptor
548 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
549 // ?_R2 # RTTI Base Class Array
550 // ?_R3 # RTTI Class Hierarchy Descriptor
551 // ?_R4 # RTTI Complete Object Locator
552 // ?_S # local vftable
553 // ?_T # local vftable constructor closure
554 // <operator-name> ::= ?_U # new[]
555 case OO_Array_New: Out << "?_U"; break;
556 // <operator-name> ::= ?_V # delete[]
557 case OO_Array_Delete: Out << "?_V"; break;
560 assert(false && "Don't know how to mangle ?:");
564 case NUM_OVERLOADED_OPERATORS:
565 assert(false && "Not an overloaded operator");
570 void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
571 // <source name> ::= <identifier> @
572 Out << II->getName() << '@';
575 void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
576 llvm::SmallString<64> Buffer;
577 MiscNameMangler(Context, Buffer).mangleObjCMethodName(MD);
581 void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
583 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
584 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
585 // 'I' means __restrict (32/64-bit).
586 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
588 // <base-cvr-qualifiers> ::= A # near
589 // ::= B # near const
590 // ::= C # near volatile
591 // ::= D # near const volatile
592 // ::= E # far (16-bit)
593 // ::= F # far const (16-bit)
594 // ::= G # far volatile (16-bit)
595 // ::= H # far const volatile (16-bit)
596 // ::= I # huge (16-bit)
597 // ::= J # huge const (16-bit)
598 // ::= K # huge volatile (16-bit)
599 // ::= L # huge const volatile (16-bit)
600 // ::= M <basis> # based
601 // ::= N <basis> # based const
602 // ::= O <basis> # based volatile
603 // ::= P <basis> # based const volatile
604 // ::= Q # near member
605 // ::= R # near const member
606 // ::= S # near volatile member
607 // ::= T # near const volatile member
608 // ::= U # far member (16-bit)
609 // ::= V # far const member (16-bit)
610 // ::= W # far volatile member (16-bit)
611 // ::= X # far const volatile member (16-bit)
612 // ::= Y # huge member (16-bit)
613 // ::= Z # huge const member (16-bit)
614 // ::= 0 # huge volatile member (16-bit)
615 // ::= 1 # huge const volatile member (16-bit)
616 // ::= 2 <basis> # based member
617 // ::= 3 <basis> # based const member
618 // ::= 4 <basis> # based volatile member
619 // ::= 5 <basis> # based const volatile member
620 // ::= 6 # near function (pointers only)
621 // ::= 7 # far function (pointers only)
622 // ::= 8 # near method (pointers only)
623 // ::= 9 # far method (pointers only)
624 // ::= _A <basis> # based function (pointers only)
625 // ::= _B <basis> # based function (far?) (pointers only)
626 // ::= _C <basis> # based method (pointers only)
627 // ::= _D <basis> # based method (far?) (pointers only)
628 // ::= _E # block (Clang)
629 // <basis> ::= 0 # __based(void)
630 // ::= 1 # __based(segment)?
631 // ::= 2 <name> # __based(name)
634 // ::= 5 # not really based
636 if (!Quals.hasVolatile()) {
637 if (!Quals.hasConst())
642 if (!Quals.hasConst())
648 if (!Quals.hasVolatile()) {
649 if (!Quals.hasConst())
654 if (!Quals.hasConst())
661 // FIXME: For now, just drop all extension qualifiers on the floor.
664 void MicrosoftCXXNameMangler::mangleType(QualType T) {
665 // Only operate on the canonical type!
666 T = getASTContext().getCanonicalType(T);
668 Qualifiers Quals = T.getLocalQualifiers();
670 // We have to mangle these now, while we still have enough information.
671 // <pointer-cvr-qualifiers> ::= P # pointer
672 // ::= Q # const pointer
673 // ::= R # volatile pointer
674 // ::= S # const volatile pointer
675 if (T->isAnyPointerType() || T->isMemberPointerType() ||
676 T->isBlockPointerType()) {
677 if (!Quals.hasVolatile())
680 if (!Quals.hasConst())
686 // Just emit qualifiers like normal.
687 // NB: When we mangle a pointer/reference type, and the pointee
688 // type has no qualifiers, the lack of qualifier gets mangled
690 mangleQualifiers(Quals, false);
691 } else if (T->isAnyPointerType() || T->isMemberPointerType() ||
692 T->isBlockPointerType()) {
695 switch (T->getTypeClass()) {
696 #define ABSTRACT_TYPE(CLASS, PARENT)
697 #define NON_CANONICAL_TYPE(CLASS, PARENT) \
699 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
701 #define TYPE(CLASS, PARENT) \
703 mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \
705 #include "clang/AST/TypeNodes.def"
709 void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T) {
710 // <type> ::= <builtin-type>
711 // <builtin-type> ::= X # void
712 // ::= C # signed char
714 // ::= E # unsigned char
716 // ::= G # unsigned short (or wchar_t if it's not a builtin)
718 // ::= I # unsigned int
720 // ::= K # unsigned long
724 // ::= O # long double (__float80 is mangled differently)
725 // ::= _D # __int8 (yup, it's a distinct type in MSVC)
726 // ::= _E # unsigned __int8
728 // ::= _G # unsigned __int16
730 // ::= _I # unsigned __int32
731 // ::= _J # long long, __int64
732 // ::= _K # unsigned long long, __int64
734 // ::= _M # unsigned __int128
736 // _O # <array in parameter>
737 // ::= _T # __float80 (Intel)
739 // ::= _Z # __float80 (Digital Mars)
740 switch (T->getKind()) {
741 case BuiltinType::Void: Out << 'X'; break;
742 case BuiltinType::SChar: Out << 'C'; break;
743 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
744 case BuiltinType::UChar: Out << 'E'; break;
745 case BuiltinType::Short: Out << 'F'; break;
746 case BuiltinType::UShort: Out << 'G'; break;
747 case BuiltinType::Int: Out << 'H'; break;
748 case BuiltinType::UInt: Out << 'I'; break;
749 case BuiltinType::Long: Out << 'J'; break;
750 case BuiltinType::ULong: Out << 'K'; break;
751 case BuiltinType::Float: Out << 'M'; break;
752 case BuiltinType::Double: Out << 'N'; break;
753 // TODO: Determine size and mangle accordingly
754 case BuiltinType::LongDouble: Out << 'O'; break;
755 // TODO: __int8 and friends
756 case BuiltinType::LongLong: Out << "_J"; break;
757 case BuiltinType::ULongLong: Out << "_K"; break;
758 case BuiltinType::Int128: Out << "_L"; break;
759 case BuiltinType::UInt128: Out << "_M"; break;
760 case BuiltinType::Bool: Out << "_N"; break;
761 case BuiltinType::WChar: Out << "_W"; break;
763 case BuiltinType::Overload:
764 case BuiltinType::Dependent:
766 "Overloaded and dependent types shouldn't get to name mangling");
768 case BuiltinType::UndeducedAuto:
769 assert(0 && "Should not see undeduced auto here");
771 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
772 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
773 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
775 case BuiltinType::Char16:
776 case BuiltinType::Char32:
777 case BuiltinType::NullPtr:
778 assert(false && "Don't know how to mangle this type");
783 // <type> ::= <function-type>
784 void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T) {
785 // Structors only appear in decls, so at this point we know it's not a
787 // I'll probably have mangleType(MemberPointerType) call the mangleType()
789 mangleType(T, NULL, false, false);
791 void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T) {
792 llvm_unreachable("Can't mangle K&R function prototypes");
795 void MicrosoftCXXNameMangler::mangleType(const FunctionType *T,
796 const FunctionDecl *D,
799 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
800 // <return-type> <argument-list> <throw-spec>
801 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
803 // If this is a C++ instance method, mangle the CVR qualifiers for the
806 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
808 mangleCallingConvention(T);
810 // <return-type> ::= <type>
811 // ::= @ # structors (they have no declared return type)
815 mangleType(Proto->getResultType());
817 // <argument-list> ::= X # void
819 // ::= <type>* Z # varargs
820 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
824 // If we got a decl, use the "types-as-written" to make sure arrays
825 // get mangled right.
826 for (FunctionDecl::param_const_iterator Parm = D->param_begin(),
827 ParmEnd = D->param_end();
828 Parm != ParmEnd; ++Parm)
829 mangleType((*Parm)->getTypeSourceInfo()->getType());
831 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
832 ArgEnd = Proto->arg_type_end();
833 Arg != ArgEnd; ++Arg)
836 // <builtin-type> ::= Z # ellipsis
837 if (Proto->isVariadic())
843 mangleThrowSpecification(Proto);
846 void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
847 // <function-class> ::= A # private: near
848 // ::= B # private: far
849 // ::= C # private: static near
850 // ::= D # private: static far
851 // ::= E # private: virtual near
852 // ::= F # private: virtual far
853 // ::= G # private: thunk near
854 // ::= H # private: thunk far
855 // ::= I # protected: near
856 // ::= J # protected: far
857 // ::= K # protected: static near
858 // ::= L # protected: static far
859 // ::= M # protected: virtual near
860 // ::= N # protected: virtual far
861 // ::= O # protected: thunk near
862 // ::= P # protected: thunk far
863 // ::= Q # public: near
864 // ::= R # public: far
865 // ::= S # public: static near
866 // ::= T # public: static far
867 // ::= U # public: virtual near
868 // ::= V # public: virtual far
869 // ::= W # public: thunk near
870 // ::= X # public: thunk far
871 // ::= Y # global near
872 // ::= Z # global far
873 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
874 switch (MD->getAccess()) {
879 else if (MD->isVirtual())
887 else if (MD->isVirtual())
895 else if (MD->isVirtual())
903 void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T) {
904 // <calling-convention> ::= A # __cdecl
905 // ::= B # __export __cdecl
907 // ::= D # __export __pascal
908 // ::= E # __thiscall
909 // ::= F # __export __thiscall
911 // ::= H # __export __stdcall
912 // ::= I # __fastcall
913 // ::= J # __export __fastcall
914 // The 'export' calling conventions are from a bygone era
915 // (*cough*Win16*cough*) when functions were declared for export with
916 // that keyword. (It didn't actually export them, it just made them so
917 // that they could be in a DLL and somebody from another module could call
919 switch (T->getCallConv()) {
921 case CC_C: Out << 'A'; break;
922 case CC_X86Pascal: Out << 'C'; break;
923 case CC_X86ThisCall: Out << 'E'; break;
924 case CC_X86StdCall: Out << 'G'; break;
925 case CC_X86FastCall: Out << 'I'; break;
928 void MicrosoftCXXNameMangler::mangleThrowSpecification(
929 const FunctionProtoType *FT) {
930 // <throw-spec> ::= Z # throw(...) (default)
931 // ::= @ # throw() or __declspec/__attribute__((nothrow))
933 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
934 // all actually mangled as 'Z'. (They're ignored because their associated
935 // functionality isn't implemented, and probably never will be.)
939 void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T) {
940 assert(false && "Don't know how to mangle UnresolvedUsingTypes yet!");
943 // <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
944 // <union-type> ::= T <name>
945 // <struct-type> ::= U <name>
946 // <class-type> ::= V <name>
947 // <enum-type> ::= W <size> <name>
948 void MicrosoftCXXNameMangler::mangleType(const EnumType *T) {
949 mangleType(static_cast<const TagType*>(T));
951 void MicrosoftCXXNameMangler::mangleType(const RecordType *T) {
952 mangleType(static_cast<const TagType*>(T));
954 void MicrosoftCXXNameMangler::mangleType(const TagType *T) {
955 switch (T->getDecl()->getTagKind()) {
967 Out << getASTContext().getTypeSizeInChars(
968 cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity();
971 mangleName(T->getDecl());
974 // <type> ::= <array-type>
975 // <array-type> ::= P <cvr-qualifiers> [Y <dimension-count> <dimension>+]
976 // <element-type> # as global
977 // ::= Q <cvr-qualifiers> [Y <dimension-count> <dimension>+]
978 // <element-type> # as param
979 // It's supposed to be the other way around, but for some strange reason, it
980 // isn't. Today this behavior is retained for the sole purpose of backwards
982 void MicrosoftCXXNameMangler::mangleType(const ArrayType *T, bool IsGlobal) {
983 // This isn't a recursive mangling, so now we have to do it all in this
989 mangleExtraDimensions(T->getElementType());
991 void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T) {
992 mangleType(static_cast<const ArrayType *>(T), false);
994 void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T) {
995 mangleType(static_cast<const ArrayType *>(T), false);
997 void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T) {
998 mangleType(static_cast<const ArrayType *>(T), false);
1000 void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T) {
1001 mangleType(static_cast<const ArrayType *>(T), false);
1003 void MicrosoftCXXNameMangler::mangleExtraDimensions(QualType ElementTy) {
1004 llvm::SmallVector<llvm::APInt, 3> Dimensions;
1006 if (ElementTy->isConstantArrayType()) {
1007 const ConstantArrayType *CAT =
1008 static_cast<const ConstantArrayType *>(ElementTy.getTypePtr());
1009 Dimensions.push_back(CAT->getSize());
1010 ElementTy = CAT->getElementType();
1011 } else if (ElementTy->isVariableArrayType()) {
1012 assert(false && "Don't know how to mangle VLAs!");
1013 } else if (ElementTy->isDependentSizedArrayType()) {
1014 // The dependent expression has to be folded into a constant (TODO).
1015 assert(false && "Don't know how to mangle dependent-sized arrays!");
1016 } else if (ElementTy->isIncompleteArrayType()) continue;
1019 mangleQualifiers(ElementTy.getQualifiers(), false);
1020 // If there are any additional dimensions, mangle them now.
1021 if (Dimensions.size() > 0) {
1023 // <dimension-count> ::= <number> # number of extra dimensions
1024 mangleNumber(Dimensions.size());
1025 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim) {
1026 mangleNumber(Dimensions[Dim].getLimitedValue());
1029 mangleType(ElementTy.getLocalUnqualifiedType());
1032 // <type> ::= <pointer-to-member-type>
1033 // <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1034 // <class name> <type>
1035 void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T) {
1036 QualType PointeeType = T->getPointeeType();
1037 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
1039 mangleName(cast<RecordType>(T->getClass())->getDecl());
1040 mangleType(FPT, NULL, false, true);
1042 mangleQualifiers(PointeeType.getQualifiers(), true);
1043 mangleName(cast<RecordType>(T->getClass())->getDecl());
1044 mangleType(PointeeType.getLocalUnqualifiedType());
1048 void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T) {
1049 assert(false && "Don't know how to mangle TemplateTypeParmTypes yet!");
1052 // <type> ::= <pointer-type>
1053 // <pointer-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1054 void MicrosoftCXXNameMangler::mangleType(const PointerType *T) {
1055 QualType PointeeTy = T->getPointeeType();
1056 if (PointeeTy->isArrayType()) {
1057 // Pointers to arrays are mangled like arrays.
1058 mangleExtraDimensions(T->getPointeeType());
1059 } else if (PointeeTy->isFunctionType()) {
1060 // Function pointers are special.
1062 mangleType(static_cast<const FunctionType *>(PointeeTy.getTypePtr()),
1063 NULL, false, false);
1065 if (!PointeeTy.hasQualifiers())
1066 // Lack of qualifiers is mangled as 'A'.
1068 mangleType(PointeeTy);
1071 void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1072 // Object pointers never have qualifiers.
1074 mangleType(T->getPointeeType());
1077 // <type> ::= <reference-type>
1078 // <reference-type> ::= A <cvr-qualifiers> <type>
1079 void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T) {
1081 QualType PointeeTy = T->getPointeeType();
1082 if (!PointeeTy.hasQualifiers())
1083 // Lack of qualifiers is mangled as 'A'.
1085 mangleType(PointeeTy);
1088 void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T) {
1089 assert(false && "Don't know how to mangle RValueReferenceTypes yet!");
1092 void MicrosoftCXXNameMangler::mangleType(const ComplexType *T) {
1093 assert(false && "Don't know how to mangle ComplexTypes yet!");
1096 void MicrosoftCXXNameMangler::mangleType(const VectorType *T) {
1097 assert(false && "Don't know how to mangle VectorTypes yet!");
1099 void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T) {
1100 assert(false && "Don't know how to mangle ExtVectorTypes yet!");
1102 void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
1103 assert(false && "Don't know how to mangle DependentSizedExtVectorTypes yet!");
1106 void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1107 // ObjC interfaces have structs underlying them.
1109 mangleName(T->getDecl());
1112 void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T) {
1113 // We don't allow overloading by different protocol qualification,
1114 // so mangling them isn't necessary.
1115 mangleType(T->getBaseType());
1118 void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T) {
1120 mangleType(T->getPointeeType());
1123 void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T) {
1124 assert(false && "Don't know how to mangle InjectedClassNameTypes yet!");
1127 void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T) {
1128 assert(false && "Don't know how to mangle TemplateSpecializationTypes yet!");
1131 void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T) {
1132 assert(false && "Don't know how to mangle DependentNameTypes yet!");
1135 void MicrosoftCXXNameMangler::mangleType(
1136 const DependentTemplateSpecializationType *T) {
1138 "Don't know how to mangle DependentTemplateSpecializationTypes yet!");
1141 void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T) {
1142 assert(false && "Don't know how to mangle TypeOfTypes yet!");
1145 void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T) {
1146 assert(false && "Don't know how to mangle TypeOfExprTypes yet!");
1149 void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T) {
1150 assert(false && "Don't know how to mangle DecltypeTypes yet!");
1153 void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1154 llvm::SmallVectorImpl<char> &Name) {
1155 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1156 "Invalid mangleName() call, argument is not a variable or function!");
1157 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1158 "Invalid mangleName() call on 'structor decl!");
1160 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1161 getASTContext().getSourceManager(),
1162 "Mangling declaration");
1164 MicrosoftCXXNameMangler Mangler(*this, Name);
1165 return Mangler.mangle(D);
1167 void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1168 const ThunkInfo &Thunk,
1169 llvm::SmallVectorImpl<char> &) {
1170 assert(false && "Can't yet mangle thunks!");
1172 void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1174 const ThisAdjustment &,
1175 llvm::SmallVectorImpl<char> &) {
1176 assert(false && "Can't yet mangle destructor thunks!");
1178 void MicrosoftMangleContext::mangleGuardVariable(const VarDecl *D,
1179 llvm::SmallVectorImpl<char> &) {
1180 assert(false && "Can't yet mangle guard variables!");
1182 void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1183 llvm::SmallVectorImpl<char> &) {
1184 assert(false && "Can't yet mangle virtual tables!");
1186 void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1187 llvm::SmallVectorImpl<char> &) {
1188 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1190 void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1192 const CXXRecordDecl *Type,
1193 llvm::SmallVectorImpl<char> &) {
1194 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1196 void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1197 llvm::SmallVectorImpl<char> &) {
1198 assert(false && "Can't yet mangle RTTI!");
1200 void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1201 llvm::SmallVectorImpl<char> &) {
1202 assert(false && "Can't yet mangle RTTI names!");
1204 void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1206 llvm::SmallVectorImpl<char> &) {
1207 assert(false && "Can't yet mangle constructors!");
1209 void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1211 llvm::SmallVectorImpl<char> &) {
1212 assert(false && "Can't yet mangle destructors!");
1215 CGCXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) {
1216 return new MicrosoftCXXABI(CGM);