clang API Documentation

MicrosoftMangle.cpp
Go to the documentation of this file.
00001 //===--- MicrosoftMangle.cpp - Microsoft Visual C++ Name Mangling ---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This provides C++ name mangling targeting the Microsoft Visual C++ ABI.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/AST/Mangle.h"
00015 #include "clang/AST/ASTContext.h"
00016 #include "clang/AST/CharUnits.h"
00017 #include "clang/AST/Decl.h"
00018 #include "clang/AST/DeclCXX.h"
00019 #include "clang/AST/DeclObjC.h"
00020 #include "clang/AST/DeclTemplate.h"
00021 #include "clang/AST/ExprCXX.h"
00022 #include "clang/Basic/ABI.h"
00023 
00024 using namespace clang;
00025 
00026 namespace {
00027 
00028 /// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
00029 /// Microsoft Visual C++ ABI.
00030 class MicrosoftCXXNameMangler {
00031   MangleContext &Context;
00032   raw_ostream &Out;
00033 
00034   ASTContext &getASTContext() const { return Context.getASTContext(); }
00035 
00036 public:
00037   MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_)
00038   : Context(C), Out(Out_) { }
00039 
00040   void mangle(const NamedDecl *D, StringRef Prefix = "?");
00041   void mangleName(const NamedDecl *ND);
00042   void mangleFunctionEncoding(const FunctionDecl *FD);
00043   void mangleVariableEncoding(const VarDecl *VD);
00044   void mangleNumber(int64_t Number);
00045   void mangleType(QualType T);
00046 
00047 private:
00048   void mangleUnqualifiedName(const NamedDecl *ND) {
00049     mangleUnqualifiedName(ND, ND->getDeclName());
00050   }
00051   void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
00052   void mangleSourceName(const IdentifierInfo *II);
00053   void manglePostfix(const DeclContext *DC, bool NoFunction=false);
00054   void mangleOperatorName(OverloadedOperatorKind OO);
00055   void mangleQualifiers(Qualifiers Quals, bool IsMember);
00056 
00057   void mangleObjCMethodName(const ObjCMethodDecl *MD);
00058 
00059   // Declare manglers for every type class.
00060 #define ABSTRACT_TYPE(CLASS, PARENT)
00061 #define NON_CANONICAL_TYPE(CLASS, PARENT)
00062 #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
00063 #include "clang/AST/TypeNodes.def"
00064   
00065   void mangleType(const TagType*);
00066   void mangleType(const FunctionType *T, const FunctionDecl *D,
00067                   bool IsStructor, bool IsInstMethod);
00068   void mangleType(const ArrayType *T, bool IsGlobal);
00069   void mangleExtraDimensions(QualType T);
00070   void mangleFunctionClass(const FunctionDecl *FD);
00071   void mangleCallingConvention(const FunctionType *T, bool IsInstMethod = false);
00072   void mangleThrowSpecification(const FunctionProtoType *T);
00073 
00074 };
00075 
00076 /// MicrosoftMangleContext - Overrides the default MangleContext for the
00077 /// Microsoft Visual C++ ABI.
00078 class MicrosoftMangleContext : public MangleContext {
00079 public:
00080   MicrosoftMangleContext(ASTContext &Context,
00081                    DiagnosticsEngine &Diags) : MangleContext(Context, Diags) { }
00082   virtual bool shouldMangleDeclName(const NamedDecl *D);
00083   virtual void mangleName(const NamedDecl *D, raw_ostream &Out);
00084   virtual void mangleThunk(const CXXMethodDecl *MD,
00085                            const ThunkInfo &Thunk,
00086                            raw_ostream &);
00087   virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
00088                                   const ThisAdjustment &ThisAdjustment,
00089                                   raw_ostream &);
00090   virtual void mangleCXXVTable(const CXXRecordDecl *RD,
00091                                raw_ostream &);
00092   virtual void mangleCXXVTT(const CXXRecordDecl *RD,
00093                             raw_ostream &);
00094   virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
00095                                    const CXXRecordDecl *Type,
00096                                    raw_ostream &);
00097   virtual void mangleCXXRTTI(QualType T, raw_ostream &);
00098   virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
00099   virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
00100                              raw_ostream &);
00101   virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
00102                              raw_ostream &);
00103   virtual void mangleReferenceTemporary(const clang::VarDecl *,
00104                                         raw_ostream &);
00105 };
00106 
00107 }
00108 
00109 static bool isInCLinkageSpecification(const Decl *D) {
00110   D = D->getCanonicalDecl();
00111   for (const DeclContext *DC = D->getDeclContext();
00112        !DC->isTranslationUnit(); DC = DC->getParent()) {
00113     if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
00114       return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
00115   }
00116 
00117   return false;
00118 }
00119 
00120 bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
00121   // In C, functions with no attributes never need to be mangled. Fastpath them.
00122   if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
00123     return false;
00124 
00125   // Any decl can be declared with __asm("foo") on it, and this takes precedence
00126   // over all other naming in the .o file.
00127   if (D->hasAttr<AsmLabelAttr>())
00128     return true;
00129 
00130   // Clang's "overloadable" attribute extension to C/C++ implies name mangling
00131   // (always) as does passing a C++ member function and a function
00132   // whose name is not a simple identifier.
00133   const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
00134   if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
00135              !FD->getDeclName().isIdentifier()))
00136     return true;
00137 
00138   // Otherwise, no mangling is done outside C++ mode.
00139   if (!getASTContext().getLangOpts().CPlusPlus)
00140     return false;
00141 
00142   // Variables at global scope with internal linkage are not mangled.
00143   if (!FD) {
00144     const DeclContext *DC = D->getDeclContext();
00145     if (DC->isTranslationUnit() && D->getLinkage() == InternalLinkage)
00146       return false;
00147   }
00148 
00149   // C functions and "main" are not mangled.
00150   if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
00151     return false;
00152 
00153   return true;
00154 }
00155 
00156 void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
00157                                      StringRef Prefix) {
00158   // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
00159   // Therefore it's really important that we don't decorate the
00160   // name with leading underscores or leading/trailing at signs. So, emit a
00161   // asm marker at the start so we get the name right.
00162   Out << '\01';  // LLVM IR Marker for __asm("foo")
00163 
00164   // Any decl can be declared with __asm("foo") on it, and this takes precedence
00165   // over all other naming in the .o file.
00166   if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
00167     // If we have an asm name, then we use it as the mangling.
00168     Out << ALA->getLabel();
00169     return;
00170   }
00171 
00172   // <mangled-name> ::= ? <name> <type-encoding>
00173   Out << Prefix;
00174   mangleName(D);
00175   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
00176     mangleFunctionEncoding(FD);
00177   else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
00178     mangleVariableEncoding(VD);
00179   // TODO: Fields? Can MSVC even mangle them?
00180 }
00181 
00182 void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
00183   // <type-encoding> ::= <function-class> <function-type>
00184 
00185   // Don't mangle in the type if this isn't a decl we should typically mangle.
00186   if (!Context.shouldMangleDeclName(FD))
00187     return;
00188   
00189   // We should never ever see a FunctionNoProtoType at this point.
00190   // We don't even know how to mangle their types anyway :).
00191   const FunctionProtoType *FT = cast<FunctionProtoType>(FD->getType());
00192 
00193   bool InStructor = false, InInstMethod = false;
00194   const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
00195   if (MD) {
00196     if (MD->isInstance())
00197       InInstMethod = true;
00198     if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
00199       InStructor = true;
00200   }
00201 
00202   // First, the function class.
00203   mangleFunctionClass(FD);
00204 
00205   mangleType(FT, FD, InStructor, InInstMethod);
00206 }
00207 
00208 void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
00209   // <type-encoding> ::= <storage-class> <variable-type>
00210   // <storage-class> ::= 0  # private static member
00211   //                 ::= 1  # protected static member
00212   //                 ::= 2  # public static member
00213   //                 ::= 3  # global
00214   //                 ::= 4  # static local
00215   
00216   // The first character in the encoding (after the name) is the storage class.
00217   if (VD->isStaticDataMember()) {
00218     // If it's a static member, it also encodes the access level.
00219     switch (VD->getAccess()) {
00220       default:
00221       case AS_private: Out << '0'; break;
00222       case AS_protected: Out << '1'; break;
00223       case AS_public: Out << '2'; break;
00224     }
00225   }
00226   else if (!VD->isStaticLocal())
00227     Out << '3';
00228   else
00229     Out << '4';
00230   // Now mangle the type.
00231   // <variable-type> ::= <type> <cvr-qualifiers>
00232   //                 ::= <type> A # pointers, references, arrays
00233   // Pointers and references are odd. The type of 'int * const foo;' gets
00234   // mangled as 'QAHA' instead of 'PAHB', for example.
00235   QualType Ty = VD->getType();
00236   if (Ty->isPointerType() || Ty->isReferenceType()) {
00237     mangleType(Ty);
00238     Out << 'A';
00239   } else if (Ty->isArrayType()) {
00240     // Global arrays are funny, too.
00241     mangleType(cast<ArrayType>(Ty.getTypePtr()), true);
00242     Out << 'A';
00243   } else {
00244     mangleType(Ty.getLocalUnqualifiedType());
00245     mangleQualifiers(Ty.getLocalQualifiers(), false);
00246   }
00247 }
00248 
00249 void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
00250   // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
00251   const DeclContext *DC = ND->getDeclContext();
00252 
00253   // Always start with the unqualified name.
00254   mangleUnqualifiedName(ND);    
00255 
00256   // If this is an extern variable declared locally, the relevant DeclContext
00257   // is that of the containing namespace, or the translation unit.
00258   if (isa<FunctionDecl>(DC) && ND->hasLinkage())
00259     while (!DC->isNamespace() && !DC->isTranslationUnit())
00260       DC = DC->getParent();
00261 
00262   manglePostfix(DC);
00263 
00264   // Terminate the whole name with an '@'.
00265   Out << '@';
00266 }
00267 
00268 void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
00269   // <number> ::= [?] <decimal digit> # <= 9
00270   //          ::= [?] <hex digit>+ @ # > 9; A = 0, B = 1, etc...
00271   if (Number < 0) {
00272     Out << '?';
00273     Number = -Number;
00274   }
00275   if (Number >= 1 && Number <= 10) {
00276     Out << Number-1;
00277   } else {
00278     // We have to build up the encoding in reverse order, so it will come
00279     // out right when we write it out.
00280     char Encoding[16];
00281     char *EndPtr = Encoding+sizeof(Encoding);
00282     char *CurPtr = EndPtr;
00283     while (Number) {
00284       *--CurPtr = 'A' + (Number % 16);
00285       Number /= 16;
00286     }
00287     Out.write(CurPtr, EndPtr-CurPtr);
00288     Out << '@';
00289   }
00290 }
00291 
00292 void
00293 MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
00294                                                DeclarationName Name) {
00295   //  <unqualified-name> ::= <operator-name>
00296   //                     ::= <ctor-dtor-name>
00297   //                     ::= <source-name>
00298   switch (Name.getNameKind()) {
00299     case DeclarationName::Identifier: {
00300       if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
00301         mangleSourceName(II);
00302         break;
00303       }
00304       
00305       // Otherwise, an anonymous entity.  We must have a declaration.
00306       assert(ND && "mangling empty name without declaration");
00307       
00308       if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
00309         if (NS->isAnonymousNamespace()) {
00310           Out << "?A";
00311           break;
00312         }
00313       }
00314       
00315       // We must have an anonymous struct.
00316       const TagDecl *TD = cast<TagDecl>(ND);
00317       if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
00318         assert(TD->getDeclContext() == D->getDeclContext() &&
00319                "Typedef should not be in another decl context!");
00320         assert(D->getDeclName().getAsIdentifierInfo() &&
00321                "Typedef was not named!");
00322         mangleSourceName(D->getDeclName().getAsIdentifierInfo());
00323         break;
00324       }
00325 
00326       // When VC encounters an anonymous type with no tag and no typedef,
00327       // it literally emits '<unnamed-tag>'.
00328       Out << "<unnamed-tag>";
00329       break;
00330     }
00331       
00332     case DeclarationName::ObjCZeroArgSelector:
00333     case DeclarationName::ObjCOneArgSelector:
00334     case DeclarationName::ObjCMultiArgSelector:
00335       llvm_unreachable("Can't mangle Objective-C selector names here!");
00336       
00337     case DeclarationName::CXXConstructorName:
00338       Out << "?0";
00339       break;
00340       
00341     case DeclarationName::CXXDestructorName:
00342       Out << "?1";
00343       break;
00344       
00345     case DeclarationName::CXXConversionFunctionName:
00346       // <operator-name> ::= ?B # (cast)
00347       // The target type is encoded as the return type.
00348       Out << "?B";
00349       break;
00350       
00351     case DeclarationName::CXXOperatorName:
00352       mangleOperatorName(Name.getCXXOverloadedOperator());
00353       break;
00354       
00355     case DeclarationName::CXXLiteralOperatorName:
00356       // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
00357       llvm_unreachable("Don't know how to mangle literal operators yet!");
00358       
00359     case DeclarationName::CXXUsingDirective:
00360       llvm_unreachable("Can't mangle a using directive name!");
00361   }
00362 }
00363 
00364 void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
00365                                             bool NoFunction) {
00366   // <postfix> ::= <unqualified-name> [<postfix>]
00367   //           ::= <template-postfix> <template-args> [<postfix>]
00368   //           ::= <template-param>
00369   //           ::= <substitution> [<postfix>]
00370 
00371   if (!DC) return;
00372 
00373   while (isa<LinkageSpecDecl>(DC))
00374     DC = DC->getParent();
00375 
00376   if (DC->isTranslationUnit())
00377     return;
00378 
00379   if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
00380     Context.mangleBlock(BD, Out);
00381     Out << '@';
00382     return manglePostfix(DC->getParent(), NoFunction);
00383   }
00384 
00385   if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
00386     return;
00387   else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
00388     mangleObjCMethodName(Method);
00389   else {
00390     mangleUnqualifiedName(cast<NamedDecl>(DC));
00391     manglePostfix(DC->getParent(), NoFunction);
00392   }
00393 }
00394 
00395 void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO) {
00396   switch (OO) {
00397   //                     ?0 # constructor
00398   //                     ?1 # destructor
00399   // <operator-name> ::= ?2 # new
00400   case OO_New: Out << "?2"; break;
00401   // <operator-name> ::= ?3 # delete
00402   case OO_Delete: Out << "?3"; break;
00403   // <operator-name> ::= ?4 # =
00404   case OO_Equal: Out << "?4"; break;
00405   // <operator-name> ::= ?5 # >>
00406   case OO_GreaterGreater: Out << "?5"; break;
00407   // <operator-name> ::= ?6 # <<
00408   case OO_LessLess: Out << "?6"; break;
00409   // <operator-name> ::= ?7 # !
00410   case OO_Exclaim: Out << "?7"; break;
00411   // <operator-name> ::= ?8 # ==
00412   case OO_EqualEqual: Out << "?8"; break;
00413   // <operator-name> ::= ?9 # !=
00414   case OO_ExclaimEqual: Out << "?9"; break;
00415   // <operator-name> ::= ?A # []
00416   case OO_Subscript: Out << "?A"; break;
00417   //                     ?B # conversion
00418   // <operator-name> ::= ?C # ->
00419   case OO_Arrow: Out << "?C"; break;
00420   // <operator-name> ::= ?D # *
00421   case OO_Star: Out << "?D"; break;
00422   // <operator-name> ::= ?E # ++
00423   case OO_PlusPlus: Out << "?E"; break;
00424   // <operator-name> ::= ?F # --
00425   case OO_MinusMinus: Out << "?F"; break;
00426   // <operator-name> ::= ?G # -
00427   case OO_Minus: Out << "?G"; break;
00428   // <operator-name> ::= ?H # +
00429   case OO_Plus: Out << "?H"; break;
00430   // <operator-name> ::= ?I # &
00431   case OO_Amp: Out << "?I"; break;
00432   // <operator-name> ::= ?J # ->*
00433   case OO_ArrowStar: Out << "?J"; break;
00434   // <operator-name> ::= ?K # /
00435   case OO_Slash: Out << "?K"; break;
00436   // <operator-name> ::= ?L # %
00437   case OO_Percent: Out << "?L"; break;
00438   // <operator-name> ::= ?M # <
00439   case OO_Less: Out << "?M"; break;
00440   // <operator-name> ::= ?N # <=
00441   case OO_LessEqual: Out << "?N"; break;
00442   // <operator-name> ::= ?O # >
00443   case OO_Greater: Out << "?O"; break;
00444   // <operator-name> ::= ?P # >=
00445   case OO_GreaterEqual: Out << "?P"; break;
00446   // <operator-name> ::= ?Q # ,
00447   case OO_Comma: Out << "?Q"; break;
00448   // <operator-name> ::= ?R # ()
00449   case OO_Call: Out << "?R"; break;
00450   // <operator-name> ::= ?S # ~
00451   case OO_Tilde: Out << "?S"; break;
00452   // <operator-name> ::= ?T # ^
00453   case OO_Caret: Out << "?T"; break;
00454   // <operator-name> ::= ?U # |
00455   case OO_Pipe: Out << "?U"; break;
00456   // <operator-name> ::= ?V # &&
00457   case OO_AmpAmp: Out << "?V"; break;
00458   // <operator-name> ::= ?W # ||
00459   case OO_PipePipe: Out << "?W"; break;
00460   // <operator-name> ::= ?X # *=
00461   case OO_StarEqual: Out << "?X"; break;
00462   // <operator-name> ::= ?Y # +=
00463   case OO_PlusEqual: Out << "?Y"; break;
00464   // <operator-name> ::= ?Z # -=
00465   case OO_MinusEqual: Out << "?Z"; break;
00466   // <operator-name> ::= ?_0 # /=
00467   case OO_SlashEqual: Out << "?_0"; break;
00468   // <operator-name> ::= ?_1 # %=
00469   case OO_PercentEqual: Out << "?_1"; break;
00470   // <operator-name> ::= ?_2 # >>=
00471   case OO_GreaterGreaterEqual: Out << "?_2"; break;
00472   // <operator-name> ::= ?_3 # <<=
00473   case OO_LessLessEqual: Out << "?_3"; break;
00474   // <operator-name> ::= ?_4 # &=
00475   case OO_AmpEqual: Out << "?_4"; break;
00476   // <operator-name> ::= ?_5 # |=
00477   case OO_PipeEqual: Out << "?_5"; break;
00478   // <operator-name> ::= ?_6 # ^=
00479   case OO_CaretEqual: Out << "?_6"; break;
00480   //                     ?_7 # vftable
00481   //                     ?_8 # vbtable
00482   //                     ?_9 # vcall
00483   //                     ?_A # typeof
00484   //                     ?_B # local static guard
00485   //                     ?_C # string
00486   //                     ?_D # vbase destructor
00487   //                     ?_E # vector deleting destructor
00488   //                     ?_F # default constructor closure
00489   //                     ?_G # scalar deleting destructor
00490   //                     ?_H # vector constructor iterator
00491   //                     ?_I # vector destructor iterator
00492   //                     ?_J # vector vbase constructor iterator
00493   //                     ?_K # virtual displacement map
00494   //                     ?_L # eh vector constructor iterator
00495   //                     ?_M # eh vector destructor iterator
00496   //                     ?_N # eh vector vbase constructor iterator
00497   //                     ?_O # copy constructor closure
00498   //                     ?_P<name> # udt returning <name>
00499   //                     ?_Q # <unknown>
00500   //                     ?_R0 # RTTI Type Descriptor
00501   //                     ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
00502   //                     ?_R2 # RTTI Base Class Array
00503   //                     ?_R3 # RTTI Class Hierarchy Descriptor
00504   //                     ?_R4 # RTTI Complete Object Locator
00505   //                     ?_S # local vftable
00506   //                     ?_T # local vftable constructor closure
00507   // <operator-name> ::= ?_U # new[]
00508   case OO_Array_New: Out << "?_U"; break;
00509   // <operator-name> ::= ?_V # delete[]
00510   case OO_Array_Delete: Out << "?_V"; break;
00511     
00512   case OO_Conditional:
00513     llvm_unreachable("Don't know how to mangle ?:");
00514     
00515   case OO_None:
00516   case NUM_OVERLOADED_OPERATORS:
00517     llvm_unreachable("Not an overloaded operator");
00518   }
00519 }
00520 
00521 void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
00522   // <source name> ::= <identifier> @
00523   Out << II->getName() << '@';
00524 }
00525 
00526 void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
00527   Context.mangleObjCMethodName(MD, Out);
00528 }
00529 
00530 void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
00531                                                bool IsMember) {
00532   // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
00533   // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
00534   // 'I' means __restrict (32/64-bit).
00535   // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
00536   // keyword!
00537   // <base-cvr-qualifiers> ::= A  # near
00538   //                       ::= B  # near const
00539   //                       ::= C  # near volatile
00540   //                       ::= D  # near const volatile
00541   //                       ::= E  # far (16-bit)
00542   //                       ::= F  # far const (16-bit)
00543   //                       ::= G  # far volatile (16-bit)
00544   //                       ::= H  # far const volatile (16-bit)
00545   //                       ::= I  # huge (16-bit)
00546   //                       ::= J  # huge const (16-bit)
00547   //                       ::= K  # huge volatile (16-bit)
00548   //                       ::= L  # huge const volatile (16-bit)
00549   //                       ::= M <basis> # based
00550   //                       ::= N <basis> # based const
00551   //                       ::= O <basis> # based volatile
00552   //                       ::= P <basis> # based const volatile
00553   //                       ::= Q  # near member
00554   //                       ::= R  # near const member
00555   //                       ::= S  # near volatile member
00556   //                       ::= T  # near const volatile member
00557   //                       ::= U  # far member (16-bit)
00558   //                       ::= V  # far const member (16-bit)
00559   //                       ::= W  # far volatile member (16-bit)
00560   //                       ::= X  # far const volatile member (16-bit)
00561   //                       ::= Y  # huge member (16-bit)
00562   //                       ::= Z  # huge const member (16-bit)
00563   //                       ::= 0  # huge volatile member (16-bit)
00564   //                       ::= 1  # huge const volatile member (16-bit)
00565   //                       ::= 2 <basis> # based member
00566   //                       ::= 3 <basis> # based const member
00567   //                       ::= 4 <basis> # based volatile member
00568   //                       ::= 5 <basis> # based const volatile member
00569   //                       ::= 6  # near function (pointers only)
00570   //                       ::= 7  # far function (pointers only)
00571   //                       ::= 8  # near method (pointers only)
00572   //                       ::= 9  # far method (pointers only)
00573   //                       ::= _A <basis> # based function (pointers only)
00574   //                       ::= _B <basis> # based function (far?) (pointers only)
00575   //                       ::= _C <basis> # based method (pointers only)
00576   //                       ::= _D <basis> # based method (far?) (pointers only)
00577   //                       ::= _E # block (Clang)
00578   // <basis> ::= 0 # __based(void)
00579   //         ::= 1 # __based(segment)?
00580   //         ::= 2 <name> # __based(name)
00581   //         ::= 3 # ?
00582   //         ::= 4 # ?
00583   //         ::= 5 # not really based
00584   if (!IsMember) {
00585     if (!Quals.hasVolatile()) {
00586       if (!Quals.hasConst())
00587         Out << 'A';
00588       else
00589         Out << 'B';
00590     } else {
00591       if (!Quals.hasConst())
00592         Out << 'C';
00593       else
00594         Out << 'D';
00595     }
00596   } else {
00597     if (!Quals.hasVolatile()) {
00598       if (!Quals.hasConst())
00599         Out << 'Q';
00600       else
00601         Out << 'R';
00602     } else {
00603       if (!Quals.hasConst())
00604         Out << 'S';
00605       else
00606         Out << 'T';
00607     }
00608   }
00609 
00610   // FIXME: For now, just drop all extension qualifiers on the floor.
00611 }
00612 
00613 void MicrosoftCXXNameMangler::mangleType(QualType T) {
00614   // Only operate on the canonical type!
00615   T = getASTContext().getCanonicalType(T);
00616   
00617   Qualifiers Quals = T.getLocalQualifiers();
00618   if (Quals) {
00619     // We have to mangle these now, while we still have enough information.
00620     // <pointer-cvr-qualifiers> ::= P  # pointer
00621     //                          ::= Q  # const pointer
00622     //                          ::= R  # volatile pointer
00623     //                          ::= S  # const volatile pointer
00624     if (T->isAnyPointerType() || T->isMemberPointerType() ||
00625         T->isBlockPointerType()) {
00626       if (!Quals.hasVolatile())
00627         Out << 'Q';
00628       else {
00629         if (!Quals.hasConst())
00630           Out << 'R';
00631         else
00632           Out << 'S';
00633       }
00634     } else
00635       // Just emit qualifiers like normal.
00636       // NB: When we mangle a pointer/reference type, and the pointee
00637       // type has no qualifiers, the lack of qualifier gets mangled
00638       // in there.
00639       mangleQualifiers(Quals, false);
00640   } else if (T->isAnyPointerType() || T->isMemberPointerType() ||
00641              T->isBlockPointerType()) {
00642     Out << 'P';
00643   }
00644   switch (T->getTypeClass()) {
00645 #define ABSTRACT_TYPE(CLASS, PARENT)
00646 #define NON_CANONICAL_TYPE(CLASS, PARENT) \
00647 case Type::CLASS: \
00648 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
00649 return;
00650 #define TYPE(CLASS, PARENT) \
00651 case Type::CLASS: \
00652 mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \
00653 break;
00654 #include "clang/AST/TypeNodes.def"
00655   }
00656 }
00657 
00658 void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T) {
00659   //  <type>         ::= <builtin-type>
00660   //  <builtin-type> ::= X  # void
00661   //                 ::= C  # signed char
00662   //                 ::= D  # char
00663   //                 ::= E  # unsigned char
00664   //                 ::= F  # short
00665   //                 ::= G  # unsigned short (or wchar_t if it's not a builtin)
00666   //                 ::= H  # int
00667   //                 ::= I  # unsigned int
00668   //                 ::= J  # long
00669   //                 ::= K  # unsigned long
00670   //                     L  # <none>
00671   //                 ::= M  # float
00672   //                 ::= N  # double
00673   //                 ::= O  # long double (__float80 is mangled differently)
00674   //                 ::= _J # long long, __int64
00675   //                 ::= _K # unsigned long long, __int64
00676   //                 ::= _L # __int128
00677   //                 ::= _M # unsigned __int128
00678   //                 ::= _N # bool
00679   //                     _O # <array in parameter>
00680   //                 ::= _T # __float80 (Intel)
00681   //                 ::= _W # wchar_t
00682   //                 ::= _Z # __float80 (Digital Mars)
00683   switch (T->getKind()) {
00684   case BuiltinType::Void: Out << 'X'; break;
00685   case BuiltinType::SChar: Out << 'C'; break;
00686   case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
00687   case BuiltinType::UChar: Out << 'E'; break;
00688   case BuiltinType::Short: Out << 'F'; break;
00689   case BuiltinType::UShort: Out << 'G'; break;
00690   case BuiltinType::Int: Out << 'H'; break;
00691   case BuiltinType::UInt: Out << 'I'; break;
00692   case BuiltinType::Long: Out << 'J'; break;
00693   case BuiltinType::ULong: Out << 'K'; break;
00694   case BuiltinType::Float: Out << 'M'; break;
00695   case BuiltinType::Double: Out << 'N'; break;
00696   // TODO: Determine size and mangle accordingly
00697   case BuiltinType::LongDouble: Out << 'O'; break;
00698   case BuiltinType::LongLong: Out << "_J"; break;
00699   case BuiltinType::ULongLong: Out << "_K"; break;
00700   case BuiltinType::Int128: Out << "_L"; break;
00701   case BuiltinType::UInt128: Out << "_M"; break;
00702   case BuiltinType::Bool: Out << "_N"; break;
00703   case BuiltinType::WChar_S:
00704   case BuiltinType::WChar_U: Out << "_W"; break;
00705 
00706 #define BUILTIN_TYPE(Id, SingletonId)
00707 #define PLACEHOLDER_TYPE(Id, SingletonId) \
00708   case BuiltinType::Id:
00709 #include "clang/AST/BuiltinTypes.def"
00710   case BuiltinType::Dependent:
00711     llvm_unreachable("placeholder types shouldn't get to name mangling");
00712 
00713   case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
00714   case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
00715   case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
00716 
00717   case BuiltinType::Char16:
00718   case BuiltinType::Char32:
00719   case BuiltinType::Half:
00720   case BuiltinType::NullPtr:
00721     assert(0 && "Don't know how to mangle this type yet");
00722   }
00723 }
00724 
00725 // <type>          ::= <function-type>
00726 void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T) {
00727   // Structors only appear in decls, so at this point we know it's not a
00728   // structor type.
00729   // I'll probably have mangleType(MemberPointerType) call the mangleType()
00730   // method directly.
00731   mangleType(T, NULL, false, false);
00732 }
00733 void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T) {
00734   llvm_unreachable("Can't mangle K&R function prototypes");
00735 }
00736 
00737 void MicrosoftCXXNameMangler::mangleType(const FunctionType *T,
00738                                          const FunctionDecl *D,
00739                                          bool IsStructor,
00740                                          bool IsInstMethod) {
00741   // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
00742   //                     <return-type> <argument-list> <throw-spec>
00743   const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
00744 
00745   // If this is a C++ instance method, mangle the CVR qualifiers for the
00746   // this pointer.
00747   if (IsInstMethod)
00748     mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
00749 
00750   mangleCallingConvention(T, IsInstMethod);
00751 
00752   // <return-type> ::= <type>
00753   //               ::= @ # structors (they have no declared return type)
00754   if (IsStructor)
00755     Out << '@';
00756   else
00757     mangleType(Proto->getResultType());
00758 
00759   // <argument-list> ::= X # void
00760   //                 ::= <type>+ @
00761   //                 ::= <type>* Z # varargs
00762   if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
00763     Out << 'X';
00764   } else {
00765     if (D) {
00766       // If we got a decl, use the type-as-written to make sure arrays
00767       // get mangled right.  Note that we can't rely on the TSI
00768       // existing if (for example) the parameter was synthesized.
00769       for (FunctionDecl::param_const_iterator Parm = D->param_begin(),
00770              ParmEnd = D->param_end(); Parm != ParmEnd; ++Parm) {
00771         if (TypeSourceInfo *typeAsWritten = (*Parm)->getTypeSourceInfo())
00772           mangleType(typeAsWritten->getType());
00773         else
00774           mangleType((*Parm)->getType());
00775       }
00776     } else {
00777       for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
00778            ArgEnd = Proto->arg_type_end();
00779            Arg != ArgEnd; ++Arg)
00780         mangleType(*Arg);
00781     }
00782     // <builtin-type>      ::= Z  # ellipsis
00783     if (Proto->isVariadic())
00784       Out << 'Z';
00785     else
00786       Out << '@';
00787   }
00788 
00789   mangleThrowSpecification(Proto);
00790 }
00791 
00792 void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
00793   // <function-class> ::= A # private: near
00794   //                  ::= B # private: far
00795   //                  ::= C # private: static near
00796   //                  ::= D # private: static far
00797   //                  ::= E # private: virtual near
00798   //                  ::= F # private: virtual far
00799   //                  ::= G # private: thunk near
00800   //                  ::= H # private: thunk far
00801   //                  ::= I # protected: near
00802   //                  ::= J # protected: far
00803   //                  ::= K # protected: static near
00804   //                  ::= L # protected: static far
00805   //                  ::= M # protected: virtual near
00806   //                  ::= N # protected: virtual far
00807   //                  ::= O # protected: thunk near
00808   //                  ::= P # protected: thunk far
00809   //                  ::= Q # public: near
00810   //                  ::= R # public: far
00811   //                  ::= S # public: static near
00812   //                  ::= T # public: static far
00813   //                  ::= U # public: virtual near
00814   //                  ::= V # public: virtual far
00815   //                  ::= W # public: thunk near
00816   //                  ::= X # public: thunk far
00817   //                  ::= Y # global near
00818   //                  ::= Z # global far
00819   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
00820     switch (MD->getAccess()) {
00821       default:
00822       case AS_private:
00823         if (MD->isStatic())
00824           Out << 'C';
00825         else if (MD->isVirtual())
00826           Out << 'E';
00827         else
00828           Out << 'A';
00829         break;
00830       case AS_protected:
00831         if (MD->isStatic())
00832           Out << 'K';
00833         else if (MD->isVirtual())
00834           Out << 'M';
00835         else
00836           Out << 'I';
00837         break;
00838       case AS_public:
00839         if (MD->isStatic())
00840           Out << 'S';
00841         else if (MD->isVirtual())
00842           Out << 'U';
00843         else
00844           Out << 'Q';
00845     }
00846   } else
00847     Out << 'Y';
00848 }
00849 void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
00850                                                       bool IsInstMethod) {
00851   // <calling-convention> ::= A # __cdecl
00852   //                      ::= B # __export __cdecl
00853   //                      ::= C # __pascal
00854   //                      ::= D # __export __pascal
00855   //                      ::= E # __thiscall
00856   //                      ::= F # __export __thiscall
00857   //                      ::= G # __stdcall
00858   //                      ::= H # __export __stdcall
00859   //                      ::= I # __fastcall
00860   //                      ::= J # __export __fastcall
00861   // The 'export' calling conventions are from a bygone era
00862   // (*cough*Win16*cough*) when functions were declared for export with
00863   // that keyword. (It didn't actually export them, it just made them so
00864   // that they could be in a DLL and somebody from another module could call
00865   // them.)
00866   CallingConv CC = T->getCallConv();
00867   if (CC == CC_Default)
00868     CC = IsInstMethod ? getASTContext().getDefaultMethodCallConv() : CC_C;
00869   switch (CC) {
00870     default:
00871       llvm_unreachable("Unsupported CC for mangling");
00872     case CC_Default:
00873     case CC_C: Out << 'A'; break;
00874     case CC_X86Pascal: Out << 'C'; break;
00875     case CC_X86ThisCall: Out << 'E'; break;
00876     case CC_X86StdCall: Out << 'G'; break;
00877     case CC_X86FastCall: Out << 'I'; break;
00878   }
00879 }
00880 void MicrosoftCXXNameMangler::mangleThrowSpecification(
00881                                                 const FunctionProtoType *FT) {
00882   // <throw-spec> ::= Z # throw(...) (default)
00883   //              ::= @ # throw() or __declspec/__attribute__((nothrow))
00884   //              ::= <type>+
00885   // NOTE: Since the Microsoft compiler ignores throw specifications, they are
00886   // all actually mangled as 'Z'. (They're ignored because their associated
00887   // functionality isn't implemented, and probably never will be.)
00888   Out << 'Z';
00889 }
00890 
00891 void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T) {
00892   llvm_unreachable("Don't know how to mangle UnresolvedUsingTypes yet!");
00893 }
00894 
00895 // <type>        ::= <union-type> | <struct-type> | <class-type> | <enum-type>
00896 // <union-type>  ::= T <name>
00897 // <struct-type> ::= U <name>
00898 // <class-type>  ::= V <name>
00899 // <enum-type>   ::= W <size> <name>
00900 void MicrosoftCXXNameMangler::mangleType(const EnumType *T) {
00901   mangleType(static_cast<const TagType*>(T));
00902 }
00903 void MicrosoftCXXNameMangler::mangleType(const RecordType *T) {
00904   mangleType(static_cast<const TagType*>(T));
00905 }
00906 void MicrosoftCXXNameMangler::mangleType(const TagType *T) {
00907   switch (T->getDecl()->getTagKind()) {
00908     case TTK_Union:
00909       Out << 'T';
00910       break;
00911     case TTK_Struct:
00912       Out << 'U';
00913       break;
00914     case TTK_Class:
00915       Out << 'V';
00916       break;
00917     case TTK_Enum:
00918       Out << 'W';
00919       Out << getASTContext().getTypeSizeInChars(
00920                 cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity();
00921       break;
00922   }
00923   mangleName(T->getDecl());
00924 }
00925 
00926 // <type>       ::= <array-type>
00927 // <array-type> ::= P <cvr-qualifiers> [Y <dimension-count> <dimension>+]
00928 //                                                  <element-type> # as global
00929 //              ::= Q <cvr-qualifiers> [Y <dimension-count> <dimension>+]
00930 //                                                  <element-type> # as param
00931 // It's supposed to be the other way around, but for some strange reason, it
00932 // isn't. Today this behavior is retained for the sole purpose of backwards
00933 // compatibility.
00934 void MicrosoftCXXNameMangler::mangleType(const ArrayType *T, bool IsGlobal) {
00935   // This isn't a recursive mangling, so now we have to do it all in this
00936   // one call.
00937   if (IsGlobal)
00938     Out << 'P';
00939   else
00940     Out << 'Q';
00941   mangleExtraDimensions(T->getElementType());
00942 }
00943 void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T) {
00944   mangleType(static_cast<const ArrayType *>(T), false);
00945 }
00946 void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T) {
00947   mangleType(static_cast<const ArrayType *>(T), false);
00948 }
00949 void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T) {
00950   mangleType(static_cast<const ArrayType *>(T), false);
00951 }
00952 void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T) {
00953   mangleType(static_cast<const ArrayType *>(T), false);
00954 }
00955 void MicrosoftCXXNameMangler::mangleExtraDimensions(QualType ElementTy) {
00956   SmallVector<llvm::APInt, 3> Dimensions;
00957   for (;;) {
00958     if (ElementTy->isConstantArrayType()) {
00959       const ConstantArrayType *CAT =
00960       static_cast<const ConstantArrayType *>(ElementTy.getTypePtr());
00961       Dimensions.push_back(CAT->getSize());
00962       ElementTy = CAT->getElementType();
00963     } else if (ElementTy->isVariableArrayType()) {
00964       llvm_unreachable("Don't know how to mangle VLAs!");
00965     } else if (ElementTy->isDependentSizedArrayType()) {
00966       // The dependent expression has to be folded into a constant (TODO).
00967       llvm_unreachable("Don't know how to mangle dependent-sized arrays!");
00968     } else if (ElementTy->isIncompleteArrayType()) continue;
00969     else break;
00970   }
00971   mangleQualifiers(ElementTy.getQualifiers(), false);
00972   // If there are any additional dimensions, mangle them now.
00973   if (Dimensions.size() > 0) {
00974     Out << 'Y';
00975     // <dimension-count> ::= <number> # number of extra dimensions
00976     mangleNumber(Dimensions.size());
00977     for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim) {
00978       mangleNumber(Dimensions[Dim].getLimitedValue());
00979     }
00980   }
00981   mangleType(ElementTy.getLocalUnqualifiedType());
00982 }
00983 
00984 // <type>                   ::= <pointer-to-member-type>
00985 // <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
00986 //                                                          <class name> <type>
00987 void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T) {
00988   QualType PointeeType = T->getPointeeType();
00989   if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
00990     Out << '8';
00991     mangleName(cast<RecordType>(T->getClass())->getDecl());
00992     mangleType(FPT, NULL, false, true);
00993   } else {
00994     mangleQualifiers(PointeeType.getQualifiers(), true);
00995     mangleName(cast<RecordType>(T->getClass())->getDecl());
00996     mangleType(PointeeType.getLocalUnqualifiedType());
00997   }
00998 }
00999 
01000 void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T) {
01001   llvm_unreachable("Don't know how to mangle TemplateTypeParmTypes yet!");
01002 }
01003 
01004 void MicrosoftCXXNameMangler::mangleType(
01005                                        const SubstTemplateTypeParmPackType *T) {
01006   llvm_unreachable(
01007          "Don't know how to mangle SubstTemplateTypeParmPackTypes yet!");
01008 }
01009 
01010 // <type> ::= <pointer-type>
01011 // <pointer-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
01012 void MicrosoftCXXNameMangler::mangleType(const PointerType *T) {
01013   QualType PointeeTy = T->getPointeeType();
01014   if (PointeeTy->isArrayType()) {
01015     // Pointers to arrays are mangled like arrays.
01016     mangleExtraDimensions(T->getPointeeType());
01017   } else if (PointeeTy->isFunctionType()) {
01018     // Function pointers are special.
01019     Out << '6';
01020     mangleType(static_cast<const FunctionType *>(PointeeTy.getTypePtr()),
01021                NULL, false, false);
01022   } else {
01023     if (!PointeeTy.hasQualifiers())
01024       // Lack of qualifiers is mangled as 'A'.
01025       Out << 'A';
01026     mangleType(PointeeTy);
01027   }
01028 }
01029 void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
01030   // Object pointers never have qualifiers.
01031   Out << 'A';
01032   mangleType(T->getPointeeType());
01033 }
01034 
01035 // <type> ::= <reference-type>
01036 // <reference-type> ::= A <cvr-qualifiers> <type>
01037 void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T) {
01038   Out << 'A';
01039   QualType PointeeTy = T->getPointeeType();
01040   if (!PointeeTy.hasQualifiers())
01041     // Lack of qualifiers is mangled as 'A'.
01042     Out << 'A';
01043   mangleType(PointeeTy);
01044 }
01045 
01046 void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T) {
01047   llvm_unreachable("Don't know how to mangle RValueReferenceTypes yet!");
01048 }
01049 
01050 void MicrosoftCXXNameMangler::mangleType(const ComplexType *T) {
01051   llvm_unreachable("Don't know how to mangle ComplexTypes yet!");
01052 }
01053 
01054 void MicrosoftCXXNameMangler::mangleType(const VectorType *T) {
01055   llvm_unreachable("Don't know how to mangle VectorTypes yet!");
01056 }
01057 void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T) {
01058   llvm_unreachable("Don't know how to mangle ExtVectorTypes yet!");
01059 }
01060 void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
01061   llvm_unreachable(
01062                   "Don't know how to mangle DependentSizedExtVectorTypes yet!");
01063 }
01064 
01065 void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T) {
01066   // ObjC interfaces have structs underlying them.
01067   Out << 'U';
01068   mangleName(T->getDecl());
01069 }
01070 
01071 void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T) {
01072   // We don't allow overloading by different protocol qualification,
01073   // so mangling them isn't necessary.
01074   mangleType(T->getBaseType());
01075 }
01076 
01077 void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T) {
01078   Out << "_E";
01079   mangleType(T->getPointeeType());
01080 }
01081 
01082 void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T) {
01083   llvm_unreachable("Don't know how to mangle InjectedClassNameTypes yet!");
01084 }
01085 
01086 void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T) {
01087   llvm_unreachable("Don't know how to mangle TemplateSpecializationTypes yet!");
01088 }
01089 
01090 void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T) {
01091   llvm_unreachable("Don't know how to mangle DependentNameTypes yet!");
01092 }
01093 
01094 void MicrosoftCXXNameMangler::mangleType(
01095                                  const DependentTemplateSpecializationType *T) {
01096   llvm_unreachable(
01097          "Don't know how to mangle DependentTemplateSpecializationTypes yet!");
01098 }
01099 
01100 void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T) {
01101   llvm_unreachable("Don't know how to mangle PackExpansionTypes yet!");
01102 }
01103 
01104 void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T) {
01105   llvm_unreachable("Don't know how to mangle TypeOfTypes yet!");
01106 }
01107 
01108 void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T) {
01109   llvm_unreachable("Don't know how to mangle TypeOfExprTypes yet!");
01110 }
01111 
01112 void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T) {
01113   llvm_unreachable("Don't know how to mangle DecltypeTypes yet!");
01114 }
01115 
01116 void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T) {
01117   llvm_unreachable("Don't know how to mangle UnaryTransformationTypes yet!");
01118 }
01119 
01120 void MicrosoftCXXNameMangler::mangleType(const AutoType *T) {
01121   llvm_unreachable("Don't know how to mangle AutoTypes yet!");
01122 }
01123 
01124 void MicrosoftCXXNameMangler::mangleType(const AtomicType *T) {
01125   llvm_unreachable("Don't know how to mangle AtomicTypes yet!");
01126 }
01127 
01128 void MicrosoftMangleContext::mangleName(const NamedDecl *D,
01129                                         raw_ostream &Out) {
01130   assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
01131          "Invalid mangleName() call, argument is not a variable or function!");
01132   assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
01133          "Invalid mangleName() call on 'structor decl!");
01134 
01135   PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
01136                                  getASTContext().getSourceManager(),
01137                                  "Mangling declaration");
01138 
01139   MicrosoftCXXNameMangler Mangler(*this, Out);
01140   return Mangler.mangle(D);
01141 }
01142 void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
01143                                          const ThunkInfo &Thunk,
01144                                          raw_ostream &) {
01145   llvm_unreachable("Can't yet mangle thunks!");
01146 }
01147 void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
01148                                                 CXXDtorType Type,
01149                                                 const ThisAdjustment &,
01150                                                 raw_ostream &) {
01151   llvm_unreachable("Can't yet mangle destructor thunks!");
01152 }
01153 void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
01154                                              raw_ostream &) {
01155   llvm_unreachable("Can't yet mangle virtual tables!");
01156 }
01157 void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
01158                                           raw_ostream &) {
01159   llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
01160 }
01161 void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
01162                                                  int64_t Offset,
01163                                                  const CXXRecordDecl *Type,
01164                                                  raw_ostream &) {
01165   llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
01166 }
01167 void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
01168                                            raw_ostream &) {
01169   llvm_unreachable("Can't yet mangle RTTI!");
01170 }
01171 void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
01172                                                raw_ostream &) {
01173   llvm_unreachable("Can't yet mangle RTTI names!");
01174 }
01175 void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
01176                                            CXXCtorType Type,
01177                                            raw_ostream & Out) {
01178   MicrosoftCXXNameMangler mangler(*this, Out);
01179   mangler.mangle(D);
01180 }
01181 void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
01182                                            CXXDtorType Type,
01183                                            raw_ostream & Out) {
01184   MicrosoftCXXNameMangler mangler(*this, Out);
01185   mangler.mangle(D);
01186 }
01187 void MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *,
01188                                                       raw_ostream &) {
01189   llvm_unreachable("Can't yet mangle reference temporaries!");
01190 }
01191 
01192 MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
01193                                                    DiagnosticsEngine &Diags) {
01194   return new MicrosoftMangleContext(Context, Diags);
01195 }