blob: 45d60c620efb38263c1227514168058d309a960a [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);
Timur Iskhodzhanov635de282013-07-30 09:46:19 +000092 void mangleFunctionType(const FunctionType *T, const FunctionDecl *D,
93 bool IsStructor, bool IsInstMethod);
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
David Majnemer02c44f02013-08-05 22:26:46 +0000126 void mangleType(const TagDecl *TD);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000127 void mangleDecayedArrayType(const ArrayType *T, bool IsGlobal);
Reid Klecknerf21818d2013-06-24 19:21:52 +0000128 void mangleArrayType(const ArrayType *T);
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);
Reid Kleckner5d90d182013-07-02 18:10:07 +0000137 void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA,
138 int ArgIndex);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000139};
140
141/// MicrosoftMangleContext - Overrides the default MangleContext for the
142/// Microsoft Visual C++ ABI.
143class MicrosoftMangleContext : public MangleContext {
144public:
145 MicrosoftMangleContext(ASTContext &Context,
146 DiagnosticsEngine &Diags) : MangleContext(Context, Diags) { }
147 virtual bool shouldMangleDeclName(const NamedDecl *D);
148 virtual void mangleName(const NamedDecl *D, raw_ostream &Out);
149 virtual void mangleThunk(const CXXMethodDecl *MD,
150 const ThunkInfo &Thunk,
151 raw_ostream &);
152 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
153 const ThisAdjustment &ThisAdjustment,
154 raw_ostream &);
155 virtual void mangleCXXVTable(const CXXRecordDecl *RD,
156 raw_ostream &);
157 virtual void mangleCXXVTT(const CXXRecordDecl *RD,
158 raw_ostream &);
Reid Kleckner90633022013-06-19 15:20:38 +0000159 virtual void mangleCXXVBTable(const CXXRecordDecl *Derived,
160 ArrayRef<const CXXRecordDecl *> BasePath,
161 raw_ostream &Out);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000162 virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
163 const CXXRecordDecl *Type,
164 raw_ostream &);
165 virtual void mangleCXXRTTI(QualType T, raw_ostream &);
166 virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
167 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
168 raw_ostream &);
169 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
170 raw_ostream &);
171 virtual void mangleReferenceTemporary(const clang::VarDecl *,
172 raw_ostream &);
173};
174
175}
176
177static bool isInCLinkageSpecification(const Decl *D) {
178 D = D->getCanonicalDecl();
179 for (const DeclContext *DC = D->getDeclContext();
180 !DC->isTranslationUnit(); DC = DC->getParent()) {
181 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
182 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
183 }
184
185 return false;
186}
187
188bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
189 // In C, functions with no attributes never need to be mangled. Fastpath them.
190 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
191 return false;
192
193 // Any decl can be declared with __asm("foo") on it, and this takes precedence
194 // over all other naming in the .o file.
195 if (D->hasAttr<AsmLabelAttr>())
196 return true;
197
198 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
199 // (always) as does passing a C++ member function and a function
200 // whose name is not a simple identifier.
201 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
202 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
203 !FD->getDeclName().isIdentifier()))
204 return true;
205
206 // Otherwise, no mangling is done outside C++ mode.
207 if (!getASTContext().getLangOpts().CPlusPlus)
208 return false;
209
210 // Variables at global scope with internal linkage are not mangled.
211 if (!FD) {
212 const DeclContext *DC = D->getDeclContext();
Rafael Espindola181e3ec2013-05-13 00:12:11 +0000213 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000214 return false;
215 }
216
217 // C functions and "main" are not mangled.
218 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
219 return false;
220
221 return true;
222}
223
224void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
225 StringRef Prefix) {
226 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
227 // Therefore it's really important that we don't decorate the
228 // name with leading underscores or leading/trailing at signs. So, by
229 // default, we emit an asm marker at the start so we get the name right.
230 // Callers can override this with a custom prefix.
231
232 // Any decl can be declared with __asm("foo") on it, and this takes precedence
233 // over all other naming in the .o file.
234 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
235 // If we have an asm name, then we use it as the mangling.
236 Out << '\01' << ALA->getLabel();
237 return;
238 }
239
240 // <mangled-name> ::= ? <name> <type-encoding>
241 Out << Prefix;
242 mangleName(D);
243 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
244 mangleFunctionEncoding(FD);
245 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
246 mangleVariableEncoding(VD);
247 else {
248 // TODO: Fields? Can MSVC even mangle them?
249 // Issue a diagnostic for now.
250 DiagnosticsEngine &Diags = Context.getDiags();
251 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
252 "cannot mangle this declaration yet");
253 Diags.Report(D->getLocation(), DiagID)
254 << D->getSourceRange();
255 }
256}
257
258void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
259 // <type-encoding> ::= <function-class> <function-type>
260
Reid Klecknerf21818d2013-06-24 19:21:52 +0000261 // Since MSVC operates on the type as written and not the canonical type, it
262 // actually matters which decl we have here. MSVC appears to choose the
263 // first, since it is most likely to be the declaration in a header file.
264 FD = FD->getFirstDeclaration();
265
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000266 // Don't mangle in the type if this isn't a decl we should typically mangle.
267 if (!Context.shouldMangleDeclName(FD))
268 return;
269
270 // We should never ever see a FunctionNoProtoType at this point.
271 // We don't even know how to mangle their types anyway :).
Reid Klecknerf21818d2013-06-24 19:21:52 +0000272 TypeSourceInfo *TSI = FD->getTypeSourceInfo();
273 QualType T = TSI ? TSI->getType() : FD->getType();
274 const FunctionProtoType *FT = T->castAs<FunctionProtoType>();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000275
276 bool InStructor = false, InInstMethod = false;
277 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
278 if (MD) {
279 if (MD->isInstance())
280 InInstMethod = true;
281 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
282 InStructor = true;
283 }
284
285 // First, the function class.
286 mangleFunctionClass(FD);
287
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000288 mangleFunctionType(FT, FD, InStructor, InInstMethod);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000289}
290
291void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
292 // <type-encoding> ::= <storage-class> <variable-type>
293 // <storage-class> ::= 0 # private static member
294 // ::= 1 # protected static member
295 // ::= 2 # public static member
296 // ::= 3 # global
297 // ::= 4 # static local
298
299 // The first character in the encoding (after the name) is the storage class.
300 if (VD->isStaticDataMember()) {
301 // If it's a static member, it also encodes the access level.
302 switch (VD->getAccess()) {
303 default:
304 case AS_private: Out << '0'; break;
305 case AS_protected: Out << '1'; break;
306 case AS_public: Out << '2'; break;
307 }
308 }
309 else if (!VD->isStaticLocal())
310 Out << '3';
311 else
312 Out << '4';
313 // Now mangle the type.
314 // <variable-type> ::= <type> <cvr-qualifiers>
315 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
316 // Pointers and references are odd. The type of 'int * const foo;' gets
317 // mangled as 'QAHA' instead of 'PAHB', for example.
318 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
319 QualType Ty = TL.getType();
David Majnemerea3db972013-08-09 05:09:04 +0000320 if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000321 // Global arrays are funny, too.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000322 mangleDecayedArrayType(AT, true);
323 if (AT->getElementType()->isArrayType())
324 Out << 'A';
325 else
326 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000327 } else {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000328 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemerc0e64f32013-08-05 22:43:06 +0000329 mangleQualifiers(Ty.getLocalQualifiers(), Ty->isMemberPointerType());
330 // Member pointers are suffixed with a back reference to the member
331 // pointer's class name.
332 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
333 mangleName(MPT->getClass()->getAsCXXRecordDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000334 }
335}
336
337void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
338 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
339 const DeclContext *DC = ND->getDeclContext();
340
341 // Always start with the unqualified name.
342 mangleUnqualifiedName(ND);
343
344 // If this is an extern variable declared locally, the relevant DeclContext
345 // is that of the containing namespace, or the translation unit.
346 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
347 while (!DC->isNamespace() && !DC->isTranslationUnit())
348 DC = DC->getParent();
349
350 manglePostfix(DC);
351
352 // Terminate the whole name with an '@'.
353 Out << '@';
354}
355
356void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
357 llvm::APSInt APSNumber(/*BitWidth=*/64, /*isUnsigned=*/false);
358 APSNumber = Number;
359 mangleNumber(APSNumber);
360}
361
362void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
363 // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
364 // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
365 // ::= [?] @ # 0 (alternate mangling, not emitted by VC)
366 if (Value.isSigned() && Value.isNegative()) {
367 Out << '?';
368 mangleNumber(llvm::APSInt(Value.abs()));
369 return;
370 }
371 llvm::APSInt Temp(Value);
372 // There's a special shorter mangling for 0, but Microsoft
373 // chose not to use it. Instead, 0 gets mangled as "A@". Oh well...
374 if (Value.uge(1) && Value.ule(10)) {
375 --Temp;
376 Temp.print(Out, false);
377 } else {
378 // We have to build up the encoding in reverse order, so it will come
379 // out right when we write it out.
380 char Encoding[64];
381 char *EndPtr = Encoding+sizeof(Encoding);
382 char *CurPtr = EndPtr;
383 llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned());
384 NibbleMask = 0xf;
385 do {
386 *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf);
387 Temp = Temp.lshr(4);
388 } while (Temp != 0);
389 Out.write(CurPtr, EndPtr-CurPtr);
390 Out << '@';
391 }
392}
393
394static const TemplateDecl *
Reid Klecknerf16216c2013-03-20 01:40:23 +0000395isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000396 // Check if we have a function template.
397 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
398 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000399 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000400 return TD;
401 }
402 }
403
404 // Check if we have a class template.
405 if (const ClassTemplateSpecializationDecl *Spec =
Reid Klecknerf16216c2013-03-20 01:40:23 +0000406 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
407 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000408 return Spec->getSpecializedTemplate();
409 }
410
411 return 0;
412}
413
414void
415MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
416 DeclarationName Name) {
417 // <unqualified-name> ::= <operator-name>
418 // ::= <ctor-dtor-name>
419 // ::= <source-name>
420 // ::= <template-name>
Reid Klecknerf16216c2013-03-20 01:40:23 +0000421
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000422 // Check if we have a template.
Reid Klecknerf16216c2013-03-20 01:40:23 +0000423 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000424 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Kleckner3be37d12013-07-13 00:43:39 +0000425 // Function templates aren't considered for name back referencing. This
426 // makes sense since function templates aren't likely to occur multiple
427 // times in a symbol.
428 // FIXME: Test alias template mangling with MSVC 2013.
429 if (!isa<ClassTemplateDecl>(TD)) {
430 mangleTemplateInstantiationName(TD, *TemplateArgs);
431 return;
432 }
433
434 // We have a class template.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000435 // Here comes the tricky thing: if we need to mangle something like
436 // void foo(A::X<Y>, B::X<Y>),
437 // the X<Y> part is aliased. However, if you need to mangle
438 // void foo(A::X<A::Y>, A::X<B::Y>),
439 // the A::X<> part is not aliased.
440 // That said, from the mangler's perspective we have a structure like this:
441 // namespace[s] -> type[ -> template-parameters]
442 // but from the Clang perspective we have
443 // type [ -> template-parameters]
444 // \-> namespace[s]
445 // What we do is we create a new mangler, mangle the same type (without
446 // a namespace suffix) using the extra mangler with back references
447 // disabled (to avoid infinite recursion) and then use the mangled type
448 // name as a key to check the mangling of different types for aliasing.
449
450 std::string BackReferenceKey;
451 BackRefMap::iterator Found;
452 if (UseNameBackReferences) {
453 llvm::raw_string_ostream Stream(BackReferenceKey);
454 MicrosoftCXXNameMangler Extra(Context, Stream);
455 Extra.disableBackReferences();
456 Extra.mangleUnqualifiedName(ND, Name);
457 Stream.flush();
458
459 Found = NameBackReferences.find(BackReferenceKey);
460 }
461 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000462 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000463 if (UseNameBackReferences && NameBackReferences.size() < 10) {
464 size_t Size = NameBackReferences.size();
465 NameBackReferences[BackReferenceKey] = Size;
466 }
467 } else {
468 Out << Found->second;
469 }
470 return;
471 }
472
473 switch (Name.getNameKind()) {
474 case DeclarationName::Identifier: {
475 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
476 mangleSourceName(II);
477 break;
478 }
479
480 // Otherwise, an anonymous entity. We must have a declaration.
481 assert(ND && "mangling empty name without declaration");
482
483 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
484 if (NS->isAnonymousNamespace()) {
485 Out << "?A@";
486 break;
487 }
488 }
489
490 // We must have an anonymous struct.
491 const TagDecl *TD = cast<TagDecl>(ND);
492 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
493 assert(TD->getDeclContext() == D->getDeclContext() &&
494 "Typedef should not be in another decl context!");
495 assert(D->getDeclName().getAsIdentifierInfo() &&
496 "Typedef was not named!");
497 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
498 break;
499 }
500
501 // When VC encounters an anonymous type with no tag and no typedef,
502 // it literally emits '<unnamed-tag>'.
503 Out << "<unnamed-tag>";
504 break;
505 }
506
507 case DeclarationName::ObjCZeroArgSelector:
508 case DeclarationName::ObjCOneArgSelector:
509 case DeclarationName::ObjCMultiArgSelector:
510 llvm_unreachable("Can't mangle Objective-C selector names here!");
511
512 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000513 if (ND == Structor) {
514 assert(StructorType == Ctor_Complete &&
515 "Should never be asked to mangle a ctor other than complete");
516 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000517 Out << "?0";
518 break;
519
520 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000521 if (ND == Structor)
522 // If the named decl is the C++ destructor we're mangling,
523 // use the type we were given.
524 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
525 else
Reid Klecknera4130ba2013-07-22 13:51:44 +0000526 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000527 // class with a destructor is declared within a destructor.
Reid Klecknera4130ba2013-07-22 13:51:44 +0000528 mangleCXXDtorType(Dtor_Base);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000529 break;
530
531 case DeclarationName::CXXConversionFunctionName:
532 // <operator-name> ::= ?B # (cast)
533 // The target type is encoded as the return type.
534 Out << "?B";
535 break;
536
537 case DeclarationName::CXXOperatorName:
538 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
539 break;
540
541 case DeclarationName::CXXLiteralOperatorName: {
542 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
543 DiagnosticsEngine Diags = Context.getDiags();
544 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
545 "cannot mangle this literal operator yet");
546 Diags.Report(ND->getLocation(), DiagID);
547 break;
548 }
549
550 case DeclarationName::CXXUsingDirective:
551 llvm_unreachable("Can't mangle a using directive name!");
552 }
553}
554
555void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
556 bool NoFunction) {
557 // <postfix> ::= <unqualified-name> [<postfix>]
558 // ::= <substitution> [<postfix>]
559
560 if (!DC) return;
561
562 while (isa<LinkageSpecDecl>(DC))
563 DC = DC->getParent();
564
565 if (DC->isTranslationUnit())
566 return;
567
568 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedmane5798892013-07-10 01:13:27 +0000569 DiagnosticsEngine Diags = Context.getDiags();
570 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
571 "cannot mangle a local inside this block yet");
572 Diags.Report(BD->getLocation(), DiagID);
573
574 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
575 // for how this should be done.
576 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000577 Out << '@';
578 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000579 } else if (isa<CapturedDecl>(DC)) {
580 // Skip CapturedDecl context.
581 manglePostfix(DC->getParent(), NoFunction);
582 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000583 }
584
585 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
586 return;
587 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
588 mangleObjCMethodName(Method);
589 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
590 mangleLocalName(Func);
591 else {
592 mangleUnqualifiedName(cast<NamedDecl>(DC));
593 manglePostfix(DC->getParent(), NoFunction);
594 }
595}
596
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000597void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000598 // Microsoft uses the names on the case labels for these dtor variants. Clang
599 // uses the Itanium terminology internally. Everything in this ABI delegates
600 // towards the base dtor.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000601 switch (T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000602 // <operator-name> ::= ?1 # destructor
603 case Dtor_Base: Out << "?1"; return;
604 // <operator-name> ::= ?_D # vbase destructor
605 case Dtor_Complete: Out << "?_D"; return;
606 // <operator-name> ::= ?_G # scalar deleting destructor
607 case Dtor_Deleting: Out << "?_G"; return;
608 // <operator-name> ::= ?_E # vector deleting destructor
609 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
610 // it.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000611 }
612 llvm_unreachable("Unsupported dtor type?");
613}
614
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000615void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
616 SourceLocation Loc) {
617 switch (OO) {
618 // ?0 # constructor
619 // ?1 # destructor
620 // <operator-name> ::= ?2 # new
621 case OO_New: Out << "?2"; break;
622 // <operator-name> ::= ?3 # delete
623 case OO_Delete: Out << "?3"; break;
624 // <operator-name> ::= ?4 # =
625 case OO_Equal: Out << "?4"; break;
626 // <operator-name> ::= ?5 # >>
627 case OO_GreaterGreater: Out << "?5"; break;
628 // <operator-name> ::= ?6 # <<
629 case OO_LessLess: Out << "?6"; break;
630 // <operator-name> ::= ?7 # !
631 case OO_Exclaim: Out << "?7"; break;
632 // <operator-name> ::= ?8 # ==
633 case OO_EqualEqual: Out << "?8"; break;
634 // <operator-name> ::= ?9 # !=
635 case OO_ExclaimEqual: Out << "?9"; break;
636 // <operator-name> ::= ?A # []
637 case OO_Subscript: Out << "?A"; break;
638 // ?B # conversion
639 // <operator-name> ::= ?C # ->
640 case OO_Arrow: Out << "?C"; break;
641 // <operator-name> ::= ?D # *
642 case OO_Star: Out << "?D"; break;
643 // <operator-name> ::= ?E # ++
644 case OO_PlusPlus: Out << "?E"; break;
645 // <operator-name> ::= ?F # --
646 case OO_MinusMinus: Out << "?F"; break;
647 // <operator-name> ::= ?G # -
648 case OO_Minus: Out << "?G"; break;
649 // <operator-name> ::= ?H # +
650 case OO_Plus: Out << "?H"; break;
651 // <operator-name> ::= ?I # &
652 case OO_Amp: Out << "?I"; break;
653 // <operator-name> ::= ?J # ->*
654 case OO_ArrowStar: Out << "?J"; break;
655 // <operator-name> ::= ?K # /
656 case OO_Slash: Out << "?K"; break;
657 // <operator-name> ::= ?L # %
658 case OO_Percent: Out << "?L"; break;
659 // <operator-name> ::= ?M # <
660 case OO_Less: Out << "?M"; break;
661 // <operator-name> ::= ?N # <=
662 case OO_LessEqual: Out << "?N"; break;
663 // <operator-name> ::= ?O # >
664 case OO_Greater: Out << "?O"; break;
665 // <operator-name> ::= ?P # >=
666 case OO_GreaterEqual: Out << "?P"; break;
667 // <operator-name> ::= ?Q # ,
668 case OO_Comma: Out << "?Q"; break;
669 // <operator-name> ::= ?R # ()
670 case OO_Call: Out << "?R"; break;
671 // <operator-name> ::= ?S # ~
672 case OO_Tilde: Out << "?S"; break;
673 // <operator-name> ::= ?T # ^
674 case OO_Caret: Out << "?T"; break;
675 // <operator-name> ::= ?U # |
676 case OO_Pipe: Out << "?U"; break;
677 // <operator-name> ::= ?V # &&
678 case OO_AmpAmp: Out << "?V"; break;
679 // <operator-name> ::= ?W # ||
680 case OO_PipePipe: Out << "?W"; break;
681 // <operator-name> ::= ?X # *=
682 case OO_StarEqual: Out << "?X"; break;
683 // <operator-name> ::= ?Y # +=
684 case OO_PlusEqual: Out << "?Y"; break;
685 // <operator-name> ::= ?Z # -=
686 case OO_MinusEqual: Out << "?Z"; break;
687 // <operator-name> ::= ?_0 # /=
688 case OO_SlashEqual: Out << "?_0"; break;
689 // <operator-name> ::= ?_1 # %=
690 case OO_PercentEqual: Out << "?_1"; break;
691 // <operator-name> ::= ?_2 # >>=
692 case OO_GreaterGreaterEqual: Out << "?_2"; break;
693 // <operator-name> ::= ?_3 # <<=
694 case OO_LessLessEqual: Out << "?_3"; break;
695 // <operator-name> ::= ?_4 # &=
696 case OO_AmpEqual: Out << "?_4"; break;
697 // <operator-name> ::= ?_5 # |=
698 case OO_PipeEqual: Out << "?_5"; break;
699 // <operator-name> ::= ?_6 # ^=
700 case OO_CaretEqual: Out << "?_6"; break;
701 // ?_7 # vftable
702 // ?_8 # vbtable
703 // ?_9 # vcall
704 // ?_A # typeof
705 // ?_B # local static guard
706 // ?_C # string
707 // ?_D # vbase destructor
708 // ?_E # vector deleting destructor
709 // ?_F # default constructor closure
710 // ?_G # scalar deleting destructor
711 // ?_H # vector constructor iterator
712 // ?_I # vector destructor iterator
713 // ?_J # vector vbase constructor iterator
714 // ?_K # virtual displacement map
715 // ?_L # eh vector constructor iterator
716 // ?_M # eh vector destructor iterator
717 // ?_N # eh vector vbase constructor iterator
718 // ?_O # copy constructor closure
719 // ?_P<name> # udt returning <name>
720 // ?_Q # <unknown>
721 // ?_R0 # RTTI Type Descriptor
722 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
723 // ?_R2 # RTTI Base Class Array
724 // ?_R3 # RTTI Class Hierarchy Descriptor
725 // ?_R4 # RTTI Complete Object Locator
726 // ?_S # local vftable
727 // ?_T # local vftable constructor closure
728 // <operator-name> ::= ?_U # new[]
729 case OO_Array_New: Out << "?_U"; break;
730 // <operator-name> ::= ?_V # delete[]
731 case OO_Array_Delete: Out << "?_V"; break;
732
733 case OO_Conditional: {
734 DiagnosticsEngine &Diags = Context.getDiags();
735 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
736 "cannot mangle this conditional operator yet");
737 Diags.Report(Loc, DiagID);
738 break;
739 }
740
741 case OO_None:
742 case NUM_OVERLOADED_OPERATORS:
743 llvm_unreachable("Not an overloaded operator");
744 }
745}
746
747void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
748 // <source name> ::= <identifier> @
749 std::string key = II->getNameStart();
750 BackRefMap::iterator Found;
751 if (UseNameBackReferences)
752 Found = NameBackReferences.find(key);
753 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
754 Out << II->getName() << '@';
755 if (UseNameBackReferences && NameBackReferences.size() < 10) {
756 size_t Size = NameBackReferences.size();
757 NameBackReferences[key] = Size;
758 }
759 } else {
760 Out << Found->second;
761 }
762}
763
764void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
765 Context.mangleObjCMethodName(MD, Out);
766}
767
768// Find out how many function decls live above this one and return an integer
769// suitable for use as the number in a numbered anonymous scope.
770// TODO: Memoize.
771static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
772 const DeclContext *DC = FD->getParent();
773 int level = 1;
774
775 while (DC && !DC->isTranslationUnit()) {
776 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
777 DC = DC->getParent();
778 }
779
780 return 2*level;
781}
782
783void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
784 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
785 // <numbered-anonymous-scope> ::= ? <number>
786 // Even though the name is rendered in reverse order (e.g.
787 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
788 // innermost. So a method bar in class C local to function foo gets mangled
789 // as something like:
790 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
791 // This is more apparent when you have a type nested inside a method of a
792 // type nested inside a function. A method baz in class D local to method
793 // bar of class C local to function foo gets mangled as:
794 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
795 // This scheme is general enough to support GCC-style nested
796 // functions. You could have a method baz of class C inside a function bar
797 // inside a function foo, like so:
798 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
799 int NestLevel = getLocalNestingLevel(FD);
800 Out << '?';
801 mangleNumber(NestLevel);
802 Out << '?';
803 mangle(FD, "?");
804}
805
806void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
807 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000808 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000809 // <template-name> ::= <unscoped-template-name> <template-args>
810 // ::= <substitution>
811 // Always start with the unqualified name.
812
813 // Templates have their own context for back references.
814 ArgBackRefMap OuterArgsContext;
815 BackRefMap OuterTemplateContext;
816 NameBackReferences.swap(OuterTemplateContext);
817 TypeBackReferences.swap(OuterArgsContext);
818
819 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000820 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000821
822 // Restore the previous back reference contexts.
823 NameBackReferences.swap(OuterTemplateContext);
824 TypeBackReferences.swap(OuterArgsContext);
825}
826
827void
828MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
829 // <unscoped-template-name> ::= ?$ <unqualified-name>
830 Out << "?$";
831 mangleUnqualifiedName(TD);
832}
833
834void
835MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
836 bool IsBoolean) {
837 // <integer-literal> ::= $0 <number>
838 Out << "$0";
839 // Make sure booleans are encoded as 0/1.
840 if (IsBoolean && Value.getBoolValue())
841 mangleNumber(1);
842 else
843 mangleNumber(Value);
844}
845
846void
847MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
848 // See if this is a constant expression.
849 llvm::APSInt Value;
850 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
851 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
852 return;
853 }
854
855 // As bad as this diagnostic is, it's better than crashing.
856 DiagnosticsEngine &Diags = Context.getDiags();
857 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
858 "cannot yet mangle expression type %0");
859 Diags.Report(E->getExprLoc(), DiagID)
860 << E->getStmtClassName() << E->getSourceRange();
861}
862
863void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000864MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
865 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000866 // <template-args> ::= {<type> | <integer-literal>}+ @
867 unsigned NumTemplateArgs = TemplateArgs.size();
868 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000869 const TemplateArgument &TA = TemplateArgs[i];
Reid Kleckner5d90d182013-07-02 18:10:07 +0000870 mangleTemplateArg(TD, TA, i);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000871 }
872 Out << '@';
873}
874
Reid Kleckner5d90d182013-07-02 18:10:07 +0000875void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
876 const TemplateArgument &TA,
877 int ArgIndex) {
878 switch (TA.getKind()) {
879 case TemplateArgument::Null:
880 llvm_unreachable("Can't mangle null template arguments!");
881 case TemplateArgument::Type: {
882 QualType T = TA.getAsType();
883 mangleType(T, SourceRange(), QMM_Escape);
884 break;
885 }
886 case TemplateArgument::Declaration:
887 mangle(cast<NamedDecl>(TA.getAsDecl()), "$1?");
888 break;
889 case TemplateArgument::Integral:
890 mangleIntegerLiteral(TA.getAsIntegral(),
891 TA.getIntegralType()->isBooleanType());
892 break;
David Majnemer7802fc92013-08-05 21:33:59 +0000893 case TemplateArgument::NullPtr:
894 Out << "$0A@";
895 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000896 case TemplateArgument::Expression:
897 mangleExpression(TA.getAsExpr());
898 break;
899 case TemplateArgument::Pack:
900 // Unlike Itanium, there is no character code to indicate an argument pack.
901 // FIXME: ArgIndex will be off, but we only use if for diagnostics that
902 // should ultimately be removed.
903 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
904 I != E; ++I)
905 mangleTemplateArg(TD, *I, ArgIndex);
906 break;
907 case TemplateArgument::Template:
David Majnemer02c44f02013-08-05 22:26:46 +0000908 mangleType(cast<TagDecl>(
909 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
910 break;
David Majnemer7802fc92013-08-05 21:33:59 +0000911 case TemplateArgument::TemplateExpansion: {
Reid Kleckner5d90d182013-07-02 18:10:07 +0000912 // Issue a diagnostic.
913 DiagnosticsEngine &Diags = Context.getDiags();
914 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
915 "cannot mangle template argument %0 of kind %select{ERROR|ERROR|"
916 "pointer/reference|nullptr|integral|template|template pack expansion|"
917 "ERROR|parameter pack}1 yet");
918 Diags.Report(TD->getLocation(), DiagID)
919 << ArgIndex + 1
920 << TA.getKind()
921 << TD->getSourceRange();
922 }
923 }
924}
925
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000926void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
927 bool IsMember) {
928 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
929 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
930 // 'I' means __restrict (32/64-bit).
931 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
932 // keyword!
933 // <base-cvr-qualifiers> ::= A # near
934 // ::= B # near const
935 // ::= C # near volatile
936 // ::= D # near const volatile
937 // ::= E # far (16-bit)
938 // ::= F # far const (16-bit)
939 // ::= G # far volatile (16-bit)
940 // ::= H # far const volatile (16-bit)
941 // ::= I # huge (16-bit)
942 // ::= J # huge const (16-bit)
943 // ::= K # huge volatile (16-bit)
944 // ::= L # huge const volatile (16-bit)
945 // ::= M <basis> # based
946 // ::= N <basis> # based const
947 // ::= O <basis> # based volatile
948 // ::= P <basis> # based const volatile
949 // ::= Q # near member
950 // ::= R # near const member
951 // ::= S # near volatile member
952 // ::= T # near const volatile member
953 // ::= U # far member (16-bit)
954 // ::= V # far const member (16-bit)
955 // ::= W # far volatile member (16-bit)
956 // ::= X # far const volatile member (16-bit)
957 // ::= Y # huge member (16-bit)
958 // ::= Z # huge const member (16-bit)
959 // ::= 0 # huge volatile member (16-bit)
960 // ::= 1 # huge const volatile member (16-bit)
961 // ::= 2 <basis> # based member
962 // ::= 3 <basis> # based const member
963 // ::= 4 <basis> # based volatile member
964 // ::= 5 <basis> # based const volatile member
965 // ::= 6 # near function (pointers only)
966 // ::= 7 # far function (pointers only)
967 // ::= 8 # near method (pointers only)
968 // ::= 9 # far method (pointers only)
969 // ::= _A <basis> # based function (pointers only)
970 // ::= _B <basis> # based function (far?) (pointers only)
971 // ::= _C <basis> # based method (pointers only)
972 // ::= _D <basis> # based method (far?) (pointers only)
973 // ::= _E # block (Clang)
974 // <basis> ::= 0 # __based(void)
975 // ::= 1 # __based(segment)?
976 // ::= 2 <name> # __based(name)
977 // ::= 3 # ?
978 // ::= 4 # ?
979 // ::= 5 # not really based
980 bool HasConst = Quals.hasConst(),
981 HasVolatile = Quals.hasVolatile();
David Majnemerc0e64f32013-08-05 22:43:06 +0000982
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000983 if (!IsMember) {
984 if (HasConst && HasVolatile) {
985 Out << 'D';
986 } else if (HasVolatile) {
987 Out << 'C';
988 } else if (HasConst) {
989 Out << 'B';
990 } else {
991 Out << 'A';
992 }
993 } else {
David Majnemerc0e64f32013-08-05 22:43:06 +0000994 if (PointersAre64Bit)
995 Out << 'E';
996
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000997 if (HasConst && HasVolatile) {
998 Out << 'T';
999 } else if (HasVolatile) {
1000 Out << 'S';
1001 } else if (HasConst) {
1002 Out << 'R';
1003 } else {
1004 Out << 'Q';
1005 }
1006 }
1007
1008 // FIXME: For now, just drop all extension qualifiers on the floor.
1009}
1010
1011void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1012 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1013 // ::= Q # const
1014 // ::= R # volatile
1015 // ::= S # const volatile
1016 bool HasConst = Quals.hasConst(),
1017 HasVolatile = Quals.hasVolatile();
1018 if (HasConst && HasVolatile) {
1019 Out << 'S';
1020 } else if (HasVolatile) {
1021 Out << 'R';
1022 } else if (HasConst) {
1023 Out << 'Q';
1024 } else {
1025 Out << 'P';
1026 }
1027}
1028
1029void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1030 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001031 // MSVC will backreference two canonically equivalent types that have slightly
1032 // different manglings when mangled alone.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001033 void *TypePtr = getASTContext().getCanonicalType(T).getAsOpaquePtr();
1034 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1035
1036 if (Found == TypeBackReferences.end()) {
1037 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1038
Reid Klecknerf21818d2013-06-24 19:21:52 +00001039 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1040 QualType OT = DT->getOriginalType();
1041 if (const ArrayType *AT = getASTContext().getAsArrayType(OT)) {
1042 mangleDecayedArrayType(AT, false);
1043 } else if (const FunctionType *FT = OT->getAs<FunctionType>()) {
1044 Out << "P6";
1045 mangleFunctionType(FT, 0, false, false);
1046 } else {
1047 llvm_unreachable("unexpected decayed type");
1048 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001049 } else {
1050 mangleType(T, Range, QMM_Drop);
1051 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001052
1053 // See if it's worth creating a back reference.
1054 // Only types longer than 1 character are considered
1055 // and only 10 back references slots are available:
1056 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1057 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1058 size_t Size = TypeBackReferences.size();
1059 TypeBackReferences[TypePtr] = Size;
1060 }
1061 } else {
1062 Out << Found->second;
1063 }
1064}
1065
1066void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001067 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001068 // Don't use the canonical types. MSVC includes things like 'const' on
1069 // pointer arguments to function pointers that canonicalization strips away.
1070 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001071 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001072 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1073 // If there were any Quals, getAsArrayType() pushed them onto the array
1074 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001075 if (QMM == QMM_Mangle)
1076 Out << 'A';
1077 else if (QMM == QMM_Escape || QMM == QMM_Result)
1078 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001079 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001080 return;
1081 }
1082
1083 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1084 T->isBlockPointerType();
1085
1086 switch (QMM) {
1087 case QMM_Drop:
1088 break;
1089 case QMM_Mangle:
1090 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1091 Out << '6';
1092 mangleFunctionType(FT, 0, false, false);
1093 return;
1094 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001095 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001096 break;
1097 case QMM_Escape:
1098 if (!IsPointer && Quals) {
1099 Out << "$$C";
1100 mangleQualifiers(Quals, false);
1101 }
1102 break;
1103 case QMM_Result:
1104 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1105 Out << '?';
1106 mangleQualifiers(Quals, false);
1107 }
1108 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001109 }
1110
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001111 // We have to mangle these now, while we still have enough information.
1112 if (IsPointer)
1113 manglePointerQualifiers(Quals);
1114 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001115
1116 switch (ty->getTypeClass()) {
1117#define ABSTRACT_TYPE(CLASS, PARENT)
1118#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1119 case Type::CLASS: \
1120 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1121 return;
1122#define TYPE(CLASS, PARENT) \
1123 case Type::CLASS: \
1124 mangleType(cast<CLASS##Type>(ty), Range); \
1125 break;
1126#include "clang/AST/TypeNodes.def"
1127#undef ABSTRACT_TYPE
1128#undef NON_CANONICAL_TYPE
1129#undef TYPE
1130 }
1131}
1132
1133void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1134 SourceRange Range) {
1135 // <type> ::= <builtin-type>
1136 // <builtin-type> ::= X # void
1137 // ::= C # signed char
1138 // ::= D # char
1139 // ::= E # unsigned char
1140 // ::= F # short
1141 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1142 // ::= H # int
1143 // ::= I # unsigned int
1144 // ::= J # long
1145 // ::= K # unsigned long
1146 // L # <none>
1147 // ::= M # float
1148 // ::= N # double
1149 // ::= O # long double (__float80 is mangled differently)
1150 // ::= _J # long long, __int64
1151 // ::= _K # unsigned long long, __int64
1152 // ::= _L # __int128
1153 // ::= _M # unsigned __int128
1154 // ::= _N # bool
1155 // _O # <array in parameter>
1156 // ::= _T # __float80 (Intel)
1157 // ::= _W # wchar_t
1158 // ::= _Z # __float80 (Digital Mars)
1159 switch (T->getKind()) {
1160 case BuiltinType::Void: Out << 'X'; break;
1161 case BuiltinType::SChar: Out << 'C'; break;
1162 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1163 case BuiltinType::UChar: Out << 'E'; break;
1164 case BuiltinType::Short: Out << 'F'; break;
1165 case BuiltinType::UShort: Out << 'G'; break;
1166 case BuiltinType::Int: Out << 'H'; break;
1167 case BuiltinType::UInt: Out << 'I'; break;
1168 case BuiltinType::Long: Out << 'J'; break;
1169 case BuiltinType::ULong: Out << 'K'; break;
1170 case BuiltinType::Float: Out << 'M'; break;
1171 case BuiltinType::Double: Out << 'N'; break;
1172 // TODO: Determine size and mangle accordingly
1173 case BuiltinType::LongDouble: Out << 'O'; break;
1174 case BuiltinType::LongLong: Out << "_J"; break;
1175 case BuiltinType::ULongLong: Out << "_K"; break;
1176 case BuiltinType::Int128: Out << "_L"; break;
1177 case BuiltinType::UInt128: Out << "_M"; break;
1178 case BuiltinType::Bool: Out << "_N"; break;
1179 case BuiltinType::WChar_S:
1180 case BuiltinType::WChar_U: Out << "_W"; break;
1181
1182#define BUILTIN_TYPE(Id, SingletonId)
1183#define PLACEHOLDER_TYPE(Id, SingletonId) \
1184 case BuiltinType::Id:
1185#include "clang/AST/BuiltinTypes.def"
1186 case BuiltinType::Dependent:
1187 llvm_unreachable("placeholder types shouldn't get to name mangling");
1188
1189 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1190 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1191 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001192
1193 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1194 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1195 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1196 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1197 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1198 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001199 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001200 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001201
1202 case BuiltinType::NullPtr: Out << "$$T"; break;
1203
1204 case BuiltinType::Char16:
1205 case BuiltinType::Char32:
1206 case BuiltinType::Half: {
1207 DiagnosticsEngine &Diags = Context.getDiags();
1208 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1209 "cannot mangle this built-in %0 type yet");
1210 Diags.Report(Range.getBegin(), DiagID)
1211 << T->getName(Context.getASTContext().getPrintingPolicy())
1212 << Range;
1213 break;
1214 }
1215 }
1216}
1217
1218// <type> ::= <function-type>
1219void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1220 SourceRange) {
1221 // Structors only appear in decls, so at this point we know it's not a
1222 // structor type.
1223 // FIXME: This may not be lambda-friendly.
1224 Out << "$$A6";
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001225 mangleFunctionType(T, NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001226}
1227void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1228 SourceRange) {
1229 llvm_unreachable("Can't mangle K&R function prototypes");
1230}
1231
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001232void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1233 const FunctionDecl *D,
1234 bool IsStructor,
1235 bool IsInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001236 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1237 // <return-type> <argument-list> <throw-spec>
1238 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1239
Reid Klecknerf21818d2013-06-24 19:21:52 +00001240 SourceRange Range;
1241 if (D) Range = D->getSourceRange();
1242
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001243 // If this is a C++ instance method, mangle the CVR qualifiers for the
1244 // this pointer.
1245 if (IsInstMethod)
1246 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
1247
1248 mangleCallingConvention(T, IsInstMethod);
1249
1250 // <return-type> ::= <type>
1251 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001252 if (IsStructor) {
1253 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1254 StructorType == Dtor_Deleting) {
1255 // The scalar deleting destructor takes an extra int argument.
1256 // However, the FunctionType generated has 0 arguments.
1257 // FIXME: This is a temporary hack.
1258 // Maybe should fix the FunctionType creation instead?
1259 Out << "PAXI@Z";
1260 return;
1261 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001262 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001263 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001264 mangleType(Proto->getResultType(), Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001265 }
1266
1267 // <argument-list> ::= X # void
1268 // ::= <type>+ @
1269 // ::= <type>* Z # varargs
1270 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1271 Out << 'X';
1272 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001273 // Happens for function pointer type arguments for example.
1274 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1275 ArgEnd = Proto->arg_type_end();
1276 Arg != ArgEnd; ++Arg)
1277 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001278 // <builtin-type> ::= Z # ellipsis
1279 if (Proto->isVariadic())
1280 Out << 'Z';
1281 else
1282 Out << '@';
1283 }
1284
1285 mangleThrowSpecification(Proto);
1286}
1287
1288void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001289 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1290 // # pointer. in 64-bit mode *all*
1291 // # 'this' pointers are 64-bit.
1292 // ::= <global-function>
1293 // <member-function> ::= A # private: near
1294 // ::= B # private: far
1295 // ::= C # private: static near
1296 // ::= D # private: static far
1297 // ::= E # private: virtual near
1298 // ::= F # private: virtual far
1299 // ::= G # private: thunk near
1300 // ::= H # private: thunk far
1301 // ::= I # protected: near
1302 // ::= J # protected: far
1303 // ::= K # protected: static near
1304 // ::= L # protected: static far
1305 // ::= M # protected: virtual near
1306 // ::= N # protected: virtual far
1307 // ::= O # protected: thunk near
1308 // ::= P # protected: thunk far
1309 // ::= Q # public: near
1310 // ::= R # public: far
1311 // ::= S # public: static near
1312 // ::= T # public: static far
1313 // ::= U # public: virtual near
1314 // ::= V # public: virtual far
1315 // ::= W # public: thunk near
1316 // ::= X # public: thunk far
1317 // <global-function> ::= Y # global near
1318 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001319 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1320 switch (MD->getAccess()) {
1321 default:
1322 case AS_private:
1323 if (MD->isStatic())
1324 Out << 'C';
1325 else if (MD->isVirtual())
1326 Out << 'E';
1327 else
1328 Out << 'A';
1329 break;
1330 case AS_protected:
1331 if (MD->isStatic())
1332 Out << 'K';
1333 else if (MD->isVirtual())
1334 Out << 'M';
1335 else
1336 Out << 'I';
1337 break;
1338 case AS_public:
1339 if (MD->isStatic())
1340 Out << 'S';
1341 else if (MD->isVirtual())
1342 Out << 'U';
1343 else
1344 Out << 'Q';
1345 }
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001346 if (PointersAre64Bit && !MD->isStatic())
1347 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001348 } else
1349 Out << 'Y';
1350}
1351void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
1352 bool IsInstMethod) {
1353 // <calling-convention> ::= A # __cdecl
1354 // ::= B # __export __cdecl
1355 // ::= C # __pascal
1356 // ::= D # __export __pascal
1357 // ::= E # __thiscall
1358 // ::= F # __export __thiscall
1359 // ::= G # __stdcall
1360 // ::= H # __export __stdcall
1361 // ::= I # __fastcall
1362 // ::= J # __export __fastcall
1363 // The 'export' calling conventions are from a bygone era
1364 // (*cough*Win16*cough*) when functions were declared for export with
1365 // that keyword. (It didn't actually export them, it just made them so
1366 // that they could be in a DLL and somebody from another module could call
1367 // them.)
1368 CallingConv CC = T->getCallConv();
1369 if (CC == CC_Default) {
1370 if (IsInstMethod) {
1371 const FunctionProtoType *FPT =
1372 T->getCanonicalTypeUnqualified().castAs<FunctionProtoType>();
1373 bool isVariadic = FPT->isVariadic();
1374 CC = getASTContext().getDefaultCXXMethodCallConv(isVariadic);
1375 } else {
1376 CC = CC_C;
1377 }
1378 }
1379 switch (CC) {
1380 default:
1381 llvm_unreachable("Unsupported CC for mangling");
1382 case CC_Default:
1383 case CC_C: Out << 'A'; break;
1384 case CC_X86Pascal: Out << 'C'; break;
1385 case CC_X86ThisCall: Out << 'E'; break;
1386 case CC_X86StdCall: Out << 'G'; break;
1387 case CC_X86FastCall: Out << 'I'; break;
1388 }
1389}
1390void MicrosoftCXXNameMangler::mangleThrowSpecification(
1391 const FunctionProtoType *FT) {
1392 // <throw-spec> ::= Z # throw(...) (default)
1393 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1394 // ::= <type>+
1395 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1396 // all actually mangled as 'Z'. (They're ignored because their associated
1397 // functionality isn't implemented, and probably never will be.)
1398 Out << 'Z';
1399}
1400
1401void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1402 SourceRange Range) {
1403 // Probably should be mangled as a template instantiation; need to see what
1404 // VC does first.
1405 DiagnosticsEngine &Diags = Context.getDiags();
1406 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1407 "cannot mangle this unresolved dependent type yet");
1408 Diags.Report(Range.getBegin(), DiagID)
1409 << Range;
1410}
1411
1412// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1413// <union-type> ::= T <name>
1414// <struct-type> ::= U <name>
1415// <class-type> ::= V <name>
1416// <enum-type> ::= W <size> <name>
1417void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001418 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001419}
1420void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001421 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001422}
David Majnemer02c44f02013-08-05 22:26:46 +00001423void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1424 switch (TD->getTagKind()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001425 case TTK_Union:
1426 Out << 'T';
1427 break;
1428 case TTK_Struct:
1429 case TTK_Interface:
1430 Out << 'U';
1431 break;
1432 case TTK_Class:
1433 Out << 'V';
1434 break;
1435 case TTK_Enum:
1436 Out << 'W';
1437 Out << getASTContext().getTypeSizeInChars(
David Majnemer02c44f02013-08-05 22:26:46 +00001438 cast<EnumDecl>(TD)->getIntegerType()).getQuantity();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001439 break;
1440 }
David Majnemer02c44f02013-08-05 22:26:46 +00001441 mangleName(TD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001442}
1443
1444// <type> ::= <array-type>
1445// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1446// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001447// <element-type> # as global, E is never required
1448// ::= Q E? <cvr-qualifiers> [Y <dimension-count> <dimension>+]
1449// <element-type> # as param, E is required for 64-bit
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001450// It's supposed to be the other way around, but for some strange reason, it
1451// isn't. Today this behavior is retained for the sole purpose of backwards
1452// compatibility.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001453void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T,
1454 bool IsGlobal) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001455 // This isn't a recursive mangling, so now we have to do it all in this
1456 // one call.
1457 if (IsGlobal) {
1458 manglePointerQualifiers(T->getElementType().getQualifiers());
1459 } else {
1460 Out << 'Q';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001461 if (PointersAre64Bit)
1462 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001463 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001464 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001465}
1466void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1467 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001468 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001469}
1470void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1471 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001472 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001473}
1474void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1475 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001476 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001477}
1478void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1479 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001480 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001481}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001482void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001483 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001484 SmallVector<llvm::APInt, 3> Dimensions;
1485 for (;;) {
1486 if (const ConstantArrayType *CAT =
1487 getASTContext().getAsConstantArrayType(ElementTy)) {
1488 Dimensions.push_back(CAT->getSize());
1489 ElementTy = CAT->getElementType();
1490 } else if (ElementTy->isVariableArrayType()) {
1491 const VariableArrayType *VAT =
1492 getASTContext().getAsVariableArrayType(ElementTy);
1493 DiagnosticsEngine &Diags = Context.getDiags();
1494 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1495 "cannot mangle this variable-length array yet");
1496 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1497 << VAT->getBracketsRange();
1498 return;
1499 } else if (ElementTy->isDependentSizedArrayType()) {
1500 // The dependent expression has to be folded into a constant (TODO).
1501 const DependentSizedArrayType *DSAT =
1502 getASTContext().getAsDependentSizedArrayType(ElementTy);
1503 DiagnosticsEngine &Diags = Context.getDiags();
1504 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1505 "cannot mangle this dependent-length array yet");
1506 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1507 << DSAT->getBracketsRange();
1508 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001509 } else if (const IncompleteArrayType *IAT =
1510 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1511 Dimensions.push_back(llvm::APInt(32, 0));
1512 ElementTy = IAT->getElementType();
1513 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001514 else break;
1515 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001516 Out << 'Y';
1517 // <dimension-count> ::= <number> # number of extra dimensions
1518 mangleNumber(Dimensions.size());
1519 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1520 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001521 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001522}
1523
1524// <type> ::= <pointer-to-member-type>
1525// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1526// <class name> <type>
1527void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1528 SourceRange Range) {
1529 QualType PointeeType = T->getPointeeType();
1530 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1531 Out << '8';
1532 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001533 mangleFunctionType(FPT, NULL, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001534 } else {
1535 mangleQualifiers(PointeeType.getQualifiers(), true);
1536 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001537 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001538 }
1539}
1540
1541void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1542 SourceRange Range) {
1543 DiagnosticsEngine &Diags = Context.getDiags();
1544 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1545 "cannot mangle this template type parameter type yet");
1546 Diags.Report(Range.getBegin(), DiagID)
1547 << Range;
1548}
1549
1550void MicrosoftCXXNameMangler::mangleType(
1551 const SubstTemplateTypeParmPackType *T,
1552 SourceRange Range) {
1553 DiagnosticsEngine &Diags = Context.getDiags();
1554 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1555 "cannot mangle this substituted parameter pack yet");
1556 Diags.Report(Range.getBegin(), DiagID)
1557 << Range;
1558}
1559
1560// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001561// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1562// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001563void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1564 SourceRange Range) {
1565 QualType PointeeTy = T->getPointeeType();
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001566 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1567 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001568 mangleType(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001569}
1570void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1571 SourceRange Range) {
1572 // Object pointers never have qualifiers.
1573 Out << 'A';
1574 mangleType(T->getPointeeType(), Range);
1575}
1576
1577// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001578// <reference-type> ::= A E? <cvr-qualifiers> <type>
1579// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001580void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1581 SourceRange Range) {
1582 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001583 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1584 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001585 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001586}
1587
1588// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001589// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1590// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001591void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1592 SourceRange Range) {
1593 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001594 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1595 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001596 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001597}
1598
1599void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1600 SourceRange Range) {
1601 DiagnosticsEngine &Diags = Context.getDiags();
1602 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1603 "cannot mangle this complex number type yet");
1604 Diags.Report(Range.getBegin(), DiagID)
1605 << Range;
1606}
1607
1608void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1609 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001610 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1611 assert(ET && "vectors with non-builtin elements are unsupported");
1612 uint64_t Width = getASTContext().getTypeSize(T);
1613 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1614 // doesn't match the Intel types uses a custom mangling below.
1615 bool IntelVector = true;
1616 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1617 Out << "T__m64";
1618 } else if (Width == 128 || Width == 256) {
1619 if (ET->getKind() == BuiltinType::Float)
1620 Out << "T__m" << Width;
1621 else if (ET->getKind() == BuiltinType::LongLong)
1622 Out << "T__m" << Width << 'i';
1623 else if (ET->getKind() == BuiltinType::Double)
1624 Out << "U__m" << Width << 'd';
1625 else
1626 IntelVector = false;
1627 } else {
1628 IntelVector = false;
1629 }
1630
1631 if (!IntelVector) {
1632 // The MS ABI doesn't have a special mangling for vector types, so we define
1633 // our own mangling to handle uses of __vector_size__ on user-specified
1634 // types, and for extensions like __v4sf.
1635 Out << "T__clang_vec" << T->getNumElements() << '_';
1636 mangleType(ET, Range);
1637 }
1638
1639 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001640}
Reid Kleckner1232e272013-03-26 16:56:59 +00001641
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001642void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1643 SourceRange Range) {
1644 DiagnosticsEngine &Diags = Context.getDiags();
1645 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1646 "cannot mangle this extended vector type yet");
1647 Diags.Report(Range.getBegin(), DiagID)
1648 << Range;
1649}
1650void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1651 SourceRange Range) {
1652 DiagnosticsEngine &Diags = Context.getDiags();
1653 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1654 "cannot mangle this dependent-sized extended vector type yet");
1655 Diags.Report(Range.getBegin(), DiagID)
1656 << Range;
1657}
1658
1659void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1660 SourceRange) {
1661 // ObjC interfaces have structs underlying them.
1662 Out << 'U';
1663 mangleName(T->getDecl());
1664}
1665
1666void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1667 SourceRange Range) {
1668 // We don't allow overloading by different protocol qualification,
1669 // so mangling them isn't necessary.
1670 mangleType(T->getBaseType(), Range);
1671}
1672
1673void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1674 SourceRange Range) {
1675 Out << "_E";
1676
1677 QualType pointee = T->getPointeeType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001678 mangleFunctionType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001679}
1680
1681void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T,
1682 SourceRange Range) {
1683 DiagnosticsEngine &Diags = Context.getDiags();
1684 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1685 "cannot mangle this injected class name type yet");
1686 Diags.Report(Range.getBegin(), DiagID)
1687 << Range;
1688}
1689
1690void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1691 SourceRange Range) {
1692 DiagnosticsEngine &Diags = Context.getDiags();
1693 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1694 "cannot mangle this template specialization type yet");
1695 Diags.Report(Range.getBegin(), DiagID)
1696 << Range;
1697}
1698
1699void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1700 SourceRange Range) {
1701 DiagnosticsEngine &Diags = Context.getDiags();
1702 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1703 "cannot mangle this dependent name type yet");
1704 Diags.Report(Range.getBegin(), DiagID)
1705 << Range;
1706}
1707
1708void MicrosoftCXXNameMangler::mangleType(
1709 const DependentTemplateSpecializationType *T,
1710 SourceRange Range) {
1711 DiagnosticsEngine &Diags = Context.getDiags();
1712 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1713 "cannot mangle this dependent template specialization type yet");
1714 Diags.Report(Range.getBegin(), DiagID)
1715 << Range;
1716}
1717
1718void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1719 SourceRange Range) {
1720 DiagnosticsEngine &Diags = Context.getDiags();
1721 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1722 "cannot mangle this pack expansion yet");
1723 Diags.Report(Range.getBegin(), DiagID)
1724 << Range;
1725}
1726
1727void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1728 SourceRange Range) {
1729 DiagnosticsEngine &Diags = Context.getDiags();
1730 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1731 "cannot mangle this typeof(type) yet");
1732 Diags.Report(Range.getBegin(), DiagID)
1733 << Range;
1734}
1735
1736void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1737 SourceRange Range) {
1738 DiagnosticsEngine &Diags = Context.getDiags();
1739 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1740 "cannot mangle this typeof(expression) yet");
1741 Diags.Report(Range.getBegin(), DiagID)
1742 << Range;
1743}
1744
1745void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1746 SourceRange Range) {
1747 DiagnosticsEngine &Diags = Context.getDiags();
1748 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1749 "cannot mangle this decltype() yet");
1750 Diags.Report(Range.getBegin(), DiagID)
1751 << Range;
1752}
1753
1754void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1755 SourceRange Range) {
1756 DiagnosticsEngine &Diags = Context.getDiags();
1757 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1758 "cannot mangle this unary transform type yet");
1759 Diags.Report(Range.getBegin(), DiagID)
1760 << Range;
1761}
1762
1763void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1764 DiagnosticsEngine &Diags = Context.getDiags();
1765 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1766 "cannot mangle this 'auto' type yet");
1767 Diags.Report(Range.getBegin(), DiagID)
1768 << Range;
1769}
1770
1771void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1772 SourceRange Range) {
1773 DiagnosticsEngine &Diags = Context.getDiags();
1774 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1775 "cannot mangle this C11 atomic type yet");
1776 Diags.Report(Range.getBegin(), DiagID)
1777 << Range;
1778}
1779
1780void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1781 raw_ostream &Out) {
1782 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1783 "Invalid mangleName() call, argument is not a variable or function!");
1784 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1785 "Invalid mangleName() call on 'structor decl!");
1786
1787 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1788 getASTContext().getSourceManager(),
1789 "Mangling declaration");
1790
1791 MicrosoftCXXNameMangler Mangler(*this, Out);
1792 return Mangler.mangle(D);
1793}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001794
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001795void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1796 const ThunkInfo &Thunk,
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001797 raw_ostream &Out) {
1798 // FIXME: this is not yet a complete implementation, but merely a
1799 // reasonably-working stub to avoid crashing when required to emit a thunk.
1800 MicrosoftCXXNameMangler Mangler(*this, Out);
1801 Out << "\01?";
1802 Mangler.mangleName(MD);
1803 if (Thunk.This.NonVirtual != 0) {
1804 // FIXME: add support for protected/private or use mangleFunctionClass.
1805 Out << "W";
1806 llvm::APSInt APSNumber(/*BitWidth=*/32 /*FIXME: check on x64*/,
1807 /*isUnsigned=*/true);
1808 APSNumber = -Thunk.This.NonVirtual;
1809 Mangler.mangleNumber(APSNumber);
1810 } else {
1811 // FIXME: add support for protected/private or use mangleFunctionClass.
1812 Out << "Q";
1813 }
1814 // FIXME: mangle return adjustment? Most likely includes using an overridee FPT?
1815 Mangler.mangleFunctionType(MD->getType()->castAs<FunctionProtoType>(), MD, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001816}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001817
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001818void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1819 CXXDtorType Type,
1820 const ThisAdjustment &,
1821 raw_ostream &) {
1822 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1823 "cannot mangle thunk for this destructor yet");
1824 getDiags().Report(DD->getLocation(), DiagID);
1825}
Reid Kleckner90633022013-06-19 15:20:38 +00001826
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001827void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1828 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001829 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1830 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001831 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001832 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001833 MicrosoftCXXNameMangler Mangler(*this, Out);
1834 Mangler.getStream() << "\01??_7";
1835 Mangler.mangleName(RD);
Reid Kleckner90633022013-06-19 15:20:38 +00001836 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001837 // TODO: If the class has more than one vtable, mangle in the class it came
1838 // from.
1839 Mangler.getStream() << '@';
1840}
Reid Kleckner90633022013-06-19 15:20:38 +00001841
1842void MicrosoftMangleContext::mangleCXXVBTable(
1843 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1844 raw_ostream &Out) {
1845 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1846 // <cvr-qualifiers> [<name>] @
1847 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1848 // is always '7' for vbtables.
1849 MicrosoftCXXNameMangler Mangler(*this, Out);
1850 Mangler.getStream() << "\01??_8";
1851 Mangler.mangleName(Derived);
1852 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1853 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1854 E = BasePath.end();
1855 I != E; ++I) {
1856 Mangler.mangleName(*I);
1857 }
1858 Mangler.getStream() << '@';
1859}
1860
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001861void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1862 raw_ostream &) {
1863 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1864}
1865void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1866 int64_t Offset,
1867 const CXXRecordDecl *Type,
1868 raw_ostream &) {
1869 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1870}
1871void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1872 raw_ostream &) {
1873 // FIXME: Give a location...
1874 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1875 "cannot mangle RTTI descriptors for type %0 yet");
1876 getDiags().Report(DiagID)
1877 << T.getBaseTypeIdentifier();
1878}
1879void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1880 raw_ostream &) {
1881 // FIXME: Give a location...
1882 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1883 "cannot mangle the name of type %0 into RTTI descriptors yet");
1884 getDiags().Report(DiagID)
1885 << T.getBaseTypeIdentifier();
1886}
1887void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1888 CXXCtorType Type,
1889 raw_ostream & Out) {
1890 MicrosoftCXXNameMangler mangler(*this, Out);
1891 mangler.mangle(D);
1892}
1893void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1894 CXXDtorType Type,
1895 raw_ostream & Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001896 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001897 mangler.mangle(D);
1898}
1899void MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *VD,
1900 raw_ostream &) {
1901 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1902 "cannot mangle this reference temporary yet");
1903 getDiags().Report(VD->getLocation(), DiagID);
1904}
1905
1906MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
1907 DiagnosticsEngine &Diags) {
1908 return new MicrosoftMangleContext(Context, Diags);
1909}