blob: 64e27f1f4bab0917b167ecbe35f594df9fd830e4 [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;
892 case TemplateArgument::Expression:
893 mangleExpression(TA.getAsExpr());
894 break;
895 case TemplateArgument::Pack:
896 // Unlike Itanium, there is no character code to indicate an argument pack.
897 // FIXME: ArgIndex will be off, but we only use if for diagnostics that
898 // should ultimately be removed.
899 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
900 I != E; ++I)
901 mangleTemplateArg(TD, *I, ArgIndex);
902 break;
903 case TemplateArgument::Template:
904 case TemplateArgument::TemplateExpansion:
905 case TemplateArgument::NullPtr: {
906 // Issue a diagnostic.
907 DiagnosticsEngine &Diags = Context.getDiags();
908 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
909 "cannot mangle template argument %0 of kind %select{ERROR|ERROR|"
910 "pointer/reference|nullptr|integral|template|template pack expansion|"
911 "ERROR|parameter pack}1 yet");
912 Diags.Report(TD->getLocation(), DiagID)
913 << ArgIndex + 1
914 << TA.getKind()
915 << TD->getSourceRange();
916 }
917 }
918}
919
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000920void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
921 bool IsMember) {
922 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
923 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
924 // 'I' means __restrict (32/64-bit).
925 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
926 // keyword!
927 // <base-cvr-qualifiers> ::= A # near
928 // ::= B # near const
929 // ::= C # near volatile
930 // ::= D # near const volatile
931 // ::= E # far (16-bit)
932 // ::= F # far const (16-bit)
933 // ::= G # far volatile (16-bit)
934 // ::= H # far const volatile (16-bit)
935 // ::= I # huge (16-bit)
936 // ::= J # huge const (16-bit)
937 // ::= K # huge volatile (16-bit)
938 // ::= L # huge const volatile (16-bit)
939 // ::= M <basis> # based
940 // ::= N <basis> # based const
941 // ::= O <basis> # based volatile
942 // ::= P <basis> # based const volatile
943 // ::= Q # near member
944 // ::= R # near const member
945 // ::= S # near volatile member
946 // ::= T # near const volatile member
947 // ::= U # far member (16-bit)
948 // ::= V # far const member (16-bit)
949 // ::= W # far volatile member (16-bit)
950 // ::= X # far const volatile member (16-bit)
951 // ::= Y # huge member (16-bit)
952 // ::= Z # huge const member (16-bit)
953 // ::= 0 # huge volatile member (16-bit)
954 // ::= 1 # huge const volatile member (16-bit)
955 // ::= 2 <basis> # based member
956 // ::= 3 <basis> # based const member
957 // ::= 4 <basis> # based volatile member
958 // ::= 5 <basis> # based const volatile member
959 // ::= 6 # near function (pointers only)
960 // ::= 7 # far function (pointers only)
961 // ::= 8 # near method (pointers only)
962 // ::= 9 # far method (pointers only)
963 // ::= _A <basis> # based function (pointers only)
964 // ::= _B <basis> # based function (far?) (pointers only)
965 // ::= _C <basis> # based method (pointers only)
966 // ::= _D <basis> # based method (far?) (pointers only)
967 // ::= _E # block (Clang)
968 // <basis> ::= 0 # __based(void)
969 // ::= 1 # __based(segment)?
970 // ::= 2 <name> # __based(name)
971 // ::= 3 # ?
972 // ::= 4 # ?
973 // ::= 5 # not really based
974 bool HasConst = Quals.hasConst(),
975 HasVolatile = Quals.hasVolatile();
976 if (!IsMember) {
977 if (HasConst && HasVolatile) {
978 Out << 'D';
979 } else if (HasVolatile) {
980 Out << 'C';
981 } else if (HasConst) {
982 Out << 'B';
983 } else {
984 Out << 'A';
985 }
986 } else {
987 if (HasConst && HasVolatile) {
988 Out << 'T';
989 } else if (HasVolatile) {
990 Out << 'S';
991 } else if (HasConst) {
992 Out << 'R';
993 } else {
994 Out << 'Q';
995 }
996 }
997
998 // FIXME: For now, just drop all extension qualifiers on the floor.
999}
1000
1001void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1002 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1003 // ::= Q # const
1004 // ::= R # volatile
1005 // ::= S # const volatile
1006 bool HasConst = Quals.hasConst(),
1007 HasVolatile = Quals.hasVolatile();
1008 if (HasConst && HasVolatile) {
1009 Out << 'S';
1010 } else if (HasVolatile) {
1011 Out << 'R';
1012 } else if (HasConst) {
1013 Out << 'Q';
1014 } else {
1015 Out << 'P';
1016 }
1017}
1018
1019void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1020 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001021 // MSVC will backreference two canonically equivalent types that have slightly
1022 // different manglings when mangled alone.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001023 void *TypePtr = getASTContext().getCanonicalType(T).getAsOpaquePtr();
1024 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1025
1026 if (Found == TypeBackReferences.end()) {
1027 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1028
Reid Klecknerf21818d2013-06-24 19:21:52 +00001029 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1030 QualType OT = DT->getOriginalType();
1031 if (const ArrayType *AT = getASTContext().getAsArrayType(OT)) {
1032 mangleDecayedArrayType(AT, false);
1033 } else if (const FunctionType *FT = OT->getAs<FunctionType>()) {
1034 Out << "P6";
1035 mangleFunctionType(FT, 0, false, false);
1036 } else {
1037 llvm_unreachable("unexpected decayed type");
1038 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001039 } else {
1040 mangleType(T, Range, QMM_Drop);
1041 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001042
1043 // See if it's worth creating a back reference.
1044 // Only types longer than 1 character are considered
1045 // and only 10 back references slots are available:
1046 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1047 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1048 size_t Size = TypeBackReferences.size();
1049 TypeBackReferences[TypePtr] = Size;
1050 }
1051 } else {
1052 Out << Found->second;
1053 }
1054}
1055
1056void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001057 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001058 // Don't use the canonical types. MSVC includes things like 'const' on
1059 // pointer arguments to function pointers that canonicalization strips away.
1060 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001061 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001062 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1063 // If there were any Quals, getAsArrayType() pushed them onto the array
1064 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001065 if (QMM == QMM_Mangle)
1066 Out << 'A';
1067 else if (QMM == QMM_Escape || QMM == QMM_Result)
1068 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001069 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001070 return;
1071 }
1072
1073 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1074 T->isBlockPointerType();
1075
1076 switch (QMM) {
1077 case QMM_Drop:
1078 break;
1079 case QMM_Mangle:
1080 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1081 Out << '6';
1082 mangleFunctionType(FT, 0, false, false);
1083 return;
1084 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001085 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001086 break;
1087 case QMM_Escape:
1088 if (!IsPointer && Quals) {
1089 Out << "$$C";
1090 mangleQualifiers(Quals, false);
1091 }
1092 break;
1093 case QMM_Result:
1094 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1095 Out << '?';
1096 mangleQualifiers(Quals, false);
1097 }
1098 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001099 }
1100
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001101 // We have to mangle these now, while we still have enough information.
1102 if (IsPointer)
1103 manglePointerQualifiers(Quals);
1104 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001105
1106 switch (ty->getTypeClass()) {
1107#define ABSTRACT_TYPE(CLASS, PARENT)
1108#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1109 case Type::CLASS: \
1110 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1111 return;
1112#define TYPE(CLASS, PARENT) \
1113 case Type::CLASS: \
1114 mangleType(cast<CLASS##Type>(ty), Range); \
1115 break;
1116#include "clang/AST/TypeNodes.def"
1117#undef ABSTRACT_TYPE
1118#undef NON_CANONICAL_TYPE
1119#undef TYPE
1120 }
1121}
1122
1123void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1124 SourceRange Range) {
1125 // <type> ::= <builtin-type>
1126 // <builtin-type> ::= X # void
1127 // ::= C # signed char
1128 // ::= D # char
1129 // ::= E # unsigned char
1130 // ::= F # short
1131 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1132 // ::= H # int
1133 // ::= I # unsigned int
1134 // ::= J # long
1135 // ::= K # unsigned long
1136 // L # <none>
1137 // ::= M # float
1138 // ::= N # double
1139 // ::= O # long double (__float80 is mangled differently)
1140 // ::= _J # long long, __int64
1141 // ::= _K # unsigned long long, __int64
1142 // ::= _L # __int128
1143 // ::= _M # unsigned __int128
1144 // ::= _N # bool
1145 // _O # <array in parameter>
1146 // ::= _T # __float80 (Intel)
1147 // ::= _W # wchar_t
1148 // ::= _Z # __float80 (Digital Mars)
1149 switch (T->getKind()) {
1150 case BuiltinType::Void: Out << 'X'; break;
1151 case BuiltinType::SChar: Out << 'C'; break;
1152 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1153 case BuiltinType::UChar: Out << 'E'; break;
1154 case BuiltinType::Short: Out << 'F'; break;
1155 case BuiltinType::UShort: Out << 'G'; break;
1156 case BuiltinType::Int: Out << 'H'; break;
1157 case BuiltinType::UInt: Out << 'I'; break;
1158 case BuiltinType::Long: Out << 'J'; break;
1159 case BuiltinType::ULong: Out << 'K'; break;
1160 case BuiltinType::Float: Out << 'M'; break;
1161 case BuiltinType::Double: Out << 'N'; break;
1162 // TODO: Determine size and mangle accordingly
1163 case BuiltinType::LongDouble: Out << 'O'; break;
1164 case BuiltinType::LongLong: Out << "_J"; break;
1165 case BuiltinType::ULongLong: Out << "_K"; break;
1166 case BuiltinType::Int128: Out << "_L"; break;
1167 case BuiltinType::UInt128: Out << "_M"; break;
1168 case BuiltinType::Bool: Out << "_N"; break;
1169 case BuiltinType::WChar_S:
1170 case BuiltinType::WChar_U: Out << "_W"; break;
1171
1172#define BUILTIN_TYPE(Id, SingletonId)
1173#define PLACEHOLDER_TYPE(Id, SingletonId) \
1174 case BuiltinType::Id:
1175#include "clang/AST/BuiltinTypes.def"
1176 case BuiltinType::Dependent:
1177 llvm_unreachable("placeholder types shouldn't get to name mangling");
1178
1179 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1180 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1181 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001182
1183 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1184 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1185 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1186 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1187 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1188 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001189 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001190 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001191
1192 case BuiltinType::NullPtr: Out << "$$T"; break;
1193
1194 case BuiltinType::Char16:
1195 case BuiltinType::Char32:
1196 case BuiltinType::Half: {
1197 DiagnosticsEngine &Diags = Context.getDiags();
1198 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1199 "cannot mangle this built-in %0 type yet");
1200 Diags.Report(Range.getBegin(), DiagID)
1201 << T->getName(Context.getASTContext().getPrintingPolicy())
1202 << Range;
1203 break;
1204 }
1205 }
1206}
1207
1208// <type> ::= <function-type>
1209void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1210 SourceRange) {
1211 // Structors only appear in decls, so at this point we know it's not a
1212 // structor type.
1213 // FIXME: This may not be lambda-friendly.
1214 Out << "$$A6";
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001215 mangleFunctionType(T, NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001216}
1217void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1218 SourceRange) {
1219 llvm_unreachable("Can't mangle K&R function prototypes");
1220}
1221
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001222void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1223 const FunctionDecl *D,
1224 bool IsStructor,
1225 bool IsInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001226 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1227 // <return-type> <argument-list> <throw-spec>
1228 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1229
Reid Klecknerf21818d2013-06-24 19:21:52 +00001230 SourceRange Range;
1231 if (D) Range = D->getSourceRange();
1232
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001233 // If this is a C++ instance method, mangle the CVR qualifiers for the
1234 // this pointer.
1235 if (IsInstMethod)
1236 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
1237
1238 mangleCallingConvention(T, IsInstMethod);
1239
1240 // <return-type> ::= <type>
1241 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001242 if (IsStructor) {
1243 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1244 StructorType == Dtor_Deleting) {
1245 // The scalar deleting destructor takes an extra int argument.
1246 // However, the FunctionType generated has 0 arguments.
1247 // FIXME: This is a temporary hack.
1248 // Maybe should fix the FunctionType creation instead?
1249 Out << "PAXI@Z";
1250 return;
1251 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001252 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001253 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001254 mangleType(Proto->getResultType(), Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001255 }
1256
1257 // <argument-list> ::= X # void
1258 // ::= <type>+ @
1259 // ::= <type>* Z # varargs
1260 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1261 Out << 'X';
1262 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001263 // Happens for function pointer type arguments for example.
1264 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1265 ArgEnd = Proto->arg_type_end();
1266 Arg != ArgEnd; ++Arg)
1267 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001268 // <builtin-type> ::= Z # ellipsis
1269 if (Proto->isVariadic())
1270 Out << 'Z';
1271 else
1272 Out << '@';
1273 }
1274
1275 mangleThrowSpecification(Proto);
1276}
1277
1278void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001279 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1280 // # pointer. in 64-bit mode *all*
1281 // # 'this' pointers are 64-bit.
1282 // ::= <global-function>
1283 // <member-function> ::= A # private: near
1284 // ::= B # private: far
1285 // ::= C # private: static near
1286 // ::= D # private: static far
1287 // ::= E # private: virtual near
1288 // ::= F # private: virtual far
1289 // ::= G # private: thunk near
1290 // ::= H # private: thunk far
1291 // ::= I # protected: near
1292 // ::= J # protected: far
1293 // ::= K # protected: static near
1294 // ::= L # protected: static far
1295 // ::= M # protected: virtual near
1296 // ::= N # protected: virtual far
1297 // ::= O # protected: thunk near
1298 // ::= P # protected: thunk far
1299 // ::= Q # public: near
1300 // ::= R # public: far
1301 // ::= S # public: static near
1302 // ::= T # public: static far
1303 // ::= U # public: virtual near
1304 // ::= V # public: virtual far
1305 // ::= W # public: thunk near
1306 // ::= X # public: thunk far
1307 // <global-function> ::= Y # global near
1308 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001309 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1310 switch (MD->getAccess()) {
1311 default:
1312 case AS_private:
1313 if (MD->isStatic())
1314 Out << 'C';
1315 else if (MD->isVirtual())
1316 Out << 'E';
1317 else
1318 Out << 'A';
1319 break;
1320 case AS_protected:
1321 if (MD->isStatic())
1322 Out << 'K';
1323 else if (MD->isVirtual())
1324 Out << 'M';
1325 else
1326 Out << 'I';
1327 break;
1328 case AS_public:
1329 if (MD->isStatic())
1330 Out << 'S';
1331 else if (MD->isVirtual())
1332 Out << 'U';
1333 else
1334 Out << 'Q';
1335 }
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001336 if (PointersAre64Bit && !MD->isStatic())
1337 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001338 } else
1339 Out << 'Y';
1340}
1341void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
1342 bool IsInstMethod) {
1343 // <calling-convention> ::= A # __cdecl
1344 // ::= B # __export __cdecl
1345 // ::= C # __pascal
1346 // ::= D # __export __pascal
1347 // ::= E # __thiscall
1348 // ::= F # __export __thiscall
1349 // ::= G # __stdcall
1350 // ::= H # __export __stdcall
1351 // ::= I # __fastcall
1352 // ::= J # __export __fastcall
1353 // The 'export' calling conventions are from a bygone era
1354 // (*cough*Win16*cough*) when functions were declared for export with
1355 // that keyword. (It didn't actually export them, it just made them so
1356 // that they could be in a DLL and somebody from another module could call
1357 // them.)
1358 CallingConv CC = T->getCallConv();
1359 if (CC == CC_Default) {
1360 if (IsInstMethod) {
1361 const FunctionProtoType *FPT =
1362 T->getCanonicalTypeUnqualified().castAs<FunctionProtoType>();
1363 bool isVariadic = FPT->isVariadic();
1364 CC = getASTContext().getDefaultCXXMethodCallConv(isVariadic);
1365 } else {
1366 CC = CC_C;
1367 }
1368 }
1369 switch (CC) {
1370 default:
1371 llvm_unreachable("Unsupported CC for mangling");
1372 case CC_Default:
1373 case CC_C: Out << 'A'; break;
1374 case CC_X86Pascal: Out << 'C'; break;
1375 case CC_X86ThisCall: Out << 'E'; break;
1376 case CC_X86StdCall: Out << 'G'; break;
1377 case CC_X86FastCall: Out << 'I'; break;
1378 }
1379}
1380void MicrosoftCXXNameMangler::mangleThrowSpecification(
1381 const FunctionProtoType *FT) {
1382 // <throw-spec> ::= Z # throw(...) (default)
1383 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1384 // ::= <type>+
1385 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1386 // all actually mangled as 'Z'. (They're ignored because their associated
1387 // functionality isn't implemented, and probably never will be.)
1388 Out << 'Z';
1389}
1390
1391void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1392 SourceRange Range) {
1393 // Probably should be mangled as a template instantiation; need to see what
1394 // VC does first.
1395 DiagnosticsEngine &Diags = Context.getDiags();
1396 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1397 "cannot mangle this unresolved dependent type yet");
1398 Diags.Report(Range.getBegin(), DiagID)
1399 << Range;
1400}
1401
1402// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1403// <union-type> ::= T <name>
1404// <struct-type> ::= U <name>
1405// <class-type> ::= V <name>
1406// <enum-type> ::= W <size> <name>
1407void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
1408 mangleType(cast<TagType>(T));
1409}
1410void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
1411 mangleType(cast<TagType>(T));
1412}
1413void MicrosoftCXXNameMangler::mangleType(const TagType *T) {
1414 switch (T->getDecl()->getTagKind()) {
1415 case TTK_Union:
1416 Out << 'T';
1417 break;
1418 case TTK_Struct:
1419 case TTK_Interface:
1420 Out << 'U';
1421 break;
1422 case TTK_Class:
1423 Out << 'V';
1424 break;
1425 case TTK_Enum:
1426 Out << 'W';
1427 Out << getASTContext().getTypeSizeInChars(
1428 cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity();
1429 break;
1430 }
1431 mangleName(T->getDecl());
1432}
1433
1434// <type> ::= <array-type>
1435// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1436// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001437// <element-type> # as global, E is never required
1438// ::= Q E? <cvr-qualifiers> [Y <dimension-count> <dimension>+]
1439// <element-type> # as param, E is required for 64-bit
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001440// It's supposed to be the other way around, but for some strange reason, it
1441// isn't. Today this behavior is retained for the sole purpose of backwards
1442// compatibility.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001443void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T,
1444 bool IsGlobal) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001445 // This isn't a recursive mangling, so now we have to do it all in this
1446 // one call.
1447 if (IsGlobal) {
1448 manglePointerQualifiers(T->getElementType().getQualifiers());
1449 } else {
1450 Out << 'Q';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001451 if (PointersAre64Bit)
1452 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001453 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001454 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001455}
1456void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1457 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001458 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001459}
1460void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1461 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001462 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001463}
1464void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1465 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001466 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001467}
1468void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1469 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001470 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001471}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001472void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001473 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001474 SmallVector<llvm::APInt, 3> Dimensions;
1475 for (;;) {
1476 if (const ConstantArrayType *CAT =
1477 getASTContext().getAsConstantArrayType(ElementTy)) {
1478 Dimensions.push_back(CAT->getSize());
1479 ElementTy = CAT->getElementType();
1480 } else if (ElementTy->isVariableArrayType()) {
1481 const VariableArrayType *VAT =
1482 getASTContext().getAsVariableArrayType(ElementTy);
1483 DiagnosticsEngine &Diags = Context.getDiags();
1484 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1485 "cannot mangle this variable-length array yet");
1486 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1487 << VAT->getBracketsRange();
1488 return;
1489 } else if (ElementTy->isDependentSizedArrayType()) {
1490 // The dependent expression has to be folded into a constant (TODO).
1491 const DependentSizedArrayType *DSAT =
1492 getASTContext().getAsDependentSizedArrayType(ElementTy);
1493 DiagnosticsEngine &Diags = Context.getDiags();
1494 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1495 "cannot mangle this dependent-length array yet");
1496 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1497 << DSAT->getBracketsRange();
1498 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001499 } else if (const IncompleteArrayType *IAT =
1500 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1501 Dimensions.push_back(llvm::APInt(32, 0));
1502 ElementTy = IAT->getElementType();
1503 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001504 else break;
1505 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001506 Out << 'Y';
1507 // <dimension-count> ::= <number> # number of extra dimensions
1508 mangleNumber(Dimensions.size());
1509 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1510 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001511 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001512}
1513
1514// <type> ::= <pointer-to-member-type>
1515// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1516// <class name> <type>
1517void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1518 SourceRange Range) {
1519 QualType PointeeType = T->getPointeeType();
1520 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1521 Out << '8';
1522 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001523 mangleFunctionType(FPT, NULL, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001524 } else {
1525 mangleQualifiers(PointeeType.getQualifiers(), true);
1526 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001527 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001528 }
1529}
1530
1531void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1532 SourceRange Range) {
1533 DiagnosticsEngine &Diags = Context.getDiags();
1534 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1535 "cannot mangle this template type parameter type yet");
1536 Diags.Report(Range.getBegin(), DiagID)
1537 << Range;
1538}
1539
1540void MicrosoftCXXNameMangler::mangleType(
1541 const SubstTemplateTypeParmPackType *T,
1542 SourceRange Range) {
1543 DiagnosticsEngine &Diags = Context.getDiags();
1544 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1545 "cannot mangle this substituted parameter pack yet");
1546 Diags.Report(Range.getBegin(), DiagID)
1547 << Range;
1548}
1549
1550// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001551// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1552// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001553void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1554 SourceRange Range) {
1555 QualType PointeeTy = T->getPointeeType();
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(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001559}
1560void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1561 SourceRange Range) {
1562 // Object pointers never have qualifiers.
1563 Out << 'A';
1564 mangleType(T->getPointeeType(), Range);
1565}
1566
1567// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001568// <reference-type> ::= A E? <cvr-qualifiers> <type>
1569// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001570void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1571 SourceRange Range) {
1572 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001573 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1574 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001575 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001576}
1577
1578// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001579// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1580// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001581void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1582 SourceRange Range) {
1583 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001584 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1585 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001586 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001587}
1588
1589void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1590 SourceRange Range) {
1591 DiagnosticsEngine &Diags = Context.getDiags();
1592 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1593 "cannot mangle this complex number type yet");
1594 Diags.Report(Range.getBegin(), DiagID)
1595 << Range;
1596}
1597
1598void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1599 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001600 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1601 assert(ET && "vectors with non-builtin elements are unsupported");
1602 uint64_t Width = getASTContext().getTypeSize(T);
1603 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1604 // doesn't match the Intel types uses a custom mangling below.
1605 bool IntelVector = true;
1606 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1607 Out << "T__m64";
1608 } else if (Width == 128 || Width == 256) {
1609 if (ET->getKind() == BuiltinType::Float)
1610 Out << "T__m" << Width;
1611 else if (ET->getKind() == BuiltinType::LongLong)
1612 Out << "T__m" << Width << 'i';
1613 else if (ET->getKind() == BuiltinType::Double)
1614 Out << "U__m" << Width << 'd';
1615 else
1616 IntelVector = false;
1617 } else {
1618 IntelVector = false;
1619 }
1620
1621 if (!IntelVector) {
1622 // The MS ABI doesn't have a special mangling for vector types, so we define
1623 // our own mangling to handle uses of __vector_size__ on user-specified
1624 // types, and for extensions like __v4sf.
1625 Out << "T__clang_vec" << T->getNumElements() << '_';
1626 mangleType(ET, Range);
1627 }
1628
1629 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001630}
Reid Kleckner1232e272013-03-26 16:56:59 +00001631
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001632void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1633 SourceRange Range) {
1634 DiagnosticsEngine &Diags = Context.getDiags();
1635 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1636 "cannot mangle this extended vector type yet");
1637 Diags.Report(Range.getBegin(), DiagID)
1638 << Range;
1639}
1640void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1641 SourceRange Range) {
1642 DiagnosticsEngine &Diags = Context.getDiags();
1643 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1644 "cannot mangle this dependent-sized extended vector type yet");
1645 Diags.Report(Range.getBegin(), DiagID)
1646 << Range;
1647}
1648
1649void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1650 SourceRange) {
1651 // ObjC interfaces have structs underlying them.
1652 Out << 'U';
1653 mangleName(T->getDecl());
1654}
1655
1656void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1657 SourceRange Range) {
1658 // We don't allow overloading by different protocol qualification,
1659 // so mangling them isn't necessary.
1660 mangleType(T->getBaseType(), Range);
1661}
1662
1663void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1664 SourceRange Range) {
1665 Out << "_E";
1666
1667 QualType pointee = T->getPointeeType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001668 mangleFunctionType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001669}
1670
1671void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T,
1672 SourceRange Range) {
1673 DiagnosticsEngine &Diags = Context.getDiags();
1674 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1675 "cannot mangle this injected class name type yet");
1676 Diags.Report(Range.getBegin(), DiagID)
1677 << Range;
1678}
1679
1680void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1681 SourceRange Range) {
1682 DiagnosticsEngine &Diags = Context.getDiags();
1683 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1684 "cannot mangle this template specialization type yet");
1685 Diags.Report(Range.getBegin(), DiagID)
1686 << Range;
1687}
1688
1689void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1690 SourceRange Range) {
1691 DiagnosticsEngine &Diags = Context.getDiags();
1692 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1693 "cannot mangle this dependent name type yet");
1694 Diags.Report(Range.getBegin(), DiagID)
1695 << Range;
1696}
1697
1698void MicrosoftCXXNameMangler::mangleType(
1699 const DependentTemplateSpecializationType *T,
1700 SourceRange Range) {
1701 DiagnosticsEngine &Diags = Context.getDiags();
1702 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1703 "cannot mangle this dependent template specialization type yet");
1704 Diags.Report(Range.getBegin(), DiagID)
1705 << Range;
1706}
1707
1708void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1709 SourceRange Range) {
1710 DiagnosticsEngine &Diags = Context.getDiags();
1711 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1712 "cannot mangle this pack expansion yet");
1713 Diags.Report(Range.getBegin(), DiagID)
1714 << Range;
1715}
1716
1717void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1718 SourceRange Range) {
1719 DiagnosticsEngine &Diags = Context.getDiags();
1720 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1721 "cannot mangle this typeof(type) yet");
1722 Diags.Report(Range.getBegin(), DiagID)
1723 << Range;
1724}
1725
1726void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1727 SourceRange Range) {
1728 DiagnosticsEngine &Diags = Context.getDiags();
1729 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1730 "cannot mangle this typeof(expression) yet");
1731 Diags.Report(Range.getBegin(), DiagID)
1732 << Range;
1733}
1734
1735void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1736 SourceRange Range) {
1737 DiagnosticsEngine &Diags = Context.getDiags();
1738 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1739 "cannot mangle this decltype() yet");
1740 Diags.Report(Range.getBegin(), DiagID)
1741 << Range;
1742}
1743
1744void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1745 SourceRange Range) {
1746 DiagnosticsEngine &Diags = Context.getDiags();
1747 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1748 "cannot mangle this unary transform type yet");
1749 Diags.Report(Range.getBegin(), DiagID)
1750 << Range;
1751}
1752
1753void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1754 DiagnosticsEngine &Diags = Context.getDiags();
1755 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1756 "cannot mangle this 'auto' type yet");
1757 Diags.Report(Range.getBegin(), DiagID)
1758 << Range;
1759}
1760
1761void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1762 SourceRange Range) {
1763 DiagnosticsEngine &Diags = Context.getDiags();
1764 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1765 "cannot mangle this C11 atomic type yet");
1766 Diags.Report(Range.getBegin(), DiagID)
1767 << Range;
1768}
1769
1770void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1771 raw_ostream &Out) {
1772 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1773 "Invalid mangleName() call, argument is not a variable or function!");
1774 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1775 "Invalid mangleName() call on 'structor decl!");
1776
1777 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1778 getASTContext().getSourceManager(),
1779 "Mangling declaration");
1780
1781 MicrosoftCXXNameMangler Mangler(*this, Out);
1782 return Mangler.mangle(D);
1783}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001784
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001785void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1786 const ThunkInfo &Thunk,
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001787 raw_ostream &Out) {
1788 // FIXME: this is not yet a complete implementation, but merely a
1789 // reasonably-working stub to avoid crashing when required to emit a thunk.
1790 MicrosoftCXXNameMangler Mangler(*this, Out);
1791 Out << "\01?";
1792 Mangler.mangleName(MD);
1793 if (Thunk.This.NonVirtual != 0) {
1794 // FIXME: add support for protected/private or use mangleFunctionClass.
1795 Out << "W";
1796 llvm::APSInt APSNumber(/*BitWidth=*/32 /*FIXME: check on x64*/,
1797 /*isUnsigned=*/true);
1798 APSNumber = -Thunk.This.NonVirtual;
1799 Mangler.mangleNumber(APSNumber);
1800 } else {
1801 // FIXME: add support for protected/private or use mangleFunctionClass.
1802 Out << "Q";
1803 }
1804 // FIXME: mangle return adjustment? Most likely includes using an overridee FPT?
1805 Mangler.mangleFunctionType(MD->getType()->castAs<FunctionProtoType>(), MD, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001806}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001807
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001808void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1809 CXXDtorType Type,
1810 const ThisAdjustment &,
1811 raw_ostream &) {
1812 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1813 "cannot mangle thunk for this destructor yet");
1814 getDiags().Report(DD->getLocation(), DiagID);
1815}
Reid Kleckner90633022013-06-19 15:20:38 +00001816
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001817void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1818 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001819 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1820 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001821 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001822 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001823 MicrosoftCXXNameMangler Mangler(*this, Out);
1824 Mangler.getStream() << "\01??_7";
1825 Mangler.mangleName(RD);
Reid Kleckner90633022013-06-19 15:20:38 +00001826 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001827 // TODO: If the class has more than one vtable, mangle in the class it came
1828 // from.
1829 Mangler.getStream() << '@';
1830}
Reid Kleckner90633022013-06-19 15:20:38 +00001831
1832void MicrosoftMangleContext::mangleCXXVBTable(
1833 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1834 raw_ostream &Out) {
1835 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1836 // <cvr-qualifiers> [<name>] @
1837 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1838 // is always '7' for vbtables.
1839 MicrosoftCXXNameMangler Mangler(*this, Out);
1840 Mangler.getStream() << "\01??_8";
1841 Mangler.mangleName(Derived);
1842 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1843 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1844 E = BasePath.end();
1845 I != E; ++I) {
1846 Mangler.mangleName(*I);
1847 }
1848 Mangler.getStream() << '@';
1849}
1850
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001851void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1852 raw_ostream &) {
1853 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1854}
1855void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1856 int64_t Offset,
1857 const CXXRecordDecl *Type,
1858 raw_ostream &) {
1859 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1860}
1861void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1862 raw_ostream &) {
1863 // FIXME: Give a location...
1864 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1865 "cannot mangle RTTI descriptors for type %0 yet");
1866 getDiags().Report(DiagID)
1867 << T.getBaseTypeIdentifier();
1868}
1869void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1870 raw_ostream &) {
1871 // FIXME: Give a location...
1872 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1873 "cannot mangle the name of type %0 into RTTI descriptors yet");
1874 getDiags().Report(DiagID)
1875 << T.getBaseTypeIdentifier();
1876}
1877void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1878 CXXCtorType Type,
1879 raw_ostream & Out) {
1880 MicrosoftCXXNameMangler mangler(*this, Out);
1881 mangler.mangle(D);
1882}
1883void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1884 CXXDtorType Type,
1885 raw_ostream & Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001886 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001887 mangler.mangle(D);
1888}
1889void MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *VD,
1890 raw_ostream &) {
1891 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1892 "cannot mangle this reference temporary yet");
1893 getDiags().Report(VD->getLocation(), DiagID);
1894}
1895
1896MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
1897 DiagnosticsEngine &Diags) {
1898 return new MicrosoftMangleContext(Context, Diags);
1899}