blob: 6ce7018be19a58728eb4067a2f8a2e9bfa65d81d [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
386 int64_t FO = 0;
387 MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel();
388 if (FD) {
389 FO = getASTContext().getFieldOffset(FD);
390 assert(FO % getASTContext().getCharWidth() == 0 &&
391 "cannot take address of bitfield");
392 FO /= getASTContext().getCharWidth();
393 } else if (!RD->nullFieldOffsetIsZero()) {
394 FO = -1;
395 }
396
397 switch (IM) {
398 case MSInheritanceAttr::Keyword_single_inheritance:
399 case MSInheritanceAttr::Keyword_multiple_inheritance: {
400 // If we only have a single field, it's just an integer literal.
401 llvm::APSInt Val(64, /*isUnsigned=*/false);
402 Val = FO;
403 mangleIntegerLiteral(Val, /*IsBoolean=*/false);
404 break;
405 }
406
407 // Otherwise, we have an aggregate, but all adjusting fields should be zero,
408 // because we don't allow casts (even implicit) in the context of a template
409 // argument.
410 case MSInheritanceAttr::Keyword_virtual_inheritance:
411 Out << "$F";
412 mangleNumber(FO);
413 mangleNumber(0);
414 break;
415
416 case MSInheritanceAttr::Keyword_unspecified_inheritance:
417 Out << "$G";
418 mangleNumber(FO);
419 mangleNumber(0);
420 mangleNumber(0);
421 break;
422 }
423}
424
425void
426MicrosoftCXXNameMangler::mangleMemberFunctionPointer(const CXXRecordDecl *RD,
427 const CXXMethodDecl *MD) {
428 // <member-function-pointer> ::= $1? <name>
429 // ::= $H? <name> <number>
430 // ::= $I? <name> <number> <number>
431 // ::= $J? <name> <number> <number> <number>
432 // ::= $0A@
433
434 MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel();
435
436 // The null member function pointer is $0A@ in function templates and crashes
437 // MSVC when used in class templates, so we don't know what they really look
438 // like.
439 if (!MD) {
440 Out << "$0A@";
441 return;
442 }
443
444 char Code = '\0';
445 switch (IM) {
446 case MSInheritanceAttr::Keyword_single_inheritance: Code = '1'; break;
447 case MSInheritanceAttr::Keyword_multiple_inheritance: Code = 'H'; break;
448 case MSInheritanceAttr::Keyword_virtual_inheritance: Code = 'I'; break;
449 case MSInheritanceAttr::Keyword_unspecified_inheritance: Code = 'J'; break;
450 }
451
452 Out << '$' << Code << '?';
453
454 // If non-virtual, mangle the name. If virtual, mangle as a virtual memptr
455 // thunk.
456 uint64_t NVOffset = 0;
457 uint64_t VBTableOffset = 0;
458 if (!MD) {
459 mangleNumber(0);
460 } else if (MD->isVirtual()) {
461 MicrosoftVTableContext *VTContext =
462 cast<MicrosoftVTableContext>(getASTContext().getVTableContext());
463 const MicrosoftVTableContext::MethodVFTableLocation &ML =
464 VTContext->getMethodVFTableLocation(GlobalDecl(MD));
465 mangleVirtualMemPtrThunk(MD, ML);
466 NVOffset = ML.VFPtrOffset.getQuantity();
467 VBTableOffset = ML.VBTableIndex * 4;
468 if (ML.VBase) {
469 DiagnosticsEngine &Diags = Context.getDiags();
470 unsigned DiagID = Diags.getCustomDiagID(
471 DiagnosticsEngine::Error,
472 "cannot mangle pointers to member functions from virtual bases");
473 Diags.Report(MD->getLocation(), DiagID);
474 }
475 } else {
476 mangleName(MD);
477 mangleFunctionEncoding(MD);
478 }
479
480 if (MSInheritanceAttr::hasNVOffsetField(/*IsMemberFunction=*/true, IM))
481 mangleNumber(NVOffset);
482 if (MSInheritanceAttr::hasVBPtrOffsetField(IM))
483 mangleNumber(0);
484 if (MSInheritanceAttr::hasVBTableOffsetField(IM))
485 mangleNumber(VBTableOffset);
486}
487
488void MicrosoftCXXNameMangler::mangleVirtualMemPtrThunk(
489 const CXXMethodDecl *MD,
490 const MicrosoftVTableContext::MethodVFTableLocation &ML) {
491 // Get the vftable offset.
492 CharUnits PointerWidth = getASTContext().toCharUnitsFromBits(
493 getASTContext().getTargetInfo().getPointerWidth(0));
494 uint64_t OffsetInVFTable = ML.Index * PointerWidth.getQuantity();
495
496 Out << "?_9";
497 mangleName(MD->getParent());
498 Out << "$B";
499 mangleNumber(OffsetInVFTable);
500 Out << 'A';
501 Out << (PointersAre64Bit ? 'A' : 'E');
502}
503
Guy Benyei11169dd2012-12-18 14:30:41 +0000504void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
505 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
506 const DeclContext *DC = ND->getDeclContext();
507
508 // Always start with the unqualified name.
David Majnemerb4119f72013-12-13 01:06:04 +0000509 mangleUnqualifiedName(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +0000510
511 // If this is an extern variable declared locally, the relevant DeclContext
512 // is that of the containing namespace, or the translation unit.
513 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
514 while (!DC->isNamespace() && !DC->isTranslationUnit())
515 DC = DC->getParent();
516
517 manglePostfix(DC);
518
519 // Terminate the whole name with an '@'.
520 Out << '@';
521}
522
David Majnemer2a816452013-12-09 10:44:32 +0000523void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
524 // <non-negative integer> ::= A@ # when Number == 0
525 // ::= <decimal digit> # when 1 <= Number <= 10
526 // ::= <hex digit>+ @ # when Number >= 10
527 //
528 // <number> ::= [?] <non-negative integer>
Guy Benyei11169dd2012-12-18 14:30:41 +0000529
David Majnemer2a816452013-12-09 10:44:32 +0000530 uint64_t Value = static_cast<uint64_t>(Number);
531 if (Number < 0) {
532 Value = -Value;
Guy Benyei11169dd2012-12-18 14:30:41 +0000533 Out << '?';
Guy Benyei11169dd2012-12-18 14:30:41 +0000534 }
David Majnemer2a816452013-12-09 10:44:32 +0000535
536 if (Value == 0)
537 Out << "A@";
538 else if (Value >= 1 && Value <= 10)
539 Out << (Value - 1);
540 else {
541 // Numbers that are not encoded as decimal digits are represented as nibbles
542 // in the range of ASCII characters 'A' to 'P'.
543 // The number 0x123450 would be encoded as 'BCDEFA'
544 char EncodedNumberBuffer[sizeof(uint64_t) * 2];
545 llvm::MutableArrayRef<char> BufferRef(EncodedNumberBuffer);
546 llvm::MutableArrayRef<char>::reverse_iterator I = BufferRef.rbegin();
547 for (; Value != 0; Value >>= 4)
548 *I++ = 'A' + (Value & 0xf);
549 Out.write(I.base(), I - BufferRef.rbegin());
Guy Benyei11169dd2012-12-18 14:30:41 +0000550 Out << '@';
551 }
552}
553
554static const TemplateDecl *
Reid Kleckner52518862013-03-20 01:40:23 +0000555isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000556 // Check if we have a function template.
557 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
558 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Kleckner52518862013-03-20 01:40:23 +0000559 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei11169dd2012-12-18 14:30:41 +0000560 return TD;
561 }
562 }
563
564 // Check if we have a class template.
565 if (const ClassTemplateSpecializationDecl *Spec =
Reid Kleckner52518862013-03-20 01:40:23 +0000566 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
567 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei11169dd2012-12-18 14:30:41 +0000568 return Spec->getSpecializedTemplate();
569 }
570
571 return 0;
572}
573
574void
575MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
576 DeclarationName Name) {
577 // <unqualified-name> ::= <operator-name>
578 // ::= <ctor-dtor-name>
579 // ::= <source-name>
580 // ::= <template-name>
Reid Kleckner52518862013-03-20 01:40:23 +0000581
Guy Benyei11169dd2012-12-18 14:30:41 +0000582 // Check if we have a template.
Reid Kleckner52518862013-03-20 01:40:23 +0000583 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000584 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Klecknerc16c4472013-07-13 00:43:39 +0000585 // Function templates aren't considered for name back referencing. This
586 // makes sense since function templates aren't likely to occur multiple
587 // times in a symbol.
588 // FIXME: Test alias template mangling with MSVC 2013.
589 if (!isa<ClassTemplateDecl>(TD)) {
590 mangleTemplateInstantiationName(TD, *TemplateArgs);
591 return;
592 }
593
594 // We have a class template.
Guy Benyei11169dd2012-12-18 14:30:41 +0000595 // Here comes the tricky thing: if we need to mangle something like
596 // void foo(A::X<Y>, B::X<Y>),
597 // the X<Y> part is aliased. However, if you need to mangle
598 // void foo(A::X<A::Y>, A::X<B::Y>),
599 // the A::X<> part is not aliased.
600 // That said, from the mangler's perspective we have a structure like this:
601 // namespace[s] -> type[ -> template-parameters]
602 // but from the Clang perspective we have
603 // type [ -> template-parameters]
604 // \-> namespace[s]
605 // What we do is we create a new mangler, mangle the same type (without
606 // a namespace suffix) using the extra mangler with back references
607 // disabled (to avoid infinite recursion) and then use the mangled type
608 // name as a key to check the mangling of different types for aliasing.
609
610 std::string BackReferenceKey;
611 BackRefMap::iterator Found;
612 if (UseNameBackReferences) {
613 llvm::raw_string_ostream Stream(BackReferenceKey);
614 MicrosoftCXXNameMangler Extra(Context, Stream);
615 Extra.disableBackReferences();
616 Extra.mangleUnqualifiedName(ND, Name);
617 Stream.flush();
618
619 Found = NameBackReferences.find(BackReferenceKey);
620 }
621 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Kleckner52518862013-03-20 01:40:23 +0000622 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei11169dd2012-12-18 14:30:41 +0000623 if (UseNameBackReferences && NameBackReferences.size() < 10) {
624 size_t Size = NameBackReferences.size();
625 NameBackReferences[BackReferenceKey] = Size;
626 }
627 } else {
628 Out << Found->second;
629 }
630 return;
631 }
632
633 switch (Name.getNameKind()) {
634 case DeclarationName::Identifier: {
635 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
David Majnemer956bc112013-11-25 17:50:19 +0000636 mangleSourceName(II->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +0000637 break;
638 }
David Majnemerb4119f72013-12-13 01:06:04 +0000639
Guy Benyei11169dd2012-12-18 14:30:41 +0000640 // Otherwise, an anonymous entity. We must have a declaration.
641 assert(ND && "mangling empty name without declaration");
David Majnemerb4119f72013-12-13 01:06:04 +0000642
Guy Benyei11169dd2012-12-18 14:30:41 +0000643 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
644 if (NS->isAnonymousNamespace()) {
645 Out << "?A@";
646 break;
647 }
648 }
David Majnemerb4119f72013-12-13 01:06:04 +0000649
Guy Benyei11169dd2012-12-18 14:30:41 +0000650 // We must have an anonymous struct.
651 const TagDecl *TD = cast<TagDecl>(ND);
652 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
653 assert(TD->getDeclContext() == D->getDeclContext() &&
654 "Typedef should not be in another decl context!");
655 assert(D->getDeclName().getAsIdentifierInfo() &&
656 "Typedef was not named!");
David Majnemer956bc112013-11-25 17:50:19 +0000657 mangleSourceName(D->getDeclName().getAsIdentifierInfo()->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +0000658 break;
659 }
660
David Majnemer956bc112013-11-25 17:50:19 +0000661 if (TD->hasDeclaratorForAnonDecl()) {
David Majnemer50ce8352013-09-17 23:57:10 +0000662 // Anonymous types with no tag or typedef get the name of their
663 // declarator mangled in.
David Majnemer956bc112013-11-25 17:50:19 +0000664 llvm::SmallString<64> Name("<unnamed-type-");
665 Name += TD->getDeclaratorForAnonDecl()->getName();
666 Name += ">";
667 mangleSourceName(Name.str());
668 } else {
David Majnemer50ce8352013-09-17 23:57:10 +0000669 // Anonymous types with no tag, no typedef, or declarator get
David Majnemer956bc112013-11-25 17:50:19 +0000670 // '<unnamed-tag>'.
671 mangleSourceName("<unnamed-tag>");
672 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000673 break;
674 }
David Majnemerb4119f72013-12-13 01:06:04 +0000675
Guy Benyei11169dd2012-12-18 14:30:41 +0000676 case DeclarationName::ObjCZeroArgSelector:
677 case DeclarationName::ObjCOneArgSelector:
678 case DeclarationName::ObjCMultiArgSelector:
679 llvm_unreachable("Can't mangle Objective-C selector names here!");
David Majnemerb4119f72013-12-13 01:06:04 +0000680
Guy Benyei11169dd2012-12-18 14:30:41 +0000681 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000682 if (ND == Structor) {
683 assert(StructorType == Ctor_Complete &&
684 "Should never be asked to mangle a ctor other than complete");
685 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000686 Out << "?0";
687 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000688
Guy Benyei11169dd2012-12-18 14:30:41 +0000689 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000690 if (ND == Structor)
691 // If the named decl is the C++ destructor we're mangling,
692 // use the type we were given.
693 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
694 else
Reid Klecknere7de47e2013-07-22 13:51:44 +0000695 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000696 // class with a destructor is declared within a destructor.
Reid Klecknere7de47e2013-07-22 13:51:44 +0000697 mangleCXXDtorType(Dtor_Base);
Guy Benyei11169dd2012-12-18 14:30:41 +0000698 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000699
Guy Benyei11169dd2012-12-18 14:30:41 +0000700 case DeclarationName::CXXConversionFunctionName:
701 // <operator-name> ::= ?B # (cast)
702 // The target type is encoded as the return type.
703 Out << "?B";
704 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000705
Guy Benyei11169dd2012-12-18 14:30:41 +0000706 case DeclarationName::CXXOperatorName:
707 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
708 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000709
Guy Benyei11169dd2012-12-18 14:30:41 +0000710 case DeclarationName::CXXLiteralOperatorName: {
711 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
712 DiagnosticsEngine Diags = Context.getDiags();
713 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
714 "cannot mangle this literal operator yet");
715 Diags.Report(ND->getLocation(), DiagID);
716 break;
717 }
David Majnemerb4119f72013-12-13 01:06:04 +0000718
Guy Benyei11169dd2012-12-18 14:30:41 +0000719 case DeclarationName::CXXUsingDirective:
720 llvm_unreachable("Can't mangle a using directive name!");
721 }
722}
723
724void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
725 bool NoFunction) {
726 // <postfix> ::= <unqualified-name> [<postfix>]
727 // ::= <substitution> [<postfix>]
728
729 if (!DC) return;
730
731 while (isa<LinkageSpecDecl>(DC))
732 DC = DC->getParent();
733
734 if (DC->isTranslationUnit())
735 return;
736
737 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedman8978a9d2013-07-10 01:13:27 +0000738 DiagnosticsEngine Diags = Context.getDiags();
739 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
740 "cannot mangle a local inside this block yet");
741 Diags.Report(BD->getLocation(), DiagID);
742
743 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
744 // for how this should be done.
745 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei11169dd2012-12-18 14:30:41 +0000746 Out << '@';
747 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir3b4c30b2013-05-09 19:17:11 +0000748 } else if (isa<CapturedDecl>(DC)) {
749 // Skip CapturedDecl context.
750 manglePostfix(DC->getParent(), NoFunction);
751 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000752 }
753
754 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
755 return;
756 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
757 mangleObjCMethodName(Method);
758 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
759 mangleLocalName(Func);
760 else {
761 mangleUnqualifiedName(cast<NamedDecl>(DC));
762 manglePostfix(DC->getParent(), NoFunction);
763 }
764}
765
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000766void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknere7de47e2013-07-22 13:51:44 +0000767 // Microsoft uses the names on the case labels for these dtor variants. Clang
768 // uses the Itanium terminology internally. Everything in this ABI delegates
769 // towards the base dtor.
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000770 switch (T) {
Reid Klecknere7de47e2013-07-22 13:51:44 +0000771 // <operator-name> ::= ?1 # destructor
772 case Dtor_Base: Out << "?1"; return;
773 // <operator-name> ::= ?_D # vbase destructor
774 case Dtor_Complete: Out << "?_D"; return;
775 // <operator-name> ::= ?_G # scalar deleting destructor
776 case Dtor_Deleting: Out << "?_G"; return;
777 // <operator-name> ::= ?_E # vector deleting destructor
778 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
779 // it.
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000780 }
781 llvm_unreachable("Unsupported dtor type?");
782}
783
Guy Benyei11169dd2012-12-18 14:30:41 +0000784void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
785 SourceLocation Loc) {
786 switch (OO) {
787 // ?0 # constructor
788 // ?1 # destructor
789 // <operator-name> ::= ?2 # new
790 case OO_New: Out << "?2"; break;
791 // <operator-name> ::= ?3 # delete
792 case OO_Delete: Out << "?3"; break;
793 // <operator-name> ::= ?4 # =
794 case OO_Equal: Out << "?4"; break;
795 // <operator-name> ::= ?5 # >>
796 case OO_GreaterGreater: Out << "?5"; break;
797 // <operator-name> ::= ?6 # <<
798 case OO_LessLess: Out << "?6"; break;
799 // <operator-name> ::= ?7 # !
800 case OO_Exclaim: Out << "?7"; break;
801 // <operator-name> ::= ?8 # ==
802 case OO_EqualEqual: Out << "?8"; break;
803 // <operator-name> ::= ?9 # !=
804 case OO_ExclaimEqual: Out << "?9"; break;
805 // <operator-name> ::= ?A # []
806 case OO_Subscript: Out << "?A"; break;
807 // ?B # conversion
808 // <operator-name> ::= ?C # ->
809 case OO_Arrow: Out << "?C"; break;
810 // <operator-name> ::= ?D # *
811 case OO_Star: Out << "?D"; break;
812 // <operator-name> ::= ?E # ++
813 case OO_PlusPlus: Out << "?E"; break;
814 // <operator-name> ::= ?F # --
815 case OO_MinusMinus: Out << "?F"; break;
816 // <operator-name> ::= ?G # -
817 case OO_Minus: Out << "?G"; break;
818 // <operator-name> ::= ?H # +
819 case OO_Plus: Out << "?H"; break;
820 // <operator-name> ::= ?I # &
821 case OO_Amp: Out << "?I"; break;
822 // <operator-name> ::= ?J # ->*
823 case OO_ArrowStar: Out << "?J"; break;
824 // <operator-name> ::= ?K # /
825 case OO_Slash: Out << "?K"; break;
826 // <operator-name> ::= ?L # %
827 case OO_Percent: Out << "?L"; break;
828 // <operator-name> ::= ?M # <
829 case OO_Less: Out << "?M"; break;
830 // <operator-name> ::= ?N # <=
831 case OO_LessEqual: Out << "?N"; break;
832 // <operator-name> ::= ?O # >
833 case OO_Greater: Out << "?O"; break;
834 // <operator-name> ::= ?P # >=
835 case OO_GreaterEqual: Out << "?P"; break;
836 // <operator-name> ::= ?Q # ,
837 case OO_Comma: Out << "?Q"; break;
838 // <operator-name> ::= ?R # ()
839 case OO_Call: Out << "?R"; break;
840 // <operator-name> ::= ?S # ~
841 case OO_Tilde: Out << "?S"; break;
842 // <operator-name> ::= ?T # ^
843 case OO_Caret: Out << "?T"; break;
844 // <operator-name> ::= ?U # |
845 case OO_Pipe: Out << "?U"; break;
846 // <operator-name> ::= ?V # &&
847 case OO_AmpAmp: Out << "?V"; break;
848 // <operator-name> ::= ?W # ||
849 case OO_PipePipe: Out << "?W"; break;
850 // <operator-name> ::= ?X # *=
851 case OO_StarEqual: Out << "?X"; break;
852 // <operator-name> ::= ?Y # +=
853 case OO_PlusEqual: Out << "?Y"; break;
854 // <operator-name> ::= ?Z # -=
855 case OO_MinusEqual: Out << "?Z"; break;
856 // <operator-name> ::= ?_0 # /=
857 case OO_SlashEqual: Out << "?_0"; break;
858 // <operator-name> ::= ?_1 # %=
859 case OO_PercentEqual: Out << "?_1"; break;
860 // <operator-name> ::= ?_2 # >>=
861 case OO_GreaterGreaterEqual: Out << "?_2"; break;
862 // <operator-name> ::= ?_3 # <<=
863 case OO_LessLessEqual: Out << "?_3"; break;
864 // <operator-name> ::= ?_4 # &=
865 case OO_AmpEqual: Out << "?_4"; break;
866 // <operator-name> ::= ?_5 # |=
867 case OO_PipeEqual: Out << "?_5"; break;
868 // <operator-name> ::= ?_6 # ^=
869 case OO_CaretEqual: Out << "?_6"; break;
870 // ?_7 # vftable
871 // ?_8 # vbtable
872 // ?_9 # vcall
873 // ?_A # typeof
874 // ?_B # local static guard
875 // ?_C # string
876 // ?_D # vbase destructor
877 // ?_E # vector deleting destructor
878 // ?_F # default constructor closure
879 // ?_G # scalar deleting destructor
880 // ?_H # vector constructor iterator
881 // ?_I # vector destructor iterator
882 // ?_J # vector vbase constructor iterator
883 // ?_K # virtual displacement map
884 // ?_L # eh vector constructor iterator
885 // ?_M # eh vector destructor iterator
886 // ?_N # eh vector vbase constructor iterator
887 // ?_O # copy constructor closure
888 // ?_P<name> # udt returning <name>
889 // ?_Q # <unknown>
890 // ?_R0 # RTTI Type Descriptor
891 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
892 // ?_R2 # RTTI Base Class Array
893 // ?_R3 # RTTI Class Hierarchy Descriptor
894 // ?_R4 # RTTI Complete Object Locator
895 // ?_S # local vftable
896 // ?_T # local vftable constructor closure
897 // <operator-name> ::= ?_U # new[]
898 case OO_Array_New: Out << "?_U"; break;
899 // <operator-name> ::= ?_V # delete[]
900 case OO_Array_Delete: Out << "?_V"; break;
David Majnemerb4119f72013-12-13 01:06:04 +0000901
Guy Benyei11169dd2012-12-18 14:30:41 +0000902 case OO_Conditional: {
903 DiagnosticsEngine &Diags = Context.getDiags();
904 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
905 "cannot mangle this conditional operator yet");
906 Diags.Report(Loc, DiagID);
907 break;
908 }
David Majnemerb4119f72013-12-13 01:06:04 +0000909
Guy Benyei11169dd2012-12-18 14:30:41 +0000910 case OO_None:
911 case NUM_OVERLOADED_OPERATORS:
912 llvm_unreachable("Not an overloaded operator");
913 }
914}
915
David Majnemer956bc112013-11-25 17:50:19 +0000916void MicrosoftCXXNameMangler::mangleSourceName(StringRef Name) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000917 // <source name> ::= <identifier> @
Guy Benyei11169dd2012-12-18 14:30:41 +0000918 BackRefMap::iterator Found;
919 if (UseNameBackReferences)
David Majnemer956bc112013-11-25 17:50:19 +0000920 Found = NameBackReferences.find(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000921 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
David Majnemer956bc112013-11-25 17:50:19 +0000922 Out << Name << '@';
Guy Benyei11169dd2012-12-18 14:30:41 +0000923 if (UseNameBackReferences && NameBackReferences.size() < 10) {
924 size_t Size = NameBackReferences.size();
David Majnemer956bc112013-11-25 17:50:19 +0000925 NameBackReferences[Name] = Size;
Guy Benyei11169dd2012-12-18 14:30:41 +0000926 }
927 } else {
928 Out << Found->second;
929 }
930}
931
932void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
933 Context.mangleObjCMethodName(MD, Out);
934}
935
936// Find out how many function decls live above this one and return an integer
937// suitable for use as the number in a numbered anonymous scope.
938// TODO: Memoize.
939static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
940 const DeclContext *DC = FD->getParent();
941 int level = 1;
942
943 while (DC && !DC->isTranslationUnit()) {
944 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
945 DC = DC->getParent();
946 }
947
948 return 2*level;
949}
950
951void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
952 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
953 // <numbered-anonymous-scope> ::= ? <number>
954 // Even though the name is rendered in reverse order (e.g.
955 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
956 // innermost. So a method bar in class C local to function foo gets mangled
957 // as something like:
958 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
959 // This is more apparent when you have a type nested inside a method of a
960 // type nested inside a function. A method baz in class D local to method
961 // bar of class C local to function foo gets mangled as:
962 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
963 // This scheme is general enough to support GCC-style nested
964 // functions. You could have a method baz of class C inside a function bar
965 // inside a function foo, like so:
966 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +0000967 unsigned NestLevel = getLocalNestingLevel(FD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000968 Out << '?';
969 mangleNumber(NestLevel);
970 Out << '?';
971 mangle(FD, "?");
972}
973
974void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
975 const TemplateDecl *TD,
Reid Kleckner52518862013-03-20 01:40:23 +0000976 const TemplateArgumentList &TemplateArgs) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000977 // <template-name> ::= <unscoped-template-name> <template-args>
978 // ::= <substitution>
979 // Always start with the unqualified name.
980
981 // Templates have their own context for back references.
982 ArgBackRefMap OuterArgsContext;
983 BackRefMap OuterTemplateContext;
984 NameBackReferences.swap(OuterTemplateContext);
985 TypeBackReferences.swap(OuterArgsContext);
986
987 mangleUnscopedTemplateName(TD);
Reid Kleckner52518862013-03-20 01:40:23 +0000988 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei11169dd2012-12-18 14:30:41 +0000989
990 // Restore the previous back reference contexts.
991 NameBackReferences.swap(OuterTemplateContext);
992 TypeBackReferences.swap(OuterArgsContext);
993}
994
995void
996MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
997 // <unscoped-template-name> ::= ?$ <unqualified-name>
998 Out << "?$";
999 mangleUnqualifiedName(TD);
1000}
1001
1002void
1003MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
1004 bool IsBoolean) {
1005 // <integer-literal> ::= $0 <number>
1006 Out << "$0";
1007 // Make sure booleans are encoded as 0/1.
1008 if (IsBoolean && Value.getBoolValue())
1009 mangleNumber(1);
1010 else
David Majnemer2a816452013-12-09 10:44:32 +00001011 mangleNumber(Value.getSExtValue());
Guy Benyei11169dd2012-12-18 14:30:41 +00001012}
1013
1014void
1015MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
1016 // See if this is a constant expression.
1017 llvm::APSInt Value;
1018 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
1019 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
1020 return;
1021 }
1022
David Majnemer8eaab6f2013-08-13 06:32:20 +00001023 const CXXUuidofExpr *UE = 0;
1024 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
1025 if (UO->getOpcode() == UO_AddrOf)
1026 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
1027 } else
1028 UE = dyn_cast<CXXUuidofExpr>(E);
1029
1030 if (UE) {
1031 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
1032 // const __s_GUID _GUID_{lower case UUID with underscores}
1033 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
1034 std::string Name = "_GUID_" + Uuid.lower();
1035 std::replace(Name.begin(), Name.end(), '-', '_');
1036
David Majnemere9cab2f2013-08-13 09:17:25 +00001037 // If we had to peek through an address-of operator, treat this like we are
David Majnemer8eaab6f2013-08-13 06:32:20 +00001038 // dealing with a pointer type. Otherwise, treat it like a const reference.
1039 //
1040 // N.B. This matches up with the handling of TemplateArgument::Declaration
1041 // in mangleTemplateArg
1042 if (UE == E)
1043 Out << "$E?";
1044 else
1045 Out << "$1?";
1046 Out << Name << "@@3U__s_GUID@@B";
1047 return;
1048 }
1049
Guy Benyei11169dd2012-12-18 14:30:41 +00001050 // As bad as this diagnostic is, it's better than crashing.
1051 DiagnosticsEngine &Diags = Context.getDiags();
1052 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1053 "cannot yet mangle expression type %0");
1054 Diags.Report(E->getExprLoc(), DiagID)
1055 << E->getStmtClassName() << E->getSourceRange();
1056}
1057
1058void
Reid Kleckner52518862013-03-20 01:40:23 +00001059MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
1060 const TemplateArgumentList &TemplateArgs) {
Reid Kleckner96f8f932014-02-05 17:27:08 +00001061 // <template-args> ::= <template-arg>+ @
Guy Benyei11169dd2012-12-18 14:30:41 +00001062 unsigned NumTemplateArgs = TemplateArgs.size();
1063 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Kleckner52518862013-03-20 01:40:23 +00001064 const TemplateArgument &TA = TemplateArgs[i];
David Majnemer08177c52013-08-27 08:21:25 +00001065 mangleTemplateArg(TD, TA);
Guy Benyei11169dd2012-12-18 14:30:41 +00001066 }
1067 Out << '@';
1068}
1069
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001070void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
David Majnemer08177c52013-08-27 08:21:25 +00001071 const TemplateArgument &TA) {
Reid Kleckner96f8f932014-02-05 17:27:08 +00001072 // <template-arg> ::= <type>
1073 // ::= <integer-literal>
1074 // ::= <member-data-pointer>
1075 // ::= <member-function-pointer>
1076 // ::= $E? <name> <type-encoding>
1077 // ::= $1? <name> <type-encoding>
1078 // ::= $0A@
1079 // ::= <template-args>
1080
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001081 switch (TA.getKind()) {
1082 case TemplateArgument::Null:
1083 llvm_unreachable("Can't mangle null template arguments!");
David Majnemer08177c52013-08-27 08:21:25 +00001084 case TemplateArgument::TemplateExpansion:
1085 llvm_unreachable("Can't mangle template expansion arguments!");
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001086 case TemplateArgument::Type: {
1087 QualType T = TA.getAsType();
1088 mangleType(T, SourceRange(), QMM_Escape);
1089 break;
1090 }
David Majnemere8fdc062013-08-13 01:25:35 +00001091 case TemplateArgument::Declaration: {
1092 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl());
Reid Kleckner09b47d12014-02-05 18:59:38 +00001093 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ND)) {
Reid Kleckner96f8f932014-02-05 17:27:08 +00001094 mangleMemberDataPointer(cast<CXXRecordDecl>(FD->getParent()), FD);
Reid Kleckner09b47d12014-02-05 18:59:38 +00001095 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1096 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND);
1097 if (MD && MD->isInstance())
1098 mangleMemberFunctionPointer(MD->getParent(), MD);
1099 else
1100 mangle(ND, "$1?");
1101 } else {
Reid Kleckner96f8f932014-02-05 17:27:08 +00001102 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?");
Reid Kleckner09b47d12014-02-05 18:59:38 +00001103 }
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001104 break;
David Majnemere8fdc062013-08-13 01:25:35 +00001105 }
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001106 case TemplateArgument::Integral:
1107 mangleIntegerLiteral(TA.getAsIntegral(),
1108 TA.getIntegralType()->isBooleanType());
1109 break;
Reid Kleckner96f8f932014-02-05 17:27:08 +00001110 case TemplateArgument::NullPtr: {
1111 QualType T = TA.getNullPtrType();
1112 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
1113 const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl();
1114 if (MPT->isMemberFunctionPointerType())
1115 mangleMemberFunctionPointer(RD, 0);
1116 else
1117 mangleMemberDataPointer(RD, 0);
1118 } else {
1119 Out << "$0A@";
1120 }
David Majnemerae465ef2013-08-05 21:33:59 +00001121 break;
Reid Kleckner96f8f932014-02-05 17:27:08 +00001122 }
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001123 case TemplateArgument::Expression:
1124 mangleExpression(TA.getAsExpr());
1125 break;
1126 case TemplateArgument::Pack:
1127 // Unlike Itanium, there is no character code to indicate an argument pack.
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001128 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
1129 I != E; ++I)
David Majnemer08177c52013-08-27 08:21:25 +00001130 mangleTemplateArg(TD, *I);
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001131 break;
1132 case TemplateArgument::Template:
David Majnemer0db0ca42013-08-05 22:26:46 +00001133 mangleType(cast<TagDecl>(
1134 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
1135 break;
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001136 }
1137}
1138
Guy Benyei11169dd2012-12-18 14:30:41 +00001139void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
1140 bool IsMember) {
1141 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
1142 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
1143 // 'I' means __restrict (32/64-bit).
1144 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
1145 // keyword!
1146 // <base-cvr-qualifiers> ::= A # near
1147 // ::= B # near const
1148 // ::= C # near volatile
1149 // ::= D # near const volatile
1150 // ::= E # far (16-bit)
1151 // ::= F # far const (16-bit)
1152 // ::= G # far volatile (16-bit)
1153 // ::= H # far const volatile (16-bit)
1154 // ::= I # huge (16-bit)
1155 // ::= J # huge const (16-bit)
1156 // ::= K # huge volatile (16-bit)
1157 // ::= L # huge const volatile (16-bit)
1158 // ::= M <basis> # based
1159 // ::= N <basis> # based const
1160 // ::= O <basis> # based volatile
1161 // ::= P <basis> # based const volatile
1162 // ::= Q # near member
1163 // ::= R # near const member
1164 // ::= S # near volatile member
1165 // ::= T # near const volatile member
1166 // ::= U # far member (16-bit)
1167 // ::= V # far const member (16-bit)
1168 // ::= W # far volatile member (16-bit)
1169 // ::= X # far const volatile member (16-bit)
1170 // ::= Y # huge member (16-bit)
1171 // ::= Z # huge const member (16-bit)
1172 // ::= 0 # huge volatile member (16-bit)
1173 // ::= 1 # huge const volatile member (16-bit)
1174 // ::= 2 <basis> # based member
1175 // ::= 3 <basis> # based const member
1176 // ::= 4 <basis> # based volatile member
1177 // ::= 5 <basis> # based const volatile member
1178 // ::= 6 # near function (pointers only)
1179 // ::= 7 # far function (pointers only)
1180 // ::= 8 # near method (pointers only)
1181 // ::= 9 # far method (pointers only)
1182 // ::= _A <basis> # based function (pointers only)
1183 // ::= _B <basis> # based function (far?) (pointers only)
1184 // ::= _C <basis> # based method (pointers only)
1185 // ::= _D <basis> # based method (far?) (pointers only)
1186 // ::= _E # block (Clang)
1187 // <basis> ::= 0 # __based(void)
1188 // ::= 1 # __based(segment)?
1189 // ::= 2 <name> # __based(name)
1190 // ::= 3 # ?
1191 // ::= 4 # ?
1192 // ::= 5 # not really based
1193 bool HasConst = Quals.hasConst(),
1194 HasVolatile = Quals.hasVolatile();
David Majnemer89594f32013-08-05 22:43:06 +00001195
Guy Benyei11169dd2012-12-18 14:30:41 +00001196 if (!IsMember) {
1197 if (HasConst && HasVolatile) {
1198 Out << 'D';
1199 } else if (HasVolatile) {
1200 Out << 'C';
1201 } else if (HasConst) {
1202 Out << 'B';
1203 } else {
1204 Out << 'A';
1205 }
1206 } else {
1207 if (HasConst && HasVolatile) {
1208 Out << 'T';
1209 } else if (HasVolatile) {
1210 Out << 'S';
1211 } else if (HasConst) {
1212 Out << 'R';
1213 } else {
1214 Out << 'Q';
1215 }
1216 }
1217
1218 // FIXME: For now, just drop all extension qualifiers on the floor.
1219}
1220
1221void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1222 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1223 // ::= Q # const
1224 // ::= R # volatile
1225 // ::= S # const volatile
1226 bool HasConst = Quals.hasConst(),
1227 HasVolatile = Quals.hasVolatile();
1228 if (HasConst && HasVolatile) {
1229 Out << 'S';
1230 } else if (HasVolatile) {
1231 Out << 'R';
1232 } else if (HasConst) {
1233 Out << 'Q';
1234 } else {
1235 Out << 'P';
1236 }
1237}
1238
1239void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1240 SourceRange Range) {
Reid Kleckner18da98e2013-06-24 19:21:52 +00001241 // MSVC will backreference two canonically equivalent types that have slightly
1242 // different manglings when mangled alone.
David Majnemer5a1b2042013-09-11 04:44:30 +00001243
1244 // Decayed types do not match up with non-decayed versions of the same type.
1245 //
1246 // e.g.
1247 // void (*x)(void) will not form a backreference with void x(void)
1248 void *TypePtr;
1249 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1250 TypePtr = DT->getOriginalType().getCanonicalType().getAsOpaquePtr();
1251 // If the original parameter was textually written as an array,
1252 // instead treat the decayed parameter like it's const.
1253 //
1254 // e.g.
1255 // int [] -> int * const
1256 if (DT->getOriginalType()->isArrayType())
1257 T = T.withConst();
1258 } else
1259 TypePtr = T.getCanonicalType().getAsOpaquePtr();
1260
Guy Benyei11169dd2012-12-18 14:30:41 +00001261 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1262
1263 if (Found == TypeBackReferences.end()) {
1264 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1265
David Majnemer5a1b2042013-09-11 04:44:30 +00001266 mangleType(T, Range, QMM_Drop);
Guy Benyei11169dd2012-12-18 14:30:41 +00001267
1268 // See if it's worth creating a back reference.
1269 // Only types longer than 1 character are considered
1270 // and only 10 back references slots are available:
1271 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1272 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1273 size_t Size = TypeBackReferences.size();
1274 TypeBackReferences[TypePtr] = Size;
1275 }
1276 } else {
1277 Out << Found->second;
1278 }
1279}
1280
1281void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourne2816c022013-04-25 04:25:40 +00001282 QualifierMangleMode QMM) {
Reid Kleckner18da98e2013-06-24 19:21:52 +00001283 // Don't use the canonical types. MSVC includes things like 'const' on
1284 // pointer arguments to function pointers that canonicalization strips away.
1285 T = T.getDesugaredType(getASTContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001286 Qualifiers Quals = T.getLocalQualifiers();
Reid Kleckner18da98e2013-06-24 19:21:52 +00001287 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1288 // If there were any Quals, getAsArrayType() pushed them onto the array
1289 // element type.
Peter Collingbourne2816c022013-04-25 04:25:40 +00001290 if (QMM == QMM_Mangle)
1291 Out << 'A';
1292 else if (QMM == QMM_Escape || QMM == QMM_Result)
1293 Out << "$$B";
Reid Kleckner18da98e2013-06-24 19:21:52 +00001294 mangleArrayType(AT);
Peter Collingbourne2816c022013-04-25 04:25:40 +00001295 return;
1296 }
1297
1298 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1299 T->isBlockPointerType();
1300
1301 switch (QMM) {
1302 case QMM_Drop:
1303 break;
1304 case QMM_Mangle:
1305 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1306 Out << '6';
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001307 mangleFunctionType(FT);
Peter Collingbourne2816c022013-04-25 04:25:40 +00001308 return;
1309 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001310 mangleQualifiers(Quals, false);
Peter Collingbourne2816c022013-04-25 04:25:40 +00001311 break;
1312 case QMM_Escape:
1313 if (!IsPointer && Quals) {
1314 Out << "$$C";
1315 mangleQualifiers(Quals, false);
1316 }
1317 break;
1318 case QMM_Result:
1319 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1320 Out << '?';
1321 mangleQualifiers(Quals, false);
1322 }
1323 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001324 }
1325
Peter Collingbourne2816c022013-04-25 04:25:40 +00001326 // We have to mangle these now, while we still have enough information.
1327 if (IsPointer)
1328 manglePointerQualifiers(Quals);
1329 const Type *ty = T.getTypePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +00001330
1331 switch (ty->getTypeClass()) {
1332#define ABSTRACT_TYPE(CLASS, PARENT)
1333#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1334 case Type::CLASS: \
1335 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1336 return;
1337#define TYPE(CLASS, PARENT) \
1338 case Type::CLASS: \
1339 mangleType(cast<CLASS##Type>(ty), Range); \
1340 break;
1341#include "clang/AST/TypeNodes.def"
1342#undef ABSTRACT_TYPE
1343#undef NON_CANONICAL_TYPE
1344#undef TYPE
1345 }
1346}
1347
1348void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1349 SourceRange Range) {
1350 // <type> ::= <builtin-type>
1351 // <builtin-type> ::= X # void
1352 // ::= C # signed char
1353 // ::= D # char
1354 // ::= E # unsigned char
1355 // ::= F # short
1356 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1357 // ::= H # int
1358 // ::= I # unsigned int
1359 // ::= J # long
1360 // ::= K # unsigned long
1361 // L # <none>
1362 // ::= M # float
1363 // ::= N # double
1364 // ::= O # long double (__float80 is mangled differently)
1365 // ::= _J # long long, __int64
1366 // ::= _K # unsigned long long, __int64
1367 // ::= _L # __int128
1368 // ::= _M # unsigned __int128
1369 // ::= _N # bool
1370 // _O # <array in parameter>
1371 // ::= _T # __float80 (Intel)
1372 // ::= _W # wchar_t
1373 // ::= _Z # __float80 (Digital Mars)
1374 switch (T->getKind()) {
1375 case BuiltinType::Void: Out << 'X'; break;
1376 case BuiltinType::SChar: Out << 'C'; break;
1377 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1378 case BuiltinType::UChar: Out << 'E'; break;
1379 case BuiltinType::Short: Out << 'F'; break;
1380 case BuiltinType::UShort: Out << 'G'; break;
1381 case BuiltinType::Int: Out << 'H'; break;
1382 case BuiltinType::UInt: Out << 'I'; break;
1383 case BuiltinType::Long: Out << 'J'; break;
1384 case BuiltinType::ULong: Out << 'K'; break;
1385 case BuiltinType::Float: Out << 'M'; break;
1386 case BuiltinType::Double: Out << 'N'; break;
1387 // TODO: Determine size and mangle accordingly
1388 case BuiltinType::LongDouble: Out << 'O'; break;
1389 case BuiltinType::LongLong: Out << "_J"; break;
1390 case BuiltinType::ULongLong: Out << "_K"; break;
1391 case BuiltinType::Int128: Out << "_L"; break;
1392 case BuiltinType::UInt128: Out << "_M"; break;
1393 case BuiltinType::Bool: Out << "_N"; break;
1394 case BuiltinType::WChar_S:
1395 case BuiltinType::WChar_U: Out << "_W"; break;
1396
1397#define BUILTIN_TYPE(Id, SingletonId)
1398#define PLACEHOLDER_TYPE(Id, SingletonId) \
1399 case BuiltinType::Id:
1400#include "clang/AST/BuiltinTypes.def"
1401 case BuiltinType::Dependent:
1402 llvm_unreachable("placeholder types shouldn't get to name mangling");
1403
1404 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1405 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1406 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00001407
1408 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1409 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1410 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1411 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1412 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1413 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei61054192013-02-07 10:55:47 +00001414 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001415 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
David Majnemerb4119f72013-12-13 01:06:04 +00001416
Guy Benyei11169dd2012-12-18 14:30:41 +00001417 case BuiltinType::NullPtr: Out << "$$T"; break;
1418
1419 case BuiltinType::Char16:
1420 case BuiltinType::Char32:
1421 case BuiltinType::Half: {
1422 DiagnosticsEngine &Diags = Context.getDiags();
1423 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1424 "cannot mangle this built-in %0 type yet");
1425 Diags.Report(Range.getBegin(), DiagID)
1426 << T->getName(Context.getASTContext().getPrintingPolicy())
1427 << Range;
1428 break;
1429 }
1430 }
1431}
1432
1433// <type> ::= <function-type>
1434void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1435 SourceRange) {
1436 // Structors only appear in decls, so at this point we know it's not a
1437 // structor type.
1438 // FIXME: This may not be lambda-friendly.
1439 Out << "$$A6";
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001440 mangleFunctionType(T);
Guy Benyei11169dd2012-12-18 14:30:41 +00001441}
1442void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1443 SourceRange) {
1444 llvm_unreachable("Can't mangle K&R function prototypes");
1445}
1446
Peter Collingbourne2816c022013-04-25 04:25:40 +00001447void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1448 const FunctionDecl *D,
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001449 bool ForceInstMethod) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001450 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1451 // <return-type> <argument-list> <throw-spec>
1452 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1453
Reid Kleckner18da98e2013-06-24 19:21:52 +00001454 SourceRange Range;
1455 if (D) Range = D->getSourceRange();
1456
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001457 bool IsStructor = false, IsInstMethod = ForceInstMethod;
1458 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(D)) {
1459 if (MD->isInstance())
1460 IsInstMethod = true;
1461 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
1462 IsStructor = true;
1463 }
1464
Guy Benyei11169dd2012-12-18 14:30:41 +00001465 // If this is a C++ instance method, mangle the CVR qualifiers for the
1466 // this pointer.
David Majnemer6dda7bb2013-08-15 08:13:23 +00001467 if (IsInstMethod) {
1468 if (PointersAre64Bit)
1469 Out << 'E';
Guy Benyei11169dd2012-12-18 14:30:41 +00001470 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
David Majnemer6dda7bb2013-08-15 08:13:23 +00001471 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001472
Reid Klecknerc5cc3382013-09-25 22:28:52 +00001473 mangleCallingConvention(T);
Guy Benyei11169dd2012-12-18 14:30:41 +00001474
1475 // <return-type> ::= <type>
1476 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001477 if (IsStructor) {
1478 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1479 StructorType == Dtor_Deleting) {
1480 // The scalar deleting destructor takes an extra int argument.
1481 // However, the FunctionType generated has 0 arguments.
1482 // FIXME: This is a temporary hack.
1483 // Maybe should fix the FunctionType creation instead?
Timur Iskhodzhanovf46993e2013-08-26 10:32:04 +00001484 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001485 return;
1486 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001487 Out << '@';
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001488 } else {
Alp Toker314cc812014-01-25 16:55:45 +00001489 QualType ResultType = Proto->getReturnType();
David Majnemer6dda7bb2013-08-15 08:13:23 +00001490 if (ResultType->isVoidType())
1491 ResultType = ResultType.getUnqualifiedType();
1492 mangleType(ResultType, Range, QMM_Result);
Guy Benyei11169dd2012-12-18 14:30:41 +00001493 }
1494
1495 // <argument-list> ::= X # void
1496 // ::= <type>+ @
1497 // ::= <type>* Z # varargs
Alp Toker9cacbab2014-01-20 20:26:09 +00001498 if (Proto->getNumParams() == 0 && !Proto->isVariadic()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001499 Out << 'X';
1500 } else {
Reid Kleckner18da98e2013-06-24 19:21:52 +00001501 // Happens for function pointer type arguments for example.
Alp Toker9cacbab2014-01-20 20:26:09 +00001502 for (FunctionProtoType::param_type_iterator
1503 Arg = Proto->param_type_begin(),
1504 ArgEnd = Proto->param_type_end();
Reid Kleckner18da98e2013-06-24 19:21:52 +00001505 Arg != ArgEnd; ++Arg)
1506 mangleArgumentType(*Arg, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001507 // <builtin-type> ::= Z # ellipsis
1508 if (Proto->isVariadic())
1509 Out << 'Z';
1510 else
1511 Out << '@';
1512 }
1513
1514 mangleThrowSpecification(Proto);
1515}
1516
1517void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Kleckner369f3162013-05-14 20:30:42 +00001518 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1519 // # pointer. in 64-bit mode *all*
1520 // # 'this' pointers are 64-bit.
1521 // ::= <global-function>
1522 // <member-function> ::= A # private: near
1523 // ::= B # private: far
1524 // ::= C # private: static near
1525 // ::= D # private: static far
1526 // ::= E # private: virtual near
1527 // ::= F # private: virtual far
Reid Kleckner369f3162013-05-14 20:30:42 +00001528 // ::= I # protected: near
1529 // ::= J # protected: far
1530 // ::= K # protected: static near
1531 // ::= L # protected: static far
1532 // ::= M # protected: virtual near
1533 // ::= N # protected: virtual far
Reid Kleckner369f3162013-05-14 20:30:42 +00001534 // ::= Q # public: near
1535 // ::= R # public: far
1536 // ::= S # public: static near
1537 // ::= T # public: static far
1538 // ::= U # public: virtual near
1539 // ::= V # public: virtual far
Reid Kleckner369f3162013-05-14 20:30:42 +00001540 // <global-function> ::= Y # global near
1541 // ::= Z # global far
Guy Benyei11169dd2012-12-18 14:30:41 +00001542 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1543 switch (MD->getAccess()) {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00001544 case AS_none:
1545 llvm_unreachable("Unsupported access specifier");
Guy Benyei11169dd2012-12-18 14:30:41 +00001546 case AS_private:
1547 if (MD->isStatic())
1548 Out << 'C';
1549 else if (MD->isVirtual())
1550 Out << 'E';
1551 else
1552 Out << 'A';
1553 break;
1554 case AS_protected:
1555 if (MD->isStatic())
1556 Out << 'K';
1557 else if (MD->isVirtual())
1558 Out << 'M';
1559 else
1560 Out << 'I';
1561 break;
1562 case AS_public:
1563 if (MD->isStatic())
1564 Out << 'S';
1565 else if (MD->isVirtual())
1566 Out << 'U';
1567 else
1568 Out << 'Q';
1569 }
1570 } else
1571 Out << 'Y';
1572}
Reid Klecknerc5cc3382013-09-25 22:28:52 +00001573void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001574 // <calling-convention> ::= A # __cdecl
1575 // ::= B # __export __cdecl
1576 // ::= C # __pascal
1577 // ::= D # __export __pascal
1578 // ::= E # __thiscall
1579 // ::= F # __export __thiscall
1580 // ::= G # __stdcall
1581 // ::= H # __export __stdcall
1582 // ::= I # __fastcall
1583 // ::= J # __export __fastcall
1584 // The 'export' calling conventions are from a bygone era
1585 // (*cough*Win16*cough*) when functions were declared for export with
1586 // that keyword. (It didn't actually export them, it just made them so
1587 // that they could be in a DLL and somebody from another module could call
1588 // them.)
1589 CallingConv CC = T->getCallConv();
Guy Benyei11169dd2012-12-18 14:30:41 +00001590 switch (CC) {
1591 default:
1592 llvm_unreachable("Unsupported CC for mangling");
Charles Davisb5a214e2013-08-30 04:39:01 +00001593 case CC_X86_64Win64:
1594 case CC_X86_64SysV:
Guy Benyei11169dd2012-12-18 14:30:41 +00001595 case CC_C: Out << 'A'; break;
1596 case CC_X86Pascal: Out << 'C'; break;
1597 case CC_X86ThisCall: Out << 'E'; break;
1598 case CC_X86StdCall: Out << 'G'; break;
1599 case CC_X86FastCall: Out << 'I'; break;
1600 }
1601}
1602void MicrosoftCXXNameMangler::mangleThrowSpecification(
1603 const FunctionProtoType *FT) {
1604 // <throw-spec> ::= Z # throw(...) (default)
1605 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1606 // ::= <type>+
1607 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1608 // all actually mangled as 'Z'. (They're ignored because their associated
1609 // functionality isn't implemented, and probably never will be.)
1610 Out << 'Z';
1611}
1612
1613void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1614 SourceRange Range) {
1615 // Probably should be mangled as a template instantiation; need to see what
1616 // VC does first.
1617 DiagnosticsEngine &Diags = Context.getDiags();
1618 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1619 "cannot mangle this unresolved dependent type yet");
1620 Diags.Report(Range.getBegin(), DiagID)
1621 << Range;
1622}
1623
1624// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1625// <union-type> ::= T <name>
1626// <struct-type> ::= U <name>
1627// <class-type> ::= V <name>
David Majnemer048f90c2013-12-09 04:28:34 +00001628// <enum-type> ::= W4 <name>
Guy Benyei11169dd2012-12-18 14:30:41 +00001629void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer0db0ca42013-08-05 22:26:46 +00001630 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00001631}
1632void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer0db0ca42013-08-05 22:26:46 +00001633 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00001634}
David Majnemer0db0ca42013-08-05 22:26:46 +00001635void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1636 switch (TD->getTagKind()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001637 case TTK_Union:
1638 Out << 'T';
1639 break;
1640 case TTK_Struct:
1641 case TTK_Interface:
1642 Out << 'U';
1643 break;
1644 case TTK_Class:
1645 Out << 'V';
1646 break;
1647 case TTK_Enum:
David Majnemer048f90c2013-12-09 04:28:34 +00001648 Out << "W4";
Guy Benyei11169dd2012-12-18 14:30:41 +00001649 break;
1650 }
David Majnemer0db0ca42013-08-05 22:26:46 +00001651 mangleName(TD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001652}
1653
1654// <type> ::= <array-type>
1655// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1656// [Y <dimension-count> <dimension>+]
Reid Kleckner369f3162013-05-14 20:30:42 +00001657// <element-type> # as global, E is never required
Guy Benyei11169dd2012-12-18 14:30:41 +00001658// It's supposed to be the other way around, but for some strange reason, it
1659// isn't. Today this behavior is retained for the sole purpose of backwards
1660// compatibility.
David Majnemer5a1b2042013-09-11 04:44:30 +00001661void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001662 // This isn't a recursive mangling, so now we have to do it all in this
1663 // one call.
David Majnemer5a1b2042013-09-11 04:44:30 +00001664 manglePointerQualifiers(T->getElementType().getQualifiers());
Peter Collingbourne2816c022013-04-25 04:25:40 +00001665 mangleType(T->getElementType(), SourceRange());
Guy Benyei11169dd2012-12-18 14:30:41 +00001666}
1667void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1668 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001669 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001670}
1671void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1672 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001673 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001674}
1675void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1676 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001677 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001678}
1679void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1680 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001681 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001682}
Reid Kleckner18da98e2013-06-24 19:21:52 +00001683void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001684 QualType ElementTy(T, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001685 SmallVector<llvm::APInt, 3> Dimensions;
1686 for (;;) {
1687 if (const ConstantArrayType *CAT =
1688 getASTContext().getAsConstantArrayType(ElementTy)) {
1689 Dimensions.push_back(CAT->getSize());
1690 ElementTy = CAT->getElementType();
1691 } else if (ElementTy->isVariableArrayType()) {
1692 const VariableArrayType *VAT =
1693 getASTContext().getAsVariableArrayType(ElementTy);
1694 DiagnosticsEngine &Diags = Context.getDiags();
1695 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1696 "cannot mangle this variable-length array yet");
1697 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1698 << VAT->getBracketsRange();
1699 return;
1700 } else if (ElementTy->isDependentSizedArrayType()) {
1701 // The dependent expression has to be folded into a constant (TODO).
1702 const DependentSizedArrayType *DSAT =
1703 getASTContext().getAsDependentSizedArrayType(ElementTy);
1704 DiagnosticsEngine &Diags = Context.getDiags();
1705 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1706 "cannot mangle this dependent-length array yet");
1707 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1708 << DSAT->getBracketsRange();
1709 return;
Peter Collingbourne2816c022013-04-25 04:25:40 +00001710 } else if (const IncompleteArrayType *IAT =
1711 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1712 Dimensions.push_back(llvm::APInt(32, 0));
1713 ElementTy = IAT->getElementType();
1714 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001715 else break;
1716 }
Peter Collingbourne2816c022013-04-25 04:25:40 +00001717 Out << 'Y';
1718 // <dimension-count> ::= <number> # number of extra dimensions
1719 mangleNumber(Dimensions.size());
1720 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1721 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Kleckner18da98e2013-06-24 19:21:52 +00001722 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei11169dd2012-12-18 14:30:41 +00001723}
1724
1725// <type> ::= <pointer-to-member-type>
1726// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1727// <class name> <type>
1728void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1729 SourceRange Range) {
1730 QualType PointeeType = T->getPointeeType();
1731 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1732 Out << '8';
1733 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001734 mangleFunctionType(FPT, 0, true);
Guy Benyei11169dd2012-12-18 14:30:41 +00001735 } else {
David Majnemer6dda7bb2013-08-15 08:13:23 +00001736 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1737 Out << 'E';
Guy Benyei11169dd2012-12-18 14:30:41 +00001738 mangleQualifiers(PointeeType.getQualifiers(), true);
1739 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourne2816c022013-04-25 04:25:40 +00001740 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei11169dd2012-12-18 14:30:41 +00001741 }
1742}
1743
1744void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1745 SourceRange Range) {
1746 DiagnosticsEngine &Diags = Context.getDiags();
1747 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1748 "cannot mangle this template type parameter type yet");
1749 Diags.Report(Range.getBegin(), DiagID)
1750 << Range;
1751}
1752
1753void MicrosoftCXXNameMangler::mangleType(
1754 const SubstTemplateTypeParmPackType *T,
1755 SourceRange Range) {
1756 DiagnosticsEngine &Diags = Context.getDiags();
1757 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1758 "cannot mangle this substituted parameter pack yet");
1759 Diags.Report(Range.getBegin(), DiagID)
1760 << Range;
1761}
1762
1763// <type> ::= <pointer-type>
Reid Kleckner369f3162013-05-14 20:30:42 +00001764// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
Alp Tokerd4733632013-12-05 04:47:09 +00001765// # the E is required for 64-bit non-static pointers
Guy Benyei11169dd2012-12-18 14:30:41 +00001766void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1767 SourceRange Range) {
1768 QualType PointeeTy = T->getPointeeType();
Reid Kleckner369f3162013-05-14 20:30:42 +00001769 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1770 Out << 'E';
Peter Collingbourne2816c022013-04-25 04:25:40 +00001771 mangleType(PointeeTy, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001772}
1773void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1774 SourceRange Range) {
1775 // Object pointers never have qualifiers.
1776 Out << 'A';
David Majnemer6dda7bb2013-08-15 08:13:23 +00001777 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1778 Out << 'E';
Guy Benyei11169dd2012-12-18 14:30:41 +00001779 mangleType(T->getPointeeType(), Range);
1780}
1781
1782// <type> ::= <reference-type>
Reid Kleckner369f3162013-05-14 20:30:42 +00001783// <reference-type> ::= A E? <cvr-qualifiers> <type>
Alp Tokerd4733632013-12-05 04:47:09 +00001784// # the E is required for 64-bit non-static lvalue references
Guy Benyei11169dd2012-12-18 14:30:41 +00001785void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1786 SourceRange Range) {
1787 Out << 'A';
Reid Kleckner369f3162013-05-14 20:30:42 +00001788 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1789 Out << 'E';
Peter Collingbourne2816c022013-04-25 04:25:40 +00001790 mangleType(T->getPointeeType(), Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001791}
1792
1793// <type> ::= <r-value-reference-type>
Reid Kleckner369f3162013-05-14 20:30:42 +00001794// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
Alp Tokerd4733632013-12-05 04:47:09 +00001795// # the E is required for 64-bit non-static rvalue references
Guy Benyei11169dd2012-12-18 14:30:41 +00001796void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1797 SourceRange Range) {
1798 Out << "$$Q";
Reid Kleckner369f3162013-05-14 20:30:42 +00001799 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1800 Out << 'E';
Peter Collingbourne2816c022013-04-25 04:25:40 +00001801 mangleType(T->getPointeeType(), Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001802}
1803
1804void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1805 SourceRange Range) {
1806 DiagnosticsEngine &Diags = Context.getDiags();
1807 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1808 "cannot mangle this complex number type yet");
1809 Diags.Report(Range.getBegin(), DiagID)
1810 << Range;
1811}
1812
1813void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1814 SourceRange Range) {
Reid Klecknere7e64d82013-03-26 16:56:59 +00001815 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1816 assert(ET && "vectors with non-builtin elements are unsupported");
1817 uint64_t Width = getASTContext().getTypeSize(T);
1818 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1819 // doesn't match the Intel types uses a custom mangling below.
1820 bool IntelVector = true;
1821 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1822 Out << "T__m64";
1823 } else if (Width == 128 || Width == 256) {
1824 if (ET->getKind() == BuiltinType::Float)
1825 Out << "T__m" << Width;
1826 else if (ET->getKind() == BuiltinType::LongLong)
1827 Out << "T__m" << Width << 'i';
1828 else if (ET->getKind() == BuiltinType::Double)
1829 Out << "U__m" << Width << 'd';
1830 else
1831 IntelVector = false;
1832 } else {
1833 IntelVector = false;
1834 }
1835
1836 if (!IntelVector) {
1837 // The MS ABI doesn't have a special mangling for vector types, so we define
1838 // our own mangling to handle uses of __vector_size__ on user-specified
1839 // types, and for extensions like __v4sf.
1840 Out << "T__clang_vec" << T->getNumElements() << '_';
1841 mangleType(ET, Range);
1842 }
1843
1844 Out << "@@";
Guy Benyei11169dd2012-12-18 14:30:41 +00001845}
Reid Klecknere7e64d82013-03-26 16:56:59 +00001846
Guy Benyei11169dd2012-12-18 14:30:41 +00001847void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1848 SourceRange Range) {
1849 DiagnosticsEngine &Diags = Context.getDiags();
1850 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1851 "cannot mangle this extended vector type yet");
1852 Diags.Report(Range.getBegin(), DiagID)
1853 << Range;
1854}
1855void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1856 SourceRange Range) {
1857 DiagnosticsEngine &Diags = Context.getDiags();
1858 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1859 "cannot mangle this dependent-sized extended vector type yet");
1860 Diags.Report(Range.getBegin(), DiagID)
1861 << Range;
1862}
1863
1864void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1865 SourceRange) {
1866 // ObjC interfaces have structs underlying them.
1867 Out << 'U';
1868 mangleName(T->getDecl());
1869}
1870
1871void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1872 SourceRange Range) {
1873 // We don't allow overloading by different protocol qualification,
1874 // so mangling them isn't necessary.
1875 mangleType(T->getBaseType(), Range);
1876}
1877
1878void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1879 SourceRange Range) {
1880 Out << "_E";
1881
1882 QualType pointee = T->getPointeeType();
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001883 mangleFunctionType(pointee->castAs<FunctionProtoType>());
Guy Benyei11169dd2012-12-18 14:30:41 +00001884}
1885
David Majnemerf0a84f22013-08-16 08:29:13 +00001886void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1887 SourceRange) {
1888 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei11169dd2012-12-18 14:30:41 +00001889}
1890
1891void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1892 SourceRange Range) {
1893 DiagnosticsEngine &Diags = Context.getDiags();
1894 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1895 "cannot mangle this template specialization type yet");
1896 Diags.Report(Range.getBegin(), DiagID)
1897 << Range;
1898}
1899
1900void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1901 SourceRange Range) {
1902 DiagnosticsEngine &Diags = Context.getDiags();
1903 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1904 "cannot mangle this dependent name type yet");
1905 Diags.Report(Range.getBegin(), DiagID)
1906 << Range;
1907}
1908
1909void MicrosoftCXXNameMangler::mangleType(
1910 const DependentTemplateSpecializationType *T,
1911 SourceRange Range) {
1912 DiagnosticsEngine &Diags = Context.getDiags();
1913 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1914 "cannot mangle this dependent template specialization type yet");
1915 Diags.Report(Range.getBegin(), DiagID)
1916 << Range;
1917}
1918
1919void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1920 SourceRange Range) {
1921 DiagnosticsEngine &Diags = Context.getDiags();
1922 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1923 "cannot mangle this pack expansion yet");
1924 Diags.Report(Range.getBegin(), DiagID)
1925 << Range;
1926}
1927
1928void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1929 SourceRange Range) {
1930 DiagnosticsEngine &Diags = Context.getDiags();
1931 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1932 "cannot mangle this typeof(type) yet");
1933 Diags.Report(Range.getBegin(), DiagID)
1934 << Range;
1935}
1936
1937void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1938 SourceRange Range) {
1939 DiagnosticsEngine &Diags = Context.getDiags();
1940 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1941 "cannot mangle this typeof(expression) yet");
1942 Diags.Report(Range.getBegin(), DiagID)
1943 << Range;
1944}
1945
1946void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1947 SourceRange Range) {
1948 DiagnosticsEngine &Diags = Context.getDiags();
1949 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1950 "cannot mangle this decltype() yet");
1951 Diags.Report(Range.getBegin(), DiagID)
1952 << Range;
1953}
1954
1955void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1956 SourceRange Range) {
1957 DiagnosticsEngine &Diags = Context.getDiags();
1958 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1959 "cannot mangle this unary transform type yet");
1960 Diags.Report(Range.getBegin(), DiagID)
1961 << Range;
1962}
1963
1964void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
David Majnemerb9a5f2d2014-01-21 20:33:36 +00001965 assert(T->getDeducedType().isNull() && "expecting a dependent type!");
1966
Guy Benyei11169dd2012-12-18 14:30:41 +00001967 DiagnosticsEngine &Diags = Context.getDiags();
1968 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1969 "cannot mangle this 'auto' type yet");
1970 Diags.Report(Range.getBegin(), DiagID)
1971 << Range;
1972}
1973
1974void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1975 SourceRange Range) {
1976 DiagnosticsEngine &Diags = Context.getDiags();
1977 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1978 "cannot mangle this C11 atomic type yet");
1979 Diags.Report(Range.getBegin(), DiagID)
1980 << Range;
1981}
1982
Rafael Espindola002667c2013-10-16 01:40:34 +00001983void MicrosoftMangleContextImpl::mangleCXXName(const NamedDecl *D,
1984 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001985 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1986 "Invalid mangleName() call, argument is not a variable or function!");
1987 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1988 "Invalid mangleName() call on 'structor decl!");
1989
1990 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1991 getASTContext().getSourceManager(),
1992 "Mangling declaration");
1993
1994 MicrosoftCXXNameMangler Mangler(*this, Out);
1995 return Mangler.mangle(D);
1996}
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001997
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00001998// <this-adjustment> ::= <no-adjustment> | <static-adjustment> |
1999// <virtual-adjustment>
2000// <no-adjustment> ::= A # private near
2001// ::= B # private far
2002// ::= I # protected near
2003// ::= J # protected far
2004// ::= Q # public near
2005// ::= R # public far
2006// <static-adjustment> ::= G <static-offset> # private near
2007// ::= H <static-offset> # private far
2008// ::= O <static-offset> # protected near
2009// ::= P <static-offset> # protected far
2010// ::= W <static-offset> # public near
2011// ::= X <static-offset> # public far
2012// <virtual-adjustment> ::= $0 <virtual-shift> <static-offset> # private near
2013// ::= $1 <virtual-shift> <static-offset> # private far
2014// ::= $2 <virtual-shift> <static-offset> # protected near
2015// ::= $3 <virtual-shift> <static-offset> # protected far
2016// ::= $4 <virtual-shift> <static-offset> # public near
2017// ::= $5 <virtual-shift> <static-offset> # public far
2018// <virtual-shift> ::= <vtordisp-shift> | <vtordispex-shift>
2019// <vtordisp-shift> ::= <offset-to-vtordisp>
2020// <vtordispex-shift> ::= <offset-to-vbptr> <vbase-offset-offset>
2021// <offset-to-vtordisp>
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002022static void mangleThunkThisAdjustment(const CXXMethodDecl *MD,
2023 const ThisAdjustment &Adjustment,
2024 MicrosoftCXXNameMangler &Mangler,
2025 raw_ostream &Out) {
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002026 if (!Adjustment.Virtual.isEmpty()) {
2027 Out << '$';
2028 char AccessSpec;
2029 switch (MD->getAccess()) {
2030 case AS_none:
2031 llvm_unreachable("Unsupported access specifier");
2032 case AS_private:
2033 AccessSpec = '0';
2034 break;
2035 case AS_protected:
2036 AccessSpec = '2';
2037 break;
2038 case AS_public:
2039 AccessSpec = '4';
2040 }
2041 if (Adjustment.Virtual.Microsoft.VBPtrOffset) {
2042 Out << 'R' << AccessSpec;
David Majnemer2a816452013-12-09 10:44:32 +00002043 Mangler.mangleNumber(
2044 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBPtrOffset));
2045 Mangler.mangleNumber(
2046 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBOffsetOffset));
2047 Mangler.mangleNumber(
2048 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset));
2049 Mangler.mangleNumber(static_cast<uint32_t>(Adjustment.NonVirtual));
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002050 } else {
2051 Out << AccessSpec;
David Majnemer2a816452013-12-09 10:44:32 +00002052 Mangler.mangleNumber(
2053 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset));
2054 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual));
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002055 }
2056 } else if (Adjustment.NonVirtual != 0) {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002057 switch (MD->getAccess()) {
2058 case AS_none:
2059 llvm_unreachable("Unsupported access specifier");
2060 case AS_private:
2061 Out << 'G';
2062 break;
2063 case AS_protected:
2064 Out << 'O';
2065 break;
2066 case AS_public:
2067 Out << 'W';
2068 }
David Majnemer2a816452013-12-09 10:44:32 +00002069 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual));
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002070 } else {
2071 switch (MD->getAccess()) {
2072 case AS_none:
2073 llvm_unreachable("Unsupported access specifier");
2074 case AS_private:
2075 Out << 'A';
2076 break;
2077 case AS_protected:
2078 Out << 'I';
2079 break;
2080 case AS_public:
2081 Out << 'Q';
2082 }
2083 }
2084}
2085
Reid Kleckner96f8f932014-02-05 17:27:08 +00002086void
2087MicrosoftMangleContextImpl::mangleVirtualMemPtrThunk(const CXXMethodDecl *MD,
2088 raw_ostream &Out) {
2089 MicrosoftVTableContext *VTContext =
2090 cast<MicrosoftVTableContext>(getASTContext().getVTableContext());
2091 const MicrosoftVTableContext::MethodVFTableLocation &ML =
2092 VTContext->getMethodVFTableLocation(GlobalDecl(MD));
Hans Wennborg88497d62013-11-15 17:24:45 +00002093
2094 MicrosoftCXXNameMangler Mangler(*this, Out);
Reid Kleckner96f8f932014-02-05 17:27:08 +00002095 Mangler.getStream() << "\01?";
2096 Mangler.mangleVirtualMemPtrThunk(MD, ML);
Hans Wennborg88497d62013-11-15 17:24:45 +00002097}
2098
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002099void MicrosoftMangleContextImpl::mangleThunk(const CXXMethodDecl *MD,
2100 const ThunkInfo &Thunk,
2101 raw_ostream &Out) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002102 MicrosoftCXXNameMangler Mangler(*this, Out);
2103 Out << "\01?";
2104 Mangler.mangleName(MD);
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002105 mangleThunkThisAdjustment(MD, Thunk.This, Mangler, Out);
2106 if (!Thunk.Return.isEmpty())
2107 assert(Thunk.Method != 0 && "Thunk info should hold the overridee decl");
2108
2109 const CXXMethodDecl *DeclForFPT = Thunk.Method ? Thunk.Method : MD;
2110 Mangler.mangleFunctionType(
2111 DeclForFPT->getType()->castAs<FunctionProtoType>(), MD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002112}
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002113
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002114void MicrosoftMangleContextImpl::mangleCXXDtorThunk(
2115 const CXXDestructorDecl *DD, CXXDtorType Type,
2116 const ThisAdjustment &Adjustment, raw_ostream &Out) {
2117 // FIXME: Actually, the dtor thunk should be emitted for vector deleting
2118 // dtors rather than scalar deleting dtors. Just use the vector deleting dtor
2119 // mangling manually until we support both deleting dtor types.
2120 assert(Type == Dtor_Deleting);
2121 MicrosoftCXXNameMangler Mangler(*this, Out, DD, Type);
2122 Out << "\01??_E";
2123 Mangler.mangleName(DD->getParent());
2124 mangleThunkThisAdjustment(DD, Adjustment, Mangler, Out);
2125 Mangler.mangleFunctionType(DD->getType()->castAs<FunctionProtoType>(), DD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002126}
Reid Kleckner7810af02013-06-19 15:20:38 +00002127
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002128void MicrosoftMangleContextImpl::mangleCXXVFTable(
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00002129 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
2130 raw_ostream &Out) {
Reid Kleckner7810af02013-06-19 15:20:38 +00002131 // <mangled-name> ::= ?_7 <class-name> <storage-class>
2132 // <cvr-qualifiers> [<name>] @
Guy Benyei11169dd2012-12-18 14:30:41 +00002133 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner7810af02013-06-19 15:20:38 +00002134 // is always '6' for vftables.
Guy Benyei11169dd2012-12-18 14:30:41 +00002135 MicrosoftCXXNameMangler Mangler(*this, Out);
2136 Mangler.getStream() << "\01??_7";
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00002137 Mangler.mangleName(Derived);
2138 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
2139 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
2140 E = BasePath.end();
2141 I != E; ++I) {
2142 Mangler.mangleName(*I);
2143 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002144 Mangler.getStream() << '@';
2145}
Reid Kleckner7810af02013-06-19 15:20:38 +00002146
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002147void MicrosoftMangleContextImpl::mangleCXXVBTable(
Reid Kleckner7810af02013-06-19 15:20:38 +00002148 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
2149 raw_ostream &Out) {
2150 // <mangled-name> ::= ?_8 <class-name> <storage-class>
2151 // <cvr-qualifiers> [<name>] @
2152 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
2153 // is always '7' for vbtables.
2154 MicrosoftCXXNameMangler Mangler(*this, Out);
2155 Mangler.getStream() << "\01??_8";
2156 Mangler.mangleName(Derived);
2157 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
2158 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
2159 E = BasePath.end();
2160 I != E; ++I) {
2161 Mangler.mangleName(*I);
2162 }
2163 Mangler.getStream() << '@';
2164}
2165
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002166void MicrosoftMangleContextImpl::mangleCXXRTTI(QualType T, raw_ostream &) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002167 // FIXME: Give a location...
2168 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2169 "cannot mangle RTTI descriptors for type %0 yet");
2170 getDiags().Report(DiagID)
2171 << T.getBaseTypeIdentifier();
2172}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002173
2174void MicrosoftMangleContextImpl::mangleCXXRTTIName(QualType T, raw_ostream &) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002175 // FIXME: Give a location...
2176 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2177 "cannot mangle the name of type %0 into RTTI descriptors yet");
2178 getDiags().Report(DiagID)
2179 << T.getBaseTypeIdentifier();
2180}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002181
Reid Klecknercc99e262013-11-19 23:23:00 +00002182void MicrosoftMangleContextImpl::mangleTypeName(QualType T, raw_ostream &Out) {
2183 // This is just a made up unique string for the purposes of tbaa. undname
2184 // does *not* know how to demangle it.
2185 MicrosoftCXXNameMangler Mangler(*this, Out);
2186 Mangler.getStream() << '?';
2187 Mangler.mangleType(T, SourceRange());
2188}
2189
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002190void MicrosoftMangleContextImpl::mangleCXXCtor(const CXXConstructorDecl *D,
2191 CXXCtorType Type,
2192 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002193 MicrosoftCXXNameMangler mangler(*this, Out);
2194 mangler.mangle(D);
2195}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002196
2197void MicrosoftMangleContextImpl::mangleCXXDtor(const CXXDestructorDecl *D,
2198 CXXDtorType Type,
2199 raw_ostream &Out) {
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00002200 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei11169dd2012-12-18 14:30:41 +00002201 mangler.mangle(D);
2202}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002203
2204void MicrosoftMangleContextImpl::mangleReferenceTemporary(const VarDecl *VD,
David Majnemer210e6bfa12013-12-13 00:39:38 +00002205 raw_ostream &) {
2206 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2207 "cannot mangle this reference temporary yet");
2208 getDiags().Report(VD->getLocation(), DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00002209}
2210
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002211void MicrosoftMangleContextImpl::mangleStaticGuardVariable(const VarDecl *VD,
2212 raw_ostream &Out) {
David Majnemer25e1a5e2013-12-13 00:52:45 +00002213 // TODO: This is not correct, especially with respect to MSVC2013. MSVC2013
2214 // utilizes thread local variables to implement thread safe, re-entrant
2215 // initialization for statics. They no longer differentiate between an
2216 // externally visible and non-externally visible static with respect to
2217 // mangling, they all get $TSS <number>.
2218 //
2219 // N.B. This means that they can get more than 32 static variable guards in a
2220 // scope. It also means that they broke compatibility with their own ABI.
2221
Reid Klecknerd8110b62013-09-10 20:14:30 +00002222 // <guard-name> ::= ?_B <postfix> @51
2223 // ::= ?$S <guard-num> @ <postfix> @4IA
2224
2225 // The first mangling is what MSVC uses to guard static locals in inline
2226 // functions. It uses a different mangling in external functions to support
2227 // guarding more than 32 variables. MSVC rejects inline functions with more
2228 // than 32 static locals. We don't fully implement the second mangling
2229 // because those guards are not externally visible, and instead use LLVM's
2230 // default renaming when creating a new guard variable.
2231 MicrosoftCXXNameMangler Mangler(*this, Out);
2232
2233 bool Visible = VD->isExternallyVisible();
2234 // <operator-name> ::= ?_B # local static guard
2235 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@");
2236 Mangler.manglePostfix(VD->getDeclContext());
2237 Mangler.getStream() << (Visible ? "@51" : "@4IA");
2238}
2239
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002240void MicrosoftMangleContextImpl::mangleInitFiniStub(const VarDecl *D,
2241 raw_ostream &Out,
2242 char CharCode) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002243 MicrosoftCXXNameMangler Mangler(*this, Out);
2244 Mangler.getStream() << "\01??__" << CharCode;
2245 Mangler.mangleName(D);
2246 // This is the function class mangling. These stubs are global, non-variadic,
2247 // cdecl functions that return void and take no args.
2248 Mangler.getStream() << "YAXXZ";
2249}
2250
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002251void MicrosoftMangleContextImpl::mangleDynamicInitializer(const VarDecl *D,
2252 raw_ostream &Out) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002253 // <initializer-name> ::= ?__E <name> YAXXZ
2254 mangleInitFiniStub(D, Out, 'E');
2255}
2256
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002257void
2258MicrosoftMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D,
2259 raw_ostream &Out) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002260 // <destructor-name> ::= ?__F <name> YAXXZ
2261 mangleInitFiniStub(D, Out, 'F');
Reid Klecknerd8110b62013-09-10 20:14:30 +00002262}
2263
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002264MicrosoftMangleContext *
2265MicrosoftMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags) {
2266 return new MicrosoftMangleContextImpl(Context, Diags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002267}