blob: cd0063c4af9bfdd22b7785805010d88d768c7216 [file] [log] [blame]
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001//===--- 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/Attr.h"
17#include "clang/AST/CharUnits.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclCXX.h"
20#include "clang/AST/DeclObjC.h"
21#include "clang/AST/DeclTemplate.h"
22#include "clang/AST/ExprCXX.h"
23#include "clang/Basic/ABI.h"
24#include "clang/Basic/DiagnosticOptions.h"
Reid Klecknerd6a08d12013-05-14 20:30:42 +000025#include "clang/Basic/TargetInfo.h"
Reid Klecknerf0219cd2013-05-22 17:16:39 +000026#include "llvm/ADT/StringMap.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000027
28using namespace clang;
29
30namespace {
31
David Majnemercab7dad2013-09-13 09:03:14 +000032/// \brief Retrieve the declaration context that should be used when mangling
33/// the given declaration.
34static const DeclContext *getEffectiveDeclContext(const Decl *D) {
35 // The ABI assumes that lambda closure types that occur within
36 // default arguments live in the context of the function. However, due to
37 // the way in which Clang parses and creates function declarations, this is
38 // not the case: the lambda closure type ends up living in the context
39 // where the function itself resides, because the function declaration itself
40 // had not yet been created. Fix the context here.
41 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
42 if (RD->isLambda())
43 if (ParmVarDecl *ContextParam =
44 dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl()))
45 return ContextParam->getDeclContext();
46 }
47
48 // Perform the same check for block literals.
49 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
50 if (ParmVarDecl *ContextParam =
51 dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl()))
52 return ContextParam->getDeclContext();
53 }
54
55 const DeclContext *DC = D->getDeclContext();
56 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(DC))
57 return getEffectiveDeclContext(CD);
58
59 return DC;
60}
61
62static const DeclContext *getEffectiveParentContext(const DeclContext *DC) {
63 return getEffectiveDeclContext(cast<Decl>(DC));
64}
65
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000066static const FunctionDecl *getStructor(const FunctionDecl *fn) {
67 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
68 return ftd->getTemplatedDecl();
69
70 return fn;
71}
72
Guy Benyei7f92f2d2012-12-18 14:30:41 +000073/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
74/// Microsoft Visual C++ ABI.
75class MicrosoftCXXNameMangler {
76 MangleContext &Context;
77 raw_ostream &Out;
78
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000079 /// The "structor" is the top-level declaration being mangled, if
80 /// that's not a template specialization; otherwise it's the pattern
81 /// for that specialization.
82 const NamedDecl *Structor;
83 unsigned StructorType;
84
Reid Klecknerf0219cd2013-05-22 17:16:39 +000085 typedef llvm::StringMap<unsigned> BackRefMap;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000086 BackRefMap NameBackReferences;
87 bool UseNameBackReferences;
88
89 typedef llvm::DenseMap<void*, unsigned> ArgBackRefMap;
90 ArgBackRefMap TypeBackReferences;
91
92 ASTContext &getASTContext() const { return Context.getASTContext(); }
93
Reid Klecknerd6a08d12013-05-14 20:30:42 +000094 // FIXME: If we add support for __ptr32/64 qualifiers, then we should push
95 // this check into mangleQualifiers().
96 const bool PointersAre64Bit;
97
Guy Benyei7f92f2d2012-12-18 14:30:41 +000098public:
Peter Collingbourneb70d1c32013-04-25 04:25:40 +000099 enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result };
100
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000101 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000102 : Context(C), Out(Out_),
103 Structor(0), StructorType(-1),
David Blaikiee7e94c92013-05-14 21:31:46 +0000104 UseNameBackReferences(true),
Reid Klecknerd6a08d12013-05-14 20:30:42 +0000105 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikiee7e94c92013-05-14 21:31:46 +0000106 64) { }
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000107
108 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_,
109 const CXXDestructorDecl *D, CXXDtorType Type)
110 : Context(C), Out(Out_),
111 Structor(getStructor(D)), StructorType(Type),
David Blaikiee7e94c92013-05-14 21:31:46 +0000112 UseNameBackReferences(true),
Reid Klecknerd6a08d12013-05-14 20:30:42 +0000113 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikiee7e94c92013-05-14 21:31:46 +0000114 64) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000115
116 raw_ostream &getStream() const { return Out; }
117
118 void mangle(const NamedDecl *D, StringRef Prefix = "\01?");
119 void mangleName(const NamedDecl *ND);
David Majnemerc80eb462013-08-13 06:32:20 +0000120 void mangleDeclaration(const NamedDecl *ND);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000121 void mangleFunctionEncoding(const FunctionDecl *FD);
122 void mangleVariableEncoding(const VarDecl *VD);
123 void mangleNumber(int64_t Number);
124 void mangleNumber(const llvm::APSInt &Value);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000125 void mangleType(QualType T, SourceRange Range,
126 QualifierMangleMode QMM = QMM_Mangle);
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +0000127 void mangleFunctionType(const FunctionType *T, const FunctionDecl *D = 0,
128 bool ForceInstMethod = false);
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000129 void manglePostfix(const DeclContext *DC, bool NoFunction = false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000130
131private:
132 void disableBackReferences() { UseNameBackReferences = false; }
133 void mangleUnqualifiedName(const NamedDecl *ND) {
134 mangleUnqualifiedName(ND, ND->getDeclName());
135 }
136 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
137 void mangleSourceName(const IdentifierInfo *II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000138 void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc);
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000139 void mangleCXXDtorType(CXXDtorType T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000140 void mangleQualifiers(Qualifiers Quals, bool IsMember);
141 void manglePointerQualifiers(Qualifiers Quals);
142
143 void mangleUnscopedTemplateName(const TemplateDecl *ND);
144 void mangleTemplateInstantiationName(const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000145 const TemplateArgumentList &TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000146 void mangleObjCMethodName(const ObjCMethodDecl *MD);
147 void mangleLocalName(const FunctionDecl *FD);
148
149 void mangleArgumentType(QualType T, SourceRange Range);
150
151 // Declare manglers for every type class.
152#define ABSTRACT_TYPE(CLASS, PARENT)
153#define NON_CANONICAL_TYPE(CLASS, PARENT)
154#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \
155 SourceRange Range);
156#include "clang/AST/TypeNodes.def"
157#undef ABSTRACT_TYPE
158#undef NON_CANONICAL_TYPE
159#undef TYPE
160
David Majnemer02c44f02013-08-05 22:26:46 +0000161 void mangleType(const TagDecl *TD);
David Majnemer58e4cd02013-09-11 04:44:30 +0000162 void mangleDecayedArrayType(const ArrayType *T);
Reid Klecknerf21818d2013-06-24 19:21:52 +0000163 void mangleArrayType(const ArrayType *T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000164 void mangleFunctionClass(const FunctionDecl *FD);
Reid Klecknere3e686f2013-09-25 22:28:52 +0000165 void mangleCallingConvention(const FunctionType *T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000166 void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean);
167 void mangleExpression(const Expr *E);
168 void mangleThrowSpecification(const FunctionProtoType *T);
169
Reid Klecknerf16216c2013-03-20 01:40:23 +0000170 void mangleTemplateArgs(const TemplateDecl *TD,
171 const TemplateArgumentList &TemplateArgs);
David Majnemer309f6452013-08-27 08:21:25 +0000172 void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000173};
174
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +0000175/// MicrosoftMangleContextImpl - Overrides the default MangleContext for the
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000176/// Microsoft Visual C++ ABI.
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +0000177class MicrosoftMangleContextImpl : public MicrosoftMangleContext {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000178public:
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +0000179 MicrosoftMangleContextImpl(ASTContext &Context, DiagnosticsEngine &Diags)
180 : MicrosoftMangleContext(Context, Diags) {}
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000181 virtual bool shouldMangleDeclName(const NamedDecl *D);
182 virtual void mangleName(const NamedDecl *D, raw_ostream &Out);
183 virtual void mangleThunk(const CXXMethodDecl *MD,
184 const ThunkInfo &Thunk,
185 raw_ostream &);
186 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
187 const ThisAdjustment &ThisAdjustment,
188 raw_ostream &);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000189 virtual void mangleCXXVFTable(const CXXRecordDecl *Derived,
190 ArrayRef<const CXXRecordDecl *> BasePath,
191 raw_ostream &Out);
Reid Kleckner90633022013-06-19 15:20:38 +0000192 virtual void mangleCXXVBTable(const CXXRecordDecl *Derived,
193 ArrayRef<const CXXRecordDecl *> BasePath,
194 raw_ostream &Out);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000195 virtual void mangleCXXRTTI(QualType T, raw_ostream &);
196 virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
197 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
198 raw_ostream &);
199 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
200 raw_ostream &);
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000201 virtual void mangleReferenceTemporary(const VarDecl *, raw_ostream &);
202 virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out);
Reid Klecknerc5c6fa72013-09-10 20:43:12 +0000203 virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out);
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000204 virtual void mangleDynamicAtExitDestructor(const VarDecl *D,
205 raw_ostream &Out);
Reid Klecknerc5c6fa72013-09-10 20:43:12 +0000206
207private:
208 void mangleInitFiniStub(const VarDecl *D, raw_ostream &Out, char CharCode);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000209};
210
211}
212
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +0000213bool MicrosoftMangleContextImpl::shouldMangleDeclName(const NamedDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000214 // In C, functions with no attributes never need to be mangled. Fastpath them.
215 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
216 return false;
217
218 // Any decl can be declared with __asm("foo") on it, and this takes precedence
219 // over all other naming in the .o file.
220 if (D->hasAttr<AsmLabelAttr>())
221 return true;
222
David Majnemercab7dad2013-09-13 09:03:14 +0000223 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
224 LanguageLinkage L = FD->getLanguageLinkage();
225 // Overloadable functions need mangling.
226 if (FD->hasAttr<OverloadableAttr>())
227 return true;
228
David Majnemere9f6f332013-09-16 22:44:20 +0000229 // The ABI expects that we would never mangle "typical" user-defined entry
230 // points regardless of visibility or freestanding-ness.
231 //
232 // N.B. This is distinct from asking about "main". "main" has a lot of
233 // special rules associated with it in the standard while these
234 // user-defined entry points are outside of the purview of the standard.
235 // For example, there can be only one definition for "main" in a standards
236 // compliant program; however nothing forbids the existence of wmain and
237 // WinMain in the same translation unit.
238 if (FD->isMSVCRTEntryPoint())
David Majnemercab7dad2013-09-13 09:03:14 +0000239 return false;
240
241 // C++ functions and those whose names are not a simple identifier need
242 // mangling.
243 if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage)
244 return true;
245
246 // C functions are not mangled.
247 if (L == CLanguageLinkage)
248 return false;
249 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000250
251 // Otherwise, no mangling is done outside C++ mode.
252 if (!getASTContext().getLangOpts().CPlusPlus)
253 return false;
254
David Majnemercab7dad2013-09-13 09:03:14 +0000255 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
256 // C variables are not mangled.
257 if (VD->isExternC())
258 return false;
259
260 // Variables at global scope with non-internal linkage are not mangled.
261 const DeclContext *DC = getEffectiveDeclContext(D);
262 // Check for extern variable declared locally.
263 if (DC->isFunctionOrMethod() && D->hasLinkage())
264 while (!DC->isNamespace() && !DC->isTranslationUnit())
265 DC = getEffectiveParentContext(DC);
266
267 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage &&
268 !isa<VarTemplateSpecializationDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000269 return false;
270 }
271
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000272 return true;
273}
274
275void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
276 StringRef Prefix) {
277 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
278 // Therefore it's really important that we don't decorate the
279 // name with leading underscores or leading/trailing at signs. So, by
280 // default, we emit an asm marker at the start so we get the name right.
281 // Callers can override this with a custom prefix.
282
283 // Any decl can be declared with __asm("foo") on it, and this takes precedence
284 // over all other naming in the .o file.
285 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
286 // If we have an asm name, then we use it as the mangling.
287 Out << '\01' << ALA->getLabel();
288 return;
289 }
290
291 // <mangled-name> ::= ? <name> <type-encoding>
292 Out << Prefix;
293 mangleName(D);
294 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
295 mangleFunctionEncoding(FD);
296 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
297 mangleVariableEncoding(VD);
298 else {
299 // TODO: Fields? Can MSVC even mangle them?
300 // Issue a diagnostic for now.
301 DiagnosticsEngine &Diags = Context.getDiags();
302 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
303 "cannot mangle this declaration yet");
304 Diags.Report(D->getLocation(), DiagID)
305 << D->getSourceRange();
306 }
307}
308
309void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
310 // <type-encoding> ::= <function-class> <function-type>
311
Reid Klecknerf21818d2013-06-24 19:21:52 +0000312 // Since MSVC operates on the type as written and not the canonical type, it
313 // actually matters which decl we have here. MSVC appears to choose the
314 // first, since it is most likely to be the declaration in a header file.
315 FD = FD->getFirstDeclaration();
316
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000317 // We should never ever see a FunctionNoProtoType at this point.
318 // We don't even know how to mangle their types anyway :).
Reid Klecknerf21818d2013-06-24 19:21:52 +0000319 TypeSourceInfo *TSI = FD->getTypeSourceInfo();
320 QualType T = TSI ? TSI->getType() : FD->getType();
321 const FunctionProtoType *FT = T->castAs<FunctionProtoType>();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000322
David Majnemercab7dad2013-09-13 09:03:14 +0000323 // extern "C" functions can hold entities that must be mangled.
324 // As it stands, these functions still need to get expressed in the full
325 // external name. They have their class and type omitted, replaced with '9'.
326 if (Context.shouldMangleDeclName(FD)) {
327 // First, the function class.
328 mangleFunctionClass(FD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000329
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +0000330 mangleFunctionType(FT, FD);
David Majnemercab7dad2013-09-13 09:03:14 +0000331 } else
332 Out << '9';
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000333}
334
335void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
336 // <type-encoding> ::= <storage-class> <variable-type>
337 // <storage-class> ::= 0 # private static member
338 // ::= 1 # protected static member
339 // ::= 2 # public static member
340 // ::= 3 # global
341 // ::= 4 # static local
342
343 // The first character in the encoding (after the name) is the storage class.
344 if (VD->isStaticDataMember()) {
345 // If it's a static member, it also encodes the access level.
346 switch (VD->getAccess()) {
347 default:
348 case AS_private: Out << '0'; break;
349 case AS_protected: Out << '1'; break;
350 case AS_public: Out << '2'; break;
351 }
352 }
353 else if (!VD->isStaticLocal())
354 Out << '3';
355 else
356 Out << '4';
357 // Now mangle the type.
358 // <variable-type> ::= <type> <cvr-qualifiers>
359 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
360 // Pointers and references are odd. The type of 'int * const foo;' gets
361 // mangled as 'QAHA' instead of 'PAHB', for example.
362 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
363 QualType Ty = TL.getType();
David Majnemer1c7a4092013-08-15 08:13:23 +0000364 if (Ty->isPointerType() || Ty->isReferenceType() ||
365 Ty->isMemberPointerType()) {
David Majnemer17ffbd02013-08-09 05:56:24 +0000366 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000367 if (PointersAre64Bit)
368 Out << 'E';
369 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) {
370 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true);
371 // Member pointers are suffixed with a back reference to the member
372 // pointer's class name.
373 mangleName(MPT->getClass()->getAsCXXRecordDecl());
374 } else
375 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
David Majnemer17ffbd02013-08-09 05:56:24 +0000376 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000377 // Global arrays are funny, too.
David Majnemer58e4cd02013-09-11 04:44:30 +0000378 mangleDecayedArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000379 if (AT->getElementType()->isArrayType())
380 Out << 'A';
381 else
382 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000383 } else {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000384 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000385 mangleQualifiers(Ty.getLocalQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000386 }
387}
388
389void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
390 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
391 const DeclContext *DC = ND->getDeclContext();
392
393 // Always start with the unqualified name.
394 mangleUnqualifiedName(ND);
395
396 // If this is an extern variable declared locally, the relevant DeclContext
397 // is that of the containing namespace, or the translation unit.
398 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
399 while (!DC->isNamespace() && !DC->isTranslationUnit())
400 DC = DC->getParent();
401
402 manglePostfix(DC);
403
404 // Terminate the whole name with an '@'.
405 Out << '@';
406}
407
408void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
409 llvm::APSInt APSNumber(/*BitWidth=*/64, /*isUnsigned=*/false);
410 APSNumber = Number;
411 mangleNumber(APSNumber);
412}
413
414void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
415 // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
416 // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
417 // ::= [?] @ # 0 (alternate mangling, not emitted by VC)
418 if (Value.isSigned() && Value.isNegative()) {
419 Out << '?';
420 mangleNumber(llvm::APSInt(Value.abs()));
421 return;
422 }
423 llvm::APSInt Temp(Value);
424 // There's a special shorter mangling for 0, but Microsoft
425 // chose not to use it. Instead, 0 gets mangled as "A@". Oh well...
426 if (Value.uge(1) && Value.ule(10)) {
427 --Temp;
428 Temp.print(Out, false);
429 } else {
430 // We have to build up the encoding in reverse order, so it will come
431 // out right when we write it out.
432 char Encoding[64];
433 char *EndPtr = Encoding+sizeof(Encoding);
434 char *CurPtr = EndPtr;
435 llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned());
436 NibbleMask = 0xf;
437 do {
438 *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf);
439 Temp = Temp.lshr(4);
440 } while (Temp != 0);
441 Out.write(CurPtr, EndPtr-CurPtr);
442 Out << '@';
443 }
444}
445
446static const TemplateDecl *
Reid Klecknerf16216c2013-03-20 01:40:23 +0000447isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000448 // Check if we have a function template.
449 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
450 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000451 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000452 return TD;
453 }
454 }
455
456 // Check if we have a class template.
457 if (const ClassTemplateSpecializationDecl *Spec =
Reid Klecknerf16216c2013-03-20 01:40:23 +0000458 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
459 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000460 return Spec->getSpecializedTemplate();
461 }
462
463 return 0;
464}
465
466void
467MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
468 DeclarationName Name) {
469 // <unqualified-name> ::= <operator-name>
470 // ::= <ctor-dtor-name>
471 // ::= <source-name>
472 // ::= <template-name>
Reid Klecknerf16216c2013-03-20 01:40:23 +0000473
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000474 // Check if we have a template.
Reid Klecknerf16216c2013-03-20 01:40:23 +0000475 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000476 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Kleckner3be37d12013-07-13 00:43:39 +0000477 // Function templates aren't considered for name back referencing. This
478 // makes sense since function templates aren't likely to occur multiple
479 // times in a symbol.
480 // FIXME: Test alias template mangling with MSVC 2013.
481 if (!isa<ClassTemplateDecl>(TD)) {
482 mangleTemplateInstantiationName(TD, *TemplateArgs);
483 return;
484 }
485
486 // We have a class template.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000487 // Here comes the tricky thing: if we need to mangle something like
488 // void foo(A::X<Y>, B::X<Y>),
489 // the X<Y> part is aliased. However, if you need to mangle
490 // void foo(A::X<A::Y>, A::X<B::Y>),
491 // the A::X<> part is not aliased.
492 // That said, from the mangler's perspective we have a structure like this:
493 // namespace[s] -> type[ -> template-parameters]
494 // but from the Clang perspective we have
495 // type [ -> template-parameters]
496 // \-> namespace[s]
497 // What we do is we create a new mangler, mangle the same type (without
498 // a namespace suffix) using the extra mangler with back references
499 // disabled (to avoid infinite recursion) and then use the mangled type
500 // name as a key to check the mangling of different types for aliasing.
501
502 std::string BackReferenceKey;
503 BackRefMap::iterator Found;
504 if (UseNameBackReferences) {
505 llvm::raw_string_ostream Stream(BackReferenceKey);
506 MicrosoftCXXNameMangler Extra(Context, Stream);
507 Extra.disableBackReferences();
508 Extra.mangleUnqualifiedName(ND, Name);
509 Stream.flush();
510
511 Found = NameBackReferences.find(BackReferenceKey);
512 }
513 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000514 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000515 if (UseNameBackReferences && NameBackReferences.size() < 10) {
516 size_t Size = NameBackReferences.size();
517 NameBackReferences[BackReferenceKey] = Size;
518 }
519 } else {
520 Out << Found->second;
521 }
522 return;
523 }
524
525 switch (Name.getNameKind()) {
526 case DeclarationName::Identifier: {
527 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
528 mangleSourceName(II);
529 break;
530 }
531
532 // Otherwise, an anonymous entity. We must have a declaration.
533 assert(ND && "mangling empty name without declaration");
534
535 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
536 if (NS->isAnonymousNamespace()) {
537 Out << "?A@";
538 break;
539 }
540 }
541
542 // We must have an anonymous struct.
543 const TagDecl *TD = cast<TagDecl>(ND);
544 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
545 assert(TD->getDeclContext() == D->getDeclContext() &&
546 "Typedef should not be in another decl context!");
547 assert(D->getDeclName().getAsIdentifierInfo() &&
548 "Typedef was not named!");
549 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
550 break;
551 }
552
David Majnemeraa824612013-09-17 23:57:10 +0000553 if (TD->hasDeclaratorForAnonDecl())
554 // Anonymous types with no tag or typedef get the name of their
555 // declarator mangled in.
556 Out << "<unnamed-type-" << TD->getDeclaratorForAnonDecl()->getName()
557 << ">@";
558 else
559 // Anonymous types with no tag, no typedef, or declarator get
560 // '<unnamed-tag>@'.
561 Out << "<unnamed-tag>@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000562 break;
563 }
564
565 case DeclarationName::ObjCZeroArgSelector:
566 case DeclarationName::ObjCOneArgSelector:
567 case DeclarationName::ObjCMultiArgSelector:
568 llvm_unreachable("Can't mangle Objective-C selector names here!");
569
570 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000571 if (ND == Structor) {
572 assert(StructorType == Ctor_Complete &&
573 "Should never be asked to mangle a ctor other than complete");
574 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000575 Out << "?0";
576 break;
577
578 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000579 if (ND == Structor)
580 // If the named decl is the C++ destructor we're mangling,
581 // use the type we were given.
582 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
583 else
Reid Klecknera4130ba2013-07-22 13:51:44 +0000584 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000585 // class with a destructor is declared within a destructor.
Reid Klecknera4130ba2013-07-22 13:51:44 +0000586 mangleCXXDtorType(Dtor_Base);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000587 break;
588
589 case DeclarationName::CXXConversionFunctionName:
590 // <operator-name> ::= ?B # (cast)
591 // The target type is encoded as the return type.
592 Out << "?B";
593 break;
594
595 case DeclarationName::CXXOperatorName:
596 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
597 break;
598
599 case DeclarationName::CXXLiteralOperatorName: {
600 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
601 DiagnosticsEngine Diags = Context.getDiags();
602 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
603 "cannot mangle this literal operator yet");
604 Diags.Report(ND->getLocation(), DiagID);
605 break;
606 }
607
608 case DeclarationName::CXXUsingDirective:
609 llvm_unreachable("Can't mangle a using directive name!");
610 }
611}
612
613void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
614 bool NoFunction) {
615 // <postfix> ::= <unqualified-name> [<postfix>]
616 // ::= <substitution> [<postfix>]
617
618 if (!DC) return;
619
620 while (isa<LinkageSpecDecl>(DC))
621 DC = DC->getParent();
622
623 if (DC->isTranslationUnit())
624 return;
625
626 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedmane5798892013-07-10 01:13:27 +0000627 DiagnosticsEngine Diags = Context.getDiags();
628 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
629 "cannot mangle a local inside this block yet");
630 Diags.Report(BD->getLocation(), DiagID);
631
632 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
633 // for how this should be done.
634 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000635 Out << '@';
636 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000637 } else if (isa<CapturedDecl>(DC)) {
638 // Skip CapturedDecl context.
639 manglePostfix(DC->getParent(), NoFunction);
640 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000641 }
642
643 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
644 return;
645 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
646 mangleObjCMethodName(Method);
647 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
648 mangleLocalName(Func);
649 else {
650 mangleUnqualifiedName(cast<NamedDecl>(DC));
651 manglePostfix(DC->getParent(), NoFunction);
652 }
653}
654
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000655void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000656 // Microsoft uses the names on the case labels for these dtor variants. Clang
657 // uses the Itanium terminology internally. Everything in this ABI delegates
658 // towards the base dtor.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000659 switch (T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000660 // <operator-name> ::= ?1 # destructor
661 case Dtor_Base: Out << "?1"; return;
662 // <operator-name> ::= ?_D # vbase destructor
663 case Dtor_Complete: Out << "?_D"; return;
664 // <operator-name> ::= ?_G # scalar deleting destructor
665 case Dtor_Deleting: Out << "?_G"; return;
666 // <operator-name> ::= ?_E # vector deleting destructor
667 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
668 // it.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000669 }
670 llvm_unreachable("Unsupported dtor type?");
671}
672
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000673void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
674 SourceLocation Loc) {
675 switch (OO) {
676 // ?0 # constructor
677 // ?1 # destructor
678 // <operator-name> ::= ?2 # new
679 case OO_New: Out << "?2"; break;
680 // <operator-name> ::= ?3 # delete
681 case OO_Delete: Out << "?3"; break;
682 // <operator-name> ::= ?4 # =
683 case OO_Equal: Out << "?4"; break;
684 // <operator-name> ::= ?5 # >>
685 case OO_GreaterGreater: Out << "?5"; break;
686 // <operator-name> ::= ?6 # <<
687 case OO_LessLess: Out << "?6"; break;
688 // <operator-name> ::= ?7 # !
689 case OO_Exclaim: Out << "?7"; break;
690 // <operator-name> ::= ?8 # ==
691 case OO_EqualEqual: Out << "?8"; break;
692 // <operator-name> ::= ?9 # !=
693 case OO_ExclaimEqual: Out << "?9"; break;
694 // <operator-name> ::= ?A # []
695 case OO_Subscript: Out << "?A"; break;
696 // ?B # conversion
697 // <operator-name> ::= ?C # ->
698 case OO_Arrow: Out << "?C"; break;
699 // <operator-name> ::= ?D # *
700 case OO_Star: Out << "?D"; break;
701 // <operator-name> ::= ?E # ++
702 case OO_PlusPlus: Out << "?E"; break;
703 // <operator-name> ::= ?F # --
704 case OO_MinusMinus: Out << "?F"; break;
705 // <operator-name> ::= ?G # -
706 case OO_Minus: Out << "?G"; break;
707 // <operator-name> ::= ?H # +
708 case OO_Plus: Out << "?H"; break;
709 // <operator-name> ::= ?I # &
710 case OO_Amp: Out << "?I"; break;
711 // <operator-name> ::= ?J # ->*
712 case OO_ArrowStar: Out << "?J"; break;
713 // <operator-name> ::= ?K # /
714 case OO_Slash: Out << "?K"; break;
715 // <operator-name> ::= ?L # %
716 case OO_Percent: Out << "?L"; break;
717 // <operator-name> ::= ?M # <
718 case OO_Less: Out << "?M"; break;
719 // <operator-name> ::= ?N # <=
720 case OO_LessEqual: Out << "?N"; break;
721 // <operator-name> ::= ?O # >
722 case OO_Greater: Out << "?O"; break;
723 // <operator-name> ::= ?P # >=
724 case OO_GreaterEqual: Out << "?P"; break;
725 // <operator-name> ::= ?Q # ,
726 case OO_Comma: Out << "?Q"; break;
727 // <operator-name> ::= ?R # ()
728 case OO_Call: Out << "?R"; break;
729 // <operator-name> ::= ?S # ~
730 case OO_Tilde: Out << "?S"; break;
731 // <operator-name> ::= ?T # ^
732 case OO_Caret: Out << "?T"; break;
733 // <operator-name> ::= ?U # |
734 case OO_Pipe: Out << "?U"; break;
735 // <operator-name> ::= ?V # &&
736 case OO_AmpAmp: Out << "?V"; break;
737 // <operator-name> ::= ?W # ||
738 case OO_PipePipe: Out << "?W"; break;
739 // <operator-name> ::= ?X # *=
740 case OO_StarEqual: Out << "?X"; break;
741 // <operator-name> ::= ?Y # +=
742 case OO_PlusEqual: Out << "?Y"; break;
743 // <operator-name> ::= ?Z # -=
744 case OO_MinusEqual: Out << "?Z"; break;
745 // <operator-name> ::= ?_0 # /=
746 case OO_SlashEqual: Out << "?_0"; break;
747 // <operator-name> ::= ?_1 # %=
748 case OO_PercentEqual: Out << "?_1"; break;
749 // <operator-name> ::= ?_2 # >>=
750 case OO_GreaterGreaterEqual: Out << "?_2"; break;
751 // <operator-name> ::= ?_3 # <<=
752 case OO_LessLessEqual: Out << "?_3"; break;
753 // <operator-name> ::= ?_4 # &=
754 case OO_AmpEqual: Out << "?_4"; break;
755 // <operator-name> ::= ?_5 # |=
756 case OO_PipeEqual: Out << "?_5"; break;
757 // <operator-name> ::= ?_6 # ^=
758 case OO_CaretEqual: Out << "?_6"; break;
759 // ?_7 # vftable
760 // ?_8 # vbtable
761 // ?_9 # vcall
762 // ?_A # typeof
763 // ?_B # local static guard
764 // ?_C # string
765 // ?_D # vbase destructor
766 // ?_E # vector deleting destructor
767 // ?_F # default constructor closure
768 // ?_G # scalar deleting destructor
769 // ?_H # vector constructor iterator
770 // ?_I # vector destructor iterator
771 // ?_J # vector vbase constructor iterator
772 // ?_K # virtual displacement map
773 // ?_L # eh vector constructor iterator
774 // ?_M # eh vector destructor iterator
775 // ?_N # eh vector vbase constructor iterator
776 // ?_O # copy constructor closure
777 // ?_P<name> # udt returning <name>
778 // ?_Q # <unknown>
779 // ?_R0 # RTTI Type Descriptor
780 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
781 // ?_R2 # RTTI Base Class Array
782 // ?_R3 # RTTI Class Hierarchy Descriptor
783 // ?_R4 # RTTI Complete Object Locator
784 // ?_S # local vftable
785 // ?_T # local vftable constructor closure
786 // <operator-name> ::= ?_U # new[]
787 case OO_Array_New: Out << "?_U"; break;
788 // <operator-name> ::= ?_V # delete[]
789 case OO_Array_Delete: Out << "?_V"; break;
790
791 case OO_Conditional: {
792 DiagnosticsEngine &Diags = Context.getDiags();
793 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
794 "cannot mangle this conditional operator yet");
795 Diags.Report(Loc, DiagID);
796 break;
797 }
798
799 case OO_None:
800 case NUM_OVERLOADED_OPERATORS:
801 llvm_unreachable("Not an overloaded operator");
802 }
803}
804
805void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
806 // <source name> ::= <identifier> @
807 std::string key = II->getNameStart();
808 BackRefMap::iterator Found;
809 if (UseNameBackReferences)
810 Found = NameBackReferences.find(key);
811 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
812 Out << II->getName() << '@';
813 if (UseNameBackReferences && NameBackReferences.size() < 10) {
814 size_t Size = NameBackReferences.size();
815 NameBackReferences[key] = Size;
816 }
817 } else {
818 Out << Found->second;
819 }
820}
821
822void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
823 Context.mangleObjCMethodName(MD, Out);
824}
825
826// Find out how many function decls live above this one and return an integer
827// suitable for use as the number in a numbered anonymous scope.
828// TODO: Memoize.
829static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
830 const DeclContext *DC = FD->getParent();
831 int level = 1;
832
833 while (DC && !DC->isTranslationUnit()) {
834 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
835 DC = DC->getParent();
836 }
837
838 return 2*level;
839}
840
841void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
842 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
843 // <numbered-anonymous-scope> ::= ? <number>
844 // Even though the name is rendered in reverse order (e.g.
845 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
846 // innermost. So a method bar in class C local to function foo gets mangled
847 // as something like:
848 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
849 // This is more apparent when you have a type nested inside a method of a
850 // type nested inside a function. A method baz in class D local to method
851 // bar of class C local to function foo gets mangled as:
852 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
853 // This scheme is general enough to support GCC-style nested
854 // functions. You could have a method baz of class C inside a function bar
855 // inside a function foo, like so:
856 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
857 int NestLevel = getLocalNestingLevel(FD);
858 Out << '?';
859 mangleNumber(NestLevel);
860 Out << '?';
861 mangle(FD, "?");
862}
863
864void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
865 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000866 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000867 // <template-name> ::= <unscoped-template-name> <template-args>
868 // ::= <substitution>
869 // Always start with the unqualified name.
870
871 // Templates have their own context for back references.
872 ArgBackRefMap OuterArgsContext;
873 BackRefMap OuterTemplateContext;
874 NameBackReferences.swap(OuterTemplateContext);
875 TypeBackReferences.swap(OuterArgsContext);
876
877 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000878 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000879
880 // Restore the previous back reference contexts.
881 NameBackReferences.swap(OuterTemplateContext);
882 TypeBackReferences.swap(OuterArgsContext);
883}
884
885void
886MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
887 // <unscoped-template-name> ::= ?$ <unqualified-name>
888 Out << "?$";
889 mangleUnqualifiedName(TD);
890}
891
892void
893MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
894 bool IsBoolean) {
895 // <integer-literal> ::= $0 <number>
896 Out << "$0";
897 // Make sure booleans are encoded as 0/1.
898 if (IsBoolean && Value.getBoolValue())
899 mangleNumber(1);
900 else
901 mangleNumber(Value);
902}
903
904void
905MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
906 // See if this is a constant expression.
907 llvm::APSInt Value;
908 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
909 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
910 return;
911 }
912
David Majnemerc80eb462013-08-13 06:32:20 +0000913 const CXXUuidofExpr *UE = 0;
914 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
915 if (UO->getOpcode() == UO_AddrOf)
916 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
917 } else
918 UE = dyn_cast<CXXUuidofExpr>(E);
919
920 if (UE) {
921 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
922 // const __s_GUID _GUID_{lower case UUID with underscores}
923 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
924 std::string Name = "_GUID_" + Uuid.lower();
925 std::replace(Name.begin(), Name.end(), '-', '_');
926
David Majnemer26314e12013-08-13 09:17:25 +0000927 // If we had to peek through an address-of operator, treat this like we are
David Majnemerc80eb462013-08-13 06:32:20 +0000928 // dealing with a pointer type. Otherwise, treat it like a const reference.
929 //
930 // N.B. This matches up with the handling of TemplateArgument::Declaration
931 // in mangleTemplateArg
932 if (UE == E)
933 Out << "$E?";
934 else
935 Out << "$1?";
936 Out << Name << "@@3U__s_GUID@@B";
937 return;
938 }
939
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000940 // As bad as this diagnostic is, it's better than crashing.
941 DiagnosticsEngine &Diags = Context.getDiags();
942 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
943 "cannot yet mangle expression type %0");
944 Diags.Report(E->getExprLoc(), DiagID)
945 << E->getStmtClassName() << E->getSourceRange();
946}
947
948void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000949MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
950 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000951 // <template-args> ::= {<type> | <integer-literal>}+ @
952 unsigned NumTemplateArgs = TemplateArgs.size();
953 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000954 const TemplateArgument &TA = TemplateArgs[i];
David Majnemer309f6452013-08-27 08:21:25 +0000955 mangleTemplateArg(TD, TA);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000956 }
957 Out << '@';
958}
959
Reid Kleckner5d90d182013-07-02 18:10:07 +0000960void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
David Majnemer309f6452013-08-27 08:21:25 +0000961 const TemplateArgument &TA) {
Reid Kleckner5d90d182013-07-02 18:10:07 +0000962 switch (TA.getKind()) {
963 case TemplateArgument::Null:
964 llvm_unreachable("Can't mangle null template arguments!");
David Majnemer309f6452013-08-27 08:21:25 +0000965 case TemplateArgument::TemplateExpansion:
966 llvm_unreachable("Can't mangle template expansion arguments!");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000967 case TemplateArgument::Type: {
968 QualType T = TA.getAsType();
969 mangleType(T, SourceRange(), QMM_Escape);
970 break;
971 }
David Majnemerf2081f62013-08-13 01:25:35 +0000972 case TemplateArgument::Declaration: {
973 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl());
974 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000975 break;
David Majnemerf2081f62013-08-13 01:25:35 +0000976 }
Reid Kleckner5d90d182013-07-02 18:10:07 +0000977 case TemplateArgument::Integral:
978 mangleIntegerLiteral(TA.getAsIntegral(),
979 TA.getIntegralType()->isBooleanType());
980 break;
David Majnemer7802fc92013-08-05 21:33:59 +0000981 case TemplateArgument::NullPtr:
982 Out << "$0A@";
983 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000984 case TemplateArgument::Expression:
985 mangleExpression(TA.getAsExpr());
986 break;
987 case TemplateArgument::Pack:
988 // Unlike Itanium, there is no character code to indicate an argument pack.
Reid Kleckner5d90d182013-07-02 18:10:07 +0000989 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
990 I != E; ++I)
David Majnemer309f6452013-08-27 08:21:25 +0000991 mangleTemplateArg(TD, *I);
Reid Kleckner5d90d182013-07-02 18:10:07 +0000992 break;
993 case TemplateArgument::Template:
David Majnemer02c44f02013-08-05 22:26:46 +0000994 mangleType(cast<TagDecl>(
995 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
996 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000997 }
998}
999
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001000void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
1001 bool IsMember) {
1002 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
1003 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
1004 // 'I' means __restrict (32/64-bit).
1005 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
1006 // keyword!
1007 // <base-cvr-qualifiers> ::= A # near
1008 // ::= B # near const
1009 // ::= C # near volatile
1010 // ::= D # near const volatile
1011 // ::= E # far (16-bit)
1012 // ::= F # far const (16-bit)
1013 // ::= G # far volatile (16-bit)
1014 // ::= H # far const volatile (16-bit)
1015 // ::= I # huge (16-bit)
1016 // ::= J # huge const (16-bit)
1017 // ::= K # huge volatile (16-bit)
1018 // ::= L # huge const volatile (16-bit)
1019 // ::= M <basis> # based
1020 // ::= N <basis> # based const
1021 // ::= O <basis> # based volatile
1022 // ::= P <basis> # based const volatile
1023 // ::= Q # near member
1024 // ::= R # near const member
1025 // ::= S # near volatile member
1026 // ::= T # near const volatile member
1027 // ::= U # far member (16-bit)
1028 // ::= V # far const member (16-bit)
1029 // ::= W # far volatile member (16-bit)
1030 // ::= X # far const volatile member (16-bit)
1031 // ::= Y # huge member (16-bit)
1032 // ::= Z # huge const member (16-bit)
1033 // ::= 0 # huge volatile member (16-bit)
1034 // ::= 1 # huge const volatile member (16-bit)
1035 // ::= 2 <basis> # based member
1036 // ::= 3 <basis> # based const member
1037 // ::= 4 <basis> # based volatile member
1038 // ::= 5 <basis> # based const volatile member
1039 // ::= 6 # near function (pointers only)
1040 // ::= 7 # far function (pointers only)
1041 // ::= 8 # near method (pointers only)
1042 // ::= 9 # far method (pointers only)
1043 // ::= _A <basis> # based function (pointers only)
1044 // ::= _B <basis> # based function (far?) (pointers only)
1045 // ::= _C <basis> # based method (pointers only)
1046 // ::= _D <basis> # based method (far?) (pointers only)
1047 // ::= _E # block (Clang)
1048 // <basis> ::= 0 # __based(void)
1049 // ::= 1 # __based(segment)?
1050 // ::= 2 <name> # __based(name)
1051 // ::= 3 # ?
1052 // ::= 4 # ?
1053 // ::= 5 # not really based
1054 bool HasConst = Quals.hasConst(),
1055 HasVolatile = Quals.hasVolatile();
David Majnemerc0e64f32013-08-05 22:43:06 +00001056
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001057 if (!IsMember) {
1058 if (HasConst && HasVolatile) {
1059 Out << 'D';
1060 } else if (HasVolatile) {
1061 Out << 'C';
1062 } else if (HasConst) {
1063 Out << 'B';
1064 } else {
1065 Out << 'A';
1066 }
1067 } else {
1068 if (HasConst && HasVolatile) {
1069 Out << 'T';
1070 } else if (HasVolatile) {
1071 Out << 'S';
1072 } else if (HasConst) {
1073 Out << 'R';
1074 } else {
1075 Out << 'Q';
1076 }
1077 }
1078
1079 // FIXME: For now, just drop all extension qualifiers on the floor.
1080}
1081
1082void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1083 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1084 // ::= Q # const
1085 // ::= R # volatile
1086 // ::= S # const volatile
1087 bool HasConst = Quals.hasConst(),
1088 HasVolatile = Quals.hasVolatile();
1089 if (HasConst && HasVolatile) {
1090 Out << 'S';
1091 } else if (HasVolatile) {
1092 Out << 'R';
1093 } else if (HasConst) {
1094 Out << 'Q';
1095 } else {
1096 Out << 'P';
1097 }
1098}
1099
1100void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1101 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001102 // MSVC will backreference two canonically equivalent types that have slightly
1103 // different manglings when mangled alone.
David Majnemer58e4cd02013-09-11 04:44:30 +00001104
1105 // Decayed types do not match up with non-decayed versions of the same type.
1106 //
1107 // e.g.
1108 // void (*x)(void) will not form a backreference with void x(void)
1109 void *TypePtr;
1110 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1111 TypePtr = DT->getOriginalType().getCanonicalType().getAsOpaquePtr();
1112 // If the original parameter was textually written as an array,
1113 // instead treat the decayed parameter like it's const.
1114 //
1115 // e.g.
1116 // int [] -> int * const
1117 if (DT->getOriginalType()->isArrayType())
1118 T = T.withConst();
1119 } else
1120 TypePtr = T.getCanonicalType().getAsOpaquePtr();
1121
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001122 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1123
1124 if (Found == TypeBackReferences.end()) {
1125 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1126
David Majnemer58e4cd02013-09-11 04:44:30 +00001127 mangleType(T, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001128
1129 // See if it's worth creating a back reference.
1130 // Only types longer than 1 character are considered
1131 // and only 10 back references slots are available:
1132 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1133 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1134 size_t Size = TypeBackReferences.size();
1135 TypeBackReferences[TypePtr] = Size;
1136 }
1137 } else {
1138 Out << Found->second;
1139 }
1140}
1141
1142void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001143 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001144 // Don't use the canonical types. MSVC includes things like 'const' on
1145 // pointer arguments to function pointers that canonicalization strips away.
1146 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001147 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001148 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1149 // If there were any Quals, getAsArrayType() pushed them onto the array
1150 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001151 if (QMM == QMM_Mangle)
1152 Out << 'A';
1153 else if (QMM == QMM_Escape || QMM == QMM_Result)
1154 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001155 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001156 return;
1157 }
1158
1159 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1160 T->isBlockPointerType();
1161
1162 switch (QMM) {
1163 case QMM_Drop:
1164 break;
1165 case QMM_Mangle:
1166 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1167 Out << '6';
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +00001168 mangleFunctionType(FT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001169 return;
1170 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001171 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001172 break;
1173 case QMM_Escape:
1174 if (!IsPointer && Quals) {
1175 Out << "$$C";
1176 mangleQualifiers(Quals, false);
1177 }
1178 break;
1179 case QMM_Result:
1180 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1181 Out << '?';
1182 mangleQualifiers(Quals, false);
1183 }
1184 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001185 }
1186
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001187 // We have to mangle these now, while we still have enough information.
1188 if (IsPointer)
1189 manglePointerQualifiers(Quals);
1190 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001191
1192 switch (ty->getTypeClass()) {
1193#define ABSTRACT_TYPE(CLASS, PARENT)
1194#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1195 case Type::CLASS: \
1196 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1197 return;
1198#define TYPE(CLASS, PARENT) \
1199 case Type::CLASS: \
1200 mangleType(cast<CLASS##Type>(ty), Range); \
1201 break;
1202#include "clang/AST/TypeNodes.def"
1203#undef ABSTRACT_TYPE
1204#undef NON_CANONICAL_TYPE
1205#undef TYPE
1206 }
1207}
1208
1209void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1210 SourceRange Range) {
1211 // <type> ::= <builtin-type>
1212 // <builtin-type> ::= X # void
1213 // ::= C # signed char
1214 // ::= D # char
1215 // ::= E # unsigned char
1216 // ::= F # short
1217 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1218 // ::= H # int
1219 // ::= I # unsigned int
1220 // ::= J # long
1221 // ::= K # unsigned long
1222 // L # <none>
1223 // ::= M # float
1224 // ::= N # double
1225 // ::= O # long double (__float80 is mangled differently)
1226 // ::= _J # long long, __int64
1227 // ::= _K # unsigned long long, __int64
1228 // ::= _L # __int128
1229 // ::= _M # unsigned __int128
1230 // ::= _N # bool
1231 // _O # <array in parameter>
1232 // ::= _T # __float80 (Intel)
1233 // ::= _W # wchar_t
1234 // ::= _Z # __float80 (Digital Mars)
1235 switch (T->getKind()) {
1236 case BuiltinType::Void: Out << 'X'; break;
1237 case BuiltinType::SChar: Out << 'C'; break;
1238 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1239 case BuiltinType::UChar: Out << 'E'; break;
1240 case BuiltinType::Short: Out << 'F'; break;
1241 case BuiltinType::UShort: Out << 'G'; break;
1242 case BuiltinType::Int: Out << 'H'; break;
1243 case BuiltinType::UInt: Out << 'I'; break;
1244 case BuiltinType::Long: Out << 'J'; break;
1245 case BuiltinType::ULong: Out << 'K'; break;
1246 case BuiltinType::Float: Out << 'M'; break;
1247 case BuiltinType::Double: Out << 'N'; break;
1248 // TODO: Determine size and mangle accordingly
1249 case BuiltinType::LongDouble: Out << 'O'; break;
1250 case BuiltinType::LongLong: Out << "_J"; break;
1251 case BuiltinType::ULongLong: Out << "_K"; break;
1252 case BuiltinType::Int128: Out << "_L"; break;
1253 case BuiltinType::UInt128: Out << "_M"; break;
1254 case BuiltinType::Bool: Out << "_N"; break;
1255 case BuiltinType::WChar_S:
1256 case BuiltinType::WChar_U: Out << "_W"; break;
1257
1258#define BUILTIN_TYPE(Id, SingletonId)
1259#define PLACEHOLDER_TYPE(Id, SingletonId) \
1260 case BuiltinType::Id:
1261#include "clang/AST/BuiltinTypes.def"
1262 case BuiltinType::Dependent:
1263 llvm_unreachable("placeholder types shouldn't get to name mangling");
1264
1265 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1266 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1267 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001268
1269 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1270 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1271 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1272 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1273 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1274 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001275 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001276 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001277
1278 case BuiltinType::NullPtr: Out << "$$T"; break;
1279
1280 case BuiltinType::Char16:
1281 case BuiltinType::Char32:
1282 case BuiltinType::Half: {
1283 DiagnosticsEngine &Diags = Context.getDiags();
1284 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1285 "cannot mangle this built-in %0 type yet");
1286 Diags.Report(Range.getBegin(), DiagID)
1287 << T->getName(Context.getASTContext().getPrintingPolicy())
1288 << Range;
1289 break;
1290 }
1291 }
1292}
1293
1294// <type> ::= <function-type>
1295void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1296 SourceRange) {
1297 // Structors only appear in decls, so at this point we know it's not a
1298 // structor type.
1299 // FIXME: This may not be lambda-friendly.
1300 Out << "$$A6";
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +00001301 mangleFunctionType(T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001302}
1303void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1304 SourceRange) {
1305 llvm_unreachable("Can't mangle K&R function prototypes");
1306}
1307
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001308void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1309 const FunctionDecl *D,
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +00001310 bool ForceInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001311 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1312 // <return-type> <argument-list> <throw-spec>
1313 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1314
Reid Klecknerf21818d2013-06-24 19:21:52 +00001315 SourceRange Range;
1316 if (D) Range = D->getSourceRange();
1317
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +00001318 bool IsStructor = false, IsInstMethod = ForceInstMethod;
1319 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(D)) {
1320 if (MD->isInstance())
1321 IsInstMethod = true;
1322 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
1323 IsStructor = true;
1324 }
1325
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001326 // If this is a C++ instance method, mangle the CVR qualifiers for the
1327 // this pointer.
David Majnemer1c7a4092013-08-15 08:13:23 +00001328 if (IsInstMethod) {
1329 if (PointersAre64Bit)
1330 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001331 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
David Majnemer1c7a4092013-08-15 08:13:23 +00001332 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001333
Reid Klecknere3e686f2013-09-25 22:28:52 +00001334 mangleCallingConvention(T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001335
1336 // <return-type> ::= <type>
1337 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001338 if (IsStructor) {
1339 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1340 StructorType == Dtor_Deleting) {
1341 // The scalar deleting destructor takes an extra int argument.
1342 // However, the FunctionType generated has 0 arguments.
1343 // FIXME: This is a temporary hack.
1344 // Maybe should fix the FunctionType creation instead?
Timur Iskhodzhanov4b104062013-08-26 10:32:04 +00001345 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001346 return;
1347 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001348 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001349 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001350 QualType ResultType = Proto->getResultType();
1351 if (ResultType->isVoidType())
1352 ResultType = ResultType.getUnqualifiedType();
1353 mangleType(ResultType, Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001354 }
1355
1356 // <argument-list> ::= X # void
1357 // ::= <type>+ @
1358 // ::= <type>* Z # varargs
1359 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1360 Out << 'X';
1361 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001362 // Happens for function pointer type arguments for example.
1363 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1364 ArgEnd = Proto->arg_type_end();
1365 Arg != ArgEnd; ++Arg)
1366 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001367 // <builtin-type> ::= Z # ellipsis
1368 if (Proto->isVariadic())
1369 Out << 'Z';
1370 else
1371 Out << '@';
1372 }
1373
1374 mangleThrowSpecification(Proto);
1375}
1376
1377void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001378 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1379 // # pointer. in 64-bit mode *all*
1380 // # 'this' pointers are 64-bit.
1381 // ::= <global-function>
1382 // <member-function> ::= A # private: near
1383 // ::= B # private: far
1384 // ::= C # private: static near
1385 // ::= D # private: static far
1386 // ::= E # private: virtual near
1387 // ::= F # private: virtual far
1388 // ::= G # private: thunk near
1389 // ::= H # private: thunk far
1390 // ::= I # protected: near
1391 // ::= J # protected: far
1392 // ::= K # protected: static near
1393 // ::= L # protected: static far
1394 // ::= M # protected: virtual near
1395 // ::= N # protected: virtual far
1396 // ::= O # protected: thunk near
1397 // ::= P # protected: thunk far
1398 // ::= Q # public: near
1399 // ::= R # public: far
1400 // ::= S # public: static near
1401 // ::= T # public: static far
1402 // ::= U # public: virtual near
1403 // ::= V # public: virtual far
1404 // ::= W # public: thunk near
1405 // ::= X # public: thunk far
1406 // <global-function> ::= Y # global near
1407 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001408 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1409 switch (MD->getAccess()) {
1410 default:
1411 case AS_private:
1412 if (MD->isStatic())
1413 Out << 'C';
1414 else if (MD->isVirtual())
1415 Out << 'E';
1416 else
1417 Out << 'A';
1418 break;
1419 case AS_protected:
1420 if (MD->isStatic())
1421 Out << 'K';
1422 else if (MD->isVirtual())
1423 Out << 'M';
1424 else
1425 Out << 'I';
1426 break;
1427 case AS_public:
1428 if (MD->isStatic())
1429 Out << 'S';
1430 else if (MD->isVirtual())
1431 Out << 'U';
1432 else
1433 Out << 'Q';
1434 }
1435 } else
1436 Out << 'Y';
1437}
Reid Klecknere3e686f2013-09-25 22:28:52 +00001438void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001439 // <calling-convention> ::= A # __cdecl
1440 // ::= B # __export __cdecl
1441 // ::= C # __pascal
1442 // ::= D # __export __pascal
1443 // ::= E # __thiscall
1444 // ::= F # __export __thiscall
1445 // ::= G # __stdcall
1446 // ::= H # __export __stdcall
1447 // ::= I # __fastcall
1448 // ::= J # __export __fastcall
1449 // The 'export' calling conventions are from a bygone era
1450 // (*cough*Win16*cough*) when functions were declared for export with
1451 // that keyword. (It didn't actually export them, it just made them so
1452 // that they could be in a DLL and somebody from another module could call
1453 // them.)
1454 CallingConv CC = T->getCallConv();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001455 switch (CC) {
1456 default:
1457 llvm_unreachable("Unsupported CC for mangling");
Charles Davise8519c32013-08-30 04:39:01 +00001458 case CC_X86_64Win64:
1459 case CC_X86_64SysV:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001460 case CC_C: Out << 'A'; break;
1461 case CC_X86Pascal: Out << 'C'; break;
1462 case CC_X86ThisCall: Out << 'E'; break;
1463 case CC_X86StdCall: Out << 'G'; break;
1464 case CC_X86FastCall: Out << 'I'; break;
1465 }
1466}
1467void MicrosoftCXXNameMangler::mangleThrowSpecification(
1468 const FunctionProtoType *FT) {
1469 // <throw-spec> ::= Z # throw(...) (default)
1470 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1471 // ::= <type>+
1472 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1473 // all actually mangled as 'Z'. (They're ignored because their associated
1474 // functionality isn't implemented, and probably never will be.)
1475 Out << 'Z';
1476}
1477
1478void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1479 SourceRange Range) {
1480 // Probably should be mangled as a template instantiation; need to see what
1481 // VC does first.
1482 DiagnosticsEngine &Diags = Context.getDiags();
1483 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1484 "cannot mangle this unresolved dependent type yet");
1485 Diags.Report(Range.getBegin(), DiagID)
1486 << Range;
1487}
1488
1489// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1490// <union-type> ::= T <name>
1491// <struct-type> ::= U <name>
1492// <class-type> ::= V <name>
1493// <enum-type> ::= W <size> <name>
1494void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001495 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001496}
1497void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001498 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001499}
David Majnemer02c44f02013-08-05 22:26:46 +00001500void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1501 switch (TD->getTagKind()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001502 case TTK_Union:
1503 Out << 'T';
1504 break;
1505 case TTK_Struct:
1506 case TTK_Interface:
1507 Out << 'U';
1508 break;
1509 case TTK_Class:
1510 Out << 'V';
1511 break;
1512 case TTK_Enum:
1513 Out << 'W';
1514 Out << getASTContext().getTypeSizeInChars(
David Majnemer02c44f02013-08-05 22:26:46 +00001515 cast<EnumDecl>(TD)->getIntegerType()).getQuantity();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001516 break;
1517 }
David Majnemer02c44f02013-08-05 22:26:46 +00001518 mangleName(TD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001519}
1520
1521// <type> ::= <array-type>
1522// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1523// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001524// <element-type> # as global, E is never required
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001525// It's supposed to be the other way around, but for some strange reason, it
1526// isn't. Today this behavior is retained for the sole purpose of backwards
1527// compatibility.
David Majnemer58e4cd02013-09-11 04:44:30 +00001528void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001529 // This isn't a recursive mangling, so now we have to do it all in this
1530 // one call.
David Majnemer58e4cd02013-09-11 04:44:30 +00001531 manglePointerQualifiers(T->getElementType().getQualifiers());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001532 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001533}
1534void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1535 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001536 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001537}
1538void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1539 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001540 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001541}
1542void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1543 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001544 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001545}
1546void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1547 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001548 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001549}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001550void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001551 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001552 SmallVector<llvm::APInt, 3> Dimensions;
1553 for (;;) {
1554 if (const ConstantArrayType *CAT =
1555 getASTContext().getAsConstantArrayType(ElementTy)) {
1556 Dimensions.push_back(CAT->getSize());
1557 ElementTy = CAT->getElementType();
1558 } else if (ElementTy->isVariableArrayType()) {
1559 const VariableArrayType *VAT =
1560 getASTContext().getAsVariableArrayType(ElementTy);
1561 DiagnosticsEngine &Diags = Context.getDiags();
1562 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1563 "cannot mangle this variable-length array yet");
1564 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1565 << VAT->getBracketsRange();
1566 return;
1567 } else if (ElementTy->isDependentSizedArrayType()) {
1568 // The dependent expression has to be folded into a constant (TODO).
1569 const DependentSizedArrayType *DSAT =
1570 getASTContext().getAsDependentSizedArrayType(ElementTy);
1571 DiagnosticsEngine &Diags = Context.getDiags();
1572 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1573 "cannot mangle this dependent-length array yet");
1574 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1575 << DSAT->getBracketsRange();
1576 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001577 } else if (const IncompleteArrayType *IAT =
1578 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1579 Dimensions.push_back(llvm::APInt(32, 0));
1580 ElementTy = IAT->getElementType();
1581 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001582 else break;
1583 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001584 Out << 'Y';
1585 // <dimension-count> ::= <number> # number of extra dimensions
1586 mangleNumber(Dimensions.size());
1587 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1588 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001589 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001590}
1591
1592// <type> ::= <pointer-to-member-type>
1593// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1594// <class name> <type>
1595void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1596 SourceRange Range) {
1597 QualType PointeeType = T->getPointeeType();
1598 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1599 Out << '8';
1600 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +00001601 mangleFunctionType(FPT, 0, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001602 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001603 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1604 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001605 mangleQualifiers(PointeeType.getQualifiers(), true);
1606 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001607 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001608 }
1609}
1610
1611void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1612 SourceRange Range) {
1613 DiagnosticsEngine &Diags = Context.getDiags();
1614 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1615 "cannot mangle this template type parameter type yet");
1616 Diags.Report(Range.getBegin(), DiagID)
1617 << Range;
1618}
1619
1620void MicrosoftCXXNameMangler::mangleType(
1621 const SubstTemplateTypeParmPackType *T,
1622 SourceRange Range) {
1623 DiagnosticsEngine &Diags = Context.getDiags();
1624 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1625 "cannot mangle this substituted parameter pack yet");
1626 Diags.Report(Range.getBegin(), DiagID)
1627 << Range;
1628}
1629
1630// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001631// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1632// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001633void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1634 SourceRange Range) {
1635 QualType PointeeTy = T->getPointeeType();
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001636 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1637 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001638 mangleType(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001639}
1640void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1641 SourceRange Range) {
1642 // Object pointers never have qualifiers.
1643 Out << 'A';
David Majnemer1c7a4092013-08-15 08:13:23 +00001644 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1645 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001646 mangleType(T->getPointeeType(), Range);
1647}
1648
1649// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001650// <reference-type> ::= A E? <cvr-qualifiers> <type>
1651// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001652void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1653 SourceRange Range) {
1654 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001655 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1656 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001657 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001658}
1659
1660// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001661// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1662// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001663void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1664 SourceRange Range) {
1665 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001666 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1667 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001668 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001669}
1670
1671void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1672 SourceRange Range) {
1673 DiagnosticsEngine &Diags = Context.getDiags();
1674 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1675 "cannot mangle this complex number type yet");
1676 Diags.Report(Range.getBegin(), DiagID)
1677 << Range;
1678}
1679
1680void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1681 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001682 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1683 assert(ET && "vectors with non-builtin elements are unsupported");
1684 uint64_t Width = getASTContext().getTypeSize(T);
1685 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1686 // doesn't match the Intel types uses a custom mangling below.
1687 bool IntelVector = true;
1688 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1689 Out << "T__m64";
1690 } else if (Width == 128 || Width == 256) {
1691 if (ET->getKind() == BuiltinType::Float)
1692 Out << "T__m" << Width;
1693 else if (ET->getKind() == BuiltinType::LongLong)
1694 Out << "T__m" << Width << 'i';
1695 else if (ET->getKind() == BuiltinType::Double)
1696 Out << "U__m" << Width << 'd';
1697 else
1698 IntelVector = false;
1699 } else {
1700 IntelVector = false;
1701 }
1702
1703 if (!IntelVector) {
1704 // The MS ABI doesn't have a special mangling for vector types, so we define
1705 // our own mangling to handle uses of __vector_size__ on user-specified
1706 // types, and for extensions like __v4sf.
1707 Out << "T__clang_vec" << T->getNumElements() << '_';
1708 mangleType(ET, Range);
1709 }
1710
1711 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001712}
Reid Kleckner1232e272013-03-26 16:56:59 +00001713
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001714void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1715 SourceRange Range) {
1716 DiagnosticsEngine &Diags = Context.getDiags();
1717 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1718 "cannot mangle this extended vector type yet");
1719 Diags.Report(Range.getBegin(), DiagID)
1720 << Range;
1721}
1722void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1723 SourceRange Range) {
1724 DiagnosticsEngine &Diags = Context.getDiags();
1725 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1726 "cannot mangle this dependent-sized extended vector type yet");
1727 Diags.Report(Range.getBegin(), DiagID)
1728 << Range;
1729}
1730
1731void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1732 SourceRange) {
1733 // ObjC interfaces have structs underlying them.
1734 Out << 'U';
1735 mangleName(T->getDecl());
1736}
1737
1738void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1739 SourceRange Range) {
1740 // We don't allow overloading by different protocol qualification,
1741 // so mangling them isn't necessary.
1742 mangleType(T->getBaseType(), Range);
1743}
1744
1745void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1746 SourceRange Range) {
1747 Out << "_E";
1748
1749 QualType pointee = T->getPointeeType();
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +00001750 mangleFunctionType(pointee->castAs<FunctionProtoType>());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001751}
1752
David Majnemer360d23e2013-08-16 08:29:13 +00001753void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1754 SourceRange) {
1755 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001756}
1757
1758void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1759 SourceRange Range) {
1760 DiagnosticsEngine &Diags = Context.getDiags();
1761 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1762 "cannot mangle this template specialization type yet");
1763 Diags.Report(Range.getBegin(), DiagID)
1764 << Range;
1765}
1766
1767void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1768 SourceRange Range) {
1769 DiagnosticsEngine &Diags = Context.getDiags();
1770 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1771 "cannot mangle this dependent name type yet");
1772 Diags.Report(Range.getBegin(), DiagID)
1773 << Range;
1774}
1775
1776void MicrosoftCXXNameMangler::mangleType(
1777 const DependentTemplateSpecializationType *T,
1778 SourceRange Range) {
1779 DiagnosticsEngine &Diags = Context.getDiags();
1780 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1781 "cannot mangle this dependent template specialization type yet");
1782 Diags.Report(Range.getBegin(), DiagID)
1783 << Range;
1784}
1785
1786void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1787 SourceRange Range) {
1788 DiagnosticsEngine &Diags = Context.getDiags();
1789 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1790 "cannot mangle this pack expansion yet");
1791 Diags.Report(Range.getBegin(), DiagID)
1792 << Range;
1793}
1794
1795void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1796 SourceRange Range) {
1797 DiagnosticsEngine &Diags = Context.getDiags();
1798 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1799 "cannot mangle this typeof(type) yet");
1800 Diags.Report(Range.getBegin(), DiagID)
1801 << Range;
1802}
1803
1804void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1805 SourceRange Range) {
1806 DiagnosticsEngine &Diags = Context.getDiags();
1807 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1808 "cannot mangle this typeof(expression) yet");
1809 Diags.Report(Range.getBegin(), DiagID)
1810 << Range;
1811}
1812
1813void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1814 SourceRange Range) {
1815 DiagnosticsEngine &Diags = Context.getDiags();
1816 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1817 "cannot mangle this decltype() yet");
1818 Diags.Report(Range.getBegin(), DiagID)
1819 << Range;
1820}
1821
1822void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1823 SourceRange Range) {
1824 DiagnosticsEngine &Diags = Context.getDiags();
1825 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1826 "cannot mangle this unary transform type yet");
1827 Diags.Report(Range.getBegin(), DiagID)
1828 << Range;
1829}
1830
1831void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1832 DiagnosticsEngine &Diags = Context.getDiags();
1833 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1834 "cannot mangle this 'auto' type yet");
1835 Diags.Report(Range.getBegin(), DiagID)
1836 << Range;
1837}
1838
1839void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1840 SourceRange Range) {
1841 DiagnosticsEngine &Diags = Context.getDiags();
1842 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1843 "cannot mangle this C11 atomic type yet");
1844 Diags.Report(Range.getBegin(), DiagID)
1845 << Range;
1846}
1847
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001848void MicrosoftMangleContextImpl::mangleName(const NamedDecl *D,
1849 raw_ostream &Out) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001850 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1851 "Invalid mangleName() call, argument is not a variable or function!");
1852 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1853 "Invalid mangleName() call on 'structor decl!");
1854
1855 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1856 getASTContext().getSourceManager(),
1857 "Mangling declaration");
1858
1859 MicrosoftCXXNameMangler Mangler(*this, Out);
1860 return Mangler.mangle(D);
1861}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001862
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001863void MicrosoftMangleContextImpl::mangleThunk(const CXXMethodDecl *MD,
1864 const ThunkInfo &Thunk,
1865 raw_ostream &Out) {
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001866 // FIXME: this is not yet a complete implementation, but merely a
1867 // reasonably-working stub to avoid crashing when required to emit a thunk.
1868 MicrosoftCXXNameMangler Mangler(*this, Out);
1869 Out << "\01?";
1870 Mangler.mangleName(MD);
1871 if (Thunk.This.NonVirtual != 0) {
1872 // FIXME: add support for protected/private or use mangleFunctionClass.
1873 Out << "W";
1874 llvm::APSInt APSNumber(/*BitWidth=*/32 /*FIXME: check on x64*/,
1875 /*isUnsigned=*/true);
1876 APSNumber = -Thunk.This.NonVirtual;
1877 Mangler.mangleNumber(APSNumber);
1878 } else {
1879 // FIXME: add support for protected/private or use mangleFunctionClass.
1880 Out << "Q";
1881 }
1882 // FIXME: mangle return adjustment? Most likely includes using an overridee FPT?
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +00001883 Mangler.mangleFunctionType(MD->getType()->castAs<FunctionProtoType>(), MD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001884}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001885
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001886void MicrosoftMangleContextImpl::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1887 CXXDtorType Type,
1888 const ThisAdjustment &,
1889 raw_ostream &) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001890 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1891 "cannot mangle thunk for this destructor yet");
1892 getDiags().Report(DD->getLocation(), DiagID);
1893}
Reid Kleckner90633022013-06-19 15:20:38 +00001894
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001895void MicrosoftMangleContextImpl::mangleCXXVFTable(
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001896 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1897 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001898 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1899 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001900 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001901 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001902 MicrosoftCXXNameMangler Mangler(*this, Out);
1903 Mangler.getStream() << "\01??_7";
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001904 Mangler.mangleName(Derived);
1905 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
1906 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1907 E = BasePath.end();
1908 I != E; ++I) {
1909 Mangler.mangleName(*I);
1910 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001911 Mangler.getStream() << '@';
1912}
Reid Kleckner90633022013-06-19 15:20:38 +00001913
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001914void MicrosoftMangleContextImpl::mangleCXXVBTable(
Reid Kleckner90633022013-06-19 15:20:38 +00001915 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1916 raw_ostream &Out) {
1917 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1918 // <cvr-qualifiers> [<name>] @
1919 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1920 // is always '7' for vbtables.
1921 MicrosoftCXXNameMangler Mangler(*this, Out);
1922 Mangler.getStream() << "\01??_8";
1923 Mangler.mangleName(Derived);
1924 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1925 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1926 E = BasePath.end();
1927 I != E; ++I) {
1928 Mangler.mangleName(*I);
1929 }
1930 Mangler.getStream() << '@';
1931}
1932
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001933void MicrosoftMangleContextImpl::mangleCXXRTTI(QualType T, raw_ostream &) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001934 // FIXME: Give a location...
1935 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1936 "cannot mangle RTTI descriptors for type %0 yet");
1937 getDiags().Report(DiagID)
1938 << T.getBaseTypeIdentifier();
1939}
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001940
1941void MicrosoftMangleContextImpl::mangleCXXRTTIName(QualType T, raw_ostream &) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001942 // FIXME: Give a location...
1943 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1944 "cannot mangle the name of type %0 into RTTI descriptors yet");
1945 getDiags().Report(DiagID)
1946 << T.getBaseTypeIdentifier();
1947}
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001948
1949void MicrosoftMangleContextImpl::mangleCXXCtor(const CXXConstructorDecl *D,
1950 CXXCtorType Type,
1951 raw_ostream &Out) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001952 MicrosoftCXXNameMangler mangler(*this, Out);
1953 mangler.mangle(D);
1954}
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001955
1956void MicrosoftMangleContextImpl::mangleCXXDtor(const CXXDestructorDecl *D,
1957 CXXDtorType Type,
1958 raw_ostream &Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001959 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001960 mangler.mangle(D);
1961}
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001962
1963void MicrosoftMangleContextImpl::mangleReferenceTemporary(const VarDecl *VD,
1964 raw_ostream &) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001965 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1966 "cannot mangle this reference temporary yet");
1967 getDiags().Report(VD->getLocation(), DiagID);
1968}
1969
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001970void MicrosoftMangleContextImpl::mangleStaticGuardVariable(const VarDecl *VD,
1971 raw_ostream &Out) {
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001972 // <guard-name> ::= ?_B <postfix> @51
1973 // ::= ?$S <guard-num> @ <postfix> @4IA
1974
1975 // The first mangling is what MSVC uses to guard static locals in inline
1976 // functions. It uses a different mangling in external functions to support
1977 // guarding more than 32 variables. MSVC rejects inline functions with more
1978 // than 32 static locals. We don't fully implement the second mangling
1979 // because those guards are not externally visible, and instead use LLVM's
1980 // default renaming when creating a new guard variable.
1981 MicrosoftCXXNameMangler Mangler(*this, Out);
1982
1983 bool Visible = VD->isExternallyVisible();
1984 // <operator-name> ::= ?_B # local static guard
1985 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@");
1986 Mangler.manglePostfix(VD->getDeclContext());
1987 Mangler.getStream() << (Visible ? "@51" : "@4IA");
1988}
1989
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001990void MicrosoftMangleContextImpl::mangleInitFiniStub(const VarDecl *D,
1991 raw_ostream &Out,
1992 char CharCode) {
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00001993 MicrosoftCXXNameMangler Mangler(*this, Out);
1994 Mangler.getStream() << "\01??__" << CharCode;
1995 Mangler.mangleName(D);
1996 // This is the function class mangling. These stubs are global, non-variadic,
1997 // cdecl functions that return void and take no args.
1998 Mangler.getStream() << "YAXXZ";
1999}
2000
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00002001void MicrosoftMangleContextImpl::mangleDynamicInitializer(const VarDecl *D,
2002 raw_ostream &Out) {
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00002003 // <initializer-name> ::= ?__E <name> YAXXZ
2004 mangleInitFiniStub(D, Out, 'E');
2005}
2006
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00002007void
2008MicrosoftMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D,
2009 raw_ostream &Out) {
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00002010 // <destructor-name> ::= ?__F <name> YAXXZ
2011 mangleInitFiniStub(D, Out, 'F');
Reid Kleckner942f9fe2013-09-10 20:14:30 +00002012}
2013
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00002014MicrosoftMangleContext *
2015MicrosoftMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags) {
2016 return new MicrosoftMangleContextImpl(Context, Diags);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002017}