blob: 143472f05969d411bd944dbf092515084e5f9889 [file] [log] [blame]
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001//===--- MicrosoftMangle.cpp - Microsoft Visual C++ Name Mangling ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides C++ name mangling targeting the Microsoft Visual C++ ABI.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Mangle.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/CharUnits.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclCXX.h"
20#include "clang/AST/DeclObjC.h"
21#include "clang/AST/DeclTemplate.h"
22#include "clang/AST/ExprCXX.h"
23#include "clang/Basic/ABI.h"
24#include "clang/Basic/DiagnosticOptions.h"
Reid Klecknerd6a08d12013-05-14 20:30:42 +000025#include "clang/Basic/TargetInfo.h"
Reid Klecknerf0219cd2013-05-22 17:16:39 +000026#include "llvm/ADT/StringMap.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000027
28using namespace clang;
29
30namespace {
31
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000032static const FunctionDecl *getStructor(const FunctionDecl *fn) {
33 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
34 return ftd->getTemplatedDecl();
35
36 return fn;
37}
38
Guy Benyei7f92f2d2012-12-18 14:30:41 +000039/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
40/// Microsoft Visual C++ ABI.
41class MicrosoftCXXNameMangler {
42 MangleContext &Context;
43 raw_ostream &Out;
44
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000045 /// The "structor" is the top-level declaration being mangled, if
46 /// that's not a template specialization; otherwise it's the pattern
47 /// for that specialization.
48 const NamedDecl *Structor;
49 unsigned StructorType;
50
Reid Klecknerf0219cd2013-05-22 17:16:39 +000051 typedef llvm::StringMap<unsigned> BackRefMap;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000052 BackRefMap NameBackReferences;
53 bool UseNameBackReferences;
54
55 typedef llvm::DenseMap<void*, unsigned> ArgBackRefMap;
56 ArgBackRefMap TypeBackReferences;
57
58 ASTContext &getASTContext() const { return Context.getASTContext(); }
59
Reid Klecknerd6a08d12013-05-14 20:30:42 +000060 // FIXME: If we add support for __ptr32/64 qualifiers, then we should push
61 // this check into mangleQualifiers().
62 const bool PointersAre64Bit;
63
Guy Benyei7f92f2d2012-12-18 14:30:41 +000064public:
Peter Collingbourneb70d1c32013-04-25 04:25:40 +000065 enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result };
66
Guy Benyei7f92f2d2012-12-18 14:30:41 +000067 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000068 : Context(C), Out(Out_),
69 Structor(0), StructorType(-1),
David Blaikiee7e94c92013-05-14 21:31:46 +000070 UseNameBackReferences(true),
Reid Klecknerd6a08d12013-05-14 20:30:42 +000071 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikiee7e94c92013-05-14 21:31:46 +000072 64) { }
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000073
74 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_,
75 const CXXDestructorDecl *D, CXXDtorType Type)
76 : Context(C), Out(Out_),
77 Structor(getStructor(D)), StructorType(Type),
David Blaikiee7e94c92013-05-14 21:31:46 +000078 UseNameBackReferences(true),
Reid Klecknerd6a08d12013-05-14 20:30:42 +000079 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikiee7e94c92013-05-14 21:31:46 +000080 64) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +000081
82 raw_ostream &getStream() const { return Out; }
83
84 void mangle(const NamedDecl *D, StringRef Prefix = "\01?");
85 void mangleName(const NamedDecl *ND);
86 void mangleFunctionEncoding(const FunctionDecl *FD);
87 void mangleVariableEncoding(const VarDecl *VD);
88 void mangleNumber(int64_t Number);
89 void mangleNumber(const llvm::APSInt &Value);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +000090 void mangleType(QualType T, SourceRange Range,
91 QualifierMangleMode QMM = QMM_Mangle);
Guy Benyei7f92f2d2012-12-18 14:30:41 +000092
93private:
94 void disableBackReferences() { UseNameBackReferences = false; }
95 void mangleUnqualifiedName(const NamedDecl *ND) {
96 mangleUnqualifiedName(ND, ND->getDeclName());
97 }
98 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
99 void mangleSourceName(const IdentifierInfo *II);
100 void manglePostfix(const DeclContext *DC, bool NoFunction=false);
101 void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc);
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000102 void mangleCXXDtorType(CXXDtorType T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000103 void mangleQualifiers(Qualifiers Quals, bool IsMember);
104 void manglePointerQualifiers(Qualifiers Quals);
105
106 void mangleUnscopedTemplateName(const TemplateDecl *ND);
107 void mangleTemplateInstantiationName(const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000108 const TemplateArgumentList &TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000109 void mangleObjCMethodName(const ObjCMethodDecl *MD);
110 void mangleLocalName(const FunctionDecl *FD);
111
112 void mangleArgumentType(QualType T, SourceRange Range);
113
114 // Declare manglers for every type class.
115#define ABSTRACT_TYPE(CLASS, PARENT)
116#define NON_CANONICAL_TYPE(CLASS, PARENT)
117#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \
118 SourceRange Range);
119#include "clang/AST/TypeNodes.def"
120#undef ABSTRACT_TYPE
121#undef NON_CANONICAL_TYPE
122#undef TYPE
123
124 void mangleType(const TagType*);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000125 void mangleFunctionType(const FunctionType *T, const FunctionDecl *D,
126 bool IsStructor, bool IsInstMethod);
127 void mangleDecayedArrayType(const ArrayType *T, bool IsGlobal);
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();
320 if (Ty->isPointerType() || Ty->isReferenceType()) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000321 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000322 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
323 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
324 // Global arrays are funny, too.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000325 mangleDecayedArrayType(AT, true);
326 if (AT->getElementType()->isArrayType())
327 Out << 'A';
328 else
329 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000330 } else {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000331 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000332 mangleQualifiers(Ty.getLocalQualifiers(), false);
333 }
334}
335
336void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
337 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
338 const DeclContext *DC = ND->getDeclContext();
339
340 // Always start with the unqualified name.
341 mangleUnqualifiedName(ND);
342
343 // If this is an extern variable declared locally, the relevant DeclContext
344 // is that of the containing namespace, or the translation unit.
345 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
346 while (!DC->isNamespace() && !DC->isTranslationUnit())
347 DC = DC->getParent();
348
349 manglePostfix(DC);
350
351 // Terminate the whole name with an '@'.
352 Out << '@';
353}
354
355void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
356 llvm::APSInt APSNumber(/*BitWidth=*/64, /*isUnsigned=*/false);
357 APSNumber = Number;
358 mangleNumber(APSNumber);
359}
360
361void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
362 // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
363 // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
364 // ::= [?] @ # 0 (alternate mangling, not emitted by VC)
365 if (Value.isSigned() && Value.isNegative()) {
366 Out << '?';
367 mangleNumber(llvm::APSInt(Value.abs()));
368 return;
369 }
370 llvm::APSInt Temp(Value);
371 // There's a special shorter mangling for 0, but Microsoft
372 // chose not to use it. Instead, 0 gets mangled as "A@". Oh well...
373 if (Value.uge(1) && Value.ule(10)) {
374 --Temp;
375 Temp.print(Out, false);
376 } else {
377 // We have to build up the encoding in reverse order, so it will come
378 // out right when we write it out.
379 char Encoding[64];
380 char *EndPtr = Encoding+sizeof(Encoding);
381 char *CurPtr = EndPtr;
382 llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned());
383 NibbleMask = 0xf;
384 do {
385 *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf);
386 Temp = Temp.lshr(4);
387 } while (Temp != 0);
388 Out.write(CurPtr, EndPtr-CurPtr);
389 Out << '@';
390 }
391}
392
393static const TemplateDecl *
Reid Klecknerf16216c2013-03-20 01:40:23 +0000394isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000395 // Check if we have a function template.
396 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
397 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000398 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000399 return TD;
400 }
401 }
402
403 // Check if we have a class template.
404 if (const ClassTemplateSpecializationDecl *Spec =
Reid Klecknerf16216c2013-03-20 01:40:23 +0000405 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
406 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000407 return Spec->getSpecializedTemplate();
408 }
409
410 return 0;
411}
412
413void
414MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
415 DeclarationName Name) {
416 // <unqualified-name> ::= <operator-name>
417 // ::= <ctor-dtor-name>
418 // ::= <source-name>
419 // ::= <template-name>
Reid Klecknerf16216c2013-03-20 01:40:23 +0000420
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000421 // Check if we have a template.
Reid Klecknerf16216c2013-03-20 01:40:23 +0000422 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000423 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
424 // We have a template.
425 // Here comes the tricky thing: if we need to mangle something like
426 // void foo(A::X<Y>, B::X<Y>),
427 // the X<Y> part is aliased. However, if you need to mangle
428 // void foo(A::X<A::Y>, A::X<B::Y>),
429 // the A::X<> part is not aliased.
430 // That said, from the mangler's perspective we have a structure like this:
431 // namespace[s] -> type[ -> template-parameters]
432 // but from the Clang perspective we have
433 // type [ -> template-parameters]
434 // \-> namespace[s]
435 // What we do is we create a new mangler, mangle the same type (without
436 // a namespace suffix) using the extra mangler with back references
437 // disabled (to avoid infinite recursion) and then use the mangled type
438 // name as a key to check the mangling of different types for aliasing.
439
440 std::string BackReferenceKey;
441 BackRefMap::iterator Found;
442 if (UseNameBackReferences) {
443 llvm::raw_string_ostream Stream(BackReferenceKey);
444 MicrosoftCXXNameMangler Extra(Context, Stream);
445 Extra.disableBackReferences();
446 Extra.mangleUnqualifiedName(ND, Name);
447 Stream.flush();
448
449 Found = NameBackReferences.find(BackReferenceKey);
450 }
451 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000452 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000453 if (UseNameBackReferences && NameBackReferences.size() < 10) {
454 size_t Size = NameBackReferences.size();
455 NameBackReferences[BackReferenceKey] = Size;
456 }
457 } else {
458 Out << Found->second;
459 }
460 return;
461 }
462
463 switch (Name.getNameKind()) {
464 case DeclarationName::Identifier: {
465 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
466 mangleSourceName(II);
467 break;
468 }
469
470 // Otherwise, an anonymous entity. We must have a declaration.
471 assert(ND && "mangling empty name without declaration");
472
473 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
474 if (NS->isAnonymousNamespace()) {
475 Out << "?A@";
476 break;
477 }
478 }
479
480 // We must have an anonymous struct.
481 const TagDecl *TD = cast<TagDecl>(ND);
482 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
483 assert(TD->getDeclContext() == D->getDeclContext() &&
484 "Typedef should not be in another decl context!");
485 assert(D->getDeclName().getAsIdentifierInfo() &&
486 "Typedef was not named!");
487 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
488 break;
489 }
490
491 // When VC encounters an anonymous type with no tag and no typedef,
492 // it literally emits '<unnamed-tag>'.
493 Out << "<unnamed-tag>";
494 break;
495 }
496
497 case DeclarationName::ObjCZeroArgSelector:
498 case DeclarationName::ObjCOneArgSelector:
499 case DeclarationName::ObjCMultiArgSelector:
500 llvm_unreachable("Can't mangle Objective-C selector names here!");
501
502 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000503 if (ND == Structor) {
504 assert(StructorType == Ctor_Complete &&
505 "Should never be asked to mangle a ctor other than complete");
506 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000507 Out << "?0";
508 break;
509
510 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000511 if (ND == Structor)
512 // If the named decl is the C++ destructor we're mangling,
513 // use the type we were given.
514 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
515 else
516 // Otherwise, use the complete destructor name. This is relevant if a
517 // class with a destructor is declared within a destructor.
518 mangleCXXDtorType(Dtor_Complete);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000519 break;
520
521 case DeclarationName::CXXConversionFunctionName:
522 // <operator-name> ::= ?B # (cast)
523 // The target type is encoded as the return type.
524 Out << "?B";
525 break;
526
527 case DeclarationName::CXXOperatorName:
528 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
529 break;
530
531 case DeclarationName::CXXLiteralOperatorName: {
532 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
533 DiagnosticsEngine Diags = Context.getDiags();
534 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
535 "cannot mangle this literal operator yet");
536 Diags.Report(ND->getLocation(), DiagID);
537 break;
538 }
539
540 case DeclarationName::CXXUsingDirective:
541 llvm_unreachable("Can't mangle a using directive name!");
542 }
543}
544
545void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
546 bool NoFunction) {
547 // <postfix> ::= <unqualified-name> [<postfix>]
548 // ::= <substitution> [<postfix>]
549
550 if (!DC) return;
551
552 while (isa<LinkageSpecDecl>(DC))
553 DC = DC->getParent();
554
555 if (DC->isTranslationUnit())
556 return;
557
558 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedmane5798892013-07-10 01:13:27 +0000559 DiagnosticsEngine Diags = Context.getDiags();
560 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
561 "cannot mangle a local inside this block yet");
562 Diags.Report(BD->getLocation(), DiagID);
563
564 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
565 // for how this should be done.
566 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000567 Out << '@';
568 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000569 } else if (isa<CapturedDecl>(DC)) {
570 // Skip CapturedDecl context.
571 manglePostfix(DC->getParent(), NoFunction);
572 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000573 }
574
575 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
576 return;
577 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
578 mangleObjCMethodName(Method);
579 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
580 mangleLocalName(Func);
581 else {
582 mangleUnqualifiedName(cast<NamedDecl>(DC));
583 manglePostfix(DC->getParent(), NoFunction);
584 }
585}
586
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000587void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
588 switch (T) {
589 case Dtor_Deleting:
590 Out << "?_G";
591 return;
592 case Dtor_Base:
593 // FIXME: We should be asked to mangle base dtors.
594 // However, fixing this would require larger changes to the CodeGenModule.
595 // Please put llvm_unreachable here when CGM is changed.
596 // For now, just mangle a base dtor the same way as a complete dtor...
597 case Dtor_Complete:
598 Out << "?1";
599 return;
600 }
601 llvm_unreachable("Unsupported dtor type?");
602}
603
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000604void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
605 SourceLocation Loc) {
606 switch (OO) {
607 // ?0 # constructor
608 // ?1 # destructor
609 // <operator-name> ::= ?2 # new
610 case OO_New: Out << "?2"; break;
611 // <operator-name> ::= ?3 # delete
612 case OO_Delete: Out << "?3"; break;
613 // <operator-name> ::= ?4 # =
614 case OO_Equal: Out << "?4"; break;
615 // <operator-name> ::= ?5 # >>
616 case OO_GreaterGreater: Out << "?5"; break;
617 // <operator-name> ::= ?6 # <<
618 case OO_LessLess: Out << "?6"; break;
619 // <operator-name> ::= ?7 # !
620 case OO_Exclaim: Out << "?7"; break;
621 // <operator-name> ::= ?8 # ==
622 case OO_EqualEqual: Out << "?8"; break;
623 // <operator-name> ::= ?9 # !=
624 case OO_ExclaimEqual: Out << "?9"; break;
625 // <operator-name> ::= ?A # []
626 case OO_Subscript: Out << "?A"; break;
627 // ?B # conversion
628 // <operator-name> ::= ?C # ->
629 case OO_Arrow: Out << "?C"; break;
630 // <operator-name> ::= ?D # *
631 case OO_Star: Out << "?D"; break;
632 // <operator-name> ::= ?E # ++
633 case OO_PlusPlus: Out << "?E"; break;
634 // <operator-name> ::= ?F # --
635 case OO_MinusMinus: Out << "?F"; break;
636 // <operator-name> ::= ?G # -
637 case OO_Minus: Out << "?G"; break;
638 // <operator-name> ::= ?H # +
639 case OO_Plus: Out << "?H"; break;
640 // <operator-name> ::= ?I # &
641 case OO_Amp: Out << "?I"; break;
642 // <operator-name> ::= ?J # ->*
643 case OO_ArrowStar: Out << "?J"; break;
644 // <operator-name> ::= ?K # /
645 case OO_Slash: Out << "?K"; break;
646 // <operator-name> ::= ?L # %
647 case OO_Percent: Out << "?L"; break;
648 // <operator-name> ::= ?M # <
649 case OO_Less: Out << "?M"; break;
650 // <operator-name> ::= ?N # <=
651 case OO_LessEqual: Out << "?N"; break;
652 // <operator-name> ::= ?O # >
653 case OO_Greater: Out << "?O"; break;
654 // <operator-name> ::= ?P # >=
655 case OO_GreaterEqual: Out << "?P"; break;
656 // <operator-name> ::= ?Q # ,
657 case OO_Comma: Out << "?Q"; break;
658 // <operator-name> ::= ?R # ()
659 case OO_Call: Out << "?R"; break;
660 // <operator-name> ::= ?S # ~
661 case OO_Tilde: Out << "?S"; break;
662 // <operator-name> ::= ?T # ^
663 case OO_Caret: Out << "?T"; break;
664 // <operator-name> ::= ?U # |
665 case OO_Pipe: Out << "?U"; break;
666 // <operator-name> ::= ?V # &&
667 case OO_AmpAmp: Out << "?V"; break;
668 // <operator-name> ::= ?W # ||
669 case OO_PipePipe: Out << "?W"; break;
670 // <operator-name> ::= ?X # *=
671 case OO_StarEqual: Out << "?X"; break;
672 // <operator-name> ::= ?Y # +=
673 case OO_PlusEqual: Out << "?Y"; break;
674 // <operator-name> ::= ?Z # -=
675 case OO_MinusEqual: Out << "?Z"; break;
676 // <operator-name> ::= ?_0 # /=
677 case OO_SlashEqual: Out << "?_0"; break;
678 // <operator-name> ::= ?_1 # %=
679 case OO_PercentEqual: Out << "?_1"; break;
680 // <operator-name> ::= ?_2 # >>=
681 case OO_GreaterGreaterEqual: Out << "?_2"; break;
682 // <operator-name> ::= ?_3 # <<=
683 case OO_LessLessEqual: Out << "?_3"; break;
684 // <operator-name> ::= ?_4 # &=
685 case OO_AmpEqual: Out << "?_4"; break;
686 // <operator-name> ::= ?_5 # |=
687 case OO_PipeEqual: Out << "?_5"; break;
688 // <operator-name> ::= ?_6 # ^=
689 case OO_CaretEqual: Out << "?_6"; break;
690 // ?_7 # vftable
691 // ?_8 # vbtable
692 // ?_9 # vcall
693 // ?_A # typeof
694 // ?_B # local static guard
695 // ?_C # string
696 // ?_D # vbase destructor
697 // ?_E # vector deleting destructor
698 // ?_F # default constructor closure
699 // ?_G # scalar deleting destructor
700 // ?_H # vector constructor iterator
701 // ?_I # vector destructor iterator
702 // ?_J # vector vbase constructor iterator
703 // ?_K # virtual displacement map
704 // ?_L # eh vector constructor iterator
705 // ?_M # eh vector destructor iterator
706 // ?_N # eh vector vbase constructor iterator
707 // ?_O # copy constructor closure
708 // ?_P<name> # udt returning <name>
709 // ?_Q # <unknown>
710 // ?_R0 # RTTI Type Descriptor
711 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
712 // ?_R2 # RTTI Base Class Array
713 // ?_R3 # RTTI Class Hierarchy Descriptor
714 // ?_R4 # RTTI Complete Object Locator
715 // ?_S # local vftable
716 // ?_T # local vftable constructor closure
717 // <operator-name> ::= ?_U # new[]
718 case OO_Array_New: Out << "?_U"; break;
719 // <operator-name> ::= ?_V # delete[]
720 case OO_Array_Delete: Out << "?_V"; break;
721
722 case OO_Conditional: {
723 DiagnosticsEngine &Diags = Context.getDiags();
724 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
725 "cannot mangle this conditional operator yet");
726 Diags.Report(Loc, DiagID);
727 break;
728 }
729
730 case OO_None:
731 case NUM_OVERLOADED_OPERATORS:
732 llvm_unreachable("Not an overloaded operator");
733 }
734}
735
736void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
737 // <source name> ::= <identifier> @
738 std::string key = II->getNameStart();
739 BackRefMap::iterator Found;
740 if (UseNameBackReferences)
741 Found = NameBackReferences.find(key);
742 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
743 Out << II->getName() << '@';
744 if (UseNameBackReferences && NameBackReferences.size() < 10) {
745 size_t Size = NameBackReferences.size();
746 NameBackReferences[key] = Size;
747 }
748 } else {
749 Out << Found->second;
750 }
751}
752
753void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
754 Context.mangleObjCMethodName(MD, Out);
755}
756
757// Find out how many function decls live above this one and return an integer
758// suitable for use as the number in a numbered anonymous scope.
759// TODO: Memoize.
760static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
761 const DeclContext *DC = FD->getParent();
762 int level = 1;
763
764 while (DC && !DC->isTranslationUnit()) {
765 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
766 DC = DC->getParent();
767 }
768
769 return 2*level;
770}
771
772void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
773 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
774 // <numbered-anonymous-scope> ::= ? <number>
775 // Even though the name is rendered in reverse order (e.g.
776 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
777 // innermost. So a method bar in class C local to function foo gets mangled
778 // as something like:
779 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
780 // This is more apparent when you have a type nested inside a method of a
781 // type nested inside a function. A method baz in class D local to method
782 // bar of class C local to function foo gets mangled as:
783 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
784 // This scheme is general enough to support GCC-style nested
785 // functions. You could have a method baz of class C inside a function bar
786 // inside a function foo, like so:
787 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
788 int NestLevel = getLocalNestingLevel(FD);
789 Out << '?';
790 mangleNumber(NestLevel);
791 Out << '?';
792 mangle(FD, "?");
793}
794
795void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
796 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000797 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000798 // <template-name> ::= <unscoped-template-name> <template-args>
799 // ::= <substitution>
800 // Always start with the unqualified name.
801
802 // Templates have their own context for back references.
803 ArgBackRefMap OuterArgsContext;
804 BackRefMap OuterTemplateContext;
805 NameBackReferences.swap(OuterTemplateContext);
806 TypeBackReferences.swap(OuterArgsContext);
807
808 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000809 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000810
811 // Restore the previous back reference contexts.
812 NameBackReferences.swap(OuterTemplateContext);
813 TypeBackReferences.swap(OuterArgsContext);
814}
815
816void
817MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
818 // <unscoped-template-name> ::= ?$ <unqualified-name>
819 Out << "?$";
820 mangleUnqualifiedName(TD);
821}
822
823void
824MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
825 bool IsBoolean) {
826 // <integer-literal> ::= $0 <number>
827 Out << "$0";
828 // Make sure booleans are encoded as 0/1.
829 if (IsBoolean && Value.getBoolValue())
830 mangleNumber(1);
831 else
832 mangleNumber(Value);
833}
834
835void
836MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
837 // See if this is a constant expression.
838 llvm::APSInt Value;
839 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
840 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
841 return;
842 }
843
844 // As bad as this diagnostic is, it's better than crashing.
845 DiagnosticsEngine &Diags = Context.getDiags();
846 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
847 "cannot yet mangle expression type %0");
848 Diags.Report(E->getExprLoc(), DiagID)
849 << E->getStmtClassName() << E->getSourceRange();
850}
851
852void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000853MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
854 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000855 // <template-args> ::= {<type> | <integer-literal>}+ @
856 unsigned NumTemplateArgs = TemplateArgs.size();
857 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000858 const TemplateArgument &TA = TemplateArgs[i];
Reid Kleckner5d90d182013-07-02 18:10:07 +0000859 mangleTemplateArg(TD, TA, i);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000860 }
861 Out << '@';
862}
863
Reid Kleckner5d90d182013-07-02 18:10:07 +0000864void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
865 const TemplateArgument &TA,
866 int ArgIndex) {
867 switch (TA.getKind()) {
868 case TemplateArgument::Null:
869 llvm_unreachable("Can't mangle null template arguments!");
870 case TemplateArgument::Type: {
871 QualType T = TA.getAsType();
872 mangleType(T, SourceRange(), QMM_Escape);
873 break;
874 }
875 case TemplateArgument::Declaration:
876 mangle(cast<NamedDecl>(TA.getAsDecl()), "$1?");
877 break;
878 case TemplateArgument::Integral:
879 mangleIntegerLiteral(TA.getAsIntegral(),
880 TA.getIntegralType()->isBooleanType());
881 break;
882 case TemplateArgument::Expression:
883 mangleExpression(TA.getAsExpr());
884 break;
885 case TemplateArgument::Pack:
886 // Unlike Itanium, there is no character code to indicate an argument pack.
887 // FIXME: ArgIndex will be off, but we only use if for diagnostics that
888 // should ultimately be removed.
889 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
890 I != E; ++I)
891 mangleTemplateArg(TD, *I, ArgIndex);
892 break;
893 case TemplateArgument::Template:
894 case TemplateArgument::TemplateExpansion:
895 case TemplateArgument::NullPtr: {
896 // Issue a diagnostic.
897 DiagnosticsEngine &Diags = Context.getDiags();
898 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
899 "cannot mangle template argument %0 of kind %select{ERROR|ERROR|"
900 "pointer/reference|nullptr|integral|template|template pack expansion|"
901 "ERROR|parameter pack}1 yet");
902 Diags.Report(TD->getLocation(), DiagID)
903 << ArgIndex + 1
904 << TA.getKind()
905 << TD->getSourceRange();
906 }
907 }
908}
909
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000910void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
911 bool IsMember) {
912 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
913 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
914 // 'I' means __restrict (32/64-bit).
915 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
916 // keyword!
917 // <base-cvr-qualifiers> ::= A # near
918 // ::= B # near const
919 // ::= C # near volatile
920 // ::= D # near const volatile
921 // ::= E # far (16-bit)
922 // ::= F # far const (16-bit)
923 // ::= G # far volatile (16-bit)
924 // ::= H # far const volatile (16-bit)
925 // ::= I # huge (16-bit)
926 // ::= J # huge const (16-bit)
927 // ::= K # huge volatile (16-bit)
928 // ::= L # huge const volatile (16-bit)
929 // ::= M <basis> # based
930 // ::= N <basis> # based const
931 // ::= O <basis> # based volatile
932 // ::= P <basis> # based const volatile
933 // ::= Q # near member
934 // ::= R # near const member
935 // ::= S # near volatile member
936 // ::= T # near const volatile member
937 // ::= U # far member (16-bit)
938 // ::= V # far const member (16-bit)
939 // ::= W # far volatile member (16-bit)
940 // ::= X # far const volatile member (16-bit)
941 // ::= Y # huge member (16-bit)
942 // ::= Z # huge const member (16-bit)
943 // ::= 0 # huge volatile member (16-bit)
944 // ::= 1 # huge const volatile member (16-bit)
945 // ::= 2 <basis> # based member
946 // ::= 3 <basis> # based const member
947 // ::= 4 <basis> # based volatile member
948 // ::= 5 <basis> # based const volatile member
949 // ::= 6 # near function (pointers only)
950 // ::= 7 # far function (pointers only)
951 // ::= 8 # near method (pointers only)
952 // ::= 9 # far method (pointers only)
953 // ::= _A <basis> # based function (pointers only)
954 // ::= _B <basis> # based function (far?) (pointers only)
955 // ::= _C <basis> # based method (pointers only)
956 // ::= _D <basis> # based method (far?) (pointers only)
957 // ::= _E # block (Clang)
958 // <basis> ::= 0 # __based(void)
959 // ::= 1 # __based(segment)?
960 // ::= 2 <name> # __based(name)
961 // ::= 3 # ?
962 // ::= 4 # ?
963 // ::= 5 # not really based
964 bool HasConst = Quals.hasConst(),
965 HasVolatile = Quals.hasVolatile();
966 if (!IsMember) {
967 if (HasConst && HasVolatile) {
968 Out << 'D';
969 } else if (HasVolatile) {
970 Out << 'C';
971 } else if (HasConst) {
972 Out << 'B';
973 } else {
974 Out << 'A';
975 }
976 } else {
977 if (HasConst && HasVolatile) {
978 Out << 'T';
979 } else if (HasVolatile) {
980 Out << 'S';
981 } else if (HasConst) {
982 Out << 'R';
983 } else {
984 Out << 'Q';
985 }
986 }
987
988 // FIXME: For now, just drop all extension qualifiers on the floor.
989}
990
991void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
992 // <pointer-cvr-qualifiers> ::= P # no qualifiers
993 // ::= Q # const
994 // ::= R # volatile
995 // ::= S # const volatile
996 bool HasConst = Quals.hasConst(),
997 HasVolatile = Quals.hasVolatile();
998 if (HasConst && HasVolatile) {
999 Out << 'S';
1000 } else if (HasVolatile) {
1001 Out << 'R';
1002 } else if (HasConst) {
1003 Out << 'Q';
1004 } else {
1005 Out << 'P';
1006 }
1007}
1008
1009void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1010 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001011 // MSVC will backreference two canonically equivalent types that have slightly
1012 // different manglings when mangled alone.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001013 void *TypePtr = getASTContext().getCanonicalType(T).getAsOpaquePtr();
1014 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1015
1016 if (Found == TypeBackReferences.end()) {
1017 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1018
Reid Klecknerf21818d2013-06-24 19:21:52 +00001019 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1020 QualType OT = DT->getOriginalType();
1021 if (const ArrayType *AT = getASTContext().getAsArrayType(OT)) {
1022 mangleDecayedArrayType(AT, false);
1023 } else if (const FunctionType *FT = OT->getAs<FunctionType>()) {
1024 Out << "P6";
1025 mangleFunctionType(FT, 0, false, false);
1026 } else {
1027 llvm_unreachable("unexpected decayed type");
1028 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001029 } else {
1030 mangleType(T, Range, QMM_Drop);
1031 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001032
1033 // See if it's worth creating a back reference.
1034 // Only types longer than 1 character are considered
1035 // and only 10 back references slots are available:
1036 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1037 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1038 size_t Size = TypeBackReferences.size();
1039 TypeBackReferences[TypePtr] = Size;
1040 }
1041 } else {
1042 Out << Found->second;
1043 }
1044}
1045
1046void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001047 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001048 // Don't use the canonical types. MSVC includes things like 'const' on
1049 // pointer arguments to function pointers that canonicalization strips away.
1050 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001051 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001052 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1053 // If there were any Quals, getAsArrayType() pushed them onto the array
1054 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001055 if (QMM == QMM_Mangle)
1056 Out << 'A';
1057 else if (QMM == QMM_Escape || QMM == QMM_Result)
1058 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001059 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001060 return;
1061 }
1062
1063 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1064 T->isBlockPointerType();
1065
1066 switch (QMM) {
1067 case QMM_Drop:
1068 break;
1069 case QMM_Mangle:
1070 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1071 Out << '6';
1072 mangleFunctionType(FT, 0, false, false);
1073 return;
1074 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001075 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001076 break;
1077 case QMM_Escape:
1078 if (!IsPointer && Quals) {
1079 Out << "$$C";
1080 mangleQualifiers(Quals, false);
1081 }
1082 break;
1083 case QMM_Result:
1084 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1085 Out << '?';
1086 mangleQualifiers(Quals, false);
1087 }
1088 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001089 }
1090
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001091 // We have to mangle these now, while we still have enough information.
1092 if (IsPointer)
1093 manglePointerQualifiers(Quals);
1094 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001095
1096 switch (ty->getTypeClass()) {
1097#define ABSTRACT_TYPE(CLASS, PARENT)
1098#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1099 case Type::CLASS: \
1100 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1101 return;
1102#define TYPE(CLASS, PARENT) \
1103 case Type::CLASS: \
1104 mangleType(cast<CLASS##Type>(ty), Range); \
1105 break;
1106#include "clang/AST/TypeNodes.def"
1107#undef ABSTRACT_TYPE
1108#undef NON_CANONICAL_TYPE
1109#undef TYPE
1110 }
1111}
1112
1113void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1114 SourceRange Range) {
1115 // <type> ::= <builtin-type>
1116 // <builtin-type> ::= X # void
1117 // ::= C # signed char
1118 // ::= D # char
1119 // ::= E # unsigned char
1120 // ::= F # short
1121 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1122 // ::= H # int
1123 // ::= I # unsigned int
1124 // ::= J # long
1125 // ::= K # unsigned long
1126 // L # <none>
1127 // ::= M # float
1128 // ::= N # double
1129 // ::= O # long double (__float80 is mangled differently)
1130 // ::= _J # long long, __int64
1131 // ::= _K # unsigned long long, __int64
1132 // ::= _L # __int128
1133 // ::= _M # unsigned __int128
1134 // ::= _N # bool
1135 // _O # <array in parameter>
1136 // ::= _T # __float80 (Intel)
1137 // ::= _W # wchar_t
1138 // ::= _Z # __float80 (Digital Mars)
1139 switch (T->getKind()) {
1140 case BuiltinType::Void: Out << 'X'; break;
1141 case BuiltinType::SChar: Out << 'C'; break;
1142 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1143 case BuiltinType::UChar: Out << 'E'; break;
1144 case BuiltinType::Short: Out << 'F'; break;
1145 case BuiltinType::UShort: Out << 'G'; break;
1146 case BuiltinType::Int: Out << 'H'; break;
1147 case BuiltinType::UInt: Out << 'I'; break;
1148 case BuiltinType::Long: Out << 'J'; break;
1149 case BuiltinType::ULong: Out << 'K'; break;
1150 case BuiltinType::Float: Out << 'M'; break;
1151 case BuiltinType::Double: Out << 'N'; break;
1152 // TODO: Determine size and mangle accordingly
1153 case BuiltinType::LongDouble: Out << 'O'; break;
1154 case BuiltinType::LongLong: Out << "_J"; break;
1155 case BuiltinType::ULongLong: Out << "_K"; break;
1156 case BuiltinType::Int128: Out << "_L"; break;
1157 case BuiltinType::UInt128: Out << "_M"; break;
1158 case BuiltinType::Bool: Out << "_N"; break;
1159 case BuiltinType::WChar_S:
1160 case BuiltinType::WChar_U: Out << "_W"; break;
1161
1162#define BUILTIN_TYPE(Id, SingletonId)
1163#define PLACEHOLDER_TYPE(Id, SingletonId) \
1164 case BuiltinType::Id:
1165#include "clang/AST/BuiltinTypes.def"
1166 case BuiltinType::Dependent:
1167 llvm_unreachable("placeholder types shouldn't get to name mangling");
1168
1169 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1170 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1171 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001172
1173 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1174 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1175 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1176 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1177 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1178 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001179 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001180 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001181
1182 case BuiltinType::NullPtr: Out << "$$T"; break;
1183
1184 case BuiltinType::Char16:
1185 case BuiltinType::Char32:
1186 case BuiltinType::Half: {
1187 DiagnosticsEngine &Diags = Context.getDiags();
1188 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1189 "cannot mangle this built-in %0 type yet");
1190 Diags.Report(Range.getBegin(), DiagID)
1191 << T->getName(Context.getASTContext().getPrintingPolicy())
1192 << Range;
1193 break;
1194 }
1195 }
1196}
1197
1198// <type> ::= <function-type>
1199void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1200 SourceRange) {
1201 // Structors only appear in decls, so at this point we know it's not a
1202 // structor type.
1203 // FIXME: This may not be lambda-friendly.
1204 Out << "$$A6";
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001205 mangleFunctionType(T, NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001206}
1207void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1208 SourceRange) {
1209 llvm_unreachable("Can't mangle K&R function prototypes");
1210}
1211
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001212void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1213 const FunctionDecl *D,
1214 bool IsStructor,
1215 bool IsInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001216 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1217 // <return-type> <argument-list> <throw-spec>
1218 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1219
Reid Klecknerf21818d2013-06-24 19:21:52 +00001220 SourceRange Range;
1221 if (D) Range = D->getSourceRange();
1222
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001223 // If this is a C++ instance method, mangle the CVR qualifiers for the
1224 // this pointer.
1225 if (IsInstMethod)
1226 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
1227
1228 mangleCallingConvention(T, IsInstMethod);
1229
1230 // <return-type> ::= <type>
1231 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001232 if (IsStructor) {
1233 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1234 StructorType == Dtor_Deleting) {
1235 // The scalar deleting destructor takes an extra int argument.
1236 // However, the FunctionType generated has 0 arguments.
1237 // FIXME: This is a temporary hack.
1238 // Maybe should fix the FunctionType creation instead?
1239 Out << "PAXI@Z";
1240 return;
1241 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001242 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001243 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001244 mangleType(Proto->getResultType(), Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001245 }
1246
1247 // <argument-list> ::= X # void
1248 // ::= <type>+ @
1249 // ::= <type>* Z # varargs
1250 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1251 Out << 'X';
1252 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001253 // Happens for function pointer type arguments for example.
1254 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1255 ArgEnd = Proto->arg_type_end();
1256 Arg != ArgEnd; ++Arg)
1257 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001258 // <builtin-type> ::= Z # ellipsis
1259 if (Proto->isVariadic())
1260 Out << 'Z';
1261 else
1262 Out << '@';
1263 }
1264
1265 mangleThrowSpecification(Proto);
1266}
1267
1268void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001269 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1270 // # pointer. in 64-bit mode *all*
1271 // # 'this' pointers are 64-bit.
1272 // ::= <global-function>
1273 // <member-function> ::= A # private: near
1274 // ::= B # private: far
1275 // ::= C # private: static near
1276 // ::= D # private: static far
1277 // ::= E # private: virtual near
1278 // ::= F # private: virtual far
1279 // ::= G # private: thunk near
1280 // ::= H # private: thunk far
1281 // ::= I # protected: near
1282 // ::= J # protected: far
1283 // ::= K # protected: static near
1284 // ::= L # protected: static far
1285 // ::= M # protected: virtual near
1286 // ::= N # protected: virtual far
1287 // ::= O # protected: thunk near
1288 // ::= P # protected: thunk far
1289 // ::= Q # public: near
1290 // ::= R # public: far
1291 // ::= S # public: static near
1292 // ::= T # public: static far
1293 // ::= U # public: virtual near
1294 // ::= V # public: virtual far
1295 // ::= W # public: thunk near
1296 // ::= X # public: thunk far
1297 // <global-function> ::= Y # global near
1298 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001299 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1300 switch (MD->getAccess()) {
1301 default:
1302 case AS_private:
1303 if (MD->isStatic())
1304 Out << 'C';
1305 else if (MD->isVirtual())
1306 Out << 'E';
1307 else
1308 Out << 'A';
1309 break;
1310 case AS_protected:
1311 if (MD->isStatic())
1312 Out << 'K';
1313 else if (MD->isVirtual())
1314 Out << 'M';
1315 else
1316 Out << 'I';
1317 break;
1318 case AS_public:
1319 if (MD->isStatic())
1320 Out << 'S';
1321 else if (MD->isVirtual())
1322 Out << 'U';
1323 else
1324 Out << 'Q';
1325 }
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001326 if (PointersAre64Bit && !MD->isStatic())
1327 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001328 } else
1329 Out << 'Y';
1330}
1331void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
1332 bool IsInstMethod) {
1333 // <calling-convention> ::= A # __cdecl
1334 // ::= B # __export __cdecl
1335 // ::= C # __pascal
1336 // ::= D # __export __pascal
1337 // ::= E # __thiscall
1338 // ::= F # __export __thiscall
1339 // ::= G # __stdcall
1340 // ::= H # __export __stdcall
1341 // ::= I # __fastcall
1342 // ::= J # __export __fastcall
1343 // The 'export' calling conventions are from a bygone era
1344 // (*cough*Win16*cough*) when functions were declared for export with
1345 // that keyword. (It didn't actually export them, it just made them so
1346 // that they could be in a DLL and somebody from another module could call
1347 // them.)
1348 CallingConv CC = T->getCallConv();
1349 if (CC == CC_Default) {
1350 if (IsInstMethod) {
1351 const FunctionProtoType *FPT =
1352 T->getCanonicalTypeUnqualified().castAs<FunctionProtoType>();
1353 bool isVariadic = FPT->isVariadic();
1354 CC = getASTContext().getDefaultCXXMethodCallConv(isVariadic);
1355 } else {
1356 CC = CC_C;
1357 }
1358 }
1359 switch (CC) {
1360 default:
1361 llvm_unreachable("Unsupported CC for mangling");
1362 case CC_Default:
1363 case CC_C: Out << 'A'; break;
1364 case CC_X86Pascal: Out << 'C'; break;
1365 case CC_X86ThisCall: Out << 'E'; break;
1366 case CC_X86StdCall: Out << 'G'; break;
1367 case CC_X86FastCall: Out << 'I'; break;
1368 }
1369}
1370void MicrosoftCXXNameMangler::mangleThrowSpecification(
1371 const FunctionProtoType *FT) {
1372 // <throw-spec> ::= Z # throw(...) (default)
1373 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1374 // ::= <type>+
1375 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1376 // all actually mangled as 'Z'. (They're ignored because their associated
1377 // functionality isn't implemented, and probably never will be.)
1378 Out << 'Z';
1379}
1380
1381void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1382 SourceRange Range) {
1383 // Probably should be mangled as a template instantiation; need to see what
1384 // VC does first.
1385 DiagnosticsEngine &Diags = Context.getDiags();
1386 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1387 "cannot mangle this unresolved dependent type yet");
1388 Diags.Report(Range.getBegin(), DiagID)
1389 << Range;
1390}
1391
1392// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1393// <union-type> ::= T <name>
1394// <struct-type> ::= U <name>
1395// <class-type> ::= V <name>
1396// <enum-type> ::= W <size> <name>
1397void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
1398 mangleType(cast<TagType>(T));
1399}
1400void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
1401 mangleType(cast<TagType>(T));
1402}
1403void MicrosoftCXXNameMangler::mangleType(const TagType *T) {
1404 switch (T->getDecl()->getTagKind()) {
1405 case TTK_Union:
1406 Out << 'T';
1407 break;
1408 case TTK_Struct:
1409 case TTK_Interface:
1410 Out << 'U';
1411 break;
1412 case TTK_Class:
1413 Out << 'V';
1414 break;
1415 case TTK_Enum:
1416 Out << 'W';
1417 Out << getASTContext().getTypeSizeInChars(
1418 cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity();
1419 break;
1420 }
1421 mangleName(T->getDecl());
1422}
1423
1424// <type> ::= <array-type>
1425// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1426// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001427// <element-type> # as global, E is never required
1428// ::= Q E? <cvr-qualifiers> [Y <dimension-count> <dimension>+]
1429// <element-type> # as param, E is required for 64-bit
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001430// It's supposed to be the other way around, but for some strange reason, it
1431// isn't. Today this behavior is retained for the sole purpose of backwards
1432// compatibility.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001433void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T,
1434 bool IsGlobal) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001435 // This isn't a recursive mangling, so now we have to do it all in this
1436 // one call.
1437 if (IsGlobal) {
1438 manglePointerQualifiers(T->getElementType().getQualifiers());
1439 } else {
1440 Out << 'Q';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001441 if (PointersAre64Bit)
1442 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001443 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001444 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001445}
1446void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1447 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001448 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001449}
1450void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1451 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001452 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001453}
1454void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1455 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001456 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001457}
1458void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1459 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001460 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001461}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001462void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001463 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001464 SmallVector<llvm::APInt, 3> Dimensions;
1465 for (;;) {
1466 if (const ConstantArrayType *CAT =
1467 getASTContext().getAsConstantArrayType(ElementTy)) {
1468 Dimensions.push_back(CAT->getSize());
1469 ElementTy = CAT->getElementType();
1470 } else if (ElementTy->isVariableArrayType()) {
1471 const VariableArrayType *VAT =
1472 getASTContext().getAsVariableArrayType(ElementTy);
1473 DiagnosticsEngine &Diags = Context.getDiags();
1474 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1475 "cannot mangle this variable-length array yet");
1476 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1477 << VAT->getBracketsRange();
1478 return;
1479 } else if (ElementTy->isDependentSizedArrayType()) {
1480 // The dependent expression has to be folded into a constant (TODO).
1481 const DependentSizedArrayType *DSAT =
1482 getASTContext().getAsDependentSizedArrayType(ElementTy);
1483 DiagnosticsEngine &Diags = Context.getDiags();
1484 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1485 "cannot mangle this dependent-length array yet");
1486 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1487 << DSAT->getBracketsRange();
1488 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001489 } else if (const IncompleteArrayType *IAT =
1490 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1491 Dimensions.push_back(llvm::APInt(32, 0));
1492 ElementTy = IAT->getElementType();
1493 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001494 else break;
1495 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001496 Out << 'Y';
1497 // <dimension-count> ::= <number> # number of extra dimensions
1498 mangleNumber(Dimensions.size());
1499 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1500 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001501 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001502}
1503
1504// <type> ::= <pointer-to-member-type>
1505// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1506// <class name> <type>
1507void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1508 SourceRange Range) {
1509 QualType PointeeType = T->getPointeeType();
1510 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1511 Out << '8';
1512 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001513 mangleFunctionType(FPT, NULL, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001514 } else {
1515 mangleQualifiers(PointeeType.getQualifiers(), true);
1516 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001517 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001518 }
1519}
1520
1521void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1522 SourceRange Range) {
1523 DiagnosticsEngine &Diags = Context.getDiags();
1524 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1525 "cannot mangle this template type parameter type yet");
1526 Diags.Report(Range.getBegin(), DiagID)
1527 << Range;
1528}
1529
1530void MicrosoftCXXNameMangler::mangleType(
1531 const SubstTemplateTypeParmPackType *T,
1532 SourceRange Range) {
1533 DiagnosticsEngine &Diags = Context.getDiags();
1534 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1535 "cannot mangle this substituted parameter pack yet");
1536 Diags.Report(Range.getBegin(), DiagID)
1537 << Range;
1538}
1539
1540// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001541// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1542// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001543void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1544 SourceRange Range) {
1545 QualType PointeeTy = T->getPointeeType();
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001546 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1547 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001548 mangleType(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001549}
1550void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1551 SourceRange Range) {
1552 // Object pointers never have qualifiers.
1553 Out << 'A';
1554 mangleType(T->getPointeeType(), Range);
1555}
1556
1557// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001558// <reference-type> ::= A E? <cvr-qualifiers> <type>
1559// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001560void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1561 SourceRange Range) {
1562 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001563 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1564 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001565 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001566}
1567
1568// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001569// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1570// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001571void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1572 SourceRange Range) {
1573 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001574 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1575 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001576 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001577}
1578
1579void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1580 SourceRange Range) {
1581 DiagnosticsEngine &Diags = Context.getDiags();
1582 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1583 "cannot mangle this complex number type yet");
1584 Diags.Report(Range.getBegin(), DiagID)
1585 << Range;
1586}
1587
1588void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1589 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001590 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1591 assert(ET && "vectors with non-builtin elements are unsupported");
1592 uint64_t Width = getASTContext().getTypeSize(T);
1593 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1594 // doesn't match the Intel types uses a custom mangling below.
1595 bool IntelVector = true;
1596 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1597 Out << "T__m64";
1598 } else if (Width == 128 || Width == 256) {
1599 if (ET->getKind() == BuiltinType::Float)
1600 Out << "T__m" << Width;
1601 else if (ET->getKind() == BuiltinType::LongLong)
1602 Out << "T__m" << Width << 'i';
1603 else if (ET->getKind() == BuiltinType::Double)
1604 Out << "U__m" << Width << 'd';
1605 else
1606 IntelVector = false;
1607 } else {
1608 IntelVector = false;
1609 }
1610
1611 if (!IntelVector) {
1612 // The MS ABI doesn't have a special mangling for vector types, so we define
1613 // our own mangling to handle uses of __vector_size__ on user-specified
1614 // types, and for extensions like __v4sf.
1615 Out << "T__clang_vec" << T->getNumElements() << '_';
1616 mangleType(ET, Range);
1617 }
1618
1619 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001620}
Reid Kleckner1232e272013-03-26 16:56:59 +00001621
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001622void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1623 SourceRange Range) {
1624 DiagnosticsEngine &Diags = Context.getDiags();
1625 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1626 "cannot mangle this extended vector type yet");
1627 Diags.Report(Range.getBegin(), DiagID)
1628 << Range;
1629}
1630void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1631 SourceRange Range) {
1632 DiagnosticsEngine &Diags = Context.getDiags();
1633 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1634 "cannot mangle this dependent-sized extended vector type yet");
1635 Diags.Report(Range.getBegin(), DiagID)
1636 << Range;
1637}
1638
1639void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1640 SourceRange) {
1641 // ObjC interfaces have structs underlying them.
1642 Out << 'U';
1643 mangleName(T->getDecl());
1644}
1645
1646void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1647 SourceRange Range) {
1648 // We don't allow overloading by different protocol qualification,
1649 // so mangling them isn't necessary.
1650 mangleType(T->getBaseType(), Range);
1651}
1652
1653void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1654 SourceRange Range) {
1655 Out << "_E";
1656
1657 QualType pointee = T->getPointeeType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001658 mangleFunctionType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001659}
1660
1661void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T,
1662 SourceRange Range) {
1663 DiagnosticsEngine &Diags = Context.getDiags();
1664 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1665 "cannot mangle this injected class name type yet");
1666 Diags.Report(Range.getBegin(), DiagID)
1667 << Range;
1668}
1669
1670void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1671 SourceRange Range) {
1672 DiagnosticsEngine &Diags = Context.getDiags();
1673 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1674 "cannot mangle this template specialization type yet");
1675 Diags.Report(Range.getBegin(), DiagID)
1676 << Range;
1677}
1678
1679void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1680 SourceRange Range) {
1681 DiagnosticsEngine &Diags = Context.getDiags();
1682 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1683 "cannot mangle this dependent name type yet");
1684 Diags.Report(Range.getBegin(), DiagID)
1685 << Range;
1686}
1687
1688void MicrosoftCXXNameMangler::mangleType(
1689 const DependentTemplateSpecializationType *T,
1690 SourceRange Range) {
1691 DiagnosticsEngine &Diags = Context.getDiags();
1692 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1693 "cannot mangle this dependent template specialization type yet");
1694 Diags.Report(Range.getBegin(), DiagID)
1695 << Range;
1696}
1697
1698void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1699 SourceRange Range) {
1700 DiagnosticsEngine &Diags = Context.getDiags();
1701 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1702 "cannot mangle this pack expansion yet");
1703 Diags.Report(Range.getBegin(), DiagID)
1704 << Range;
1705}
1706
1707void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1708 SourceRange Range) {
1709 DiagnosticsEngine &Diags = Context.getDiags();
1710 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1711 "cannot mangle this typeof(type) yet");
1712 Diags.Report(Range.getBegin(), DiagID)
1713 << Range;
1714}
1715
1716void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1717 SourceRange Range) {
1718 DiagnosticsEngine &Diags = Context.getDiags();
1719 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1720 "cannot mangle this typeof(expression) yet");
1721 Diags.Report(Range.getBegin(), DiagID)
1722 << Range;
1723}
1724
1725void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1726 SourceRange Range) {
1727 DiagnosticsEngine &Diags = Context.getDiags();
1728 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1729 "cannot mangle this decltype() yet");
1730 Diags.Report(Range.getBegin(), DiagID)
1731 << Range;
1732}
1733
1734void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1735 SourceRange Range) {
1736 DiagnosticsEngine &Diags = Context.getDiags();
1737 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1738 "cannot mangle this unary transform type yet");
1739 Diags.Report(Range.getBegin(), DiagID)
1740 << Range;
1741}
1742
1743void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1744 DiagnosticsEngine &Diags = Context.getDiags();
1745 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1746 "cannot mangle this 'auto' type yet");
1747 Diags.Report(Range.getBegin(), DiagID)
1748 << Range;
1749}
1750
1751void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1752 SourceRange Range) {
1753 DiagnosticsEngine &Diags = Context.getDiags();
1754 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1755 "cannot mangle this C11 atomic type yet");
1756 Diags.Report(Range.getBegin(), DiagID)
1757 << Range;
1758}
1759
1760void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1761 raw_ostream &Out) {
1762 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1763 "Invalid mangleName() call, argument is not a variable or function!");
1764 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1765 "Invalid mangleName() call on 'structor decl!");
1766
1767 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1768 getASTContext().getSourceManager(),
1769 "Mangling declaration");
1770
1771 MicrosoftCXXNameMangler Mangler(*this, Out);
1772 return Mangler.mangle(D);
1773}
1774void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1775 const ThunkInfo &Thunk,
1776 raw_ostream &) {
1777 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1778 "cannot mangle thunk for this method yet");
1779 getDiags().Report(MD->getLocation(), DiagID);
1780}
1781void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1782 CXXDtorType Type,
1783 const ThisAdjustment &,
1784 raw_ostream &) {
1785 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1786 "cannot mangle thunk for this destructor yet");
1787 getDiags().Report(DD->getLocation(), DiagID);
1788}
Reid Kleckner90633022013-06-19 15:20:38 +00001789
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001790void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1791 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001792 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1793 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001794 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001795 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001796 MicrosoftCXXNameMangler Mangler(*this, Out);
1797 Mangler.getStream() << "\01??_7";
1798 Mangler.mangleName(RD);
Reid Kleckner90633022013-06-19 15:20:38 +00001799 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001800 // TODO: If the class has more than one vtable, mangle in the class it came
1801 // from.
1802 Mangler.getStream() << '@';
1803}
Reid Kleckner90633022013-06-19 15:20:38 +00001804
1805void MicrosoftMangleContext::mangleCXXVBTable(
1806 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1807 raw_ostream &Out) {
1808 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1809 // <cvr-qualifiers> [<name>] @
1810 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1811 // is always '7' for vbtables.
1812 MicrosoftCXXNameMangler Mangler(*this, Out);
1813 Mangler.getStream() << "\01??_8";
1814 Mangler.mangleName(Derived);
1815 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1816 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1817 E = BasePath.end();
1818 I != E; ++I) {
1819 Mangler.mangleName(*I);
1820 }
1821 Mangler.getStream() << '@';
1822}
1823
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001824void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1825 raw_ostream &) {
1826 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1827}
1828void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1829 int64_t Offset,
1830 const CXXRecordDecl *Type,
1831 raw_ostream &) {
1832 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1833}
1834void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1835 raw_ostream &) {
1836 // FIXME: Give a location...
1837 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1838 "cannot mangle RTTI descriptors for type %0 yet");
1839 getDiags().Report(DiagID)
1840 << T.getBaseTypeIdentifier();
1841}
1842void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1843 raw_ostream &) {
1844 // FIXME: Give a location...
1845 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1846 "cannot mangle the name of type %0 into RTTI descriptors yet");
1847 getDiags().Report(DiagID)
1848 << T.getBaseTypeIdentifier();
1849}
1850void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1851 CXXCtorType Type,
1852 raw_ostream & Out) {
1853 MicrosoftCXXNameMangler mangler(*this, Out);
1854 mangler.mangle(D);
1855}
1856void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1857 CXXDtorType Type,
1858 raw_ostream & Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001859 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001860 mangler.mangle(D);
1861}
1862void MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *VD,
1863 raw_ostream &) {
1864 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1865 "cannot mangle this reference temporary yet");
1866 getDiags().Report(VD->getLocation(), DiagID);
1867}
1868
1869MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
1870 DiagnosticsEngine &Diags) {
1871 return new MicrosoftMangleContext(Context, Diags);
1872}