blob: 119bc86be298adb2aa1e84f962cd3488819d1b4b [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 Majnemeraa824612013-09-17 23:57:10 +0000566 if (TD->hasDeclaratorForAnonDecl())
567 // Anonymous types with no tag or typedef get the name of their
568 // declarator mangled in.
569 Out << "<unnamed-type-" << TD->getDeclaratorForAnonDecl()->getName()
570 << ">@";
571 else
572 // Anonymous types with no tag, no typedef, or declarator get
573 // '<unnamed-tag>@'.
574 Out << "<unnamed-tag>@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000575 break;
576 }
577
578 case DeclarationName::ObjCZeroArgSelector:
579 case DeclarationName::ObjCOneArgSelector:
580 case DeclarationName::ObjCMultiArgSelector:
581 llvm_unreachable("Can't mangle Objective-C selector names here!");
582
583 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000584 if (ND == Structor) {
585 assert(StructorType == Ctor_Complete &&
586 "Should never be asked to mangle a ctor other than complete");
587 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000588 Out << "?0";
589 break;
590
591 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000592 if (ND == Structor)
593 // If the named decl is the C++ destructor we're mangling,
594 // use the type we were given.
595 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
596 else
Reid Klecknera4130ba2013-07-22 13:51:44 +0000597 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000598 // class with a destructor is declared within a destructor.
Reid Klecknera4130ba2013-07-22 13:51:44 +0000599 mangleCXXDtorType(Dtor_Base);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000600 break;
601
602 case DeclarationName::CXXConversionFunctionName:
603 // <operator-name> ::= ?B # (cast)
604 // The target type is encoded as the return type.
605 Out << "?B";
606 break;
607
608 case DeclarationName::CXXOperatorName:
609 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
610 break;
611
612 case DeclarationName::CXXLiteralOperatorName: {
613 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
614 DiagnosticsEngine Diags = Context.getDiags();
615 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
616 "cannot mangle this literal operator yet");
617 Diags.Report(ND->getLocation(), DiagID);
618 break;
619 }
620
621 case DeclarationName::CXXUsingDirective:
622 llvm_unreachable("Can't mangle a using directive name!");
623 }
624}
625
626void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
627 bool NoFunction) {
628 // <postfix> ::= <unqualified-name> [<postfix>]
629 // ::= <substitution> [<postfix>]
630
631 if (!DC) return;
632
633 while (isa<LinkageSpecDecl>(DC))
634 DC = DC->getParent();
635
636 if (DC->isTranslationUnit())
637 return;
638
639 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedmane5798892013-07-10 01:13:27 +0000640 DiagnosticsEngine Diags = Context.getDiags();
641 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
642 "cannot mangle a local inside this block yet");
643 Diags.Report(BD->getLocation(), DiagID);
644
645 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
646 // for how this should be done.
647 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000648 Out << '@';
649 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000650 } else if (isa<CapturedDecl>(DC)) {
651 // Skip CapturedDecl context.
652 manglePostfix(DC->getParent(), NoFunction);
653 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000654 }
655
656 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
657 return;
658 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
659 mangleObjCMethodName(Method);
660 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
661 mangleLocalName(Func);
662 else {
663 mangleUnqualifiedName(cast<NamedDecl>(DC));
664 manglePostfix(DC->getParent(), NoFunction);
665 }
666}
667
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000668void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000669 // Microsoft uses the names on the case labels for these dtor variants. Clang
670 // uses the Itanium terminology internally. Everything in this ABI delegates
671 // towards the base dtor.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000672 switch (T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000673 // <operator-name> ::= ?1 # destructor
674 case Dtor_Base: Out << "?1"; return;
675 // <operator-name> ::= ?_D # vbase destructor
676 case Dtor_Complete: Out << "?_D"; return;
677 // <operator-name> ::= ?_G # scalar deleting destructor
678 case Dtor_Deleting: Out << "?_G"; return;
679 // <operator-name> ::= ?_E # vector deleting destructor
680 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
681 // it.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000682 }
683 llvm_unreachable("Unsupported dtor type?");
684}
685
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000686void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
687 SourceLocation Loc) {
688 switch (OO) {
689 // ?0 # constructor
690 // ?1 # destructor
691 // <operator-name> ::= ?2 # new
692 case OO_New: Out << "?2"; break;
693 // <operator-name> ::= ?3 # delete
694 case OO_Delete: Out << "?3"; break;
695 // <operator-name> ::= ?4 # =
696 case OO_Equal: Out << "?4"; break;
697 // <operator-name> ::= ?5 # >>
698 case OO_GreaterGreater: Out << "?5"; break;
699 // <operator-name> ::= ?6 # <<
700 case OO_LessLess: Out << "?6"; break;
701 // <operator-name> ::= ?7 # !
702 case OO_Exclaim: Out << "?7"; break;
703 // <operator-name> ::= ?8 # ==
704 case OO_EqualEqual: Out << "?8"; break;
705 // <operator-name> ::= ?9 # !=
706 case OO_ExclaimEqual: Out << "?9"; break;
707 // <operator-name> ::= ?A # []
708 case OO_Subscript: Out << "?A"; break;
709 // ?B # conversion
710 // <operator-name> ::= ?C # ->
711 case OO_Arrow: Out << "?C"; break;
712 // <operator-name> ::= ?D # *
713 case OO_Star: Out << "?D"; break;
714 // <operator-name> ::= ?E # ++
715 case OO_PlusPlus: Out << "?E"; break;
716 // <operator-name> ::= ?F # --
717 case OO_MinusMinus: Out << "?F"; break;
718 // <operator-name> ::= ?G # -
719 case OO_Minus: Out << "?G"; break;
720 // <operator-name> ::= ?H # +
721 case OO_Plus: Out << "?H"; break;
722 // <operator-name> ::= ?I # &
723 case OO_Amp: Out << "?I"; break;
724 // <operator-name> ::= ?J # ->*
725 case OO_ArrowStar: Out << "?J"; break;
726 // <operator-name> ::= ?K # /
727 case OO_Slash: Out << "?K"; break;
728 // <operator-name> ::= ?L # %
729 case OO_Percent: Out << "?L"; break;
730 // <operator-name> ::= ?M # <
731 case OO_Less: Out << "?M"; break;
732 // <operator-name> ::= ?N # <=
733 case OO_LessEqual: Out << "?N"; break;
734 // <operator-name> ::= ?O # >
735 case OO_Greater: Out << "?O"; break;
736 // <operator-name> ::= ?P # >=
737 case OO_GreaterEqual: Out << "?P"; break;
738 // <operator-name> ::= ?Q # ,
739 case OO_Comma: Out << "?Q"; break;
740 // <operator-name> ::= ?R # ()
741 case OO_Call: Out << "?R"; break;
742 // <operator-name> ::= ?S # ~
743 case OO_Tilde: Out << "?S"; break;
744 // <operator-name> ::= ?T # ^
745 case OO_Caret: Out << "?T"; break;
746 // <operator-name> ::= ?U # |
747 case OO_Pipe: Out << "?U"; break;
748 // <operator-name> ::= ?V # &&
749 case OO_AmpAmp: Out << "?V"; break;
750 // <operator-name> ::= ?W # ||
751 case OO_PipePipe: Out << "?W"; break;
752 // <operator-name> ::= ?X # *=
753 case OO_StarEqual: Out << "?X"; break;
754 // <operator-name> ::= ?Y # +=
755 case OO_PlusEqual: Out << "?Y"; break;
756 // <operator-name> ::= ?Z # -=
757 case OO_MinusEqual: Out << "?Z"; break;
758 // <operator-name> ::= ?_0 # /=
759 case OO_SlashEqual: Out << "?_0"; break;
760 // <operator-name> ::= ?_1 # %=
761 case OO_PercentEqual: Out << "?_1"; break;
762 // <operator-name> ::= ?_2 # >>=
763 case OO_GreaterGreaterEqual: Out << "?_2"; break;
764 // <operator-name> ::= ?_3 # <<=
765 case OO_LessLessEqual: Out << "?_3"; break;
766 // <operator-name> ::= ?_4 # &=
767 case OO_AmpEqual: Out << "?_4"; break;
768 // <operator-name> ::= ?_5 # |=
769 case OO_PipeEqual: Out << "?_5"; break;
770 // <operator-name> ::= ?_6 # ^=
771 case OO_CaretEqual: Out << "?_6"; break;
772 // ?_7 # vftable
773 // ?_8 # vbtable
774 // ?_9 # vcall
775 // ?_A # typeof
776 // ?_B # local static guard
777 // ?_C # string
778 // ?_D # vbase destructor
779 // ?_E # vector deleting destructor
780 // ?_F # default constructor closure
781 // ?_G # scalar deleting destructor
782 // ?_H # vector constructor iterator
783 // ?_I # vector destructor iterator
784 // ?_J # vector vbase constructor iterator
785 // ?_K # virtual displacement map
786 // ?_L # eh vector constructor iterator
787 // ?_M # eh vector destructor iterator
788 // ?_N # eh vector vbase constructor iterator
789 // ?_O # copy constructor closure
790 // ?_P<name> # udt returning <name>
791 // ?_Q # <unknown>
792 // ?_R0 # RTTI Type Descriptor
793 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
794 // ?_R2 # RTTI Base Class Array
795 // ?_R3 # RTTI Class Hierarchy Descriptor
796 // ?_R4 # RTTI Complete Object Locator
797 // ?_S # local vftable
798 // ?_T # local vftable constructor closure
799 // <operator-name> ::= ?_U # new[]
800 case OO_Array_New: Out << "?_U"; break;
801 // <operator-name> ::= ?_V # delete[]
802 case OO_Array_Delete: Out << "?_V"; break;
803
804 case OO_Conditional: {
805 DiagnosticsEngine &Diags = Context.getDiags();
806 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
807 "cannot mangle this conditional operator yet");
808 Diags.Report(Loc, DiagID);
809 break;
810 }
811
812 case OO_None:
813 case NUM_OVERLOADED_OPERATORS:
814 llvm_unreachable("Not an overloaded operator");
815 }
816}
817
818void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
819 // <source name> ::= <identifier> @
820 std::string key = II->getNameStart();
821 BackRefMap::iterator Found;
822 if (UseNameBackReferences)
823 Found = NameBackReferences.find(key);
824 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
825 Out << II->getName() << '@';
826 if (UseNameBackReferences && NameBackReferences.size() < 10) {
827 size_t Size = NameBackReferences.size();
828 NameBackReferences[key] = Size;
829 }
830 } else {
831 Out << Found->second;
832 }
833}
834
835void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
836 Context.mangleObjCMethodName(MD, Out);
837}
838
839// Find out how many function decls live above this one and return an integer
840// suitable for use as the number in a numbered anonymous scope.
841// TODO: Memoize.
842static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
843 const DeclContext *DC = FD->getParent();
844 int level = 1;
845
846 while (DC && !DC->isTranslationUnit()) {
847 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
848 DC = DC->getParent();
849 }
850
851 return 2*level;
852}
853
854void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
855 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
856 // <numbered-anonymous-scope> ::= ? <number>
857 // Even though the name is rendered in reverse order (e.g.
858 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
859 // innermost. So a method bar in class C local to function foo gets mangled
860 // as something like:
861 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
862 // This is more apparent when you have a type nested inside a method of a
863 // type nested inside a function. A method baz in class D local to method
864 // bar of class C local to function foo gets mangled as:
865 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
866 // This scheme is general enough to support GCC-style nested
867 // functions. You could have a method baz of class C inside a function bar
868 // inside a function foo, like so:
869 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
870 int NestLevel = getLocalNestingLevel(FD);
871 Out << '?';
872 mangleNumber(NestLevel);
873 Out << '?';
874 mangle(FD, "?");
875}
876
877void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
878 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000879 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000880 // <template-name> ::= <unscoped-template-name> <template-args>
881 // ::= <substitution>
882 // Always start with the unqualified name.
883
884 // Templates have their own context for back references.
885 ArgBackRefMap OuterArgsContext;
886 BackRefMap OuterTemplateContext;
887 NameBackReferences.swap(OuterTemplateContext);
888 TypeBackReferences.swap(OuterArgsContext);
889
890 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000891 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000892
893 // Restore the previous back reference contexts.
894 NameBackReferences.swap(OuterTemplateContext);
895 TypeBackReferences.swap(OuterArgsContext);
896}
897
898void
899MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
900 // <unscoped-template-name> ::= ?$ <unqualified-name>
901 Out << "?$";
902 mangleUnqualifiedName(TD);
903}
904
905void
906MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
907 bool IsBoolean) {
908 // <integer-literal> ::= $0 <number>
909 Out << "$0";
910 // Make sure booleans are encoded as 0/1.
911 if (IsBoolean && Value.getBoolValue())
912 mangleNumber(1);
913 else
914 mangleNumber(Value);
915}
916
917void
918MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
919 // See if this is a constant expression.
920 llvm::APSInt Value;
921 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
922 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
923 return;
924 }
925
David Majnemerc80eb462013-08-13 06:32:20 +0000926 const CXXUuidofExpr *UE = 0;
927 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
928 if (UO->getOpcode() == UO_AddrOf)
929 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
930 } else
931 UE = dyn_cast<CXXUuidofExpr>(E);
932
933 if (UE) {
934 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
935 // const __s_GUID _GUID_{lower case UUID with underscores}
936 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
937 std::string Name = "_GUID_" + Uuid.lower();
938 std::replace(Name.begin(), Name.end(), '-', '_');
939
David Majnemer26314e12013-08-13 09:17:25 +0000940 // If we had to peek through an address-of operator, treat this like we are
David Majnemerc80eb462013-08-13 06:32:20 +0000941 // dealing with a pointer type. Otherwise, treat it like a const reference.
942 //
943 // N.B. This matches up with the handling of TemplateArgument::Declaration
944 // in mangleTemplateArg
945 if (UE == E)
946 Out << "$E?";
947 else
948 Out << "$1?";
949 Out << Name << "@@3U__s_GUID@@B";
950 return;
951 }
952
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000953 // As bad as this diagnostic is, it's better than crashing.
954 DiagnosticsEngine &Diags = Context.getDiags();
955 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
956 "cannot yet mangle expression type %0");
957 Diags.Report(E->getExprLoc(), DiagID)
958 << E->getStmtClassName() << E->getSourceRange();
959}
960
961void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000962MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
963 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000964 // <template-args> ::= {<type> | <integer-literal>}+ @
965 unsigned NumTemplateArgs = TemplateArgs.size();
966 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000967 const TemplateArgument &TA = TemplateArgs[i];
David Majnemer309f6452013-08-27 08:21:25 +0000968 mangleTemplateArg(TD, TA);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000969 }
970 Out << '@';
971}
972
Reid Kleckner5d90d182013-07-02 18:10:07 +0000973void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
David Majnemer309f6452013-08-27 08:21:25 +0000974 const TemplateArgument &TA) {
Reid Kleckner5d90d182013-07-02 18:10:07 +0000975 switch (TA.getKind()) {
976 case TemplateArgument::Null:
977 llvm_unreachable("Can't mangle null template arguments!");
David Majnemer309f6452013-08-27 08:21:25 +0000978 case TemplateArgument::TemplateExpansion:
979 llvm_unreachable("Can't mangle template expansion arguments!");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000980 case TemplateArgument::Type: {
981 QualType T = TA.getAsType();
982 mangleType(T, SourceRange(), QMM_Escape);
983 break;
984 }
David Majnemerf2081f62013-08-13 01:25:35 +0000985 case TemplateArgument::Declaration: {
986 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl());
987 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000988 break;
David Majnemerf2081f62013-08-13 01:25:35 +0000989 }
Reid Kleckner5d90d182013-07-02 18:10:07 +0000990 case TemplateArgument::Integral:
991 mangleIntegerLiteral(TA.getAsIntegral(),
992 TA.getIntegralType()->isBooleanType());
993 break;
David Majnemer7802fc92013-08-05 21:33:59 +0000994 case TemplateArgument::NullPtr:
995 Out << "$0A@";
996 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000997 case TemplateArgument::Expression:
998 mangleExpression(TA.getAsExpr());
999 break;
1000 case TemplateArgument::Pack:
1001 // Unlike Itanium, there is no character code to indicate an argument pack.
Reid Kleckner5d90d182013-07-02 18:10:07 +00001002 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
1003 I != E; ++I)
David Majnemer309f6452013-08-27 08:21:25 +00001004 mangleTemplateArg(TD, *I);
Reid Kleckner5d90d182013-07-02 18:10:07 +00001005 break;
1006 case TemplateArgument::Template:
David Majnemer02c44f02013-08-05 22:26:46 +00001007 mangleType(cast<TagDecl>(
1008 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
1009 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +00001010 }
1011}
1012
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001013void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
1014 bool IsMember) {
1015 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
1016 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
1017 // 'I' means __restrict (32/64-bit).
1018 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
1019 // keyword!
1020 // <base-cvr-qualifiers> ::= A # near
1021 // ::= B # near const
1022 // ::= C # near volatile
1023 // ::= D # near const volatile
1024 // ::= E # far (16-bit)
1025 // ::= F # far const (16-bit)
1026 // ::= G # far volatile (16-bit)
1027 // ::= H # far const volatile (16-bit)
1028 // ::= I # huge (16-bit)
1029 // ::= J # huge const (16-bit)
1030 // ::= K # huge volatile (16-bit)
1031 // ::= L # huge const volatile (16-bit)
1032 // ::= M <basis> # based
1033 // ::= N <basis> # based const
1034 // ::= O <basis> # based volatile
1035 // ::= P <basis> # based const volatile
1036 // ::= Q # near member
1037 // ::= R # near const member
1038 // ::= S # near volatile member
1039 // ::= T # near const volatile member
1040 // ::= U # far member (16-bit)
1041 // ::= V # far const member (16-bit)
1042 // ::= W # far volatile member (16-bit)
1043 // ::= X # far const volatile member (16-bit)
1044 // ::= Y # huge member (16-bit)
1045 // ::= Z # huge const member (16-bit)
1046 // ::= 0 # huge volatile member (16-bit)
1047 // ::= 1 # huge const volatile member (16-bit)
1048 // ::= 2 <basis> # based member
1049 // ::= 3 <basis> # based const member
1050 // ::= 4 <basis> # based volatile member
1051 // ::= 5 <basis> # based const volatile member
1052 // ::= 6 # near function (pointers only)
1053 // ::= 7 # far function (pointers only)
1054 // ::= 8 # near method (pointers only)
1055 // ::= 9 # far method (pointers only)
1056 // ::= _A <basis> # based function (pointers only)
1057 // ::= _B <basis> # based function (far?) (pointers only)
1058 // ::= _C <basis> # based method (pointers only)
1059 // ::= _D <basis> # based method (far?) (pointers only)
1060 // ::= _E # block (Clang)
1061 // <basis> ::= 0 # __based(void)
1062 // ::= 1 # __based(segment)?
1063 // ::= 2 <name> # __based(name)
1064 // ::= 3 # ?
1065 // ::= 4 # ?
1066 // ::= 5 # not really based
1067 bool HasConst = Quals.hasConst(),
1068 HasVolatile = Quals.hasVolatile();
David Majnemerc0e64f32013-08-05 22:43:06 +00001069
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001070 if (!IsMember) {
1071 if (HasConst && HasVolatile) {
1072 Out << 'D';
1073 } else if (HasVolatile) {
1074 Out << 'C';
1075 } else if (HasConst) {
1076 Out << 'B';
1077 } else {
1078 Out << 'A';
1079 }
1080 } else {
1081 if (HasConst && HasVolatile) {
1082 Out << 'T';
1083 } else if (HasVolatile) {
1084 Out << 'S';
1085 } else if (HasConst) {
1086 Out << 'R';
1087 } else {
1088 Out << 'Q';
1089 }
1090 }
1091
1092 // FIXME: For now, just drop all extension qualifiers on the floor.
1093}
1094
1095void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1096 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1097 // ::= Q # const
1098 // ::= R # volatile
1099 // ::= S # const volatile
1100 bool HasConst = Quals.hasConst(),
1101 HasVolatile = Quals.hasVolatile();
1102 if (HasConst && HasVolatile) {
1103 Out << 'S';
1104 } else if (HasVolatile) {
1105 Out << 'R';
1106 } else if (HasConst) {
1107 Out << 'Q';
1108 } else {
1109 Out << 'P';
1110 }
1111}
1112
1113void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1114 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001115 // MSVC will backreference two canonically equivalent types that have slightly
1116 // different manglings when mangled alone.
David Majnemer58e4cd02013-09-11 04:44:30 +00001117
1118 // Decayed types do not match up with non-decayed versions of the same type.
1119 //
1120 // e.g.
1121 // void (*x)(void) will not form a backreference with void x(void)
1122 void *TypePtr;
1123 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1124 TypePtr = DT->getOriginalType().getCanonicalType().getAsOpaquePtr();
1125 // If the original parameter was textually written as an array,
1126 // instead treat the decayed parameter like it's const.
1127 //
1128 // e.g.
1129 // int [] -> int * const
1130 if (DT->getOriginalType()->isArrayType())
1131 T = T.withConst();
1132 } else
1133 TypePtr = T.getCanonicalType().getAsOpaquePtr();
1134
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001135 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1136
1137 if (Found == TypeBackReferences.end()) {
1138 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1139
David Majnemer58e4cd02013-09-11 04:44:30 +00001140 mangleType(T, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001141
1142 // See if it's worth creating a back reference.
1143 // Only types longer than 1 character are considered
1144 // and only 10 back references slots are available:
1145 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1146 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1147 size_t Size = TypeBackReferences.size();
1148 TypeBackReferences[TypePtr] = Size;
1149 }
1150 } else {
1151 Out << Found->second;
1152 }
1153}
1154
1155void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001156 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001157 // Don't use the canonical types. MSVC includes things like 'const' on
1158 // pointer arguments to function pointers that canonicalization strips away.
1159 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001160 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001161 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1162 // If there were any Quals, getAsArrayType() pushed them onto the array
1163 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001164 if (QMM == QMM_Mangle)
1165 Out << 'A';
1166 else if (QMM == QMM_Escape || QMM == QMM_Result)
1167 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001168 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001169 return;
1170 }
1171
1172 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1173 T->isBlockPointerType();
1174
1175 switch (QMM) {
1176 case QMM_Drop:
1177 break;
1178 case QMM_Mangle:
1179 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1180 Out << '6';
1181 mangleFunctionType(FT, 0, false, false);
1182 return;
1183 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001184 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001185 break;
1186 case QMM_Escape:
1187 if (!IsPointer && Quals) {
1188 Out << "$$C";
1189 mangleQualifiers(Quals, false);
1190 }
1191 break;
1192 case QMM_Result:
1193 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1194 Out << '?';
1195 mangleQualifiers(Quals, false);
1196 }
1197 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001198 }
1199
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001200 // We have to mangle these now, while we still have enough information.
1201 if (IsPointer)
1202 manglePointerQualifiers(Quals);
1203 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001204
1205 switch (ty->getTypeClass()) {
1206#define ABSTRACT_TYPE(CLASS, PARENT)
1207#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1208 case Type::CLASS: \
1209 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1210 return;
1211#define TYPE(CLASS, PARENT) \
1212 case Type::CLASS: \
1213 mangleType(cast<CLASS##Type>(ty), Range); \
1214 break;
1215#include "clang/AST/TypeNodes.def"
1216#undef ABSTRACT_TYPE
1217#undef NON_CANONICAL_TYPE
1218#undef TYPE
1219 }
1220}
1221
1222void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1223 SourceRange Range) {
1224 // <type> ::= <builtin-type>
1225 // <builtin-type> ::= X # void
1226 // ::= C # signed char
1227 // ::= D # char
1228 // ::= E # unsigned char
1229 // ::= F # short
1230 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1231 // ::= H # int
1232 // ::= I # unsigned int
1233 // ::= J # long
1234 // ::= K # unsigned long
1235 // L # <none>
1236 // ::= M # float
1237 // ::= N # double
1238 // ::= O # long double (__float80 is mangled differently)
1239 // ::= _J # long long, __int64
1240 // ::= _K # unsigned long long, __int64
1241 // ::= _L # __int128
1242 // ::= _M # unsigned __int128
1243 // ::= _N # bool
1244 // _O # <array in parameter>
1245 // ::= _T # __float80 (Intel)
1246 // ::= _W # wchar_t
1247 // ::= _Z # __float80 (Digital Mars)
1248 switch (T->getKind()) {
1249 case BuiltinType::Void: Out << 'X'; break;
1250 case BuiltinType::SChar: Out << 'C'; break;
1251 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1252 case BuiltinType::UChar: Out << 'E'; break;
1253 case BuiltinType::Short: Out << 'F'; break;
1254 case BuiltinType::UShort: Out << 'G'; break;
1255 case BuiltinType::Int: Out << 'H'; break;
1256 case BuiltinType::UInt: Out << 'I'; break;
1257 case BuiltinType::Long: Out << 'J'; break;
1258 case BuiltinType::ULong: Out << 'K'; break;
1259 case BuiltinType::Float: Out << 'M'; break;
1260 case BuiltinType::Double: Out << 'N'; break;
1261 // TODO: Determine size and mangle accordingly
1262 case BuiltinType::LongDouble: Out << 'O'; break;
1263 case BuiltinType::LongLong: Out << "_J"; break;
1264 case BuiltinType::ULongLong: Out << "_K"; break;
1265 case BuiltinType::Int128: Out << "_L"; break;
1266 case BuiltinType::UInt128: Out << "_M"; break;
1267 case BuiltinType::Bool: Out << "_N"; break;
1268 case BuiltinType::WChar_S:
1269 case BuiltinType::WChar_U: Out << "_W"; break;
1270
1271#define BUILTIN_TYPE(Id, SingletonId)
1272#define PLACEHOLDER_TYPE(Id, SingletonId) \
1273 case BuiltinType::Id:
1274#include "clang/AST/BuiltinTypes.def"
1275 case BuiltinType::Dependent:
1276 llvm_unreachable("placeholder types shouldn't get to name mangling");
1277
1278 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1279 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1280 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001281
1282 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1283 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1284 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1285 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1286 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1287 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001288 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001289 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001290
1291 case BuiltinType::NullPtr: Out << "$$T"; break;
1292
1293 case BuiltinType::Char16:
1294 case BuiltinType::Char32:
1295 case BuiltinType::Half: {
1296 DiagnosticsEngine &Diags = Context.getDiags();
1297 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1298 "cannot mangle this built-in %0 type yet");
1299 Diags.Report(Range.getBegin(), DiagID)
1300 << T->getName(Context.getASTContext().getPrintingPolicy())
1301 << Range;
1302 break;
1303 }
1304 }
1305}
1306
1307// <type> ::= <function-type>
1308void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1309 SourceRange) {
1310 // Structors only appear in decls, so at this point we know it's not a
1311 // structor type.
1312 // FIXME: This may not be lambda-friendly.
1313 Out << "$$A6";
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001314 mangleFunctionType(T, NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001315}
1316void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1317 SourceRange) {
1318 llvm_unreachable("Can't mangle K&R function prototypes");
1319}
1320
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001321void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1322 const FunctionDecl *D,
1323 bool IsStructor,
1324 bool IsInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001325 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1326 // <return-type> <argument-list> <throw-spec>
1327 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1328
Reid Klecknerf21818d2013-06-24 19:21:52 +00001329 SourceRange Range;
1330 if (D) Range = D->getSourceRange();
1331
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001332 // If this is a C++ instance method, mangle the CVR qualifiers for the
1333 // this pointer.
David Majnemer1c7a4092013-08-15 08:13:23 +00001334 if (IsInstMethod) {
1335 if (PointersAre64Bit)
1336 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001337 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
David Majnemer1c7a4092013-08-15 08:13:23 +00001338 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001339
1340 mangleCallingConvention(T, IsInstMethod);
1341
1342 // <return-type> ::= <type>
1343 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001344 if (IsStructor) {
1345 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1346 StructorType == Dtor_Deleting) {
1347 // The scalar deleting destructor takes an extra int argument.
1348 // However, the FunctionType generated has 0 arguments.
1349 // FIXME: This is a temporary hack.
1350 // Maybe should fix the FunctionType creation instead?
Timur Iskhodzhanov4b104062013-08-26 10:32:04 +00001351 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001352 return;
1353 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001354 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001355 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001356 QualType ResultType = Proto->getResultType();
1357 if (ResultType->isVoidType())
1358 ResultType = ResultType.getUnqualifiedType();
1359 mangleType(ResultType, Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001360 }
1361
1362 // <argument-list> ::= X # void
1363 // ::= <type>+ @
1364 // ::= <type>* Z # varargs
1365 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1366 Out << 'X';
1367 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001368 // Happens for function pointer type arguments for example.
1369 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1370 ArgEnd = Proto->arg_type_end();
1371 Arg != ArgEnd; ++Arg)
1372 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001373 // <builtin-type> ::= Z # ellipsis
1374 if (Proto->isVariadic())
1375 Out << 'Z';
1376 else
1377 Out << '@';
1378 }
1379
1380 mangleThrowSpecification(Proto);
1381}
1382
1383void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001384 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1385 // # pointer. in 64-bit mode *all*
1386 // # 'this' pointers are 64-bit.
1387 // ::= <global-function>
1388 // <member-function> ::= A # private: near
1389 // ::= B # private: far
1390 // ::= C # private: static near
1391 // ::= D # private: static far
1392 // ::= E # private: virtual near
1393 // ::= F # private: virtual far
1394 // ::= G # private: thunk near
1395 // ::= H # private: thunk far
1396 // ::= I # protected: near
1397 // ::= J # protected: far
1398 // ::= K # protected: static near
1399 // ::= L # protected: static far
1400 // ::= M # protected: virtual near
1401 // ::= N # protected: virtual far
1402 // ::= O # protected: thunk near
1403 // ::= P # protected: thunk far
1404 // ::= Q # public: near
1405 // ::= R # public: far
1406 // ::= S # public: static near
1407 // ::= T # public: static far
1408 // ::= U # public: virtual near
1409 // ::= V # public: virtual far
1410 // ::= W # public: thunk near
1411 // ::= X # public: thunk far
1412 // <global-function> ::= Y # global near
1413 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001414 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1415 switch (MD->getAccess()) {
1416 default:
1417 case AS_private:
1418 if (MD->isStatic())
1419 Out << 'C';
1420 else if (MD->isVirtual())
1421 Out << 'E';
1422 else
1423 Out << 'A';
1424 break;
1425 case AS_protected:
1426 if (MD->isStatic())
1427 Out << 'K';
1428 else if (MD->isVirtual())
1429 Out << 'M';
1430 else
1431 Out << 'I';
1432 break;
1433 case AS_public:
1434 if (MD->isStatic())
1435 Out << 'S';
1436 else if (MD->isVirtual())
1437 Out << 'U';
1438 else
1439 Out << 'Q';
1440 }
1441 } else
1442 Out << 'Y';
1443}
1444void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
1445 bool IsInstMethod) {
1446 // <calling-convention> ::= A # __cdecl
1447 // ::= B # __export __cdecl
1448 // ::= C # __pascal
1449 // ::= D # __export __pascal
1450 // ::= E # __thiscall
1451 // ::= F # __export __thiscall
1452 // ::= G # __stdcall
1453 // ::= H # __export __stdcall
1454 // ::= I # __fastcall
1455 // ::= J # __export __fastcall
1456 // The 'export' calling conventions are from a bygone era
1457 // (*cough*Win16*cough*) when functions were declared for export with
1458 // that keyword. (It didn't actually export them, it just made them so
1459 // that they could be in a DLL and somebody from another module could call
1460 // them.)
1461 CallingConv CC = T->getCallConv();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001462 switch (CC) {
1463 default:
1464 llvm_unreachable("Unsupported CC for mangling");
Charles Davise8519c32013-08-30 04:39:01 +00001465 case CC_X86_64Win64:
1466 case CC_X86_64SysV:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001467 case CC_C: Out << 'A'; break;
1468 case CC_X86Pascal: Out << 'C'; break;
1469 case CC_X86ThisCall: Out << 'E'; break;
1470 case CC_X86StdCall: Out << 'G'; break;
1471 case CC_X86FastCall: Out << 'I'; break;
1472 }
1473}
1474void MicrosoftCXXNameMangler::mangleThrowSpecification(
1475 const FunctionProtoType *FT) {
1476 // <throw-spec> ::= Z # throw(...) (default)
1477 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1478 // ::= <type>+
1479 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1480 // all actually mangled as 'Z'. (They're ignored because their associated
1481 // functionality isn't implemented, and probably never will be.)
1482 Out << 'Z';
1483}
1484
1485void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1486 SourceRange Range) {
1487 // Probably should be mangled as a template instantiation; need to see what
1488 // VC does first.
1489 DiagnosticsEngine &Diags = Context.getDiags();
1490 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1491 "cannot mangle this unresolved dependent type yet");
1492 Diags.Report(Range.getBegin(), DiagID)
1493 << Range;
1494}
1495
1496// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1497// <union-type> ::= T <name>
1498// <struct-type> ::= U <name>
1499// <class-type> ::= V <name>
1500// <enum-type> ::= W <size> <name>
1501void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001502 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001503}
1504void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001505 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001506}
David Majnemer02c44f02013-08-05 22:26:46 +00001507void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1508 switch (TD->getTagKind()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001509 case TTK_Union:
1510 Out << 'T';
1511 break;
1512 case TTK_Struct:
1513 case TTK_Interface:
1514 Out << 'U';
1515 break;
1516 case TTK_Class:
1517 Out << 'V';
1518 break;
1519 case TTK_Enum:
1520 Out << 'W';
1521 Out << getASTContext().getTypeSizeInChars(
David Majnemer02c44f02013-08-05 22:26:46 +00001522 cast<EnumDecl>(TD)->getIntegerType()).getQuantity();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001523 break;
1524 }
David Majnemer02c44f02013-08-05 22:26:46 +00001525 mangleName(TD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001526}
1527
1528// <type> ::= <array-type>
1529// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1530// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001531// <element-type> # as global, E is never required
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001532// It's supposed to be the other way around, but for some strange reason, it
1533// isn't. Today this behavior is retained for the sole purpose of backwards
1534// compatibility.
David Majnemer58e4cd02013-09-11 04:44:30 +00001535void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001536 // This isn't a recursive mangling, so now we have to do it all in this
1537 // one call.
David Majnemer58e4cd02013-09-11 04:44:30 +00001538 manglePointerQualifiers(T->getElementType().getQualifiers());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001539 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001540}
1541void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1542 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001543 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001544}
1545void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1546 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001547 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001548}
1549void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1550 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001551 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001552}
1553void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1554 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001555 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001556}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001557void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001558 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001559 SmallVector<llvm::APInt, 3> Dimensions;
1560 for (;;) {
1561 if (const ConstantArrayType *CAT =
1562 getASTContext().getAsConstantArrayType(ElementTy)) {
1563 Dimensions.push_back(CAT->getSize());
1564 ElementTy = CAT->getElementType();
1565 } else if (ElementTy->isVariableArrayType()) {
1566 const VariableArrayType *VAT =
1567 getASTContext().getAsVariableArrayType(ElementTy);
1568 DiagnosticsEngine &Diags = Context.getDiags();
1569 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1570 "cannot mangle this variable-length array yet");
1571 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1572 << VAT->getBracketsRange();
1573 return;
1574 } else if (ElementTy->isDependentSizedArrayType()) {
1575 // The dependent expression has to be folded into a constant (TODO).
1576 const DependentSizedArrayType *DSAT =
1577 getASTContext().getAsDependentSizedArrayType(ElementTy);
1578 DiagnosticsEngine &Diags = Context.getDiags();
1579 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1580 "cannot mangle this dependent-length array yet");
1581 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1582 << DSAT->getBracketsRange();
1583 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001584 } else if (const IncompleteArrayType *IAT =
1585 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1586 Dimensions.push_back(llvm::APInt(32, 0));
1587 ElementTy = IAT->getElementType();
1588 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001589 else break;
1590 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001591 Out << 'Y';
1592 // <dimension-count> ::= <number> # number of extra dimensions
1593 mangleNumber(Dimensions.size());
1594 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1595 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001596 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001597}
1598
1599// <type> ::= <pointer-to-member-type>
1600// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1601// <class name> <type>
1602void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1603 SourceRange Range) {
1604 QualType PointeeType = T->getPointeeType();
1605 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1606 Out << '8';
1607 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001608 mangleFunctionType(FPT, NULL, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001609 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001610 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1611 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001612 mangleQualifiers(PointeeType.getQualifiers(), true);
1613 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001614 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001615 }
1616}
1617
1618void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1619 SourceRange Range) {
1620 DiagnosticsEngine &Diags = Context.getDiags();
1621 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1622 "cannot mangle this template type parameter type yet");
1623 Diags.Report(Range.getBegin(), DiagID)
1624 << Range;
1625}
1626
1627void MicrosoftCXXNameMangler::mangleType(
1628 const SubstTemplateTypeParmPackType *T,
1629 SourceRange Range) {
1630 DiagnosticsEngine &Diags = Context.getDiags();
1631 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1632 "cannot mangle this substituted parameter pack yet");
1633 Diags.Report(Range.getBegin(), DiagID)
1634 << Range;
1635}
1636
1637// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001638// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1639// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001640void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1641 SourceRange Range) {
1642 QualType PointeeTy = T->getPointeeType();
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001643 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1644 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001645 mangleType(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001646}
1647void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1648 SourceRange Range) {
1649 // Object pointers never have qualifiers.
1650 Out << 'A';
David Majnemer1c7a4092013-08-15 08:13:23 +00001651 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1652 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001653 mangleType(T->getPointeeType(), Range);
1654}
1655
1656// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001657// <reference-type> ::= A E? <cvr-qualifiers> <type>
1658// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001659void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1660 SourceRange Range) {
1661 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001662 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1663 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001664 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001665}
1666
1667// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001668// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1669// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001670void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1671 SourceRange Range) {
1672 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001673 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1674 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001675 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001676}
1677
1678void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1679 SourceRange Range) {
1680 DiagnosticsEngine &Diags = Context.getDiags();
1681 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1682 "cannot mangle this complex number type yet");
1683 Diags.Report(Range.getBegin(), DiagID)
1684 << Range;
1685}
1686
1687void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1688 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001689 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1690 assert(ET && "vectors with non-builtin elements are unsupported");
1691 uint64_t Width = getASTContext().getTypeSize(T);
1692 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1693 // doesn't match the Intel types uses a custom mangling below.
1694 bool IntelVector = true;
1695 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1696 Out << "T__m64";
1697 } else if (Width == 128 || Width == 256) {
1698 if (ET->getKind() == BuiltinType::Float)
1699 Out << "T__m" << Width;
1700 else if (ET->getKind() == BuiltinType::LongLong)
1701 Out << "T__m" << Width << 'i';
1702 else if (ET->getKind() == BuiltinType::Double)
1703 Out << "U__m" << Width << 'd';
1704 else
1705 IntelVector = false;
1706 } else {
1707 IntelVector = false;
1708 }
1709
1710 if (!IntelVector) {
1711 // The MS ABI doesn't have a special mangling for vector types, so we define
1712 // our own mangling to handle uses of __vector_size__ on user-specified
1713 // types, and for extensions like __v4sf.
1714 Out << "T__clang_vec" << T->getNumElements() << '_';
1715 mangleType(ET, Range);
1716 }
1717
1718 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001719}
Reid Kleckner1232e272013-03-26 16:56:59 +00001720
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001721void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1722 SourceRange Range) {
1723 DiagnosticsEngine &Diags = Context.getDiags();
1724 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1725 "cannot mangle this extended vector type yet");
1726 Diags.Report(Range.getBegin(), DiagID)
1727 << Range;
1728}
1729void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1730 SourceRange Range) {
1731 DiagnosticsEngine &Diags = Context.getDiags();
1732 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1733 "cannot mangle this dependent-sized extended vector type yet");
1734 Diags.Report(Range.getBegin(), DiagID)
1735 << Range;
1736}
1737
1738void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1739 SourceRange) {
1740 // ObjC interfaces have structs underlying them.
1741 Out << 'U';
1742 mangleName(T->getDecl());
1743}
1744
1745void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1746 SourceRange Range) {
1747 // We don't allow overloading by different protocol qualification,
1748 // so mangling them isn't necessary.
1749 mangleType(T->getBaseType(), Range);
1750}
1751
1752void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1753 SourceRange Range) {
1754 Out << "_E";
1755
1756 QualType pointee = T->getPointeeType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001757 mangleFunctionType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001758}
1759
David Majnemer360d23e2013-08-16 08:29:13 +00001760void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1761 SourceRange) {
1762 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001763}
1764
1765void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1766 SourceRange Range) {
1767 DiagnosticsEngine &Diags = Context.getDiags();
1768 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1769 "cannot mangle this template specialization type yet");
1770 Diags.Report(Range.getBegin(), DiagID)
1771 << Range;
1772}
1773
1774void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1775 SourceRange Range) {
1776 DiagnosticsEngine &Diags = Context.getDiags();
1777 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1778 "cannot mangle this dependent name type yet");
1779 Diags.Report(Range.getBegin(), DiagID)
1780 << Range;
1781}
1782
1783void MicrosoftCXXNameMangler::mangleType(
1784 const DependentTemplateSpecializationType *T,
1785 SourceRange Range) {
1786 DiagnosticsEngine &Diags = Context.getDiags();
1787 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1788 "cannot mangle this dependent template specialization type yet");
1789 Diags.Report(Range.getBegin(), DiagID)
1790 << Range;
1791}
1792
1793void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1794 SourceRange Range) {
1795 DiagnosticsEngine &Diags = Context.getDiags();
1796 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1797 "cannot mangle this pack expansion yet");
1798 Diags.Report(Range.getBegin(), DiagID)
1799 << Range;
1800}
1801
1802void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1803 SourceRange Range) {
1804 DiagnosticsEngine &Diags = Context.getDiags();
1805 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1806 "cannot mangle this typeof(type) yet");
1807 Diags.Report(Range.getBegin(), DiagID)
1808 << Range;
1809}
1810
1811void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1812 SourceRange Range) {
1813 DiagnosticsEngine &Diags = Context.getDiags();
1814 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1815 "cannot mangle this typeof(expression) yet");
1816 Diags.Report(Range.getBegin(), DiagID)
1817 << Range;
1818}
1819
1820void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1821 SourceRange Range) {
1822 DiagnosticsEngine &Diags = Context.getDiags();
1823 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1824 "cannot mangle this decltype() yet");
1825 Diags.Report(Range.getBegin(), DiagID)
1826 << Range;
1827}
1828
1829void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1830 SourceRange Range) {
1831 DiagnosticsEngine &Diags = Context.getDiags();
1832 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1833 "cannot mangle this unary transform type yet");
1834 Diags.Report(Range.getBegin(), DiagID)
1835 << Range;
1836}
1837
1838void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1839 DiagnosticsEngine &Diags = Context.getDiags();
1840 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1841 "cannot mangle this 'auto' type yet");
1842 Diags.Report(Range.getBegin(), DiagID)
1843 << Range;
1844}
1845
1846void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1847 SourceRange Range) {
1848 DiagnosticsEngine &Diags = Context.getDiags();
1849 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1850 "cannot mangle this C11 atomic type yet");
1851 Diags.Report(Range.getBegin(), DiagID)
1852 << Range;
1853}
1854
1855void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1856 raw_ostream &Out) {
1857 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1858 "Invalid mangleName() call, argument is not a variable or function!");
1859 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1860 "Invalid mangleName() call on 'structor decl!");
1861
1862 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1863 getASTContext().getSourceManager(),
1864 "Mangling declaration");
1865
1866 MicrosoftCXXNameMangler Mangler(*this, Out);
1867 return Mangler.mangle(D);
1868}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001869
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001870void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1871 const ThunkInfo &Thunk,
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001872 raw_ostream &Out) {
1873 // FIXME: this is not yet a complete implementation, but merely a
1874 // reasonably-working stub to avoid crashing when required to emit a thunk.
1875 MicrosoftCXXNameMangler Mangler(*this, Out);
1876 Out << "\01?";
1877 Mangler.mangleName(MD);
1878 if (Thunk.This.NonVirtual != 0) {
1879 // FIXME: add support for protected/private or use mangleFunctionClass.
1880 Out << "W";
1881 llvm::APSInt APSNumber(/*BitWidth=*/32 /*FIXME: check on x64*/,
1882 /*isUnsigned=*/true);
1883 APSNumber = -Thunk.This.NonVirtual;
1884 Mangler.mangleNumber(APSNumber);
1885 } else {
1886 // FIXME: add support for protected/private or use mangleFunctionClass.
1887 Out << "Q";
1888 }
1889 // FIXME: mangle return adjustment? Most likely includes using an overridee FPT?
1890 Mangler.mangleFunctionType(MD->getType()->castAs<FunctionProtoType>(), MD, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001891}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001892
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001893void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1894 CXXDtorType Type,
1895 const ThisAdjustment &,
1896 raw_ostream &) {
1897 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1898 "cannot mangle thunk for this destructor yet");
1899 getDiags().Report(DD->getLocation(), DiagID);
1900}
Reid Kleckner90633022013-06-19 15:20:38 +00001901
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001902void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1903 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001904 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1905 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001906 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001907 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001908 MicrosoftCXXNameMangler Mangler(*this, Out);
1909 Mangler.getStream() << "\01??_7";
1910 Mangler.mangleName(RD);
Reid Kleckner90633022013-06-19 15:20:38 +00001911 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001912 // TODO: If the class has more than one vtable, mangle in the class it came
1913 // from.
1914 Mangler.getStream() << '@';
1915}
Reid Kleckner90633022013-06-19 15:20:38 +00001916
1917void MicrosoftMangleContext::mangleCXXVBTable(
1918 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1919 raw_ostream &Out) {
1920 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1921 // <cvr-qualifiers> [<name>] @
1922 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1923 // is always '7' for vbtables.
1924 MicrosoftCXXNameMangler Mangler(*this, Out);
1925 Mangler.getStream() << "\01??_8";
1926 Mangler.mangleName(Derived);
1927 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1928 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1929 E = BasePath.end();
1930 I != E; ++I) {
1931 Mangler.mangleName(*I);
1932 }
1933 Mangler.getStream() << '@';
1934}
1935
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001936void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1937 raw_ostream &) {
1938 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1939}
1940void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1941 int64_t Offset,
1942 const CXXRecordDecl *Type,
1943 raw_ostream &) {
1944 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1945}
1946void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1947 raw_ostream &) {
1948 // FIXME: Give a location...
1949 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1950 "cannot mangle RTTI descriptors for type %0 yet");
1951 getDiags().Report(DiagID)
1952 << T.getBaseTypeIdentifier();
1953}
1954void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1955 raw_ostream &) {
1956 // FIXME: Give a location...
1957 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1958 "cannot mangle the name of type %0 into RTTI descriptors yet");
1959 getDiags().Report(DiagID)
1960 << T.getBaseTypeIdentifier();
1961}
1962void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1963 CXXCtorType Type,
1964 raw_ostream & Out) {
1965 MicrosoftCXXNameMangler mangler(*this, Out);
1966 mangler.mangle(D);
1967}
1968void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1969 CXXDtorType Type,
1970 raw_ostream & Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001971 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001972 mangler.mangle(D);
1973}
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001974void MicrosoftMangleContext::mangleReferenceTemporary(const VarDecl *VD,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001975 raw_ostream &) {
1976 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1977 "cannot mangle this reference temporary yet");
1978 getDiags().Report(VD->getLocation(), DiagID);
1979}
1980
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001981void MicrosoftMangleContext::mangleStaticGuardVariable(const VarDecl *VD,
1982 raw_ostream &Out) {
1983 // <guard-name> ::= ?_B <postfix> @51
1984 // ::= ?$S <guard-num> @ <postfix> @4IA
1985
1986 // The first mangling is what MSVC uses to guard static locals in inline
1987 // functions. It uses a different mangling in external functions to support
1988 // guarding more than 32 variables. MSVC rejects inline functions with more
1989 // than 32 static locals. We don't fully implement the second mangling
1990 // because those guards are not externally visible, and instead use LLVM's
1991 // default renaming when creating a new guard variable.
1992 MicrosoftCXXNameMangler Mangler(*this, Out);
1993
1994 bool Visible = VD->isExternallyVisible();
1995 // <operator-name> ::= ?_B # local static guard
1996 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@");
1997 Mangler.manglePostfix(VD->getDeclContext());
1998 Mangler.getStream() << (Visible ? "@51" : "@4IA");
1999}
2000
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00002001void MicrosoftMangleContext::mangleInitFiniStub(const VarDecl *D,
2002 raw_ostream &Out,
2003 char CharCode) {
2004 MicrosoftCXXNameMangler Mangler(*this, Out);
2005 Mangler.getStream() << "\01??__" << CharCode;
2006 Mangler.mangleName(D);
2007 // This is the function class mangling. These stubs are global, non-variadic,
2008 // cdecl functions that return void and take no args.
2009 Mangler.getStream() << "YAXXZ";
2010}
2011
2012void MicrosoftMangleContext::mangleDynamicInitializer(const VarDecl *D,
2013 raw_ostream &Out) {
2014 // <initializer-name> ::= ?__E <name> YAXXZ
2015 mangleInitFiniStub(D, Out, 'E');
2016}
2017
Reid Kleckner942f9fe2013-09-10 20:14:30 +00002018void MicrosoftMangleContext::mangleDynamicAtExitDestructor(const VarDecl *D,
2019 raw_ostream &Out) {
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00002020 // <destructor-name> ::= ?__F <name> YAXXZ
2021 mangleInitFiniStub(D, Out, 'F');
Reid Kleckner942f9fe2013-09-10 20:14:30 +00002022}
2023
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002024MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
2025 DiagnosticsEngine &Diags) {
2026 return new MicrosoftMangleContext(Context, Diags);
2027}