blob: d9e18eb067faf64c2b1c63d258ab9626cfc16573 [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);
Reid Kleckner5d90d182013-07-02 18:10:07 +0000138 void mangleTemplateArg(const TemplateDecl *TD, const TemplateArgument &TA,
139 int ArgIndex);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000140};
141
142/// MicrosoftMangleContext - Overrides the default MangleContext for the
143/// Microsoft Visual C++ ABI.
144class MicrosoftMangleContext : public MangleContext {
145public:
146 MicrosoftMangleContext(ASTContext &Context,
147 DiagnosticsEngine &Diags) : MangleContext(Context, Diags) { }
148 virtual bool shouldMangleDeclName(const NamedDecl *D);
149 virtual void mangleName(const NamedDecl *D, raw_ostream &Out);
150 virtual void mangleThunk(const CXXMethodDecl *MD,
151 const ThunkInfo &Thunk,
152 raw_ostream &);
153 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
154 const ThisAdjustment &ThisAdjustment,
155 raw_ostream &);
156 virtual void mangleCXXVTable(const CXXRecordDecl *RD,
157 raw_ostream &);
158 virtual void mangleCXXVTT(const CXXRecordDecl *RD,
159 raw_ostream &);
Reid Kleckner90633022013-06-19 15:20:38 +0000160 virtual void mangleCXXVBTable(const CXXRecordDecl *Derived,
161 ArrayRef<const CXXRecordDecl *> BasePath,
162 raw_ostream &Out);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000163 virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
164 const CXXRecordDecl *Type,
165 raw_ostream &);
166 virtual void mangleCXXRTTI(QualType T, raw_ostream &);
167 virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
168 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
169 raw_ostream &);
170 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
171 raw_ostream &);
172 virtual void mangleReferenceTemporary(const clang::VarDecl *,
173 raw_ostream &);
174};
175
176}
177
178static bool isInCLinkageSpecification(const Decl *D) {
179 D = D->getCanonicalDecl();
180 for (const DeclContext *DC = D->getDeclContext();
181 !DC->isTranslationUnit(); DC = DC->getParent()) {
182 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
183 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
184 }
185
186 return false;
187}
188
189bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
190 // In C, functions with no attributes never need to be mangled. Fastpath them.
191 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
192 return false;
193
194 // Any decl can be declared with __asm("foo") on it, and this takes precedence
195 // over all other naming in the .o file.
196 if (D->hasAttr<AsmLabelAttr>())
197 return true;
198
199 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
200 // (always) as does passing a C++ member function and a function
201 // whose name is not a simple identifier.
202 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
203 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
204 !FD->getDeclName().isIdentifier()))
205 return true;
206
207 // Otherwise, no mangling is done outside C++ mode.
208 if (!getASTContext().getLangOpts().CPlusPlus)
209 return false;
210
211 // Variables at global scope with internal linkage are not mangled.
212 if (!FD) {
213 const DeclContext *DC = D->getDeclContext();
Rafael Espindola181e3ec2013-05-13 00:12:11 +0000214 if (DC->isTranslationUnit() && D->getFormalLinkage() == InternalLinkage)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000215 return false;
216 }
217
218 // C functions and "main" are not mangled.
219 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
220 return false;
221
222 return true;
223}
224
225void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
226 StringRef Prefix) {
227 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
228 // Therefore it's really important that we don't decorate the
229 // name with leading underscores or leading/trailing at signs. So, by
230 // default, we emit an asm marker at the start so we get the name right.
231 // Callers can override this with a custom prefix.
232
233 // Any decl can be declared with __asm("foo") on it, and this takes precedence
234 // over all other naming in the .o file.
235 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
236 // If we have an asm name, then we use it as the mangling.
237 Out << '\01' << ALA->getLabel();
238 return;
239 }
240
241 // <mangled-name> ::= ? <name> <type-encoding>
242 Out << Prefix;
243 mangleName(D);
244 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
245 mangleFunctionEncoding(FD);
246 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
247 mangleVariableEncoding(VD);
248 else {
249 // TODO: Fields? Can MSVC even mangle them?
250 // Issue a diagnostic for now.
251 DiagnosticsEngine &Diags = Context.getDiags();
252 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
253 "cannot mangle this declaration yet");
254 Diags.Report(D->getLocation(), DiagID)
255 << D->getSourceRange();
256 }
257}
258
259void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
260 // <type-encoding> ::= <function-class> <function-type>
261
Reid Klecknerf21818d2013-06-24 19:21:52 +0000262 // Since MSVC operates on the type as written and not the canonical type, it
263 // actually matters which decl we have here. MSVC appears to choose the
264 // first, since it is most likely to be the declaration in a header file.
265 FD = FD->getFirstDeclaration();
266
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000267 // Don't mangle in the type if this isn't a decl we should typically mangle.
268 if (!Context.shouldMangleDeclName(FD))
269 return;
270
271 // We should never ever see a FunctionNoProtoType at this point.
272 // We don't even know how to mangle their types anyway :).
Reid Klecknerf21818d2013-06-24 19:21:52 +0000273 TypeSourceInfo *TSI = FD->getTypeSourceInfo();
274 QualType T = TSI ? TSI->getType() : FD->getType();
275 const FunctionProtoType *FT = T->castAs<FunctionProtoType>();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000276
277 bool InStructor = false, InInstMethod = false;
278 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
279 if (MD) {
280 if (MD->isInstance())
281 InInstMethod = true;
282 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
283 InStructor = true;
284 }
285
286 // First, the function class.
287 mangleFunctionClass(FD);
288
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000289 mangleFunctionType(FT, FD, InStructor, InInstMethod);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000290}
291
292void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
293 // <type-encoding> ::= <storage-class> <variable-type>
294 // <storage-class> ::= 0 # private static member
295 // ::= 1 # protected static member
296 // ::= 2 # public static member
297 // ::= 3 # global
298 // ::= 4 # static local
299
300 // The first character in the encoding (after the name) is the storage class.
301 if (VD->isStaticDataMember()) {
302 // If it's a static member, it also encodes the access level.
303 switch (VD->getAccess()) {
304 default:
305 case AS_private: Out << '0'; break;
306 case AS_protected: Out << '1'; break;
307 case AS_public: Out << '2'; break;
308 }
309 }
310 else if (!VD->isStaticLocal())
311 Out << '3';
312 else
313 Out << '4';
314 // Now mangle the type.
315 // <variable-type> ::= <type> <cvr-qualifiers>
316 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
317 // Pointers and references are odd. The type of 'int * const foo;' gets
318 // mangled as 'QAHA' instead of 'PAHB', for example.
319 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
320 QualType Ty = TL.getType();
David Majnemer1c7a4092013-08-15 08:13:23 +0000321 if (Ty->isPointerType() || Ty->isReferenceType() ||
322 Ty->isMemberPointerType()) {
David Majnemer17ffbd02013-08-09 05:56:24 +0000323 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000324 if (PointersAre64Bit)
325 Out << 'E';
326 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) {
327 mangleQualifiers(MPT->getPointeeType().getQualifiers(), true);
328 // Member pointers are suffixed with a back reference to the member
329 // pointer's class name.
330 mangleName(MPT->getClass()->getAsCXXRecordDecl());
331 } else
332 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
David Majnemer17ffbd02013-08-09 05:56:24 +0000333 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000334 // Global arrays are funny, too.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000335 mangleDecayedArrayType(AT, true);
336 if (AT->getElementType()->isArrayType())
337 Out << 'A';
338 else
339 mangleQualifiers(Ty.getQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000340 } else {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +0000341 mangleType(Ty, TL.getSourceRange(), QMM_Drop);
David Majnemer1c7a4092013-08-15 08:13:23 +0000342 mangleQualifiers(Ty.getLocalQualifiers(), false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000343 }
344}
345
346void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
347 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
348 const DeclContext *DC = ND->getDeclContext();
349
350 // Always start with the unqualified name.
351 mangleUnqualifiedName(ND);
352
353 // If this is an extern variable declared locally, the relevant DeclContext
354 // is that of the containing namespace, or the translation unit.
355 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
356 while (!DC->isNamespace() && !DC->isTranslationUnit())
357 DC = DC->getParent();
358
359 manglePostfix(DC);
360
361 // Terminate the whole name with an '@'.
362 Out << '@';
363}
364
365void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
366 llvm::APSInt APSNumber(/*BitWidth=*/64, /*isUnsigned=*/false);
367 APSNumber = Number;
368 mangleNumber(APSNumber);
369}
370
371void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
372 // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
373 // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
374 // ::= [?] @ # 0 (alternate mangling, not emitted by VC)
375 if (Value.isSigned() && Value.isNegative()) {
376 Out << '?';
377 mangleNumber(llvm::APSInt(Value.abs()));
378 return;
379 }
380 llvm::APSInt Temp(Value);
381 // There's a special shorter mangling for 0, but Microsoft
382 // chose not to use it. Instead, 0 gets mangled as "A@". Oh well...
383 if (Value.uge(1) && Value.ule(10)) {
384 --Temp;
385 Temp.print(Out, false);
386 } else {
387 // We have to build up the encoding in reverse order, so it will come
388 // out right when we write it out.
389 char Encoding[64];
390 char *EndPtr = Encoding+sizeof(Encoding);
391 char *CurPtr = EndPtr;
392 llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned());
393 NibbleMask = 0xf;
394 do {
395 *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf);
396 Temp = Temp.lshr(4);
397 } while (Temp != 0);
398 Out.write(CurPtr, EndPtr-CurPtr);
399 Out << '@';
400 }
401}
402
403static const TemplateDecl *
Reid Klecknerf16216c2013-03-20 01:40:23 +0000404isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000405 // Check if we have a function template.
406 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
407 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000408 TemplateArgs = FD->getTemplateSpecializationArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000409 return TD;
410 }
411 }
412
413 // Check if we have a class template.
414 if (const ClassTemplateSpecializationDecl *Spec =
Reid Klecknerf16216c2013-03-20 01:40:23 +0000415 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
416 TemplateArgs = &Spec->getTemplateArgs();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000417 return Spec->getSpecializedTemplate();
418 }
419
420 return 0;
421}
422
423void
424MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
425 DeclarationName Name) {
426 // <unqualified-name> ::= <operator-name>
427 // ::= <ctor-dtor-name>
428 // ::= <source-name>
429 // ::= <template-name>
Reid Klecknerf16216c2013-03-20 01:40:23 +0000430
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000431 // Check if we have a template.
Reid Klecknerf16216c2013-03-20 01:40:23 +0000432 const TemplateArgumentList *TemplateArgs = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000433 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Reid Kleckner3be37d12013-07-13 00:43:39 +0000434 // Function templates aren't considered for name back referencing. This
435 // makes sense since function templates aren't likely to occur multiple
436 // times in a symbol.
437 // FIXME: Test alias template mangling with MSVC 2013.
438 if (!isa<ClassTemplateDecl>(TD)) {
439 mangleTemplateInstantiationName(TD, *TemplateArgs);
440 return;
441 }
442
443 // We have a class template.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000444 // Here comes the tricky thing: if we need to mangle something like
445 // void foo(A::X<Y>, B::X<Y>),
446 // the X<Y> part is aliased. However, if you need to mangle
447 // void foo(A::X<A::Y>, A::X<B::Y>),
448 // the A::X<> part is not aliased.
449 // That said, from the mangler's perspective we have a structure like this:
450 // namespace[s] -> type[ -> template-parameters]
451 // but from the Clang perspective we have
452 // type [ -> template-parameters]
453 // \-> namespace[s]
454 // What we do is we create a new mangler, mangle the same type (without
455 // a namespace suffix) using the extra mangler with back references
456 // disabled (to avoid infinite recursion) and then use the mangled type
457 // name as a key to check the mangling of different types for aliasing.
458
459 std::string BackReferenceKey;
460 BackRefMap::iterator Found;
461 if (UseNameBackReferences) {
462 llvm::raw_string_ostream Stream(BackReferenceKey);
463 MicrosoftCXXNameMangler Extra(Context, Stream);
464 Extra.disableBackReferences();
465 Extra.mangleUnqualifiedName(ND, Name);
466 Stream.flush();
467
468 Found = NameBackReferences.find(BackReferenceKey);
469 }
470 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000471 mangleTemplateInstantiationName(TD, *TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000472 if (UseNameBackReferences && NameBackReferences.size() < 10) {
473 size_t Size = NameBackReferences.size();
474 NameBackReferences[BackReferenceKey] = Size;
475 }
476 } else {
477 Out << Found->second;
478 }
479 return;
480 }
481
482 switch (Name.getNameKind()) {
483 case DeclarationName::Identifier: {
484 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
485 mangleSourceName(II);
486 break;
487 }
488
489 // Otherwise, an anonymous entity. We must have a declaration.
490 assert(ND && "mangling empty name without declaration");
491
492 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
493 if (NS->isAnonymousNamespace()) {
494 Out << "?A@";
495 break;
496 }
497 }
498
499 // We must have an anonymous struct.
500 const TagDecl *TD = cast<TagDecl>(ND);
501 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
502 assert(TD->getDeclContext() == D->getDeclContext() &&
503 "Typedef should not be in another decl context!");
504 assert(D->getDeclName().getAsIdentifierInfo() &&
505 "Typedef was not named!");
506 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
507 break;
508 }
509
510 // When VC encounters an anonymous type with no tag and no typedef,
David Majnemerec0258a2013-08-26 02:35:51 +0000511 // it literally emits '<unnamed-tag>@'.
512 Out << "<unnamed-tag>@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000513 break;
514 }
515
516 case DeclarationName::ObjCZeroArgSelector:
517 case DeclarationName::ObjCOneArgSelector:
518 case DeclarationName::ObjCMultiArgSelector:
519 llvm_unreachable("Can't mangle Objective-C selector names here!");
520
521 case DeclarationName::CXXConstructorName:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000522 if (ND == Structor) {
523 assert(StructorType == Ctor_Complete &&
524 "Should never be asked to mangle a ctor other than complete");
525 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000526 Out << "?0";
527 break;
528
529 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000530 if (ND == Structor)
531 // If the named decl is the C++ destructor we're mangling,
532 // use the type we were given.
533 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
534 else
Reid Klecknera4130ba2013-07-22 13:51:44 +0000535 // Otherwise, use the base destructor name. This is relevant if a
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000536 // class with a destructor is declared within a destructor.
Reid Klecknera4130ba2013-07-22 13:51:44 +0000537 mangleCXXDtorType(Dtor_Base);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000538 break;
539
540 case DeclarationName::CXXConversionFunctionName:
541 // <operator-name> ::= ?B # (cast)
542 // The target type is encoded as the return type.
543 Out << "?B";
544 break;
545
546 case DeclarationName::CXXOperatorName:
547 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
548 break;
549
550 case DeclarationName::CXXLiteralOperatorName: {
551 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
552 DiagnosticsEngine Diags = Context.getDiags();
553 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
554 "cannot mangle this literal operator yet");
555 Diags.Report(ND->getLocation(), DiagID);
556 break;
557 }
558
559 case DeclarationName::CXXUsingDirective:
560 llvm_unreachable("Can't mangle a using directive name!");
561 }
562}
563
564void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
565 bool NoFunction) {
566 // <postfix> ::= <unqualified-name> [<postfix>]
567 // ::= <substitution> [<postfix>]
568
569 if (!DC) return;
570
571 while (isa<LinkageSpecDecl>(DC))
572 DC = DC->getParent();
573
574 if (DC->isTranslationUnit())
575 return;
576
577 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Eli Friedmane5798892013-07-10 01:13:27 +0000578 DiagnosticsEngine Diags = Context.getDiags();
579 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
580 "cannot mangle a local inside this block yet");
581 Diags.Report(BD->getLocation(), DiagID);
582
583 // FIXME: This is completely, utterly, wrong; see ItaniumMangle
584 // for how this should be done.
585 Out << "__block_invoke" << Context.getBlockId(BD, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000586 Out << '@';
587 return manglePostfix(DC->getParent(), NoFunction);
Ben Langmuir524387a2013-05-09 19:17:11 +0000588 } else if (isa<CapturedDecl>(DC)) {
589 // Skip CapturedDecl context.
590 manglePostfix(DC->getParent(), NoFunction);
591 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000592 }
593
594 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
595 return;
596 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
597 mangleObjCMethodName(Method);
598 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
599 mangleLocalName(Func);
600 else {
601 mangleUnqualifiedName(cast<NamedDecl>(DC));
602 manglePostfix(DC->getParent(), NoFunction);
603 }
604}
605
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000606void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000607 // Microsoft uses the names on the case labels for these dtor variants. Clang
608 // uses the Itanium terminology internally. Everything in this ABI delegates
609 // towards the base dtor.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000610 switch (T) {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000611 // <operator-name> ::= ?1 # destructor
612 case Dtor_Base: Out << "?1"; return;
613 // <operator-name> ::= ?_D # vbase destructor
614 case Dtor_Complete: Out << "?_D"; return;
615 // <operator-name> ::= ?_G # scalar deleting destructor
616 case Dtor_Deleting: Out << "?_G"; return;
617 // <operator-name> ::= ?_E # vector deleting destructor
618 // FIXME: Add a vector deleting dtor type. It goes in the vtable, so we need
619 // it.
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000620 }
621 llvm_unreachable("Unsupported dtor type?");
622}
623
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000624void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
625 SourceLocation Loc) {
626 switch (OO) {
627 // ?0 # constructor
628 // ?1 # destructor
629 // <operator-name> ::= ?2 # new
630 case OO_New: Out << "?2"; break;
631 // <operator-name> ::= ?3 # delete
632 case OO_Delete: Out << "?3"; break;
633 // <operator-name> ::= ?4 # =
634 case OO_Equal: Out << "?4"; break;
635 // <operator-name> ::= ?5 # >>
636 case OO_GreaterGreater: Out << "?5"; break;
637 // <operator-name> ::= ?6 # <<
638 case OO_LessLess: Out << "?6"; break;
639 // <operator-name> ::= ?7 # !
640 case OO_Exclaim: Out << "?7"; break;
641 // <operator-name> ::= ?8 # ==
642 case OO_EqualEqual: Out << "?8"; break;
643 // <operator-name> ::= ?9 # !=
644 case OO_ExclaimEqual: Out << "?9"; break;
645 // <operator-name> ::= ?A # []
646 case OO_Subscript: Out << "?A"; break;
647 // ?B # conversion
648 // <operator-name> ::= ?C # ->
649 case OO_Arrow: Out << "?C"; break;
650 // <operator-name> ::= ?D # *
651 case OO_Star: Out << "?D"; break;
652 // <operator-name> ::= ?E # ++
653 case OO_PlusPlus: Out << "?E"; break;
654 // <operator-name> ::= ?F # --
655 case OO_MinusMinus: Out << "?F"; break;
656 // <operator-name> ::= ?G # -
657 case OO_Minus: Out << "?G"; break;
658 // <operator-name> ::= ?H # +
659 case OO_Plus: Out << "?H"; break;
660 // <operator-name> ::= ?I # &
661 case OO_Amp: Out << "?I"; break;
662 // <operator-name> ::= ?J # ->*
663 case OO_ArrowStar: Out << "?J"; break;
664 // <operator-name> ::= ?K # /
665 case OO_Slash: Out << "?K"; break;
666 // <operator-name> ::= ?L # %
667 case OO_Percent: Out << "?L"; break;
668 // <operator-name> ::= ?M # <
669 case OO_Less: Out << "?M"; break;
670 // <operator-name> ::= ?N # <=
671 case OO_LessEqual: Out << "?N"; break;
672 // <operator-name> ::= ?O # >
673 case OO_Greater: Out << "?O"; break;
674 // <operator-name> ::= ?P # >=
675 case OO_GreaterEqual: Out << "?P"; break;
676 // <operator-name> ::= ?Q # ,
677 case OO_Comma: Out << "?Q"; break;
678 // <operator-name> ::= ?R # ()
679 case OO_Call: Out << "?R"; break;
680 // <operator-name> ::= ?S # ~
681 case OO_Tilde: Out << "?S"; break;
682 // <operator-name> ::= ?T # ^
683 case OO_Caret: Out << "?T"; break;
684 // <operator-name> ::= ?U # |
685 case OO_Pipe: Out << "?U"; break;
686 // <operator-name> ::= ?V # &&
687 case OO_AmpAmp: Out << "?V"; break;
688 // <operator-name> ::= ?W # ||
689 case OO_PipePipe: Out << "?W"; break;
690 // <operator-name> ::= ?X # *=
691 case OO_StarEqual: Out << "?X"; break;
692 // <operator-name> ::= ?Y # +=
693 case OO_PlusEqual: Out << "?Y"; break;
694 // <operator-name> ::= ?Z # -=
695 case OO_MinusEqual: Out << "?Z"; break;
696 // <operator-name> ::= ?_0 # /=
697 case OO_SlashEqual: Out << "?_0"; break;
698 // <operator-name> ::= ?_1 # %=
699 case OO_PercentEqual: Out << "?_1"; break;
700 // <operator-name> ::= ?_2 # >>=
701 case OO_GreaterGreaterEqual: Out << "?_2"; break;
702 // <operator-name> ::= ?_3 # <<=
703 case OO_LessLessEqual: Out << "?_3"; break;
704 // <operator-name> ::= ?_4 # &=
705 case OO_AmpEqual: Out << "?_4"; break;
706 // <operator-name> ::= ?_5 # |=
707 case OO_PipeEqual: Out << "?_5"; break;
708 // <operator-name> ::= ?_6 # ^=
709 case OO_CaretEqual: Out << "?_6"; break;
710 // ?_7 # vftable
711 // ?_8 # vbtable
712 // ?_9 # vcall
713 // ?_A # typeof
714 // ?_B # local static guard
715 // ?_C # string
716 // ?_D # vbase destructor
717 // ?_E # vector deleting destructor
718 // ?_F # default constructor closure
719 // ?_G # scalar deleting destructor
720 // ?_H # vector constructor iterator
721 // ?_I # vector destructor iterator
722 // ?_J # vector vbase constructor iterator
723 // ?_K # virtual displacement map
724 // ?_L # eh vector constructor iterator
725 // ?_M # eh vector destructor iterator
726 // ?_N # eh vector vbase constructor iterator
727 // ?_O # copy constructor closure
728 // ?_P<name> # udt returning <name>
729 // ?_Q # <unknown>
730 // ?_R0 # RTTI Type Descriptor
731 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
732 // ?_R2 # RTTI Base Class Array
733 // ?_R3 # RTTI Class Hierarchy Descriptor
734 // ?_R4 # RTTI Complete Object Locator
735 // ?_S # local vftable
736 // ?_T # local vftable constructor closure
737 // <operator-name> ::= ?_U # new[]
738 case OO_Array_New: Out << "?_U"; break;
739 // <operator-name> ::= ?_V # delete[]
740 case OO_Array_Delete: Out << "?_V"; break;
741
742 case OO_Conditional: {
743 DiagnosticsEngine &Diags = Context.getDiags();
744 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
745 "cannot mangle this conditional operator yet");
746 Diags.Report(Loc, DiagID);
747 break;
748 }
749
750 case OO_None:
751 case NUM_OVERLOADED_OPERATORS:
752 llvm_unreachable("Not an overloaded operator");
753 }
754}
755
756void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
757 // <source name> ::= <identifier> @
758 std::string key = II->getNameStart();
759 BackRefMap::iterator Found;
760 if (UseNameBackReferences)
761 Found = NameBackReferences.find(key);
762 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
763 Out << II->getName() << '@';
764 if (UseNameBackReferences && NameBackReferences.size() < 10) {
765 size_t Size = NameBackReferences.size();
766 NameBackReferences[key] = Size;
767 }
768 } else {
769 Out << Found->second;
770 }
771}
772
773void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
774 Context.mangleObjCMethodName(MD, Out);
775}
776
777// Find out how many function decls live above this one and return an integer
778// suitable for use as the number in a numbered anonymous scope.
779// TODO: Memoize.
780static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
781 const DeclContext *DC = FD->getParent();
782 int level = 1;
783
784 while (DC && !DC->isTranslationUnit()) {
785 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
786 DC = DC->getParent();
787 }
788
789 return 2*level;
790}
791
792void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
793 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
794 // <numbered-anonymous-scope> ::= ? <number>
795 // Even though the name is rendered in reverse order (e.g.
796 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
797 // innermost. So a method bar in class C local to function foo gets mangled
798 // as something like:
799 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
800 // This is more apparent when you have a type nested inside a method of a
801 // type nested inside a function. A method baz in class D local to method
802 // bar of class C local to function foo gets mangled as:
803 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
804 // This scheme is general enough to support GCC-style nested
805 // functions. You could have a method baz of class C inside a function bar
806 // inside a function foo, like so:
807 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
808 int NestLevel = getLocalNestingLevel(FD);
809 Out << '?';
810 mangleNumber(NestLevel);
811 Out << '?';
812 mangle(FD, "?");
813}
814
815void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
816 const TemplateDecl *TD,
Reid Klecknerf16216c2013-03-20 01:40:23 +0000817 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000818 // <template-name> ::= <unscoped-template-name> <template-args>
819 // ::= <substitution>
820 // Always start with the unqualified name.
821
822 // Templates have their own context for back references.
823 ArgBackRefMap OuterArgsContext;
824 BackRefMap OuterTemplateContext;
825 NameBackReferences.swap(OuterTemplateContext);
826 TypeBackReferences.swap(OuterArgsContext);
827
828 mangleUnscopedTemplateName(TD);
Reid Klecknerf16216c2013-03-20 01:40:23 +0000829 mangleTemplateArgs(TD, TemplateArgs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000830
831 // Restore the previous back reference contexts.
832 NameBackReferences.swap(OuterTemplateContext);
833 TypeBackReferences.swap(OuterArgsContext);
834}
835
836void
837MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
838 // <unscoped-template-name> ::= ?$ <unqualified-name>
839 Out << "?$";
840 mangleUnqualifiedName(TD);
841}
842
843void
844MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
845 bool IsBoolean) {
846 // <integer-literal> ::= $0 <number>
847 Out << "$0";
848 // Make sure booleans are encoded as 0/1.
849 if (IsBoolean && Value.getBoolValue())
850 mangleNumber(1);
851 else
852 mangleNumber(Value);
853}
854
855void
856MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
857 // See if this is a constant expression.
858 llvm::APSInt Value;
859 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
860 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
861 return;
862 }
863
David Majnemerc80eb462013-08-13 06:32:20 +0000864 const CXXUuidofExpr *UE = 0;
865 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
866 if (UO->getOpcode() == UO_AddrOf)
867 UE = dyn_cast<CXXUuidofExpr>(UO->getSubExpr());
868 } else
869 UE = dyn_cast<CXXUuidofExpr>(E);
870
871 if (UE) {
872 // This CXXUuidofExpr is mangled as-if it were actually a VarDecl from
873 // const __s_GUID _GUID_{lower case UUID with underscores}
874 StringRef Uuid = UE->getUuidAsStringRef(Context.getASTContext());
875 std::string Name = "_GUID_" + Uuid.lower();
876 std::replace(Name.begin(), Name.end(), '-', '_');
877
David Majnemer26314e12013-08-13 09:17:25 +0000878 // If we had to peek through an address-of operator, treat this like we are
David Majnemerc80eb462013-08-13 06:32:20 +0000879 // dealing with a pointer type. Otherwise, treat it like a const reference.
880 //
881 // N.B. This matches up with the handling of TemplateArgument::Declaration
882 // in mangleTemplateArg
883 if (UE == E)
884 Out << "$E?";
885 else
886 Out << "$1?";
887 Out << Name << "@@3U__s_GUID@@B";
888 return;
889 }
890
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000891 // As bad as this diagnostic is, it's better than crashing.
892 DiagnosticsEngine &Diags = Context.getDiags();
893 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
894 "cannot yet mangle expression type %0");
895 Diags.Report(E->getExprLoc(), DiagID)
896 << E->getStmtClassName() << E->getSourceRange();
897}
898
899void
Reid Klecknerf16216c2013-03-20 01:40:23 +0000900MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateDecl *TD,
901 const TemplateArgumentList &TemplateArgs) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000902 // <template-args> ::= {<type> | <integer-literal>}+ @
903 unsigned NumTemplateArgs = TemplateArgs.size();
904 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
Reid Klecknerf16216c2013-03-20 01:40:23 +0000905 const TemplateArgument &TA = TemplateArgs[i];
Reid Kleckner5d90d182013-07-02 18:10:07 +0000906 mangleTemplateArg(TD, TA, i);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000907 }
908 Out << '@';
909}
910
Reid Kleckner5d90d182013-07-02 18:10:07 +0000911void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD,
912 const TemplateArgument &TA,
913 int ArgIndex) {
914 switch (TA.getKind()) {
915 case TemplateArgument::Null:
916 llvm_unreachable("Can't mangle null template arguments!");
917 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.
939 // FIXME: ArgIndex will be off, but we only use if for diagnostics that
940 // should ultimately be removed.
941 for (TemplateArgument::pack_iterator I = TA.pack_begin(), E = TA.pack_end();
942 I != E; ++I)
943 mangleTemplateArg(TD, *I, ArgIndex);
944 break;
945 case TemplateArgument::Template:
David Majnemer02c44f02013-08-05 22:26:46 +0000946 mangleType(cast<TagDecl>(
947 TA.getAsTemplate().getAsTemplateDecl()->getTemplatedDecl()));
948 break;
David Majnemer7802fc92013-08-05 21:33:59 +0000949 case TemplateArgument::TemplateExpansion: {
Reid Kleckner5d90d182013-07-02 18:10:07 +0000950 // Issue a diagnostic.
951 DiagnosticsEngine &Diags = Context.getDiags();
952 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
953 "cannot mangle template argument %0 of kind %select{ERROR|ERROR|"
954 "pointer/reference|nullptr|integral|template|template pack expansion|"
955 "ERROR|parameter pack}1 yet");
956 Diags.Report(TD->getLocation(), DiagID)
957 << ArgIndex + 1
958 << TA.getKind()
959 << TD->getSourceRange();
960 }
961 }
962}
963
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000964void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
965 bool IsMember) {
966 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
967 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
968 // 'I' means __restrict (32/64-bit).
969 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
970 // keyword!
971 // <base-cvr-qualifiers> ::= A # near
972 // ::= B # near const
973 // ::= C # near volatile
974 // ::= D # near const volatile
975 // ::= E # far (16-bit)
976 // ::= F # far const (16-bit)
977 // ::= G # far volatile (16-bit)
978 // ::= H # far const volatile (16-bit)
979 // ::= I # huge (16-bit)
980 // ::= J # huge const (16-bit)
981 // ::= K # huge volatile (16-bit)
982 // ::= L # huge const volatile (16-bit)
983 // ::= M <basis> # based
984 // ::= N <basis> # based const
985 // ::= O <basis> # based volatile
986 // ::= P <basis> # based const volatile
987 // ::= Q # near member
988 // ::= R # near const member
989 // ::= S # near volatile member
990 // ::= T # near const volatile member
991 // ::= U # far member (16-bit)
992 // ::= V # far const member (16-bit)
993 // ::= W # far volatile member (16-bit)
994 // ::= X # far const volatile member (16-bit)
995 // ::= Y # huge member (16-bit)
996 // ::= Z # huge const member (16-bit)
997 // ::= 0 # huge volatile member (16-bit)
998 // ::= 1 # huge const volatile member (16-bit)
999 // ::= 2 <basis> # based member
1000 // ::= 3 <basis> # based const member
1001 // ::= 4 <basis> # based volatile member
1002 // ::= 5 <basis> # based const volatile member
1003 // ::= 6 # near function (pointers only)
1004 // ::= 7 # far function (pointers only)
1005 // ::= 8 # near method (pointers only)
1006 // ::= 9 # far method (pointers only)
1007 // ::= _A <basis> # based function (pointers only)
1008 // ::= _B <basis> # based function (far?) (pointers only)
1009 // ::= _C <basis> # based method (pointers only)
1010 // ::= _D <basis> # based method (far?) (pointers only)
1011 // ::= _E # block (Clang)
1012 // <basis> ::= 0 # __based(void)
1013 // ::= 1 # __based(segment)?
1014 // ::= 2 <name> # __based(name)
1015 // ::= 3 # ?
1016 // ::= 4 # ?
1017 // ::= 5 # not really based
1018 bool HasConst = Quals.hasConst(),
1019 HasVolatile = Quals.hasVolatile();
David Majnemerc0e64f32013-08-05 22:43:06 +00001020
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001021 if (!IsMember) {
1022 if (HasConst && HasVolatile) {
1023 Out << 'D';
1024 } else if (HasVolatile) {
1025 Out << 'C';
1026 } else if (HasConst) {
1027 Out << 'B';
1028 } else {
1029 Out << 'A';
1030 }
1031 } else {
1032 if (HasConst && HasVolatile) {
1033 Out << 'T';
1034 } else if (HasVolatile) {
1035 Out << 'S';
1036 } else if (HasConst) {
1037 Out << 'R';
1038 } else {
1039 Out << 'Q';
1040 }
1041 }
1042
1043 // FIXME: For now, just drop all extension qualifiers on the floor.
1044}
1045
1046void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
1047 // <pointer-cvr-qualifiers> ::= P # no qualifiers
1048 // ::= Q # const
1049 // ::= R # volatile
1050 // ::= S # const volatile
1051 bool HasConst = Quals.hasConst(),
1052 HasVolatile = Quals.hasVolatile();
1053 if (HasConst && HasVolatile) {
1054 Out << 'S';
1055 } else if (HasVolatile) {
1056 Out << 'R';
1057 } else if (HasConst) {
1058 Out << 'Q';
1059 } else {
1060 Out << 'P';
1061 }
1062}
1063
1064void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
1065 SourceRange Range) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001066 // MSVC will backreference two canonically equivalent types that have slightly
1067 // different manglings when mangled alone.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001068 void *TypePtr = getASTContext().getCanonicalType(T).getAsOpaquePtr();
1069 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
1070
1071 if (Found == TypeBackReferences.end()) {
1072 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
1073
Reid Klecknerf21818d2013-06-24 19:21:52 +00001074 if (const DecayedType *DT = T->getAs<DecayedType>()) {
1075 QualType OT = DT->getOriginalType();
1076 if (const ArrayType *AT = getASTContext().getAsArrayType(OT)) {
1077 mangleDecayedArrayType(AT, false);
1078 } else if (const FunctionType *FT = OT->getAs<FunctionType>()) {
1079 Out << "P6";
1080 mangleFunctionType(FT, 0, false, false);
1081 } else {
1082 llvm_unreachable("unexpected decayed type");
1083 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001084 } else {
1085 mangleType(T, Range, QMM_Drop);
1086 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001087
1088 // See if it's worth creating a back reference.
1089 // Only types longer than 1 character are considered
1090 // and only 10 back references slots are available:
1091 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
1092 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
1093 size_t Size = TypeBackReferences.size();
1094 TypeBackReferences[TypePtr] = Size;
1095 }
1096 } else {
1097 Out << Found->second;
1098 }
1099}
1100
1101void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001102 QualifierMangleMode QMM) {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001103 // Don't use the canonical types. MSVC includes things like 'const' on
1104 // pointer arguments to function pointers that canonicalization strips away.
1105 T = T.getDesugaredType(getASTContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001106 Qualifiers Quals = T.getLocalQualifiers();
Reid Klecknerf21818d2013-06-24 19:21:52 +00001107 if (const ArrayType *AT = getASTContext().getAsArrayType(T)) {
1108 // If there were any Quals, getAsArrayType() pushed them onto the array
1109 // element type.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001110 if (QMM == QMM_Mangle)
1111 Out << 'A';
1112 else if (QMM == QMM_Escape || QMM == QMM_Result)
1113 Out << "$$B";
Reid Klecknerf21818d2013-06-24 19:21:52 +00001114 mangleArrayType(AT);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001115 return;
1116 }
1117
1118 bool IsPointer = T->isAnyPointerType() || T->isMemberPointerType() ||
1119 T->isBlockPointerType();
1120
1121 switch (QMM) {
1122 case QMM_Drop:
1123 break;
1124 case QMM_Mangle:
1125 if (const FunctionType *FT = dyn_cast<FunctionType>(T)) {
1126 Out << '6';
1127 mangleFunctionType(FT, 0, false, false);
1128 return;
1129 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001130 mangleQualifiers(Quals, false);
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001131 break;
1132 case QMM_Escape:
1133 if (!IsPointer && Quals) {
1134 Out << "$$C";
1135 mangleQualifiers(Quals, false);
1136 }
1137 break;
1138 case QMM_Result:
1139 if ((!IsPointer && Quals) || isa<TagType>(T)) {
1140 Out << '?';
1141 mangleQualifiers(Quals, false);
1142 }
1143 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001144 }
1145
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001146 // We have to mangle these now, while we still have enough information.
1147 if (IsPointer)
1148 manglePointerQualifiers(Quals);
1149 const Type *ty = T.getTypePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001150
1151 switch (ty->getTypeClass()) {
1152#define ABSTRACT_TYPE(CLASS, PARENT)
1153#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1154 case Type::CLASS: \
1155 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1156 return;
1157#define TYPE(CLASS, PARENT) \
1158 case Type::CLASS: \
1159 mangleType(cast<CLASS##Type>(ty), Range); \
1160 break;
1161#include "clang/AST/TypeNodes.def"
1162#undef ABSTRACT_TYPE
1163#undef NON_CANONICAL_TYPE
1164#undef TYPE
1165 }
1166}
1167
1168void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1169 SourceRange Range) {
1170 // <type> ::= <builtin-type>
1171 // <builtin-type> ::= X # void
1172 // ::= C # signed char
1173 // ::= D # char
1174 // ::= E # unsigned char
1175 // ::= F # short
1176 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1177 // ::= H # int
1178 // ::= I # unsigned int
1179 // ::= J # long
1180 // ::= K # unsigned long
1181 // L # <none>
1182 // ::= M # float
1183 // ::= N # double
1184 // ::= O # long double (__float80 is mangled differently)
1185 // ::= _J # long long, __int64
1186 // ::= _K # unsigned long long, __int64
1187 // ::= _L # __int128
1188 // ::= _M # unsigned __int128
1189 // ::= _N # bool
1190 // _O # <array in parameter>
1191 // ::= _T # __float80 (Intel)
1192 // ::= _W # wchar_t
1193 // ::= _Z # __float80 (Digital Mars)
1194 switch (T->getKind()) {
1195 case BuiltinType::Void: Out << 'X'; break;
1196 case BuiltinType::SChar: Out << 'C'; break;
1197 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1198 case BuiltinType::UChar: Out << 'E'; break;
1199 case BuiltinType::Short: Out << 'F'; break;
1200 case BuiltinType::UShort: Out << 'G'; break;
1201 case BuiltinType::Int: Out << 'H'; break;
1202 case BuiltinType::UInt: Out << 'I'; break;
1203 case BuiltinType::Long: Out << 'J'; break;
1204 case BuiltinType::ULong: Out << 'K'; break;
1205 case BuiltinType::Float: Out << 'M'; break;
1206 case BuiltinType::Double: Out << 'N'; break;
1207 // TODO: Determine size and mangle accordingly
1208 case BuiltinType::LongDouble: Out << 'O'; break;
1209 case BuiltinType::LongLong: Out << "_J"; break;
1210 case BuiltinType::ULongLong: Out << "_K"; break;
1211 case BuiltinType::Int128: Out << "_L"; break;
1212 case BuiltinType::UInt128: Out << "_M"; break;
1213 case BuiltinType::Bool: Out << "_N"; break;
1214 case BuiltinType::WChar_S:
1215 case BuiltinType::WChar_U: Out << "_W"; break;
1216
1217#define BUILTIN_TYPE(Id, SingletonId)
1218#define PLACEHOLDER_TYPE(Id, SingletonId) \
1219 case BuiltinType::Id:
1220#include "clang/AST/BuiltinTypes.def"
1221 case BuiltinType::Dependent:
1222 llvm_unreachable("placeholder types shouldn't get to name mangling");
1223
1224 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1225 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1226 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001227
1228 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1229 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1230 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1231 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1232 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1233 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001234 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001235 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001236
1237 case BuiltinType::NullPtr: Out << "$$T"; break;
1238
1239 case BuiltinType::Char16:
1240 case BuiltinType::Char32:
1241 case BuiltinType::Half: {
1242 DiagnosticsEngine &Diags = Context.getDiags();
1243 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1244 "cannot mangle this built-in %0 type yet");
1245 Diags.Report(Range.getBegin(), DiagID)
1246 << T->getName(Context.getASTContext().getPrintingPolicy())
1247 << Range;
1248 break;
1249 }
1250 }
1251}
1252
1253// <type> ::= <function-type>
1254void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1255 SourceRange) {
1256 // Structors only appear in decls, so at this point we know it's not a
1257 // structor type.
1258 // FIXME: This may not be lambda-friendly.
1259 Out << "$$A6";
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001260 mangleFunctionType(T, NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001261}
1262void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1263 SourceRange) {
1264 llvm_unreachable("Can't mangle K&R function prototypes");
1265}
1266
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001267void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
1268 const FunctionDecl *D,
1269 bool IsStructor,
1270 bool IsInstMethod) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001271 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1272 // <return-type> <argument-list> <throw-spec>
1273 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1274
Reid Klecknerf21818d2013-06-24 19:21:52 +00001275 SourceRange Range;
1276 if (D) Range = D->getSourceRange();
1277
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001278 // If this is a C++ instance method, mangle the CVR qualifiers for the
1279 // this pointer.
David Majnemer1c7a4092013-08-15 08:13:23 +00001280 if (IsInstMethod) {
1281 if (PointersAre64Bit)
1282 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001283 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
David Majnemer1c7a4092013-08-15 08:13:23 +00001284 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001285
1286 mangleCallingConvention(T, IsInstMethod);
1287
1288 // <return-type> ::= <type>
1289 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001290 if (IsStructor) {
1291 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1292 StructorType == Dtor_Deleting) {
1293 // The scalar deleting destructor takes an extra int argument.
1294 // However, the FunctionType generated has 0 arguments.
1295 // FIXME: This is a temporary hack.
1296 // Maybe should fix the FunctionType creation instead?
1297 Out << "PAXI@Z";
1298 return;
1299 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001300 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001301 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001302 QualType ResultType = Proto->getResultType();
1303 if (ResultType->isVoidType())
1304 ResultType = ResultType.getUnqualifiedType();
1305 mangleType(ResultType, Range, QMM_Result);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001306 }
1307
1308 // <argument-list> ::= X # void
1309 // ::= <type>+ @
1310 // ::= <type>* Z # varargs
1311 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1312 Out << 'X';
1313 } else {
Reid Klecknerf21818d2013-06-24 19:21:52 +00001314 // Happens for function pointer type arguments for example.
1315 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1316 ArgEnd = Proto->arg_type_end();
1317 Arg != ArgEnd; ++Arg)
1318 mangleArgumentType(*Arg, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001319 // <builtin-type> ::= Z # ellipsis
1320 if (Proto->isVariadic())
1321 Out << 'Z';
1322 else
1323 Out << '@';
1324 }
1325
1326 mangleThrowSpecification(Proto);
1327}
1328
1329void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001330 // <function-class> ::= <member-function> E? # E designates a 64-bit 'this'
1331 // # pointer. in 64-bit mode *all*
1332 // # 'this' pointers are 64-bit.
1333 // ::= <global-function>
1334 // <member-function> ::= A # private: near
1335 // ::= B # private: far
1336 // ::= C # private: static near
1337 // ::= D # private: static far
1338 // ::= E # private: virtual near
1339 // ::= F # private: virtual far
1340 // ::= G # private: thunk near
1341 // ::= H # private: thunk far
1342 // ::= I # protected: near
1343 // ::= J # protected: far
1344 // ::= K # protected: static near
1345 // ::= L # protected: static far
1346 // ::= M # protected: virtual near
1347 // ::= N # protected: virtual far
1348 // ::= O # protected: thunk near
1349 // ::= P # protected: thunk far
1350 // ::= Q # public: near
1351 // ::= R # public: far
1352 // ::= S # public: static near
1353 // ::= T # public: static far
1354 // ::= U # public: virtual near
1355 // ::= V # public: virtual far
1356 // ::= W # public: thunk near
1357 // ::= X # public: thunk far
1358 // <global-function> ::= Y # global near
1359 // ::= Z # global far
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001360 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1361 switch (MD->getAccess()) {
1362 default:
1363 case AS_private:
1364 if (MD->isStatic())
1365 Out << 'C';
1366 else if (MD->isVirtual())
1367 Out << 'E';
1368 else
1369 Out << 'A';
1370 break;
1371 case AS_protected:
1372 if (MD->isStatic())
1373 Out << 'K';
1374 else if (MD->isVirtual())
1375 Out << 'M';
1376 else
1377 Out << 'I';
1378 break;
1379 case AS_public:
1380 if (MD->isStatic())
1381 Out << 'S';
1382 else if (MD->isVirtual())
1383 Out << 'U';
1384 else
1385 Out << 'Q';
1386 }
1387 } else
1388 Out << 'Y';
1389}
1390void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
1391 bool IsInstMethod) {
1392 // <calling-convention> ::= A # __cdecl
1393 // ::= B # __export __cdecl
1394 // ::= C # __pascal
1395 // ::= D # __export __pascal
1396 // ::= E # __thiscall
1397 // ::= F # __export __thiscall
1398 // ::= G # __stdcall
1399 // ::= H # __export __stdcall
1400 // ::= I # __fastcall
1401 // ::= J # __export __fastcall
1402 // The 'export' calling conventions are from a bygone era
1403 // (*cough*Win16*cough*) when functions were declared for export with
1404 // that keyword. (It didn't actually export them, it just made them so
1405 // that they could be in a DLL and somebody from another module could call
1406 // them.)
1407 CallingConv CC = T->getCallConv();
1408 if (CC == CC_Default) {
1409 if (IsInstMethod) {
1410 const FunctionProtoType *FPT =
1411 T->getCanonicalTypeUnqualified().castAs<FunctionProtoType>();
1412 bool isVariadic = FPT->isVariadic();
1413 CC = getASTContext().getDefaultCXXMethodCallConv(isVariadic);
1414 } else {
1415 CC = CC_C;
1416 }
1417 }
1418 switch (CC) {
1419 default:
1420 llvm_unreachable("Unsupported CC for mangling");
1421 case CC_Default:
1422 case CC_C: Out << 'A'; break;
1423 case CC_X86Pascal: Out << 'C'; break;
1424 case CC_X86ThisCall: Out << 'E'; break;
1425 case CC_X86StdCall: Out << 'G'; break;
1426 case CC_X86FastCall: Out << 'I'; break;
1427 }
1428}
1429void MicrosoftCXXNameMangler::mangleThrowSpecification(
1430 const FunctionProtoType *FT) {
1431 // <throw-spec> ::= Z # throw(...) (default)
1432 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1433 // ::= <type>+
1434 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1435 // all actually mangled as 'Z'. (They're ignored because their associated
1436 // functionality isn't implemented, and probably never will be.)
1437 Out << 'Z';
1438}
1439
1440void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1441 SourceRange Range) {
1442 // Probably should be mangled as a template instantiation; need to see what
1443 // VC does first.
1444 DiagnosticsEngine &Diags = Context.getDiags();
1445 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1446 "cannot mangle this unresolved dependent type yet");
1447 Diags.Report(Range.getBegin(), DiagID)
1448 << Range;
1449}
1450
1451// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1452// <union-type> ::= T <name>
1453// <struct-type> ::= U <name>
1454// <class-type> ::= V <name>
1455// <enum-type> ::= W <size> <name>
1456void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001457 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001458}
1459void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
David Majnemer02c44f02013-08-05 22:26:46 +00001460 mangleType(cast<TagType>(T)->getDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001461}
David Majnemer02c44f02013-08-05 22:26:46 +00001462void MicrosoftCXXNameMangler::mangleType(const TagDecl *TD) {
1463 switch (TD->getTagKind()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001464 case TTK_Union:
1465 Out << 'T';
1466 break;
1467 case TTK_Struct:
1468 case TTK_Interface:
1469 Out << 'U';
1470 break;
1471 case TTK_Class:
1472 Out << 'V';
1473 break;
1474 case TTK_Enum:
1475 Out << 'W';
1476 Out << getASTContext().getTypeSizeInChars(
David Majnemer02c44f02013-08-05 22:26:46 +00001477 cast<EnumDecl>(TD)->getIntegerType()).getQuantity();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001478 break;
1479 }
David Majnemer02c44f02013-08-05 22:26:46 +00001480 mangleName(TD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001481}
1482
1483// <type> ::= <array-type>
1484// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1485// [Y <dimension-count> <dimension>+]
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001486// <element-type> # as global, E is never required
1487// ::= Q E? <cvr-qualifiers> [Y <dimension-count> <dimension>+]
1488// <element-type> # as param, E is required for 64-bit
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001489// It's supposed to be the other way around, but for some strange reason, it
1490// isn't. Today this behavior is retained for the sole purpose of backwards
1491// compatibility.
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001492void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T,
1493 bool IsGlobal) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001494 // This isn't a recursive mangling, so now we have to do it all in this
1495 // one call.
1496 if (IsGlobal) {
1497 manglePointerQualifiers(T->getElementType().getQualifiers());
1498 } else {
1499 Out << 'Q';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001500 if (PointersAre64Bit)
1501 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001502 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001503 mangleType(T->getElementType(), SourceRange());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001504}
1505void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1506 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001507 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001508}
1509void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1510 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001511 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001512}
1513void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1514 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001515 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001516}
1517void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1518 SourceRange) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001519 llvm_unreachable("Should have been special cased");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001520}
Reid Klecknerf21818d2013-06-24 19:21:52 +00001521void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001522 QualType ElementTy(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001523 SmallVector<llvm::APInt, 3> Dimensions;
1524 for (;;) {
1525 if (const ConstantArrayType *CAT =
1526 getASTContext().getAsConstantArrayType(ElementTy)) {
1527 Dimensions.push_back(CAT->getSize());
1528 ElementTy = CAT->getElementType();
1529 } else if (ElementTy->isVariableArrayType()) {
1530 const VariableArrayType *VAT =
1531 getASTContext().getAsVariableArrayType(ElementTy);
1532 DiagnosticsEngine &Diags = Context.getDiags();
1533 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1534 "cannot mangle this variable-length array yet");
1535 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1536 << VAT->getBracketsRange();
1537 return;
1538 } else if (ElementTy->isDependentSizedArrayType()) {
1539 // The dependent expression has to be folded into a constant (TODO).
1540 const DependentSizedArrayType *DSAT =
1541 getASTContext().getAsDependentSizedArrayType(ElementTy);
1542 DiagnosticsEngine &Diags = Context.getDiags();
1543 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1544 "cannot mangle this dependent-length array yet");
1545 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1546 << DSAT->getBracketsRange();
1547 return;
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001548 } else if (const IncompleteArrayType *IAT =
1549 getASTContext().getAsIncompleteArrayType(ElementTy)) {
1550 Dimensions.push_back(llvm::APInt(32, 0));
1551 ElementTy = IAT->getElementType();
1552 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001553 else break;
1554 }
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001555 Out << 'Y';
1556 // <dimension-count> ::= <number> # number of extra dimensions
1557 mangleNumber(Dimensions.size());
1558 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim)
1559 mangleNumber(Dimensions[Dim].getLimitedValue());
Reid Klecknerf21818d2013-06-24 19:21:52 +00001560 mangleType(ElementTy, SourceRange(), QMM_Escape);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001561}
1562
1563// <type> ::= <pointer-to-member-type>
1564// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1565// <class name> <type>
1566void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1567 SourceRange Range) {
1568 QualType PointeeType = T->getPointeeType();
1569 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1570 Out << '8';
1571 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001572 mangleFunctionType(FPT, NULL, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001573 } else {
David Majnemer1c7a4092013-08-15 08:13:23 +00001574 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1575 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001576 mangleQualifiers(PointeeType.getQualifiers(), true);
1577 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001578 mangleType(PointeeType, Range, QMM_Drop);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001579 }
1580}
1581
1582void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1583 SourceRange Range) {
1584 DiagnosticsEngine &Diags = Context.getDiags();
1585 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1586 "cannot mangle this template type parameter type yet");
1587 Diags.Report(Range.getBegin(), DiagID)
1588 << Range;
1589}
1590
1591void MicrosoftCXXNameMangler::mangleType(
1592 const SubstTemplateTypeParmPackType *T,
1593 SourceRange Range) {
1594 DiagnosticsEngine &Diags = Context.getDiags();
1595 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1596 "cannot mangle this substituted parameter pack yet");
1597 Diags.Report(Range.getBegin(), DiagID)
1598 << Range;
1599}
1600
1601// <type> ::= <pointer-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001602// <pointer-type> ::= E? <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1603// # the E is required for 64-bit non static pointers
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001604void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1605 SourceRange Range) {
1606 QualType PointeeTy = T->getPointeeType();
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001607 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1608 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001609 mangleType(PointeeTy, Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001610}
1611void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1612 SourceRange Range) {
1613 // Object pointers never have qualifiers.
1614 Out << 'A';
David Majnemer1c7a4092013-08-15 08:13:23 +00001615 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1616 Out << 'E';
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001617 mangleType(T->getPointeeType(), Range);
1618}
1619
1620// <type> ::= <reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001621// <reference-type> ::= A E? <cvr-qualifiers> <type>
1622// # the E is required for 64-bit non static lvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001623void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1624 SourceRange Range) {
1625 Out << 'A';
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001626 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1627 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001628 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001629}
1630
1631// <type> ::= <r-value-reference-type>
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001632// <r-value-reference-type> ::= $$Q E? <cvr-qualifiers> <type>
1633// # the E is required for 64-bit non static rvalue references
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001634void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1635 SourceRange Range) {
1636 Out << "$$Q";
Reid Klecknerd6a08d12013-05-14 20:30:42 +00001637 if (PointersAre64Bit && !T->getPointeeType()->isFunctionType())
1638 Out << 'E';
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001639 mangleType(T->getPointeeType(), Range);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001640}
1641
1642void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1643 SourceRange Range) {
1644 DiagnosticsEngine &Diags = Context.getDiags();
1645 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1646 "cannot mangle this complex number type yet");
1647 Diags.Report(Range.getBegin(), DiagID)
1648 << Range;
1649}
1650
1651void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1652 SourceRange Range) {
Reid Kleckner1232e272013-03-26 16:56:59 +00001653 const BuiltinType *ET = T->getElementType()->getAs<BuiltinType>();
1654 assert(ET && "vectors with non-builtin elements are unsupported");
1655 uint64_t Width = getASTContext().getTypeSize(T);
1656 // Pattern match exactly the typedefs in our intrinsic headers. Anything that
1657 // doesn't match the Intel types uses a custom mangling below.
1658 bool IntelVector = true;
1659 if (Width == 64 && ET->getKind() == BuiltinType::LongLong) {
1660 Out << "T__m64";
1661 } else if (Width == 128 || Width == 256) {
1662 if (ET->getKind() == BuiltinType::Float)
1663 Out << "T__m" << Width;
1664 else if (ET->getKind() == BuiltinType::LongLong)
1665 Out << "T__m" << Width << 'i';
1666 else if (ET->getKind() == BuiltinType::Double)
1667 Out << "U__m" << Width << 'd';
1668 else
1669 IntelVector = false;
1670 } else {
1671 IntelVector = false;
1672 }
1673
1674 if (!IntelVector) {
1675 // The MS ABI doesn't have a special mangling for vector types, so we define
1676 // our own mangling to handle uses of __vector_size__ on user-specified
1677 // types, and for extensions like __v4sf.
1678 Out << "T__clang_vec" << T->getNumElements() << '_';
1679 mangleType(ET, Range);
1680 }
1681
1682 Out << "@@";
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001683}
Reid Kleckner1232e272013-03-26 16:56:59 +00001684
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001685void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1686 SourceRange Range) {
1687 DiagnosticsEngine &Diags = Context.getDiags();
1688 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1689 "cannot mangle this extended vector type yet");
1690 Diags.Report(Range.getBegin(), DiagID)
1691 << Range;
1692}
1693void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1694 SourceRange Range) {
1695 DiagnosticsEngine &Diags = Context.getDiags();
1696 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1697 "cannot mangle this dependent-sized extended vector type yet");
1698 Diags.Report(Range.getBegin(), DiagID)
1699 << Range;
1700}
1701
1702void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1703 SourceRange) {
1704 // ObjC interfaces have structs underlying them.
1705 Out << 'U';
1706 mangleName(T->getDecl());
1707}
1708
1709void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1710 SourceRange Range) {
1711 // We don't allow overloading by different protocol qualification,
1712 // so mangling them isn't necessary.
1713 mangleType(T->getBaseType(), Range);
1714}
1715
1716void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1717 SourceRange Range) {
1718 Out << "_E";
1719
1720 QualType pointee = T->getPointeeType();
Peter Collingbourneb70d1c32013-04-25 04:25:40 +00001721 mangleFunctionType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001722}
1723
David Majnemer360d23e2013-08-16 08:29:13 +00001724void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
1725 SourceRange) {
1726 llvm_unreachable("Cannot mangle injected class name type.");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001727}
1728
1729void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1730 SourceRange Range) {
1731 DiagnosticsEngine &Diags = Context.getDiags();
1732 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1733 "cannot mangle this template specialization type yet");
1734 Diags.Report(Range.getBegin(), DiagID)
1735 << Range;
1736}
1737
1738void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1739 SourceRange Range) {
1740 DiagnosticsEngine &Diags = Context.getDiags();
1741 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1742 "cannot mangle this dependent name type yet");
1743 Diags.Report(Range.getBegin(), DiagID)
1744 << Range;
1745}
1746
1747void MicrosoftCXXNameMangler::mangleType(
1748 const DependentTemplateSpecializationType *T,
1749 SourceRange Range) {
1750 DiagnosticsEngine &Diags = Context.getDiags();
1751 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1752 "cannot mangle this dependent template specialization type yet");
1753 Diags.Report(Range.getBegin(), DiagID)
1754 << Range;
1755}
1756
1757void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1758 SourceRange Range) {
1759 DiagnosticsEngine &Diags = Context.getDiags();
1760 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1761 "cannot mangle this pack expansion yet");
1762 Diags.Report(Range.getBegin(), DiagID)
1763 << Range;
1764}
1765
1766void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1767 SourceRange Range) {
1768 DiagnosticsEngine &Diags = Context.getDiags();
1769 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1770 "cannot mangle this typeof(type) yet");
1771 Diags.Report(Range.getBegin(), DiagID)
1772 << Range;
1773}
1774
1775void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1776 SourceRange Range) {
1777 DiagnosticsEngine &Diags = Context.getDiags();
1778 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1779 "cannot mangle this typeof(expression) yet");
1780 Diags.Report(Range.getBegin(), DiagID)
1781 << Range;
1782}
1783
1784void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1785 SourceRange Range) {
1786 DiagnosticsEngine &Diags = Context.getDiags();
1787 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1788 "cannot mangle this decltype() yet");
1789 Diags.Report(Range.getBegin(), DiagID)
1790 << Range;
1791}
1792
1793void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1794 SourceRange Range) {
1795 DiagnosticsEngine &Diags = Context.getDiags();
1796 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1797 "cannot mangle this unary transform type yet");
1798 Diags.Report(Range.getBegin(), DiagID)
1799 << Range;
1800}
1801
1802void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1803 DiagnosticsEngine &Diags = Context.getDiags();
1804 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1805 "cannot mangle this 'auto' type yet");
1806 Diags.Report(Range.getBegin(), DiagID)
1807 << Range;
1808}
1809
1810void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1811 SourceRange Range) {
1812 DiagnosticsEngine &Diags = Context.getDiags();
1813 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1814 "cannot mangle this C11 atomic type yet");
1815 Diags.Report(Range.getBegin(), DiagID)
1816 << Range;
1817}
1818
1819void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1820 raw_ostream &Out) {
1821 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1822 "Invalid mangleName() call, argument is not a variable or function!");
1823 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1824 "Invalid mangleName() call on 'structor decl!");
1825
1826 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1827 getASTContext().getSourceManager(),
1828 "Mangling declaration");
1829
1830 MicrosoftCXXNameMangler Mangler(*this, Out);
1831 return Mangler.mangle(D);
1832}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001833
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001834void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1835 const ThunkInfo &Thunk,
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001836 raw_ostream &Out) {
1837 // FIXME: this is not yet a complete implementation, but merely a
1838 // reasonably-working stub to avoid crashing when required to emit a thunk.
1839 MicrosoftCXXNameMangler Mangler(*this, Out);
1840 Out << "\01?";
1841 Mangler.mangleName(MD);
1842 if (Thunk.This.NonVirtual != 0) {
1843 // FIXME: add support for protected/private or use mangleFunctionClass.
1844 Out << "W";
1845 llvm::APSInt APSNumber(/*BitWidth=*/32 /*FIXME: check on x64*/,
1846 /*isUnsigned=*/true);
1847 APSNumber = -Thunk.This.NonVirtual;
1848 Mangler.mangleNumber(APSNumber);
1849 } else {
1850 // FIXME: add support for protected/private or use mangleFunctionClass.
1851 Out << "Q";
1852 }
1853 // FIXME: mangle return adjustment? Most likely includes using an overridee FPT?
1854 Mangler.mangleFunctionType(MD->getType()->castAs<FunctionProtoType>(), MD, false, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001855}
Timur Iskhodzhanov635de282013-07-30 09:46:19 +00001856
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001857void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1858 CXXDtorType Type,
1859 const ThisAdjustment &,
1860 raw_ostream &) {
1861 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1862 "cannot mangle thunk for this destructor yet");
1863 getDiags().Report(DD->getLocation(), DiagID);
1864}
Reid Kleckner90633022013-06-19 15:20:38 +00001865
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001866void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1867 raw_ostream &Out) {
Reid Kleckner90633022013-06-19 15:20:38 +00001868 // <mangled-name> ::= ?_7 <class-name> <storage-class>
1869 // <cvr-qualifiers> [<name>] @
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001870 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
Reid Kleckner90633022013-06-19 15:20:38 +00001871 // is always '6' for vftables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001872 MicrosoftCXXNameMangler Mangler(*this, Out);
1873 Mangler.getStream() << "\01??_7";
1874 Mangler.mangleName(RD);
Reid Kleckner90633022013-06-19 15:20:38 +00001875 Mangler.getStream() << "6B"; // '6' for vftable, 'B' for const.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001876 // TODO: If the class has more than one vtable, mangle in the class it came
1877 // from.
1878 Mangler.getStream() << '@';
1879}
Reid Kleckner90633022013-06-19 15:20:38 +00001880
1881void MicrosoftMangleContext::mangleCXXVBTable(
1882 const CXXRecordDecl *Derived, ArrayRef<const CXXRecordDecl *> BasePath,
1883 raw_ostream &Out) {
1884 // <mangled-name> ::= ?_8 <class-name> <storage-class>
1885 // <cvr-qualifiers> [<name>] @
1886 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1887 // is always '7' for vbtables.
1888 MicrosoftCXXNameMangler Mangler(*this, Out);
1889 Mangler.getStream() << "\01??_8";
1890 Mangler.mangleName(Derived);
1891 Mangler.getStream() << "7B"; // '7' for vbtable, 'B' for const.
1892 for (ArrayRef<const CXXRecordDecl *>::iterator I = BasePath.begin(),
1893 E = BasePath.end();
1894 I != E; ++I) {
1895 Mangler.mangleName(*I);
1896 }
1897 Mangler.getStream() << '@';
1898}
1899
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001900void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1901 raw_ostream &) {
1902 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1903}
1904void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1905 int64_t Offset,
1906 const CXXRecordDecl *Type,
1907 raw_ostream &) {
1908 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1909}
1910void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1911 raw_ostream &) {
1912 // FIXME: Give a location...
1913 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1914 "cannot mangle RTTI descriptors for type %0 yet");
1915 getDiags().Report(DiagID)
1916 << T.getBaseTypeIdentifier();
1917}
1918void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1919 raw_ostream &) {
1920 // FIXME: Give a location...
1921 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1922 "cannot mangle the name of type %0 into RTTI descriptors yet");
1923 getDiags().Report(DiagID)
1924 << T.getBaseTypeIdentifier();
1925}
1926void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1927 CXXCtorType Type,
1928 raw_ostream & Out) {
1929 MicrosoftCXXNameMangler mangler(*this, Out);
1930 mangler.mangle(D);
1931}
1932void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1933 CXXDtorType Type,
1934 raw_ostream & Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001935 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001936 mangler.mangle(D);
1937}
1938void MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *VD,
1939 raw_ostream &) {
1940 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1941 "cannot mangle this reference temporary yet");
1942 getDiags().Report(VD->getLocation(), DiagID);
1943}
1944
1945MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
1946 DiagnosticsEngine &Diags) {
1947 return new MicrosoftMangleContext(Context, Diags);
1948}