blob: d7237d395f9239120a04d157a1ef759d3f281c3a [file] [log] [blame]
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001//===--- MicrosoftMangle.cpp - Microsoft Visual C++ Name Mangling ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides C++ name mangling targeting the Microsoft Visual C++ ABI.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Mangle.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/CharUnits.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclCXX.h"
20#include "clang/AST/DeclObjC.h"
21#include "clang/AST/DeclTemplate.h"
22#include "clang/AST/ExprCXX.h"
23#include "clang/Basic/ABI.h"
24#include "clang/Basic/DiagnosticOptions.h"
Reid Klecknerd6a08d12013-05-14 20:30:42 +000025#include "clang/Basic/TargetInfo.h"
Reid Klecknerf0219cd2013-05-22 17:16:39 +000026#include "llvm/ADT/StringMap.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000027
28using namespace clang;
29
30namespace {
31
David Majnemercab7dad2013-09-13 09:03:14 +000032/// \brief Retrieve the declaration context that should be used when mangling
33/// the given declaration.
34static const DeclContext *getEffectiveDeclContext(const Decl *D) {
35 // The ABI assumes that lambda closure types that occur within
36 // default arguments live in the context of the function. However, due to
37 // the way in which Clang parses and creates function declarations, this is
38 // not the case: the lambda closure type ends up living in the context
39 // where the function itself resides, because the function declaration itself
40 // had not yet been created. Fix the context here.
41 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
42 if (RD->isLambda())
43 if (ParmVarDecl *ContextParam =
44 dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl()))
45 return ContextParam->getDeclContext();
46 }
47
48 // Perform the same check for block literals.
49 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
50 if (ParmVarDecl *ContextParam =
51 dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl()))
52 return ContextParam->getDeclContext();
53 }
54
55 const DeclContext *DC = D->getDeclContext();
56 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(DC))
57 return getEffectiveDeclContext(CD);
58
59 return DC;
60}
61
62static const DeclContext *getEffectiveParentContext(const DeclContext *DC) {
63 return getEffectiveDeclContext(cast<Decl>(DC));
64}
65
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000066static const FunctionDecl *getStructor(const FunctionDecl *fn) {
67 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
68 return ftd->getTemplatedDecl();
69
70 return fn;
71}
72
Guy Benyei7f92f2d2012-12-18 14:30:41 +000073/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
74/// Microsoft Visual C++ ABI.
75class MicrosoftCXXNameMangler {
76 MangleContext &Context;
77 raw_ostream &Out;
78
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000079 /// The "structor" is the top-level declaration being mangled, if
80 /// that's not a template specialization; otherwise it's the pattern
81 /// for that specialization.
82 const NamedDecl *Structor;
83 unsigned StructorType;
84
Reid Klecknerf0219cd2013-05-22 17:16:39 +000085 typedef llvm::StringMap<unsigned> BackRefMap;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000086 BackRefMap NameBackReferences;
87 bool UseNameBackReferences;
88
89 typedef llvm::DenseMap<void*, unsigned> ArgBackRefMap;
90 ArgBackRefMap TypeBackReferences;
91
92 ASTContext &getASTContext() const { return Context.getASTContext(); }
93
Reid Klecknerd6a08d12013-05-14 20:30:42 +000094 // FIXME: If we add support for __ptr32/64 qualifiers, then we should push
95 // this check into mangleQualifiers().
96 const bool PointersAre64Bit;
97
Guy Benyei7f92f2d2012-12-18 14:30:41 +000098public:
Peter Collingbourneb70d1c32013-04-25 04:25:40 +000099 enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result };
100
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000101 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000102 : Context(C), Out(Out_),
103 Structor(0), StructorType(-1),
David Blaikiee7e94c92013-05-14 21:31:46 +0000104 UseNameBackReferences(true),
Reid Klecknerd6a08d12013-05-14 20:30:42 +0000105 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikiee7e94c92013-05-14 21:31:46 +0000106 64) { }
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000107
108 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_,
109 const CXXDestructorDecl *D, CXXDtorType Type)
110 : Context(C), Out(Out_),
111 Structor(getStructor(D)), StructorType(Type),
David Blaikiee7e94c92013-05-14 21:31:46 +0000112 UseNameBackReferences(true),
Reid Klecknerd6a08d12013-05-14 20:30:42 +0000113 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikiee7e94c92013-05-14 21:31:46 +0000114 64) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000115
116 raw_ostream &getStream() const { return Out; }
117
118 void mangle(const NamedDecl *D, StringRef Prefix = "\01?");
119 void mangleName(const NamedDecl *ND);
David Majnemerc80eb462013-08-13 06:32:20 +0000120 void mangleDeclaration(const NamedDecl *ND);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000121 void mangleFunctionEncoding(const FunctionDecl *FD);
122 void mangleVariableEncoding(const VarDecl *VD);
123 void mangleNumber(int64_t Number);
124 void mangleNumber(const llvm::APSInt &Value);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000125 void mangleType(QualType T, SourceRange Range,
126 QualifierMangleMode QMM = QMM_Mangle);
Timur Iskhodzhanov635de282013-07-30 09:46:19 +0000127 void mangleFunctionType(const FunctionType *T, const FunctionDecl *D,
128 bool IsStructor, bool IsInstMethod);
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000129 void manglePostfix(const DeclContext *DC, bool NoFunction = false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000130
131private:
132 void disableBackReferences() { UseNameBackReferences = false; }
133 void mangleUnqualifiedName(const NamedDecl *ND) {
134 mangleUnqualifiedName(ND, ND->getDeclName());
135 }
136 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
137 void mangleSourceName(const IdentifierInfo *II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000138 void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc);
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000139 void mangleCXXDtorType(CXXDtorType T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000140 void mangleQualifiers(Qualifiers Quals, bool IsMember);
141 void manglePointerQualifiers(Qualifiers Quals);
142
143 void mangleUnscopedTemplateName(const TemplateDecl *ND);
144 void mangleTemplateInstantiationName(const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000145 const TemplateArgumentList &TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000146 void mangleObjCMethodName(const ObjCMethodDecl *MD);
147 void mangleLocalName(const FunctionDecl *FD);
148
149 void mangleArgumentType(QualType T, SourceRange Range);
150
151 // Declare manglers for every type class.
152#define ABSTRACT_TYPE(CLASS, PARENT)
153#define NON_CANONICAL_TYPE(CLASS, PARENT)
154#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \
155 SourceRange Range);
156#include "clang/AST/TypeNodes.def"
157#undef ABSTRACT_TYPE
158#undef NON_CANONICAL_TYPE
159#undef TYPE
160
David Majnemer02c44f02013-08-05 22:26:46 +0000161 void mangleType(const TagDecl *TD);
David Majnemer58e4cd02013-09-11 04:44:30 +0000162 void mangleDecayedArrayType(const ArrayType *T);
Reid Klecknerf21818d2013-06-24 19:21:52 +0000163 void mangleArrayType(const ArrayType *T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000164 void mangleFunctionClass(const FunctionDecl *FD);
Reid Klecknere3e686f2013-09-25 22:28:52 +0000165 void mangleCallingConvention(const FunctionType *T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000166 void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean);
167 void mangleExpression(const Expr *E);
168 void mangleThrowSpecification(const FunctionProtoType *T);
169
Reid Klecknerf16216c2013-03-20 01:40:23 +0000170 void mangleTemplateArgs(const TemplateDecl *TD,
171 const TemplateArgumentList &TemplateArgs);
David Majnemer309f6452013-08-27 08:21:25 +0000172 void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000173};
174
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +0000175/// MicrosoftMangleContextImpl - Overrides the default MangleContext for the
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000176/// Microsoft Visual C++ ABI.
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +0000177class MicrosoftMangleContextImpl : public MicrosoftMangleContext {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000178public:
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +0000179 MicrosoftMangleContextImpl(ASTContext &Context, DiagnosticsEngine &Diags)
180 : MicrosoftMangleContext(Context, Diags) {}
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000181 virtual bool shouldMangleDeclName(const NamedDecl *D);
182 virtual void mangleName(const NamedDecl *D, raw_ostream &Out);
183 virtual void mangleThunk(const CXXMethodDecl *MD,
184 const ThunkInfo &Thunk,
185 raw_ostream &);
186 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
187 const ThisAdjustment &ThisAdjustment,
188 raw_ostream &);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000189 virtual void mangleCXXVFTable(const CXXRecordDecl *Derived,
190 ArrayRef<const CXXRecordDecl *> BasePath,
191 raw_ostream &Out);
Reid Kleckner90633022013-06-19 15:20:38 +0000192 virtual void mangleCXXVBTable(const CXXRecordDecl *Derived,
193 ArrayRef<const CXXRecordDecl *> BasePath,
194 raw_ostream &Out);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000195 virtual void mangleCXXRTTI(QualType T, raw_ostream &);
196 virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
197 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
198 raw_ostream &);
199 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
200 raw_ostream &);
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000201 virtual void mangleReferenceTemporary(const VarDecl *, raw_ostream &);
202 virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out);
Reid Klecknerc5c6fa72013-09-10 20:43:12 +0000203 virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out);
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000204 virtual void mangleDynamicAtExitDestructor(const VarDecl *D,
205 raw_ostream &Out);
Reid Klecknerc5c6fa72013-09-10 20:43:12 +0000206
207private:
208 void mangleInitFiniStub(const VarDecl *D, raw_ostream &Out, char CharCode);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000209};
210
211}
212
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +0000213bool MicrosoftMangleContextImpl::shouldMangleDeclName(const NamedDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000214 // In C, functions with no attributes never need to be mangled. Fastpath them.
215 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
216 return false;
217
218 // Any decl can be declared with __asm("foo") on it, and this takes precedence
219 // over all other naming in the .o file.
220 if (D->hasAttr<AsmLabelAttr>())
221 return true;
222
David Majnemercab7dad2013-09-13 09:03:14 +0000223 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
224 LanguageLinkage L = FD->getLanguageLinkage();
225 // Overloadable functions need mangling.
226 if (FD->hasAttr<OverloadableAttr>())
227 return true;
228
David Majnemere9f6f332013-09-16 22:44:20 +0000229 // The ABI expects that we would never mangle "typical" user-defined entry
230 // points regardless of visibility or freestanding-ness.
231 //
232 // N.B. This is distinct from asking about "main". "main" has a lot of
233 // special rules associated with it in the standard while these
234 // user-defined entry points are outside of the purview of the standard.
235 // For example, there can be only one definition for "main" in a standards
236 // compliant program; however nothing forbids the existence of wmain and
237 // WinMain in the same translation unit.
238 if (FD->isMSVCRTEntryPoint())
David Majnemercab7dad2013-09-13 09:03:14 +0000239 return false;
240
241 // C++ functions and those whose names are not a simple identifier need
242 // mangling.
243 if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage)
244 return true;
245
246 // C functions are not mangled.
247 if (L == CLanguageLinkage)
248 return false;
249 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000250
251 // Otherwise, no mangling is done outside C++ mode.
252 if (!getASTContext().getLangOpts().CPlusPlus)
253 return false;
254
David Majnemercab7dad2013-09-13 09:03:14 +0000255 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
256 // C variables are not mangled.
257 if (VD->isExternC())
258 return false;
259
260 // Variables at global scope with non-internal linkage are not mangled.
261 const DeclContext *DC = getEffectiveDeclContext(D);
262 // Check for extern variable declared locally.
263 if (DC->isFunctionOrMethod() && D->hasLinkage())
264 while (!DC->isNamespace() && !DC->isTranslationUnit())
265 DC = getEffectiveParentContext(DC);
266
267 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage &&
268 !isa<VarTemplateSpecializationDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000269 return false;
270 }
271
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000272 return true;
273}
274
275void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
276 StringRef Prefix) {
277 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
278 // Therefore it's really important that we don't decorate the
279 // name with leading underscores or leading/trailing at signs. So, by
280 // default, we emit an asm marker at the start so we get the name right.
281 // Callers can override this with a custom prefix.
282
283 // Any decl can be declared with __asm("foo") on it, and this takes precedence
284 // over all other naming in the .o file.
285 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
286 // If we have an asm name, then we use it as the mangling.
287 Out << '\01' << ALA->getLabel();
288 return;
289 }
290
291 // <mangled-name> ::= ? <name> <type-encoding>
292 Out << Prefix;
293 mangleName(D);
294 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
295 mangleFunctionEncoding(FD);
296 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
297 mangleVariableEncoding(VD);
298 else {
299 // TODO: Fields? Can MSVC even mangle them?
300 // Issue a diagnostic for now.
301 DiagnosticsEngine &Diags = Context.getDiags();
302 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
303 "cannot mangle this declaration yet");
304 Diags.Report(D->getLocation(), DiagID)
305 << D->getSourceRange();
306 }
307}
308
309void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
310 // <type-encoding> ::= <function-class> <function-type>
311
Reid Klecknerf21818d2013-06-24 19:21:52 +0000312 // Since MSVC operates on the type as written and not the canonical type, it
313 // actually matters which decl we have here. MSVC appears to choose the
314 // first, since it is most likely to be the declaration in a header file.
315 FD = FD->getFirstDeclaration();
316
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000317 // We should never ever see a FunctionNoProtoType at this point.
318 // We don't even know how to mangle their types anyway :).
Reid Klecknerf21818d2013-06-24 19:21:52 +0000319 TypeSourceInfo *TSI = FD->getTypeSourceInfo();
320 QualType T = TSI ? TSI->getType() : FD->getType();
321 const FunctionProtoType *FT = T->castAs<FunctionProtoType>();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000322
323 bool InStructor = false, InInstMethod = false;
324 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
325 if (MD) {
326 if (MD->isInstance())
327 InInstMethod = true;
328 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
329 InStructor = true;
330 }
331
David Majnemercab7dad2013-09-13 09:03:14 +0000332 // extern "C" functions can hold entities that must be mangled.
333 // As it stands, these functions still need to get expressed in the full
334 // external name. They have their class and type omitted, replaced with '9'.
335 if (Context.shouldMangleDeclName(FD)) {
336 // First, the function class.
337 mangleFunctionClass(FD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000338
David Majnemercab7dad2013-09-13 09:03:14 +0000339 mangleFunctionType(FT, FD, InStructor, InInstMethod);
340 } else
341 Out << '9';
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000342}
343
344void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
345 // <type-encoding> ::= <storage-class> <variable-type>
346 // <storage-class> ::= 0 # private static member
347 // ::= 1 # protected static member
348 // ::= 2 # public static member
349 // ::= 3 # global
350 // ::= 4 # static local
351
352 // The first character in the encoding (after the name) is the storage class.
353 if (VD->isStaticDataMember()) {
354 // If it's a static member, it also encodes the access level.
355 switch (VD->getAccess()) {
356 default:
357 case AS_private: Out << '0'; break;
358 case AS_protected: Out << '1'; break;
359 case AS_public: Out << '2'; break;
360 }
361 }
362 else if (!VD->isStaticLocal())
363 Out << '3';
364 else
365 Out << '4';
366 // Now mangle the type.
367 // <variable-type> ::= <type> <cvr-qualifiers>
368 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
369 // Pointers and references are odd. The type of 'int * const foo;' gets
370 // mangled as 'QAHA' instead of 'PAHB', for example.
371 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
372 QualType Ty = TL.getType();
David Majnemer1c7a4092013-08-15 08:13:23 +0000373 if (Ty->isPointerType() || Ty->isReferenceType() ||
374 Ty->isMemberPointerType()) {
David Majnemer17ffbd02013-08-09 05:56:24 +0000375 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000376 if (PointersAre64Bit)
377 Out << 'E';
378 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) {
379 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true);
380 // Member pointers are suffixed with a back reference to the member
381 // pointer's class name.
382 mangleName(MPT->getClass()->getAsCXXRecordDecl());
383 } else
384 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
David Majnemer17ffbd02013-08-09 05:56:24 +0000385 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000386 // Global arrays are funny, too.
David Majnemer58e4cd02013-09-11 04:44:30 +0000387 mangleDecayedArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000388 if (AT->getElementType()->isArrayType())
389 Out << 'A';
390 else
391 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000392 } else {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000393 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000394 mangleQualifiers(Ty.getLocalQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000395 }
396}
397
398void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
399 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
400 const DeclContext *DC = ND->getDeclContext();
401
402 // Always start with the unqualified name.
403 mangleUnqualifiedName(ND);
404
405 // If this is an extern variable declared locally, the relevant DeclContext
406 // is that of the containing namespace, or the translation unit.
407 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
408 while (!DC->isNamespace() && !DC->isTranslationUnit())
409 DC = DC->getParent();
410
411 manglePostfix(DC);
412
413 // Terminate the whole name with an '@'.
414 Out << '@';
415}
416
417void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
418 llvm::APSInt APSNumber(/*BitWidth=*/64, /*isUnsigned=*/false);
419 APSNumber = Number;
420 mangleNumber(APSNumber);
421}
422
423void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
424 // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
425 // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
426 // ::= [?] @ # 0 (alternate mangling, not emitted by VC)
427 if (Value.isSigned() && Value.isNegative()) {
428 Out << '?';
429 mangleNumber(llvm::APSInt(Value.abs()));
430 return;
431 }
432 llvm::APSInt Temp(Value);
433 // There's a special shorter mangling for 0, but Microsoft
434 // chose not to use it. Instead, 0 gets mangled as "A@". Oh well...
435 if (Value.uge(1) && Value.ule(10)) {
436 --Temp;
437 Temp.print(Out, false);
438 } else {
439 // We have to build up the encoding in reverse order, so it will come
440 // out right when we write it out.
441 char Encoding[64];
442 char *EndPtr = Encoding+sizeof(Encoding);
443 char *CurPtr = EndPtr;
444 llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned());
445 NibbleMask = 0xf;
446 do {
447 *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf);
448 Temp = Temp.lshr(4);
449 } while (Temp != 0);
450 Out.write(CurPtr, EndPtr-CurPtr);
451 Out << '@';
452 }
453}
454
455static const TemplateDecl *
Reid Klecknerf16216c2013-03-20 01:40:23 +0000456isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000457 // Check if we have a function template.
458 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
459 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000460 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000461 return TD;
462 }
463 }
464
465 // Check if we have a class template.
466 if (const ClassTemplateSpecializationDecl *Spec =
Reid Klecknerf16216c2013-03-20 01:40:23 +0000467 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
468 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000469 return Spec->getSpecializedTemplate();
470 }
471
472 return 0;
473}
474
475void
476MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
477 DeclarationName Name) {
478 // <unqualified-name> ::= <operator-name>
479 // ::= <ctor-dtor-name>
480 // ::= <source-name>
481 // ::= <template-name>
Reid Klecknerf16216c2013-03-20 01:40:23 +0000482
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000483 // Check if we have a template.
Reid Klecknerf16216c2013-03-20 01:40:23 +0000484 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000485 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Kleckner3be37d12013-07-13 00:43:39 +0000486 // Function templates aren't considered for name back referencing. This
487 // makes sense since function templates aren't likely to occur multiple
488 // times in a symbol.
489 // FIXME: Test alias template mangling with MSVC 2013.
490 if (!isa<ClassTemplateDecl>(TD)) {
491 mangleTemplateInstantiationName(TD, *TemplateArgs);
492 return;
493 }
494
495 // We have a class template.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000496 // Here comes the tricky thing: if we need to mangle something like
497 // void foo(A::X<Y>, B::X<Y>),
498 // the X<Y> part is aliased. However, if you need to mangle
499 // void foo(A::X<A::Y>, A::X<B::Y>),
500 // the A::X<> part is not aliased.
501 // That said, from the mangler's perspective we have a structure like this:
502 // namespace[s] -> type[ -> template-parameters]
503 // but from the Clang perspective we have
504 // type [ -> template-parameters]
505 // \-> namespace[s]
506 // What we do is we create a new mangler, mangle the same type (without
507 // a namespace suffix) using the extra mangler with back references
508 // disabled (to avoid infinite recursion) and then use the mangled type
509 // name as a key to check the mangling of different types for aliasing.
510
511 std::string BackReferenceKey;
512 BackRefMap::iterator Found;
513 if (UseNameBackReferences) {
514 llvm::raw_string_ostream Stream(BackReferenceKey);
515 MicrosoftCXXNameMangler Extra(Context, Stream);
516 Extra.disableBackReferences();
517 Extra.mangleUnqualifiedName(ND, Name);
518 Stream.flush();
519
520 Found = NameBackReferences.find(BackReferenceKey);
521 }
522 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000523 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000524 if (UseNameBackReferences && NameBackReferences.size() < 10) {
525 size_t Size = NameBackReferences.size();
526 NameBackReferences[BackReferenceKey] = Size;
527 }
528 } else {
529 Out << Found->second;
530 }
531 return;
532 }
533
534 switch (Name.getNameKind()) {
535 case DeclarationName::Identifier: {
536 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
537 mangleSourceName(II);
538 break;
539 }
540
541 // Otherwise, an anonymous entity. We must have a declaration.
542 assert(ND && "mangling empty name without declaration");
543
544 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
545 if (NS->isAnonymousNamespace()) {
546 Out << "?A@";
547 break;
548 }
549 }
550
551 // We must have an anonymous struct.
552 const TagDecl *TD = cast<TagDecl>(ND);
553 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
554 assert(TD->getDeclContext() == D->getDeclContext() &&
555 "Typedef should not be in another decl context!");
556 assert(D->getDeclName().getAsIdentifierInfo() &&
557 "Typedef was not named!");
558 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
559 break;
560 }
561
David Majnemeraa824612013-09-17 23:57:10 +0000562 if (TD->hasDeclaratorForAnonDecl())
563 // Anonymous types with no tag or typedef get the name of their
564 // declarator mangled in.
565 Out << "<unnamed-type-" << TD->getDeclaratorForAnonDecl()->getName()
566 << ">@";
567 else
568 // Anonymous types with no tag, no typedef, or declarator get
569 // '<unnamed-tag>@'.
570 Out << "<unnamed-tag>@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000571 break;
572 }
573
574 case DeclarationName::ObjCZeroArgSelector:
575 case DeclarationName::ObjCOneArgSelector:
576 case DeclarationName::ObjCMultiArgSelector:
577 llvm_unreachable("Can't mangle Objective-C selector names here!");
578
579 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000580 if (ND == Structor) {
581 assert(StructorType == Ctor_Complete &&
582 "Should never be asked to mangle a ctor other than complete");
583 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000584 Out << "?0";
585 break;
586
587 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000588 if (ND == Structor)
589 // If the named decl is the C++ destructor we're mangling,
590 // use the type we were given.
591 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
592 else
Reid Klecknera4130ba2013-07-22 13:51:44 +0000593 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000594 // class with a destructor is declared within a destructor.
Reid Klecknera4130ba2013-07-22 13:51:44 +0000595 mangleCXXDtorType(Dtor_Base);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000596 break;
597
598 case DeclarationName::CXXConversionFunctionName:
599 // <operator-name> ::= ?B # (cast)
600 // The target type is encoded as the return type.
601 Out << "?B";
602 break;
603
604 case DeclarationName::CXXOperatorName:
605 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
606 break;
607
608 case DeclarationName::CXXLiteralOperatorName: {
609 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
610 DiagnosticsEngine Diags = Context.getDiags();
611 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
612 "cannot mangle this literal operator yet");
613 Diags.Report(ND->getLocation(), DiagID);
614 break;
615 }
616
617 case DeclarationName::CXXUsingDirective:
618 llvm_unreachable("Can't mangle a using directive name!");
619 }
620}
621
622void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
623 bool NoFunction) {
624 // <postfix> ::= <unqualified-name> [<postfix>]
625 // ::= <substitution> [<postfix>]
626
627 if (!DC) return;
628
629 while (isa<LinkageSpecDecl>(DC))
630 DC = DC->getParent();
631
632 if (DC->isTranslationUnit())
633 return;
634
635 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedmane5798892013-07-10 01:13:27 +0000636 DiagnosticsEngine Diags = Context.getDiags();
637 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
638 "cannot mangle a local inside this block yet");
639 Diags.Report(BD->getLocation(), DiagID);
640
641 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
642 // for how this should be done.
643 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000644 Out << '@';
645 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000646 } else if (isa<CapturedDecl>(DC)) {
647 // Skip CapturedDecl context.
648 manglePostfix(DC->getParent(), NoFunction);
649 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000650 }
651
652 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
653 return;
654 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
655 mangleObjCMethodName(Method);
656 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
657 mangleLocalName(Func);
658 else {
659 mangleUnqualifiedName(cast<NamedDecl>(DC));
660 manglePostfix(DC->getParent(), NoFunction);
661 }
662}
663
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000664void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000665 // Microsoft uses the names on the case labels for these dtor variants. Clang
666 // uses the Itanium terminology internally. Everything in this ABI delegates
667 // towards the base dtor.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000668 switch (T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000669 // <operator-name> ::= ?1 # destructor
670 case Dtor_Base: Out << "?1"; return;
671 // <operator-name> ::= ?_D # vbase destructor
672 case Dtor_Complete: Out << "?_D"; return;
673 // <operator-name> ::= ?_G # scalar deleting destructor
674 case Dtor_Deleting: Out << "?_G"; return;
675 // <operator-name> ::= ?_E # vector deleting destructor
676 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
677 // it.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000678 }
679 llvm_unreachable("Unsupported dtor type?");
680}
681
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000682void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
683 SourceLocation Loc) {
684 switch (OO) {
685 // ?0 # constructor
686 // ?1 # destructor
687 // <operator-name> ::= ?2 # new
688 case OO_New: Out << "?2"; break;
689 // <operator-name> ::= ?3 # delete
690 case OO_Delete: Out << "?3"; break;
691 // <operator-name> ::= ?4 # =
692 case OO_Equal: Out << "?4"; break;
693 // <operator-name> ::= ?5 # >>
694 case OO_GreaterGreater: Out << "?5"; break;
695 // <operator-name> ::= ?6 # <<
696 case OO_LessLess: Out << "?6"; break;
697 // <operator-name> ::= ?7 # !
698 case OO_Exclaim: Out << "?7"; break;
699 // <operator-name> ::= ?8 # ==
700 case OO_EqualEqual: Out << "?8"; break;
701 // <operator-name> ::= ?9 # !=
702 case OO_ExclaimEqual: Out << "?9"; break;
703 // <operator-name> ::= ?A # []
704 case OO_Subscript: Out << "?A"; break;
705 // ?B # conversion
706 // <operator-name> ::= ?C # ->
707 case OO_Arrow: Out << "?C"; break;
708 // <operator-name> ::= ?D # *
709 case OO_Star: Out << "?D"; break;
710 // <operator-name> ::= ?E # ++
711 case OO_PlusPlus: Out << "?E"; break;
712 // <operator-name> ::= ?F # --
713 case OO_MinusMinus: Out << "?F"; break;
714 // <operator-name> ::= ?G # -
715 case OO_Minus: Out << "?G"; break;
716 // <operator-name> ::= ?H # +
717 case OO_Plus: Out << "?H"; break;
718 // <operator-name> ::= ?I # &
719 case OO_Amp: Out << "?I"; break;
720 // <operator-name> ::= ?J # ->*
721 case OO_ArrowStar: Out << "?J"; break;
722 // <operator-name> ::= ?K # /
723 case OO_Slash: Out << "?K"; break;
724 // <operator-name> ::= ?L # %
725 case OO_Percent: Out << "?L"; break;
726 // <operator-name> ::= ?M # <
727 case OO_Less: Out << "?M"; break;
728 // <operator-name> ::= ?N # <=
729 case OO_LessEqual: Out << "?N"; break;
730 // <operator-name> ::= ?O # >
731 case OO_Greater: Out << "?O"; break;
732 // <operator-name> ::= ?P # >=
733 case OO_GreaterEqual: Out << "?P"; break;
734 // <operator-name> ::= ?Q # ,
735 case OO_Comma: Out << "?Q"; break;
736 // <operator-name> ::= ?R # ()
737 case OO_Call: Out << "?R"; break;
738 // <operator-name> ::= ?S # ~
739 case OO_Tilde: Out << "?S"; break;
740 // <operator-name> ::= ?T # ^
741 case OO_Caret: Out << "?T"; break;
742 // <operator-name> ::= ?U # |
743 case OO_Pipe: Out << "?U"; break;
744 // <operator-name> ::= ?V # &&
745 case OO_AmpAmp: Out << "?V"; break;
746 // <operator-name> ::= ?W # ||
747 case OO_PipePipe: Out << "?W"; break;
748 // <operator-name> ::= ?X # *=
749 case OO_StarEqual: Out << "?X"; break;
750 // <operator-name> ::= ?Y # +=
751 case OO_PlusEqual: Out << "?Y"; break;
752 // <operator-name> ::= ?Z # -=
753 case OO_MinusEqual: Out << "?Z"; break;
754 // <operator-name> ::= ?_0 # /=
755 case OO_SlashEqual: Out << "?_0"; break;
756 // <operator-name> ::= ?_1 # %=
757 case OO_PercentEqual: Out << "?_1"; break;
758 // <operator-name> ::= ?_2 # >>=
759 case OO_GreaterGreaterEqual: Out << "?_2"; break;
760 // <operator-name> ::= ?_3 # <<=
761 case OO_LessLessEqual: Out << "?_3"; break;
762 // <operator-name> ::= ?_4 # &=
763 case OO_AmpEqual: Out << "?_4"; break;
764 // <operator-name> ::= ?_5 # |=
765 case OO_PipeEqual: Out << "?_5"; break;
766 // <operator-name> ::= ?_6 # ^=
767 case OO_CaretEqual: Out << "?_6"; break;
768 // ?_7 # vftable
769 // ?_8 # vbtable
770 // ?_9 # vcall
771 // ?_A # typeof
772 // ?_B # local static guard
773 // ?_C # string
774 // ?_D # vbase destructor
775 // ?_E # vector deleting destructor
776 // ?_F # default constructor closure
777 // ?_G # scalar deleting destructor
778 // ?_H # vector constructor iterator
779 // ?_I # vector destructor iterator
780 // ?_J # vector vbase constructor iterator
781 // ?_K # virtual displacement map
782 // ?_L # eh vector constructor iterator
783 // ?_M # eh vector destructor iterator
784 // ?_N # eh vector vbase constructor iterator
785 // ?_O # copy constructor closure
786 // ?_P<name> # udt returning <name>
787 // ?_Q # <unknown>
788 // ?_R0 # RTTI Type Descriptor
789 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
790 // ?_R2 # RTTI Base Class Array
791 // ?_R3 # RTTI Class Hierarchy Descriptor
792 // ?_R4 # RTTI Complete Object Locator
793 // ?_S # local vftable
794 // ?_T # local vftable constructor closure
795 // <operator-name> ::= ?_U # new[]
796 case OO_Array_New: Out << "?_U"; break;
797 // <operator-name> ::= ?_V # delete[]
798 case OO_Array_Delete: Out << "?_V"; break;
799
800 case OO_Conditional: {
801 DiagnosticsEngine &Diags = Context.getDiags();
802 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
803 "cannot mangle this conditional operator yet");
804 Diags.Report(Loc, DiagID);
805 break;
806 }
807
808 case OO_None:
809 case NUM_OVERLOADED_OPERATORS:
810 llvm_unreachable("Not an overloaded operator");
811 }
812}
813
814void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
815 // <source name> ::= <identifier> @
816 std::string key = II->getNameStart();
817 BackRefMap::iterator Found;
818 if (UseNameBackReferences)
819 Found = NameBackReferences.find(key);
820 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
821 Out << II->getName() << '@';
822 if (UseNameBackReferences && NameBackReferences.size() < 10) {
823 size_t Size = NameBackReferences.size();
824 NameBackReferences[key] = Size;
825 }
826 } else {
827 Out << Found->second;
828 }
829}
830
831void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
832 Context.mangleObjCMethodName(MD, Out);
833}
834
835// Find out how many function decls live above this one and return an integer
836// suitable for use as the number in a numbered anonymous scope.
837// TODO: Memoize.
838static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
839 const DeclContext *DC = FD->getParent();
840 int level = 1;
841
842 while (DC && !DC->isTranslationUnit()) {
843 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
844 DC = DC->getParent();
845 }
846
847 return 2*level;
848}
849
850void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
851 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
852 // <numbered-anonymous-scope> ::= ? <number>
853 // Even though the name is rendered in reverse order (e.g.
854 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
855 // innermost. So a method bar in class C local to function foo gets mangled
856 // as something like:
857 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
858 // This is more apparent when you have a type nested inside a method of a
859 // type nested inside a function. A method baz in class D local to method
860 // bar of class C local to function foo gets mangled as:
861 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
862 // This scheme is general enough to support GCC-style nested
863 // functions. You could have a method baz of class C inside a function bar
864 // inside a function foo, like so:
865 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
866 int NestLevel = getLocalNestingLevel(FD);
867 Out << '?';
868 mangleNumber(NestLevel);
869 Out << '?';
870 mangle(FD, "?");
871}
872
873void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
874 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000875 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000876 // <template-name> ::= <unscoped-template-name> <template-args>
877 // ::= <substitution>
878 // Always start with the unqualified name.
879
880 // Templates have their own context for back references.
881 ArgBackRefMap OuterArgsContext;
882 BackRefMap OuterTemplateContext;
883 NameBackReferences.swap(OuterTemplateContext);
884 TypeBackReferences.swap(OuterArgsContext);
885
886 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000887 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000888
889 // Restore the previous back reference contexts.
890 NameBackReferences.swap(OuterTemplateContext);
891 TypeBackReferences.swap(OuterArgsContext);
892}
893
894void
895MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
896 // <unscoped-template-name> ::= ?$ <unqualified-name>
897 Out << "?$";
898 mangleUnqualifiedName(TD);
899}
900
901void
902MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
903 bool IsBoolean) {
904 // <integer-literal> ::= $0 <number>
905 Out << "$0";
906 // Make sure booleans are encoded as 0/1.
907 if (IsBoolean && Value.getBoolValue())
908 mangleNumber(1);
909 else
910 mangleNumber(Value);
911}
912
913void
914MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
915 // See if this is a constant expression.
916 llvm::APSInt Value;
917 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
918 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
919 return;
920 }
921
David Majnemerc80eb462013-08-13 06:32:20 +0000922 const CXXUuidofExpr *UE = 0;
923 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
924 if (UO->getOpcode() == UO_AddrOf)
925 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
926 } else
927 UE = dyn_cast<CXXUuidofExpr>(E);
928
929 if (UE) {
930 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
931 // const __s_GUID _GUID_{lower case UUID with underscores}
932 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
933 std::string Name = "_GUID_" + Uuid.lower();
934 std::replace(Name.begin(), Name.end(), '-', '_');
935
David Majnemer26314e12013-08-13 09:17:25 +0000936 // If we had to peek through an address-of operator, treat this like we are
David Majnemerc80eb462013-08-13 06:32:20 +0000937 // dealing with a pointer type. Otherwise, treat it like a const reference.
938 //
939 // N.B. This matches up with the handling of TemplateArgument::Declaration
940 // in mangleTemplateArg
941 if (UE == E)
942 Out << "$E?";
943 else
944 Out << "$1?";
945 Out << Name << "@@3U__s_GUID@@B";
946 return;
947 }
948
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000949 // As bad as this diagnostic is, it's better than crashing.
950 DiagnosticsEngine &Diags = Context.getDiags();
951 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
952 "cannot yet mangle expression type %0");
953 Diags.Report(E->getExprLoc(), DiagID)
954 << E->getStmtClassName() << E->getSourceRange();
955}
956
957void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000958MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
959 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000960 // <template-args> ::= {<type> | <integer-literal>}+ @
961 unsigned NumTemplateArgs = TemplateArgs.size();
962 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000963 const TemplateArgument &TA = TemplateArgs[i];
David Majnemer309f6452013-08-27 08:21:25 +0000964 mangleTemplateArg(TD, TA);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000965 }
966 Out << '@';
967}
968
Reid Kleckner5d90d182013-07-02 18:10:07 +0000969void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
David Majnemer309f6452013-08-27 08:21:25 +0000970 const TemplateArgument &TA) {
Reid Kleckner5d90d182013-07-02 18:10:07 +0000971 switch (TA.getKind()) {
972 case TemplateArgument::Null:
973 llvm_unreachable("Can't mangle null template arguments!");
David Majnemer309f6452013-08-27 08:21:25 +0000974 case TemplateArgument::TemplateExpansion:
975 llvm_unreachable("Can't mangle template expansion arguments!");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000976 case TemplateArgument::Type: {
977 QualType T = TA.getAsType();
978 mangleType(T, SourceRange(), QMM_Escape);
979 break;
980 }
David Majnemerf2081f62013-08-13 01:25:35 +0000981 case TemplateArgument::Declaration: {
982 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl());
983 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000984 break;
David Majnemerf2081f62013-08-13 01:25:35 +0000985 }
Reid Kleckner5d90d182013-07-02 18:10:07 +0000986 case TemplateArgument::Integral:
987 mangleIntegerLiteral(TA.getAsIntegral(),
988 TA.getIntegralType()->isBooleanType());
989 break;
David Majnemer7802fc92013-08-05 21:33:59 +0000990 case TemplateArgument::NullPtr:
991 Out << "$0A@";
992 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000993 case TemplateArgument::Expression:
994 mangleExpression(TA.getAsExpr());
995 break;
996 case TemplateArgument::Pack:
997 // Unlike Itanium, there is no character code to indicate an argument pack.
Reid Kleckner5d90d182013-07-02 18:10:07 +0000998 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
999 I != E; ++I)
David Majnemer309f6452013-08-27 08:21:25 +00001000 mangleTemplateArg(TD, *I);
Reid Kleckner5d90d182013-07-02 18:10:07 +00001001 break;
1002 case TemplateArgument::Template:
David Majnemer02c44f02013-08-05 22:26:46 +00001003 mangleType(cast<TagDecl>(
1004 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
1005 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +00001006 }
1007}
1008
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001009void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
1010 bool IsMember) {
1011 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
1012 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
1013 // 'I' means __restrict (32/64-bit).
1014 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
1015 // keyword!
1016 // <base-cvr-qualifiers> ::= A # near
1017 // ::= B # near const
1018 // ::= C # near volatile
1019 // ::= D # near const volatile
1020 // ::= E # far (16-bit)
1021 // ::= F # far const (16-bit)
1022 // ::= G # far volatile (16-bit)
1023 // ::= H # far const volatile (16-bit)
1024 // ::= I # huge (16-bit)
1025 // ::= J # huge const (16-bit)
1026 // ::= K # huge volatile (16-bit)
1027 // ::= L # huge const volatile (16-bit)
1028 // ::= M <basis> # based
1029 // ::= N <basis> # based const
1030 // ::= O <basis> # based volatile
1031 // ::= P <basis> # based const volatile
1032 // ::= Q # near member
1033 // ::= R # near const member
1034 // ::= S # near volatile member
1035 // ::= T # near const volatile member
1036 // ::= U # far member (16-bit)
1037 // ::= V # far const member (16-bit)
1038 // ::= W # far volatile member (16-bit)
1039 // ::= X # far const volatile member (16-bit)
1040 // ::= Y # huge member (16-bit)
1041 // ::= Z # huge const member (16-bit)
1042 // ::= 0 # huge volatile member (16-bit)
1043 // ::= 1 # huge const volatile member (16-bit)
1044 // ::= 2 <basis> # based member
1045 // ::= 3 <basis> # based const member
1046 // ::= 4 <basis> # based volatile member
1047 // ::= 5 <basis> # based const volatile member
1048 // ::= 6 # near function (pointers only)
1049 // ::= 7 # far function (pointers only)
1050 // ::= 8 # near method (pointers only)
1051 // ::= 9 # far method (pointers only)
1052 // ::= _A <basis> # based function (pointers only)
1053 // ::= _B <basis> # based function (far?) (pointers only)
1054 // ::= _C <basis> # based method (pointers only)
1055 // ::= _D <basis> # based method (far?) (pointers only)
1056 // ::= _E # block (Clang)
1057 // <basis> ::= 0 # __based(void)
1058 // ::= 1 # __based(segment)?
1059 // ::= 2 <name> # __based(name)
1060 // ::= 3 # ?
1061 // ::= 4 # ?
1062 // ::= 5 # not really based
1063 bool HasConst = Quals.hasConst(),
1064 HasVolatile = Quals.hasVolatile();
David Majnemerc0e64f32013-08-05 22:43:06 +00001065
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001066 if (!IsMember) {
1067 if (HasConst && HasVolatile) {
1068 Out << 'D';
1069 } else if (HasVolatile) {
1070 Out << 'C';
1071 } else if (HasConst) {
1072 Out << 'B';
1073 } else {
1074 Out << 'A';
1075 }
1076 } else {
1077 if (HasConst && HasVolatile) {
1078 Out << 'T';
1079 } else if (HasVolatile) {
1080 Out << 'S';
1081 } else if (HasConst) {
1082 Out << 'R';
1083 } else {
1084 Out << 'Q';
1085 }
1086 }
1087
1088 // FIXME: For now, just drop all extension qualifiers on the floor.
1089}
1090
1091void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1092 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1093 // ::= Q # const
1094 // ::= R # volatile
1095 // ::= S # const volatile
1096 bool HasConst = Quals.hasConst(),
1097 HasVolatile = Quals.hasVolatile();
1098 if (HasConst && HasVolatile) {
1099 Out << 'S';
1100 } else if (HasVolatile) {
1101 Out << 'R';
1102 } else if (HasConst) {
1103 Out << 'Q';
1104 } else {
1105 Out << 'P';
1106 }
1107}
1108
1109void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1110 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001111 // MSVC will backreference two canonically equivalent types that have slightly
1112 // different manglings when mangled alone.
David Majnemer58e4cd02013-09-11 04:44:30 +00001113
1114 // Decayed types do not match up with non-decayed versions of the same type.
1115 //
1116 // e.g.
1117 // void (*x)(void) will not form a backreference with void x(void)
1118 void *TypePtr;
1119 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1120 TypePtr = DT->getOriginalType().getCanonicalType().getAsOpaquePtr();
1121 // If the original parameter was textually written as an array,
1122 // instead treat the decayed parameter like it's const.
1123 //
1124 // e.g.
1125 // int [] -> int * const
1126 if (DT->getOriginalType()->isArrayType())
1127 T = T.withConst();
1128 } else
1129 TypePtr = T.getCanonicalType().getAsOpaquePtr();
1130
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001131 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1132
1133 if (Found == TypeBackReferences.end()) {
1134 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1135
David Majnemer58e4cd02013-09-11 04:44:30 +00001136 mangleType(T, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001137
1138 // See if it's worth creating a back reference.
1139 // Only types longer than 1 character are considered
1140 // and only 10 back references slots are available:
1141 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1142 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1143 size_t Size = TypeBackReferences.size();
1144 TypeBackReferences[TypePtr] = Size;
1145 }
1146 } else {
1147 Out << Found->second;
1148 }
1149}
1150
1151void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001152 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001153 // Don't use the canonical types. MSVC includes things like 'const' on
1154 // pointer arguments to function pointers that canonicalization strips away.
1155 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001156 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001157 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1158 // If there were any Quals, getAsArrayType() pushed them onto the array
1159 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001160 if (QMM == QMM_Mangle)
1161 Out << 'A';
1162 else if (QMM == QMM_Escape || QMM == QMM_Result)
1163 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001164 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001165 return;
1166 }
1167
1168 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1169 T->isBlockPointerType();
1170
1171 switch (QMM) {
1172 case QMM_Drop:
1173 break;
1174 case QMM_Mangle:
1175 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1176 Out << '6';
1177 mangleFunctionType(FT, 0, false, false);
1178 return;
1179 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001180 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001181 break;
1182 case QMM_Escape:
1183 if (!IsPointer && Quals) {
1184 Out << "$$C";
1185 mangleQualifiers(Quals, false);
1186 }
1187 break;
1188 case QMM_Result:
1189 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1190 Out << '?';
1191 mangleQualifiers(Quals, false);
1192 }
1193 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001194 }
1195
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001196 // We have to mangle these now, while we still have enough information.
1197 if (IsPointer)
1198 manglePointerQualifiers(Quals);
1199 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001200
1201 switch (ty->getTypeClass()) {
1202#define ABSTRACT_TYPE(CLASS, PARENT)
1203#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1204 case Type::CLASS: \
1205 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1206 return;
1207#define TYPE(CLASS, PARENT) \
1208 case Type::CLASS: \
1209 mangleType(cast<CLASS##Type>(ty), Range); \
1210 break;
1211#include "clang/AST/TypeNodes.def"
1212#undef ABSTRACT_TYPE
1213#undef NON_CANONICAL_TYPE
1214#undef TYPE
1215 }
1216}
1217
1218void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1219 SourceRange Range) {
1220 // <type> ::= <builtin-type>
1221 // <builtin-type> ::= X # void
1222 // ::= C # signed char
1223 // ::= D # char
1224 // ::= E # unsigned char
1225 // ::= F # short
1226 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1227 // ::= H # int
1228 // ::= I # unsigned int
1229 // ::= J # long
1230 // ::= K # unsigned long
1231 // L # <none>
1232 // ::= M # float
1233 // ::= N # double
1234 // ::= O # long double (__float80 is mangled differently)
1235 // ::= _J # long long, __int64
1236 // ::= _K # unsigned long long, __int64
1237 // ::= _L # __int128
1238 // ::= _M # unsigned __int128
1239 // ::= _N # bool
1240 // _O # <array in parameter>
1241 // ::= _T # __float80 (Intel)
1242 // ::= _W # wchar_t
1243 // ::= _Z # __float80 (Digital Mars)
1244 switch (T->getKind()) {
1245 case BuiltinType::Void: Out << 'X'; break;
1246 case BuiltinType::SChar: Out << 'C'; break;
1247 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1248 case BuiltinType::UChar: Out << 'E'; break;
1249 case BuiltinType::Short: Out << 'F'; break;
1250 case BuiltinType::UShort: Out << 'G'; break;
1251 case BuiltinType::Int: Out << 'H'; break;
1252 case BuiltinType::UInt: Out << 'I'; break;
1253 case BuiltinType::Long: Out << 'J'; break;
1254 case BuiltinType::ULong: Out << 'K'; break;
1255 case BuiltinType::Float: Out << 'M'; break;
1256 case BuiltinType::Double: Out << 'N'; break;
1257 // TODO: Determine size and mangle accordingly
1258 case BuiltinType::LongDouble: Out << 'O'; break;
1259 case BuiltinType::LongLong: Out << "_J"; break;
1260 case BuiltinType::ULongLong: Out << "_K"; break;
1261 case BuiltinType::Int128: Out << "_L"; break;
1262 case BuiltinType::UInt128: Out << "_M"; break;
1263 case BuiltinType::Bool: Out << "_N"; break;
1264 case BuiltinType::WChar_S:
1265 case BuiltinType::WChar_U: Out << "_W"; break;
1266
1267#define BUILTIN_TYPE(Id, SingletonId)
1268#define PLACEHOLDER_TYPE(Id, SingletonId) \
1269 case BuiltinType::Id:
1270#include "clang/AST/BuiltinTypes.def"
1271 case BuiltinType::Dependent:
1272 llvm_unreachable("placeholder types shouldn't get to name mangling");
1273
1274 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1275 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1276 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001277
1278 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1279 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1280 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1281 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1282 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1283 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001284 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001285 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001286
1287 case BuiltinType::NullPtr: Out << "$$T"; break;
1288
1289 case BuiltinType::Char16:
1290 case BuiltinType::Char32:
1291 case BuiltinType::Half: {
1292 DiagnosticsEngine &Diags = Context.getDiags();
1293 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1294 "cannot mangle this built-in %0 type yet");
1295 Diags.Report(Range.getBegin(), DiagID)
1296 << T->getName(Context.getASTContext().getPrintingPolicy())
1297 << Range;
1298 break;
1299 }
1300 }
1301}
1302
1303// <type> ::= <function-type>
1304void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1305 SourceRange) {
1306 // Structors only appear in decls, so at this point we know it's not a
1307 // structor type.
1308 // FIXME: This may not be lambda-friendly.
1309 Out << "$$A6";
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001310 mangleFunctionType(T, NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001311}
1312void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1313 SourceRange) {
1314 llvm_unreachable("Can't mangle K&R function prototypes");
1315}
1316
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001317void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1318 const FunctionDecl *D,
1319 bool IsStructor,
1320 bool IsInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001321 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1322 // <return-type> <argument-list> <throw-spec>
1323 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1324
Reid Klecknerf21818d2013-06-24 19:21:52 +00001325 SourceRange Range;
1326 if (D) Range = D->getSourceRange();
1327
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001328 // If this is a C++ instance method, mangle the CVR qualifiers for the
1329 // this pointer.
David Majnemer1c7a4092013-08-15 08:13:23 +00001330 if (IsInstMethod) {
1331 if (PointersAre64Bit)
1332 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001333 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
David Majnemer1c7a4092013-08-15 08:13:23 +00001334 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001335
Reid Klecknere3e686f2013-09-25 22:28:52 +00001336 mangleCallingConvention(T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001337
1338 // <return-type> ::= <type>
1339 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001340 if (IsStructor) {
1341 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1342 StructorType == Dtor_Deleting) {
1343 // The scalar deleting destructor takes an extra int argument.
1344 // However, the FunctionType generated has 0 arguments.
1345 // FIXME: This is a temporary hack.
1346 // Maybe should fix the FunctionType creation instead?
Timur Iskhodzhanov4b104062013-08-26 10:32:04 +00001347 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001348 return;
1349 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001350 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001351 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001352 QualType ResultType = Proto->getResultType();
1353 if (ResultType->isVoidType())
1354 ResultType = ResultType.getUnqualifiedType();
1355 mangleType(ResultType, Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001356 }
1357
1358 // <argument-list> ::= X # void
1359 // ::= <type>+ @
1360 // ::= <type>* Z # varargs
1361 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1362 Out << 'X';
1363 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001364 // Happens for function pointer type arguments for example.
1365 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1366 ArgEnd = Proto->arg_type_end();
1367 Arg != ArgEnd; ++Arg)
1368 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001369 // <builtin-type> ::= Z # ellipsis
1370 if (Proto->isVariadic())
1371 Out << 'Z';
1372 else
1373 Out << '@';
1374 }
1375
1376 mangleThrowSpecification(Proto);
1377}
1378
1379void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001380 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1381 // # pointer. in 64-bit mode *all*
1382 // # 'this' pointers are 64-bit.
1383 // ::= <global-function>
1384 // <member-function> ::= A # private: near
1385 // ::= B # private: far
1386 // ::= C # private: static near
1387 // ::= D # private: static far
1388 // ::= E # private: virtual near
1389 // ::= F # private: virtual far
1390 // ::= G # private: thunk near
1391 // ::= H # private: thunk far
1392 // ::= I # protected: near
1393 // ::= J # protected: far
1394 // ::= K # protected: static near
1395 // ::= L # protected: static far
1396 // ::= M # protected: virtual near
1397 // ::= N # protected: virtual far
1398 // ::= O # protected: thunk near
1399 // ::= P # protected: thunk far
1400 // ::= Q # public: near
1401 // ::= R # public: far
1402 // ::= S # public: static near
1403 // ::= T # public: static far
1404 // ::= U # public: virtual near
1405 // ::= V # public: virtual far
1406 // ::= W # public: thunk near
1407 // ::= X # public: thunk far
1408 // <global-function> ::= Y # global near
1409 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001410 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1411 switch (MD->getAccess()) {
1412 default:
1413 case AS_private:
1414 if (MD->isStatic())
1415 Out << 'C';
1416 else if (MD->isVirtual())
1417 Out << 'E';
1418 else
1419 Out << 'A';
1420 break;
1421 case AS_protected:
1422 if (MD->isStatic())
1423 Out << 'K';
1424 else if (MD->isVirtual())
1425 Out << 'M';
1426 else
1427 Out << 'I';
1428 break;
1429 case AS_public:
1430 if (MD->isStatic())
1431 Out << 'S';
1432 else if (MD->isVirtual())
1433 Out << 'U';
1434 else
1435 Out << 'Q';
1436 }
1437 } else
1438 Out << 'Y';
1439}
Reid Klecknere3e686f2013-09-25 22:28:52 +00001440void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001441 // <calling-convention> ::= A # __cdecl
1442 // ::= B # __export __cdecl
1443 // ::= C # __pascal
1444 // ::= D # __export __pascal
1445 // ::= E # __thiscall
1446 // ::= F # __export __thiscall
1447 // ::= G # __stdcall
1448 // ::= H # __export __stdcall
1449 // ::= I # __fastcall
1450 // ::= J # __export __fastcall
1451 // The 'export' calling conventions are from a bygone era
1452 // (*cough*Win16*cough*) when functions were declared for export with
1453 // that keyword. (It didn't actually export them, it just made them so
1454 // that they could be in a DLL and somebody from another module could call
1455 // them.)
1456 CallingConv CC = T->getCallConv();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001457 switch (CC) {
1458 default:
1459 llvm_unreachable("Unsupported CC for mangling");
Charles Davise8519c32013-08-30 04:39:01 +00001460 case CC_X86_64Win64:
1461 case CC_X86_64SysV:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001462 case CC_C: Out << 'A'; break;
1463 case CC_X86Pascal: Out << 'C'; break;
1464 case CC_X86ThisCall: Out << 'E'; break;
1465 case CC_X86StdCall: Out << 'G'; break;
1466 case CC_X86FastCall: Out << 'I'; break;
1467 }
1468}
1469void MicrosoftCXXNameMangler::mangleThrowSpecification(
1470 const FunctionProtoType *FT) {
1471 // <throw-spec> ::= Z # throw(...) (default)
1472 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1473 // ::= <type>+
1474 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1475 // all actually mangled as 'Z'. (They're ignored because their associated
1476 // functionality isn't implemented, and probably never will be.)
1477 Out << 'Z';
1478}
1479
1480void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1481 SourceRange Range) {
1482 // Probably should be mangled as a template instantiation; need to see what
1483 // VC does first.
1484 DiagnosticsEngine &Diags = Context.getDiags();
1485 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1486 "cannot mangle this unresolved dependent type yet");
1487 Diags.Report(Range.getBegin(), DiagID)
1488 << Range;
1489}
1490
1491// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1492// <union-type> ::= T <name>
1493// <struct-type> ::= U <name>
1494// <class-type> ::= V <name>
1495// <enum-type> ::= W <size> <name>
1496void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001497 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001498}
1499void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001500 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001501}
David Majnemer02c44f02013-08-05 22:26:46 +00001502void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1503 switch (TD->getTagKind()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001504 case TTK_Union:
1505 Out << 'T';
1506 break;
1507 case TTK_Struct:
1508 case TTK_Interface:
1509 Out << 'U';
1510 break;
1511 case TTK_Class:
1512 Out << 'V';
1513 break;
1514 case TTK_Enum:
1515 Out << 'W';
1516 Out << getASTContext().getTypeSizeInChars(
David Majnemer02c44f02013-08-05 22:26:46 +00001517 cast<EnumDecl>(TD)->getIntegerType()).getQuantity();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001518 break;
1519 }
David Majnemer02c44f02013-08-05 22:26:46 +00001520 mangleName(TD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001521}
1522
1523// <type> ::= <array-type>
1524// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1525// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001526// <element-type> # as global, E is never required
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001527// It's supposed to be the other way around, but for some strange reason, it
1528// isn't. Today this behavior is retained for the sole purpose of backwards
1529// compatibility.
David Majnemer58e4cd02013-09-11 04:44:30 +00001530void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001531 // This isn't a recursive mangling, so now we have to do it all in this
1532 // one call.
David Majnemer58e4cd02013-09-11 04:44:30 +00001533 manglePointerQualifiers(T->getElementType().getQualifiers());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001534 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001535}
1536void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1537 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001538 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001539}
1540void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1541 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001542 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001543}
1544void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1545 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001546 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001547}
1548void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1549 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001550 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001551}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001552void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001553 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001554 SmallVector<llvm::APInt, 3> Dimensions;
1555 for (;;) {
1556 if (const ConstantArrayType *CAT =
1557 getASTContext().getAsConstantArrayType(ElementTy)) {
1558 Dimensions.push_back(CAT->getSize());
1559 ElementTy = CAT->getElementType();
1560 } else if (ElementTy->isVariableArrayType()) {
1561 const VariableArrayType *VAT =
1562 getASTContext().getAsVariableArrayType(ElementTy);
1563 DiagnosticsEngine &Diags = Context.getDiags();
1564 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1565 "cannot mangle this variable-length array yet");
1566 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1567 << VAT->getBracketsRange();
1568 return;
1569 } else if (ElementTy->isDependentSizedArrayType()) {
1570 // The dependent expression has to be folded into a constant (TODO).
1571 const DependentSizedArrayType *DSAT =
1572 getASTContext().getAsDependentSizedArrayType(ElementTy);
1573 DiagnosticsEngine &Diags = Context.getDiags();
1574 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1575 "cannot mangle this dependent-length array yet");
1576 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1577 << DSAT->getBracketsRange();
1578 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001579 } else if (const IncompleteArrayType *IAT =
1580 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1581 Dimensions.push_back(llvm::APInt(32, 0));
1582 ElementTy = IAT->getElementType();
1583 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001584 else break;
1585 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001586 Out << 'Y';
1587 // <dimension-count> ::= <number> # number of extra dimensions
1588 mangleNumber(Dimensions.size());
1589 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1590 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001591 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001592}
1593
1594// <type> ::= <pointer-to-member-type>
1595// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1596// <class name> <type>
1597void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1598 SourceRange Range) {
1599 QualType PointeeType = T->getPointeeType();
1600 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1601 Out << '8';
1602 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001603 mangleFunctionType(FPT, NULL, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001604 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001605 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1606 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001607 mangleQualifiers(PointeeType.getQualifiers(), true);
1608 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001609 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001610 }
1611}
1612
1613void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1614 SourceRange Range) {
1615 DiagnosticsEngine &Diags = Context.getDiags();
1616 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1617 "cannot mangle this template type parameter type yet");
1618 Diags.Report(Range.getBegin(), DiagID)
1619 << Range;
1620}
1621
1622void MicrosoftCXXNameMangler::mangleType(
1623 const SubstTemplateTypeParmPackType *T,
1624 SourceRange Range) {
1625 DiagnosticsEngine &Diags = Context.getDiags();
1626 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1627 "cannot mangle this substituted parameter pack yet");
1628 Diags.Report(Range.getBegin(), DiagID)
1629 << Range;
1630}
1631
1632// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001633// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1634// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001635void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1636 SourceRange Range) {
1637 QualType PointeeTy = T->getPointeeType();
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001638 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1639 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001640 mangleType(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001641}
1642void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1643 SourceRange Range) {
1644 // Object pointers never have qualifiers.
1645 Out << 'A';
David Majnemer1c7a4092013-08-15 08:13:23 +00001646 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1647 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001648 mangleType(T->getPointeeType(), Range);
1649}
1650
1651// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001652// <reference-type> ::= A E? <cvr-qualifiers> <type>
1653// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001654void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1655 SourceRange Range) {
1656 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001657 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1658 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001659 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001660}
1661
1662// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001663// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1664// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001665void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1666 SourceRange Range) {
1667 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001668 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1669 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001670 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001671}
1672
1673void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1674 SourceRange Range) {
1675 DiagnosticsEngine &Diags = Context.getDiags();
1676 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1677 "cannot mangle this complex number type yet");
1678 Diags.Report(Range.getBegin(), DiagID)
1679 << Range;
1680}
1681
1682void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1683 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001684 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1685 assert(ET && "vectors with non-builtin elements are unsupported");
1686 uint64_t Width = getASTContext().getTypeSize(T);
1687 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1688 // doesn't match the Intel types uses a custom mangling below.
1689 bool IntelVector = true;
1690 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1691 Out << "T__m64";
1692 } else if (Width == 128 || Width == 256) {
1693 if (ET->getKind() == BuiltinType::Float)
1694 Out << "T__m" << Width;
1695 else if (ET->getKind() == BuiltinType::LongLong)
1696 Out << "T__m" << Width << 'i';
1697 else if (ET->getKind() == BuiltinType::Double)
1698 Out << "U__m" << Width << 'd';
1699 else
1700 IntelVector = false;
1701 } else {
1702 IntelVector = false;
1703 }
1704
1705 if (!IntelVector) {
1706 // The MS ABI doesn't have a special mangling for vector types, so we define
1707 // our own mangling to handle uses of __vector_size__ on user-specified
1708 // types, and for extensions like __v4sf.
1709 Out << "T__clang_vec" << T->getNumElements() << '_';
1710 mangleType(ET, Range);
1711 }
1712
1713 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001714}
Reid Kleckner1232e272013-03-26 16:56:59 +00001715
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001716void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1717 SourceRange Range) {
1718 DiagnosticsEngine &Diags = Context.getDiags();
1719 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1720 "cannot mangle this extended vector type yet");
1721 Diags.Report(Range.getBegin(), DiagID)
1722 << Range;
1723}
1724void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1725 SourceRange Range) {
1726 DiagnosticsEngine &Diags = Context.getDiags();
1727 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1728 "cannot mangle this dependent-sized extended vector type yet");
1729 Diags.Report(Range.getBegin(), DiagID)
1730 << Range;
1731}
1732
1733void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1734 SourceRange) {
1735 // ObjC interfaces have structs underlying them.
1736 Out << 'U';
1737 mangleName(T->getDecl());
1738}
1739
1740void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1741 SourceRange Range) {
1742 // We don't allow overloading by different protocol qualification,
1743 // so mangling them isn't necessary.
1744 mangleType(T->getBaseType(), Range);
1745}
1746
1747void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1748 SourceRange Range) {
1749 Out << "_E";
1750
1751 QualType pointee = T->getPointeeType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001752 mangleFunctionType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001753}
1754
David Majnemer360d23e2013-08-16 08:29:13 +00001755void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1756 SourceRange) {
1757 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001758}
1759
1760void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1761 SourceRange Range) {
1762 DiagnosticsEngine &Diags = Context.getDiags();
1763 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1764 "cannot mangle this template specialization type yet");
1765 Diags.Report(Range.getBegin(), DiagID)
1766 << Range;
1767}
1768
1769void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1770 SourceRange Range) {
1771 DiagnosticsEngine &Diags = Context.getDiags();
1772 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1773 "cannot mangle this dependent name type yet");
1774 Diags.Report(Range.getBegin(), DiagID)
1775 << Range;
1776}
1777
1778void MicrosoftCXXNameMangler::mangleType(
1779 const DependentTemplateSpecializationType *T,
1780 SourceRange Range) {
1781 DiagnosticsEngine &Diags = Context.getDiags();
1782 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1783 "cannot mangle this dependent template specialization type yet");
1784 Diags.Report(Range.getBegin(), DiagID)
1785 << Range;
1786}
1787
1788void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1789 SourceRange Range) {
1790 DiagnosticsEngine &Diags = Context.getDiags();
1791 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1792 "cannot mangle this pack expansion yet");
1793 Diags.Report(Range.getBegin(), DiagID)
1794 << Range;
1795}
1796
1797void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1798 SourceRange Range) {
1799 DiagnosticsEngine &Diags = Context.getDiags();
1800 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1801 "cannot mangle this typeof(type) yet");
1802 Diags.Report(Range.getBegin(), DiagID)
1803 << Range;
1804}
1805
1806void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1807 SourceRange Range) {
1808 DiagnosticsEngine &Diags = Context.getDiags();
1809 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1810 "cannot mangle this typeof(expression) yet");
1811 Diags.Report(Range.getBegin(), DiagID)
1812 << Range;
1813}
1814
1815void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1816 SourceRange Range) {
1817 DiagnosticsEngine &Diags = Context.getDiags();
1818 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1819 "cannot mangle this decltype() yet");
1820 Diags.Report(Range.getBegin(), DiagID)
1821 << Range;
1822}
1823
1824void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1825 SourceRange Range) {
1826 DiagnosticsEngine &Diags = Context.getDiags();
1827 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1828 "cannot mangle this unary transform type yet");
1829 Diags.Report(Range.getBegin(), DiagID)
1830 << Range;
1831}
1832
1833void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1834 DiagnosticsEngine &Diags = Context.getDiags();
1835 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1836 "cannot mangle this 'auto' type yet");
1837 Diags.Report(Range.getBegin(), DiagID)
1838 << Range;
1839}
1840
1841void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1842 SourceRange Range) {
1843 DiagnosticsEngine &Diags = Context.getDiags();
1844 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1845 "cannot mangle this C11 atomic type yet");
1846 Diags.Report(Range.getBegin(), DiagID)
1847 << Range;
1848}
1849
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001850void MicrosoftMangleContextImpl::mangleName(const NamedDecl *D,
1851 raw_ostream &Out) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001852 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1853 "Invalid mangleName() call, argument is not a variable or function!");
1854 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1855 "Invalid mangleName() call on 'structor decl!");
1856
1857 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1858 getASTContext().getSourceManager(),
1859 "Mangling declaration");
1860
1861 MicrosoftCXXNameMangler Mangler(*this, Out);
1862 return Mangler.mangle(D);
1863}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001864
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001865void MicrosoftMangleContextImpl::mangleThunk(const CXXMethodDecl *MD,
1866 const ThunkInfo &Thunk,
1867 raw_ostream &Out) {
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001868 // FIXME: this is not yet a complete implementation, but merely a
1869 // reasonably-working stub to avoid crashing when required to emit a thunk.
1870 MicrosoftCXXNameMangler Mangler(*this, Out);
1871 Out << "\01?";
1872 Mangler.mangleName(MD);
1873 if (Thunk.This.NonVirtual != 0) {
1874 // FIXME: add support for protected/private or use mangleFunctionClass.
1875 Out << "W";
1876 llvm::APSInt APSNumber(/*BitWidth=*/32 /*FIXME: check on x64*/,
1877 /*isUnsigned=*/true);
1878 APSNumber = -Thunk.This.NonVirtual;
1879 Mangler.mangleNumber(APSNumber);
1880 } else {
1881 // FIXME: add support for protected/private or use mangleFunctionClass.
1882 Out << "Q";
1883 }
1884 // FIXME: mangle return adjustment? Most likely includes using an overridee FPT?
1885 Mangler.mangleFunctionType(MD->getType()->castAs<FunctionProtoType>(), MD, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001886}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001887
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001888void MicrosoftMangleContextImpl::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1889 CXXDtorType Type,
1890 const ThisAdjustment &,
1891 raw_ostream &) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001892 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1893 "cannot mangle thunk for this destructor yet");
1894 getDiags().Report(DD->getLocation(), DiagID);
1895}
Reid Kleckner90633022013-06-19 15:20:38 +00001896
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001897void MicrosoftMangleContextImpl::mangleCXXVFTable(
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001898 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1899 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001900 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1901 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001902 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001903 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001904 MicrosoftCXXNameMangler Mangler(*this, Out);
1905 Mangler.getStream() << "\01??_7";
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001906 Mangler.mangleName(Derived);
1907 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
1908 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1909 E = BasePath.end();
1910 I != E; ++I) {
1911 Mangler.mangleName(*I);
1912 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001913 Mangler.getStream() << '@';
1914}
Reid Kleckner90633022013-06-19 15:20:38 +00001915
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001916void MicrosoftMangleContextImpl::mangleCXXVBTable(
Reid Kleckner90633022013-06-19 15:20:38 +00001917 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1918 raw_ostream &Out) {
1919 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1920 // <cvr-qualifiers> [<name>] @
1921 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1922 // is always '7' for vbtables.
1923 MicrosoftCXXNameMangler Mangler(*this, Out);
1924 Mangler.getStream() << "\01??_8";
1925 Mangler.mangleName(Derived);
1926 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1927 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1928 E = BasePath.end();
1929 I != E; ++I) {
1930 Mangler.mangleName(*I);
1931 }
1932 Mangler.getStream() << '@';
1933}
1934
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001935void MicrosoftMangleContextImpl::mangleCXXRTTI(QualType T, raw_ostream &) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001936 // FIXME: Give a location...
1937 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1938 "cannot mangle RTTI descriptors for type %0 yet");
1939 getDiags().Report(DiagID)
1940 << T.getBaseTypeIdentifier();
1941}
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001942
1943void MicrosoftMangleContextImpl::mangleCXXRTTIName(QualType T, raw_ostream &) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001944 // FIXME: Give a location...
1945 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1946 "cannot mangle the name of type %0 into RTTI descriptors yet");
1947 getDiags().Report(DiagID)
1948 << T.getBaseTypeIdentifier();
1949}
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001950
1951void MicrosoftMangleContextImpl::mangleCXXCtor(const CXXConstructorDecl *D,
1952 CXXCtorType Type,
1953 raw_ostream &Out) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001954 MicrosoftCXXNameMangler mangler(*this, Out);
1955 mangler.mangle(D);
1956}
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001957
1958void MicrosoftMangleContextImpl::mangleCXXDtor(const CXXDestructorDecl *D,
1959 CXXDtorType Type,
1960 raw_ostream &Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001961 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001962 mangler.mangle(D);
1963}
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001964
1965void MicrosoftMangleContextImpl::mangleReferenceTemporary(const VarDecl *VD,
1966 raw_ostream &) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001967 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1968 "cannot mangle this reference temporary yet");
1969 getDiags().Report(VD->getLocation(), DiagID);
1970}
1971
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001972void MicrosoftMangleContextImpl::mangleStaticGuardVariable(const VarDecl *VD,
1973 raw_ostream &Out) {
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001974 // <guard-name> ::= ?_B <postfix> @51
1975 // ::= ?$S <guard-num> @ <postfix> @4IA
1976
1977 // The first mangling is what MSVC uses to guard static locals in inline
1978 // functions. It uses a different mangling in external functions to support
1979 // guarding more than 32 variables. MSVC rejects inline functions with more
1980 // than 32 static locals. We don't fully implement the second mangling
1981 // because those guards are not externally visible, and instead use LLVM's
1982 // default renaming when creating a new guard variable.
1983 MicrosoftCXXNameMangler Mangler(*this, Out);
1984
1985 bool Visible = VD->isExternallyVisible();
1986 // <operator-name> ::= ?_B # local static guard
1987 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@");
1988 Mangler.manglePostfix(VD->getDeclContext());
1989 Mangler.getStream() << (Visible ? "@51" : "@4IA");
1990}
1991
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001992void MicrosoftMangleContextImpl::mangleInitFiniStub(const VarDecl *D,
1993 raw_ostream &Out,
1994 char CharCode) {
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00001995 MicrosoftCXXNameMangler Mangler(*this, Out);
1996 Mangler.getStream() << "\01??__" << CharCode;
1997 Mangler.mangleName(D);
1998 // This is the function class mangling. These stubs are global, non-variadic,
1999 // cdecl functions that return void and take no args.
2000 Mangler.getStream() << "YAXXZ";
2001}
2002
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00002003void MicrosoftMangleContextImpl::mangleDynamicInitializer(const VarDecl *D,
2004 raw_ostream &Out) {
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00002005 // <initializer-name> ::= ?__E <name> YAXXZ
2006 mangleInitFiniStub(D, Out, 'E');
2007}
2008
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00002009void
2010MicrosoftMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D,
2011 raw_ostream &Out) {
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00002012 // <destructor-name> ::= ?__F <name> YAXXZ
2013 mangleInitFiniStub(D, Out, 'F');
Reid Kleckner942f9fe2013-09-10 20:14:30 +00002014}
2015
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00002016MicrosoftMangleContext *
2017MicrosoftMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags) {
2018 return new MicrosoftMangleContextImpl(Context, Diags);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002019}