blob: e082759cb92e9cf498375049af9e8aa06a806032 [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
233 // "main" is not mangled.
234 if (FD->isMain())
235 return false;
236
237 // C++ functions and those whose names are not a simple identifier need
238 // mangling.
239 if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage)
240 return true;
241
242 // C functions are not mangled.
243 if (L == CLanguageLinkage)
244 return false;
245 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000246
247 // Otherwise, no mangling is done outside C++ mode.
248 if (!getASTContext().getLangOpts().CPlusPlus)
249 return false;
250
David Majnemercab7dad2013-09-13 09:03:14 +0000251 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
252 // C variables are not mangled.
253 if (VD->isExternC())
254 return false;
255
256 // Variables at global scope with non-internal linkage are not mangled.
257 const DeclContext *DC = getEffectiveDeclContext(D);
258 // Check for extern variable declared locally.
259 if (DC->isFunctionOrMethod() && D->hasLinkage())
260 while (!DC->isNamespace() && !DC->isTranslationUnit())
261 DC = getEffectiveParentContext(DC);
262
263 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage &&
264 !isa<VarTemplateSpecializationDecl>(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000265 return false;
266 }
267
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000268 return true;
269}
270
271void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
272 StringRef Prefix) {
273 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
274 // Therefore it's really important that we don't decorate the
275 // name with leading underscores or leading/trailing at signs. So, by
276 // default, we emit an asm marker at the start so we get the name right.
277 // Callers can override this with a custom prefix.
278
279 // Any decl can be declared with __asm("foo") on it, and this takes precedence
280 // over all other naming in the .o file.
281 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
282 // If we have an asm name, then we use it as the mangling.
283 Out << '\01' << ALA->getLabel();
284 return;
285 }
286
287 // <mangled-name> ::= ? <name> <type-encoding>
288 Out << Prefix;
289 mangleName(D);
290 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
291 mangleFunctionEncoding(FD);
292 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
293 mangleVariableEncoding(VD);
294 else {
295 // TODO: Fields? Can MSVC even mangle them?
296 // Issue a diagnostic for now.
297 DiagnosticsEngine &Diags = Context.getDiags();
298 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
299 "cannot mangle this declaration yet");
300 Diags.Report(D->getLocation(), DiagID)
301 << D->getSourceRange();
302 }
303}
304
305void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
306 // <type-encoding> ::= <function-class> <function-type>
307
Reid Klecknerf21818d2013-06-24 19:21:52 +0000308 // Since MSVC operates on the type as written and not the canonical type, it
309 // actually matters which decl we have here. MSVC appears to choose the
310 // first, since it is most likely to be the declaration in a header file.
311 FD = FD->getFirstDeclaration();
312
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000313 // We should never ever see a FunctionNoProtoType at this point.
314 // We don't even know how to mangle their types anyway :).
Reid Klecknerf21818d2013-06-24 19:21:52 +0000315 TypeSourceInfo *TSI = FD->getTypeSourceInfo();
316 QualType T = TSI ? TSI->getType() : FD->getType();
317 const FunctionProtoType *FT = T->castAs<FunctionProtoType>();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000318
319 bool InStructor = false, InInstMethod = false;
320 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
321 if (MD) {
322 if (MD->isInstance())
323 InInstMethod = true;
324 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
325 InStructor = true;
326 }
327
David Majnemercab7dad2013-09-13 09:03:14 +0000328 // extern "C" functions can hold entities that must be mangled.
329 // As it stands, these functions still need to get expressed in the full
330 // external name. They have their class and type omitted, replaced with '9'.
331 if (Context.shouldMangleDeclName(FD)) {
332 // First, the function class.
333 mangleFunctionClass(FD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000334
David Majnemercab7dad2013-09-13 09:03:14 +0000335 mangleFunctionType(FT, FD, InStructor, InInstMethod);
336 } else
337 Out << '9';
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000338}
339
340void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
341 // <type-encoding> ::= <storage-class> <variable-type>
342 // <storage-class> ::= 0 # private static member
343 // ::= 1 # protected static member
344 // ::= 2 # public static member
345 // ::= 3 # global
346 // ::= 4 # static local
347
348 // The first character in the encoding (after the name) is the storage class.
349 if (VD->isStaticDataMember()) {
350 // If it's a static member, it also encodes the access level.
351 switch (VD->getAccess()) {
352 default:
353 case AS_private: Out << '0'; break;
354 case AS_protected: Out << '1'; break;
355 case AS_public: Out << '2'; break;
356 }
357 }
358 else if (!VD->isStaticLocal())
359 Out << '3';
360 else
361 Out << '4';
362 // Now mangle the type.
363 // <variable-type> ::= <type> <cvr-qualifiers>
364 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
365 // Pointers and references are odd. The type of 'int * const foo;' gets
366 // mangled as 'QAHA' instead of 'PAHB', for example.
367 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
368 QualType Ty = TL.getType();
David Majnemer1c7a4092013-08-15 08:13:23 +0000369 if (Ty->isPointerType() || Ty->isReferenceType() ||
370 Ty->isMemberPointerType()) {
David Majnemer17ffbd02013-08-09 05:56:24 +0000371 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000372 if (PointersAre64Bit)
373 Out << 'E';
374 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) {
375 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true);
376 // Member pointers are suffixed with a back reference to the member
377 // pointer's class name.
378 mangleName(MPT->getClass()->getAsCXXRecordDecl());
379 } else
380 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
David Majnemer17ffbd02013-08-09 05:56:24 +0000381 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000382 // Global arrays are funny, too.
David Majnemer58e4cd02013-09-11 04:44:30 +0000383 mangleDecayedArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000384 if (AT->getElementType()->isArrayType())
385 Out << 'A';
386 else
387 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000388 } else {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000389 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000390 mangleQualifiers(Ty.getLocalQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000391 }
392}
393
394void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
395 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
396 const DeclContext *DC = ND->getDeclContext();
397
398 // Always start with the unqualified name.
399 mangleUnqualifiedName(ND);
400
401 // If this is an extern variable declared locally, the relevant DeclContext
402 // is that of the containing namespace, or the translation unit.
403 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
404 while (!DC->isNamespace() && !DC->isTranslationUnit())
405 DC = DC->getParent();
406
407 manglePostfix(DC);
408
409 // Terminate the whole name with an '@'.
410 Out << '@';
411}
412
413void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
414 llvm::APSInt APSNumber(/*BitWidth=*/64, /*isUnsigned=*/false);
415 APSNumber = Number;
416 mangleNumber(APSNumber);
417}
418
419void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
420 // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
421 // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
422 // ::= [?] @ # 0 (alternate mangling, not emitted by VC)
423 if (Value.isSigned() && Value.isNegative()) {
424 Out << '?';
425 mangleNumber(llvm::APSInt(Value.abs()));
426 return;
427 }
428 llvm::APSInt Temp(Value);
429 // There's a special shorter mangling for 0, but Microsoft
430 // chose not to use it. Instead, 0 gets mangled as "A@". Oh well...
431 if (Value.uge(1) && Value.ule(10)) {
432 --Temp;
433 Temp.print(Out, false);
434 } else {
435 // We have to build up the encoding in reverse order, so it will come
436 // out right when we write it out.
437 char Encoding[64];
438 char *EndPtr = Encoding+sizeof(Encoding);
439 char *CurPtr = EndPtr;
440 llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned());
441 NibbleMask = 0xf;
442 do {
443 *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf);
444 Temp = Temp.lshr(4);
445 } while (Temp != 0);
446 Out.write(CurPtr, EndPtr-CurPtr);
447 Out << '@';
448 }
449}
450
451static const TemplateDecl *
Reid Klecknerf16216c2013-03-20 01:40:23 +0000452isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000453 // Check if we have a function template.
454 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
455 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000456 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000457 return TD;
458 }
459 }
460
461 // Check if we have a class template.
462 if (const ClassTemplateSpecializationDecl *Spec =
Reid Klecknerf16216c2013-03-20 01:40:23 +0000463 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
464 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000465 return Spec->getSpecializedTemplate();
466 }
467
468 return 0;
469}
470
471void
472MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
473 DeclarationName Name) {
474 // <unqualified-name> ::= <operator-name>
475 // ::= <ctor-dtor-name>
476 // ::= <source-name>
477 // ::= <template-name>
Reid Klecknerf16216c2013-03-20 01:40:23 +0000478
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000479 // Check if we have a template.
Reid Klecknerf16216c2013-03-20 01:40:23 +0000480 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000481 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Kleckner3be37d12013-07-13 00:43:39 +0000482 // Function templates aren't considered for name back referencing. This
483 // makes sense since function templates aren't likely to occur multiple
484 // times in a symbol.
485 // FIXME: Test alias template mangling with MSVC 2013.
486 if (!isa<ClassTemplateDecl>(TD)) {
487 mangleTemplateInstantiationName(TD, *TemplateArgs);
488 return;
489 }
490
491 // We have a class template.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000492 // Here comes the tricky thing: if we need to mangle something like
493 // void foo(A::X<Y>, B::X<Y>),
494 // the X<Y> part is aliased. However, if you need to mangle
495 // void foo(A::X<A::Y>, A::X<B::Y>),
496 // the A::X<> part is not aliased.
497 // That said, from the mangler's perspective we have a structure like this:
498 // namespace[s] -> type[ -> template-parameters]
499 // but from the Clang perspective we have
500 // type [ -> template-parameters]
501 // \-> namespace[s]
502 // What we do is we create a new mangler, mangle the same type (without
503 // a namespace suffix) using the extra mangler with back references
504 // disabled (to avoid infinite recursion) and then use the mangled type
505 // name as a key to check the mangling of different types for aliasing.
506
507 std::string BackReferenceKey;
508 BackRefMap::iterator Found;
509 if (UseNameBackReferences) {
510 llvm::raw_string_ostream Stream(BackReferenceKey);
511 MicrosoftCXXNameMangler Extra(Context, Stream);
512 Extra.disableBackReferences();
513 Extra.mangleUnqualifiedName(ND, Name);
514 Stream.flush();
515
516 Found = NameBackReferences.find(BackReferenceKey);
517 }
518 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000519 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000520 if (UseNameBackReferences && NameBackReferences.size() < 10) {
521 size_t Size = NameBackReferences.size();
522 NameBackReferences[BackReferenceKey] = Size;
523 }
524 } else {
525 Out << Found->second;
526 }
527 return;
528 }
529
530 switch (Name.getNameKind()) {
531 case DeclarationName::Identifier: {
532 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
533 mangleSourceName(II);
534 break;
535 }
536
537 // Otherwise, an anonymous entity. We must have a declaration.
538 assert(ND && "mangling empty name without declaration");
539
540 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
541 if (NS->isAnonymousNamespace()) {
542 Out << "?A@";
543 break;
544 }
545 }
546
547 // We must have an anonymous struct.
548 const TagDecl *TD = cast<TagDecl>(ND);
549 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
550 assert(TD->getDeclContext() == D->getDeclContext() &&
551 "Typedef should not be in another decl context!");
552 assert(D->getDeclName().getAsIdentifierInfo() &&
553 "Typedef was not named!");
554 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
555 break;
556 }
557
558 // When VC encounters an anonymous type with no tag and no typedef,
David Majnemerec0258a2013-08-26 02:35:51 +0000559 // it literally emits '<unnamed-tag>@'.
560 Out << "<unnamed-tag>@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000561 break;
562 }
563
564 case DeclarationName::ObjCZeroArgSelector:
565 case DeclarationName::ObjCOneArgSelector:
566 case DeclarationName::ObjCMultiArgSelector:
567 llvm_unreachable("Can't mangle Objective-C selector names here!");
568
569 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000570 if (ND == Structor) {
571 assert(StructorType == Ctor_Complete &&
572 "Should never be asked to mangle a ctor other than complete");
573 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000574 Out << "?0";
575 break;
576
577 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000578 if (ND == Structor)
579 // If the named decl is the C++ destructor we're mangling,
580 // use the type we were given.
581 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
582 else
Reid Klecknera4130ba2013-07-22 13:51:44 +0000583 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000584 // class with a destructor is declared within a destructor.
Reid Klecknera4130ba2013-07-22 13:51:44 +0000585 mangleCXXDtorType(Dtor_Base);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000586 break;
587
588 case DeclarationName::CXXConversionFunctionName:
589 // <operator-name> ::= ?B # (cast)
590 // The target type is encoded as the return type.
591 Out << "?B";
592 break;
593
594 case DeclarationName::CXXOperatorName:
595 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
596 break;
597
598 case DeclarationName::CXXLiteralOperatorName: {
599 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
600 DiagnosticsEngine Diags = Context.getDiags();
601 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
602 "cannot mangle this literal operator yet");
603 Diags.Report(ND->getLocation(), DiagID);
604 break;
605 }
606
607 case DeclarationName::CXXUsingDirective:
608 llvm_unreachable("Can't mangle a using directive name!");
609 }
610}
611
612void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
613 bool NoFunction) {
614 // <postfix> ::= <unqualified-name> [<postfix>]
615 // ::= <substitution> [<postfix>]
616
617 if (!DC) return;
618
619 while (isa<LinkageSpecDecl>(DC))
620 DC = DC->getParent();
621
622 if (DC->isTranslationUnit())
623 return;
624
625 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedmane5798892013-07-10 01:13:27 +0000626 DiagnosticsEngine Diags = Context.getDiags();
627 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
628 "cannot mangle a local inside this block yet");
629 Diags.Report(BD->getLocation(), DiagID);
630
631 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
632 // for how this should be done.
633 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000634 Out << '@';
635 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000636 } else if (isa<CapturedDecl>(DC)) {
637 // Skip CapturedDecl context.
638 manglePostfix(DC->getParent(), NoFunction);
639 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000640 }
641
642 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
643 return;
644 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
645 mangleObjCMethodName(Method);
646 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
647 mangleLocalName(Func);
648 else {
649 mangleUnqualifiedName(cast<NamedDecl>(DC));
650 manglePostfix(DC->getParent(), NoFunction);
651 }
652}
653
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000654void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000655 // Microsoft uses the names on the case labels for these dtor variants. Clang
656 // uses the Itanium terminology internally. Everything in this ABI delegates
657 // towards the base dtor.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000658 switch (T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000659 // <operator-name> ::= ?1 # destructor
660 case Dtor_Base: Out << "?1"; return;
661 // <operator-name> ::= ?_D # vbase destructor
662 case Dtor_Complete: Out << "?_D"; return;
663 // <operator-name> ::= ?_G # scalar deleting destructor
664 case Dtor_Deleting: Out << "?_G"; return;
665 // <operator-name> ::= ?_E # vector deleting destructor
666 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
667 // it.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000668 }
669 llvm_unreachable("Unsupported dtor type?");
670}
671
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000672void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
673 SourceLocation Loc) {
674 switch (OO) {
675 // ?0 # constructor
676 // ?1 # destructor
677 // <operator-name> ::= ?2 # new
678 case OO_New: Out << "?2"; break;
679 // <operator-name> ::= ?3 # delete
680 case OO_Delete: Out << "?3"; break;
681 // <operator-name> ::= ?4 # =
682 case OO_Equal: Out << "?4"; break;
683 // <operator-name> ::= ?5 # >>
684 case OO_GreaterGreater: Out << "?5"; break;
685 // <operator-name> ::= ?6 # <<
686 case OO_LessLess: Out << "?6"; break;
687 // <operator-name> ::= ?7 # !
688 case OO_Exclaim: Out << "?7"; break;
689 // <operator-name> ::= ?8 # ==
690 case OO_EqualEqual: Out << "?8"; break;
691 // <operator-name> ::= ?9 # !=
692 case OO_ExclaimEqual: Out << "?9"; break;
693 // <operator-name> ::= ?A # []
694 case OO_Subscript: Out << "?A"; break;
695 // ?B # conversion
696 // <operator-name> ::= ?C # ->
697 case OO_Arrow: Out << "?C"; break;
698 // <operator-name> ::= ?D # *
699 case OO_Star: Out << "?D"; break;
700 // <operator-name> ::= ?E # ++
701 case OO_PlusPlus: Out << "?E"; break;
702 // <operator-name> ::= ?F # --
703 case OO_MinusMinus: Out << "?F"; break;
704 // <operator-name> ::= ?G # -
705 case OO_Minus: Out << "?G"; break;
706 // <operator-name> ::= ?H # +
707 case OO_Plus: Out << "?H"; break;
708 // <operator-name> ::= ?I # &
709 case OO_Amp: Out << "?I"; break;
710 // <operator-name> ::= ?J # ->*
711 case OO_ArrowStar: Out << "?J"; break;
712 // <operator-name> ::= ?K # /
713 case OO_Slash: Out << "?K"; break;
714 // <operator-name> ::= ?L # %
715 case OO_Percent: Out << "?L"; break;
716 // <operator-name> ::= ?M # <
717 case OO_Less: Out << "?M"; break;
718 // <operator-name> ::= ?N # <=
719 case OO_LessEqual: Out << "?N"; break;
720 // <operator-name> ::= ?O # >
721 case OO_Greater: Out << "?O"; break;
722 // <operator-name> ::= ?P # >=
723 case OO_GreaterEqual: Out << "?P"; break;
724 // <operator-name> ::= ?Q # ,
725 case OO_Comma: Out << "?Q"; break;
726 // <operator-name> ::= ?R # ()
727 case OO_Call: Out << "?R"; break;
728 // <operator-name> ::= ?S # ~
729 case OO_Tilde: Out << "?S"; break;
730 // <operator-name> ::= ?T # ^
731 case OO_Caret: Out << "?T"; break;
732 // <operator-name> ::= ?U # |
733 case OO_Pipe: Out << "?U"; break;
734 // <operator-name> ::= ?V # &&
735 case OO_AmpAmp: Out << "?V"; break;
736 // <operator-name> ::= ?W # ||
737 case OO_PipePipe: Out << "?W"; break;
738 // <operator-name> ::= ?X # *=
739 case OO_StarEqual: Out << "?X"; break;
740 // <operator-name> ::= ?Y # +=
741 case OO_PlusEqual: Out << "?Y"; break;
742 // <operator-name> ::= ?Z # -=
743 case OO_MinusEqual: Out << "?Z"; break;
744 // <operator-name> ::= ?_0 # /=
745 case OO_SlashEqual: Out << "?_0"; break;
746 // <operator-name> ::= ?_1 # %=
747 case OO_PercentEqual: Out << "?_1"; break;
748 // <operator-name> ::= ?_2 # >>=
749 case OO_GreaterGreaterEqual: Out << "?_2"; break;
750 // <operator-name> ::= ?_3 # <<=
751 case OO_LessLessEqual: Out << "?_3"; break;
752 // <operator-name> ::= ?_4 # &=
753 case OO_AmpEqual: Out << "?_4"; break;
754 // <operator-name> ::= ?_5 # |=
755 case OO_PipeEqual: Out << "?_5"; break;
756 // <operator-name> ::= ?_6 # ^=
757 case OO_CaretEqual: Out << "?_6"; break;
758 // ?_7 # vftable
759 // ?_8 # vbtable
760 // ?_9 # vcall
761 // ?_A # typeof
762 // ?_B # local static guard
763 // ?_C # string
764 // ?_D # vbase destructor
765 // ?_E # vector deleting destructor
766 // ?_F # default constructor closure
767 // ?_G # scalar deleting destructor
768 // ?_H # vector constructor iterator
769 // ?_I # vector destructor iterator
770 // ?_J # vector vbase constructor iterator
771 // ?_K # virtual displacement map
772 // ?_L # eh vector constructor iterator
773 // ?_M # eh vector destructor iterator
774 // ?_N # eh vector vbase constructor iterator
775 // ?_O # copy constructor closure
776 // ?_P<name> # udt returning <name>
777 // ?_Q # <unknown>
778 // ?_R0 # RTTI Type Descriptor
779 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
780 // ?_R2 # RTTI Base Class Array
781 // ?_R3 # RTTI Class Hierarchy Descriptor
782 // ?_R4 # RTTI Complete Object Locator
783 // ?_S # local vftable
784 // ?_T # local vftable constructor closure
785 // <operator-name> ::= ?_U # new[]
786 case OO_Array_New: Out << "?_U"; break;
787 // <operator-name> ::= ?_V # delete[]
788 case OO_Array_Delete: Out << "?_V"; break;
789
790 case OO_Conditional: {
791 DiagnosticsEngine &Diags = Context.getDiags();
792 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
793 "cannot mangle this conditional operator yet");
794 Diags.Report(Loc, DiagID);
795 break;
796 }
797
798 case OO_None:
799 case NUM_OVERLOADED_OPERATORS:
800 llvm_unreachable("Not an overloaded operator");
801 }
802}
803
804void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
805 // <source name> ::= <identifier> @
806 std::string key = II->getNameStart();
807 BackRefMap::iterator Found;
808 if (UseNameBackReferences)
809 Found = NameBackReferences.find(key);
810 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
811 Out << II->getName() << '@';
812 if (UseNameBackReferences && NameBackReferences.size() < 10) {
813 size_t Size = NameBackReferences.size();
814 NameBackReferences[key] = Size;
815 }
816 } else {
817 Out << Found->second;
818 }
819}
820
821void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
822 Context.mangleObjCMethodName(MD, Out);
823}
824
825// Find out how many function decls live above this one and return an integer
826// suitable for use as the number in a numbered anonymous scope.
827// TODO: Memoize.
828static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
829 const DeclContext *DC = FD->getParent();
830 int level = 1;
831
832 while (DC && !DC->isTranslationUnit()) {
833 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
834 DC = DC->getParent();
835 }
836
837 return 2*level;
838}
839
840void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
841 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
842 // <numbered-anonymous-scope> ::= ? <number>
843 // Even though the name is rendered in reverse order (e.g.
844 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
845 // innermost. So a method bar in class C local to function foo gets mangled
846 // as something like:
847 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
848 // This is more apparent when you have a type nested inside a method of a
849 // type nested inside a function. A method baz in class D local to method
850 // bar of class C local to function foo gets mangled as:
851 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
852 // This scheme is general enough to support GCC-style nested
853 // functions. You could have a method baz of class C inside a function bar
854 // inside a function foo, like so:
855 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
856 int NestLevel = getLocalNestingLevel(FD);
857 Out << '?';
858 mangleNumber(NestLevel);
859 Out << '?';
860 mangle(FD, "?");
861}
862
863void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
864 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000865 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000866 // <template-name> ::= <unscoped-template-name> <template-args>
867 // ::= <substitution>
868 // Always start with the unqualified name.
869
870 // Templates have their own context for back references.
871 ArgBackRefMap OuterArgsContext;
872 BackRefMap OuterTemplateContext;
873 NameBackReferences.swap(OuterTemplateContext);
874 TypeBackReferences.swap(OuterArgsContext);
875
876 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000877 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000878
879 // Restore the previous back reference contexts.
880 NameBackReferences.swap(OuterTemplateContext);
881 TypeBackReferences.swap(OuterArgsContext);
882}
883
884void
885MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
886 // <unscoped-template-name> ::= ?$ <unqualified-name>
887 Out << "?$";
888 mangleUnqualifiedName(TD);
889}
890
891void
892MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
893 bool IsBoolean) {
894 // <integer-literal> ::= $0 <number>
895 Out << "$0";
896 // Make sure booleans are encoded as 0/1.
897 if (IsBoolean && Value.getBoolValue())
898 mangleNumber(1);
899 else
900 mangleNumber(Value);
901}
902
903void
904MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
905 // See if this is a constant expression.
906 llvm::APSInt Value;
907 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
908 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
909 return;
910 }
911
David Majnemerc80eb462013-08-13 06:32:20 +0000912 const CXXUuidofExpr *UE = 0;
913 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
914 if (UO->getOpcode() == UO_AddrOf)
915 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
916 } else
917 UE = dyn_cast<CXXUuidofExpr>(E);
918
919 if (UE) {
920 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
921 // const __s_GUID _GUID_{lower case UUID with underscores}
922 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
923 std::string Name = "_GUID_" + Uuid.lower();
924 std::replace(Name.begin(), Name.end(), '-', '_');
925
David Majnemer26314e12013-08-13 09:17:25 +0000926 // If we had to peek through an address-of operator, treat this like we are
David Majnemerc80eb462013-08-13 06:32:20 +0000927 // dealing with a pointer type. Otherwise, treat it like a const reference.
928 //
929 // N.B. This matches up with the handling of TemplateArgument::Declaration
930 // in mangleTemplateArg
931 if (UE == E)
932 Out << "$E?";
933 else
934 Out << "$1?";
935 Out << Name << "@@3U__s_GUID@@B";
936 return;
937 }
938
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000939 // As bad as this diagnostic is, it's better than crashing.
940 DiagnosticsEngine &Diags = Context.getDiags();
941 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
942 "cannot yet mangle expression type %0");
943 Diags.Report(E->getExprLoc(), DiagID)
944 << E->getStmtClassName() << E->getSourceRange();
945}
946
947void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000948MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
949 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000950 // <template-args> ::= {<type> | <integer-literal>}+ @
951 unsigned NumTemplateArgs = TemplateArgs.size();
952 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000953 const TemplateArgument &TA = TemplateArgs[i];
David Majnemer309f6452013-08-27 08:21:25 +0000954 mangleTemplateArg(TD, TA);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000955 }
956 Out << '@';
957}
958
Reid Kleckner5d90d182013-07-02 18:10:07 +0000959void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
David Majnemer309f6452013-08-27 08:21:25 +0000960 const TemplateArgument &TA) {
Reid Kleckner5d90d182013-07-02 18:10:07 +0000961 switch (TA.getKind()) {
962 case TemplateArgument::Null:
963 llvm_unreachable("Can't mangle null template arguments!");
David Majnemer309f6452013-08-27 08:21:25 +0000964 case TemplateArgument::TemplateExpansion:
965 llvm_unreachable("Can't mangle template expansion arguments!");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000966 case TemplateArgument::Type: {
967 QualType T = TA.getAsType();
968 mangleType(T, SourceRange(), QMM_Escape);
969 break;
970 }
David Majnemerf2081f62013-08-13 01:25:35 +0000971 case TemplateArgument::Declaration: {
972 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl());
973 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000974 break;
David Majnemerf2081f62013-08-13 01:25:35 +0000975 }
Reid Kleckner5d90d182013-07-02 18:10:07 +0000976 case TemplateArgument::Integral:
977 mangleIntegerLiteral(TA.getAsIntegral(),
978 TA.getIntegralType()->isBooleanType());
979 break;
David Majnemer7802fc92013-08-05 21:33:59 +0000980 case TemplateArgument::NullPtr:
981 Out << "$0A@";
982 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000983 case TemplateArgument::Expression:
984 mangleExpression(TA.getAsExpr());
985 break;
986 case TemplateArgument::Pack:
987 // Unlike Itanium, there is no character code to indicate an argument pack.
Reid Kleckner5d90d182013-07-02 18:10:07 +0000988 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
989 I != E; ++I)
David Majnemer309f6452013-08-27 08:21:25 +0000990 mangleTemplateArg(TD, *I);
Reid Kleckner5d90d182013-07-02 18:10:07 +0000991 break;
992 case TemplateArgument::Template:
David Majnemer02c44f02013-08-05 22:26:46 +0000993 mangleType(cast<TagDecl>(
994 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
995 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000996 }
997}
998
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000999void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
1000 bool IsMember) {
1001 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
1002 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
1003 // 'I' means __restrict (32/64-bit).
1004 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
1005 // keyword!
1006 // <base-cvr-qualifiers> ::= A # near
1007 // ::= B # near const
1008 // ::= C # near volatile
1009 // ::= D # near const volatile
1010 // ::= E # far (16-bit)
1011 // ::= F # far const (16-bit)
1012 // ::= G # far volatile (16-bit)
1013 // ::= H # far const volatile (16-bit)
1014 // ::= I # huge (16-bit)
1015 // ::= J # huge const (16-bit)
1016 // ::= K # huge volatile (16-bit)
1017 // ::= L # huge const volatile (16-bit)
1018 // ::= M <basis> # based
1019 // ::= N <basis> # based const
1020 // ::= O <basis> # based volatile
1021 // ::= P <basis> # based const volatile
1022 // ::= Q # near member
1023 // ::= R # near const member
1024 // ::= S # near volatile member
1025 // ::= T # near const volatile member
1026 // ::= U # far member (16-bit)
1027 // ::= V # far const member (16-bit)
1028 // ::= W # far volatile member (16-bit)
1029 // ::= X # far const volatile member (16-bit)
1030 // ::= Y # huge member (16-bit)
1031 // ::= Z # huge const member (16-bit)
1032 // ::= 0 # huge volatile member (16-bit)
1033 // ::= 1 # huge const volatile member (16-bit)
1034 // ::= 2 <basis> # based member
1035 // ::= 3 <basis> # based const member
1036 // ::= 4 <basis> # based volatile member
1037 // ::= 5 <basis> # based const volatile member
1038 // ::= 6 # near function (pointers only)
1039 // ::= 7 # far function (pointers only)
1040 // ::= 8 # near method (pointers only)
1041 // ::= 9 # far method (pointers only)
1042 // ::= _A <basis> # based function (pointers only)
1043 // ::= _B <basis> # based function (far?) (pointers only)
1044 // ::= _C <basis> # based method (pointers only)
1045 // ::= _D <basis> # based method (far?) (pointers only)
1046 // ::= _E # block (Clang)
1047 // <basis> ::= 0 # __based(void)
1048 // ::= 1 # __based(segment)?
1049 // ::= 2 <name> # __based(name)
1050 // ::= 3 # ?
1051 // ::= 4 # ?
1052 // ::= 5 # not really based
1053 bool HasConst = Quals.hasConst(),
1054 HasVolatile = Quals.hasVolatile();
David Majnemerc0e64f32013-08-05 22:43:06 +00001055
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001056 if (!IsMember) {
1057 if (HasConst && HasVolatile) {
1058 Out << 'D';
1059 } else if (HasVolatile) {
1060 Out << 'C';
1061 } else if (HasConst) {
1062 Out << 'B';
1063 } else {
1064 Out << 'A';
1065 }
1066 } else {
1067 if (HasConst && HasVolatile) {
1068 Out << 'T';
1069 } else if (HasVolatile) {
1070 Out << 'S';
1071 } else if (HasConst) {
1072 Out << 'R';
1073 } else {
1074 Out << 'Q';
1075 }
1076 }
1077
1078 // FIXME: For now, just drop all extension qualifiers on the floor.
1079}
1080
1081void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1082 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1083 // ::= Q # const
1084 // ::= R # volatile
1085 // ::= S # const volatile
1086 bool HasConst = Quals.hasConst(),
1087 HasVolatile = Quals.hasVolatile();
1088 if (HasConst && HasVolatile) {
1089 Out << 'S';
1090 } else if (HasVolatile) {
1091 Out << 'R';
1092 } else if (HasConst) {
1093 Out << 'Q';
1094 } else {
1095 Out << 'P';
1096 }
1097}
1098
1099void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1100 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001101 // MSVC will backreference two canonically equivalent types that have slightly
1102 // different manglings when mangled alone.
David Majnemer58e4cd02013-09-11 04:44:30 +00001103
1104 // Decayed types do not match up with non-decayed versions of the same type.
1105 //
1106 // e.g.
1107 // void (*x)(void) will not form a backreference with void x(void)
1108 void *TypePtr;
1109 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1110 TypePtr = DT->getOriginalType().getCanonicalType().getAsOpaquePtr();
1111 // If the original parameter was textually written as an array,
1112 // instead treat the decayed parameter like it's const.
1113 //
1114 // e.g.
1115 // int [] -> int * const
1116 if (DT->getOriginalType()->isArrayType())
1117 T = T.withConst();
1118 } else
1119 TypePtr = T.getCanonicalType().getAsOpaquePtr();
1120
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001121 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1122
1123 if (Found == TypeBackReferences.end()) {
1124 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1125
David Majnemer58e4cd02013-09-11 04:44:30 +00001126 mangleType(T, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001127
1128 // See if it's worth creating a back reference.
1129 // Only types longer than 1 character are considered
1130 // and only 10 back references slots are available:
1131 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1132 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1133 size_t Size = TypeBackReferences.size();
1134 TypeBackReferences[TypePtr] = Size;
1135 }
1136 } else {
1137 Out << Found->second;
1138 }
1139}
1140
1141void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001142 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001143 // Don't use the canonical types. MSVC includes things like 'const' on
1144 // pointer arguments to function pointers that canonicalization strips away.
1145 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001146 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001147 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1148 // If there were any Quals, getAsArrayType() pushed them onto the array
1149 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001150 if (QMM == QMM_Mangle)
1151 Out << 'A';
1152 else if (QMM == QMM_Escape || QMM == QMM_Result)
1153 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001154 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001155 return;
1156 }
1157
1158 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1159 T->isBlockPointerType();
1160
1161 switch (QMM) {
1162 case QMM_Drop:
1163 break;
1164 case QMM_Mangle:
1165 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1166 Out << '6';
1167 mangleFunctionType(FT, 0, false, false);
1168 return;
1169 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001170 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001171 break;
1172 case QMM_Escape:
1173 if (!IsPointer && Quals) {
1174 Out << "$$C";
1175 mangleQualifiers(Quals, false);
1176 }
1177 break;
1178 case QMM_Result:
1179 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1180 Out << '?';
1181 mangleQualifiers(Quals, false);
1182 }
1183 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001184 }
1185
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001186 // We have to mangle these now, while we still have enough information.
1187 if (IsPointer)
1188 manglePointerQualifiers(Quals);
1189 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001190
1191 switch (ty->getTypeClass()) {
1192#define ABSTRACT_TYPE(CLASS, PARENT)
1193#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1194 case Type::CLASS: \
1195 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1196 return;
1197#define TYPE(CLASS, PARENT) \
1198 case Type::CLASS: \
1199 mangleType(cast<CLASS##Type>(ty), Range); \
1200 break;
1201#include "clang/AST/TypeNodes.def"
1202#undef ABSTRACT_TYPE
1203#undef NON_CANONICAL_TYPE
1204#undef TYPE
1205 }
1206}
1207
1208void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1209 SourceRange Range) {
1210 // <type> ::= <builtin-type>
1211 // <builtin-type> ::= X # void
1212 // ::= C # signed char
1213 // ::= D # char
1214 // ::= E # unsigned char
1215 // ::= F # short
1216 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1217 // ::= H # int
1218 // ::= I # unsigned int
1219 // ::= J # long
1220 // ::= K # unsigned long
1221 // L # <none>
1222 // ::= M # float
1223 // ::= N # double
1224 // ::= O # long double (__float80 is mangled differently)
1225 // ::= _J # long long, __int64
1226 // ::= _K # unsigned long long, __int64
1227 // ::= _L # __int128
1228 // ::= _M # unsigned __int128
1229 // ::= _N # bool
1230 // _O # <array in parameter>
1231 // ::= _T # __float80 (Intel)
1232 // ::= _W # wchar_t
1233 // ::= _Z # __float80 (Digital Mars)
1234 switch (T->getKind()) {
1235 case BuiltinType::Void: Out << 'X'; break;
1236 case BuiltinType::SChar: Out << 'C'; break;
1237 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1238 case BuiltinType::UChar: Out << 'E'; break;
1239 case BuiltinType::Short: Out << 'F'; break;
1240 case BuiltinType::UShort: Out << 'G'; break;
1241 case BuiltinType::Int: Out << 'H'; break;
1242 case BuiltinType::UInt: Out << 'I'; break;
1243 case BuiltinType::Long: Out << 'J'; break;
1244 case BuiltinType::ULong: Out << 'K'; break;
1245 case BuiltinType::Float: Out << 'M'; break;
1246 case BuiltinType::Double: Out << 'N'; break;
1247 // TODO: Determine size and mangle accordingly
1248 case BuiltinType::LongDouble: Out << 'O'; break;
1249 case BuiltinType::LongLong: Out << "_J"; break;
1250 case BuiltinType::ULongLong: Out << "_K"; break;
1251 case BuiltinType::Int128: Out << "_L"; break;
1252 case BuiltinType::UInt128: Out << "_M"; break;
1253 case BuiltinType::Bool: Out << "_N"; break;
1254 case BuiltinType::WChar_S:
1255 case BuiltinType::WChar_U: Out << "_W"; break;
1256
1257#define BUILTIN_TYPE(Id, SingletonId)
1258#define PLACEHOLDER_TYPE(Id, SingletonId) \
1259 case BuiltinType::Id:
1260#include "clang/AST/BuiltinTypes.def"
1261 case BuiltinType::Dependent:
1262 llvm_unreachable("placeholder types shouldn't get to name mangling");
1263
1264 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1265 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1266 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001267
1268 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1269 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1270 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1271 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1272 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1273 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001274 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001275 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001276
1277 case BuiltinType::NullPtr: Out << "$$T"; break;
1278
1279 case BuiltinType::Char16:
1280 case BuiltinType::Char32:
1281 case BuiltinType::Half: {
1282 DiagnosticsEngine &Diags = Context.getDiags();
1283 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1284 "cannot mangle this built-in %0 type yet");
1285 Diags.Report(Range.getBegin(), DiagID)
1286 << T->getName(Context.getASTContext().getPrintingPolicy())
1287 << Range;
1288 break;
1289 }
1290 }
1291}
1292
1293// <type> ::= <function-type>
1294void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1295 SourceRange) {
1296 // Structors only appear in decls, so at this point we know it's not a
1297 // structor type.
1298 // FIXME: This may not be lambda-friendly.
1299 Out << "$$A6";
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001300 mangleFunctionType(T, NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001301}
1302void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1303 SourceRange) {
1304 llvm_unreachable("Can't mangle K&R function prototypes");
1305}
1306
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001307void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1308 const FunctionDecl *D,
1309 bool IsStructor,
1310 bool IsInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001311 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1312 // <return-type> <argument-list> <throw-spec>
1313 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1314
Reid Klecknerf21818d2013-06-24 19:21:52 +00001315 SourceRange Range;
1316 if (D) Range = D->getSourceRange();
1317
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001318 // If this is a C++ instance method, mangle the CVR qualifiers for the
1319 // this pointer.
David Majnemer1c7a4092013-08-15 08:13:23 +00001320 if (IsInstMethod) {
1321 if (PointersAre64Bit)
1322 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001323 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
David Majnemer1c7a4092013-08-15 08:13:23 +00001324 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001325
1326 mangleCallingConvention(T, IsInstMethod);
1327
1328 // <return-type> ::= <type>
1329 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001330 if (IsStructor) {
1331 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1332 StructorType == Dtor_Deleting) {
1333 // The scalar deleting destructor takes an extra int argument.
1334 // However, the FunctionType generated has 0 arguments.
1335 // FIXME: This is a temporary hack.
1336 // Maybe should fix the FunctionType creation instead?
Timur Iskhodzhanov4b104062013-08-26 10:32:04 +00001337 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001338 return;
1339 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001340 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001341 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001342 QualType ResultType = Proto->getResultType();
1343 if (ResultType->isVoidType())
1344 ResultType = ResultType.getUnqualifiedType();
1345 mangleType(ResultType, Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001346 }
1347
1348 // <argument-list> ::= X # void
1349 // ::= <type>+ @
1350 // ::= <type>* Z # varargs
1351 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1352 Out << 'X';
1353 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001354 // Happens for function pointer type arguments for example.
1355 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1356 ArgEnd = Proto->arg_type_end();
1357 Arg != ArgEnd; ++Arg)
1358 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001359 // <builtin-type> ::= Z # ellipsis
1360 if (Proto->isVariadic())
1361 Out << 'Z';
1362 else
1363 Out << '@';
1364 }
1365
1366 mangleThrowSpecification(Proto);
1367}
1368
1369void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001370 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1371 // # pointer. in 64-bit mode *all*
1372 // # 'this' pointers are 64-bit.
1373 // ::= <global-function>
1374 // <member-function> ::= A # private: near
1375 // ::= B # private: far
1376 // ::= C # private: static near
1377 // ::= D # private: static far
1378 // ::= E # private: virtual near
1379 // ::= F # private: virtual far
1380 // ::= G # private: thunk near
1381 // ::= H # private: thunk far
1382 // ::= I # protected: near
1383 // ::= J # protected: far
1384 // ::= K # protected: static near
1385 // ::= L # protected: static far
1386 // ::= M # protected: virtual near
1387 // ::= N # protected: virtual far
1388 // ::= O # protected: thunk near
1389 // ::= P # protected: thunk far
1390 // ::= Q # public: near
1391 // ::= R # public: far
1392 // ::= S # public: static near
1393 // ::= T # public: static far
1394 // ::= U # public: virtual near
1395 // ::= V # public: virtual far
1396 // ::= W # public: thunk near
1397 // ::= X # public: thunk far
1398 // <global-function> ::= Y # global near
1399 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001400 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1401 switch (MD->getAccess()) {
1402 default:
1403 case AS_private:
1404 if (MD->isStatic())
1405 Out << 'C';
1406 else if (MD->isVirtual())
1407 Out << 'E';
1408 else
1409 Out << 'A';
1410 break;
1411 case AS_protected:
1412 if (MD->isStatic())
1413 Out << 'K';
1414 else if (MD->isVirtual())
1415 Out << 'M';
1416 else
1417 Out << 'I';
1418 break;
1419 case AS_public:
1420 if (MD->isStatic())
1421 Out << 'S';
1422 else if (MD->isVirtual())
1423 Out << 'U';
1424 else
1425 Out << 'Q';
1426 }
1427 } else
1428 Out << 'Y';
1429}
1430void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
1431 bool IsInstMethod) {
1432 // <calling-convention> ::= A # __cdecl
1433 // ::= B # __export __cdecl
1434 // ::= C # __pascal
1435 // ::= D # __export __pascal
1436 // ::= E # __thiscall
1437 // ::= F # __export __thiscall
1438 // ::= G # __stdcall
1439 // ::= H # __export __stdcall
1440 // ::= I # __fastcall
1441 // ::= J # __export __fastcall
1442 // The 'export' calling conventions are from a bygone era
1443 // (*cough*Win16*cough*) when functions were declared for export with
1444 // that keyword. (It didn't actually export them, it just made them so
1445 // that they could be in a DLL and somebody from another module could call
1446 // them.)
1447 CallingConv CC = T->getCallConv();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001448 switch (CC) {
1449 default:
1450 llvm_unreachable("Unsupported CC for mangling");
Charles Davise8519c32013-08-30 04:39:01 +00001451 case CC_X86_64Win64:
1452 case CC_X86_64SysV:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001453 case CC_C: Out << 'A'; break;
1454 case CC_X86Pascal: Out << 'C'; break;
1455 case CC_X86ThisCall: Out << 'E'; break;
1456 case CC_X86StdCall: Out << 'G'; break;
1457 case CC_X86FastCall: Out << 'I'; break;
1458 }
1459}
1460void MicrosoftCXXNameMangler::mangleThrowSpecification(
1461 const FunctionProtoType *FT) {
1462 // <throw-spec> ::= Z # throw(...) (default)
1463 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1464 // ::= <type>+
1465 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1466 // all actually mangled as 'Z'. (They're ignored because their associated
1467 // functionality isn't implemented, and probably never will be.)
1468 Out << 'Z';
1469}
1470
1471void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1472 SourceRange Range) {
1473 // Probably should be mangled as a template instantiation; need to see what
1474 // VC does first.
1475 DiagnosticsEngine &Diags = Context.getDiags();
1476 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1477 "cannot mangle this unresolved dependent type yet");
1478 Diags.Report(Range.getBegin(), DiagID)
1479 << Range;
1480}
1481
1482// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1483// <union-type> ::= T <name>
1484// <struct-type> ::= U <name>
1485// <class-type> ::= V <name>
1486// <enum-type> ::= W <size> <name>
1487void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001488 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001489}
1490void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001491 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001492}
David Majnemer02c44f02013-08-05 22:26:46 +00001493void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1494 switch (TD->getTagKind()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001495 case TTK_Union:
1496 Out << 'T';
1497 break;
1498 case TTK_Struct:
1499 case TTK_Interface:
1500 Out << 'U';
1501 break;
1502 case TTK_Class:
1503 Out << 'V';
1504 break;
1505 case TTK_Enum:
1506 Out << 'W';
1507 Out << getASTContext().getTypeSizeInChars(
David Majnemer02c44f02013-08-05 22:26:46 +00001508 cast<EnumDecl>(TD)->getIntegerType()).getQuantity();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001509 break;
1510 }
David Majnemer02c44f02013-08-05 22:26:46 +00001511 mangleName(TD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001512}
1513
1514// <type> ::= <array-type>
1515// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1516// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001517// <element-type> # as global, E is never required
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001518// It's supposed to be the other way around, but for some strange reason, it
1519// isn't. Today this behavior is retained for the sole purpose of backwards
1520// compatibility.
David Majnemer58e4cd02013-09-11 04:44:30 +00001521void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001522 // This isn't a recursive mangling, so now we have to do it all in this
1523 // one call.
David Majnemer58e4cd02013-09-11 04:44:30 +00001524 manglePointerQualifiers(T->getElementType().getQualifiers());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001525 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001526}
1527void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1528 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001529 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001530}
1531void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1532 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001533 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001534}
1535void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *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 IncompleteArrayType *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}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001543void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001544 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001545 SmallVector<llvm::APInt, 3> Dimensions;
1546 for (;;) {
1547 if (const ConstantArrayType *CAT =
1548 getASTContext().getAsConstantArrayType(ElementTy)) {
1549 Dimensions.push_back(CAT->getSize());
1550 ElementTy = CAT->getElementType();
1551 } else if (ElementTy->isVariableArrayType()) {
1552 const VariableArrayType *VAT =
1553 getASTContext().getAsVariableArrayType(ElementTy);
1554 DiagnosticsEngine &Diags = Context.getDiags();
1555 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1556 "cannot mangle this variable-length array yet");
1557 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1558 << VAT->getBracketsRange();
1559 return;
1560 } else if (ElementTy->isDependentSizedArrayType()) {
1561 // The dependent expression has to be folded into a constant (TODO).
1562 const DependentSizedArrayType *DSAT =
1563 getASTContext().getAsDependentSizedArrayType(ElementTy);
1564 DiagnosticsEngine &Diags = Context.getDiags();
1565 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1566 "cannot mangle this dependent-length array yet");
1567 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1568 << DSAT->getBracketsRange();
1569 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001570 } else if (const IncompleteArrayType *IAT =
1571 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1572 Dimensions.push_back(llvm::APInt(32, 0));
1573 ElementTy = IAT->getElementType();
1574 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001575 else break;
1576 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001577 Out << 'Y';
1578 // <dimension-count> ::= <number> # number of extra dimensions
1579 mangleNumber(Dimensions.size());
1580 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1581 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001582 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001583}
1584
1585// <type> ::= <pointer-to-member-type>
1586// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1587// <class name> <type>
1588void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1589 SourceRange Range) {
1590 QualType PointeeType = T->getPointeeType();
1591 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1592 Out << '8';
1593 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001594 mangleFunctionType(FPT, NULL, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001595 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001596 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1597 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001598 mangleQualifiers(PointeeType.getQualifiers(), true);
1599 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001600 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001601 }
1602}
1603
1604void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1605 SourceRange Range) {
1606 DiagnosticsEngine &Diags = Context.getDiags();
1607 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1608 "cannot mangle this template type parameter type yet");
1609 Diags.Report(Range.getBegin(), DiagID)
1610 << Range;
1611}
1612
1613void MicrosoftCXXNameMangler::mangleType(
1614 const SubstTemplateTypeParmPackType *T,
1615 SourceRange Range) {
1616 DiagnosticsEngine &Diags = Context.getDiags();
1617 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1618 "cannot mangle this substituted parameter pack yet");
1619 Diags.Report(Range.getBegin(), DiagID)
1620 << Range;
1621}
1622
1623// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001624// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1625// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001626void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1627 SourceRange Range) {
1628 QualType PointeeTy = T->getPointeeType();
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001629 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1630 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001631 mangleType(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001632}
1633void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1634 SourceRange Range) {
1635 // Object pointers never have qualifiers.
1636 Out << 'A';
David Majnemer1c7a4092013-08-15 08:13:23 +00001637 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1638 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001639 mangleType(T->getPointeeType(), Range);
1640}
1641
1642// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001643// <reference-type> ::= A E? <cvr-qualifiers> <type>
1644// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001645void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1646 SourceRange Range) {
1647 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001648 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1649 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001650 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001651}
1652
1653// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001654// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1655// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001656void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1657 SourceRange Range) {
1658 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001659 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1660 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001661 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001662}
1663
1664void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1665 SourceRange Range) {
1666 DiagnosticsEngine &Diags = Context.getDiags();
1667 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1668 "cannot mangle this complex number type yet");
1669 Diags.Report(Range.getBegin(), DiagID)
1670 << Range;
1671}
1672
1673void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1674 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001675 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1676 assert(ET && "vectors with non-builtin elements are unsupported");
1677 uint64_t Width = getASTContext().getTypeSize(T);
1678 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1679 // doesn't match the Intel types uses a custom mangling below.
1680 bool IntelVector = true;
1681 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1682 Out << "T__m64";
1683 } else if (Width == 128 || Width == 256) {
1684 if (ET->getKind() == BuiltinType::Float)
1685 Out << "T__m" << Width;
1686 else if (ET->getKind() == BuiltinType::LongLong)
1687 Out << "T__m" << Width << 'i';
1688 else if (ET->getKind() == BuiltinType::Double)
1689 Out << "U__m" << Width << 'd';
1690 else
1691 IntelVector = false;
1692 } else {
1693 IntelVector = false;
1694 }
1695
1696 if (!IntelVector) {
1697 // The MS ABI doesn't have a special mangling for vector types, so we define
1698 // our own mangling to handle uses of __vector_size__ on user-specified
1699 // types, and for extensions like __v4sf.
1700 Out << "T__clang_vec" << T->getNumElements() << '_';
1701 mangleType(ET, Range);
1702 }
1703
1704 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001705}
Reid Kleckner1232e272013-03-26 16:56:59 +00001706
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001707void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1708 SourceRange Range) {
1709 DiagnosticsEngine &Diags = Context.getDiags();
1710 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1711 "cannot mangle this extended vector type yet");
1712 Diags.Report(Range.getBegin(), DiagID)
1713 << Range;
1714}
1715void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1716 SourceRange Range) {
1717 DiagnosticsEngine &Diags = Context.getDiags();
1718 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1719 "cannot mangle this dependent-sized extended vector type yet");
1720 Diags.Report(Range.getBegin(), DiagID)
1721 << Range;
1722}
1723
1724void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1725 SourceRange) {
1726 // ObjC interfaces have structs underlying them.
1727 Out << 'U';
1728 mangleName(T->getDecl());
1729}
1730
1731void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1732 SourceRange Range) {
1733 // We don't allow overloading by different protocol qualification,
1734 // so mangling them isn't necessary.
1735 mangleType(T->getBaseType(), Range);
1736}
1737
1738void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1739 SourceRange Range) {
1740 Out << "_E";
1741
1742 QualType pointee = T->getPointeeType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001743 mangleFunctionType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001744}
1745
David Majnemer360d23e2013-08-16 08:29:13 +00001746void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1747 SourceRange) {
1748 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001749}
1750
1751void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1752 SourceRange Range) {
1753 DiagnosticsEngine &Diags = Context.getDiags();
1754 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1755 "cannot mangle this template specialization type yet");
1756 Diags.Report(Range.getBegin(), DiagID)
1757 << Range;
1758}
1759
1760void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1761 SourceRange Range) {
1762 DiagnosticsEngine &Diags = Context.getDiags();
1763 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1764 "cannot mangle this dependent name type yet");
1765 Diags.Report(Range.getBegin(), DiagID)
1766 << Range;
1767}
1768
1769void MicrosoftCXXNameMangler::mangleType(
1770 const DependentTemplateSpecializationType *T,
1771 SourceRange Range) {
1772 DiagnosticsEngine &Diags = Context.getDiags();
1773 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1774 "cannot mangle this dependent template specialization type yet");
1775 Diags.Report(Range.getBegin(), DiagID)
1776 << Range;
1777}
1778
1779void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1780 SourceRange Range) {
1781 DiagnosticsEngine &Diags = Context.getDiags();
1782 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1783 "cannot mangle this pack expansion yet");
1784 Diags.Report(Range.getBegin(), DiagID)
1785 << Range;
1786}
1787
1788void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1789 SourceRange Range) {
1790 DiagnosticsEngine &Diags = Context.getDiags();
1791 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1792 "cannot mangle this typeof(type) yet");
1793 Diags.Report(Range.getBegin(), DiagID)
1794 << Range;
1795}
1796
1797void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1798 SourceRange Range) {
1799 DiagnosticsEngine &Diags = Context.getDiags();
1800 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1801 "cannot mangle this typeof(expression) yet");
1802 Diags.Report(Range.getBegin(), DiagID)
1803 << Range;
1804}
1805
1806void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1807 SourceRange Range) {
1808 DiagnosticsEngine &Diags = Context.getDiags();
1809 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1810 "cannot mangle this decltype() yet");
1811 Diags.Report(Range.getBegin(), DiagID)
1812 << Range;
1813}
1814
1815void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1816 SourceRange Range) {
1817 DiagnosticsEngine &Diags = Context.getDiags();
1818 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1819 "cannot mangle this unary transform type yet");
1820 Diags.Report(Range.getBegin(), DiagID)
1821 << Range;
1822}
1823
1824void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1825 DiagnosticsEngine &Diags = Context.getDiags();
1826 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1827 "cannot mangle this 'auto' type yet");
1828 Diags.Report(Range.getBegin(), DiagID)
1829 << Range;
1830}
1831
1832void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1833 SourceRange Range) {
1834 DiagnosticsEngine &Diags = Context.getDiags();
1835 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1836 "cannot mangle this C11 atomic type yet");
1837 Diags.Report(Range.getBegin(), DiagID)
1838 << Range;
1839}
1840
1841void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1842 raw_ostream &Out) {
1843 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1844 "Invalid mangleName() call, argument is not a variable or function!");
1845 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1846 "Invalid mangleName() call on 'structor decl!");
1847
1848 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1849 getASTContext().getSourceManager(),
1850 "Mangling declaration");
1851
1852 MicrosoftCXXNameMangler Mangler(*this, Out);
1853 return Mangler.mangle(D);
1854}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001855
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001856void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1857 const ThunkInfo &Thunk,
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001858 raw_ostream &Out) {
1859 // FIXME: this is not yet a complete implementation, but merely a
1860 // reasonably-working stub to avoid crashing when required to emit a thunk.
1861 MicrosoftCXXNameMangler Mangler(*this, Out);
1862 Out << "\01?";
1863 Mangler.mangleName(MD);
1864 if (Thunk.This.NonVirtual != 0) {
1865 // FIXME: add support for protected/private or use mangleFunctionClass.
1866 Out << "W";
1867 llvm::APSInt APSNumber(/*BitWidth=*/32 /*FIXME: check on x64*/,
1868 /*isUnsigned=*/true);
1869 APSNumber = -Thunk.This.NonVirtual;
1870 Mangler.mangleNumber(APSNumber);
1871 } else {
1872 // FIXME: add support for protected/private or use mangleFunctionClass.
1873 Out << "Q";
1874 }
1875 // FIXME: mangle return adjustment? Most likely includes using an overridee FPT?
1876 Mangler.mangleFunctionType(MD->getType()->castAs<FunctionProtoType>(), MD, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001877}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001878
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001879void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1880 CXXDtorType Type,
1881 const ThisAdjustment &,
1882 raw_ostream &) {
1883 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1884 "cannot mangle thunk for this destructor yet");
1885 getDiags().Report(DD->getLocation(), DiagID);
1886}
Reid Kleckner90633022013-06-19 15:20:38 +00001887
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001888void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1889 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001890 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1891 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001892 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001893 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001894 MicrosoftCXXNameMangler Mangler(*this, Out);
1895 Mangler.getStream() << "\01??_7";
1896 Mangler.mangleName(RD);
Reid Kleckner90633022013-06-19 15:20:38 +00001897 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001898 // TODO: If the class has more than one vtable, mangle in the class it came
1899 // from.
1900 Mangler.getStream() << '@';
1901}
Reid Kleckner90633022013-06-19 15:20:38 +00001902
1903void MicrosoftMangleContext::mangleCXXVBTable(
1904 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1905 raw_ostream &Out) {
1906 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1907 // <cvr-qualifiers> [<name>] @
1908 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1909 // is always '7' for vbtables.
1910 MicrosoftCXXNameMangler Mangler(*this, Out);
1911 Mangler.getStream() << "\01??_8";
1912 Mangler.mangleName(Derived);
1913 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1914 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1915 E = BasePath.end();
1916 I != E; ++I) {
1917 Mangler.mangleName(*I);
1918 }
1919 Mangler.getStream() << '@';
1920}
1921
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001922void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1923 raw_ostream &) {
1924 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1925}
1926void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1927 int64_t Offset,
1928 const CXXRecordDecl *Type,
1929 raw_ostream &) {
1930 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1931}
1932void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1933 raw_ostream &) {
1934 // FIXME: Give a location...
1935 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1936 "cannot mangle RTTI descriptors for type %0 yet");
1937 getDiags().Report(DiagID)
1938 << T.getBaseTypeIdentifier();
1939}
1940void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1941 raw_ostream &) {
1942 // FIXME: Give a location...
1943 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1944 "cannot mangle the name of type %0 into RTTI descriptors yet");
1945 getDiags().Report(DiagID)
1946 << T.getBaseTypeIdentifier();
1947}
1948void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1949 CXXCtorType Type,
1950 raw_ostream & Out) {
1951 MicrosoftCXXNameMangler mangler(*this, Out);
1952 mangler.mangle(D);
1953}
1954void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1955 CXXDtorType Type,
1956 raw_ostream & Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001957 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001958 mangler.mangle(D);
1959}
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001960void MicrosoftMangleContext::mangleReferenceTemporary(const VarDecl *VD,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001961 raw_ostream &) {
1962 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1963 "cannot mangle this reference temporary yet");
1964 getDiags().Report(VD->getLocation(), DiagID);
1965}
1966
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001967void MicrosoftMangleContext::mangleStaticGuardVariable(const VarDecl *VD,
1968 raw_ostream &Out) {
1969 // <guard-name> ::= ?_B <postfix> @51
1970 // ::= ?$S <guard-num> @ <postfix> @4IA
1971
1972 // The first mangling is what MSVC uses to guard static locals in inline
1973 // functions. It uses a different mangling in external functions to support
1974 // guarding more than 32 variables. MSVC rejects inline functions with more
1975 // than 32 static locals. We don't fully implement the second mangling
1976 // because those guards are not externally visible, and instead use LLVM's
1977 // default renaming when creating a new guard variable.
1978 MicrosoftCXXNameMangler Mangler(*this, Out);
1979
1980 bool Visible = VD->isExternallyVisible();
1981 // <operator-name> ::= ?_B # local static guard
1982 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@");
1983 Mangler.manglePostfix(VD->getDeclContext());
1984 Mangler.getStream() << (Visible ? "@51" : "@4IA");
1985}
1986
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00001987void MicrosoftMangleContext::mangleInitFiniStub(const VarDecl *D,
1988 raw_ostream &Out,
1989 char CharCode) {
1990 MicrosoftCXXNameMangler Mangler(*this, Out);
1991 Mangler.getStream() << "\01??__" << CharCode;
1992 Mangler.mangleName(D);
1993 // This is the function class mangling. These stubs are global, non-variadic,
1994 // cdecl functions that return void and take no args.
1995 Mangler.getStream() << "YAXXZ";
1996}
1997
1998void MicrosoftMangleContext::mangleDynamicInitializer(const VarDecl *D,
1999 raw_ostream &Out) {
2000 // <initializer-name> ::= ?__E <name> YAXXZ
2001 mangleInitFiniStub(D, Out, 'E');
2002}
2003
Reid Kleckner942f9fe2013-09-10 20:14:30 +00002004void MicrosoftMangleContext::mangleDynamicAtExitDestructor(const VarDecl *D,
2005 raw_ostream &Out) {
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00002006 // <destructor-name> ::= ?__F <name> YAXXZ
2007 mangleInitFiniStub(D, Out, 'F');
Reid Kleckner942f9fe2013-09-10 20:14:30 +00002008}
2009
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002010MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
2011 DiagnosticsEngine &Diags) {
2012 return new MicrosoftMangleContext(Context, Diags);
2013}