blob: 97edce105cd430d9729ecd646dacce6a66cf0f0c [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);
Guy Benyei7f92f2d2012-12-18 14:30:41 +000095
96private:
97 void disableBackReferences() { UseNameBackReferences = false; }
98 void mangleUnqualifiedName(const NamedDecl *ND) {
99 mangleUnqualifiedName(ND, ND->getDeclName());
100 }
101 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
102 void mangleSourceName(const IdentifierInfo *II);
103 void manglePostfix(const DeclContext *DC, bool NoFunction=false);
104 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 &);
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();
David Majnemer1c7a4092013-08-15 08:13:23 +0000320 if (Ty->isPointerType() || Ty->isReferenceType() ||
321 Ty->isMemberPointerType()) {
David Majnemer17ffbd02013-08-09 05:56:24 +0000322 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000323 if (PointersAre64Bit)
324 Out << 'E';
325 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) {
326 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true);
327 // Member pointers are suffixed with a back reference to the member
328 // pointer's class name.
329 mangleName(MPT->getClass()->getAsCXXRecordDecl());
330 } else
331 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
David Majnemer17ffbd02013-08-09 05:56:24 +0000332 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000333 // Global arrays are funny, too.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000334 mangleDecayedArrayType(AT, true);
335 if (AT->getElementType()->isArrayType())
336 Out << 'A';
337 else
338 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000339 } else {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000340 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000341 mangleQualifiers(Ty.getLocalQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000342 }
343}
344
345void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
346 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
347 const DeclContext *DC = ND->getDeclContext();
348
349 // Always start with the unqualified name.
350 mangleUnqualifiedName(ND);
351
352 // If this is an extern variable declared locally, the relevant DeclContext
353 // is that of the containing namespace, or the translation unit.
354 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
355 while (!DC->isNamespace() && !DC->isTranslationUnit())
356 DC = DC->getParent();
357
358 manglePostfix(DC);
359
360 // Terminate the whole name with an '@'.
361 Out << '@';
362}
363
364void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
365 llvm::APSInt APSNumber(/*BitWidth=*/64, /*isUnsigned=*/false);
366 APSNumber = Number;
367 mangleNumber(APSNumber);
368}
369
370void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
371 // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
372 // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
373 // ::= [?] @ # 0 (alternate mangling, not emitted by VC)
374 if (Value.isSigned() && Value.isNegative()) {
375 Out << '?';
376 mangleNumber(llvm::APSInt(Value.abs()));
377 return;
378 }
379 llvm::APSInt Temp(Value);
380 // There's a special shorter mangling for 0, but Microsoft
381 // chose not to use it. Instead, 0 gets mangled as "A@". Oh well...
382 if (Value.uge(1) && Value.ule(10)) {
383 --Temp;
384 Temp.print(Out, false);
385 } else {
386 // We have to build up the encoding in reverse order, so it will come
387 // out right when we write it out.
388 char Encoding[64];
389 char *EndPtr = Encoding+sizeof(Encoding);
390 char *CurPtr = EndPtr;
391 llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned());
392 NibbleMask = 0xf;
393 do {
394 *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf);
395 Temp = Temp.lshr(4);
396 } while (Temp != 0);
397 Out.write(CurPtr, EndPtr-CurPtr);
398 Out << '@';
399 }
400}
401
402static const TemplateDecl *
Reid Klecknerf16216c2013-03-20 01:40:23 +0000403isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000404 // Check if we have a function template.
405 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
406 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000407 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000408 return TD;
409 }
410 }
411
412 // Check if we have a class template.
413 if (const ClassTemplateSpecializationDecl *Spec =
Reid Klecknerf16216c2013-03-20 01:40:23 +0000414 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
415 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000416 return Spec->getSpecializedTemplate();
417 }
418
419 return 0;
420}
421
422void
423MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
424 DeclarationName Name) {
425 // <unqualified-name> ::= <operator-name>
426 // ::= <ctor-dtor-name>
427 // ::= <source-name>
428 // ::= <template-name>
Reid Klecknerf16216c2013-03-20 01:40:23 +0000429
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000430 // Check if we have a template.
Reid Klecknerf16216c2013-03-20 01:40:23 +0000431 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000432 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Kleckner3be37d12013-07-13 00:43:39 +0000433 // Function templates aren't considered for name back referencing. This
434 // makes sense since function templates aren't likely to occur multiple
435 // times in a symbol.
436 // FIXME: Test alias template mangling with MSVC 2013.
437 if (!isa<ClassTemplateDecl>(TD)) {
438 mangleTemplateInstantiationName(TD, *TemplateArgs);
439 return;
440 }
441
442 // We have a class template.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000443 // Here comes the tricky thing: if we need to mangle something like
444 // void foo(A::X<Y>, B::X<Y>),
445 // the X<Y> part is aliased. However, if you need to mangle
446 // void foo(A::X<A::Y>, A::X<B::Y>),
447 // the A::X<> part is not aliased.
448 // That said, from the mangler's perspective we have a structure like this:
449 // namespace[s] -> type[ -> template-parameters]
450 // but from the Clang perspective we have
451 // type [ -> template-parameters]
452 // \-> namespace[s]
453 // What we do is we create a new mangler, mangle the same type (without
454 // a namespace suffix) using the extra mangler with back references
455 // disabled (to avoid infinite recursion) and then use the mangled type
456 // name as a key to check the mangling of different types for aliasing.
457
458 std::string BackReferenceKey;
459 BackRefMap::iterator Found;
460 if (UseNameBackReferences) {
461 llvm::raw_string_ostream Stream(BackReferenceKey);
462 MicrosoftCXXNameMangler Extra(Context, Stream);
463 Extra.disableBackReferences();
464 Extra.mangleUnqualifiedName(ND, Name);
465 Stream.flush();
466
467 Found = NameBackReferences.find(BackReferenceKey);
468 }
469 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000470 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000471 if (UseNameBackReferences && NameBackReferences.size() < 10) {
472 size_t Size = NameBackReferences.size();
473 NameBackReferences[BackReferenceKey] = Size;
474 }
475 } else {
476 Out << Found->second;
477 }
478 return;
479 }
480
481 switch (Name.getNameKind()) {
482 case DeclarationName::Identifier: {
483 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
484 mangleSourceName(II);
485 break;
486 }
487
488 // Otherwise, an anonymous entity. We must have a declaration.
489 assert(ND && "mangling empty name without declaration");
490
491 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
492 if (NS->isAnonymousNamespace()) {
493 Out << "?A@";
494 break;
495 }
496 }
497
498 // We must have an anonymous struct.
499 const TagDecl *TD = cast<TagDecl>(ND);
500 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
501 assert(TD->getDeclContext() == D->getDeclContext() &&
502 "Typedef should not be in another decl context!");
503 assert(D->getDeclName().getAsIdentifierInfo() &&
504 "Typedef was not named!");
505 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
506 break;
507 }
508
509 // When VC encounters an anonymous type with no tag and no typedef,
David Majnemerec0258a2013-08-26 02:35:51 +0000510 // it literally emits '<unnamed-tag>@'.
511 Out << "<unnamed-tag>@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000512 break;
513 }
514
515 case DeclarationName::ObjCZeroArgSelector:
516 case DeclarationName::ObjCOneArgSelector:
517 case DeclarationName::ObjCMultiArgSelector:
518 llvm_unreachable("Can't mangle Objective-C selector names here!");
519
520 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000521 if (ND == Structor) {
522 assert(StructorType == Ctor_Complete &&
523 "Should never be asked to mangle a ctor other than complete");
524 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000525 Out << "?0";
526 break;
527
528 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000529 if (ND == Structor)
530 // If the named decl is the C++ destructor we're mangling,
531 // use the type we were given.
532 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
533 else
Reid Klecknera4130ba2013-07-22 13:51:44 +0000534 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000535 // class with a destructor is declared within a destructor.
Reid Klecknera4130ba2013-07-22 13:51:44 +0000536 mangleCXXDtorType(Dtor_Base);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000537 break;
538
539 case DeclarationName::CXXConversionFunctionName:
540 // <operator-name> ::= ?B # (cast)
541 // The target type is encoded as the return type.
542 Out << "?B";
543 break;
544
545 case DeclarationName::CXXOperatorName:
546 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
547 break;
548
549 case DeclarationName::CXXLiteralOperatorName: {
550 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
551 DiagnosticsEngine Diags = Context.getDiags();
552 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
553 "cannot mangle this literal operator yet");
554 Diags.Report(ND->getLocation(), DiagID);
555 break;
556 }
557
558 case DeclarationName::CXXUsingDirective:
559 llvm_unreachable("Can't mangle a using directive name!");
560 }
561}
562
563void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
564 bool NoFunction) {
565 // <postfix> ::= <unqualified-name> [<postfix>]
566 // ::= <substitution> [<postfix>]
567
568 if (!DC) return;
569
570 while (isa<LinkageSpecDecl>(DC))
571 DC = DC->getParent();
572
573 if (DC->isTranslationUnit())
574 return;
575
576 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedmane5798892013-07-10 01:13:27 +0000577 DiagnosticsEngine Diags = Context.getDiags();
578 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
579 "cannot mangle a local inside this block yet");
580 Diags.Report(BD->getLocation(), DiagID);
581
582 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
583 // for how this should be done.
584 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000585 Out << '@';
586 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000587 } else if (isa<CapturedDecl>(DC)) {
588 // Skip CapturedDecl context.
589 manglePostfix(DC->getParent(), NoFunction);
590 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000591 }
592
593 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
594 return;
595 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
596 mangleObjCMethodName(Method);
597 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
598 mangleLocalName(Func);
599 else {
600 mangleUnqualifiedName(cast<NamedDecl>(DC));
601 manglePostfix(DC->getParent(), NoFunction);
602 }
603}
604
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000605void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000606 // Microsoft uses the names on the case labels for these dtor variants. Clang
607 // uses the Itanium terminology internally. Everything in this ABI delegates
608 // towards the base dtor.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000609 switch (T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000610 // <operator-name> ::= ?1 # destructor
611 case Dtor_Base: Out << "?1"; return;
612 // <operator-name> ::= ?_D # vbase destructor
613 case Dtor_Complete: Out << "?_D"; return;
614 // <operator-name> ::= ?_G # scalar deleting destructor
615 case Dtor_Deleting: Out << "?_G"; return;
616 // <operator-name> ::= ?_E # vector deleting destructor
617 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
618 // it.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000619 }
620 llvm_unreachable("Unsupported dtor type?");
621}
622
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000623void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
624 SourceLocation Loc) {
625 switch (OO) {
626 // ?0 # constructor
627 // ?1 # destructor
628 // <operator-name> ::= ?2 # new
629 case OO_New: Out << "?2"; break;
630 // <operator-name> ::= ?3 # delete
631 case OO_Delete: Out << "?3"; break;
632 // <operator-name> ::= ?4 # =
633 case OO_Equal: Out << "?4"; break;
634 // <operator-name> ::= ?5 # >>
635 case OO_GreaterGreater: Out << "?5"; break;
636 // <operator-name> ::= ?6 # <<
637 case OO_LessLess: Out << "?6"; break;
638 // <operator-name> ::= ?7 # !
639 case OO_Exclaim: Out << "?7"; break;
640 // <operator-name> ::= ?8 # ==
641 case OO_EqualEqual: Out << "?8"; break;
642 // <operator-name> ::= ?9 # !=
643 case OO_ExclaimEqual: Out << "?9"; break;
644 // <operator-name> ::= ?A # []
645 case OO_Subscript: Out << "?A"; break;
646 // ?B # conversion
647 // <operator-name> ::= ?C # ->
648 case OO_Arrow: Out << "?C"; break;
649 // <operator-name> ::= ?D # *
650 case OO_Star: Out << "?D"; break;
651 // <operator-name> ::= ?E # ++
652 case OO_PlusPlus: Out << "?E"; break;
653 // <operator-name> ::= ?F # --
654 case OO_MinusMinus: Out << "?F"; break;
655 // <operator-name> ::= ?G # -
656 case OO_Minus: Out << "?G"; break;
657 // <operator-name> ::= ?H # +
658 case OO_Plus: Out << "?H"; break;
659 // <operator-name> ::= ?I # &
660 case OO_Amp: Out << "?I"; break;
661 // <operator-name> ::= ?J # ->*
662 case OO_ArrowStar: Out << "?J"; break;
663 // <operator-name> ::= ?K # /
664 case OO_Slash: Out << "?K"; break;
665 // <operator-name> ::= ?L # %
666 case OO_Percent: Out << "?L"; break;
667 // <operator-name> ::= ?M # <
668 case OO_Less: Out << "?M"; break;
669 // <operator-name> ::= ?N # <=
670 case OO_LessEqual: Out << "?N"; break;
671 // <operator-name> ::= ?O # >
672 case OO_Greater: Out << "?O"; break;
673 // <operator-name> ::= ?P # >=
674 case OO_GreaterEqual: Out << "?P"; break;
675 // <operator-name> ::= ?Q # ,
676 case OO_Comma: Out << "?Q"; break;
677 // <operator-name> ::= ?R # ()
678 case OO_Call: Out << "?R"; break;
679 // <operator-name> ::= ?S # ~
680 case OO_Tilde: Out << "?S"; break;
681 // <operator-name> ::= ?T # ^
682 case OO_Caret: Out << "?T"; break;
683 // <operator-name> ::= ?U # |
684 case OO_Pipe: Out << "?U"; break;
685 // <operator-name> ::= ?V # &&
686 case OO_AmpAmp: Out << "?V"; break;
687 // <operator-name> ::= ?W # ||
688 case OO_PipePipe: Out << "?W"; break;
689 // <operator-name> ::= ?X # *=
690 case OO_StarEqual: Out << "?X"; break;
691 // <operator-name> ::= ?Y # +=
692 case OO_PlusEqual: Out << "?Y"; break;
693 // <operator-name> ::= ?Z # -=
694 case OO_MinusEqual: Out << "?Z"; break;
695 // <operator-name> ::= ?_0 # /=
696 case OO_SlashEqual: Out << "?_0"; break;
697 // <operator-name> ::= ?_1 # %=
698 case OO_PercentEqual: Out << "?_1"; break;
699 // <operator-name> ::= ?_2 # >>=
700 case OO_GreaterGreaterEqual: Out << "?_2"; break;
701 // <operator-name> ::= ?_3 # <<=
702 case OO_LessLessEqual: Out << "?_3"; break;
703 // <operator-name> ::= ?_4 # &=
704 case OO_AmpEqual: Out << "?_4"; break;
705 // <operator-name> ::= ?_5 # |=
706 case OO_PipeEqual: Out << "?_5"; break;
707 // <operator-name> ::= ?_6 # ^=
708 case OO_CaretEqual: Out << "?_6"; break;
709 // ?_7 # vftable
710 // ?_8 # vbtable
711 // ?_9 # vcall
712 // ?_A # typeof
713 // ?_B # local static guard
714 // ?_C # string
715 // ?_D # vbase destructor
716 // ?_E # vector deleting destructor
717 // ?_F # default constructor closure
718 // ?_G # scalar deleting destructor
719 // ?_H # vector constructor iterator
720 // ?_I # vector destructor iterator
721 // ?_J # vector vbase constructor iterator
722 // ?_K # virtual displacement map
723 // ?_L # eh vector constructor iterator
724 // ?_M # eh vector destructor iterator
725 // ?_N # eh vector vbase constructor iterator
726 // ?_O # copy constructor closure
727 // ?_P<name> # udt returning <name>
728 // ?_Q # <unknown>
729 // ?_R0 # RTTI Type Descriptor
730 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
731 // ?_R2 # RTTI Base Class Array
732 // ?_R3 # RTTI Class Hierarchy Descriptor
733 // ?_R4 # RTTI Complete Object Locator
734 // ?_S # local vftable
735 // ?_T # local vftable constructor closure
736 // <operator-name> ::= ?_U # new[]
737 case OO_Array_New: Out << "?_U"; break;
738 // <operator-name> ::= ?_V # delete[]
739 case OO_Array_Delete: Out << "?_V"; break;
740
741 case OO_Conditional: {
742 DiagnosticsEngine &Diags = Context.getDiags();
743 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
744 "cannot mangle this conditional operator yet");
745 Diags.Report(Loc, DiagID);
746 break;
747 }
748
749 case OO_None:
750 case NUM_OVERLOADED_OPERATORS:
751 llvm_unreachable("Not an overloaded operator");
752 }
753}
754
755void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
756 // <source name> ::= <identifier> @
757 std::string key = II->getNameStart();
758 BackRefMap::iterator Found;
759 if (UseNameBackReferences)
760 Found = NameBackReferences.find(key);
761 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
762 Out << II->getName() << '@';
763 if (UseNameBackReferences && NameBackReferences.size() < 10) {
764 size_t Size = NameBackReferences.size();
765 NameBackReferences[key] = Size;
766 }
767 } else {
768 Out << Found->second;
769 }
770}
771
772void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
773 Context.mangleObjCMethodName(MD, Out);
774}
775
776// Find out how many function decls live above this one and return an integer
777// suitable for use as the number in a numbered anonymous scope.
778// TODO: Memoize.
779static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
780 const DeclContext *DC = FD->getParent();
781 int level = 1;
782
783 while (DC && !DC->isTranslationUnit()) {
784 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
785 DC = DC->getParent();
786 }
787
788 return 2*level;
789}
790
791void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
792 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
793 // <numbered-anonymous-scope> ::= ? <number>
794 // Even though the name is rendered in reverse order (e.g.
795 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
796 // innermost. So a method bar in class C local to function foo gets mangled
797 // as something like:
798 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
799 // This is more apparent when you have a type nested inside a method of a
800 // type nested inside a function. A method baz in class D local to method
801 // bar of class C local to function foo gets mangled as:
802 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
803 // This scheme is general enough to support GCC-style nested
804 // functions. You could have a method baz of class C inside a function bar
805 // inside a function foo, like so:
806 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
807 int NestLevel = getLocalNestingLevel(FD);
808 Out << '?';
809 mangleNumber(NestLevel);
810 Out << '?';
811 mangle(FD, "?");
812}
813
814void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
815 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000816 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000817 // <template-name> ::= <unscoped-template-name> <template-args>
818 // ::= <substitution>
819 // Always start with the unqualified name.
820
821 // Templates have their own context for back references.
822 ArgBackRefMap OuterArgsContext;
823 BackRefMap OuterTemplateContext;
824 NameBackReferences.swap(OuterTemplateContext);
825 TypeBackReferences.swap(OuterArgsContext);
826
827 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000828 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000829
830 // Restore the previous back reference contexts.
831 NameBackReferences.swap(OuterTemplateContext);
832 TypeBackReferences.swap(OuterArgsContext);
833}
834
835void
836MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
837 // <unscoped-template-name> ::= ?$ <unqualified-name>
838 Out << "?$";
839 mangleUnqualifiedName(TD);
840}
841
842void
843MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
844 bool IsBoolean) {
845 // <integer-literal> ::= $0 <number>
846 Out << "$0";
847 // Make sure booleans are encoded as 0/1.
848 if (IsBoolean && Value.getBoolValue())
849 mangleNumber(1);
850 else
851 mangleNumber(Value);
852}
853
854void
855MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
856 // See if this is a constant expression.
857 llvm::APSInt Value;
858 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
859 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
860 return;
861 }
862
David Majnemerc80eb462013-08-13 06:32:20 +0000863 const CXXUuidofExpr *UE = 0;
864 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
865 if (UO->getOpcode() == UO_AddrOf)
866 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
867 } else
868 UE = dyn_cast<CXXUuidofExpr>(E);
869
870 if (UE) {
871 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
872 // const __s_GUID _GUID_{lower case UUID with underscores}
873 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
874 std::string Name = "_GUID_" + Uuid.lower();
875 std::replace(Name.begin(), Name.end(), '-', '_');
876
David Majnemer26314e12013-08-13 09:17:25 +0000877 // If we had to peek through an address-of operator, treat this like we are
David Majnemerc80eb462013-08-13 06:32:20 +0000878 // dealing with a pointer type. Otherwise, treat it like a const reference.
879 //
880 // N.B. This matches up with the handling of TemplateArgument::Declaration
881 // in mangleTemplateArg
882 if (UE == E)
883 Out << "$E?";
884 else
885 Out << "$1?";
886 Out << Name << "@@3U__s_GUID@@B";
887 return;
888 }
889
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000890 // As bad as this diagnostic is, it's better than crashing.
891 DiagnosticsEngine &Diags = Context.getDiags();
892 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
893 "cannot yet mangle expression type %0");
894 Diags.Report(E->getExprLoc(), DiagID)
895 << E->getStmtClassName() << E->getSourceRange();
896}
897
898void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000899MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
900 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000901 // <template-args> ::= {<type> | <integer-literal>}+ @
902 unsigned NumTemplateArgs = TemplateArgs.size();
903 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000904 const TemplateArgument &TA = TemplateArgs[i];
David Majnemer309f6452013-08-27 08:21:25 +0000905 mangleTemplateArg(TD, TA);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000906 }
907 Out << '@';
908}
909
Reid Kleckner5d90d182013-07-02 18:10:07 +0000910void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
David Majnemer309f6452013-08-27 08:21:25 +0000911 const TemplateArgument &TA) {
Reid Kleckner5d90d182013-07-02 18:10:07 +0000912 switch (TA.getKind()) {
913 case TemplateArgument::Null:
914 llvm_unreachable("Can't mangle null template arguments!");
David Majnemer309f6452013-08-27 08:21:25 +0000915 case TemplateArgument::TemplateExpansion:
916 llvm_unreachable("Can't mangle template expansion arguments!");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000917 case TemplateArgument::Type: {
918 QualType T = TA.getAsType();
919 mangleType(T, SourceRange(), QMM_Escape);
920 break;
921 }
David Majnemerf2081f62013-08-13 01:25:35 +0000922 case TemplateArgument::Declaration: {
923 const NamedDecl *ND = cast<NamedDecl>(TA.getAsDecl());
924 mangle(ND, TA.isDeclForReferenceParam() ? "$E?" : "$1?");
Reid Kleckner5d90d182013-07-02 18:10:07 +0000925 break;
David Majnemerf2081f62013-08-13 01:25:35 +0000926 }
Reid Kleckner5d90d182013-07-02 18:10:07 +0000927 case TemplateArgument::Integral:
928 mangleIntegerLiteral(TA.getAsIntegral(),
929 TA.getIntegralType()->isBooleanType());
930 break;
David Majnemer7802fc92013-08-05 21:33:59 +0000931 case TemplateArgument::NullPtr:
932 Out << "$0A@";
933 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000934 case TemplateArgument::Expression:
935 mangleExpression(TA.getAsExpr());
936 break;
937 case TemplateArgument::Pack:
938 // Unlike Itanium, there is no character code to indicate an argument pack.
Reid Kleckner5d90d182013-07-02 18:10:07 +0000939 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
940 I != E; ++I)
David Majnemer309f6452013-08-27 08:21:25 +0000941 mangleTemplateArg(TD, *I);
Reid Kleckner5d90d182013-07-02 18:10:07 +0000942 break;
943 case TemplateArgument::Template:
David Majnemer02c44f02013-08-05 22:26:46 +0000944 mangleType(cast<TagDecl>(
945 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
946 break;
Reid Kleckner5d90d182013-07-02 18:10:07 +0000947 }
948}
949
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000950void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
951 bool IsMember) {
952 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
953 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
954 // 'I' means __restrict (32/64-bit).
955 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
956 // keyword!
957 // <base-cvr-qualifiers> ::= A # near
958 // ::= B # near const
959 // ::= C # near volatile
960 // ::= D # near const volatile
961 // ::= E # far (16-bit)
962 // ::= F # far const (16-bit)
963 // ::= G # far volatile (16-bit)
964 // ::= H # far const volatile (16-bit)
965 // ::= I # huge (16-bit)
966 // ::= J # huge const (16-bit)
967 // ::= K # huge volatile (16-bit)
968 // ::= L # huge const volatile (16-bit)
969 // ::= M <basis> # based
970 // ::= N <basis> # based const
971 // ::= O <basis> # based volatile
972 // ::= P <basis> # based const volatile
973 // ::= Q # near member
974 // ::= R # near const member
975 // ::= S # near volatile member
976 // ::= T # near const volatile member
977 // ::= U # far member (16-bit)
978 // ::= V # far const member (16-bit)
979 // ::= W # far volatile member (16-bit)
980 // ::= X # far const volatile member (16-bit)
981 // ::= Y # huge member (16-bit)
982 // ::= Z # huge const member (16-bit)
983 // ::= 0 # huge volatile member (16-bit)
984 // ::= 1 # huge const volatile member (16-bit)
985 // ::= 2 <basis> # based member
986 // ::= 3 <basis> # based const member
987 // ::= 4 <basis> # based volatile member
988 // ::= 5 <basis> # based const volatile member
989 // ::= 6 # near function (pointers only)
990 // ::= 7 # far function (pointers only)
991 // ::= 8 # near method (pointers only)
992 // ::= 9 # far method (pointers only)
993 // ::= _A <basis> # based function (pointers only)
994 // ::= _B <basis> # based function (far?) (pointers only)
995 // ::= _C <basis> # based method (pointers only)
996 // ::= _D <basis> # based method (far?) (pointers only)
997 // ::= _E # block (Clang)
998 // <basis> ::= 0 # __based(void)
999 // ::= 1 # __based(segment)?
1000 // ::= 2 <name> # __based(name)
1001 // ::= 3 # ?
1002 // ::= 4 # ?
1003 // ::= 5 # not really based
1004 bool HasConst = Quals.hasConst(),
1005 HasVolatile = Quals.hasVolatile();
David Majnemerc0e64f32013-08-05 22:43:06 +00001006
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001007 if (!IsMember) {
1008 if (HasConst && HasVolatile) {
1009 Out << 'D';
1010 } else if (HasVolatile) {
1011 Out << 'C';
1012 } else if (HasConst) {
1013 Out << 'B';
1014 } else {
1015 Out << 'A';
1016 }
1017 } else {
1018 if (HasConst && HasVolatile) {
1019 Out << 'T';
1020 } else if (HasVolatile) {
1021 Out << 'S';
1022 } else if (HasConst) {
1023 Out << 'R';
1024 } else {
1025 Out << 'Q';
1026 }
1027 }
1028
1029 // FIXME: For now, just drop all extension qualifiers on the floor.
1030}
1031
1032void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1033 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1034 // ::= Q # const
1035 // ::= R # volatile
1036 // ::= S # const volatile
1037 bool HasConst = Quals.hasConst(),
1038 HasVolatile = Quals.hasVolatile();
1039 if (HasConst && HasVolatile) {
1040 Out << 'S';
1041 } else if (HasVolatile) {
1042 Out << 'R';
1043 } else if (HasConst) {
1044 Out << 'Q';
1045 } else {
1046 Out << 'P';
1047 }
1048}
1049
1050void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1051 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001052 // MSVC will backreference two canonically equivalent types that have slightly
1053 // different manglings when mangled alone.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001054 void *TypePtr = getASTContext().getCanonicalType(T).getAsOpaquePtr();
1055 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1056
1057 if (Found == TypeBackReferences.end()) {
1058 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1059
Reid Klecknerf21818d2013-06-24 19:21:52 +00001060 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1061 QualType OT = DT->getOriginalType();
1062 if (const ArrayType *AT = getASTContext().getAsArrayType(OT)) {
1063 mangleDecayedArrayType(AT, false);
1064 } else if (const FunctionType *FT = OT->getAs<FunctionType>()) {
1065 Out << "P6";
1066 mangleFunctionType(FT, 0, false, false);
1067 } else {
1068 llvm_unreachable("unexpected decayed type");
1069 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001070 } else {
1071 mangleType(T, Range, QMM_Drop);
1072 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001073
1074 // See if it's worth creating a back reference.
1075 // Only types longer than 1 character are considered
1076 // and only 10 back references slots are available:
1077 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1078 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1079 size_t Size = TypeBackReferences.size();
1080 TypeBackReferences[TypePtr] = Size;
1081 }
1082 } else {
1083 Out << Found->second;
1084 }
1085}
1086
1087void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001088 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001089 // Don't use the canonical types. MSVC includes things like 'const' on
1090 // pointer arguments to function pointers that canonicalization strips away.
1091 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001092 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001093 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1094 // If there were any Quals, getAsArrayType() pushed them onto the array
1095 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001096 if (QMM == QMM_Mangle)
1097 Out << 'A';
1098 else if (QMM == QMM_Escape || QMM == QMM_Result)
1099 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001100 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001101 return;
1102 }
1103
1104 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1105 T->isBlockPointerType();
1106
1107 switch (QMM) {
1108 case QMM_Drop:
1109 break;
1110 case QMM_Mangle:
1111 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1112 Out << '6';
1113 mangleFunctionType(FT, 0, false, false);
1114 return;
1115 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001116 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001117 break;
1118 case QMM_Escape:
1119 if (!IsPointer && Quals) {
1120 Out << "$$C";
1121 mangleQualifiers(Quals, false);
1122 }
1123 break;
1124 case QMM_Result:
1125 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1126 Out << '?';
1127 mangleQualifiers(Quals, false);
1128 }
1129 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001130 }
1131
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001132 // We have to mangle these now, while we still have enough information.
1133 if (IsPointer)
1134 manglePointerQualifiers(Quals);
1135 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001136
1137 switch (ty->getTypeClass()) {
1138#define ABSTRACT_TYPE(CLASS, PARENT)
1139#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1140 case Type::CLASS: \
1141 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1142 return;
1143#define TYPE(CLASS, PARENT) \
1144 case Type::CLASS: \
1145 mangleType(cast<CLASS##Type>(ty), Range); \
1146 break;
1147#include "clang/AST/TypeNodes.def"
1148#undef ABSTRACT_TYPE
1149#undef NON_CANONICAL_TYPE
1150#undef TYPE
1151 }
1152}
1153
1154void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1155 SourceRange Range) {
1156 // <type> ::= <builtin-type>
1157 // <builtin-type> ::= X # void
1158 // ::= C # signed char
1159 // ::= D # char
1160 // ::= E # unsigned char
1161 // ::= F # short
1162 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1163 // ::= H # int
1164 // ::= I # unsigned int
1165 // ::= J # long
1166 // ::= K # unsigned long
1167 // L # <none>
1168 // ::= M # float
1169 // ::= N # double
1170 // ::= O # long double (__float80 is mangled differently)
1171 // ::= _J # long long, __int64
1172 // ::= _K # unsigned long long, __int64
1173 // ::= _L # __int128
1174 // ::= _M # unsigned __int128
1175 // ::= _N # bool
1176 // _O # <array in parameter>
1177 // ::= _T # __float80 (Intel)
1178 // ::= _W # wchar_t
1179 // ::= _Z # __float80 (Digital Mars)
1180 switch (T->getKind()) {
1181 case BuiltinType::Void: Out << 'X'; break;
1182 case BuiltinType::SChar: Out << 'C'; break;
1183 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1184 case BuiltinType::UChar: Out << 'E'; break;
1185 case BuiltinType::Short: Out << 'F'; break;
1186 case BuiltinType::UShort: Out << 'G'; break;
1187 case BuiltinType::Int: Out << 'H'; break;
1188 case BuiltinType::UInt: Out << 'I'; break;
1189 case BuiltinType::Long: Out << 'J'; break;
1190 case BuiltinType::ULong: Out << 'K'; break;
1191 case BuiltinType::Float: Out << 'M'; break;
1192 case BuiltinType::Double: Out << 'N'; break;
1193 // TODO: Determine size and mangle accordingly
1194 case BuiltinType::LongDouble: Out << 'O'; break;
1195 case BuiltinType::LongLong: Out << "_J"; break;
1196 case BuiltinType::ULongLong: Out << "_K"; break;
1197 case BuiltinType::Int128: Out << "_L"; break;
1198 case BuiltinType::UInt128: Out << "_M"; break;
1199 case BuiltinType::Bool: Out << "_N"; break;
1200 case BuiltinType::WChar_S:
1201 case BuiltinType::WChar_U: Out << "_W"; break;
1202
1203#define BUILTIN_TYPE(Id, SingletonId)
1204#define PLACEHOLDER_TYPE(Id, SingletonId) \
1205 case BuiltinType::Id:
1206#include "clang/AST/BuiltinTypes.def"
1207 case BuiltinType::Dependent:
1208 llvm_unreachable("placeholder types shouldn't get to name mangling");
1209
1210 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1211 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1212 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001213
1214 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1215 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1216 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1217 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1218 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1219 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001220 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001221 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001222
1223 case BuiltinType::NullPtr: Out << "$$T"; break;
1224
1225 case BuiltinType::Char16:
1226 case BuiltinType::Char32:
1227 case BuiltinType::Half: {
1228 DiagnosticsEngine &Diags = Context.getDiags();
1229 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1230 "cannot mangle this built-in %0 type yet");
1231 Diags.Report(Range.getBegin(), DiagID)
1232 << T->getName(Context.getASTContext().getPrintingPolicy())
1233 << Range;
1234 break;
1235 }
1236 }
1237}
1238
1239// <type> ::= <function-type>
1240void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1241 SourceRange) {
1242 // Structors only appear in decls, so at this point we know it's not a
1243 // structor type.
1244 // FIXME: This may not be lambda-friendly.
1245 Out << "$$A6";
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001246 mangleFunctionType(T, NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001247}
1248void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1249 SourceRange) {
1250 llvm_unreachable("Can't mangle K&R function prototypes");
1251}
1252
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001253void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1254 const FunctionDecl *D,
1255 bool IsStructor,
1256 bool IsInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001257 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1258 // <return-type> <argument-list> <throw-spec>
1259 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1260
Reid Klecknerf21818d2013-06-24 19:21:52 +00001261 SourceRange Range;
1262 if (D) Range = D->getSourceRange();
1263
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001264 // If this is a C++ instance method, mangle the CVR qualifiers for the
1265 // this pointer.
David Majnemer1c7a4092013-08-15 08:13:23 +00001266 if (IsInstMethod) {
1267 if (PointersAre64Bit)
1268 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001269 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
David Majnemer1c7a4092013-08-15 08:13:23 +00001270 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001271
1272 mangleCallingConvention(T, IsInstMethod);
1273
1274 // <return-type> ::= <type>
1275 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001276 if (IsStructor) {
1277 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1278 StructorType == Dtor_Deleting) {
1279 // The scalar deleting destructor takes an extra int argument.
1280 // However, the FunctionType generated has 0 arguments.
1281 // FIXME: This is a temporary hack.
1282 // Maybe should fix the FunctionType creation instead?
Timur Iskhodzhanov4b104062013-08-26 10:32:04 +00001283 Out << (PointersAre64Bit ? "PEAXI@Z" : "PAXI@Z");
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001284 return;
1285 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001286 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001287 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001288 QualType ResultType = Proto->getResultType();
1289 if (ResultType->isVoidType())
1290 ResultType = ResultType.getUnqualifiedType();
1291 mangleType(ResultType, Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001292 }
1293
1294 // <argument-list> ::= X # void
1295 // ::= <type>+ @
1296 // ::= <type>* Z # varargs
1297 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1298 Out << 'X';
1299 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001300 // Happens for function pointer type arguments for example.
1301 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1302 ArgEnd = Proto->arg_type_end();
1303 Arg != ArgEnd; ++Arg)
1304 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001305 // <builtin-type> ::= Z # ellipsis
1306 if (Proto->isVariadic())
1307 Out << 'Z';
1308 else
1309 Out << '@';
1310 }
1311
1312 mangleThrowSpecification(Proto);
1313}
1314
1315void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001316 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1317 // # pointer. in 64-bit mode *all*
1318 // # 'this' pointers are 64-bit.
1319 // ::= <global-function>
1320 // <member-function> ::= A # private: near
1321 // ::= B # private: far
1322 // ::= C # private: static near
1323 // ::= D # private: static far
1324 // ::= E # private: virtual near
1325 // ::= F # private: virtual far
1326 // ::= G # private: thunk near
1327 // ::= H # private: thunk far
1328 // ::= I # protected: near
1329 // ::= J # protected: far
1330 // ::= K # protected: static near
1331 // ::= L # protected: static far
1332 // ::= M # protected: virtual near
1333 // ::= N # protected: virtual far
1334 // ::= O # protected: thunk near
1335 // ::= P # protected: thunk far
1336 // ::= Q # public: near
1337 // ::= R # public: far
1338 // ::= S # public: static near
1339 // ::= T # public: static far
1340 // ::= U # public: virtual near
1341 // ::= V # public: virtual far
1342 // ::= W # public: thunk near
1343 // ::= X # public: thunk far
1344 // <global-function> ::= Y # global near
1345 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001346 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1347 switch (MD->getAccess()) {
1348 default:
1349 case AS_private:
1350 if (MD->isStatic())
1351 Out << 'C';
1352 else if (MD->isVirtual())
1353 Out << 'E';
1354 else
1355 Out << 'A';
1356 break;
1357 case AS_protected:
1358 if (MD->isStatic())
1359 Out << 'K';
1360 else if (MD->isVirtual())
1361 Out << 'M';
1362 else
1363 Out << 'I';
1364 break;
1365 case AS_public:
1366 if (MD->isStatic())
1367 Out << 'S';
1368 else if (MD->isVirtual())
1369 Out << 'U';
1370 else
1371 Out << 'Q';
1372 }
1373 } else
1374 Out << 'Y';
1375}
1376void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
1377 bool IsInstMethod) {
1378 // <calling-convention> ::= A # __cdecl
1379 // ::= B # __export __cdecl
1380 // ::= C # __pascal
1381 // ::= D # __export __pascal
1382 // ::= E # __thiscall
1383 // ::= F # __export __thiscall
1384 // ::= G # __stdcall
1385 // ::= H # __export __stdcall
1386 // ::= I # __fastcall
1387 // ::= J # __export __fastcall
1388 // The 'export' calling conventions are from a bygone era
1389 // (*cough*Win16*cough*) when functions were declared for export with
1390 // that keyword. (It didn't actually export them, it just made them so
1391 // that they could be in a DLL and somebody from another module could call
1392 // them.)
1393 CallingConv CC = T->getCallConv();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001394 switch (CC) {
1395 default:
1396 llvm_unreachable("Unsupported CC for mangling");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001397 case CC_C: Out << 'A'; break;
1398 case CC_X86Pascal: Out << 'C'; break;
1399 case CC_X86ThisCall: Out << 'E'; break;
1400 case CC_X86StdCall: Out << 'G'; break;
1401 case CC_X86FastCall: Out << 'I'; break;
1402 }
1403}
1404void MicrosoftCXXNameMangler::mangleThrowSpecification(
1405 const FunctionProtoType *FT) {
1406 // <throw-spec> ::= Z # throw(...) (default)
1407 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1408 // ::= <type>+
1409 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1410 // all actually mangled as 'Z'. (They're ignored because their associated
1411 // functionality isn't implemented, and probably never will be.)
1412 Out << 'Z';
1413}
1414
1415void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1416 SourceRange Range) {
1417 // Probably should be mangled as a template instantiation; need to see what
1418 // VC does first.
1419 DiagnosticsEngine &Diags = Context.getDiags();
1420 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1421 "cannot mangle this unresolved dependent type yet");
1422 Diags.Report(Range.getBegin(), DiagID)
1423 << Range;
1424}
1425
1426// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1427// <union-type> ::= T <name>
1428// <struct-type> ::= U <name>
1429// <class-type> ::= V <name>
1430// <enum-type> ::= W <size> <name>
1431void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001432 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001433}
1434void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001435 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001436}
David Majnemer02c44f02013-08-05 22:26:46 +00001437void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1438 switch (TD->getTagKind()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001439 case TTK_Union:
1440 Out << 'T';
1441 break;
1442 case TTK_Struct:
1443 case TTK_Interface:
1444 Out << 'U';
1445 break;
1446 case TTK_Class:
1447 Out << 'V';
1448 break;
1449 case TTK_Enum:
1450 Out << 'W';
1451 Out << getASTContext().getTypeSizeInChars(
David Majnemer02c44f02013-08-05 22:26:46 +00001452 cast<EnumDecl>(TD)->getIntegerType()).getQuantity();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001453 break;
1454 }
David Majnemer02c44f02013-08-05 22:26:46 +00001455 mangleName(TD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001456}
1457
1458// <type> ::= <array-type>
1459// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1460// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001461// <element-type> # as global, E is never required
1462// ::= Q E? <cvr-qualifiers> [Y <dimension-count> <dimension>+]
1463// <element-type> # as param, E is required for 64-bit
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001464// It's supposed to be the other way around, but for some strange reason, it
1465// isn't. Today this behavior is retained for the sole purpose of backwards
1466// compatibility.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001467void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T,
1468 bool IsGlobal) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001469 // This isn't a recursive mangling, so now we have to do it all in this
1470 // one call.
1471 if (IsGlobal) {
1472 manglePointerQualifiers(T->getElementType().getQualifiers());
1473 } else {
1474 Out << 'Q';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001475 if (PointersAre64Bit)
1476 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001477 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001478 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001479}
1480void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1481 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001482 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001483}
1484void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *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 DependentSizedArrayType *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 IncompleteArrayType *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}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001496void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001497 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001498 SmallVector<llvm::APInt, 3> Dimensions;
1499 for (;;) {
1500 if (const ConstantArrayType *CAT =
1501 getASTContext().getAsConstantArrayType(ElementTy)) {
1502 Dimensions.push_back(CAT->getSize());
1503 ElementTy = CAT->getElementType();
1504 } else if (ElementTy->isVariableArrayType()) {
1505 const VariableArrayType *VAT =
1506 getASTContext().getAsVariableArrayType(ElementTy);
1507 DiagnosticsEngine &Diags = Context.getDiags();
1508 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1509 "cannot mangle this variable-length array yet");
1510 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1511 << VAT->getBracketsRange();
1512 return;
1513 } else if (ElementTy->isDependentSizedArrayType()) {
1514 // The dependent expression has to be folded into a constant (TODO).
1515 const DependentSizedArrayType *DSAT =
1516 getASTContext().getAsDependentSizedArrayType(ElementTy);
1517 DiagnosticsEngine &Diags = Context.getDiags();
1518 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1519 "cannot mangle this dependent-length array yet");
1520 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1521 << DSAT->getBracketsRange();
1522 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001523 } else if (const IncompleteArrayType *IAT =
1524 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1525 Dimensions.push_back(llvm::APInt(32, 0));
1526 ElementTy = IAT->getElementType();
1527 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001528 else break;
1529 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001530 Out << 'Y';
1531 // <dimension-count> ::= <number> # number of extra dimensions
1532 mangleNumber(Dimensions.size());
1533 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1534 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001535 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001536}
1537
1538// <type> ::= <pointer-to-member-type>
1539// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1540// <class name> <type>
1541void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1542 SourceRange Range) {
1543 QualType PointeeType = T->getPointeeType();
1544 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1545 Out << '8';
1546 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001547 mangleFunctionType(FPT, NULL, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001548 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001549 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1550 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001551 mangleQualifiers(PointeeType.getQualifiers(), true);
1552 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001553 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001554 }
1555}
1556
1557void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1558 SourceRange Range) {
1559 DiagnosticsEngine &Diags = Context.getDiags();
1560 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1561 "cannot mangle this template type parameter type yet");
1562 Diags.Report(Range.getBegin(), DiagID)
1563 << Range;
1564}
1565
1566void MicrosoftCXXNameMangler::mangleType(
1567 const SubstTemplateTypeParmPackType *T,
1568 SourceRange Range) {
1569 DiagnosticsEngine &Diags = Context.getDiags();
1570 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1571 "cannot mangle this substituted parameter pack yet");
1572 Diags.Report(Range.getBegin(), DiagID)
1573 << Range;
1574}
1575
1576// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001577// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1578// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001579void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1580 SourceRange Range) {
1581 QualType PointeeTy = T->getPointeeType();
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001582 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1583 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001584 mangleType(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001585}
1586void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1587 SourceRange Range) {
1588 // Object pointers never have qualifiers.
1589 Out << 'A';
David Majnemer1c7a4092013-08-15 08:13:23 +00001590 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1591 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001592 mangleType(T->getPointeeType(), Range);
1593}
1594
1595// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001596// <reference-type> ::= A E? <cvr-qualifiers> <type>
1597// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001598void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1599 SourceRange Range) {
1600 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001601 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1602 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001603 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001604}
1605
1606// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001607// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1608// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001609void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1610 SourceRange Range) {
1611 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001612 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1613 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001614 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001615}
1616
1617void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1618 SourceRange Range) {
1619 DiagnosticsEngine &Diags = Context.getDiags();
1620 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1621 "cannot mangle this complex number type yet");
1622 Diags.Report(Range.getBegin(), DiagID)
1623 << Range;
1624}
1625
1626void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1627 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001628 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1629 assert(ET && "vectors with non-builtin elements are unsupported");
1630 uint64_t Width = getASTContext().getTypeSize(T);
1631 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1632 // doesn't match the Intel types uses a custom mangling below.
1633 bool IntelVector = true;
1634 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1635 Out << "T__m64";
1636 } else if (Width == 128 || Width == 256) {
1637 if (ET->getKind() == BuiltinType::Float)
1638 Out << "T__m" << Width;
1639 else if (ET->getKind() == BuiltinType::LongLong)
1640 Out << "T__m" << Width << 'i';
1641 else if (ET->getKind() == BuiltinType::Double)
1642 Out << "U__m" << Width << 'd';
1643 else
1644 IntelVector = false;
1645 } else {
1646 IntelVector = false;
1647 }
1648
1649 if (!IntelVector) {
1650 // The MS ABI doesn't have a special mangling for vector types, so we define
1651 // our own mangling to handle uses of __vector_size__ on user-specified
1652 // types, and for extensions like __v4sf.
1653 Out << "T__clang_vec" << T->getNumElements() << '_';
1654 mangleType(ET, Range);
1655 }
1656
1657 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001658}
Reid Kleckner1232e272013-03-26 16:56:59 +00001659
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001660void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1661 SourceRange Range) {
1662 DiagnosticsEngine &Diags = Context.getDiags();
1663 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1664 "cannot mangle this extended vector type yet");
1665 Diags.Report(Range.getBegin(), DiagID)
1666 << Range;
1667}
1668void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1669 SourceRange Range) {
1670 DiagnosticsEngine &Diags = Context.getDiags();
1671 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1672 "cannot mangle this dependent-sized extended vector type yet");
1673 Diags.Report(Range.getBegin(), DiagID)
1674 << Range;
1675}
1676
1677void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1678 SourceRange) {
1679 // ObjC interfaces have structs underlying them.
1680 Out << 'U';
1681 mangleName(T->getDecl());
1682}
1683
1684void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1685 SourceRange Range) {
1686 // We don't allow overloading by different protocol qualification,
1687 // so mangling them isn't necessary.
1688 mangleType(T->getBaseType(), Range);
1689}
1690
1691void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1692 SourceRange Range) {
1693 Out << "_E";
1694
1695 QualType pointee = T->getPointeeType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001696 mangleFunctionType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001697}
1698
David Majnemer360d23e2013-08-16 08:29:13 +00001699void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1700 SourceRange) {
1701 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001702}
1703
1704void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1705 SourceRange Range) {
1706 DiagnosticsEngine &Diags = Context.getDiags();
1707 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1708 "cannot mangle this template specialization type yet");
1709 Diags.Report(Range.getBegin(), DiagID)
1710 << Range;
1711}
1712
1713void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1714 SourceRange Range) {
1715 DiagnosticsEngine &Diags = Context.getDiags();
1716 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1717 "cannot mangle this dependent name type yet");
1718 Diags.Report(Range.getBegin(), DiagID)
1719 << Range;
1720}
1721
1722void MicrosoftCXXNameMangler::mangleType(
1723 const DependentTemplateSpecializationType *T,
1724 SourceRange Range) {
1725 DiagnosticsEngine &Diags = Context.getDiags();
1726 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1727 "cannot mangle this dependent template specialization type yet");
1728 Diags.Report(Range.getBegin(), DiagID)
1729 << Range;
1730}
1731
1732void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1733 SourceRange Range) {
1734 DiagnosticsEngine &Diags = Context.getDiags();
1735 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1736 "cannot mangle this pack expansion yet");
1737 Diags.Report(Range.getBegin(), DiagID)
1738 << Range;
1739}
1740
1741void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1742 SourceRange Range) {
1743 DiagnosticsEngine &Diags = Context.getDiags();
1744 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1745 "cannot mangle this typeof(type) yet");
1746 Diags.Report(Range.getBegin(), DiagID)
1747 << Range;
1748}
1749
1750void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1751 SourceRange Range) {
1752 DiagnosticsEngine &Diags = Context.getDiags();
1753 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1754 "cannot mangle this typeof(expression) yet");
1755 Diags.Report(Range.getBegin(), DiagID)
1756 << Range;
1757}
1758
1759void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1760 SourceRange Range) {
1761 DiagnosticsEngine &Diags = Context.getDiags();
1762 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1763 "cannot mangle this decltype() yet");
1764 Diags.Report(Range.getBegin(), DiagID)
1765 << Range;
1766}
1767
1768void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1769 SourceRange Range) {
1770 DiagnosticsEngine &Diags = Context.getDiags();
1771 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1772 "cannot mangle this unary transform type yet");
1773 Diags.Report(Range.getBegin(), DiagID)
1774 << Range;
1775}
1776
1777void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1778 DiagnosticsEngine &Diags = Context.getDiags();
1779 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1780 "cannot mangle this 'auto' type yet");
1781 Diags.Report(Range.getBegin(), DiagID)
1782 << Range;
1783}
1784
1785void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1786 SourceRange Range) {
1787 DiagnosticsEngine &Diags = Context.getDiags();
1788 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1789 "cannot mangle this C11 atomic type yet");
1790 Diags.Report(Range.getBegin(), DiagID)
1791 << Range;
1792}
1793
1794void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1795 raw_ostream &Out) {
1796 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1797 "Invalid mangleName() call, argument is not a variable or function!");
1798 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1799 "Invalid mangleName() call on 'structor decl!");
1800
1801 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1802 getASTContext().getSourceManager(),
1803 "Mangling declaration");
1804
1805 MicrosoftCXXNameMangler Mangler(*this, Out);
1806 return Mangler.mangle(D);
1807}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001808
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001809void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1810 const ThunkInfo &Thunk,
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001811 raw_ostream &Out) {
1812 // FIXME: this is not yet a complete implementation, but merely a
1813 // reasonably-working stub to avoid crashing when required to emit a thunk.
1814 MicrosoftCXXNameMangler Mangler(*this, Out);
1815 Out << "\01?";
1816 Mangler.mangleName(MD);
1817 if (Thunk.This.NonVirtual != 0) {
1818 // FIXME: add support for protected/private or use mangleFunctionClass.
1819 Out << "W";
1820 llvm::APSInt APSNumber(/*BitWidth=*/32 /*FIXME: check on x64*/,
1821 /*isUnsigned=*/true);
1822 APSNumber = -Thunk.This.NonVirtual;
1823 Mangler.mangleNumber(APSNumber);
1824 } else {
1825 // FIXME: add support for protected/private or use mangleFunctionClass.
1826 Out << "Q";
1827 }
1828 // FIXME: mangle return adjustment? Most likely includes using an overridee FPT?
1829 Mangler.mangleFunctionType(MD->getType()->castAs<FunctionProtoType>(), MD, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001830}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001831
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001832void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1833 CXXDtorType Type,
1834 const ThisAdjustment &,
1835 raw_ostream &) {
1836 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1837 "cannot mangle thunk for this destructor yet");
1838 getDiags().Report(DD->getLocation(), DiagID);
1839}
Reid Kleckner90633022013-06-19 15:20:38 +00001840
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001841void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1842 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001843 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1844 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001845 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001846 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001847 MicrosoftCXXNameMangler Mangler(*this, Out);
1848 Mangler.getStream() << "\01??_7";
1849 Mangler.mangleName(RD);
Reid Kleckner90633022013-06-19 15:20:38 +00001850 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001851 // TODO: If the class has more than one vtable, mangle in the class it came
1852 // from.
1853 Mangler.getStream() << '@';
1854}
Reid Kleckner90633022013-06-19 15:20:38 +00001855
1856void MicrosoftMangleContext::mangleCXXVBTable(
1857 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1858 raw_ostream &Out) {
1859 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1860 // <cvr-qualifiers> [<name>] @
1861 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1862 // is always '7' for vbtables.
1863 MicrosoftCXXNameMangler Mangler(*this, Out);
1864 Mangler.getStream() << "\01??_8";
1865 Mangler.mangleName(Derived);
1866 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1867 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1868 E = BasePath.end();
1869 I != E; ++I) {
1870 Mangler.mangleName(*I);
1871 }
1872 Mangler.getStream() << '@';
1873}
1874
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001875void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1876 raw_ostream &) {
1877 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1878}
1879void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1880 int64_t Offset,
1881 const CXXRecordDecl *Type,
1882 raw_ostream &) {
1883 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1884}
1885void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1886 raw_ostream &) {
1887 // FIXME: Give a location...
1888 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1889 "cannot mangle RTTI descriptors for type %0 yet");
1890 getDiags().Report(DiagID)
1891 << T.getBaseTypeIdentifier();
1892}
1893void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1894 raw_ostream &) {
1895 // FIXME: Give a location...
1896 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1897 "cannot mangle the name of type %0 into RTTI descriptors yet");
1898 getDiags().Report(DiagID)
1899 << T.getBaseTypeIdentifier();
1900}
1901void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1902 CXXCtorType Type,
1903 raw_ostream & Out) {
1904 MicrosoftCXXNameMangler mangler(*this, Out);
1905 mangler.mangle(D);
1906}
1907void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1908 CXXDtorType Type,
1909 raw_ostream & Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001910 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001911 mangler.mangle(D);
1912}
1913void MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *VD,
1914 raw_ostream &) {
1915 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1916 "cannot mangle this reference temporary yet");
1917 getDiags().Report(VD->getLocation(), DiagID);
1918}
1919
1920MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
1921 DiagnosticsEngine &Diags) {
1922 return new MicrosoftMangleContext(Context, Diags);
1923}