blob: 3ccc2bddf6dc0cba719c7565b1edd966af9862eb [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
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000032static const FunctionDecl *getStructor(const FunctionDecl *fn) {
33 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
34 return ftd->getTemplatedDecl();
35
36 return fn;
37}
38
Guy Benyei7f92f2d2012-12-18 14:30:41 +000039/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
40/// Microsoft Visual C++ ABI.
41class MicrosoftCXXNameMangler {
42 MangleContext &Context;
43 raw_ostream &Out;
44
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000045 /// The "structor" is the top-level declaration being mangled, if
46 /// that's not a template specialization; otherwise it's the pattern
47 /// for that specialization.
48 const NamedDecl *Structor;
49 unsigned StructorType;
50
Reid Klecknerf0219cd2013-05-22 17:16:39 +000051 typedef llvm::StringMap<unsigned> BackRefMap;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000052 BackRefMap NameBackReferences;
53 bool UseNameBackReferences;
54
55 typedef llvm::DenseMap<void*, unsigned> ArgBackRefMap;
56 ArgBackRefMap TypeBackReferences;
57
58 ASTContext &getASTContext() const { return Context.getASTContext(); }
59
Reid Klecknerd6a08d12013-05-14 20:30:42 +000060 // FIXME: If we add support for __ptr32/64 qualifiers, then we should push
61 // this check into mangleQualifiers().
62 const bool PointersAre64Bit;
63
Guy Benyei7f92f2d2012-12-18 14:30:41 +000064public:
Peter Collingbourneb70d1c32013-04-25 04:25:40 +000065 enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result };
66
Guy Benyei7f92f2d2012-12-18 14:30:41 +000067 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000068 : Context(C), Out(Out_),
69 Structor(0), StructorType(-1),
David Blaikiee7e94c92013-05-14 21:31:46 +000070 UseNameBackReferences(true),
Reid Klecknerd6a08d12013-05-14 20:30:42 +000071 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikiee7e94c92013-05-14 21:31:46 +000072 64) { }
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000073
74 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_,
75 const CXXDestructorDecl *D, CXXDtorType Type)
76 : Context(C), Out(Out_),
77 Structor(getStructor(D)), StructorType(Type),
David Blaikiee7e94c92013-05-14 21:31:46 +000078 UseNameBackReferences(true),
Reid Klecknerd6a08d12013-05-14 20:30:42 +000079 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikiee7e94c92013-05-14 21:31:46 +000080 64) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +000081
82 raw_ostream &getStream() const { return Out; }
83
84 void mangle(const NamedDecl *D, StringRef Prefix = "\01?");
85 void mangleName(const NamedDecl *ND);
David Majnemerc80eb462013-08-13 06:32:20 +000086 void mangleDeclaration(const NamedDecl *ND);
Guy Benyei7f92f2d2012-12-18 14:30:41 +000087 void mangleFunctionEncoding(const FunctionDecl *FD);
88 void mangleVariableEncoding(const VarDecl *VD);
89 void mangleNumber(int64_t Number);
90 void mangleNumber(const llvm::APSInt &Value);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +000091 void mangleType(QualType T, SourceRange Range,
92 QualifierMangleMode QMM = QMM_Mangle);
Timur Iskhodzhanov635de282013-07-30 09:46:19 +000093 void mangleFunctionType(const FunctionType *T, const FunctionDecl *D,
94 bool IsStructor, bool IsInstMethod);
Reid Kleckner942f9fe2013-09-10 20:14:30 +000095 void manglePostfix(const DeclContext *DC, bool NoFunction = false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +000096
97private:
98 void disableBackReferences() { UseNameBackReferences = false; }
99 void mangleUnqualifiedName(const NamedDecl *ND) {
100 mangleUnqualifiedName(ND, ND->getDeclName());
101 }
102 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
103 void mangleSourceName(const IdentifierInfo *II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000104 void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc);
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000105 void mangleCXXDtorType(CXXDtorType T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000106 void mangleQualifiers(Qualifiers Quals, bool IsMember);
107 void manglePointerQualifiers(Qualifiers Quals);
108
109 void mangleUnscopedTemplateName(const TemplateDecl *ND);
110 void mangleTemplateInstantiationName(const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000111 const TemplateArgumentList &TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000112 void mangleObjCMethodName(const ObjCMethodDecl *MD);
113 void mangleLocalName(const FunctionDecl *FD);
114
115 void mangleArgumentType(QualType T, SourceRange Range);
116
117 // Declare manglers for every type class.
118#define ABSTRACT_TYPE(CLASS, PARENT)
119#define NON_CANONICAL_TYPE(CLASS, PARENT)
120#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \
121 SourceRange Range);
122#include "clang/AST/TypeNodes.def"
123#undef ABSTRACT_TYPE
124#undef NON_CANONICAL_TYPE
125#undef TYPE
126
David Majnemer02c44f02013-08-05 22:26:46 +0000127 void mangleType(const TagDecl *TD);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000128 void mangleDecayedArrayType(const ArrayType *T, bool IsGlobal);
Reid Klecknerf21818d2013-06-24 19:21:52 +0000129 void mangleArrayType(const ArrayType *T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000130 void mangleFunctionClass(const FunctionDecl *FD);
131 void mangleCallingConvention(const FunctionType *T, bool IsInstMethod = false);
132 void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean);
133 void mangleExpression(const Expr *E);
134 void mangleThrowSpecification(const FunctionProtoType *T);
135
Reid Klecknerf16216c2013-03-20 01:40:23 +0000136 void mangleTemplateArgs(const TemplateDecl *TD,
137 const TemplateArgumentList &TemplateArgs);
David Majnemer309f6452013-08-27 08:21:25 +0000138 void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000139};
140
141/// MicrosoftMangleContext - Overrides the default MangleContext for the
142/// Microsoft Visual C++ ABI.
143class MicrosoftMangleContext : public MangleContext {
144public:
145 MicrosoftMangleContext(ASTContext &Context,
146 DiagnosticsEngine &Diags) : MangleContext(Context, Diags) { }
147 virtual bool shouldMangleDeclName(const NamedDecl *D);
148 virtual void mangleName(const NamedDecl *D, raw_ostream &Out);
149 virtual void mangleThunk(const CXXMethodDecl *MD,
150 const ThunkInfo &Thunk,
151 raw_ostream &);
152 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
153 const ThisAdjustment &ThisAdjustment,
154 raw_ostream &);
155 virtual void mangleCXXVTable(const CXXRecordDecl *RD,
156 raw_ostream &);
157 virtual void mangleCXXVTT(const CXXRecordDecl *RD,
158 raw_ostream &);
Reid Kleckner90633022013-06-19 15:20:38 +0000159 virtual void mangleCXXVBTable(const CXXRecordDecl *Derived,
160 ArrayRef<const CXXRecordDecl *> BasePath,
161 raw_ostream &Out);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000162 virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
163 const CXXRecordDecl *Type,
164 raw_ostream &);
165 virtual void mangleCXXRTTI(QualType T, raw_ostream &);
166 virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
167 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
168 raw_ostream &);
169 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
170 raw_ostream &);
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000171 virtual void mangleReferenceTemporary(const VarDecl *, raw_ostream &);
172 virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out);
Reid Klecknerc5c6fa72013-09-10 20:43:12 +0000173 virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out);
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000174 virtual void mangleDynamicAtExitDestructor(const VarDecl *D,
175 raw_ostream &Out);
Reid Klecknerc5c6fa72013-09-10 20:43:12 +0000176
177private:
178 void mangleInitFiniStub(const VarDecl *D, raw_ostream &Out, char CharCode);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000179};
180
181}
182
183static bool isInCLinkageSpecification(const Decl *D) {
184 D = D->getCanonicalDecl();
185 for (const DeclContext *DC = D->getDeclContext();
186 !DC->isTranslationUnit(); DC = DC->getParent()) {
187 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
188 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
189 }
190
191 return false;
192}
193
194bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
195 // In C, functions with no attributes never need to be mangled. Fastpath them.
196 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
197 return false;
198
199 // Any decl can be declared with __asm("foo") on it, and this takes precedence
200 // over all other naming in the .o file.
201 if (D->hasAttr<AsmLabelAttr>())
202 return true;
203
204 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
205 // (always) as does passing a C++ member function and a function
206 // whose name is not a simple identifier.
207 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
208 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
209 !FD->getDeclName().isIdentifier()))
210 return true;
211
212 // Otherwise, no mangling is done outside C++ mode.
213 if (!getASTContext().getLangOpts().CPlusPlus)
214 return false;
215
216 // Variables at global scope with internal linkage are not mangled.
217 if (!FD) {
218 const DeclContext *DC = D->getDeclContext();
Rafael Espindola181e3ec2013-05-13 00:12:11 +0000219 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000220 return false;
221 }
222
223 // C functions and "main" are not mangled.
224 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
225 return false;
226
227 return true;
228}
229
230void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
231 StringRef Prefix) {
232 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
233 // Therefore it's really important that we don't decorate the
234 // name with leading underscores or leading/trailing at signs. So, by
235 // default, we emit an asm marker at the start so we get the name right.
236 // Callers can override this with a custom prefix.
237
238 // Any decl can be declared with __asm("foo") on it, and this takes precedence
239 // over all other naming in the .o file.
240 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
241 // If we have an asm name, then we use it as the mangling.
242 Out << '\01' << ALA->getLabel();
243 return;
244 }
245
246 // <mangled-name> ::= ? <name> <type-encoding>
247 Out << Prefix;
248 mangleName(D);
249 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
250 mangleFunctionEncoding(FD);
251 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
252 mangleVariableEncoding(VD);
253 else {
254 // TODO: Fields? Can MSVC even mangle them?
255 // Issue a diagnostic for now.
256 DiagnosticsEngine &Diags = Context.getDiags();
257 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
258 "cannot mangle this declaration yet");
259 Diags.Report(D->getLocation(), DiagID)
260 << D->getSourceRange();
261 }
262}
263
264void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
265 // <type-encoding> ::= <function-class> <function-type>
266
Reid Klecknerf21818d2013-06-24 19:21:52 +0000267 // Since MSVC operates on the type as written and not the canonical type, it
268 // actually matters which decl we have here. MSVC appears to choose the
269 // first, since it is most likely to be the declaration in a header file.
270 FD = FD->getFirstDeclaration();
271
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000272 // Don't mangle in the type if this isn't a decl we should typically mangle.
273 if (!Context.shouldMangleDeclName(FD))
274 return;
275
276 // We should never ever see a FunctionNoProtoType at this point.
277 // We don't even know how to mangle their types anyway :).
Reid Klecknerf21818d2013-06-24 19:21:52 +0000278 TypeSourceInfo *TSI = FD->getTypeSourceInfo();
279 QualType T = TSI ? TSI->getType() : FD->getType();
280 const FunctionProtoType *FT = T->castAs<FunctionProtoType>();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000281
282 bool InStructor = false, InInstMethod = false;
283 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
284 if (MD) {
285 if (MD->isInstance())
286 InInstMethod = true;
287 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
288 InStructor = true;
289 }
290
291 // First, the function class.
292 mangleFunctionClass(FD);
293
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000294 mangleFunctionType(FT, FD, InStructor, InInstMethod);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000295}
296
297void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
298 // <type-encoding> ::= <storage-class> <variable-type>
299 // <storage-class> ::= 0 # private static member
300 // ::= 1 # protected static member
301 // ::= 2 # public static member
302 // ::= 3 # global
303 // ::= 4 # static local
304
305 // The first character in the encoding (after the name) is the storage class.
306 if (VD->isStaticDataMember()) {
307 // If it's a static member, it also encodes the access level.
308 switch (VD->getAccess()) {
309 default:
310 case AS_private: Out << '0'; break;
311 case AS_protected: Out << '1'; break;
312 case AS_public: Out << '2'; break;
313 }
314 }
315 else if (!VD->isStaticLocal())
316 Out << '3';
317 else
318 Out << '4';
319 // Now mangle the type.
320 // <variable-type> ::= <type> <cvr-qualifiers>
321 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
322 // Pointers and references are odd. The type of 'int * const foo;' gets
323 // mangled as 'QAHA' instead of 'PAHB', for example.
324 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
325 QualType Ty = TL.getType();
David Majnemer1c7a4092013-08-15 08:13:23 +0000326 if (Ty->isPointerType() || Ty->isReferenceType() ||
327 Ty->isMemberPointerType()) {
David Majnemer17ffbd02013-08-09 05:56:24 +0000328 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000329 if (PointersAre64Bit)
330 Out << 'E';
331 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) {
332 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true);
333 // Member pointers are suffixed with a back reference to the member
334 // pointer's class name.
335 mangleName(MPT->getClass()->getAsCXXRecordDecl());
336 } else
337 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
David Majnemer17ffbd02013-08-09 05:56:24 +0000338 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000339 // Global arrays are funny, too.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000340 mangleDecayedArrayType(AT, true);
341 if (AT->getElementType()->isArrayType())
342 Out << 'A';
343 else
344 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000345 } else {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000346 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000347 mangleQualifiers(Ty.getLocalQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000348 }
349}
350
351void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
352 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
353 const DeclContext *DC = ND->getDeclContext();
354
355 // Always start with the unqualified name.
356 mangleUnqualifiedName(ND);
357
358 // If this is an extern variable declared locally, the relevant DeclContext
359 // is that of the containing namespace, or the translation unit.
360 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
361 while (!DC->isNamespace() && !DC->isTranslationUnit())
362 DC = DC->getParent();
363
364 manglePostfix(DC);
365
366 // Terminate the whole name with an '@'.
367 Out << '@';
368}
369
370void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
371 llvm::APSInt APSNumber(/*BitWidth=*/64, /*isUnsigned=*/false);
372 APSNumber = Number;
373 mangleNumber(APSNumber);
374}
375
376void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
377 // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
378 // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
379 // ::= [?] @ # 0 (alternate mangling, not emitted by VC)
380 if (Value.isSigned() && Value.isNegative()) {
381 Out << '?';
382 mangleNumber(llvm::APSInt(Value.abs()));
383 return;
384 }
385 llvm::APSInt Temp(Value);
386 // There's a special shorter mangling for 0, but Microsoft
387 // chose not to use it. Instead, 0 gets mangled as "A@". Oh well...
388 if (Value.uge(1) && Value.ule(10)) {
389 --Temp;
390 Temp.print(Out, false);
391 } else {
392 // We have to build up the encoding in reverse order, so it will come
393 // out right when we write it out.
394 char Encoding[64];
395 char *EndPtr = Encoding+sizeof(Encoding);
396 char *CurPtr = EndPtr;
397 llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned());
398 NibbleMask = 0xf;
399 do {
400 *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf);
401 Temp = Temp.lshr(4);
402 } while (Temp != 0);
403 Out.write(CurPtr, EndPtr-CurPtr);
404 Out << '@';
405 }
406}
407
408static const TemplateDecl *
Reid Klecknerf16216c2013-03-20 01:40:23 +0000409isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000410 // Check if we have a function template.
411 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
412 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000413 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000414 return TD;
415 }
416 }
417
418 // Check if we have a class template.
419 if (const ClassTemplateSpecializationDecl *Spec =
Reid Klecknerf16216c2013-03-20 01:40:23 +0000420 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
421 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000422 return Spec->getSpecializedTemplate();
423 }
424
425 return 0;
426}
427
428void
429MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
430 DeclarationName Name) {
431 // <unqualified-name> ::= <operator-name>
432 // ::= <ctor-dtor-name>
433 // ::= <source-name>
434 // ::= <template-name>
Reid Klecknerf16216c2013-03-20 01:40:23 +0000435
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000436 // Check if we have a template.
Reid Klecknerf16216c2013-03-20 01:40:23 +0000437 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000438 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Kleckner3be37d12013-07-13 00:43:39 +0000439 // Function templates aren't considered for name back referencing. This
440 // makes sense since function templates aren't likely to occur multiple
441 // times in a symbol.
442 // FIXME: Test alias template mangling with MSVC 2013.
443 if (!isa<ClassTemplateDecl>(TD)) {
444 mangleTemplateInstantiationName(TD, *TemplateArgs);
445 return;
446 }
447
448 // We have a class template.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000449 // Here comes the tricky thing: if we need to mangle something like
450 // void foo(A::X<Y>, B::X<Y>),
451 // the X<Y> part is aliased. However, if you need to mangle
452 // void foo(A::X<A::Y>, A::X<B::Y>),
453 // the A::X<> part is not aliased.
454 // That said, from the mangler's perspective we have a structure like this:
455 // namespace[s] -> type[ -> template-parameters]
456 // but from the Clang perspective we have
457 // type [ -> template-parameters]
458 // \-> namespace[s]
459 // What we do is we create a new mangler, mangle the same type (without
460 // a namespace suffix) using the extra mangler with back references
461 // disabled (to avoid infinite recursion) and then use the mangled type
462 // name as a key to check the mangling of different types for aliasing.
463
464 std::string BackReferenceKey;
465 BackRefMap::iterator Found;
466 if (UseNameBackReferences) {
467 llvm::raw_string_ostream Stream(BackReferenceKey);
468 MicrosoftCXXNameMangler Extra(Context, Stream);
469 Extra.disableBackReferences();
470 Extra.mangleUnqualifiedName(ND, Name);
471 Stream.flush();
472
473 Found = NameBackReferences.find(BackReferenceKey);
474 }
475 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000476 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000477 if (UseNameBackReferences && NameBackReferences.size() < 10) {
478 size_t Size = NameBackReferences.size();
479 NameBackReferences[BackReferenceKey] = Size;
480 }
481 } else {
482 Out << Found->second;
483 }
484 return;
485 }
486
487 switch (Name.getNameKind()) {
488 case DeclarationName::Identifier: {
489 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
490 mangleSourceName(II);
491 break;
492 }
493
494 // Otherwise, an anonymous entity. We must have a declaration.
495 assert(ND && "mangling empty name without declaration");
496
497 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
498 if (NS->isAnonymousNamespace()) {
499 Out << "?A@";
500 break;
501 }
502 }
503
504 // We must have an anonymous struct.
505 const TagDecl *TD = cast<TagDecl>(ND);
506 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
507 assert(TD->getDeclContext() == D->getDeclContext() &&
508 "Typedef should not be in another decl context!");
509 assert(D->getDeclName().getAsIdentifierInfo() &&
510 "Typedef was not named!");
511 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
512 break;
513 }
514
515 // When VC encounters an anonymous type with no tag and no typedef,
David Majnemerec0258a2013-08-26 02:35:51 +0000516 // it literally emits '<unnamed-tag>@'.
517 Out << "<unnamed-tag>@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000518 break;
519 }
520
521 case DeclarationName::ObjCZeroArgSelector:
522 case DeclarationName::ObjCOneArgSelector:
523 case DeclarationName::ObjCMultiArgSelector:
524 llvm_unreachable("Can't mangle Objective-C selector names here!");
525
526 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000527 if (ND == Structor) {
528 assert(StructorType == Ctor_Complete &&
529 "Should never be asked to mangle a ctor other than complete");
530 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000531 Out << "?0";
532 break;
533
534 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000535 if (ND == Structor)
536 // If the named decl is the C++ destructor we're mangling,
537 // use the type we were given.
538 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
539 else
Reid Klecknera4130ba2013-07-22 13:51:44 +0000540 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000541 // class with a destructor is declared within a destructor.
Reid Klecknera4130ba2013-07-22 13:51:44 +0000542 mangleCXXDtorType(Dtor_Base);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000543 break;
544
545 case DeclarationName::CXXConversionFunctionName:
546 // <operator-name> ::= ?B # (cast)
547 // The target type is encoded as the return type.
548 Out << "?B";
549 break;
550
551 case DeclarationName::CXXOperatorName:
552 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
553 break;
554
555 case DeclarationName::CXXLiteralOperatorName: {
556 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
557 DiagnosticsEngine Diags = Context.getDiags();
558 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
559 "cannot mangle this literal operator yet");
560 Diags.Report(ND->getLocation(), DiagID);
561 break;
562 }
563
564 case DeclarationName::CXXUsingDirective:
565 llvm_unreachable("Can't mangle a using directive name!");
566 }
567}
568
569void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
570 bool NoFunction) {
571 // <postfix> ::= <unqualified-name> [<postfix>]
572 // ::= <substitution> [<postfix>]
573
574 if (!DC) return;
575
576 while (isa<LinkageSpecDecl>(DC))
577 DC = DC->getParent();
578
579 if (DC->isTranslationUnit())
580 return;
581
582 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedmane5798892013-07-10 01:13:27 +0000583 DiagnosticsEngine Diags = Context.getDiags();
584 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
585 "cannot mangle a local inside this block yet");
586 Diags.Report(BD->getLocation(), DiagID);
587
588 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
589 // for how this should be done.
590 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000591 Out << '@';
592 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000593 } else if (isa<CapturedDecl>(DC)) {
594 // Skip CapturedDecl context.
595 manglePostfix(DC->getParent(), NoFunction);
596 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000597 }
598
599 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
600 return;
601 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
602 mangleObjCMethodName(Method);
603 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
604 mangleLocalName(Func);
605 else {
606 mangleUnqualifiedName(cast<NamedDecl>(DC));
607 manglePostfix(DC->getParent(), NoFunction);
608 }
609}
610
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000611void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000612 // Microsoft uses the names on the case labels for these dtor variants. Clang
613 // uses the Itanium terminology internally. Everything in this ABI delegates
614 // towards the base dtor.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000615 switch (T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000616 // <operator-name> ::= ?1 # destructor
617 case Dtor_Base: Out << "?1"; return;
618 // <operator-name> ::= ?_D # vbase destructor
619 case Dtor_Complete: Out << "?_D"; return;
620 // <operator-name> ::= ?_G # scalar deleting destructor
621 case Dtor_Deleting: Out << "?_G"; return;
622 // <operator-name> ::= ?_E # vector deleting destructor
623 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
624 // it.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000625 }
626 llvm_unreachable("Unsupported dtor type?");
627}
628
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000629void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
630 SourceLocation Loc) {
631 switch (OO) {
632 // ?0 # constructor
633 // ?1 # destructor
634 // <operator-name> ::= ?2 # new
635 case OO_New: Out << "?2"; break;
636 // <operator-name> ::= ?3 # delete
637 case OO_Delete: Out << "?3"; break;
638 // <operator-name> ::= ?4 # =
639 case OO_Equal: Out << "?4"; break;
640 // <operator-name> ::= ?5 # >>
641 case OO_GreaterGreater: Out << "?5"; break;
642 // <operator-name> ::= ?6 # <<
643 case OO_LessLess: Out << "?6"; break;
644 // <operator-name> ::= ?7 # !
645 case OO_Exclaim: Out << "?7"; break;
646 // <operator-name> ::= ?8 # ==
647 case OO_EqualEqual: Out << "?8"; break;
648 // <operator-name> ::= ?9 # !=
649 case OO_ExclaimEqual: Out << "?9"; break;
650 // <operator-name> ::= ?A # []
651 case OO_Subscript: Out << "?A"; break;
652 // ?B # conversion
653 // <operator-name> ::= ?C # ->
654 case OO_Arrow: Out << "?C"; break;
655 // <operator-name> ::= ?D # *
656 case OO_Star: Out << "?D"; break;
657 // <operator-name> ::= ?E # ++
658 case OO_PlusPlus: Out << "?E"; break;
659 // <operator-name> ::= ?F # --
660 case OO_MinusMinus: Out << "?F"; break;
661 // <operator-name> ::= ?G # -
662 case OO_Minus: Out << "?G"; break;
663 // <operator-name> ::= ?H # +
664 case OO_Plus: Out << "?H"; break;
665 // <operator-name> ::= ?I # &
666 case OO_Amp: Out << "?I"; break;
667 // <operator-name> ::= ?J # ->*
668 case OO_ArrowStar: Out << "?J"; break;
669 // <operator-name> ::= ?K # /
670 case OO_Slash: Out << "?K"; break;
671 // <operator-name> ::= ?L # %
672 case OO_Percent: Out << "?L"; break;
673 // <operator-name> ::= ?M # <
674 case OO_Less: Out << "?M"; break;
675 // <operator-name> ::= ?N # <=
676 case OO_LessEqual: Out << "?N"; break;
677 // <operator-name> ::= ?O # >
678 case OO_Greater: Out << "?O"; break;
679 // <operator-name> ::= ?P # >=
680 case OO_GreaterEqual: Out << "?P"; break;
681 // <operator-name> ::= ?Q # ,
682 case OO_Comma: Out << "?Q"; break;
683 // <operator-name> ::= ?R # ()
684 case OO_Call: Out << "?R"; break;
685 // <operator-name> ::= ?S # ~
686 case OO_Tilde: Out << "?S"; break;
687 // <operator-name> ::= ?T # ^
688 case OO_Caret: Out << "?T"; break;
689 // <operator-name> ::= ?U # |
690 case OO_Pipe: Out << "?U"; break;
691 // <operator-name> ::= ?V # &&
692 case OO_AmpAmp: Out << "?V"; break;
693 // <operator-name> ::= ?W # ||
694 case OO_PipePipe: Out << "?W"; break;
695 // <operator-name> ::= ?X # *=
696 case OO_StarEqual: Out << "?X"; break;
697 // <operator-name> ::= ?Y # +=
698 case OO_PlusEqual: Out << "?Y"; break;
699 // <operator-name> ::= ?Z # -=
700 case OO_MinusEqual: Out << "?Z"; break;
701 // <operator-name> ::= ?_0 # /=
702 case OO_SlashEqual: Out << "?_0"; break;
703 // <operator-name> ::= ?_1 # %=
704 case OO_PercentEqual: Out << "?_1"; break;
705 // <operator-name> ::= ?_2 # >>=
706 case OO_GreaterGreaterEqual: Out << "?_2"; break;
707 // <operator-name> ::= ?_3 # <<=
708 case OO_LessLessEqual: Out << "?_3"; break;
709 // <operator-name> ::= ?_4 # &=
710 case OO_AmpEqual: Out << "?_4"; break;
711 // <operator-name> ::= ?_5 # |=
712 case OO_PipeEqual: Out << "?_5"; break;
713 // <operator-name> ::= ?_6 # ^=
714 case OO_CaretEqual: Out << "?_6"; break;
715 // ?_7 # vftable
716 // ?_8 # vbtable
717 // ?_9 # vcall
718 // ?_A # typeof
719 // ?_B # local static guard
720 // ?_C # string
721 // ?_D # vbase destructor
722 // ?_E # vector deleting destructor
723 // ?_F # default constructor closure
724 // ?_G # scalar deleting destructor
725 // ?_H # vector constructor iterator
726 // ?_I # vector destructor iterator
727 // ?_J # vector vbase constructor iterator
728 // ?_K # virtual displacement map
729 // ?_L # eh vector constructor iterator
730 // ?_M # eh vector destructor iterator
731 // ?_N # eh vector vbase constructor iterator
732 // ?_O # copy constructor closure
733 // ?_P<name> # udt returning <name>
734 // ?_Q # <unknown>
735 // ?_R0 # RTTI Type Descriptor
736 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
737 // ?_R2 # RTTI Base Class Array
738 // ?_R3 # RTTI Class Hierarchy Descriptor
739 // ?_R4 # RTTI Complete Object Locator
740 // ?_S # local vftable
741 // ?_T # local vftable constructor closure
742 // <operator-name> ::= ?_U # new[]
743 case OO_Array_New: Out << "?_U"; break;
744 // <operator-name> ::= ?_V # delete[]
745 case OO_Array_Delete: Out << "?_V"; break;
746
747 case OO_Conditional: {
748 DiagnosticsEngine &Diags = Context.getDiags();
749 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
750 "cannot mangle this conditional operator yet");
751 Diags.Report(Loc, DiagID);
752 break;
753 }
754
755 case OO_None:
756 case NUM_OVERLOADED_OPERATORS:
757 llvm_unreachable("Not an overloaded operator");
758 }
759}
760
761void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
762 // <source name> ::= <identifier> @
763 std::string key = II->getNameStart();
764 BackRefMap::iterator Found;
765 if (UseNameBackReferences)
766 Found = NameBackReferences.find(key);
767 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
768 Out << II->getName() << '@';
769 if (UseNameBackReferences && NameBackReferences.size() < 10) {
770 size_t Size = NameBackReferences.size();
771 NameBackReferences[key] = Size;
772 }
773 } else {
774 Out << Found->second;
775 }
776}
777
778void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
779 Context.mangleObjCMethodName(MD, Out);
780}
781
782// Find out how many function decls live above this one and return an integer
783// suitable for use as the number in a numbered anonymous scope.
784// TODO: Memoize.
785static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
786 const DeclContext *DC = FD->getParent();
787 int level = 1;
788
789 while (DC && !DC->isTranslationUnit()) {
790 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
791 DC = DC->getParent();
792 }
793
794 return 2*level;
795}
796
797void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
798 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
799 // <numbered-anonymous-scope> ::= ? <number>
800 // Even though the name is rendered in reverse order (e.g.
801 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
802 // innermost. So a method bar in class C local to function foo gets mangled
803 // as something like:
804 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
805 // This is more apparent when you have a type nested inside a method of a
806 // type nested inside a function. A method baz in class D local to method
807 // bar of class C local to function foo gets mangled as:
808 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
809 // This scheme is general enough to support GCC-style nested
810 // functions. You could have a method baz of class C inside a function bar
811 // inside a function foo, like so:
812 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
813 int NestLevel = getLocalNestingLevel(FD);
814 Out << '?';
815 mangleNumber(NestLevel);
816 Out << '?';
817 mangle(FD, "?");
818}
819
820void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
821 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000822 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000823 // <template-name> ::= <unscoped-template-name> <template-args>
824 // ::= <substitution>
825 // Always start with the unqualified name.
826
827 // Templates have their own context for back references.
828 ArgBackRefMap OuterArgsContext;
829 BackRefMap OuterTemplateContext;
830 NameBackReferences.swap(OuterTemplateContext);
831 TypeBackReferences.swap(OuterArgsContext);
832
833 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000834 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000835
836 // Restore the previous back reference contexts.
837 NameBackReferences.swap(OuterTemplateContext);
838 TypeBackReferences.swap(OuterArgsContext);
839}
840
841void
842MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
843 // <unscoped-template-name> ::= ?$ <unqualified-name>
844 Out << "?$";
845 mangleUnqualifiedName(TD);
846}
847
848void
849MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
850 bool IsBoolean) {
851 // <integer-literal> ::= $0 <number>
852 Out << "$0";
853 // Make sure booleans are encoded as 0/1.
854 if (IsBoolean && Value.getBoolValue())
855 mangleNumber(1);
856 else
857 mangleNumber(Value);
858}
859
860void
861MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
862 // See if this is a constant expression.
863 llvm::APSInt Value;
864 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
865 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
866 return;
867 }
868
David Majnemerc80eb462013-08-13 06:32:20 +0000869 const CXXUuidofExpr *UE = 0;
870 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
871 if (UO->getOpcode() == UO_AddrOf)
872 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
873 } else
874 UE = dyn_cast<CXXUuidofExpr>(E);
875
876 if (UE) {
877 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
878 // const __s_GUID _GUID_{lower case UUID with underscores}
879 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
880 std::string Name = "_GUID_" + Uuid.lower();
881 std::replace(Name.begin(), Name.end(), '-', '_');
882
David Majnemer26314e12013-08-13 09:17:25 +0000883 // If we had to peek through an address-of operator, treat this like we are
David Majnemerc80eb462013-08-13 06:32:20 +0000884 // dealing with a pointer type. Otherwise, treat it like a const reference.
885 //
886 // N.B. This matches up with the handling of TemplateArgument::Declaration
887 // in mangleTemplateArg
888 if (UE == E)
889 Out << "$E?";
890 else
891 Out << "$1?";
892 Out << Name << "@@3U__s_GUID@@B";
893 return;
894 }
895
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000896 // As bad as this diagnostic is, it's better than crashing.
897 DiagnosticsEngine &Diags = Context.getDiags();
898 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
899 "cannot yet mangle expression type %0");
900 Diags.Report(E->getExprLoc(), DiagID)
901 << E->getStmtClassName() << E->getSourceRange();
902}
903
904void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000905MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
906 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000907 // <template-args> ::= {<type> | <integer-literal>}+ @
908 unsigned NumTemplateArgs = TemplateArgs.size();
909 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000910 const TemplateArgument &TA = TemplateArgs[i];
David Majnemer309f6452013-08-27 08:21:25 +0000911 mangleTemplateArg(TD, TA);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000912 }
913 Out << '@';
914}
915
Reid Kleckner5d90d182013-07-02 18:10:07 +0000916void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
David Majnemer309f6452013-08-27 08:21:25 +0000917 const TemplateArgument &TA) {
Reid Kleckner5d90d182013-07-02 18:10:07 +0000918 switch (TA.getKind()) {
919 case TemplateArgument::Null:
920 llvm_unreachable("Can't mangle null template arguments!");
David Majnemer309f6452013-08-27 08:21:25 +0000921 case TemplateArgument::TemplateExpansion:
922 llvm_unreachable("Can't mangle template expansion arguments!");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000923 case TemplateArgument::Type: {
924 QualType T = TA.getAsType();
925 mangleType(T, SourceRange(), QMM_Escape);
926 break;
927 }
David Majnemerf2081f62013-08-13 01:25:35 +0000928 case TemplateArgument::Declaration: {
929 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl());
930 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000931 break;
David Majnemerf2081f62013-08-13 01:25:35 +0000932 }
Reid Kleckner5d90d182013-07-02 18:10:07 +0000933 case TemplateArgument::Integral:
934 mangleIntegerLiteral(TA.getAsIntegral(),
935 TA.getIntegralType()->isBooleanType());
936 break;
David Majnemer7802fc92013-08-05 21:33:59 +0000937 case TemplateArgument::NullPtr:
938 Out << "$0A@";
939 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000940 case TemplateArgument::Expression:
941 mangleExpression(TA.getAsExpr());
942 break;
943 case TemplateArgument::Pack:
944 // Unlike Itanium, there is no character code to indicate an argument pack.
Reid Kleckner5d90d182013-07-02 18:10:07 +0000945 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
946 I != E; ++I)
David Majnemer309f6452013-08-27 08:21:25 +0000947 mangleTemplateArg(TD, *I);
Reid Kleckner5d90d182013-07-02 18:10:07 +0000948 break;
949 case TemplateArgument::Template:
David Majnemer02c44f02013-08-05 22:26:46 +0000950 mangleType(cast<TagDecl>(
951 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
952 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000953 }
954}
955
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000956void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
957 bool IsMember) {
958 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
959 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
960 // 'I' means __restrict (32/64-bit).
961 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
962 // keyword!
963 // <base-cvr-qualifiers> ::= A # near
964 // ::= B # near const
965 // ::= C # near volatile
966 // ::= D # near const volatile
967 // ::= E # far (16-bit)
968 // ::= F # far const (16-bit)
969 // ::= G # far volatile (16-bit)
970 // ::= H # far const volatile (16-bit)
971 // ::= I # huge (16-bit)
972 // ::= J # huge const (16-bit)
973 // ::= K # huge volatile (16-bit)
974 // ::= L # huge const volatile (16-bit)
975 // ::= M <basis> # based
976 // ::= N <basis> # based const
977 // ::= O <basis> # based volatile
978 // ::= P <basis> # based const volatile
979 // ::= Q # near member
980 // ::= R # near const member
981 // ::= S # near volatile member
982 // ::= T # near const volatile member
983 // ::= U # far member (16-bit)
984 // ::= V # far const member (16-bit)
985 // ::= W # far volatile member (16-bit)
986 // ::= X # far const volatile member (16-bit)
987 // ::= Y # huge member (16-bit)
988 // ::= Z # huge const member (16-bit)
989 // ::= 0 # huge volatile member (16-bit)
990 // ::= 1 # huge const volatile member (16-bit)
991 // ::= 2 <basis> # based member
992 // ::= 3 <basis> # based const member
993 // ::= 4 <basis> # based volatile member
994 // ::= 5 <basis> # based const volatile member
995 // ::= 6 # near function (pointers only)
996 // ::= 7 # far function (pointers only)
997 // ::= 8 # near method (pointers only)
998 // ::= 9 # far method (pointers only)
999 // ::= _A <basis> # based function (pointers only)
1000 // ::= _B <basis> # based function (far?) (pointers only)
1001 // ::= _C <basis> # based method (pointers only)
1002 // ::= _D <basis> # based method (far?) (pointers only)
1003 // ::= _E # block (Clang)
1004 // <basis> ::= 0 # __based(void)
1005 // ::= 1 # __based(segment)?
1006 // ::= 2 <name> # __based(name)
1007 // ::= 3 # ?
1008 // ::= 4 # ?
1009 // ::= 5 # not really based
1010 bool HasConst = Quals.hasConst(),
1011 HasVolatile = Quals.hasVolatile();
David Majnemerc0e64f32013-08-05 22:43:06 +00001012
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001013 if (!IsMember) {
1014 if (HasConst && HasVolatile) {
1015 Out << 'D';
1016 } else if (HasVolatile) {
1017 Out << 'C';
1018 } else if (HasConst) {
1019 Out << 'B';
1020 } else {
1021 Out << 'A';
1022 }
1023 } else {
1024 if (HasConst && HasVolatile) {
1025 Out << 'T';
1026 } else if (HasVolatile) {
1027 Out << 'S';
1028 } else if (HasConst) {
1029 Out << 'R';
1030 } else {
1031 Out << 'Q';
1032 }
1033 }
1034
1035 // FIXME: For now, just drop all extension qualifiers on the floor.
1036}
1037
1038void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1039 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1040 // ::= Q # const
1041 // ::= R # volatile
1042 // ::= S # const volatile
1043 bool HasConst = Quals.hasConst(),
1044 HasVolatile = Quals.hasVolatile();
1045 if (HasConst && HasVolatile) {
1046 Out << 'S';
1047 } else if (HasVolatile) {
1048 Out << 'R';
1049 } else if (HasConst) {
1050 Out << 'Q';
1051 } else {
1052 Out << 'P';
1053 }
1054}
1055
1056void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1057 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001058 // MSVC will backreference two canonically equivalent types that have slightly
1059 // different manglings when mangled alone.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001060 void *TypePtr = getASTContext().getCanonicalType(T).getAsOpaquePtr();
1061 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1062
1063 if (Found == TypeBackReferences.end()) {
1064 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1065
Reid Klecknerf21818d2013-06-24 19:21:52 +00001066 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1067 QualType OT = DT->getOriginalType();
1068 if (const ArrayType *AT = getASTContext().getAsArrayType(OT)) {
1069 mangleDecayedArrayType(AT, false);
1070 } else if (const FunctionType *FT = OT->getAs<FunctionType>()) {
1071 Out << "P6";
1072 mangleFunctionType(FT, 0, false, false);
1073 } else {
1074 llvm_unreachable("unexpected decayed type");
1075 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001076 } else {
1077 mangleType(T, Range, QMM_Drop);
1078 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001079
1080 // See if it's worth creating a back reference.
1081 // Only types longer than 1 character are considered
1082 // and only 10 back references slots are available:
1083 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1084 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1085 size_t Size = TypeBackReferences.size();
1086 TypeBackReferences[TypePtr] = Size;
1087 }
1088 } else {
1089 Out << Found->second;
1090 }
1091}
1092
1093void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001094 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001095 // Don't use the canonical types. MSVC includes things like 'const' on
1096 // pointer arguments to function pointers that canonicalization strips away.
1097 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001098 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001099 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1100 // If there were any Quals, getAsArrayType() pushed them onto the array
1101 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001102 if (QMM == QMM_Mangle)
1103 Out << 'A';
1104 else if (QMM == QMM_Escape || QMM == QMM_Result)
1105 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001106 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001107 return;
1108 }
1109
1110 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1111 T->isBlockPointerType();
1112
1113 switch (QMM) {
1114 case QMM_Drop:
1115 break;
1116 case QMM_Mangle:
1117 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1118 Out << '6';
1119 mangleFunctionType(FT, 0, false, false);
1120 return;
1121 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001122 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001123 break;
1124 case QMM_Escape:
1125 if (!IsPointer && Quals) {
1126 Out << "$$C";
1127 mangleQualifiers(Quals, false);
1128 }
1129 break;
1130 case QMM_Result:
1131 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1132 Out << '?';
1133 mangleQualifiers(Quals, false);
1134 }
1135 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001136 }
1137
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001138 // We have to mangle these now, while we still have enough information.
1139 if (IsPointer)
1140 manglePointerQualifiers(Quals);
1141 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001142
1143 switch (ty->getTypeClass()) {
1144#define ABSTRACT_TYPE(CLASS, PARENT)
1145#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1146 case Type::CLASS: \
1147 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1148 return;
1149#define TYPE(CLASS, PARENT) \
1150 case Type::CLASS: \
1151 mangleType(cast<CLASS##Type>(ty), Range); \
1152 break;
1153#include "clang/AST/TypeNodes.def"
1154#undef ABSTRACT_TYPE
1155#undef NON_CANONICAL_TYPE
1156#undef TYPE
1157 }
1158}
1159
1160void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1161 SourceRange Range) {
1162 // <type> ::= <builtin-type>
1163 // <builtin-type> ::= X # void
1164 // ::= C # signed char
1165 // ::= D # char
1166 // ::= E # unsigned char
1167 // ::= F # short
1168 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1169 // ::= H # int
1170 // ::= I # unsigned int
1171 // ::= J # long
1172 // ::= K # unsigned long
1173 // L # <none>
1174 // ::= M # float
1175 // ::= N # double
1176 // ::= O # long double (__float80 is mangled differently)
1177 // ::= _J # long long, __int64
1178 // ::= _K # unsigned long long, __int64
1179 // ::= _L # __int128
1180 // ::= _M # unsigned __int128
1181 // ::= _N # bool
1182 // _O # <array in parameter>
1183 // ::= _T # __float80 (Intel)
1184 // ::= _W # wchar_t
1185 // ::= _Z # __float80 (Digital Mars)
1186 switch (T->getKind()) {
1187 case BuiltinType::Void: Out << 'X'; break;
1188 case BuiltinType::SChar: Out << 'C'; break;
1189 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1190 case BuiltinType::UChar: Out << 'E'; break;
1191 case BuiltinType::Short: Out << 'F'; break;
1192 case BuiltinType::UShort: Out << 'G'; break;
1193 case BuiltinType::Int: Out << 'H'; break;
1194 case BuiltinType::UInt: Out << 'I'; break;
1195 case BuiltinType::Long: Out << 'J'; break;
1196 case BuiltinType::ULong: Out << 'K'; break;
1197 case BuiltinType::Float: Out << 'M'; break;
1198 case BuiltinType::Double: Out << 'N'; break;
1199 // TODO: Determine size and mangle accordingly
1200 case BuiltinType::LongDouble: Out << 'O'; break;
1201 case BuiltinType::LongLong: Out << "_J"; break;
1202 case BuiltinType::ULongLong: Out << "_K"; break;
1203 case BuiltinType::Int128: Out << "_L"; break;
1204 case BuiltinType::UInt128: Out << "_M"; break;
1205 case BuiltinType::Bool: Out << "_N"; break;
1206 case BuiltinType::WChar_S:
1207 case BuiltinType::WChar_U: Out << "_W"; break;
1208
1209#define BUILTIN_TYPE(Id, SingletonId)
1210#define PLACEHOLDER_TYPE(Id, SingletonId) \
1211 case BuiltinType::Id:
1212#include "clang/AST/BuiltinTypes.def"
1213 case BuiltinType::Dependent:
1214 llvm_unreachable("placeholder types shouldn't get to name mangling");
1215
1216 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1217 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1218 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001219
1220 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1221 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1222 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1223 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1224 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1225 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001226 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001227 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001228
1229 case BuiltinType::NullPtr: Out << "$$T"; break;
1230
1231 case BuiltinType::Char16:
1232 case BuiltinType::Char32:
1233 case BuiltinType::Half: {
1234 DiagnosticsEngine &Diags = Context.getDiags();
1235 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1236 "cannot mangle this built-in %0 type yet");
1237 Diags.Report(Range.getBegin(), DiagID)
1238 << T->getName(Context.getASTContext().getPrintingPolicy())
1239 << Range;
1240 break;
1241 }
1242 }
1243}
1244
1245// <type> ::= <function-type>
1246void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1247 SourceRange) {
1248 // Structors only appear in decls, so at this point we know it's not a
1249 // structor type.
1250 // FIXME: This may not be lambda-friendly.
1251 Out << "$$A6";
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001252 mangleFunctionType(T, NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001253}
1254void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1255 SourceRange) {
1256 llvm_unreachable("Can't mangle K&R function prototypes");
1257}
1258
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001259void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1260 const FunctionDecl *D,
1261 bool IsStructor,
1262 bool IsInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001263 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1264 // <return-type> <argument-list> <throw-spec>
1265 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1266
Reid Klecknerf21818d2013-06-24 19:21:52 +00001267 SourceRange Range;
1268 if (D) Range = D->getSourceRange();
1269
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001270 // If this is a C++ instance method, mangle the CVR qualifiers for the
1271 // this pointer.
David Majnemer1c7a4092013-08-15 08:13:23 +00001272 if (IsInstMethod) {
1273 if (PointersAre64Bit)
1274 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001275 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
David Majnemer1c7a4092013-08-15 08:13:23 +00001276 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001277
1278 mangleCallingConvention(T, IsInstMethod);
1279
1280 // <return-type> ::= <type>
1281 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001282 if (IsStructor) {
1283 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1284 StructorType == Dtor_Deleting) {
1285 // The scalar deleting destructor takes an extra int argument.
1286 // However, the FunctionType generated has 0 arguments.
1287 // FIXME: This is a temporary hack.
1288 // Maybe should fix the FunctionType creation instead?
Timur Iskhodzhanov4b104062013-08-26 10:32:04 +00001289 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001290 return;
1291 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001292 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001293 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001294 QualType ResultType = Proto->getResultType();
1295 if (ResultType->isVoidType())
1296 ResultType = ResultType.getUnqualifiedType();
1297 mangleType(ResultType, Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001298 }
1299
1300 // <argument-list> ::= X # void
1301 // ::= <type>+ @
1302 // ::= <type>* Z # varargs
1303 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1304 Out << 'X';
1305 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001306 // Happens for function pointer type arguments for example.
1307 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1308 ArgEnd = Proto->arg_type_end();
1309 Arg != ArgEnd; ++Arg)
1310 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001311 // <builtin-type> ::= Z # ellipsis
1312 if (Proto->isVariadic())
1313 Out << 'Z';
1314 else
1315 Out << '@';
1316 }
1317
1318 mangleThrowSpecification(Proto);
1319}
1320
1321void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001322 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1323 // # pointer. in 64-bit mode *all*
1324 // # 'this' pointers are 64-bit.
1325 // ::= <global-function>
1326 // <member-function> ::= A # private: near
1327 // ::= B # private: far
1328 // ::= C # private: static near
1329 // ::= D # private: static far
1330 // ::= E # private: virtual near
1331 // ::= F # private: virtual far
1332 // ::= G # private: thunk near
1333 // ::= H # private: thunk far
1334 // ::= I # protected: near
1335 // ::= J # protected: far
1336 // ::= K # protected: static near
1337 // ::= L # protected: static far
1338 // ::= M # protected: virtual near
1339 // ::= N # protected: virtual far
1340 // ::= O # protected: thunk near
1341 // ::= P # protected: thunk far
1342 // ::= Q # public: near
1343 // ::= R # public: far
1344 // ::= S # public: static near
1345 // ::= T # public: static far
1346 // ::= U # public: virtual near
1347 // ::= V # public: virtual far
1348 // ::= W # public: thunk near
1349 // ::= X # public: thunk far
1350 // <global-function> ::= Y # global near
1351 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001352 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1353 switch (MD->getAccess()) {
1354 default:
1355 case AS_private:
1356 if (MD->isStatic())
1357 Out << 'C';
1358 else if (MD->isVirtual())
1359 Out << 'E';
1360 else
1361 Out << 'A';
1362 break;
1363 case AS_protected:
1364 if (MD->isStatic())
1365 Out << 'K';
1366 else if (MD->isVirtual())
1367 Out << 'M';
1368 else
1369 Out << 'I';
1370 break;
1371 case AS_public:
1372 if (MD->isStatic())
1373 Out << 'S';
1374 else if (MD->isVirtual())
1375 Out << 'U';
1376 else
1377 Out << 'Q';
1378 }
1379 } else
1380 Out << 'Y';
1381}
1382void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
1383 bool IsInstMethod) {
1384 // <calling-convention> ::= A # __cdecl
1385 // ::= B # __export __cdecl
1386 // ::= C # __pascal
1387 // ::= D # __export __pascal
1388 // ::= E # __thiscall
1389 // ::= F # __export __thiscall
1390 // ::= G # __stdcall
1391 // ::= H # __export __stdcall
1392 // ::= I # __fastcall
1393 // ::= J # __export __fastcall
1394 // The 'export' calling conventions are from a bygone era
1395 // (*cough*Win16*cough*) when functions were declared for export with
1396 // that keyword. (It didn't actually export them, it just made them so
1397 // that they could be in a DLL and somebody from another module could call
1398 // them.)
1399 CallingConv CC = T->getCallConv();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001400 switch (CC) {
1401 default:
1402 llvm_unreachable("Unsupported CC for mangling");
Charles Davise8519c32013-08-30 04:39:01 +00001403 case CC_X86_64Win64:
1404 case CC_X86_64SysV:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001405 case CC_C: Out << 'A'; break;
1406 case CC_X86Pascal: Out << 'C'; break;
1407 case CC_X86ThisCall: Out << 'E'; break;
1408 case CC_X86StdCall: Out << 'G'; break;
1409 case CC_X86FastCall: Out << 'I'; break;
1410 }
1411}
1412void MicrosoftCXXNameMangler::mangleThrowSpecification(
1413 const FunctionProtoType *FT) {
1414 // <throw-spec> ::= Z # throw(...) (default)
1415 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1416 // ::= <type>+
1417 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1418 // all actually mangled as 'Z'. (They're ignored because their associated
1419 // functionality isn't implemented, and probably never will be.)
1420 Out << 'Z';
1421}
1422
1423void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1424 SourceRange Range) {
1425 // Probably should be mangled as a template instantiation; need to see what
1426 // VC does first.
1427 DiagnosticsEngine &Diags = Context.getDiags();
1428 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1429 "cannot mangle this unresolved dependent type yet");
1430 Diags.Report(Range.getBegin(), DiagID)
1431 << Range;
1432}
1433
1434// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1435// <union-type> ::= T <name>
1436// <struct-type> ::= U <name>
1437// <class-type> ::= V <name>
1438// <enum-type> ::= W <size> <name>
1439void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001440 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001441}
1442void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001443 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001444}
David Majnemer02c44f02013-08-05 22:26:46 +00001445void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1446 switch (TD->getTagKind()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001447 case TTK_Union:
1448 Out << 'T';
1449 break;
1450 case TTK_Struct:
1451 case TTK_Interface:
1452 Out << 'U';
1453 break;
1454 case TTK_Class:
1455 Out << 'V';
1456 break;
1457 case TTK_Enum:
1458 Out << 'W';
1459 Out << getASTContext().getTypeSizeInChars(
David Majnemer02c44f02013-08-05 22:26:46 +00001460 cast<EnumDecl>(TD)->getIntegerType()).getQuantity();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001461 break;
1462 }
David Majnemer02c44f02013-08-05 22:26:46 +00001463 mangleName(TD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001464}
1465
1466// <type> ::= <array-type>
1467// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1468// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001469// <element-type> # as global, E is never required
1470// ::= Q E? <cvr-qualifiers> [Y <dimension-count> <dimension>+]
1471// <element-type> # as param, E is required for 64-bit
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001472// It's supposed to be the other way around, but for some strange reason, it
1473// isn't. Today this behavior is retained for the sole purpose of backwards
1474// compatibility.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001475void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T,
1476 bool IsGlobal) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001477 // This isn't a recursive mangling, so now we have to do it all in this
1478 // one call.
1479 if (IsGlobal) {
1480 manglePointerQualifiers(T->getElementType().getQualifiers());
1481 } else {
1482 Out << 'Q';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001483 if (PointersAre64Bit)
1484 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001485 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001486 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001487}
1488void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1489 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001490 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001491}
1492void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1493 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001494 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001495}
1496void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1497 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001498 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001499}
1500void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1501 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001502 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001503}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001504void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001505 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001506 SmallVector<llvm::APInt, 3> Dimensions;
1507 for (;;) {
1508 if (const ConstantArrayType *CAT =
1509 getASTContext().getAsConstantArrayType(ElementTy)) {
1510 Dimensions.push_back(CAT->getSize());
1511 ElementTy = CAT->getElementType();
1512 } else if (ElementTy->isVariableArrayType()) {
1513 const VariableArrayType *VAT =
1514 getASTContext().getAsVariableArrayType(ElementTy);
1515 DiagnosticsEngine &Diags = Context.getDiags();
1516 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1517 "cannot mangle this variable-length array yet");
1518 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1519 << VAT->getBracketsRange();
1520 return;
1521 } else if (ElementTy->isDependentSizedArrayType()) {
1522 // The dependent expression has to be folded into a constant (TODO).
1523 const DependentSizedArrayType *DSAT =
1524 getASTContext().getAsDependentSizedArrayType(ElementTy);
1525 DiagnosticsEngine &Diags = Context.getDiags();
1526 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1527 "cannot mangle this dependent-length array yet");
1528 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1529 << DSAT->getBracketsRange();
1530 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001531 } else if (const IncompleteArrayType *IAT =
1532 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1533 Dimensions.push_back(llvm::APInt(32, 0));
1534 ElementTy = IAT->getElementType();
1535 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001536 else break;
1537 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001538 Out << 'Y';
1539 // <dimension-count> ::= <number> # number of extra dimensions
1540 mangleNumber(Dimensions.size());
1541 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1542 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001543 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001544}
1545
1546// <type> ::= <pointer-to-member-type>
1547// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1548// <class name> <type>
1549void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1550 SourceRange Range) {
1551 QualType PointeeType = T->getPointeeType();
1552 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1553 Out << '8';
1554 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001555 mangleFunctionType(FPT, NULL, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001556 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001557 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1558 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001559 mangleQualifiers(PointeeType.getQualifiers(), true);
1560 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001561 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001562 }
1563}
1564
1565void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1566 SourceRange Range) {
1567 DiagnosticsEngine &Diags = Context.getDiags();
1568 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1569 "cannot mangle this template type parameter type yet");
1570 Diags.Report(Range.getBegin(), DiagID)
1571 << Range;
1572}
1573
1574void MicrosoftCXXNameMangler::mangleType(
1575 const SubstTemplateTypeParmPackType *T,
1576 SourceRange Range) {
1577 DiagnosticsEngine &Diags = Context.getDiags();
1578 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1579 "cannot mangle this substituted parameter pack yet");
1580 Diags.Report(Range.getBegin(), DiagID)
1581 << Range;
1582}
1583
1584// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001585// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1586// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001587void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1588 SourceRange Range) {
1589 QualType PointeeTy = T->getPointeeType();
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001590 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1591 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001592 mangleType(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001593}
1594void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1595 SourceRange Range) {
1596 // Object pointers never have qualifiers.
1597 Out << 'A';
David Majnemer1c7a4092013-08-15 08:13:23 +00001598 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1599 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001600 mangleType(T->getPointeeType(), Range);
1601}
1602
1603// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001604// <reference-type> ::= A E? <cvr-qualifiers> <type>
1605// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001606void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1607 SourceRange Range) {
1608 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001609 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1610 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001611 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001612}
1613
1614// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001615// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1616// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001617void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1618 SourceRange Range) {
1619 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001620 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1621 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001622 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001623}
1624
1625void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1626 SourceRange Range) {
1627 DiagnosticsEngine &Diags = Context.getDiags();
1628 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1629 "cannot mangle this complex number type yet");
1630 Diags.Report(Range.getBegin(), DiagID)
1631 << Range;
1632}
1633
1634void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1635 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001636 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1637 assert(ET && "vectors with non-builtin elements are unsupported");
1638 uint64_t Width = getASTContext().getTypeSize(T);
1639 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1640 // doesn't match the Intel types uses a custom mangling below.
1641 bool IntelVector = true;
1642 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1643 Out << "T__m64";
1644 } else if (Width == 128 || Width == 256) {
1645 if (ET->getKind() == BuiltinType::Float)
1646 Out << "T__m" << Width;
1647 else if (ET->getKind() == BuiltinType::LongLong)
1648 Out << "T__m" << Width << 'i';
1649 else if (ET->getKind() == BuiltinType::Double)
1650 Out << "U__m" << Width << 'd';
1651 else
1652 IntelVector = false;
1653 } else {
1654 IntelVector = false;
1655 }
1656
1657 if (!IntelVector) {
1658 // The MS ABI doesn't have a special mangling for vector types, so we define
1659 // our own mangling to handle uses of __vector_size__ on user-specified
1660 // types, and for extensions like __v4sf.
1661 Out << "T__clang_vec" << T->getNumElements() << '_';
1662 mangleType(ET, Range);
1663 }
1664
1665 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001666}
Reid Kleckner1232e272013-03-26 16:56:59 +00001667
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001668void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1669 SourceRange Range) {
1670 DiagnosticsEngine &Diags = Context.getDiags();
1671 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1672 "cannot mangle this extended vector type yet");
1673 Diags.Report(Range.getBegin(), DiagID)
1674 << Range;
1675}
1676void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1677 SourceRange Range) {
1678 DiagnosticsEngine &Diags = Context.getDiags();
1679 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1680 "cannot mangle this dependent-sized extended vector type yet");
1681 Diags.Report(Range.getBegin(), DiagID)
1682 << Range;
1683}
1684
1685void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1686 SourceRange) {
1687 // ObjC interfaces have structs underlying them.
1688 Out << 'U';
1689 mangleName(T->getDecl());
1690}
1691
1692void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1693 SourceRange Range) {
1694 // We don't allow overloading by different protocol qualification,
1695 // so mangling them isn't necessary.
1696 mangleType(T->getBaseType(), Range);
1697}
1698
1699void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1700 SourceRange Range) {
1701 Out << "_E";
1702
1703 QualType pointee = T->getPointeeType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001704 mangleFunctionType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001705}
1706
David Majnemer360d23e2013-08-16 08:29:13 +00001707void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1708 SourceRange) {
1709 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001710}
1711
1712void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1713 SourceRange Range) {
1714 DiagnosticsEngine &Diags = Context.getDiags();
1715 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1716 "cannot mangle this template specialization type yet");
1717 Diags.Report(Range.getBegin(), DiagID)
1718 << Range;
1719}
1720
1721void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1722 SourceRange Range) {
1723 DiagnosticsEngine &Diags = Context.getDiags();
1724 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1725 "cannot mangle this dependent name type yet");
1726 Diags.Report(Range.getBegin(), DiagID)
1727 << Range;
1728}
1729
1730void MicrosoftCXXNameMangler::mangleType(
1731 const DependentTemplateSpecializationType *T,
1732 SourceRange Range) {
1733 DiagnosticsEngine &Diags = Context.getDiags();
1734 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1735 "cannot mangle this dependent template specialization type yet");
1736 Diags.Report(Range.getBegin(), DiagID)
1737 << Range;
1738}
1739
1740void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1741 SourceRange Range) {
1742 DiagnosticsEngine &Diags = Context.getDiags();
1743 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1744 "cannot mangle this pack expansion yet");
1745 Diags.Report(Range.getBegin(), DiagID)
1746 << Range;
1747}
1748
1749void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1750 SourceRange Range) {
1751 DiagnosticsEngine &Diags = Context.getDiags();
1752 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1753 "cannot mangle this typeof(type) yet");
1754 Diags.Report(Range.getBegin(), DiagID)
1755 << Range;
1756}
1757
1758void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1759 SourceRange Range) {
1760 DiagnosticsEngine &Diags = Context.getDiags();
1761 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1762 "cannot mangle this typeof(expression) yet");
1763 Diags.Report(Range.getBegin(), DiagID)
1764 << Range;
1765}
1766
1767void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1768 SourceRange Range) {
1769 DiagnosticsEngine &Diags = Context.getDiags();
1770 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1771 "cannot mangle this decltype() yet");
1772 Diags.Report(Range.getBegin(), DiagID)
1773 << Range;
1774}
1775
1776void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1777 SourceRange Range) {
1778 DiagnosticsEngine &Diags = Context.getDiags();
1779 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1780 "cannot mangle this unary transform type yet");
1781 Diags.Report(Range.getBegin(), DiagID)
1782 << Range;
1783}
1784
1785void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1786 DiagnosticsEngine &Diags = Context.getDiags();
1787 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1788 "cannot mangle this 'auto' type yet");
1789 Diags.Report(Range.getBegin(), DiagID)
1790 << Range;
1791}
1792
1793void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1794 SourceRange Range) {
1795 DiagnosticsEngine &Diags = Context.getDiags();
1796 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1797 "cannot mangle this C11 atomic type yet");
1798 Diags.Report(Range.getBegin(), DiagID)
1799 << Range;
1800}
1801
1802void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1803 raw_ostream &Out) {
1804 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1805 "Invalid mangleName() call, argument is not a variable or function!");
1806 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1807 "Invalid mangleName() call on 'structor decl!");
1808
1809 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1810 getASTContext().getSourceManager(),
1811 "Mangling declaration");
1812
1813 MicrosoftCXXNameMangler Mangler(*this, Out);
1814 return Mangler.mangle(D);
1815}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001816
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001817void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1818 const ThunkInfo &Thunk,
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001819 raw_ostream &Out) {
1820 // FIXME: this is not yet a complete implementation, but merely a
1821 // reasonably-working stub to avoid crashing when required to emit a thunk.
1822 MicrosoftCXXNameMangler Mangler(*this, Out);
1823 Out << "\01?";
1824 Mangler.mangleName(MD);
1825 if (Thunk.This.NonVirtual != 0) {
1826 // FIXME: add support for protected/private or use mangleFunctionClass.
1827 Out << "W";
1828 llvm::APSInt APSNumber(/*BitWidth=*/32 /*FIXME: check on x64*/,
1829 /*isUnsigned=*/true);
1830 APSNumber = -Thunk.This.NonVirtual;
1831 Mangler.mangleNumber(APSNumber);
1832 } else {
1833 // FIXME: add support for protected/private or use mangleFunctionClass.
1834 Out << "Q";
1835 }
1836 // FIXME: mangle return adjustment? Most likely includes using an overridee FPT?
1837 Mangler.mangleFunctionType(MD->getType()->castAs<FunctionProtoType>(), MD, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001838}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001839
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001840void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1841 CXXDtorType Type,
1842 const ThisAdjustment &,
1843 raw_ostream &) {
1844 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1845 "cannot mangle thunk for this destructor yet");
1846 getDiags().Report(DD->getLocation(), DiagID);
1847}
Reid Kleckner90633022013-06-19 15:20:38 +00001848
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001849void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1850 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001851 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1852 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001853 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001854 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001855 MicrosoftCXXNameMangler Mangler(*this, Out);
1856 Mangler.getStream() << "\01??_7";
1857 Mangler.mangleName(RD);
Reid Kleckner90633022013-06-19 15:20:38 +00001858 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001859 // TODO: If the class has more than one vtable, mangle in the class it came
1860 // from.
1861 Mangler.getStream() << '@';
1862}
Reid Kleckner90633022013-06-19 15:20:38 +00001863
1864void MicrosoftMangleContext::mangleCXXVBTable(
1865 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1866 raw_ostream &Out) {
1867 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1868 // <cvr-qualifiers> [<name>] @
1869 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1870 // is always '7' for vbtables.
1871 MicrosoftCXXNameMangler Mangler(*this, Out);
1872 Mangler.getStream() << "\01??_8";
1873 Mangler.mangleName(Derived);
1874 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1875 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1876 E = BasePath.end();
1877 I != E; ++I) {
1878 Mangler.mangleName(*I);
1879 }
1880 Mangler.getStream() << '@';
1881}
1882
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001883void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1884 raw_ostream &) {
1885 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1886}
1887void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1888 int64_t Offset,
1889 const CXXRecordDecl *Type,
1890 raw_ostream &) {
1891 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1892}
1893void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1894 raw_ostream &) {
1895 // FIXME: Give a location...
1896 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1897 "cannot mangle RTTI descriptors for type %0 yet");
1898 getDiags().Report(DiagID)
1899 << T.getBaseTypeIdentifier();
1900}
1901void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1902 raw_ostream &) {
1903 // FIXME: Give a location...
1904 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1905 "cannot mangle the name of type %0 into RTTI descriptors yet");
1906 getDiags().Report(DiagID)
1907 << T.getBaseTypeIdentifier();
1908}
1909void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1910 CXXCtorType Type,
1911 raw_ostream & Out) {
1912 MicrosoftCXXNameMangler mangler(*this, Out);
1913 mangler.mangle(D);
1914}
1915void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1916 CXXDtorType Type,
1917 raw_ostream & Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001918 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001919 mangler.mangle(D);
1920}
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001921void MicrosoftMangleContext::mangleReferenceTemporary(const VarDecl *VD,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001922 raw_ostream &) {
1923 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1924 "cannot mangle this reference temporary yet");
1925 getDiags().Report(VD->getLocation(), DiagID);
1926}
1927
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001928void MicrosoftMangleContext::mangleStaticGuardVariable(const VarDecl *VD,
1929 raw_ostream &Out) {
1930 // <guard-name> ::= ?_B <postfix> @51
1931 // ::= ?$S <guard-num> @ <postfix> @4IA
1932
1933 // The first mangling is what MSVC uses to guard static locals in inline
1934 // functions. It uses a different mangling in external functions to support
1935 // guarding more than 32 variables. MSVC rejects inline functions with more
1936 // than 32 static locals. We don't fully implement the second mangling
1937 // because those guards are not externally visible, and instead use LLVM's
1938 // default renaming when creating a new guard variable.
1939 MicrosoftCXXNameMangler Mangler(*this, Out);
1940
1941 bool Visible = VD->isExternallyVisible();
1942 // <operator-name> ::= ?_B # local static guard
1943 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@");
1944 Mangler.manglePostfix(VD->getDeclContext());
1945 Mangler.getStream() << (Visible ? "@51" : "@4IA");
1946}
1947
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00001948void MicrosoftMangleContext::mangleInitFiniStub(const VarDecl *D,
1949 raw_ostream &Out,
1950 char CharCode) {
1951 MicrosoftCXXNameMangler Mangler(*this, Out);
1952 Mangler.getStream() << "\01??__" << CharCode;
1953 Mangler.mangleName(D);
1954 // This is the function class mangling. These stubs are global, non-variadic,
1955 // cdecl functions that return void and take no args.
1956 Mangler.getStream() << "YAXXZ";
1957}
1958
1959void MicrosoftMangleContext::mangleDynamicInitializer(const VarDecl *D,
1960 raw_ostream &Out) {
1961 // <initializer-name> ::= ?__E <name> YAXXZ
1962 mangleInitFiniStub(D, Out, 'E');
1963}
1964
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001965void MicrosoftMangleContext::mangleDynamicAtExitDestructor(const VarDecl *D,
1966 raw_ostream &Out) {
Reid Klecknerc5c6fa72013-09-10 20:43:12 +00001967 // <destructor-name> ::= ?__F <name> YAXXZ
1968 mangleInitFiniStub(D, Out, 'F');
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001969}
1970
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001971MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
1972 DiagnosticsEngine &Diags) {
1973 return new MicrosoftMangleContext(Context, Diags);
1974}