blob: e360a934083c304ce2619f6608c3d1381aebb3ba [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#include <map>
28
29using namespace clang;
30
31namespace {
32
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000033static const FunctionDecl *getStructor(const FunctionDecl *fn) {
34 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
35 return ftd->getTemplatedDecl();
36
37 return fn;
38}
39
Guy Benyei7f92f2d2012-12-18 14:30:41 +000040/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
41/// Microsoft Visual C++ ABI.
42class MicrosoftCXXNameMangler {
43 MangleContext &Context;
44 raw_ostream &Out;
45
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000046 /// The "structor" is the top-level declaration being mangled, if
47 /// that's not a template specialization; otherwise it's the pattern
48 /// for that specialization.
49 const NamedDecl *Structor;
50 unsigned StructorType;
51
Reid Klecknerf0219cd2013-05-22 17:16:39 +000052 typedef llvm::StringMap<unsigned> BackRefMap;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000053 BackRefMap NameBackReferences;
54 bool UseNameBackReferences;
55
56 typedef llvm::DenseMap<void*, unsigned> ArgBackRefMap;
57 ArgBackRefMap TypeBackReferences;
58
59 ASTContext &getASTContext() const { return Context.getASTContext(); }
60
Reid Klecknerd6a08d12013-05-14 20:30:42 +000061 // FIXME: If we add support for __ptr32/64 qualifiers, then we should push
62 // this check into mangleQualifiers().
63 const bool PointersAre64Bit;
64
Guy Benyei7f92f2d2012-12-18 14:30:41 +000065public:
Peter Collingbourneb70d1c32013-04-25 04:25:40 +000066 enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result };
67
Guy Benyei7f92f2d2012-12-18 14:30:41 +000068 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000069 : Context(C), Out(Out_),
70 Structor(0), StructorType(-1),
David Blaikiee7e94c92013-05-14 21:31:46 +000071 UseNameBackReferences(true),
Reid Klecknerd6a08d12013-05-14 20:30:42 +000072 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikiee7e94c92013-05-14 21:31:46 +000073 64) { }
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000074
75 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_,
76 const CXXDestructorDecl *D, CXXDtorType Type)
77 : Context(C), Out(Out_),
78 Structor(getStructor(D)), StructorType(Type),
David Blaikiee7e94c92013-05-14 21:31:46 +000079 UseNameBackReferences(true),
Reid Klecknerd6a08d12013-05-14 20:30:42 +000080 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikiee7e94c92013-05-14 21:31:46 +000081 64) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +000082
83 raw_ostream &getStream() const { return Out; }
84
85 void mangle(const NamedDecl *D, StringRef Prefix = "\01?");
86 void mangleName(const NamedDecl *ND);
87 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);
Guy Benyei7f92f2d2012-12-18 14:30:41 +000093
94private:
95 void disableBackReferences() { UseNameBackReferences = false; }
96 void mangleUnqualifiedName(const NamedDecl *ND) {
97 mangleUnqualifiedName(ND, ND->getDeclName());
98 }
99 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
100 void mangleSourceName(const IdentifierInfo *II);
101 void manglePostfix(const DeclContext *DC, bool NoFunction=false);
102 void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc);
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000103 void mangleCXXDtorType(CXXDtorType T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000104 void mangleQualifiers(Qualifiers Quals, bool IsMember);
105 void manglePointerQualifiers(Qualifiers Quals);
106
107 void mangleUnscopedTemplateName(const TemplateDecl *ND);
108 void mangleTemplateInstantiationName(const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000109 const TemplateArgumentList &TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000110 void mangleObjCMethodName(const ObjCMethodDecl *MD);
111 void mangleLocalName(const FunctionDecl *FD);
112
113 void mangleArgumentType(QualType T, SourceRange Range);
114
115 // Declare manglers for every type class.
116#define ABSTRACT_TYPE(CLASS, PARENT)
117#define NON_CANONICAL_TYPE(CLASS, PARENT)
118#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \
119 SourceRange Range);
120#include "clang/AST/TypeNodes.def"
121#undef ABSTRACT_TYPE
122#undef NON_CANONICAL_TYPE
123#undef TYPE
124
125 void mangleType(const TagType*);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000126 void mangleFunctionType(const FunctionType *T, const FunctionDecl *D,
127 bool IsStructor, bool IsInstMethod);
128 void mangleDecayedArrayType(const ArrayType *T, bool IsGlobal);
129 void mangleArrayType(const ArrayType *T, Qualifiers Quals);
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);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000138
139};
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 &);
159 virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
160 const CXXRecordDecl *Type,
161 raw_ostream &);
162 virtual void mangleCXXRTTI(QualType T, raw_ostream &);
163 virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
164 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
165 raw_ostream &);
166 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
167 raw_ostream &);
168 virtual void mangleReferenceTemporary(const clang::VarDecl *,
169 raw_ostream &);
170};
171
172}
173
174static bool isInCLinkageSpecification(const Decl *D) {
175 D = D->getCanonicalDecl();
176 for (const DeclContext *DC = D->getDeclContext();
177 !DC->isTranslationUnit(); DC = DC->getParent()) {
178 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
179 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
180 }
181
182 return false;
183}
184
185bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
186 // In C, functions with no attributes never need to be mangled. Fastpath them.
187 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
188 return false;
189
190 // Any decl can be declared with __asm("foo") on it, and this takes precedence
191 // over all other naming in the .o file.
192 if (D->hasAttr<AsmLabelAttr>())
193 return true;
194
195 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
196 // (always) as does passing a C++ member function and a function
197 // whose name is not a simple identifier.
198 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
199 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
200 !FD->getDeclName().isIdentifier()))
201 return true;
202
203 // Otherwise, no mangling is done outside C++ mode.
204 if (!getASTContext().getLangOpts().CPlusPlus)
205 return false;
206
207 // Variables at global scope with internal linkage are not mangled.
208 if (!FD) {
209 const DeclContext *DC = D->getDeclContext();
Rafael Espindola181e3ec2013-05-13 00:12:11 +0000210 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000211 return false;
212 }
213
214 // C functions and "main" are not mangled.
215 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
216 return false;
217
218 return true;
219}
220
221void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
222 StringRef Prefix) {
223 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
224 // Therefore it's really important that we don't decorate the
225 // name with leading underscores or leading/trailing at signs. So, by
226 // default, we emit an asm marker at the start so we get the name right.
227 // Callers can override this with a custom prefix.
228
229 // Any decl can be declared with __asm("foo") on it, and this takes precedence
230 // over all other naming in the .o file.
231 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
232 // If we have an asm name, then we use it as the mangling.
233 Out << '\01' << ALA->getLabel();
234 return;
235 }
236
237 // <mangled-name> ::= ? <name> <type-encoding>
238 Out << Prefix;
239 mangleName(D);
240 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
241 mangleFunctionEncoding(FD);
242 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
243 mangleVariableEncoding(VD);
244 else {
245 // TODO: Fields? Can MSVC even mangle them?
246 // Issue a diagnostic for now.
247 DiagnosticsEngine &Diags = Context.getDiags();
248 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
249 "cannot mangle this declaration yet");
250 Diags.Report(D->getLocation(), DiagID)
251 << D->getSourceRange();
252 }
253}
254
255void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
256 // <type-encoding> ::= <function-class> <function-type>
257
258 // Don't mangle in the type if this isn't a decl we should typically mangle.
259 if (!Context.shouldMangleDeclName(FD))
260 return;
261
262 // We should never ever see a FunctionNoProtoType at this point.
263 // We don't even know how to mangle their types anyway :).
264 const FunctionProtoType *FT = FD->getType()->castAs<FunctionProtoType>();
265
266 bool InStructor = false, InInstMethod = false;
267 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
268 if (MD) {
269 if (MD->isInstance())
270 InInstMethod = true;
271 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
272 InStructor = true;
273 }
274
275 // First, the function class.
276 mangleFunctionClass(FD);
277
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000278 mangleFunctionType(FT, FD, InStructor, InInstMethod);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000279}
280
281void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
282 // <type-encoding> ::= <storage-class> <variable-type>
283 // <storage-class> ::= 0 # private static member
284 // ::= 1 # protected static member
285 // ::= 2 # public static member
286 // ::= 3 # global
287 // ::= 4 # static local
288
289 // The first character in the encoding (after the name) is the storage class.
290 if (VD->isStaticDataMember()) {
291 // If it's a static member, it also encodes the access level.
292 switch (VD->getAccess()) {
293 default:
294 case AS_private: Out << '0'; break;
295 case AS_protected: Out << '1'; break;
296 case AS_public: Out << '2'; break;
297 }
298 }
299 else if (!VD->isStaticLocal())
300 Out << '3';
301 else
302 Out << '4';
303 // Now mangle the type.
304 // <variable-type> ::= <type> <cvr-qualifiers>
305 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
306 // Pointers and references are odd. The type of 'int * const foo;' gets
307 // mangled as 'QAHA' instead of 'PAHB', for example.
308 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
309 QualType Ty = TL.getType();
310 if (Ty->isPointerType() || Ty->isReferenceType()) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000311 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000312 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
313 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
314 // Global arrays are funny, too.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000315 mangleDecayedArrayType(AT, true);
316 if (AT->getElementType()->isArrayType())
317 Out << 'A';
318 else
319 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000320 } else {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000321 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000322 mangleQualifiers(Ty.getLocalQualifiers(), false);
323 }
324}
325
326void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
327 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
328 const DeclContext *DC = ND->getDeclContext();
329
330 // Always start with the unqualified name.
331 mangleUnqualifiedName(ND);
332
333 // If this is an extern variable declared locally, the relevant DeclContext
334 // is that of the containing namespace, or the translation unit.
335 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
336 while (!DC->isNamespace() && !DC->isTranslationUnit())
337 DC = DC->getParent();
338
339 manglePostfix(DC);
340
341 // Terminate the whole name with an '@'.
342 Out << '@';
343}
344
345void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
346 llvm::APSInt APSNumber(/*BitWidth=*/64, /*isUnsigned=*/false);
347 APSNumber = Number;
348 mangleNumber(APSNumber);
349}
350
351void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
352 // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
353 // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
354 // ::= [?] @ # 0 (alternate mangling, not emitted by VC)
355 if (Value.isSigned() && Value.isNegative()) {
356 Out << '?';
357 mangleNumber(llvm::APSInt(Value.abs()));
358 return;
359 }
360 llvm::APSInt Temp(Value);
361 // There's a special shorter mangling for 0, but Microsoft
362 // chose not to use it. Instead, 0 gets mangled as "A@". Oh well...
363 if (Value.uge(1) && Value.ule(10)) {
364 --Temp;
365 Temp.print(Out, false);
366 } else {
367 // We have to build up the encoding in reverse order, so it will come
368 // out right when we write it out.
369 char Encoding[64];
370 char *EndPtr = Encoding+sizeof(Encoding);
371 char *CurPtr = EndPtr;
372 llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned());
373 NibbleMask = 0xf;
374 do {
375 *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf);
376 Temp = Temp.lshr(4);
377 } while (Temp != 0);
378 Out.write(CurPtr, EndPtr-CurPtr);
379 Out << '@';
380 }
381}
382
383static const TemplateDecl *
Reid Klecknerf16216c2013-03-20 01:40:23 +0000384isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000385 // Check if we have a function template.
386 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
387 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000388 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000389 return TD;
390 }
391 }
392
393 // Check if we have a class template.
394 if (const ClassTemplateSpecializationDecl *Spec =
Reid Klecknerf16216c2013-03-20 01:40:23 +0000395 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
396 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000397 return Spec->getSpecializedTemplate();
398 }
399
400 return 0;
401}
402
403void
404MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
405 DeclarationName Name) {
406 // <unqualified-name> ::= <operator-name>
407 // ::= <ctor-dtor-name>
408 // ::= <source-name>
409 // ::= <template-name>
Reid Klecknerf16216c2013-03-20 01:40:23 +0000410
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000411 // Check if we have a template.
Reid Klecknerf16216c2013-03-20 01:40:23 +0000412 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000413 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
414 // We have a template.
415 // Here comes the tricky thing: if we need to mangle something like
416 // void foo(A::X<Y>, B::X<Y>),
417 // the X<Y> part is aliased. However, if you need to mangle
418 // void foo(A::X<A::Y>, A::X<B::Y>),
419 // the A::X<> part is not aliased.
420 // That said, from the mangler's perspective we have a structure like this:
421 // namespace[s] -> type[ -> template-parameters]
422 // but from the Clang perspective we have
423 // type [ -> template-parameters]
424 // \-> namespace[s]
425 // What we do is we create a new mangler, mangle the same type (without
426 // a namespace suffix) using the extra mangler with back references
427 // disabled (to avoid infinite recursion) and then use the mangled type
428 // name as a key to check the mangling of different types for aliasing.
429
430 std::string BackReferenceKey;
431 BackRefMap::iterator Found;
432 if (UseNameBackReferences) {
433 llvm::raw_string_ostream Stream(BackReferenceKey);
434 MicrosoftCXXNameMangler Extra(Context, Stream);
435 Extra.disableBackReferences();
436 Extra.mangleUnqualifiedName(ND, Name);
437 Stream.flush();
438
439 Found = NameBackReferences.find(BackReferenceKey);
440 }
441 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000442 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000443 if (UseNameBackReferences && NameBackReferences.size() < 10) {
444 size_t Size = NameBackReferences.size();
445 NameBackReferences[BackReferenceKey] = Size;
446 }
447 } else {
448 Out << Found->second;
449 }
450 return;
451 }
452
453 switch (Name.getNameKind()) {
454 case DeclarationName::Identifier: {
455 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
456 mangleSourceName(II);
457 break;
458 }
459
460 // Otherwise, an anonymous entity. We must have a declaration.
461 assert(ND && "mangling empty name without declaration");
462
463 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
464 if (NS->isAnonymousNamespace()) {
465 Out << "?A@";
466 break;
467 }
468 }
469
470 // We must have an anonymous struct.
471 const TagDecl *TD = cast<TagDecl>(ND);
472 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
473 assert(TD->getDeclContext() == D->getDeclContext() &&
474 "Typedef should not be in another decl context!");
475 assert(D->getDeclName().getAsIdentifierInfo() &&
476 "Typedef was not named!");
477 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
478 break;
479 }
480
481 // When VC encounters an anonymous type with no tag and no typedef,
482 // it literally emits '<unnamed-tag>'.
483 Out << "<unnamed-tag>";
484 break;
485 }
486
487 case DeclarationName::ObjCZeroArgSelector:
488 case DeclarationName::ObjCOneArgSelector:
489 case DeclarationName::ObjCMultiArgSelector:
490 llvm_unreachable("Can't mangle Objective-C selector names here!");
491
492 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000493 if (ND == Structor) {
494 assert(StructorType == Ctor_Complete &&
495 "Should never be asked to mangle a ctor other than complete");
496 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000497 Out << "?0";
498 break;
499
500 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000501 if (ND == Structor)
502 // If the named decl is the C++ destructor we're mangling,
503 // use the type we were given.
504 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
505 else
506 // Otherwise, use the complete destructor name. This is relevant if a
507 // class with a destructor is declared within a destructor.
508 mangleCXXDtorType(Dtor_Complete);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000509 break;
510
511 case DeclarationName::CXXConversionFunctionName:
512 // <operator-name> ::= ?B # (cast)
513 // The target type is encoded as the return type.
514 Out << "?B";
515 break;
516
517 case DeclarationName::CXXOperatorName:
518 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
519 break;
520
521 case DeclarationName::CXXLiteralOperatorName: {
522 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
523 DiagnosticsEngine Diags = Context.getDiags();
524 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
525 "cannot mangle this literal operator yet");
526 Diags.Report(ND->getLocation(), DiagID);
527 break;
528 }
529
530 case DeclarationName::CXXUsingDirective:
531 llvm_unreachable("Can't mangle a using directive name!");
532 }
533}
534
535void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
536 bool NoFunction) {
537 // <postfix> ::= <unqualified-name> [<postfix>]
538 // ::= <substitution> [<postfix>]
539
540 if (!DC) return;
541
542 while (isa<LinkageSpecDecl>(DC))
543 DC = DC->getParent();
544
545 if (DC->isTranslationUnit())
546 return;
547
548 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
549 Context.mangleBlock(BD, Out);
550 Out << '@';
551 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000552 } else if (isa<CapturedDecl>(DC)) {
553 // Skip CapturedDecl context.
554 manglePostfix(DC->getParent(), NoFunction);
555 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000556 }
557
558 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
559 return;
560 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
561 mangleObjCMethodName(Method);
562 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
563 mangleLocalName(Func);
564 else {
565 mangleUnqualifiedName(cast<NamedDecl>(DC));
566 manglePostfix(DC->getParent(), NoFunction);
567 }
568}
569
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000570void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
571 switch (T) {
572 case Dtor_Deleting:
573 Out << "?_G";
574 return;
575 case Dtor_Base:
576 // FIXME: We should be asked to mangle base dtors.
577 // However, fixing this would require larger changes to the CodeGenModule.
578 // Please put llvm_unreachable here when CGM is changed.
579 // For now, just mangle a base dtor the same way as a complete dtor...
580 case Dtor_Complete:
581 Out << "?1";
582 return;
583 }
584 llvm_unreachable("Unsupported dtor type?");
585}
586
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000587void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
588 SourceLocation Loc) {
589 switch (OO) {
590 // ?0 # constructor
591 // ?1 # destructor
592 // <operator-name> ::= ?2 # new
593 case OO_New: Out << "?2"; break;
594 // <operator-name> ::= ?3 # delete
595 case OO_Delete: Out << "?3"; break;
596 // <operator-name> ::= ?4 # =
597 case OO_Equal: Out << "?4"; break;
598 // <operator-name> ::= ?5 # >>
599 case OO_GreaterGreater: Out << "?5"; break;
600 // <operator-name> ::= ?6 # <<
601 case OO_LessLess: Out << "?6"; break;
602 // <operator-name> ::= ?7 # !
603 case OO_Exclaim: Out << "?7"; break;
604 // <operator-name> ::= ?8 # ==
605 case OO_EqualEqual: Out << "?8"; break;
606 // <operator-name> ::= ?9 # !=
607 case OO_ExclaimEqual: Out << "?9"; break;
608 // <operator-name> ::= ?A # []
609 case OO_Subscript: Out << "?A"; break;
610 // ?B # conversion
611 // <operator-name> ::= ?C # ->
612 case OO_Arrow: Out << "?C"; break;
613 // <operator-name> ::= ?D # *
614 case OO_Star: Out << "?D"; break;
615 // <operator-name> ::= ?E # ++
616 case OO_PlusPlus: Out << "?E"; break;
617 // <operator-name> ::= ?F # --
618 case OO_MinusMinus: Out << "?F"; break;
619 // <operator-name> ::= ?G # -
620 case OO_Minus: Out << "?G"; break;
621 // <operator-name> ::= ?H # +
622 case OO_Plus: Out << "?H"; break;
623 // <operator-name> ::= ?I # &
624 case OO_Amp: Out << "?I"; break;
625 // <operator-name> ::= ?J # ->*
626 case OO_ArrowStar: Out << "?J"; break;
627 // <operator-name> ::= ?K # /
628 case OO_Slash: Out << "?K"; break;
629 // <operator-name> ::= ?L # %
630 case OO_Percent: Out << "?L"; break;
631 // <operator-name> ::= ?M # <
632 case OO_Less: Out << "?M"; break;
633 // <operator-name> ::= ?N # <=
634 case OO_LessEqual: Out << "?N"; break;
635 // <operator-name> ::= ?O # >
636 case OO_Greater: Out << "?O"; break;
637 // <operator-name> ::= ?P # >=
638 case OO_GreaterEqual: Out << "?P"; break;
639 // <operator-name> ::= ?Q # ,
640 case OO_Comma: Out << "?Q"; break;
641 // <operator-name> ::= ?R # ()
642 case OO_Call: Out << "?R"; break;
643 // <operator-name> ::= ?S # ~
644 case OO_Tilde: Out << "?S"; break;
645 // <operator-name> ::= ?T # ^
646 case OO_Caret: Out << "?T"; break;
647 // <operator-name> ::= ?U # |
648 case OO_Pipe: Out << "?U"; break;
649 // <operator-name> ::= ?V # &&
650 case OO_AmpAmp: Out << "?V"; break;
651 // <operator-name> ::= ?W # ||
652 case OO_PipePipe: Out << "?W"; break;
653 // <operator-name> ::= ?X # *=
654 case OO_StarEqual: Out << "?X"; break;
655 // <operator-name> ::= ?Y # +=
656 case OO_PlusEqual: Out << "?Y"; break;
657 // <operator-name> ::= ?Z # -=
658 case OO_MinusEqual: Out << "?Z"; break;
659 // <operator-name> ::= ?_0 # /=
660 case OO_SlashEqual: Out << "?_0"; break;
661 // <operator-name> ::= ?_1 # %=
662 case OO_PercentEqual: Out << "?_1"; break;
663 // <operator-name> ::= ?_2 # >>=
664 case OO_GreaterGreaterEqual: Out << "?_2"; break;
665 // <operator-name> ::= ?_3 # <<=
666 case OO_LessLessEqual: Out << "?_3"; break;
667 // <operator-name> ::= ?_4 # &=
668 case OO_AmpEqual: Out << "?_4"; break;
669 // <operator-name> ::= ?_5 # |=
670 case OO_PipeEqual: Out << "?_5"; break;
671 // <operator-name> ::= ?_6 # ^=
672 case OO_CaretEqual: Out << "?_6"; break;
673 // ?_7 # vftable
674 // ?_8 # vbtable
675 // ?_9 # vcall
676 // ?_A # typeof
677 // ?_B # local static guard
678 // ?_C # string
679 // ?_D # vbase destructor
680 // ?_E # vector deleting destructor
681 // ?_F # default constructor closure
682 // ?_G # scalar deleting destructor
683 // ?_H # vector constructor iterator
684 // ?_I # vector destructor iterator
685 // ?_J # vector vbase constructor iterator
686 // ?_K # virtual displacement map
687 // ?_L # eh vector constructor iterator
688 // ?_M # eh vector destructor iterator
689 // ?_N # eh vector vbase constructor iterator
690 // ?_O # copy constructor closure
691 // ?_P<name> # udt returning <name>
692 // ?_Q # <unknown>
693 // ?_R0 # RTTI Type Descriptor
694 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
695 // ?_R2 # RTTI Base Class Array
696 // ?_R3 # RTTI Class Hierarchy Descriptor
697 // ?_R4 # RTTI Complete Object Locator
698 // ?_S # local vftable
699 // ?_T # local vftable constructor closure
700 // <operator-name> ::= ?_U # new[]
701 case OO_Array_New: Out << "?_U"; break;
702 // <operator-name> ::= ?_V # delete[]
703 case OO_Array_Delete: Out << "?_V"; break;
704
705 case OO_Conditional: {
706 DiagnosticsEngine &Diags = Context.getDiags();
707 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
708 "cannot mangle this conditional operator yet");
709 Diags.Report(Loc, DiagID);
710 break;
711 }
712
713 case OO_None:
714 case NUM_OVERLOADED_OPERATORS:
715 llvm_unreachable("Not an overloaded operator");
716 }
717}
718
719void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
720 // <source name> ::= <identifier> @
721 std::string key = II->getNameStart();
722 BackRefMap::iterator Found;
723 if (UseNameBackReferences)
724 Found = NameBackReferences.find(key);
725 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
726 Out << II->getName() << '@';
727 if (UseNameBackReferences && NameBackReferences.size() < 10) {
728 size_t Size = NameBackReferences.size();
729 NameBackReferences[key] = Size;
730 }
731 } else {
732 Out << Found->second;
733 }
734}
735
736void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
737 Context.mangleObjCMethodName(MD, Out);
738}
739
740// Find out how many function decls live above this one and return an integer
741// suitable for use as the number in a numbered anonymous scope.
742// TODO: Memoize.
743static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
744 const DeclContext *DC = FD->getParent();
745 int level = 1;
746
747 while (DC && !DC->isTranslationUnit()) {
748 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
749 DC = DC->getParent();
750 }
751
752 return 2*level;
753}
754
755void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
756 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
757 // <numbered-anonymous-scope> ::= ? <number>
758 // Even though the name is rendered in reverse order (e.g.
759 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
760 // innermost. So a method bar in class C local to function foo gets mangled
761 // as something like:
762 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
763 // This is more apparent when you have a type nested inside a method of a
764 // type nested inside a function. A method baz in class D local to method
765 // bar of class C local to function foo gets mangled as:
766 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
767 // This scheme is general enough to support GCC-style nested
768 // functions. You could have a method baz of class C inside a function bar
769 // inside a function foo, like so:
770 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
771 int NestLevel = getLocalNestingLevel(FD);
772 Out << '?';
773 mangleNumber(NestLevel);
774 Out << '?';
775 mangle(FD, "?");
776}
777
778void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
779 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000780 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000781 // <template-name> ::= <unscoped-template-name> <template-args>
782 // ::= <substitution>
783 // Always start with the unqualified name.
784
785 // Templates have their own context for back references.
786 ArgBackRefMap OuterArgsContext;
787 BackRefMap OuterTemplateContext;
788 NameBackReferences.swap(OuterTemplateContext);
789 TypeBackReferences.swap(OuterArgsContext);
790
791 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000792 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000793
794 // Restore the previous back reference contexts.
795 NameBackReferences.swap(OuterTemplateContext);
796 TypeBackReferences.swap(OuterArgsContext);
797}
798
799void
800MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
801 // <unscoped-template-name> ::= ?$ <unqualified-name>
802 Out << "?$";
803 mangleUnqualifiedName(TD);
804}
805
806void
807MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
808 bool IsBoolean) {
809 // <integer-literal> ::= $0 <number>
810 Out << "$0";
811 // Make sure booleans are encoded as 0/1.
812 if (IsBoolean && Value.getBoolValue())
813 mangleNumber(1);
814 else
815 mangleNumber(Value);
816}
817
818void
819MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
820 // See if this is a constant expression.
821 llvm::APSInt Value;
822 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
823 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
824 return;
825 }
826
827 // As bad as this diagnostic is, it's better than crashing.
828 DiagnosticsEngine &Diags = Context.getDiags();
829 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
830 "cannot yet mangle expression type %0");
831 Diags.Report(E->getExprLoc(), DiagID)
832 << E->getStmtClassName() << E->getSourceRange();
833}
834
835void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000836MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
837 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000838 // <template-args> ::= {<type> | <integer-literal>}+ @
839 unsigned NumTemplateArgs = TemplateArgs.size();
840 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000841 const TemplateArgument &TA = TemplateArgs[i];
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000842 switch (TA.getKind()) {
843 case TemplateArgument::Null:
844 llvm_unreachable("Can't mangle null template arguments!");
Reid Klecknercb5949d2013-04-09 12:47:38 +0000845 case TemplateArgument::Type: {
846 QualType T = TA.getAsType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000847 mangleType(T, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000848 break;
Reid Klecknercb5949d2013-04-09 12:47:38 +0000849 }
Reid Klecknerff430f62013-03-20 22:29:42 +0000850 case TemplateArgument::Declaration:
851 mangle(cast<NamedDecl>(TA.getAsDecl()), "$1?");
852 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000853 case TemplateArgument::Integral:
854 mangleIntegerLiteral(TA.getAsIntegral(),
855 TA.getIntegralType()->isBooleanType());
856 break;
857 case TemplateArgument::Expression:
858 mangleExpression(TA.getAsExpr());
859 break;
860 case TemplateArgument::Template:
861 case TemplateArgument::TemplateExpansion:
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000862 case TemplateArgument::NullPtr:
863 case TemplateArgument::Pack: {
864 // Issue a diagnostic.
865 DiagnosticsEngine &Diags = Context.getDiags();
866 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000867 "cannot mangle template argument %0 of kind %select{ERROR|ERROR|"
868 "pointer/reference|nullptr|integral|template|template pack expansion|"
869 "ERROR|parameter pack}1 yet");
870 Diags.Report(TD->getLocation(), DiagID)
871 << i + 1
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000872 << TA.getKind()
Reid Klecknerf16216c2013-03-20 01:40:23 +0000873 << TD->getSourceRange();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000874 }
875 }
876 }
877 Out << '@';
878}
879
880void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
881 bool IsMember) {
882 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
883 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
884 // 'I' means __restrict (32/64-bit).
885 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
886 // keyword!
887 // <base-cvr-qualifiers> ::= A # near
888 // ::= B # near const
889 // ::= C # near volatile
890 // ::= D # near const volatile
891 // ::= E # far (16-bit)
892 // ::= F # far const (16-bit)
893 // ::= G # far volatile (16-bit)
894 // ::= H # far const volatile (16-bit)
895 // ::= I # huge (16-bit)
896 // ::= J # huge const (16-bit)
897 // ::= K # huge volatile (16-bit)
898 // ::= L # huge const volatile (16-bit)
899 // ::= M <basis> # based
900 // ::= N <basis> # based const
901 // ::= O <basis> # based volatile
902 // ::= P <basis> # based const volatile
903 // ::= Q # near member
904 // ::= R # near const member
905 // ::= S # near volatile member
906 // ::= T # near const volatile member
907 // ::= U # far member (16-bit)
908 // ::= V # far const member (16-bit)
909 // ::= W # far volatile member (16-bit)
910 // ::= X # far const volatile member (16-bit)
911 // ::= Y # huge member (16-bit)
912 // ::= Z # huge const member (16-bit)
913 // ::= 0 # huge volatile member (16-bit)
914 // ::= 1 # huge const volatile member (16-bit)
915 // ::= 2 <basis> # based member
916 // ::= 3 <basis> # based const member
917 // ::= 4 <basis> # based volatile member
918 // ::= 5 <basis> # based const volatile member
919 // ::= 6 # near function (pointers only)
920 // ::= 7 # far function (pointers only)
921 // ::= 8 # near method (pointers only)
922 // ::= 9 # far method (pointers only)
923 // ::= _A <basis> # based function (pointers only)
924 // ::= _B <basis> # based function (far?) (pointers only)
925 // ::= _C <basis> # based method (pointers only)
926 // ::= _D <basis> # based method (far?) (pointers only)
927 // ::= _E # block (Clang)
928 // <basis> ::= 0 # __based(void)
929 // ::= 1 # __based(segment)?
930 // ::= 2 <name> # __based(name)
931 // ::= 3 # ?
932 // ::= 4 # ?
933 // ::= 5 # not really based
934 bool HasConst = Quals.hasConst(),
935 HasVolatile = Quals.hasVolatile();
936 if (!IsMember) {
937 if (HasConst && HasVolatile) {
938 Out << 'D';
939 } else if (HasVolatile) {
940 Out << 'C';
941 } else if (HasConst) {
942 Out << 'B';
943 } else {
944 Out << 'A';
945 }
946 } else {
947 if (HasConst && HasVolatile) {
948 Out << 'T';
949 } else if (HasVolatile) {
950 Out << 'S';
951 } else if (HasConst) {
952 Out << 'R';
953 } else {
954 Out << 'Q';
955 }
956 }
957
958 // FIXME: For now, just drop all extension qualifiers on the floor.
959}
960
961void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
962 // <pointer-cvr-qualifiers> ::= P # no qualifiers
963 // ::= Q # const
964 // ::= R # volatile
965 // ::= S # const volatile
966 bool HasConst = Quals.hasConst(),
967 HasVolatile = Quals.hasVolatile();
968 if (HasConst && HasVolatile) {
969 Out << 'S';
970 } else if (HasVolatile) {
971 Out << 'R';
972 } else if (HasConst) {
973 Out << 'Q';
974 } else {
975 Out << 'P';
976 }
977}
978
979void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
980 SourceRange Range) {
981 void *TypePtr = getASTContext().getCanonicalType(T).getAsOpaquePtr();
982 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
983
984 if (Found == TypeBackReferences.end()) {
985 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
986
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000987 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
988 mangleDecayedArrayType(AT, false);
989 } else if (const FunctionType *FT = T->getAs<FunctionType>()) {
990 Out << "P6";
991 mangleFunctionType(FT, 0, false, false);
992 } else {
993 mangleType(T, Range, QMM_Drop);
994 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000995
996 // See if it's worth creating a back reference.
997 // Only types longer than 1 character are considered
998 // and only 10 back references slots are available:
999 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1000 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1001 size_t Size = TypeBackReferences.size();
1002 TypeBackReferences[TypePtr] = Size;
1003 }
1004 } else {
1005 Out << Found->second;
1006 }
1007}
1008
1009void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001010 QualifierMangleMode QMM) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001011 // Only operate on the canonical type!
1012 T = getASTContext().getCanonicalType(T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001013 Qualifiers Quals = T.getLocalQualifiers();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001014
1015 if (const ArrayType *AT = dyn_cast<ArrayType>(T)) {
1016 if (QMM == QMM_Mangle)
1017 Out << 'A';
1018 else if (QMM == QMM_Escape || QMM == QMM_Result)
1019 Out << "$$B";
1020 mangleArrayType(AT, Quals);
1021 return;
1022 }
1023
1024 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1025 T->isBlockPointerType();
1026
1027 switch (QMM) {
1028 case QMM_Drop:
1029 break;
1030 case QMM_Mangle:
1031 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1032 Out << '6';
1033 mangleFunctionType(FT, 0, false, false);
1034 return;
1035 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001036 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001037 break;
1038 case QMM_Escape:
1039 if (!IsPointer && Quals) {
1040 Out << "$$C";
1041 mangleQualifiers(Quals, false);
1042 }
1043 break;
1044 case QMM_Result:
1045 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1046 Out << '?';
1047 mangleQualifiers(Quals, false);
1048 }
1049 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001050 }
1051
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001052 // We have to mangle these now, while we still have enough information.
1053 if (IsPointer)
1054 manglePointerQualifiers(Quals);
1055 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001056
1057 switch (ty->getTypeClass()) {
1058#define ABSTRACT_TYPE(CLASS, PARENT)
1059#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1060 case Type::CLASS: \
1061 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1062 return;
1063#define TYPE(CLASS, PARENT) \
1064 case Type::CLASS: \
1065 mangleType(cast<CLASS##Type>(ty), Range); \
1066 break;
1067#include "clang/AST/TypeNodes.def"
1068#undef ABSTRACT_TYPE
1069#undef NON_CANONICAL_TYPE
1070#undef TYPE
1071 }
1072}
1073
1074void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1075 SourceRange Range) {
1076 // <type> ::= <builtin-type>
1077 // <builtin-type> ::= X # void
1078 // ::= C # signed char
1079 // ::= D # char
1080 // ::= E # unsigned char
1081 // ::= F # short
1082 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1083 // ::= H # int
1084 // ::= I # unsigned int
1085 // ::= J # long
1086 // ::= K # unsigned long
1087 // L # <none>
1088 // ::= M # float
1089 // ::= N # double
1090 // ::= O # long double (__float80 is mangled differently)
1091 // ::= _J # long long, __int64
1092 // ::= _K # unsigned long long, __int64
1093 // ::= _L # __int128
1094 // ::= _M # unsigned __int128
1095 // ::= _N # bool
1096 // _O # <array in parameter>
1097 // ::= _T # __float80 (Intel)
1098 // ::= _W # wchar_t
1099 // ::= _Z # __float80 (Digital Mars)
1100 switch (T->getKind()) {
1101 case BuiltinType::Void: Out << 'X'; break;
1102 case BuiltinType::SChar: Out << 'C'; break;
1103 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1104 case BuiltinType::UChar: Out << 'E'; break;
1105 case BuiltinType::Short: Out << 'F'; break;
1106 case BuiltinType::UShort: Out << 'G'; break;
1107 case BuiltinType::Int: Out << 'H'; break;
1108 case BuiltinType::UInt: Out << 'I'; break;
1109 case BuiltinType::Long: Out << 'J'; break;
1110 case BuiltinType::ULong: Out << 'K'; break;
1111 case BuiltinType::Float: Out << 'M'; break;
1112 case BuiltinType::Double: Out << 'N'; break;
1113 // TODO: Determine size and mangle accordingly
1114 case BuiltinType::LongDouble: Out << 'O'; break;
1115 case BuiltinType::LongLong: Out << "_J"; break;
1116 case BuiltinType::ULongLong: Out << "_K"; break;
1117 case BuiltinType::Int128: Out << "_L"; break;
1118 case BuiltinType::UInt128: Out << "_M"; break;
1119 case BuiltinType::Bool: Out << "_N"; break;
1120 case BuiltinType::WChar_S:
1121 case BuiltinType::WChar_U: Out << "_W"; break;
1122
1123#define BUILTIN_TYPE(Id, SingletonId)
1124#define PLACEHOLDER_TYPE(Id, SingletonId) \
1125 case BuiltinType::Id:
1126#include "clang/AST/BuiltinTypes.def"
1127 case BuiltinType::Dependent:
1128 llvm_unreachable("placeholder types shouldn't get to name mangling");
1129
1130 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1131 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1132 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001133
1134 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1135 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1136 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1137 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1138 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1139 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001140 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001141 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001142
1143 case BuiltinType::NullPtr: Out << "$$T"; break;
1144
1145 case BuiltinType::Char16:
1146 case BuiltinType::Char32:
1147 case BuiltinType::Half: {
1148 DiagnosticsEngine &Diags = Context.getDiags();
1149 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1150 "cannot mangle this built-in %0 type yet");
1151 Diags.Report(Range.getBegin(), DiagID)
1152 << T->getName(Context.getASTContext().getPrintingPolicy())
1153 << Range;
1154 break;
1155 }
1156 }
1157}
1158
1159// <type> ::= <function-type>
1160void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1161 SourceRange) {
1162 // Structors only appear in decls, so at this point we know it's not a
1163 // structor type.
1164 // FIXME: This may not be lambda-friendly.
1165 Out << "$$A6";
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001166 mangleFunctionType(T, NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001167}
1168void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1169 SourceRange) {
1170 llvm_unreachable("Can't mangle K&R function prototypes");
1171}
1172
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001173void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1174 const FunctionDecl *D,
1175 bool IsStructor,
1176 bool IsInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001177 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1178 // <return-type> <argument-list> <throw-spec>
1179 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1180
1181 // If this is a C++ instance method, mangle the CVR qualifiers for the
1182 // this pointer.
1183 if (IsInstMethod)
1184 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
1185
1186 mangleCallingConvention(T, IsInstMethod);
1187
1188 // <return-type> ::= <type>
1189 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001190 if (IsStructor) {
1191 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1192 StructorType == Dtor_Deleting) {
1193 // The scalar deleting destructor takes an extra int argument.
1194 // However, the FunctionType generated has 0 arguments.
1195 // FIXME: This is a temporary hack.
1196 // Maybe should fix the FunctionType creation instead?
1197 Out << "PAXI@Z";
1198 return;
1199 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001200 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001201 } else {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001202 mangleType(Proto->getResultType(), SourceRange(), QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001203 }
1204
1205 // <argument-list> ::= X # void
1206 // ::= <type>+ @
1207 // ::= <type>* Z # varargs
1208 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1209 Out << 'X';
1210 } else {
1211 if (D) {
1212 // If we got a decl, use the type-as-written to make sure arrays
1213 // get mangled right. Note that we can't rely on the TSI
1214 // existing if (for example) the parameter was synthesized.
1215 for (FunctionDecl::param_const_iterator Parm = D->param_begin(),
1216 ParmEnd = D->param_end(); Parm != ParmEnd; ++Parm) {
1217 TypeSourceInfo *TSI = (*Parm)->getTypeSourceInfo();
1218 QualType Type = TSI ? TSI->getType() : (*Parm)->getType();
1219 mangleArgumentType(Type, (*Parm)->getSourceRange());
1220 }
1221 } else {
1222 // Happens for function pointer type arguments for example.
1223 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1224 ArgEnd = Proto->arg_type_end();
1225 Arg != ArgEnd; ++Arg)
1226 mangleArgumentType(*Arg, SourceRange());
1227 }
1228 // <builtin-type> ::= Z # ellipsis
1229 if (Proto->isVariadic())
1230 Out << 'Z';
1231 else
1232 Out << '@';
1233 }
1234
1235 mangleThrowSpecification(Proto);
1236}
1237
1238void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001239 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1240 // # pointer. in 64-bit mode *all*
1241 // # 'this' pointers are 64-bit.
1242 // ::= <global-function>
1243 // <member-function> ::= A # private: near
1244 // ::= B # private: far
1245 // ::= C # private: static near
1246 // ::= D # private: static far
1247 // ::= E # private: virtual near
1248 // ::= F # private: virtual far
1249 // ::= G # private: thunk near
1250 // ::= H # private: thunk far
1251 // ::= I # protected: near
1252 // ::= J # protected: far
1253 // ::= K # protected: static near
1254 // ::= L # protected: static far
1255 // ::= M # protected: virtual near
1256 // ::= N # protected: virtual far
1257 // ::= O # protected: thunk near
1258 // ::= P # protected: thunk far
1259 // ::= Q # public: near
1260 // ::= R # public: far
1261 // ::= S # public: static near
1262 // ::= T # public: static far
1263 // ::= U # public: virtual near
1264 // ::= V # public: virtual far
1265 // ::= W # public: thunk near
1266 // ::= X # public: thunk far
1267 // <global-function> ::= Y # global near
1268 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001269 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1270 switch (MD->getAccess()) {
1271 default:
1272 case AS_private:
1273 if (MD->isStatic())
1274 Out << 'C';
1275 else if (MD->isVirtual())
1276 Out << 'E';
1277 else
1278 Out << 'A';
1279 break;
1280 case AS_protected:
1281 if (MD->isStatic())
1282 Out << 'K';
1283 else if (MD->isVirtual())
1284 Out << 'M';
1285 else
1286 Out << 'I';
1287 break;
1288 case AS_public:
1289 if (MD->isStatic())
1290 Out << 'S';
1291 else if (MD->isVirtual())
1292 Out << 'U';
1293 else
1294 Out << 'Q';
1295 }
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001296 if (PointersAre64Bit && !MD->isStatic())
1297 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001298 } else
1299 Out << 'Y';
1300}
1301void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
1302 bool IsInstMethod) {
1303 // <calling-convention> ::= A # __cdecl
1304 // ::= B # __export __cdecl
1305 // ::= C # __pascal
1306 // ::= D # __export __pascal
1307 // ::= E # __thiscall
1308 // ::= F # __export __thiscall
1309 // ::= G # __stdcall
1310 // ::= H # __export __stdcall
1311 // ::= I # __fastcall
1312 // ::= J # __export __fastcall
1313 // The 'export' calling conventions are from a bygone era
1314 // (*cough*Win16*cough*) when functions were declared for export with
1315 // that keyword. (It didn't actually export them, it just made them so
1316 // that they could be in a DLL and somebody from another module could call
1317 // them.)
1318 CallingConv CC = T->getCallConv();
1319 if (CC == CC_Default) {
1320 if (IsInstMethod) {
1321 const FunctionProtoType *FPT =
1322 T->getCanonicalTypeUnqualified().castAs<FunctionProtoType>();
1323 bool isVariadic = FPT->isVariadic();
1324 CC = getASTContext().getDefaultCXXMethodCallConv(isVariadic);
1325 } else {
1326 CC = CC_C;
1327 }
1328 }
1329 switch (CC) {
1330 default:
1331 llvm_unreachable("Unsupported CC for mangling");
1332 case CC_Default:
1333 case CC_C: Out << 'A'; break;
1334 case CC_X86Pascal: Out << 'C'; break;
1335 case CC_X86ThisCall: Out << 'E'; break;
1336 case CC_X86StdCall: Out << 'G'; break;
1337 case CC_X86FastCall: Out << 'I'; break;
1338 }
1339}
1340void MicrosoftCXXNameMangler::mangleThrowSpecification(
1341 const FunctionProtoType *FT) {
1342 // <throw-spec> ::= Z # throw(...) (default)
1343 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1344 // ::= <type>+
1345 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1346 // all actually mangled as 'Z'. (They're ignored because their associated
1347 // functionality isn't implemented, and probably never will be.)
1348 Out << 'Z';
1349}
1350
1351void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1352 SourceRange Range) {
1353 // Probably should be mangled as a template instantiation; need to see what
1354 // VC does first.
1355 DiagnosticsEngine &Diags = Context.getDiags();
1356 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1357 "cannot mangle this unresolved dependent type yet");
1358 Diags.Report(Range.getBegin(), DiagID)
1359 << Range;
1360}
1361
1362// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1363// <union-type> ::= T <name>
1364// <struct-type> ::= U <name>
1365// <class-type> ::= V <name>
1366// <enum-type> ::= W <size> <name>
1367void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
1368 mangleType(cast<TagType>(T));
1369}
1370void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
1371 mangleType(cast<TagType>(T));
1372}
1373void MicrosoftCXXNameMangler::mangleType(const TagType *T) {
1374 switch (T->getDecl()->getTagKind()) {
1375 case TTK_Union:
1376 Out << 'T';
1377 break;
1378 case TTK_Struct:
1379 case TTK_Interface:
1380 Out << 'U';
1381 break;
1382 case TTK_Class:
1383 Out << 'V';
1384 break;
1385 case TTK_Enum:
1386 Out << 'W';
1387 Out << getASTContext().getTypeSizeInChars(
1388 cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity();
1389 break;
1390 }
1391 mangleName(T->getDecl());
1392}
1393
1394// <type> ::= <array-type>
1395// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1396// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001397// <element-type> # as global, E is never required
1398// ::= Q E? <cvr-qualifiers> [Y <dimension-count> <dimension>+]
1399// <element-type> # as param, E is required for 64-bit
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001400// It's supposed to be the other way around, but for some strange reason, it
1401// isn't. Today this behavior is retained for the sole purpose of backwards
1402// compatibility.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001403void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T,
1404 bool IsGlobal) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001405 // This isn't a recursive mangling, so now we have to do it all in this
1406 // one call.
1407 if (IsGlobal) {
1408 manglePointerQualifiers(T->getElementType().getQualifiers());
1409 } else {
1410 Out << 'Q';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001411 if (PointersAre64Bit)
1412 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001413 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001414 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001415}
1416void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1417 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001418 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001419}
1420void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1421 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001422 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001423}
1424void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1425 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001426 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001427}
1428void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1429 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001430 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001431}
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001432void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T,
1433 Qualifiers Quals) {
1434 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001435 SmallVector<llvm::APInt, 3> Dimensions;
1436 for (;;) {
1437 if (const ConstantArrayType *CAT =
1438 getASTContext().getAsConstantArrayType(ElementTy)) {
1439 Dimensions.push_back(CAT->getSize());
1440 ElementTy = CAT->getElementType();
1441 } else if (ElementTy->isVariableArrayType()) {
1442 const VariableArrayType *VAT =
1443 getASTContext().getAsVariableArrayType(ElementTy);
1444 DiagnosticsEngine &Diags = Context.getDiags();
1445 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1446 "cannot mangle this variable-length array yet");
1447 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1448 << VAT->getBracketsRange();
1449 return;
1450 } else if (ElementTy->isDependentSizedArrayType()) {
1451 // The dependent expression has to be folded into a constant (TODO).
1452 const DependentSizedArrayType *DSAT =
1453 getASTContext().getAsDependentSizedArrayType(ElementTy);
1454 DiagnosticsEngine &Diags = Context.getDiags();
1455 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1456 "cannot mangle this dependent-length array yet");
1457 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1458 << DSAT->getBracketsRange();
1459 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001460 } else if (const IncompleteArrayType *IAT =
1461 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1462 Dimensions.push_back(llvm::APInt(32, 0));
1463 ElementTy = IAT->getElementType();
1464 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001465 else break;
1466 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001467 Out << 'Y';
1468 // <dimension-count> ::= <number> # number of extra dimensions
1469 mangleNumber(Dimensions.size());
1470 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1471 mangleNumber(Dimensions[Dim].getLimitedValue());
1472 mangleType(getASTContext().getQualifiedType(ElementTy.getTypePtr(), Quals),
1473 SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001474}
1475
1476// <type> ::= <pointer-to-member-type>
1477// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1478// <class name> <type>
1479void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1480 SourceRange Range) {
1481 QualType PointeeType = T->getPointeeType();
1482 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1483 Out << '8';
1484 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001485 mangleFunctionType(FPT, NULL, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001486 } else {
1487 mangleQualifiers(PointeeType.getQualifiers(), true);
1488 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001489 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001490 }
1491}
1492
1493void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1494 SourceRange Range) {
1495 DiagnosticsEngine &Diags = Context.getDiags();
1496 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1497 "cannot mangle this template type parameter type yet");
1498 Diags.Report(Range.getBegin(), DiagID)
1499 << Range;
1500}
1501
1502void MicrosoftCXXNameMangler::mangleType(
1503 const SubstTemplateTypeParmPackType *T,
1504 SourceRange Range) {
1505 DiagnosticsEngine &Diags = Context.getDiags();
1506 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1507 "cannot mangle this substituted parameter pack yet");
1508 Diags.Report(Range.getBegin(), DiagID)
1509 << Range;
1510}
1511
1512// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001513// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1514// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001515void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1516 SourceRange Range) {
1517 QualType PointeeTy = T->getPointeeType();
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001518 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1519 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001520 mangleType(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001521}
1522void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1523 SourceRange Range) {
1524 // Object pointers never have qualifiers.
1525 Out << 'A';
1526 mangleType(T->getPointeeType(), Range);
1527}
1528
1529// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001530// <reference-type> ::= A E? <cvr-qualifiers> <type>
1531// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001532void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1533 SourceRange Range) {
1534 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001535 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1536 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001537 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001538}
1539
1540// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001541// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1542// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001543void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1544 SourceRange Range) {
1545 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001546 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1547 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001548 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001549}
1550
1551void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1552 SourceRange Range) {
1553 DiagnosticsEngine &Diags = Context.getDiags();
1554 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1555 "cannot mangle this complex number type yet");
1556 Diags.Report(Range.getBegin(), DiagID)
1557 << Range;
1558}
1559
1560void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1561 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001562 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1563 assert(ET && "vectors with non-builtin elements are unsupported");
1564 uint64_t Width = getASTContext().getTypeSize(T);
1565 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1566 // doesn't match the Intel types uses a custom mangling below.
1567 bool IntelVector = true;
1568 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1569 Out << "T__m64";
1570 } else if (Width == 128 || Width == 256) {
1571 if (ET->getKind() == BuiltinType::Float)
1572 Out << "T__m" << Width;
1573 else if (ET->getKind() == BuiltinType::LongLong)
1574 Out << "T__m" << Width << 'i';
1575 else if (ET->getKind() == BuiltinType::Double)
1576 Out << "U__m" << Width << 'd';
1577 else
1578 IntelVector = false;
1579 } else {
1580 IntelVector = false;
1581 }
1582
1583 if (!IntelVector) {
1584 // The MS ABI doesn't have a special mangling for vector types, so we define
1585 // our own mangling to handle uses of __vector_size__ on user-specified
1586 // types, and for extensions like __v4sf.
1587 Out << "T__clang_vec" << T->getNumElements() << '_';
1588 mangleType(ET, Range);
1589 }
1590
1591 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001592}
Reid Kleckner1232e272013-03-26 16:56:59 +00001593
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001594void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1595 SourceRange Range) {
1596 DiagnosticsEngine &Diags = Context.getDiags();
1597 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1598 "cannot mangle this extended vector type yet");
1599 Diags.Report(Range.getBegin(), DiagID)
1600 << Range;
1601}
1602void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1603 SourceRange Range) {
1604 DiagnosticsEngine &Diags = Context.getDiags();
1605 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1606 "cannot mangle this dependent-sized extended vector type yet");
1607 Diags.Report(Range.getBegin(), DiagID)
1608 << Range;
1609}
1610
1611void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1612 SourceRange) {
1613 // ObjC interfaces have structs underlying them.
1614 Out << 'U';
1615 mangleName(T->getDecl());
1616}
1617
1618void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1619 SourceRange Range) {
1620 // We don't allow overloading by different protocol qualification,
1621 // so mangling them isn't necessary.
1622 mangleType(T->getBaseType(), Range);
1623}
1624
1625void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1626 SourceRange Range) {
1627 Out << "_E";
1628
1629 QualType pointee = T->getPointeeType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001630 mangleFunctionType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001631}
1632
1633void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T,
1634 SourceRange Range) {
1635 DiagnosticsEngine &Diags = Context.getDiags();
1636 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1637 "cannot mangle this injected class name type yet");
1638 Diags.Report(Range.getBegin(), DiagID)
1639 << Range;
1640}
1641
1642void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1643 SourceRange Range) {
1644 DiagnosticsEngine &Diags = Context.getDiags();
1645 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1646 "cannot mangle this template specialization type yet");
1647 Diags.Report(Range.getBegin(), DiagID)
1648 << Range;
1649}
1650
1651void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1652 SourceRange Range) {
1653 DiagnosticsEngine &Diags = Context.getDiags();
1654 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1655 "cannot mangle this dependent name type yet");
1656 Diags.Report(Range.getBegin(), DiagID)
1657 << Range;
1658}
1659
1660void MicrosoftCXXNameMangler::mangleType(
1661 const DependentTemplateSpecializationType *T,
1662 SourceRange Range) {
1663 DiagnosticsEngine &Diags = Context.getDiags();
1664 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1665 "cannot mangle this dependent template specialization type yet");
1666 Diags.Report(Range.getBegin(), DiagID)
1667 << Range;
1668}
1669
1670void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1671 SourceRange Range) {
1672 DiagnosticsEngine &Diags = Context.getDiags();
1673 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1674 "cannot mangle this pack expansion yet");
1675 Diags.Report(Range.getBegin(), DiagID)
1676 << Range;
1677}
1678
1679void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1680 SourceRange Range) {
1681 DiagnosticsEngine &Diags = Context.getDiags();
1682 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1683 "cannot mangle this typeof(type) yet");
1684 Diags.Report(Range.getBegin(), DiagID)
1685 << Range;
1686}
1687
1688void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1689 SourceRange Range) {
1690 DiagnosticsEngine &Diags = Context.getDiags();
1691 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1692 "cannot mangle this typeof(expression) yet");
1693 Diags.Report(Range.getBegin(), DiagID)
1694 << Range;
1695}
1696
1697void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1698 SourceRange Range) {
1699 DiagnosticsEngine &Diags = Context.getDiags();
1700 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1701 "cannot mangle this decltype() yet");
1702 Diags.Report(Range.getBegin(), DiagID)
1703 << Range;
1704}
1705
1706void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1707 SourceRange Range) {
1708 DiagnosticsEngine &Diags = Context.getDiags();
1709 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1710 "cannot mangle this unary transform type yet");
1711 Diags.Report(Range.getBegin(), DiagID)
1712 << Range;
1713}
1714
1715void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1716 DiagnosticsEngine &Diags = Context.getDiags();
1717 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1718 "cannot mangle this 'auto' type yet");
1719 Diags.Report(Range.getBegin(), DiagID)
1720 << Range;
1721}
1722
1723void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1724 SourceRange Range) {
1725 DiagnosticsEngine &Diags = Context.getDiags();
1726 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1727 "cannot mangle this C11 atomic type yet");
1728 Diags.Report(Range.getBegin(), DiagID)
1729 << Range;
1730}
1731
1732void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1733 raw_ostream &Out) {
1734 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1735 "Invalid mangleName() call, argument is not a variable or function!");
1736 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1737 "Invalid mangleName() call on 'structor decl!");
1738
1739 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1740 getASTContext().getSourceManager(),
1741 "Mangling declaration");
1742
1743 MicrosoftCXXNameMangler Mangler(*this, Out);
1744 return Mangler.mangle(D);
1745}
1746void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1747 const ThunkInfo &Thunk,
1748 raw_ostream &) {
1749 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1750 "cannot mangle thunk for this method yet");
1751 getDiags().Report(MD->getLocation(), DiagID);
1752}
1753void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1754 CXXDtorType Type,
1755 const ThisAdjustment &,
1756 raw_ostream &) {
1757 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1758 "cannot mangle thunk for this destructor yet");
1759 getDiags().Report(DD->getLocation(), DiagID);
1760}
1761void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1762 raw_ostream &Out) {
1763 // <mangled-name> ::= ? <operator-name> <class-name> <storage-class>
1764 // <cvr-qualifiers> [<name>] @
1765 // <operator-name> ::= _7 # vftable
1766 // ::= _8 # vbtable
1767 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1768 // is always '6' for vftables and '7' for vbtables. (The difference is
1769 // beyond me.)
1770 // TODO: vbtables.
1771 MicrosoftCXXNameMangler Mangler(*this, Out);
1772 Mangler.getStream() << "\01??_7";
1773 Mangler.mangleName(RD);
1774 Mangler.getStream() << "6B";
1775 // TODO: If the class has more than one vtable, mangle in the class it came
1776 // from.
1777 Mangler.getStream() << '@';
1778}
1779void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1780 raw_ostream &) {
1781 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1782}
1783void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1784 int64_t Offset,
1785 const CXXRecordDecl *Type,
1786 raw_ostream &) {
1787 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1788}
1789void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1790 raw_ostream &) {
1791 // FIXME: Give a location...
1792 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1793 "cannot mangle RTTI descriptors for type %0 yet");
1794 getDiags().Report(DiagID)
1795 << T.getBaseTypeIdentifier();
1796}
1797void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1798 raw_ostream &) {
1799 // FIXME: Give a location...
1800 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1801 "cannot mangle the name of type %0 into RTTI descriptors yet");
1802 getDiags().Report(DiagID)
1803 << T.getBaseTypeIdentifier();
1804}
1805void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1806 CXXCtorType Type,
1807 raw_ostream & Out) {
1808 MicrosoftCXXNameMangler mangler(*this, Out);
1809 mangler.mangle(D);
1810}
1811void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1812 CXXDtorType Type,
1813 raw_ostream & Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001814 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001815 mangler.mangle(D);
1816}
1817void MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *VD,
1818 raw_ostream &) {
1819 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1820 "cannot mangle this reference temporary yet");
1821 getDiags().Report(VD->getLocation(), DiagID);
1822}
1823
1824MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
1825 DiagnosticsEngine &Diags) {
1826 return new MicrosoftMangleContext(Context, Diags);
1827}