blob: 8506c4c8dde691f187e54c593c1d1cef8d7a4c75 [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);
165 void mangleCallingConvention(const FunctionType *T, bool IsInstMethod = false);
166 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 &);
191 virtual void mangleCXXVTT(const CXXRecordDecl *RD,
192 raw_ostream &);
Reid Kleckner90633022013-06-19 15:20:38 +0000193 virtual void mangleCXXVBTable(const CXXRecordDecl *Derived,
194 ArrayRef<const CXXRecordDecl *> BasePath,
195 raw_ostream &Out);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000196 virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
197 const CXXRecordDecl *Type,
198 raw_ostream &);
199 virtual void mangleCXXRTTI(QualType T, raw_ostream &);
200 virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
201 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
202 raw_ostream &);
203 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
204 raw_ostream &);
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000205 virtual void mangleReferenceTemporary(const VarDecl *, raw_ostream &);
206 virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out);
Reid Klecknerc5c6fa72013-09-10 20:43:12 +0000207 virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out);
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000208 virtual void mangleDynamicAtExitDestructor(const VarDecl *D,
209 raw_ostream &Out);
Reid Klecknerc5c6fa72013-09-10 20:43:12 +0000210
211private:
212 void mangleInitFiniStub(const VarDecl *D, raw_ostream &Out, char CharCode);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000213};
214
215}
216
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000217bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
218 // In C, functions with no attributes never need to be mangled. Fastpath them.
219 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
220 return false;
221
222 // Any decl can be declared with __asm("foo") on it, and this takes precedence
223 // over all other naming in the .o file.
224 if (D->hasAttr<AsmLabelAttr>())
225 return true;
226
David Majnemercab7dad2013-09-13 09:03:14 +0000227 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
228 LanguageLinkage L = FD->getLanguageLinkage();
229 // Overloadable functions need mangling.
230 if (FD->hasAttr<OverloadableAttr>())
231 return true;
232
David Majnemere9f6f332013-09-16 22:44:20 +0000233 // The ABI expects that we would never mangle "typical" user-defined entry
234 // points regardless of visibility or freestanding-ness.
235 //
236 // N.B. This is distinct from asking about "main". "main" has a lot of
237 // special rules associated with it in the standard while these
238 // user-defined entry points are outside of the purview of the standard.
239 // For example, there can be only one definition for "main" in a standards
240 // compliant program; however nothing forbids the existence of wmain and
241 // WinMain in the same translation unit.
242 if (FD->isMSVCRTEntryPoint())
David Majnemercab7dad2013-09-13 09:03:14 +0000243 return false;
244
245 // C++ functions and those whose names are not a simple identifier need
246 // mangling.
247 if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage)
248 return true;
249
250 // C functions are not mangled.
251 if (L == CLanguageLinkage)
252 return false;
253 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000254
255 // Otherwise, no mangling is done outside C++ mode.
256 if (!getASTContext().getLangOpts().CPlusPlus)
257 return false;
258
David Majnemercab7dad2013-09-13 09:03:14 +0000259 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
260 // C variables are not mangled.
261 if (VD->isExternC())
262 return false;
263
264 // Variables at global scope with non-internal linkage are not mangled.
265 const DeclContext *DC = getEffectiveDeclContext(D);
266 // Check for extern variable declared locally.
267 if (DC->isFunctionOrMethod() && D->hasLinkage())
268 while (!DC->isNamespace() && !DC->isTranslationUnit())
269 DC = getEffectiveParentContext(DC);
270
271 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage &&
272 !isa<VarTemplateSpecializationDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000273 return false;
274 }
275
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000276 return true;
277}
278
279void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
280 StringRef Prefix) {
281 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
282 // Therefore it's really important that we don't decorate the
283 // name with leading underscores or leading/trailing at signs. So, by
284 // default, we emit an asm marker at the start so we get the name right.
285 // Callers can override this with a custom prefix.
286
287 // Any decl can be declared with __asm("foo") on it, and this takes precedence
288 // over all other naming in the .o file.
289 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
290 // If we have an asm name, then we use it as the mangling.
291 Out << '\01' << ALA->getLabel();
292 return;
293 }
294
295 // <mangled-name> ::= ? <name> <type-encoding>
296 Out << Prefix;
297 mangleName(D);
298 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
299 mangleFunctionEncoding(FD);
300 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
301 mangleVariableEncoding(VD);
302 else {
303 // TODO: Fields? Can MSVC even mangle them?
304 // Issue a diagnostic for now.
305 DiagnosticsEngine &Diags = Context.getDiags();
306 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
307 "cannot mangle this declaration yet");
308 Diags.Report(D->getLocation(), DiagID)
309 << D->getSourceRange();
310 }
311}
312
313void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
314 // <type-encoding> ::= <function-class> <function-type>
315
Reid Klecknerf21818d2013-06-24 19:21:52 +0000316 // Since MSVC operates on the type as written and not the canonical type, it
317 // actually matters which decl we have here. MSVC appears to choose the
318 // first, since it is most likely to be the declaration in a header file.
319 FD = FD->getFirstDeclaration();
320
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000321 // We should never ever see a FunctionNoProtoType at this point.
322 // We don't even know how to mangle their types anyway :).
Reid Klecknerf21818d2013-06-24 19:21:52 +0000323 TypeSourceInfo *TSI = FD->getTypeSourceInfo();
324 QualType T = TSI ? TSI->getType() : FD->getType();
325 const FunctionProtoType *FT = T->castAs<FunctionProtoType>();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000326
327 bool InStructor = false, InInstMethod = false;
328 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
329 if (MD) {
330 if (MD->isInstance())
331 InInstMethod = true;
332 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
333 InStructor = true;
334 }
335
David Majnemercab7dad2013-09-13 09:03:14 +0000336 // extern "C" functions can hold entities that must be mangled.
337 // As it stands, these functions still need to get expressed in the full
338 // external name. They have their class and type omitted, replaced with '9'.
339 if (Context.shouldMangleDeclName(FD)) {
340 // First, the function class.
341 mangleFunctionClass(FD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000342
David Majnemercab7dad2013-09-13 09:03:14 +0000343 mangleFunctionType(FT, FD, InStructor, InInstMethod);
344 } else
345 Out << '9';
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000346}
347
348void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
349 // <type-encoding> ::= <storage-class> <variable-type>
350 // <storage-class> ::= 0 # private static member
351 // ::= 1 # protected static member
352 // ::= 2 # public static member
353 // ::= 3 # global
354 // ::= 4 # static local
355
356 // The first character in the encoding (after the name) is the storage class.
357 if (VD->isStaticDataMember()) {
358 // If it's a static member, it also encodes the access level.
359 switch (VD->getAccess()) {
360 default:
361 case AS_private: Out << '0'; break;
362 case AS_protected: Out << '1'; break;
363 case AS_public: Out << '2'; break;
364 }
365 }
366 else if (!VD->isStaticLocal())
367 Out << '3';
368 else
369 Out << '4';
370 // Now mangle the type.
371 // <variable-type> ::= <type> <cvr-qualifiers>
372 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
373 // Pointers and references are odd. The type of 'int * const foo;' gets
374 // mangled as 'QAHA' instead of 'PAHB', for example.
375 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
376 QualType Ty = TL.getType();
David Majnemer1c7a4092013-08-15 08:13:23 +0000377 if (Ty->isPointerType() || Ty->isReferenceType() ||
378 Ty->isMemberPointerType()) {
David Majnemer17ffbd02013-08-09 05:56:24 +0000379 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000380 if (PointersAre64Bit)
381 Out << 'E';
382 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) {
383 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true);
384 // Member pointers are suffixed with a back reference to the member
385 // pointer's class name.
386 mangleName(MPT->getClass()->getAsCXXRecordDecl());
387 } else
388 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
David Majnemer17ffbd02013-08-09 05:56:24 +0000389 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000390 // Global arrays are funny, too.
David Majnemer58e4cd02013-09-11 04:44:30 +0000391 mangleDecayedArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000392 if (AT->getElementType()->isArrayType())
393 Out << 'A';
394 else
395 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000396 } else {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000397 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000398 mangleQualifiers(Ty.getLocalQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000399 }
400}
401
402void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
403 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
404 const DeclContext *DC = ND->getDeclContext();
405
406 // Always start with the unqualified name.
407 mangleUnqualifiedName(ND);
408
409 // If this is an extern variable declared locally, the relevant DeclContext
410 // is that of the containing namespace, or the translation unit.
411 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
412 while (!DC->isNamespace() && !DC->isTranslationUnit())
413 DC = DC->getParent();
414
415 manglePostfix(DC);
416
417 // Terminate the whole name with an '@'.
418 Out << '@';
419}
420
421void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
422 llvm::APSInt APSNumber(/*BitWidth=*/64, /*isUnsigned=*/false);
423 APSNumber = Number;
424 mangleNumber(APSNumber);
425}
426
427void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
428 // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
429 // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
430 // ::= [?] @ # 0 (alternate mangling, not emitted by VC)
431 if (Value.isSigned() && Value.isNegative()) {
432 Out << '?';
433 mangleNumber(llvm::APSInt(Value.abs()));
434 return;
435 }
436 llvm::APSInt Temp(Value);
437 // There's a special shorter mangling for 0, but Microsoft
438 // chose not to use it. Instead, 0 gets mangled as "A@". Oh well...
439 if (Value.uge(1) && Value.ule(10)) {
440 --Temp;
441 Temp.print(Out, false);
442 } else {
443 // We have to build up the encoding in reverse order, so it will come
444 // out right when we write it out.
445 char Encoding[64];
446 char *EndPtr = Encoding+sizeof(Encoding);
447 char *CurPtr = EndPtr;
448 llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned());
449 NibbleMask = 0xf;
450 do {
451 *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf);
452 Temp = Temp.lshr(4);
453 } while (Temp != 0);
454 Out.write(CurPtr, EndPtr-CurPtr);
455 Out << '@';
456 }
457}
458
459static const TemplateDecl *
Reid Klecknerf16216c2013-03-20 01:40:23 +0000460isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000461 // Check if we have a function template.
462 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
463 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000464 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000465 return TD;
466 }
467 }
468
469 // Check if we have a class template.
470 if (const ClassTemplateSpecializationDecl *Spec =
Reid Klecknerf16216c2013-03-20 01:40:23 +0000471 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
472 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000473 return Spec->getSpecializedTemplate();
474 }
475
476 return 0;
477}
478
479void
480MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
481 DeclarationName Name) {
482 // <unqualified-name> ::= <operator-name>
483 // ::= <ctor-dtor-name>
484 // ::= <source-name>
485 // ::= <template-name>
Reid Klecknerf16216c2013-03-20 01:40:23 +0000486
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000487 // Check if we have a template.
Reid Klecknerf16216c2013-03-20 01:40:23 +0000488 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000489 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Kleckner3be37d12013-07-13 00:43:39 +0000490 // Function templates aren't considered for name back referencing. This
491 // makes sense since function templates aren't likely to occur multiple
492 // times in a symbol.
493 // FIXME: Test alias template mangling with MSVC 2013.
494 if (!isa<ClassTemplateDecl>(TD)) {
495 mangleTemplateInstantiationName(TD, *TemplateArgs);
496 return;
497 }
498
499 // We have a class template.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000500 // Here comes the tricky thing: if we need to mangle something like
501 // void foo(A::X<Y>, B::X<Y>),
502 // the X<Y> part is aliased. However, if you need to mangle
503 // void foo(A::X<A::Y>, A::X<B::Y>),
504 // the A::X<> part is not aliased.
505 // That said, from the mangler's perspective we have a structure like this:
506 // namespace[s] -> type[ -> template-parameters]
507 // but from the Clang perspective we have
508 // type [ -> template-parameters]
509 // \-> namespace[s]
510 // What we do is we create a new mangler, mangle the same type (without
511 // a namespace suffix) using the extra mangler with back references
512 // disabled (to avoid infinite recursion) and then use the mangled type
513 // name as a key to check the mangling of different types for aliasing.
514
515 std::string BackReferenceKey;
516 BackRefMap::iterator Found;
517 if (UseNameBackReferences) {
518 llvm::raw_string_ostream Stream(BackReferenceKey);
519 MicrosoftCXXNameMangler Extra(Context, Stream);
520 Extra.disableBackReferences();
521 Extra.mangleUnqualifiedName(ND, Name);
522 Stream.flush();
523
524 Found = NameBackReferences.find(BackReferenceKey);
525 }
526 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000527 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000528 if (UseNameBackReferences && NameBackReferences.size() < 10) {
529 size_t Size = NameBackReferences.size();
530 NameBackReferences[BackReferenceKey] = Size;
531 }
532 } else {
533 Out << Found->second;
534 }
535 return;
536 }
537
538 switch (Name.getNameKind()) {
539 case DeclarationName::Identifier: {
540 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
541 mangleSourceName(II);
542 break;
543 }
544
545 // Otherwise, an anonymous entity. We must have a declaration.
546 assert(ND && "mangling empty name without declaration");
547
548 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
549 if (NS->isAnonymousNamespace()) {
550 Out << "?A@";
551 break;
552 }
553 }
554
555 // We must have an anonymous struct.
556 const TagDecl *TD = cast<TagDecl>(ND);
557 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
558 assert(TD->getDeclContext() == D->getDeclContext() &&
559 "Typedef should not be in another decl context!");
560 assert(D->getDeclName().getAsIdentifierInfo() &&
561 "Typedef was not named!");
562 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
563 break;
564 }
565
David Majnemer1f7fd682013-09-17 22:45:28 +0000566 // When VC encounters an anonymous type with no tag and no typedef,
567 // it literally emits '<unnamed-tag>@'.
568 Out << "<unnamed-tag>@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000569 break;
570 }
571
572 case DeclarationName::ObjCZeroArgSelector:
573 case DeclarationName::ObjCOneArgSelector:
574 case DeclarationName::ObjCMultiArgSelector:
575 llvm_unreachable("Can't mangle Objective-C selector names here!");
576
577 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000578 if (ND == Structor) {
579 assert(StructorType == Ctor_Complete &&
580 "Should never be asked to mangle a ctor other than complete");
581 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000582 Out << "?0";
583 break;
584
585 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000586 if (ND == Structor)
587 // If the named decl is the C++ destructor we're mangling,
588 // use the type we were given.
589 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
590 else
Reid Klecknera4130ba2013-07-22 13:51:44 +0000591 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000592 // class with a destructor is declared within a destructor.
Reid Klecknera4130ba2013-07-22 13:51:44 +0000593 mangleCXXDtorType(Dtor_Base);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000594 break;
595
596 case DeclarationName::CXXConversionFunctionName:
597 // <operator-name> ::= ?B # (cast)
598 // The target type is encoded as the return type.
599 Out << "?B";
600 break;
601
602 case DeclarationName::CXXOperatorName:
603 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
604 break;
605
606 case DeclarationName::CXXLiteralOperatorName: {
607 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
608 DiagnosticsEngine Diags = Context.getDiags();
609 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
610 "cannot mangle this literal operator yet");
611 Diags.Report(ND->getLocation(), DiagID);
612 break;
613 }
614
615 case DeclarationName::CXXUsingDirective:
616 llvm_unreachable("Can't mangle a using directive name!");
617 }
618}
619
620void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
621 bool NoFunction) {
622 // <postfix> ::= <unqualified-name> [<postfix>]
623 // ::= <substitution> [<postfix>]
624
625 if (!DC) return;
626
627 while (isa<LinkageSpecDecl>(DC))
628 DC = DC->getParent();
629
630 if (DC->isTranslationUnit())
631 return;
632
633 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedmane5798892013-07-10 01:13:27 +0000634 DiagnosticsEngine Diags = Context.getDiags();
635 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
636 "cannot mangle a local inside this block yet");
637 Diags.Report(BD->getLocation(), DiagID);
638
639 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
640 // for how this should be done.
641 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000642 Out << '@';
643 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000644 } else if (isa<CapturedDecl>(DC)) {
645 // Skip CapturedDecl context.
646 manglePostfix(DC->getParent(), NoFunction);
647 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000648 }
649
650 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
651 return;
652 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
653 mangleObjCMethodName(Method);
654 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
655 mangleLocalName(Func);
656 else {
657 mangleUnqualifiedName(cast<NamedDecl>(DC));
658 manglePostfix(DC->getParent(), NoFunction);
659 }
660}
661
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000662void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000663 // Microsoft uses the names on the case labels for these dtor variants. Clang
664 // uses the Itanium terminology internally. Everything in this ABI delegates
665 // towards the base dtor.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000666 switch (T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000667 // <operator-name> ::= ?1 # destructor
668 case Dtor_Base: Out << "?1"; return;
669 // <operator-name> ::= ?_D # vbase destructor
670 case Dtor_Complete: Out << "?_D"; return;
671 // <operator-name> ::= ?_G # scalar deleting destructor
672 case Dtor_Deleting: Out << "?_G"; return;
673 // <operator-name> ::= ?_E # vector deleting destructor
674 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
675 // it.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000676 }
677 llvm_unreachable("Unsupported dtor type?");
678}
679
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000680void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
681 SourceLocation Loc) {
682 switch (OO) {
683 // ?0 # constructor
684 // ?1 # destructor
685 // <operator-name> ::= ?2 # new
686 case OO_New: Out << "?2"; break;
687 // <operator-name> ::= ?3 # delete
688 case OO_Delete: Out << "?3"; break;
689 // <operator-name> ::= ?4 # =
690 case OO_Equal: Out << "?4"; break;
691 // <operator-name> ::= ?5 # >>
692 case OO_GreaterGreater: Out << "?5"; break;
693 // <operator-name> ::= ?6 # <<
694 case OO_LessLess: Out << "?6"; break;
695 // <operator-name> ::= ?7 # !
696 case OO_Exclaim: Out << "?7"; break;
697 // <operator-name> ::= ?8 # ==
698 case OO_EqualEqual: Out << "?8"; break;
699 // <operator-name> ::= ?9 # !=
700 case OO_ExclaimEqual: Out << "?9"; break;
701 // <operator-name> ::= ?A # []
702 case OO_Subscript: Out << "?A"; break;
703 // ?B # conversion
704 // <operator-name> ::= ?C # ->
705 case OO_Arrow: Out << "?C"; break;
706 // <operator-name> ::= ?D # *
707 case OO_Star: Out << "?D"; break;
708 // <operator-name> ::= ?E # ++
709 case OO_PlusPlus: Out << "?E"; break;
710 // <operator-name> ::= ?F # --
711 case OO_MinusMinus: Out << "?F"; break;
712 // <operator-name> ::= ?G # -
713 case OO_Minus: Out << "?G"; break;
714 // <operator-name> ::= ?H # +
715 case OO_Plus: Out << "?H"; break;
716 // <operator-name> ::= ?I # &
717 case OO_Amp: Out << "?I"; break;
718 // <operator-name> ::= ?J # ->*
719 case OO_ArrowStar: Out << "?J"; break;
720 // <operator-name> ::= ?K # /
721 case OO_Slash: Out << "?K"; break;
722 // <operator-name> ::= ?L # %
723 case OO_Percent: Out << "?L"; break;
724 // <operator-name> ::= ?M # <
725 case OO_Less: Out << "?M"; break;
726 // <operator-name> ::= ?N # <=
727 case OO_LessEqual: Out << "?N"; break;
728 // <operator-name> ::= ?O # >
729 case OO_Greater: Out << "?O"; break;
730 // <operator-name> ::= ?P # >=
731 case OO_GreaterEqual: Out << "?P"; break;
732 // <operator-name> ::= ?Q # ,
733 case OO_Comma: Out << "?Q"; break;
734 // <operator-name> ::= ?R # ()
735 case OO_Call: Out << "?R"; break;
736 // <operator-name> ::= ?S # ~
737 case OO_Tilde: Out << "?S"; break;
738 // <operator-name> ::= ?T # ^
739 case OO_Caret: Out << "?T"; break;
740 // <operator-name> ::= ?U # |
741 case OO_Pipe: Out << "?U"; break;
742 // <operator-name> ::= ?V # &&
743 case OO_AmpAmp: Out << "?V"; break;
744 // <operator-name> ::= ?W # ||
745 case OO_PipePipe: Out << "?W"; break;
746 // <operator-name> ::= ?X # *=
747 case OO_StarEqual: Out << "?X"; break;
748 // <operator-name> ::= ?Y # +=
749 case OO_PlusEqual: Out << "?Y"; break;
750 // <operator-name> ::= ?Z # -=
751 case OO_MinusEqual: Out << "?Z"; break;
752 // <operator-name> ::= ?_0 # /=
753 case OO_SlashEqual: Out << "?_0"; break;
754 // <operator-name> ::= ?_1 # %=
755 case OO_PercentEqual: Out << "?_1"; break;
756 // <operator-name> ::= ?_2 # >>=
757 case OO_GreaterGreaterEqual: Out << "?_2"; break;
758 // <operator-name> ::= ?_3 # <<=
759 case OO_LessLessEqual: Out << "?_3"; break;
760 // <operator-name> ::= ?_4 # &=
761 case OO_AmpEqual: Out << "?_4"; break;
762 // <operator-name> ::= ?_5 # |=
763 case OO_PipeEqual: Out << "?_5"; break;
764 // <operator-name> ::= ?_6 # ^=
765 case OO_CaretEqual: Out << "?_6"; break;
766 // ?_7 # vftable
767 // ?_8 # vbtable
768 // ?_9 # vcall
769 // ?_A # typeof
770 // ?_B # local static guard
771 // ?_C # string
772 // ?_D # vbase destructor
773 // ?_E # vector deleting destructor
774 // ?_F # default constructor closure
775 // ?_G # scalar deleting destructor
776 // ?_H # vector constructor iterator
777 // ?_I # vector destructor iterator
778 // ?_J # vector vbase constructor iterator
779 // ?_K # virtual displacement map
780 // ?_L # eh vector constructor iterator
781 // ?_M # eh vector destructor iterator
782 // ?_N # eh vector vbase constructor iterator
783 // ?_O # copy constructor closure
784 // ?_P<name> # udt returning <name>
785 // ?_Q # <unknown>
786 // ?_R0 # RTTI Type Descriptor
787 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
788 // ?_R2 # RTTI Base Class Array
789 // ?_R3 # RTTI Class Hierarchy Descriptor
790 // ?_R4 # RTTI Complete Object Locator
791 // ?_S # local vftable
792 // ?_T # local vftable constructor closure
793 // <operator-name> ::= ?_U # new[]
794 case OO_Array_New: Out << "?_U"; break;
795 // <operator-name> ::= ?_V # delete[]
796 case OO_Array_Delete: Out << "?_V"; break;
797
798 case OO_Conditional: {
799 DiagnosticsEngine &Diags = Context.getDiags();
800 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
801 "cannot mangle this conditional operator yet");
802 Diags.Report(Loc, DiagID);
803 break;
804 }
805
806 case OO_None:
807 case NUM_OVERLOADED_OPERATORS:
808 llvm_unreachable("Not an overloaded operator");
809 }
810}
811
812void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
813 // <source name> ::= <identifier> @
814 std::string key = II->getNameStart();
815 BackRefMap::iterator Found;
816 if (UseNameBackReferences)
817 Found = NameBackReferences.find(key);
818 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
819 Out << II->getName() << '@';
820 if (UseNameBackReferences && NameBackReferences.size() < 10) {
821 size_t Size = NameBackReferences.size();
822 NameBackReferences[key] = Size;
823 }
824 } else {
825 Out << Found->second;
826 }
827}
828
829void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
830 Context.mangleObjCMethodName(MD, Out);
831}
832
833// Find out how many function decls live above this one and return an integer
834// suitable for use as the number in a numbered anonymous scope.
835// TODO: Memoize.
836static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
837 const DeclContext *DC = FD->getParent();
838 int level = 1;
839
840 while (DC && !DC->isTranslationUnit()) {
841 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
842 DC = DC->getParent();
843 }
844
845 return 2*level;
846}
847
848void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
849 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
850 // <numbered-anonymous-scope> ::= ? <number>
851 // Even though the name is rendered in reverse order (e.g.
852 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
853 // innermost. So a method bar in class C local to function foo gets mangled
854 // as something like:
855 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
856 // This is more apparent when you have a type nested inside a method of a
857 // type nested inside a function. A method baz in class D local to method
858 // bar of class C local to function foo gets mangled as:
859 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
860 // This scheme is general enough to support GCC-style nested
861 // functions. You could have a method baz of class C inside a function bar
862 // inside a function foo, like so:
863 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
864 int NestLevel = getLocalNestingLevel(FD);
865 Out << '?';
866 mangleNumber(NestLevel);
867 Out << '?';
868 mangle(FD, "?");
869}
870
871void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
872 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000873 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000874 // <template-name> ::= <unscoped-template-name> <template-args>
875 // ::= <substitution>
876 // Always start with the unqualified name.
877
878 // Templates have their own context for back references.
879 ArgBackRefMap OuterArgsContext;
880 BackRefMap OuterTemplateContext;
881 NameBackReferences.swap(OuterTemplateContext);
882 TypeBackReferences.swap(OuterArgsContext);
883
884 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000885 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000886
887 // Restore the previous back reference contexts.
888 NameBackReferences.swap(OuterTemplateContext);
889 TypeBackReferences.swap(OuterArgsContext);
890}
891
892void
893MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
894 // <unscoped-template-name> ::= ?$ <unqualified-name>
895 Out << "?$";
896 mangleUnqualifiedName(TD);
897}
898
899void
900MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
901 bool IsBoolean) {
902 // <integer-literal> ::= $0 <number>
903 Out << "$0";
904 // Make sure booleans are encoded as 0/1.
905 if (IsBoolean && Value.getBoolValue())
906 mangleNumber(1);
907 else
908 mangleNumber(Value);
909}
910
911void
912MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
913 // See if this is a constant expression.
914 llvm::APSInt Value;
915 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
916 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
917 return;
918 }
919
David Majnemerc80eb462013-08-13 06:32:20 +0000920 const CXXUuidofExpr *UE = 0;
921 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
922 if (UO->getOpcode() == UO_AddrOf)
923 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
924 } else
925 UE = dyn_cast<CXXUuidofExpr>(E);
926
927 if (UE) {
928 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
929 // const __s_GUID _GUID_{lower case UUID with underscores}
930 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
931 std::string Name = "_GUID_" + Uuid.lower();
932 std::replace(Name.begin(), Name.end(), '-', '_');
933
David Majnemer26314e12013-08-13 09:17:25 +0000934 // If we had to peek through an address-of operator, treat this like we are
David Majnemerc80eb462013-08-13 06:32:20 +0000935 // dealing with a pointer type. Otherwise, treat it like a const reference.
936 //
937 // N.B. This matches up with the handling of TemplateArgument::Declaration
938 // in mangleTemplateArg
939 if (UE == E)
940 Out << "$E?";
941 else
942 Out << "$1?";
943 Out << Name << "@@3U__s_GUID@@B";
944 return;
945 }
946
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000947 // As bad as this diagnostic is, it's better than crashing.
948 DiagnosticsEngine &Diags = Context.getDiags();
949 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
950 "cannot yet mangle expression type %0");
951 Diags.Report(E->getExprLoc(), DiagID)
952 << E->getStmtClassName() << E->getSourceRange();
953}
954
955void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000956MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
957 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000958 // <template-args> ::= {<type> | <integer-literal>}+ @
959 unsigned NumTemplateArgs = TemplateArgs.size();
960 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000961 const TemplateArgument &TA = TemplateArgs[i];
David Majnemer309f6452013-08-27 08:21:25 +0000962 mangleTemplateArg(TD, TA);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000963 }
964 Out << '@';
965}
966
Reid Kleckner5d90d182013-07-02 18:10:07 +0000967void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
David Majnemer309f6452013-08-27 08:21:25 +0000968 const TemplateArgument &TA) {
Reid Kleckner5d90d182013-07-02 18:10:07 +0000969 switch (TA.getKind()) {
970 case TemplateArgument::Null:
971 llvm_unreachable("Can't mangle null template arguments!");
David Majnemer309f6452013-08-27 08:21:25 +0000972 case TemplateArgument::TemplateExpansion:
973 llvm_unreachable("Can't mangle template expansion arguments!");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000974 case TemplateArgument::Type: {
975 QualType T = TA.getAsType();
976 mangleType(T, SourceRange(), QMM_Escape);
977 break;
978 }
David Majnemerf2081f62013-08-13 01:25:35 +0000979 case TemplateArgument::Declaration: {
980 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl());
981 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000982 break;
David Majnemerf2081f62013-08-13 01:25:35 +0000983 }
Reid Kleckner5d90d182013-07-02 18:10:07 +0000984 case TemplateArgument::Integral:
985 mangleIntegerLiteral(TA.getAsIntegral(),
986 TA.getIntegralType()->isBooleanType());
987 break;
David Majnemer7802fc92013-08-05 21:33:59 +0000988 case TemplateArgument::NullPtr:
989 Out << "$0A@";
990 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000991 case TemplateArgument::Expression:
992 mangleExpression(TA.getAsExpr());
993 break;
994 case TemplateArgument::Pack:
995 // Unlike Itanium, there is no character code to indicate an argument pack.
Reid Kleckner5d90d182013-07-02 18:10:07 +0000996 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
997 I != E; ++I)
David Majnemer309f6452013-08-27 08:21:25 +0000998 mangleTemplateArg(TD, *I);
Reid Kleckner5d90d182013-07-02 18:10:07 +0000999 break;
1000 case TemplateArgument::Template:
David Majnemer02c44f02013-08-05 22:26:46 +00001001 mangleType(cast<TagDecl>(
1002 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
1003 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +00001004 }
1005}
1006
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001007void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
1008 bool IsMember) {
1009 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
1010 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
1011 // 'I' means __restrict (32/64-bit).
1012 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
1013 // keyword!
1014 // <base-cvr-qualifiers> ::= A # near
1015 // ::= B # near const
1016 // ::= C # near volatile
1017 // ::= D # near const volatile
1018 // ::= E # far (16-bit)
1019 // ::= F # far const (16-bit)
1020 // ::= G # far volatile (16-bit)
1021 // ::= H # far const volatile (16-bit)
1022 // ::= I # huge (16-bit)
1023 // ::= J # huge const (16-bit)
1024 // ::= K # huge volatile (16-bit)
1025 // ::= L # huge const volatile (16-bit)
1026 // ::= M <basis> # based
1027 // ::= N <basis> # based const
1028 // ::= O <basis> # based volatile
1029 // ::= P <basis> # based const volatile
1030 // ::= Q # near member
1031 // ::= R # near const member
1032 // ::= S # near volatile member
1033 // ::= T # near const volatile member
1034 // ::= U # far member (16-bit)
1035 // ::= V # far const member (16-bit)
1036 // ::= W # far volatile member (16-bit)
1037 // ::= X # far const volatile member (16-bit)
1038 // ::= Y # huge member (16-bit)
1039 // ::= Z # huge const member (16-bit)
1040 // ::= 0 # huge volatile member (16-bit)
1041 // ::= 1 # huge const volatile member (16-bit)
1042 // ::= 2 <basis> # based member
1043 // ::= 3 <basis> # based const member
1044 // ::= 4 <basis> # based volatile member
1045 // ::= 5 <basis> # based const volatile member
1046 // ::= 6 # near function (pointers only)
1047 // ::= 7 # far function (pointers only)
1048 // ::= 8 # near method (pointers only)
1049 // ::= 9 # far method (pointers only)
1050 // ::= _A <basis> # based function (pointers only)
1051 // ::= _B <basis> # based function (far?) (pointers only)
1052 // ::= _C <basis> # based method (pointers only)
1053 // ::= _D <basis> # based method (far?) (pointers only)
1054 // ::= _E # block (Clang)
1055 // <basis> ::= 0 # __based(void)
1056 // ::= 1 # __based(segment)?
1057 // ::= 2 <name> # __based(name)
1058 // ::= 3 # ?
1059 // ::= 4 # ?
1060 // ::= 5 # not really based
1061 bool HasConst = Quals.hasConst(),
1062 HasVolatile = Quals.hasVolatile();
David Majnemerc0e64f32013-08-05 22:43:06 +00001063
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001064 if (!IsMember) {
1065 if (HasConst && HasVolatile) {
1066 Out << 'D';
1067 } else if (HasVolatile) {
1068 Out << 'C';
1069 } else if (HasConst) {
1070 Out << 'B';
1071 } else {
1072 Out << 'A';
1073 }
1074 } else {
1075 if (HasConst && HasVolatile) {
1076 Out << 'T';
1077 } else if (HasVolatile) {
1078 Out << 'S';
1079 } else if (HasConst) {
1080 Out << 'R';
1081 } else {
1082 Out << 'Q';
1083 }
1084 }
1085
1086 // FIXME: For now, just drop all extension qualifiers on the floor.
1087}
1088
1089void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1090 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1091 // ::= Q # const
1092 // ::= R # volatile
1093 // ::= S # const volatile
1094 bool HasConst = Quals.hasConst(),
1095 HasVolatile = Quals.hasVolatile();
1096 if (HasConst && HasVolatile) {
1097 Out << 'S';
1098 } else if (HasVolatile) {
1099 Out << 'R';
1100 } else if (HasConst) {
1101 Out << 'Q';
1102 } else {
1103 Out << 'P';
1104 }
1105}
1106
1107void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1108 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001109 // MSVC will backreference two canonically equivalent types that have slightly
1110 // different manglings when mangled alone.
David Majnemer58e4cd02013-09-11 04:44:30 +00001111
1112 // Decayed types do not match up with non-decayed versions of the same type.
1113 //
1114 // e.g.
1115 // void (*x)(void) will not form a backreference with void x(void)
1116 void *TypePtr;
1117 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1118 TypePtr = DT->getOriginalType().getCanonicalType().getAsOpaquePtr();
1119 // If the original parameter was textually written as an array,
1120 // instead treat the decayed parameter like it's const.
1121 //
1122 // e.g.
1123 // int [] -> int * const
1124 if (DT->getOriginalType()->isArrayType())
1125 T = T.withConst();
1126 } else
1127 TypePtr = T.getCanonicalType().getAsOpaquePtr();
1128
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001129 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1130
1131 if (Found == TypeBackReferences.end()) {
1132 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1133
David Majnemer58e4cd02013-09-11 04:44:30 +00001134 mangleType(T, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001135
1136 // See if it's worth creating a back reference.
1137 // Only types longer than 1 character are considered
1138 // and only 10 back references slots are available:
1139 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1140 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1141 size_t Size = TypeBackReferences.size();
1142 TypeBackReferences[TypePtr] = Size;
1143 }
1144 } else {
1145 Out << Found->second;
1146 }
1147}
1148
1149void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001150 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001151 // Don't use the canonical types. MSVC includes things like 'const' on
1152 // pointer arguments to function pointers that canonicalization strips away.
1153 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001154 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001155 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1156 // If there were any Quals, getAsArrayType() pushed them onto the array
1157 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001158 if (QMM == QMM_Mangle)
1159 Out << 'A';
1160 else if (QMM == QMM_Escape || QMM == QMM_Result)
1161 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001162 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001163 return;
1164 }
1165
1166 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1167 T->isBlockPointerType();
1168
1169 switch (QMM) {
1170 case QMM_Drop:
1171 break;
1172 case QMM_Mangle:
1173 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1174 Out << '6';
1175 mangleFunctionType(FT, 0, false, false);
1176 return;
1177 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001178 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001179 break;
1180 case QMM_Escape:
1181 if (!IsPointer && Quals) {
1182 Out << "$$C";
1183 mangleQualifiers(Quals, false);
1184 }
1185 break;
1186 case QMM_Result:
1187 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1188 Out << '?';
1189 mangleQualifiers(Quals, false);
1190 }
1191 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001192 }
1193
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001194 // We have to mangle these now, while we still have enough information.
1195 if (IsPointer)
1196 manglePointerQualifiers(Quals);
1197 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001198
1199 switch (ty->getTypeClass()) {
1200#define ABSTRACT_TYPE(CLASS, PARENT)
1201#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1202 case Type::CLASS: \
1203 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1204 return;
1205#define TYPE(CLASS, PARENT) \
1206 case Type::CLASS: \
1207 mangleType(cast<CLASS##Type>(ty), Range); \
1208 break;
1209#include "clang/AST/TypeNodes.def"
1210#undef ABSTRACT_TYPE
1211#undef NON_CANONICAL_TYPE
1212#undef TYPE
1213 }
1214}
1215
1216void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1217 SourceRange Range) {
1218 // <type> ::= <builtin-type>
1219 // <builtin-type> ::= X # void
1220 // ::= C # signed char
1221 // ::= D # char
1222 // ::= E # unsigned char
1223 // ::= F # short
1224 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1225 // ::= H # int
1226 // ::= I # unsigned int
1227 // ::= J # long
1228 // ::= K # unsigned long
1229 // L # <none>
1230 // ::= M # float
1231 // ::= N # double
1232 // ::= O # long double (__float80 is mangled differently)
1233 // ::= _J # long long, __int64
1234 // ::= _K # unsigned long long, __int64
1235 // ::= _L # __int128
1236 // ::= _M # unsigned __int128
1237 // ::= _N # bool
1238 // _O # <array in parameter>
1239 // ::= _T # __float80 (Intel)
1240 // ::= _W # wchar_t
1241 // ::= _Z # __float80 (Digital Mars)
1242 switch (T->getKind()) {
1243 case BuiltinType::Void: Out << 'X'; break;
1244 case BuiltinType::SChar: Out << 'C'; break;
1245 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1246 case BuiltinType::UChar: Out << 'E'; break;
1247 case BuiltinType::Short: Out << 'F'; break;
1248 case BuiltinType::UShort: Out << 'G'; break;
1249 case BuiltinType::Int: Out << 'H'; break;
1250 case BuiltinType::UInt: Out << 'I'; break;
1251 case BuiltinType::Long: Out << 'J'; break;
1252 case BuiltinType::ULong: Out << 'K'; break;
1253 case BuiltinType::Float: Out << 'M'; break;
1254 case BuiltinType::Double: Out << 'N'; break;
1255 // TODO: Determine size and mangle accordingly
1256 case BuiltinType::LongDouble: Out << 'O'; break;
1257 case BuiltinType::LongLong: Out << "_J"; break;
1258 case BuiltinType::ULongLong: Out << "_K"; break;
1259 case BuiltinType::Int128: Out << "_L"; break;
1260 case BuiltinType::UInt128: Out << "_M"; break;
1261 case BuiltinType::Bool: Out << "_N"; break;
1262 case BuiltinType::WChar_S:
1263 case BuiltinType::WChar_U: Out << "_W"; break;
1264
1265#define BUILTIN_TYPE(Id, SingletonId)
1266#define PLACEHOLDER_TYPE(Id, SingletonId) \
1267 case BuiltinType::Id:
1268#include "clang/AST/BuiltinTypes.def"
1269 case BuiltinType::Dependent:
1270 llvm_unreachable("placeholder types shouldn't get to name mangling");
1271
1272 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1273 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1274 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001275
1276 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1277 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1278 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1279 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1280 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1281 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001282 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001283 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001284
1285 case BuiltinType::NullPtr: Out << "$$T"; break;
1286
1287 case BuiltinType::Char16:
1288 case BuiltinType::Char32:
1289 case BuiltinType::Half: {
1290 DiagnosticsEngine &Diags = Context.getDiags();
1291 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1292 "cannot mangle this built-in %0 type yet");
1293 Diags.Report(Range.getBegin(), DiagID)
1294 << T->getName(Context.getASTContext().getPrintingPolicy())
1295 << Range;
1296 break;
1297 }
1298 }
1299}
1300
1301// <type> ::= <function-type>
1302void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1303 SourceRange) {
1304 // Structors only appear in decls, so at this point we know it's not a
1305 // structor type.
1306 // FIXME: This may not be lambda-friendly.
1307 Out << "$$A6";
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001308 mangleFunctionType(T, NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001309}
1310void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1311 SourceRange) {
1312 llvm_unreachable("Can't mangle K&R function prototypes");
1313}
1314
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001315void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1316 const FunctionDecl *D,
1317 bool IsStructor,
1318 bool IsInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001319 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1320 // <return-type> <argument-list> <throw-spec>
1321 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1322
Reid Klecknerf21818d2013-06-24 19:21:52 +00001323 SourceRange Range;
1324 if (D) Range = D->getSourceRange();
1325
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001326 // If this is a C++ instance method, mangle the CVR qualifiers for the
1327 // this pointer.
David Majnemer1c7a4092013-08-15 08:13:23 +00001328 if (IsInstMethod) {
1329 if (PointersAre64Bit)
1330 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001331 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
David Majnemer1c7a4092013-08-15 08:13:23 +00001332 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001333
1334 mangleCallingConvention(T, IsInstMethod);
1335
1336 // <return-type> ::= <type>
1337 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001338 if (IsStructor) {
1339 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1340 StructorType == Dtor_Deleting) {
1341 // The scalar deleting destructor takes an extra int argument.
1342 // However, the FunctionType generated has 0 arguments.
1343 // FIXME: This is a temporary hack.
1344 // Maybe should fix the FunctionType creation instead?
Timur Iskhodzhanov4b104062013-08-26 10:32:04 +00001345 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001346 return;
1347 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001348 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001349 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001350 QualType ResultType = Proto->getResultType();
1351 if (ResultType->isVoidType())
1352 ResultType = ResultType.getUnqualifiedType();
1353 mangleType(ResultType, Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001354 }
1355
1356 // <argument-list> ::= X # void
1357 // ::= <type>+ @
1358 // ::= <type>* Z # varargs
1359 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1360 Out << 'X';
1361 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001362 // Happens for function pointer type arguments for example.
1363 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1364 ArgEnd = Proto->arg_type_end();
1365 Arg != ArgEnd; ++Arg)
1366 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001367 // <builtin-type> ::= Z # ellipsis
1368 if (Proto->isVariadic())
1369 Out << 'Z';
1370 else
1371 Out << '@';
1372 }
1373
1374 mangleThrowSpecification(Proto);
1375}
1376
1377void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001378 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1379 // # pointer. in 64-bit mode *all*
1380 // # 'this' pointers are 64-bit.
1381 // ::= <global-function>
1382 // <member-function> ::= A # private: near
1383 // ::= B # private: far
1384 // ::= C # private: static near
1385 // ::= D # private: static far
1386 // ::= E # private: virtual near
1387 // ::= F # private: virtual far
1388 // ::= G # private: thunk near
1389 // ::= H # private: thunk far
1390 // ::= I # protected: near
1391 // ::= J # protected: far
1392 // ::= K # protected: static near
1393 // ::= L # protected: static far
1394 // ::= M # protected: virtual near
1395 // ::= N # protected: virtual far
1396 // ::= O # protected: thunk near
1397 // ::= P # protected: thunk far
1398 // ::= Q # public: near
1399 // ::= R # public: far
1400 // ::= S # public: static near
1401 // ::= T # public: static far
1402 // ::= U # public: virtual near
1403 // ::= V # public: virtual far
1404 // ::= W # public: thunk near
1405 // ::= X # public: thunk far
1406 // <global-function> ::= Y # global near
1407 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001408 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1409 switch (MD->getAccess()) {
1410 default:
1411 case AS_private:
1412 if (MD->isStatic())
1413 Out << 'C';
1414 else if (MD->isVirtual())
1415 Out << 'E';
1416 else
1417 Out << 'A';
1418 break;
1419 case AS_protected:
1420 if (MD->isStatic())
1421 Out << 'K';
1422 else if (MD->isVirtual())
1423 Out << 'M';
1424 else
1425 Out << 'I';
1426 break;
1427 case AS_public:
1428 if (MD->isStatic())
1429 Out << 'S';
1430 else if (MD->isVirtual())
1431 Out << 'U';
1432 else
1433 Out << 'Q';
1434 }
1435 } else
1436 Out << 'Y';
1437}
1438void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
1439 bool IsInstMethod) {
1440 // <calling-convention> ::= A # __cdecl
1441 // ::= B # __export __cdecl
1442 // ::= C # __pascal
1443 // ::= D # __export __pascal
1444 // ::= E # __thiscall
1445 // ::= F # __export __thiscall
1446 // ::= G # __stdcall
1447 // ::= H # __export __stdcall
1448 // ::= I # __fastcall
1449 // ::= J # __export __fastcall
1450 // The 'export' calling conventions are from a bygone era
1451 // (*cough*Win16*cough*) when functions were declared for export with
1452 // that keyword. (It didn't actually export them, it just made them so
1453 // that they could be in a DLL and somebody from another module could call
1454 // them.)
1455 CallingConv CC = T->getCallConv();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001456 switch (CC) {
1457 default:
1458 llvm_unreachable("Unsupported CC for mangling");
Charles Davise8519c32013-08-30 04:39:01 +00001459 case CC_X86_64Win64:
1460 case CC_X86_64SysV:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001461 case CC_C: Out << 'A'; break;
1462 case CC_X86Pascal: Out << 'C'; break;
1463 case CC_X86ThisCall: Out << 'E'; break;
1464 case CC_X86StdCall: Out << 'G'; break;
1465 case CC_X86FastCall: Out << 'I'; break;
1466 }
1467}
1468void MicrosoftCXXNameMangler::mangleThrowSpecification(
1469 const FunctionProtoType *FT) {
1470 // <throw-spec> ::= Z # throw(...) (default)
1471 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1472 // ::= <type>+
1473 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1474 // all actually mangled as 'Z'. (They're ignored because their associated
1475 // functionality isn't implemented, and probably never will be.)
1476 Out << 'Z';
1477}
1478
1479void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1480 SourceRange Range) {
1481 // Probably should be mangled as a template instantiation; need to see what
1482 // VC does first.
1483 DiagnosticsEngine &Diags = Context.getDiags();
1484 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1485 "cannot mangle this unresolved dependent type yet");
1486 Diags.Report(Range.getBegin(), DiagID)
1487 << Range;
1488}
1489
1490// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1491// <union-type> ::= T <name>
1492// <struct-type> ::= U <name>
1493// <class-type> ::= V <name>
1494// <enum-type> ::= W <size> <name>
1495void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001496 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001497}
1498void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001499 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001500}
David Majnemer02c44f02013-08-05 22:26:46 +00001501void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1502 switch (TD->getTagKind()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001503 case TTK_Union:
1504 Out << 'T';
1505 break;
1506 case TTK_Struct:
1507 case TTK_Interface:
1508 Out << 'U';
1509 break;
1510 case TTK_Class:
1511 Out << 'V';
1512 break;
1513 case TTK_Enum:
1514 Out << 'W';
1515 Out << getASTContext().getTypeSizeInChars(
David Majnemer02c44f02013-08-05 22:26:46 +00001516 cast<EnumDecl>(TD)->getIntegerType()).getQuantity();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001517 break;
1518 }
David Majnemer02c44f02013-08-05 22:26:46 +00001519 mangleName(TD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001520}
1521
1522// <type> ::= <array-type>
1523// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1524// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001525// <element-type> # as global, E is never required
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001526// It's supposed to be the other way around, but for some strange reason, it
1527// isn't. Today this behavior is retained for the sole purpose of backwards
1528// compatibility.
David Majnemer58e4cd02013-09-11 04:44:30 +00001529void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001530 // This isn't a recursive mangling, so now we have to do it all in this
1531 // one call.
David Majnemer58e4cd02013-09-11 04:44:30 +00001532 manglePointerQualifiers(T->getElementType().getQualifiers());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001533 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001534}
1535void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1536 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001537 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001538}
1539void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1540 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001541 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001542}
1543void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *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 IncompleteArrayType *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}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001551void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001552 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001553 SmallVector<llvm::APInt, 3> Dimensions;
1554 for (;;) {
1555 if (const ConstantArrayType *CAT =
1556 getASTContext().getAsConstantArrayType(ElementTy)) {
1557 Dimensions.push_back(CAT->getSize());
1558 ElementTy = CAT->getElementType();
1559 } else if (ElementTy->isVariableArrayType()) {
1560 const VariableArrayType *VAT =
1561 getASTContext().getAsVariableArrayType(ElementTy);
1562 DiagnosticsEngine &Diags = Context.getDiags();
1563 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1564 "cannot mangle this variable-length array yet");
1565 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1566 << VAT->getBracketsRange();
1567 return;
1568 } else if (ElementTy->isDependentSizedArrayType()) {
1569 // The dependent expression has to be folded into a constant (TODO).
1570 const DependentSizedArrayType *DSAT =
1571 getASTContext().getAsDependentSizedArrayType(ElementTy);
1572 DiagnosticsEngine &Diags = Context.getDiags();
1573 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1574 "cannot mangle this dependent-length array yet");
1575 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1576 << DSAT->getBracketsRange();
1577 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001578 } else if (const IncompleteArrayType *IAT =
1579 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1580 Dimensions.push_back(llvm::APInt(32, 0));
1581 ElementTy = IAT->getElementType();
1582 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001583 else break;
1584 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001585 Out << 'Y';
1586 // <dimension-count> ::= <number> # number of extra dimensions
1587 mangleNumber(Dimensions.size());
1588 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1589 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001590 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001591}
1592
1593// <type> ::= <pointer-to-member-type>
1594// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1595// <class name> <type>
1596void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1597 SourceRange Range) {
1598 QualType PointeeType = T->getPointeeType();
1599 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1600 Out << '8';
1601 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001602 mangleFunctionType(FPT, NULL, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001603 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001604 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1605 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001606 mangleQualifiers(PointeeType.getQualifiers(), true);
1607 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001608 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001609 }
1610}
1611
1612void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1613 SourceRange Range) {
1614 DiagnosticsEngine &Diags = Context.getDiags();
1615 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1616 "cannot mangle this template type parameter type yet");
1617 Diags.Report(Range.getBegin(), DiagID)
1618 << Range;
1619}
1620
1621void MicrosoftCXXNameMangler::mangleType(
1622 const SubstTemplateTypeParmPackType *T,
1623 SourceRange Range) {
1624 DiagnosticsEngine &Diags = Context.getDiags();
1625 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1626 "cannot mangle this substituted parameter pack yet");
1627 Diags.Report(Range.getBegin(), DiagID)
1628 << Range;
1629}
1630
1631// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001632// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1633// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001634void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1635 SourceRange Range) {
1636 QualType PointeeTy = T->getPointeeType();
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001637 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1638 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001639 mangleType(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001640}
1641void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1642 SourceRange Range) {
1643 // Object pointers never have qualifiers.
1644 Out << 'A';
David Majnemer1c7a4092013-08-15 08:13:23 +00001645 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1646 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001647 mangleType(T->getPointeeType(), Range);
1648}
1649
1650// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001651// <reference-type> ::= A E? <cvr-qualifiers> <type>
1652// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001653void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1654 SourceRange Range) {
1655 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001656 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1657 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001658 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001659}
1660
1661// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001662// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1663// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001664void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1665 SourceRange Range) {
1666 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001667 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1668 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001669 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001670}
1671
1672void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1673 SourceRange Range) {
1674 DiagnosticsEngine &Diags = Context.getDiags();
1675 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1676 "cannot mangle this complex number type yet");
1677 Diags.Report(Range.getBegin(), DiagID)
1678 << Range;
1679}
1680
1681void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1682 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001683 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1684 assert(ET && "vectors with non-builtin elements are unsupported");
1685 uint64_t Width = getASTContext().getTypeSize(T);
1686 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1687 // doesn't match the Intel types uses a custom mangling below.
1688 bool IntelVector = true;
1689 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1690 Out << "T__m64";
1691 } else if (Width == 128 || Width == 256) {
1692 if (ET->getKind() == BuiltinType::Float)
1693 Out << "T__m" << Width;
1694 else if (ET->getKind() == BuiltinType::LongLong)
1695 Out << "T__m" << Width << 'i';
1696 else if (ET->getKind() == BuiltinType::Double)
1697 Out << "U__m" << Width << 'd';
1698 else
1699 IntelVector = false;
1700 } else {
1701 IntelVector = false;
1702 }
1703
1704 if (!IntelVector) {
1705 // The MS ABI doesn't have a special mangling for vector types, so we define
1706 // our own mangling to handle uses of __vector_size__ on user-specified
1707 // types, and for extensions like __v4sf.
1708 Out << "T__clang_vec" << T->getNumElements() << '_';
1709 mangleType(ET, Range);
1710 }
1711
1712 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001713}
Reid Kleckner1232e272013-03-26 16:56:59 +00001714
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001715void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1716 SourceRange Range) {
1717 DiagnosticsEngine &Diags = Context.getDiags();
1718 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1719 "cannot mangle this extended vector type yet");
1720 Diags.Report(Range.getBegin(), DiagID)
1721 << Range;
1722}
1723void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1724 SourceRange Range) {
1725 DiagnosticsEngine &Diags = Context.getDiags();
1726 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1727 "cannot mangle this dependent-sized extended vector type yet");
1728 Diags.Report(Range.getBegin(), DiagID)
1729 << Range;
1730}
1731
1732void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1733 SourceRange) {
1734 // ObjC interfaces have structs underlying them.
1735 Out << 'U';
1736 mangleName(T->getDecl());
1737}
1738
1739void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1740 SourceRange Range) {
1741 // We don't allow overloading by different protocol qualification,
1742 // so mangling them isn't necessary.
1743 mangleType(T->getBaseType(), Range);
1744}
1745
1746void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1747 SourceRange Range) {
1748 Out << "_E";
1749
1750 QualType pointee = T->getPointeeType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001751 mangleFunctionType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001752}
1753
David Majnemer360d23e2013-08-16 08:29:13 +00001754void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1755 SourceRange) {
1756 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001757}
1758
1759void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1760 SourceRange Range) {
1761 DiagnosticsEngine &Diags = Context.getDiags();
1762 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1763 "cannot mangle this template specialization type yet");
1764 Diags.Report(Range.getBegin(), DiagID)
1765 << Range;
1766}
1767
1768void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1769 SourceRange Range) {
1770 DiagnosticsEngine &Diags = Context.getDiags();
1771 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1772 "cannot mangle this dependent name type yet");
1773 Diags.Report(Range.getBegin(), DiagID)
1774 << Range;
1775}
1776
1777void MicrosoftCXXNameMangler::mangleType(
1778 const DependentTemplateSpecializationType *T,
1779 SourceRange Range) {
1780 DiagnosticsEngine &Diags = Context.getDiags();
1781 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1782 "cannot mangle this dependent template specialization type yet");
1783 Diags.Report(Range.getBegin(), DiagID)
1784 << Range;
1785}
1786
1787void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1788 SourceRange Range) {
1789 DiagnosticsEngine &Diags = Context.getDiags();
1790 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1791 "cannot mangle this pack expansion yet");
1792 Diags.Report(Range.getBegin(), DiagID)
1793 << Range;
1794}
1795
1796void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1797 SourceRange Range) {
1798 DiagnosticsEngine &Diags = Context.getDiags();
1799 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1800 "cannot mangle this typeof(type) yet");
1801 Diags.Report(Range.getBegin(), DiagID)
1802 << Range;
1803}
1804
1805void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1806 SourceRange Range) {
1807 DiagnosticsEngine &Diags = Context.getDiags();
1808 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1809 "cannot mangle this typeof(expression) yet");
1810 Diags.Report(Range.getBegin(), DiagID)
1811 << Range;
1812}
1813
1814void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1815 SourceRange Range) {
1816 DiagnosticsEngine &Diags = Context.getDiags();
1817 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1818 "cannot mangle this decltype() yet");
1819 Diags.Report(Range.getBegin(), DiagID)
1820 << Range;
1821}
1822
1823void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1824 SourceRange Range) {
1825 DiagnosticsEngine &Diags = Context.getDiags();
1826 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1827 "cannot mangle this unary transform type yet");
1828 Diags.Report(Range.getBegin(), DiagID)
1829 << Range;
1830}
1831
1832void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1833 DiagnosticsEngine &Diags = Context.getDiags();
1834 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1835 "cannot mangle this 'auto' type yet");
1836 Diags.Report(Range.getBegin(), DiagID)
1837 << Range;
1838}
1839
1840void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1841 SourceRange Range) {
1842 DiagnosticsEngine &Diags = Context.getDiags();
1843 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1844 "cannot mangle this C11 atomic type yet");
1845 Diags.Report(Range.getBegin(), DiagID)
1846 << Range;
1847}
1848
1849void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1850 raw_ostream &Out) {
1851 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1852 "Invalid mangleName() call, argument is not a variable or function!");
1853 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1854 "Invalid mangleName() call on 'structor decl!");
1855
1856 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1857 getASTContext().getSourceManager(),
1858 "Mangling declaration");
1859
1860 MicrosoftCXXNameMangler Mangler(*this, Out);
1861 return Mangler.mangle(D);
1862}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001863
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001864void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1865 const ThunkInfo &Thunk,
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001866 raw_ostream &Out) {
1867 // FIXME: this is not yet a complete implementation, but merely a
1868 // reasonably-working stub to avoid crashing when required to emit a thunk.
1869 MicrosoftCXXNameMangler Mangler(*this, Out);
1870 Out << "\01?";
1871 Mangler.mangleName(MD);
1872 if (Thunk.This.NonVirtual != 0) {
1873 // FIXME: add support for protected/private or use mangleFunctionClass.
1874 Out << "W";
1875 llvm::APSInt APSNumber(/*BitWidth=*/32 /*FIXME: check on x64*/,
1876 /*isUnsigned=*/true);
1877 APSNumber = -Thunk.This.NonVirtual;
1878 Mangler.mangleNumber(APSNumber);
1879 } else {
1880 // FIXME: add support for protected/private or use mangleFunctionClass.
1881 Out << "Q";
1882 }
1883 // FIXME: mangle return adjustment? Most likely includes using an overridee FPT?
1884 Mangler.mangleFunctionType(MD->getType()->castAs<FunctionProtoType>(), MD, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001885}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001886
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001887void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1888 CXXDtorType Type,
1889 const ThisAdjustment &,
1890 raw_ostream &) {
1891 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1892 "cannot mangle thunk for this destructor yet");
1893 getDiags().Report(DD->getLocation(), DiagID);
1894}
Reid Kleckner90633022013-06-19 15:20:38 +00001895
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001896void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1897 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001898 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1899 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001900 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001901 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001902 MicrosoftCXXNameMangler Mangler(*this, Out);
1903 Mangler.getStream() << "\01??_7";
1904 Mangler.mangleName(RD);
Reid Kleckner90633022013-06-19 15:20:38 +00001905 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001906 // TODO: If the class has more than one vtable, mangle in the class it came
1907 // from.
1908 Mangler.getStream() << '@';
1909}
Reid Kleckner90633022013-06-19 15:20:38 +00001910
1911void MicrosoftMangleContext::mangleCXXVBTable(
1912 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1913 raw_ostream &Out) {
1914 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1915 // <cvr-qualifiers> [<name>] @
1916 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1917 // is always '7' for vbtables.
1918 MicrosoftCXXNameMangler Mangler(*this, Out);
1919 Mangler.getStream() << "\01??_8";
1920 Mangler.mangleName(Derived);
1921 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1922 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1923 E = BasePath.end();
1924 I != E; ++I) {
1925 Mangler.mangleName(*I);
1926 }
1927 Mangler.getStream() << '@';
1928}
1929
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001930void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1931 raw_ostream &) {
1932 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1933}
1934void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1935 int64_t Offset,
1936 const CXXRecordDecl *Type,
1937 raw_ostream &) {
1938 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1939}
1940void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1941 raw_ostream &) {
1942 // FIXME: Give a location...
1943 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1944 "cannot mangle RTTI descriptors for type %0 yet");
1945 getDiags().Report(DiagID)
1946 << T.getBaseTypeIdentifier();
1947}
1948void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1949 raw_ostream &) {
1950 // FIXME: Give a location...
1951 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1952 "cannot mangle the name of type %0 into RTTI descriptors yet");
1953 getDiags().Report(DiagID)
1954 << T.getBaseTypeIdentifier();
1955}
1956void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1957 CXXCtorType Type,
1958 raw_ostream & Out) {
1959 MicrosoftCXXNameMangler mangler(*this, Out);
1960 mangler.mangle(D);
1961}
1962void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1963 CXXDtorType Type,
1964 raw_ostream & Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001965 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001966 mangler.mangle(D);
1967}
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001968void MicrosoftMangleContext::mangleReferenceTemporary(const VarDecl *VD,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001969 raw_ostream &) {
1970 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1971 "cannot mangle this reference temporary yet");
1972 getDiags().Report(VD->getLocation(), DiagID);
1973}
1974
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001975void MicrosoftMangleContext::mangleStaticGuardVariable(const VarDecl *VD,
1976 raw_ostream &Out) {
1977 // <guard-name> ::= ?_B <postfix> @51
1978 // ::= ?$S <guard-num> @ <postfix> @4IA
1979
1980 // The first mangling is what MSVC uses to guard static locals in inline
1981 // functions. It uses a different mangling in external functions to support
1982 // guarding more than 32 variables. MSVC rejects inline functions with more
1983 // than 32 static locals. We don't fully implement the second mangling
1984 // because those guards are not externally visible, and instead use LLVM's
1985 // default renaming when creating a new guard variable.
1986 MicrosoftCXXNameMangler Mangler(*this, Out);
1987
1988 bool Visible = VD->isExternallyVisible();
1989 // <operator-name> ::= ?_B # local static guard
1990 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@");
1991 Mangler.manglePostfix(VD->getDeclContext());
1992 Mangler.getStream() << (Visible ? "@51" : "@4IA");
1993}
1994
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00001995void MicrosoftMangleContext::mangleInitFiniStub(const VarDecl *D,
1996 raw_ostream &Out,
1997 char CharCode) {
1998 MicrosoftCXXNameMangler Mangler(*this, Out);
1999 Mangler.getStream() << "\01??__" << CharCode;
2000 Mangler.mangleName(D);
2001 // This is the function class mangling. These stubs are global, non-variadic,
2002 // cdecl functions that return void and take no args.
2003 Mangler.getStream() << "YAXXZ";
2004}
2005
2006void MicrosoftMangleContext::mangleDynamicInitializer(const VarDecl *D,
2007 raw_ostream &Out) {
2008 // <initializer-name> ::= ?__E <name> YAXXZ
2009 mangleInitFiniStub(D, Out, 'E');
2010}
2011
Reid Kleckner942f9fe2013-09-10 20:14:30 +00002012void MicrosoftMangleContext::mangleDynamicAtExitDestructor(const VarDecl *D,
2013 raw_ostream &Out) {
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00002014 // <destructor-name> ::= ?__F <name> YAXXZ
2015 mangleInitFiniStub(D, Out, 'F');
Reid Kleckner942f9fe2013-09-10 20:14:30 +00002016}
2017
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002018MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
2019 DiagnosticsEngine &Diags) {
2020 return new MicrosoftMangleContext(Context, Diags);
2021}