blob: 211e6e79314e893ae05f8e2f33218083bf8c87f7 [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
175/// MicrosoftMangleContext - Overrides the default MangleContext for the
176/// Microsoft Visual C++ ABI.
177class MicrosoftMangleContext : public MangleContext {
178public:
179 MicrosoftMangleContext(ASTContext &Context,
180 DiagnosticsEngine &Diags) : MangleContext(Context, Diags) { }
181 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 &);
189 virtual void mangleCXXVTable(const CXXRecordDecl *RD,
190 raw_ostream &);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000191 virtual void mangleCXXVFTable(const CXXRecordDecl *Derived,
192 ArrayRef<const CXXRecordDecl *> BasePath,
193 raw_ostream &Out);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000194 virtual void mangleCXXVTT(const CXXRecordDecl *RD,
195 raw_ostream &);
Reid Kleckner90633022013-06-19 15:20:38 +0000196 virtual void mangleCXXVBTable(const CXXRecordDecl *Derived,
197 ArrayRef<const CXXRecordDecl *> BasePath,
198 raw_ostream &Out);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000199 virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
200 const CXXRecordDecl *Type,
201 raw_ostream &);
202 virtual void mangleCXXRTTI(QualType T, raw_ostream &);
203 virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
204 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
205 raw_ostream &);
206 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
207 raw_ostream &);
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000208 virtual void mangleReferenceTemporary(const VarDecl *, raw_ostream &);
209 virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out);
Reid Klecknerc5c6fa72013-09-10 20:43:12 +0000210 virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out);
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000211 virtual void mangleDynamicAtExitDestructor(const VarDecl *D,
212 raw_ostream &Out);
Reid Klecknerc5c6fa72013-09-10 20:43:12 +0000213
214private:
215 void mangleInitFiniStub(const VarDecl *D, raw_ostream &Out, char CharCode);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000216};
217
218}
219
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000220bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
221 // In C, functions with no attributes never need to be mangled. Fastpath them.
222 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
223 return false;
224
225 // Any decl can be declared with __asm("foo") on it, and this takes precedence
226 // over all other naming in the .o file.
227 if (D->hasAttr<AsmLabelAttr>())
228 return true;
229
David Majnemercab7dad2013-09-13 09:03:14 +0000230 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
231 LanguageLinkage L = FD->getLanguageLinkage();
232 // Overloadable functions need mangling.
233 if (FD->hasAttr<OverloadableAttr>())
234 return true;
235
David Majnemere9f6f332013-09-16 22:44:20 +0000236 // The ABI expects that we would never mangle "typical" user-defined entry
237 // points regardless of visibility or freestanding-ness.
238 //
239 // N.B. This is distinct from asking about "main". "main" has a lot of
240 // special rules associated with it in the standard while these
241 // user-defined entry points are outside of the purview of the standard.
242 // For example, there can be only one definition for "main" in a standards
243 // compliant program; however nothing forbids the existence of wmain and
244 // WinMain in the same translation unit.
245 if (FD->isMSVCRTEntryPoint())
David Majnemercab7dad2013-09-13 09:03:14 +0000246 return false;
247
248 // C++ functions and those whose names are not a simple identifier need
249 // mangling.
250 if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage)
251 return true;
252
253 // C functions are not mangled.
254 if (L == CLanguageLinkage)
255 return false;
256 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000257
258 // Otherwise, no mangling is done outside C++ mode.
259 if (!getASTContext().getLangOpts().CPlusPlus)
260 return false;
261
David Majnemercab7dad2013-09-13 09:03:14 +0000262 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
263 // C variables are not mangled.
264 if (VD->isExternC())
265 return false;
266
267 // Variables at global scope with non-internal linkage are not mangled.
268 const DeclContext *DC = getEffectiveDeclContext(D);
269 // Check for extern variable declared locally.
270 if (DC->isFunctionOrMethod() && D->hasLinkage())
271 while (!DC->isNamespace() && !DC->isTranslationUnit())
272 DC = getEffectiveParentContext(DC);
273
274 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage &&
275 !isa<VarTemplateSpecializationDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000276 return false;
277 }
278
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000279 return true;
280}
281
282void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
283 StringRef Prefix) {
284 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
285 // Therefore it's really important that we don't decorate the
286 // name with leading underscores or leading/trailing at signs. So, by
287 // default, we emit an asm marker at the start so we get the name right.
288 // Callers can override this with a custom prefix.
289
290 // Any decl can be declared with __asm("foo") on it, and this takes precedence
291 // over all other naming in the .o file.
292 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
293 // If we have an asm name, then we use it as the mangling.
294 Out << '\01' << ALA->getLabel();
295 return;
296 }
297
298 // <mangled-name> ::= ? <name> <type-encoding>
299 Out << Prefix;
300 mangleName(D);
301 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
302 mangleFunctionEncoding(FD);
303 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
304 mangleVariableEncoding(VD);
305 else {
306 // TODO: Fields? Can MSVC even mangle them?
307 // Issue a diagnostic for now.
308 DiagnosticsEngine &Diags = Context.getDiags();
309 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
310 "cannot mangle this declaration yet");
311 Diags.Report(D->getLocation(), DiagID)
312 << D->getSourceRange();
313 }
314}
315
316void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
317 // <type-encoding> ::= <function-class> <function-type>
318
Reid Klecknerf21818d2013-06-24 19:21:52 +0000319 // Since MSVC operates on the type as written and not the canonical type, it
320 // actually matters which decl we have here. MSVC appears to choose the
321 // first, since it is most likely to be the declaration in a header file.
322 FD = FD->getFirstDeclaration();
323
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000324 // We should never ever see a FunctionNoProtoType at this point.
325 // We don't even know how to mangle their types anyway :).
Reid Klecknerf21818d2013-06-24 19:21:52 +0000326 TypeSourceInfo *TSI = FD->getTypeSourceInfo();
327 QualType T = TSI ? TSI->getType() : FD->getType();
328 const FunctionProtoType *FT = T->castAs<FunctionProtoType>();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000329
330 bool InStructor = false, InInstMethod = false;
331 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
332 if (MD) {
333 if (MD->isInstance())
334 InInstMethod = true;
335 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
336 InStructor = true;
337 }
338
David Majnemercab7dad2013-09-13 09:03:14 +0000339 // extern "C" functions can hold entities that must be mangled.
340 // As it stands, these functions still need to get expressed in the full
341 // external name. They have their class and type omitted, replaced with '9'.
342 if (Context.shouldMangleDeclName(FD)) {
343 // First, the function class.
344 mangleFunctionClass(FD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000345
David Majnemercab7dad2013-09-13 09:03:14 +0000346 mangleFunctionType(FT, FD, InStructor, InInstMethod);
347 } else
348 Out << '9';
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000349}
350
351void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
352 // <type-encoding> ::= <storage-class> <variable-type>
353 // <storage-class> ::= 0 # private static member
354 // ::= 1 # protected static member
355 // ::= 2 # public static member
356 // ::= 3 # global
357 // ::= 4 # static local
358
359 // The first character in the encoding (after the name) is the storage class.
360 if (VD->isStaticDataMember()) {
361 // If it's a static member, it also encodes the access level.
362 switch (VD->getAccess()) {
363 default:
364 case AS_private: Out << '0'; break;
365 case AS_protected: Out << '1'; break;
366 case AS_public: Out << '2'; break;
367 }
368 }
369 else if (!VD->isStaticLocal())
370 Out << '3';
371 else
372 Out << '4';
373 // Now mangle the type.
374 // <variable-type> ::= <type> <cvr-qualifiers>
375 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
376 // Pointers and references are odd. The type of 'int * const foo;' gets
377 // mangled as 'QAHA' instead of 'PAHB', for example.
378 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
379 QualType Ty = TL.getType();
David Majnemer1c7a4092013-08-15 08:13:23 +0000380 if (Ty->isPointerType() || Ty->isReferenceType() ||
381 Ty->isMemberPointerType()) {
David Majnemer17ffbd02013-08-09 05:56:24 +0000382 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000383 if (PointersAre64Bit)
384 Out << 'E';
385 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) {
386 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true);
387 // Member pointers are suffixed with a back reference to the member
388 // pointer's class name.
389 mangleName(MPT->getClass()->getAsCXXRecordDecl());
390 } else
391 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
David Majnemer17ffbd02013-08-09 05:56:24 +0000392 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000393 // Global arrays are funny, too.
David Majnemer58e4cd02013-09-11 04:44:30 +0000394 mangleDecayedArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000395 if (AT->getElementType()->isArrayType())
396 Out << 'A';
397 else
398 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000399 } else {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000400 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000401 mangleQualifiers(Ty.getLocalQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000402 }
403}
404
405void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
406 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
407 const DeclContext *DC = ND->getDeclContext();
408
409 // Always start with the unqualified name.
410 mangleUnqualifiedName(ND);
411
412 // If this is an extern variable declared locally, the relevant DeclContext
413 // is that of the containing namespace, or the translation unit.
414 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
415 while (!DC->isNamespace() && !DC->isTranslationUnit())
416 DC = DC->getParent();
417
418 manglePostfix(DC);
419
420 // Terminate the whole name with an '@'.
421 Out << '@';
422}
423
424void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
425 llvm::APSInt APSNumber(/*BitWidth=*/64, /*isUnsigned=*/false);
426 APSNumber = Number;
427 mangleNumber(APSNumber);
428}
429
430void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
431 // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
432 // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
433 // ::= [?] @ # 0 (alternate mangling, not emitted by VC)
434 if (Value.isSigned() && Value.isNegative()) {
435 Out << '?';
436 mangleNumber(llvm::APSInt(Value.abs()));
437 return;
438 }
439 llvm::APSInt Temp(Value);
440 // There's a special shorter mangling for 0, but Microsoft
441 // chose not to use it. Instead, 0 gets mangled as "A@". Oh well...
442 if (Value.uge(1) && Value.ule(10)) {
443 --Temp;
444 Temp.print(Out, false);
445 } else {
446 // We have to build up the encoding in reverse order, so it will come
447 // out right when we write it out.
448 char Encoding[64];
449 char *EndPtr = Encoding+sizeof(Encoding);
450 char *CurPtr = EndPtr;
451 llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned());
452 NibbleMask = 0xf;
453 do {
454 *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf);
455 Temp = Temp.lshr(4);
456 } while (Temp != 0);
457 Out.write(CurPtr, EndPtr-CurPtr);
458 Out << '@';
459 }
460}
461
462static const TemplateDecl *
Reid Klecknerf16216c2013-03-20 01:40:23 +0000463isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000464 // Check if we have a function template.
465 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
466 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000467 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000468 return TD;
469 }
470 }
471
472 // Check if we have a class template.
473 if (const ClassTemplateSpecializationDecl *Spec =
Reid Klecknerf16216c2013-03-20 01:40:23 +0000474 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
475 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000476 return Spec->getSpecializedTemplate();
477 }
478
479 return 0;
480}
481
482void
483MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
484 DeclarationName Name) {
485 // <unqualified-name> ::= <operator-name>
486 // ::= <ctor-dtor-name>
487 // ::= <source-name>
488 // ::= <template-name>
Reid Klecknerf16216c2013-03-20 01:40:23 +0000489
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000490 // Check if we have a template.
Reid Klecknerf16216c2013-03-20 01:40:23 +0000491 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000492 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Kleckner3be37d12013-07-13 00:43:39 +0000493 // Function templates aren't considered for name back referencing. This
494 // makes sense since function templates aren't likely to occur multiple
495 // times in a symbol.
496 // FIXME: Test alias template mangling with MSVC 2013.
497 if (!isa<ClassTemplateDecl>(TD)) {
498 mangleTemplateInstantiationName(TD, *TemplateArgs);
499 return;
500 }
501
502 // We have a class template.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000503 // Here comes the tricky thing: if we need to mangle something like
504 // void foo(A::X<Y>, B::X<Y>),
505 // the X<Y> part is aliased. However, if you need to mangle
506 // void foo(A::X<A::Y>, A::X<B::Y>),
507 // the A::X<> part is not aliased.
508 // That said, from the mangler's perspective we have a structure like this:
509 // namespace[s] -> type[ -> template-parameters]
510 // but from the Clang perspective we have
511 // type [ -> template-parameters]
512 // \-> namespace[s]
513 // What we do is we create a new mangler, mangle the same type (without
514 // a namespace suffix) using the extra mangler with back references
515 // disabled (to avoid infinite recursion) and then use the mangled type
516 // name as a key to check the mangling of different types for aliasing.
517
518 std::string BackReferenceKey;
519 BackRefMap::iterator Found;
520 if (UseNameBackReferences) {
521 llvm::raw_string_ostream Stream(BackReferenceKey);
522 MicrosoftCXXNameMangler Extra(Context, Stream);
523 Extra.disableBackReferences();
524 Extra.mangleUnqualifiedName(ND, Name);
525 Stream.flush();
526
527 Found = NameBackReferences.find(BackReferenceKey);
528 }
529 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000530 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000531 if (UseNameBackReferences && NameBackReferences.size() < 10) {
532 size_t Size = NameBackReferences.size();
533 NameBackReferences[BackReferenceKey] = Size;
534 }
535 } else {
536 Out << Found->second;
537 }
538 return;
539 }
540
541 switch (Name.getNameKind()) {
542 case DeclarationName::Identifier: {
543 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
544 mangleSourceName(II);
545 break;
546 }
547
548 // Otherwise, an anonymous entity. We must have a declaration.
549 assert(ND && "mangling empty name without declaration");
550
551 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
552 if (NS->isAnonymousNamespace()) {
553 Out << "?A@";
554 break;
555 }
556 }
557
558 // We must have an anonymous struct.
559 const TagDecl *TD = cast<TagDecl>(ND);
560 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
561 assert(TD->getDeclContext() == D->getDeclContext() &&
562 "Typedef should not be in another decl context!");
563 assert(D->getDeclName().getAsIdentifierInfo() &&
564 "Typedef was not named!");
565 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
566 break;
567 }
568
David Majnemeraa824612013-09-17 23:57:10 +0000569 if (TD->hasDeclaratorForAnonDecl())
570 // Anonymous types with no tag or typedef get the name of their
571 // declarator mangled in.
572 Out << "<unnamed-type-" << TD->getDeclaratorForAnonDecl()->getName()
573 << ">@";
574 else
575 // Anonymous types with no tag, no typedef, or declarator get
576 // '<unnamed-tag>@'.
577 Out << "<unnamed-tag>@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000578 break;
579 }
580
581 case DeclarationName::ObjCZeroArgSelector:
582 case DeclarationName::ObjCOneArgSelector:
583 case DeclarationName::ObjCMultiArgSelector:
584 llvm_unreachable("Can't mangle Objective-C selector names here!");
585
586 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000587 if (ND == Structor) {
588 assert(StructorType == Ctor_Complete &&
589 "Should never be asked to mangle a ctor other than complete");
590 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000591 Out << "?0";
592 break;
593
594 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000595 if (ND == Structor)
596 // If the named decl is the C++ destructor we're mangling,
597 // use the type we were given.
598 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
599 else
Reid Klecknera4130ba2013-07-22 13:51:44 +0000600 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000601 // class with a destructor is declared within a destructor.
Reid Klecknera4130ba2013-07-22 13:51:44 +0000602 mangleCXXDtorType(Dtor_Base);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000603 break;
604
605 case DeclarationName::CXXConversionFunctionName:
606 // <operator-name> ::= ?B # (cast)
607 // The target type is encoded as the return type.
608 Out << "?B";
609 break;
610
611 case DeclarationName::CXXOperatorName:
612 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
613 break;
614
615 case DeclarationName::CXXLiteralOperatorName: {
616 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
617 DiagnosticsEngine Diags = Context.getDiags();
618 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
619 "cannot mangle this literal operator yet");
620 Diags.Report(ND->getLocation(), DiagID);
621 break;
622 }
623
624 case DeclarationName::CXXUsingDirective:
625 llvm_unreachable("Can't mangle a using directive name!");
626 }
627}
628
629void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
630 bool NoFunction) {
631 // <postfix> ::= <unqualified-name> [<postfix>]
632 // ::= <substitution> [<postfix>]
633
634 if (!DC) return;
635
636 while (isa<LinkageSpecDecl>(DC))
637 DC = DC->getParent();
638
639 if (DC->isTranslationUnit())
640 return;
641
642 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedmane5798892013-07-10 01:13:27 +0000643 DiagnosticsEngine Diags = Context.getDiags();
644 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
645 "cannot mangle a local inside this block yet");
646 Diags.Report(BD->getLocation(), DiagID);
647
648 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
649 // for how this should be done.
650 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000651 Out << '@';
652 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000653 } else if (isa<CapturedDecl>(DC)) {
654 // Skip CapturedDecl context.
655 manglePostfix(DC->getParent(), NoFunction);
656 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000657 }
658
659 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
660 return;
661 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
662 mangleObjCMethodName(Method);
663 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
664 mangleLocalName(Func);
665 else {
666 mangleUnqualifiedName(cast<NamedDecl>(DC));
667 manglePostfix(DC->getParent(), NoFunction);
668 }
669}
670
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000671void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000672 // Microsoft uses the names on the case labels for these dtor variants. Clang
673 // uses the Itanium terminology internally. Everything in this ABI delegates
674 // towards the base dtor.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000675 switch (T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000676 // <operator-name> ::= ?1 # destructor
677 case Dtor_Base: Out << "?1"; return;
678 // <operator-name> ::= ?_D # vbase destructor
679 case Dtor_Complete: Out << "?_D"; return;
680 // <operator-name> ::= ?_G # scalar deleting destructor
681 case Dtor_Deleting: Out << "?_G"; return;
682 // <operator-name> ::= ?_E # vector deleting destructor
683 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
684 // it.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000685 }
686 llvm_unreachable("Unsupported dtor type?");
687}
688
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000689void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
690 SourceLocation Loc) {
691 switch (OO) {
692 // ?0 # constructor
693 // ?1 # destructor
694 // <operator-name> ::= ?2 # new
695 case OO_New: Out << "?2"; break;
696 // <operator-name> ::= ?3 # delete
697 case OO_Delete: Out << "?3"; break;
698 // <operator-name> ::= ?4 # =
699 case OO_Equal: Out << "?4"; break;
700 // <operator-name> ::= ?5 # >>
701 case OO_GreaterGreater: Out << "?5"; break;
702 // <operator-name> ::= ?6 # <<
703 case OO_LessLess: Out << "?6"; break;
704 // <operator-name> ::= ?7 # !
705 case OO_Exclaim: Out << "?7"; break;
706 // <operator-name> ::= ?8 # ==
707 case OO_EqualEqual: Out << "?8"; break;
708 // <operator-name> ::= ?9 # !=
709 case OO_ExclaimEqual: Out << "?9"; break;
710 // <operator-name> ::= ?A # []
711 case OO_Subscript: Out << "?A"; break;
712 // ?B # conversion
713 // <operator-name> ::= ?C # ->
714 case OO_Arrow: Out << "?C"; break;
715 // <operator-name> ::= ?D # *
716 case OO_Star: Out << "?D"; break;
717 // <operator-name> ::= ?E # ++
718 case OO_PlusPlus: Out << "?E"; break;
719 // <operator-name> ::= ?F # --
720 case OO_MinusMinus: Out << "?F"; break;
721 // <operator-name> ::= ?G # -
722 case OO_Minus: Out << "?G"; break;
723 // <operator-name> ::= ?H # +
724 case OO_Plus: Out << "?H"; break;
725 // <operator-name> ::= ?I # &
726 case OO_Amp: Out << "?I"; break;
727 // <operator-name> ::= ?J # ->*
728 case OO_ArrowStar: Out << "?J"; break;
729 // <operator-name> ::= ?K # /
730 case OO_Slash: Out << "?K"; break;
731 // <operator-name> ::= ?L # %
732 case OO_Percent: Out << "?L"; break;
733 // <operator-name> ::= ?M # <
734 case OO_Less: Out << "?M"; break;
735 // <operator-name> ::= ?N # <=
736 case OO_LessEqual: Out << "?N"; break;
737 // <operator-name> ::= ?O # >
738 case OO_Greater: Out << "?O"; break;
739 // <operator-name> ::= ?P # >=
740 case OO_GreaterEqual: Out << "?P"; break;
741 // <operator-name> ::= ?Q # ,
742 case OO_Comma: Out << "?Q"; break;
743 // <operator-name> ::= ?R # ()
744 case OO_Call: Out << "?R"; break;
745 // <operator-name> ::= ?S # ~
746 case OO_Tilde: Out << "?S"; break;
747 // <operator-name> ::= ?T # ^
748 case OO_Caret: Out << "?T"; break;
749 // <operator-name> ::= ?U # |
750 case OO_Pipe: Out << "?U"; break;
751 // <operator-name> ::= ?V # &&
752 case OO_AmpAmp: Out << "?V"; break;
753 // <operator-name> ::= ?W # ||
754 case OO_PipePipe: Out << "?W"; break;
755 // <operator-name> ::= ?X # *=
756 case OO_StarEqual: Out << "?X"; break;
757 // <operator-name> ::= ?Y # +=
758 case OO_PlusEqual: Out << "?Y"; break;
759 // <operator-name> ::= ?Z # -=
760 case OO_MinusEqual: Out << "?Z"; break;
761 // <operator-name> ::= ?_0 # /=
762 case OO_SlashEqual: Out << "?_0"; break;
763 // <operator-name> ::= ?_1 # %=
764 case OO_PercentEqual: Out << "?_1"; break;
765 // <operator-name> ::= ?_2 # >>=
766 case OO_GreaterGreaterEqual: Out << "?_2"; break;
767 // <operator-name> ::= ?_3 # <<=
768 case OO_LessLessEqual: Out << "?_3"; break;
769 // <operator-name> ::= ?_4 # &=
770 case OO_AmpEqual: Out << "?_4"; break;
771 // <operator-name> ::= ?_5 # |=
772 case OO_PipeEqual: Out << "?_5"; break;
773 // <operator-name> ::= ?_6 # ^=
774 case OO_CaretEqual: Out << "?_6"; break;
775 // ?_7 # vftable
776 // ?_8 # vbtable
777 // ?_9 # vcall
778 // ?_A # typeof
779 // ?_B # local static guard
780 // ?_C # string
781 // ?_D # vbase destructor
782 // ?_E # vector deleting destructor
783 // ?_F # default constructor closure
784 // ?_G # scalar deleting destructor
785 // ?_H # vector constructor iterator
786 // ?_I # vector destructor iterator
787 // ?_J # vector vbase constructor iterator
788 // ?_K # virtual displacement map
789 // ?_L # eh vector constructor iterator
790 // ?_M # eh vector destructor iterator
791 // ?_N # eh vector vbase constructor iterator
792 // ?_O # copy constructor closure
793 // ?_P<name> # udt returning <name>
794 // ?_Q # <unknown>
795 // ?_R0 # RTTI Type Descriptor
796 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
797 // ?_R2 # RTTI Base Class Array
798 // ?_R3 # RTTI Class Hierarchy Descriptor
799 // ?_R4 # RTTI Complete Object Locator
800 // ?_S # local vftable
801 // ?_T # local vftable constructor closure
802 // <operator-name> ::= ?_U # new[]
803 case OO_Array_New: Out << "?_U"; break;
804 // <operator-name> ::= ?_V # delete[]
805 case OO_Array_Delete: Out << "?_V"; break;
806
807 case OO_Conditional: {
808 DiagnosticsEngine &Diags = Context.getDiags();
809 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
810 "cannot mangle this conditional operator yet");
811 Diags.Report(Loc, DiagID);
812 break;
813 }
814
815 case OO_None:
816 case NUM_OVERLOADED_OPERATORS:
817 llvm_unreachable("Not an overloaded operator");
818 }
819}
820
821void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
822 // <source name> ::= <identifier> @
823 std::string key = II->getNameStart();
824 BackRefMap::iterator Found;
825 if (UseNameBackReferences)
826 Found = NameBackReferences.find(key);
827 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
828 Out << II->getName() << '@';
829 if (UseNameBackReferences && NameBackReferences.size() < 10) {
830 size_t Size = NameBackReferences.size();
831 NameBackReferences[key] = Size;
832 }
833 } else {
834 Out << Found->second;
835 }
836}
837
838void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
839 Context.mangleObjCMethodName(MD, Out);
840}
841
842// Find out how many function decls live above this one and return an integer
843// suitable for use as the number in a numbered anonymous scope.
844// TODO: Memoize.
845static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
846 const DeclContext *DC = FD->getParent();
847 int level = 1;
848
849 while (DC && !DC->isTranslationUnit()) {
850 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
851 DC = DC->getParent();
852 }
853
854 return 2*level;
855}
856
857void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
858 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
859 // <numbered-anonymous-scope> ::= ? <number>
860 // Even though the name is rendered in reverse order (e.g.
861 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
862 // innermost. So a method bar in class C local to function foo gets mangled
863 // as something like:
864 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
865 // This is more apparent when you have a type nested inside a method of a
866 // type nested inside a function. A method baz in class D local to method
867 // bar of class C local to function foo gets mangled as:
868 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
869 // This scheme is general enough to support GCC-style nested
870 // functions. You could have a method baz of class C inside a function bar
871 // inside a function foo, like so:
872 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
873 int NestLevel = getLocalNestingLevel(FD);
874 Out << '?';
875 mangleNumber(NestLevel);
876 Out << '?';
877 mangle(FD, "?");
878}
879
880void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
881 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000882 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000883 // <template-name> ::= <unscoped-template-name> <template-args>
884 // ::= <substitution>
885 // Always start with the unqualified name.
886
887 // Templates have their own context for back references.
888 ArgBackRefMap OuterArgsContext;
889 BackRefMap OuterTemplateContext;
890 NameBackReferences.swap(OuterTemplateContext);
891 TypeBackReferences.swap(OuterArgsContext);
892
893 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000894 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000895
896 // Restore the previous back reference contexts.
897 NameBackReferences.swap(OuterTemplateContext);
898 TypeBackReferences.swap(OuterArgsContext);
899}
900
901void
902MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
903 // <unscoped-template-name> ::= ?$ <unqualified-name>
904 Out << "?$";
905 mangleUnqualifiedName(TD);
906}
907
908void
909MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
910 bool IsBoolean) {
911 // <integer-literal> ::= $0 <number>
912 Out << "$0";
913 // Make sure booleans are encoded as 0/1.
914 if (IsBoolean && Value.getBoolValue())
915 mangleNumber(1);
916 else
917 mangleNumber(Value);
918}
919
920void
921MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
922 // See if this is a constant expression.
923 llvm::APSInt Value;
924 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
925 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
926 return;
927 }
928
David Majnemerc80eb462013-08-13 06:32:20 +0000929 const CXXUuidofExpr *UE = 0;
930 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
931 if (UO->getOpcode() == UO_AddrOf)
932 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
933 } else
934 UE = dyn_cast<CXXUuidofExpr>(E);
935
936 if (UE) {
937 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
938 // const __s_GUID _GUID_{lower case UUID with underscores}
939 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
940 std::string Name = "_GUID_" + Uuid.lower();
941 std::replace(Name.begin(), Name.end(), '-', '_');
942
David Majnemer26314e12013-08-13 09:17:25 +0000943 // If we had to peek through an address-of operator, treat this like we are
David Majnemerc80eb462013-08-13 06:32:20 +0000944 // dealing with a pointer type. Otherwise, treat it like a const reference.
945 //
946 // N.B. This matches up with the handling of TemplateArgument::Declaration
947 // in mangleTemplateArg
948 if (UE == E)
949 Out << "$E?";
950 else
951 Out << "$1?";
952 Out << Name << "@@3U__s_GUID@@B";
953 return;
954 }
955
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000956 // As bad as this diagnostic is, it's better than crashing.
957 DiagnosticsEngine &Diags = Context.getDiags();
958 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
959 "cannot yet mangle expression type %0");
960 Diags.Report(E->getExprLoc(), DiagID)
961 << E->getStmtClassName() << E->getSourceRange();
962}
963
964void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000965MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
966 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000967 // <template-args> ::= {<type> | <integer-literal>}+ @
968 unsigned NumTemplateArgs = TemplateArgs.size();
969 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000970 const TemplateArgument &TA = TemplateArgs[i];
David Majnemer309f6452013-08-27 08:21:25 +0000971 mangleTemplateArg(TD, TA);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000972 }
973 Out << '@';
974}
975
Reid Kleckner5d90d182013-07-02 18:10:07 +0000976void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
David Majnemer309f6452013-08-27 08:21:25 +0000977 const TemplateArgument &TA) {
Reid Kleckner5d90d182013-07-02 18:10:07 +0000978 switch (TA.getKind()) {
979 case TemplateArgument::Null:
980 llvm_unreachable("Can't mangle null template arguments!");
David Majnemer309f6452013-08-27 08:21:25 +0000981 case TemplateArgument::TemplateExpansion:
982 llvm_unreachable("Can't mangle template expansion arguments!");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000983 case TemplateArgument::Type: {
984 QualType T = TA.getAsType();
985 mangleType(T, SourceRange(), QMM_Escape);
986 break;
987 }
David Majnemerf2081f62013-08-13 01:25:35 +0000988 case TemplateArgument::Declaration: {
989 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl());
990 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000991 break;
David Majnemerf2081f62013-08-13 01:25:35 +0000992 }
Reid Kleckner5d90d182013-07-02 18:10:07 +0000993 case TemplateArgument::Integral:
994 mangleIntegerLiteral(TA.getAsIntegral(),
995 TA.getIntegralType()->isBooleanType());
996 break;
David Majnemer7802fc92013-08-05 21:33:59 +0000997 case TemplateArgument::NullPtr:
998 Out << "$0A@";
999 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +00001000 case TemplateArgument::Expression:
1001 mangleExpression(TA.getAsExpr());
1002 break;
1003 case TemplateArgument::Pack:
1004 // Unlike Itanium, there is no character code to indicate an argument pack.
Reid Kleckner5d90d182013-07-02 18:10:07 +00001005 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
1006 I != E; ++I)
David Majnemer309f6452013-08-27 08:21:25 +00001007 mangleTemplateArg(TD, *I);
Reid Kleckner5d90d182013-07-02 18:10:07 +00001008 break;
1009 case TemplateArgument::Template:
David Majnemer02c44f02013-08-05 22:26:46 +00001010 mangleType(cast<TagDecl>(
1011 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
1012 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +00001013 }
1014}
1015
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001016void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
1017 bool IsMember) {
1018 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
1019 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
1020 // 'I' means __restrict (32/64-bit).
1021 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
1022 // keyword!
1023 // <base-cvr-qualifiers> ::= A # near
1024 // ::= B # near const
1025 // ::= C # near volatile
1026 // ::= D # near const volatile
1027 // ::= E # far (16-bit)
1028 // ::= F # far const (16-bit)
1029 // ::= G # far volatile (16-bit)
1030 // ::= H # far const volatile (16-bit)
1031 // ::= I # huge (16-bit)
1032 // ::= J # huge const (16-bit)
1033 // ::= K # huge volatile (16-bit)
1034 // ::= L # huge const volatile (16-bit)
1035 // ::= M <basis> # based
1036 // ::= N <basis> # based const
1037 // ::= O <basis> # based volatile
1038 // ::= P <basis> # based const volatile
1039 // ::= Q # near member
1040 // ::= R # near const member
1041 // ::= S # near volatile member
1042 // ::= T # near const volatile member
1043 // ::= U # far member (16-bit)
1044 // ::= V # far const member (16-bit)
1045 // ::= W # far volatile member (16-bit)
1046 // ::= X # far const volatile member (16-bit)
1047 // ::= Y # huge member (16-bit)
1048 // ::= Z # huge const member (16-bit)
1049 // ::= 0 # huge volatile member (16-bit)
1050 // ::= 1 # huge const volatile member (16-bit)
1051 // ::= 2 <basis> # based member
1052 // ::= 3 <basis> # based const member
1053 // ::= 4 <basis> # based volatile member
1054 // ::= 5 <basis> # based const volatile member
1055 // ::= 6 # near function (pointers only)
1056 // ::= 7 # far function (pointers only)
1057 // ::= 8 # near method (pointers only)
1058 // ::= 9 # far method (pointers only)
1059 // ::= _A <basis> # based function (pointers only)
1060 // ::= _B <basis> # based function (far?) (pointers only)
1061 // ::= _C <basis> # based method (pointers only)
1062 // ::= _D <basis> # based method (far?) (pointers only)
1063 // ::= _E # block (Clang)
1064 // <basis> ::= 0 # __based(void)
1065 // ::= 1 # __based(segment)?
1066 // ::= 2 <name> # __based(name)
1067 // ::= 3 # ?
1068 // ::= 4 # ?
1069 // ::= 5 # not really based
1070 bool HasConst = Quals.hasConst(),
1071 HasVolatile = Quals.hasVolatile();
David Majnemerc0e64f32013-08-05 22:43:06 +00001072
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001073 if (!IsMember) {
1074 if (HasConst && HasVolatile) {
1075 Out << 'D';
1076 } else if (HasVolatile) {
1077 Out << 'C';
1078 } else if (HasConst) {
1079 Out << 'B';
1080 } else {
1081 Out << 'A';
1082 }
1083 } else {
1084 if (HasConst && HasVolatile) {
1085 Out << 'T';
1086 } else if (HasVolatile) {
1087 Out << 'S';
1088 } else if (HasConst) {
1089 Out << 'R';
1090 } else {
1091 Out << 'Q';
1092 }
1093 }
1094
1095 // FIXME: For now, just drop all extension qualifiers on the floor.
1096}
1097
1098void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1099 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1100 // ::= Q # const
1101 // ::= R # volatile
1102 // ::= S # const volatile
1103 bool HasConst = Quals.hasConst(),
1104 HasVolatile = Quals.hasVolatile();
1105 if (HasConst && HasVolatile) {
1106 Out << 'S';
1107 } else if (HasVolatile) {
1108 Out << 'R';
1109 } else if (HasConst) {
1110 Out << 'Q';
1111 } else {
1112 Out << 'P';
1113 }
1114}
1115
1116void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1117 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001118 // MSVC will backreference two canonically equivalent types that have slightly
1119 // different manglings when mangled alone.
David Majnemer58e4cd02013-09-11 04:44:30 +00001120
1121 // Decayed types do not match up with non-decayed versions of the same type.
1122 //
1123 // e.g.
1124 // void (*x)(void) will not form a backreference with void x(void)
1125 void *TypePtr;
1126 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1127 TypePtr = DT->getOriginalType().getCanonicalType().getAsOpaquePtr();
1128 // If the original parameter was textually written as an array,
1129 // instead treat the decayed parameter like it's const.
1130 //
1131 // e.g.
1132 // int [] -> int * const
1133 if (DT->getOriginalType()->isArrayType())
1134 T = T.withConst();
1135 } else
1136 TypePtr = T.getCanonicalType().getAsOpaquePtr();
1137
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001138 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1139
1140 if (Found == TypeBackReferences.end()) {
1141 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1142
David Majnemer58e4cd02013-09-11 04:44:30 +00001143 mangleType(T, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001144
1145 // See if it's worth creating a back reference.
1146 // Only types longer than 1 character are considered
1147 // and only 10 back references slots are available:
1148 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1149 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1150 size_t Size = TypeBackReferences.size();
1151 TypeBackReferences[TypePtr] = Size;
1152 }
1153 } else {
1154 Out << Found->second;
1155 }
1156}
1157
1158void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001159 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001160 // Don't use the canonical types. MSVC includes things like 'const' on
1161 // pointer arguments to function pointers that canonicalization strips away.
1162 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001163 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001164 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1165 // If there were any Quals, getAsArrayType() pushed them onto the array
1166 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001167 if (QMM == QMM_Mangle)
1168 Out << 'A';
1169 else if (QMM == QMM_Escape || QMM == QMM_Result)
1170 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001171 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001172 return;
1173 }
1174
1175 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1176 T->isBlockPointerType();
1177
1178 switch (QMM) {
1179 case QMM_Drop:
1180 break;
1181 case QMM_Mangle:
1182 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1183 Out << '6';
1184 mangleFunctionType(FT, 0, false, false);
1185 return;
1186 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001187 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001188 break;
1189 case QMM_Escape:
1190 if (!IsPointer && Quals) {
1191 Out << "$$C";
1192 mangleQualifiers(Quals, false);
1193 }
1194 break;
1195 case QMM_Result:
1196 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1197 Out << '?';
1198 mangleQualifiers(Quals, false);
1199 }
1200 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001201 }
1202
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001203 // We have to mangle these now, while we still have enough information.
1204 if (IsPointer)
1205 manglePointerQualifiers(Quals);
1206 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001207
1208 switch (ty->getTypeClass()) {
1209#define ABSTRACT_TYPE(CLASS, PARENT)
1210#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1211 case Type::CLASS: \
1212 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1213 return;
1214#define TYPE(CLASS, PARENT) \
1215 case Type::CLASS: \
1216 mangleType(cast<CLASS##Type>(ty), Range); \
1217 break;
1218#include "clang/AST/TypeNodes.def"
1219#undef ABSTRACT_TYPE
1220#undef NON_CANONICAL_TYPE
1221#undef TYPE
1222 }
1223}
1224
1225void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1226 SourceRange Range) {
1227 // <type> ::= <builtin-type>
1228 // <builtin-type> ::= X # void
1229 // ::= C # signed char
1230 // ::= D # char
1231 // ::= E # unsigned char
1232 // ::= F # short
1233 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1234 // ::= H # int
1235 // ::= I # unsigned int
1236 // ::= J # long
1237 // ::= K # unsigned long
1238 // L # <none>
1239 // ::= M # float
1240 // ::= N # double
1241 // ::= O # long double (__float80 is mangled differently)
1242 // ::= _J # long long, __int64
1243 // ::= _K # unsigned long long, __int64
1244 // ::= _L # __int128
1245 // ::= _M # unsigned __int128
1246 // ::= _N # bool
1247 // _O # <array in parameter>
1248 // ::= _T # __float80 (Intel)
1249 // ::= _W # wchar_t
1250 // ::= _Z # __float80 (Digital Mars)
1251 switch (T->getKind()) {
1252 case BuiltinType::Void: Out << 'X'; break;
1253 case BuiltinType::SChar: Out << 'C'; break;
1254 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1255 case BuiltinType::UChar: Out << 'E'; break;
1256 case BuiltinType::Short: Out << 'F'; break;
1257 case BuiltinType::UShort: Out << 'G'; break;
1258 case BuiltinType::Int: Out << 'H'; break;
1259 case BuiltinType::UInt: Out << 'I'; break;
1260 case BuiltinType::Long: Out << 'J'; break;
1261 case BuiltinType::ULong: Out << 'K'; break;
1262 case BuiltinType::Float: Out << 'M'; break;
1263 case BuiltinType::Double: Out << 'N'; break;
1264 // TODO: Determine size and mangle accordingly
1265 case BuiltinType::LongDouble: Out << 'O'; break;
1266 case BuiltinType::LongLong: Out << "_J"; break;
1267 case BuiltinType::ULongLong: Out << "_K"; break;
1268 case BuiltinType::Int128: Out << "_L"; break;
1269 case BuiltinType::UInt128: Out << "_M"; break;
1270 case BuiltinType::Bool: Out << "_N"; break;
1271 case BuiltinType::WChar_S:
1272 case BuiltinType::WChar_U: Out << "_W"; break;
1273
1274#define BUILTIN_TYPE(Id, SingletonId)
1275#define PLACEHOLDER_TYPE(Id, SingletonId) \
1276 case BuiltinType::Id:
1277#include "clang/AST/BuiltinTypes.def"
1278 case BuiltinType::Dependent:
1279 llvm_unreachable("placeholder types shouldn't get to name mangling");
1280
1281 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1282 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1283 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001284
1285 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1286 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1287 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1288 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1289 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1290 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001291 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001292 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001293
1294 case BuiltinType::NullPtr: Out << "$$T"; break;
1295
1296 case BuiltinType::Char16:
1297 case BuiltinType::Char32:
1298 case BuiltinType::Half: {
1299 DiagnosticsEngine &Diags = Context.getDiags();
1300 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1301 "cannot mangle this built-in %0 type yet");
1302 Diags.Report(Range.getBegin(), DiagID)
1303 << T->getName(Context.getASTContext().getPrintingPolicy())
1304 << Range;
1305 break;
1306 }
1307 }
1308}
1309
1310// <type> ::= <function-type>
1311void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1312 SourceRange) {
1313 // Structors only appear in decls, so at this point we know it's not a
1314 // structor type.
1315 // FIXME: This may not be lambda-friendly.
1316 Out << "$$A6";
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001317 mangleFunctionType(T, NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001318}
1319void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1320 SourceRange) {
1321 llvm_unreachable("Can't mangle K&R function prototypes");
1322}
1323
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001324void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1325 const FunctionDecl *D,
1326 bool IsStructor,
1327 bool IsInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001328 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1329 // <return-type> <argument-list> <throw-spec>
1330 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1331
Reid Klecknerf21818d2013-06-24 19:21:52 +00001332 SourceRange Range;
1333 if (D) Range = D->getSourceRange();
1334
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001335 // If this is a C++ instance method, mangle the CVR qualifiers for the
1336 // this pointer.
David Majnemer1c7a4092013-08-15 08:13:23 +00001337 if (IsInstMethod) {
1338 if (PointersAre64Bit)
1339 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001340 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
David Majnemer1c7a4092013-08-15 08:13:23 +00001341 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001342
Reid Klecknere3e686f2013-09-25 22:28:52 +00001343 mangleCallingConvention(T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001344
1345 // <return-type> ::= <type>
1346 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001347 if (IsStructor) {
1348 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1349 StructorType == Dtor_Deleting) {
1350 // The scalar deleting destructor takes an extra int argument.
1351 // However, the FunctionType generated has 0 arguments.
1352 // FIXME: This is a temporary hack.
1353 // Maybe should fix the FunctionType creation instead?
Timur Iskhodzhanov4b104062013-08-26 10:32:04 +00001354 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001355 return;
1356 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001357 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001358 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001359 QualType ResultType = Proto->getResultType();
1360 if (ResultType->isVoidType())
1361 ResultType = ResultType.getUnqualifiedType();
1362 mangleType(ResultType, Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001363 }
1364
1365 // <argument-list> ::= X # void
1366 // ::= <type>+ @
1367 // ::= <type>* Z # varargs
1368 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1369 Out << 'X';
1370 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001371 // Happens for function pointer type arguments for example.
1372 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1373 ArgEnd = Proto->arg_type_end();
1374 Arg != ArgEnd; ++Arg)
1375 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001376 // <builtin-type> ::= Z # ellipsis
1377 if (Proto->isVariadic())
1378 Out << 'Z';
1379 else
1380 Out << '@';
1381 }
1382
1383 mangleThrowSpecification(Proto);
1384}
1385
1386void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001387 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1388 // # pointer. in 64-bit mode *all*
1389 // # 'this' pointers are 64-bit.
1390 // ::= <global-function>
1391 // <member-function> ::= A # private: near
1392 // ::= B # private: far
1393 // ::= C # private: static near
1394 // ::= D # private: static far
1395 // ::= E # private: virtual near
1396 // ::= F # private: virtual far
1397 // ::= G # private: thunk near
1398 // ::= H # private: thunk far
1399 // ::= I # protected: near
1400 // ::= J # protected: far
1401 // ::= K # protected: static near
1402 // ::= L # protected: static far
1403 // ::= M # protected: virtual near
1404 // ::= N # protected: virtual far
1405 // ::= O # protected: thunk near
1406 // ::= P # protected: thunk far
1407 // ::= Q # public: near
1408 // ::= R # public: far
1409 // ::= S # public: static near
1410 // ::= T # public: static far
1411 // ::= U # public: virtual near
1412 // ::= V # public: virtual far
1413 // ::= W # public: thunk near
1414 // ::= X # public: thunk far
1415 // <global-function> ::= Y # global near
1416 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001417 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1418 switch (MD->getAccess()) {
1419 default:
1420 case AS_private:
1421 if (MD->isStatic())
1422 Out << 'C';
1423 else if (MD->isVirtual())
1424 Out << 'E';
1425 else
1426 Out << 'A';
1427 break;
1428 case AS_protected:
1429 if (MD->isStatic())
1430 Out << 'K';
1431 else if (MD->isVirtual())
1432 Out << 'M';
1433 else
1434 Out << 'I';
1435 break;
1436 case AS_public:
1437 if (MD->isStatic())
1438 Out << 'S';
1439 else if (MD->isVirtual())
1440 Out << 'U';
1441 else
1442 Out << 'Q';
1443 }
1444 } else
1445 Out << 'Y';
1446}
Reid Klecknere3e686f2013-09-25 22:28:52 +00001447void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001448 // <calling-convention> ::= A # __cdecl
1449 // ::= B # __export __cdecl
1450 // ::= C # __pascal
1451 // ::= D # __export __pascal
1452 // ::= E # __thiscall
1453 // ::= F # __export __thiscall
1454 // ::= G # __stdcall
1455 // ::= H # __export __stdcall
1456 // ::= I # __fastcall
1457 // ::= J # __export __fastcall
1458 // The 'export' calling conventions are from a bygone era
1459 // (*cough*Win16*cough*) when functions were declared for export with
1460 // that keyword. (It didn't actually export them, it just made them so
1461 // that they could be in a DLL and somebody from another module could call
1462 // them.)
1463 CallingConv CC = T->getCallConv();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001464 switch (CC) {
1465 default:
1466 llvm_unreachable("Unsupported CC for mangling");
Charles Davise8519c32013-08-30 04:39:01 +00001467 case CC_X86_64Win64:
1468 case CC_X86_64SysV:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001469 case CC_C: Out << 'A'; break;
1470 case CC_X86Pascal: Out << 'C'; break;
1471 case CC_X86ThisCall: Out << 'E'; break;
1472 case CC_X86StdCall: Out << 'G'; break;
1473 case CC_X86FastCall: Out << 'I'; break;
1474 }
1475}
1476void MicrosoftCXXNameMangler::mangleThrowSpecification(
1477 const FunctionProtoType *FT) {
1478 // <throw-spec> ::= Z # throw(...) (default)
1479 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1480 // ::= <type>+
1481 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1482 // all actually mangled as 'Z'. (They're ignored because their associated
1483 // functionality isn't implemented, and probably never will be.)
1484 Out << 'Z';
1485}
1486
1487void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1488 SourceRange Range) {
1489 // Probably should be mangled as a template instantiation; need to see what
1490 // VC does first.
1491 DiagnosticsEngine &Diags = Context.getDiags();
1492 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1493 "cannot mangle this unresolved dependent type yet");
1494 Diags.Report(Range.getBegin(), DiagID)
1495 << Range;
1496}
1497
1498// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1499// <union-type> ::= T <name>
1500// <struct-type> ::= U <name>
1501// <class-type> ::= V <name>
1502// <enum-type> ::= W <size> <name>
1503void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001504 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001505}
1506void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001507 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001508}
David Majnemer02c44f02013-08-05 22:26:46 +00001509void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1510 switch (TD->getTagKind()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001511 case TTK_Union:
1512 Out << 'T';
1513 break;
1514 case TTK_Struct:
1515 case TTK_Interface:
1516 Out << 'U';
1517 break;
1518 case TTK_Class:
1519 Out << 'V';
1520 break;
1521 case TTK_Enum:
1522 Out << 'W';
1523 Out << getASTContext().getTypeSizeInChars(
David Majnemer02c44f02013-08-05 22:26:46 +00001524 cast<EnumDecl>(TD)->getIntegerType()).getQuantity();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001525 break;
1526 }
David Majnemer02c44f02013-08-05 22:26:46 +00001527 mangleName(TD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001528}
1529
1530// <type> ::= <array-type>
1531// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1532// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001533// <element-type> # as global, E is never required
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001534// It's supposed to be the other way around, but for some strange reason, it
1535// isn't. Today this behavior is retained for the sole purpose of backwards
1536// compatibility.
David Majnemer58e4cd02013-09-11 04:44:30 +00001537void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001538 // This isn't a recursive mangling, so now we have to do it all in this
1539 // one call.
David Majnemer58e4cd02013-09-11 04:44:30 +00001540 manglePointerQualifiers(T->getElementType().getQualifiers());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001541 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001542}
1543void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1544 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001545 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001546}
1547void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1548 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001549 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001550}
1551void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1552 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001553 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001554}
1555void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1556 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001557 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001558}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001559void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001560 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001561 SmallVector<llvm::APInt, 3> Dimensions;
1562 for (;;) {
1563 if (const ConstantArrayType *CAT =
1564 getASTContext().getAsConstantArrayType(ElementTy)) {
1565 Dimensions.push_back(CAT->getSize());
1566 ElementTy = CAT->getElementType();
1567 } else if (ElementTy->isVariableArrayType()) {
1568 const VariableArrayType *VAT =
1569 getASTContext().getAsVariableArrayType(ElementTy);
1570 DiagnosticsEngine &Diags = Context.getDiags();
1571 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1572 "cannot mangle this variable-length array yet");
1573 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1574 << VAT->getBracketsRange();
1575 return;
1576 } else if (ElementTy->isDependentSizedArrayType()) {
1577 // The dependent expression has to be folded into a constant (TODO).
1578 const DependentSizedArrayType *DSAT =
1579 getASTContext().getAsDependentSizedArrayType(ElementTy);
1580 DiagnosticsEngine &Diags = Context.getDiags();
1581 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1582 "cannot mangle this dependent-length array yet");
1583 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1584 << DSAT->getBracketsRange();
1585 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001586 } else if (const IncompleteArrayType *IAT =
1587 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1588 Dimensions.push_back(llvm::APInt(32, 0));
1589 ElementTy = IAT->getElementType();
1590 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001591 else break;
1592 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001593 Out << 'Y';
1594 // <dimension-count> ::= <number> # number of extra dimensions
1595 mangleNumber(Dimensions.size());
1596 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1597 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001598 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001599}
1600
1601// <type> ::= <pointer-to-member-type>
1602// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1603// <class name> <type>
1604void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1605 SourceRange Range) {
1606 QualType PointeeType = T->getPointeeType();
1607 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1608 Out << '8';
1609 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001610 mangleFunctionType(FPT, NULL, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001611 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001612 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1613 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001614 mangleQualifiers(PointeeType.getQualifiers(), true);
1615 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001616 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001617 }
1618}
1619
1620void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1621 SourceRange Range) {
1622 DiagnosticsEngine &Diags = Context.getDiags();
1623 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1624 "cannot mangle this template type parameter type yet");
1625 Diags.Report(Range.getBegin(), DiagID)
1626 << Range;
1627}
1628
1629void MicrosoftCXXNameMangler::mangleType(
1630 const SubstTemplateTypeParmPackType *T,
1631 SourceRange Range) {
1632 DiagnosticsEngine &Diags = Context.getDiags();
1633 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1634 "cannot mangle this substituted parameter pack yet");
1635 Diags.Report(Range.getBegin(), DiagID)
1636 << Range;
1637}
1638
1639// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001640// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1641// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001642void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1643 SourceRange Range) {
1644 QualType PointeeTy = T->getPointeeType();
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001645 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1646 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001647 mangleType(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001648}
1649void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1650 SourceRange Range) {
1651 // Object pointers never have qualifiers.
1652 Out << 'A';
David Majnemer1c7a4092013-08-15 08:13:23 +00001653 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1654 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001655 mangleType(T->getPointeeType(), Range);
1656}
1657
1658// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001659// <reference-type> ::= A E? <cvr-qualifiers> <type>
1660// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001661void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1662 SourceRange Range) {
1663 Out << 'A';
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
1669// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001670// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1671// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001672void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1673 SourceRange Range) {
1674 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001675 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1676 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001677 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001678}
1679
1680void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1681 SourceRange Range) {
1682 DiagnosticsEngine &Diags = Context.getDiags();
1683 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1684 "cannot mangle this complex number type yet");
1685 Diags.Report(Range.getBegin(), DiagID)
1686 << Range;
1687}
1688
1689void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1690 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001691 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1692 assert(ET && "vectors with non-builtin elements are unsupported");
1693 uint64_t Width = getASTContext().getTypeSize(T);
1694 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1695 // doesn't match the Intel types uses a custom mangling below.
1696 bool IntelVector = true;
1697 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1698 Out << "T__m64";
1699 } else if (Width == 128 || Width == 256) {
1700 if (ET->getKind() == BuiltinType::Float)
1701 Out << "T__m" << Width;
1702 else if (ET->getKind() == BuiltinType::LongLong)
1703 Out << "T__m" << Width << 'i';
1704 else if (ET->getKind() == BuiltinType::Double)
1705 Out << "U__m" << Width << 'd';
1706 else
1707 IntelVector = false;
1708 } else {
1709 IntelVector = false;
1710 }
1711
1712 if (!IntelVector) {
1713 // The MS ABI doesn't have a special mangling for vector types, so we define
1714 // our own mangling to handle uses of __vector_size__ on user-specified
1715 // types, and for extensions like __v4sf.
1716 Out << "T__clang_vec" << T->getNumElements() << '_';
1717 mangleType(ET, Range);
1718 }
1719
1720 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001721}
Reid Kleckner1232e272013-03-26 16:56:59 +00001722
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001723void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1724 SourceRange Range) {
1725 DiagnosticsEngine &Diags = Context.getDiags();
1726 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1727 "cannot mangle this extended vector type yet");
1728 Diags.Report(Range.getBegin(), DiagID)
1729 << Range;
1730}
1731void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1732 SourceRange Range) {
1733 DiagnosticsEngine &Diags = Context.getDiags();
1734 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1735 "cannot mangle this dependent-sized extended vector type yet");
1736 Diags.Report(Range.getBegin(), DiagID)
1737 << Range;
1738}
1739
1740void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1741 SourceRange) {
1742 // ObjC interfaces have structs underlying them.
1743 Out << 'U';
1744 mangleName(T->getDecl());
1745}
1746
1747void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1748 SourceRange Range) {
1749 // We don't allow overloading by different protocol qualification,
1750 // so mangling them isn't necessary.
1751 mangleType(T->getBaseType(), Range);
1752}
1753
1754void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1755 SourceRange Range) {
1756 Out << "_E";
1757
1758 QualType pointee = T->getPointeeType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001759 mangleFunctionType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001760}
1761
David Majnemer360d23e2013-08-16 08:29:13 +00001762void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1763 SourceRange) {
1764 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001765}
1766
1767void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1768 SourceRange Range) {
1769 DiagnosticsEngine &Diags = Context.getDiags();
1770 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1771 "cannot mangle this template specialization type yet");
1772 Diags.Report(Range.getBegin(), DiagID)
1773 << Range;
1774}
1775
1776void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1777 SourceRange Range) {
1778 DiagnosticsEngine &Diags = Context.getDiags();
1779 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1780 "cannot mangle this dependent name type yet");
1781 Diags.Report(Range.getBegin(), DiagID)
1782 << Range;
1783}
1784
1785void MicrosoftCXXNameMangler::mangleType(
1786 const DependentTemplateSpecializationType *T,
1787 SourceRange Range) {
1788 DiagnosticsEngine &Diags = Context.getDiags();
1789 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1790 "cannot mangle this dependent template specialization type yet");
1791 Diags.Report(Range.getBegin(), DiagID)
1792 << Range;
1793}
1794
1795void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1796 SourceRange Range) {
1797 DiagnosticsEngine &Diags = Context.getDiags();
1798 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1799 "cannot mangle this pack expansion yet");
1800 Diags.Report(Range.getBegin(), DiagID)
1801 << Range;
1802}
1803
1804void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1805 SourceRange Range) {
1806 DiagnosticsEngine &Diags = Context.getDiags();
1807 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1808 "cannot mangle this typeof(type) yet");
1809 Diags.Report(Range.getBegin(), DiagID)
1810 << Range;
1811}
1812
1813void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1814 SourceRange Range) {
1815 DiagnosticsEngine &Diags = Context.getDiags();
1816 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1817 "cannot mangle this typeof(expression) yet");
1818 Diags.Report(Range.getBegin(), DiagID)
1819 << Range;
1820}
1821
1822void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1823 SourceRange Range) {
1824 DiagnosticsEngine &Diags = Context.getDiags();
1825 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1826 "cannot mangle this decltype() yet");
1827 Diags.Report(Range.getBegin(), DiagID)
1828 << Range;
1829}
1830
1831void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1832 SourceRange Range) {
1833 DiagnosticsEngine &Diags = Context.getDiags();
1834 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1835 "cannot mangle this unary transform type yet");
1836 Diags.Report(Range.getBegin(), DiagID)
1837 << Range;
1838}
1839
1840void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1841 DiagnosticsEngine &Diags = Context.getDiags();
1842 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1843 "cannot mangle this 'auto' type yet");
1844 Diags.Report(Range.getBegin(), DiagID)
1845 << Range;
1846}
1847
1848void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1849 SourceRange Range) {
1850 DiagnosticsEngine &Diags = Context.getDiags();
1851 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1852 "cannot mangle this C11 atomic type yet");
1853 Diags.Report(Range.getBegin(), DiagID)
1854 << Range;
1855}
1856
1857void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1858 raw_ostream &Out) {
1859 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1860 "Invalid mangleName() call, argument is not a variable or function!");
1861 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1862 "Invalid mangleName() call on 'structor decl!");
1863
1864 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1865 getASTContext().getSourceManager(),
1866 "Mangling declaration");
1867
1868 MicrosoftCXXNameMangler Mangler(*this, Out);
1869 return Mangler.mangle(D);
1870}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001871
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001872void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1873 const ThunkInfo &Thunk,
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001874 raw_ostream &Out) {
1875 // FIXME: this is not yet a complete implementation, but merely a
1876 // reasonably-working stub to avoid crashing when required to emit a thunk.
1877 MicrosoftCXXNameMangler Mangler(*this, Out);
1878 Out << "\01?";
1879 Mangler.mangleName(MD);
1880 if (Thunk.This.NonVirtual != 0) {
1881 // FIXME: add support for protected/private or use mangleFunctionClass.
1882 Out << "W";
1883 llvm::APSInt APSNumber(/*BitWidth=*/32 /*FIXME: check on x64*/,
1884 /*isUnsigned=*/true);
1885 APSNumber = -Thunk.This.NonVirtual;
1886 Mangler.mangleNumber(APSNumber);
1887 } else {
1888 // FIXME: add support for protected/private or use mangleFunctionClass.
1889 Out << "Q";
1890 }
1891 // FIXME: mangle return adjustment? Most likely includes using an overridee FPT?
1892 Mangler.mangleFunctionType(MD->getType()->castAs<FunctionProtoType>(), MD, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001893}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001894
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001895void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1896 CXXDtorType Type,
1897 const ThisAdjustment &,
1898 raw_ostream &) {
1899 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1900 "cannot mangle thunk for this destructor yet");
1901 getDiags().Report(DD->getLocation(), DiagID);
1902}
Reid Kleckner90633022013-06-19 15:20:38 +00001903
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001904void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1905 raw_ostream &Out) {
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001906 llvm_unreachable(
1907 "The Microsoft C++ ABI does not have vtables (use vftables instead)!");
1908}
1909
1910void MicrosoftMangleContext::mangleCXXVFTable(
1911 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1912 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001913 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1914 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001915 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001916 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001917 MicrosoftCXXNameMangler Mangler(*this, Out);
1918 Mangler.getStream() << "\01??_7";
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001919 Mangler.mangleName(Derived);
1920 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
1921 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1922 E = BasePath.end();
1923 I != E; ++I) {
1924 Mangler.mangleName(*I);
1925 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001926 Mangler.getStream() << '@';
1927}
Reid Kleckner90633022013-06-19 15:20:38 +00001928
1929void MicrosoftMangleContext::mangleCXXVBTable(
1930 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1931 raw_ostream &Out) {
1932 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1933 // <cvr-qualifiers> [<name>] @
1934 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1935 // is always '7' for vbtables.
1936 MicrosoftCXXNameMangler Mangler(*this, Out);
1937 Mangler.getStream() << "\01??_8";
1938 Mangler.mangleName(Derived);
1939 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1940 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1941 E = BasePath.end();
1942 I != E; ++I) {
1943 Mangler.mangleName(*I);
1944 }
1945 Mangler.getStream() << '@';
1946}
1947
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001948void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1949 raw_ostream &) {
1950 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1951}
1952void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1953 int64_t Offset,
1954 const CXXRecordDecl *Type,
1955 raw_ostream &) {
1956 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1957}
1958void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1959 raw_ostream &) {
1960 // FIXME: Give a location...
1961 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1962 "cannot mangle RTTI descriptors for type %0 yet");
1963 getDiags().Report(DiagID)
1964 << T.getBaseTypeIdentifier();
1965}
1966void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1967 raw_ostream &) {
1968 // FIXME: Give a location...
1969 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1970 "cannot mangle the name of type %0 into RTTI descriptors yet");
1971 getDiags().Report(DiagID)
1972 << T.getBaseTypeIdentifier();
1973}
1974void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1975 CXXCtorType Type,
1976 raw_ostream & Out) {
1977 MicrosoftCXXNameMangler mangler(*this, Out);
1978 mangler.mangle(D);
1979}
1980void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1981 CXXDtorType Type,
1982 raw_ostream & Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001983 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001984 mangler.mangle(D);
1985}
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001986void MicrosoftMangleContext::mangleReferenceTemporary(const VarDecl *VD,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001987 raw_ostream &) {
1988 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1989 "cannot mangle this reference temporary yet");
1990 getDiags().Report(VD->getLocation(), DiagID);
1991}
1992
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001993void MicrosoftMangleContext::mangleStaticGuardVariable(const VarDecl *VD,
1994 raw_ostream &Out) {
1995 // <guard-name> ::= ?_B <postfix> @51
1996 // ::= ?$S <guard-num> @ <postfix> @4IA
1997
1998 // The first mangling is what MSVC uses to guard static locals in inline
1999 // functions. It uses a different mangling in external functions to support
2000 // guarding more than 32 variables. MSVC rejects inline functions with more
2001 // than 32 static locals. We don't fully implement the second mangling
2002 // because those guards are not externally visible, and instead use LLVM's
2003 // default renaming when creating a new guard variable.
2004 MicrosoftCXXNameMangler Mangler(*this, Out);
2005
2006 bool Visible = VD->isExternallyVisible();
2007 // <operator-name> ::= ?_B # local static guard
2008 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@");
2009 Mangler.manglePostfix(VD->getDeclContext());
2010 Mangler.getStream() << (Visible ? "@51" : "@4IA");
2011}
2012
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00002013void MicrosoftMangleContext::mangleInitFiniStub(const VarDecl *D,
2014 raw_ostream &Out,
2015 char CharCode) {
2016 MicrosoftCXXNameMangler Mangler(*this, Out);
2017 Mangler.getStream() << "\01??__" << CharCode;
2018 Mangler.mangleName(D);
2019 // This is the function class mangling. These stubs are global, non-variadic,
2020 // cdecl functions that return void and take no args.
2021 Mangler.getStream() << "YAXXZ";
2022}
2023
2024void MicrosoftMangleContext::mangleDynamicInitializer(const VarDecl *D,
2025 raw_ostream &Out) {
2026 // <initializer-name> ::= ?__E <name> YAXXZ
2027 mangleInitFiniStub(D, Out, 'E');
2028}
2029
Reid Kleckner942f9fe2013-09-10 20:14:30 +00002030void MicrosoftMangleContext::mangleDynamicAtExitDestructor(const VarDecl *D,
2031 raw_ostream &Out) {
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00002032 // <destructor-name> ::= ?__F <name> YAXXZ
2033 mangleInitFiniStub(D, Out, 'F');
Reid Kleckner942f9fe2013-09-10 20:14:30 +00002034}
2035
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002036MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
2037 DiagnosticsEngine &Diags) {
2038 return new MicrosoftMangleContext(Context, Diags);
2039}