blob: 68f57a9212903e7cb134cb28c47b6387e2c2488a [file] [log] [blame]
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001//===--- MicrosoftMangle.cpp - Microsoft Visual C++ Name Mangling ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides C++ name mangling targeting the Microsoft Visual C++ ABI.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Mangle.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/CharUnits.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclCXX.h"
20#include "clang/AST/DeclObjC.h"
21#include "clang/AST/DeclTemplate.h"
22#include "clang/AST/ExprCXX.h"
23#include "clang/Basic/ABI.h"
24#include "clang/Basic/DiagnosticOptions.h"
Reid Klecknerd6a08d12013-05-14 20:30:42 +000025#include "clang/Basic/TargetInfo.h"
Reid Klecknerf0219cd2013-05-22 17:16:39 +000026#include "llvm/ADT/StringMap.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000027
28using namespace clang;
29
30namespace {
31
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000032static const FunctionDecl *getStructor(const FunctionDecl *fn) {
33 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
34 return ftd->getTemplatedDecl();
35
36 return fn;
37}
38
Guy Benyei7f92f2d2012-12-18 14:30:41 +000039/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
40/// Microsoft Visual C++ ABI.
41class MicrosoftCXXNameMangler {
42 MangleContext &Context;
43 raw_ostream &Out;
44
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000045 /// The "structor" is the top-level declaration being mangled, if
46 /// that's not a template specialization; otherwise it's the pattern
47 /// for that specialization.
48 const NamedDecl *Structor;
49 unsigned StructorType;
50
Reid Klecknerf0219cd2013-05-22 17:16:39 +000051 typedef llvm::StringMap<unsigned> BackRefMap;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000052 BackRefMap NameBackReferences;
53 bool UseNameBackReferences;
54
55 typedef llvm::DenseMap<void*, unsigned> ArgBackRefMap;
56 ArgBackRefMap TypeBackReferences;
57
58 ASTContext &getASTContext() const { return Context.getASTContext(); }
59
Reid Klecknerd6a08d12013-05-14 20:30:42 +000060 // FIXME: If we add support for __ptr32/64 qualifiers, then we should push
61 // this check into mangleQualifiers().
62 const bool PointersAre64Bit;
63
Guy Benyei7f92f2d2012-12-18 14:30:41 +000064public:
Peter Collingbourneb70d1c32013-04-25 04:25:40 +000065 enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result };
66
Guy Benyei7f92f2d2012-12-18 14:30:41 +000067 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000068 : Context(C), Out(Out_),
69 Structor(0), StructorType(-1),
David Blaikiee7e94c92013-05-14 21:31:46 +000070 UseNameBackReferences(true),
Reid Klecknerd6a08d12013-05-14 20:30:42 +000071 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikiee7e94c92013-05-14 21:31:46 +000072 64) { }
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000073
74 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_,
75 const CXXDestructorDecl *D, CXXDtorType Type)
76 : Context(C), Out(Out_),
77 Structor(getStructor(D)), StructorType(Type),
David Blaikiee7e94c92013-05-14 21:31:46 +000078 UseNameBackReferences(true),
Reid Klecknerd6a08d12013-05-14 20:30:42 +000079 PointersAre64Bit(C.getASTContext().getTargetInfo().getPointerWidth(0) ==
David Blaikiee7e94c92013-05-14 21:31:46 +000080 64) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +000081
82 raw_ostream &getStream() const { return Out; }
83
84 void mangle(const NamedDecl *D, StringRef Prefix = "\01?");
85 void mangleName(const NamedDecl *ND);
86 void mangleFunctionEncoding(const FunctionDecl *FD);
87 void mangleVariableEncoding(const VarDecl *VD);
88 void mangleNumber(int64_t Number);
89 void mangleNumber(const llvm::APSInt &Value);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +000090 void mangleType(QualType T, SourceRange Range,
91 QualifierMangleMode QMM = QMM_Mangle);
Timur Iskhodzhanov635de282013-07-30 09:46:19 +000092 void mangleFunctionType(const FunctionType *T, const FunctionDecl *D,
93 bool IsStructor, bool IsInstMethod);
Guy Benyei7f92f2d2012-12-18 14:30:41 +000094
95private:
96 void disableBackReferences() { UseNameBackReferences = false; }
97 void mangleUnqualifiedName(const NamedDecl *ND) {
98 mangleUnqualifiedName(ND, ND->getDeclName());
99 }
100 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
101 void mangleSourceName(const IdentifierInfo *II);
102 void manglePostfix(const DeclContext *DC, bool NoFunction=false);
103 void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc);
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000104 void mangleCXXDtorType(CXXDtorType T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000105 void mangleQualifiers(Qualifiers Quals, bool IsMember);
106 void manglePointerQualifiers(Qualifiers Quals);
107
108 void mangleUnscopedTemplateName(const TemplateDecl *ND);
109 void mangleTemplateInstantiationName(const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000110 const TemplateArgumentList &TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000111 void mangleObjCMethodName(const ObjCMethodDecl *MD);
112 void mangleLocalName(const FunctionDecl *FD);
113
114 void mangleArgumentType(QualType T, SourceRange Range);
115
116 // Declare manglers for every type class.
117#define ABSTRACT_TYPE(CLASS, PARENT)
118#define NON_CANONICAL_TYPE(CLASS, PARENT)
119#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \
120 SourceRange Range);
121#include "clang/AST/TypeNodes.def"
122#undef ABSTRACT_TYPE
123#undef NON_CANONICAL_TYPE
124#undef TYPE
125
126 void mangleType(const TagType*);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000127 void mangleDecayedArrayType(const ArrayType *T, bool IsGlobal);
Reid Klecknerf21818d2013-06-24 19:21:52 +0000128 void mangleArrayType(const ArrayType *T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000129 void mangleFunctionClass(const FunctionDecl *FD);
130 void mangleCallingConvention(const FunctionType *T, bool IsInstMethod = false);
131 void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean);
132 void mangleExpression(const Expr *E);
133 void mangleThrowSpecification(const FunctionProtoType *T);
134
Reid Klecknerf16216c2013-03-20 01:40:23 +0000135 void mangleTemplateArgs(const TemplateDecl *TD,
136 const TemplateArgumentList &TemplateArgs);
Reid Kleckner5d90d182013-07-02 18:10:07 +0000137 void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA,
138 int ArgIndex);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000139};
140
141/// MicrosoftMangleContext - Overrides the default MangleContext for the
142/// Microsoft Visual C++ ABI.
143class MicrosoftMangleContext : public MangleContext {
144public:
145 MicrosoftMangleContext(ASTContext &Context,
146 DiagnosticsEngine &Diags) : MangleContext(Context, Diags) { }
147 virtual bool shouldMangleDeclName(const NamedDecl *D);
148 virtual void mangleName(const NamedDecl *D, raw_ostream &Out);
149 virtual void mangleThunk(const CXXMethodDecl *MD,
150 const ThunkInfo &Thunk,
151 raw_ostream &);
152 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
153 const ThisAdjustment &ThisAdjustment,
154 raw_ostream &);
155 virtual void mangleCXXVTable(const CXXRecordDecl *RD,
156 raw_ostream &);
157 virtual void mangleCXXVTT(const CXXRecordDecl *RD,
158 raw_ostream &);
Reid Kleckner90633022013-06-19 15:20:38 +0000159 virtual void mangleCXXVBTable(const CXXRecordDecl *Derived,
160 ArrayRef<const CXXRecordDecl *> BasePath,
161 raw_ostream &Out);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000162 virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
163 const CXXRecordDecl *Type,
164 raw_ostream &);
165 virtual void mangleCXXRTTI(QualType T, raw_ostream &);
166 virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
167 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
168 raw_ostream &);
169 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
170 raw_ostream &);
171 virtual void mangleReferenceTemporary(const clang::VarDecl *,
172 raw_ostream &);
173};
174
175}
176
177static bool isInCLinkageSpecification(const Decl *D) {
178 D = D->getCanonicalDecl();
179 for (const DeclContext *DC = D->getDeclContext();
180 !DC->isTranslationUnit(); DC = DC->getParent()) {
181 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
182 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
183 }
184
185 return false;
186}
187
188bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
189 // In C, functions with no attributes never need to be mangled. Fastpath them.
190 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
191 return false;
192
193 // Any decl can be declared with __asm("foo") on it, and this takes precedence
194 // over all other naming in the .o file.
195 if (D->hasAttr<AsmLabelAttr>())
196 return true;
197
198 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
199 // (always) as does passing a C++ member function and a function
200 // whose name is not a simple identifier.
201 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
202 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
203 !FD->getDeclName().isIdentifier()))
204 return true;
205
206 // Otherwise, no mangling is done outside C++ mode.
207 if (!getASTContext().getLangOpts().CPlusPlus)
208 return false;
209
210 // Variables at global scope with internal linkage are not mangled.
211 if (!FD) {
212 const DeclContext *DC = D->getDeclContext();
Rafael Espindola181e3ec2013-05-13 00:12:11 +0000213 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000214 return false;
215 }
216
217 // C functions and "main" are not mangled.
218 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
219 return false;
220
221 return true;
222}
223
224void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
225 StringRef Prefix) {
226 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
227 // Therefore it's really important that we don't decorate the
228 // name with leading underscores or leading/trailing at signs. So, by
229 // default, we emit an asm marker at the start so we get the name right.
230 // Callers can override this with a custom prefix.
231
232 // Any decl can be declared with __asm("foo") on it, and this takes precedence
233 // over all other naming in the .o file.
234 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
235 // If we have an asm name, then we use it as the mangling.
236 Out << '\01' << ALA->getLabel();
237 return;
238 }
239
240 // <mangled-name> ::= ? <name> <type-encoding>
241 Out << Prefix;
242 mangleName(D);
243 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
244 mangleFunctionEncoding(FD);
245 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
246 mangleVariableEncoding(VD);
247 else {
248 // TODO: Fields? Can MSVC even mangle them?
249 // Issue a diagnostic for now.
250 DiagnosticsEngine &Diags = Context.getDiags();
251 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
252 "cannot mangle this declaration yet");
253 Diags.Report(D->getLocation(), DiagID)
254 << D->getSourceRange();
255 }
256}
257
258void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
259 // <type-encoding> ::= <function-class> <function-type>
260
Reid Klecknerf21818d2013-06-24 19:21:52 +0000261 // Since MSVC operates on the type as written and not the canonical type, it
262 // actually matters which decl we have here. MSVC appears to choose the
263 // first, since it is most likely to be the declaration in a header file.
264 FD = FD->getFirstDeclaration();
265
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000266 // Don't mangle in the type if this isn't a decl we should typically mangle.
267 if (!Context.shouldMangleDeclName(FD))
268 return;
269
270 // We should never ever see a FunctionNoProtoType at this point.
271 // We don't even know how to mangle their types anyway :).
Reid Klecknerf21818d2013-06-24 19:21:52 +0000272 TypeSourceInfo *TSI = FD->getTypeSourceInfo();
273 QualType T = TSI ? TSI->getType() : FD->getType();
274 const FunctionProtoType *FT = T->castAs<FunctionProtoType>();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000275
276 bool InStructor = false, InInstMethod = false;
277 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
278 if (MD) {
279 if (MD->isInstance())
280 InInstMethod = true;
281 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
282 InStructor = true;
283 }
284
285 // First, the function class.
286 mangleFunctionClass(FD);
287
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000288 mangleFunctionType(FT, FD, InStructor, InInstMethod);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000289}
290
291void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
292 // <type-encoding> ::= <storage-class> <variable-type>
293 // <storage-class> ::= 0 # private static member
294 // ::= 1 # protected static member
295 // ::= 2 # public static member
296 // ::= 3 # global
297 // ::= 4 # static local
298
299 // The first character in the encoding (after the name) is the storage class.
300 if (VD->isStaticDataMember()) {
301 // If it's a static member, it also encodes the access level.
302 switch (VD->getAccess()) {
303 default:
304 case AS_private: Out << '0'; break;
305 case AS_protected: Out << '1'; break;
306 case AS_public: Out << '2'; break;
307 }
308 }
309 else if (!VD->isStaticLocal())
310 Out << '3';
311 else
312 Out << '4';
313 // Now mangle the type.
314 // <variable-type> ::= <type> <cvr-qualifiers>
315 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
316 // Pointers and references are odd. The type of 'int * const foo;' gets
317 // mangled as 'QAHA' instead of 'PAHB', for example.
318 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
319 QualType Ty = TL.getType();
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)) {
Reid Kleckner3be37d12013-07-13 00:43:39 +0000424 // Function templates aren't considered for name back referencing. This
425 // makes sense since function templates aren't likely to occur multiple
426 // times in a symbol.
427 // FIXME: Test alias template mangling with MSVC 2013.
428 if (!isa<ClassTemplateDecl>(TD)) {
429 mangleTemplateInstantiationName(TD, *TemplateArgs);
430 return;
431 }
432
433 // We have a class template.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000434 // Here comes the tricky thing: if we need to mangle something like
435 // void foo(A::X<Y>, B::X<Y>),
436 // the X<Y> part is aliased. However, if you need to mangle
437 // void foo(A::X<A::Y>, A::X<B::Y>),
438 // the A::X<> part is not aliased.
439 // That said, from the mangler's perspective we have a structure like this:
440 // namespace[s] -> type[ -> template-parameters]
441 // but from the Clang perspective we have
442 // type [ -> template-parameters]
443 // \-> namespace[s]
444 // What we do is we create a new mangler, mangle the same type (without
445 // a namespace suffix) using the extra mangler with back references
446 // disabled (to avoid infinite recursion) and then use the mangled type
447 // name as a key to check the mangling of different types for aliasing.
448
449 std::string BackReferenceKey;
450 BackRefMap::iterator Found;
451 if (UseNameBackReferences) {
452 llvm::raw_string_ostream Stream(BackReferenceKey);
453 MicrosoftCXXNameMangler Extra(Context, Stream);
454 Extra.disableBackReferences();
455 Extra.mangleUnqualifiedName(ND, Name);
456 Stream.flush();
457
458 Found = NameBackReferences.find(BackReferenceKey);
459 }
460 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000461 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000462 if (UseNameBackReferences && NameBackReferences.size() < 10) {
463 size_t Size = NameBackReferences.size();
464 NameBackReferences[BackReferenceKey] = Size;
465 }
466 } else {
467 Out << Found->second;
468 }
469 return;
470 }
471
472 switch (Name.getNameKind()) {
473 case DeclarationName::Identifier: {
474 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
475 mangleSourceName(II);
476 break;
477 }
478
479 // Otherwise, an anonymous entity. We must have a declaration.
480 assert(ND && "mangling empty name without declaration");
481
482 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
483 if (NS->isAnonymousNamespace()) {
484 Out << "?A@";
485 break;
486 }
487 }
488
489 // We must have an anonymous struct.
490 const TagDecl *TD = cast<TagDecl>(ND);
491 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
492 assert(TD->getDeclContext() == D->getDeclContext() &&
493 "Typedef should not be in another decl context!");
494 assert(D->getDeclName().getAsIdentifierInfo() &&
495 "Typedef was not named!");
496 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
497 break;
498 }
499
500 // When VC encounters an anonymous type with no tag and no typedef,
501 // it literally emits '<unnamed-tag>'.
502 Out << "<unnamed-tag>";
503 break;
504 }
505
506 case DeclarationName::ObjCZeroArgSelector:
507 case DeclarationName::ObjCOneArgSelector:
508 case DeclarationName::ObjCMultiArgSelector:
509 llvm_unreachable("Can't mangle Objective-C selector names here!");
510
511 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000512 if (ND == Structor) {
513 assert(StructorType == Ctor_Complete &&
514 "Should never be asked to mangle a ctor other than complete");
515 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000516 Out << "?0";
517 break;
518
519 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000520 if (ND == Structor)
521 // If the named decl is the C++ destructor we're mangling,
522 // use the type we were given.
523 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
524 else
Reid Klecknera4130ba2013-07-22 13:51:44 +0000525 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000526 // class with a destructor is declared within a destructor.
Reid Klecknera4130ba2013-07-22 13:51:44 +0000527 mangleCXXDtorType(Dtor_Base);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000528 break;
529
530 case DeclarationName::CXXConversionFunctionName:
531 // <operator-name> ::= ?B # (cast)
532 // The target type is encoded as the return type.
533 Out << "?B";
534 break;
535
536 case DeclarationName::CXXOperatorName:
537 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
538 break;
539
540 case DeclarationName::CXXLiteralOperatorName: {
541 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
542 DiagnosticsEngine Diags = Context.getDiags();
543 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
544 "cannot mangle this literal operator yet");
545 Diags.Report(ND->getLocation(), DiagID);
546 break;
547 }
548
549 case DeclarationName::CXXUsingDirective:
550 llvm_unreachable("Can't mangle a using directive name!");
551 }
552}
553
554void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
555 bool NoFunction) {
556 // <postfix> ::= <unqualified-name> [<postfix>]
557 // ::= <substitution> [<postfix>]
558
559 if (!DC) return;
560
561 while (isa<LinkageSpecDecl>(DC))
562 DC = DC->getParent();
563
564 if (DC->isTranslationUnit())
565 return;
566
567 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedmane5798892013-07-10 01:13:27 +0000568 DiagnosticsEngine Diags = Context.getDiags();
569 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
570 "cannot mangle a local inside this block yet");
571 Diags.Report(BD->getLocation(), DiagID);
572
573 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
574 // for how this should be done.
575 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000576 Out << '@';
577 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000578 } else if (isa<CapturedDecl>(DC)) {
579 // Skip CapturedDecl context.
580 manglePostfix(DC->getParent(), NoFunction);
581 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000582 }
583
584 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
585 return;
586 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
587 mangleObjCMethodName(Method);
588 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
589 mangleLocalName(Func);
590 else {
591 mangleUnqualifiedName(cast<NamedDecl>(DC));
592 manglePostfix(DC->getParent(), NoFunction);
593 }
594}
595
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000596void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000597 // Microsoft uses the names on the case labels for these dtor variants. Clang
598 // uses the Itanium terminology internally. Everything in this ABI delegates
599 // towards the base dtor.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000600 switch (T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000601 // <operator-name> ::= ?1 # destructor
602 case Dtor_Base: Out << "?1"; return;
603 // <operator-name> ::= ?_D # vbase destructor
604 case Dtor_Complete: Out << "?_D"; return;
605 // <operator-name> ::= ?_G # scalar deleting destructor
606 case Dtor_Deleting: Out << "?_G"; return;
607 // <operator-name> ::= ?_E # vector deleting destructor
608 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
609 // it.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000610 }
611 llvm_unreachable("Unsupported dtor type?");
612}
613
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000614void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
615 SourceLocation Loc) {
616 switch (OO) {
617 // ?0 # constructor
618 // ?1 # destructor
619 // <operator-name> ::= ?2 # new
620 case OO_New: Out << "?2"; break;
621 // <operator-name> ::= ?3 # delete
622 case OO_Delete: Out << "?3"; break;
623 // <operator-name> ::= ?4 # =
624 case OO_Equal: Out << "?4"; break;
625 // <operator-name> ::= ?5 # >>
626 case OO_GreaterGreater: Out << "?5"; break;
627 // <operator-name> ::= ?6 # <<
628 case OO_LessLess: Out << "?6"; break;
629 // <operator-name> ::= ?7 # !
630 case OO_Exclaim: Out << "?7"; break;
631 // <operator-name> ::= ?8 # ==
632 case OO_EqualEqual: Out << "?8"; break;
633 // <operator-name> ::= ?9 # !=
634 case OO_ExclaimEqual: Out << "?9"; break;
635 // <operator-name> ::= ?A # []
636 case OO_Subscript: Out << "?A"; break;
637 // ?B # conversion
638 // <operator-name> ::= ?C # ->
639 case OO_Arrow: Out << "?C"; break;
640 // <operator-name> ::= ?D # *
641 case OO_Star: Out << "?D"; break;
642 // <operator-name> ::= ?E # ++
643 case OO_PlusPlus: Out << "?E"; break;
644 // <operator-name> ::= ?F # --
645 case OO_MinusMinus: Out << "?F"; break;
646 // <operator-name> ::= ?G # -
647 case OO_Minus: Out << "?G"; break;
648 // <operator-name> ::= ?H # +
649 case OO_Plus: Out << "?H"; break;
650 // <operator-name> ::= ?I # &
651 case OO_Amp: Out << "?I"; break;
652 // <operator-name> ::= ?J # ->*
653 case OO_ArrowStar: Out << "?J"; break;
654 // <operator-name> ::= ?K # /
655 case OO_Slash: Out << "?K"; break;
656 // <operator-name> ::= ?L # %
657 case OO_Percent: Out << "?L"; break;
658 // <operator-name> ::= ?M # <
659 case OO_Less: Out << "?M"; break;
660 // <operator-name> ::= ?N # <=
661 case OO_LessEqual: Out << "?N"; break;
662 // <operator-name> ::= ?O # >
663 case OO_Greater: Out << "?O"; break;
664 // <operator-name> ::= ?P # >=
665 case OO_GreaterEqual: Out << "?P"; break;
666 // <operator-name> ::= ?Q # ,
667 case OO_Comma: Out << "?Q"; break;
668 // <operator-name> ::= ?R # ()
669 case OO_Call: Out << "?R"; break;
670 // <operator-name> ::= ?S # ~
671 case OO_Tilde: Out << "?S"; break;
672 // <operator-name> ::= ?T # ^
673 case OO_Caret: Out << "?T"; break;
674 // <operator-name> ::= ?U # |
675 case OO_Pipe: Out << "?U"; break;
676 // <operator-name> ::= ?V # &&
677 case OO_AmpAmp: Out << "?V"; break;
678 // <operator-name> ::= ?W # ||
679 case OO_PipePipe: Out << "?W"; break;
680 // <operator-name> ::= ?X # *=
681 case OO_StarEqual: Out << "?X"; break;
682 // <operator-name> ::= ?Y # +=
683 case OO_PlusEqual: Out << "?Y"; break;
684 // <operator-name> ::= ?Z # -=
685 case OO_MinusEqual: Out << "?Z"; break;
686 // <operator-name> ::= ?_0 # /=
687 case OO_SlashEqual: Out << "?_0"; break;
688 // <operator-name> ::= ?_1 # %=
689 case OO_PercentEqual: Out << "?_1"; break;
690 // <operator-name> ::= ?_2 # >>=
691 case OO_GreaterGreaterEqual: Out << "?_2"; break;
692 // <operator-name> ::= ?_3 # <<=
693 case OO_LessLessEqual: Out << "?_3"; break;
694 // <operator-name> ::= ?_4 # &=
695 case OO_AmpEqual: Out << "?_4"; break;
696 // <operator-name> ::= ?_5 # |=
697 case OO_PipeEqual: Out << "?_5"; break;
698 // <operator-name> ::= ?_6 # ^=
699 case OO_CaretEqual: Out << "?_6"; break;
700 // ?_7 # vftable
701 // ?_8 # vbtable
702 // ?_9 # vcall
703 // ?_A # typeof
704 // ?_B # local static guard
705 // ?_C # string
706 // ?_D # vbase destructor
707 // ?_E # vector deleting destructor
708 // ?_F # default constructor closure
709 // ?_G # scalar deleting destructor
710 // ?_H # vector constructor iterator
711 // ?_I # vector destructor iterator
712 // ?_J # vector vbase constructor iterator
713 // ?_K # virtual displacement map
714 // ?_L # eh vector constructor iterator
715 // ?_M # eh vector destructor iterator
716 // ?_N # eh vector vbase constructor iterator
717 // ?_O # copy constructor closure
718 // ?_P<name> # udt returning <name>
719 // ?_Q # <unknown>
720 // ?_R0 # RTTI Type Descriptor
721 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
722 // ?_R2 # RTTI Base Class Array
723 // ?_R3 # RTTI Class Hierarchy Descriptor
724 // ?_R4 # RTTI Complete Object Locator
725 // ?_S # local vftable
726 // ?_T # local vftable constructor closure
727 // <operator-name> ::= ?_U # new[]
728 case OO_Array_New: Out << "?_U"; break;
729 // <operator-name> ::= ?_V # delete[]
730 case OO_Array_Delete: Out << "?_V"; break;
731
732 case OO_Conditional: {
733 DiagnosticsEngine &Diags = Context.getDiags();
734 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
735 "cannot mangle this conditional operator yet");
736 Diags.Report(Loc, DiagID);
737 break;
738 }
739
740 case OO_None:
741 case NUM_OVERLOADED_OPERATORS:
742 llvm_unreachable("Not an overloaded operator");
743 }
744}
745
746void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
747 // <source name> ::= <identifier> @
748 std::string key = II->getNameStart();
749 BackRefMap::iterator Found;
750 if (UseNameBackReferences)
751 Found = NameBackReferences.find(key);
752 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
753 Out << II->getName() << '@';
754 if (UseNameBackReferences && NameBackReferences.size() < 10) {
755 size_t Size = NameBackReferences.size();
756 NameBackReferences[key] = Size;
757 }
758 } else {
759 Out << Found->second;
760 }
761}
762
763void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
764 Context.mangleObjCMethodName(MD, Out);
765}
766
767// Find out how many function decls live above this one and return an integer
768// suitable for use as the number in a numbered anonymous scope.
769// TODO: Memoize.
770static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
771 const DeclContext *DC = FD->getParent();
772 int level = 1;
773
774 while (DC && !DC->isTranslationUnit()) {
775 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
776 DC = DC->getParent();
777 }
778
779 return 2*level;
780}
781
782void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
783 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
784 // <numbered-anonymous-scope> ::= ? <number>
785 // Even though the name is rendered in reverse order (e.g.
786 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
787 // innermost. So a method bar in class C local to function foo gets mangled
788 // as something like:
789 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
790 // This is more apparent when you have a type nested inside a method of a
791 // type nested inside a function. A method baz in class D local to method
792 // bar of class C local to function foo gets mangled as:
793 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
794 // This scheme is general enough to support GCC-style nested
795 // functions. You could have a method baz of class C inside a function bar
796 // inside a function foo, like so:
797 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
798 int NestLevel = getLocalNestingLevel(FD);
799 Out << '?';
800 mangleNumber(NestLevel);
801 Out << '?';
802 mangle(FD, "?");
803}
804
805void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
806 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000807 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000808 // <template-name> ::= <unscoped-template-name> <template-args>
809 // ::= <substitution>
810 // Always start with the unqualified name.
811
812 // Templates have their own context for back references.
813 ArgBackRefMap OuterArgsContext;
814 BackRefMap OuterTemplateContext;
815 NameBackReferences.swap(OuterTemplateContext);
816 TypeBackReferences.swap(OuterArgsContext);
817
818 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000819 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000820
821 // Restore the previous back reference contexts.
822 NameBackReferences.swap(OuterTemplateContext);
823 TypeBackReferences.swap(OuterArgsContext);
824}
825
826void
827MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
828 // <unscoped-template-name> ::= ?$ <unqualified-name>
829 Out << "?$";
830 mangleUnqualifiedName(TD);
831}
832
833void
834MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
835 bool IsBoolean) {
836 // <integer-literal> ::= $0 <number>
837 Out << "$0";
838 // Make sure booleans are encoded as 0/1.
839 if (IsBoolean && Value.getBoolValue())
840 mangleNumber(1);
841 else
842 mangleNumber(Value);
843}
844
845void
846MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
847 // See if this is a constant expression.
848 llvm::APSInt Value;
849 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
850 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
851 return;
852 }
853
854 // As bad as this diagnostic is, it's better than crashing.
855 DiagnosticsEngine &Diags = Context.getDiags();
856 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
857 "cannot yet mangle expression type %0");
858 Diags.Report(E->getExprLoc(), DiagID)
859 << E->getStmtClassName() << E->getSourceRange();
860}
861
862void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000863MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
864 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000865 // <template-args> ::= {<type> | <integer-literal>}+ @
866 unsigned NumTemplateArgs = TemplateArgs.size();
867 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000868 const TemplateArgument &TA = TemplateArgs[i];
Reid Kleckner5d90d182013-07-02 18:10:07 +0000869 mangleTemplateArg(TD, TA, i);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000870 }
871 Out << '@';
872}
873
Reid Kleckner5d90d182013-07-02 18:10:07 +0000874void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
875 const TemplateArgument &TA,
876 int ArgIndex) {
877 switch (TA.getKind()) {
878 case TemplateArgument::Null:
879 llvm_unreachable("Can't mangle null template arguments!");
880 case TemplateArgument::Type: {
881 QualType T = TA.getAsType();
882 mangleType(T, SourceRange(), QMM_Escape);
883 break;
884 }
885 case TemplateArgument::Declaration:
886 mangle(cast<NamedDecl>(TA.getAsDecl()), "$1?");
887 break;
888 case TemplateArgument::Integral:
889 mangleIntegerLiteral(TA.getAsIntegral(),
890 TA.getIntegralType()->isBooleanType());
891 break;
David Majnemer7802fc92013-08-05 21:33:59 +0000892 case TemplateArgument::NullPtr:
893 Out << "$0A@";
894 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000895 case TemplateArgument::Expression:
896 mangleExpression(TA.getAsExpr());
897 break;
898 case TemplateArgument::Pack:
899 // Unlike Itanium, there is no character code to indicate an argument pack.
900 // FIXME: ArgIndex will be off, but we only use if for diagnostics that
901 // should ultimately be removed.
902 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
903 I != E; ++I)
904 mangleTemplateArg(TD, *I, ArgIndex);
905 break;
906 case TemplateArgument::Template:
David Majnemer7802fc92013-08-05 21:33:59 +0000907 case TemplateArgument::TemplateExpansion: {
Reid Kleckner5d90d182013-07-02 18:10:07 +0000908 // Issue a diagnostic.
909 DiagnosticsEngine &Diags = Context.getDiags();
910 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
911 "cannot mangle template argument %0 of kind %select{ERROR|ERROR|"
912 "pointer/reference|nullptr|integral|template|template pack expansion|"
913 "ERROR|parameter pack}1 yet");
914 Diags.Report(TD->getLocation(), DiagID)
915 << ArgIndex + 1
916 << TA.getKind()
917 << TD->getSourceRange();
918 }
919 }
920}
921
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000922void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
923 bool IsMember) {
924 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
925 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
926 // 'I' means __restrict (32/64-bit).
927 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
928 // keyword!
929 // <base-cvr-qualifiers> ::= A # near
930 // ::= B # near const
931 // ::= C # near volatile
932 // ::= D # near const volatile
933 // ::= E # far (16-bit)
934 // ::= F # far const (16-bit)
935 // ::= G # far volatile (16-bit)
936 // ::= H # far const volatile (16-bit)
937 // ::= I # huge (16-bit)
938 // ::= J # huge const (16-bit)
939 // ::= K # huge volatile (16-bit)
940 // ::= L # huge const volatile (16-bit)
941 // ::= M <basis> # based
942 // ::= N <basis> # based const
943 // ::= O <basis> # based volatile
944 // ::= P <basis> # based const volatile
945 // ::= Q # near member
946 // ::= R # near const member
947 // ::= S # near volatile member
948 // ::= T # near const volatile member
949 // ::= U # far member (16-bit)
950 // ::= V # far const member (16-bit)
951 // ::= W # far volatile member (16-bit)
952 // ::= X # far const volatile member (16-bit)
953 // ::= Y # huge member (16-bit)
954 // ::= Z # huge const member (16-bit)
955 // ::= 0 # huge volatile member (16-bit)
956 // ::= 1 # huge const volatile member (16-bit)
957 // ::= 2 <basis> # based member
958 // ::= 3 <basis> # based const member
959 // ::= 4 <basis> # based volatile member
960 // ::= 5 <basis> # based const volatile member
961 // ::= 6 # near function (pointers only)
962 // ::= 7 # far function (pointers only)
963 // ::= 8 # near method (pointers only)
964 // ::= 9 # far method (pointers only)
965 // ::= _A <basis> # based function (pointers only)
966 // ::= _B <basis> # based function (far?) (pointers only)
967 // ::= _C <basis> # based method (pointers only)
968 // ::= _D <basis> # based method (far?) (pointers only)
969 // ::= _E # block (Clang)
970 // <basis> ::= 0 # __based(void)
971 // ::= 1 # __based(segment)?
972 // ::= 2 <name> # __based(name)
973 // ::= 3 # ?
974 // ::= 4 # ?
975 // ::= 5 # not really based
976 bool HasConst = Quals.hasConst(),
977 HasVolatile = Quals.hasVolatile();
978 if (!IsMember) {
979 if (HasConst && HasVolatile) {
980 Out << 'D';
981 } else if (HasVolatile) {
982 Out << 'C';
983 } else if (HasConst) {
984 Out << 'B';
985 } else {
986 Out << 'A';
987 }
988 } else {
989 if (HasConst && HasVolatile) {
990 Out << 'T';
991 } else if (HasVolatile) {
992 Out << 'S';
993 } else if (HasConst) {
994 Out << 'R';
995 } else {
996 Out << 'Q';
997 }
998 }
999
1000 // FIXME: For now, just drop all extension qualifiers on the floor.
1001}
1002
1003void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1004 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1005 // ::= Q # const
1006 // ::= R # volatile
1007 // ::= S # const volatile
1008 bool HasConst = Quals.hasConst(),
1009 HasVolatile = Quals.hasVolatile();
1010 if (HasConst && HasVolatile) {
1011 Out << 'S';
1012 } else if (HasVolatile) {
1013 Out << 'R';
1014 } else if (HasConst) {
1015 Out << 'Q';
1016 } else {
1017 Out << 'P';
1018 }
1019}
1020
1021void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1022 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001023 // MSVC will backreference two canonically equivalent types that have slightly
1024 // different manglings when mangled alone.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001025 void *TypePtr = getASTContext().getCanonicalType(T).getAsOpaquePtr();
1026 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1027
1028 if (Found == TypeBackReferences.end()) {
1029 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1030
Reid Klecknerf21818d2013-06-24 19:21:52 +00001031 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1032 QualType OT = DT->getOriginalType();
1033 if (const ArrayType *AT = getASTContext().getAsArrayType(OT)) {
1034 mangleDecayedArrayType(AT, false);
1035 } else if (const FunctionType *FT = OT->getAs<FunctionType>()) {
1036 Out << "P6";
1037 mangleFunctionType(FT, 0, false, false);
1038 } else {
1039 llvm_unreachable("unexpected decayed type");
1040 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001041 } else {
1042 mangleType(T, Range, QMM_Drop);
1043 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001044
1045 // See if it's worth creating a back reference.
1046 // Only types longer than 1 character are considered
1047 // and only 10 back references slots are available:
1048 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1049 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1050 size_t Size = TypeBackReferences.size();
1051 TypeBackReferences[TypePtr] = Size;
1052 }
1053 } else {
1054 Out << Found->second;
1055 }
1056}
1057
1058void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001059 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001060 // Don't use the canonical types. MSVC includes things like 'const' on
1061 // pointer arguments to function pointers that canonicalization strips away.
1062 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001063 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001064 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1065 // If there were any Quals, getAsArrayType() pushed them onto the array
1066 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001067 if (QMM == QMM_Mangle)
1068 Out << 'A';
1069 else if (QMM == QMM_Escape || QMM == QMM_Result)
1070 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001071 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001072 return;
1073 }
1074
1075 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1076 T->isBlockPointerType();
1077
1078 switch (QMM) {
1079 case QMM_Drop:
1080 break;
1081 case QMM_Mangle:
1082 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1083 Out << '6';
1084 mangleFunctionType(FT, 0, false, false);
1085 return;
1086 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001087 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001088 break;
1089 case QMM_Escape:
1090 if (!IsPointer && Quals) {
1091 Out << "$$C";
1092 mangleQualifiers(Quals, false);
1093 }
1094 break;
1095 case QMM_Result:
1096 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1097 Out << '?';
1098 mangleQualifiers(Quals, false);
1099 }
1100 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001101 }
1102
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001103 // We have to mangle these now, while we still have enough information.
1104 if (IsPointer)
1105 manglePointerQualifiers(Quals);
1106 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001107
1108 switch (ty->getTypeClass()) {
1109#define ABSTRACT_TYPE(CLASS, PARENT)
1110#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1111 case Type::CLASS: \
1112 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1113 return;
1114#define TYPE(CLASS, PARENT) \
1115 case Type::CLASS: \
1116 mangleType(cast<CLASS##Type>(ty), Range); \
1117 break;
1118#include "clang/AST/TypeNodes.def"
1119#undef ABSTRACT_TYPE
1120#undef NON_CANONICAL_TYPE
1121#undef TYPE
1122 }
1123}
1124
1125void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1126 SourceRange Range) {
1127 // <type> ::= <builtin-type>
1128 // <builtin-type> ::= X # void
1129 // ::= C # signed char
1130 // ::= D # char
1131 // ::= E # unsigned char
1132 // ::= F # short
1133 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1134 // ::= H # int
1135 // ::= I # unsigned int
1136 // ::= J # long
1137 // ::= K # unsigned long
1138 // L # <none>
1139 // ::= M # float
1140 // ::= N # double
1141 // ::= O # long double (__float80 is mangled differently)
1142 // ::= _J # long long, __int64
1143 // ::= _K # unsigned long long, __int64
1144 // ::= _L # __int128
1145 // ::= _M # unsigned __int128
1146 // ::= _N # bool
1147 // _O # <array in parameter>
1148 // ::= _T # __float80 (Intel)
1149 // ::= _W # wchar_t
1150 // ::= _Z # __float80 (Digital Mars)
1151 switch (T->getKind()) {
1152 case BuiltinType::Void: Out << 'X'; break;
1153 case BuiltinType::SChar: Out << 'C'; break;
1154 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1155 case BuiltinType::UChar: Out << 'E'; break;
1156 case BuiltinType::Short: Out << 'F'; break;
1157 case BuiltinType::UShort: Out << 'G'; break;
1158 case BuiltinType::Int: Out << 'H'; break;
1159 case BuiltinType::UInt: Out << 'I'; break;
1160 case BuiltinType::Long: Out << 'J'; break;
1161 case BuiltinType::ULong: Out << 'K'; break;
1162 case BuiltinType::Float: Out << 'M'; break;
1163 case BuiltinType::Double: Out << 'N'; break;
1164 // TODO: Determine size and mangle accordingly
1165 case BuiltinType::LongDouble: Out << 'O'; break;
1166 case BuiltinType::LongLong: Out << "_J"; break;
1167 case BuiltinType::ULongLong: Out << "_K"; break;
1168 case BuiltinType::Int128: Out << "_L"; break;
1169 case BuiltinType::UInt128: Out << "_M"; break;
1170 case BuiltinType::Bool: Out << "_N"; break;
1171 case BuiltinType::WChar_S:
1172 case BuiltinType::WChar_U: Out << "_W"; break;
1173
1174#define BUILTIN_TYPE(Id, SingletonId)
1175#define PLACEHOLDER_TYPE(Id, SingletonId) \
1176 case BuiltinType::Id:
1177#include "clang/AST/BuiltinTypes.def"
1178 case BuiltinType::Dependent:
1179 llvm_unreachable("placeholder types shouldn't get to name mangling");
1180
1181 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1182 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1183 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001184
1185 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1186 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1187 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1188 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1189 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1190 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001191 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001192 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001193
1194 case BuiltinType::NullPtr: Out << "$$T"; break;
1195
1196 case BuiltinType::Char16:
1197 case BuiltinType::Char32:
1198 case BuiltinType::Half: {
1199 DiagnosticsEngine &Diags = Context.getDiags();
1200 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1201 "cannot mangle this built-in %0 type yet");
1202 Diags.Report(Range.getBegin(), DiagID)
1203 << T->getName(Context.getASTContext().getPrintingPolicy())
1204 << Range;
1205 break;
1206 }
1207 }
1208}
1209
1210// <type> ::= <function-type>
1211void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1212 SourceRange) {
1213 // Structors only appear in decls, so at this point we know it's not a
1214 // structor type.
1215 // FIXME: This may not be lambda-friendly.
1216 Out << "$$A6";
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001217 mangleFunctionType(T, NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001218}
1219void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1220 SourceRange) {
1221 llvm_unreachable("Can't mangle K&R function prototypes");
1222}
1223
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001224void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1225 const FunctionDecl *D,
1226 bool IsStructor,
1227 bool IsInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001228 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1229 // <return-type> <argument-list> <throw-spec>
1230 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1231
Reid Klecknerf21818d2013-06-24 19:21:52 +00001232 SourceRange Range;
1233 if (D) Range = D->getSourceRange();
1234
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001235 // If this is a C++ instance method, mangle the CVR qualifiers for the
1236 // this pointer.
1237 if (IsInstMethod)
1238 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
1239
1240 mangleCallingConvention(T, IsInstMethod);
1241
1242 // <return-type> ::= <type>
1243 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001244 if (IsStructor) {
1245 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1246 StructorType == Dtor_Deleting) {
1247 // The scalar deleting destructor takes an extra int argument.
1248 // However, the FunctionType generated has 0 arguments.
1249 // FIXME: This is a temporary hack.
1250 // Maybe should fix the FunctionType creation instead?
1251 Out << "PAXI@Z";
1252 return;
1253 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001254 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001255 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001256 mangleType(Proto->getResultType(), Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001257 }
1258
1259 // <argument-list> ::= X # void
1260 // ::= <type>+ @
1261 // ::= <type>* Z # varargs
1262 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1263 Out << 'X';
1264 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001265 // Happens for function pointer type arguments for example.
1266 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1267 ArgEnd = Proto->arg_type_end();
1268 Arg != ArgEnd; ++Arg)
1269 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001270 // <builtin-type> ::= Z # ellipsis
1271 if (Proto->isVariadic())
1272 Out << 'Z';
1273 else
1274 Out << '@';
1275 }
1276
1277 mangleThrowSpecification(Proto);
1278}
1279
1280void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001281 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1282 // # pointer. in 64-bit mode *all*
1283 // # 'this' pointers are 64-bit.
1284 // ::= <global-function>
1285 // <member-function> ::= A # private: near
1286 // ::= B # private: far
1287 // ::= C # private: static near
1288 // ::= D # private: static far
1289 // ::= E # private: virtual near
1290 // ::= F # private: virtual far
1291 // ::= G # private: thunk near
1292 // ::= H # private: thunk far
1293 // ::= I # protected: near
1294 // ::= J # protected: far
1295 // ::= K # protected: static near
1296 // ::= L # protected: static far
1297 // ::= M # protected: virtual near
1298 // ::= N # protected: virtual far
1299 // ::= O # protected: thunk near
1300 // ::= P # protected: thunk far
1301 // ::= Q # public: near
1302 // ::= R # public: far
1303 // ::= S # public: static near
1304 // ::= T # public: static far
1305 // ::= U # public: virtual near
1306 // ::= V # public: virtual far
1307 // ::= W # public: thunk near
1308 // ::= X # public: thunk far
1309 // <global-function> ::= Y # global near
1310 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001311 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1312 switch (MD->getAccess()) {
1313 default:
1314 case AS_private:
1315 if (MD->isStatic())
1316 Out << 'C';
1317 else if (MD->isVirtual())
1318 Out << 'E';
1319 else
1320 Out << 'A';
1321 break;
1322 case AS_protected:
1323 if (MD->isStatic())
1324 Out << 'K';
1325 else if (MD->isVirtual())
1326 Out << 'M';
1327 else
1328 Out << 'I';
1329 break;
1330 case AS_public:
1331 if (MD->isStatic())
1332 Out << 'S';
1333 else if (MD->isVirtual())
1334 Out << 'U';
1335 else
1336 Out << 'Q';
1337 }
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001338 if (PointersAre64Bit && !MD->isStatic())
1339 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001340 } else
1341 Out << 'Y';
1342}
1343void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
1344 bool IsInstMethod) {
1345 // <calling-convention> ::= A # __cdecl
1346 // ::= B # __export __cdecl
1347 // ::= C # __pascal
1348 // ::= D # __export __pascal
1349 // ::= E # __thiscall
1350 // ::= F # __export __thiscall
1351 // ::= G # __stdcall
1352 // ::= H # __export __stdcall
1353 // ::= I # __fastcall
1354 // ::= J # __export __fastcall
1355 // The 'export' calling conventions are from a bygone era
1356 // (*cough*Win16*cough*) when functions were declared for export with
1357 // that keyword. (It didn't actually export them, it just made them so
1358 // that they could be in a DLL and somebody from another module could call
1359 // them.)
1360 CallingConv CC = T->getCallConv();
1361 if (CC == CC_Default) {
1362 if (IsInstMethod) {
1363 const FunctionProtoType *FPT =
1364 T->getCanonicalTypeUnqualified().castAs<FunctionProtoType>();
1365 bool isVariadic = FPT->isVariadic();
1366 CC = getASTContext().getDefaultCXXMethodCallConv(isVariadic);
1367 } else {
1368 CC = CC_C;
1369 }
1370 }
1371 switch (CC) {
1372 default:
1373 llvm_unreachable("Unsupported CC for mangling");
1374 case CC_Default:
1375 case CC_C: Out << 'A'; break;
1376 case CC_X86Pascal: Out << 'C'; break;
1377 case CC_X86ThisCall: Out << 'E'; break;
1378 case CC_X86StdCall: Out << 'G'; break;
1379 case CC_X86FastCall: Out << 'I'; break;
1380 }
1381}
1382void MicrosoftCXXNameMangler::mangleThrowSpecification(
1383 const FunctionProtoType *FT) {
1384 // <throw-spec> ::= Z # throw(...) (default)
1385 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1386 // ::= <type>+
1387 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1388 // all actually mangled as 'Z'. (They're ignored because their associated
1389 // functionality isn't implemented, and probably never will be.)
1390 Out << 'Z';
1391}
1392
1393void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1394 SourceRange Range) {
1395 // Probably should be mangled as a template instantiation; need to see what
1396 // VC does first.
1397 DiagnosticsEngine &Diags = Context.getDiags();
1398 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1399 "cannot mangle this unresolved dependent type yet");
1400 Diags.Report(Range.getBegin(), DiagID)
1401 << Range;
1402}
1403
1404// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1405// <union-type> ::= T <name>
1406// <struct-type> ::= U <name>
1407// <class-type> ::= V <name>
1408// <enum-type> ::= W <size> <name>
1409void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
1410 mangleType(cast<TagType>(T));
1411}
1412void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
1413 mangleType(cast<TagType>(T));
1414}
1415void MicrosoftCXXNameMangler::mangleType(const TagType *T) {
1416 switch (T->getDecl()->getTagKind()) {
1417 case TTK_Union:
1418 Out << 'T';
1419 break;
1420 case TTK_Struct:
1421 case TTK_Interface:
1422 Out << 'U';
1423 break;
1424 case TTK_Class:
1425 Out << 'V';
1426 break;
1427 case TTK_Enum:
1428 Out << 'W';
1429 Out << getASTContext().getTypeSizeInChars(
1430 cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity();
1431 break;
1432 }
1433 mangleName(T->getDecl());
1434}
1435
1436// <type> ::= <array-type>
1437// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1438// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001439// <element-type> # as global, E is never required
1440// ::= Q E? <cvr-qualifiers> [Y <dimension-count> <dimension>+]
1441// <element-type> # as param, E is required for 64-bit
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001442// It's supposed to be the other way around, but for some strange reason, it
1443// isn't. Today this behavior is retained for the sole purpose of backwards
1444// compatibility.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001445void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T,
1446 bool IsGlobal) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001447 // This isn't a recursive mangling, so now we have to do it all in this
1448 // one call.
1449 if (IsGlobal) {
1450 manglePointerQualifiers(T->getElementType().getQualifiers());
1451 } else {
1452 Out << 'Q';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001453 if (PointersAre64Bit)
1454 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001455 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001456 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001457}
1458void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *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}
1462void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1463 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001464 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001465}
1466void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1467 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001468 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001469}
1470void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1471 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001472 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001473}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001474void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001475 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001476 SmallVector<llvm::APInt, 3> Dimensions;
1477 for (;;) {
1478 if (const ConstantArrayType *CAT =
1479 getASTContext().getAsConstantArrayType(ElementTy)) {
1480 Dimensions.push_back(CAT->getSize());
1481 ElementTy = CAT->getElementType();
1482 } else if (ElementTy->isVariableArrayType()) {
1483 const VariableArrayType *VAT =
1484 getASTContext().getAsVariableArrayType(ElementTy);
1485 DiagnosticsEngine &Diags = Context.getDiags();
1486 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1487 "cannot mangle this variable-length array yet");
1488 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1489 << VAT->getBracketsRange();
1490 return;
1491 } else if (ElementTy->isDependentSizedArrayType()) {
1492 // The dependent expression has to be folded into a constant (TODO).
1493 const DependentSizedArrayType *DSAT =
1494 getASTContext().getAsDependentSizedArrayType(ElementTy);
1495 DiagnosticsEngine &Diags = Context.getDiags();
1496 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1497 "cannot mangle this dependent-length array yet");
1498 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1499 << DSAT->getBracketsRange();
1500 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001501 } else if (const IncompleteArrayType *IAT =
1502 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1503 Dimensions.push_back(llvm::APInt(32, 0));
1504 ElementTy = IAT->getElementType();
1505 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001506 else break;
1507 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001508 Out << 'Y';
1509 // <dimension-count> ::= <number> # number of extra dimensions
1510 mangleNumber(Dimensions.size());
1511 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1512 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001513 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001514}
1515
1516// <type> ::= <pointer-to-member-type>
1517// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1518// <class name> <type>
1519void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1520 SourceRange Range) {
1521 QualType PointeeType = T->getPointeeType();
1522 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1523 Out << '8';
1524 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001525 mangleFunctionType(FPT, NULL, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001526 } else {
1527 mangleQualifiers(PointeeType.getQualifiers(), true);
1528 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001529 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001530 }
1531}
1532
1533void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1534 SourceRange Range) {
1535 DiagnosticsEngine &Diags = Context.getDiags();
1536 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1537 "cannot mangle this template type parameter type yet");
1538 Diags.Report(Range.getBegin(), DiagID)
1539 << Range;
1540}
1541
1542void MicrosoftCXXNameMangler::mangleType(
1543 const SubstTemplateTypeParmPackType *T,
1544 SourceRange Range) {
1545 DiagnosticsEngine &Diags = Context.getDiags();
1546 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1547 "cannot mangle this substituted parameter pack yet");
1548 Diags.Report(Range.getBegin(), DiagID)
1549 << Range;
1550}
1551
1552// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001553// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1554// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001555void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1556 SourceRange Range) {
1557 QualType PointeeTy = T->getPointeeType();
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001558 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1559 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001560 mangleType(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001561}
1562void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1563 SourceRange Range) {
1564 // Object pointers never have qualifiers.
1565 Out << 'A';
1566 mangleType(T->getPointeeType(), Range);
1567}
1568
1569// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001570// <reference-type> ::= A E? <cvr-qualifiers> <type>
1571// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001572void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1573 SourceRange Range) {
1574 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001575 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1576 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001577 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001578}
1579
1580// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001581// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1582// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001583void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1584 SourceRange Range) {
1585 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001586 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1587 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001588 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001589}
1590
1591void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1592 SourceRange Range) {
1593 DiagnosticsEngine &Diags = Context.getDiags();
1594 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1595 "cannot mangle this complex number type yet");
1596 Diags.Report(Range.getBegin(), DiagID)
1597 << Range;
1598}
1599
1600void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1601 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001602 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1603 assert(ET && "vectors with non-builtin elements are unsupported");
1604 uint64_t Width = getASTContext().getTypeSize(T);
1605 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1606 // doesn't match the Intel types uses a custom mangling below.
1607 bool IntelVector = true;
1608 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1609 Out << "T__m64";
1610 } else if (Width == 128 || Width == 256) {
1611 if (ET->getKind() == BuiltinType::Float)
1612 Out << "T__m" << Width;
1613 else if (ET->getKind() == BuiltinType::LongLong)
1614 Out << "T__m" << Width << 'i';
1615 else if (ET->getKind() == BuiltinType::Double)
1616 Out << "U__m" << Width << 'd';
1617 else
1618 IntelVector = false;
1619 } else {
1620 IntelVector = false;
1621 }
1622
1623 if (!IntelVector) {
1624 // The MS ABI doesn't have a special mangling for vector types, so we define
1625 // our own mangling to handle uses of __vector_size__ on user-specified
1626 // types, and for extensions like __v4sf.
1627 Out << "T__clang_vec" << T->getNumElements() << '_';
1628 mangleType(ET, Range);
1629 }
1630
1631 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001632}
Reid Kleckner1232e272013-03-26 16:56:59 +00001633
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001634void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1635 SourceRange Range) {
1636 DiagnosticsEngine &Diags = Context.getDiags();
1637 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1638 "cannot mangle this extended vector type yet");
1639 Diags.Report(Range.getBegin(), DiagID)
1640 << Range;
1641}
1642void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1643 SourceRange Range) {
1644 DiagnosticsEngine &Diags = Context.getDiags();
1645 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1646 "cannot mangle this dependent-sized extended vector type yet");
1647 Diags.Report(Range.getBegin(), DiagID)
1648 << Range;
1649}
1650
1651void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1652 SourceRange) {
1653 // ObjC interfaces have structs underlying them.
1654 Out << 'U';
1655 mangleName(T->getDecl());
1656}
1657
1658void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1659 SourceRange Range) {
1660 // We don't allow overloading by different protocol qualification,
1661 // so mangling them isn't necessary.
1662 mangleType(T->getBaseType(), Range);
1663}
1664
1665void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1666 SourceRange Range) {
1667 Out << "_E";
1668
1669 QualType pointee = T->getPointeeType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001670 mangleFunctionType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001671}
1672
1673void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T,
1674 SourceRange Range) {
1675 DiagnosticsEngine &Diags = Context.getDiags();
1676 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1677 "cannot mangle this injected class name type yet");
1678 Diags.Report(Range.getBegin(), DiagID)
1679 << Range;
1680}
1681
1682void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1683 SourceRange Range) {
1684 DiagnosticsEngine &Diags = Context.getDiags();
1685 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1686 "cannot mangle this template specialization type yet");
1687 Diags.Report(Range.getBegin(), DiagID)
1688 << Range;
1689}
1690
1691void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1692 SourceRange Range) {
1693 DiagnosticsEngine &Diags = Context.getDiags();
1694 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1695 "cannot mangle this dependent name type yet");
1696 Diags.Report(Range.getBegin(), DiagID)
1697 << Range;
1698}
1699
1700void MicrosoftCXXNameMangler::mangleType(
1701 const DependentTemplateSpecializationType *T,
1702 SourceRange Range) {
1703 DiagnosticsEngine &Diags = Context.getDiags();
1704 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1705 "cannot mangle this dependent template specialization type yet");
1706 Diags.Report(Range.getBegin(), DiagID)
1707 << Range;
1708}
1709
1710void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1711 SourceRange Range) {
1712 DiagnosticsEngine &Diags = Context.getDiags();
1713 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1714 "cannot mangle this pack expansion yet");
1715 Diags.Report(Range.getBegin(), DiagID)
1716 << Range;
1717}
1718
1719void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1720 SourceRange Range) {
1721 DiagnosticsEngine &Diags = Context.getDiags();
1722 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1723 "cannot mangle this typeof(type) yet");
1724 Diags.Report(Range.getBegin(), DiagID)
1725 << Range;
1726}
1727
1728void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1729 SourceRange Range) {
1730 DiagnosticsEngine &Diags = Context.getDiags();
1731 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1732 "cannot mangle this typeof(expression) yet");
1733 Diags.Report(Range.getBegin(), DiagID)
1734 << Range;
1735}
1736
1737void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1738 SourceRange Range) {
1739 DiagnosticsEngine &Diags = Context.getDiags();
1740 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1741 "cannot mangle this decltype() yet");
1742 Diags.Report(Range.getBegin(), DiagID)
1743 << Range;
1744}
1745
1746void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1747 SourceRange Range) {
1748 DiagnosticsEngine &Diags = Context.getDiags();
1749 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1750 "cannot mangle this unary transform type yet");
1751 Diags.Report(Range.getBegin(), DiagID)
1752 << Range;
1753}
1754
1755void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1756 DiagnosticsEngine &Diags = Context.getDiags();
1757 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1758 "cannot mangle this 'auto' type yet");
1759 Diags.Report(Range.getBegin(), DiagID)
1760 << Range;
1761}
1762
1763void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1764 SourceRange Range) {
1765 DiagnosticsEngine &Diags = Context.getDiags();
1766 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1767 "cannot mangle this C11 atomic type yet");
1768 Diags.Report(Range.getBegin(), DiagID)
1769 << Range;
1770}
1771
1772void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1773 raw_ostream &Out) {
1774 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1775 "Invalid mangleName() call, argument is not a variable or function!");
1776 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1777 "Invalid mangleName() call on 'structor decl!");
1778
1779 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1780 getASTContext().getSourceManager(),
1781 "Mangling declaration");
1782
1783 MicrosoftCXXNameMangler Mangler(*this, Out);
1784 return Mangler.mangle(D);
1785}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001786
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001787void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1788 const ThunkInfo &Thunk,
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001789 raw_ostream &Out) {
1790 // FIXME: this is not yet a complete implementation, but merely a
1791 // reasonably-working stub to avoid crashing when required to emit a thunk.
1792 MicrosoftCXXNameMangler Mangler(*this, Out);
1793 Out << "\01?";
1794 Mangler.mangleName(MD);
1795 if (Thunk.This.NonVirtual != 0) {
1796 // FIXME: add support for protected/private or use mangleFunctionClass.
1797 Out << "W";
1798 llvm::APSInt APSNumber(/*BitWidth=*/32 /*FIXME: check on x64*/,
1799 /*isUnsigned=*/true);
1800 APSNumber = -Thunk.This.NonVirtual;
1801 Mangler.mangleNumber(APSNumber);
1802 } else {
1803 // FIXME: add support for protected/private or use mangleFunctionClass.
1804 Out << "Q";
1805 }
1806 // FIXME: mangle return adjustment? Most likely includes using an overridee FPT?
1807 Mangler.mangleFunctionType(MD->getType()->castAs<FunctionProtoType>(), MD, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001808}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001809
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001810void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1811 CXXDtorType Type,
1812 const ThisAdjustment &,
1813 raw_ostream &) {
1814 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1815 "cannot mangle thunk for this destructor yet");
1816 getDiags().Report(DD->getLocation(), DiagID);
1817}
Reid Kleckner90633022013-06-19 15:20:38 +00001818
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001819void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1820 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001821 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1822 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001823 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001824 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001825 MicrosoftCXXNameMangler Mangler(*this, Out);
1826 Mangler.getStream() << "\01??_7";
1827 Mangler.mangleName(RD);
Reid Kleckner90633022013-06-19 15:20:38 +00001828 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001829 // TODO: If the class has more than one vtable, mangle in the class it came
1830 // from.
1831 Mangler.getStream() << '@';
1832}
Reid Kleckner90633022013-06-19 15:20:38 +00001833
1834void MicrosoftMangleContext::mangleCXXVBTable(
1835 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1836 raw_ostream &Out) {
1837 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1838 // <cvr-qualifiers> [<name>] @
1839 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1840 // is always '7' for vbtables.
1841 MicrosoftCXXNameMangler Mangler(*this, Out);
1842 Mangler.getStream() << "\01??_8";
1843 Mangler.mangleName(Derived);
1844 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1845 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1846 E = BasePath.end();
1847 I != E; ++I) {
1848 Mangler.mangleName(*I);
1849 }
1850 Mangler.getStream() << '@';
1851}
1852
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001853void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1854 raw_ostream &) {
1855 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1856}
1857void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1858 int64_t Offset,
1859 const CXXRecordDecl *Type,
1860 raw_ostream &) {
1861 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1862}
1863void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1864 raw_ostream &) {
1865 // FIXME: Give a location...
1866 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1867 "cannot mangle RTTI descriptors for type %0 yet");
1868 getDiags().Report(DiagID)
1869 << T.getBaseTypeIdentifier();
1870}
1871void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1872 raw_ostream &) {
1873 // FIXME: Give a location...
1874 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1875 "cannot mangle the name of type %0 into RTTI descriptors yet");
1876 getDiags().Report(DiagID)
1877 << T.getBaseTypeIdentifier();
1878}
1879void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1880 CXXCtorType Type,
1881 raw_ostream & Out) {
1882 MicrosoftCXXNameMangler mangler(*this, Out);
1883 mangler.mangle(D);
1884}
1885void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1886 CXXDtorType Type,
1887 raw_ostream & Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001888 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001889 mangler.mangle(D);
1890}
1891void MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *VD,
1892 raw_ostream &) {
1893 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1894 "cannot mangle this reference temporary yet");
1895 getDiags().Report(VD->getLocation(), DiagID);
1896}
1897
1898MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
1899 DiagnosticsEngine &Diags) {
1900 return new MicrosoftMangleContext(Context, Diags);
1901}