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