blob: 24bd2a5a365f62ddc13aaec1d1931d026af59604 [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- MicrosoftMangle.cpp - Microsoft Visual C++ Name Mangling ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides C++ name mangling targeting the Microsoft Visual C++ ABI.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Mangle.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +000017#include "clang/AST/CXXInheritance.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000018#include "clang/AST/CharUnits.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000019#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/AST/DeclTemplate.h"
23#include "clang/AST/ExprCXX.h"
Reid Kleckner96f8f932014-02-05 17:27:08 +000024#include "clang/AST/VTableBuilder.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000025#include "clang/Basic/ABI.h"
26#include "clang/Basic/DiagnosticOptions.h"
Reid Kleckner369f3162013-05-14 20:30:42 +000027#include "clang/Basic/TargetInfo.h"
David Majnemer2206bf52014-03-05 08:57:59 +000028#include "llvm/ADT/StringExtras.h"
Reid Kleckner7dafb232013-05-22 17:16:39 +000029#include "llvm/ADT/StringMap.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000030
31using namespace clang;
32
33namespace {
34
David Majnemerd5a42b82013-09-13 09:03:14 +000035/// \brief Retrieve the declaration context that should be used when mangling
36/// the given declaration.
37static const DeclContext *getEffectiveDeclContext(const Decl *D) {
38 // The ABI assumes that lambda closure types that occur within
39 // default arguments live in the context of the function. However, due to
40 // the way in which Clang parses and creates function declarations, this is
41 // not the case: the lambda closure type ends up living in the context
42 // where the function itself resides, because the function declaration itself
43 // had not yet been created. Fix the context here.
44 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
45 if (RD->isLambda())
46 if (ParmVarDecl *ContextParam =
47 dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl()))
48 return ContextParam->getDeclContext();
49 }
50
51 // Perform the same check for block literals.
52 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
53 if (ParmVarDecl *ContextParam =
54 dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl()))
55 return ContextParam->getDeclContext();
56 }
57
58 const DeclContext *DC = D->getDeclContext();
59 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(DC))
60 return getEffectiveDeclContext(CD);
61
62 return DC;
63}
64
65static const DeclContext *getEffectiveParentContext(const DeclContext *DC) {
66 return getEffectiveDeclContext(cast<Decl>(DC));
67}
68
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +000069static const FunctionDecl *getStructor(const FunctionDecl *fn) {
70 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
71 return ftd->getTemplatedDecl();
72
73 return fn;
74}
75
David Majnemer2206bf52014-03-05 08:57:59 +000076static bool isLambda(const NamedDecl *ND) {
77 const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(ND);
78 if (!Record)
79 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +000080
David Majnemer2206bf52014-03-05 08:57:59 +000081 return Record->isLambda();
82}
Guy Benyei11169dd2012-12-18 14:30:41 +000083
Timur Iskhodzhanov67455222013-10-03 06:26:13 +000084/// MicrosoftMangleContextImpl - Overrides the default MangleContext for the
Guy Benyei11169dd2012-12-18 14:30:41 +000085/// Microsoft Visual C++ ABI.
Timur Iskhodzhanov67455222013-10-03 06:26:13 +000086class MicrosoftMangleContextImpl : public MicrosoftMangleContext {
David Majnemer2206bf52014-03-05 08:57:59 +000087 typedef std::pair<const DeclContext *, IdentifierInfo *> DiscriminatorKeyTy;
88 llvm::DenseMap<DiscriminatorKeyTy, unsigned> Discriminator;
89 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
90
Guy Benyei11169dd2012-12-18 14:30:41 +000091public:
Timur Iskhodzhanov67455222013-10-03 06:26:13 +000092 MicrosoftMangleContextImpl(ASTContext &Context, DiagnosticsEngine &Diags)
93 : MicrosoftMangleContext(Context, Diags) {}
Rafael Espindola002667c2013-10-16 01:40:34 +000094 virtual bool shouldMangleCXXName(const NamedDecl *D);
95 virtual void mangleCXXName(const NamedDecl *D, raw_ostream &Out);
Hans Wennborg88497d62013-11-15 17:24:45 +000096 virtual void mangleVirtualMemPtrThunk(const CXXMethodDecl *MD,
David Majnemer2a816452013-12-09 10:44:32 +000097 raw_ostream &);
Guy Benyei11169dd2012-12-18 14:30:41 +000098 virtual void mangleThunk(const CXXMethodDecl *MD,
99 const ThunkInfo &Thunk,
100 raw_ostream &);
101 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
102 const ThisAdjustment &ThisAdjustment,
103 raw_ostream &);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000104 virtual void mangleCXXVFTable(const CXXRecordDecl *Derived,
105 ArrayRef<const CXXRecordDecl *> BasePath,
106 raw_ostream &Out);
Reid Kleckner7810af02013-06-19 15:20:38 +0000107 virtual void mangleCXXVBTable(const CXXRecordDecl *Derived,
108 ArrayRef<const CXXRecordDecl *> BasePath,
109 raw_ostream &Out);
Guy Benyei11169dd2012-12-18 14:30:41 +0000110 virtual void mangleCXXRTTI(QualType T, raw_ostream &);
111 virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
Reid Klecknercc99e262013-11-19 23:23:00 +0000112 virtual void mangleTypeName(QualType T, raw_ostream &);
Guy Benyei11169dd2012-12-18 14:30:41 +0000113 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
114 raw_ostream &);
115 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
116 raw_ostream &);
Reid Klecknerd8110b62013-09-10 20:14:30 +0000117 virtual void mangleReferenceTemporary(const VarDecl *, raw_ostream &);
118 virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out);
Reid Kleckner1ece9fc2013-09-10 20:43:12 +0000119 virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out);
Reid Klecknerd8110b62013-09-10 20:14:30 +0000120 virtual void mangleDynamicAtExitDestructor(const VarDecl *D,
121 raw_ostream &Out);
David Majnemer2206bf52014-03-05 08:57:59 +0000122 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
123 // Lambda closure types are already numbered.
124 if (isLambda(ND))
125 return false;
126
127 const DeclContext *DC = getEffectiveDeclContext(ND);
128 if (!DC->isFunctionOrMethod())
129 return false;
130
131 // Use the canonical number for externally visible decls.
132 if (ND->isExternallyVisible()) {
133 disc = getASTContext().getManglingNumber(ND);
134 return true;
135 }
136
137 // Anonymous tags are already numbered.
138 if (const TagDecl *Tag = dyn_cast<TagDecl>(ND)) {
139 if (Tag->getName().empty() && !Tag->getTypedefNameForAnonDecl())
140 return false;
141 }
142
143 // Make up a reasonable number for internal decls.
144 unsigned &discriminator = Uniquifier[ND];
145 if (!discriminator)
146 discriminator = ++Discriminator[std::make_pair(DC, ND->getIdentifier())];
147 disc = discriminator;
148 return true;
149 }
Reid Kleckner1ece9fc2013-09-10 20:43:12 +0000150
151private:
152 void mangleInitFiniStub(const VarDecl *D, raw_ostream &Out, char CharCode);
Guy Benyei11169dd2012-12-18 14:30:41 +0000153};
154
David Majnemer2206bf52014-03-05 08:57:59 +0000155/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
156/// Microsoft Visual C++ ABI.
157class MicrosoftCXXNameMangler {
158 MicrosoftMangleContextImpl &Context;
159 raw_ostream &Out;
160
161 /// The "structor" is the top-level declaration being mangled, if
162 /// that's not a template specialization; otherwise it's the pattern
163 /// for that specialization.
164 const NamedDecl *Structor;
165 unsigned StructorType;
166
167 typedef llvm::StringMap<unsigned> BackRefMap;
168 BackRefMap NameBackReferences;
169 bool UseNameBackReferences;
170
171 typedef llvm::DenseMap<void*, unsigned> ArgBackRefMap;
172 ArgBackRefMap TypeBackReferences;
173
174 ASTContext &getASTContext() const { return Context.getASTContext(); }
175
176 // FIXME: If we add support for __ptr32/64 qualifiers, then we should push
177 // this check into mangleQualifiers().
178 const bool PointersAre64Bit;
179
180public:
181 enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result };
182
183 MicrosoftCXXNameMangler(MicrosoftMangleContextImpl &C, raw_ostream &Out_)
184 : Context(C), Out(Out_),
185 Structor(0), StructorType(-1),
186 UseNameBackReferences(true),
187 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
188 64) { }
189
190 MicrosoftCXXNameMangler(MicrosoftMangleContextImpl &C, raw_ostream &Out_,
191 const CXXDestructorDecl *D, CXXDtorType Type)
192 : Context(C), Out(Out_),
193 Structor(getStructor(D)), StructorType(Type),
194 UseNameBackReferences(true),
195 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
196 64) { }
197
198 raw_ostream &getStream() const { return Out; }
199
200 void mangle(const NamedDecl *D, StringRef Prefix = "\01?");
201 void mangleName(const NamedDecl *ND);
202 void mangleDeclaration(const NamedDecl *ND);
203 void mangleFunctionEncoding(const FunctionDecl *FD);
204 void mangleVariableEncoding(const VarDecl *VD);
205 void mangleMemberDataPointer(const CXXRecordDecl *RD, const ValueDecl *VD);
206 void mangleMemberFunctionPointer(const CXXRecordDecl *RD,
207 const CXXMethodDecl *MD);
208 void mangleVirtualMemPtrThunk(
209 const CXXMethodDecl *MD,
210 const MicrosoftVTableContext::MethodVFTableLocation &ML);
211 void mangleNumber(int64_t Number);
212 void mangleType(QualType T, SourceRange Range,
213 QualifierMangleMode QMM = QMM_Mangle);
214 void mangleFunctionType(const FunctionType *T, const FunctionDecl *D = 0,
215 bool ForceInstMethod = false);
216 void mangleNestedName(const NamedDecl *ND);
217
218private:
219 void disableBackReferences() { UseNameBackReferences = false; }
220 void mangleUnqualifiedName(const NamedDecl *ND) {
221 mangleUnqualifiedName(ND, ND->getDeclName());
222 }
223 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
224 void mangleSourceName(StringRef Name);
225 void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc);
226 void mangleCXXDtorType(CXXDtorType T);
227 void mangleQualifiers(Qualifiers Quals, bool IsMember);
228 void manglePointerCVQualifiers(Qualifiers Quals);
229 void manglePointerExtQualifiers(Qualifiers Quals, const Type *PointeeType);
230
231 void mangleUnscopedTemplateName(const TemplateDecl *ND);
232 void mangleTemplateInstantiationName(const TemplateDecl *TD,
233 const TemplateArgumentList &TemplateArgs);
234 void mangleObjCMethodName(const ObjCMethodDecl *MD);
235
236 void mangleArgumentType(QualType T, SourceRange Range);
237
238 // Declare manglers for every type class.
239#define ABSTRACT_TYPE(CLASS, PARENT)
240#define NON_CANONICAL_TYPE(CLASS, PARENT)
241#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \
242 SourceRange Range);
243#include "clang/AST/TypeNodes.def"
244#undef ABSTRACT_TYPE
245#undef NON_CANONICAL_TYPE
246#undef TYPE
247
248 void mangleType(const TagDecl *TD);
249 void mangleDecayedArrayType(const ArrayType *T);
250 void mangleArrayType(const ArrayType *T);
251 void mangleFunctionClass(const FunctionDecl *FD);
252 void mangleCallingConvention(const FunctionType *T);
253 void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean);
254 void mangleExpression(const Expr *E);
255 void mangleThrowSpecification(const FunctionProtoType *T);
256
257 void mangleTemplateArgs(const TemplateDecl *TD,
258 const TemplateArgumentList &TemplateArgs);
259 void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA);
260};
Guy Benyei11169dd2012-12-18 14:30:41 +0000261}
262
Rafael Espindola002667c2013-10-16 01:40:34 +0000263bool MicrosoftMangleContextImpl::shouldMangleCXXName(const NamedDecl *D) {
David Majnemerd5a42b82013-09-13 09:03:14 +0000264 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
265 LanguageLinkage L = FD->getLanguageLinkage();
266 // Overloadable functions need mangling.
267 if (FD->hasAttr<OverloadableAttr>())
268 return true;
269
David Majnemerc729b0b2013-09-16 22:44:20 +0000270 // The ABI expects that we would never mangle "typical" user-defined entry
271 // points regardless of visibility or freestanding-ness.
272 //
273 // N.B. This is distinct from asking about "main". "main" has a lot of
274 // special rules associated with it in the standard while these
275 // user-defined entry points are outside of the purview of the standard.
276 // For example, there can be only one definition for "main" in a standards
277 // compliant program; however nothing forbids the existence of wmain and
278 // WinMain in the same translation unit.
279 if (FD->isMSVCRTEntryPoint())
David Majnemerd5a42b82013-09-13 09:03:14 +0000280 return false;
281
282 // C++ functions and those whose names are not a simple identifier need
283 // mangling.
284 if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage)
285 return true;
286
287 // C functions are not mangled.
288 if (L == CLanguageLinkage)
289 return false;
290 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000291
292 // Otherwise, no mangling is done outside C++ mode.
293 if (!getASTContext().getLangOpts().CPlusPlus)
294 return false;
295
David Majnemerd5a42b82013-09-13 09:03:14 +0000296 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
297 // C variables are not mangled.
298 if (VD->isExternC())
299 return false;
300
301 // Variables at global scope with non-internal linkage are not mangled.
302 const DeclContext *DC = getEffectiveDeclContext(D);
303 // Check for extern variable declared locally.
304 if (DC->isFunctionOrMethod() && D->hasLinkage())
305 while (!DC->isNamespace() && !DC->isTranslationUnit())
306 DC = getEffectiveParentContext(DC);
307
308 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage &&
309 !isa<VarTemplateSpecializationDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +0000310 return false;
311 }
312
Guy Benyei11169dd2012-12-18 14:30:41 +0000313 return true;
314}
315
316void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
317 StringRef Prefix) {
318 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
319 // Therefore it's really important that we don't decorate the
320 // name with leading underscores or leading/trailing at signs. So, by
321 // default, we emit an asm marker at the start so we get the name right.
322 // Callers can override this with a custom prefix.
323
Guy Benyei11169dd2012-12-18 14:30:41 +0000324 // <mangled-name> ::= ? <name> <type-encoding>
325 Out << Prefix;
326 mangleName(D);
327 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
328 mangleFunctionEncoding(FD);
329 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
330 mangleVariableEncoding(VD);
331 else {
332 // TODO: Fields? Can MSVC even mangle them?
333 // Issue a diagnostic for now.
334 DiagnosticsEngine &Diags = Context.getDiags();
335 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
336 "cannot mangle this declaration yet");
337 Diags.Report(D->getLocation(), DiagID)
338 << D->getSourceRange();
339 }
340}
341
342void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
343 // <type-encoding> ::= <function-class> <function-type>
344
Reid Kleckner18da98e2013-06-24 19:21:52 +0000345 // Since MSVC operates on the type as written and not the canonical type, it
346 // actually matters which decl we have here. MSVC appears to choose the
347 // first, since it is most likely to be the declaration in a header file.
Rafael Espindola8db352d2013-10-17 15:37:26 +0000348 FD = FD->getFirstDecl();
Reid Kleckner18da98e2013-06-24 19:21:52 +0000349
Guy Benyei11169dd2012-12-18 14:30:41 +0000350 // We should never ever see a FunctionNoProtoType at this point.
351 // We don't even know how to mangle their types anyway :).
Reid Kleckner9a7f3e62013-10-08 00:58:57 +0000352 const FunctionProtoType *FT = FD->getType()->castAs<FunctionProtoType>();
Guy Benyei11169dd2012-12-18 14:30:41 +0000353
David Majnemerd5a42b82013-09-13 09:03:14 +0000354 // extern "C" functions can hold entities that must be mangled.
355 // As it stands, these functions still need to get expressed in the full
356 // external name. They have their class and type omitted, replaced with '9'.
357 if (Context.shouldMangleDeclName(FD)) {
358 // First, the function class.
359 mangleFunctionClass(FD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000360
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +0000361 mangleFunctionType(FT, FD);
David Majnemerd5a42b82013-09-13 09:03:14 +0000362 } else
363 Out << '9';
Guy Benyei11169dd2012-12-18 14:30:41 +0000364}
365
366void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
367 // <type-encoding> ::= <storage-class> <variable-type>
368 // <storage-class> ::= 0 # private static member
369 // ::= 1 # protected static member
370 // ::= 2 # public static member
371 // ::= 3 # global
372 // ::= 4 # static local
David Majnemerb4119f72013-12-13 01:06:04 +0000373
Guy Benyei11169dd2012-12-18 14:30:41 +0000374 // The first character in the encoding (after the name) is the storage class.
375 if (VD->isStaticDataMember()) {
376 // If it's a static member, it also encodes the access level.
377 switch (VD->getAccess()) {
378 default:
379 case AS_private: Out << '0'; break;
380 case AS_protected: Out << '1'; break;
381 case AS_public: Out << '2'; break;
382 }
383 }
384 else if (!VD->isStaticLocal())
385 Out << '3';
386 else
387 Out << '4';
388 // Now mangle the type.
389 // <variable-type> ::= <type> <cvr-qualifiers>
390 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
391 // Pointers and references are odd. The type of 'int * const foo;' gets
392 // mangled as 'QAHA' instead of 'PAHB', for example.
393 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
David Majnemerb9a5f2d2014-01-21 20:33:36 +0000394 QualType Ty = VD->getType();
David Majnemer6dda7bb2013-08-15 08:13:23 +0000395 if (Ty->isPointerType() || Ty->isReferenceType() ||
396 Ty->isMemberPointerType()) {
David Majnemera2724ae2013-08-09 05:56:24 +0000397 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer8eec58f2014-02-18 14:20:10 +0000398 manglePointerExtQualifiers(
399 Ty.getDesugaredType(getASTContext()).getLocalQualifiers(), 0);
David Majnemer6dda7bb2013-08-15 08:13:23 +0000400 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) {
401 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true);
402 // Member pointers are suffixed with a back reference to the member
403 // pointer's class name.
404 mangleName(MPT->getClass()->getAsCXXRecordDecl());
405 } else
406 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
David Majnemera2724ae2013-08-09 05:56:24 +0000407 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000408 // Global arrays are funny, too.
David Majnemer5a1b2042013-09-11 04:44:30 +0000409 mangleDecayedArrayType(AT);
Peter Collingbourne2816c022013-04-25 04:25:40 +0000410 if (AT->getElementType()->isArrayType())
411 Out << 'A';
412 else
413 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei11169dd2012-12-18 14:30:41 +0000414 } else {
Peter Collingbourne2816c022013-04-25 04:25:40 +0000415 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer6dda7bb2013-08-15 08:13:23 +0000416 mangleQualifiers(Ty.getLocalQualifiers(), false);
Guy Benyei11169dd2012-12-18 14:30:41 +0000417 }
418}
419
Reid Kleckner96f8f932014-02-05 17:27:08 +0000420void MicrosoftCXXNameMangler::mangleMemberDataPointer(const CXXRecordDecl *RD,
David Majnemer1e378e42014-02-06 12:46:52 +0000421 const ValueDecl *VD) {
Reid Kleckner96f8f932014-02-05 17:27:08 +0000422 // <member-data-pointer> ::= <integer-literal>
423 // ::= $F <number> <number>
424 // ::= $G <number> <number> <number>
425
David Majnemer763584d2014-02-06 10:59:19 +0000426 int64_t FieldOffset;
427 int64_t VBTableOffset;
Reid Kleckner96f8f932014-02-05 17:27:08 +0000428 MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel();
David Majnemer1e378e42014-02-06 12:46:52 +0000429 if (VD) {
430 FieldOffset = getASTContext().getFieldOffset(VD);
David Majnemer763584d2014-02-06 10:59:19 +0000431 assert(FieldOffset % getASTContext().getCharWidth() == 0 &&
Reid Kleckner96f8f932014-02-05 17:27:08 +0000432 "cannot take address of bitfield");
David Majnemer763584d2014-02-06 10:59:19 +0000433 FieldOffset /= getASTContext().getCharWidth();
434
435 VBTableOffset = 0;
436 } else {
437 FieldOffset = RD->nullFieldOffsetIsZero() ? 0 : -1;
438
439 VBTableOffset = -1;
Reid Kleckner96f8f932014-02-05 17:27:08 +0000440 }
441
David Majnemer763584d2014-02-06 10:59:19 +0000442 char Code = '\0';
Reid Kleckner96f8f932014-02-05 17:27:08 +0000443 switch (IM) {
David Majnemer763584d2014-02-06 10:59:19 +0000444 case MSInheritanceAttr::Keyword_single_inheritance: Code = '0'; break;
445 case MSInheritanceAttr::Keyword_multiple_inheritance: Code = '0'; break;
446 case MSInheritanceAttr::Keyword_virtual_inheritance: Code = 'F'; break;
447 case MSInheritanceAttr::Keyword_unspecified_inheritance: Code = 'G'; break;
Reid Kleckner96f8f932014-02-05 17:27:08 +0000448 }
449
David Majnemer763584d2014-02-06 10:59:19 +0000450 Out << '$' << Code;
Reid Kleckner96f8f932014-02-05 17:27:08 +0000451
David Majnemer763584d2014-02-06 10:59:19 +0000452 mangleNumber(FieldOffset);
453
454 if (MSInheritanceAttr::hasVBPtrOffsetField(IM))
Reid Kleckner96f8f932014-02-05 17:27:08 +0000455 mangleNumber(0);
David Majnemer763584d2014-02-06 10:59:19 +0000456 if (MSInheritanceAttr::hasVBTableOffsetField(IM))
457 mangleNumber(VBTableOffset);
Reid Kleckner96f8f932014-02-05 17:27:08 +0000458}
459
460void
461MicrosoftCXXNameMangler::mangleMemberFunctionPointer(const CXXRecordDecl *RD,
462 const CXXMethodDecl *MD) {
463 // <member-function-pointer> ::= $1? <name>
464 // ::= $H? <name> <number>
465 // ::= $I? <name> <number> <number>
466 // ::= $J? <name> <number> <number> <number>
467 // ::= $0A@
468
469 MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel();
470
471 // The null member function pointer is $0A@ in function templates and crashes
472 // MSVC when used in class templates, so we don't know what they really look
473 // like.
474 if (!MD) {
475 Out << "$0A@";
476 return;
477 }
478
479 char Code = '\0';
480 switch (IM) {
481 case MSInheritanceAttr::Keyword_single_inheritance: Code = '1'; break;
482 case MSInheritanceAttr::Keyword_multiple_inheritance: Code = 'H'; break;
483 case MSInheritanceAttr::Keyword_virtual_inheritance: Code = 'I'; break;
484 case MSInheritanceAttr::Keyword_unspecified_inheritance: Code = 'J'; break;
485 }
486
487 Out << '$' << Code << '?';
488
489 // If non-virtual, mangle the name. If virtual, mangle as a virtual memptr
490 // thunk.
491 uint64_t NVOffset = 0;
492 uint64_t VBTableOffset = 0;
David Majnemer763584d2014-02-06 10:59:19 +0000493 if (MD->isVirtual()) {
Reid Kleckner96f8f932014-02-05 17:27:08 +0000494 MicrosoftVTableContext *VTContext =
495 cast<MicrosoftVTableContext>(getASTContext().getVTableContext());
496 const MicrosoftVTableContext::MethodVFTableLocation &ML =
497 VTContext->getMethodVFTableLocation(GlobalDecl(MD));
498 mangleVirtualMemPtrThunk(MD, ML);
499 NVOffset = ML.VFPtrOffset.getQuantity();
500 VBTableOffset = ML.VBTableIndex * 4;
501 if (ML.VBase) {
502 DiagnosticsEngine &Diags = Context.getDiags();
503 unsigned DiagID = Diags.getCustomDiagID(
504 DiagnosticsEngine::Error,
505 "cannot mangle pointers to member functions from virtual bases");
506 Diags.Report(MD->getLocation(), DiagID);
507 }
508 } else {
509 mangleName(MD);
510 mangleFunctionEncoding(MD);
511 }
512
513 if (MSInheritanceAttr::hasNVOffsetField(/*IsMemberFunction=*/true, IM))
514 mangleNumber(NVOffset);
515 if (MSInheritanceAttr::hasVBPtrOffsetField(IM))
516 mangleNumber(0);
517 if (MSInheritanceAttr::hasVBTableOffsetField(IM))
518 mangleNumber(VBTableOffset);
519}
520
521void MicrosoftCXXNameMangler::mangleVirtualMemPtrThunk(
522 const CXXMethodDecl *MD,
523 const MicrosoftVTableContext::MethodVFTableLocation &ML) {
524 // Get the vftable offset.
525 CharUnits PointerWidth = getASTContext().toCharUnitsFromBits(
526 getASTContext().getTargetInfo().getPointerWidth(0));
527 uint64_t OffsetInVFTable = ML.Index * PointerWidth.getQuantity();
528
529 Out << "?_9";
530 mangleName(MD->getParent());
531 Out << "$B";
532 mangleNumber(OffsetInVFTable);
533 Out << 'A';
534 Out << (PointersAre64Bit ? 'A' : 'E');
535}
536
Guy Benyei11169dd2012-12-18 14:30:41 +0000537void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
538 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
Guy Benyei11169dd2012-12-18 14:30:41 +0000539
540 // Always start with the unqualified name.
David Majnemerb4119f72013-12-13 01:06:04 +0000541 mangleUnqualifiedName(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +0000542
David Majnemer2206bf52014-03-05 08:57:59 +0000543 mangleNestedName(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +0000544
545 // Terminate the whole name with an '@'.
546 Out << '@';
547}
548
David Majnemer2a816452013-12-09 10:44:32 +0000549void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
550 // <non-negative integer> ::= A@ # when Number == 0
551 // ::= <decimal digit> # when 1 <= Number <= 10
552 // ::= <hex digit>+ @ # when Number >= 10
553 //
554 // <number> ::= [?] <non-negative integer>
Guy Benyei11169dd2012-12-18 14:30:41 +0000555
David Majnemer2a816452013-12-09 10:44:32 +0000556 uint64_t Value = static_cast<uint64_t>(Number);
557 if (Number < 0) {
558 Value = -Value;
Guy Benyei11169dd2012-12-18 14:30:41 +0000559 Out << '?';
Guy Benyei11169dd2012-12-18 14:30:41 +0000560 }
David Majnemer2a816452013-12-09 10:44:32 +0000561
562 if (Value == 0)
563 Out << "A@";
564 else if (Value >= 1 && Value <= 10)
565 Out << (Value - 1);
566 else {
567 // Numbers that are not encoded as decimal digits are represented as nibbles
568 // in the range of ASCII characters 'A' to 'P'.
569 // The number 0x123450 would be encoded as 'BCDEFA'
570 char EncodedNumberBuffer[sizeof(uint64_t) * 2];
571 llvm::MutableArrayRef<char> BufferRef(EncodedNumberBuffer);
572 llvm::MutableArrayRef<char>::reverse_iterator I = BufferRef.rbegin();
573 for (; Value != 0; Value >>= 4)
574 *I++ = 'A' + (Value & 0xf);
575 Out.write(I.base(), I - BufferRef.rbegin());
Guy Benyei11169dd2012-12-18 14:30:41 +0000576 Out << '@';
577 }
578}
579
580static const TemplateDecl *
Reid Kleckner52518862013-03-20 01:40:23 +0000581isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000582 // Check if we have a function template.
583 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
584 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Kleckner52518862013-03-20 01:40:23 +0000585 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei11169dd2012-12-18 14:30:41 +0000586 return TD;
587 }
588 }
589
590 // Check if we have a class template.
591 if (const ClassTemplateSpecializationDecl *Spec =
Reid Kleckner52518862013-03-20 01:40:23 +0000592 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
593 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei11169dd2012-12-18 14:30:41 +0000594 return Spec->getSpecializedTemplate();
595 }
596
David Majnemer8f774532014-03-04 05:38:05 +0000597 // Check if we have a variable template.
598 if (const VarTemplateSpecializationDecl *Spec =
599 dyn_cast<VarTemplateSpecializationDecl>(ND)) {
600 TemplateArgs = &Spec->getTemplateArgs();
601 return Spec->getSpecializedTemplate();
602 }
603
Guy Benyei11169dd2012-12-18 14:30:41 +0000604 return 0;
605}
606
607void
608MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
609 DeclarationName Name) {
610 // <unqualified-name> ::= <operator-name>
611 // ::= <ctor-dtor-name>
612 // ::= <source-name>
613 // ::= <template-name>
Reid Kleckner52518862013-03-20 01:40:23 +0000614
Guy Benyei11169dd2012-12-18 14:30:41 +0000615 // Check if we have a template.
Reid Kleckner52518862013-03-20 01:40:23 +0000616 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000617 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Klecknerc16c4472013-07-13 00:43:39 +0000618 // Function templates aren't considered for name back referencing. This
619 // makes sense since function templates aren't likely to occur multiple
620 // times in a symbol.
621 // FIXME: Test alias template mangling with MSVC 2013.
622 if (!isa<ClassTemplateDecl>(TD)) {
623 mangleTemplateInstantiationName(TD, *TemplateArgs);
624 return;
625 }
626
Guy Benyei11169dd2012-12-18 14:30:41 +0000627 // Here comes the tricky thing: if we need to mangle something like
628 // void foo(A::X<Y>, B::X<Y>),
629 // the X<Y> part is aliased. However, if you need to mangle
630 // void foo(A::X<A::Y>, A::X<B::Y>),
631 // the A::X<> part is not aliased.
632 // That said, from the mangler's perspective we have a structure like this:
633 // namespace[s] -> type[ -> template-parameters]
634 // but from the Clang perspective we have
635 // type [ -> template-parameters]
636 // \-> namespace[s]
637 // What we do is we create a new mangler, mangle the same type (without
638 // a namespace suffix) using the extra mangler with back references
639 // disabled (to avoid infinite recursion) and then use the mangled type
640 // name as a key to check the mangling of different types for aliasing.
641
642 std::string BackReferenceKey;
643 BackRefMap::iterator Found;
644 if (UseNameBackReferences) {
645 llvm::raw_string_ostream Stream(BackReferenceKey);
646 MicrosoftCXXNameMangler Extra(Context, Stream);
647 Extra.disableBackReferences();
648 Extra.mangleUnqualifiedName(ND, Name);
649 Stream.flush();
650
651 Found = NameBackReferences.find(BackReferenceKey);
652 }
653 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Kleckner52518862013-03-20 01:40:23 +0000654 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei11169dd2012-12-18 14:30:41 +0000655 if (UseNameBackReferences && NameBackReferences.size() < 10) {
656 size_t Size = NameBackReferences.size();
657 NameBackReferences[BackReferenceKey] = Size;
658 }
659 } else {
660 Out << Found->second;
661 }
662 return;
663 }
664
665 switch (Name.getNameKind()) {
666 case DeclarationName::Identifier: {
667 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
David Majnemer956bc112013-11-25 17:50:19 +0000668 mangleSourceName(II->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +0000669 break;
670 }
David Majnemerb4119f72013-12-13 01:06:04 +0000671
Guy Benyei11169dd2012-12-18 14:30:41 +0000672 // Otherwise, an anonymous entity. We must have a declaration.
673 assert(ND && "mangling empty name without declaration");
David Majnemerb4119f72013-12-13 01:06:04 +0000674
Guy Benyei11169dd2012-12-18 14:30:41 +0000675 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
676 if (NS->isAnonymousNamespace()) {
677 Out << "?A@";
678 break;
679 }
680 }
David Majnemerb4119f72013-12-13 01:06:04 +0000681
David Majnemer2206bf52014-03-05 08:57:59 +0000682 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
683 // We must have an anonymous union or struct declaration.
684 const CXXRecordDecl *RD = VD->getType()->getAsCXXRecordDecl();
685 assert(RD && "expected variable decl to have a record type");
686 // Anonymous types with no tag or typedef get the name of their
687 // declarator mangled in. If they have no declarator, number them with
688 // a $S prefix.
689 llvm::SmallString<64> Name("$S");
690 // Get a unique id for the anonymous struct.
691 Name += llvm::utostr(Context.getAnonymousStructId(RD) + 1);
692 mangleSourceName(Name.str());
693 break;
694 }
695
Guy Benyei11169dd2012-12-18 14:30:41 +0000696 // We must have an anonymous struct.
697 const TagDecl *TD = cast<TagDecl>(ND);
698 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
699 assert(TD->getDeclContext() == D->getDeclContext() &&
700 "Typedef should not be in another decl context!");
701 assert(D->getDeclName().getAsIdentifierInfo() &&
702 "Typedef was not named!");
David Majnemer956bc112013-11-25 17:50:19 +0000703 mangleSourceName(D->getDeclName().getAsIdentifierInfo()->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +0000704 break;
705 }
706
David Majnemer2206bf52014-03-05 08:57:59 +0000707 llvm::SmallString<64> Name("<unnamed-type-");
David Majnemer956bc112013-11-25 17:50:19 +0000708 if (TD->hasDeclaratorForAnonDecl()) {
David Majnemer50ce8352013-09-17 23:57:10 +0000709 // Anonymous types with no tag or typedef get the name of their
David Majnemer2206bf52014-03-05 08:57:59 +0000710 // declarator mangled in if they have one.
David Majnemer956bc112013-11-25 17:50:19 +0000711 Name += TD->getDeclaratorForAnonDecl()->getName();
David Majnemer956bc112013-11-25 17:50:19 +0000712 } else {
David Majnemer2206bf52014-03-05 08:57:59 +0000713 // Otherwise, number the types using a $S prefix.
714 Name += "$S";
715 Name += llvm::utostr(Context.getAnonymousStructId(TD) + 1);
David Majnemer956bc112013-11-25 17:50:19 +0000716 }
David Majnemer2206bf52014-03-05 08:57:59 +0000717 Name += ">";
718 mangleSourceName(Name.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000719 break;
720 }
David Majnemerb4119f72013-12-13 01:06:04 +0000721
Guy Benyei11169dd2012-12-18 14:30:41 +0000722 case DeclarationName::ObjCZeroArgSelector:
723 case DeclarationName::ObjCOneArgSelector:
724 case DeclarationName::ObjCMultiArgSelector:
725 llvm_unreachable("Can't mangle Objective-C selector names here!");
David Majnemerb4119f72013-12-13 01:06:04 +0000726
Guy Benyei11169dd2012-12-18 14:30:41 +0000727 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000728 if (ND == Structor) {
729 assert(StructorType == Ctor_Complete &&
730 "Should never be asked to mangle a ctor other than complete");
731 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000732 Out << "?0";
733 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000734
Guy Benyei11169dd2012-12-18 14:30:41 +0000735 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000736 if (ND == Structor)
737 // If the named decl is the C++ destructor we're mangling,
738 // use the type we were given.
739 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
740 else
Reid Klecknere7de47e2013-07-22 13:51:44 +0000741 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000742 // class with a destructor is declared within a destructor.
Reid Klecknere7de47e2013-07-22 13:51:44 +0000743 mangleCXXDtorType(Dtor_Base);
Guy Benyei11169dd2012-12-18 14:30:41 +0000744 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000745
Guy Benyei11169dd2012-12-18 14:30:41 +0000746 case DeclarationName::CXXConversionFunctionName:
747 // <operator-name> ::= ?B # (cast)
748 // The target type is encoded as the return type.
749 Out << "?B";
750 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000751
Guy Benyei11169dd2012-12-18 14:30:41 +0000752 case DeclarationName::CXXOperatorName:
753 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
754 break;
David Majnemerb4119f72013-12-13 01:06:04 +0000755
Guy Benyei11169dd2012-12-18 14:30:41 +0000756 case DeclarationName::CXXLiteralOperatorName: {
757 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
758 DiagnosticsEngine Diags = Context.getDiags();
759 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
760 "cannot mangle this literal operator yet");
761 Diags.Report(ND->getLocation(), DiagID);
762 break;
763 }
David Majnemerb4119f72013-12-13 01:06:04 +0000764
Guy Benyei11169dd2012-12-18 14:30:41 +0000765 case DeclarationName::CXXUsingDirective:
766 llvm_unreachable("Can't mangle a using directive name!");
767 }
768}
769
David Majnemer2206bf52014-03-05 08:57:59 +0000770void MicrosoftCXXNameMangler::mangleNestedName(const NamedDecl *ND) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000771 // <postfix> ::= <unqualified-name> [<postfix>]
772 // ::= <substitution> [<postfix>]
David Majnemer2206bf52014-03-05 08:57:59 +0000773 const DeclContext *DC = ND->getDeclContext();
Guy Benyei11169dd2012-12-18 14:30:41 +0000774
David Majnemer2206bf52014-03-05 08:57:59 +0000775 while (!DC->isTranslationUnit()) {
776 if (isa<TagDecl>(ND) || isa<VarDecl>(ND)) {
777 unsigned Disc;
778 if (Context.getNextDiscriminator(ND, Disc)) {
779 Out << '?';
780 mangleNumber(Disc);
781 Out << '?';
782 }
783 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000784
David Majnemer2206bf52014-03-05 08:57:59 +0000785 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
786 DiagnosticsEngine Diags = Context.getDiags();
787 unsigned DiagID =
788 Diags.getCustomDiagID(DiagnosticsEngine::Error,
789 "cannot mangle a local inside this block yet");
790 Diags.Report(BD->getLocation(), DiagID);
791
792 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
793 // for how this should be done.
794 Out << "__block_invoke" << Context.getBlockId(BD, false);
795 Out << '@';
796 continue;
797 } else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
798 mangleObjCMethodName(Method);
799 } else if (isa<NamedDecl>(DC)) {
800 ND = cast<NamedDecl>(DC);
801 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
802 mangle(FD, "?");
803 break;
804 } else
805 mangleUnqualifiedName(ND);
806 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000807 DC = DC->getParent();
Guy Benyei11169dd2012-12-18 14:30:41 +0000808 }
809}
810
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000811void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknere7de47e2013-07-22 13:51:44 +0000812 // Microsoft uses the names on the case labels for these dtor variants. Clang
813 // uses the Itanium terminology internally. Everything in this ABI delegates
814 // towards the base dtor.
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000815 switch (T) {
Reid Klecknere7de47e2013-07-22 13:51:44 +0000816 // <operator-name> ::= ?1 # destructor
817 case Dtor_Base: Out << "?1"; return;
818 // <operator-name> ::= ?_D # vbase destructor
819 case Dtor_Complete: Out << "?_D"; return;
820 // <operator-name> ::= ?_G # scalar deleting destructor
821 case Dtor_Deleting: Out << "?_G"; return;
822 // <operator-name> ::= ?_E # vector deleting destructor
823 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
824 // it.
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +0000825 }
826 llvm_unreachable("Unsupported dtor type?");
827}
828
Guy Benyei11169dd2012-12-18 14:30:41 +0000829void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
830 SourceLocation Loc) {
831 switch (OO) {
832 // ?0 # constructor
833 // ?1 # destructor
834 // <operator-name> ::= ?2 # new
835 case OO_New: Out << "?2"; break;
836 // <operator-name> ::= ?3 # delete
837 case OO_Delete: Out << "?3"; break;
838 // <operator-name> ::= ?4 # =
839 case OO_Equal: Out << "?4"; break;
840 // <operator-name> ::= ?5 # >>
841 case OO_GreaterGreater: Out << "?5"; break;
842 // <operator-name> ::= ?6 # <<
843 case OO_LessLess: Out << "?6"; break;
844 // <operator-name> ::= ?7 # !
845 case OO_Exclaim: Out << "?7"; break;
846 // <operator-name> ::= ?8 # ==
847 case OO_EqualEqual: Out << "?8"; break;
848 // <operator-name> ::= ?9 # !=
849 case OO_ExclaimEqual: Out << "?9"; break;
850 // <operator-name> ::= ?A # []
851 case OO_Subscript: Out << "?A"; break;
852 // ?B # conversion
853 // <operator-name> ::= ?C # ->
854 case OO_Arrow: Out << "?C"; break;
855 // <operator-name> ::= ?D # *
856 case OO_Star: Out << "?D"; break;
857 // <operator-name> ::= ?E # ++
858 case OO_PlusPlus: Out << "?E"; break;
859 // <operator-name> ::= ?F # --
860 case OO_MinusMinus: Out << "?F"; break;
861 // <operator-name> ::= ?G # -
862 case OO_Minus: Out << "?G"; break;
863 // <operator-name> ::= ?H # +
864 case OO_Plus: Out << "?H"; break;
865 // <operator-name> ::= ?I # &
866 case OO_Amp: Out << "?I"; break;
867 // <operator-name> ::= ?J # ->*
868 case OO_ArrowStar: Out << "?J"; break;
869 // <operator-name> ::= ?K # /
870 case OO_Slash: Out << "?K"; break;
871 // <operator-name> ::= ?L # %
872 case OO_Percent: Out << "?L"; break;
873 // <operator-name> ::= ?M # <
874 case OO_Less: Out << "?M"; break;
875 // <operator-name> ::= ?N # <=
876 case OO_LessEqual: Out << "?N"; break;
877 // <operator-name> ::= ?O # >
878 case OO_Greater: Out << "?O"; break;
879 // <operator-name> ::= ?P # >=
880 case OO_GreaterEqual: Out << "?P"; break;
881 // <operator-name> ::= ?Q # ,
882 case OO_Comma: Out << "?Q"; break;
883 // <operator-name> ::= ?R # ()
884 case OO_Call: Out << "?R"; break;
885 // <operator-name> ::= ?S # ~
886 case OO_Tilde: Out << "?S"; break;
887 // <operator-name> ::= ?T # ^
888 case OO_Caret: Out << "?T"; break;
889 // <operator-name> ::= ?U # |
890 case OO_Pipe: Out << "?U"; break;
891 // <operator-name> ::= ?V # &&
892 case OO_AmpAmp: Out << "?V"; break;
893 // <operator-name> ::= ?W # ||
894 case OO_PipePipe: Out << "?W"; break;
895 // <operator-name> ::= ?X # *=
896 case OO_StarEqual: Out << "?X"; break;
897 // <operator-name> ::= ?Y # +=
898 case OO_PlusEqual: Out << "?Y"; break;
899 // <operator-name> ::= ?Z # -=
900 case OO_MinusEqual: Out << "?Z"; break;
901 // <operator-name> ::= ?_0 # /=
902 case OO_SlashEqual: Out << "?_0"; break;
903 // <operator-name> ::= ?_1 # %=
904 case OO_PercentEqual: Out << "?_1"; break;
905 // <operator-name> ::= ?_2 # >>=
906 case OO_GreaterGreaterEqual: Out << "?_2"; break;
907 // <operator-name> ::= ?_3 # <<=
908 case OO_LessLessEqual: Out << "?_3"; break;
909 // <operator-name> ::= ?_4 # &=
910 case OO_AmpEqual: Out << "?_4"; break;
911 // <operator-name> ::= ?_5 # |=
912 case OO_PipeEqual: Out << "?_5"; break;
913 // <operator-name> ::= ?_6 # ^=
914 case OO_CaretEqual: Out << "?_6"; break;
915 // ?_7 # vftable
916 // ?_8 # vbtable
917 // ?_9 # vcall
918 // ?_A # typeof
919 // ?_B # local static guard
920 // ?_C # string
921 // ?_D # vbase destructor
922 // ?_E # vector deleting destructor
923 // ?_F # default constructor closure
924 // ?_G # scalar deleting destructor
925 // ?_H # vector constructor iterator
926 // ?_I # vector destructor iterator
927 // ?_J # vector vbase constructor iterator
928 // ?_K # virtual displacement map
929 // ?_L # eh vector constructor iterator
930 // ?_M # eh vector destructor iterator
931 // ?_N # eh vector vbase constructor iterator
932 // ?_O # copy constructor closure
933 // ?_P<name> # udt returning <name>
934 // ?_Q # <unknown>
935 // ?_R0 # RTTI Type Descriptor
936 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
937 // ?_R2 # RTTI Base Class Array
938 // ?_R3 # RTTI Class Hierarchy Descriptor
939 // ?_R4 # RTTI Complete Object Locator
940 // ?_S # local vftable
941 // ?_T # local vftable constructor closure
942 // <operator-name> ::= ?_U # new[]
943 case OO_Array_New: Out << "?_U"; break;
944 // <operator-name> ::= ?_V # delete[]
945 case OO_Array_Delete: Out << "?_V"; break;
David Majnemerb4119f72013-12-13 01:06:04 +0000946
Guy Benyei11169dd2012-12-18 14:30:41 +0000947 case OO_Conditional: {
948 DiagnosticsEngine &Diags = Context.getDiags();
949 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
950 "cannot mangle this conditional operator yet");
951 Diags.Report(Loc, DiagID);
952 break;
953 }
David Majnemerb4119f72013-12-13 01:06:04 +0000954
Guy Benyei11169dd2012-12-18 14:30:41 +0000955 case OO_None:
956 case NUM_OVERLOADED_OPERATORS:
957 llvm_unreachable("Not an overloaded operator");
958 }
959}
960
David Majnemer956bc112013-11-25 17:50:19 +0000961void MicrosoftCXXNameMangler::mangleSourceName(StringRef Name) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000962 // <source name> ::= <identifier> @
Guy Benyei11169dd2012-12-18 14:30:41 +0000963 BackRefMap::iterator Found;
964 if (UseNameBackReferences)
David Majnemer956bc112013-11-25 17:50:19 +0000965 Found = NameBackReferences.find(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000966 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
David Majnemer956bc112013-11-25 17:50:19 +0000967 Out << Name << '@';
Guy Benyei11169dd2012-12-18 14:30:41 +0000968 if (UseNameBackReferences && NameBackReferences.size() < 10) {
969 size_t Size = NameBackReferences.size();
David Majnemer956bc112013-11-25 17:50:19 +0000970 NameBackReferences[Name] = Size;
Guy Benyei11169dd2012-12-18 14:30:41 +0000971 }
972 } else {
973 Out << Found->second;
974 }
975}
976
977void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
978 Context.mangleObjCMethodName(MD, Out);
979}
980
Guy Benyei11169dd2012-12-18 14:30:41 +0000981void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
982 const TemplateDecl *TD,
Reid Kleckner52518862013-03-20 01:40:23 +0000983 const TemplateArgumentList &TemplateArgs) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000984 // <template-name> ::= <unscoped-template-name> <template-args>
985 // ::= <substitution>
986 // Always start with the unqualified name.
987
988 // Templates have their own context for back references.
989 ArgBackRefMap OuterArgsContext;
990 BackRefMap OuterTemplateContext;
991 NameBackReferences.swap(OuterTemplateContext);
992 TypeBackReferences.swap(OuterArgsContext);
993
994 mangleUnscopedTemplateName(TD);
Reid Kleckner52518862013-03-20 01:40:23 +0000995 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei11169dd2012-12-18 14:30:41 +0000996
997 // Restore the previous back reference contexts.
998 NameBackReferences.swap(OuterTemplateContext);
999 TypeBackReferences.swap(OuterArgsContext);
1000}
1001
1002void
1003MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
1004 // <unscoped-template-name> ::= ?$ <unqualified-name>
1005 Out << "?$";
1006 mangleUnqualifiedName(TD);
1007}
1008
1009void
1010MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
1011 bool IsBoolean) {
1012 // <integer-literal> ::= $0 <number>
1013 Out << "$0";
1014 // Make sure booleans are encoded as 0/1.
1015 if (IsBoolean && Value.getBoolValue())
1016 mangleNumber(1);
1017 else
David Majnemer2a816452013-12-09 10:44:32 +00001018 mangleNumber(Value.getSExtValue());
Guy Benyei11169dd2012-12-18 14:30:41 +00001019}
1020
1021void
1022MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
1023 // See if this is a constant expression.
1024 llvm::APSInt Value;
1025 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
1026 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
1027 return;
1028 }
1029
David Majnemer8eaab6f2013-08-13 06:32:20 +00001030 const CXXUuidofExpr *UE = 0;
1031 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
1032 if (UO->getOpcode() == UO_AddrOf)
1033 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
1034 } else
1035 UE = dyn_cast<CXXUuidofExpr>(E);
1036
1037 if (UE) {
1038 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
1039 // const __s_GUID _GUID_{lower case UUID with underscores}
1040 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
1041 std::string Name = "_GUID_" + Uuid.lower();
1042 std::replace(Name.begin(), Name.end(), '-', '_');
1043
David Majnemere9cab2f2013-08-13 09:17:25 +00001044 // If we had to peek through an address-of operator, treat this like we are
David Majnemer8eaab6f2013-08-13 06:32:20 +00001045 // dealing with a pointer type. Otherwise, treat it like a const reference.
1046 //
1047 // N.B. This matches up with the handling of TemplateArgument::Declaration
1048 // in mangleTemplateArg
1049 if (UE == E)
1050 Out << "$E?";
1051 else
1052 Out << "$1?";
1053 Out << Name << "@@3U__s_GUID@@B";
1054 return;
1055 }
1056
Guy Benyei11169dd2012-12-18 14:30:41 +00001057 // As bad as this diagnostic is, it's better than crashing.
1058 DiagnosticsEngine &Diags = Context.getDiags();
1059 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1060 "cannot yet mangle expression type %0");
1061 Diags.Report(E->getExprLoc(), DiagID)
1062 << E->getStmtClassName() << E->getSourceRange();
1063}
1064
1065void
Reid Kleckner52518862013-03-20 01:40:23 +00001066MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
1067 const TemplateArgumentList &TemplateArgs) {
Reid Kleckner96f8f932014-02-05 17:27:08 +00001068 // <template-args> ::= <template-arg>+ @
Guy Benyei11169dd2012-12-18 14:30:41 +00001069 unsigned NumTemplateArgs = TemplateArgs.size();
1070 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Kleckner52518862013-03-20 01:40:23 +00001071 const TemplateArgument &TA = TemplateArgs[i];
David Majnemer08177c52013-08-27 08:21:25 +00001072 mangleTemplateArg(TD, TA);
Guy Benyei11169dd2012-12-18 14:30:41 +00001073 }
1074 Out << '@';
1075}
1076
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001077void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
David Majnemer08177c52013-08-27 08:21:25 +00001078 const TemplateArgument &TA) {
Reid Kleckner96f8f932014-02-05 17:27:08 +00001079 // <template-arg> ::= <type>
1080 // ::= <integer-literal>
1081 // ::= <member-data-pointer>
1082 // ::= <member-function-pointer>
1083 // ::= $E? <name> <type-encoding>
1084 // ::= $1? <name> <type-encoding>
1085 // ::= $0A@
1086 // ::= <template-args>
1087
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001088 switch (TA.getKind()) {
1089 case TemplateArgument::Null:
1090 llvm_unreachable("Can't mangle null template arguments!");
David Majnemer08177c52013-08-27 08:21:25 +00001091 case TemplateArgument::TemplateExpansion:
1092 llvm_unreachable("Can't mangle template expansion arguments!");
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001093 case TemplateArgument::Type: {
1094 QualType T = TA.getAsType();
1095 mangleType(T, SourceRange(), QMM_Escape);
1096 break;
1097 }
David Majnemere8fdc062013-08-13 01:25:35 +00001098 case TemplateArgument::Declaration: {
1099 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl());
David Majnemer1e378e42014-02-06 12:46:52 +00001100 if (isa<FieldDecl>(ND) || isa<IndirectFieldDecl>(ND)) {
Reid Klecknere253b092014-02-08 01:15:37 +00001101 mangleMemberDataPointer(
1102 cast<CXXRecordDecl>(ND->getDeclContext())->getMostRecentDecl(),
1103 cast<ValueDecl>(ND));
Reid Kleckner09b47d12014-02-05 18:59:38 +00001104 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Nick Lewycky1f529662014-02-05 23:53:29 +00001105 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
Reid Kleckner09b47d12014-02-05 18:59:38 +00001106 if (MD && MD->isInstance())
Reid Klecknere253b092014-02-08 01:15:37 +00001107 mangleMemberFunctionPointer(MD->getParent()->getMostRecentDecl(), MD);
Reid Kleckner09b47d12014-02-05 18:59:38 +00001108 else
Nick Lewycky1f529662014-02-05 23:53:29 +00001109 mangle(FD, "$1?");
Reid Kleckner09b47d12014-02-05 18:59:38 +00001110 } else {
Reid Kleckner96f8f932014-02-05 17:27:08 +00001111 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?");
Reid Kleckner09b47d12014-02-05 18:59:38 +00001112 }
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001113 break;
David Majnemere8fdc062013-08-13 01:25:35 +00001114 }
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001115 case TemplateArgument::Integral:
1116 mangleIntegerLiteral(TA.getAsIntegral(),
1117 TA.getIntegralType()->isBooleanType());
1118 break;
Reid Kleckner96f8f932014-02-05 17:27:08 +00001119 case TemplateArgument::NullPtr: {
1120 QualType T = TA.getNullPtrType();
1121 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
David Majnemer763584d2014-02-06 10:59:19 +00001122 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
Reid Kleckner96f8f932014-02-05 17:27:08 +00001123 if (MPT->isMemberFunctionPointerType())
1124 mangleMemberFunctionPointer(RD, 0);
1125 else
1126 mangleMemberDataPointer(RD, 0);
1127 } else {
1128 Out << "$0A@";
1129 }
David Majnemerae465ef2013-08-05 21:33:59 +00001130 break;
Reid Kleckner96f8f932014-02-05 17:27:08 +00001131 }
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001132 case TemplateArgument::Expression:
1133 mangleExpression(TA.getAsExpr());
1134 break;
1135 case TemplateArgument::Pack:
1136 // Unlike Itanium, there is no character code to indicate an argument pack.
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001137 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
1138 I != E; ++I)
David Majnemer08177c52013-08-27 08:21:25 +00001139 mangleTemplateArg(TD, *I);
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001140 break;
1141 case TemplateArgument::Template:
David Majnemer0db0ca42013-08-05 22:26:46 +00001142 mangleType(cast<TagDecl>(
1143 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
1144 break;
Reid Klecknerf0ae35b2013-07-02 18:10:07 +00001145 }
1146}
1147
Guy Benyei11169dd2012-12-18 14:30:41 +00001148void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
1149 bool IsMember) {
1150 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
1151 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
1152 // 'I' means __restrict (32/64-bit).
1153 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
1154 // keyword!
1155 // <base-cvr-qualifiers> ::= A # near
1156 // ::= B # near const
1157 // ::= C # near volatile
1158 // ::= D # near const volatile
1159 // ::= E # far (16-bit)
1160 // ::= F # far const (16-bit)
1161 // ::= G # far volatile (16-bit)
1162 // ::= H # far const volatile (16-bit)
1163 // ::= I # huge (16-bit)
1164 // ::= J # huge const (16-bit)
1165 // ::= K # huge volatile (16-bit)
1166 // ::= L # huge const volatile (16-bit)
1167 // ::= M <basis> # based
1168 // ::= N <basis> # based const
1169 // ::= O <basis> # based volatile
1170 // ::= P <basis> # based const volatile
1171 // ::= Q # near member
1172 // ::= R # near const member
1173 // ::= S # near volatile member
1174 // ::= T # near const volatile member
1175 // ::= U # far member (16-bit)
1176 // ::= V # far const member (16-bit)
1177 // ::= W # far volatile member (16-bit)
1178 // ::= X # far const volatile member (16-bit)
1179 // ::= Y # huge member (16-bit)
1180 // ::= Z # huge const member (16-bit)
1181 // ::= 0 # huge volatile member (16-bit)
1182 // ::= 1 # huge const volatile member (16-bit)
1183 // ::= 2 <basis> # based member
1184 // ::= 3 <basis> # based const member
1185 // ::= 4 <basis> # based volatile member
1186 // ::= 5 <basis> # based const volatile member
1187 // ::= 6 # near function (pointers only)
1188 // ::= 7 # far function (pointers only)
1189 // ::= 8 # near method (pointers only)
1190 // ::= 9 # far method (pointers only)
1191 // ::= _A <basis> # based function (pointers only)
1192 // ::= _B <basis> # based function (far?) (pointers only)
1193 // ::= _C <basis> # based method (pointers only)
1194 // ::= _D <basis> # based method (far?) (pointers only)
1195 // ::= _E # block (Clang)
1196 // <basis> ::= 0 # __based(void)
1197 // ::= 1 # __based(segment)?
1198 // ::= 2 <name> # __based(name)
1199 // ::= 3 # ?
1200 // ::= 4 # ?
1201 // ::= 5 # not really based
1202 bool HasConst = Quals.hasConst(),
1203 HasVolatile = Quals.hasVolatile();
David Majnemer89594f32013-08-05 22:43:06 +00001204
Guy Benyei11169dd2012-12-18 14:30:41 +00001205 if (!IsMember) {
1206 if (HasConst && HasVolatile) {
1207 Out << 'D';
1208 } else if (HasVolatile) {
1209 Out << 'C';
1210 } else if (HasConst) {
1211 Out << 'B';
1212 } else {
1213 Out << 'A';
1214 }
1215 } else {
1216 if (HasConst && HasVolatile) {
1217 Out << 'T';
1218 } else if (HasVolatile) {
1219 Out << 'S';
1220 } else if (HasConst) {
1221 Out << 'R';
1222 } else {
1223 Out << 'Q';
1224 }
1225 }
1226
1227 // FIXME: For now, just drop all extension qualifiers on the floor.
1228}
1229
David Majnemer8eec58f2014-02-18 14:20:10 +00001230void
1231MicrosoftCXXNameMangler::manglePointerExtQualifiers(Qualifiers Quals,
1232 const Type *PointeeType) {
1233 bool HasRestrict = Quals.hasRestrict();
1234 if (PointersAre64Bit && (!PointeeType || !PointeeType->isFunctionType()))
1235 Out << 'E';
1236
1237 if (HasRestrict)
1238 Out << 'I';
1239}
1240
1241void MicrosoftCXXNameMangler::manglePointerCVQualifiers(Qualifiers Quals) {
1242 // <pointer-cv-qualifiers> ::= P # no qualifiers
1243 // ::= Q # const
1244 // ::= R # volatile
1245 // ::= S # const volatile
Guy Benyei11169dd2012-12-18 14:30:41 +00001246 bool HasConst = Quals.hasConst(),
David Majnemer8eec58f2014-02-18 14:20:10 +00001247 HasVolatile = Quals.hasVolatile();
David Majnemer0b6bf8a2014-02-18 12:58:35 +00001248
Guy Benyei11169dd2012-12-18 14:30:41 +00001249 if (HasConst && HasVolatile) {
1250 Out << 'S';
1251 } else if (HasVolatile) {
1252 Out << 'R';
1253 } else if (HasConst) {
1254 Out << 'Q';
1255 } else {
1256 Out << 'P';
1257 }
1258}
1259
1260void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1261 SourceRange Range) {
Reid Kleckner18da98e2013-06-24 19:21:52 +00001262 // MSVC will backreference two canonically equivalent types that have slightly
1263 // different manglings when mangled alone.
David Majnemer5a1b2042013-09-11 04:44:30 +00001264
1265 // Decayed types do not match up with non-decayed versions of the same type.
1266 //
1267 // e.g.
1268 // void (*x)(void) will not form a backreference with void x(void)
1269 void *TypePtr;
1270 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1271 TypePtr = DT->getOriginalType().getCanonicalType().getAsOpaquePtr();
1272 // If the original parameter was textually written as an array,
1273 // instead treat the decayed parameter like it's const.
1274 //
1275 // e.g.
1276 // int [] -> int * const
1277 if (DT->getOriginalType()->isArrayType())
1278 T = T.withConst();
1279 } else
1280 TypePtr = T.getCanonicalType().getAsOpaquePtr();
1281
Guy Benyei11169dd2012-12-18 14:30:41 +00001282 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1283
1284 if (Found == TypeBackReferences.end()) {
1285 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1286
David Majnemer5a1b2042013-09-11 04:44:30 +00001287 mangleType(T, Range, QMM_Drop);
Guy Benyei11169dd2012-12-18 14:30:41 +00001288
1289 // See if it's worth creating a back reference.
1290 // Only types longer than 1 character are considered
1291 // and only 10 back references slots are available:
1292 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1293 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1294 size_t Size = TypeBackReferences.size();
1295 TypeBackReferences[TypePtr] = Size;
1296 }
1297 } else {
1298 Out << Found->second;
1299 }
1300}
1301
1302void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourne2816c022013-04-25 04:25:40 +00001303 QualifierMangleMode QMM) {
Reid Kleckner18da98e2013-06-24 19:21:52 +00001304 // Don't use the canonical types. MSVC includes things like 'const' on
1305 // pointer arguments to function pointers that canonicalization strips away.
1306 T = T.getDesugaredType(getASTContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001307 Qualifiers Quals = T.getLocalQualifiers();
Reid Kleckner18da98e2013-06-24 19:21:52 +00001308 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1309 // If there were any Quals, getAsArrayType() pushed them onto the array
1310 // element type.
Peter Collingbourne2816c022013-04-25 04:25:40 +00001311 if (QMM == QMM_Mangle)
1312 Out << 'A';
1313 else if (QMM == QMM_Escape || QMM == QMM_Result)
1314 Out << "$$B";
Reid Kleckner18da98e2013-06-24 19:21:52 +00001315 mangleArrayType(AT);
Peter Collingbourne2816c022013-04-25 04:25:40 +00001316 return;
1317 }
1318
1319 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1320 T->isBlockPointerType();
1321
1322 switch (QMM) {
1323 case QMM_Drop:
1324 break;
1325 case QMM_Mangle:
1326 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1327 Out << '6';
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001328 mangleFunctionType(FT);
Peter Collingbourne2816c022013-04-25 04:25:40 +00001329 return;
1330 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001331 mangleQualifiers(Quals, false);
Peter Collingbourne2816c022013-04-25 04:25:40 +00001332 break;
1333 case QMM_Escape:
1334 if (!IsPointer && Quals) {
1335 Out << "$$C";
1336 mangleQualifiers(Quals, false);
1337 }
1338 break;
1339 case QMM_Result:
1340 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1341 Out << '?';
1342 mangleQualifiers(Quals, false);
1343 }
1344 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001345 }
1346
Peter Collingbourne2816c022013-04-25 04:25:40 +00001347 // We have to mangle these now, while we still have enough information.
David Majnemer8eec58f2014-02-18 14:20:10 +00001348 if (IsPointer) {
1349 manglePointerCVQualifiers(Quals);
1350 manglePointerExtQualifiers(Quals, T->getPointeeType().getTypePtr());
1351 }
Peter Collingbourne2816c022013-04-25 04:25:40 +00001352 const Type *ty = T.getTypePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +00001353
1354 switch (ty->getTypeClass()) {
1355#define ABSTRACT_TYPE(CLASS, PARENT)
1356#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1357 case Type::CLASS: \
1358 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1359 return;
1360#define TYPE(CLASS, PARENT) \
1361 case Type::CLASS: \
1362 mangleType(cast<CLASS##Type>(ty), Range); \
1363 break;
1364#include "clang/AST/TypeNodes.def"
1365#undef ABSTRACT_TYPE
1366#undef NON_CANONICAL_TYPE
1367#undef TYPE
1368 }
1369}
1370
1371void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1372 SourceRange Range) {
1373 // <type> ::= <builtin-type>
1374 // <builtin-type> ::= X # void
1375 // ::= C # signed char
1376 // ::= D # char
1377 // ::= E # unsigned char
1378 // ::= F # short
1379 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1380 // ::= H # int
1381 // ::= I # unsigned int
1382 // ::= J # long
1383 // ::= K # unsigned long
1384 // L # <none>
1385 // ::= M # float
1386 // ::= N # double
1387 // ::= O # long double (__float80 is mangled differently)
1388 // ::= _J # long long, __int64
1389 // ::= _K # unsigned long long, __int64
1390 // ::= _L # __int128
1391 // ::= _M # unsigned __int128
1392 // ::= _N # bool
1393 // _O # <array in parameter>
1394 // ::= _T # __float80 (Intel)
1395 // ::= _W # wchar_t
1396 // ::= _Z # __float80 (Digital Mars)
1397 switch (T->getKind()) {
1398 case BuiltinType::Void: Out << 'X'; break;
1399 case BuiltinType::SChar: Out << 'C'; break;
1400 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1401 case BuiltinType::UChar: Out << 'E'; break;
1402 case BuiltinType::Short: Out << 'F'; break;
1403 case BuiltinType::UShort: Out << 'G'; break;
1404 case BuiltinType::Int: Out << 'H'; break;
1405 case BuiltinType::UInt: Out << 'I'; break;
1406 case BuiltinType::Long: Out << 'J'; break;
1407 case BuiltinType::ULong: Out << 'K'; break;
1408 case BuiltinType::Float: Out << 'M'; break;
1409 case BuiltinType::Double: Out << 'N'; break;
1410 // TODO: Determine size and mangle accordingly
1411 case BuiltinType::LongDouble: Out << 'O'; break;
1412 case BuiltinType::LongLong: Out << "_J"; break;
1413 case BuiltinType::ULongLong: Out << "_K"; break;
1414 case BuiltinType::Int128: Out << "_L"; break;
1415 case BuiltinType::UInt128: Out << "_M"; break;
1416 case BuiltinType::Bool: Out << "_N"; break;
1417 case BuiltinType::WChar_S:
1418 case BuiltinType::WChar_U: Out << "_W"; break;
1419
1420#define BUILTIN_TYPE(Id, SingletonId)
1421#define PLACEHOLDER_TYPE(Id, SingletonId) \
1422 case BuiltinType::Id:
1423#include "clang/AST/BuiltinTypes.def"
1424 case BuiltinType::Dependent:
1425 llvm_unreachable("placeholder types shouldn't get to name mangling");
1426
1427 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1428 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1429 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00001430
1431 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1432 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1433 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1434 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1435 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1436 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei61054192013-02-07 10:55:47 +00001437 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001438 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
David Majnemerb4119f72013-12-13 01:06:04 +00001439
Guy Benyei11169dd2012-12-18 14:30:41 +00001440 case BuiltinType::NullPtr: Out << "$$T"; break;
1441
1442 case BuiltinType::Char16:
1443 case BuiltinType::Char32:
1444 case BuiltinType::Half: {
1445 DiagnosticsEngine &Diags = Context.getDiags();
1446 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1447 "cannot mangle this built-in %0 type yet");
1448 Diags.Report(Range.getBegin(), DiagID)
1449 << T->getName(Context.getASTContext().getPrintingPolicy())
1450 << Range;
1451 break;
1452 }
1453 }
1454}
1455
1456// <type> ::= <function-type>
1457void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1458 SourceRange) {
1459 // Structors only appear in decls, so at this point we know it's not a
1460 // structor type.
1461 // FIXME: This may not be lambda-friendly.
1462 Out << "$$A6";
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001463 mangleFunctionType(T);
Guy Benyei11169dd2012-12-18 14:30:41 +00001464}
1465void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1466 SourceRange) {
1467 llvm_unreachable("Can't mangle K&R function prototypes");
1468}
1469
Peter Collingbourne2816c022013-04-25 04:25:40 +00001470void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1471 const FunctionDecl *D,
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001472 bool ForceInstMethod) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001473 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1474 // <return-type> <argument-list> <throw-spec>
1475 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1476
Reid Kleckner18da98e2013-06-24 19:21:52 +00001477 SourceRange Range;
1478 if (D) Range = D->getSourceRange();
1479
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001480 bool IsStructor = false, IsInstMethod = ForceInstMethod;
1481 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(D)) {
1482 if (MD->isInstance())
1483 IsInstMethod = true;
1484 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
1485 IsStructor = true;
1486 }
1487
Guy Benyei11169dd2012-12-18 14:30:41 +00001488 // If this is a C++ instance method, mangle the CVR qualifiers for the
1489 // this pointer.
David Majnemer6dda7bb2013-08-15 08:13:23 +00001490 if (IsInstMethod) {
David Majnemer8eec58f2014-02-18 14:20:10 +00001491 Qualifiers Quals = Qualifiers::fromCVRMask(Proto->getTypeQuals());
1492 manglePointerExtQualifiers(Quals, 0);
1493 mangleQualifiers(Quals, false);
David Majnemer6dda7bb2013-08-15 08:13:23 +00001494 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001495
Reid Klecknerc5cc3382013-09-25 22:28:52 +00001496 mangleCallingConvention(T);
Guy Benyei11169dd2012-12-18 14:30:41 +00001497
1498 // <return-type> ::= <type>
1499 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001500 if (IsStructor) {
1501 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1502 StructorType == Dtor_Deleting) {
1503 // The scalar deleting destructor takes an extra int argument.
1504 // However, the FunctionType generated has 0 arguments.
1505 // FIXME: This is a temporary hack.
1506 // Maybe should fix the FunctionType creation instead?
Timur Iskhodzhanovf46993e2013-08-26 10:32:04 +00001507 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001508 return;
1509 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001510 Out << '@';
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001511 } else {
Alp Toker314cc812014-01-25 16:55:45 +00001512 QualType ResultType = Proto->getReturnType();
David Majnemer6dda7bb2013-08-15 08:13:23 +00001513 if (ResultType->isVoidType())
1514 ResultType = ResultType.getUnqualifiedType();
1515 mangleType(ResultType, Range, QMM_Result);
Guy Benyei11169dd2012-12-18 14:30:41 +00001516 }
1517
1518 // <argument-list> ::= X # void
1519 // ::= <type>+ @
1520 // ::= <type>* Z # varargs
Alp Toker9cacbab2014-01-20 20:26:09 +00001521 if (Proto->getNumParams() == 0 && !Proto->isVariadic()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001522 Out << 'X';
1523 } else {
Reid Kleckner18da98e2013-06-24 19:21:52 +00001524 // Happens for function pointer type arguments for example.
Alp Toker9cacbab2014-01-20 20:26:09 +00001525 for (FunctionProtoType::param_type_iterator
1526 Arg = Proto->param_type_begin(),
1527 ArgEnd = Proto->param_type_end();
Reid Kleckner18da98e2013-06-24 19:21:52 +00001528 Arg != ArgEnd; ++Arg)
1529 mangleArgumentType(*Arg, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001530 // <builtin-type> ::= Z # ellipsis
1531 if (Proto->isVariadic())
1532 Out << 'Z';
1533 else
1534 Out << '@';
1535 }
1536
1537 mangleThrowSpecification(Proto);
1538}
1539
1540void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Kleckner369f3162013-05-14 20:30:42 +00001541 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1542 // # pointer. in 64-bit mode *all*
1543 // # 'this' pointers are 64-bit.
1544 // ::= <global-function>
1545 // <member-function> ::= A # private: near
1546 // ::= B # private: far
1547 // ::= C # private: static near
1548 // ::= D # private: static far
1549 // ::= E # private: virtual near
1550 // ::= F # private: virtual far
Reid Kleckner369f3162013-05-14 20:30:42 +00001551 // ::= I # protected: near
1552 // ::= J # protected: far
1553 // ::= K # protected: static near
1554 // ::= L # protected: static far
1555 // ::= M # protected: virtual near
1556 // ::= N # protected: virtual far
Reid Kleckner369f3162013-05-14 20:30:42 +00001557 // ::= Q # public: near
1558 // ::= R # public: far
1559 // ::= S # public: static near
1560 // ::= T # public: static far
1561 // ::= U # public: virtual near
1562 // ::= V # public: virtual far
Reid Kleckner369f3162013-05-14 20:30:42 +00001563 // <global-function> ::= Y # global near
1564 // ::= Z # global far
Guy Benyei11169dd2012-12-18 14:30:41 +00001565 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1566 switch (MD->getAccess()) {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00001567 case AS_none:
1568 llvm_unreachable("Unsupported access specifier");
Guy Benyei11169dd2012-12-18 14:30:41 +00001569 case AS_private:
1570 if (MD->isStatic())
1571 Out << 'C';
1572 else if (MD->isVirtual())
1573 Out << 'E';
1574 else
1575 Out << 'A';
1576 break;
1577 case AS_protected:
1578 if (MD->isStatic())
1579 Out << 'K';
1580 else if (MD->isVirtual())
1581 Out << 'M';
1582 else
1583 Out << 'I';
1584 break;
1585 case AS_public:
1586 if (MD->isStatic())
1587 Out << 'S';
1588 else if (MD->isVirtual())
1589 Out << 'U';
1590 else
1591 Out << 'Q';
1592 }
1593 } else
1594 Out << 'Y';
1595}
Reid Klecknerc5cc3382013-09-25 22:28:52 +00001596void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001597 // <calling-convention> ::= A # __cdecl
1598 // ::= B # __export __cdecl
1599 // ::= C # __pascal
1600 // ::= D # __export __pascal
1601 // ::= E # __thiscall
1602 // ::= F # __export __thiscall
1603 // ::= G # __stdcall
1604 // ::= H # __export __stdcall
1605 // ::= I # __fastcall
1606 // ::= J # __export __fastcall
1607 // The 'export' calling conventions are from a bygone era
1608 // (*cough*Win16*cough*) when functions were declared for export with
1609 // that keyword. (It didn't actually export them, it just made them so
1610 // that they could be in a DLL and somebody from another module could call
1611 // them.)
1612 CallingConv CC = T->getCallConv();
Guy Benyei11169dd2012-12-18 14:30:41 +00001613 switch (CC) {
1614 default:
1615 llvm_unreachable("Unsupported CC for mangling");
Charles Davisb5a214e2013-08-30 04:39:01 +00001616 case CC_X86_64Win64:
1617 case CC_X86_64SysV:
Guy Benyei11169dd2012-12-18 14:30:41 +00001618 case CC_C: Out << 'A'; break;
1619 case CC_X86Pascal: Out << 'C'; break;
1620 case CC_X86ThisCall: Out << 'E'; break;
1621 case CC_X86StdCall: Out << 'G'; break;
1622 case CC_X86FastCall: Out << 'I'; break;
1623 }
1624}
1625void MicrosoftCXXNameMangler::mangleThrowSpecification(
1626 const FunctionProtoType *FT) {
1627 // <throw-spec> ::= Z # throw(...) (default)
1628 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1629 // ::= <type>+
1630 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1631 // all actually mangled as 'Z'. (They're ignored because their associated
1632 // functionality isn't implemented, and probably never will be.)
1633 Out << 'Z';
1634}
1635
1636void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1637 SourceRange Range) {
1638 // Probably should be mangled as a template instantiation; need to see what
1639 // VC does first.
1640 DiagnosticsEngine &Diags = Context.getDiags();
1641 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1642 "cannot mangle this unresolved dependent type yet");
1643 Diags.Report(Range.getBegin(), DiagID)
1644 << Range;
1645}
1646
1647// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1648// <union-type> ::= T <name>
1649// <struct-type> ::= U <name>
1650// <class-type> ::= V <name>
David Majnemer048f90c2013-12-09 04:28:34 +00001651// <enum-type> ::= W4 <name>
Guy Benyei11169dd2012-12-18 14:30:41 +00001652void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer0db0ca42013-08-05 22:26:46 +00001653 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00001654}
1655void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer0db0ca42013-08-05 22:26:46 +00001656 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00001657}
David Majnemer0db0ca42013-08-05 22:26:46 +00001658void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1659 switch (TD->getTagKind()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001660 case TTK_Union:
1661 Out << 'T';
1662 break;
1663 case TTK_Struct:
1664 case TTK_Interface:
1665 Out << 'U';
1666 break;
1667 case TTK_Class:
1668 Out << 'V';
1669 break;
1670 case TTK_Enum:
David Majnemer048f90c2013-12-09 04:28:34 +00001671 Out << "W4";
Guy Benyei11169dd2012-12-18 14:30:41 +00001672 break;
1673 }
David Majnemer0db0ca42013-08-05 22:26:46 +00001674 mangleName(TD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001675}
1676
1677// <type> ::= <array-type>
1678// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1679// [Y <dimension-count> <dimension>+]
Reid Kleckner369f3162013-05-14 20:30:42 +00001680// <element-type> # as global, E is never required
Guy Benyei11169dd2012-12-18 14:30:41 +00001681// It's supposed to be the other way around, but for some strange reason, it
1682// isn't. Today this behavior is retained for the sole purpose of backwards
1683// compatibility.
David Majnemer5a1b2042013-09-11 04:44:30 +00001684void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001685 // This isn't a recursive mangling, so now we have to do it all in this
1686 // one call.
David Majnemer8eec58f2014-02-18 14:20:10 +00001687 manglePointerCVQualifiers(T->getElementType().getQualifiers());
Peter Collingbourne2816c022013-04-25 04:25:40 +00001688 mangleType(T->getElementType(), SourceRange());
Guy Benyei11169dd2012-12-18 14:30:41 +00001689}
1690void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1691 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001692 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001693}
1694void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1695 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001696 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001697}
1698void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1699 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001700 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001701}
1702void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1703 SourceRange) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001704 llvm_unreachable("Should have been special cased");
Guy Benyei11169dd2012-12-18 14:30:41 +00001705}
Reid Kleckner18da98e2013-06-24 19:21:52 +00001706void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourne2816c022013-04-25 04:25:40 +00001707 QualType ElementTy(T, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001708 SmallVector<llvm::APInt, 3> Dimensions;
1709 for (;;) {
1710 if (const ConstantArrayType *CAT =
1711 getASTContext().getAsConstantArrayType(ElementTy)) {
1712 Dimensions.push_back(CAT->getSize());
1713 ElementTy = CAT->getElementType();
1714 } else if (ElementTy->isVariableArrayType()) {
1715 const VariableArrayType *VAT =
1716 getASTContext().getAsVariableArrayType(ElementTy);
1717 DiagnosticsEngine &Diags = Context.getDiags();
1718 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1719 "cannot mangle this variable-length array yet");
1720 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1721 << VAT->getBracketsRange();
1722 return;
1723 } else if (ElementTy->isDependentSizedArrayType()) {
1724 // The dependent expression has to be folded into a constant (TODO).
1725 const DependentSizedArrayType *DSAT =
1726 getASTContext().getAsDependentSizedArrayType(ElementTy);
1727 DiagnosticsEngine &Diags = Context.getDiags();
1728 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1729 "cannot mangle this dependent-length array yet");
1730 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1731 << DSAT->getBracketsRange();
1732 return;
Peter Collingbourne2816c022013-04-25 04:25:40 +00001733 } else if (const IncompleteArrayType *IAT =
1734 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1735 Dimensions.push_back(llvm::APInt(32, 0));
1736 ElementTy = IAT->getElementType();
1737 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001738 else break;
1739 }
Peter Collingbourne2816c022013-04-25 04:25:40 +00001740 Out << 'Y';
1741 // <dimension-count> ::= <number> # number of extra dimensions
1742 mangleNumber(Dimensions.size());
1743 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1744 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Kleckner18da98e2013-06-24 19:21:52 +00001745 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei11169dd2012-12-18 14:30:41 +00001746}
1747
1748// <type> ::= <pointer-to-member-type>
1749// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1750// <class name> <type>
1751void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1752 SourceRange Range) {
1753 QualType PointeeType = T->getPointeeType();
1754 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1755 Out << '8';
1756 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001757 mangleFunctionType(FPT, 0, true);
Guy Benyei11169dd2012-12-18 14:30:41 +00001758 } else {
1759 mangleQualifiers(PointeeType.getQualifiers(), true);
1760 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourne2816c022013-04-25 04:25:40 +00001761 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei11169dd2012-12-18 14:30:41 +00001762 }
1763}
1764
1765void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1766 SourceRange Range) {
1767 DiagnosticsEngine &Diags = Context.getDiags();
1768 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1769 "cannot mangle this template type parameter type yet");
1770 Diags.Report(Range.getBegin(), DiagID)
1771 << Range;
1772}
1773
1774void MicrosoftCXXNameMangler::mangleType(
1775 const SubstTemplateTypeParmPackType *T,
1776 SourceRange Range) {
1777 DiagnosticsEngine &Diags = Context.getDiags();
1778 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1779 "cannot mangle this substituted parameter pack yet");
1780 Diags.Report(Range.getBegin(), DiagID)
1781 << Range;
1782}
1783
1784// <type> ::= <pointer-type>
Reid Kleckner369f3162013-05-14 20:30:42 +00001785// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
Alp Tokerd4733632013-12-05 04:47:09 +00001786// # the E is required for 64-bit non-static pointers
Guy Benyei11169dd2012-12-18 14:30:41 +00001787void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1788 SourceRange Range) {
1789 QualType PointeeTy = T->getPointeeType();
Peter Collingbourne2816c022013-04-25 04:25:40 +00001790 mangleType(PointeeTy, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001791}
1792void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1793 SourceRange Range) {
1794 // Object pointers never have qualifiers.
1795 Out << 'A';
David Majnemer8eec58f2014-02-18 14:20:10 +00001796 manglePointerExtQualifiers(Qualifiers(), T->getPointeeType().getTypePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001797 mangleType(T->getPointeeType(), Range);
1798}
1799
1800// <type> ::= <reference-type>
Reid Kleckner369f3162013-05-14 20:30:42 +00001801// <reference-type> ::= A E? <cvr-qualifiers> <type>
Alp Tokerd4733632013-12-05 04:47:09 +00001802// # the E is required for 64-bit non-static lvalue references
Guy Benyei11169dd2012-12-18 14:30:41 +00001803void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1804 SourceRange Range) {
1805 Out << 'A';
David Majnemer8eec58f2014-02-18 14:20:10 +00001806 manglePointerExtQualifiers(Qualifiers(), T->getPointeeType().getTypePtr());
Peter Collingbourne2816c022013-04-25 04:25:40 +00001807 mangleType(T->getPointeeType(), Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001808}
1809
1810// <type> ::= <r-value-reference-type>
Reid Kleckner369f3162013-05-14 20:30:42 +00001811// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
Alp Tokerd4733632013-12-05 04:47:09 +00001812// # the E is required for 64-bit non-static rvalue references
Guy Benyei11169dd2012-12-18 14:30:41 +00001813void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1814 SourceRange Range) {
1815 Out << "$$Q";
David Majnemer8eec58f2014-02-18 14:20:10 +00001816 manglePointerExtQualifiers(Qualifiers(), T->getPointeeType().getTypePtr());
Peter Collingbourne2816c022013-04-25 04:25:40 +00001817 mangleType(T->getPointeeType(), Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00001818}
1819
1820void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1821 SourceRange Range) {
1822 DiagnosticsEngine &Diags = Context.getDiags();
1823 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1824 "cannot mangle this complex number type yet");
1825 Diags.Report(Range.getBegin(), DiagID)
1826 << Range;
1827}
1828
1829void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1830 SourceRange Range) {
Reid Klecknere7e64d82013-03-26 16:56:59 +00001831 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1832 assert(ET && "vectors with non-builtin elements are unsupported");
1833 uint64_t Width = getASTContext().getTypeSize(T);
1834 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1835 // doesn't match the Intel types uses a custom mangling below.
1836 bool IntelVector = true;
1837 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1838 Out << "T__m64";
1839 } else if (Width == 128 || Width == 256) {
1840 if (ET->getKind() == BuiltinType::Float)
1841 Out << "T__m" << Width;
1842 else if (ET->getKind() == BuiltinType::LongLong)
1843 Out << "T__m" << Width << 'i';
1844 else if (ET->getKind() == BuiltinType::Double)
1845 Out << "U__m" << Width << 'd';
1846 else
1847 IntelVector = false;
1848 } else {
1849 IntelVector = false;
1850 }
1851
1852 if (!IntelVector) {
1853 // The MS ABI doesn't have a special mangling for vector types, so we define
1854 // our own mangling to handle uses of __vector_size__ on user-specified
1855 // types, and for extensions like __v4sf.
1856 Out << "T__clang_vec" << T->getNumElements() << '_';
1857 mangleType(ET, Range);
1858 }
1859
1860 Out << "@@";
Guy Benyei11169dd2012-12-18 14:30:41 +00001861}
Reid Klecknere7e64d82013-03-26 16:56:59 +00001862
Guy Benyei11169dd2012-12-18 14:30:41 +00001863void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1864 SourceRange Range) {
1865 DiagnosticsEngine &Diags = Context.getDiags();
1866 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1867 "cannot mangle this extended vector type yet");
1868 Diags.Report(Range.getBegin(), DiagID)
1869 << Range;
1870}
1871void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1872 SourceRange Range) {
1873 DiagnosticsEngine &Diags = Context.getDiags();
1874 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1875 "cannot mangle this dependent-sized extended vector type yet");
1876 Diags.Report(Range.getBegin(), DiagID)
1877 << Range;
1878}
1879
1880void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1881 SourceRange) {
1882 // ObjC interfaces have structs underlying them.
1883 Out << 'U';
1884 mangleName(T->getDecl());
1885}
1886
1887void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1888 SourceRange Range) {
1889 // We don't allow overloading by different protocol qualification,
1890 // so mangling them isn't necessary.
1891 mangleType(T->getBaseType(), Range);
1892}
1893
1894void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1895 SourceRange Range) {
1896 Out << "_E";
1897
1898 QualType pointee = T->getPointeeType();
Timur Iskhodzhanov555a7722013-10-04 11:25:05 +00001899 mangleFunctionType(pointee->castAs<FunctionProtoType>());
Guy Benyei11169dd2012-12-18 14:30:41 +00001900}
1901
David Majnemerf0a84f22013-08-16 08:29:13 +00001902void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1903 SourceRange) {
1904 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei11169dd2012-12-18 14:30:41 +00001905}
1906
1907void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1908 SourceRange Range) {
1909 DiagnosticsEngine &Diags = Context.getDiags();
1910 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1911 "cannot mangle this template specialization type yet");
1912 Diags.Report(Range.getBegin(), DiagID)
1913 << Range;
1914}
1915
1916void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1917 SourceRange Range) {
1918 DiagnosticsEngine &Diags = Context.getDiags();
1919 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1920 "cannot mangle this dependent name type yet");
1921 Diags.Report(Range.getBegin(), DiagID)
1922 << Range;
1923}
1924
1925void MicrosoftCXXNameMangler::mangleType(
1926 const DependentTemplateSpecializationType *T,
1927 SourceRange Range) {
1928 DiagnosticsEngine &Diags = Context.getDiags();
1929 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1930 "cannot mangle this dependent template specialization type yet");
1931 Diags.Report(Range.getBegin(), DiagID)
1932 << Range;
1933}
1934
1935void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1936 SourceRange Range) {
1937 DiagnosticsEngine &Diags = Context.getDiags();
1938 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1939 "cannot mangle this pack expansion yet");
1940 Diags.Report(Range.getBegin(), DiagID)
1941 << Range;
1942}
1943
1944void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1945 SourceRange Range) {
1946 DiagnosticsEngine &Diags = Context.getDiags();
1947 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1948 "cannot mangle this typeof(type) yet");
1949 Diags.Report(Range.getBegin(), DiagID)
1950 << Range;
1951}
1952
1953void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1954 SourceRange Range) {
1955 DiagnosticsEngine &Diags = Context.getDiags();
1956 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1957 "cannot mangle this typeof(expression) yet");
1958 Diags.Report(Range.getBegin(), DiagID)
1959 << Range;
1960}
1961
1962void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1963 SourceRange Range) {
1964 DiagnosticsEngine &Diags = Context.getDiags();
1965 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1966 "cannot mangle this decltype() yet");
1967 Diags.Report(Range.getBegin(), DiagID)
1968 << Range;
1969}
1970
1971void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1972 SourceRange Range) {
1973 DiagnosticsEngine &Diags = Context.getDiags();
1974 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1975 "cannot mangle this unary transform type yet");
1976 Diags.Report(Range.getBegin(), DiagID)
1977 << Range;
1978}
1979
1980void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
David Majnemerb9a5f2d2014-01-21 20:33:36 +00001981 assert(T->getDeducedType().isNull() && "expecting a dependent type!");
1982
Guy Benyei11169dd2012-12-18 14:30:41 +00001983 DiagnosticsEngine &Diags = Context.getDiags();
1984 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1985 "cannot mangle this 'auto' type yet");
1986 Diags.Report(Range.getBegin(), DiagID)
1987 << Range;
1988}
1989
1990void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1991 SourceRange Range) {
1992 DiagnosticsEngine &Diags = Context.getDiags();
1993 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1994 "cannot mangle this C11 atomic type yet");
1995 Diags.Report(Range.getBegin(), DiagID)
1996 << Range;
1997}
1998
Rafael Espindola002667c2013-10-16 01:40:34 +00001999void MicrosoftMangleContextImpl::mangleCXXName(const NamedDecl *D,
2000 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002001 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2002 "Invalid mangleName() call, argument is not a variable or function!");
2003 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2004 "Invalid mangleName() call on 'structor decl!");
2005
2006 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2007 getASTContext().getSourceManager(),
2008 "Mangling declaration");
2009
2010 MicrosoftCXXNameMangler Mangler(*this, Out);
2011 return Mangler.mangle(D);
2012}
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002013
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002014// <this-adjustment> ::= <no-adjustment> | <static-adjustment> |
2015// <virtual-adjustment>
2016// <no-adjustment> ::= A # private near
2017// ::= B # private far
2018// ::= I # protected near
2019// ::= J # protected far
2020// ::= Q # public near
2021// ::= R # public far
2022// <static-adjustment> ::= G <static-offset> # private near
2023// ::= H <static-offset> # private far
2024// ::= O <static-offset> # protected near
2025// ::= P <static-offset> # protected far
2026// ::= W <static-offset> # public near
2027// ::= X <static-offset> # public far
2028// <virtual-adjustment> ::= $0 <virtual-shift> <static-offset> # private near
2029// ::= $1 <virtual-shift> <static-offset> # private far
2030// ::= $2 <virtual-shift> <static-offset> # protected near
2031// ::= $3 <virtual-shift> <static-offset> # protected far
2032// ::= $4 <virtual-shift> <static-offset> # public near
2033// ::= $5 <virtual-shift> <static-offset> # public far
2034// <virtual-shift> ::= <vtordisp-shift> | <vtordispex-shift>
2035// <vtordisp-shift> ::= <offset-to-vtordisp>
2036// <vtordispex-shift> ::= <offset-to-vbptr> <vbase-offset-offset>
2037// <offset-to-vtordisp>
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002038static void mangleThunkThisAdjustment(const CXXMethodDecl *MD,
2039 const ThisAdjustment &Adjustment,
2040 MicrosoftCXXNameMangler &Mangler,
2041 raw_ostream &Out) {
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002042 if (!Adjustment.Virtual.isEmpty()) {
2043 Out << '$';
2044 char AccessSpec;
2045 switch (MD->getAccess()) {
2046 case AS_none:
2047 llvm_unreachable("Unsupported access specifier");
2048 case AS_private:
2049 AccessSpec = '0';
2050 break;
2051 case AS_protected:
2052 AccessSpec = '2';
2053 break;
2054 case AS_public:
2055 AccessSpec = '4';
2056 }
2057 if (Adjustment.Virtual.Microsoft.VBPtrOffset) {
2058 Out << 'R' << AccessSpec;
David Majnemer2a816452013-12-09 10:44:32 +00002059 Mangler.mangleNumber(
2060 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBPtrOffset));
2061 Mangler.mangleNumber(
2062 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VBOffsetOffset));
2063 Mangler.mangleNumber(
2064 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset));
2065 Mangler.mangleNumber(static_cast<uint32_t>(Adjustment.NonVirtual));
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002066 } else {
2067 Out << AccessSpec;
David Majnemer2a816452013-12-09 10:44:32 +00002068 Mangler.mangleNumber(
2069 static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset));
2070 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual));
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002071 }
2072 } else if (Adjustment.NonVirtual != 0) {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002073 switch (MD->getAccess()) {
2074 case AS_none:
2075 llvm_unreachable("Unsupported access specifier");
2076 case AS_private:
2077 Out << 'G';
2078 break;
2079 case AS_protected:
2080 Out << 'O';
2081 break;
2082 case AS_public:
2083 Out << 'W';
2084 }
David Majnemer2a816452013-12-09 10:44:32 +00002085 Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual));
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002086 } else {
2087 switch (MD->getAccess()) {
2088 case AS_none:
2089 llvm_unreachable("Unsupported access specifier");
2090 case AS_private:
2091 Out << 'A';
2092 break;
2093 case AS_protected:
2094 Out << 'I';
2095 break;
2096 case AS_public:
2097 Out << 'Q';
2098 }
2099 }
2100}
2101
Reid Kleckner96f8f932014-02-05 17:27:08 +00002102void
2103MicrosoftMangleContextImpl::mangleVirtualMemPtrThunk(const CXXMethodDecl *MD,
2104 raw_ostream &Out) {
2105 MicrosoftVTableContext *VTContext =
2106 cast<MicrosoftVTableContext>(getASTContext().getVTableContext());
2107 const MicrosoftVTableContext::MethodVFTableLocation &ML =
2108 VTContext->getMethodVFTableLocation(GlobalDecl(MD));
Hans Wennborg88497d62013-11-15 17:24:45 +00002109
2110 MicrosoftCXXNameMangler Mangler(*this, Out);
Reid Kleckner96f8f932014-02-05 17:27:08 +00002111 Mangler.getStream() << "\01?";
2112 Mangler.mangleVirtualMemPtrThunk(MD, ML);
Hans Wennborg88497d62013-11-15 17:24:45 +00002113}
2114
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002115void MicrosoftMangleContextImpl::mangleThunk(const CXXMethodDecl *MD,
2116 const ThunkInfo &Thunk,
2117 raw_ostream &Out) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002118 MicrosoftCXXNameMangler Mangler(*this, Out);
2119 Out << "\01?";
2120 Mangler.mangleName(MD);
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002121 mangleThunkThisAdjustment(MD, Thunk.This, Mangler, Out);
2122 if (!Thunk.Return.isEmpty())
2123 assert(Thunk.Method != 0 && "Thunk info should hold the overridee decl");
2124
2125 const CXXMethodDecl *DeclForFPT = Thunk.Method ? Thunk.Method : MD;
2126 Mangler.mangleFunctionType(
2127 DeclForFPT->getType()->castAs<FunctionProtoType>(), MD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002128}
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002129
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002130void MicrosoftMangleContextImpl::mangleCXXDtorThunk(
2131 const CXXDestructorDecl *DD, CXXDtorType Type,
2132 const ThisAdjustment &Adjustment, raw_ostream &Out) {
2133 // FIXME: Actually, the dtor thunk should be emitted for vector deleting
2134 // dtors rather than scalar deleting dtors. Just use the vector deleting dtor
2135 // mangling manually until we support both deleting dtor types.
2136 assert(Type == Dtor_Deleting);
2137 MicrosoftCXXNameMangler Mangler(*this, Out, DD, Type);
2138 Out << "\01??_E";
2139 Mangler.mangleName(DD->getParent());
2140 mangleThunkThisAdjustment(DD, Adjustment, Mangler, Out);
2141 Mangler.mangleFunctionType(DD->getType()->castAs<FunctionProtoType>(), DD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002142}
Reid Kleckner7810af02013-06-19 15:20:38 +00002143
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002144void MicrosoftMangleContextImpl::mangleCXXVFTable(
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00002145 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
2146 raw_ostream &Out) {
Reid Kleckner7810af02013-06-19 15:20:38 +00002147 // <mangled-name> ::= ?_7 <class-name> <storage-class>
2148 // <cvr-qualifiers> [<name>] @
Guy Benyei11169dd2012-12-18 14:30:41 +00002149 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner7810af02013-06-19 15:20:38 +00002150 // is always '6' for vftables.
Guy Benyei11169dd2012-12-18 14:30:41 +00002151 MicrosoftCXXNameMangler Mangler(*this, Out);
2152 Mangler.getStream() << "\01??_7";
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00002153 Mangler.mangleName(Derived);
2154 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
2155 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
2156 E = BasePath.end();
2157 I != E; ++I) {
2158 Mangler.mangleName(*I);
2159 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002160 Mangler.getStream() << '@';
2161}
Reid Kleckner7810af02013-06-19 15:20:38 +00002162
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002163void MicrosoftMangleContextImpl::mangleCXXVBTable(
Reid Kleckner7810af02013-06-19 15:20:38 +00002164 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
2165 raw_ostream &Out) {
2166 // <mangled-name> ::= ?_8 <class-name> <storage-class>
2167 // <cvr-qualifiers> [<name>] @
2168 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
2169 // is always '7' for vbtables.
2170 MicrosoftCXXNameMangler Mangler(*this, Out);
2171 Mangler.getStream() << "\01??_8";
2172 Mangler.mangleName(Derived);
2173 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
2174 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
2175 E = BasePath.end();
2176 I != E; ++I) {
2177 Mangler.mangleName(*I);
2178 }
2179 Mangler.getStream() << '@';
2180}
2181
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002182void MicrosoftMangleContextImpl::mangleCXXRTTI(QualType T, raw_ostream &) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002183 // FIXME: Give a location...
2184 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2185 "cannot mangle RTTI descriptors for type %0 yet");
2186 getDiags().Report(DiagID)
2187 << T.getBaseTypeIdentifier();
2188}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002189
2190void MicrosoftMangleContextImpl::mangleCXXRTTIName(QualType T, raw_ostream &) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002191 // FIXME: Give a location...
2192 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2193 "cannot mangle the name of type %0 into RTTI descriptors yet");
2194 getDiags().Report(DiagID)
2195 << T.getBaseTypeIdentifier();
2196}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002197
Reid Klecknercc99e262013-11-19 23:23:00 +00002198void MicrosoftMangleContextImpl::mangleTypeName(QualType T, raw_ostream &Out) {
2199 // This is just a made up unique string for the purposes of tbaa. undname
2200 // does *not* know how to demangle it.
2201 MicrosoftCXXNameMangler Mangler(*this, Out);
2202 Mangler.getStream() << '?';
2203 Mangler.mangleType(T, SourceRange());
2204}
2205
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002206void MicrosoftMangleContextImpl::mangleCXXCtor(const CXXConstructorDecl *D,
2207 CXXCtorType Type,
2208 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002209 MicrosoftCXXNameMangler mangler(*this, Out);
2210 mangler.mangle(D);
2211}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002212
2213void MicrosoftMangleContextImpl::mangleCXXDtor(const CXXDestructorDecl *D,
2214 CXXDtorType Type,
2215 raw_ostream &Out) {
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00002216 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei11169dd2012-12-18 14:30:41 +00002217 mangler.mangle(D);
2218}
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002219
2220void MicrosoftMangleContextImpl::mangleReferenceTemporary(const VarDecl *VD,
David Majnemer210e6bfa12013-12-13 00:39:38 +00002221 raw_ostream &) {
2222 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2223 "cannot mangle this reference temporary yet");
2224 getDiags().Report(VD->getLocation(), DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00002225}
2226
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002227void MicrosoftMangleContextImpl::mangleStaticGuardVariable(const VarDecl *VD,
2228 raw_ostream &Out) {
David Majnemer25e1a5e2013-12-13 00:52:45 +00002229 // TODO: This is not correct, especially with respect to MSVC2013. MSVC2013
2230 // utilizes thread local variables to implement thread safe, re-entrant
2231 // initialization for statics. They no longer differentiate between an
2232 // externally visible and non-externally visible static with respect to
2233 // mangling, they all get $TSS <number>.
2234 //
2235 // N.B. This means that they can get more than 32 static variable guards in a
2236 // scope. It also means that they broke compatibility with their own ABI.
2237
David Majnemer2206bf52014-03-05 08:57:59 +00002238 // <guard-name> ::= ?_B <postfix> @5 <scope-depth>
Reid Klecknerd8110b62013-09-10 20:14:30 +00002239 // ::= ?$S <guard-num> @ <postfix> @4IA
2240
2241 // The first mangling is what MSVC uses to guard static locals in inline
2242 // functions. It uses a different mangling in external functions to support
2243 // guarding more than 32 variables. MSVC rejects inline functions with more
2244 // than 32 static locals. We don't fully implement the second mangling
2245 // because those guards are not externally visible, and instead use LLVM's
2246 // default renaming when creating a new guard variable.
2247 MicrosoftCXXNameMangler Mangler(*this, Out);
2248
2249 bool Visible = VD->isExternallyVisible();
2250 // <operator-name> ::= ?_B # local static guard
2251 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@");
David Majnemer2206bf52014-03-05 08:57:59 +00002252 Mangler.mangleNestedName(VD);
2253 Mangler.getStream() << (Visible ? "@5" : "@4IA");
2254 if (Visible) {
2255 unsigned ScopeDepth;
2256 getNextDiscriminator(VD, ScopeDepth);
2257 Mangler.mangleNumber(ScopeDepth);
2258 }
Reid Klecknerd8110b62013-09-10 20:14:30 +00002259}
2260
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002261void MicrosoftMangleContextImpl::mangleInitFiniStub(const VarDecl *D,
2262 raw_ostream &Out,
2263 char CharCode) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002264 MicrosoftCXXNameMangler Mangler(*this, Out);
2265 Mangler.getStream() << "\01??__" << CharCode;
2266 Mangler.mangleName(D);
2267 // This is the function class mangling. These stubs are global, non-variadic,
2268 // cdecl functions that return void and take no args.
2269 Mangler.getStream() << "YAXXZ";
2270}
2271
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002272void MicrosoftMangleContextImpl::mangleDynamicInitializer(const VarDecl *D,
2273 raw_ostream &Out) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002274 // <initializer-name> ::= ?__E <name> YAXXZ
2275 mangleInitFiniStub(D, Out, 'E');
2276}
2277
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002278void
2279MicrosoftMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D,
2280 raw_ostream &Out) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00002281 // <destructor-name> ::= ?__F <name> YAXXZ
2282 mangleInitFiniStub(D, Out, 'F');
Reid Klecknerd8110b62013-09-10 20:14:30 +00002283}
2284
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00002285MicrosoftMangleContext *
2286MicrosoftMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags) {
2287 return new MicrosoftMangleContextImpl(Context, Diags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002288}