1//===--- MicrosoftMangle.cpp - Microsoft Visual C++ Name Mangling ---------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides C++ name mangling targeting the Microsoft Visual C++ ABI.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Mangle.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/CharUnits.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
21#include "clang/AST/ExprCXX.h"
22#include "clang/Basic/ABI.h"
23
24#include <map>
25
26using namespace clang;
27
28namespace {
29
30/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
31/// Microsoft Visual C++ ABI.
32class MicrosoftCXXNameMangler {
33  MangleContext &Context;
34  raw_ostream &Out;
35
36  // FIXME: audit the performance of BackRefMap as it might do way too many
37  // copying of strings.
38  typedef std::map<std::string, unsigned> BackRefMap;
39  BackRefMap NameBackReferences;
40  bool UseNameBackReferences;
41
42  typedef llvm::DenseMap<void*, unsigned> ArgBackRefMap;
43  ArgBackRefMap TypeBackReferences;
44
45  ASTContext &getASTContext() const { return Context.getASTContext(); }
46
47public:
48  MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_)
49  : Context(C), Out(Out_), UseNameBackReferences(true) { }
50
51  raw_ostream &getStream() const { return Out; }
52
53  void mangle(const NamedDecl *D, StringRef Prefix = "\01?");
54  void mangleName(const NamedDecl *ND);
55  void mangleFunctionEncoding(const FunctionDecl *FD);
56  void mangleVariableEncoding(const VarDecl *VD);
57  void mangleNumber(int64_t Number);
58  void mangleNumber(const llvm::APSInt &Value);
59  void mangleType(QualType T, SourceRange Range, bool MangleQualifiers = true);
60
61private:
62  void disableBackReferences() { UseNameBackReferences = false; }
63  void mangleUnqualifiedName(const NamedDecl *ND) {
64    mangleUnqualifiedName(ND, ND->getDeclName());
65  }
66  void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
67  void mangleSourceName(const IdentifierInfo *II);
68  void manglePostfix(const DeclContext *DC, bool NoFunction=false);
69  void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc);
70  void mangleQualifiers(Qualifiers Quals, bool IsMember);
71  void manglePointerQualifiers(Qualifiers Quals);
72
73  void mangleUnscopedTemplateName(const TemplateDecl *ND);
74  void mangleTemplateInstantiationName(const TemplateDecl *TD,
75                      const SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs);
76  void mangleObjCMethodName(const ObjCMethodDecl *MD);
77  void mangleLocalName(const FunctionDecl *FD);
78
79  void mangleArgumentType(QualType T, SourceRange Range);
80
81  // Declare manglers for every type class.
82#define ABSTRACT_TYPE(CLASS, PARENT)
83#define NON_CANONICAL_TYPE(CLASS, PARENT)
84#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \
85                                            SourceRange Range);
86#include "clang/AST/TypeNodes.def"
87#undef ABSTRACT_TYPE
88#undef NON_CANONICAL_TYPE
89#undef TYPE
90
91  void mangleType(const TagType*);
92  void mangleType(const FunctionType *T, const FunctionDecl *D,
93                  bool IsStructor, bool IsInstMethod);
94  void mangleType(const ArrayType *T, bool IsGlobal);
95  void mangleExtraDimensions(QualType T);
96  void mangleFunctionClass(const FunctionDecl *FD);
97  void mangleCallingConvention(const FunctionType *T, bool IsInstMethod = false);
98  void mangleIntegerLiteral(QualType T, const llvm::APSInt &Number);
99  void mangleThrowSpecification(const FunctionProtoType *T);
100
101  void mangleTemplateArgs(
102                      const SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs);
103
104};
105
106/// MicrosoftMangleContext - Overrides the default MangleContext for the
107/// Microsoft Visual C++ ABI.
108class MicrosoftMangleContext : public MangleContext {
109public:
110  MicrosoftMangleContext(ASTContext &Context,
111                   DiagnosticsEngine &Diags) : MangleContext(Context, Diags) { }
112  virtual bool shouldMangleDeclName(const NamedDecl *D);
113  virtual void mangleName(const NamedDecl *D, raw_ostream &Out);
114  virtual void mangleThunk(const CXXMethodDecl *MD,
115                           const ThunkInfo &Thunk,
116                           raw_ostream &);
117  virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
118                                  const ThisAdjustment &ThisAdjustment,
119                                  raw_ostream &);
120  virtual void mangleCXXVTable(const CXXRecordDecl *RD,
121                               raw_ostream &);
122  virtual void mangleCXXVTT(const CXXRecordDecl *RD,
123                            raw_ostream &);
124  virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
125                                   const CXXRecordDecl *Type,
126                                   raw_ostream &);
127  virtual void mangleCXXRTTI(QualType T, raw_ostream &);
128  virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
129  virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
130                             raw_ostream &);
131  virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
132                             raw_ostream &);
133  virtual void mangleReferenceTemporary(const clang::VarDecl *,
134                                        raw_ostream &);
135};
136
137}
138
139static bool isInCLinkageSpecification(const Decl *D) {
140  D = D->getCanonicalDecl();
141  for (const DeclContext *DC = D->getDeclContext();
142       !DC->isTranslationUnit(); DC = DC->getParent()) {
143    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
144      return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
145  }
146
147  return false;
148}
149
150bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
151  // In C, functions with no attributes never need to be mangled. Fastpath them.
152  if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
153    return false;
154
155  // Any decl can be declared with __asm("foo") on it, and this takes precedence
156  // over all other naming in the .o file.
157  if (D->hasAttr<AsmLabelAttr>())
158    return true;
159
160  // Clang's "overloadable" attribute extension to C/C++ implies name mangling
161  // (always) as does passing a C++ member function and a function
162  // whose name is not a simple identifier.
163  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
164  if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
165             !FD->getDeclName().isIdentifier()))
166    return true;
167
168  // Otherwise, no mangling is done outside C++ mode.
169  if (!getASTContext().getLangOpts().CPlusPlus)
170    return false;
171
172  // Variables at global scope with internal linkage are not mangled.
173  if (!FD) {
174    const DeclContext *DC = D->getDeclContext();
175    if (DC->isTranslationUnit() && D->getLinkage() == InternalLinkage)
176      return false;
177  }
178
179  // C functions and "main" are not mangled.
180  if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
181    return false;
182
183  return true;
184}
185
186void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
187                                     StringRef Prefix) {
188  // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
189  // Therefore it's really important that we don't decorate the
190  // name with leading underscores or leading/trailing at signs. So, by
191  // default, we emit an asm marker at the start so we get the name right.
192  // Callers can override this with a custom prefix.
193
194  // Any decl can be declared with __asm("foo") on it, and this takes precedence
195  // over all other naming in the .o file.
196  if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
197    // If we have an asm name, then we use it as the mangling.
198    Out << '\01' << ALA->getLabel();
199    return;
200  }
201
202  // <mangled-name> ::= ? <name> <type-encoding>
203  Out << Prefix;
204  mangleName(D);
205  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
206    mangleFunctionEncoding(FD);
207  else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
208    mangleVariableEncoding(VD);
209  else {
210    // TODO: Fields? Can MSVC even mangle them?
211    // Issue a diagnostic for now.
212    DiagnosticsEngine &Diags = Context.getDiags();
213    unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
214      "cannot mangle this declaration yet");
215    Diags.Report(D->getLocation(), DiagID)
216      << D->getSourceRange();
217  }
218}
219
220void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
221  // <type-encoding> ::= <function-class> <function-type>
222
223  // Don't mangle in the type if this isn't a decl we should typically mangle.
224  if (!Context.shouldMangleDeclName(FD))
225    return;
226
227  // We should never ever see a FunctionNoProtoType at this point.
228  // We don't even know how to mangle their types anyway :).
229  const FunctionProtoType *FT = FD->getType()->castAs<FunctionProtoType>();
230
231  bool InStructor = false, InInstMethod = false;
232  const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
233  if (MD) {
234    if (MD->isInstance())
235      InInstMethod = true;
236    if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
237      InStructor = true;
238  }
239
240  // First, the function class.
241  mangleFunctionClass(FD);
242
243  mangleType(FT, FD, InStructor, InInstMethod);
244}
245
246void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
247  // <type-encoding> ::= <storage-class> <variable-type>
248  // <storage-class> ::= 0  # private static member
249  //                 ::= 1  # protected static member
250  //                 ::= 2  # public static member
251  //                 ::= 3  # global
252  //                 ::= 4  # static local
253
254  // The first character in the encoding (after the name) is the storage class.
255  if (VD->isStaticDataMember()) {
256    // If it's a static member, it also encodes the access level.
257    switch (VD->getAccess()) {
258      default:
259      case AS_private: Out << '0'; break;
260      case AS_protected: Out << '1'; break;
261      case AS_public: Out << '2'; break;
262    }
263  }
264  else if (!VD->isStaticLocal())
265    Out << '3';
266  else
267    Out << '4';
268  // Now mangle the type.
269  // <variable-type> ::= <type> <cvr-qualifiers>
270  //                 ::= <type> <pointee-cvr-qualifiers> # pointers, references
271  // Pointers and references are odd. The type of 'int * const foo;' gets
272  // mangled as 'QAHA' instead of 'PAHB', for example.
273  TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
274  QualType Ty = TL.getType();
275  if (Ty->isPointerType() || Ty->isReferenceType()) {
276    mangleType(Ty, TL.getSourceRange());
277    mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
278  } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
279    // Global arrays are funny, too.
280    mangleType(AT, true);
281    mangleQualifiers(Ty.getQualifiers(), false);
282  } else {
283    mangleType(Ty.getLocalUnqualifiedType(), TL.getSourceRange());
284    mangleQualifiers(Ty.getLocalQualifiers(), false);
285  }
286}
287
288void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
289  // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
290  const DeclContext *DC = ND->getDeclContext();
291
292  // Always start with the unqualified name.
293  mangleUnqualifiedName(ND);
294
295  // If this is an extern variable declared locally, the relevant DeclContext
296  // is that of the containing namespace, or the translation unit.
297  if (isa<FunctionDecl>(DC) && ND->hasLinkage())
298    while (!DC->isNamespace() && !DC->isTranslationUnit())
299      DC = DC->getParent();
300
301  manglePostfix(DC);
302
303  // Terminate the whole name with an '@'.
304  Out << '@';
305}
306
307void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
308  // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
309  //          ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
310  //          ::= [?] @ # 0 (alternate mangling, not emitted by VC)
311  if (Number < 0) {
312    Out << '?';
313    Number = -Number;
314  }
315  // There's a special shorter mangling for 0, but Microsoft
316  // chose not to use it. Instead, 0 gets mangled as "A@". Oh well...
317  if (Number >= 1 && Number <= 10)
318    Out << Number-1;
319  else {
320    // We have to build up the encoding in reverse order, so it will come
321    // out right when we write it out.
322    char Encoding[16];
323    char *EndPtr = Encoding+sizeof(Encoding);
324    char *CurPtr = EndPtr;
325    do {
326      *--CurPtr = 'A' + (Number % 16);
327      Number /= 16;
328    } while (Number);
329    Out.write(CurPtr, EndPtr-CurPtr);
330    Out << '@';
331  }
332}
333
334void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
335  if (Value.isSigned() && Value.isNegative()) {
336    Out << '?';
337    mangleNumber(llvm::APSInt(Value.abs()));
338    return;
339  }
340  llvm::APSInt Temp(Value);
341  if (Value.uge(1) && Value.ule(10)) {
342    --Temp;
343    Temp.print(Out, false);
344  } else {
345    // We have to build up the encoding in reverse order, so it will come
346    // out right when we write it out.
347    char Encoding[64];
348    char *EndPtr = Encoding+sizeof(Encoding);
349    char *CurPtr = EndPtr;
350    llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned());
351    NibbleMask = 0xf;
352    for (int i = 0, e = Value.getActiveBits() / 4; i != e; ++i) {
353      *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf);
354      Temp = Temp.lshr(4);
355    }
356    Out.write(CurPtr, EndPtr-CurPtr);
357    Out << '@';
358  }
359}
360
361static const TemplateDecl *
362isTemplate(const NamedDecl *ND,
363           SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs) {
364  // Check if we have a function template.
365  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
366    if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
367      if (FD->getTemplateSpecializationArgsAsWritten()) {
368        const ASTTemplateArgumentListInfo *ArgList =
369          FD->getTemplateSpecializationArgsAsWritten();
370        TemplateArgs.append(ArgList->getTemplateArgs(),
371                            ArgList->getTemplateArgs() +
372                              ArgList->NumTemplateArgs);
373      } else {
374        const TemplateArgumentList *ArgList =
375          FD->getTemplateSpecializationArgs();
376        TemplateArgumentListInfo LI;
377        for (unsigned i = 0, e = ArgList->size(); i != e; ++i)
378          TemplateArgs.push_back(TemplateArgumentLoc(ArgList->get(i),
379                                                     FD->getTypeSourceInfo()));
380      }
381      return TD;
382    }
383  }
384
385  // Check if we have a class template.
386  if (const ClassTemplateSpecializationDecl *Spec =
387      dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
388    TypeSourceInfo *TSI = Spec->getTypeAsWritten();
389    if (TSI) {
390      TemplateSpecializationTypeLoc &TSTL =
391        cast<TemplateSpecializationTypeLoc>(TSI->getTypeLoc());
392      TemplateArgumentListInfo LI(TSTL.getLAngleLoc(), TSTL.getRAngleLoc());
393      for (unsigned i = 0, e = TSTL.getNumArgs(); i != e; ++i)
394        TemplateArgs.push_back(TSTL.getArgLoc(i));
395    } else {
396      TemplateArgumentListInfo LI;
397      const TemplateArgumentList &ArgList =
398        Spec->getTemplateArgs();
399      for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
400        TemplateArgs.push_back(TemplateArgumentLoc(ArgList[i],
401                                                   TemplateArgumentLocInfo()));
402    }
403    return Spec->getSpecializedTemplate();
404  }
405
406  return 0;
407}
408
409void
410MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
411                                               DeclarationName Name) {
412  //  <unqualified-name> ::= <operator-name>
413  //                     ::= <ctor-dtor-name>
414  //                     ::= <source-name>
415  //                     ::= <template-name>
416  SmallVector<TemplateArgumentLoc, 2> TemplateArgs;
417  // Check if we have a template.
418  if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
419    // We have a template.
420    // Here comes the tricky thing: if we need to mangle something like
421    //   void foo(A::X<Y>, B::X<Y>),
422    // the X<Y> part is aliased. However, if you need to mangle
423    //   void foo(A::X<A::Y>, A::X<B::Y>),
424    // the A::X<> part is not aliased.
425    // That said, from the mangler's perspective we have a structure like this:
426    //   namespace[s] -> type[ -> template-parameters]
427    // but from the Clang perspective we have
428    //   type [ -> template-parameters]
429    //      \-> namespace[s]
430    // What we do is we create a new mangler, mangle the same type (without
431    // a namespace suffix) using the extra mangler with back references
432    // disabled (to avoid infinite recursion) and then use the mangled type
433    // name as a key to check the mangling of different types for aliasing.
434
435    std::string BackReferenceKey;
436    BackRefMap::iterator Found;
437    if (UseNameBackReferences) {
438      llvm::raw_string_ostream Stream(BackReferenceKey);
439      MicrosoftCXXNameMangler Extra(Context, Stream);
440      Extra.disableBackReferences();
441      Extra.mangleUnqualifiedName(ND, Name);
442      Stream.flush();
443
444      Found = NameBackReferences.find(BackReferenceKey);
445    }
446    if (!UseNameBackReferences || Found == NameBackReferences.end()) {
447      mangleTemplateInstantiationName(TD, TemplateArgs);
448      if (UseNameBackReferences && NameBackReferences.size() < 10) {
449        size_t Size = NameBackReferences.size();
450        NameBackReferences[BackReferenceKey] = Size;
451      }
452    } else {
453      Out << Found->second;
454    }
455    return;
456  }
457
458  switch (Name.getNameKind()) {
459    case DeclarationName::Identifier: {
460      if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
461        mangleSourceName(II);
462        break;
463      }
464
465      // Otherwise, an anonymous entity.  We must have a declaration.
466      assert(ND && "mangling empty name without declaration");
467
468      if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
469        if (NS->isAnonymousNamespace()) {
470          Out << "?A";
471          break;
472        }
473      }
474
475      // We must have an anonymous struct.
476      const TagDecl *TD = cast<TagDecl>(ND);
477      if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
478        assert(TD->getDeclContext() == D->getDeclContext() &&
479               "Typedef should not be in another decl context!");
480        assert(D->getDeclName().getAsIdentifierInfo() &&
481               "Typedef was not named!");
482        mangleSourceName(D->getDeclName().getAsIdentifierInfo());
483        break;
484      }
485
486      // When VC encounters an anonymous type with no tag and no typedef,
487      // it literally emits '<unnamed-tag>'.
488      Out << "<unnamed-tag>";
489      break;
490    }
491
492    case DeclarationName::ObjCZeroArgSelector:
493    case DeclarationName::ObjCOneArgSelector:
494    case DeclarationName::ObjCMultiArgSelector:
495      llvm_unreachable("Can't mangle Objective-C selector names here!");
496
497    case DeclarationName::CXXConstructorName:
498      Out << "?0";
499      break;
500
501    case DeclarationName::CXXDestructorName:
502      Out << "?1";
503      break;
504
505    case DeclarationName::CXXConversionFunctionName:
506      // <operator-name> ::= ?B # (cast)
507      // The target type is encoded as the return type.
508      Out << "?B";
509      break;
510
511    case DeclarationName::CXXOperatorName:
512      mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
513      break;
514
515    case DeclarationName::CXXLiteralOperatorName: {
516      // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
517      DiagnosticsEngine Diags = Context.getDiags();
518      unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
519        "cannot mangle this literal operator yet");
520      Diags.Report(ND->getLocation(), DiagID);
521      break;
522    }
523
524    case DeclarationName::CXXUsingDirective:
525      llvm_unreachable("Can't mangle a using directive name!");
526  }
527}
528
529void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
530                                            bool NoFunction) {
531  // <postfix> ::= <unqualified-name> [<postfix>]
532  //           ::= <substitution> [<postfix>]
533
534  if (!DC) return;
535
536  while (isa<LinkageSpecDecl>(DC))
537    DC = DC->getParent();
538
539  if (DC->isTranslationUnit())
540    return;
541
542  if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
543    Context.mangleBlock(BD, Out);
544    Out << '@';
545    return manglePostfix(DC->getParent(), NoFunction);
546  }
547
548  if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
549    return;
550  else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
551    mangleObjCMethodName(Method);
552  else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
553    mangleLocalName(Func);
554  else {
555    mangleUnqualifiedName(cast<NamedDecl>(DC));
556    manglePostfix(DC->getParent(), NoFunction);
557  }
558}
559
560void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
561                                                 SourceLocation Loc) {
562  switch (OO) {
563  //                     ?0 # constructor
564  //                     ?1 # destructor
565  // <operator-name> ::= ?2 # new
566  case OO_New: Out << "?2"; break;
567  // <operator-name> ::= ?3 # delete
568  case OO_Delete: Out << "?3"; break;
569  // <operator-name> ::= ?4 # =
570  case OO_Equal: Out << "?4"; break;
571  // <operator-name> ::= ?5 # >>
572  case OO_GreaterGreater: Out << "?5"; break;
573  // <operator-name> ::= ?6 # <<
574  case OO_LessLess: Out << "?6"; break;
575  // <operator-name> ::= ?7 # !
576  case OO_Exclaim: Out << "?7"; break;
577  // <operator-name> ::= ?8 # ==
578  case OO_EqualEqual: Out << "?8"; break;
579  // <operator-name> ::= ?9 # !=
580  case OO_ExclaimEqual: Out << "?9"; break;
581  // <operator-name> ::= ?A # []
582  case OO_Subscript: Out << "?A"; break;
583  //                     ?B # conversion
584  // <operator-name> ::= ?C # ->
585  case OO_Arrow: Out << "?C"; break;
586  // <operator-name> ::= ?D # *
587  case OO_Star: Out << "?D"; break;
588  // <operator-name> ::= ?E # ++
589  case OO_PlusPlus: Out << "?E"; break;
590  // <operator-name> ::= ?F # --
591  case OO_MinusMinus: Out << "?F"; break;
592  // <operator-name> ::= ?G # -
593  case OO_Minus: Out << "?G"; break;
594  // <operator-name> ::= ?H # +
595  case OO_Plus: Out << "?H"; break;
596  // <operator-name> ::= ?I # &
597  case OO_Amp: Out << "?I"; break;
598  // <operator-name> ::= ?J # ->*
599  case OO_ArrowStar: Out << "?J"; break;
600  // <operator-name> ::= ?K # /
601  case OO_Slash: Out << "?K"; break;
602  // <operator-name> ::= ?L # %
603  case OO_Percent: Out << "?L"; break;
604  // <operator-name> ::= ?M # <
605  case OO_Less: Out << "?M"; break;
606  // <operator-name> ::= ?N # <=
607  case OO_LessEqual: Out << "?N"; break;
608  // <operator-name> ::= ?O # >
609  case OO_Greater: Out << "?O"; break;
610  // <operator-name> ::= ?P # >=
611  case OO_GreaterEqual: Out << "?P"; break;
612  // <operator-name> ::= ?Q # ,
613  case OO_Comma: Out << "?Q"; break;
614  // <operator-name> ::= ?R # ()
615  case OO_Call: Out << "?R"; break;
616  // <operator-name> ::= ?S # ~
617  case OO_Tilde: Out << "?S"; break;
618  // <operator-name> ::= ?T # ^
619  case OO_Caret: Out << "?T"; break;
620  // <operator-name> ::= ?U # |
621  case OO_Pipe: Out << "?U"; break;
622  // <operator-name> ::= ?V # &&
623  case OO_AmpAmp: Out << "?V"; break;
624  // <operator-name> ::= ?W # ||
625  case OO_PipePipe: Out << "?W"; break;
626  // <operator-name> ::= ?X # *=
627  case OO_StarEqual: Out << "?X"; break;
628  // <operator-name> ::= ?Y # +=
629  case OO_PlusEqual: Out << "?Y"; break;
630  // <operator-name> ::= ?Z # -=
631  case OO_MinusEqual: Out << "?Z"; break;
632  // <operator-name> ::= ?_0 # /=
633  case OO_SlashEqual: Out << "?_0"; break;
634  // <operator-name> ::= ?_1 # %=
635  case OO_PercentEqual: Out << "?_1"; break;
636  // <operator-name> ::= ?_2 # >>=
637  case OO_GreaterGreaterEqual: Out << "?_2"; break;
638  // <operator-name> ::= ?_3 # <<=
639  case OO_LessLessEqual: Out << "?_3"; break;
640  // <operator-name> ::= ?_4 # &=
641  case OO_AmpEqual: Out << "?_4"; break;
642  // <operator-name> ::= ?_5 # |=
643  case OO_PipeEqual: Out << "?_5"; break;
644  // <operator-name> ::= ?_6 # ^=
645  case OO_CaretEqual: Out << "?_6"; break;
646  //                     ?_7 # vftable
647  //                     ?_8 # vbtable
648  //                     ?_9 # vcall
649  //                     ?_A # typeof
650  //                     ?_B # local static guard
651  //                     ?_C # string
652  //                     ?_D # vbase destructor
653  //                     ?_E # vector deleting destructor
654  //                     ?_F # default constructor closure
655  //                     ?_G # scalar deleting destructor
656  //                     ?_H # vector constructor iterator
657  //                     ?_I # vector destructor iterator
658  //                     ?_J # vector vbase constructor iterator
659  //                     ?_K # virtual displacement map
660  //                     ?_L # eh vector constructor iterator
661  //                     ?_M # eh vector destructor iterator
662  //                     ?_N # eh vector vbase constructor iterator
663  //                     ?_O # copy constructor closure
664  //                     ?_P<name> # udt returning <name>
665  //                     ?_Q # <unknown>
666  //                     ?_R0 # RTTI Type Descriptor
667  //                     ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
668  //                     ?_R2 # RTTI Base Class Array
669  //                     ?_R3 # RTTI Class Hierarchy Descriptor
670  //                     ?_R4 # RTTI Complete Object Locator
671  //                     ?_S # local vftable
672  //                     ?_T # local vftable constructor closure
673  // <operator-name> ::= ?_U # new[]
674  case OO_Array_New: Out << "?_U"; break;
675  // <operator-name> ::= ?_V # delete[]
676  case OO_Array_Delete: Out << "?_V"; break;
677
678  case OO_Conditional: {
679    DiagnosticsEngine &Diags = Context.getDiags();
680    unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
681      "cannot mangle this conditional operator yet");
682    Diags.Report(Loc, DiagID);
683    break;
684  }
685
686  case OO_None:
687  case NUM_OVERLOADED_OPERATORS:
688    llvm_unreachable("Not an overloaded operator");
689  }
690}
691
692void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
693  // <source name> ::= <identifier> @
694  std::string key = II->getNameStart();
695  BackRefMap::iterator Found;
696  if (UseNameBackReferences)
697    Found = NameBackReferences.find(key);
698  if (!UseNameBackReferences || Found == NameBackReferences.end()) {
699    Out << II->getName() << '@';
700    if (UseNameBackReferences && NameBackReferences.size() < 10) {
701      size_t Size = NameBackReferences.size();
702      NameBackReferences[key] = Size;
703    }
704  } else {
705    Out << Found->second;
706  }
707}
708
709void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
710  Context.mangleObjCMethodName(MD, Out);
711}
712
713// Find out how many function decls live above this one and return an integer
714// suitable for use as the number in a numbered anonymous scope.
715// TODO: Memoize.
716static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
717  const DeclContext *DC = FD->getParent();
718  int level = 1;
719
720  while (DC && !DC->isTranslationUnit()) {
721    if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
722    DC = DC->getParent();
723  }
724
725  return 2*level;
726}
727
728void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
729  // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
730  // <numbered-anonymous-scope> ::= ? <number>
731  // Even though the name is rendered in reverse order (e.g.
732  // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
733  // innermost. So a method bar in class C local to function foo gets mangled
734  // as something like:
735  // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
736  // This is more apparent when you have a type nested inside a method of a
737  // type nested inside a function. A method baz in class D local to method
738  // bar of class C local to function foo gets mangled as:
739  // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
740  // This scheme is general enough to support GCC-style nested
741  // functions. You could have a method baz of class C inside a function bar
742  // inside a function foo, like so:
743  // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
744  int NestLevel = getLocalNestingLevel(FD);
745  Out << '?';
746  mangleNumber(NestLevel);
747  Out << '?';
748  mangle(FD, "?");
749}
750
751void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
752                                                         const TemplateDecl *TD,
753                     const SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs) {
754  // <template-name> ::= <unscoped-template-name> <template-args>
755  //                 ::= <substitution>
756  // Always start with the unqualified name.
757
758  // Templates have their own context for back references.
759  BackRefMap TemplateContext;
760  NameBackReferences.swap(TemplateContext);
761
762  mangleUnscopedTemplateName(TD);
763  mangleTemplateArgs(TemplateArgs);
764
765  NameBackReferences.swap(TemplateContext);
766}
767
768void
769MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
770  // <unscoped-template-name> ::= ?$ <unqualified-name>
771  Out << "?$";
772  mangleUnqualifiedName(TD);
773}
774
775void
776MicrosoftCXXNameMangler::mangleIntegerLiteral(QualType T,
777                                              const llvm::APSInt &Value) {
778  // <integer-literal> ::= $0 <number>
779  Out << "$0";
780  // Make sure booleans are encoded as 0/1.
781  if (T->isBooleanType())
782    Out << (Value.getBoolValue() ? "0" : "A@");
783  else
784    mangleNumber(Value);
785}
786
787void
788MicrosoftCXXNameMangler::mangleTemplateArgs(
789                     const SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs) {
790  // <template-args> ::= {<type> | <integer-literal>}+ @
791  unsigned NumTemplateArgs = TemplateArgs.size();
792  for (unsigned i = 0; i < NumTemplateArgs; ++i) {
793    const TemplateArgumentLoc &TAL = TemplateArgs[i];
794    const TemplateArgument &TA = TAL.getArgument();
795    switch (TA.getKind()) {
796    case TemplateArgument::Null:
797      llvm_unreachable("Can't mangle null template arguments!");
798    case TemplateArgument::Type:
799      mangleType(TA.getAsType(), TAL.getSourceRange());
800      break;
801    case TemplateArgument::Integral:
802      mangleIntegerLiteral(TA.getIntegralType(), TA.getAsIntegral());
803      break;
804    case TemplateArgument::Expression: {
805      // See if this is a constant expression.
806      Expr *TAE = TA.getAsExpr();
807      llvm::APSInt Value;
808      if (TAE->isIntegerConstantExpr(Value, Context.getASTContext())) {
809        mangleIntegerLiteral(TAE->getType(), Value);
810        break;
811      }
812      /* fallthrough */
813    } default: {
814      // Issue a diagnostic.
815      DiagnosticsEngine &Diags = Context.getDiags();
816      unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
817        "cannot mangle this %select{ERROR|ERROR|pointer/reference|ERROR|"
818        "template|template pack expansion|expression|parameter pack}0 "
819        "template argument yet");
820      Diags.Report(TAL.getLocation(), DiagID)
821        << TA.getKind()
822        << TAL.getSourceRange();
823    }
824    }
825  }
826  Out << '@';
827}
828
829void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
830                                               bool IsMember) {
831  // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
832  // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
833  // 'I' means __restrict (32/64-bit).
834  // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
835  // keyword!
836  // <base-cvr-qualifiers> ::= A  # near
837  //                       ::= B  # near const
838  //                       ::= C  # near volatile
839  //                       ::= D  # near const volatile
840  //                       ::= E  # far (16-bit)
841  //                       ::= F  # far const (16-bit)
842  //                       ::= G  # far volatile (16-bit)
843  //                       ::= H  # far const volatile (16-bit)
844  //                       ::= I  # huge (16-bit)
845  //                       ::= J  # huge const (16-bit)
846  //                       ::= K  # huge volatile (16-bit)
847  //                       ::= L  # huge const volatile (16-bit)
848  //                       ::= M <basis> # based
849  //                       ::= N <basis> # based const
850  //                       ::= O <basis> # based volatile
851  //                       ::= P <basis> # based const volatile
852  //                       ::= Q  # near member
853  //                       ::= R  # near const member
854  //                       ::= S  # near volatile member
855  //                       ::= T  # near const volatile member
856  //                       ::= U  # far member (16-bit)
857  //                       ::= V  # far const member (16-bit)
858  //                       ::= W  # far volatile member (16-bit)
859  //                       ::= X  # far const volatile member (16-bit)
860  //                       ::= Y  # huge member (16-bit)
861  //                       ::= Z  # huge const member (16-bit)
862  //                       ::= 0  # huge volatile member (16-bit)
863  //                       ::= 1  # huge const volatile member (16-bit)
864  //                       ::= 2 <basis> # based member
865  //                       ::= 3 <basis> # based const member
866  //                       ::= 4 <basis> # based volatile member
867  //                       ::= 5 <basis> # based const volatile member
868  //                       ::= 6  # near function (pointers only)
869  //                       ::= 7  # far function (pointers only)
870  //                       ::= 8  # near method (pointers only)
871  //                       ::= 9  # far method (pointers only)
872  //                       ::= _A <basis> # based function (pointers only)
873  //                       ::= _B <basis> # based function (far?) (pointers only)
874  //                       ::= _C <basis> # based method (pointers only)
875  //                       ::= _D <basis> # based method (far?) (pointers only)
876  //                       ::= _E # block (Clang)
877  // <basis> ::= 0 # __based(void)
878  //         ::= 1 # __based(segment)?
879  //         ::= 2 <name> # __based(name)
880  //         ::= 3 # ?
881  //         ::= 4 # ?
882  //         ::= 5 # not really based
883  bool HasConst = Quals.hasConst(),
884       HasVolatile = Quals.hasVolatile();
885  if (!IsMember) {
886    if (HasConst && HasVolatile) {
887      Out << 'D';
888    } else if (HasVolatile) {
889      Out << 'C';
890    } else if (HasConst) {
891      Out << 'B';
892    } else {
893      Out << 'A';
894    }
895  } else {
896    if (HasConst && HasVolatile) {
897      Out << 'T';
898    } else if (HasVolatile) {
899      Out << 'S';
900    } else if (HasConst) {
901      Out << 'R';
902    } else {
903      Out << 'Q';
904    }
905  }
906
907  // FIXME: For now, just drop all extension qualifiers on the floor.
908}
909
910void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
911  // <pointer-cvr-qualifiers> ::= P  # no qualifiers
912  //                          ::= Q  # const
913  //                          ::= R  # volatile
914  //                          ::= S  # const volatile
915  bool HasConst = Quals.hasConst(),
916       HasVolatile = Quals.hasVolatile();
917  if (HasConst && HasVolatile) {
918    Out << 'S';
919  } else if (HasVolatile) {
920    Out << 'R';
921  } else if (HasConst) {
922    Out << 'Q';
923  } else {
924    Out << 'P';
925  }
926}
927
928void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
929                                                 SourceRange Range) {
930  void *TypePtr = getASTContext().getCanonicalType(T).getAsOpaquePtr();
931  ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
932
933  if (Found == TypeBackReferences.end()) {
934    size_t OutSizeBefore = Out.GetNumBytesInBuffer();
935
936    mangleType(T, Range, false);
937
938    // See if it's worth creating a back reference.
939    // Only types longer than 1 character are considered
940    // and only 10 back references slots are available:
941    bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
942    if (LongerThanOneChar && TypeBackReferences.size() < 10) {
943      size_t Size = TypeBackReferences.size();
944      TypeBackReferences[TypePtr] = Size;
945    }
946  } else {
947    Out << Found->second;
948  }
949}
950
951void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
952                                         bool MangleQualifiers) {
953  // Only operate on the canonical type!
954  T = getASTContext().getCanonicalType(T);
955
956  Qualifiers Quals = T.getLocalQualifiers();
957  // We have to mangle these now, while we still have enough information.
958  if (T->isAnyPointerType() || T->isMemberPointerType() ||
959      T->isBlockPointerType()) {
960    manglePointerQualifiers(Quals);
961  } else if (Quals && MangleQualifiers) {
962    mangleQualifiers(Quals, false);
963  }
964
965  SplitQualType split = T.split();
966  const Type *ty = split.Ty;
967
968  // If we're mangling a qualified array type, push the qualifiers to
969  // the element type.
970  if (split.Quals && isa<ArrayType>(T)) {
971    ty = Context.getASTContext().getAsArrayType(T);
972  }
973
974  switch (ty->getTypeClass()) {
975#define ABSTRACT_TYPE(CLASS, PARENT)
976#define NON_CANONICAL_TYPE(CLASS, PARENT) \
977  case Type::CLASS: \
978    llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
979    return;
980#define TYPE(CLASS, PARENT) \
981  case Type::CLASS: \
982    mangleType(cast<CLASS##Type>(ty), Range); \
983    break;
984#include "clang/AST/TypeNodes.def"
985#undef ABSTRACT_TYPE
986#undef NON_CANONICAL_TYPE
987#undef TYPE
988  }
989}
990
991void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
992                                         SourceRange Range) {
993  //  <type>         ::= <builtin-type>
994  //  <builtin-type> ::= X  # void
995  //                 ::= C  # signed char
996  //                 ::= D  # char
997  //                 ::= E  # unsigned char
998  //                 ::= F  # short
999  //                 ::= G  # unsigned short (or wchar_t if it's not a builtin)
1000  //                 ::= H  # int
1001  //                 ::= I  # unsigned int
1002  //                 ::= J  # long
1003  //                 ::= K  # unsigned long
1004  //                     L  # <none>
1005  //                 ::= M  # float
1006  //                 ::= N  # double
1007  //                 ::= O  # long double (__float80 is mangled differently)
1008  //                 ::= _J # long long, __int64
1009  //                 ::= _K # unsigned long long, __int64
1010  //                 ::= _L # __int128
1011  //                 ::= _M # unsigned __int128
1012  //                 ::= _N # bool
1013  //                     _O # <array in parameter>
1014  //                 ::= _T # __float80 (Intel)
1015  //                 ::= _W # wchar_t
1016  //                 ::= _Z # __float80 (Digital Mars)
1017  switch (T->getKind()) {
1018  case BuiltinType::Void: Out << 'X'; break;
1019  case BuiltinType::SChar: Out << 'C'; break;
1020  case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1021  case BuiltinType::UChar: Out << 'E'; break;
1022  case BuiltinType::Short: Out << 'F'; break;
1023  case BuiltinType::UShort: Out << 'G'; break;
1024  case BuiltinType::Int: Out << 'H'; break;
1025  case BuiltinType::UInt: Out << 'I'; break;
1026  case BuiltinType::Long: Out << 'J'; break;
1027  case BuiltinType::ULong: Out << 'K'; break;
1028  case BuiltinType::Float: Out << 'M'; break;
1029  case BuiltinType::Double: Out << 'N'; break;
1030  // TODO: Determine size and mangle accordingly
1031  case BuiltinType::LongDouble: Out << 'O'; break;
1032  case BuiltinType::LongLong: Out << "_J"; break;
1033  case BuiltinType::ULongLong: Out << "_K"; break;
1034  case BuiltinType::Int128: Out << "_L"; break;
1035  case BuiltinType::UInt128: Out << "_M"; break;
1036  case BuiltinType::Bool: Out << "_N"; break;
1037  case BuiltinType::WChar_S:
1038  case BuiltinType::WChar_U: Out << "_W"; break;
1039
1040#define BUILTIN_TYPE(Id, SingletonId)
1041#define PLACEHOLDER_TYPE(Id, SingletonId) \
1042  case BuiltinType::Id:
1043#include "clang/AST/BuiltinTypes.def"
1044  case BuiltinType::Dependent:
1045    llvm_unreachable("placeholder types shouldn't get to name mangling");
1046
1047  case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1048  case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1049  case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
1050
1051  case BuiltinType::NullPtr: Out << "$$T"; break;
1052
1053  case BuiltinType::Char16:
1054  case BuiltinType::Char32:
1055  case BuiltinType::Half: {
1056    DiagnosticsEngine &Diags = Context.getDiags();
1057    unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1058      "cannot mangle this built-in %0 type yet");
1059    Diags.Report(Range.getBegin(), DiagID)
1060      << T->getName(Context.getASTContext().getPrintingPolicy())
1061      << Range;
1062    break;
1063  }
1064  }
1065}
1066
1067// <type>          ::= <function-type>
1068void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1069                                         SourceRange) {
1070  // Structors only appear in decls, so at this point we know it's not a
1071  // structor type.
1072  // FIXME: This may not be lambda-friendly.
1073  Out << "$$A6";
1074  mangleType(T, NULL, false, false);
1075}
1076void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1077                                         SourceRange) {
1078  llvm_unreachable("Can't mangle K&R function prototypes");
1079}
1080
1081void MicrosoftCXXNameMangler::mangleType(const FunctionType *T,
1082                                         const FunctionDecl *D,
1083                                         bool IsStructor,
1084                                         bool IsInstMethod) {
1085  // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1086  //                     <return-type> <argument-list> <throw-spec>
1087  const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1088
1089  // If this is a C++ instance method, mangle the CVR qualifiers for the
1090  // this pointer.
1091  if (IsInstMethod)
1092    mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
1093
1094  mangleCallingConvention(T, IsInstMethod);
1095
1096  // <return-type> ::= <type>
1097  //               ::= @ # structors (they have no declared return type)
1098  if (IsStructor)
1099    Out << '@';
1100  else {
1101    QualType Result = Proto->getResultType();
1102    const Type* RT = Result.getTypePtr();
1103    if (!RT->isAnyPointerType() && !RT->isReferenceType()) {
1104      if (Result.hasQualifiers() || !RT->isBuiltinType())
1105        Out << '?';
1106      if (!RT->isBuiltinType() && !Result.hasQualifiers()) {
1107        // Lack of qualifiers for user types is mangled as 'A'.
1108        Out << 'A';
1109      }
1110    }
1111
1112    // FIXME: Get the source range for the result type. Or, better yet,
1113    // implement the unimplemented stuff so we don't need accurate source
1114    // location info anymore :).
1115    mangleType(Result, SourceRange());
1116  }
1117
1118  // <argument-list> ::= X # void
1119  //                 ::= <type>+ @
1120  //                 ::= <type>* Z # varargs
1121  if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1122    Out << 'X';
1123  } else {
1124    if (D) {
1125      // If we got a decl, use the type-as-written to make sure arrays
1126      // get mangled right.  Note that we can't rely on the TSI
1127      // existing if (for example) the parameter was synthesized.
1128      for (FunctionDecl::param_const_iterator Parm = D->param_begin(),
1129             ParmEnd = D->param_end(); Parm != ParmEnd; ++Parm) {
1130        TypeSourceInfo *TSI = (*Parm)->getTypeSourceInfo();
1131        QualType Type = TSI ? TSI->getType() : (*Parm)->getType();
1132        mangleArgumentType(Type, (*Parm)->getSourceRange());
1133      }
1134    } else {
1135      // Happens for function pointer type arguments for example.
1136      for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1137           ArgEnd = Proto->arg_type_end();
1138           Arg != ArgEnd; ++Arg)
1139        mangleArgumentType(*Arg, SourceRange());
1140    }
1141    // <builtin-type>      ::= Z  # ellipsis
1142    if (Proto->isVariadic())
1143      Out << 'Z';
1144    else
1145      Out << '@';
1146  }
1147
1148  mangleThrowSpecification(Proto);
1149}
1150
1151void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
1152  // <function-class> ::= A # private: near
1153  //                  ::= B # private: far
1154  //                  ::= C # private: static near
1155  //                  ::= D # private: static far
1156  //                  ::= E # private: virtual near
1157  //                  ::= F # private: virtual far
1158  //                  ::= G # private: thunk near
1159  //                  ::= H # private: thunk far
1160  //                  ::= I # protected: near
1161  //                  ::= J # protected: far
1162  //                  ::= K # protected: static near
1163  //                  ::= L # protected: static far
1164  //                  ::= M # protected: virtual near
1165  //                  ::= N # protected: virtual far
1166  //                  ::= O # protected: thunk near
1167  //                  ::= P # protected: thunk far
1168  //                  ::= Q # public: near
1169  //                  ::= R # public: far
1170  //                  ::= S # public: static near
1171  //                  ::= T # public: static far
1172  //                  ::= U # public: virtual near
1173  //                  ::= V # public: virtual far
1174  //                  ::= W # public: thunk near
1175  //                  ::= X # public: thunk far
1176  //                  ::= Y # global near
1177  //                  ::= Z # global far
1178  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1179    switch (MD->getAccess()) {
1180      default:
1181      case AS_private:
1182        if (MD->isStatic())
1183          Out << 'C';
1184        else if (MD->isVirtual())
1185          Out << 'E';
1186        else
1187          Out << 'A';
1188        break;
1189      case AS_protected:
1190        if (MD->isStatic())
1191          Out << 'K';
1192        else if (MD->isVirtual())
1193          Out << 'M';
1194        else
1195          Out << 'I';
1196        break;
1197      case AS_public:
1198        if (MD->isStatic())
1199          Out << 'S';
1200        else if (MD->isVirtual())
1201          Out << 'U';
1202        else
1203          Out << 'Q';
1204    }
1205  } else
1206    Out << 'Y';
1207}
1208void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
1209                                                      bool IsInstMethod) {
1210  // <calling-convention> ::= A # __cdecl
1211  //                      ::= B # __export __cdecl
1212  //                      ::= C # __pascal
1213  //                      ::= D # __export __pascal
1214  //                      ::= E # __thiscall
1215  //                      ::= F # __export __thiscall
1216  //                      ::= G # __stdcall
1217  //                      ::= H # __export __stdcall
1218  //                      ::= I # __fastcall
1219  //                      ::= J # __export __fastcall
1220  // The 'export' calling conventions are from a bygone era
1221  // (*cough*Win16*cough*) when functions were declared for export with
1222  // that keyword. (It didn't actually export them, it just made them so
1223  // that they could be in a DLL and somebody from another module could call
1224  // them.)
1225  CallingConv CC = T->getCallConv();
1226  if (CC == CC_Default) {
1227    if (IsInstMethod) {
1228      const FunctionProtoType *FPT =
1229        T->getCanonicalTypeUnqualified().castAs<FunctionProtoType>();
1230      bool isVariadic = FPT->isVariadic();
1231      CC = getASTContext().getDefaultCXXMethodCallConv(isVariadic);
1232    } else {
1233      CC = CC_C;
1234    }
1235  }
1236  switch (CC) {
1237    default:
1238      llvm_unreachable("Unsupported CC for mangling");
1239    case CC_Default:
1240    case CC_C: Out << 'A'; break;
1241    case CC_X86Pascal: Out << 'C'; break;
1242    case CC_X86ThisCall: Out << 'E'; break;
1243    case CC_X86StdCall: Out << 'G'; break;
1244    case CC_X86FastCall: Out << 'I'; break;
1245  }
1246}
1247void MicrosoftCXXNameMangler::mangleThrowSpecification(
1248                                                const FunctionProtoType *FT) {
1249  // <throw-spec> ::= Z # throw(...) (default)
1250  //              ::= @ # throw() or __declspec/__attribute__((nothrow))
1251  //              ::= <type>+
1252  // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1253  // all actually mangled as 'Z'. (They're ignored because their associated
1254  // functionality isn't implemented, and probably never will be.)
1255  Out << 'Z';
1256}
1257
1258void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1259                                         SourceRange Range) {
1260  // Probably should be mangled as a template instantiation; need to see what
1261  // VC does first.
1262  DiagnosticsEngine &Diags = Context.getDiags();
1263  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1264    "cannot mangle this unresolved dependent type yet");
1265  Diags.Report(Range.getBegin(), DiagID)
1266    << Range;
1267}
1268
1269// <type>        ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1270// <union-type>  ::= T <name>
1271// <struct-type> ::= U <name>
1272// <class-type>  ::= V <name>
1273// <enum-type>   ::= W <size> <name>
1274void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
1275  mangleType(cast<TagType>(T));
1276}
1277void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
1278  mangleType(cast<TagType>(T));
1279}
1280void MicrosoftCXXNameMangler::mangleType(const TagType *T) {
1281  switch (T->getDecl()->getTagKind()) {
1282    case TTK_Union:
1283      Out << 'T';
1284      break;
1285    case TTK_Struct:
1286    case TTK_Interface:
1287      Out << 'U';
1288      break;
1289    case TTK_Class:
1290      Out << 'V';
1291      break;
1292    case TTK_Enum:
1293      Out << 'W';
1294      Out << getASTContext().getTypeSizeInChars(
1295                cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity();
1296      break;
1297  }
1298  mangleName(T->getDecl());
1299}
1300
1301// <type>       ::= <array-type>
1302// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1303//                  [Y <dimension-count> <dimension>+]
1304//                  <element-type> # as global
1305//              ::= Q <cvr-qualifiers> [Y <dimension-count> <dimension>+]
1306//                  <element-type> # as param
1307// It's supposed to be the other way around, but for some strange reason, it
1308// isn't. Today this behavior is retained for the sole purpose of backwards
1309// compatibility.
1310void MicrosoftCXXNameMangler::mangleType(const ArrayType *T, bool IsGlobal) {
1311  // This isn't a recursive mangling, so now we have to do it all in this
1312  // one call.
1313  if (IsGlobal) {
1314    manglePointerQualifiers(T->getElementType().getQualifiers());
1315  } else {
1316    Out << 'Q';
1317  }
1318  mangleExtraDimensions(T->getElementType());
1319}
1320void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1321                                         SourceRange) {
1322  mangleType(cast<ArrayType>(T), false);
1323}
1324void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1325                                         SourceRange) {
1326  mangleType(cast<ArrayType>(T), false);
1327}
1328void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1329                                         SourceRange) {
1330  mangleType(cast<ArrayType>(T), false);
1331}
1332void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1333                                         SourceRange) {
1334  mangleType(cast<ArrayType>(T), false);
1335}
1336void MicrosoftCXXNameMangler::mangleExtraDimensions(QualType ElementTy) {
1337  SmallVector<llvm::APInt, 3> Dimensions;
1338  for (;;) {
1339    if (const ConstantArrayType *CAT =
1340          getASTContext().getAsConstantArrayType(ElementTy)) {
1341      Dimensions.push_back(CAT->getSize());
1342      ElementTy = CAT->getElementType();
1343    } else if (ElementTy->isVariableArrayType()) {
1344      const VariableArrayType *VAT =
1345        getASTContext().getAsVariableArrayType(ElementTy);
1346      DiagnosticsEngine &Diags = Context.getDiags();
1347      unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1348        "cannot mangle this variable-length array yet");
1349      Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1350        << VAT->getBracketsRange();
1351      return;
1352    } else if (ElementTy->isDependentSizedArrayType()) {
1353      // The dependent expression has to be folded into a constant (TODO).
1354      const DependentSizedArrayType *DSAT =
1355        getASTContext().getAsDependentSizedArrayType(ElementTy);
1356      DiagnosticsEngine &Diags = Context.getDiags();
1357      unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1358        "cannot mangle this dependent-length array yet");
1359      Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1360        << DSAT->getBracketsRange();
1361      return;
1362    } else if (ElementTy->isIncompleteArrayType()) continue;
1363    else break;
1364  }
1365  mangleQualifiers(ElementTy.getQualifiers(), false);
1366  // If there are any additional dimensions, mangle them now.
1367  if (Dimensions.size() > 0) {
1368    Out << 'Y';
1369    // <dimension-count> ::= <number> # number of extra dimensions
1370    mangleNumber(Dimensions.size());
1371    for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim) {
1372      mangleNumber(Dimensions[Dim].getLimitedValue());
1373    }
1374  }
1375  mangleType(ElementTy.getLocalUnqualifiedType(), SourceRange());
1376}
1377
1378// <type>                   ::= <pointer-to-member-type>
1379// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1380//                                                          <class name> <type>
1381void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1382                                         SourceRange Range) {
1383  QualType PointeeType = T->getPointeeType();
1384  if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1385    Out << '8';
1386    mangleName(T->getClass()->castAs<RecordType>()->getDecl());
1387    mangleType(FPT, NULL, false, true);
1388  } else {
1389    mangleQualifiers(PointeeType.getQualifiers(), true);
1390    mangleName(T->getClass()->castAs<RecordType>()->getDecl());
1391    mangleType(PointeeType.getLocalUnqualifiedType(), Range);
1392  }
1393}
1394
1395void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1396                                         SourceRange Range) {
1397  DiagnosticsEngine &Diags = Context.getDiags();
1398  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1399    "cannot mangle this template type parameter type yet");
1400  Diags.Report(Range.getBegin(), DiagID)
1401    << Range;
1402}
1403
1404void MicrosoftCXXNameMangler::mangleType(
1405                                       const SubstTemplateTypeParmPackType *T,
1406                                       SourceRange Range) {
1407  DiagnosticsEngine &Diags = Context.getDiags();
1408  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1409    "cannot mangle this substituted parameter pack yet");
1410  Diags.Report(Range.getBegin(), DiagID)
1411    << Range;
1412}
1413
1414// <type> ::= <pointer-type>
1415// <pointer-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1416void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1417                                         SourceRange Range) {
1418  QualType PointeeTy = T->getPointeeType();
1419  if (PointeeTy->isArrayType()) {
1420    // Pointers to arrays are mangled like arrays.
1421    mangleExtraDimensions(PointeeTy);
1422  } else if (const FunctionType *FT = PointeeTy->getAs<FunctionType>()) {
1423    // Function pointers are special.
1424    Out << '6';
1425    mangleType(FT, NULL, false, false);
1426  } else {
1427    mangleQualifiers(PointeeTy.getQualifiers(), false);
1428    mangleType(PointeeTy, Range, false);
1429  }
1430}
1431void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1432                                         SourceRange Range) {
1433  // Object pointers never have qualifiers.
1434  Out << 'A';
1435  mangleType(T->getPointeeType(), Range);
1436}
1437
1438// <type> ::= <reference-type>
1439// <reference-type> ::= A <cvr-qualifiers> <type>
1440void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1441                                         SourceRange Range) {
1442  Out << 'A';
1443  QualType PointeeTy = T->getPointeeType();
1444  if (!PointeeTy.hasQualifiers())
1445    // Lack of qualifiers is mangled as 'A'.
1446    Out << 'A';
1447  mangleType(PointeeTy, Range);
1448}
1449
1450// <type> ::= <r-value-reference-type>
1451// <r-value-reference-type> ::= $$Q <cvr-qualifiers> <type>
1452void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1453                                         SourceRange Range) {
1454  Out << "$$Q";
1455  QualType PointeeTy = T->getPointeeType();
1456  if (!PointeeTy.hasQualifiers())
1457    // Lack of qualifiers is mangled as 'A'.
1458    Out << 'A';
1459  mangleType(PointeeTy, Range);
1460}
1461
1462void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1463                                         SourceRange Range) {
1464  DiagnosticsEngine &Diags = Context.getDiags();
1465  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1466    "cannot mangle this complex number type yet");
1467  Diags.Report(Range.getBegin(), DiagID)
1468    << Range;
1469}
1470
1471void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1472                                         SourceRange Range) {
1473  DiagnosticsEngine &Diags = Context.getDiags();
1474  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1475    "cannot mangle this vector type yet");
1476  Diags.Report(Range.getBegin(), DiagID)
1477    << Range;
1478}
1479void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1480                                         SourceRange Range) {
1481  DiagnosticsEngine &Diags = Context.getDiags();
1482  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1483    "cannot mangle this extended vector type yet");
1484  Diags.Report(Range.getBegin(), DiagID)
1485    << Range;
1486}
1487void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1488                                         SourceRange Range) {
1489  DiagnosticsEngine &Diags = Context.getDiags();
1490  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1491    "cannot mangle this dependent-sized extended vector type yet");
1492  Diags.Report(Range.getBegin(), DiagID)
1493    << Range;
1494}
1495
1496void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1497                                         SourceRange) {
1498  // ObjC interfaces have structs underlying them.
1499  Out << 'U';
1500  mangleName(T->getDecl());
1501}
1502
1503void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1504                                         SourceRange Range) {
1505  // We don't allow overloading by different protocol qualification,
1506  // so mangling them isn't necessary.
1507  mangleType(T->getBaseType(), Range);
1508}
1509
1510void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1511                                         SourceRange Range) {
1512  Out << "_E";
1513
1514  QualType pointee = T->getPointeeType();
1515  mangleType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
1516}
1517
1518void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T,
1519                                         SourceRange Range) {
1520  DiagnosticsEngine &Diags = Context.getDiags();
1521  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1522    "cannot mangle this injected class name type yet");
1523  Diags.Report(Range.getBegin(), DiagID)
1524    << Range;
1525}
1526
1527void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1528                                         SourceRange Range) {
1529  DiagnosticsEngine &Diags = Context.getDiags();
1530  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1531    "cannot mangle this template specialization type yet");
1532  Diags.Report(Range.getBegin(), DiagID)
1533    << Range;
1534}
1535
1536void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1537                                         SourceRange Range) {
1538  DiagnosticsEngine &Diags = Context.getDiags();
1539  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1540    "cannot mangle this dependent name type yet");
1541  Diags.Report(Range.getBegin(), DiagID)
1542    << Range;
1543}
1544
1545void MicrosoftCXXNameMangler::mangleType(
1546                                 const DependentTemplateSpecializationType *T,
1547                                 SourceRange Range) {
1548  DiagnosticsEngine &Diags = Context.getDiags();
1549  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1550    "cannot mangle this dependent template specialization type yet");
1551  Diags.Report(Range.getBegin(), DiagID)
1552    << Range;
1553}
1554
1555void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1556                                         SourceRange Range) {
1557  DiagnosticsEngine &Diags = Context.getDiags();
1558  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1559    "cannot mangle this pack expansion yet");
1560  Diags.Report(Range.getBegin(), DiagID)
1561    << Range;
1562}
1563
1564void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1565                                         SourceRange Range) {
1566  DiagnosticsEngine &Diags = Context.getDiags();
1567  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1568    "cannot mangle this typeof(type) yet");
1569  Diags.Report(Range.getBegin(), DiagID)
1570    << Range;
1571}
1572
1573void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1574                                         SourceRange Range) {
1575  DiagnosticsEngine &Diags = Context.getDiags();
1576  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1577    "cannot mangle this typeof(expression) yet");
1578  Diags.Report(Range.getBegin(), DiagID)
1579    << Range;
1580}
1581
1582void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1583                                         SourceRange Range) {
1584  DiagnosticsEngine &Diags = Context.getDiags();
1585  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1586    "cannot mangle this decltype() yet");
1587  Diags.Report(Range.getBegin(), DiagID)
1588    << Range;
1589}
1590
1591void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1592                                         SourceRange Range) {
1593  DiagnosticsEngine &Diags = Context.getDiags();
1594  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1595    "cannot mangle this unary transform type yet");
1596  Diags.Report(Range.getBegin(), DiagID)
1597    << Range;
1598}
1599
1600void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1601  DiagnosticsEngine &Diags = Context.getDiags();
1602  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1603    "cannot mangle this 'auto' type yet");
1604  Diags.Report(Range.getBegin(), DiagID)
1605    << Range;
1606}
1607
1608void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1609                                         SourceRange Range) {
1610  DiagnosticsEngine &Diags = Context.getDiags();
1611  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1612    "cannot mangle this C11 atomic type yet");
1613  Diags.Report(Range.getBegin(), DiagID)
1614    << Range;
1615}
1616
1617void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1618                                        raw_ostream &Out) {
1619  assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1620         "Invalid mangleName() call, argument is not a variable or function!");
1621  assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1622         "Invalid mangleName() call on 'structor decl!");
1623
1624  PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1625                                 getASTContext().getSourceManager(),
1626                                 "Mangling declaration");
1627
1628  MicrosoftCXXNameMangler Mangler(*this, Out);
1629  return Mangler.mangle(D);
1630}
1631void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1632                                         const ThunkInfo &Thunk,
1633                                         raw_ostream &) {
1634  unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1635    "cannot mangle thunk for this method yet");
1636  getDiags().Report(MD->getLocation(), DiagID);
1637}
1638void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1639                                                CXXDtorType Type,
1640                                                const ThisAdjustment &,
1641                                                raw_ostream &) {
1642  unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1643    "cannot mangle thunk for this destructor yet");
1644  getDiags().Report(DD->getLocation(), DiagID);
1645}
1646void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1647                                             raw_ostream &Out) {
1648  // <mangled-name> ::= ? <operator-name> <class-name> <storage-class>
1649  //                      <cvr-qualifiers> [<name>] @
1650  // <operator-name> ::= _7 # vftable
1651  //                 ::= _8 # vbtable
1652  // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1653  // is always '6' for vftables and '7' for vbtables. (The difference is
1654  // beyond me.)
1655  // TODO: vbtables.
1656  MicrosoftCXXNameMangler Mangler(*this, Out);
1657  Mangler.getStream() << "\01??_7";
1658  Mangler.mangleName(RD);
1659  Mangler.getStream() << "6B";
1660  // TODO: If the class has more than one vtable, mangle in the class it came
1661  // from.
1662  Mangler.getStream() << '@';
1663}
1664void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1665                                          raw_ostream &) {
1666  llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1667}
1668void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1669                                                 int64_t Offset,
1670                                                 const CXXRecordDecl *Type,
1671                                                 raw_ostream &) {
1672  llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1673}
1674void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1675                                           raw_ostream &) {
1676  // FIXME: Give a location...
1677  unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1678    "cannot mangle RTTI descriptors for type %0 yet");
1679  getDiags().Report(DiagID)
1680    << T.getBaseTypeIdentifier();
1681}
1682void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1683                                               raw_ostream &) {
1684  // FIXME: Give a location...
1685  unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1686    "cannot mangle the name of type %0 into RTTI descriptors yet");
1687  getDiags().Report(DiagID)
1688    << T.getBaseTypeIdentifier();
1689}
1690void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1691                                           CXXCtorType Type,
1692                                           raw_ostream & Out) {
1693  MicrosoftCXXNameMangler mangler(*this, Out);
1694  mangler.mangle(D);
1695}
1696void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1697                                           CXXDtorType Type,
1698                                           raw_ostream & Out) {
1699  MicrosoftCXXNameMangler mangler(*this, Out);
1700  mangler.mangle(D);
1701}
1702void MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *VD,
1703                                                      raw_ostream &) {
1704  unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1705    "cannot mangle this reference temporary yet");
1706  getDiags().Report(VD->getLocation(), DiagID);
1707}
1708
1709MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
1710                                                   DiagnosticsEngine &Diags) {
1711  return new MicrosoftMangleContext(Context, Diags);
1712}
1713