blob: f02dfc72ee90661ba0055868c0e620c99433c71c [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"
Reid Kleckner96f8f932014-02-05 17:27:08 +000024#include "clang/AST/VTableBuilder.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000025#include "clang/Basic/ABI.h"
26#include "clang/Basic/DiagnosticOptions.h"
Reid Kleckner369f3162013-05-14 20:30:42 +000027#include "clang/Basic/TargetInfo.h"
Reid Kleckner7dafb232013-05-22 17:16:39 +000028#include "llvm/ADT/StringMap.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000029
30using namespace clang;
31
32namespace {
33
David Majnemerd5a42b82013-09-13 09:03:14 +000034/// \brief Retrieve the declaration context that should be used when mangling
35/// the given declaration.
36static const DeclContext *getEffectiveDeclContext(const Decl *D) {
37 // The ABI assumes that lambda closure types that occur within
38 // default arguments live in the context of the function. However, due to
39 // the way in which Clang parses and creates function declarations, this is
40 // not the case: the lambda closure type ends up living in the context
41 // where the function itself resides, because the function declaration itself
42 // had not yet been created. Fix the context here.
43 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
44 if (RD->isLambda())
45 if (ParmVarDecl *ContextParam =
46 dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl()))
47 return ContextParam->getDeclContext();
48 }
49
50 // Perform the same check for block literals.
51 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
52 if (ParmVarDecl *ContextParam =
53 dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl()))
54 return ContextParam->getDeclContext();
55 }
56
57 const DeclContext *DC = D->getDeclContext();
58 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(DC))
59 return getEffectiveDeclContext(CD);
60
61 return DC;
62}
63
64static const DeclContext *getEffectiveParentContext(const DeclContext *DC) {
65 return getEffectiveDeclContext(cast<Decl>(DC));
66}
67
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +000068static const FunctionDecl *getStructor(const FunctionDecl *fn) {
69 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
70 return ftd->getTemplatedDecl();
71
72 return fn;
73}
74
Guy Benyei11169dd2012-12-18 14:30:41 +000075/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
76/// Microsoft Visual C++ ABI.
77class MicrosoftCXXNameMangler {
78 MangleContext &Context;
79 raw_ostream &Out;
80
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +000081 /// The "structor" is the top-level declaration being mangled, if
82 /// that's not a template specialization; otherwise it's the pattern
83 /// for that specialization.
84 const NamedDecl *Structor;
85 unsigned StructorType;
86
Reid Kleckner7dafb232013-05-22 17:16:39 +000087 typedef llvm::StringMap<unsigned> BackRefMap;
Guy Benyei11169dd2012-12-18 14:30:41 +000088 BackRefMap NameBackReferences;
89 bool UseNameBackReferences;
90
91 typedef llvm::DenseMap<void*, unsigned> ArgBackRefMap;
92 ArgBackRefMap TypeBackReferences;
93
94 ASTContext &getASTContext() const { return Context.getASTContext(); }
95
Reid Kleckner369f3162013-05-14 20:30:42 +000096 // FIXME: If we add support for __ptr32/64 qualifiers, then we should push
97 // this check into mangleQualifiers().
98 const bool PointersAre64Bit;
99
Guy Benyei11169dd2012-12-18 14:30:41 +0000100public:
Peter Collingbourne2816c022013-04-25 04:25:40 +0000101 enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result };
102
Guy Benyei11169dd2012-12-18 14:30:41 +0000103 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_)
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000104 : Context(C), Out(Out_),
105 Structor(0), StructorType(-1),
David Blaikie014399c2013-05-14 21:31:46 +0000106 UseNameBackReferences(true),
Reid Kleckner369f3162013-05-14 20:30:42 +0000107 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikie014399c2013-05-14 21:31:46 +0000108 64) { }
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000109
110 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_,
111 const CXXDestructorDecl *D, CXXDtorType Type)
112 : Context(C), Out(Out_),
113 Structor(getStructor(D)), StructorType(Type),
David Blaikie014399c2013-05-14 21:31:46 +0000114 UseNameBackReferences(true),
Reid Kleckner369f3162013-05-14 20:30:42 +0000115 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikie014399c2013-05-14 21:31:46 +0000116 64) { }
Guy Benyei11169dd2012-12-18 14:30:41 +0000117
118 raw_ostream &getStream() const { return Out; }
119
120 void mangle(const NamedDecl *D, StringRef Prefix = "\01?");
121 void mangleName(const NamedDecl *ND);
David Majnemer8eaab6f2013-08-13 06:32:20 +0000122 void mangleDeclaration(const NamedDecl *ND);
Guy Benyei11169dd2012-12-18 14:30:41 +0000123 void mangleFunctionEncoding(const FunctionDecl *FD);
124 void mangleVariableEncoding(const VarDecl *VD);
Reid Kleckner96f8f932014-02-05 17:27:08 +0000125 void mangleMemberDataPointer(const CXXRecordDecl *RD, const FieldDecl *FD);
126 void mangleMemberFunctionPointer(const CXXRecordDecl *RD,
127 const CXXMethodDecl *MD);
128 void mangleVirtualMemPtrThunk(
129 const CXXMethodDecl *MD,
130 const MicrosoftVTableContext::MethodVFTableLocation &ML);
David Majnemer2a816452013-12-09 10:44:32 +0000131 void mangleNumber(int64_t Number);
Peter Collingbourne2816c022013-04-25 04:25:40 +0000132 void mangleType(QualType T, SourceRange Range,
133 QualifierMangleMode QMM = QMM_Mangle);
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +0000134 void mangleFunctionType(const FunctionType *T, const FunctionDecl *D = 0,
135 bool ForceInstMethod = false);
Reid Klecknerd8110b62013-09-10 20:14:30 +0000136 void manglePostfix(const DeclContext *DC, bool NoFunction = false);
Guy Benyei11169dd2012-12-18 14:30:41 +0000137
138private:
139 void disableBackReferences() { UseNameBackReferences = false; }
140 void mangleUnqualifiedName(const NamedDecl *ND) {
141 mangleUnqualifiedName(ND, ND->getDeclName());
142 }
143 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
David Majnemer956bc112013-11-25 17:50:19 +0000144 void mangleSourceName(StringRef Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000145 void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc);
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000146 void mangleCXXDtorType(CXXDtorType T);
Guy Benyei11169dd2012-12-18 14:30:41 +0000147 void mangleQualifiers(Qualifiers Quals, bool IsMember);
148 void manglePointerQualifiers(Qualifiers Quals);
149
150 void mangleUnscopedTemplateName(const TemplateDecl *ND);
151 void mangleTemplateInstantiationName(const TemplateDecl *TD,
Reid Kleckner52518862013-03-20 01:40:23 +0000152 const TemplateArgumentList &TemplateArgs);
Guy Benyei11169dd2012-12-18 14:30:41 +0000153 void mangleObjCMethodName(const ObjCMethodDecl *MD);
154 void mangleLocalName(const FunctionDecl *FD);
155
156 void mangleArgumentType(QualType T, SourceRange Range);
157
158 // Declare manglers for every type class.
159#define ABSTRACT_TYPE(CLASS, PARENT)
160#define NON_CANONICAL_TYPE(CLASS, PARENT)
161#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \
162 SourceRange Range);
163#include "clang/AST/TypeNodes.def"
164#undef ABSTRACT_TYPE
165#undef NON_CANONICAL_TYPE
166#undef TYPE
David Majnemerb4119f72013-12-13 01:06:04 +0000167
David Majnemer0db0ca42013-08-05 22:26:46 +0000168 void mangleType(const TagDecl *TD);
David Majnemer5a1b2042013-09-11 04:44:30 +0000169 void mangleDecayedArrayType(const ArrayType *T);
Reid Kleckner18da98e2013-06-24 19:21:52 +0000170 void mangleArrayType(const ArrayType *T);
Guy Benyei11169dd2012-12-18 14:30:41 +0000171 void mangleFunctionClass(const FunctionDecl *FD);
Reid Klecknerc5cc3382013-09-25 22:28:52 +0000172 void mangleCallingConvention(const FunctionType *T);
Guy Benyei11169dd2012-12-18 14:30:41 +0000173 void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean);
174 void mangleExpression(const Expr *E);
175 void mangleThrowSpecification(const FunctionProtoType *T);
176
Reid Kleckner52518862013-03-20 01:40:23 +0000177 void mangleTemplateArgs(const TemplateDecl *TD,
178 const TemplateArgumentList &TemplateArgs);
David Majnemer08177c52013-08-27 08:21:25 +0000179 void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA);
Guy Benyei11169dd2012-12-18 14:30:41 +0000180};
181
Timur Iskhodzhanov67455222013-10-03 06:26:13 +0000182/// MicrosoftMangleContextImpl - Overrides the default MangleContext for the
Guy Benyei11169dd2012-12-18 14:30:41 +0000183/// Microsoft Visual C++ ABI.
Timur Iskhodzhanov67455222013-10-03 06:26:13 +0000184class MicrosoftMangleContextImpl : public MicrosoftMangleContext {
Guy Benyei11169dd2012-12-18 14:30:41 +0000185public:
Timur Iskhodzhanov67455222013-10-03 06:26:13 +0000186 MicrosoftMangleContextImpl(ASTContext &Context, DiagnosticsEngine &Diags)
187 : MicrosoftMangleContext(Context, Diags) {}
Rafael Espindola002667c2013-10-16 01:40:34 +0000188 virtual bool shouldMangleCXXName(const NamedDecl *D);
189 virtual void mangleCXXName(const NamedDecl *D, raw_ostream &Out);
Hans Wennborg88497d62013-11-15 17:24:45 +0000190 virtual void mangleVirtualMemPtrThunk(const CXXMethodDecl *MD,
David Majnemer2a816452013-12-09 10:44:32 +0000191 raw_ostream &);
Guy Benyei11169dd2012-12-18 14:30:41 +0000192 virtual void mangleThunk(const CXXMethodDecl *MD,
193 const ThunkInfo &Thunk,
194 raw_ostream &);
195 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
196 const ThisAdjustment &ThisAdjustment,
197 raw_ostream &);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000198 virtual void mangleCXXVFTable(const CXXRecordDecl *Derived,
199 ArrayRef<const CXXRecordDecl *> BasePath,
200 raw_ostream &Out);
Reid Kleckner7810af02013-06-19 15:20:38 +0000201 virtual void mangleCXXVBTable(const CXXRecordDecl *Derived,
202 ArrayRef<const CXXRecordDecl *> BasePath,
203 raw_ostream &Out);
Guy Benyei11169dd2012-12-18 14:30:41 +0000204 virtual void mangleCXXRTTI(QualType T, raw_ostream &);
205 virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
Reid Klecknercc99e262013-11-19 23:23:00 +0000206 virtual void mangleTypeName(QualType T, raw_ostream &);
Guy Benyei11169dd2012-12-18 14:30:41 +0000207 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
208 raw_ostream &);
209 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
210 raw_ostream &);
Reid Klecknerd8110b62013-09-10 20:14:30 +0000211 virtual void mangleReferenceTemporary(const VarDecl *, raw_ostream &);
212 virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out);
Reid Kleckner1ece9fc2013-09-10 20:43:12 +0000213 virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out);
Reid Klecknerd8110b62013-09-10 20:14:30 +0000214 virtual void mangleDynamicAtExitDestructor(const VarDecl *D,
215 raw_ostream &Out);
Reid Kleckner1ece9fc2013-09-10 20:43:12 +0000216
217private:
218 void mangleInitFiniStub(const VarDecl *D, raw_ostream &Out, char CharCode);
Guy Benyei11169dd2012-12-18 14:30:41 +0000219};
220
221}
222
Rafael Espindola002667c2013-10-16 01:40:34 +0000223bool MicrosoftMangleContextImpl::shouldMangleCXXName(const NamedDecl *D) {
David Majnemerd5a42b82013-09-13 09:03:14 +0000224 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
225 LanguageLinkage L = FD->getLanguageLinkage();
226 // Overloadable functions need mangling.
227 if (FD->hasAttr<OverloadableAttr>())
228 return true;
229
David Majnemerc729b0b2013-09-16 22:44:20 +0000230 // The ABI expects that we would never mangle "typical" user-defined entry
231 // points regardless of visibility or freestanding-ness.
232 //
233 // N.B. This is distinct from asking about "main". "main" has a lot of
234 // special rules associated with it in the standard while these
235 // user-defined entry points are outside of the purview of the standard.
236 // For example, there can be only one definition for "main" in a standards
237 // compliant program; however nothing forbids the existence of wmain and
238 // WinMain in the same translation unit.
239 if (FD->isMSVCRTEntryPoint())
David Majnemerd5a42b82013-09-13 09:03:14 +0000240 return false;
241
242 // C++ functions and those whose names are not a simple identifier need
243 // mangling.
244 if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage)
245 return true;
246
247 // C functions are not mangled.
248 if (L == CLanguageLinkage)
249 return false;
250 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000251
252 // Otherwise, no mangling is done outside C++ mode.
253 if (!getASTContext().getLangOpts().CPlusPlus)
254 return false;
255
David Majnemerd5a42b82013-09-13 09:03:14 +0000256 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
257 // C variables are not mangled.
258 if (VD->isExternC())
259 return false;
260
261 // Variables at global scope with non-internal linkage are not mangled.
262 const DeclContext *DC = getEffectiveDeclContext(D);
263 // Check for extern variable declared locally.
264 if (DC->isFunctionOrMethod() && D->hasLinkage())
265 while (!DC->isNamespace() && !DC->isTranslationUnit())
266 DC = getEffectiveParentContext(DC);
267
268 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage &&
269 !isa<VarTemplateSpecializationDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +0000270 return false;
271 }
272
Guy Benyei11169dd2012-12-18 14:30:41 +0000273 return true;
274}
275
276void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
277 StringRef Prefix) {
278 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
279 // Therefore it's really important that we don't decorate the
280 // name with leading underscores or leading/trailing at signs. So, by
281 // default, we emit an asm marker at the start so we get the name right.
282 // Callers can override this with a custom prefix.
283
Guy Benyei11169dd2012-12-18 14:30:41 +0000284 // <mangled-name> ::= ? <name> <type-encoding>
285 Out << Prefix;
286 mangleName(D);
287 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
288 mangleFunctionEncoding(FD);
289 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
290 mangleVariableEncoding(VD);
291 else {
292 // TODO: Fields? Can MSVC even mangle them?
293 // Issue a diagnostic for now.
294 DiagnosticsEngine &Diags = Context.getDiags();
295 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
296 "cannot mangle this declaration yet");
297 Diags.Report(D->getLocation(), DiagID)
298 << D->getSourceRange();
299 }
300}
301
302void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
303 // <type-encoding> ::= <function-class> <function-type>
304
Reid Kleckner18da98e2013-06-24 19:21:52 +0000305 // Since MSVC operates on the type as written and not the canonical type, it
306 // actually matters which decl we have here. MSVC appears to choose the
307 // first, since it is most likely to be the declaration in a header file.
Rafael Espindola8db352d2013-10-17 15:37:26 +0000308 FD = FD->getFirstDecl();
Reid Kleckner18da98e2013-06-24 19:21:52 +0000309
Guy Benyei11169dd2012-12-18 14:30:41 +0000310 // We should never ever see a FunctionNoProtoType at this point.
311 // We don't even know how to mangle their types anyway :).
Reid Kleckner9a7f3e62013-10-08 00:58:57 +0000312 const FunctionProtoType *FT = FD->getType()->castAs<FunctionProtoType>();
Guy Benyei11169dd2012-12-18 14:30:41 +0000313
David Majnemerd5a42b82013-09-13 09:03:14 +0000314 // extern "C" functions can hold entities that must be mangled.
315 // As it stands, these functions still need to get expressed in the full
316 // external name. They have their class and type omitted, replaced with '9'.
317 if (Context.shouldMangleDeclName(FD)) {
318 // First, the function class.
319 mangleFunctionClass(FD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000320
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +0000321 mangleFunctionType(FT, FD);
David Majnemerd5a42b82013-09-13 09:03:14 +0000322 } else
323 Out << '9';
Guy Benyei11169dd2012-12-18 14:30:41 +0000324}
325
326void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
327 // <type-encoding> ::= <storage-class> <variable-type>
328 // <storage-class> ::= 0 # private static member
329 // ::= 1 # protected static member
330 // ::= 2 # public static member
331 // ::= 3 # global
332 // ::= 4 # static local
David Majnemerb4119f72013-12-13 01:06:04 +0000333
Guy Benyei11169dd2012-12-18 14:30:41 +0000334 // The first character in the encoding (after the name) is the storage class.
335 if (VD->isStaticDataMember()) {
336 // If it's a static member, it also encodes the access level.
337 switch (VD->getAccess()) {
338 default:
339 case AS_private: Out << '0'; break;
340 case AS_protected: Out << '1'; break;
341 case AS_public: Out << '2'; break;
342 }
343 }
344 else if (!VD->isStaticLocal())
345 Out << '3';
346 else
347 Out << '4';
348 // Now mangle the type.
349 // <variable-type> ::= <type> <cvr-qualifiers>
350 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
351 // Pointers and references are odd. The type of 'int * const foo;' gets
352 // mangled as 'QAHA' instead of 'PAHB', for example.
353 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
David Majnemerb9a5f2d2014-01-21 20:33:36 +0000354 QualType Ty = VD->getType();
David Majnemer6dda7bb2013-08-15 08:13:23 +0000355 if (Ty->isPointerType() || Ty->isReferenceType() ||
356 Ty->isMemberPointerType()) {
David Majnemera2724ae2013-08-09 05:56:24 +0000357 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer6dda7bb2013-08-15 08:13:23 +0000358 if (PointersAre64Bit)
359 Out << 'E';
360 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) {
361 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true);
362 // Member pointers are suffixed with a back reference to the member
363 // pointer's class name.
364 mangleName(MPT->getClass()->getAsCXXRecordDecl());
365 } else
366 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
David Majnemera2724ae2013-08-09 05:56:24 +0000367 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000368 // Global arrays are funny, too.
David Majnemer5a1b2042013-09-11 04:44:30 +0000369 mangleDecayedArrayType(AT);
Peter Collingbourne2816c022013-04-25 04:25:40 +0000370 if (AT->getElementType()->isArrayType())
371 Out << 'A';
372 else
373 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei11169dd2012-12-18 14:30:41 +0000374 } else {
Peter Collingbourne2816c022013-04-25 04:25:40 +0000375 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer6dda7bb2013-08-15 08:13:23 +0000376 mangleQualifiers(Ty.getLocalQualifiers(), false);
Guy Benyei11169dd2012-12-18 14:30:41 +0000377 }
378}
379
Reid Kleckner96f8f932014-02-05 17:27:08 +0000380void MicrosoftCXXNameMangler::mangleMemberDataPointer(const CXXRecordDecl *RD,
381 const FieldDecl *FD) {
382 // <member-data-pointer> ::= <integer-literal>
383 // ::= $F <number> <number>
384 // ::= $G <number> <number> <number>
385
David Majnemer763584d2014-02-06 10:59:19 +0000386 int64_t FieldOffset;
387 int64_t VBTableOffset;
Reid Kleckner96f8f932014-02-05 17:27:08 +0000388 MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel();
389 if (FD) {
David Majnemer763584d2014-02-06 10:59:19 +0000390 FieldOffset = getASTContext().getFieldOffset(FD);
391 assert(FieldOffset % getASTContext().getCharWidth() == 0 &&
Reid Kleckner96f8f932014-02-05 17:27:08 +0000392 "cannot take address of bitfield");
David Majnemer763584d2014-02-06 10:59:19 +0000393 FieldOffset /= getASTContext().getCharWidth();
394
395 VBTableOffset = 0;
396 } else {
397 FieldOffset = RD->nullFieldOffsetIsZero() ? 0 : -1;
398
399 VBTableOffset = -1;
Reid Kleckner96f8f932014-02-05 17:27:08 +0000400 }
401
David Majnemer763584d2014-02-06 10:59:19 +0000402 char Code = '\0';
Reid Kleckner96f8f932014-02-05 17:27:08 +0000403 switch (IM) {
David Majnemer763584d2014-02-06 10:59:19 +0000404 case MSInheritanceAttr::Keyword_single_inheritance: Code = '0'; break;
405 case MSInheritanceAttr::Keyword_multiple_inheritance: Code = '0'; break;
406 case MSInheritanceAttr::Keyword_virtual_inheritance: Code = 'F'; break;
407 case MSInheritanceAttr::Keyword_unspecified_inheritance: Code = 'G'; break;
Reid Kleckner96f8f932014-02-05 17:27:08 +0000408 }
409
David Majnemer763584d2014-02-06 10:59:19 +0000410 Out << '$' << Code;
Reid Kleckner96f8f932014-02-05 17:27:08 +0000411
David Majnemer763584d2014-02-06 10:59:19 +0000412 mangleNumber(FieldOffset);
413
414 if (MSInheritanceAttr::hasVBPtrOffsetField(IM))
Reid Kleckner96f8f932014-02-05 17:27:08 +0000415 mangleNumber(0);
David Majnemer763584d2014-02-06 10:59:19 +0000416 if (MSInheritanceAttr::hasVBTableOffsetField(IM))
417 mangleNumber(VBTableOffset);
Reid Kleckner96f8f932014-02-05 17:27:08 +0000418}
419
420void
421MicrosoftCXXNameMangler::mangleMemberFunctionPointer(const CXXRecordDecl *RD,
422 const CXXMethodDecl *MD) {
423 // <member-function-pointer> ::= $1? <name>
424 // ::= $H? <name> <number>
425 // ::= $I? <name> <number> <number>
426 // ::= $J? <name> <number> <number> <number>
427 // ::= $0A@
428
429 MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel();
430
431 // The null member function pointer is $0A@ in function templates and crashes
432 // MSVC when used in class templates, so we don't know what they really look
433 // like.
434 if (!MD) {
435 Out << "$0A@";
436 return;
437 }
438
439 char Code = '\0';
440 switch (IM) {
441 case MSInheritanceAttr::Keyword_single_inheritance: Code = '1'; break;
442 case MSInheritanceAttr::Keyword_multiple_inheritance: Code = 'H'; break;
443 case MSInheritanceAttr::Keyword_virtual_inheritance: Code = 'I'; break;
444 case MSInheritanceAttr::Keyword_unspecified_inheritance: Code = 'J'; break;
445 }
446
447 Out << '$' << Code << '?';
448
449 // If non-virtual, mangle the name. If virtual, mangle as a virtual memptr
450 // thunk.
451 uint64_t NVOffset = 0;
452 uint64_t VBTableOffset = 0;
David Majnemer763584d2014-02-06 10:59:19 +0000453 if (MD->isVirtual()) {
Reid Kleckner96f8f932014-02-05 17:27:08 +0000454 MicrosoftVTableContext *VTContext =
455 cast<MicrosoftVTableContext>(getASTContext().getVTableContext());
456 const MicrosoftVTableContext::MethodVFTableLocation &ML =
457 VTContext->getMethodVFTableLocation(GlobalDecl(MD));
458 mangleVirtualMemPtrThunk(MD, ML);
459 NVOffset = ML.VFPtrOffset.getQuantity();
460 VBTableOffset = ML.VBTableIndex * 4;
461 if (ML.VBase) {
462 DiagnosticsEngine &Diags = Context.getDiags();
463 unsigned DiagID = Diags.getCustomDiagID(
464 DiagnosticsEngine::Error,
465 "cannot mangle pointers to member functions from virtual bases");
466 Diags.Report(MD->getLocation(), DiagID);
467 }
468 } else {
469 mangleName(MD);
470 mangleFunctionEncoding(MD);
471 }
472
473 if (MSInheritanceAttr::hasNVOffsetField(/*IsMemberFunction=*/true, IM))
474 mangleNumber(NVOffset);
475 if (MSInheritanceAttr::hasVBPtrOffsetField(IM))
476 mangleNumber(0);
477 if (MSInheritanceAttr::hasVBTableOffsetField(IM))
478 mangleNumber(VBTableOffset);
479}
480
481void MicrosoftCXXNameMangler::mangleVirtualMemPtrThunk(
482 const CXXMethodDecl *MD,
483 const MicrosoftVTableContext::MethodVFTableLocation &ML) {
484 // Get the vftable offset.
485 CharUnits PointerWidth = getASTContext().toCharUnitsFromBits(
486 getASTContext().getTargetInfo().getPointerWidth(0));
487 uint64_t OffsetInVFTable = ML.Index * PointerWidth.getQuantity();
488
489 Out << "?_9";
490 mangleName(MD->getParent());
491 Out << "$B";
492 mangleNumber(OffsetInVFTable);
493 Out << 'A';
494 Out << (PointersAre64Bit ? 'A' : 'E');
495}
496
Guy Benyei11169dd2012-12-18 14:30:41 +0000497void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
498 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
499 const DeclContext *DC = ND->getDeclContext();
500
501 // Always start with the unqualified name.
David Majnemerb4119f72013-12-13 01:06:04 +0000502 mangleUnqualifiedName(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +0000503
504 // If this is an extern variable declared locally, the relevant DeclContext
505 // is that of the containing namespace, or the translation unit.
506 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
507 while (!DC->isNamespace() && !DC->isTranslationUnit())
508 DC = DC->getParent();
509
510 manglePostfix(DC);
511
512 // Terminate the whole name with an '@'.
513 Out << '@';
514}
515
David Majnemer2a816452013-12-09 10:44:32 +0000516void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
517 // <non-negative integer> ::= A@ # when Number == 0
518 // ::= <decimal digit> # when 1 <= Number <= 10
519 // ::= <hex digit>+ @ # when Number >= 10
520 //
521 // <number> ::= [?] <non-negative integer>
Guy Benyei11169dd2012-12-18 14:30:41 +0000522
David Majnemer2a816452013-12-09 10:44:32 +0000523 uint64_t Value = static_cast<uint64_t>(Number);
524 if (Number < 0) {
525 Value = -Value;
Guy Benyei11169dd2012-12-18 14:30:41 +0000526 Out << '?';
Guy Benyei11169dd2012-12-18 14:30:41 +0000527 }
David Majnemer2a816452013-12-09 10:44:32 +0000528
529 if (Value == 0)
530 Out << "A@";
531 else if (Value >= 1 && Value <= 10)
532 Out << (Value - 1);
533 else {
534 // Numbers that are not encoded as decimal digits are represented as nibbles
535 // in the range of ASCII characters 'A' to 'P'.
536 // The number 0x123450 would be encoded as 'BCDEFA'
537 char EncodedNumberBuffer[sizeof(uint64_t) * 2];
538 llvm::MutableArrayRef<char> BufferRef(EncodedNumberBuffer);
539 llvm::MutableArrayRef<char>::reverse_iterator I = BufferRef.rbegin();
540 for (; Value != 0; Value >>= 4)
541 *I++ = 'A' + (Value & 0xf);
542 Out.write(I.base(), I - BufferRef.rbegin());
Guy Benyei11169dd2012-12-18 14:30:41 +0000543 Out << '@';
544 }
545}
546
547static const TemplateDecl *
Reid Kleckner52518862013-03-20 01:40:23 +0000548isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000549 // Check if we have a function template.
550 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
551 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Kleckner52518862013-03-20 01:40:23 +0000552 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei11169dd2012-12-18 14:30:41 +0000553 return TD;
554 }
555 }
556
557 // Check if we have a class template.
558 if (const ClassTemplateSpecializationDecl *Spec =
Reid Kleckner52518862013-03-20 01:40:23 +0000559 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
560 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei11169dd2012-12-18 14:30:41 +0000561 return Spec->getSpecializedTemplate();
562 }
563
564 return 0;
565}
566
567void
568MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
569 DeclarationName Name) {
570 // <unqualified-name> ::= <operator-name>
571 // ::= <ctor-dtor-name>
572 // ::= <source-name>
573 // ::= <template-name>
Reid Kleckner52518862013-03-20 01:40:23 +0000574
Guy Benyei11169dd2012-12-18 14:30:41 +0000575 // Check if we have a template.
Reid Kleckner52518862013-03-20 01:40:23 +0000576 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000577 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Klecknerc16c4472013-07-13 00:43:39 +0000578 // Function templates aren't considered for name back referencing. This
579 // makes sense since function templates aren't likely to occur multiple
580 // times in a symbol.
581 // FIXME: Test alias template mangling with MSVC 2013.
582 if (!isa<ClassTemplateDecl>(TD)) {
583 mangleTemplateInstantiationName(TD, *TemplateArgs);
584 return;
585 }
586
587 // We have a class template.
Guy Benyei11169dd2012-12-18 14:30:41 +0000588 // Here comes the tricky thing: if we need to mangle something like
589 // void foo(A::X<Y>, B::X<Y>),
590 // the X<Y> part is aliased. However, if you need to mangle
591 // void foo(A::X<A::Y>, A::X<B::Y>),
592 // the A::X<> part is not aliased.
593 // That said, from the mangler's perspective we have a structure like this:
594 // namespace[s] -> type[ -> template-parameters]
595 // but from the Clang perspective we have
596 // type [ -> template-parameters]
597 // \-> namespace[s]
598 // What we do is we create a new mangler, mangle the same type (without
599 // a namespace suffix) using the extra mangler with back references
600 // disabled (to avoid infinite recursion) and then use the mangled type
601 // name as a key to check the mangling of different types for aliasing.
602
603 std::string BackReferenceKey;
604 BackRefMap::iterator Found;
605 if (UseNameBackReferences) {
606 llvm::raw_string_ostream Stream(BackReferenceKey);
607 MicrosoftCXXNameMangler Extra(Context, Stream);
608 Extra.disableBackReferences();
609 Extra.mangleUnqualifiedName(ND, Name);
610 Stream.flush();
611
612 Found = NameBackReferences.find(BackReferenceKey);
613 }
614 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Kleckner52518862013-03-20 01:40:23 +0000615 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei11169dd2012-12-18 14:30:41 +0000616 if (UseNameBackReferences && NameBackReferences.size() < 10) {
617 size_t Size = NameBackReferences.size();
618 NameBackReferences[BackReferenceKey] = Size;
619 }
620 } else {
621 Out << Found->second;
622 }
623 return;
624 }
625
626 switch (Name.getNameKind()) {
627 case DeclarationName::Identifier: {
628 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
David Majnemer956bc112013-11-25 17:50:19 +0000629 mangleSourceName(II->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +0000630 break;
631 }
David Majnemerb4119f72013-12-13 01:06:04 +0000632
Guy Benyei11169dd2012-12-18 14:30:41 +0000633 // Otherwise, an anonymous entity. We must have a declaration.
634 assert(ND && "mangling empty name without declaration");
David Majnemerb4119f72013-12-13 01:06:04 +0000635
Guy Benyei11169dd2012-12-18 14:30:41 +0000636 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
637 if (NS->isAnonymousNamespace()) {
638 Out << "?A@";
639 break;
640 }
641 }
David Majnemerb4119f72013-12-13 01:06:04 +0000642
Guy Benyei11169dd2012-12-18 14:30:41 +0000643 // We must have an anonymous struct.
644 const TagDecl *TD = cast<TagDecl>(ND);
645 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
646 assert(TD->getDeclContext() == D->getDeclContext() &&
647 "Typedef should not be in another decl context!");
648 assert(D->getDeclName().getAsIdentifierInfo() &&
649 "Typedef was not named!");
David Majnemer956bc112013-11-25 17:50:19 +0000650 mangleSourceName(D->getDeclName().getAsIdentifierInfo()->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +0000651 break;
652 }
653
David Majnemer956bc112013-11-25 17:50:19 +0000654 if (TD->hasDeclaratorForAnonDecl()) {
David Majnemer50ce8352013-09-17 23:57:10 +0000655 // Anonymous types with no tag or typedef get the name of their
656 // declarator mangled in.
David Majnemer956bc112013-11-25 17:50:19 +0000657 llvm::SmallString<64> Name("<unnamed-type-");
658 Name += TD->getDeclaratorForAnonDecl()->getName();
659 Name += ">";
660 mangleSourceName(Name.str());
661 } else {
David Majnemer50ce8352013-09-17 23:57:10 +0000662 // Anonymous types with no tag, no typedef, or declarator get
David Majnemer956bc112013-11-25 17:50:19 +0000663 // '<unnamed-tag>'.
664 mangleSourceName("<unnamed-tag>");
665 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000666 break;
667 }
David Majnemerb4119f72013-12-13 01:06:04 +0000668
Guy Benyei11169dd2012-12-18 14:30:41 +0000669 case DeclarationName::ObjCZeroArgSelector:
670 case DeclarationName::ObjCOneArgSelector:
671 case DeclarationName::ObjCMultiArgSelector:
672 llvm_unreachable("Can't mangle Objective-C selector names here!");
David Majnemerb4119f72013-12-13 01:06:04 +0000673
Guy Benyei11169dd2012-12-18 14:30:41 +0000674 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000675 if (ND == Structor) {
676 assert(StructorType == Ctor_Complete &&
677 "Should never be asked to mangle a ctor other than complete");
678 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000679 Out << "?0";
680 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000681
Guy Benyei11169dd2012-12-18 14:30:41 +0000682 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000683 if (ND == Structor)
684 // If the named decl is the C++ destructor we're mangling,
685 // use the type we were given.
686 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
687 else
Reid Klecknere7de47e2013-07-22 13:51:44 +0000688 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000689 // class with a destructor is declared within a destructor.
Reid Klecknere7de47e2013-07-22 13:51:44 +0000690 mangleCXXDtorType(Dtor_Base);
Guy Benyei11169dd2012-12-18 14:30:41 +0000691 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000692
Guy Benyei11169dd2012-12-18 14:30:41 +0000693 case DeclarationName::CXXConversionFunctionName:
694 // <operator-name> ::= ?B # (cast)
695 // The target type is encoded as the return type.
696 Out << "?B";
697 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000698
Guy Benyei11169dd2012-12-18 14:30:41 +0000699 case DeclarationName::CXXOperatorName:
700 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
701 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000702
Guy Benyei11169dd2012-12-18 14:30:41 +0000703 case DeclarationName::CXXLiteralOperatorName: {
704 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
705 DiagnosticsEngine Diags = Context.getDiags();
706 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
707 "cannot mangle this literal operator yet");
708 Diags.Report(ND->getLocation(), DiagID);
709 break;
710 }
David Majnemerb4119f72013-12-13 01:06:04 +0000711
Guy Benyei11169dd2012-12-18 14:30:41 +0000712 case DeclarationName::CXXUsingDirective:
713 llvm_unreachable("Can't mangle a using directive name!");
714 }
715}
716
717void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
718 bool NoFunction) {
719 // <postfix> ::= <unqualified-name> [<postfix>]
720 // ::= <substitution> [<postfix>]
721
722 if (!DC) return;
723
724 while (isa<LinkageSpecDecl>(DC))
725 DC = DC->getParent();
726
727 if (DC->isTranslationUnit())
728 return;
729
730 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedman8978a9d2013-07-10 01:13:27 +0000731 DiagnosticsEngine Diags = Context.getDiags();
732 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
733 "cannot mangle a local inside this block yet");
734 Diags.Report(BD->getLocation(), DiagID);
735
736 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
737 // for how this should be done.
738 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei11169dd2012-12-18 14:30:41 +0000739 Out << '@';
740 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir3b4c30b2013-05-09 19:17:11 +0000741 } else if (isa<CapturedDecl>(DC)) {
742 // Skip CapturedDecl context.
743 manglePostfix(DC->getParent(), NoFunction);
744 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000745 }
746
747 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
748 return;
749 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
750 mangleObjCMethodName(Method);
751 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
752 mangleLocalName(Func);
753 else {
754 mangleUnqualifiedName(cast<NamedDecl>(DC));
755 manglePostfix(DC->getParent(), NoFunction);
756 }
757}
758
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000759void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknere7de47e2013-07-22 13:51:44 +0000760 // Microsoft uses the names on the case labels for these dtor variants. Clang
761 // uses the Itanium terminology internally. Everything in this ABI delegates
762 // towards the base dtor.
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000763 switch (T) {
Reid Klecknere7de47e2013-07-22 13:51:44 +0000764 // <operator-name> ::= ?1 # destructor
765 case Dtor_Base: Out << "?1"; return;
766 // <operator-name> ::= ?_D # vbase destructor
767 case Dtor_Complete: Out << "?_D"; return;
768 // <operator-name> ::= ?_G # scalar deleting destructor
769 case Dtor_Deleting: Out << "?_G"; return;
770 // <operator-name> ::= ?_E # vector deleting destructor
771 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
772 // it.
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000773 }
774 llvm_unreachable("Unsupported dtor type?");
775}
776
Guy Benyei11169dd2012-12-18 14:30:41 +0000777void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
778 SourceLocation Loc) {
779 switch (OO) {
780 // ?0 # constructor
781 // ?1 # destructor
782 // <operator-name> ::= ?2 # new
783 case OO_New: Out << "?2"; break;
784 // <operator-name> ::= ?3 # delete
785 case OO_Delete: Out << "?3"; break;
786 // <operator-name> ::= ?4 # =
787 case OO_Equal: Out << "?4"; break;
788 // <operator-name> ::= ?5 # >>
789 case OO_GreaterGreater: Out << "?5"; break;
790 // <operator-name> ::= ?6 # <<
791 case OO_LessLess: Out << "?6"; break;
792 // <operator-name> ::= ?7 # !
793 case OO_Exclaim: Out << "?7"; break;
794 // <operator-name> ::= ?8 # ==
795 case OO_EqualEqual: Out << "?8"; break;
796 // <operator-name> ::= ?9 # !=
797 case OO_ExclaimEqual: Out << "?9"; break;
798 // <operator-name> ::= ?A # []
799 case OO_Subscript: Out << "?A"; break;
800 // ?B # conversion
801 // <operator-name> ::= ?C # ->
802 case OO_Arrow: Out << "?C"; break;
803 // <operator-name> ::= ?D # *
804 case OO_Star: Out << "?D"; break;
805 // <operator-name> ::= ?E # ++
806 case OO_PlusPlus: Out << "?E"; break;
807 // <operator-name> ::= ?F # --
808 case OO_MinusMinus: Out << "?F"; break;
809 // <operator-name> ::= ?G # -
810 case OO_Minus: Out << "?G"; break;
811 // <operator-name> ::= ?H # +
812 case OO_Plus: Out << "?H"; break;
813 // <operator-name> ::= ?I # &
814 case OO_Amp: Out << "?I"; break;
815 // <operator-name> ::= ?J # ->*
816 case OO_ArrowStar: Out << "?J"; break;
817 // <operator-name> ::= ?K # /
818 case OO_Slash: Out << "?K"; break;
819 // <operator-name> ::= ?L # %
820 case OO_Percent: Out << "?L"; break;
821 // <operator-name> ::= ?M # <
822 case OO_Less: Out << "?M"; break;
823 // <operator-name> ::= ?N # <=
824 case OO_LessEqual: Out << "?N"; break;
825 // <operator-name> ::= ?O # >
826 case OO_Greater: Out << "?O"; break;
827 // <operator-name> ::= ?P # >=
828 case OO_GreaterEqual: Out << "?P"; break;
829 // <operator-name> ::= ?Q # ,
830 case OO_Comma: Out << "?Q"; break;
831 // <operator-name> ::= ?R # ()
832 case OO_Call: Out << "?R"; break;
833 // <operator-name> ::= ?S # ~
834 case OO_Tilde: Out << "?S"; break;
835 // <operator-name> ::= ?T # ^
836 case OO_Caret: Out << "?T"; break;
837 // <operator-name> ::= ?U # |
838 case OO_Pipe: Out << "?U"; break;
839 // <operator-name> ::= ?V # &&
840 case OO_AmpAmp: Out << "?V"; break;
841 // <operator-name> ::= ?W # ||
842 case OO_PipePipe: Out << "?W"; break;
843 // <operator-name> ::= ?X # *=
844 case OO_StarEqual: Out << "?X"; break;
845 // <operator-name> ::= ?Y # +=
846 case OO_PlusEqual: Out << "?Y"; break;
847 // <operator-name> ::= ?Z # -=
848 case OO_MinusEqual: Out << "?Z"; break;
849 // <operator-name> ::= ?_0 # /=
850 case OO_SlashEqual: Out << "?_0"; break;
851 // <operator-name> ::= ?_1 # %=
852 case OO_PercentEqual: Out << "?_1"; break;
853 // <operator-name> ::= ?_2 # >>=
854 case OO_GreaterGreaterEqual: Out << "?_2"; break;
855 // <operator-name> ::= ?_3 # <<=
856 case OO_LessLessEqual: Out << "?_3"; break;
857 // <operator-name> ::= ?_4 # &=
858 case OO_AmpEqual: Out << "?_4"; break;
859 // <operator-name> ::= ?_5 # |=
860 case OO_PipeEqual: Out << "?_5"; break;
861 // <operator-name> ::= ?_6 # ^=
862 case OO_CaretEqual: Out << "?_6"; break;
863 // ?_7 # vftable
864 // ?_8 # vbtable
865 // ?_9 # vcall
866 // ?_A # typeof
867 // ?_B # local static guard
868 // ?_C # string
869 // ?_D # vbase destructor
870 // ?_E # vector deleting destructor
871 // ?_F # default constructor closure
872 // ?_G # scalar deleting destructor
873 // ?_H # vector constructor iterator
874 // ?_I # vector destructor iterator
875 // ?_J # vector vbase constructor iterator
876 // ?_K # virtual displacement map
877 // ?_L # eh vector constructor iterator
878 // ?_M # eh vector destructor iterator
879 // ?_N # eh vector vbase constructor iterator
880 // ?_O # copy constructor closure
881 // ?_P<name> # udt returning <name>
882 // ?_Q # <unknown>
883 // ?_R0 # RTTI Type Descriptor
884 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
885 // ?_R2 # RTTI Base Class Array
886 // ?_R3 # RTTI Class Hierarchy Descriptor
887 // ?_R4 # RTTI Complete Object Locator
888 // ?_S # local vftable
889 // ?_T # local vftable constructor closure
890 // <operator-name> ::= ?_U # new[]
891 case OO_Array_New: Out << "?_U"; break;
892 // <operator-name> ::= ?_V # delete[]
893 case OO_Array_Delete: Out << "?_V"; break;
David Majnemerb4119f72013-12-13 01:06:04 +0000894
Guy Benyei11169dd2012-12-18 14:30:41 +0000895 case OO_Conditional: {
896 DiagnosticsEngine &Diags = Context.getDiags();
897 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
898 "cannot mangle this conditional operator yet");
899 Diags.Report(Loc, DiagID);
900 break;
901 }
David Majnemerb4119f72013-12-13 01:06:04 +0000902
Guy Benyei11169dd2012-12-18 14:30:41 +0000903 case OO_None:
904 case NUM_OVERLOADED_OPERATORS:
905 llvm_unreachable("Not an overloaded operator");
906 }
907}
908
David Majnemer956bc112013-11-25 17:50:19 +0000909void MicrosoftCXXNameMangler::mangleSourceName(StringRef Name) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000910 // <source name> ::= <identifier> @
Guy Benyei11169dd2012-12-18 14:30:41 +0000911 BackRefMap::iterator Found;
912 if (UseNameBackReferences)
David Majnemer956bc112013-11-25 17:50:19 +0000913 Found = NameBackReferences.find(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000914 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
David Majnemer956bc112013-11-25 17:50:19 +0000915 Out << Name << '@';
Guy Benyei11169dd2012-12-18 14:30:41 +0000916 if (UseNameBackReferences && NameBackReferences.size() < 10) {
917 size_t Size = NameBackReferences.size();
David Majnemer956bc112013-11-25 17:50:19 +0000918 NameBackReferences[Name] = Size;
Guy Benyei11169dd2012-12-18 14:30:41 +0000919 }
920 } else {
921 Out << Found->second;
922 }
923}
924
925void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
926 Context.mangleObjCMethodName(MD, Out);
927}
928
929// Find out how many function decls live above this one and return an integer
930// suitable for use as the number in a numbered anonymous scope.
931// TODO: Memoize.
932static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
933 const DeclContext *DC = FD->getParent();
934 int level = 1;
935
936 while (DC && !DC->isTranslationUnit()) {
937 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
938 DC = DC->getParent();
939 }
940
941 return 2*level;
942}
943
944void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
945 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
946 // <numbered-anonymous-scope> ::= ? <number>
947 // Even though the name is rendered in reverse order (e.g.
948 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
949 // innermost. So a method bar in class C local to function foo gets mangled
950 // as something like:
951 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
952 // This is more apparent when you have a type nested inside a method of a
953 // type nested inside a function. A method baz in class D local to method
954 // bar of class C local to function foo gets mangled as:
955 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
956 // This scheme is general enough to support GCC-style nested
957 // functions. You could have a method baz of class C inside a function bar
958 // inside a function foo, like so:
959 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +0000960 unsigned NestLevel = getLocalNestingLevel(FD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000961 Out << '?';
962 mangleNumber(NestLevel);
963 Out << '?';
964 mangle(FD, "?");
965}
966
967void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
968 const TemplateDecl *TD,
Reid Kleckner52518862013-03-20 01:40:23 +0000969 const TemplateArgumentList &TemplateArgs) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000970 // <template-name> ::= <unscoped-template-name> <template-args>
971 // ::= <substitution>
972 // Always start with the unqualified name.
973
974 // Templates have their own context for back references.
975 ArgBackRefMap OuterArgsContext;
976 BackRefMap OuterTemplateContext;
977 NameBackReferences.swap(OuterTemplateContext);
978 TypeBackReferences.swap(OuterArgsContext);
979
980 mangleUnscopedTemplateName(TD);
Reid Kleckner52518862013-03-20 01:40:23 +0000981 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei11169dd2012-12-18 14:30:41 +0000982
983 // Restore the previous back reference contexts.
984 NameBackReferences.swap(OuterTemplateContext);
985 TypeBackReferences.swap(OuterArgsContext);
986}
987
988void
989MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
990 // <unscoped-template-name> ::= ?$ <unqualified-name>
991 Out << "?$";
992 mangleUnqualifiedName(TD);
993}
994
995void
996MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
997 bool IsBoolean) {
998 // <integer-literal> ::= $0 <number>
999 Out << "$0";
1000 // Make sure booleans are encoded as 0/1.
1001 if (IsBoolean && Value.getBoolValue())
1002 mangleNumber(1);
1003 else
David Majnemer2a816452013-12-09 10:44:32 +00001004 mangleNumber(Value.getSExtValue());
Guy Benyei11169dd2012-12-18 14:30:41 +00001005}
1006
1007void
1008MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
1009 // See if this is a constant expression.
1010 llvm::APSInt Value;
1011 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
1012 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
1013 return;
1014 }
1015
David Majnemer8eaab6f2013-08-13 06:32:20 +00001016 const CXXUuidofExpr *UE = 0;
1017 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
1018 if (UO->getOpcode() == UO_AddrOf)
1019 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
1020 } else
1021 UE = dyn_cast<CXXUuidofExpr>(E);
1022
1023 if (UE) {
1024 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
1025 // const __s_GUID _GUID_{lower case UUID with underscores}
1026 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
1027 std::string Name = "_GUID_" + Uuid.lower();
1028 std::replace(Name.begin(), Name.end(), '-', '_');
1029
David Majnemere9cab2f2013-08-13 09:17:25 +00001030 // If we had to peek through an address-of operator, treat this like we are
David Majnemer8eaab6f2013-08-13 06:32:20 +00001031 // dealing with a pointer type. Otherwise, treat it like a const reference.
1032 //
1033 // N.B. This matches up with the handling of TemplateArgument::Declaration
1034 // in mangleTemplateArg
1035 if (UE == E)
1036 Out << "$E?";
1037 else
1038 Out << "$1?";
1039 Out << Name << "@@3U__s_GUID@@B";
1040 return;
1041 }
1042
Guy Benyei11169dd2012-12-18 14:30:41 +00001043 // As bad as this diagnostic is, it's better than crashing.
1044 DiagnosticsEngine &Diags = Context.getDiags();
1045 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1046 "cannot yet mangle expression type %0");
1047 Diags.Report(E->getExprLoc(), DiagID)
1048 << E->getStmtClassName() << E->getSourceRange();
1049}
1050
1051void
Reid Kleckner52518862013-03-20 01:40:23 +00001052MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
1053 const TemplateArgumentList &TemplateArgs) {
Reid Kleckner96f8f932014-02-05 17:27:08 +00001054 // <template-args> ::= <template-arg>+ @
Guy Benyei11169dd2012-12-18 14:30:41 +00001055 unsigned NumTemplateArgs = TemplateArgs.size();
1056 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Kleckner52518862013-03-20 01:40:23 +00001057 const TemplateArgument &TA = TemplateArgs[i];
David Majnemer08177c52013-08-27 08:21:25 +00001058 mangleTemplateArg(TD, TA);
Guy Benyei11169dd2012-12-18 14:30:41 +00001059 }
1060 Out << '@';
1061}
1062
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001063void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
David Majnemer08177c52013-08-27 08:21:25 +00001064 const TemplateArgument &TA) {
Reid Kleckner96f8f932014-02-05 17:27:08 +00001065 // <template-arg> ::= <type>
1066 // ::= <integer-literal>
1067 // ::= <member-data-pointer>
1068 // ::= <member-function-pointer>
1069 // ::= $E? <name> <type-encoding>
1070 // ::= $1? <name> <type-encoding>
1071 // ::= $0A@
1072 // ::= <template-args>
1073
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001074 switch (TA.getKind()) {
1075 case TemplateArgument::Null:
1076 llvm_unreachable("Can't mangle null template arguments!");
David Majnemer08177c52013-08-27 08:21:25 +00001077 case TemplateArgument::TemplateExpansion:
1078 llvm_unreachable("Can't mangle template expansion arguments!");
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001079 case TemplateArgument::Type: {
1080 QualType T = TA.getAsType();
1081 mangleType(T, SourceRange(), QMM_Escape);
1082 break;
1083 }
David Majnemere8fdc062013-08-13 01:25:35 +00001084 case TemplateArgument::Declaration: {
1085 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl());
Reid Kleckner09b47d12014-02-05 18:59:38 +00001086 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ND)) {
Reid Kleckner96f8f932014-02-05 17:27:08 +00001087 mangleMemberDataPointer(cast<CXXRecordDecl>(FD->getParent()), FD);
Reid Kleckner09b47d12014-02-05 18:59:38 +00001088 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Nick Lewycky1f529662014-02-05 23:53:29 +00001089 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
Reid Kleckner09b47d12014-02-05 18:59:38 +00001090 if (MD && MD->isInstance())
1091 mangleMemberFunctionPointer(MD->getParent(), MD);
1092 else
Nick Lewycky1f529662014-02-05 23:53:29 +00001093 mangle(FD, "$1?");
Reid Kleckner09b47d12014-02-05 18:59:38 +00001094 } else {
Reid Kleckner96f8f932014-02-05 17:27:08 +00001095 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?");
Reid Kleckner09b47d12014-02-05 18:59:38 +00001096 }
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001097 break;
David Majnemere8fdc062013-08-13 01:25:35 +00001098 }
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001099 case TemplateArgument::Integral:
1100 mangleIntegerLiteral(TA.getAsIntegral(),
1101 TA.getIntegralType()->isBooleanType());
1102 break;
Reid Kleckner96f8f932014-02-05 17:27:08 +00001103 case TemplateArgument::NullPtr: {
1104 QualType T = TA.getNullPtrType();
1105 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
David Majnemer763584d2014-02-06 10:59:19 +00001106 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
Reid Kleckner96f8f932014-02-05 17:27:08 +00001107 if (MPT->isMemberFunctionPointerType())
1108 mangleMemberFunctionPointer(RD, 0);
1109 else
1110 mangleMemberDataPointer(RD, 0);
1111 } else {
1112 Out << "$0A@";
1113 }
David Majnemerae465ef2013-08-05 21:33:59 +00001114 break;
Reid Kleckner96f8f932014-02-05 17:27:08 +00001115 }
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001116 case TemplateArgument::Expression:
1117 mangleExpression(TA.getAsExpr());
1118 break;
1119 case TemplateArgument::Pack:
1120 // Unlike Itanium, there is no character code to indicate an argument pack.
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001121 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
1122 I != E; ++I)
David Majnemer08177c52013-08-27 08:21:25 +00001123 mangleTemplateArg(TD, *I);
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001124 break;
1125 case TemplateArgument::Template:
David Majnemer0db0ca42013-08-05 22:26:46 +00001126 mangleType(cast<TagDecl>(
1127 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
1128 break;
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001129 }
1130}
1131
Guy Benyei11169dd2012-12-18 14:30:41 +00001132void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
1133 bool IsMember) {
1134 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
1135 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
1136 // 'I' means __restrict (32/64-bit).
1137 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
1138 // keyword!
1139 // <base-cvr-qualifiers> ::= A # near
1140 // ::= B # near const
1141 // ::= C # near volatile
1142 // ::= D # near const volatile
1143 // ::= E # far (16-bit)
1144 // ::= F # far const (16-bit)
1145 // ::= G # far volatile (16-bit)
1146 // ::= H # far const volatile (16-bit)
1147 // ::= I # huge (16-bit)
1148 // ::= J # huge const (16-bit)
1149 // ::= K # huge volatile (16-bit)
1150 // ::= L # huge const volatile (16-bit)
1151 // ::= M <basis> # based
1152 // ::= N <basis> # based const
1153 // ::= O <basis> # based volatile
1154 // ::= P <basis> # based const volatile
1155 // ::= Q # near member
1156 // ::= R # near const member
1157 // ::= S # near volatile member
1158 // ::= T # near const volatile member
1159 // ::= U # far member (16-bit)
1160 // ::= V # far const member (16-bit)
1161 // ::= W # far volatile member (16-bit)
1162 // ::= X # far const volatile member (16-bit)
1163 // ::= Y # huge member (16-bit)
1164 // ::= Z # huge const member (16-bit)
1165 // ::= 0 # huge volatile member (16-bit)
1166 // ::= 1 # huge const volatile member (16-bit)
1167 // ::= 2 <basis> # based member
1168 // ::= 3 <basis> # based const member
1169 // ::= 4 <basis> # based volatile member
1170 // ::= 5 <basis> # based const volatile member
1171 // ::= 6 # near function (pointers only)
1172 // ::= 7 # far function (pointers only)
1173 // ::= 8 # near method (pointers only)
1174 // ::= 9 # far method (pointers only)
1175 // ::= _A <basis> # based function (pointers only)
1176 // ::= _B <basis> # based function (far?) (pointers only)
1177 // ::= _C <basis> # based method (pointers only)
1178 // ::= _D <basis> # based method (far?) (pointers only)
1179 // ::= _E # block (Clang)
1180 // <basis> ::= 0 # __based(void)
1181 // ::= 1 # __based(segment)?
1182 // ::= 2 <name> # __based(name)
1183 // ::= 3 # ?
1184 // ::= 4 # ?
1185 // ::= 5 # not really based
1186 bool HasConst = Quals.hasConst(),
1187 HasVolatile = Quals.hasVolatile();
David Majnemer89594f32013-08-05 22:43:06 +00001188
Guy Benyei11169dd2012-12-18 14:30:41 +00001189 if (!IsMember) {
1190 if (HasConst && HasVolatile) {
1191 Out << 'D';
1192 } else if (HasVolatile) {
1193 Out << 'C';
1194 } else if (HasConst) {
1195 Out << 'B';
1196 } else {
1197 Out << 'A';
1198 }
1199 } else {
1200 if (HasConst && HasVolatile) {
1201 Out << 'T';
1202 } else if (HasVolatile) {
1203 Out << 'S';
1204 } else if (HasConst) {
1205 Out << 'R';
1206 } else {
1207 Out << 'Q';
1208 }
1209 }
1210
1211 // FIXME: For now, just drop all extension qualifiers on the floor.
1212}
1213
1214void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1215 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1216 // ::= Q # const
1217 // ::= R # volatile
1218 // ::= S # const volatile
1219 bool HasConst = Quals.hasConst(),
1220 HasVolatile = Quals.hasVolatile();
1221 if (HasConst && HasVolatile) {
1222 Out << 'S';
1223 } else if (HasVolatile) {
1224 Out << 'R';
1225 } else if (HasConst) {
1226 Out << 'Q';
1227 } else {
1228 Out << 'P';
1229 }
1230}
1231
1232void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1233 SourceRange Range) {
Reid Kleckner18da98e2013-06-24 19:21:52 +00001234 // MSVC will backreference two canonically equivalent types that have slightly
1235 // different manglings when mangled alone.
David Majnemer5a1b2042013-09-11 04:44:30 +00001236
1237 // Decayed types do not match up with non-decayed versions of the same type.
1238 //
1239 // e.g.
1240 // void (*x)(void) will not form a backreference with void x(void)
1241 void *TypePtr;
1242 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1243 TypePtr = DT->getOriginalType().getCanonicalType().getAsOpaquePtr();
1244 // If the original parameter was textually written as an array,
1245 // instead treat the decayed parameter like it's const.
1246 //
1247 // e.g.
1248 // int [] -> int * const
1249 if (DT->getOriginalType()->isArrayType())
1250 T = T.withConst();
1251 } else
1252 TypePtr = T.getCanonicalType().getAsOpaquePtr();
1253
Guy Benyei11169dd2012-12-18 14:30:41 +00001254 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1255
1256 if (Found == TypeBackReferences.end()) {
1257 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1258
David Majnemer5a1b2042013-09-11 04:44:30 +00001259 mangleType(T, Range, QMM_Drop);
Guy Benyei11169dd2012-12-18 14:30:41 +00001260
1261 // See if it's worth creating a back reference.
1262 // Only types longer than 1 character are considered
1263 // and only 10 back references slots are available:
1264 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1265 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1266 size_t Size = TypeBackReferences.size();
1267 TypeBackReferences[TypePtr] = Size;
1268 }
1269 } else {
1270 Out << Found->second;
1271 }
1272}
1273
1274void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourne2816c022013-04-25 04:25:40 +00001275 QualifierMangleMode QMM) {
Reid Kleckner18da98e2013-06-24 19:21:52 +00001276 // Don't use the canonical types. MSVC includes things like 'const' on
1277 // pointer arguments to function pointers that canonicalization strips away.
1278 T = T.getDesugaredType(getASTContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001279 Qualifiers Quals = T.getLocalQualifiers();
Reid Kleckner18da98e2013-06-24 19:21:52 +00001280 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1281 // If there were any Quals, getAsArrayType() pushed them onto the array
1282 // element type.
Peter Collingbourne2816c022013-04-25 04:25:40 +00001283 if (QMM == QMM_Mangle)
1284 Out << 'A';
1285 else if (QMM == QMM_Escape || QMM == QMM_Result)
1286 Out << "$$B";
Reid Kleckner18da98e2013-06-24 19:21:52 +00001287 mangleArrayType(AT);
Peter Collingbourne2816c022013-04-25 04:25:40 +00001288 return;
1289 }
1290
1291 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1292 T->isBlockPointerType();
1293
1294 switch (QMM) {
1295 case QMM_Drop:
1296 break;
1297 case QMM_Mangle:
1298 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1299 Out << '6';
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001300 mangleFunctionType(FT);
Peter Collingbourne2816c022013-04-25 04:25:40 +00001301 return;
1302 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001303 mangleQualifiers(Quals, false);
Peter Collingbourne2816c022013-04-25 04:25:40 +00001304 break;
1305 case QMM_Escape:
1306 if (!IsPointer && Quals) {
1307 Out << "$$C";
1308 mangleQualifiers(Quals, false);
1309 }
1310 break;
1311 case QMM_Result:
1312 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1313 Out << '?';
1314 mangleQualifiers(Quals, false);
1315 }
1316 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001317 }
1318
Peter Collingbourne2816c022013-04-25 04:25:40 +00001319 // We have to mangle these now, while we still have enough information.
1320 if (IsPointer)
1321 manglePointerQualifiers(Quals);
1322 const Type *ty = T.getTypePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +00001323
1324 switch (ty->getTypeClass()) {
1325#define ABSTRACT_TYPE(CLASS, PARENT)
1326#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1327 case Type::CLASS: \
1328 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1329 return;
1330#define TYPE(CLASS, PARENT) \
1331 case Type::CLASS: \
1332 mangleType(cast<CLASS##Type>(ty), Range); \
1333 break;
1334#include "clang/AST/TypeNodes.def"
1335#undef ABSTRACT_TYPE
1336#undef NON_CANONICAL_TYPE
1337#undef TYPE
1338 }
1339}
1340
1341void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1342 SourceRange Range) {
1343 // <type> ::= <builtin-type>
1344 // <builtin-type> ::= X # void
1345 // ::= C # signed char
1346 // ::= D # char
1347 // ::= E # unsigned char
1348 // ::= F # short
1349 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1350 // ::= H # int
1351 // ::= I # unsigned int
1352 // ::= J # long
1353 // ::= K # unsigned long
1354 // L # <none>
1355 // ::= M # float
1356 // ::= N # double
1357 // ::= O # long double (__float80 is mangled differently)
1358 // ::= _J # long long, __int64
1359 // ::= _K # unsigned long long, __int64
1360 // ::= _L # __int128
1361 // ::= _M # unsigned __int128
1362 // ::= _N # bool
1363 // _O # <array in parameter>
1364 // ::= _T # __float80 (Intel)
1365 // ::= _W # wchar_t
1366 // ::= _Z # __float80 (Digital Mars)
1367 switch (T->getKind()) {
1368 case BuiltinType::Void: Out << 'X'; break;
1369 case BuiltinType::SChar: Out << 'C'; break;
1370 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1371 case BuiltinType::UChar: Out << 'E'; break;
1372 case BuiltinType::Short: Out << 'F'; break;
1373 case BuiltinType::UShort: Out << 'G'; break;
1374 case BuiltinType::Int: Out << 'H'; break;
1375 case BuiltinType::UInt: Out << 'I'; break;
1376 case BuiltinType::Long: Out << 'J'; break;
1377 case BuiltinType::ULong: Out << 'K'; break;
1378 case BuiltinType::Float: Out << 'M'; break;
1379 case BuiltinType::Double: Out << 'N'; break;
1380 // TODO: Determine size and mangle accordingly
1381 case BuiltinType::LongDouble: Out << 'O'; break;
1382 case BuiltinType::LongLong: Out << "_J"; break;
1383 case BuiltinType::ULongLong: Out << "_K"; break;
1384 case BuiltinType::Int128: Out << "_L"; break;
1385 case BuiltinType::UInt128: Out << "_M"; break;
1386 case BuiltinType::Bool: Out << "_N"; break;
1387 case BuiltinType::WChar_S:
1388 case BuiltinType::WChar_U: Out << "_W"; break;
1389
1390#define BUILTIN_TYPE(Id, SingletonId)
1391#define PLACEHOLDER_TYPE(Id, SingletonId) \
1392 case BuiltinType::Id:
1393#include "clang/AST/BuiltinTypes.def"
1394 case BuiltinType::Dependent:
1395 llvm_unreachable("placeholder types shouldn't get to name mangling");
1396
1397 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1398 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1399 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00001400
1401 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1402 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1403 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1404 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1405 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1406 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei61054192013-02-07 10:55:47 +00001407 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001408 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
David Majnemerb4119f72013-12-13 01:06:04 +00001409
Guy Benyei11169dd2012-12-18 14:30:41 +00001410 case BuiltinType::NullPtr: Out << "$$T"; break;
1411
1412 case BuiltinType::Char16:
1413 case BuiltinType::Char32:
1414 case BuiltinType::Half: {
1415 DiagnosticsEngine &Diags = Context.getDiags();
1416 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1417 "cannot mangle this built-in %0 type yet");
1418 Diags.Report(Range.getBegin(), DiagID)
1419 << T->getName(Context.getASTContext().getPrintingPolicy())
1420 << Range;
1421 break;
1422 }
1423 }
1424}
1425
1426// <type> ::= <function-type>
1427void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1428 SourceRange) {
1429 // Structors only appear in decls, so at this point we know it's not a
1430 // structor type.
1431 // FIXME: This may not be lambda-friendly.
1432 Out << "$$A6";
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001433 mangleFunctionType(T);
Guy Benyei11169dd2012-12-18 14:30:41 +00001434}
1435void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1436 SourceRange) {
1437 llvm_unreachable("Can't mangle K&R function prototypes");
1438}
1439
Peter Collingbourne2816c022013-04-25 04:25:40 +00001440void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1441 const FunctionDecl *D,
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001442 bool ForceInstMethod) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001443 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1444 // <return-type> <argument-list> <throw-spec>
1445 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1446
Reid Kleckner18da98e2013-06-24 19:21:52 +00001447 SourceRange Range;
1448 if (D) Range = D->getSourceRange();
1449
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001450 bool IsStructor = false, IsInstMethod = ForceInstMethod;
1451 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(D)) {
1452 if (MD->isInstance())
1453 IsInstMethod = true;
1454 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
1455 IsStructor = true;
1456 }
1457
Guy Benyei11169dd2012-12-18 14:30:41 +00001458 // If this is a C++ instance method, mangle the CVR qualifiers for the
1459 // this pointer.
David Majnemer6dda7bb2013-08-15 08:13:23 +00001460 if (IsInstMethod) {
1461 if (PointersAre64Bit)
1462 Out << 'E';
Guy Benyei11169dd2012-12-18 14:30:41 +00001463 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
David Majnemer6dda7bb2013-08-15 08:13:23 +00001464 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001465
Reid Klecknerc5cc3382013-09-25 22:28:52 +00001466 mangleCallingConvention(T);
Guy Benyei11169dd2012-12-18 14:30:41 +00001467
1468 // <return-type> ::= <type>
1469 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001470 if (IsStructor) {
1471 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1472 StructorType == Dtor_Deleting) {
1473 // The scalar deleting destructor takes an extra int argument.
1474 // However, the FunctionType generated has 0 arguments.
1475 // FIXME: This is a temporary hack.
1476 // Maybe should fix the FunctionType creation instead?
Timur Iskhodzhanovf46993e2013-08-26 10:32:04 +00001477 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001478 return;
1479 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001480 Out << '@';
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001481 } else {
Alp Toker314cc812014-01-25 16:55:45 +00001482 QualType ResultType = Proto->getReturnType();
David Majnemer6dda7bb2013-08-15 08:13:23 +00001483 if (ResultType->isVoidType())
1484 ResultType = ResultType.getUnqualifiedType();
1485 mangleType(ResultType, Range, QMM_Result);
Guy Benyei11169dd2012-12-18 14:30:41 +00001486 }
1487
1488 // <argument-list> ::= X # void
1489 // ::= <type>+ @
1490 // ::= <type>* Z # varargs
Alp Toker9cacbab2014-01-20 20:26:09 +00001491 if (Proto->getNumParams() == 0 && !Proto->isVariadic()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001492 Out << 'X';
1493 } else {
Reid Kleckner18da98e2013-06-24 19:21:52 +00001494 // Happens for function pointer type arguments for example.
Alp Toker9cacbab2014-01-20 20:26:09 +00001495 for (FunctionProtoType::param_type_iterator
1496 Arg = Proto->param_type_begin(),
1497 ArgEnd = Proto->param_type_end();
Reid Kleckner18da98e2013-06-24 19:21:52 +00001498 Arg != ArgEnd; ++Arg)
1499 mangleArgumentType(*Arg, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001500 // <builtin-type> ::= Z # ellipsis
1501 if (Proto->isVariadic())
1502 Out << 'Z';
1503 else
1504 Out << '@';
1505 }
1506
1507 mangleThrowSpecification(Proto);
1508}
1509
1510void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Kleckner369f3162013-05-14 20:30:42 +00001511 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1512 // # pointer. in 64-bit mode *all*
1513 // # 'this' pointers are 64-bit.
1514 // ::= <global-function>
1515 // <member-function> ::= A # private: near
1516 // ::= B # private: far
1517 // ::= C # private: static near
1518 // ::= D # private: static far
1519 // ::= E # private: virtual near
1520 // ::= F # private: virtual far
Reid Kleckner369f3162013-05-14 20:30:42 +00001521 // ::= I # protected: near
1522 // ::= J # protected: far
1523 // ::= K # protected: static near
1524 // ::= L # protected: static far
1525 // ::= M # protected: virtual near
1526 // ::= N # protected: virtual far
Reid Kleckner369f3162013-05-14 20:30:42 +00001527 // ::= Q # public: near
1528 // ::= R # public: far
1529 // ::= S # public: static near
1530 // ::= T # public: static far
1531 // ::= U # public: virtual near
1532 // ::= V # public: virtual far
Reid Kleckner369f3162013-05-14 20:30:42 +00001533 // <global-function> ::= Y # global near
1534 // ::= Z # global far
Guy Benyei11169dd2012-12-18 14:30:41 +00001535 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1536 switch (MD->getAccess()) {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00001537 case AS_none:
1538 llvm_unreachable("Unsupported access specifier");
Guy Benyei11169dd2012-12-18 14:30:41 +00001539 case AS_private:
1540 if (MD->isStatic())
1541 Out << 'C';
1542 else if (MD->isVirtual())
1543 Out << 'E';
1544 else
1545 Out << 'A';
1546 break;
1547 case AS_protected:
1548 if (MD->isStatic())
1549 Out << 'K';
1550 else if (MD->isVirtual())
1551 Out << 'M';
1552 else
1553 Out << 'I';
1554 break;
1555 case AS_public:
1556 if (MD->isStatic())
1557 Out << 'S';
1558 else if (MD->isVirtual())
1559 Out << 'U';
1560 else
1561 Out << 'Q';
1562 }
1563 } else
1564 Out << 'Y';
1565}
Reid Klecknerc5cc3382013-09-25 22:28:52 +00001566void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001567 // <calling-convention> ::= A # __cdecl
1568 // ::= B # __export __cdecl
1569 // ::= C # __pascal
1570 // ::= D # __export __pascal
1571 // ::= E # __thiscall
1572 // ::= F # __export __thiscall
1573 // ::= G # __stdcall
1574 // ::= H # __export __stdcall
1575 // ::= I # __fastcall
1576 // ::= J # __export __fastcall
1577 // The 'export' calling conventions are from a bygone era
1578 // (*cough*Win16*cough*) when functions were declared for export with
1579 // that keyword. (It didn't actually export them, it just made them so
1580 // that they could be in a DLL and somebody from another module could call
1581 // them.)
1582 CallingConv CC = T->getCallConv();
Guy Benyei11169dd2012-12-18 14:30:41 +00001583 switch (CC) {
1584 default:
1585 llvm_unreachable("Unsupported CC for mangling");
Charles Davisb5a214e2013-08-30 04:39:01 +00001586 case CC_X86_64Win64:
1587 case CC_X86_64SysV:
Guy Benyei11169dd2012-12-18 14:30:41 +00001588 case CC_C: Out << 'A'; break;
1589 case CC_X86Pascal: Out << 'C'; break;
1590 case CC_X86ThisCall: Out << 'E'; break;
1591 case CC_X86StdCall: Out << 'G'; break;
1592 case CC_X86FastCall: Out << 'I'; break;
1593 }
1594}
1595void MicrosoftCXXNameMangler::mangleThrowSpecification(
1596 const FunctionProtoType *FT) {
1597 // <throw-spec> ::= Z # throw(...) (default)
1598 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1599 // ::= <type>+
1600 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1601 // all actually mangled as 'Z'. (They're ignored because their associated
1602 // functionality isn't implemented, and probably never will be.)
1603 Out << 'Z';
1604}
1605
1606void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1607 SourceRange Range) {
1608 // Probably should be mangled as a template instantiation; need to see what
1609 // VC does first.
1610 DiagnosticsEngine &Diags = Context.getDiags();
1611 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1612 "cannot mangle this unresolved dependent type yet");
1613 Diags.Report(Range.getBegin(), DiagID)
1614 << Range;
1615}
1616
1617// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1618// <union-type> ::= T <name>
1619// <struct-type> ::= U <name>
1620// <class-type> ::= V <name>
David Majnemer048f90c2013-12-09 04:28:34 +00001621// <enum-type> ::= W4 <name>
Guy Benyei11169dd2012-12-18 14:30:41 +00001622void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer0db0ca42013-08-05 22:26:46 +00001623 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00001624}
1625void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer0db0ca42013-08-05 22:26:46 +00001626 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00001627}
David Majnemer0db0ca42013-08-05 22:26:46 +00001628void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1629 switch (TD->getTagKind()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001630 case TTK_Union:
1631 Out << 'T';
1632 break;
1633 case TTK_Struct:
1634 case TTK_Interface:
1635 Out << 'U';
1636 break;
1637 case TTK_Class:
1638 Out << 'V';
1639 break;
1640 case TTK_Enum:
David Majnemer048f90c2013-12-09 04:28:34 +00001641 Out << "W4";
Guy Benyei11169dd2012-12-18 14:30:41 +00001642 break;
1643 }
David Majnemer0db0ca42013-08-05 22:26:46 +00001644 mangleName(TD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001645}
1646
1647// <type> ::= <array-type>
1648// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1649// [Y <dimension-count> <dimension>+]
Reid Kleckner369f3162013-05-14 20:30:42 +00001650// <element-type> # as global, E is never required
Guy Benyei11169dd2012-12-18 14:30:41 +00001651// It's supposed to be the other way around, but for some strange reason, it
1652// isn't. Today this behavior is retained for the sole purpose of backwards
1653// compatibility.
David Majnemer5a1b2042013-09-11 04:44:30 +00001654void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001655 // This isn't a recursive mangling, so now we have to do it all in this
1656 // one call.
David Majnemer5a1b2042013-09-11 04:44:30 +00001657 manglePointerQualifiers(T->getElementType().getQualifiers());
Peter Collingbourne2816c022013-04-25 04:25:40 +00001658 mangleType(T->getElementType(), SourceRange());
Guy Benyei11169dd2012-12-18 14:30:41 +00001659}
1660void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1661 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001662 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001663}
1664void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1665 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001666 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001667}
1668void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1669 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001670 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001671}
1672void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1673 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001674 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001675}
Reid Kleckner18da98e2013-06-24 19:21:52 +00001676void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001677 QualType ElementTy(T, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001678 SmallVector<llvm::APInt, 3> Dimensions;
1679 for (;;) {
1680 if (const ConstantArrayType *CAT =
1681 getASTContext().getAsConstantArrayType(ElementTy)) {
1682 Dimensions.push_back(CAT->getSize());
1683 ElementTy = CAT->getElementType();
1684 } else if (ElementTy->isVariableArrayType()) {
1685 const VariableArrayType *VAT =
1686 getASTContext().getAsVariableArrayType(ElementTy);
1687 DiagnosticsEngine &Diags = Context.getDiags();
1688 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1689 "cannot mangle this variable-length array yet");
1690 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1691 << VAT->getBracketsRange();
1692 return;
1693 } else if (ElementTy->isDependentSizedArrayType()) {
1694 // The dependent expression has to be folded into a constant (TODO).
1695 const DependentSizedArrayType *DSAT =
1696 getASTContext().getAsDependentSizedArrayType(ElementTy);
1697 DiagnosticsEngine &Diags = Context.getDiags();
1698 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1699 "cannot mangle this dependent-length array yet");
1700 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1701 << DSAT->getBracketsRange();
1702 return;
Peter Collingbourne2816c022013-04-25 04:25:40 +00001703 } else if (const IncompleteArrayType *IAT =
1704 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1705 Dimensions.push_back(llvm::APInt(32, 0));
1706 ElementTy = IAT->getElementType();
1707 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001708 else break;
1709 }
Peter Collingbourne2816c022013-04-25 04:25:40 +00001710 Out << 'Y';
1711 // <dimension-count> ::= <number> # number of extra dimensions
1712 mangleNumber(Dimensions.size());
1713 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1714 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Kleckner18da98e2013-06-24 19:21:52 +00001715 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei11169dd2012-12-18 14:30:41 +00001716}
1717
1718// <type> ::= <pointer-to-member-type>
1719// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1720// <class name> <type>
1721void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1722 SourceRange Range) {
1723 QualType PointeeType = T->getPointeeType();
1724 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1725 Out << '8';
1726 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001727 mangleFunctionType(FPT, 0, true);
Guy Benyei11169dd2012-12-18 14:30:41 +00001728 } else {
David Majnemer6dda7bb2013-08-15 08:13:23 +00001729 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1730 Out << 'E';
Guy Benyei11169dd2012-12-18 14:30:41 +00001731 mangleQualifiers(PointeeType.getQualifiers(), true);
1732 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourne2816c022013-04-25 04:25:40 +00001733 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei11169dd2012-12-18 14:30:41 +00001734 }
1735}
1736
1737void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1738 SourceRange Range) {
1739 DiagnosticsEngine &Diags = Context.getDiags();
1740 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1741 "cannot mangle this template type parameter type yet");
1742 Diags.Report(Range.getBegin(), DiagID)
1743 << Range;
1744}
1745
1746void MicrosoftCXXNameMangler::mangleType(
1747 const SubstTemplateTypeParmPackType *T,
1748 SourceRange Range) {
1749 DiagnosticsEngine &Diags = Context.getDiags();
1750 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1751 "cannot mangle this substituted parameter pack yet");
1752 Diags.Report(Range.getBegin(), DiagID)
1753 << Range;
1754}
1755
1756// <type> ::= <pointer-type>
Reid Kleckner369f3162013-05-14 20:30:42 +00001757// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
Alp Tokerd4733632013-12-05 04:47:09 +00001758// # the E is required for 64-bit non-static pointers
Guy Benyei11169dd2012-12-18 14:30:41 +00001759void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1760 SourceRange Range) {
1761 QualType PointeeTy = T->getPointeeType();
Reid Kleckner369f3162013-05-14 20:30:42 +00001762 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1763 Out << 'E';
Peter Collingbourne2816c022013-04-25 04:25:40 +00001764 mangleType(PointeeTy, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001765}
1766void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1767 SourceRange Range) {
1768 // Object pointers never have qualifiers.
1769 Out << 'A';
David Majnemer6dda7bb2013-08-15 08:13:23 +00001770 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1771 Out << 'E';
Guy Benyei11169dd2012-12-18 14:30:41 +00001772 mangleType(T->getPointeeType(), Range);
1773}
1774
1775// <type> ::= <reference-type>
Reid Kleckner369f3162013-05-14 20:30:42 +00001776// <reference-type> ::= A E? <cvr-qualifiers> <type>
Alp Tokerd4733632013-12-05 04:47:09 +00001777// # the E is required for 64-bit non-static lvalue references
Guy Benyei11169dd2012-12-18 14:30:41 +00001778void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1779 SourceRange Range) {
1780 Out << 'A';
Reid Kleckner369f3162013-05-14 20:30:42 +00001781 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1782 Out << 'E';
Peter Collingbourne2816c022013-04-25 04:25:40 +00001783 mangleType(T->getPointeeType(), Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001784}
1785
1786// <type> ::= <r-value-reference-type>
Reid Kleckner369f3162013-05-14 20:30:42 +00001787// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
Alp Tokerd4733632013-12-05 04:47:09 +00001788// # the E is required for 64-bit non-static rvalue references
Guy Benyei11169dd2012-12-18 14:30:41 +00001789void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1790 SourceRange Range) {
1791 Out << "$$Q";
Reid Kleckner369f3162013-05-14 20:30:42 +00001792 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1793 Out << 'E';
Peter Collingbourne2816c022013-04-25 04:25:40 +00001794 mangleType(T->getPointeeType(), Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001795}
1796
1797void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1798 SourceRange Range) {
1799 DiagnosticsEngine &Diags = Context.getDiags();
1800 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1801 "cannot mangle this complex number type yet");
1802 Diags.Report(Range.getBegin(), DiagID)
1803 << Range;
1804}
1805
1806void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1807 SourceRange Range) {
Reid Klecknere7e64d82013-03-26 16:56:59 +00001808 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1809 assert(ET && "vectors with non-builtin elements are unsupported");
1810 uint64_t Width = getASTContext().getTypeSize(T);
1811 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1812 // doesn't match the Intel types uses a custom mangling below.
1813 bool IntelVector = true;
1814 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1815 Out << "T__m64";
1816 } else if (Width == 128 || Width == 256) {
1817 if (ET->getKind() == BuiltinType::Float)
1818 Out << "T__m" << Width;
1819 else if (ET->getKind() == BuiltinType::LongLong)
1820 Out << "T__m" << Width << 'i';
1821 else if (ET->getKind() == BuiltinType::Double)
1822 Out << "U__m" << Width << 'd';
1823 else
1824 IntelVector = false;
1825 } else {
1826 IntelVector = false;
1827 }
1828
1829 if (!IntelVector) {
1830 // The MS ABI doesn't have a special mangling for vector types, so we define
1831 // our own mangling to handle uses of __vector_size__ on user-specified
1832 // types, and for extensions like __v4sf.
1833 Out << "T__clang_vec" << T->getNumElements() << '_';
1834 mangleType(ET, Range);
1835 }
1836
1837 Out << "@@";
Guy Benyei11169dd2012-12-18 14:30:41 +00001838}
Reid Klecknere7e64d82013-03-26 16:56:59 +00001839
Guy Benyei11169dd2012-12-18 14:30:41 +00001840void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1841 SourceRange Range) {
1842 DiagnosticsEngine &Diags = Context.getDiags();
1843 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1844 "cannot mangle this extended vector type yet");
1845 Diags.Report(Range.getBegin(), DiagID)
1846 << Range;
1847}
1848void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1849 SourceRange Range) {
1850 DiagnosticsEngine &Diags = Context.getDiags();
1851 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1852 "cannot mangle this dependent-sized extended vector type yet");
1853 Diags.Report(Range.getBegin(), DiagID)
1854 << Range;
1855}
1856
1857void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1858 SourceRange) {
1859 // ObjC interfaces have structs underlying them.
1860 Out << 'U';
1861 mangleName(T->getDecl());
1862}
1863
1864void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1865 SourceRange Range) {
1866 // We don't allow overloading by different protocol qualification,
1867 // so mangling them isn't necessary.
1868 mangleType(T->getBaseType(), Range);
1869}
1870
1871void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1872 SourceRange Range) {
1873 Out << "_E";
1874
1875 QualType pointee = T->getPointeeType();
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001876 mangleFunctionType(pointee->castAs<FunctionProtoType>());
Guy Benyei11169dd2012-12-18 14:30:41 +00001877}
1878
David Majnemerf0a84f22013-08-16 08:29:13 +00001879void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1880 SourceRange) {
1881 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei11169dd2012-12-18 14:30:41 +00001882}
1883
1884void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1885 SourceRange Range) {
1886 DiagnosticsEngine &Diags = Context.getDiags();
1887 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1888 "cannot mangle this template specialization type yet");
1889 Diags.Report(Range.getBegin(), DiagID)
1890 << Range;
1891}
1892
1893void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1894 SourceRange Range) {
1895 DiagnosticsEngine &Diags = Context.getDiags();
1896 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1897 "cannot mangle this dependent name type yet");
1898 Diags.Report(Range.getBegin(), DiagID)
1899 << Range;
1900}
1901
1902void MicrosoftCXXNameMangler::mangleType(
1903 const DependentTemplateSpecializationType *T,
1904 SourceRange Range) {
1905 DiagnosticsEngine &Diags = Context.getDiags();
1906 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1907 "cannot mangle this dependent template specialization type yet");
1908 Diags.Report(Range.getBegin(), DiagID)
1909 << Range;
1910}
1911
1912void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1913 SourceRange Range) {
1914 DiagnosticsEngine &Diags = Context.getDiags();
1915 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1916 "cannot mangle this pack expansion yet");
1917 Diags.Report(Range.getBegin(), DiagID)
1918 << Range;
1919}
1920
1921void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1922 SourceRange Range) {
1923 DiagnosticsEngine &Diags = Context.getDiags();
1924 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1925 "cannot mangle this typeof(type) yet");
1926 Diags.Report(Range.getBegin(), DiagID)
1927 << Range;
1928}
1929
1930void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1931 SourceRange Range) {
1932 DiagnosticsEngine &Diags = Context.getDiags();
1933 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1934 "cannot mangle this typeof(expression) yet");
1935 Diags.Report(Range.getBegin(), DiagID)
1936 << Range;
1937}
1938
1939void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1940 SourceRange Range) {
1941 DiagnosticsEngine &Diags = Context.getDiags();
1942 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1943 "cannot mangle this decltype() yet");
1944 Diags.Report(Range.getBegin(), DiagID)
1945 << Range;
1946}
1947
1948void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1949 SourceRange Range) {
1950 DiagnosticsEngine &Diags = Context.getDiags();
1951 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1952 "cannot mangle this unary transform type yet");
1953 Diags.Report(Range.getBegin(), DiagID)
1954 << Range;
1955}
1956
1957void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
David Majnemerb9a5f2d2014-01-21 20:33:36 +00001958 assert(T->getDeducedType().isNull() && "expecting a dependent type!");
1959
Guy Benyei11169dd2012-12-18 14:30:41 +00001960 DiagnosticsEngine &Diags = Context.getDiags();
1961 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1962 "cannot mangle this 'auto' type yet");
1963 Diags.Report(Range.getBegin(), DiagID)
1964 << Range;
1965}
1966
1967void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1968 SourceRange Range) {
1969 DiagnosticsEngine &Diags = Context.getDiags();
1970 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1971 "cannot mangle this C11 atomic type yet");
1972 Diags.Report(Range.getBegin(), DiagID)
1973 << Range;
1974}
1975
Rafael Espindola002667c2013-10-16 01:40:34 +00001976void MicrosoftMangleContextImpl::mangleCXXName(const NamedDecl *D,
1977 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001978 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1979 "Invalid mangleName() call, argument is not a variable or function!");
1980 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1981 "Invalid mangleName() call on 'structor decl!");
1982
1983 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1984 getASTContext().getSourceManager(),
1985 "Mangling declaration");
1986
1987 MicrosoftCXXNameMangler Mangler(*this, Out);
1988 return Mangler.mangle(D);
1989}
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001990
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00001991// <this-adjustment> ::= <no-adjustment> | <static-adjustment> |
1992// <virtual-adjustment>
1993// <no-adjustment> ::= A # private near
1994// ::= B # private far
1995// ::= I # protected near
1996// ::= J # protected far
1997// ::= Q # public near
1998// ::= R # public far
1999// <static-adjustment> ::= G <static-offset> # private near
2000// ::= H <static-offset> # private far
2001// ::= O <static-offset> # protected near
2002// ::= P <static-offset> # protected far
2003// ::= W <static-offset> # public near
2004// ::= X <static-offset> # public far
2005// <virtual-adjustment> ::= $0 <virtual-shift> <static-offset> # private near
2006// ::= $1 <virtual-shift> <static-offset> # private far
2007// ::= $2 <virtual-shift> <static-offset> # protected near
2008// ::= $3 <virtual-shift> <static-offset> # protected far
2009// ::= $4 <virtual-shift> <static-offset> # public near
2010// ::= $5 <virtual-shift> <static-offset> # public far
2011// <virtual-shift> ::= <vtordisp-shift> | <vtordispex-shift>
2012// <vtordisp-shift> ::= <offset-to-vtordisp>
2013// <vtordispex-shift> ::= <offset-to-vbptr> <vbase-offset-offset>
2014// <offset-to-vtordisp>
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002015static void mangleThunkThisAdjustment(const CXXMethodDecl *MD,
2016 const ThisAdjustment &Adjustment,
2017 MicrosoftCXXNameMangler &Mangler,
2018 raw_ostream &Out) {
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002019 if (!Adjustment.Virtual.isEmpty()) {
2020 Out << '$';
2021 char AccessSpec;
2022 switch (MD->getAccess()) {
2023 case AS_none:
2024 llvm_unreachable("Unsupported access specifier");
2025 case AS_private:
2026 AccessSpec = '0';
2027 break;
2028 case AS_protected:
2029 AccessSpec = '2';
2030 break;
2031 case AS_public:
2032 AccessSpec = '4';
2033 }
2034 if (Adjustment.Virtual.Microsoft.VBPtrOffset) {
2035 Out << 'R' << AccessSpec;
David Majnemer2a816452013-12-09 10:44:32 +00002036 Mangler.mangleNumber(
2037 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBPtrOffset));
2038 Mangler.mangleNumber(
2039 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBOffsetOffset));
2040 Mangler.mangleNumber(
2041 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset));
2042 Mangler.mangleNumber(static_cast<uint32_t>(Adjustment.NonVirtual));
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002043 } else {
2044 Out << AccessSpec;
David Majnemer2a816452013-12-09 10:44:32 +00002045 Mangler.mangleNumber(
2046 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset));
2047 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual));
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002048 }
2049 } else if (Adjustment.NonVirtual != 0) {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002050 switch (MD->getAccess()) {
2051 case AS_none:
2052 llvm_unreachable("Unsupported access specifier");
2053 case AS_private:
2054 Out << 'G';
2055 break;
2056 case AS_protected:
2057 Out << 'O';
2058 break;
2059 case AS_public:
2060 Out << 'W';
2061 }
David Majnemer2a816452013-12-09 10:44:32 +00002062 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual));
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002063 } else {
2064 switch (MD->getAccess()) {
2065 case AS_none:
2066 llvm_unreachable("Unsupported access specifier");
2067 case AS_private:
2068 Out << 'A';
2069 break;
2070 case AS_protected:
2071 Out << 'I';
2072 break;
2073 case AS_public:
2074 Out << 'Q';
2075 }
2076 }
2077}
2078
Reid Kleckner96f8f932014-02-05 17:27:08 +00002079void
2080MicrosoftMangleContextImpl::mangleVirtualMemPtrThunk(const CXXMethodDecl *MD,
2081 raw_ostream &Out) {
2082 MicrosoftVTableContext *VTContext =
2083 cast<MicrosoftVTableContext>(getASTContext().getVTableContext());
2084 const MicrosoftVTableContext::MethodVFTableLocation &ML =
2085 VTContext->getMethodVFTableLocation(GlobalDecl(MD));
Hans Wennborg88497d62013-11-15 17:24:45 +00002086
2087 MicrosoftCXXNameMangler Mangler(*this, Out);
Reid Kleckner96f8f932014-02-05 17:27:08 +00002088 Mangler.getStream() << "\01?";
2089 Mangler.mangleVirtualMemPtrThunk(MD, ML);
Hans Wennborg88497d62013-11-15 17:24:45 +00002090}
2091
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002092void MicrosoftMangleContextImpl::mangleThunk(const CXXMethodDecl *MD,
2093 const ThunkInfo &Thunk,
2094 raw_ostream &Out) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002095 MicrosoftCXXNameMangler Mangler(*this, Out);
2096 Out << "\01?";
2097 Mangler.mangleName(MD);
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002098 mangleThunkThisAdjustment(MD, Thunk.This, Mangler, Out);
2099 if (!Thunk.Return.isEmpty())
2100 assert(Thunk.Method != 0 && "Thunk info should hold the overridee decl");
2101
2102 const CXXMethodDecl *DeclForFPT = Thunk.Method ? Thunk.Method : MD;
2103 Mangler.mangleFunctionType(
2104 DeclForFPT->getType()->castAs<FunctionProtoType>(), MD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002105}
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002106
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002107void MicrosoftMangleContextImpl::mangleCXXDtorThunk(
2108 const CXXDestructorDecl *DD, CXXDtorType Type,
2109 const ThisAdjustment &Adjustment, raw_ostream &Out) {
2110 // FIXME: Actually, the dtor thunk should be emitted for vector deleting
2111 // dtors rather than scalar deleting dtors. Just use the vector deleting dtor
2112 // mangling manually until we support both deleting dtor types.
2113 assert(Type == Dtor_Deleting);
2114 MicrosoftCXXNameMangler Mangler(*this, Out, DD, Type);
2115 Out << "\01??_E";
2116 Mangler.mangleName(DD->getParent());
2117 mangleThunkThisAdjustment(DD, Adjustment, Mangler, Out);
2118 Mangler.mangleFunctionType(DD->getType()->castAs<FunctionProtoType>(), DD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002119}
Reid Kleckner7810af02013-06-19 15:20:38 +00002120
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002121void MicrosoftMangleContextImpl::mangleCXXVFTable(
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00002122 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
2123 raw_ostream &Out) {
Reid Kleckner7810af02013-06-19 15:20:38 +00002124 // <mangled-name> ::= ?_7 <class-name> <storage-class>
2125 // <cvr-qualifiers> [<name>] @
Guy Benyei11169dd2012-12-18 14:30:41 +00002126 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner7810af02013-06-19 15:20:38 +00002127 // is always '6' for vftables.
Guy Benyei11169dd2012-12-18 14:30:41 +00002128 MicrosoftCXXNameMangler Mangler(*this, Out);
2129 Mangler.getStream() << "\01??_7";
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00002130 Mangler.mangleName(Derived);
2131 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
2132 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
2133 E = BasePath.end();
2134 I != E; ++I) {
2135 Mangler.mangleName(*I);
2136 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002137 Mangler.getStream() << '@';
2138}
Reid Kleckner7810af02013-06-19 15:20:38 +00002139
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002140void MicrosoftMangleContextImpl::mangleCXXVBTable(
Reid Kleckner7810af02013-06-19 15:20:38 +00002141 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
2142 raw_ostream &Out) {
2143 // <mangled-name> ::= ?_8 <class-name> <storage-class>
2144 // <cvr-qualifiers> [<name>] @
2145 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
2146 // is always '7' for vbtables.
2147 MicrosoftCXXNameMangler Mangler(*this, Out);
2148 Mangler.getStream() << "\01??_8";
2149 Mangler.mangleName(Derived);
2150 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
2151 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
2152 E = BasePath.end();
2153 I != E; ++I) {
2154 Mangler.mangleName(*I);
2155 }
2156 Mangler.getStream() << '@';
2157}
2158
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002159void MicrosoftMangleContextImpl::mangleCXXRTTI(QualType T, raw_ostream &) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002160 // FIXME: Give a location...
2161 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2162 "cannot mangle RTTI descriptors for type %0 yet");
2163 getDiags().Report(DiagID)
2164 << T.getBaseTypeIdentifier();
2165}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002166
2167void MicrosoftMangleContextImpl::mangleCXXRTTIName(QualType T, raw_ostream &) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002168 // FIXME: Give a location...
2169 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2170 "cannot mangle the name of type %0 into RTTI descriptors yet");
2171 getDiags().Report(DiagID)
2172 << T.getBaseTypeIdentifier();
2173}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002174
Reid Klecknercc99e262013-11-19 23:23:00 +00002175void MicrosoftMangleContextImpl::mangleTypeName(QualType T, raw_ostream &Out) {
2176 // This is just a made up unique string for the purposes of tbaa. undname
2177 // does *not* know how to demangle it.
2178 MicrosoftCXXNameMangler Mangler(*this, Out);
2179 Mangler.getStream() << '?';
2180 Mangler.mangleType(T, SourceRange());
2181}
2182
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002183void MicrosoftMangleContextImpl::mangleCXXCtor(const CXXConstructorDecl *D,
2184 CXXCtorType Type,
2185 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002186 MicrosoftCXXNameMangler mangler(*this, Out);
2187 mangler.mangle(D);
2188}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002189
2190void MicrosoftMangleContextImpl::mangleCXXDtor(const CXXDestructorDecl *D,
2191 CXXDtorType Type,
2192 raw_ostream &Out) {
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00002193 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei11169dd2012-12-18 14:30:41 +00002194 mangler.mangle(D);
2195}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002196
2197void MicrosoftMangleContextImpl::mangleReferenceTemporary(const VarDecl *VD,
David Majnemer210e6bfa12013-12-13 00:39:38 +00002198 raw_ostream &) {
2199 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2200 "cannot mangle this reference temporary yet");
2201 getDiags().Report(VD->getLocation(), DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00002202}
2203
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002204void MicrosoftMangleContextImpl::mangleStaticGuardVariable(const VarDecl *VD,
2205 raw_ostream &Out) {
David Majnemer25e1a5e2013-12-13 00:52:45 +00002206 // TODO: This is not correct, especially with respect to MSVC2013. MSVC2013
2207 // utilizes thread local variables to implement thread safe, re-entrant
2208 // initialization for statics. They no longer differentiate between an
2209 // externally visible and non-externally visible static with respect to
2210 // mangling, they all get $TSS <number>.
2211 //
2212 // N.B. This means that they can get more than 32 static variable guards in a
2213 // scope. It also means that they broke compatibility with their own ABI.
2214
Reid Klecknerd8110b62013-09-10 20:14:30 +00002215 // <guard-name> ::= ?_B <postfix> @51
2216 // ::= ?$S <guard-num> @ <postfix> @4IA
2217
2218 // The first mangling is what MSVC uses to guard static locals in inline
2219 // functions. It uses a different mangling in external functions to support
2220 // guarding more than 32 variables. MSVC rejects inline functions with more
2221 // than 32 static locals. We don't fully implement the second mangling
2222 // because those guards are not externally visible, and instead use LLVM's
2223 // default renaming when creating a new guard variable.
2224 MicrosoftCXXNameMangler Mangler(*this, Out);
2225
2226 bool Visible = VD->isExternallyVisible();
2227 // <operator-name> ::= ?_B # local static guard
2228 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@");
2229 Mangler.manglePostfix(VD->getDeclContext());
2230 Mangler.getStream() << (Visible ? "@51" : "@4IA");
2231}
2232
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002233void MicrosoftMangleContextImpl::mangleInitFiniStub(const VarDecl *D,
2234 raw_ostream &Out,
2235 char CharCode) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002236 MicrosoftCXXNameMangler Mangler(*this, Out);
2237 Mangler.getStream() << "\01??__" << CharCode;
2238 Mangler.mangleName(D);
2239 // This is the function class mangling. These stubs are global, non-variadic,
2240 // cdecl functions that return void and take no args.
2241 Mangler.getStream() << "YAXXZ";
2242}
2243
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002244void MicrosoftMangleContextImpl::mangleDynamicInitializer(const VarDecl *D,
2245 raw_ostream &Out) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002246 // <initializer-name> ::= ?__E <name> YAXXZ
2247 mangleInitFiniStub(D, Out, 'E');
2248}
2249
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002250void
2251MicrosoftMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D,
2252 raw_ostream &Out) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002253 // <destructor-name> ::= ?__F <name> YAXXZ
2254 mangleInitFiniStub(D, Out, 'F');
Reid Klecknerd8110b62013-09-10 20:14:30 +00002255}
2256
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002257MicrosoftMangleContext *
2258MicrosoftMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags) {
2259 return new MicrosoftMangleContextImpl(Context, Diags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002260}