blob: 979523af4b1c73db3f10ac824901a46ef44f09b2 [file] [log] [blame]
Guy Benyei11169dd2012-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"
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +000017#include "clang/AST/CXXInheritance.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000018#include "clang/AST/CharUnits.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000019#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/AST/DeclTemplate.h"
23#include "clang/AST/ExprCXX.h"
24#include "clang/Basic/ABI.h"
25#include "clang/Basic/DiagnosticOptions.h"
Reid Kleckner369f3162013-05-14 20:30:42 +000026#include "clang/Basic/TargetInfo.h"
Reid Kleckner7dafb232013-05-22 17:16:39 +000027#include "llvm/ADT/StringMap.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000028
29using namespace clang;
30
31namespace {
32
David Majnemerd5a42b82013-09-13 09:03:14 +000033/// \brief Retrieve the declaration context that should be used when mangling
34/// the given declaration.
35static const DeclContext *getEffectiveDeclContext(const Decl *D) {
36 // The ABI assumes that lambda closure types that occur within
37 // default arguments live in the context of the function. However, due to
38 // the way in which Clang parses and creates function declarations, this is
39 // not the case: the lambda closure type ends up living in the context
40 // where the function itself resides, because the function declaration itself
41 // had not yet been created. Fix the context here.
42 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
43 if (RD->isLambda())
44 if (ParmVarDecl *ContextParam =
45 dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl()))
46 return ContextParam->getDeclContext();
47 }
48
49 // Perform the same check for block literals.
50 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
51 if (ParmVarDecl *ContextParam =
52 dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl()))
53 return ContextParam->getDeclContext();
54 }
55
56 const DeclContext *DC = D->getDeclContext();
57 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(DC))
58 return getEffectiveDeclContext(CD);
59
60 return DC;
61}
62
63static const DeclContext *getEffectiveParentContext(const DeclContext *DC) {
64 return getEffectiveDeclContext(cast<Decl>(DC));
65}
66
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +000067static const FunctionDecl *getStructor(const FunctionDecl *fn) {
68 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
69 return ftd->getTemplatedDecl();
70
71 return fn;
72}
73
Guy Benyei11169dd2012-12-18 14:30:41 +000074/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
75/// Microsoft Visual C++ ABI.
76class MicrosoftCXXNameMangler {
77 MangleContext &Context;
78 raw_ostream &Out;
79
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +000080 /// The "structor" is the top-level declaration being mangled, if
81 /// that's not a template specialization; otherwise it's the pattern
82 /// for that specialization.
83 const NamedDecl *Structor;
84 unsigned StructorType;
85
Reid Kleckner7dafb232013-05-22 17:16:39 +000086 typedef llvm::StringMap<unsigned> BackRefMap;
Guy Benyei11169dd2012-12-18 14:30:41 +000087 BackRefMap NameBackReferences;
88 bool UseNameBackReferences;
89
90 typedef llvm::DenseMap<void*, unsigned> ArgBackRefMap;
91 ArgBackRefMap TypeBackReferences;
92
93 ASTContext &getASTContext() const { return Context.getASTContext(); }
94
Reid Kleckner369f3162013-05-14 20:30:42 +000095 // FIXME: If we add support for __ptr32/64 qualifiers, then we should push
96 // this check into mangleQualifiers().
97 const bool PointersAre64Bit;
98
Guy Benyei11169dd2012-12-18 14:30:41 +000099public:
Peter Collingbourne2816c022013-04-25 04:25:40 +0000100 enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result };
101
Guy Benyei11169dd2012-12-18 14:30:41 +0000102 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_)
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000103 : Context(C), Out(Out_),
104 Structor(0), StructorType(-1),
David Blaikie014399c2013-05-14 21:31:46 +0000105 UseNameBackReferences(true),
Reid Kleckner369f3162013-05-14 20:30:42 +0000106 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikie014399c2013-05-14 21:31:46 +0000107 64) { }
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000108
109 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_,
110 const CXXDestructorDecl *D, CXXDtorType Type)
111 : Context(C), Out(Out_),
112 Structor(getStructor(D)), StructorType(Type),
David Blaikie014399c2013-05-14 21:31:46 +0000113 UseNameBackReferences(true),
Reid Kleckner369f3162013-05-14 20:30:42 +0000114 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikie014399c2013-05-14 21:31:46 +0000115 64) { }
Guy Benyei11169dd2012-12-18 14:30:41 +0000116
117 raw_ostream &getStream() const { return Out; }
118
119 void mangle(const NamedDecl *D, StringRef Prefix = "\01?");
120 void mangleName(const NamedDecl *ND);
David Majnemer8eaab6f2013-08-13 06:32:20 +0000121 void mangleDeclaration(const NamedDecl *ND);
Guy Benyei11169dd2012-12-18 14:30:41 +0000122 void mangleFunctionEncoding(const FunctionDecl *FD);
123 void mangleVariableEncoding(const VarDecl *VD);
David Majnemer2a816452013-12-09 10:44:32 +0000124 void mangleNumber(int64_t Number);
Peter Collingbourne2816c022013-04-25 04:25:40 +0000125 void mangleType(QualType T, SourceRange Range,
126 QualifierMangleMode QMM = QMM_Mangle);
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +0000127 void mangleFunctionType(const FunctionType *T, const FunctionDecl *D = 0,
128 bool ForceInstMethod = false);
Reid Klecknerd8110b62013-09-10 20:14:30 +0000129 void manglePostfix(const DeclContext *DC, bool NoFunction = false);
Guy Benyei11169dd2012-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);
David Majnemer956bc112013-11-25 17:50:19 +0000137 void mangleSourceName(StringRef Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000138 void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc);
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000139 void mangleCXXDtorType(CXXDtorType T);
Guy Benyei11169dd2012-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 Kleckner52518862013-03-20 01:40:23 +0000145 const TemplateArgumentList &TemplateArgs);
Guy Benyei11169dd2012-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
David Majnemerb4119f72013-12-13 01:06:04 +0000160
David Majnemer0db0ca42013-08-05 22:26:46 +0000161 void mangleType(const TagDecl *TD);
David Majnemer5a1b2042013-09-11 04:44:30 +0000162 void mangleDecayedArrayType(const ArrayType *T);
Reid Kleckner18da98e2013-06-24 19:21:52 +0000163 void mangleArrayType(const ArrayType *T);
Guy Benyei11169dd2012-12-18 14:30:41 +0000164 void mangleFunctionClass(const FunctionDecl *FD);
Reid Klecknerc5cc3382013-09-25 22:28:52 +0000165 void mangleCallingConvention(const FunctionType *T);
Guy Benyei11169dd2012-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 Kleckner52518862013-03-20 01:40:23 +0000170 void mangleTemplateArgs(const TemplateDecl *TD,
171 const TemplateArgumentList &TemplateArgs);
David Majnemer08177c52013-08-27 08:21:25 +0000172 void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA);
Guy Benyei11169dd2012-12-18 14:30:41 +0000173};
174
Timur Iskhodzhanov67455222013-10-03 06:26:13 +0000175/// MicrosoftMangleContextImpl - Overrides the default MangleContext for the
Guy Benyei11169dd2012-12-18 14:30:41 +0000176/// Microsoft Visual C++ ABI.
Timur Iskhodzhanov67455222013-10-03 06:26:13 +0000177class MicrosoftMangleContextImpl : public MicrosoftMangleContext {
Guy Benyei11169dd2012-12-18 14:30:41 +0000178public:
Timur Iskhodzhanov67455222013-10-03 06:26:13 +0000179 MicrosoftMangleContextImpl(ASTContext &Context, DiagnosticsEngine &Diags)
180 : MicrosoftMangleContext(Context, Diags) {}
Rafael Espindola002667c2013-10-16 01:40:34 +0000181 virtual bool shouldMangleCXXName(const NamedDecl *D);
182 virtual void mangleCXXName(const NamedDecl *D, raw_ostream &Out);
Hans Wennborg88497d62013-11-15 17:24:45 +0000183 virtual void mangleVirtualMemPtrThunk(const CXXMethodDecl *MD,
David Majnemer2a816452013-12-09 10:44:32 +0000184 uint64_t OffsetInVFTable,
185 raw_ostream &);
Guy Benyei11169dd2012-12-18 14:30:41 +0000186 virtual void mangleThunk(const CXXMethodDecl *MD,
187 const ThunkInfo &Thunk,
188 raw_ostream &);
189 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
190 const ThisAdjustment &ThisAdjustment,
191 raw_ostream &);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000192 virtual void mangleCXXVFTable(const CXXRecordDecl *Derived,
193 ArrayRef<const CXXRecordDecl *> BasePath,
194 raw_ostream &Out);
Reid Kleckner7810af02013-06-19 15:20:38 +0000195 virtual void mangleCXXVBTable(const CXXRecordDecl *Derived,
196 ArrayRef<const CXXRecordDecl *> BasePath,
197 raw_ostream &Out);
Guy Benyei11169dd2012-12-18 14:30:41 +0000198 virtual void mangleCXXRTTI(QualType T, raw_ostream &);
199 virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
Reid Klecknercc99e262013-11-19 23:23:00 +0000200 virtual void mangleTypeName(QualType T, raw_ostream &);
Guy Benyei11169dd2012-12-18 14:30:41 +0000201 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
202 raw_ostream &);
203 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
204 raw_ostream &);
Reid Klecknerd8110b62013-09-10 20:14:30 +0000205 virtual void mangleReferenceTemporary(const VarDecl *, raw_ostream &);
206 virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out);
Reid Kleckner1ece9fc2013-09-10 20:43:12 +0000207 virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out);
Reid Klecknerd8110b62013-09-10 20:14:30 +0000208 virtual void mangleDynamicAtExitDestructor(const VarDecl *D,
209 raw_ostream &Out);
Reid Kleckner1ece9fc2013-09-10 20:43:12 +0000210
211private:
212 void mangleInitFiniStub(const VarDecl *D, raw_ostream &Out, char CharCode);
Guy Benyei11169dd2012-12-18 14:30:41 +0000213};
214
215}
216
Rafael Espindola002667c2013-10-16 01:40:34 +0000217bool MicrosoftMangleContextImpl::shouldMangleCXXName(const NamedDecl *D) {
David Majnemerd5a42b82013-09-13 09:03:14 +0000218 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
219 LanguageLinkage L = FD->getLanguageLinkage();
220 // Overloadable functions need mangling.
221 if (FD->hasAttr<OverloadableAttr>())
222 return true;
223
David Majnemerc729b0b2013-09-16 22:44:20 +0000224 // The ABI expects that we would never mangle "typical" user-defined entry
225 // points regardless of visibility or freestanding-ness.
226 //
227 // N.B. This is distinct from asking about "main". "main" has a lot of
228 // special rules associated with it in the standard while these
229 // user-defined entry points are outside of the purview of the standard.
230 // For example, there can be only one definition for "main" in a standards
231 // compliant program; however nothing forbids the existence of wmain and
232 // WinMain in the same translation unit.
233 if (FD->isMSVCRTEntryPoint())
David Majnemerd5a42b82013-09-13 09:03:14 +0000234 return false;
235
236 // C++ functions and those whose names are not a simple identifier need
237 // mangling.
238 if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage)
239 return true;
240
241 // C functions are not mangled.
242 if (L == CLanguageLinkage)
243 return false;
244 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000245
246 // Otherwise, no mangling is done outside C++ mode.
247 if (!getASTContext().getLangOpts().CPlusPlus)
248 return false;
249
David Majnemerd5a42b82013-09-13 09:03:14 +0000250 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
251 // C variables are not mangled.
252 if (VD->isExternC())
253 return false;
254
255 // Variables at global scope with non-internal linkage are not mangled.
256 const DeclContext *DC = getEffectiveDeclContext(D);
257 // Check for extern variable declared locally.
258 if (DC->isFunctionOrMethod() && D->hasLinkage())
259 while (!DC->isNamespace() && !DC->isTranslationUnit())
260 DC = getEffectiveParentContext(DC);
261
262 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage &&
263 !isa<VarTemplateSpecializationDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +0000264 return false;
265 }
266
Guy Benyei11169dd2012-12-18 14:30:41 +0000267 return true;
268}
269
270void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
271 StringRef Prefix) {
272 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
273 // Therefore it's really important that we don't decorate the
274 // name with leading underscores or leading/trailing at signs. So, by
275 // default, we emit an asm marker at the start so we get the name right.
276 // Callers can override this with a custom prefix.
277
Guy Benyei11169dd2012-12-18 14:30:41 +0000278 // <mangled-name> ::= ? <name> <type-encoding>
279 Out << Prefix;
280 mangleName(D);
281 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
282 mangleFunctionEncoding(FD);
283 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
284 mangleVariableEncoding(VD);
285 else {
286 // TODO: Fields? Can MSVC even mangle them?
287 // Issue a diagnostic for now.
288 DiagnosticsEngine &Diags = Context.getDiags();
289 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
290 "cannot mangle this declaration yet");
291 Diags.Report(D->getLocation(), DiagID)
292 << D->getSourceRange();
293 }
294}
295
296void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
297 // <type-encoding> ::= <function-class> <function-type>
298
Reid Kleckner18da98e2013-06-24 19:21:52 +0000299 // Since MSVC operates on the type as written and not the canonical type, it
300 // actually matters which decl we have here. MSVC appears to choose the
301 // first, since it is most likely to be the declaration in a header file.
Rafael Espindola8db352d2013-10-17 15:37:26 +0000302 FD = FD->getFirstDecl();
Reid Kleckner18da98e2013-06-24 19:21:52 +0000303
Guy Benyei11169dd2012-12-18 14:30:41 +0000304 // We should never ever see a FunctionNoProtoType at this point.
305 // We don't even know how to mangle their types anyway :).
Reid Kleckner9a7f3e62013-10-08 00:58:57 +0000306 const FunctionProtoType *FT = FD->getType()->castAs<FunctionProtoType>();
Guy Benyei11169dd2012-12-18 14:30:41 +0000307
David Majnemerd5a42b82013-09-13 09:03:14 +0000308 // extern "C" functions can hold entities that must be mangled.
309 // As it stands, these functions still need to get expressed in the full
310 // external name. They have their class and type omitted, replaced with '9'.
311 if (Context.shouldMangleDeclName(FD)) {
312 // First, the function class.
313 mangleFunctionClass(FD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000314
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +0000315 mangleFunctionType(FT, FD);
David Majnemerd5a42b82013-09-13 09:03:14 +0000316 } else
317 Out << '9';
Guy Benyei11169dd2012-12-18 14:30:41 +0000318}
319
320void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
321 // <type-encoding> ::= <storage-class> <variable-type>
322 // <storage-class> ::= 0 # private static member
323 // ::= 1 # protected static member
324 // ::= 2 # public static member
325 // ::= 3 # global
326 // ::= 4 # static local
David Majnemerb4119f72013-12-13 01:06:04 +0000327
Guy Benyei11169dd2012-12-18 14:30:41 +0000328 // The first character in the encoding (after the name) is the storage class.
329 if (VD->isStaticDataMember()) {
330 // If it's a static member, it also encodes the access level.
331 switch (VD->getAccess()) {
332 default:
333 case AS_private: Out << '0'; break;
334 case AS_protected: Out << '1'; break;
335 case AS_public: Out << '2'; break;
336 }
337 }
338 else if (!VD->isStaticLocal())
339 Out << '3';
340 else
341 Out << '4';
342 // Now mangle the type.
343 // <variable-type> ::= <type> <cvr-qualifiers>
344 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
345 // Pointers and references are odd. The type of 'int * const foo;' gets
346 // mangled as 'QAHA' instead of 'PAHB', for example.
347 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
David Majnemerb9a5f2d2014-01-21 20:33:36 +0000348 QualType Ty = VD->getType();
David Majnemer6dda7bb2013-08-15 08:13:23 +0000349 if (Ty->isPointerType() || Ty->isReferenceType() ||
350 Ty->isMemberPointerType()) {
David Majnemera2724ae2013-08-09 05:56:24 +0000351 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer6dda7bb2013-08-15 08:13:23 +0000352 if (PointersAre64Bit)
353 Out << 'E';
354 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) {
355 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true);
356 // Member pointers are suffixed with a back reference to the member
357 // pointer's class name.
358 mangleName(MPT->getClass()->getAsCXXRecordDecl());
359 } else
360 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
David Majnemera2724ae2013-08-09 05:56:24 +0000361 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000362 // Global arrays are funny, too.
David Majnemer5a1b2042013-09-11 04:44:30 +0000363 mangleDecayedArrayType(AT);
Peter Collingbourne2816c022013-04-25 04:25:40 +0000364 if (AT->getElementType()->isArrayType())
365 Out << 'A';
366 else
367 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei11169dd2012-12-18 14:30:41 +0000368 } else {
Peter Collingbourne2816c022013-04-25 04:25:40 +0000369 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer6dda7bb2013-08-15 08:13:23 +0000370 mangleQualifiers(Ty.getLocalQualifiers(), false);
Guy Benyei11169dd2012-12-18 14:30:41 +0000371 }
372}
373
374void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
375 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
376 const DeclContext *DC = ND->getDeclContext();
377
378 // Always start with the unqualified name.
David Majnemerb4119f72013-12-13 01:06:04 +0000379 mangleUnqualifiedName(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +0000380
381 // If this is an extern variable declared locally, the relevant DeclContext
382 // is that of the containing namespace, or the translation unit.
383 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
384 while (!DC->isNamespace() && !DC->isTranslationUnit())
385 DC = DC->getParent();
386
387 manglePostfix(DC);
388
389 // Terminate the whole name with an '@'.
390 Out << '@';
391}
392
David Majnemer2a816452013-12-09 10:44:32 +0000393void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
394 // <non-negative integer> ::= A@ # when Number == 0
395 // ::= <decimal digit> # when 1 <= Number <= 10
396 // ::= <hex digit>+ @ # when Number >= 10
397 //
398 // <number> ::= [?] <non-negative integer>
Guy Benyei11169dd2012-12-18 14:30:41 +0000399
David Majnemer2a816452013-12-09 10:44:32 +0000400 uint64_t Value = static_cast<uint64_t>(Number);
401 if (Number < 0) {
402 Value = -Value;
Guy Benyei11169dd2012-12-18 14:30:41 +0000403 Out << '?';
Guy Benyei11169dd2012-12-18 14:30:41 +0000404 }
David Majnemer2a816452013-12-09 10:44:32 +0000405
406 if (Value == 0)
407 Out << "A@";
408 else if (Value >= 1 && Value <= 10)
409 Out << (Value - 1);
410 else {
411 // Numbers that are not encoded as decimal digits are represented as nibbles
412 // in the range of ASCII characters 'A' to 'P'.
413 // The number 0x123450 would be encoded as 'BCDEFA'
414 char EncodedNumberBuffer[sizeof(uint64_t) * 2];
415 llvm::MutableArrayRef<char> BufferRef(EncodedNumberBuffer);
416 llvm::MutableArrayRef<char>::reverse_iterator I = BufferRef.rbegin();
417 for (; Value != 0; Value >>= 4)
418 *I++ = 'A' + (Value & 0xf);
419 Out.write(I.base(), I - BufferRef.rbegin());
Guy Benyei11169dd2012-12-18 14:30:41 +0000420 Out << '@';
421 }
422}
423
424static const TemplateDecl *
Reid Kleckner52518862013-03-20 01:40:23 +0000425isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000426 // Check if we have a function template.
427 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
428 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Kleckner52518862013-03-20 01:40:23 +0000429 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei11169dd2012-12-18 14:30:41 +0000430 return TD;
431 }
432 }
433
434 // Check if we have a class template.
435 if (const ClassTemplateSpecializationDecl *Spec =
Reid Kleckner52518862013-03-20 01:40:23 +0000436 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
437 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei11169dd2012-12-18 14:30:41 +0000438 return Spec->getSpecializedTemplate();
439 }
440
441 return 0;
442}
443
444void
445MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
446 DeclarationName Name) {
447 // <unqualified-name> ::= <operator-name>
448 // ::= <ctor-dtor-name>
449 // ::= <source-name>
450 // ::= <template-name>
Reid Kleckner52518862013-03-20 01:40:23 +0000451
Guy Benyei11169dd2012-12-18 14:30:41 +0000452 // Check if we have a template.
Reid Kleckner52518862013-03-20 01:40:23 +0000453 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000454 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Klecknerc16c4472013-07-13 00:43:39 +0000455 // Function templates aren't considered for name back referencing. This
456 // makes sense since function templates aren't likely to occur multiple
457 // times in a symbol.
458 // FIXME: Test alias template mangling with MSVC 2013.
459 if (!isa<ClassTemplateDecl>(TD)) {
460 mangleTemplateInstantiationName(TD, *TemplateArgs);
461 return;
462 }
463
464 // We have a class template.
Guy Benyei11169dd2012-12-18 14:30:41 +0000465 // Here comes the tricky thing: if we need to mangle something like
466 // void foo(A::X<Y>, B::X<Y>),
467 // the X<Y> part is aliased. However, if you need to mangle
468 // void foo(A::X<A::Y>, A::X<B::Y>),
469 // the A::X<> part is not aliased.
470 // That said, from the mangler's perspective we have a structure like this:
471 // namespace[s] -> type[ -> template-parameters]
472 // but from the Clang perspective we have
473 // type [ -> template-parameters]
474 // \-> namespace[s]
475 // What we do is we create a new mangler, mangle the same type (without
476 // a namespace suffix) using the extra mangler with back references
477 // disabled (to avoid infinite recursion) and then use the mangled type
478 // name as a key to check the mangling of different types for aliasing.
479
480 std::string BackReferenceKey;
481 BackRefMap::iterator Found;
482 if (UseNameBackReferences) {
483 llvm::raw_string_ostream Stream(BackReferenceKey);
484 MicrosoftCXXNameMangler Extra(Context, Stream);
485 Extra.disableBackReferences();
486 Extra.mangleUnqualifiedName(ND, Name);
487 Stream.flush();
488
489 Found = NameBackReferences.find(BackReferenceKey);
490 }
491 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Kleckner52518862013-03-20 01:40:23 +0000492 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei11169dd2012-12-18 14:30:41 +0000493 if (UseNameBackReferences && NameBackReferences.size() < 10) {
494 size_t Size = NameBackReferences.size();
495 NameBackReferences[BackReferenceKey] = Size;
496 }
497 } else {
498 Out << Found->second;
499 }
500 return;
501 }
502
503 switch (Name.getNameKind()) {
504 case DeclarationName::Identifier: {
505 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
David Majnemer956bc112013-11-25 17:50:19 +0000506 mangleSourceName(II->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +0000507 break;
508 }
David Majnemerb4119f72013-12-13 01:06:04 +0000509
Guy Benyei11169dd2012-12-18 14:30:41 +0000510 // Otherwise, an anonymous entity. We must have a declaration.
511 assert(ND && "mangling empty name without declaration");
David Majnemerb4119f72013-12-13 01:06:04 +0000512
Guy Benyei11169dd2012-12-18 14:30:41 +0000513 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
514 if (NS->isAnonymousNamespace()) {
515 Out << "?A@";
516 break;
517 }
518 }
David Majnemerb4119f72013-12-13 01:06:04 +0000519
Guy Benyei11169dd2012-12-18 14:30:41 +0000520 // We must have an anonymous struct.
521 const TagDecl *TD = cast<TagDecl>(ND);
522 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
523 assert(TD->getDeclContext() == D->getDeclContext() &&
524 "Typedef should not be in another decl context!");
525 assert(D->getDeclName().getAsIdentifierInfo() &&
526 "Typedef was not named!");
David Majnemer956bc112013-11-25 17:50:19 +0000527 mangleSourceName(D->getDeclName().getAsIdentifierInfo()->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +0000528 break;
529 }
530
David Majnemer956bc112013-11-25 17:50:19 +0000531 if (TD->hasDeclaratorForAnonDecl()) {
David Majnemer50ce8352013-09-17 23:57:10 +0000532 // Anonymous types with no tag or typedef get the name of their
533 // declarator mangled in.
David Majnemer956bc112013-11-25 17:50:19 +0000534 llvm::SmallString<64> Name("<unnamed-type-");
535 Name += TD->getDeclaratorForAnonDecl()->getName();
536 Name += ">";
537 mangleSourceName(Name.str());
538 } else {
David Majnemer50ce8352013-09-17 23:57:10 +0000539 // Anonymous types with no tag, no typedef, or declarator get
David Majnemer956bc112013-11-25 17:50:19 +0000540 // '<unnamed-tag>'.
541 mangleSourceName("<unnamed-tag>");
542 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000543 break;
544 }
David Majnemerb4119f72013-12-13 01:06:04 +0000545
Guy Benyei11169dd2012-12-18 14:30:41 +0000546 case DeclarationName::ObjCZeroArgSelector:
547 case DeclarationName::ObjCOneArgSelector:
548 case DeclarationName::ObjCMultiArgSelector:
549 llvm_unreachable("Can't mangle Objective-C selector names here!");
David Majnemerb4119f72013-12-13 01:06:04 +0000550
Guy Benyei11169dd2012-12-18 14:30:41 +0000551 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000552 if (ND == Structor) {
553 assert(StructorType == Ctor_Complete &&
554 "Should never be asked to mangle a ctor other than complete");
555 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000556 Out << "?0";
557 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000558
Guy Benyei11169dd2012-12-18 14:30:41 +0000559 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000560 if (ND == Structor)
561 // If the named decl is the C++ destructor we're mangling,
562 // use the type we were given.
563 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
564 else
Reid Klecknere7de47e2013-07-22 13:51:44 +0000565 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000566 // class with a destructor is declared within a destructor.
Reid Klecknere7de47e2013-07-22 13:51:44 +0000567 mangleCXXDtorType(Dtor_Base);
Guy Benyei11169dd2012-12-18 14:30:41 +0000568 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000569
Guy Benyei11169dd2012-12-18 14:30:41 +0000570 case DeclarationName::CXXConversionFunctionName:
571 // <operator-name> ::= ?B # (cast)
572 // The target type is encoded as the return type.
573 Out << "?B";
574 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000575
Guy Benyei11169dd2012-12-18 14:30:41 +0000576 case DeclarationName::CXXOperatorName:
577 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
578 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000579
Guy Benyei11169dd2012-12-18 14:30:41 +0000580 case DeclarationName::CXXLiteralOperatorName: {
581 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
582 DiagnosticsEngine Diags = Context.getDiags();
583 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
584 "cannot mangle this literal operator yet");
585 Diags.Report(ND->getLocation(), DiagID);
586 break;
587 }
David Majnemerb4119f72013-12-13 01:06:04 +0000588
Guy Benyei11169dd2012-12-18 14:30:41 +0000589 case DeclarationName::CXXUsingDirective:
590 llvm_unreachable("Can't mangle a using directive name!");
591 }
592}
593
594void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
595 bool NoFunction) {
596 // <postfix> ::= <unqualified-name> [<postfix>]
597 // ::= <substitution> [<postfix>]
598
599 if (!DC) return;
600
601 while (isa<LinkageSpecDecl>(DC))
602 DC = DC->getParent();
603
604 if (DC->isTranslationUnit())
605 return;
606
607 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedman8978a9d2013-07-10 01:13:27 +0000608 DiagnosticsEngine Diags = Context.getDiags();
609 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
610 "cannot mangle a local inside this block yet");
611 Diags.Report(BD->getLocation(), DiagID);
612
613 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
614 // for how this should be done.
615 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei11169dd2012-12-18 14:30:41 +0000616 Out << '@';
617 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir3b4c30b2013-05-09 19:17:11 +0000618 } else if (isa<CapturedDecl>(DC)) {
619 // Skip CapturedDecl context.
620 manglePostfix(DC->getParent(), NoFunction);
621 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000622 }
623
624 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
625 return;
626 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
627 mangleObjCMethodName(Method);
628 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
629 mangleLocalName(Func);
630 else {
631 mangleUnqualifiedName(cast<NamedDecl>(DC));
632 manglePostfix(DC->getParent(), NoFunction);
633 }
634}
635
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000636void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknere7de47e2013-07-22 13:51:44 +0000637 // Microsoft uses the names on the case labels for these dtor variants. Clang
638 // uses the Itanium terminology internally. Everything in this ABI delegates
639 // towards the base dtor.
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000640 switch (T) {
Reid Klecknere7de47e2013-07-22 13:51:44 +0000641 // <operator-name> ::= ?1 # destructor
642 case Dtor_Base: Out << "?1"; return;
643 // <operator-name> ::= ?_D # vbase destructor
644 case Dtor_Complete: Out << "?_D"; return;
645 // <operator-name> ::= ?_G # scalar deleting destructor
646 case Dtor_Deleting: Out << "?_G"; return;
647 // <operator-name> ::= ?_E # vector deleting destructor
648 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
649 // it.
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000650 }
651 llvm_unreachable("Unsupported dtor type?");
652}
653
Guy Benyei11169dd2012-12-18 14:30:41 +0000654void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
655 SourceLocation Loc) {
656 switch (OO) {
657 // ?0 # constructor
658 // ?1 # destructor
659 // <operator-name> ::= ?2 # new
660 case OO_New: Out << "?2"; break;
661 // <operator-name> ::= ?3 # delete
662 case OO_Delete: Out << "?3"; break;
663 // <operator-name> ::= ?4 # =
664 case OO_Equal: Out << "?4"; break;
665 // <operator-name> ::= ?5 # >>
666 case OO_GreaterGreater: Out << "?5"; break;
667 // <operator-name> ::= ?6 # <<
668 case OO_LessLess: Out << "?6"; break;
669 // <operator-name> ::= ?7 # !
670 case OO_Exclaim: Out << "?7"; break;
671 // <operator-name> ::= ?8 # ==
672 case OO_EqualEqual: Out << "?8"; break;
673 // <operator-name> ::= ?9 # !=
674 case OO_ExclaimEqual: Out << "?9"; break;
675 // <operator-name> ::= ?A # []
676 case OO_Subscript: Out << "?A"; break;
677 // ?B # conversion
678 // <operator-name> ::= ?C # ->
679 case OO_Arrow: Out << "?C"; break;
680 // <operator-name> ::= ?D # *
681 case OO_Star: Out << "?D"; break;
682 // <operator-name> ::= ?E # ++
683 case OO_PlusPlus: Out << "?E"; break;
684 // <operator-name> ::= ?F # --
685 case OO_MinusMinus: Out << "?F"; break;
686 // <operator-name> ::= ?G # -
687 case OO_Minus: Out << "?G"; break;
688 // <operator-name> ::= ?H # +
689 case OO_Plus: Out << "?H"; break;
690 // <operator-name> ::= ?I # &
691 case OO_Amp: Out << "?I"; break;
692 // <operator-name> ::= ?J # ->*
693 case OO_ArrowStar: Out << "?J"; break;
694 // <operator-name> ::= ?K # /
695 case OO_Slash: Out << "?K"; break;
696 // <operator-name> ::= ?L # %
697 case OO_Percent: Out << "?L"; break;
698 // <operator-name> ::= ?M # <
699 case OO_Less: Out << "?M"; break;
700 // <operator-name> ::= ?N # <=
701 case OO_LessEqual: Out << "?N"; break;
702 // <operator-name> ::= ?O # >
703 case OO_Greater: Out << "?O"; break;
704 // <operator-name> ::= ?P # >=
705 case OO_GreaterEqual: Out << "?P"; break;
706 // <operator-name> ::= ?Q # ,
707 case OO_Comma: Out << "?Q"; break;
708 // <operator-name> ::= ?R # ()
709 case OO_Call: Out << "?R"; break;
710 // <operator-name> ::= ?S # ~
711 case OO_Tilde: Out << "?S"; break;
712 // <operator-name> ::= ?T # ^
713 case OO_Caret: Out << "?T"; break;
714 // <operator-name> ::= ?U # |
715 case OO_Pipe: Out << "?U"; break;
716 // <operator-name> ::= ?V # &&
717 case OO_AmpAmp: Out << "?V"; break;
718 // <operator-name> ::= ?W # ||
719 case OO_PipePipe: Out << "?W"; break;
720 // <operator-name> ::= ?X # *=
721 case OO_StarEqual: Out << "?X"; break;
722 // <operator-name> ::= ?Y # +=
723 case OO_PlusEqual: Out << "?Y"; break;
724 // <operator-name> ::= ?Z # -=
725 case OO_MinusEqual: Out << "?Z"; break;
726 // <operator-name> ::= ?_0 # /=
727 case OO_SlashEqual: Out << "?_0"; break;
728 // <operator-name> ::= ?_1 # %=
729 case OO_PercentEqual: Out << "?_1"; break;
730 // <operator-name> ::= ?_2 # >>=
731 case OO_GreaterGreaterEqual: Out << "?_2"; break;
732 // <operator-name> ::= ?_3 # <<=
733 case OO_LessLessEqual: Out << "?_3"; break;
734 // <operator-name> ::= ?_4 # &=
735 case OO_AmpEqual: Out << "?_4"; break;
736 // <operator-name> ::= ?_5 # |=
737 case OO_PipeEqual: Out << "?_5"; break;
738 // <operator-name> ::= ?_6 # ^=
739 case OO_CaretEqual: Out << "?_6"; break;
740 // ?_7 # vftable
741 // ?_8 # vbtable
742 // ?_9 # vcall
743 // ?_A # typeof
744 // ?_B # local static guard
745 // ?_C # string
746 // ?_D # vbase destructor
747 // ?_E # vector deleting destructor
748 // ?_F # default constructor closure
749 // ?_G # scalar deleting destructor
750 // ?_H # vector constructor iterator
751 // ?_I # vector destructor iterator
752 // ?_J # vector vbase constructor iterator
753 // ?_K # virtual displacement map
754 // ?_L # eh vector constructor iterator
755 // ?_M # eh vector destructor iterator
756 // ?_N # eh vector vbase constructor iterator
757 // ?_O # copy constructor closure
758 // ?_P<name> # udt returning <name>
759 // ?_Q # <unknown>
760 // ?_R0 # RTTI Type Descriptor
761 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
762 // ?_R2 # RTTI Base Class Array
763 // ?_R3 # RTTI Class Hierarchy Descriptor
764 // ?_R4 # RTTI Complete Object Locator
765 // ?_S # local vftable
766 // ?_T # local vftable constructor closure
767 // <operator-name> ::= ?_U # new[]
768 case OO_Array_New: Out << "?_U"; break;
769 // <operator-name> ::= ?_V # delete[]
770 case OO_Array_Delete: Out << "?_V"; break;
David Majnemerb4119f72013-12-13 01:06:04 +0000771
Guy Benyei11169dd2012-12-18 14:30:41 +0000772 case OO_Conditional: {
773 DiagnosticsEngine &Diags = Context.getDiags();
774 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
775 "cannot mangle this conditional operator yet");
776 Diags.Report(Loc, DiagID);
777 break;
778 }
David Majnemerb4119f72013-12-13 01:06:04 +0000779
Guy Benyei11169dd2012-12-18 14:30:41 +0000780 case OO_None:
781 case NUM_OVERLOADED_OPERATORS:
782 llvm_unreachable("Not an overloaded operator");
783 }
784}
785
David Majnemer956bc112013-11-25 17:50:19 +0000786void MicrosoftCXXNameMangler::mangleSourceName(StringRef Name) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000787 // <source name> ::= <identifier> @
Guy Benyei11169dd2012-12-18 14:30:41 +0000788 BackRefMap::iterator Found;
789 if (UseNameBackReferences)
David Majnemer956bc112013-11-25 17:50:19 +0000790 Found = NameBackReferences.find(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000791 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
David Majnemer956bc112013-11-25 17:50:19 +0000792 Out << Name << '@';
Guy Benyei11169dd2012-12-18 14:30:41 +0000793 if (UseNameBackReferences && NameBackReferences.size() < 10) {
794 size_t Size = NameBackReferences.size();
David Majnemer956bc112013-11-25 17:50:19 +0000795 NameBackReferences[Name] = Size;
Guy Benyei11169dd2012-12-18 14:30:41 +0000796 }
797 } else {
798 Out << Found->second;
799 }
800}
801
802void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
803 Context.mangleObjCMethodName(MD, Out);
804}
805
806// Find out how many function decls live above this one and return an integer
807// suitable for use as the number in a numbered anonymous scope.
808// TODO: Memoize.
809static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
810 const DeclContext *DC = FD->getParent();
811 int level = 1;
812
813 while (DC && !DC->isTranslationUnit()) {
814 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
815 DC = DC->getParent();
816 }
817
818 return 2*level;
819}
820
821void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
822 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
823 // <numbered-anonymous-scope> ::= ? <number>
824 // Even though the name is rendered in reverse order (e.g.
825 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
826 // innermost. So a method bar in class C local to function foo gets mangled
827 // as something like:
828 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
829 // This is more apparent when you have a type nested inside a method of a
830 // type nested inside a function. A method baz in class D local to method
831 // bar of class C local to function foo gets mangled as:
832 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
833 // This scheme is general enough to support GCC-style nested
834 // functions. You could have a method baz of class C inside a function bar
835 // inside a function foo, like so:
836 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +0000837 unsigned NestLevel = getLocalNestingLevel(FD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000838 Out << '?';
839 mangleNumber(NestLevel);
840 Out << '?';
841 mangle(FD, "?");
842}
843
844void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
845 const TemplateDecl *TD,
Reid Kleckner52518862013-03-20 01:40:23 +0000846 const TemplateArgumentList &TemplateArgs) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000847 // <template-name> ::= <unscoped-template-name> <template-args>
848 // ::= <substitution>
849 // Always start with the unqualified name.
850
851 // Templates have their own context for back references.
852 ArgBackRefMap OuterArgsContext;
853 BackRefMap OuterTemplateContext;
854 NameBackReferences.swap(OuterTemplateContext);
855 TypeBackReferences.swap(OuterArgsContext);
856
857 mangleUnscopedTemplateName(TD);
Reid Kleckner52518862013-03-20 01:40:23 +0000858 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei11169dd2012-12-18 14:30:41 +0000859
860 // Restore the previous back reference contexts.
861 NameBackReferences.swap(OuterTemplateContext);
862 TypeBackReferences.swap(OuterArgsContext);
863}
864
865void
866MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
867 // <unscoped-template-name> ::= ?$ <unqualified-name>
868 Out << "?$";
869 mangleUnqualifiedName(TD);
870}
871
872void
873MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
874 bool IsBoolean) {
875 // <integer-literal> ::= $0 <number>
876 Out << "$0";
877 // Make sure booleans are encoded as 0/1.
878 if (IsBoolean && Value.getBoolValue())
879 mangleNumber(1);
880 else
David Majnemer2a816452013-12-09 10:44:32 +0000881 mangleNumber(Value.getSExtValue());
Guy Benyei11169dd2012-12-18 14:30:41 +0000882}
883
884void
885MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
886 // See if this is a constant expression.
887 llvm::APSInt Value;
888 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
889 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
890 return;
891 }
892
David Majnemer8eaab6f2013-08-13 06:32:20 +0000893 const CXXUuidofExpr *UE = 0;
894 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
895 if (UO->getOpcode() == UO_AddrOf)
896 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
897 } else
898 UE = dyn_cast<CXXUuidofExpr>(E);
899
900 if (UE) {
901 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
902 // const __s_GUID _GUID_{lower case UUID with underscores}
903 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
904 std::string Name = "_GUID_" + Uuid.lower();
905 std::replace(Name.begin(), Name.end(), '-', '_');
906
David Majnemere9cab2f2013-08-13 09:17:25 +0000907 // If we had to peek through an address-of operator, treat this like we are
David Majnemer8eaab6f2013-08-13 06:32:20 +0000908 // dealing with a pointer type. Otherwise, treat it like a const reference.
909 //
910 // N.B. This matches up with the handling of TemplateArgument::Declaration
911 // in mangleTemplateArg
912 if (UE == E)
913 Out << "$E?";
914 else
915 Out << "$1?";
916 Out << Name << "@@3U__s_GUID@@B";
917 return;
918 }
919
Guy Benyei11169dd2012-12-18 14:30:41 +0000920 // As bad as this diagnostic is, it's better than crashing.
921 DiagnosticsEngine &Diags = Context.getDiags();
922 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
923 "cannot yet mangle expression type %0");
924 Diags.Report(E->getExprLoc(), DiagID)
925 << E->getStmtClassName() << E->getSourceRange();
926}
927
928void
Reid Kleckner52518862013-03-20 01:40:23 +0000929MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
930 const TemplateArgumentList &TemplateArgs) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000931 // <template-args> ::= {<type> | <integer-literal>}+ @
932 unsigned NumTemplateArgs = TemplateArgs.size();
933 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Kleckner52518862013-03-20 01:40:23 +0000934 const TemplateArgument &TA = TemplateArgs[i];
David Majnemer08177c52013-08-27 08:21:25 +0000935 mangleTemplateArg(TD, TA);
Guy Benyei11169dd2012-12-18 14:30:41 +0000936 }
937 Out << '@';
938}
939
Reid Klecknerf0ae35b2013-07-02 18:10:07 +0000940void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
David Majnemer08177c52013-08-27 08:21:25 +0000941 const TemplateArgument &TA) {
Reid Klecknerf0ae35b2013-07-02 18:10:07 +0000942 switch (TA.getKind()) {
943 case TemplateArgument::Null:
944 llvm_unreachable("Can't mangle null template arguments!");
David Majnemer08177c52013-08-27 08:21:25 +0000945 case TemplateArgument::TemplateExpansion:
946 llvm_unreachable("Can't mangle template expansion arguments!");
Reid Klecknerf0ae35b2013-07-02 18:10:07 +0000947 case TemplateArgument::Type: {
948 QualType T = TA.getAsType();
949 mangleType(T, SourceRange(), QMM_Escape);
950 break;
951 }
David Majnemere8fdc062013-08-13 01:25:35 +0000952 case TemplateArgument::Declaration: {
953 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl());
954 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?");
Reid Klecknerf0ae35b2013-07-02 18:10:07 +0000955 break;
David Majnemere8fdc062013-08-13 01:25:35 +0000956 }
Reid Klecknerf0ae35b2013-07-02 18:10:07 +0000957 case TemplateArgument::Integral:
958 mangleIntegerLiteral(TA.getAsIntegral(),
959 TA.getIntegralType()->isBooleanType());
960 break;
David Majnemerae465ef2013-08-05 21:33:59 +0000961 case TemplateArgument::NullPtr:
962 Out << "$0A@";
963 break;
Reid Klecknerf0ae35b2013-07-02 18:10:07 +0000964 case TemplateArgument::Expression:
965 mangleExpression(TA.getAsExpr());
966 break;
967 case TemplateArgument::Pack:
968 // Unlike Itanium, there is no character code to indicate an argument pack.
Reid Klecknerf0ae35b2013-07-02 18:10:07 +0000969 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
970 I != E; ++I)
David Majnemer08177c52013-08-27 08:21:25 +0000971 mangleTemplateArg(TD, *I);
Reid Klecknerf0ae35b2013-07-02 18:10:07 +0000972 break;
973 case TemplateArgument::Template:
David Majnemer0db0ca42013-08-05 22:26:46 +0000974 mangleType(cast<TagDecl>(
975 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
976 break;
Reid Klecknerf0ae35b2013-07-02 18:10:07 +0000977 }
978}
979
Guy Benyei11169dd2012-12-18 14:30:41 +0000980void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
981 bool IsMember) {
982 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
983 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
984 // 'I' means __restrict (32/64-bit).
985 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
986 // keyword!
987 // <base-cvr-qualifiers> ::= A # near
988 // ::= B # near const
989 // ::= C # near volatile
990 // ::= D # near const volatile
991 // ::= E # far (16-bit)
992 // ::= F # far const (16-bit)
993 // ::= G # far volatile (16-bit)
994 // ::= H # far const volatile (16-bit)
995 // ::= I # huge (16-bit)
996 // ::= J # huge const (16-bit)
997 // ::= K # huge volatile (16-bit)
998 // ::= L # huge const volatile (16-bit)
999 // ::= M <basis> # based
1000 // ::= N <basis> # based const
1001 // ::= O <basis> # based volatile
1002 // ::= P <basis> # based const volatile
1003 // ::= Q # near member
1004 // ::= R # near const member
1005 // ::= S # near volatile member
1006 // ::= T # near const volatile member
1007 // ::= U # far member (16-bit)
1008 // ::= V # far const member (16-bit)
1009 // ::= W # far volatile member (16-bit)
1010 // ::= X # far const volatile member (16-bit)
1011 // ::= Y # huge member (16-bit)
1012 // ::= Z # huge const member (16-bit)
1013 // ::= 0 # huge volatile member (16-bit)
1014 // ::= 1 # huge const volatile member (16-bit)
1015 // ::= 2 <basis> # based member
1016 // ::= 3 <basis> # based const member
1017 // ::= 4 <basis> # based volatile member
1018 // ::= 5 <basis> # based const volatile member
1019 // ::= 6 # near function (pointers only)
1020 // ::= 7 # far function (pointers only)
1021 // ::= 8 # near method (pointers only)
1022 // ::= 9 # far method (pointers only)
1023 // ::= _A <basis> # based function (pointers only)
1024 // ::= _B <basis> # based function (far?) (pointers only)
1025 // ::= _C <basis> # based method (pointers only)
1026 // ::= _D <basis> # based method (far?) (pointers only)
1027 // ::= _E # block (Clang)
1028 // <basis> ::= 0 # __based(void)
1029 // ::= 1 # __based(segment)?
1030 // ::= 2 <name> # __based(name)
1031 // ::= 3 # ?
1032 // ::= 4 # ?
1033 // ::= 5 # not really based
1034 bool HasConst = Quals.hasConst(),
1035 HasVolatile = Quals.hasVolatile();
David Majnemer89594f32013-08-05 22:43:06 +00001036
Guy Benyei11169dd2012-12-18 14:30:41 +00001037 if (!IsMember) {
1038 if (HasConst && HasVolatile) {
1039 Out << 'D';
1040 } else if (HasVolatile) {
1041 Out << 'C';
1042 } else if (HasConst) {
1043 Out << 'B';
1044 } else {
1045 Out << 'A';
1046 }
1047 } else {
1048 if (HasConst && HasVolatile) {
1049 Out << 'T';
1050 } else if (HasVolatile) {
1051 Out << 'S';
1052 } else if (HasConst) {
1053 Out << 'R';
1054 } else {
1055 Out << 'Q';
1056 }
1057 }
1058
1059 // FIXME: For now, just drop all extension qualifiers on the floor.
1060}
1061
1062void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1063 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1064 // ::= Q # const
1065 // ::= R # volatile
1066 // ::= S # const volatile
1067 bool HasConst = Quals.hasConst(),
1068 HasVolatile = Quals.hasVolatile();
1069 if (HasConst && HasVolatile) {
1070 Out << 'S';
1071 } else if (HasVolatile) {
1072 Out << 'R';
1073 } else if (HasConst) {
1074 Out << 'Q';
1075 } else {
1076 Out << 'P';
1077 }
1078}
1079
1080void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1081 SourceRange Range) {
Reid Kleckner18da98e2013-06-24 19:21:52 +00001082 // MSVC will backreference two canonically equivalent types that have slightly
1083 // different manglings when mangled alone.
David Majnemer5a1b2042013-09-11 04:44:30 +00001084
1085 // Decayed types do not match up with non-decayed versions of the same type.
1086 //
1087 // e.g.
1088 // void (*x)(void) will not form a backreference with void x(void)
1089 void *TypePtr;
1090 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1091 TypePtr = DT->getOriginalType().getCanonicalType().getAsOpaquePtr();
1092 // If the original parameter was textually written as an array,
1093 // instead treat the decayed parameter like it's const.
1094 //
1095 // e.g.
1096 // int [] -> int * const
1097 if (DT->getOriginalType()->isArrayType())
1098 T = T.withConst();
1099 } else
1100 TypePtr = T.getCanonicalType().getAsOpaquePtr();
1101
Guy Benyei11169dd2012-12-18 14:30:41 +00001102 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1103
1104 if (Found == TypeBackReferences.end()) {
1105 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1106
David Majnemer5a1b2042013-09-11 04:44:30 +00001107 mangleType(T, Range, QMM_Drop);
Guy Benyei11169dd2012-12-18 14:30:41 +00001108
1109 // See if it's worth creating a back reference.
1110 // Only types longer than 1 character are considered
1111 // and only 10 back references slots are available:
1112 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1113 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1114 size_t Size = TypeBackReferences.size();
1115 TypeBackReferences[TypePtr] = Size;
1116 }
1117 } else {
1118 Out << Found->second;
1119 }
1120}
1121
1122void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourne2816c022013-04-25 04:25:40 +00001123 QualifierMangleMode QMM) {
Reid Kleckner18da98e2013-06-24 19:21:52 +00001124 // Don't use the canonical types. MSVC includes things like 'const' on
1125 // pointer arguments to function pointers that canonicalization strips away.
1126 T = T.getDesugaredType(getASTContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001127 Qualifiers Quals = T.getLocalQualifiers();
Reid Kleckner18da98e2013-06-24 19:21:52 +00001128 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1129 // If there were any Quals, getAsArrayType() pushed them onto the array
1130 // element type.
Peter Collingbourne2816c022013-04-25 04:25:40 +00001131 if (QMM == QMM_Mangle)
1132 Out << 'A';
1133 else if (QMM == QMM_Escape || QMM == QMM_Result)
1134 Out << "$$B";
Reid Kleckner18da98e2013-06-24 19:21:52 +00001135 mangleArrayType(AT);
Peter Collingbourne2816c022013-04-25 04:25:40 +00001136 return;
1137 }
1138
1139 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1140 T->isBlockPointerType();
1141
1142 switch (QMM) {
1143 case QMM_Drop:
1144 break;
1145 case QMM_Mangle:
1146 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1147 Out << '6';
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001148 mangleFunctionType(FT);
Peter Collingbourne2816c022013-04-25 04:25:40 +00001149 return;
1150 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001151 mangleQualifiers(Quals, false);
Peter Collingbourne2816c022013-04-25 04:25:40 +00001152 break;
1153 case QMM_Escape:
1154 if (!IsPointer && Quals) {
1155 Out << "$$C";
1156 mangleQualifiers(Quals, false);
1157 }
1158 break;
1159 case QMM_Result:
1160 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1161 Out << '?';
1162 mangleQualifiers(Quals, false);
1163 }
1164 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001165 }
1166
Peter Collingbourne2816c022013-04-25 04:25:40 +00001167 // We have to mangle these now, while we still have enough information.
1168 if (IsPointer)
1169 manglePointerQualifiers(Quals);
1170 const Type *ty = T.getTypePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +00001171
1172 switch (ty->getTypeClass()) {
1173#define ABSTRACT_TYPE(CLASS, PARENT)
1174#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1175 case Type::CLASS: \
1176 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1177 return;
1178#define TYPE(CLASS, PARENT) \
1179 case Type::CLASS: \
1180 mangleType(cast<CLASS##Type>(ty), Range); \
1181 break;
1182#include "clang/AST/TypeNodes.def"
1183#undef ABSTRACT_TYPE
1184#undef NON_CANONICAL_TYPE
1185#undef TYPE
1186 }
1187}
1188
1189void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1190 SourceRange Range) {
1191 // <type> ::= <builtin-type>
1192 // <builtin-type> ::= X # void
1193 // ::= C # signed char
1194 // ::= D # char
1195 // ::= E # unsigned char
1196 // ::= F # short
1197 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1198 // ::= H # int
1199 // ::= I # unsigned int
1200 // ::= J # long
1201 // ::= K # unsigned long
1202 // L # <none>
1203 // ::= M # float
1204 // ::= N # double
1205 // ::= O # long double (__float80 is mangled differently)
1206 // ::= _J # long long, __int64
1207 // ::= _K # unsigned long long, __int64
1208 // ::= _L # __int128
1209 // ::= _M # unsigned __int128
1210 // ::= _N # bool
1211 // _O # <array in parameter>
1212 // ::= _T # __float80 (Intel)
1213 // ::= _W # wchar_t
1214 // ::= _Z # __float80 (Digital Mars)
1215 switch (T->getKind()) {
1216 case BuiltinType::Void: Out << 'X'; break;
1217 case BuiltinType::SChar: Out << 'C'; break;
1218 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1219 case BuiltinType::UChar: Out << 'E'; break;
1220 case BuiltinType::Short: Out << 'F'; break;
1221 case BuiltinType::UShort: Out << 'G'; break;
1222 case BuiltinType::Int: Out << 'H'; break;
1223 case BuiltinType::UInt: Out << 'I'; break;
1224 case BuiltinType::Long: Out << 'J'; break;
1225 case BuiltinType::ULong: Out << 'K'; break;
1226 case BuiltinType::Float: Out << 'M'; break;
1227 case BuiltinType::Double: Out << 'N'; break;
1228 // TODO: Determine size and mangle accordingly
1229 case BuiltinType::LongDouble: Out << 'O'; break;
1230 case BuiltinType::LongLong: Out << "_J"; break;
1231 case BuiltinType::ULongLong: Out << "_K"; break;
1232 case BuiltinType::Int128: Out << "_L"; break;
1233 case BuiltinType::UInt128: Out << "_M"; break;
1234 case BuiltinType::Bool: Out << "_N"; break;
1235 case BuiltinType::WChar_S:
1236 case BuiltinType::WChar_U: Out << "_W"; break;
1237
1238#define BUILTIN_TYPE(Id, SingletonId)
1239#define PLACEHOLDER_TYPE(Id, SingletonId) \
1240 case BuiltinType::Id:
1241#include "clang/AST/BuiltinTypes.def"
1242 case BuiltinType::Dependent:
1243 llvm_unreachable("placeholder types shouldn't get to name mangling");
1244
1245 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1246 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1247 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00001248
1249 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1250 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1251 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1252 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1253 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1254 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei61054192013-02-07 10:55:47 +00001255 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001256 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
David Majnemerb4119f72013-12-13 01:06:04 +00001257
Guy Benyei11169dd2012-12-18 14:30:41 +00001258 case BuiltinType::NullPtr: Out << "$$T"; break;
1259
1260 case BuiltinType::Char16:
1261 case BuiltinType::Char32:
1262 case BuiltinType::Half: {
1263 DiagnosticsEngine &Diags = Context.getDiags();
1264 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1265 "cannot mangle this built-in %0 type yet");
1266 Diags.Report(Range.getBegin(), DiagID)
1267 << T->getName(Context.getASTContext().getPrintingPolicy())
1268 << Range;
1269 break;
1270 }
1271 }
1272}
1273
1274// <type> ::= <function-type>
1275void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1276 SourceRange) {
1277 // Structors only appear in decls, so at this point we know it's not a
1278 // structor type.
1279 // FIXME: This may not be lambda-friendly.
1280 Out << "$$A6";
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001281 mangleFunctionType(T);
Guy Benyei11169dd2012-12-18 14:30:41 +00001282}
1283void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1284 SourceRange) {
1285 llvm_unreachable("Can't mangle K&R function prototypes");
1286}
1287
Peter Collingbourne2816c022013-04-25 04:25:40 +00001288void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1289 const FunctionDecl *D,
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001290 bool ForceInstMethod) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001291 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1292 // <return-type> <argument-list> <throw-spec>
1293 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1294
Reid Kleckner18da98e2013-06-24 19:21:52 +00001295 SourceRange Range;
1296 if (D) Range = D->getSourceRange();
1297
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001298 bool IsStructor = false, IsInstMethod = ForceInstMethod;
1299 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(D)) {
1300 if (MD->isInstance())
1301 IsInstMethod = true;
1302 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
1303 IsStructor = true;
1304 }
1305
Guy Benyei11169dd2012-12-18 14:30:41 +00001306 // If this is a C++ instance method, mangle the CVR qualifiers for the
1307 // this pointer.
David Majnemer6dda7bb2013-08-15 08:13:23 +00001308 if (IsInstMethod) {
1309 if (PointersAre64Bit)
1310 Out << 'E';
Guy Benyei11169dd2012-12-18 14:30:41 +00001311 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
David Majnemer6dda7bb2013-08-15 08:13:23 +00001312 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001313
Reid Klecknerc5cc3382013-09-25 22:28:52 +00001314 mangleCallingConvention(T);
Guy Benyei11169dd2012-12-18 14:30:41 +00001315
1316 // <return-type> ::= <type>
1317 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001318 if (IsStructor) {
1319 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1320 StructorType == Dtor_Deleting) {
1321 // The scalar deleting destructor takes an extra int argument.
1322 // However, the FunctionType generated has 0 arguments.
1323 // FIXME: This is a temporary hack.
1324 // Maybe should fix the FunctionType creation instead?
Timur Iskhodzhanovf46993e2013-08-26 10:32:04 +00001325 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001326 return;
1327 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001328 Out << '@';
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001329 } else {
Alp Toker314cc812014-01-25 16:55:45 +00001330 QualType ResultType = Proto->getReturnType();
David Majnemer6dda7bb2013-08-15 08:13:23 +00001331 if (ResultType->isVoidType())
1332 ResultType = ResultType.getUnqualifiedType();
1333 mangleType(ResultType, Range, QMM_Result);
Guy Benyei11169dd2012-12-18 14:30:41 +00001334 }
1335
1336 // <argument-list> ::= X # void
1337 // ::= <type>+ @
1338 // ::= <type>* Z # varargs
Alp Toker9cacbab2014-01-20 20:26:09 +00001339 if (Proto->getNumParams() == 0 && !Proto->isVariadic()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001340 Out << 'X';
1341 } else {
Reid Kleckner18da98e2013-06-24 19:21:52 +00001342 // Happens for function pointer type arguments for example.
Alp Toker9cacbab2014-01-20 20:26:09 +00001343 for (FunctionProtoType::param_type_iterator
1344 Arg = Proto->param_type_begin(),
1345 ArgEnd = Proto->param_type_end();
Reid Kleckner18da98e2013-06-24 19:21:52 +00001346 Arg != ArgEnd; ++Arg)
1347 mangleArgumentType(*Arg, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001348 // <builtin-type> ::= Z # ellipsis
1349 if (Proto->isVariadic())
1350 Out << 'Z';
1351 else
1352 Out << '@';
1353 }
1354
1355 mangleThrowSpecification(Proto);
1356}
1357
1358void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Kleckner369f3162013-05-14 20:30:42 +00001359 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1360 // # pointer. in 64-bit mode *all*
1361 // # 'this' pointers are 64-bit.
1362 // ::= <global-function>
1363 // <member-function> ::= A # private: near
1364 // ::= B # private: far
1365 // ::= C # private: static near
1366 // ::= D # private: static far
1367 // ::= E # private: virtual near
1368 // ::= F # private: virtual far
Reid Kleckner369f3162013-05-14 20:30:42 +00001369 // ::= I # protected: near
1370 // ::= J # protected: far
1371 // ::= K # protected: static near
1372 // ::= L # protected: static far
1373 // ::= M # protected: virtual near
1374 // ::= N # protected: virtual far
Reid Kleckner369f3162013-05-14 20:30:42 +00001375 // ::= Q # public: near
1376 // ::= R # public: far
1377 // ::= S # public: static near
1378 // ::= T # public: static far
1379 // ::= U # public: virtual near
1380 // ::= V # public: virtual far
Reid Kleckner369f3162013-05-14 20:30:42 +00001381 // <global-function> ::= Y # global near
1382 // ::= Z # global far
Guy Benyei11169dd2012-12-18 14:30:41 +00001383 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1384 switch (MD->getAccess()) {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00001385 case AS_none:
1386 llvm_unreachable("Unsupported access specifier");
Guy Benyei11169dd2012-12-18 14:30:41 +00001387 case AS_private:
1388 if (MD->isStatic())
1389 Out << 'C';
1390 else if (MD->isVirtual())
1391 Out << 'E';
1392 else
1393 Out << 'A';
1394 break;
1395 case AS_protected:
1396 if (MD->isStatic())
1397 Out << 'K';
1398 else if (MD->isVirtual())
1399 Out << 'M';
1400 else
1401 Out << 'I';
1402 break;
1403 case AS_public:
1404 if (MD->isStatic())
1405 Out << 'S';
1406 else if (MD->isVirtual())
1407 Out << 'U';
1408 else
1409 Out << 'Q';
1410 }
1411 } else
1412 Out << 'Y';
1413}
Reid Klecknerc5cc3382013-09-25 22:28:52 +00001414void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001415 // <calling-convention> ::= A # __cdecl
1416 // ::= B # __export __cdecl
1417 // ::= C # __pascal
1418 // ::= D # __export __pascal
1419 // ::= E # __thiscall
1420 // ::= F # __export __thiscall
1421 // ::= G # __stdcall
1422 // ::= H # __export __stdcall
1423 // ::= I # __fastcall
1424 // ::= J # __export __fastcall
1425 // The 'export' calling conventions are from a bygone era
1426 // (*cough*Win16*cough*) when functions were declared for export with
1427 // that keyword. (It didn't actually export them, it just made them so
1428 // that they could be in a DLL and somebody from another module could call
1429 // them.)
1430 CallingConv CC = T->getCallConv();
Guy Benyei11169dd2012-12-18 14:30:41 +00001431 switch (CC) {
1432 default:
1433 llvm_unreachable("Unsupported CC for mangling");
Charles Davisb5a214e2013-08-30 04:39:01 +00001434 case CC_X86_64Win64:
1435 case CC_X86_64SysV:
Guy Benyei11169dd2012-12-18 14:30:41 +00001436 case CC_C: Out << 'A'; break;
1437 case CC_X86Pascal: Out << 'C'; break;
1438 case CC_X86ThisCall: Out << 'E'; break;
1439 case CC_X86StdCall: Out << 'G'; break;
1440 case CC_X86FastCall: Out << 'I'; break;
1441 }
1442}
1443void MicrosoftCXXNameMangler::mangleThrowSpecification(
1444 const FunctionProtoType *FT) {
1445 // <throw-spec> ::= Z # throw(...) (default)
1446 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1447 // ::= <type>+
1448 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1449 // all actually mangled as 'Z'. (They're ignored because their associated
1450 // functionality isn't implemented, and probably never will be.)
1451 Out << 'Z';
1452}
1453
1454void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1455 SourceRange Range) {
1456 // Probably should be mangled as a template instantiation; need to see what
1457 // VC does first.
1458 DiagnosticsEngine &Diags = Context.getDiags();
1459 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1460 "cannot mangle this unresolved dependent type yet");
1461 Diags.Report(Range.getBegin(), DiagID)
1462 << Range;
1463}
1464
1465// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1466// <union-type> ::= T <name>
1467// <struct-type> ::= U <name>
1468// <class-type> ::= V <name>
David Majnemer048f90c2013-12-09 04:28:34 +00001469// <enum-type> ::= W4 <name>
Guy Benyei11169dd2012-12-18 14:30:41 +00001470void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer0db0ca42013-08-05 22:26:46 +00001471 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00001472}
1473void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer0db0ca42013-08-05 22:26:46 +00001474 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00001475}
David Majnemer0db0ca42013-08-05 22:26:46 +00001476void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1477 switch (TD->getTagKind()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001478 case TTK_Union:
1479 Out << 'T';
1480 break;
1481 case TTK_Struct:
1482 case TTK_Interface:
1483 Out << 'U';
1484 break;
1485 case TTK_Class:
1486 Out << 'V';
1487 break;
1488 case TTK_Enum:
David Majnemer048f90c2013-12-09 04:28:34 +00001489 Out << "W4";
Guy Benyei11169dd2012-12-18 14:30:41 +00001490 break;
1491 }
David Majnemer0db0ca42013-08-05 22:26:46 +00001492 mangleName(TD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001493}
1494
1495// <type> ::= <array-type>
1496// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1497// [Y <dimension-count> <dimension>+]
Reid Kleckner369f3162013-05-14 20:30:42 +00001498// <element-type> # as global, E is never required
Guy Benyei11169dd2012-12-18 14:30:41 +00001499// It's supposed to be the other way around, but for some strange reason, it
1500// isn't. Today this behavior is retained for the sole purpose of backwards
1501// compatibility.
David Majnemer5a1b2042013-09-11 04:44:30 +00001502void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001503 // This isn't a recursive mangling, so now we have to do it all in this
1504 // one call.
David Majnemer5a1b2042013-09-11 04:44:30 +00001505 manglePointerQualifiers(T->getElementType().getQualifiers());
Peter Collingbourne2816c022013-04-25 04:25:40 +00001506 mangleType(T->getElementType(), SourceRange());
Guy Benyei11169dd2012-12-18 14:30:41 +00001507}
1508void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1509 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001510 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001511}
1512void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1513 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001514 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001515}
1516void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1517 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001518 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001519}
1520void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1521 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001522 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001523}
Reid Kleckner18da98e2013-06-24 19:21:52 +00001524void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001525 QualType ElementTy(T, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001526 SmallVector<llvm::APInt, 3> Dimensions;
1527 for (;;) {
1528 if (const ConstantArrayType *CAT =
1529 getASTContext().getAsConstantArrayType(ElementTy)) {
1530 Dimensions.push_back(CAT->getSize());
1531 ElementTy = CAT->getElementType();
1532 } else if (ElementTy->isVariableArrayType()) {
1533 const VariableArrayType *VAT =
1534 getASTContext().getAsVariableArrayType(ElementTy);
1535 DiagnosticsEngine &Diags = Context.getDiags();
1536 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1537 "cannot mangle this variable-length array yet");
1538 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1539 << VAT->getBracketsRange();
1540 return;
1541 } else if (ElementTy->isDependentSizedArrayType()) {
1542 // The dependent expression has to be folded into a constant (TODO).
1543 const DependentSizedArrayType *DSAT =
1544 getASTContext().getAsDependentSizedArrayType(ElementTy);
1545 DiagnosticsEngine &Diags = Context.getDiags();
1546 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1547 "cannot mangle this dependent-length array yet");
1548 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1549 << DSAT->getBracketsRange();
1550 return;
Peter Collingbourne2816c022013-04-25 04:25:40 +00001551 } else if (const IncompleteArrayType *IAT =
1552 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1553 Dimensions.push_back(llvm::APInt(32, 0));
1554 ElementTy = IAT->getElementType();
1555 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001556 else break;
1557 }
Peter Collingbourne2816c022013-04-25 04:25:40 +00001558 Out << 'Y';
1559 // <dimension-count> ::= <number> # number of extra dimensions
1560 mangleNumber(Dimensions.size());
1561 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1562 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Kleckner18da98e2013-06-24 19:21:52 +00001563 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei11169dd2012-12-18 14:30:41 +00001564}
1565
1566// <type> ::= <pointer-to-member-type>
1567// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1568// <class name> <type>
1569void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1570 SourceRange Range) {
1571 QualType PointeeType = T->getPointeeType();
1572 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1573 Out << '8';
1574 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001575 mangleFunctionType(FPT, 0, true);
Guy Benyei11169dd2012-12-18 14:30:41 +00001576 } else {
David Majnemer6dda7bb2013-08-15 08:13:23 +00001577 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1578 Out << 'E';
Guy Benyei11169dd2012-12-18 14:30:41 +00001579 mangleQualifiers(PointeeType.getQualifiers(), true);
1580 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourne2816c022013-04-25 04:25:40 +00001581 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei11169dd2012-12-18 14:30:41 +00001582 }
1583}
1584
1585void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1586 SourceRange Range) {
1587 DiagnosticsEngine &Diags = Context.getDiags();
1588 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1589 "cannot mangle this template type parameter type yet");
1590 Diags.Report(Range.getBegin(), DiagID)
1591 << Range;
1592}
1593
1594void MicrosoftCXXNameMangler::mangleType(
1595 const SubstTemplateTypeParmPackType *T,
1596 SourceRange Range) {
1597 DiagnosticsEngine &Diags = Context.getDiags();
1598 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1599 "cannot mangle this substituted parameter pack yet");
1600 Diags.Report(Range.getBegin(), DiagID)
1601 << Range;
1602}
1603
1604// <type> ::= <pointer-type>
Reid Kleckner369f3162013-05-14 20:30:42 +00001605// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
Alp Tokerd4733632013-12-05 04:47:09 +00001606// # the E is required for 64-bit non-static pointers
Guy Benyei11169dd2012-12-18 14:30:41 +00001607void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1608 SourceRange Range) {
1609 QualType PointeeTy = T->getPointeeType();
Reid Kleckner369f3162013-05-14 20:30:42 +00001610 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1611 Out << 'E';
Peter Collingbourne2816c022013-04-25 04:25:40 +00001612 mangleType(PointeeTy, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001613}
1614void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1615 SourceRange Range) {
1616 // Object pointers never have qualifiers.
1617 Out << 'A';
David Majnemer6dda7bb2013-08-15 08:13:23 +00001618 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1619 Out << 'E';
Guy Benyei11169dd2012-12-18 14:30:41 +00001620 mangleType(T->getPointeeType(), Range);
1621}
1622
1623// <type> ::= <reference-type>
Reid Kleckner369f3162013-05-14 20:30:42 +00001624// <reference-type> ::= A E? <cvr-qualifiers> <type>
Alp Tokerd4733632013-12-05 04:47:09 +00001625// # the E is required for 64-bit non-static lvalue references
Guy Benyei11169dd2012-12-18 14:30:41 +00001626void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1627 SourceRange Range) {
1628 Out << 'A';
Reid Kleckner369f3162013-05-14 20:30:42 +00001629 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1630 Out << 'E';
Peter Collingbourne2816c022013-04-25 04:25:40 +00001631 mangleType(T->getPointeeType(), Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001632}
1633
1634// <type> ::= <r-value-reference-type>
Reid Kleckner369f3162013-05-14 20:30:42 +00001635// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
Alp Tokerd4733632013-12-05 04:47:09 +00001636// # the E is required for 64-bit non-static rvalue references
Guy Benyei11169dd2012-12-18 14:30:41 +00001637void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1638 SourceRange Range) {
1639 Out << "$$Q";
Reid Kleckner369f3162013-05-14 20:30:42 +00001640 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1641 Out << 'E';
Peter Collingbourne2816c022013-04-25 04:25:40 +00001642 mangleType(T->getPointeeType(), Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001643}
1644
1645void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1646 SourceRange Range) {
1647 DiagnosticsEngine &Diags = Context.getDiags();
1648 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1649 "cannot mangle this complex number type yet");
1650 Diags.Report(Range.getBegin(), DiagID)
1651 << Range;
1652}
1653
1654void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1655 SourceRange Range) {
Reid Klecknere7e64d82013-03-26 16:56:59 +00001656 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1657 assert(ET && "vectors with non-builtin elements are unsupported");
1658 uint64_t Width = getASTContext().getTypeSize(T);
1659 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1660 // doesn't match the Intel types uses a custom mangling below.
1661 bool IntelVector = true;
1662 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1663 Out << "T__m64";
1664 } else if (Width == 128 || Width == 256) {
1665 if (ET->getKind() == BuiltinType::Float)
1666 Out << "T__m" << Width;
1667 else if (ET->getKind() == BuiltinType::LongLong)
1668 Out << "T__m" << Width << 'i';
1669 else if (ET->getKind() == BuiltinType::Double)
1670 Out << "U__m" << Width << 'd';
1671 else
1672 IntelVector = false;
1673 } else {
1674 IntelVector = false;
1675 }
1676
1677 if (!IntelVector) {
1678 // The MS ABI doesn't have a special mangling for vector types, so we define
1679 // our own mangling to handle uses of __vector_size__ on user-specified
1680 // types, and for extensions like __v4sf.
1681 Out << "T__clang_vec" << T->getNumElements() << '_';
1682 mangleType(ET, Range);
1683 }
1684
1685 Out << "@@";
Guy Benyei11169dd2012-12-18 14:30:41 +00001686}
Reid Klecknere7e64d82013-03-26 16:56:59 +00001687
Guy Benyei11169dd2012-12-18 14:30:41 +00001688void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1689 SourceRange Range) {
1690 DiagnosticsEngine &Diags = Context.getDiags();
1691 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1692 "cannot mangle this extended vector type yet");
1693 Diags.Report(Range.getBegin(), DiagID)
1694 << Range;
1695}
1696void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1697 SourceRange Range) {
1698 DiagnosticsEngine &Diags = Context.getDiags();
1699 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1700 "cannot mangle this dependent-sized extended vector type yet");
1701 Diags.Report(Range.getBegin(), DiagID)
1702 << Range;
1703}
1704
1705void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1706 SourceRange) {
1707 // ObjC interfaces have structs underlying them.
1708 Out << 'U';
1709 mangleName(T->getDecl());
1710}
1711
1712void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1713 SourceRange Range) {
1714 // We don't allow overloading by different protocol qualification,
1715 // so mangling them isn't necessary.
1716 mangleType(T->getBaseType(), Range);
1717}
1718
1719void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1720 SourceRange Range) {
1721 Out << "_E";
1722
1723 QualType pointee = T->getPointeeType();
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001724 mangleFunctionType(pointee->castAs<FunctionProtoType>());
Guy Benyei11169dd2012-12-18 14:30:41 +00001725}
1726
David Majnemerf0a84f22013-08-16 08:29:13 +00001727void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1728 SourceRange) {
1729 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei11169dd2012-12-18 14:30:41 +00001730}
1731
1732void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1733 SourceRange Range) {
1734 DiagnosticsEngine &Diags = Context.getDiags();
1735 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1736 "cannot mangle this template specialization type yet");
1737 Diags.Report(Range.getBegin(), DiagID)
1738 << Range;
1739}
1740
1741void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1742 SourceRange Range) {
1743 DiagnosticsEngine &Diags = Context.getDiags();
1744 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1745 "cannot mangle this dependent name type yet");
1746 Diags.Report(Range.getBegin(), DiagID)
1747 << Range;
1748}
1749
1750void MicrosoftCXXNameMangler::mangleType(
1751 const DependentTemplateSpecializationType *T,
1752 SourceRange Range) {
1753 DiagnosticsEngine &Diags = Context.getDiags();
1754 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1755 "cannot mangle this dependent template specialization type yet");
1756 Diags.Report(Range.getBegin(), DiagID)
1757 << Range;
1758}
1759
1760void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1761 SourceRange Range) {
1762 DiagnosticsEngine &Diags = Context.getDiags();
1763 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1764 "cannot mangle this pack expansion yet");
1765 Diags.Report(Range.getBegin(), DiagID)
1766 << Range;
1767}
1768
1769void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1770 SourceRange Range) {
1771 DiagnosticsEngine &Diags = Context.getDiags();
1772 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1773 "cannot mangle this typeof(type) yet");
1774 Diags.Report(Range.getBegin(), DiagID)
1775 << Range;
1776}
1777
1778void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1779 SourceRange Range) {
1780 DiagnosticsEngine &Diags = Context.getDiags();
1781 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1782 "cannot mangle this typeof(expression) yet");
1783 Diags.Report(Range.getBegin(), DiagID)
1784 << Range;
1785}
1786
1787void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1788 SourceRange Range) {
1789 DiagnosticsEngine &Diags = Context.getDiags();
1790 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1791 "cannot mangle this decltype() yet");
1792 Diags.Report(Range.getBegin(), DiagID)
1793 << Range;
1794}
1795
1796void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1797 SourceRange Range) {
1798 DiagnosticsEngine &Diags = Context.getDiags();
1799 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1800 "cannot mangle this unary transform type yet");
1801 Diags.Report(Range.getBegin(), DiagID)
1802 << Range;
1803}
1804
1805void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
David Majnemerb9a5f2d2014-01-21 20:33:36 +00001806 assert(T->getDeducedType().isNull() && "expecting a dependent type!");
1807
Guy Benyei11169dd2012-12-18 14:30:41 +00001808 DiagnosticsEngine &Diags = Context.getDiags();
1809 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1810 "cannot mangle this 'auto' type yet");
1811 Diags.Report(Range.getBegin(), DiagID)
1812 << Range;
1813}
1814
1815void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1816 SourceRange Range) {
1817 DiagnosticsEngine &Diags = Context.getDiags();
1818 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1819 "cannot mangle this C11 atomic type yet");
1820 Diags.Report(Range.getBegin(), DiagID)
1821 << Range;
1822}
1823
Rafael Espindola002667c2013-10-16 01:40:34 +00001824void MicrosoftMangleContextImpl::mangleCXXName(const NamedDecl *D,
1825 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001826 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1827 "Invalid mangleName() call, argument is not a variable or function!");
1828 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1829 "Invalid mangleName() call on 'structor decl!");
1830
1831 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1832 getASTContext().getSourceManager(),
1833 "Mangling declaration");
1834
1835 MicrosoftCXXNameMangler Mangler(*this, Out);
1836 return Mangler.mangle(D);
1837}
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001838
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00001839// <this-adjustment> ::= <no-adjustment> | <static-adjustment> |
1840// <virtual-adjustment>
1841// <no-adjustment> ::= A # private near
1842// ::= B # private far
1843// ::= I # protected near
1844// ::= J # protected far
1845// ::= Q # public near
1846// ::= R # public far
1847// <static-adjustment> ::= G <static-offset> # private near
1848// ::= H <static-offset> # private far
1849// ::= O <static-offset> # protected near
1850// ::= P <static-offset> # protected far
1851// ::= W <static-offset> # public near
1852// ::= X <static-offset> # public far
1853// <virtual-adjustment> ::= $0 <virtual-shift> <static-offset> # private near
1854// ::= $1 <virtual-shift> <static-offset> # private far
1855// ::= $2 <virtual-shift> <static-offset> # protected near
1856// ::= $3 <virtual-shift> <static-offset> # protected far
1857// ::= $4 <virtual-shift> <static-offset> # public near
1858// ::= $5 <virtual-shift> <static-offset> # public far
1859// <virtual-shift> ::= <vtordisp-shift> | <vtordispex-shift>
1860// <vtordisp-shift> ::= <offset-to-vtordisp>
1861// <vtordispex-shift> ::= <offset-to-vbptr> <vbase-offset-offset>
1862// <offset-to-vtordisp>
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00001863static void mangleThunkThisAdjustment(const CXXMethodDecl *MD,
1864 const ThisAdjustment &Adjustment,
1865 MicrosoftCXXNameMangler &Mangler,
1866 raw_ostream &Out) {
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00001867 if (!Adjustment.Virtual.isEmpty()) {
1868 Out << '$';
1869 char AccessSpec;
1870 switch (MD->getAccess()) {
1871 case AS_none:
1872 llvm_unreachable("Unsupported access specifier");
1873 case AS_private:
1874 AccessSpec = '0';
1875 break;
1876 case AS_protected:
1877 AccessSpec = '2';
1878 break;
1879 case AS_public:
1880 AccessSpec = '4';
1881 }
1882 if (Adjustment.Virtual.Microsoft.VBPtrOffset) {
1883 Out << 'R' << AccessSpec;
David Majnemer2a816452013-12-09 10:44:32 +00001884 Mangler.mangleNumber(
1885 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBPtrOffset));
1886 Mangler.mangleNumber(
1887 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBOffsetOffset));
1888 Mangler.mangleNumber(
1889 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset));
1890 Mangler.mangleNumber(static_cast<uint32_t>(Adjustment.NonVirtual));
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00001891 } else {
1892 Out << AccessSpec;
David Majnemer2a816452013-12-09 10:44:32 +00001893 Mangler.mangleNumber(
1894 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset));
1895 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual));
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00001896 }
1897 } else if (Adjustment.NonVirtual != 0) {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00001898 switch (MD->getAccess()) {
1899 case AS_none:
1900 llvm_unreachable("Unsupported access specifier");
1901 case AS_private:
1902 Out << 'G';
1903 break;
1904 case AS_protected:
1905 Out << 'O';
1906 break;
1907 case AS_public:
1908 Out << 'W';
1909 }
David Majnemer2a816452013-12-09 10:44:32 +00001910 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual));
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00001911 } else {
1912 switch (MD->getAccess()) {
1913 case AS_none:
1914 llvm_unreachable("Unsupported access specifier");
1915 case AS_private:
1916 Out << 'A';
1917 break;
1918 case AS_protected:
1919 Out << 'I';
1920 break;
1921 case AS_public:
1922 Out << 'Q';
1923 }
1924 }
1925}
1926
Hans Wennborg88497d62013-11-15 17:24:45 +00001927void MicrosoftMangleContextImpl::mangleVirtualMemPtrThunk(
David Majnemer2a816452013-12-09 10:44:32 +00001928 const CXXMethodDecl *MD, uint64_t OffsetInVFTable, raw_ostream &Out) {
Hans Wennborg88497d62013-11-15 17:24:45 +00001929 bool Is64Bit = getASTContext().getTargetInfo().getPointerWidth(0) == 64;
1930
1931 MicrosoftCXXNameMangler Mangler(*this, Out);
1932 Mangler.getStream() << "\01??_9";
1933 Mangler.mangleName(MD->getParent());
1934 Mangler.getStream() << "$B";
1935 Mangler.mangleNumber(OffsetInVFTable);
1936 Mangler.getStream() << "A";
1937 Mangler.getStream() << (Is64Bit ? "A" : "E");
1938}
1939
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00001940void MicrosoftMangleContextImpl::mangleThunk(const CXXMethodDecl *MD,
1941 const ThunkInfo &Thunk,
1942 raw_ostream &Out) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001943 MicrosoftCXXNameMangler Mangler(*this, Out);
1944 Out << "\01?";
1945 Mangler.mangleName(MD);
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00001946 mangleThunkThisAdjustment(MD, Thunk.This, Mangler, Out);
1947 if (!Thunk.Return.isEmpty())
1948 assert(Thunk.Method != 0 && "Thunk info should hold the overridee decl");
1949
1950 const CXXMethodDecl *DeclForFPT = Thunk.Method ? Thunk.Method : MD;
1951 Mangler.mangleFunctionType(
1952 DeclForFPT->getType()->castAs<FunctionProtoType>(), MD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001953}
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001954
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00001955void MicrosoftMangleContextImpl::mangleCXXDtorThunk(
1956 const CXXDestructorDecl *DD, CXXDtorType Type,
1957 const ThisAdjustment &Adjustment, raw_ostream &Out) {
1958 // FIXME: Actually, the dtor thunk should be emitted for vector deleting
1959 // dtors rather than scalar deleting dtors. Just use the vector deleting dtor
1960 // mangling manually until we support both deleting dtor types.
1961 assert(Type == Dtor_Deleting);
1962 MicrosoftCXXNameMangler Mangler(*this, Out, DD, Type);
1963 Out << "\01??_E";
1964 Mangler.mangleName(DD->getParent());
1965 mangleThunkThisAdjustment(DD, Adjustment, Mangler, Out);
1966 Mangler.mangleFunctionType(DD->getType()->castAs<FunctionProtoType>(), DD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001967}
Reid Kleckner7810af02013-06-19 15:20:38 +00001968
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00001969void MicrosoftMangleContextImpl::mangleCXXVFTable(
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001970 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1971 raw_ostream &Out) {
Reid Kleckner7810af02013-06-19 15:20:38 +00001972 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1973 // <cvr-qualifiers> [<name>] @
Guy Benyei11169dd2012-12-18 14:30:41 +00001974 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner7810af02013-06-19 15:20:38 +00001975 // is always '6' for vftables.
Guy Benyei11169dd2012-12-18 14:30:41 +00001976 MicrosoftCXXNameMangler Mangler(*this, Out);
1977 Mangler.getStream() << "\01??_7";
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001978 Mangler.mangleName(Derived);
1979 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
1980 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1981 E = BasePath.end();
1982 I != E; ++I) {
1983 Mangler.mangleName(*I);
1984 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001985 Mangler.getStream() << '@';
1986}
Reid Kleckner7810af02013-06-19 15:20:38 +00001987
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00001988void MicrosoftMangleContextImpl::mangleCXXVBTable(
Reid Kleckner7810af02013-06-19 15:20:38 +00001989 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1990 raw_ostream &Out) {
1991 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1992 // <cvr-qualifiers> [<name>] @
1993 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1994 // is always '7' for vbtables.
1995 MicrosoftCXXNameMangler Mangler(*this, Out);
1996 Mangler.getStream() << "\01??_8";
1997 Mangler.mangleName(Derived);
1998 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1999 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
2000 E = BasePath.end();
2001 I != E; ++I) {
2002 Mangler.mangleName(*I);
2003 }
2004 Mangler.getStream() << '@';
2005}
2006
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002007void MicrosoftMangleContextImpl::mangleCXXRTTI(QualType T, raw_ostream &) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002008 // FIXME: Give a location...
2009 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2010 "cannot mangle RTTI descriptors for type %0 yet");
2011 getDiags().Report(DiagID)
2012 << T.getBaseTypeIdentifier();
2013}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002014
2015void MicrosoftMangleContextImpl::mangleCXXRTTIName(QualType T, raw_ostream &) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002016 // FIXME: Give a location...
2017 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2018 "cannot mangle the name of type %0 into RTTI descriptors yet");
2019 getDiags().Report(DiagID)
2020 << T.getBaseTypeIdentifier();
2021}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002022
Reid Klecknercc99e262013-11-19 23:23:00 +00002023void MicrosoftMangleContextImpl::mangleTypeName(QualType T, raw_ostream &Out) {
2024 // This is just a made up unique string for the purposes of tbaa. undname
2025 // does *not* know how to demangle it.
2026 MicrosoftCXXNameMangler Mangler(*this, Out);
2027 Mangler.getStream() << '?';
2028 Mangler.mangleType(T, SourceRange());
2029}
2030
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002031void MicrosoftMangleContextImpl::mangleCXXCtor(const CXXConstructorDecl *D,
2032 CXXCtorType Type,
2033 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002034 MicrosoftCXXNameMangler mangler(*this, Out);
2035 mangler.mangle(D);
2036}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002037
2038void MicrosoftMangleContextImpl::mangleCXXDtor(const CXXDestructorDecl *D,
2039 CXXDtorType Type,
2040 raw_ostream &Out) {
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00002041 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei11169dd2012-12-18 14:30:41 +00002042 mangler.mangle(D);
2043}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002044
2045void MicrosoftMangleContextImpl::mangleReferenceTemporary(const VarDecl *VD,
David Majnemer210e6bfa12013-12-13 00:39:38 +00002046 raw_ostream &) {
2047 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2048 "cannot mangle this reference temporary yet");
2049 getDiags().Report(VD->getLocation(), DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00002050}
2051
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002052void MicrosoftMangleContextImpl::mangleStaticGuardVariable(const VarDecl *VD,
2053 raw_ostream &Out) {
David Majnemer25e1a5e2013-12-13 00:52:45 +00002054 // TODO: This is not correct, especially with respect to MSVC2013. MSVC2013
2055 // utilizes thread local variables to implement thread safe, re-entrant
2056 // initialization for statics. They no longer differentiate between an
2057 // externally visible and non-externally visible static with respect to
2058 // mangling, they all get $TSS <number>.
2059 //
2060 // N.B. This means that they can get more than 32 static variable guards in a
2061 // scope. It also means that they broke compatibility with their own ABI.
2062
Reid Klecknerd8110b62013-09-10 20:14:30 +00002063 // <guard-name> ::= ?_B <postfix> @51
2064 // ::= ?$S <guard-num> @ <postfix> @4IA
2065
2066 // The first mangling is what MSVC uses to guard static locals in inline
2067 // functions. It uses a different mangling in external functions to support
2068 // guarding more than 32 variables. MSVC rejects inline functions with more
2069 // than 32 static locals. We don't fully implement the second mangling
2070 // because those guards are not externally visible, and instead use LLVM's
2071 // default renaming when creating a new guard variable.
2072 MicrosoftCXXNameMangler Mangler(*this, Out);
2073
2074 bool Visible = VD->isExternallyVisible();
2075 // <operator-name> ::= ?_B # local static guard
2076 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@");
2077 Mangler.manglePostfix(VD->getDeclContext());
2078 Mangler.getStream() << (Visible ? "@51" : "@4IA");
2079}
2080
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002081void MicrosoftMangleContextImpl::mangleInitFiniStub(const VarDecl *D,
2082 raw_ostream &Out,
2083 char CharCode) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002084 MicrosoftCXXNameMangler Mangler(*this, Out);
2085 Mangler.getStream() << "\01??__" << CharCode;
2086 Mangler.mangleName(D);
2087 // This is the function class mangling. These stubs are global, non-variadic,
2088 // cdecl functions that return void and take no args.
2089 Mangler.getStream() << "YAXXZ";
2090}
2091
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002092void MicrosoftMangleContextImpl::mangleDynamicInitializer(const VarDecl *D,
2093 raw_ostream &Out) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002094 // <initializer-name> ::= ?__E <name> YAXXZ
2095 mangleInitFiniStub(D, Out, 'E');
2096}
2097
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002098void
2099MicrosoftMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D,
2100 raw_ostream &Out) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002101 // <destructor-name> ::= ?__F <name> YAXXZ
2102 mangleInitFiniStub(D, Out, 'F');
Reid Klecknerd8110b62013-09-10 20:14:30 +00002103}
2104
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002105MicrosoftMangleContext *
2106MicrosoftMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags) {
2107 return new MicrosoftMangleContextImpl(Context, Diags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002108}