MicrosoftMangle.cpp revision c3069d618f4661d923cb1b5c4525b082fce73b04
114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne//===--- MicrosoftMangle.cpp - Microsoft Visual C++ Name Mangling ---------===//
214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne//
314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne//                     The LLVM Compiler Infrastructure
414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne//
514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// This file is distributed under the University of Illinois Open Source
614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// License. See LICENSE.TXT for details.
714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne//
814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne//===----------------------------------------------------------------------===//
914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne//
1014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// This provides C++ name mangling targetting the Microsoft Visual C++ ABI.
1114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne//
1214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne//===----------------------------------------------------------------------===//
1314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
1414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/Mangle.h"
1514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/ASTContext.h"
1614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/CharUnits.h"
1714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/Decl.h"
1814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/DeclCXX.h"
1914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/DeclObjC.h"
2014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/DeclTemplate.h"
2114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/ExprCXX.h"
2214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/Basic/ABI.h"
2314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
2414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourneusing namespace clang;
2514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
2614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournenamespace {
2714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
2814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
2914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne/// Microsoft Visual C++ ABI.
3014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourneclass MicrosoftCXXNameMangler {
3114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  MangleContext &Context;
3214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  llvm::raw_svector_ostream Out;
3314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
3414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  ASTContext &getASTContext() const { return Context.getASTContext(); }
3514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
3614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournepublic:
3714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  MicrosoftCXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res)
3814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  : Context(C), Out(Res) { }
3914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
4014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangle(const NamedDecl *D, llvm::StringRef Prefix = "?");
4114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleName(const NamedDecl *ND);
4214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleFunctionEncoding(const FunctionDecl *FD);
4314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleVariableEncoding(const VarDecl *VD);
4414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleNumber(int64_t Number);
4514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleType(QualType T);
4614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
4714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourneprivate:
4814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleUnqualifiedName(const NamedDecl *ND) {
4914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleUnqualifiedName(ND, ND->getDeclName());
5014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
5114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
5214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleSourceName(const IdentifierInfo *II);
5314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void manglePostfix(const DeclContext *DC, bool NoFunction=false);
5414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleOperatorName(OverloadedOperatorKind OO);
5514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleQualifiers(Qualifiers Quals, bool IsMember);
5614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
5714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleObjCMethodName(const ObjCMethodDecl *MD);
5814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
5914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Declare manglers for every type class.
6014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#define ABSTRACT_TYPE(CLASS, PARENT)
6114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#define NON_CANONICAL_TYPE(CLASS, PARENT)
6214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
6314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/TypeNodes.def"
6414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
6514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleType(const TagType*);
6614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleType(const FunctionType *T, const FunctionDecl *D,
6714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                  bool IsStructor, bool IsInstMethod);
6814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleType(const ArrayType *T, bool IsGlobal);
6914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleExtraDimensions(QualType T);
7014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleFunctionClass(const FunctionDecl *FD);
7114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleCallingConvention(const FunctionType *T, bool IsInstMethod = false);
7214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  void mangleThrowSpecification(const FunctionProtoType *T);
7314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
7414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne};
7514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
7614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne/// MicrosoftMangleContext - Overrides the default MangleContext for the
7714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne/// Microsoft Visual C++ ABI.
7814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourneclass MicrosoftMangleContext : public MangleContext {
7914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournepublic:
8014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  MicrosoftMangleContext(ASTContext &Context,
8114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                         Diagnostic &Diags) : MangleContext(Context, Diags) { }
8214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  virtual bool shouldMangleDeclName(const NamedDecl *D);
8314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  virtual void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &);
8414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  virtual void mangleThunk(const CXXMethodDecl *MD,
8514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                           const ThunkInfo &Thunk,
8614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                           llvm::SmallVectorImpl<char> &);
8714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
8814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                  const ThisAdjustment &ThisAdjustment,
8914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                  llvm::SmallVectorImpl<char> &);
9014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  virtual void mangleCXXVTable(const CXXRecordDecl *RD,
9114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                               llvm::SmallVectorImpl<char> &);
9214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  virtual void mangleCXXVTT(const CXXRecordDecl *RD,
9314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                            llvm::SmallVectorImpl<char> &);
9414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
9514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                   const CXXRecordDecl *Type,
9614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                   llvm::SmallVectorImpl<char> &);
9714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  virtual void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &);
9814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  virtual void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &);
9914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
10014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                             llvm::SmallVectorImpl<char> &);
10114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
10214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                             llvm::SmallVectorImpl<char> &);
10314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  virtual void mangleReferenceTemporary(const clang::VarDecl *,
10414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                        llvm::SmallVectorImpl<char> &);
10514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne};
10614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
10714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
10814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
10914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournestatic bool isInCLinkageSpecification(const Decl *D) {
11014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  D = D->getCanonicalDecl();
11114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  for (const DeclContext *DC = D->getDeclContext();
11214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne       !DC->isTranslationUnit(); DC = DC->getParent()) {
11314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
11414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
11514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
11614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
11714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  return false;
11814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
11914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
12014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournebool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
12114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // In C, functions with no attributes never need to be mangled. Fastpath them.
12214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
12314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return false;
12414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
12514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Any decl can be declared with __asm("foo") on it, and this takes precedence
12614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // over all other naming in the .o file.
12714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (D->hasAttr<AsmLabelAttr>())
12814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return true;
12914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
13014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Clang's "overloadable" attribute extension to C/C++ implies name mangling
13114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // (always) as does passing a C++ member function and a function
13214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // whose name is not a simple identifier.
13314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
13414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
13514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne             !FD->getDeclName().isIdentifier()))
13614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return true;
13714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
13814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Otherwise, no mangling is done outside C++ mode.
13914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (!getASTContext().getLangOptions().CPlusPlus)
14014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return false;
14114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
14214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Variables at global scope with internal linkage are not mangled.
14314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (!FD) {
14414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    const DeclContext *DC = D->getDeclContext();
14514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    if (DC->isTranslationUnit() && D->getLinkage() == InternalLinkage)
14614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      return false;
14714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
14814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
14914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // C functions and "main" are not mangled.
15014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
15114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return false;
15214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
15314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  return true;
15414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
15514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
15614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
15714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                     llvm::StringRef Prefix) {
15814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
15914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Therefore it's really important that we don't decorate the
16014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // name with leading underscores or leading/trailing at signs. So, emit a
16114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // asm marker at the start so we get the name right.
16214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  Out << '\01';  // LLVM IR Marker for __asm("foo")
16314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
16414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Any decl can be declared with __asm("foo") on it, and this takes precedence
16514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // over all other naming in the .o file.
16614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
16714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    // If we have an asm name, then we use it as the mangling.
16814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << ALA->getLabel();
16914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return;
17014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
17114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
17214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <mangled-name> ::= ? <name> <type-encoding>
17314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  Out << Prefix;
17414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleName(D);
17514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
17614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleFunctionEncoding(FD);
17714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
17814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleVariableEncoding(VD);
17914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // TODO: Fields? Can MSVC even mangle them?
18014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
18114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
18214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
18314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <type-encoding> ::= <function-class> <function-type>
18414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
18514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Don't mangle in the type if this isn't a decl we should typically mangle.
18614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (!Context.shouldMangleDeclName(FD))
18714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return;
18814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
18914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // We should never ever see a FunctionNoProtoType at this point.
19014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // We don't even know how to mangle their types anyway :).
19114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  const FunctionProtoType *FT = cast<FunctionProtoType>(FD->getType());
19214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
19314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  bool InStructor = false, InInstMethod = false;
19414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
19514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (MD) {
19614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    if (MD->isInstance())
19714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      InInstMethod = true;
19814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
19914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      InStructor = true;
20014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
20114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
20214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // First, the function class.
20314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleFunctionClass(FD);
20414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
20514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleType(FT, FD, InStructor, InInstMethod);
20614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
20714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
20814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
20914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <type-encoding> ::= <storage-class> <variable-type>
21014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <storage-class> ::= 0  # private static member
21114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= 1  # protected static member
21214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= 2  # public static member
21314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= 3  # global
21414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= 4  # static local
21514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
21614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // The first character in the encoding (after the name) is the storage class.
21714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (VD->isStaticDataMember()) {
21814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    // If it's a static member, it also encodes the access level.
21914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    switch (VD->getAccess()) {
22014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      default:
22114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      case AS_private: Out << '0'; break;
22214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      case AS_protected: Out << '1'; break;
22314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      case AS_public: Out << '2'; break;
22414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    }
22514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
22614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  else if (!VD->isStaticLocal())
22714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << '3';
22814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  else
22914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << '4';
23014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Now mangle the type.
23114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <variable-type> ::= <type> <cvr-qualifiers>
23214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= <type> A # pointers, references, arrays
23314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Pointers and references are odd. The type of 'int * const foo;' gets
23414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // mangled as 'QAHA' instead of 'PAHB', for example.
23514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  QualType Ty = VD->getType();
23614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (Ty->isPointerType() || Ty->isReferenceType()) {
23714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleType(Ty);
23814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << 'A';
23914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  } else if (Ty->isArrayType()) {
24014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    // Global arrays are funny, too.
24114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleType(static_cast<ArrayType *>(Ty.getTypePtr()), true);
24214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << 'A';
24314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  } else {
24414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleType(Ty.getLocalUnqualifiedType());
24514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleQualifiers(Ty.getLocalQualifiers(), false);
24614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
24714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
24814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
24914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
25014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
25114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  const DeclContext *DC = ND->getDeclContext();
25214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
25314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Always start with the unqualified name.
25414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleUnqualifiedName(ND);
25514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
25614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // If this is an extern variable declared locally, the relevant DeclContext
25714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // is that of the containing namespace, or the translation unit.
25814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (isa<FunctionDecl>(DC) && ND->hasLinkage())
25914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    while (!DC->isNamespace() && !DC->isTranslationUnit())
26014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      DC = DC->getParent();
26114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
26214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  manglePostfix(DC);
26314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
26414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Terminate the whole name with an '@'.
26514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  Out << '@';
26614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
26714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
26814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
26914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <number> ::= [?] <decimal digit> # <= 9
27014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //          ::= [?] <hex digit>+ @ # > 9; A = 0, B = 1, etc...
27114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (Number < 0) {
27214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << '?';
27314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Number = -Number;
27414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
27514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (Number >= 1 && Number <= 10) {
27614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << Number-1;
27714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  } else {
27814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    // We have to build up the encoding in reverse order, so it will come
27914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    // out right when we write it out.
28014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    char Encoding[16];
28114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    char *EndPtr = Encoding+sizeof(Encoding);
28214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    char *CurPtr = EndPtr;
28314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    while (Number) {
28414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      *--CurPtr = 'A' + (Number % 16);
28514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      Number /= 16;
28614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    }
28714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out.write(CurPtr, EndPtr-CurPtr);
28814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << '@';
28914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
29014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
29114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
29214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid
29314110477887e3dc168ffc6c191e72d705051f99ePeter CollingbourneMicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
29414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                               DeclarationName Name) {
29514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //  <unqualified-name> ::= <operator-name>
29614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ::= <ctor-dtor-name>
29714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ::= <source-name>
29814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  switch (Name.getNameKind()) {
29914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case DeclarationName::Identifier: {
30014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
30114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        mangleSourceName(II);
30214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        break;
30314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      }
30414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
30514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      // Otherwise, an anonymous entity.  We must have a declaration.
30614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      assert(ND && "mangling empty name without declaration");
30714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
30814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
30914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        if (NS->isAnonymousNamespace()) {
31014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne          Out << "?A";
31114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne          break;
31214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        }
31314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      }
31414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
31514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      // We must have an anonymous struct.
31614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      const TagDecl *TD = cast<TagDecl>(ND);
31714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
31814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        assert(TD->getDeclContext() == D->getDeclContext() &&
31914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne               "Typedef should not be in another decl context!");
32014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        assert(D->getDeclName().getAsIdentifierInfo() &&
32114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne               "Typedef was not named!");
32214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        mangleSourceName(D->getDeclName().getAsIdentifierInfo());
32314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        break;
32414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      }
32514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
32614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      // When VC encounters an anonymous type with no tag and no typedef,
32714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      // it literally emits '<unnamed-tag>'.
32814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      Out << "<unnamed-tag>";
32914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      break;
33014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    }
33114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
33214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case DeclarationName::ObjCZeroArgSelector:
33314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case DeclarationName::ObjCOneArgSelector:
33414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case DeclarationName::ObjCMultiArgSelector:
33514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      assert(false && "Can't mangle Objective-C selector names here!");
33614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      break;
33714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
33814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case DeclarationName::CXXConstructorName:
33914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      assert(false && "Can't mangle constructors yet!");
34014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      break;
34114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
34214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case DeclarationName::CXXDestructorName:
34314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      assert(false && "Can't mangle destructors yet!");
34414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      break;
34514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
34614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case DeclarationName::CXXConversionFunctionName:
34714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      // <operator-name> ::= ?B # (cast)
34814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      // The target type is encoded as the return type.
34914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      Out << "?B";
35014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      break;
35114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
35214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case DeclarationName::CXXOperatorName:
35314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      mangleOperatorName(Name.getCXXOverloadedOperator());
35414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      break;
35514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
35614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case DeclarationName::CXXLiteralOperatorName:
35714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
35814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      assert(false && "Don't know how to mangle literal operators yet!");
35914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      break;
36014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
36114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case DeclarationName::CXXUsingDirective:
36214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      assert(false && "Can't mangle a using directive name!");
36314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      break;
36414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
36514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
36614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
36714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
36814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                            bool NoFunction) {
36914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <postfix> ::= <unqualified-name> [<postfix>]
37014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //           ::= <template-postfix> <template-args> [<postfix>]
37114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //           ::= <template-param>
37214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //           ::= <substitution> [<postfix>]
37314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
37414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (!DC) return;
37514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
37614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  while (isa<LinkageSpecDecl>(DC))
37714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    DC = DC->getParent();
37814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
37914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (DC->isTranslationUnit())
38014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return;
38114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
38214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
38314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    llvm::SmallString<64> Name;
38414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Context.mangleBlock(BD, Name);
38514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << Name << '@';
38614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return manglePostfix(DC->getParent(), NoFunction);
38714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
38814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
38914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
39014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return;
39114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
39214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleObjCMethodName(Method);
39314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  else {
39414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleUnqualifiedName(cast<NamedDecl>(DC));
39514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    manglePostfix(DC->getParent(), NoFunction);
39614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
39714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
39814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
39914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO) {
40014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  switch (OO) {
40114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?0 # constructor
40214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?1 # destructor
40314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?2 # new
40414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_New: Out << "?2"; break;
40514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?3 # delete
40614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Delete: Out << "?3"; break;
40714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?4 # =
40814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Equal: Out << "?4"; break;
40914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?5 # >>
41014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_GreaterGreater: Out << "?5"; break;
41114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?6 # <<
41214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_LessLess: Out << "?6"; break;
41314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?7 # !
41414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Exclaim: Out << "?7"; break;
41514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?8 # ==
41614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_EqualEqual: Out << "?8"; break;
41714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?9 # !=
41814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_ExclaimEqual: Out << "?9"; break;
41914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?A # []
42014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Subscript: Out << "?A"; break;
42114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?B # conversion
42214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?C # ->
42314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Arrow: Out << "?C"; break;
42414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?D # *
42514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Star: Out << "?D"; break;
42614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?E # ++
42714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_PlusPlus: Out << "?E"; break;
42814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?F # --
42914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_MinusMinus: Out << "?F"; break;
43014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?G # -
43114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Minus: Out << "?G"; break;
43214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?H # +
43314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Plus: Out << "?H"; break;
43414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?I # &
43514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Amp: Out << "?I"; break;
43614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?J # ->*
43714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_ArrowStar: Out << "?J"; break;
43814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?K # /
43914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Slash: Out << "?K"; break;
44014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?L # %
44114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Percent: Out << "?L"; break;
44214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?M # <
44314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Less: Out << "?M"; break;
44414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?N # <=
44514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_LessEqual: Out << "?N"; break;
44614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?O # >
44714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Greater: Out << "?O"; break;
44814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?P # >=
44914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_GreaterEqual: Out << "?P"; break;
45014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?Q # ,
45114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Comma: Out << "?Q"; break;
45214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?R # ()
45314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Call: Out << "?R"; break;
45414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?S # ~
45514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Tilde: Out << "?S"; break;
45614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?T # ^
45714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Caret: Out << "?T"; break;
45814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?U # |
45914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Pipe: Out << "?U"; break;
46014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?V # &&
46114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_AmpAmp: Out << "?V"; break;
46214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?W # ||
46314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_PipePipe: Out << "?W"; break;
46414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?X # *=
46514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_StarEqual: Out << "?X"; break;
46614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?Y # +=
46714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_PlusEqual: Out << "?Y"; break;
46814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?Z # -=
46914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_MinusEqual: Out << "?Z"; break;
47014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?_0 # /=
47114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_SlashEqual: Out << "?_0"; break;
47214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?_1 # %=
47314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_PercentEqual: Out << "?_1"; break;
47414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?_2 # >>=
47514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_GreaterGreaterEqual: Out << "?_2"; break;
47614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?_3 # <<=
47714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_LessLessEqual: Out << "?_3"; break;
47814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?_4 # &=
47914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_AmpEqual: Out << "?_4"; break;
48014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?_5 # |=
48114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_PipeEqual: Out << "?_5"; break;
48214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?_6 # ^=
48314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_CaretEqual: Out << "?_6"; break;
48414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_7 # vftable
48514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_8 # vbtable
48614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_9 # vcall
48714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_A # typeof
48814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_B # local static guard
48914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_C # string
49014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_D # vbase destructor
49114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_E # vector deleting destructor
49214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_F # default constructor closure
49314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_G # scalar deleting destructor
49414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_H # vector constructor iterator
49514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_I # vector destructor iterator
49614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_J # vector vbase constructor iterator
49714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_K # virtual displacement map
49814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_L # eh vector constructor iterator
49914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_M # eh vector destructor iterator
50014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_N # eh vector vbase constructor iterator
50114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_O # copy constructor closure
50214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_P<name> # udt returning <name>
50314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_Q # <unknown>
50414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_R0 # RTTI Type Descriptor
50514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
50614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_R2 # RTTI Base Class Array
50714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_R3 # RTTI Class Hierarchy Descriptor
50814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_R4 # RTTI Complete Object Locator
50914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_S # local vftable
51014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     ?_T # local vftable constructor closure
51114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?_U # new[]
51214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Array_New: Out << "?_U"; break;
51314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <operator-name> ::= ?_V # delete[]
51414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Array_Delete: Out << "?_V"; break;
51514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
51614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_Conditional:
51714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    assert(false && "Don't know how to mangle ?:");
51814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    break;
51914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
52014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case OO_None:
52114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case NUM_OVERLOADED_OPERATORS:
52214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    assert(false && "Not an overloaded operator");
52314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    break;
52414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
52514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
52614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
52714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
52814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <source name> ::= <identifier> @
52914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  Out << II->getName() << '@';
53014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
53114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
53214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
53314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  llvm::SmallString<64> Buffer;
53414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  Context.mangleObjCMethodName(MD, Buffer);
53514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  Out << Buffer;
53614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
53714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
53814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
53914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                               bool IsMember) {
54014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
54114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
54214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // 'I' means __restrict (32/64-bit).
54314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
54414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // keyword!
54514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <base-cvr-qualifiers> ::= A  # near
54614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= B  # near const
54714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= C  # near volatile
54814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= D  # near const volatile
54914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= E  # far (16-bit)
55014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= F  # far const (16-bit)
55114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= G  # far volatile (16-bit)
55214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= H  # far const volatile (16-bit)
55314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= I  # huge (16-bit)
55414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= J  # huge const (16-bit)
55514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= K  # huge volatile (16-bit)
55614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= L  # huge const volatile (16-bit)
55714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= M <basis> # based
55814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= N <basis> # based const
55914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= O <basis> # based volatile
56014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= P <basis> # based const volatile
56114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= Q  # near member
56214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= R  # near const member
56314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= S  # near volatile member
56414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= T  # near const volatile member
56514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= U  # far member (16-bit)
56614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= V  # far const member (16-bit)
56714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= W  # far volatile member (16-bit)
56814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= X  # far const volatile member (16-bit)
56914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= Y  # huge member (16-bit)
57014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= Z  # huge const member (16-bit)
57114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= 0  # huge volatile member (16-bit)
57214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= 1  # huge const volatile member (16-bit)
57314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= 2 <basis> # based member
57414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= 3 <basis> # based const member
57514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= 4 <basis> # based volatile member
57614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= 5 <basis> # based const volatile member
57714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= 6  # near function (pointers only)
57814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= 7  # far function (pointers only)
57914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= 8  # near method (pointers only)
58014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= 9  # far method (pointers only)
58114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= _A <basis> # based function (pointers only)
58214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= _B <basis> # based function (far?) (pointers only)
58314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= _C <basis> # based method (pointers only)
58414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= _D <basis> # based method (far?) (pointers only)
58514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                       ::= _E # block (Clang)
58614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <basis> ::= 0 # __based(void)
58714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //         ::= 1 # __based(segment)?
58814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //         ::= 2 <name> # __based(name)
58914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //         ::= 3 # ?
59014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //         ::= 4 # ?
59114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //         ::= 5 # not really based
59214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (!IsMember) {
59314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    if (!Quals.hasVolatile()) {
59414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      if (!Quals.hasConst())
59514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        Out << 'A';
59614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      else
59714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        Out << 'B';
59814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    } else {
59914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      if (!Quals.hasConst())
60014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        Out << 'C';
60114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      else
60214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        Out << 'D';
60314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    }
60414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  } else {
60514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    if (!Quals.hasVolatile()) {
60614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      if (!Quals.hasConst())
60714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        Out << 'Q';
60814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      else
60914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        Out << 'R';
61014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    } else {
61114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      if (!Quals.hasConst())
61214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        Out << 'S';
61314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      else
61414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        Out << 'T';
61514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    }
61614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
61714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
61814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // FIXME: For now, just drop all extension qualifiers on the floor.
61914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
62014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
62114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(QualType T) {
62214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Only operate on the canonical type!
62314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  T = getASTContext().getCanonicalType(T);
62414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
62514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  Qualifiers Quals = T.getLocalQualifiers();
62614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (Quals) {
62714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    // We have to mangle these now, while we still have enough information.
62814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    // <pointer-cvr-qualifiers> ::= P  # pointer
62914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    //                          ::= Q  # const pointer
63014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    //                          ::= R  # volatile pointer
63114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    //                          ::= S  # const volatile pointer
63214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    if (T->isAnyPointerType() || T->isMemberPointerType() ||
63314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        T->isBlockPointerType()) {
63414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      if (!Quals.hasVolatile())
63514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        Out << 'Q';
63614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      else {
63714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        if (!Quals.hasConst())
63814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne          Out << 'R';
63914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        else
64014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne          Out << 'S';
64114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      }
64214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    } else
64314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      // Just emit qualifiers like normal.
64414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      // NB: When we mangle a pointer/reference type, and the pointee
64514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      // type has no qualifiers, the lack of qualifier gets mangled
64614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      // in there.
64714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      mangleQualifiers(Quals, false);
64814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  } else if (T->isAnyPointerType() || T->isMemberPointerType() ||
64914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne             T->isBlockPointerType()) {
65014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << 'P';
65114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
65214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  switch (T->getTypeClass()) {
65314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#define ABSTRACT_TYPE(CLASS, PARENT)
65414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#define NON_CANONICAL_TYPE(CLASS, PARENT) \
65514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournecase Type::CLASS: \
65614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournellvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
65714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournereturn;
65814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#define TYPE(CLASS, PARENT) \
65914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournecase Type::CLASS: \
66014110477887e3dc168ffc6c191e72d705051f99ePeter CollingbournemangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \
66114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournebreak;
66214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/TypeNodes.def"
66314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
66414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
66514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
66614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const BuiltinType *T) {
66714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //  <type>         ::= <builtin-type>
66814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //  <builtin-type> ::= X  # void
66914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= C  # signed char
67014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= D  # char
67114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= E  # unsigned char
67214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= F  # short
67314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= G  # unsigned short (or wchar_t if it's not a builtin)
67414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= H  # int
67514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= I  # unsigned int
67614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= J  # long
67714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= K  # unsigned long
67814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     L  # <none>
67914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= M  # float
68014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= N  # double
68114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= O  # long double (__float80 is mangled differently)
68214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= _D # __int8 (yup, it's a distinct type in MSVC)
68314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= _E # unsigned __int8
68414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= _F # __int16
68514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= _G # unsigned __int16
68614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= _H # __int32
68714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= _I # unsigned __int32
68814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= _J # long long, __int64
68914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= _K # unsigned long long, __int64
69014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= _L # __int128
69114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= _M # unsigned __int128
69214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= _N # bool
69314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     _O # <array in parameter>
69414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= _T # __float80 (Intel)
69514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= _W # wchar_t
69614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= _Z # __float80 (Digital Mars)
69714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  switch (T->getKind()) {
69814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::Void: Out << 'X'; break;
69914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::SChar: Out << 'C'; break;
70014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
70114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::UChar: Out << 'E'; break;
70214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::Short: Out << 'F'; break;
70314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::UShort: Out << 'G'; break;
70414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::Int: Out << 'H'; break;
70514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::UInt: Out << 'I'; break;
70614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::Long: Out << 'J'; break;
70714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::ULong: Out << 'K'; break;
70814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::Float: Out << 'M'; break;
70914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::Double: Out << 'N'; break;
71014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // TODO: Determine size and mangle accordingly
71114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::LongDouble: Out << 'O'; break;
71214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // TODO: __int8 and friends
71314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::LongLong: Out << "_J"; break;
71414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::ULongLong: Out << "_K"; break;
71514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::Int128: Out << "_L"; break;
71614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::UInt128: Out << "_M"; break;
71714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::Bool: Out << "_N"; break;
71814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::WChar_S:
71914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::WChar_U: Out << "_W"; break;
72014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
72114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::Overload:
72214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::Dependent:
72314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    assert(false &&
72414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne           "Overloaded and dependent types shouldn't get to name mangling");
72514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    break;
72614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::UndeducedAuto:
72714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    assert(0 && "Should not see undeduced auto here");
72814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    break;
72914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
73014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
73114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
73214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
73314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::Char16:
73414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::Char32:
73514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case BuiltinType::NullPtr:
73614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    assert(false && "Don't know how to mangle this type");
73714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    break;
73814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
73914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
74014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
74114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// <type>          ::= <function-type>
74214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T) {
74314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Structors only appear in decls, so at this point we know it's not a
74414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // structor type.
74514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // I'll probably have mangleType(MemberPointerType) call the mangleType()
74614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // method directly.
74714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleType(T, NULL, false, false);
74814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
74914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T) {
75014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  llvm_unreachable("Can't mangle K&R function prototypes");
75114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
75214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
75314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const FunctionType *T,
75414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                         const FunctionDecl *D,
75514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                         bool IsStructor,
75614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                         bool IsInstMethod) {
75714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
75814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                     <return-type> <argument-list> <throw-spec>
75914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
76014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
76114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // If this is a C++ instance method, mangle the CVR qualifiers for the
76214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // this pointer.
76314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (IsInstMethod)
76414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
76514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
76614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleCallingConvention(T, IsInstMethod);
76714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
76814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <return-type> ::= <type>
76914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //               ::= @ # structors (they have no declared return type)
77014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (IsStructor)
77114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << '@';
77214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  else
77314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleType(Proto->getResultType());
77414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
77514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <argument-list> ::= X # void
77614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= <type>+ @
77714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                 ::= <type>* Z # varargs
77814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
77914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << 'X';
78014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  } else {
78114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    if (D) {
78214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      // If we got a decl, use the "types-as-written" to make sure arrays
78314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      // get mangled right.
78414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      for (FunctionDecl::param_const_iterator Parm = D->param_begin(),
78514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne           ParmEnd = D->param_end();
78614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne           Parm != ParmEnd; ++Parm)
78714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        mangleType((*Parm)->getTypeSourceInfo()->getType());
78814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    } else {
78914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
79014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne           ArgEnd = Proto->arg_type_end();
79114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne           Arg != ArgEnd; ++Arg)
79214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        mangleType(*Arg);
79314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    }
79414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    // <builtin-type>      ::= Z  # ellipsis
79514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    if (Proto->isVariadic())
79614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      Out << 'Z';
79714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    else
79814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      Out << '@';
79914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
80014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
80114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleThrowSpecification(Proto);
80214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
80314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
80414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
80514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <function-class> ::= A # private: near
80614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= B # private: far
80714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= C # private: static near
80814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= D # private: static far
80914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= E # private: virtual near
81014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= F # private: virtual far
81114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= G # private: thunk near
81214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= H # private: thunk far
81314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= I # protected: near
81414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= J # protected: far
81514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= K # protected: static near
81614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= L # protected: static far
81714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= M # protected: virtual near
81814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= N # protected: virtual far
81914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= O # protected: thunk near
82014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= P # protected: thunk far
82114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= Q # public: near
82214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= R # public: far
82314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= S # public: static near
82414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= T # public: static far
82514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= U # public: virtual near
82614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= V # public: virtual far
82714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= W # public: thunk near
82814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= X # public: thunk far
82914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= Y # global near
83014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                  ::= Z # global far
83114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
83214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    switch (MD->getAccess()) {
83314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      default:
83414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      case AS_private:
83514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        if (MD->isStatic())
83614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne          Out << 'C';
83714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        else if (MD->isVirtual())
83814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne          Out << 'E';
83914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        else
84014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne          Out << 'A';
84114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        break;
84214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      case AS_protected:
84314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        if (MD->isStatic())
84414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne          Out << 'K';
84514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        else if (MD->isVirtual())
84614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne          Out << 'M';
84714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        else
84814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne          Out << 'I';
84914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        break;
85014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      case AS_public:
85114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        if (MD->isStatic())
85214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne          Out << 'S';
85314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        else if (MD->isVirtual())
85414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne          Out << 'U';
85514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne        else
85614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne          Out << 'Q';
85714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    }
85814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  } else
85914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << 'Y';
86014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
86114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
86214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                                      bool IsInstMethod) {
86314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <calling-convention> ::= A # __cdecl
86414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                      ::= B # __export __cdecl
86514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                      ::= C # __pascal
86614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                      ::= D # __export __pascal
86714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                      ::= E # __thiscall
86814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                      ::= F # __export __thiscall
86914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                      ::= G # __stdcall
87014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                      ::= H # __export __stdcall
87114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                      ::= I # __fastcall
87214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //                      ::= J # __export __fastcall
87314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // The 'export' calling conventions are from a bygone era
87414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // (*cough*Win16*cough*) when functions were declared for export with
87514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // that keyword. (It didn't actually export them, it just made them so
87614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // that they could be in a DLL and somebody from another module could call
87714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // them.)
87814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  CallingConv CC = T->getCallConv();
87914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (CC == CC_Default)
88014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    CC = IsInstMethod ? getASTContext().getDefaultMethodCallConv() : CC_C;
88114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  switch (CC) {
88214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case CC_Default:
88314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case CC_C: Out << 'A'; break;
88414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case CC_X86Pascal: Out << 'C'; break;
88514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case CC_X86ThisCall: Out << 'E'; break;
88614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case CC_X86StdCall: Out << 'G'; break;
88714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case CC_X86FastCall: Out << 'I'; break;
88814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
88914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
89014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleThrowSpecification(
89114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                                const FunctionProtoType *FT) {
89214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // <throw-spec> ::= Z # throw(...) (default)
89314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //              ::= @ # throw() or __declspec/__attribute__((nothrow))
89414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  //              ::= <type>+
89514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // NOTE: Since the Microsoft compiler ignores throw specifications, they are
89614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // all actually mangled as 'Z'. (They're ignored because their associated
89714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // functionality isn't implemented, and probably never will be.)
89814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  Out << 'Z';
89914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
90014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
90114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T) {
90214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Don't know how to mangle UnresolvedUsingTypes yet!");
90314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
90414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
90514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// <type>        ::= <union-type> | <struct-type> | <class-type> | <enum-type>
90614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// <union-type>  ::= T <name>
90714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// <struct-type> ::= U <name>
90814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// <class-type>  ::= V <name>
90914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// <enum-type>   ::= W <size> <name>
91014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const EnumType *T) {
91114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleType(static_cast<const TagType*>(T));
91214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
91314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const RecordType *T) {
91414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleType(static_cast<const TagType*>(T));
91514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
91614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const TagType *T) {
91714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  switch (T->getDecl()->getTagKind()) {
91814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case TTK_Union:
91914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      Out << 'T';
92014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      break;
92114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case TTK_Struct:
92214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      Out << 'U';
92314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      break;
92414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case TTK_Class:
92514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      Out << 'V';
92614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      break;
92714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    case TTK_Enum:
92814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      Out << 'W';
92914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      Out << getASTContext().getTypeSizeInChars(
93014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity();
93114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      break;
93214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
93314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleName(T->getDecl());
93414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
93514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
93614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// <type>       ::= <array-type>
93714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// <array-type> ::= P <cvr-qualifiers> [Y <dimension-count> <dimension>+]
93814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne//                                                  <element-type> # as global
93914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne//              ::= Q <cvr-qualifiers> [Y <dimension-count> <dimension>+]
94014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne//                                                  <element-type> # as param
94114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// It's supposed to be the other way around, but for some strange reason, it
94214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// isn't. Today this behavior is retained for the sole purpose of backwards
94314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// compatibility.
94414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const ArrayType *T, bool IsGlobal) {
94514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // This isn't a recursive mangling, so now we have to do it all in this
94614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // one call.
94714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (IsGlobal)
94814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << 'P';
94914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  else
95014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << 'Q';
95114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleExtraDimensions(T->getElementType());
95214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
95314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T) {
95414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleType(static_cast<const ArrayType *>(T), false);
95514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
95614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T) {
95714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleType(static_cast<const ArrayType *>(T), false);
95814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
95914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T) {
96014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleType(static_cast<const ArrayType *>(T), false);
96114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
96214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T) {
96314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleType(static_cast<const ArrayType *>(T), false);
96414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
96514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleExtraDimensions(QualType ElementTy) {
96614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  llvm::SmallVector<llvm::APInt, 3> Dimensions;
96714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  for (;;) {
96814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    if (ElementTy->isConstantArrayType()) {
96914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      const ConstantArrayType *CAT =
97014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      static_cast<const ConstantArrayType *>(ElementTy.getTypePtr());
97114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      Dimensions.push_back(CAT->getSize());
97214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      ElementTy = CAT->getElementType();
97314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    } else if (ElementTy->isVariableArrayType()) {
97414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      assert(false && "Don't know how to mangle VLAs!");
97514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    } else if (ElementTy->isDependentSizedArrayType()) {
97614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      // The dependent expression has to be folded into a constant (TODO).
97714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      assert(false && "Don't know how to mangle dependent-sized arrays!");
97814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    } else if (ElementTy->isIncompleteArrayType()) continue;
97914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    else break;
98014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
98114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleQualifiers(ElementTy.getQualifiers(), false);
98214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // If there are any additional dimensions, mangle them now.
98314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (Dimensions.size() > 0) {
98414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << 'Y';
98514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    // <dimension-count> ::= <number> # number of extra dimensions
98614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleNumber(Dimensions.size());
98714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim) {
98814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      mangleNumber(Dimensions[Dim].getLimitedValue());
98914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    }
99014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
99114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleType(ElementTy.getLocalUnqualifiedType());
99214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
99314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
99414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// <type>                   ::= <pointer-to-member-type>
99514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
99614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne//                                                          <class name> <type>
99714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T) {
99814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  QualType PointeeType = T->getPointeeType();
99914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
100014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << '8';
100114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleName(cast<RecordType>(T->getClass())->getDecl());
100214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleType(FPT, NULL, false, true);
100314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  } else {
100414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleQualifiers(PointeeType.getQualifiers(), true);
100514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleName(cast<RecordType>(T->getClass())->getDecl());
100614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleType(PointeeType.getLocalUnqualifiedType());
100714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
100814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
100914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
101014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T) {
101114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Don't know how to mangle TemplateTypeParmTypes yet!");
101214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
101314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
1014c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregorvoid MicrosoftCXXNameMangler::mangleType(
1015c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                       const SubstTemplateTypeParmPackType *T) {
1016c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  assert(false &&
1017c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor         "Don't know how to mangle SubstTemplateTypeParmPackTypes yet!");
1018c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
1019c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
102014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// <type> ::= <pointer-type>
102114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// <pointer-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
102214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const PointerType *T) {
102314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  QualType PointeeTy = T->getPointeeType();
102414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (PointeeTy->isArrayType()) {
102514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    // Pointers to arrays are mangled like arrays.
102614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleExtraDimensions(T->getPointeeType());
102714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  } else if (PointeeTy->isFunctionType()) {
102814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    // Function pointers are special.
102914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << '6';
103014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleType(static_cast<const FunctionType *>(PointeeTy.getTypePtr()),
103114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne               NULL, false, false);
103214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  } else {
103314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    if (!PointeeTy.hasQualifiers())
103414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      // Lack of qualifiers is mangled as 'A'.
103514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne      Out << 'A';
103614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    mangleType(PointeeTy);
103714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
103814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
103914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
104014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // Object pointers never have qualifiers.
104114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  Out << 'A';
104214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleType(T->getPointeeType());
104314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
104414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
104514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// <type> ::= <reference-type>
104614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne// <reference-type> ::= A <cvr-qualifiers> <type>
104714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T) {
104814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  Out << 'A';
104914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  QualType PointeeTy = T->getPointeeType();
105014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  if (!PointeeTy.hasQualifiers())
105114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    // Lack of qualifiers is mangled as 'A'.
105214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    Out << 'A';
105314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleType(PointeeTy);
105414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
105514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
105614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T) {
105714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Don't know how to mangle RValueReferenceTypes yet!");
105814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
105914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
106014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const ComplexType *T) {
106114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Don't know how to mangle ComplexTypes yet!");
106214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
106314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
106414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const VectorType *T) {
106514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Don't know how to mangle VectorTypes yet!");
106614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
106714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T) {
106814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Don't know how to mangle ExtVectorTypes yet!");
106914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
107014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
107114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Don't know how to mangle DependentSizedExtVectorTypes yet!");
107214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
107314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
107414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T) {
107514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // ObjC interfaces have structs underlying them.
107614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  Out << 'U';
107714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleName(T->getDecl());
107814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
107914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
108014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T) {
108114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // We don't allow overloading by different protocol qualification,
108214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  // so mangling them isn't necessary.
108314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleType(T->getBaseType());
108414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
108514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
108614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T) {
108714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  Out << "_E";
108814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  mangleType(T->getPointeeType());
108914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
109014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
109114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T) {
109214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Don't know how to mangle InjectedClassNameTypes yet!");
109314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
109414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
109514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T) {
109614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Don't know how to mangle TemplateSpecializationTypes yet!");
109714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
109814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
109914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const DependentNameType *T) {
110014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Don't know how to mangle DependentNameTypes yet!");
110114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
110214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
110314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(
110414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                 const DependentTemplateSpecializationType *T) {
110514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false &&
110614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne         "Don't know how to mangle DependentTemplateSpecializationTypes yet!");
110714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
110814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
110914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T) {
111014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Don't know how to mangle PackExpansionTypes yet!");
111114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
111214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
111314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const TypeOfType *T) {
111414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Don't know how to mangle TypeOfTypes yet!");
111514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
111614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
111714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T) {
111814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Don't know how to mangle TypeOfExprTypes yet!");
111914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
112014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
112114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftCXXNameMangler::mangleType(const DecltypeType *T) {
112214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Don't know how to mangle DecltypeTypes yet!");
112314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
112414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
112514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftMangleContext::mangleName(const NamedDecl *D,
112614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                        llvm::SmallVectorImpl<char> &Name) {
112714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
112814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne         "Invalid mangleName() call, argument is not a variable or function!");
112914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
113014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne         "Invalid mangleName() call on 'structor decl!");
113114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
113214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
113314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                 getASTContext().getSourceManager(),
113414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                 "Mangling declaration");
113514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
113614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  MicrosoftCXXNameMangler Mangler(*this, Name);
113714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  return Mangler.mangle(D);
113814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
113914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
114014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                         const ThunkInfo &Thunk,
114114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                         llvm::SmallVectorImpl<char> &) {
114214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Can't yet mangle thunks!");
114314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
114414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
114514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                                CXXDtorType Type,
114614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                                const ThisAdjustment &,
114714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                                llvm::SmallVectorImpl<char> &) {
114814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Can't yet mangle destructor thunks!");
114914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
115014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
115114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                             llvm::SmallVectorImpl<char> &) {
115214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Can't yet mangle virtual tables!");
115314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
115414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
115514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                          llvm::SmallVectorImpl<char> &) {
115614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
115714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
115814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
115914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                                 int64_t Offset,
116014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                                 const CXXRecordDecl *Type,
116114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                                 llvm::SmallVectorImpl<char> &) {
116214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
116314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
116414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftMangleContext::mangleCXXRTTI(QualType T,
116514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                           llvm::SmallVectorImpl<char> &) {
116614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Can't yet mangle RTTI!");
116714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
116814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
116914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                               llvm::SmallVectorImpl<char> &) {
117014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Can't yet mangle RTTI names!");
117114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
117214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
117314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                           CXXCtorType Type,
117414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                           llvm::SmallVectorImpl<char> &) {
117514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Can't yet mangle constructors!");
117614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
117714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
117814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                           CXXDtorType Type,
117914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                           llvm::SmallVectorImpl<char> &) {
118014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Can't yet mangle destructors!");
118114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
118214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbournevoid MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *,
118314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                                llvm::SmallVectorImpl<char> &) {
118414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(false && "Can't yet mangle reference temporaries!");
118514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
118614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
118714110477887e3dc168ffc6c191e72d705051f99ePeter CollingbourneMangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
118814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne                                                   Diagnostic &Diags) {
118914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  return new MicrosoftMangleContext(Context, Diags);
119014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
1191