blob: 8c02b3310dbaa6dd2f0574fa5887572147d5d2af [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)) {
559 Context.mangleBlock(BD, Out);
560 Out << '@';
561 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000562 } else if (isa<CapturedDecl>(DC)) {
563 // Skip CapturedDecl context.
564 manglePostfix(DC->getParent(), NoFunction);
565 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000566 }
567
568 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
569 return;
570 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
571 mangleObjCMethodName(Method);
572 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
573 mangleLocalName(Func);
574 else {
575 mangleUnqualifiedName(cast<NamedDecl>(DC));
576 manglePostfix(DC->getParent(), NoFunction);
577 }
578}
579
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000580void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
581 switch (T) {
582 case Dtor_Deleting:
583 Out << "?_G";
584 return;
585 case Dtor_Base:
586 // FIXME: We should be asked to mangle base dtors.
587 // However, fixing this would require larger changes to the CodeGenModule.
588 // Please put llvm_unreachable here when CGM is changed.
589 // For now, just mangle a base dtor the same way as a complete dtor...
590 case Dtor_Complete:
591 Out << "?1";
592 return;
593 }
594 llvm_unreachable("Unsupported dtor type?");
595}
596
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000597void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
598 SourceLocation Loc) {
599 switch (OO) {
600 // ?0 # constructor
601 // ?1 # destructor
602 // <operator-name> ::= ?2 # new
603 case OO_New: Out << "?2"; break;
604 // <operator-name> ::= ?3 # delete
605 case OO_Delete: Out << "?3"; break;
606 // <operator-name> ::= ?4 # =
607 case OO_Equal: Out << "?4"; break;
608 // <operator-name> ::= ?5 # >>
609 case OO_GreaterGreater: Out << "?5"; break;
610 // <operator-name> ::= ?6 # <<
611 case OO_LessLess: Out << "?6"; break;
612 // <operator-name> ::= ?7 # !
613 case OO_Exclaim: Out << "?7"; break;
614 // <operator-name> ::= ?8 # ==
615 case OO_EqualEqual: Out << "?8"; break;
616 // <operator-name> ::= ?9 # !=
617 case OO_ExclaimEqual: Out << "?9"; break;
618 // <operator-name> ::= ?A # []
619 case OO_Subscript: Out << "?A"; break;
620 // ?B # conversion
621 // <operator-name> ::= ?C # ->
622 case OO_Arrow: Out << "?C"; break;
623 // <operator-name> ::= ?D # *
624 case OO_Star: Out << "?D"; break;
625 // <operator-name> ::= ?E # ++
626 case OO_PlusPlus: Out << "?E"; break;
627 // <operator-name> ::= ?F # --
628 case OO_MinusMinus: Out << "?F"; break;
629 // <operator-name> ::= ?G # -
630 case OO_Minus: Out << "?G"; break;
631 // <operator-name> ::= ?H # +
632 case OO_Plus: Out << "?H"; break;
633 // <operator-name> ::= ?I # &
634 case OO_Amp: Out << "?I"; break;
635 // <operator-name> ::= ?J # ->*
636 case OO_ArrowStar: Out << "?J"; break;
637 // <operator-name> ::= ?K # /
638 case OO_Slash: Out << "?K"; break;
639 // <operator-name> ::= ?L # %
640 case OO_Percent: Out << "?L"; break;
641 // <operator-name> ::= ?M # <
642 case OO_Less: Out << "?M"; break;
643 // <operator-name> ::= ?N # <=
644 case OO_LessEqual: Out << "?N"; break;
645 // <operator-name> ::= ?O # >
646 case OO_Greater: Out << "?O"; break;
647 // <operator-name> ::= ?P # >=
648 case OO_GreaterEqual: Out << "?P"; break;
649 // <operator-name> ::= ?Q # ,
650 case OO_Comma: Out << "?Q"; break;
651 // <operator-name> ::= ?R # ()
652 case OO_Call: Out << "?R"; break;
653 // <operator-name> ::= ?S # ~
654 case OO_Tilde: Out << "?S"; break;
655 // <operator-name> ::= ?T # ^
656 case OO_Caret: Out << "?T"; break;
657 // <operator-name> ::= ?U # |
658 case OO_Pipe: Out << "?U"; break;
659 // <operator-name> ::= ?V # &&
660 case OO_AmpAmp: Out << "?V"; break;
661 // <operator-name> ::= ?W # ||
662 case OO_PipePipe: Out << "?W"; break;
663 // <operator-name> ::= ?X # *=
664 case OO_StarEqual: Out << "?X"; break;
665 // <operator-name> ::= ?Y # +=
666 case OO_PlusEqual: Out << "?Y"; break;
667 // <operator-name> ::= ?Z # -=
668 case OO_MinusEqual: Out << "?Z"; break;
669 // <operator-name> ::= ?_0 # /=
670 case OO_SlashEqual: Out << "?_0"; break;
671 // <operator-name> ::= ?_1 # %=
672 case OO_PercentEqual: Out << "?_1"; break;
673 // <operator-name> ::= ?_2 # >>=
674 case OO_GreaterGreaterEqual: Out << "?_2"; break;
675 // <operator-name> ::= ?_3 # <<=
676 case OO_LessLessEqual: Out << "?_3"; break;
677 // <operator-name> ::= ?_4 # &=
678 case OO_AmpEqual: Out << "?_4"; break;
679 // <operator-name> ::= ?_5 # |=
680 case OO_PipeEqual: Out << "?_5"; break;
681 // <operator-name> ::= ?_6 # ^=
682 case OO_CaretEqual: Out << "?_6"; break;
683 // ?_7 # vftable
684 // ?_8 # vbtable
685 // ?_9 # vcall
686 // ?_A # typeof
687 // ?_B # local static guard
688 // ?_C # string
689 // ?_D # vbase destructor
690 // ?_E # vector deleting destructor
691 // ?_F # default constructor closure
692 // ?_G # scalar deleting destructor
693 // ?_H # vector constructor iterator
694 // ?_I # vector destructor iterator
695 // ?_J # vector vbase constructor iterator
696 // ?_K # virtual displacement map
697 // ?_L # eh vector constructor iterator
698 // ?_M # eh vector destructor iterator
699 // ?_N # eh vector vbase constructor iterator
700 // ?_O # copy constructor closure
701 // ?_P<name> # udt returning <name>
702 // ?_Q # <unknown>
703 // ?_R0 # RTTI Type Descriptor
704 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
705 // ?_R2 # RTTI Base Class Array
706 // ?_R3 # RTTI Class Hierarchy Descriptor
707 // ?_R4 # RTTI Complete Object Locator
708 // ?_S # local vftable
709 // ?_T # local vftable constructor closure
710 // <operator-name> ::= ?_U # new[]
711 case OO_Array_New: Out << "?_U"; break;
712 // <operator-name> ::= ?_V # delete[]
713 case OO_Array_Delete: Out << "?_V"; break;
714
715 case OO_Conditional: {
716 DiagnosticsEngine &Diags = Context.getDiags();
717 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
718 "cannot mangle this conditional operator yet");
719 Diags.Report(Loc, DiagID);
720 break;
721 }
722
723 case OO_None:
724 case NUM_OVERLOADED_OPERATORS:
725 llvm_unreachable("Not an overloaded operator");
726 }
727}
728
729void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
730 // <source name> ::= <identifier> @
731 std::string key = II->getNameStart();
732 BackRefMap::iterator Found;
733 if (UseNameBackReferences)
734 Found = NameBackReferences.find(key);
735 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
736 Out << II->getName() << '@';
737 if (UseNameBackReferences && NameBackReferences.size() < 10) {
738 size_t Size = NameBackReferences.size();
739 NameBackReferences[key] = Size;
740 }
741 } else {
742 Out << Found->second;
743 }
744}
745
746void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
747 Context.mangleObjCMethodName(MD, Out);
748}
749
750// Find out how many function decls live above this one and return an integer
751// suitable for use as the number in a numbered anonymous scope.
752// TODO: Memoize.
753static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
754 const DeclContext *DC = FD->getParent();
755 int level = 1;
756
757 while (DC && !DC->isTranslationUnit()) {
758 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
759 DC = DC->getParent();
760 }
761
762 return 2*level;
763}
764
765void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
766 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
767 // <numbered-anonymous-scope> ::= ? <number>
768 // Even though the name is rendered in reverse order (e.g.
769 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
770 // innermost. So a method bar in class C local to function foo gets mangled
771 // as something like:
772 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
773 // This is more apparent when you have a type nested inside a method of a
774 // type nested inside a function. A method baz in class D local to method
775 // bar of class C local to function foo gets mangled as:
776 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
777 // This scheme is general enough to support GCC-style nested
778 // functions. You could have a method baz of class C inside a function bar
779 // inside a function foo, like so:
780 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
781 int NestLevel = getLocalNestingLevel(FD);
782 Out << '?';
783 mangleNumber(NestLevel);
784 Out << '?';
785 mangle(FD, "?");
786}
787
788void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
789 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000790 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000791 // <template-name> ::= <unscoped-template-name> <template-args>
792 // ::= <substitution>
793 // Always start with the unqualified name.
794
795 // Templates have their own context for back references.
796 ArgBackRefMap OuterArgsContext;
797 BackRefMap OuterTemplateContext;
798 NameBackReferences.swap(OuterTemplateContext);
799 TypeBackReferences.swap(OuterArgsContext);
800
801 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000802 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000803
804 // Restore the previous back reference contexts.
805 NameBackReferences.swap(OuterTemplateContext);
806 TypeBackReferences.swap(OuterArgsContext);
807}
808
809void
810MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
811 // <unscoped-template-name> ::= ?$ <unqualified-name>
812 Out << "?$";
813 mangleUnqualifiedName(TD);
814}
815
816void
817MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
818 bool IsBoolean) {
819 // <integer-literal> ::= $0 <number>
820 Out << "$0";
821 // Make sure booleans are encoded as 0/1.
822 if (IsBoolean && Value.getBoolValue())
823 mangleNumber(1);
824 else
825 mangleNumber(Value);
826}
827
828void
829MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
830 // See if this is a constant expression.
831 llvm::APSInt Value;
832 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
833 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
834 return;
835 }
836
837 // As bad as this diagnostic is, it's better than crashing.
838 DiagnosticsEngine &Diags = Context.getDiags();
839 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
840 "cannot yet mangle expression type %0");
841 Diags.Report(E->getExprLoc(), DiagID)
842 << E->getStmtClassName() << E->getSourceRange();
843}
844
845void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000846MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
847 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000848 // <template-args> ::= {<type> | <integer-literal>}+ @
849 unsigned NumTemplateArgs = TemplateArgs.size();
850 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000851 const TemplateArgument &TA = TemplateArgs[i];
Reid Kleckner5d90d182013-07-02 18:10:07 +0000852 mangleTemplateArg(TD, TA, i);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000853 }
854 Out << '@';
855}
856
Reid Kleckner5d90d182013-07-02 18:10:07 +0000857void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
858 const TemplateArgument &TA,
859 int ArgIndex) {
860 switch (TA.getKind()) {
861 case TemplateArgument::Null:
862 llvm_unreachable("Can't mangle null template arguments!");
863 case TemplateArgument::Type: {
864 QualType T = TA.getAsType();
865 mangleType(T, SourceRange(), QMM_Escape);
866 break;
867 }
868 case TemplateArgument::Declaration:
869 mangle(cast<NamedDecl>(TA.getAsDecl()), "$1?");
870 break;
871 case TemplateArgument::Integral:
872 mangleIntegerLiteral(TA.getAsIntegral(),
873 TA.getIntegralType()->isBooleanType());
874 break;
875 case TemplateArgument::Expression:
876 mangleExpression(TA.getAsExpr());
877 break;
878 case TemplateArgument::Pack:
879 // Unlike Itanium, there is no character code to indicate an argument pack.
880 // FIXME: ArgIndex will be off, but we only use if for diagnostics that
881 // should ultimately be removed.
882 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
883 I != E; ++I)
884 mangleTemplateArg(TD, *I, ArgIndex);
885 break;
886 case TemplateArgument::Template:
887 case TemplateArgument::TemplateExpansion:
888 case TemplateArgument::NullPtr: {
889 // Issue a diagnostic.
890 DiagnosticsEngine &Diags = Context.getDiags();
891 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
892 "cannot mangle template argument %0 of kind %select{ERROR|ERROR|"
893 "pointer/reference|nullptr|integral|template|template pack expansion|"
894 "ERROR|parameter pack}1 yet");
895 Diags.Report(TD->getLocation(), DiagID)
896 << ArgIndex + 1
897 << TA.getKind()
898 << TD->getSourceRange();
899 }
900 }
901}
902
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000903void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
904 bool IsMember) {
905 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
906 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
907 // 'I' means __restrict (32/64-bit).
908 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
909 // keyword!
910 // <base-cvr-qualifiers> ::= A # near
911 // ::= B # near const
912 // ::= C # near volatile
913 // ::= D # near const volatile
914 // ::= E # far (16-bit)
915 // ::= F # far const (16-bit)
916 // ::= G # far volatile (16-bit)
917 // ::= H # far const volatile (16-bit)
918 // ::= I # huge (16-bit)
919 // ::= J # huge const (16-bit)
920 // ::= K # huge volatile (16-bit)
921 // ::= L # huge const volatile (16-bit)
922 // ::= M <basis> # based
923 // ::= N <basis> # based const
924 // ::= O <basis> # based volatile
925 // ::= P <basis> # based const volatile
926 // ::= Q # near member
927 // ::= R # near const member
928 // ::= S # near volatile member
929 // ::= T # near const volatile member
930 // ::= U # far member (16-bit)
931 // ::= V # far const member (16-bit)
932 // ::= W # far volatile member (16-bit)
933 // ::= X # far const volatile member (16-bit)
934 // ::= Y # huge member (16-bit)
935 // ::= Z # huge const member (16-bit)
936 // ::= 0 # huge volatile member (16-bit)
937 // ::= 1 # huge const volatile member (16-bit)
938 // ::= 2 <basis> # based member
939 // ::= 3 <basis> # based const member
940 // ::= 4 <basis> # based volatile member
941 // ::= 5 <basis> # based const volatile member
942 // ::= 6 # near function (pointers only)
943 // ::= 7 # far function (pointers only)
944 // ::= 8 # near method (pointers only)
945 // ::= 9 # far method (pointers only)
946 // ::= _A <basis> # based function (pointers only)
947 // ::= _B <basis> # based function (far?) (pointers only)
948 // ::= _C <basis> # based method (pointers only)
949 // ::= _D <basis> # based method (far?) (pointers only)
950 // ::= _E # block (Clang)
951 // <basis> ::= 0 # __based(void)
952 // ::= 1 # __based(segment)?
953 // ::= 2 <name> # __based(name)
954 // ::= 3 # ?
955 // ::= 4 # ?
956 // ::= 5 # not really based
957 bool HasConst = Quals.hasConst(),
958 HasVolatile = Quals.hasVolatile();
959 if (!IsMember) {
960 if (HasConst && HasVolatile) {
961 Out << 'D';
962 } else if (HasVolatile) {
963 Out << 'C';
964 } else if (HasConst) {
965 Out << 'B';
966 } else {
967 Out << 'A';
968 }
969 } else {
970 if (HasConst && HasVolatile) {
971 Out << 'T';
972 } else if (HasVolatile) {
973 Out << 'S';
974 } else if (HasConst) {
975 Out << 'R';
976 } else {
977 Out << 'Q';
978 }
979 }
980
981 // FIXME: For now, just drop all extension qualifiers on the floor.
982}
983
984void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
985 // <pointer-cvr-qualifiers> ::= P # no qualifiers
986 // ::= Q # const
987 // ::= R # volatile
988 // ::= S # const volatile
989 bool HasConst = Quals.hasConst(),
990 HasVolatile = Quals.hasVolatile();
991 if (HasConst && HasVolatile) {
992 Out << 'S';
993 } else if (HasVolatile) {
994 Out << 'R';
995 } else if (HasConst) {
996 Out << 'Q';
997 } else {
998 Out << 'P';
999 }
1000}
1001
1002void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1003 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001004 // MSVC will backreference two canonically equivalent types that have slightly
1005 // different manglings when mangled alone.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001006 void *TypePtr = getASTContext().getCanonicalType(T).getAsOpaquePtr();
1007 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1008
1009 if (Found == TypeBackReferences.end()) {
1010 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1011
Reid Klecknerf21818d2013-06-24 19:21:52 +00001012 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1013 QualType OT = DT->getOriginalType();
1014 if (const ArrayType *AT = getASTContext().getAsArrayType(OT)) {
1015 mangleDecayedArrayType(AT, false);
1016 } else if (const FunctionType *FT = OT->getAs<FunctionType>()) {
1017 Out << "P6";
1018 mangleFunctionType(FT, 0, false, false);
1019 } else {
1020 llvm_unreachable("unexpected decayed type");
1021 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001022 } else {
1023 mangleType(T, Range, QMM_Drop);
1024 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001025
1026 // See if it's worth creating a back reference.
1027 // Only types longer than 1 character are considered
1028 // and only 10 back references slots are available:
1029 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1030 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1031 size_t Size = TypeBackReferences.size();
1032 TypeBackReferences[TypePtr] = Size;
1033 }
1034 } else {
1035 Out << Found->second;
1036 }
1037}
1038
1039void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001040 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001041 // Don't use the canonical types. MSVC includes things like 'const' on
1042 // pointer arguments to function pointers that canonicalization strips away.
1043 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001044 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001045 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1046 // If there were any Quals, getAsArrayType() pushed them onto the array
1047 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001048 if (QMM == QMM_Mangle)
1049 Out << 'A';
1050 else if (QMM == QMM_Escape || QMM == QMM_Result)
1051 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001052 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001053 return;
1054 }
1055
1056 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1057 T->isBlockPointerType();
1058
1059 switch (QMM) {
1060 case QMM_Drop:
1061 break;
1062 case QMM_Mangle:
1063 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1064 Out << '6';
1065 mangleFunctionType(FT, 0, false, false);
1066 return;
1067 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001068 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001069 break;
1070 case QMM_Escape:
1071 if (!IsPointer && Quals) {
1072 Out << "$$C";
1073 mangleQualifiers(Quals, false);
1074 }
1075 break;
1076 case QMM_Result:
1077 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1078 Out << '?';
1079 mangleQualifiers(Quals, false);
1080 }
1081 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001082 }
1083
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001084 // We have to mangle these now, while we still have enough information.
1085 if (IsPointer)
1086 manglePointerQualifiers(Quals);
1087 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001088
1089 switch (ty->getTypeClass()) {
1090#define ABSTRACT_TYPE(CLASS, PARENT)
1091#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1092 case Type::CLASS: \
1093 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1094 return;
1095#define TYPE(CLASS, PARENT) \
1096 case Type::CLASS: \
1097 mangleType(cast<CLASS##Type>(ty), Range); \
1098 break;
1099#include "clang/AST/TypeNodes.def"
1100#undef ABSTRACT_TYPE
1101#undef NON_CANONICAL_TYPE
1102#undef TYPE
1103 }
1104}
1105
1106void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1107 SourceRange Range) {
1108 // <type> ::= <builtin-type>
1109 // <builtin-type> ::= X # void
1110 // ::= C # signed char
1111 // ::= D # char
1112 // ::= E # unsigned char
1113 // ::= F # short
1114 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1115 // ::= H # int
1116 // ::= I # unsigned int
1117 // ::= J # long
1118 // ::= K # unsigned long
1119 // L # <none>
1120 // ::= M # float
1121 // ::= N # double
1122 // ::= O # long double (__float80 is mangled differently)
1123 // ::= _J # long long, __int64
1124 // ::= _K # unsigned long long, __int64
1125 // ::= _L # __int128
1126 // ::= _M # unsigned __int128
1127 // ::= _N # bool
1128 // _O # <array in parameter>
1129 // ::= _T # __float80 (Intel)
1130 // ::= _W # wchar_t
1131 // ::= _Z # __float80 (Digital Mars)
1132 switch (T->getKind()) {
1133 case BuiltinType::Void: Out << 'X'; break;
1134 case BuiltinType::SChar: Out << 'C'; break;
1135 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1136 case BuiltinType::UChar: Out << 'E'; break;
1137 case BuiltinType::Short: Out << 'F'; break;
1138 case BuiltinType::UShort: Out << 'G'; break;
1139 case BuiltinType::Int: Out << 'H'; break;
1140 case BuiltinType::UInt: Out << 'I'; break;
1141 case BuiltinType::Long: Out << 'J'; break;
1142 case BuiltinType::ULong: Out << 'K'; break;
1143 case BuiltinType::Float: Out << 'M'; break;
1144 case BuiltinType::Double: Out << 'N'; break;
1145 // TODO: Determine size and mangle accordingly
1146 case BuiltinType::LongDouble: Out << 'O'; break;
1147 case BuiltinType::LongLong: Out << "_J"; break;
1148 case BuiltinType::ULongLong: Out << "_K"; break;
1149 case BuiltinType::Int128: Out << "_L"; break;
1150 case BuiltinType::UInt128: Out << "_M"; break;
1151 case BuiltinType::Bool: Out << "_N"; break;
1152 case BuiltinType::WChar_S:
1153 case BuiltinType::WChar_U: Out << "_W"; break;
1154
1155#define BUILTIN_TYPE(Id, SingletonId)
1156#define PLACEHOLDER_TYPE(Id, SingletonId) \
1157 case BuiltinType::Id:
1158#include "clang/AST/BuiltinTypes.def"
1159 case BuiltinType::Dependent:
1160 llvm_unreachable("placeholder types shouldn't get to name mangling");
1161
1162 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1163 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1164 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001165
1166 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1167 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1168 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1169 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1170 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1171 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001172 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001173 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001174
1175 case BuiltinType::NullPtr: Out << "$$T"; break;
1176
1177 case BuiltinType::Char16:
1178 case BuiltinType::Char32:
1179 case BuiltinType::Half: {
1180 DiagnosticsEngine &Diags = Context.getDiags();
1181 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1182 "cannot mangle this built-in %0 type yet");
1183 Diags.Report(Range.getBegin(), DiagID)
1184 << T->getName(Context.getASTContext().getPrintingPolicy())
1185 << Range;
1186 break;
1187 }
1188 }
1189}
1190
1191// <type> ::= <function-type>
1192void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1193 SourceRange) {
1194 // Structors only appear in decls, so at this point we know it's not a
1195 // structor type.
1196 // FIXME: This may not be lambda-friendly.
1197 Out << "$$A6";
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001198 mangleFunctionType(T, NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001199}
1200void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1201 SourceRange) {
1202 llvm_unreachable("Can't mangle K&R function prototypes");
1203}
1204
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001205void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1206 const FunctionDecl *D,
1207 bool IsStructor,
1208 bool IsInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001209 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1210 // <return-type> <argument-list> <throw-spec>
1211 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1212
Reid Klecknerf21818d2013-06-24 19:21:52 +00001213 SourceRange Range;
1214 if (D) Range = D->getSourceRange();
1215
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001216 // If this is a C++ instance method, mangle the CVR qualifiers for the
1217 // this pointer.
1218 if (IsInstMethod)
1219 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
1220
1221 mangleCallingConvention(T, IsInstMethod);
1222
1223 // <return-type> ::= <type>
1224 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001225 if (IsStructor) {
1226 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1227 StructorType == Dtor_Deleting) {
1228 // The scalar deleting destructor takes an extra int argument.
1229 // However, the FunctionType generated has 0 arguments.
1230 // FIXME: This is a temporary hack.
1231 // Maybe should fix the FunctionType creation instead?
1232 Out << "PAXI@Z";
1233 return;
1234 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001235 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001236 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001237 mangleType(Proto->getResultType(), Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001238 }
1239
1240 // <argument-list> ::= X # void
1241 // ::= <type>+ @
1242 // ::= <type>* Z # varargs
1243 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1244 Out << 'X';
1245 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001246 // Happens for function pointer type arguments for example.
1247 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1248 ArgEnd = Proto->arg_type_end();
1249 Arg != ArgEnd; ++Arg)
1250 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001251 // <builtin-type> ::= Z # ellipsis
1252 if (Proto->isVariadic())
1253 Out << 'Z';
1254 else
1255 Out << '@';
1256 }
1257
1258 mangleThrowSpecification(Proto);
1259}
1260
1261void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001262 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1263 // # pointer. in 64-bit mode *all*
1264 // # 'this' pointers are 64-bit.
1265 // ::= <global-function>
1266 // <member-function> ::= A # private: near
1267 // ::= B # private: far
1268 // ::= C # private: static near
1269 // ::= D # private: static far
1270 // ::= E # private: virtual near
1271 // ::= F # private: virtual far
1272 // ::= G # private: thunk near
1273 // ::= H # private: thunk far
1274 // ::= I # protected: near
1275 // ::= J # protected: far
1276 // ::= K # protected: static near
1277 // ::= L # protected: static far
1278 // ::= M # protected: virtual near
1279 // ::= N # protected: virtual far
1280 // ::= O # protected: thunk near
1281 // ::= P # protected: thunk far
1282 // ::= Q # public: near
1283 // ::= R # public: far
1284 // ::= S # public: static near
1285 // ::= T # public: static far
1286 // ::= U # public: virtual near
1287 // ::= V # public: virtual far
1288 // ::= W # public: thunk near
1289 // ::= X # public: thunk far
1290 // <global-function> ::= Y # global near
1291 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001292 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1293 switch (MD->getAccess()) {
1294 default:
1295 case AS_private:
1296 if (MD->isStatic())
1297 Out << 'C';
1298 else if (MD->isVirtual())
1299 Out << 'E';
1300 else
1301 Out << 'A';
1302 break;
1303 case AS_protected:
1304 if (MD->isStatic())
1305 Out << 'K';
1306 else if (MD->isVirtual())
1307 Out << 'M';
1308 else
1309 Out << 'I';
1310 break;
1311 case AS_public:
1312 if (MD->isStatic())
1313 Out << 'S';
1314 else if (MD->isVirtual())
1315 Out << 'U';
1316 else
1317 Out << 'Q';
1318 }
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001319 if (PointersAre64Bit && !MD->isStatic())
1320 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001321 } else
1322 Out << 'Y';
1323}
1324void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
1325 bool IsInstMethod) {
1326 // <calling-convention> ::= A # __cdecl
1327 // ::= B # __export __cdecl
1328 // ::= C # __pascal
1329 // ::= D # __export __pascal
1330 // ::= E # __thiscall
1331 // ::= F # __export __thiscall
1332 // ::= G # __stdcall
1333 // ::= H # __export __stdcall
1334 // ::= I # __fastcall
1335 // ::= J # __export __fastcall
1336 // The 'export' calling conventions are from a bygone era
1337 // (*cough*Win16*cough*) when functions were declared for export with
1338 // that keyword. (It didn't actually export them, it just made them so
1339 // that they could be in a DLL and somebody from another module could call
1340 // them.)
1341 CallingConv CC = T->getCallConv();
1342 if (CC == CC_Default) {
1343 if (IsInstMethod) {
1344 const FunctionProtoType *FPT =
1345 T->getCanonicalTypeUnqualified().castAs<FunctionProtoType>();
1346 bool isVariadic = FPT->isVariadic();
1347 CC = getASTContext().getDefaultCXXMethodCallConv(isVariadic);
1348 } else {
1349 CC = CC_C;
1350 }
1351 }
1352 switch (CC) {
1353 default:
1354 llvm_unreachable("Unsupported CC for mangling");
1355 case CC_Default:
1356 case CC_C: Out << 'A'; break;
1357 case CC_X86Pascal: Out << 'C'; break;
1358 case CC_X86ThisCall: Out << 'E'; break;
1359 case CC_X86StdCall: Out << 'G'; break;
1360 case CC_X86FastCall: Out << 'I'; break;
1361 }
1362}
1363void MicrosoftCXXNameMangler::mangleThrowSpecification(
1364 const FunctionProtoType *FT) {
1365 // <throw-spec> ::= Z # throw(...) (default)
1366 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1367 // ::= <type>+
1368 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1369 // all actually mangled as 'Z'. (They're ignored because their associated
1370 // functionality isn't implemented, and probably never will be.)
1371 Out << 'Z';
1372}
1373
1374void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1375 SourceRange Range) {
1376 // Probably should be mangled as a template instantiation; need to see what
1377 // VC does first.
1378 DiagnosticsEngine &Diags = Context.getDiags();
1379 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1380 "cannot mangle this unresolved dependent type yet");
1381 Diags.Report(Range.getBegin(), DiagID)
1382 << Range;
1383}
1384
1385// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1386// <union-type> ::= T <name>
1387// <struct-type> ::= U <name>
1388// <class-type> ::= V <name>
1389// <enum-type> ::= W <size> <name>
1390void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
1391 mangleType(cast<TagType>(T));
1392}
1393void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
1394 mangleType(cast<TagType>(T));
1395}
1396void MicrosoftCXXNameMangler::mangleType(const TagType *T) {
1397 switch (T->getDecl()->getTagKind()) {
1398 case TTK_Union:
1399 Out << 'T';
1400 break;
1401 case TTK_Struct:
1402 case TTK_Interface:
1403 Out << 'U';
1404 break;
1405 case TTK_Class:
1406 Out << 'V';
1407 break;
1408 case TTK_Enum:
1409 Out << 'W';
1410 Out << getASTContext().getTypeSizeInChars(
1411 cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity();
1412 break;
1413 }
1414 mangleName(T->getDecl());
1415}
1416
1417// <type> ::= <array-type>
1418// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1419// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001420// <element-type> # as global, E is never required
1421// ::= Q E? <cvr-qualifiers> [Y <dimension-count> <dimension>+]
1422// <element-type> # as param, E is required for 64-bit
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001423// It's supposed to be the other way around, but for some strange reason, it
1424// isn't. Today this behavior is retained for the sole purpose of backwards
1425// compatibility.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001426void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T,
1427 bool IsGlobal) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001428 // This isn't a recursive mangling, so now we have to do it all in this
1429 // one call.
1430 if (IsGlobal) {
1431 manglePointerQualifiers(T->getElementType().getQualifiers());
1432 } else {
1433 Out << 'Q';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001434 if (PointersAre64Bit)
1435 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001436 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001437 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001438}
1439void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1440 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001441 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001442}
1443void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1444 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001445 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001446}
1447void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1448 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001449 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001450}
1451void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1452 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001453 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001454}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001455void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001456 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001457 SmallVector<llvm::APInt, 3> Dimensions;
1458 for (;;) {
1459 if (const ConstantArrayType *CAT =
1460 getASTContext().getAsConstantArrayType(ElementTy)) {
1461 Dimensions.push_back(CAT->getSize());
1462 ElementTy = CAT->getElementType();
1463 } else if (ElementTy->isVariableArrayType()) {
1464 const VariableArrayType *VAT =
1465 getASTContext().getAsVariableArrayType(ElementTy);
1466 DiagnosticsEngine &Diags = Context.getDiags();
1467 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1468 "cannot mangle this variable-length array yet");
1469 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1470 << VAT->getBracketsRange();
1471 return;
1472 } else if (ElementTy->isDependentSizedArrayType()) {
1473 // The dependent expression has to be folded into a constant (TODO).
1474 const DependentSizedArrayType *DSAT =
1475 getASTContext().getAsDependentSizedArrayType(ElementTy);
1476 DiagnosticsEngine &Diags = Context.getDiags();
1477 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1478 "cannot mangle this dependent-length array yet");
1479 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1480 << DSAT->getBracketsRange();
1481 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001482 } else if (const IncompleteArrayType *IAT =
1483 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1484 Dimensions.push_back(llvm::APInt(32, 0));
1485 ElementTy = IAT->getElementType();
1486 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001487 else break;
1488 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001489 Out << 'Y';
1490 // <dimension-count> ::= <number> # number of extra dimensions
1491 mangleNumber(Dimensions.size());
1492 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1493 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001494 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001495}
1496
1497// <type> ::= <pointer-to-member-type>
1498// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1499// <class name> <type>
1500void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1501 SourceRange Range) {
1502 QualType PointeeType = T->getPointeeType();
1503 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1504 Out << '8';
1505 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001506 mangleFunctionType(FPT, NULL, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001507 } else {
1508 mangleQualifiers(PointeeType.getQualifiers(), true);
1509 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001510 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001511 }
1512}
1513
1514void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1515 SourceRange Range) {
1516 DiagnosticsEngine &Diags = Context.getDiags();
1517 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1518 "cannot mangle this template type parameter type yet");
1519 Diags.Report(Range.getBegin(), DiagID)
1520 << Range;
1521}
1522
1523void MicrosoftCXXNameMangler::mangleType(
1524 const SubstTemplateTypeParmPackType *T,
1525 SourceRange Range) {
1526 DiagnosticsEngine &Diags = Context.getDiags();
1527 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1528 "cannot mangle this substituted parameter pack yet");
1529 Diags.Report(Range.getBegin(), DiagID)
1530 << Range;
1531}
1532
1533// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001534// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1535// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001536void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1537 SourceRange Range) {
1538 QualType PointeeTy = T->getPointeeType();
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001539 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1540 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001541 mangleType(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001542}
1543void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1544 SourceRange Range) {
1545 // Object pointers never have qualifiers.
1546 Out << 'A';
1547 mangleType(T->getPointeeType(), Range);
1548}
1549
1550// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001551// <reference-type> ::= A E? <cvr-qualifiers> <type>
1552// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001553void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1554 SourceRange Range) {
1555 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001556 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1557 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001558 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001559}
1560
1561// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001562// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1563// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001564void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1565 SourceRange Range) {
1566 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001567 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1568 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001569 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001570}
1571
1572void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1573 SourceRange Range) {
1574 DiagnosticsEngine &Diags = Context.getDiags();
1575 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1576 "cannot mangle this complex number type yet");
1577 Diags.Report(Range.getBegin(), DiagID)
1578 << Range;
1579}
1580
1581void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1582 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001583 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1584 assert(ET && "vectors with non-builtin elements are unsupported");
1585 uint64_t Width = getASTContext().getTypeSize(T);
1586 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1587 // doesn't match the Intel types uses a custom mangling below.
1588 bool IntelVector = true;
1589 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1590 Out << "T__m64";
1591 } else if (Width == 128 || Width == 256) {
1592 if (ET->getKind() == BuiltinType::Float)
1593 Out << "T__m" << Width;
1594 else if (ET->getKind() == BuiltinType::LongLong)
1595 Out << "T__m" << Width << 'i';
1596 else if (ET->getKind() == BuiltinType::Double)
1597 Out << "U__m" << Width << 'd';
1598 else
1599 IntelVector = false;
1600 } else {
1601 IntelVector = false;
1602 }
1603
1604 if (!IntelVector) {
1605 // The MS ABI doesn't have a special mangling for vector types, so we define
1606 // our own mangling to handle uses of __vector_size__ on user-specified
1607 // types, and for extensions like __v4sf.
1608 Out << "T__clang_vec" << T->getNumElements() << '_';
1609 mangleType(ET, Range);
1610 }
1611
1612 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001613}
Reid Kleckner1232e272013-03-26 16:56:59 +00001614
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001615void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1616 SourceRange Range) {
1617 DiagnosticsEngine &Diags = Context.getDiags();
1618 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1619 "cannot mangle this extended vector type yet");
1620 Diags.Report(Range.getBegin(), DiagID)
1621 << Range;
1622}
1623void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1624 SourceRange Range) {
1625 DiagnosticsEngine &Diags = Context.getDiags();
1626 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1627 "cannot mangle this dependent-sized extended vector type yet");
1628 Diags.Report(Range.getBegin(), DiagID)
1629 << Range;
1630}
1631
1632void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1633 SourceRange) {
1634 // ObjC interfaces have structs underlying them.
1635 Out << 'U';
1636 mangleName(T->getDecl());
1637}
1638
1639void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1640 SourceRange Range) {
1641 // We don't allow overloading by different protocol qualification,
1642 // so mangling them isn't necessary.
1643 mangleType(T->getBaseType(), Range);
1644}
1645
1646void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1647 SourceRange Range) {
1648 Out << "_E";
1649
1650 QualType pointee = T->getPointeeType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001651 mangleFunctionType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001652}
1653
1654void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T,
1655 SourceRange Range) {
1656 DiagnosticsEngine &Diags = Context.getDiags();
1657 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1658 "cannot mangle this injected class name type yet");
1659 Diags.Report(Range.getBegin(), DiagID)
1660 << Range;
1661}
1662
1663void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1664 SourceRange Range) {
1665 DiagnosticsEngine &Diags = Context.getDiags();
1666 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1667 "cannot mangle this template specialization type yet");
1668 Diags.Report(Range.getBegin(), DiagID)
1669 << Range;
1670}
1671
1672void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1673 SourceRange Range) {
1674 DiagnosticsEngine &Diags = Context.getDiags();
1675 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1676 "cannot mangle this dependent name type yet");
1677 Diags.Report(Range.getBegin(), DiagID)
1678 << Range;
1679}
1680
1681void MicrosoftCXXNameMangler::mangleType(
1682 const DependentTemplateSpecializationType *T,
1683 SourceRange Range) {
1684 DiagnosticsEngine &Diags = Context.getDiags();
1685 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1686 "cannot mangle this dependent template specialization type yet");
1687 Diags.Report(Range.getBegin(), DiagID)
1688 << Range;
1689}
1690
1691void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1692 SourceRange Range) {
1693 DiagnosticsEngine &Diags = Context.getDiags();
1694 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1695 "cannot mangle this pack expansion yet");
1696 Diags.Report(Range.getBegin(), DiagID)
1697 << Range;
1698}
1699
1700void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1701 SourceRange Range) {
1702 DiagnosticsEngine &Diags = Context.getDiags();
1703 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1704 "cannot mangle this typeof(type) yet");
1705 Diags.Report(Range.getBegin(), DiagID)
1706 << Range;
1707}
1708
1709void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1710 SourceRange Range) {
1711 DiagnosticsEngine &Diags = Context.getDiags();
1712 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1713 "cannot mangle this typeof(expression) yet");
1714 Diags.Report(Range.getBegin(), DiagID)
1715 << Range;
1716}
1717
1718void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1719 SourceRange Range) {
1720 DiagnosticsEngine &Diags = Context.getDiags();
1721 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1722 "cannot mangle this decltype() yet");
1723 Diags.Report(Range.getBegin(), DiagID)
1724 << Range;
1725}
1726
1727void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1728 SourceRange Range) {
1729 DiagnosticsEngine &Diags = Context.getDiags();
1730 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1731 "cannot mangle this unary transform type yet");
1732 Diags.Report(Range.getBegin(), DiagID)
1733 << Range;
1734}
1735
1736void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1737 DiagnosticsEngine &Diags = Context.getDiags();
1738 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1739 "cannot mangle this 'auto' type yet");
1740 Diags.Report(Range.getBegin(), DiagID)
1741 << Range;
1742}
1743
1744void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1745 SourceRange Range) {
1746 DiagnosticsEngine &Diags = Context.getDiags();
1747 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1748 "cannot mangle this C11 atomic type yet");
1749 Diags.Report(Range.getBegin(), DiagID)
1750 << Range;
1751}
1752
1753void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1754 raw_ostream &Out) {
1755 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1756 "Invalid mangleName() call, argument is not a variable or function!");
1757 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1758 "Invalid mangleName() call on 'structor decl!");
1759
1760 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1761 getASTContext().getSourceManager(),
1762 "Mangling declaration");
1763
1764 MicrosoftCXXNameMangler Mangler(*this, Out);
1765 return Mangler.mangle(D);
1766}
1767void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1768 const ThunkInfo &Thunk,
1769 raw_ostream &) {
1770 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1771 "cannot mangle thunk for this method yet");
1772 getDiags().Report(MD->getLocation(), DiagID);
1773}
1774void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1775 CXXDtorType Type,
1776 const ThisAdjustment &,
1777 raw_ostream &) {
1778 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1779 "cannot mangle thunk for this destructor yet");
1780 getDiags().Report(DD->getLocation(), DiagID);
1781}
Reid Kleckner90633022013-06-19 15:20:38 +00001782
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001783void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1784 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001785 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1786 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001787 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001788 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001789 MicrosoftCXXNameMangler Mangler(*this, Out);
1790 Mangler.getStream() << "\01??_7";
1791 Mangler.mangleName(RD);
Reid Kleckner90633022013-06-19 15:20:38 +00001792 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001793 // TODO: If the class has more than one vtable, mangle in the class it came
1794 // from.
1795 Mangler.getStream() << '@';
1796}
Reid Kleckner90633022013-06-19 15:20:38 +00001797
1798void MicrosoftMangleContext::mangleCXXVBTable(
1799 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1800 raw_ostream &Out) {
1801 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1802 // <cvr-qualifiers> [<name>] @
1803 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1804 // is always '7' for vbtables.
1805 MicrosoftCXXNameMangler Mangler(*this, Out);
1806 Mangler.getStream() << "\01??_8";
1807 Mangler.mangleName(Derived);
1808 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1809 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1810 E = BasePath.end();
1811 I != E; ++I) {
1812 Mangler.mangleName(*I);
1813 }
1814 Mangler.getStream() << '@';
1815}
1816
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001817void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1818 raw_ostream &) {
1819 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1820}
1821void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1822 int64_t Offset,
1823 const CXXRecordDecl *Type,
1824 raw_ostream &) {
1825 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1826}
1827void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1828 raw_ostream &) {
1829 // FIXME: Give a location...
1830 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1831 "cannot mangle RTTI descriptors for type %0 yet");
1832 getDiags().Report(DiagID)
1833 << T.getBaseTypeIdentifier();
1834}
1835void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1836 raw_ostream &) {
1837 // FIXME: Give a location...
1838 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1839 "cannot mangle the name of type %0 into RTTI descriptors yet");
1840 getDiags().Report(DiagID)
1841 << T.getBaseTypeIdentifier();
1842}
1843void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1844 CXXCtorType Type,
1845 raw_ostream & Out) {
1846 MicrosoftCXXNameMangler mangler(*this, Out);
1847 mangler.mangle(D);
1848}
1849void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1850 CXXDtorType Type,
1851 raw_ostream & Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001852 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001853 mangler.mangle(D);
1854}
1855void MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *VD,
1856 raw_ostream &) {
1857 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1858 "cannot mangle this reference temporary yet");
1859 getDiags().Report(VD->getLocation(), DiagID);
1860}
1861
1862MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
1863 DiagnosticsEngine &Diags) {
1864 return new MicrosoftMangleContext(Context, Diags);
1865}