blob: 7e3d6c213953a8c266f74e548566cdea1f102761 [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);
David Majnemerc80eb462013-08-13 06:32:20 +000086 void mangleDeclaration(const NamedDecl *ND);
Guy Benyei7f92f2d2012-12-18 14:30:41 +000087 void mangleFunctionEncoding(const FunctionDecl *FD);
88 void mangleVariableEncoding(const VarDecl *VD);
89 void mangleNumber(int64_t Number);
90 void mangleNumber(const llvm::APSInt &Value);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +000091 void mangleType(QualType T, SourceRange Range,
92 QualifierMangleMode QMM = QMM_Mangle);
Timur Iskhodzhanov635de282013-07-30 09:46:19 +000093 void mangleFunctionType(const FunctionType *T, const FunctionDecl *D,
94 bool IsStructor, bool IsInstMethod);
Reid Kleckner942f9fe2013-09-10 20:14:30 +000095 void manglePostfix(const DeclContext *DC, bool NoFunction = false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +000096
97private:
98 void disableBackReferences() { UseNameBackReferences = false; }
99 void mangleUnqualifiedName(const NamedDecl *ND) {
100 mangleUnqualifiedName(ND, ND->getDeclName());
101 }
102 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
103 void mangleSourceName(const IdentifierInfo *II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000104 void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc);
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000105 void mangleCXXDtorType(CXXDtorType T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000106 void mangleQualifiers(Qualifiers Quals, bool IsMember);
107 void manglePointerQualifiers(Qualifiers Quals);
108
109 void mangleUnscopedTemplateName(const TemplateDecl *ND);
110 void mangleTemplateInstantiationName(const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000111 const TemplateArgumentList &TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000112 void mangleObjCMethodName(const ObjCMethodDecl *MD);
113 void mangleLocalName(const FunctionDecl *FD);
114
115 void mangleArgumentType(QualType T, SourceRange Range);
116
117 // Declare manglers for every type class.
118#define ABSTRACT_TYPE(CLASS, PARENT)
119#define NON_CANONICAL_TYPE(CLASS, PARENT)
120#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \
121 SourceRange Range);
122#include "clang/AST/TypeNodes.def"
123#undef ABSTRACT_TYPE
124#undef NON_CANONICAL_TYPE
125#undef TYPE
126
David Majnemer02c44f02013-08-05 22:26:46 +0000127 void mangleType(const TagDecl *TD);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000128 void mangleDecayedArrayType(const ArrayType *T, bool IsGlobal);
Reid Klecknerf21818d2013-06-24 19:21:52 +0000129 void mangleArrayType(const ArrayType *T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000130 void mangleFunctionClass(const FunctionDecl *FD);
131 void mangleCallingConvention(const FunctionType *T, bool IsInstMethod = false);
132 void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean);
133 void mangleExpression(const Expr *E);
134 void mangleThrowSpecification(const FunctionProtoType *T);
135
Reid Klecknerf16216c2013-03-20 01:40:23 +0000136 void mangleTemplateArgs(const TemplateDecl *TD,
137 const TemplateArgumentList &TemplateArgs);
David Majnemer309f6452013-08-27 08:21:25 +0000138 void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA);
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 &);
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000171 virtual void mangleReferenceTemporary(const VarDecl *, raw_ostream &);
172 virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out);
173 virtual void mangleDynamicAtExitDestructor(const VarDecl *D,
174 raw_ostream &Out);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000175};
176
177}
178
179static bool isInCLinkageSpecification(const Decl *D) {
180 D = D->getCanonicalDecl();
181 for (const DeclContext *DC = D->getDeclContext();
182 !DC->isTranslationUnit(); DC = DC->getParent()) {
183 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
184 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
185 }
186
187 return false;
188}
189
190bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
191 // In C, functions with no attributes never need to be mangled. Fastpath them.
192 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
193 return false;
194
195 // Any decl can be declared with __asm("foo") on it, and this takes precedence
196 // over all other naming in the .o file.
197 if (D->hasAttr<AsmLabelAttr>())
198 return true;
199
200 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
201 // (always) as does passing a C++ member function and a function
202 // whose name is not a simple identifier.
203 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
204 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
205 !FD->getDeclName().isIdentifier()))
206 return true;
207
208 // Otherwise, no mangling is done outside C++ mode.
209 if (!getASTContext().getLangOpts().CPlusPlus)
210 return false;
211
212 // Variables at global scope with internal linkage are not mangled.
213 if (!FD) {
214 const DeclContext *DC = D->getDeclContext();
Rafael Espindola181e3ec2013-05-13 00:12:11 +0000215 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000216 return false;
217 }
218
219 // C functions and "main" are not mangled.
220 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
221 return false;
222
223 return true;
224}
225
226void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
227 StringRef Prefix) {
228 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
229 // Therefore it's really important that we don't decorate the
230 // name with leading underscores or leading/trailing at signs. So, by
231 // default, we emit an asm marker at the start so we get the name right.
232 // Callers can override this with a custom prefix.
233
234 // Any decl can be declared with __asm("foo") on it, and this takes precedence
235 // over all other naming in the .o file.
236 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
237 // If we have an asm name, then we use it as the mangling.
238 Out << '\01' << ALA->getLabel();
239 return;
240 }
241
242 // <mangled-name> ::= ? <name> <type-encoding>
243 Out << Prefix;
244 mangleName(D);
245 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
246 mangleFunctionEncoding(FD);
247 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
248 mangleVariableEncoding(VD);
249 else {
250 // TODO: Fields? Can MSVC even mangle them?
251 // Issue a diagnostic for now.
252 DiagnosticsEngine &Diags = Context.getDiags();
253 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
254 "cannot mangle this declaration yet");
255 Diags.Report(D->getLocation(), DiagID)
256 << D->getSourceRange();
257 }
258}
259
260void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
261 // <type-encoding> ::= <function-class> <function-type>
262
Reid Klecknerf21818d2013-06-24 19:21:52 +0000263 // Since MSVC operates on the type as written and not the canonical type, it
264 // actually matters which decl we have here. MSVC appears to choose the
265 // first, since it is most likely to be the declaration in a header file.
266 FD = FD->getFirstDeclaration();
267
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000268 // Don't mangle in the type if this isn't a decl we should typically mangle.
269 if (!Context.shouldMangleDeclName(FD))
270 return;
271
272 // We should never ever see a FunctionNoProtoType at this point.
273 // We don't even know how to mangle their types anyway :).
Reid Klecknerf21818d2013-06-24 19:21:52 +0000274 TypeSourceInfo *TSI = FD->getTypeSourceInfo();
275 QualType T = TSI ? TSI->getType() : FD->getType();
276 const FunctionProtoType *FT = T->castAs<FunctionProtoType>();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000277
278 bool InStructor = false, InInstMethod = false;
279 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
280 if (MD) {
281 if (MD->isInstance())
282 InInstMethod = true;
283 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
284 InStructor = true;
285 }
286
287 // First, the function class.
288 mangleFunctionClass(FD);
289
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000290 mangleFunctionType(FT, FD, InStructor, InInstMethod);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000291}
292
293void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
294 // <type-encoding> ::= <storage-class> <variable-type>
295 // <storage-class> ::= 0 # private static member
296 // ::= 1 # protected static member
297 // ::= 2 # public static member
298 // ::= 3 # global
299 // ::= 4 # static local
300
301 // The first character in the encoding (after the name) is the storage class.
302 if (VD->isStaticDataMember()) {
303 // If it's a static member, it also encodes the access level.
304 switch (VD->getAccess()) {
305 default:
306 case AS_private: Out << '0'; break;
307 case AS_protected: Out << '1'; break;
308 case AS_public: Out << '2'; break;
309 }
310 }
311 else if (!VD->isStaticLocal())
312 Out << '3';
313 else
314 Out << '4';
315 // Now mangle the type.
316 // <variable-type> ::= <type> <cvr-qualifiers>
317 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
318 // Pointers and references are odd. The type of 'int * const foo;' gets
319 // mangled as 'QAHA' instead of 'PAHB', for example.
320 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
321 QualType Ty = TL.getType();
David Majnemer1c7a4092013-08-15 08:13:23 +0000322 if (Ty->isPointerType() || Ty->isReferenceType() ||
323 Ty->isMemberPointerType()) {
David Majnemer17ffbd02013-08-09 05:56:24 +0000324 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000325 if (PointersAre64Bit)
326 Out << 'E';
327 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) {
328 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true);
329 // Member pointers are suffixed with a back reference to the member
330 // pointer's class name.
331 mangleName(MPT->getClass()->getAsCXXRecordDecl());
332 } else
333 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
David Majnemer17ffbd02013-08-09 05:56:24 +0000334 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000335 // Global arrays are funny, too.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000336 mangleDecayedArrayType(AT, true);
337 if (AT->getElementType()->isArrayType())
338 Out << 'A';
339 else
340 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000341 } else {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000342 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000343 mangleQualifiers(Ty.getLocalQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000344 }
345}
346
347void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
348 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
349 const DeclContext *DC = ND->getDeclContext();
350
351 // Always start with the unqualified name.
352 mangleUnqualifiedName(ND);
353
354 // If this is an extern variable declared locally, the relevant DeclContext
355 // is that of the containing namespace, or the translation unit.
356 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
357 while (!DC->isNamespace() && !DC->isTranslationUnit())
358 DC = DC->getParent();
359
360 manglePostfix(DC);
361
362 // Terminate the whole name with an '@'.
363 Out << '@';
364}
365
366void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
367 llvm::APSInt APSNumber(/*BitWidth=*/64, /*isUnsigned=*/false);
368 APSNumber = Number;
369 mangleNumber(APSNumber);
370}
371
372void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
373 // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
374 // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
375 // ::= [?] @ # 0 (alternate mangling, not emitted by VC)
376 if (Value.isSigned() && Value.isNegative()) {
377 Out << '?';
378 mangleNumber(llvm::APSInt(Value.abs()));
379 return;
380 }
381 llvm::APSInt Temp(Value);
382 // There's a special shorter mangling for 0, but Microsoft
383 // chose not to use it. Instead, 0 gets mangled as "A@". Oh well...
384 if (Value.uge(1) && Value.ule(10)) {
385 --Temp;
386 Temp.print(Out, false);
387 } else {
388 // We have to build up the encoding in reverse order, so it will come
389 // out right when we write it out.
390 char Encoding[64];
391 char *EndPtr = Encoding+sizeof(Encoding);
392 char *CurPtr = EndPtr;
393 llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned());
394 NibbleMask = 0xf;
395 do {
396 *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf);
397 Temp = Temp.lshr(4);
398 } while (Temp != 0);
399 Out.write(CurPtr, EndPtr-CurPtr);
400 Out << '@';
401 }
402}
403
404static const TemplateDecl *
Reid Klecknerf16216c2013-03-20 01:40:23 +0000405isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000406 // Check if we have a function template.
407 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
408 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000409 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000410 return TD;
411 }
412 }
413
414 // Check if we have a class template.
415 if (const ClassTemplateSpecializationDecl *Spec =
Reid Klecknerf16216c2013-03-20 01:40:23 +0000416 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
417 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000418 return Spec->getSpecializedTemplate();
419 }
420
421 return 0;
422}
423
424void
425MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
426 DeclarationName Name) {
427 // <unqualified-name> ::= <operator-name>
428 // ::= <ctor-dtor-name>
429 // ::= <source-name>
430 // ::= <template-name>
Reid Klecknerf16216c2013-03-20 01:40:23 +0000431
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000432 // Check if we have a template.
Reid Klecknerf16216c2013-03-20 01:40:23 +0000433 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000434 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Kleckner3be37d12013-07-13 00:43:39 +0000435 // Function templates aren't considered for name back referencing. This
436 // makes sense since function templates aren't likely to occur multiple
437 // times in a symbol.
438 // FIXME: Test alias template mangling with MSVC 2013.
439 if (!isa<ClassTemplateDecl>(TD)) {
440 mangleTemplateInstantiationName(TD, *TemplateArgs);
441 return;
442 }
443
444 // We have a class template.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000445 // Here comes the tricky thing: if we need to mangle something like
446 // void foo(A::X<Y>, B::X<Y>),
447 // the X<Y> part is aliased. However, if you need to mangle
448 // void foo(A::X<A::Y>, A::X<B::Y>),
449 // the A::X<> part is not aliased.
450 // That said, from the mangler's perspective we have a structure like this:
451 // namespace[s] -> type[ -> template-parameters]
452 // but from the Clang perspective we have
453 // type [ -> template-parameters]
454 // \-> namespace[s]
455 // What we do is we create a new mangler, mangle the same type (without
456 // a namespace suffix) using the extra mangler with back references
457 // disabled (to avoid infinite recursion) and then use the mangled type
458 // name as a key to check the mangling of different types for aliasing.
459
460 std::string BackReferenceKey;
461 BackRefMap::iterator Found;
462 if (UseNameBackReferences) {
463 llvm::raw_string_ostream Stream(BackReferenceKey);
464 MicrosoftCXXNameMangler Extra(Context, Stream);
465 Extra.disableBackReferences();
466 Extra.mangleUnqualifiedName(ND, Name);
467 Stream.flush();
468
469 Found = NameBackReferences.find(BackReferenceKey);
470 }
471 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000472 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000473 if (UseNameBackReferences && NameBackReferences.size() < 10) {
474 size_t Size = NameBackReferences.size();
475 NameBackReferences[BackReferenceKey] = Size;
476 }
477 } else {
478 Out << Found->second;
479 }
480 return;
481 }
482
483 switch (Name.getNameKind()) {
484 case DeclarationName::Identifier: {
485 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
486 mangleSourceName(II);
487 break;
488 }
489
490 // Otherwise, an anonymous entity. We must have a declaration.
491 assert(ND && "mangling empty name without declaration");
492
493 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
494 if (NS->isAnonymousNamespace()) {
495 Out << "?A@";
496 break;
497 }
498 }
499
500 // We must have an anonymous struct.
501 const TagDecl *TD = cast<TagDecl>(ND);
502 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
503 assert(TD->getDeclContext() == D->getDeclContext() &&
504 "Typedef should not be in another decl context!");
505 assert(D->getDeclName().getAsIdentifierInfo() &&
506 "Typedef was not named!");
507 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
508 break;
509 }
510
511 // When VC encounters an anonymous type with no tag and no typedef,
David Majnemerec0258a2013-08-26 02:35:51 +0000512 // it literally emits '<unnamed-tag>@'.
513 Out << "<unnamed-tag>@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000514 break;
515 }
516
517 case DeclarationName::ObjCZeroArgSelector:
518 case DeclarationName::ObjCOneArgSelector:
519 case DeclarationName::ObjCMultiArgSelector:
520 llvm_unreachable("Can't mangle Objective-C selector names here!");
521
522 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000523 if (ND == Structor) {
524 assert(StructorType == Ctor_Complete &&
525 "Should never be asked to mangle a ctor other than complete");
526 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000527 Out << "?0";
528 break;
529
530 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000531 if (ND == Structor)
532 // If the named decl is the C++ destructor we're mangling,
533 // use the type we were given.
534 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
535 else
Reid Klecknera4130ba2013-07-22 13:51:44 +0000536 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000537 // class with a destructor is declared within a destructor.
Reid Klecknera4130ba2013-07-22 13:51:44 +0000538 mangleCXXDtorType(Dtor_Base);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000539 break;
540
541 case DeclarationName::CXXConversionFunctionName:
542 // <operator-name> ::= ?B # (cast)
543 // The target type is encoded as the return type.
544 Out << "?B";
545 break;
546
547 case DeclarationName::CXXOperatorName:
548 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
549 break;
550
551 case DeclarationName::CXXLiteralOperatorName: {
552 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
553 DiagnosticsEngine Diags = Context.getDiags();
554 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
555 "cannot mangle this literal operator yet");
556 Diags.Report(ND->getLocation(), DiagID);
557 break;
558 }
559
560 case DeclarationName::CXXUsingDirective:
561 llvm_unreachable("Can't mangle a using directive name!");
562 }
563}
564
565void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
566 bool NoFunction) {
567 // <postfix> ::= <unqualified-name> [<postfix>]
568 // ::= <substitution> [<postfix>]
569
570 if (!DC) return;
571
572 while (isa<LinkageSpecDecl>(DC))
573 DC = DC->getParent();
574
575 if (DC->isTranslationUnit())
576 return;
577
578 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedmane5798892013-07-10 01:13:27 +0000579 DiagnosticsEngine Diags = Context.getDiags();
580 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
581 "cannot mangle a local inside this block yet");
582 Diags.Report(BD->getLocation(), DiagID);
583
584 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
585 // for how this should be done.
586 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000587 Out << '@';
588 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000589 } else if (isa<CapturedDecl>(DC)) {
590 // Skip CapturedDecl context.
591 manglePostfix(DC->getParent(), NoFunction);
592 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000593 }
594
595 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
596 return;
597 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
598 mangleObjCMethodName(Method);
599 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
600 mangleLocalName(Func);
601 else {
602 mangleUnqualifiedName(cast<NamedDecl>(DC));
603 manglePostfix(DC->getParent(), NoFunction);
604 }
605}
606
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000607void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000608 // Microsoft uses the names on the case labels for these dtor variants. Clang
609 // uses the Itanium terminology internally. Everything in this ABI delegates
610 // towards the base dtor.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000611 switch (T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000612 // <operator-name> ::= ?1 # destructor
613 case Dtor_Base: Out << "?1"; return;
614 // <operator-name> ::= ?_D # vbase destructor
615 case Dtor_Complete: Out << "?_D"; return;
616 // <operator-name> ::= ?_G # scalar deleting destructor
617 case Dtor_Deleting: Out << "?_G"; return;
618 // <operator-name> ::= ?_E # vector deleting destructor
619 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
620 // it.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000621 }
622 llvm_unreachable("Unsupported dtor type?");
623}
624
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000625void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
626 SourceLocation Loc) {
627 switch (OO) {
628 // ?0 # constructor
629 // ?1 # destructor
630 // <operator-name> ::= ?2 # new
631 case OO_New: Out << "?2"; break;
632 // <operator-name> ::= ?3 # delete
633 case OO_Delete: Out << "?3"; break;
634 // <operator-name> ::= ?4 # =
635 case OO_Equal: Out << "?4"; break;
636 // <operator-name> ::= ?5 # >>
637 case OO_GreaterGreater: Out << "?5"; break;
638 // <operator-name> ::= ?6 # <<
639 case OO_LessLess: Out << "?6"; break;
640 // <operator-name> ::= ?7 # !
641 case OO_Exclaim: Out << "?7"; break;
642 // <operator-name> ::= ?8 # ==
643 case OO_EqualEqual: Out << "?8"; break;
644 // <operator-name> ::= ?9 # !=
645 case OO_ExclaimEqual: Out << "?9"; break;
646 // <operator-name> ::= ?A # []
647 case OO_Subscript: Out << "?A"; break;
648 // ?B # conversion
649 // <operator-name> ::= ?C # ->
650 case OO_Arrow: Out << "?C"; break;
651 // <operator-name> ::= ?D # *
652 case OO_Star: Out << "?D"; break;
653 // <operator-name> ::= ?E # ++
654 case OO_PlusPlus: Out << "?E"; break;
655 // <operator-name> ::= ?F # --
656 case OO_MinusMinus: Out << "?F"; break;
657 // <operator-name> ::= ?G # -
658 case OO_Minus: Out << "?G"; break;
659 // <operator-name> ::= ?H # +
660 case OO_Plus: Out << "?H"; break;
661 // <operator-name> ::= ?I # &
662 case OO_Amp: Out << "?I"; break;
663 // <operator-name> ::= ?J # ->*
664 case OO_ArrowStar: Out << "?J"; break;
665 // <operator-name> ::= ?K # /
666 case OO_Slash: Out << "?K"; break;
667 // <operator-name> ::= ?L # %
668 case OO_Percent: Out << "?L"; break;
669 // <operator-name> ::= ?M # <
670 case OO_Less: Out << "?M"; break;
671 // <operator-name> ::= ?N # <=
672 case OO_LessEqual: Out << "?N"; break;
673 // <operator-name> ::= ?O # >
674 case OO_Greater: Out << "?O"; break;
675 // <operator-name> ::= ?P # >=
676 case OO_GreaterEqual: Out << "?P"; break;
677 // <operator-name> ::= ?Q # ,
678 case OO_Comma: Out << "?Q"; break;
679 // <operator-name> ::= ?R # ()
680 case OO_Call: Out << "?R"; break;
681 // <operator-name> ::= ?S # ~
682 case OO_Tilde: Out << "?S"; break;
683 // <operator-name> ::= ?T # ^
684 case OO_Caret: Out << "?T"; break;
685 // <operator-name> ::= ?U # |
686 case OO_Pipe: Out << "?U"; break;
687 // <operator-name> ::= ?V # &&
688 case OO_AmpAmp: Out << "?V"; break;
689 // <operator-name> ::= ?W # ||
690 case OO_PipePipe: Out << "?W"; break;
691 // <operator-name> ::= ?X # *=
692 case OO_StarEqual: Out << "?X"; break;
693 // <operator-name> ::= ?Y # +=
694 case OO_PlusEqual: Out << "?Y"; break;
695 // <operator-name> ::= ?Z # -=
696 case OO_MinusEqual: Out << "?Z"; break;
697 // <operator-name> ::= ?_0 # /=
698 case OO_SlashEqual: Out << "?_0"; break;
699 // <operator-name> ::= ?_1 # %=
700 case OO_PercentEqual: Out << "?_1"; break;
701 // <operator-name> ::= ?_2 # >>=
702 case OO_GreaterGreaterEqual: Out << "?_2"; break;
703 // <operator-name> ::= ?_3 # <<=
704 case OO_LessLessEqual: Out << "?_3"; break;
705 // <operator-name> ::= ?_4 # &=
706 case OO_AmpEqual: Out << "?_4"; break;
707 // <operator-name> ::= ?_5 # |=
708 case OO_PipeEqual: Out << "?_5"; break;
709 // <operator-name> ::= ?_6 # ^=
710 case OO_CaretEqual: Out << "?_6"; break;
711 // ?_7 # vftable
712 // ?_8 # vbtable
713 // ?_9 # vcall
714 // ?_A # typeof
715 // ?_B # local static guard
716 // ?_C # string
717 // ?_D # vbase destructor
718 // ?_E # vector deleting destructor
719 // ?_F # default constructor closure
720 // ?_G # scalar deleting destructor
721 // ?_H # vector constructor iterator
722 // ?_I # vector destructor iterator
723 // ?_J # vector vbase constructor iterator
724 // ?_K # virtual displacement map
725 // ?_L # eh vector constructor iterator
726 // ?_M # eh vector destructor iterator
727 // ?_N # eh vector vbase constructor iterator
728 // ?_O # copy constructor closure
729 // ?_P<name> # udt returning <name>
730 // ?_Q # <unknown>
731 // ?_R0 # RTTI Type Descriptor
732 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
733 // ?_R2 # RTTI Base Class Array
734 // ?_R3 # RTTI Class Hierarchy Descriptor
735 // ?_R4 # RTTI Complete Object Locator
736 // ?_S # local vftable
737 // ?_T # local vftable constructor closure
738 // <operator-name> ::= ?_U # new[]
739 case OO_Array_New: Out << "?_U"; break;
740 // <operator-name> ::= ?_V # delete[]
741 case OO_Array_Delete: Out << "?_V"; break;
742
743 case OO_Conditional: {
744 DiagnosticsEngine &Diags = Context.getDiags();
745 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
746 "cannot mangle this conditional operator yet");
747 Diags.Report(Loc, DiagID);
748 break;
749 }
750
751 case OO_None:
752 case NUM_OVERLOADED_OPERATORS:
753 llvm_unreachable("Not an overloaded operator");
754 }
755}
756
757void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
758 // <source name> ::= <identifier> @
759 std::string key = II->getNameStart();
760 BackRefMap::iterator Found;
761 if (UseNameBackReferences)
762 Found = NameBackReferences.find(key);
763 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
764 Out << II->getName() << '@';
765 if (UseNameBackReferences && NameBackReferences.size() < 10) {
766 size_t Size = NameBackReferences.size();
767 NameBackReferences[key] = Size;
768 }
769 } else {
770 Out << Found->second;
771 }
772}
773
774void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
775 Context.mangleObjCMethodName(MD, Out);
776}
777
778// Find out how many function decls live above this one and return an integer
779// suitable for use as the number in a numbered anonymous scope.
780// TODO: Memoize.
781static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
782 const DeclContext *DC = FD->getParent();
783 int level = 1;
784
785 while (DC && !DC->isTranslationUnit()) {
786 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
787 DC = DC->getParent();
788 }
789
790 return 2*level;
791}
792
793void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
794 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
795 // <numbered-anonymous-scope> ::= ? <number>
796 // Even though the name is rendered in reverse order (e.g.
797 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
798 // innermost. So a method bar in class C local to function foo gets mangled
799 // as something like:
800 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
801 // This is more apparent when you have a type nested inside a method of a
802 // type nested inside a function. A method baz in class D local to method
803 // bar of class C local to function foo gets mangled as:
804 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
805 // This scheme is general enough to support GCC-style nested
806 // functions. You could have a method baz of class C inside a function bar
807 // inside a function foo, like so:
808 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
809 int NestLevel = getLocalNestingLevel(FD);
810 Out << '?';
811 mangleNumber(NestLevel);
812 Out << '?';
813 mangle(FD, "?");
814}
815
816void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
817 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000818 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000819 // <template-name> ::= <unscoped-template-name> <template-args>
820 // ::= <substitution>
821 // Always start with the unqualified name.
822
823 // Templates have their own context for back references.
824 ArgBackRefMap OuterArgsContext;
825 BackRefMap OuterTemplateContext;
826 NameBackReferences.swap(OuterTemplateContext);
827 TypeBackReferences.swap(OuterArgsContext);
828
829 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000830 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000831
832 // Restore the previous back reference contexts.
833 NameBackReferences.swap(OuterTemplateContext);
834 TypeBackReferences.swap(OuterArgsContext);
835}
836
837void
838MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
839 // <unscoped-template-name> ::= ?$ <unqualified-name>
840 Out << "?$";
841 mangleUnqualifiedName(TD);
842}
843
844void
845MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
846 bool IsBoolean) {
847 // <integer-literal> ::= $0 <number>
848 Out << "$0";
849 // Make sure booleans are encoded as 0/1.
850 if (IsBoolean && Value.getBoolValue())
851 mangleNumber(1);
852 else
853 mangleNumber(Value);
854}
855
856void
857MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
858 // See if this is a constant expression.
859 llvm::APSInt Value;
860 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
861 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
862 return;
863 }
864
David Majnemerc80eb462013-08-13 06:32:20 +0000865 const CXXUuidofExpr *UE = 0;
866 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
867 if (UO->getOpcode() == UO_AddrOf)
868 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
869 } else
870 UE = dyn_cast<CXXUuidofExpr>(E);
871
872 if (UE) {
873 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
874 // const __s_GUID _GUID_{lower case UUID with underscores}
875 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
876 std::string Name = "_GUID_" + Uuid.lower();
877 std::replace(Name.begin(), Name.end(), '-', '_');
878
David Majnemer26314e12013-08-13 09:17:25 +0000879 // If we had to peek through an address-of operator, treat this like we are
David Majnemerc80eb462013-08-13 06:32:20 +0000880 // dealing with a pointer type. Otherwise, treat it like a const reference.
881 //
882 // N.B. This matches up with the handling of TemplateArgument::Declaration
883 // in mangleTemplateArg
884 if (UE == E)
885 Out << "$E?";
886 else
887 Out << "$1?";
888 Out << Name << "@@3U__s_GUID@@B";
889 return;
890 }
891
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000892 // As bad as this diagnostic is, it's better than crashing.
893 DiagnosticsEngine &Diags = Context.getDiags();
894 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
895 "cannot yet mangle expression type %0");
896 Diags.Report(E->getExprLoc(), DiagID)
897 << E->getStmtClassName() << E->getSourceRange();
898}
899
900void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000901MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
902 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000903 // <template-args> ::= {<type> | <integer-literal>}+ @
904 unsigned NumTemplateArgs = TemplateArgs.size();
905 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000906 const TemplateArgument &TA = TemplateArgs[i];
David Majnemer309f6452013-08-27 08:21:25 +0000907 mangleTemplateArg(TD, TA);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000908 }
909 Out << '@';
910}
911
Reid Kleckner5d90d182013-07-02 18:10:07 +0000912void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
David Majnemer309f6452013-08-27 08:21:25 +0000913 const TemplateArgument &TA) {
Reid Kleckner5d90d182013-07-02 18:10:07 +0000914 switch (TA.getKind()) {
915 case TemplateArgument::Null:
916 llvm_unreachable("Can't mangle null template arguments!");
David Majnemer309f6452013-08-27 08:21:25 +0000917 case TemplateArgument::TemplateExpansion:
918 llvm_unreachable("Can't mangle template expansion arguments!");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000919 case TemplateArgument::Type: {
920 QualType T = TA.getAsType();
921 mangleType(T, SourceRange(), QMM_Escape);
922 break;
923 }
David Majnemerf2081f62013-08-13 01:25:35 +0000924 case TemplateArgument::Declaration: {
925 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl());
926 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000927 break;
David Majnemerf2081f62013-08-13 01:25:35 +0000928 }
Reid Kleckner5d90d182013-07-02 18:10:07 +0000929 case TemplateArgument::Integral:
930 mangleIntegerLiteral(TA.getAsIntegral(),
931 TA.getIntegralType()->isBooleanType());
932 break;
David Majnemer7802fc92013-08-05 21:33:59 +0000933 case TemplateArgument::NullPtr:
934 Out << "$0A@";
935 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000936 case TemplateArgument::Expression:
937 mangleExpression(TA.getAsExpr());
938 break;
939 case TemplateArgument::Pack:
940 // Unlike Itanium, there is no character code to indicate an argument pack.
Reid Kleckner5d90d182013-07-02 18:10:07 +0000941 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
942 I != E; ++I)
David Majnemer309f6452013-08-27 08:21:25 +0000943 mangleTemplateArg(TD, *I);
Reid Kleckner5d90d182013-07-02 18:10:07 +0000944 break;
945 case TemplateArgument::Template:
David Majnemer02c44f02013-08-05 22:26:46 +0000946 mangleType(cast<TagDecl>(
947 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
948 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000949 }
950}
951
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000952void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
953 bool IsMember) {
954 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
955 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
956 // 'I' means __restrict (32/64-bit).
957 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
958 // keyword!
959 // <base-cvr-qualifiers> ::= A # near
960 // ::= B # near const
961 // ::= C # near volatile
962 // ::= D # near const volatile
963 // ::= E # far (16-bit)
964 // ::= F # far const (16-bit)
965 // ::= G # far volatile (16-bit)
966 // ::= H # far const volatile (16-bit)
967 // ::= I # huge (16-bit)
968 // ::= J # huge const (16-bit)
969 // ::= K # huge volatile (16-bit)
970 // ::= L # huge const volatile (16-bit)
971 // ::= M <basis> # based
972 // ::= N <basis> # based const
973 // ::= O <basis> # based volatile
974 // ::= P <basis> # based const volatile
975 // ::= Q # near member
976 // ::= R # near const member
977 // ::= S # near volatile member
978 // ::= T # near const volatile member
979 // ::= U # far member (16-bit)
980 // ::= V # far const member (16-bit)
981 // ::= W # far volatile member (16-bit)
982 // ::= X # far const volatile member (16-bit)
983 // ::= Y # huge member (16-bit)
984 // ::= Z # huge const member (16-bit)
985 // ::= 0 # huge volatile member (16-bit)
986 // ::= 1 # huge const volatile member (16-bit)
987 // ::= 2 <basis> # based member
988 // ::= 3 <basis> # based const member
989 // ::= 4 <basis> # based volatile member
990 // ::= 5 <basis> # based const volatile member
991 // ::= 6 # near function (pointers only)
992 // ::= 7 # far function (pointers only)
993 // ::= 8 # near method (pointers only)
994 // ::= 9 # far method (pointers only)
995 // ::= _A <basis> # based function (pointers only)
996 // ::= _B <basis> # based function (far?) (pointers only)
997 // ::= _C <basis> # based method (pointers only)
998 // ::= _D <basis> # based method (far?) (pointers only)
999 // ::= _E # block (Clang)
1000 // <basis> ::= 0 # __based(void)
1001 // ::= 1 # __based(segment)?
1002 // ::= 2 <name> # __based(name)
1003 // ::= 3 # ?
1004 // ::= 4 # ?
1005 // ::= 5 # not really based
1006 bool HasConst = Quals.hasConst(),
1007 HasVolatile = Quals.hasVolatile();
David Majnemerc0e64f32013-08-05 22:43:06 +00001008
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001009 if (!IsMember) {
1010 if (HasConst && HasVolatile) {
1011 Out << 'D';
1012 } else if (HasVolatile) {
1013 Out << 'C';
1014 } else if (HasConst) {
1015 Out << 'B';
1016 } else {
1017 Out << 'A';
1018 }
1019 } else {
1020 if (HasConst && HasVolatile) {
1021 Out << 'T';
1022 } else if (HasVolatile) {
1023 Out << 'S';
1024 } else if (HasConst) {
1025 Out << 'R';
1026 } else {
1027 Out << 'Q';
1028 }
1029 }
1030
1031 // FIXME: For now, just drop all extension qualifiers on the floor.
1032}
1033
1034void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1035 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1036 // ::= Q # const
1037 // ::= R # volatile
1038 // ::= S # const volatile
1039 bool HasConst = Quals.hasConst(),
1040 HasVolatile = Quals.hasVolatile();
1041 if (HasConst && HasVolatile) {
1042 Out << 'S';
1043 } else if (HasVolatile) {
1044 Out << 'R';
1045 } else if (HasConst) {
1046 Out << 'Q';
1047 } else {
1048 Out << 'P';
1049 }
1050}
1051
1052void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1053 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001054 // MSVC will backreference two canonically equivalent types that have slightly
1055 // different manglings when mangled alone.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001056 void *TypePtr = getASTContext().getCanonicalType(T).getAsOpaquePtr();
1057 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1058
1059 if (Found == TypeBackReferences.end()) {
1060 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1061
Reid Klecknerf21818d2013-06-24 19:21:52 +00001062 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1063 QualType OT = DT->getOriginalType();
1064 if (const ArrayType *AT = getASTContext().getAsArrayType(OT)) {
1065 mangleDecayedArrayType(AT, false);
1066 } else if (const FunctionType *FT = OT->getAs<FunctionType>()) {
1067 Out << "P6";
1068 mangleFunctionType(FT, 0, false, false);
1069 } else {
1070 llvm_unreachable("unexpected decayed type");
1071 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001072 } else {
1073 mangleType(T, Range, QMM_Drop);
1074 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001075
1076 // See if it's worth creating a back reference.
1077 // Only types longer than 1 character are considered
1078 // and only 10 back references slots are available:
1079 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1080 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1081 size_t Size = TypeBackReferences.size();
1082 TypeBackReferences[TypePtr] = Size;
1083 }
1084 } else {
1085 Out << Found->second;
1086 }
1087}
1088
1089void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001090 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001091 // Don't use the canonical types. MSVC includes things like 'const' on
1092 // pointer arguments to function pointers that canonicalization strips away.
1093 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001094 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001095 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1096 // If there were any Quals, getAsArrayType() pushed them onto the array
1097 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001098 if (QMM == QMM_Mangle)
1099 Out << 'A';
1100 else if (QMM == QMM_Escape || QMM == QMM_Result)
1101 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001102 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001103 return;
1104 }
1105
1106 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1107 T->isBlockPointerType();
1108
1109 switch (QMM) {
1110 case QMM_Drop:
1111 break;
1112 case QMM_Mangle:
1113 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1114 Out << '6';
1115 mangleFunctionType(FT, 0, false, false);
1116 return;
1117 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001118 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001119 break;
1120 case QMM_Escape:
1121 if (!IsPointer && Quals) {
1122 Out << "$$C";
1123 mangleQualifiers(Quals, false);
1124 }
1125 break;
1126 case QMM_Result:
1127 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1128 Out << '?';
1129 mangleQualifiers(Quals, false);
1130 }
1131 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001132 }
1133
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001134 // We have to mangle these now, while we still have enough information.
1135 if (IsPointer)
1136 manglePointerQualifiers(Quals);
1137 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001138
1139 switch (ty->getTypeClass()) {
1140#define ABSTRACT_TYPE(CLASS, PARENT)
1141#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1142 case Type::CLASS: \
1143 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1144 return;
1145#define TYPE(CLASS, PARENT) \
1146 case Type::CLASS: \
1147 mangleType(cast<CLASS##Type>(ty), Range); \
1148 break;
1149#include "clang/AST/TypeNodes.def"
1150#undef ABSTRACT_TYPE
1151#undef NON_CANONICAL_TYPE
1152#undef TYPE
1153 }
1154}
1155
1156void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1157 SourceRange Range) {
1158 // <type> ::= <builtin-type>
1159 // <builtin-type> ::= X # void
1160 // ::= C # signed char
1161 // ::= D # char
1162 // ::= E # unsigned char
1163 // ::= F # short
1164 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1165 // ::= H # int
1166 // ::= I # unsigned int
1167 // ::= J # long
1168 // ::= K # unsigned long
1169 // L # <none>
1170 // ::= M # float
1171 // ::= N # double
1172 // ::= O # long double (__float80 is mangled differently)
1173 // ::= _J # long long, __int64
1174 // ::= _K # unsigned long long, __int64
1175 // ::= _L # __int128
1176 // ::= _M # unsigned __int128
1177 // ::= _N # bool
1178 // _O # <array in parameter>
1179 // ::= _T # __float80 (Intel)
1180 // ::= _W # wchar_t
1181 // ::= _Z # __float80 (Digital Mars)
1182 switch (T->getKind()) {
1183 case BuiltinType::Void: Out << 'X'; break;
1184 case BuiltinType::SChar: Out << 'C'; break;
1185 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1186 case BuiltinType::UChar: Out << 'E'; break;
1187 case BuiltinType::Short: Out << 'F'; break;
1188 case BuiltinType::UShort: Out << 'G'; break;
1189 case BuiltinType::Int: Out << 'H'; break;
1190 case BuiltinType::UInt: Out << 'I'; break;
1191 case BuiltinType::Long: Out << 'J'; break;
1192 case BuiltinType::ULong: Out << 'K'; break;
1193 case BuiltinType::Float: Out << 'M'; break;
1194 case BuiltinType::Double: Out << 'N'; break;
1195 // TODO: Determine size and mangle accordingly
1196 case BuiltinType::LongDouble: Out << 'O'; break;
1197 case BuiltinType::LongLong: Out << "_J"; break;
1198 case BuiltinType::ULongLong: Out << "_K"; break;
1199 case BuiltinType::Int128: Out << "_L"; break;
1200 case BuiltinType::UInt128: Out << "_M"; break;
1201 case BuiltinType::Bool: Out << "_N"; break;
1202 case BuiltinType::WChar_S:
1203 case BuiltinType::WChar_U: Out << "_W"; break;
1204
1205#define BUILTIN_TYPE(Id, SingletonId)
1206#define PLACEHOLDER_TYPE(Id, SingletonId) \
1207 case BuiltinType::Id:
1208#include "clang/AST/BuiltinTypes.def"
1209 case BuiltinType::Dependent:
1210 llvm_unreachable("placeholder types shouldn't get to name mangling");
1211
1212 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1213 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1214 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001215
1216 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1217 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1218 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1219 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1220 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1221 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001222 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001223 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001224
1225 case BuiltinType::NullPtr: Out << "$$T"; break;
1226
1227 case BuiltinType::Char16:
1228 case BuiltinType::Char32:
1229 case BuiltinType::Half: {
1230 DiagnosticsEngine &Diags = Context.getDiags();
1231 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1232 "cannot mangle this built-in %0 type yet");
1233 Diags.Report(Range.getBegin(), DiagID)
1234 << T->getName(Context.getASTContext().getPrintingPolicy())
1235 << Range;
1236 break;
1237 }
1238 }
1239}
1240
1241// <type> ::= <function-type>
1242void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1243 SourceRange) {
1244 // Structors only appear in decls, so at this point we know it's not a
1245 // structor type.
1246 // FIXME: This may not be lambda-friendly.
1247 Out << "$$A6";
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001248 mangleFunctionType(T, NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001249}
1250void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1251 SourceRange) {
1252 llvm_unreachable("Can't mangle K&R function prototypes");
1253}
1254
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001255void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1256 const FunctionDecl *D,
1257 bool IsStructor,
1258 bool IsInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001259 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1260 // <return-type> <argument-list> <throw-spec>
1261 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1262
Reid Klecknerf21818d2013-06-24 19:21:52 +00001263 SourceRange Range;
1264 if (D) Range = D->getSourceRange();
1265
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001266 // If this is a C++ instance method, mangle the CVR qualifiers for the
1267 // this pointer.
David Majnemer1c7a4092013-08-15 08:13:23 +00001268 if (IsInstMethod) {
1269 if (PointersAre64Bit)
1270 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001271 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
David Majnemer1c7a4092013-08-15 08:13:23 +00001272 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001273
1274 mangleCallingConvention(T, IsInstMethod);
1275
1276 // <return-type> ::= <type>
1277 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001278 if (IsStructor) {
1279 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1280 StructorType == Dtor_Deleting) {
1281 // The scalar deleting destructor takes an extra int argument.
1282 // However, the FunctionType generated has 0 arguments.
1283 // FIXME: This is a temporary hack.
1284 // Maybe should fix the FunctionType creation instead?
Timur Iskhodzhanov4b104062013-08-26 10:32:04 +00001285 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001286 return;
1287 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001288 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001289 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001290 QualType ResultType = Proto->getResultType();
1291 if (ResultType->isVoidType())
1292 ResultType = ResultType.getUnqualifiedType();
1293 mangleType(ResultType, Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001294 }
1295
1296 // <argument-list> ::= X # void
1297 // ::= <type>+ @
1298 // ::= <type>* Z # varargs
1299 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1300 Out << 'X';
1301 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001302 // Happens for function pointer type arguments for example.
1303 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1304 ArgEnd = Proto->arg_type_end();
1305 Arg != ArgEnd; ++Arg)
1306 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001307 // <builtin-type> ::= Z # ellipsis
1308 if (Proto->isVariadic())
1309 Out << 'Z';
1310 else
1311 Out << '@';
1312 }
1313
1314 mangleThrowSpecification(Proto);
1315}
1316
1317void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001318 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1319 // # pointer. in 64-bit mode *all*
1320 // # 'this' pointers are 64-bit.
1321 // ::= <global-function>
1322 // <member-function> ::= A # private: near
1323 // ::= B # private: far
1324 // ::= C # private: static near
1325 // ::= D # private: static far
1326 // ::= E # private: virtual near
1327 // ::= F # private: virtual far
1328 // ::= G # private: thunk near
1329 // ::= H # private: thunk far
1330 // ::= I # protected: near
1331 // ::= J # protected: far
1332 // ::= K # protected: static near
1333 // ::= L # protected: static far
1334 // ::= M # protected: virtual near
1335 // ::= N # protected: virtual far
1336 // ::= O # protected: thunk near
1337 // ::= P # protected: thunk far
1338 // ::= Q # public: near
1339 // ::= R # public: far
1340 // ::= S # public: static near
1341 // ::= T # public: static far
1342 // ::= U # public: virtual near
1343 // ::= V # public: virtual far
1344 // ::= W # public: thunk near
1345 // ::= X # public: thunk far
1346 // <global-function> ::= Y # global near
1347 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001348 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1349 switch (MD->getAccess()) {
1350 default:
1351 case AS_private:
1352 if (MD->isStatic())
1353 Out << 'C';
1354 else if (MD->isVirtual())
1355 Out << 'E';
1356 else
1357 Out << 'A';
1358 break;
1359 case AS_protected:
1360 if (MD->isStatic())
1361 Out << 'K';
1362 else if (MD->isVirtual())
1363 Out << 'M';
1364 else
1365 Out << 'I';
1366 break;
1367 case AS_public:
1368 if (MD->isStatic())
1369 Out << 'S';
1370 else if (MD->isVirtual())
1371 Out << 'U';
1372 else
1373 Out << 'Q';
1374 }
1375 } else
1376 Out << 'Y';
1377}
1378void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
1379 bool IsInstMethod) {
1380 // <calling-convention> ::= A # __cdecl
1381 // ::= B # __export __cdecl
1382 // ::= C # __pascal
1383 // ::= D # __export __pascal
1384 // ::= E # __thiscall
1385 // ::= F # __export __thiscall
1386 // ::= G # __stdcall
1387 // ::= H # __export __stdcall
1388 // ::= I # __fastcall
1389 // ::= J # __export __fastcall
1390 // The 'export' calling conventions are from a bygone era
1391 // (*cough*Win16*cough*) when functions were declared for export with
1392 // that keyword. (It didn't actually export them, it just made them so
1393 // that they could be in a DLL and somebody from another module could call
1394 // them.)
1395 CallingConv CC = T->getCallConv();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001396 switch (CC) {
1397 default:
1398 llvm_unreachable("Unsupported CC for mangling");
Charles Davise8519c32013-08-30 04:39:01 +00001399 case CC_X86_64Win64:
1400 case CC_X86_64SysV:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001401 case CC_C: Out << 'A'; break;
1402 case CC_X86Pascal: Out << 'C'; break;
1403 case CC_X86ThisCall: Out << 'E'; break;
1404 case CC_X86StdCall: Out << 'G'; break;
1405 case CC_X86FastCall: Out << 'I'; break;
1406 }
1407}
1408void MicrosoftCXXNameMangler::mangleThrowSpecification(
1409 const FunctionProtoType *FT) {
1410 // <throw-spec> ::= Z # throw(...) (default)
1411 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1412 // ::= <type>+
1413 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1414 // all actually mangled as 'Z'. (They're ignored because their associated
1415 // functionality isn't implemented, and probably never will be.)
1416 Out << 'Z';
1417}
1418
1419void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1420 SourceRange Range) {
1421 // Probably should be mangled as a template instantiation; need to see what
1422 // VC does first.
1423 DiagnosticsEngine &Diags = Context.getDiags();
1424 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1425 "cannot mangle this unresolved dependent type yet");
1426 Diags.Report(Range.getBegin(), DiagID)
1427 << Range;
1428}
1429
1430// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1431// <union-type> ::= T <name>
1432// <struct-type> ::= U <name>
1433// <class-type> ::= V <name>
1434// <enum-type> ::= W <size> <name>
1435void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001436 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001437}
1438void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001439 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001440}
David Majnemer02c44f02013-08-05 22:26:46 +00001441void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1442 switch (TD->getTagKind()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001443 case TTK_Union:
1444 Out << 'T';
1445 break;
1446 case TTK_Struct:
1447 case TTK_Interface:
1448 Out << 'U';
1449 break;
1450 case TTK_Class:
1451 Out << 'V';
1452 break;
1453 case TTK_Enum:
1454 Out << 'W';
1455 Out << getASTContext().getTypeSizeInChars(
David Majnemer02c44f02013-08-05 22:26:46 +00001456 cast<EnumDecl>(TD)->getIntegerType()).getQuantity();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001457 break;
1458 }
David Majnemer02c44f02013-08-05 22:26:46 +00001459 mangleName(TD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001460}
1461
1462// <type> ::= <array-type>
1463// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1464// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001465// <element-type> # as global, E is never required
1466// ::= Q E? <cvr-qualifiers> [Y <dimension-count> <dimension>+]
1467// <element-type> # as param, E is required for 64-bit
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001468// It's supposed to be the other way around, but for some strange reason, it
1469// isn't. Today this behavior is retained for the sole purpose of backwards
1470// compatibility.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001471void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T,
1472 bool IsGlobal) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001473 // This isn't a recursive mangling, so now we have to do it all in this
1474 // one call.
1475 if (IsGlobal) {
1476 manglePointerQualifiers(T->getElementType().getQualifiers());
1477 } else {
1478 Out << 'Q';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001479 if (PointersAre64Bit)
1480 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001481 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001482 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001483}
1484void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1485 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001486 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001487}
1488void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1489 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001490 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001491}
1492void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1493 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001494 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001495}
1496void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1497 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001498 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001499}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001500void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001501 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001502 SmallVector<llvm::APInt, 3> Dimensions;
1503 for (;;) {
1504 if (const ConstantArrayType *CAT =
1505 getASTContext().getAsConstantArrayType(ElementTy)) {
1506 Dimensions.push_back(CAT->getSize());
1507 ElementTy = CAT->getElementType();
1508 } else if (ElementTy->isVariableArrayType()) {
1509 const VariableArrayType *VAT =
1510 getASTContext().getAsVariableArrayType(ElementTy);
1511 DiagnosticsEngine &Diags = Context.getDiags();
1512 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1513 "cannot mangle this variable-length array yet");
1514 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1515 << VAT->getBracketsRange();
1516 return;
1517 } else if (ElementTy->isDependentSizedArrayType()) {
1518 // The dependent expression has to be folded into a constant (TODO).
1519 const DependentSizedArrayType *DSAT =
1520 getASTContext().getAsDependentSizedArrayType(ElementTy);
1521 DiagnosticsEngine &Diags = Context.getDiags();
1522 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1523 "cannot mangle this dependent-length array yet");
1524 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1525 << DSAT->getBracketsRange();
1526 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001527 } else if (const IncompleteArrayType *IAT =
1528 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1529 Dimensions.push_back(llvm::APInt(32, 0));
1530 ElementTy = IAT->getElementType();
1531 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001532 else break;
1533 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001534 Out << 'Y';
1535 // <dimension-count> ::= <number> # number of extra dimensions
1536 mangleNumber(Dimensions.size());
1537 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1538 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001539 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001540}
1541
1542// <type> ::= <pointer-to-member-type>
1543// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1544// <class name> <type>
1545void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1546 SourceRange Range) {
1547 QualType PointeeType = T->getPointeeType();
1548 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1549 Out << '8';
1550 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001551 mangleFunctionType(FPT, NULL, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001552 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001553 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1554 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001555 mangleQualifiers(PointeeType.getQualifiers(), true);
1556 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001557 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001558 }
1559}
1560
1561void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1562 SourceRange Range) {
1563 DiagnosticsEngine &Diags = Context.getDiags();
1564 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1565 "cannot mangle this template type parameter type yet");
1566 Diags.Report(Range.getBegin(), DiagID)
1567 << Range;
1568}
1569
1570void MicrosoftCXXNameMangler::mangleType(
1571 const SubstTemplateTypeParmPackType *T,
1572 SourceRange Range) {
1573 DiagnosticsEngine &Diags = Context.getDiags();
1574 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1575 "cannot mangle this substituted parameter pack yet");
1576 Diags.Report(Range.getBegin(), DiagID)
1577 << Range;
1578}
1579
1580// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001581// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1582// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001583void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1584 SourceRange Range) {
1585 QualType PointeeTy = T->getPointeeType();
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(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001589}
1590void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1591 SourceRange Range) {
1592 // Object pointers never have qualifiers.
1593 Out << 'A';
David Majnemer1c7a4092013-08-15 08:13:23 +00001594 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1595 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001596 mangleType(T->getPointeeType(), Range);
1597}
1598
1599// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001600// <reference-type> ::= A E? <cvr-qualifiers> <type>
1601// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001602void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1603 SourceRange Range) {
1604 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001605 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1606 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001607 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001608}
1609
1610// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001611// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1612// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001613void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1614 SourceRange Range) {
1615 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001616 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1617 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001618 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001619}
1620
1621void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1622 SourceRange Range) {
1623 DiagnosticsEngine &Diags = Context.getDiags();
1624 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1625 "cannot mangle this complex number type yet");
1626 Diags.Report(Range.getBegin(), DiagID)
1627 << Range;
1628}
1629
1630void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1631 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001632 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1633 assert(ET && "vectors with non-builtin elements are unsupported");
1634 uint64_t Width = getASTContext().getTypeSize(T);
1635 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1636 // doesn't match the Intel types uses a custom mangling below.
1637 bool IntelVector = true;
1638 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1639 Out << "T__m64";
1640 } else if (Width == 128 || Width == 256) {
1641 if (ET->getKind() == BuiltinType::Float)
1642 Out << "T__m" << Width;
1643 else if (ET->getKind() == BuiltinType::LongLong)
1644 Out << "T__m" << Width << 'i';
1645 else if (ET->getKind() == BuiltinType::Double)
1646 Out << "U__m" << Width << 'd';
1647 else
1648 IntelVector = false;
1649 } else {
1650 IntelVector = false;
1651 }
1652
1653 if (!IntelVector) {
1654 // The MS ABI doesn't have a special mangling for vector types, so we define
1655 // our own mangling to handle uses of __vector_size__ on user-specified
1656 // types, and for extensions like __v4sf.
1657 Out << "T__clang_vec" << T->getNumElements() << '_';
1658 mangleType(ET, Range);
1659 }
1660
1661 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001662}
Reid Kleckner1232e272013-03-26 16:56:59 +00001663
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001664void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1665 SourceRange Range) {
1666 DiagnosticsEngine &Diags = Context.getDiags();
1667 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1668 "cannot mangle this extended vector type yet");
1669 Diags.Report(Range.getBegin(), DiagID)
1670 << Range;
1671}
1672void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1673 SourceRange Range) {
1674 DiagnosticsEngine &Diags = Context.getDiags();
1675 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1676 "cannot mangle this dependent-sized extended vector type yet");
1677 Diags.Report(Range.getBegin(), DiagID)
1678 << Range;
1679}
1680
1681void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1682 SourceRange) {
1683 // ObjC interfaces have structs underlying them.
1684 Out << 'U';
1685 mangleName(T->getDecl());
1686}
1687
1688void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1689 SourceRange Range) {
1690 // We don't allow overloading by different protocol qualification,
1691 // so mangling them isn't necessary.
1692 mangleType(T->getBaseType(), Range);
1693}
1694
1695void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1696 SourceRange Range) {
1697 Out << "_E";
1698
1699 QualType pointee = T->getPointeeType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001700 mangleFunctionType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001701}
1702
David Majnemer360d23e2013-08-16 08:29:13 +00001703void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1704 SourceRange) {
1705 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001706}
1707
1708void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1709 SourceRange Range) {
1710 DiagnosticsEngine &Diags = Context.getDiags();
1711 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1712 "cannot mangle this template specialization type yet");
1713 Diags.Report(Range.getBegin(), DiagID)
1714 << Range;
1715}
1716
1717void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1718 SourceRange Range) {
1719 DiagnosticsEngine &Diags = Context.getDiags();
1720 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1721 "cannot mangle this dependent name type yet");
1722 Diags.Report(Range.getBegin(), DiagID)
1723 << Range;
1724}
1725
1726void MicrosoftCXXNameMangler::mangleType(
1727 const DependentTemplateSpecializationType *T,
1728 SourceRange Range) {
1729 DiagnosticsEngine &Diags = Context.getDiags();
1730 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1731 "cannot mangle this dependent template specialization type yet");
1732 Diags.Report(Range.getBegin(), DiagID)
1733 << Range;
1734}
1735
1736void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1737 SourceRange Range) {
1738 DiagnosticsEngine &Diags = Context.getDiags();
1739 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1740 "cannot mangle this pack expansion yet");
1741 Diags.Report(Range.getBegin(), DiagID)
1742 << Range;
1743}
1744
1745void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1746 SourceRange Range) {
1747 DiagnosticsEngine &Diags = Context.getDiags();
1748 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1749 "cannot mangle this typeof(type) yet");
1750 Diags.Report(Range.getBegin(), DiagID)
1751 << Range;
1752}
1753
1754void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1755 SourceRange Range) {
1756 DiagnosticsEngine &Diags = Context.getDiags();
1757 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1758 "cannot mangle this typeof(expression) yet");
1759 Diags.Report(Range.getBegin(), DiagID)
1760 << Range;
1761}
1762
1763void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1764 SourceRange Range) {
1765 DiagnosticsEngine &Diags = Context.getDiags();
1766 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1767 "cannot mangle this decltype() yet");
1768 Diags.Report(Range.getBegin(), DiagID)
1769 << Range;
1770}
1771
1772void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1773 SourceRange Range) {
1774 DiagnosticsEngine &Diags = Context.getDiags();
1775 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1776 "cannot mangle this unary transform type yet");
1777 Diags.Report(Range.getBegin(), DiagID)
1778 << Range;
1779}
1780
1781void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1782 DiagnosticsEngine &Diags = Context.getDiags();
1783 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1784 "cannot mangle this 'auto' type yet");
1785 Diags.Report(Range.getBegin(), DiagID)
1786 << Range;
1787}
1788
1789void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1790 SourceRange Range) {
1791 DiagnosticsEngine &Diags = Context.getDiags();
1792 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1793 "cannot mangle this C11 atomic type yet");
1794 Diags.Report(Range.getBegin(), DiagID)
1795 << Range;
1796}
1797
1798void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1799 raw_ostream &Out) {
1800 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1801 "Invalid mangleName() call, argument is not a variable or function!");
1802 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1803 "Invalid mangleName() call on 'structor decl!");
1804
1805 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1806 getASTContext().getSourceManager(),
1807 "Mangling declaration");
1808
1809 MicrosoftCXXNameMangler Mangler(*this, Out);
1810 return Mangler.mangle(D);
1811}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001812
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001813void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1814 const ThunkInfo &Thunk,
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001815 raw_ostream &Out) {
1816 // FIXME: this is not yet a complete implementation, but merely a
1817 // reasonably-working stub to avoid crashing when required to emit a thunk.
1818 MicrosoftCXXNameMangler Mangler(*this, Out);
1819 Out << "\01?";
1820 Mangler.mangleName(MD);
1821 if (Thunk.This.NonVirtual != 0) {
1822 // FIXME: add support for protected/private or use mangleFunctionClass.
1823 Out << "W";
1824 llvm::APSInt APSNumber(/*BitWidth=*/32 /*FIXME: check on x64*/,
1825 /*isUnsigned=*/true);
1826 APSNumber = -Thunk.This.NonVirtual;
1827 Mangler.mangleNumber(APSNumber);
1828 } else {
1829 // FIXME: add support for protected/private or use mangleFunctionClass.
1830 Out << "Q";
1831 }
1832 // FIXME: mangle return adjustment? Most likely includes using an overridee FPT?
1833 Mangler.mangleFunctionType(MD->getType()->castAs<FunctionProtoType>(), MD, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001834}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001835
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001836void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1837 CXXDtorType Type,
1838 const ThisAdjustment &,
1839 raw_ostream &) {
1840 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1841 "cannot mangle thunk for this destructor yet");
1842 getDiags().Report(DD->getLocation(), DiagID);
1843}
Reid Kleckner90633022013-06-19 15:20:38 +00001844
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001845void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1846 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001847 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1848 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001849 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001850 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001851 MicrosoftCXXNameMangler Mangler(*this, Out);
1852 Mangler.getStream() << "\01??_7";
1853 Mangler.mangleName(RD);
Reid Kleckner90633022013-06-19 15:20:38 +00001854 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001855 // TODO: If the class has more than one vtable, mangle in the class it came
1856 // from.
1857 Mangler.getStream() << '@';
1858}
Reid Kleckner90633022013-06-19 15:20:38 +00001859
1860void MicrosoftMangleContext::mangleCXXVBTable(
1861 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1862 raw_ostream &Out) {
1863 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1864 // <cvr-qualifiers> [<name>] @
1865 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1866 // is always '7' for vbtables.
1867 MicrosoftCXXNameMangler Mangler(*this, Out);
1868 Mangler.getStream() << "\01??_8";
1869 Mangler.mangleName(Derived);
1870 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1871 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1872 E = BasePath.end();
1873 I != E; ++I) {
1874 Mangler.mangleName(*I);
1875 }
1876 Mangler.getStream() << '@';
1877}
1878
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001879void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1880 raw_ostream &) {
1881 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1882}
1883void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1884 int64_t Offset,
1885 const CXXRecordDecl *Type,
1886 raw_ostream &) {
1887 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1888}
1889void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1890 raw_ostream &) {
1891 // FIXME: Give a location...
1892 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1893 "cannot mangle RTTI descriptors for type %0 yet");
1894 getDiags().Report(DiagID)
1895 << T.getBaseTypeIdentifier();
1896}
1897void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1898 raw_ostream &) {
1899 // FIXME: Give a location...
1900 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1901 "cannot mangle the name of type %0 into RTTI descriptors yet");
1902 getDiags().Report(DiagID)
1903 << T.getBaseTypeIdentifier();
1904}
1905void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1906 CXXCtorType Type,
1907 raw_ostream & Out) {
1908 MicrosoftCXXNameMangler mangler(*this, Out);
1909 mangler.mangle(D);
1910}
1911void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1912 CXXDtorType Type,
1913 raw_ostream & Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001914 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001915 mangler.mangle(D);
1916}
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001917void MicrosoftMangleContext::mangleReferenceTemporary(const VarDecl *VD,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001918 raw_ostream &) {
1919 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1920 "cannot mangle this reference temporary yet");
1921 getDiags().Report(VD->getLocation(), DiagID);
1922}
1923
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001924void MicrosoftMangleContext::mangleStaticGuardVariable(const VarDecl *VD,
1925 raw_ostream &Out) {
1926 // <guard-name> ::= ?_B <postfix> @51
1927 // ::= ?$S <guard-num> @ <postfix> @4IA
1928
1929 // The first mangling is what MSVC uses to guard static locals in inline
1930 // functions. It uses a different mangling in external functions to support
1931 // guarding more than 32 variables. MSVC rejects inline functions with more
1932 // than 32 static locals. We don't fully implement the second mangling
1933 // because those guards are not externally visible, and instead use LLVM's
1934 // default renaming when creating a new guard variable.
1935 MicrosoftCXXNameMangler Mangler(*this, Out);
1936
1937 bool Visible = VD->isExternallyVisible();
1938 // <operator-name> ::= ?_B # local static guard
1939 Mangler.getStream() << (Visible ? "\01??_B" : "\01?$S1@");
1940 Mangler.manglePostfix(VD->getDeclContext());
1941 Mangler.getStream() << (Visible ? "@51" : "@4IA");
1942}
1943
1944void MicrosoftMangleContext::mangleDynamicAtExitDestructor(const VarDecl *D,
1945 raw_ostream &Out) {
1946 // <destructor-name> ::= ?__F <postfix> YAXXZ
1947 MicrosoftCXXNameMangler Mangler(*this, Out);
1948 Mangler.getStream() << "\01??__F";
1949 Mangler.mangleName(D);
1950 // This is the mangling of the function type of the stub, which is a global,
1951 // non-variadic, cdecl function that returns void and takes no args.
1952 Mangler.getStream() << "YAXXZ";
1953}
1954
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001955MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
1956 DiagnosticsEngine &Diags) {
1957 return new MicrosoftMangleContext(Context, Diags);
1958}