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