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