blob: ab0db1acbf6f4c271f93e6f88662d3e7c403a140 [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 Iskhodzhanov8a5fb992013-10-04 11:25:05 +0000127 void mangleFunctionType(const FunctionType *T, const FunctionDecl *D = 0,
128 bool ForceInstMethod = false);
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 Klecknerd1a32c32013-10-08 00:58:57 +0000319 const FunctionProtoType *FT = FD->getType()->castAs<FunctionProtoType>();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000320
David Majnemercab7dad2013-09-13 09:03:14 +0000321 // extern "C" functions can hold entities that must be mangled.
322 // As it stands, these functions still need to get expressed in the full
323 // external name. They have their class and type omitted, replaced with '9'.
324 if (Context.shouldMangleDeclName(FD)) {
325 // First, the function class.
326 mangleFunctionClass(FD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000327
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +0000328 mangleFunctionType(FT, FD);
David Majnemercab7dad2013-09-13 09:03:14 +0000329 } else
330 Out << '9';
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000331}
332
333void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
334 // <type-encoding> ::= <storage-class> <variable-type>
335 // <storage-class> ::= 0 # private static member
336 // ::= 1 # protected static member
337 // ::= 2 # public static member
338 // ::= 3 # global
339 // ::= 4 # static local
340
341 // The first character in the encoding (after the name) is the storage class.
342 if (VD->isStaticDataMember()) {
343 // If it's a static member, it also encodes the access level.
344 switch (VD->getAccess()) {
345 default:
346 case AS_private: Out << '0'; break;
347 case AS_protected: Out << '1'; break;
348 case AS_public: Out << '2'; break;
349 }
350 }
351 else if (!VD->isStaticLocal())
352 Out << '3';
353 else
354 Out << '4';
355 // Now mangle the type.
356 // <variable-type> ::= <type> <cvr-qualifiers>
357 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
358 // Pointers and references are odd. The type of 'int * const foo;' gets
359 // mangled as 'QAHA' instead of 'PAHB', for example.
360 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
361 QualType Ty = TL.getType();
David Majnemer1c7a4092013-08-15 08:13:23 +0000362 if (Ty->isPointerType() || Ty->isReferenceType() ||
363 Ty->isMemberPointerType()) {
David Majnemer17ffbd02013-08-09 05:56:24 +0000364 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000365 if (PointersAre64Bit)
366 Out << 'E';
367 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) {
368 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true);
369 // Member pointers are suffixed with a back reference to the member
370 // pointer's class name.
371 mangleName(MPT->getClass()->getAsCXXRecordDecl());
372 } else
373 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
David Majnemer17ffbd02013-08-09 05:56:24 +0000374 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000375 // Global arrays are funny, too.
David Majnemer58e4cd02013-09-11 04:44:30 +0000376 mangleDecayedArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000377 if (AT->getElementType()->isArrayType())
378 Out << 'A';
379 else
380 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000381 } else {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000382 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000383 mangleQualifiers(Ty.getLocalQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000384 }
385}
386
387void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
388 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
389 const DeclContext *DC = ND->getDeclContext();
390
391 // Always start with the unqualified name.
392 mangleUnqualifiedName(ND);
393
394 // If this is an extern variable declared locally, the relevant DeclContext
395 // is that of the containing namespace, or the translation unit.
396 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
397 while (!DC->isNamespace() && !DC->isTranslationUnit())
398 DC = DC->getParent();
399
400 manglePostfix(DC);
401
402 // Terminate the whole name with an '@'.
403 Out << '@';
404}
405
406void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
407 llvm::APSInt APSNumber(/*BitWidth=*/64, /*isUnsigned=*/false);
408 APSNumber = Number;
409 mangleNumber(APSNumber);
410}
411
412void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
413 // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
414 // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
415 // ::= [?] @ # 0 (alternate mangling, not emitted by VC)
416 if (Value.isSigned() && Value.isNegative()) {
417 Out << '?';
418 mangleNumber(llvm::APSInt(Value.abs()));
419 return;
420 }
421 llvm::APSInt Temp(Value);
422 // There's a special shorter mangling for 0, but Microsoft
423 // chose not to use it. Instead, 0 gets mangled as "A@". Oh well...
424 if (Value.uge(1) && Value.ule(10)) {
425 --Temp;
426 Temp.print(Out, false);
427 } else {
428 // We have to build up the encoding in reverse order, so it will come
429 // out right when we write it out.
430 char Encoding[64];
431 char *EndPtr = Encoding+sizeof(Encoding);
432 char *CurPtr = EndPtr;
433 llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned());
434 NibbleMask = 0xf;
435 do {
436 *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf);
437 Temp = Temp.lshr(4);
438 } while (Temp != 0);
439 Out.write(CurPtr, EndPtr-CurPtr);
440 Out << '@';
441 }
442}
443
444static const TemplateDecl *
Reid Klecknerf16216c2013-03-20 01:40:23 +0000445isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000446 // Check if we have a function template.
447 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
448 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000449 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000450 return TD;
451 }
452 }
453
454 // Check if we have a class template.
455 if (const ClassTemplateSpecializationDecl *Spec =
Reid Klecknerf16216c2013-03-20 01:40:23 +0000456 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
457 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000458 return Spec->getSpecializedTemplate();
459 }
460
461 return 0;
462}
463
464void
465MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
466 DeclarationName Name) {
467 // <unqualified-name> ::= <operator-name>
468 // ::= <ctor-dtor-name>
469 // ::= <source-name>
470 // ::= <template-name>
Reid Klecknerf16216c2013-03-20 01:40:23 +0000471
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000472 // Check if we have a template.
Reid Klecknerf16216c2013-03-20 01:40:23 +0000473 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000474 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Kleckner3be37d12013-07-13 00:43:39 +0000475 // Function templates aren't considered for name back referencing. This
476 // makes sense since function templates aren't likely to occur multiple
477 // times in a symbol.
478 // FIXME: Test alias template mangling with MSVC 2013.
479 if (!isa<ClassTemplateDecl>(TD)) {
480 mangleTemplateInstantiationName(TD, *TemplateArgs);
481 return;
482 }
483
484 // We have a class template.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000485 // Here comes the tricky thing: if we need to mangle something like
486 // void foo(A::X<Y>, B::X<Y>),
487 // the X<Y> part is aliased. However, if you need to mangle
488 // void foo(A::X<A::Y>, A::X<B::Y>),
489 // the A::X<> part is not aliased.
490 // That said, from the mangler's perspective we have a structure like this:
491 // namespace[s] -> type[ -> template-parameters]
492 // but from the Clang perspective we have
493 // type [ -> template-parameters]
494 // \-> namespace[s]
495 // What we do is we create a new mangler, mangle the same type (without
496 // a namespace suffix) using the extra mangler with back references
497 // disabled (to avoid infinite recursion) and then use the mangled type
498 // name as a key to check the mangling of different types for aliasing.
499
500 std::string BackReferenceKey;
501 BackRefMap::iterator Found;
502 if (UseNameBackReferences) {
503 llvm::raw_string_ostream Stream(BackReferenceKey);
504 MicrosoftCXXNameMangler Extra(Context, Stream);
505 Extra.disableBackReferences();
506 Extra.mangleUnqualifiedName(ND, Name);
507 Stream.flush();
508
509 Found = NameBackReferences.find(BackReferenceKey);
510 }
511 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000512 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000513 if (UseNameBackReferences && NameBackReferences.size() < 10) {
514 size_t Size = NameBackReferences.size();
515 NameBackReferences[BackReferenceKey] = Size;
516 }
517 } else {
518 Out << Found->second;
519 }
520 return;
521 }
522
523 switch (Name.getNameKind()) {
524 case DeclarationName::Identifier: {
525 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
526 mangleSourceName(II);
527 break;
528 }
529
530 // Otherwise, an anonymous entity. We must have a declaration.
531 assert(ND && "mangling empty name without declaration");
532
533 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
534 if (NS->isAnonymousNamespace()) {
535 Out << "?A@";
536 break;
537 }
538 }
539
540 // We must have an anonymous struct.
541 const TagDecl *TD = cast<TagDecl>(ND);
542 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
543 assert(TD->getDeclContext() == D->getDeclContext() &&
544 "Typedef should not be in another decl context!");
545 assert(D->getDeclName().getAsIdentifierInfo() &&
546 "Typedef was not named!");
547 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
548 break;
549 }
550
David Majnemeraa824612013-09-17 23:57:10 +0000551 if (TD->hasDeclaratorForAnonDecl())
552 // Anonymous types with no tag or typedef get the name of their
553 // declarator mangled in.
554 Out << "<unnamed-type-" << TD->getDeclaratorForAnonDecl()->getName()
555 << ">@";
556 else
557 // Anonymous types with no tag, no typedef, or declarator get
558 // '<unnamed-tag>@'.
559 Out << "<unnamed-tag>@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000560 break;
561 }
562
563 case DeclarationName::ObjCZeroArgSelector:
564 case DeclarationName::ObjCOneArgSelector:
565 case DeclarationName::ObjCMultiArgSelector:
566 llvm_unreachable("Can't mangle Objective-C selector names here!");
567
568 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000569 if (ND == Structor) {
570 assert(StructorType == Ctor_Complete &&
571 "Should never be asked to mangle a ctor other than complete");
572 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000573 Out << "?0";
574 break;
575
576 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000577 if (ND == Structor)
578 // If the named decl is the C++ destructor we're mangling,
579 // use the type we were given.
580 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
581 else
Reid Klecknera4130ba2013-07-22 13:51:44 +0000582 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000583 // class with a destructor is declared within a destructor.
Reid Klecknera4130ba2013-07-22 13:51:44 +0000584 mangleCXXDtorType(Dtor_Base);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000585 break;
586
587 case DeclarationName::CXXConversionFunctionName:
588 // <operator-name> ::= ?B # (cast)
589 // The target type is encoded as the return type.
590 Out << "?B";
591 break;
592
593 case DeclarationName::CXXOperatorName:
594 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
595 break;
596
597 case DeclarationName::CXXLiteralOperatorName: {
598 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
599 DiagnosticsEngine Diags = Context.getDiags();
600 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
601 "cannot mangle this literal operator yet");
602 Diags.Report(ND->getLocation(), DiagID);
603 break;
604 }
605
606 case DeclarationName::CXXUsingDirective:
607 llvm_unreachable("Can't mangle a using directive name!");
608 }
609}
610
611void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
612 bool NoFunction) {
613 // <postfix> ::= <unqualified-name> [<postfix>]
614 // ::= <substitution> [<postfix>]
615
616 if (!DC) return;
617
618 while (isa<LinkageSpecDecl>(DC))
619 DC = DC->getParent();
620
621 if (DC->isTranslationUnit())
622 return;
623
624 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedmane5798892013-07-10 01:13:27 +0000625 DiagnosticsEngine Diags = Context.getDiags();
626 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
627 "cannot mangle a local inside this block yet");
628 Diags.Report(BD->getLocation(), DiagID);
629
630 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
631 // for how this should be done.
632 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000633 Out << '@';
634 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000635 } else if (isa<CapturedDecl>(DC)) {
636 // Skip CapturedDecl context.
637 manglePostfix(DC->getParent(), NoFunction);
638 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000639 }
640
641 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
642 return;
643 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
644 mangleObjCMethodName(Method);
645 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
646 mangleLocalName(Func);
647 else {
648 mangleUnqualifiedName(cast<NamedDecl>(DC));
649 manglePostfix(DC->getParent(), NoFunction);
650 }
651}
652
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000653void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000654 // Microsoft uses the names on the case labels for these dtor variants. Clang
655 // uses the Itanium terminology internally. Everything in this ABI delegates
656 // towards the base dtor.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000657 switch (T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000658 // <operator-name> ::= ?1 # destructor
659 case Dtor_Base: Out << "?1"; return;
660 // <operator-name> ::= ?_D # vbase destructor
661 case Dtor_Complete: Out << "?_D"; return;
662 // <operator-name> ::= ?_G # scalar deleting destructor
663 case Dtor_Deleting: Out << "?_G"; return;
664 // <operator-name> ::= ?_E # vector deleting destructor
665 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
666 // it.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000667 }
668 llvm_unreachable("Unsupported dtor type?");
669}
670
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000671void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
672 SourceLocation Loc) {
673 switch (OO) {
674 // ?0 # constructor
675 // ?1 # destructor
676 // <operator-name> ::= ?2 # new
677 case OO_New: Out << "?2"; break;
678 // <operator-name> ::= ?3 # delete
679 case OO_Delete: Out << "?3"; break;
680 // <operator-name> ::= ?4 # =
681 case OO_Equal: Out << "?4"; break;
682 // <operator-name> ::= ?5 # >>
683 case OO_GreaterGreater: Out << "?5"; break;
684 // <operator-name> ::= ?6 # <<
685 case OO_LessLess: Out << "?6"; break;
686 // <operator-name> ::= ?7 # !
687 case OO_Exclaim: Out << "?7"; break;
688 // <operator-name> ::= ?8 # ==
689 case OO_EqualEqual: Out << "?8"; break;
690 // <operator-name> ::= ?9 # !=
691 case OO_ExclaimEqual: Out << "?9"; break;
692 // <operator-name> ::= ?A # []
693 case OO_Subscript: Out << "?A"; break;
694 // ?B # conversion
695 // <operator-name> ::= ?C # ->
696 case OO_Arrow: Out << "?C"; break;
697 // <operator-name> ::= ?D # *
698 case OO_Star: Out << "?D"; break;
699 // <operator-name> ::= ?E # ++
700 case OO_PlusPlus: Out << "?E"; break;
701 // <operator-name> ::= ?F # --
702 case OO_MinusMinus: Out << "?F"; break;
703 // <operator-name> ::= ?G # -
704 case OO_Minus: Out << "?G"; break;
705 // <operator-name> ::= ?H # +
706 case OO_Plus: Out << "?H"; break;
707 // <operator-name> ::= ?I # &
708 case OO_Amp: Out << "?I"; break;
709 // <operator-name> ::= ?J # ->*
710 case OO_ArrowStar: Out << "?J"; break;
711 // <operator-name> ::= ?K # /
712 case OO_Slash: Out << "?K"; break;
713 // <operator-name> ::= ?L # %
714 case OO_Percent: Out << "?L"; break;
715 // <operator-name> ::= ?M # <
716 case OO_Less: Out << "?M"; break;
717 // <operator-name> ::= ?N # <=
718 case OO_LessEqual: Out << "?N"; break;
719 // <operator-name> ::= ?O # >
720 case OO_Greater: Out << "?O"; break;
721 // <operator-name> ::= ?P # >=
722 case OO_GreaterEqual: Out << "?P"; break;
723 // <operator-name> ::= ?Q # ,
724 case OO_Comma: Out << "?Q"; break;
725 // <operator-name> ::= ?R # ()
726 case OO_Call: Out << "?R"; break;
727 // <operator-name> ::= ?S # ~
728 case OO_Tilde: Out << "?S"; break;
729 // <operator-name> ::= ?T # ^
730 case OO_Caret: Out << "?T"; break;
731 // <operator-name> ::= ?U # |
732 case OO_Pipe: Out << "?U"; break;
733 // <operator-name> ::= ?V # &&
734 case OO_AmpAmp: Out << "?V"; break;
735 // <operator-name> ::= ?W # ||
736 case OO_PipePipe: Out << "?W"; break;
737 // <operator-name> ::= ?X # *=
738 case OO_StarEqual: Out << "?X"; break;
739 // <operator-name> ::= ?Y # +=
740 case OO_PlusEqual: Out << "?Y"; break;
741 // <operator-name> ::= ?Z # -=
742 case OO_MinusEqual: Out << "?Z"; break;
743 // <operator-name> ::= ?_0 # /=
744 case OO_SlashEqual: Out << "?_0"; break;
745 // <operator-name> ::= ?_1 # %=
746 case OO_PercentEqual: Out << "?_1"; break;
747 // <operator-name> ::= ?_2 # >>=
748 case OO_GreaterGreaterEqual: Out << "?_2"; break;
749 // <operator-name> ::= ?_3 # <<=
750 case OO_LessLessEqual: Out << "?_3"; break;
751 // <operator-name> ::= ?_4 # &=
752 case OO_AmpEqual: Out << "?_4"; break;
753 // <operator-name> ::= ?_5 # |=
754 case OO_PipeEqual: Out << "?_5"; break;
755 // <operator-name> ::= ?_6 # ^=
756 case OO_CaretEqual: Out << "?_6"; break;
757 // ?_7 # vftable
758 // ?_8 # vbtable
759 // ?_9 # vcall
760 // ?_A # typeof
761 // ?_B # local static guard
762 // ?_C # string
763 // ?_D # vbase destructor
764 // ?_E # vector deleting destructor
765 // ?_F # default constructor closure
766 // ?_G # scalar deleting destructor
767 // ?_H # vector constructor iterator
768 // ?_I # vector destructor iterator
769 // ?_J # vector vbase constructor iterator
770 // ?_K # virtual displacement map
771 // ?_L # eh vector constructor iterator
772 // ?_M # eh vector destructor iterator
773 // ?_N # eh vector vbase constructor iterator
774 // ?_O # copy constructor closure
775 // ?_P<name> # udt returning <name>
776 // ?_Q # <unknown>
777 // ?_R0 # RTTI Type Descriptor
778 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
779 // ?_R2 # RTTI Base Class Array
780 // ?_R3 # RTTI Class Hierarchy Descriptor
781 // ?_R4 # RTTI Complete Object Locator
782 // ?_S # local vftable
783 // ?_T # local vftable constructor closure
784 // <operator-name> ::= ?_U # new[]
785 case OO_Array_New: Out << "?_U"; break;
786 // <operator-name> ::= ?_V # delete[]
787 case OO_Array_Delete: Out << "?_V"; break;
788
789 case OO_Conditional: {
790 DiagnosticsEngine &Diags = Context.getDiags();
791 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
792 "cannot mangle this conditional operator yet");
793 Diags.Report(Loc, DiagID);
794 break;
795 }
796
797 case OO_None:
798 case NUM_OVERLOADED_OPERATORS:
799 llvm_unreachable("Not an overloaded operator");
800 }
801}
802
803void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
804 // <source name> ::= <identifier> @
805 std::string key = II->getNameStart();
806 BackRefMap::iterator Found;
807 if (UseNameBackReferences)
808 Found = NameBackReferences.find(key);
809 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
810 Out << II->getName() << '@';
811 if (UseNameBackReferences && NameBackReferences.size() < 10) {
812 size_t Size = NameBackReferences.size();
813 NameBackReferences[key] = Size;
814 }
815 } else {
816 Out << Found->second;
817 }
818}
819
820void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
821 Context.mangleObjCMethodName(MD, Out);
822}
823
824// Find out how many function decls live above this one and return an integer
825// suitable for use as the number in a numbered anonymous scope.
826// TODO: Memoize.
827static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
828 const DeclContext *DC = FD->getParent();
829 int level = 1;
830
831 while (DC && !DC->isTranslationUnit()) {
832 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
833 DC = DC->getParent();
834 }
835
836 return 2*level;
837}
838
839void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
840 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
841 // <numbered-anonymous-scope> ::= ? <number>
842 // Even though the name is rendered in reverse order (e.g.
843 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
844 // innermost. So a method bar in class C local to function foo gets mangled
845 // as something like:
846 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
847 // This is more apparent when you have a type nested inside a method of a
848 // type nested inside a function. A method baz in class D local to method
849 // bar of class C local to function foo gets mangled as:
850 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
851 // This scheme is general enough to support GCC-style nested
852 // functions. You could have a method baz of class C inside a function bar
853 // inside a function foo, like so:
854 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
855 int NestLevel = getLocalNestingLevel(FD);
856 Out << '?';
857 mangleNumber(NestLevel);
858 Out << '?';
859 mangle(FD, "?");
860}
861
862void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
863 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000864 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000865 // <template-name> ::= <unscoped-template-name> <template-args>
866 // ::= <substitution>
867 // Always start with the unqualified name.
868
869 // Templates have their own context for back references.
870 ArgBackRefMap OuterArgsContext;
871 BackRefMap OuterTemplateContext;
872 NameBackReferences.swap(OuterTemplateContext);
873 TypeBackReferences.swap(OuterArgsContext);
874
875 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000876 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000877
878 // Restore the previous back reference contexts.
879 NameBackReferences.swap(OuterTemplateContext);
880 TypeBackReferences.swap(OuterArgsContext);
881}
882
883void
884MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
885 // <unscoped-template-name> ::= ?$ <unqualified-name>
886 Out << "?$";
887 mangleUnqualifiedName(TD);
888}
889
890void
891MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
892 bool IsBoolean) {
893 // <integer-literal> ::= $0 <number>
894 Out << "$0";
895 // Make sure booleans are encoded as 0/1.
896 if (IsBoolean && Value.getBoolValue())
897 mangleNumber(1);
898 else
899 mangleNumber(Value);
900}
901
902void
903MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
904 // See if this is a constant expression.
905 llvm::APSInt Value;
906 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
907 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
908 return;
909 }
910
David Majnemerc80eb462013-08-13 06:32:20 +0000911 const CXXUuidofExpr *UE = 0;
912 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
913 if (UO->getOpcode() == UO_AddrOf)
914 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
915 } else
916 UE = dyn_cast<CXXUuidofExpr>(E);
917
918 if (UE) {
919 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
920 // const __s_GUID _GUID_{lower case UUID with underscores}
921 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
922 std::string Name = "_GUID_" + Uuid.lower();
923 std::replace(Name.begin(), Name.end(), '-', '_');
924
David Majnemer26314e12013-08-13 09:17:25 +0000925 // If we had to peek through an address-of operator, treat this like we are
David Majnemerc80eb462013-08-13 06:32:20 +0000926 // dealing with a pointer type. Otherwise, treat it like a const reference.
927 //
928 // N.B. This matches up with the handling of TemplateArgument::Declaration
929 // in mangleTemplateArg
930 if (UE == E)
931 Out << "$E?";
932 else
933 Out << "$1?";
934 Out << Name << "@@3U__s_GUID@@B";
935 return;
936 }
937
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000938 // As bad as this diagnostic is, it's better than crashing.
939 DiagnosticsEngine &Diags = Context.getDiags();
940 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
941 "cannot yet mangle expression type %0");
942 Diags.Report(E->getExprLoc(), DiagID)
943 << E->getStmtClassName() << E->getSourceRange();
944}
945
946void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000947MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
948 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000949 // <template-args> ::= {<type> | <integer-literal>}+ @
950 unsigned NumTemplateArgs = TemplateArgs.size();
951 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000952 const TemplateArgument &TA = TemplateArgs[i];
David Majnemer309f6452013-08-27 08:21:25 +0000953 mangleTemplateArg(TD, TA);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000954 }
955 Out << '@';
956}
957
Reid Kleckner5d90d182013-07-02 18:10:07 +0000958void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
David Majnemer309f6452013-08-27 08:21:25 +0000959 const TemplateArgument &TA) {
Reid Kleckner5d90d182013-07-02 18:10:07 +0000960 switch (TA.getKind()) {
961 case TemplateArgument::Null:
962 llvm_unreachable("Can't mangle null template arguments!");
David Majnemer309f6452013-08-27 08:21:25 +0000963 case TemplateArgument::TemplateExpansion:
964 llvm_unreachable("Can't mangle template expansion arguments!");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000965 case TemplateArgument::Type: {
966 QualType T = TA.getAsType();
967 mangleType(T, SourceRange(), QMM_Escape);
968 break;
969 }
David Majnemerf2081f62013-08-13 01:25:35 +0000970 case TemplateArgument::Declaration: {
971 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl());
972 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000973 break;
David Majnemerf2081f62013-08-13 01:25:35 +0000974 }
Reid Kleckner5d90d182013-07-02 18:10:07 +0000975 case TemplateArgument::Integral:
976 mangleIntegerLiteral(TA.getAsIntegral(),
977 TA.getIntegralType()->isBooleanType());
978 break;
David Majnemer7802fc92013-08-05 21:33:59 +0000979 case TemplateArgument::NullPtr:
980 Out << "$0A@";
981 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000982 case TemplateArgument::Expression:
983 mangleExpression(TA.getAsExpr());
984 break;
985 case TemplateArgument::Pack:
986 // Unlike Itanium, there is no character code to indicate an argument pack.
Reid Kleckner5d90d182013-07-02 18:10:07 +0000987 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
988 I != E; ++I)
David Majnemer309f6452013-08-27 08:21:25 +0000989 mangleTemplateArg(TD, *I);
Reid Kleckner5d90d182013-07-02 18:10:07 +0000990 break;
991 case TemplateArgument::Template:
David Majnemer02c44f02013-08-05 22:26:46 +0000992 mangleType(cast<TagDecl>(
993 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
994 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000995 }
996}
997
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000998void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
999 bool IsMember) {
1000 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
1001 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
1002 // 'I' means __restrict (32/64-bit).
1003 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
1004 // keyword!
1005 // <base-cvr-qualifiers> ::= A # near
1006 // ::= B # near const
1007 // ::= C # near volatile
1008 // ::= D # near const volatile
1009 // ::= E # far (16-bit)
1010 // ::= F # far const (16-bit)
1011 // ::= G # far volatile (16-bit)
1012 // ::= H # far const volatile (16-bit)
1013 // ::= I # huge (16-bit)
1014 // ::= J # huge const (16-bit)
1015 // ::= K # huge volatile (16-bit)
1016 // ::= L # huge const volatile (16-bit)
1017 // ::= M <basis> # based
1018 // ::= N <basis> # based const
1019 // ::= O <basis> # based volatile
1020 // ::= P <basis> # based const volatile
1021 // ::= Q # near member
1022 // ::= R # near const member
1023 // ::= S # near volatile member
1024 // ::= T # near const volatile member
1025 // ::= U # far member (16-bit)
1026 // ::= V # far const member (16-bit)
1027 // ::= W # far volatile member (16-bit)
1028 // ::= X # far const volatile member (16-bit)
1029 // ::= Y # huge member (16-bit)
1030 // ::= Z # huge const member (16-bit)
1031 // ::= 0 # huge volatile member (16-bit)
1032 // ::= 1 # huge const volatile member (16-bit)
1033 // ::= 2 <basis> # based member
1034 // ::= 3 <basis> # based const member
1035 // ::= 4 <basis> # based volatile member
1036 // ::= 5 <basis> # based const volatile member
1037 // ::= 6 # near function (pointers only)
1038 // ::= 7 # far function (pointers only)
1039 // ::= 8 # near method (pointers only)
1040 // ::= 9 # far method (pointers only)
1041 // ::= _A <basis> # based function (pointers only)
1042 // ::= _B <basis> # based function (far?) (pointers only)
1043 // ::= _C <basis> # based method (pointers only)
1044 // ::= _D <basis> # based method (far?) (pointers only)
1045 // ::= _E # block (Clang)
1046 // <basis> ::= 0 # __based(void)
1047 // ::= 1 # __based(segment)?
1048 // ::= 2 <name> # __based(name)
1049 // ::= 3 # ?
1050 // ::= 4 # ?
1051 // ::= 5 # not really based
1052 bool HasConst = Quals.hasConst(),
1053 HasVolatile = Quals.hasVolatile();
David Majnemerc0e64f32013-08-05 22:43:06 +00001054
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001055 if (!IsMember) {
1056 if (HasConst && HasVolatile) {
1057 Out << 'D';
1058 } else if (HasVolatile) {
1059 Out << 'C';
1060 } else if (HasConst) {
1061 Out << 'B';
1062 } else {
1063 Out << 'A';
1064 }
1065 } else {
1066 if (HasConst && HasVolatile) {
1067 Out << 'T';
1068 } else if (HasVolatile) {
1069 Out << 'S';
1070 } else if (HasConst) {
1071 Out << 'R';
1072 } else {
1073 Out << 'Q';
1074 }
1075 }
1076
1077 // FIXME: For now, just drop all extension qualifiers on the floor.
1078}
1079
1080void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1081 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1082 // ::= Q # const
1083 // ::= R # volatile
1084 // ::= S # const volatile
1085 bool HasConst = Quals.hasConst(),
1086 HasVolatile = Quals.hasVolatile();
1087 if (HasConst && HasVolatile) {
1088 Out << 'S';
1089 } else if (HasVolatile) {
1090 Out << 'R';
1091 } else if (HasConst) {
1092 Out << 'Q';
1093 } else {
1094 Out << 'P';
1095 }
1096}
1097
1098void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1099 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001100 // MSVC will backreference two canonically equivalent types that have slightly
1101 // different manglings when mangled alone.
David Majnemer58e4cd02013-09-11 04:44:30 +00001102
1103 // Decayed types do not match up with non-decayed versions of the same type.
1104 //
1105 // e.g.
1106 // void (*x)(void) will not form a backreference with void x(void)
1107 void *TypePtr;
1108 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1109 TypePtr = DT->getOriginalType().getCanonicalType().getAsOpaquePtr();
1110 // If the original parameter was textually written as an array,
1111 // instead treat the decayed parameter like it's const.
1112 //
1113 // e.g.
1114 // int [] -> int * const
1115 if (DT->getOriginalType()->isArrayType())
1116 T = T.withConst();
1117 } else
1118 TypePtr = T.getCanonicalType().getAsOpaquePtr();
1119
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001120 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1121
1122 if (Found == TypeBackReferences.end()) {
1123 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1124
David Majnemer58e4cd02013-09-11 04:44:30 +00001125 mangleType(T, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001126
1127 // See if it's worth creating a back reference.
1128 // Only types longer than 1 character are considered
1129 // and only 10 back references slots are available:
1130 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1131 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1132 size_t Size = TypeBackReferences.size();
1133 TypeBackReferences[TypePtr] = Size;
1134 }
1135 } else {
1136 Out << Found->second;
1137 }
1138}
1139
1140void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001141 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001142 // Don't use the canonical types. MSVC includes things like 'const' on
1143 // pointer arguments to function pointers that canonicalization strips away.
1144 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001145 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001146 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1147 // If there were any Quals, getAsArrayType() pushed them onto the array
1148 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001149 if (QMM == QMM_Mangle)
1150 Out << 'A';
1151 else if (QMM == QMM_Escape || QMM == QMM_Result)
1152 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001153 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001154 return;
1155 }
1156
1157 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1158 T->isBlockPointerType();
1159
1160 switch (QMM) {
1161 case QMM_Drop:
1162 break;
1163 case QMM_Mangle:
1164 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1165 Out << '6';
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +00001166 mangleFunctionType(FT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001167 return;
1168 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001169 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001170 break;
1171 case QMM_Escape:
1172 if (!IsPointer && Quals) {
1173 Out << "$$C";
1174 mangleQualifiers(Quals, false);
1175 }
1176 break;
1177 case QMM_Result:
1178 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1179 Out << '?';
1180 mangleQualifiers(Quals, false);
1181 }
1182 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001183 }
1184
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001185 // We have to mangle these now, while we still have enough information.
1186 if (IsPointer)
1187 manglePointerQualifiers(Quals);
1188 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001189
1190 switch (ty->getTypeClass()) {
1191#define ABSTRACT_TYPE(CLASS, PARENT)
1192#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1193 case Type::CLASS: \
1194 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1195 return;
1196#define TYPE(CLASS, PARENT) \
1197 case Type::CLASS: \
1198 mangleType(cast<CLASS##Type>(ty), Range); \
1199 break;
1200#include "clang/AST/TypeNodes.def"
1201#undef ABSTRACT_TYPE
1202#undef NON_CANONICAL_TYPE
1203#undef TYPE
1204 }
1205}
1206
1207void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1208 SourceRange Range) {
1209 // <type> ::= <builtin-type>
1210 // <builtin-type> ::= X # void
1211 // ::= C # signed char
1212 // ::= D # char
1213 // ::= E # unsigned char
1214 // ::= F # short
1215 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1216 // ::= H # int
1217 // ::= I # unsigned int
1218 // ::= J # long
1219 // ::= K # unsigned long
1220 // L # <none>
1221 // ::= M # float
1222 // ::= N # double
1223 // ::= O # long double (__float80 is mangled differently)
1224 // ::= _J # long long, __int64
1225 // ::= _K # unsigned long long, __int64
1226 // ::= _L # __int128
1227 // ::= _M # unsigned __int128
1228 // ::= _N # bool
1229 // _O # <array in parameter>
1230 // ::= _T # __float80 (Intel)
1231 // ::= _W # wchar_t
1232 // ::= _Z # __float80 (Digital Mars)
1233 switch (T->getKind()) {
1234 case BuiltinType::Void: Out << 'X'; break;
1235 case BuiltinType::SChar: Out << 'C'; break;
1236 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1237 case BuiltinType::UChar: Out << 'E'; break;
1238 case BuiltinType::Short: Out << 'F'; break;
1239 case BuiltinType::UShort: Out << 'G'; break;
1240 case BuiltinType::Int: Out << 'H'; break;
1241 case BuiltinType::UInt: Out << 'I'; break;
1242 case BuiltinType::Long: Out << 'J'; break;
1243 case BuiltinType::ULong: Out << 'K'; break;
1244 case BuiltinType::Float: Out << 'M'; break;
1245 case BuiltinType::Double: Out << 'N'; break;
1246 // TODO: Determine size and mangle accordingly
1247 case BuiltinType::LongDouble: Out << 'O'; break;
1248 case BuiltinType::LongLong: Out << "_J"; break;
1249 case BuiltinType::ULongLong: Out << "_K"; break;
1250 case BuiltinType::Int128: Out << "_L"; break;
1251 case BuiltinType::UInt128: Out << "_M"; break;
1252 case BuiltinType::Bool: Out << "_N"; break;
1253 case BuiltinType::WChar_S:
1254 case BuiltinType::WChar_U: Out << "_W"; break;
1255
1256#define BUILTIN_TYPE(Id, SingletonId)
1257#define PLACEHOLDER_TYPE(Id, SingletonId) \
1258 case BuiltinType::Id:
1259#include "clang/AST/BuiltinTypes.def"
1260 case BuiltinType::Dependent:
1261 llvm_unreachable("placeholder types shouldn't get to name mangling");
1262
1263 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1264 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1265 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001266
1267 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1268 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1269 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1270 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1271 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1272 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001273 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001274 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001275
1276 case BuiltinType::NullPtr: Out << "$$T"; break;
1277
1278 case BuiltinType::Char16:
1279 case BuiltinType::Char32:
1280 case BuiltinType::Half: {
1281 DiagnosticsEngine &Diags = Context.getDiags();
1282 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1283 "cannot mangle this built-in %0 type yet");
1284 Diags.Report(Range.getBegin(), DiagID)
1285 << T->getName(Context.getASTContext().getPrintingPolicy())
1286 << Range;
1287 break;
1288 }
1289 }
1290}
1291
1292// <type> ::= <function-type>
1293void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1294 SourceRange) {
1295 // Structors only appear in decls, so at this point we know it's not a
1296 // structor type.
1297 // FIXME: This may not be lambda-friendly.
1298 Out << "$$A6";
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +00001299 mangleFunctionType(T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001300}
1301void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1302 SourceRange) {
1303 llvm_unreachable("Can't mangle K&R function prototypes");
1304}
1305
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001306void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1307 const FunctionDecl *D,
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +00001308 bool ForceInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001309 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1310 // <return-type> <argument-list> <throw-spec>
1311 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1312
Reid Klecknerf21818d2013-06-24 19:21:52 +00001313 SourceRange Range;
1314 if (D) Range = D->getSourceRange();
1315
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +00001316 bool IsStructor = false, IsInstMethod = ForceInstMethod;
1317 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(D)) {
1318 if (MD->isInstance())
1319 IsInstMethod = true;
1320 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
1321 IsStructor = true;
1322 }
1323
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001324 // If this is a C++ instance method, mangle the CVR qualifiers for the
1325 // this pointer.
David Majnemer1c7a4092013-08-15 08:13:23 +00001326 if (IsInstMethod) {
1327 if (PointersAre64Bit)
1328 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001329 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
David Majnemer1c7a4092013-08-15 08:13:23 +00001330 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001331
Reid Klecknere3e686f2013-09-25 22:28:52 +00001332 mangleCallingConvention(T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001333
1334 // <return-type> ::= <type>
1335 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001336 if (IsStructor) {
1337 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1338 StructorType == Dtor_Deleting) {
1339 // The scalar deleting destructor takes an extra int argument.
1340 // However, the FunctionType generated has 0 arguments.
1341 // FIXME: This is a temporary hack.
1342 // Maybe should fix the FunctionType creation instead?
Timur Iskhodzhanov4b104062013-08-26 10:32:04 +00001343 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001344 return;
1345 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001346 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001347 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001348 QualType ResultType = Proto->getResultType();
1349 if (ResultType->isVoidType())
1350 ResultType = ResultType.getUnqualifiedType();
1351 mangleType(ResultType, Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001352 }
1353
1354 // <argument-list> ::= X # void
1355 // ::= <type>+ @
1356 // ::= <type>* Z # varargs
1357 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1358 Out << 'X';
1359 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001360 // Happens for function pointer type arguments for example.
1361 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1362 ArgEnd = Proto->arg_type_end();
1363 Arg != ArgEnd; ++Arg)
1364 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001365 // <builtin-type> ::= Z # ellipsis
1366 if (Proto->isVariadic())
1367 Out << 'Z';
1368 else
1369 Out << '@';
1370 }
1371
1372 mangleThrowSpecification(Proto);
1373}
1374
1375void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001376 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1377 // # pointer. in 64-bit mode *all*
1378 // # 'this' pointers are 64-bit.
1379 // ::= <global-function>
1380 // <member-function> ::= A # private: near
1381 // ::= B # private: far
1382 // ::= C # private: static near
1383 // ::= D # private: static far
1384 // ::= E # private: virtual near
1385 // ::= F # private: virtual far
1386 // ::= G # private: thunk near
1387 // ::= H # private: thunk far
1388 // ::= I # protected: near
1389 // ::= J # protected: far
1390 // ::= K # protected: static near
1391 // ::= L # protected: static far
1392 // ::= M # protected: virtual near
1393 // ::= N # protected: virtual far
1394 // ::= O # protected: thunk near
1395 // ::= P # protected: thunk far
1396 // ::= Q # public: near
1397 // ::= R # public: far
1398 // ::= S # public: static near
1399 // ::= T # public: static far
1400 // ::= U # public: virtual near
1401 // ::= V # public: virtual far
1402 // ::= W # public: thunk near
1403 // ::= X # public: thunk far
1404 // <global-function> ::= Y # global near
1405 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001406 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1407 switch (MD->getAccess()) {
1408 default:
1409 case AS_private:
1410 if (MD->isStatic())
1411 Out << 'C';
1412 else if (MD->isVirtual())
1413 Out << 'E';
1414 else
1415 Out << 'A';
1416 break;
1417 case AS_protected:
1418 if (MD->isStatic())
1419 Out << 'K';
1420 else if (MD->isVirtual())
1421 Out << 'M';
1422 else
1423 Out << 'I';
1424 break;
1425 case AS_public:
1426 if (MD->isStatic())
1427 Out << 'S';
1428 else if (MD->isVirtual())
1429 Out << 'U';
1430 else
1431 Out << 'Q';
1432 }
1433 } else
1434 Out << 'Y';
1435}
Reid Klecknere3e686f2013-09-25 22:28:52 +00001436void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001437 // <calling-convention> ::= A # __cdecl
1438 // ::= B # __export __cdecl
1439 // ::= C # __pascal
1440 // ::= D # __export __pascal
1441 // ::= E # __thiscall
1442 // ::= F # __export __thiscall
1443 // ::= G # __stdcall
1444 // ::= H # __export __stdcall
1445 // ::= I # __fastcall
1446 // ::= J # __export __fastcall
1447 // The 'export' calling conventions are from a bygone era
1448 // (*cough*Win16*cough*) when functions were declared for export with
1449 // that keyword. (It didn't actually export them, it just made them so
1450 // that they could be in a DLL and somebody from another module could call
1451 // them.)
1452 CallingConv CC = T->getCallConv();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001453 switch (CC) {
1454 default:
1455 llvm_unreachable("Unsupported CC for mangling");
Charles Davise8519c32013-08-30 04:39:01 +00001456 case CC_X86_64Win64:
1457 case CC_X86_64SysV:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001458 case CC_C: Out << 'A'; break;
1459 case CC_X86Pascal: Out << 'C'; break;
1460 case CC_X86ThisCall: Out << 'E'; break;
1461 case CC_X86StdCall: Out << 'G'; break;
1462 case CC_X86FastCall: Out << 'I'; break;
1463 }
1464}
1465void MicrosoftCXXNameMangler::mangleThrowSpecification(
1466 const FunctionProtoType *FT) {
1467 // <throw-spec> ::= Z # throw(...) (default)
1468 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1469 // ::= <type>+
1470 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1471 // all actually mangled as 'Z'. (They're ignored because their associated
1472 // functionality isn't implemented, and probably never will be.)
1473 Out << 'Z';
1474}
1475
1476void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1477 SourceRange Range) {
1478 // Probably should be mangled as a template instantiation; need to see what
1479 // VC does first.
1480 DiagnosticsEngine &Diags = Context.getDiags();
1481 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1482 "cannot mangle this unresolved dependent type yet");
1483 Diags.Report(Range.getBegin(), DiagID)
1484 << Range;
1485}
1486
1487// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1488// <union-type> ::= T <name>
1489// <struct-type> ::= U <name>
1490// <class-type> ::= V <name>
1491// <enum-type> ::= W <size> <name>
1492void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001493 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001494}
1495void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001496 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001497}
David Majnemer02c44f02013-08-05 22:26:46 +00001498void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1499 switch (TD->getTagKind()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001500 case TTK_Union:
1501 Out << 'T';
1502 break;
1503 case TTK_Struct:
1504 case TTK_Interface:
1505 Out << 'U';
1506 break;
1507 case TTK_Class:
1508 Out << 'V';
1509 break;
1510 case TTK_Enum:
1511 Out << 'W';
1512 Out << getASTContext().getTypeSizeInChars(
David Majnemer02c44f02013-08-05 22:26:46 +00001513 cast<EnumDecl>(TD)->getIntegerType()).getQuantity();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001514 break;
1515 }
David Majnemer02c44f02013-08-05 22:26:46 +00001516 mangleName(TD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001517}
1518
1519// <type> ::= <array-type>
1520// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1521// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001522// <element-type> # as global, E is never required
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001523// It's supposed to be the other way around, but for some strange reason, it
1524// isn't. Today this behavior is retained for the sole purpose of backwards
1525// compatibility.
David Majnemer58e4cd02013-09-11 04:44:30 +00001526void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001527 // This isn't a recursive mangling, so now we have to do it all in this
1528 // one call.
David Majnemer58e4cd02013-09-11 04:44:30 +00001529 manglePointerQualifiers(T->getElementType().getQualifiers());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001530 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001531}
1532void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1533 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001534 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001535}
1536void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *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 DependentSizedArrayType *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 IncompleteArrayType *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}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001548void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001549 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001550 SmallVector<llvm::APInt, 3> Dimensions;
1551 for (;;) {
1552 if (const ConstantArrayType *CAT =
1553 getASTContext().getAsConstantArrayType(ElementTy)) {
1554 Dimensions.push_back(CAT->getSize());
1555 ElementTy = CAT->getElementType();
1556 } else if (ElementTy->isVariableArrayType()) {
1557 const VariableArrayType *VAT =
1558 getASTContext().getAsVariableArrayType(ElementTy);
1559 DiagnosticsEngine &Diags = Context.getDiags();
1560 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1561 "cannot mangle this variable-length array yet");
1562 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1563 << VAT->getBracketsRange();
1564 return;
1565 } else if (ElementTy->isDependentSizedArrayType()) {
1566 // The dependent expression has to be folded into a constant (TODO).
1567 const DependentSizedArrayType *DSAT =
1568 getASTContext().getAsDependentSizedArrayType(ElementTy);
1569 DiagnosticsEngine &Diags = Context.getDiags();
1570 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1571 "cannot mangle this dependent-length array yet");
1572 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1573 << DSAT->getBracketsRange();
1574 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001575 } else if (const IncompleteArrayType *IAT =
1576 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1577 Dimensions.push_back(llvm::APInt(32, 0));
1578 ElementTy = IAT->getElementType();
1579 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001580 else break;
1581 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001582 Out << 'Y';
1583 // <dimension-count> ::= <number> # number of extra dimensions
1584 mangleNumber(Dimensions.size());
1585 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1586 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001587 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001588}
1589
1590// <type> ::= <pointer-to-member-type>
1591// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1592// <class name> <type>
1593void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1594 SourceRange Range) {
1595 QualType PointeeType = T->getPointeeType();
1596 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1597 Out << '8';
1598 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +00001599 mangleFunctionType(FPT, 0, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001600 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001601 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1602 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001603 mangleQualifiers(PointeeType.getQualifiers(), true);
1604 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001605 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001606 }
1607}
1608
1609void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1610 SourceRange Range) {
1611 DiagnosticsEngine &Diags = Context.getDiags();
1612 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1613 "cannot mangle this template type parameter type yet");
1614 Diags.Report(Range.getBegin(), DiagID)
1615 << Range;
1616}
1617
1618void MicrosoftCXXNameMangler::mangleType(
1619 const SubstTemplateTypeParmPackType *T,
1620 SourceRange Range) {
1621 DiagnosticsEngine &Diags = Context.getDiags();
1622 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1623 "cannot mangle this substituted parameter pack yet");
1624 Diags.Report(Range.getBegin(), DiagID)
1625 << Range;
1626}
1627
1628// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001629// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1630// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001631void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1632 SourceRange Range) {
1633 QualType PointeeTy = T->getPointeeType();
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001634 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1635 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001636 mangleType(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001637}
1638void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1639 SourceRange Range) {
1640 // Object pointers never have qualifiers.
1641 Out << 'A';
David Majnemer1c7a4092013-08-15 08:13:23 +00001642 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1643 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001644 mangleType(T->getPointeeType(), Range);
1645}
1646
1647// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001648// <reference-type> ::= A E? <cvr-qualifiers> <type>
1649// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001650void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1651 SourceRange Range) {
1652 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001653 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1654 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001655 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001656}
1657
1658// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001659// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1660// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001661void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1662 SourceRange Range) {
1663 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001664 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1665 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001666 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001667}
1668
1669void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1670 SourceRange Range) {
1671 DiagnosticsEngine &Diags = Context.getDiags();
1672 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1673 "cannot mangle this complex number type yet");
1674 Diags.Report(Range.getBegin(), DiagID)
1675 << Range;
1676}
1677
1678void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1679 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001680 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1681 assert(ET && "vectors with non-builtin elements are unsupported");
1682 uint64_t Width = getASTContext().getTypeSize(T);
1683 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1684 // doesn't match the Intel types uses a custom mangling below.
1685 bool IntelVector = true;
1686 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1687 Out << "T__m64";
1688 } else if (Width == 128 || Width == 256) {
1689 if (ET->getKind() == BuiltinType::Float)
1690 Out << "T__m" << Width;
1691 else if (ET->getKind() == BuiltinType::LongLong)
1692 Out << "T__m" << Width << 'i';
1693 else if (ET->getKind() == BuiltinType::Double)
1694 Out << "U__m" << Width << 'd';
1695 else
1696 IntelVector = false;
1697 } else {
1698 IntelVector = false;
1699 }
1700
1701 if (!IntelVector) {
1702 // The MS ABI doesn't have a special mangling for vector types, so we define
1703 // our own mangling to handle uses of __vector_size__ on user-specified
1704 // types, and for extensions like __v4sf.
1705 Out << "T__clang_vec" << T->getNumElements() << '_';
1706 mangleType(ET, Range);
1707 }
1708
1709 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001710}
Reid Kleckner1232e272013-03-26 16:56:59 +00001711
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001712void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1713 SourceRange Range) {
1714 DiagnosticsEngine &Diags = Context.getDiags();
1715 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1716 "cannot mangle this extended vector type yet");
1717 Diags.Report(Range.getBegin(), DiagID)
1718 << Range;
1719}
1720void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1721 SourceRange Range) {
1722 DiagnosticsEngine &Diags = Context.getDiags();
1723 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1724 "cannot mangle this dependent-sized extended vector type yet");
1725 Diags.Report(Range.getBegin(), DiagID)
1726 << Range;
1727}
1728
1729void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1730 SourceRange) {
1731 // ObjC interfaces have structs underlying them.
1732 Out << 'U';
1733 mangleName(T->getDecl());
1734}
1735
1736void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1737 SourceRange Range) {
1738 // We don't allow overloading by different protocol qualification,
1739 // so mangling them isn't necessary.
1740 mangleType(T->getBaseType(), Range);
1741}
1742
1743void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1744 SourceRange Range) {
1745 Out << "_E";
1746
1747 QualType pointee = T->getPointeeType();
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +00001748 mangleFunctionType(pointee->castAs<FunctionProtoType>());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001749}
1750
David Majnemer360d23e2013-08-16 08:29:13 +00001751void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1752 SourceRange) {
1753 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001754}
1755
1756void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1757 SourceRange Range) {
1758 DiagnosticsEngine &Diags = Context.getDiags();
1759 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1760 "cannot mangle this template specialization type yet");
1761 Diags.Report(Range.getBegin(), DiagID)
1762 << Range;
1763}
1764
1765void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1766 SourceRange Range) {
1767 DiagnosticsEngine &Diags = Context.getDiags();
1768 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1769 "cannot mangle this dependent name type yet");
1770 Diags.Report(Range.getBegin(), DiagID)
1771 << Range;
1772}
1773
1774void MicrosoftCXXNameMangler::mangleType(
1775 const DependentTemplateSpecializationType *T,
1776 SourceRange Range) {
1777 DiagnosticsEngine &Diags = Context.getDiags();
1778 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1779 "cannot mangle this dependent template specialization type yet");
1780 Diags.Report(Range.getBegin(), DiagID)
1781 << Range;
1782}
1783
1784void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1785 SourceRange Range) {
1786 DiagnosticsEngine &Diags = Context.getDiags();
1787 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1788 "cannot mangle this pack expansion yet");
1789 Diags.Report(Range.getBegin(), DiagID)
1790 << Range;
1791}
1792
1793void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1794 SourceRange Range) {
1795 DiagnosticsEngine &Diags = Context.getDiags();
1796 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1797 "cannot mangle this typeof(type) yet");
1798 Diags.Report(Range.getBegin(), DiagID)
1799 << Range;
1800}
1801
1802void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1803 SourceRange Range) {
1804 DiagnosticsEngine &Diags = Context.getDiags();
1805 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1806 "cannot mangle this typeof(expression) yet");
1807 Diags.Report(Range.getBegin(), DiagID)
1808 << Range;
1809}
1810
1811void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1812 SourceRange Range) {
1813 DiagnosticsEngine &Diags = Context.getDiags();
1814 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1815 "cannot mangle this decltype() yet");
1816 Diags.Report(Range.getBegin(), DiagID)
1817 << Range;
1818}
1819
1820void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1821 SourceRange Range) {
1822 DiagnosticsEngine &Diags = Context.getDiags();
1823 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1824 "cannot mangle this unary transform type yet");
1825 Diags.Report(Range.getBegin(), DiagID)
1826 << Range;
1827}
1828
1829void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1830 DiagnosticsEngine &Diags = Context.getDiags();
1831 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1832 "cannot mangle this 'auto' type yet");
1833 Diags.Report(Range.getBegin(), DiagID)
1834 << Range;
1835}
1836
1837void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1838 SourceRange Range) {
1839 DiagnosticsEngine &Diags = Context.getDiags();
1840 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1841 "cannot mangle this C11 atomic type yet");
1842 Diags.Report(Range.getBegin(), DiagID)
1843 << Range;
1844}
1845
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001846void MicrosoftMangleContextImpl::mangleName(const NamedDecl *D,
1847 raw_ostream &Out) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001848 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1849 "Invalid mangleName() call, argument is not a variable or function!");
1850 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1851 "Invalid mangleName() call on 'structor decl!");
1852
1853 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1854 getASTContext().getSourceManager(),
1855 "Mangling declaration");
1856
1857 MicrosoftCXXNameMangler Mangler(*this, Out);
1858 return Mangler.mangle(D);
1859}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001860
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001861void MicrosoftMangleContextImpl::mangleThunk(const CXXMethodDecl *MD,
1862 const ThunkInfo &Thunk,
1863 raw_ostream &Out) {
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001864 // FIXME: this is not yet a complete implementation, but merely a
1865 // reasonably-working stub to avoid crashing when required to emit a thunk.
1866 MicrosoftCXXNameMangler Mangler(*this, Out);
1867 Out << "\01?";
1868 Mangler.mangleName(MD);
1869 if (Thunk.This.NonVirtual != 0) {
1870 // FIXME: add support for protected/private or use mangleFunctionClass.
1871 Out << "W";
1872 llvm::APSInt APSNumber(/*BitWidth=*/32 /*FIXME: check on x64*/,
1873 /*isUnsigned=*/true);
1874 APSNumber = -Thunk.This.NonVirtual;
1875 Mangler.mangleNumber(APSNumber);
1876 } else {
1877 // FIXME: add support for protected/private or use mangleFunctionClass.
1878 Out << "Q";
1879 }
1880 // FIXME: mangle return adjustment? Most likely includes using an overridee FPT?
Timur Iskhodzhanov8a5fb992013-10-04 11:25:05 +00001881 Mangler.mangleFunctionType(MD->getType()->castAs<FunctionProtoType>(), MD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001882}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001883
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001884void MicrosoftMangleContextImpl::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1885 CXXDtorType Type,
1886 const ThisAdjustment &,
1887 raw_ostream &) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001888 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1889 "cannot mangle thunk for this destructor yet");
1890 getDiags().Report(DD->getLocation(), DiagID);
1891}
Reid Kleckner90633022013-06-19 15:20:38 +00001892
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001893void MicrosoftMangleContextImpl::mangleCXXVFTable(
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001894 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1895 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001896 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1897 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001898 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001899 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001900 MicrosoftCXXNameMangler Mangler(*this, Out);
1901 Mangler.getStream() << "\01??_7";
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001902 Mangler.mangleName(Derived);
1903 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
1904 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1905 E = BasePath.end();
1906 I != E; ++I) {
1907 Mangler.mangleName(*I);
1908 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001909 Mangler.getStream() << '@';
1910}
Reid Kleckner90633022013-06-19 15:20:38 +00001911
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001912void MicrosoftMangleContextImpl::mangleCXXVBTable(
Reid Kleckner90633022013-06-19 15:20:38 +00001913 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1914 raw_ostream &Out) {
1915 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1916 // <cvr-qualifiers> [<name>] @
1917 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1918 // is always '7' for vbtables.
1919 MicrosoftCXXNameMangler Mangler(*this, Out);
1920 Mangler.getStream() << "\01??_8";
1921 Mangler.mangleName(Derived);
1922 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1923 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1924 E = BasePath.end();
1925 I != E; ++I) {
1926 Mangler.mangleName(*I);
1927 }
1928 Mangler.getStream() << '@';
1929}
1930
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001931void MicrosoftMangleContextImpl::mangleCXXRTTI(QualType T, raw_ostream &) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001932 // FIXME: Give a location...
1933 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1934 "cannot mangle RTTI descriptors for type %0 yet");
1935 getDiags().Report(DiagID)
1936 << T.getBaseTypeIdentifier();
1937}
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001938
1939void MicrosoftMangleContextImpl::mangleCXXRTTIName(QualType T, raw_ostream &) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001940 // FIXME: Give a location...
1941 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1942 "cannot mangle the name of type %0 into RTTI descriptors yet");
1943 getDiags().Report(DiagID)
1944 << T.getBaseTypeIdentifier();
1945}
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001946
1947void MicrosoftMangleContextImpl::mangleCXXCtor(const CXXConstructorDecl *D,
1948 CXXCtorType Type,
1949 raw_ostream &Out) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001950 MicrosoftCXXNameMangler mangler(*this, Out);
1951 mangler.mangle(D);
1952}
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001953
1954void MicrosoftMangleContextImpl::mangleCXXDtor(const CXXDestructorDecl *D,
1955 CXXDtorType Type,
1956 raw_ostream &Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001957 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001958 mangler.mangle(D);
1959}
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001960
1961void MicrosoftMangleContextImpl::mangleReferenceTemporary(const VarDecl *VD,
1962 raw_ostream &) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001963 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1964 "cannot mangle this reference temporary yet");
1965 getDiags().Report(VD->getLocation(), DiagID);
1966}
1967
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001968void MicrosoftMangleContextImpl::mangleStaticGuardVariable(const VarDecl *VD,
1969 raw_ostream &Out) {
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001970 // <guard-name> ::= ?_B <postfix> @51
1971 // ::= ?$S <guard-num> @ <postfix> @4IA
1972
1973 // The first mangling is what MSVC uses to guard static locals in inline
1974 // functions. It uses a different mangling in external functions to support
1975 // guarding more than 32 variables. MSVC rejects inline functions with more
1976 // than 32 static locals. We don't fully implement the second mangling
1977 // because those guards are not externally visible, and instead use LLVM's
1978 // default renaming when creating a new guard variable.
1979 MicrosoftCXXNameMangler Mangler(*this, Out);
1980
1981 bool Visible = VD->isExternallyVisible();
1982 // <operator-name> ::= ?_B # local static guard
1983 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@");
1984 Mangler.manglePostfix(VD->getDeclContext());
1985 Mangler.getStream() << (Visible ? "@51" : "@4IA");
1986}
1987
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001988void MicrosoftMangleContextImpl::mangleInitFiniStub(const VarDecl *D,
1989 raw_ostream &Out,
1990 char CharCode) {
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00001991 MicrosoftCXXNameMangler Mangler(*this, Out);
1992 Mangler.getStream() << "\01??__" << CharCode;
1993 Mangler.mangleName(D);
1994 // This is the function class mangling. These stubs are global, non-variadic,
1995 // cdecl functions that return void and take no args.
1996 Mangler.getStream() << "YAXXZ";
1997}
1998
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001999void MicrosoftMangleContextImpl::mangleDynamicInitializer(const VarDecl *D,
2000 raw_ostream &Out) {
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00002001 // <initializer-name> ::= ?__E <name> YAXXZ
2002 mangleInitFiniStub(D, Out, 'E');
2003}
2004
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00002005void
2006MicrosoftMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D,
2007 raw_ostream &Out) {
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00002008 // <destructor-name> ::= ?__F <name> YAXXZ
2009 mangleInitFiniStub(D, Out, 'F');
Reid Kleckner942f9fe2013-09-10 20:14:30 +00002010}
2011
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00002012MicrosoftMangleContext *
2013MicrosoftMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags) {
2014 return new MicrosoftMangleContextImpl(Context, Diags);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002015}