blob: b0fc25cfa4c9a08bdb3554074248aebcb6aec144 [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"
25#include <map>
26
27using namespace clang;
28
29namespace {
30
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000031static const FunctionDecl *getStructor(const FunctionDecl *fn) {
32 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
33 return ftd->getTemplatedDecl();
34
35 return fn;
36}
37
Guy Benyei7f92f2d2012-12-18 14:30:41 +000038/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
39/// Microsoft Visual C++ ABI.
40class MicrosoftCXXNameMangler {
41 MangleContext &Context;
42 raw_ostream &Out;
43
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000044 /// The "structor" is the top-level declaration being mangled, if
45 /// that's not a template specialization; otherwise it's the pattern
46 /// for that specialization.
47 const NamedDecl *Structor;
48 unsigned StructorType;
49
Guy Benyei7f92f2d2012-12-18 14:30:41 +000050 // FIXME: audit the performance of BackRefMap as it might do way too many
51 // copying of strings.
52 typedef std::map<std::string, unsigned> BackRefMap;
53 BackRefMap NameBackReferences;
54 bool UseNameBackReferences;
55
56 typedef llvm::DenseMap<void*, unsigned> ArgBackRefMap;
57 ArgBackRefMap TypeBackReferences;
58
59 ASTContext &getASTContext() const { return Context.getASTContext(); }
60
61public:
62 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000063 : Context(C), Out(Out_),
64 Structor(0), StructorType(-1),
65 UseNameBackReferences(true) { }
66
67 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_,
68 const CXXDestructorDecl *D, CXXDtorType Type)
69 : Context(C), Out(Out_),
70 Structor(getStructor(D)), StructorType(Type),
71 UseNameBackReferences(true) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +000072
73 raw_ostream &getStream() const { return Out; }
74
75 void mangle(const NamedDecl *D, StringRef Prefix = "\01?");
76 void mangleName(const NamedDecl *ND);
77 void mangleFunctionEncoding(const FunctionDecl *FD);
78 void mangleVariableEncoding(const VarDecl *VD);
79 void mangleNumber(int64_t Number);
80 void mangleNumber(const llvm::APSInt &Value);
81 void mangleType(QualType T, SourceRange Range, bool MangleQualifiers = true);
82
83private:
84 void disableBackReferences() { UseNameBackReferences = false; }
85 void mangleUnqualifiedName(const NamedDecl *ND) {
86 mangleUnqualifiedName(ND, ND->getDeclName());
87 }
88 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
89 void mangleSourceName(const IdentifierInfo *II);
90 void manglePostfix(const DeclContext *DC, bool NoFunction=false);
91 void mangleOperatorName(OverloadedOperatorKind OO, SourceLocation Loc);
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000092 void mangleCXXDtorType(CXXDtorType T);
Guy Benyei7f92f2d2012-12-18 14:30:41 +000093 void mangleQualifiers(Qualifiers Quals, bool IsMember);
94 void manglePointerQualifiers(Qualifiers Quals);
95
96 void mangleUnscopedTemplateName(const TemplateDecl *ND);
97 void mangleTemplateInstantiationName(const TemplateDecl *TD,
98 const SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs);
99 void mangleObjCMethodName(const ObjCMethodDecl *MD);
100 void mangleLocalName(const FunctionDecl *FD);
101
102 void mangleArgumentType(QualType T, SourceRange Range);
103
104 // Declare manglers for every type class.
105#define ABSTRACT_TYPE(CLASS, PARENT)
106#define NON_CANONICAL_TYPE(CLASS, PARENT)
107#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T, \
108 SourceRange Range);
109#include "clang/AST/TypeNodes.def"
110#undef ABSTRACT_TYPE
111#undef NON_CANONICAL_TYPE
112#undef TYPE
113
114 void mangleType(const TagType*);
115 void mangleType(const FunctionType *T, const FunctionDecl *D,
116 bool IsStructor, bool IsInstMethod);
117 void mangleType(const ArrayType *T, bool IsGlobal);
118 void mangleExtraDimensions(QualType T);
119 void mangleFunctionClass(const FunctionDecl *FD);
120 void mangleCallingConvention(const FunctionType *T, bool IsInstMethod = false);
121 void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean);
122 void mangleExpression(const Expr *E);
123 void mangleThrowSpecification(const FunctionProtoType *T);
124
125 void mangleTemplateArgs(
126 const SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs);
127
128};
129
130/// MicrosoftMangleContext - Overrides the default MangleContext for the
131/// Microsoft Visual C++ ABI.
132class MicrosoftMangleContext : public MangleContext {
133public:
134 MicrosoftMangleContext(ASTContext &Context,
135 DiagnosticsEngine &Diags) : MangleContext(Context, Diags) { }
136 virtual bool shouldMangleDeclName(const NamedDecl *D);
137 virtual void mangleName(const NamedDecl *D, raw_ostream &Out);
138 virtual void mangleThunk(const CXXMethodDecl *MD,
139 const ThunkInfo &Thunk,
140 raw_ostream &);
141 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
142 const ThisAdjustment &ThisAdjustment,
143 raw_ostream &);
144 virtual void mangleCXXVTable(const CXXRecordDecl *RD,
145 raw_ostream &);
146 virtual void mangleCXXVTT(const CXXRecordDecl *RD,
147 raw_ostream &);
148 virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
149 const CXXRecordDecl *Type,
150 raw_ostream &);
151 virtual void mangleCXXRTTI(QualType T, raw_ostream &);
152 virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
153 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
154 raw_ostream &);
155 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
156 raw_ostream &);
157 virtual void mangleReferenceTemporary(const clang::VarDecl *,
158 raw_ostream &);
159};
160
161}
162
163static bool isInCLinkageSpecification(const Decl *D) {
164 D = D->getCanonicalDecl();
165 for (const DeclContext *DC = D->getDeclContext();
166 !DC->isTranslationUnit(); DC = DC->getParent()) {
167 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
168 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
169 }
170
171 return false;
172}
173
174bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
175 // In C, functions with no attributes never need to be mangled. Fastpath them.
176 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
177 return false;
178
179 // Any decl can be declared with __asm("foo") on it, and this takes precedence
180 // over all other naming in the .o file.
181 if (D->hasAttr<AsmLabelAttr>())
182 return true;
183
184 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
185 // (always) as does passing a C++ member function and a function
186 // whose name is not a simple identifier.
187 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
188 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
189 !FD->getDeclName().isIdentifier()))
190 return true;
191
192 // Otherwise, no mangling is done outside C++ mode.
193 if (!getASTContext().getLangOpts().CPlusPlus)
194 return false;
195
196 // Variables at global scope with internal linkage are not mangled.
197 if (!FD) {
198 const DeclContext *DC = D->getDeclContext();
199 if (DC->isTranslationUnit() && D->getLinkage() == InternalLinkage)
200 return false;
201 }
202
203 // C functions and "main" are not mangled.
204 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
205 return false;
206
207 return true;
208}
209
210void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
211 StringRef Prefix) {
212 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
213 // Therefore it's really important that we don't decorate the
214 // name with leading underscores or leading/trailing at signs. So, by
215 // default, we emit an asm marker at the start so we get the name right.
216 // Callers can override this with a custom prefix.
217
218 // Any decl can be declared with __asm("foo") on it, and this takes precedence
219 // over all other naming in the .o file.
220 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
221 // If we have an asm name, then we use it as the mangling.
222 Out << '\01' << ALA->getLabel();
223 return;
224 }
225
226 // <mangled-name> ::= ? <name> <type-encoding>
227 Out << Prefix;
228 mangleName(D);
229 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
230 mangleFunctionEncoding(FD);
231 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
232 mangleVariableEncoding(VD);
233 else {
234 // TODO: Fields? Can MSVC even mangle them?
235 // Issue a diagnostic for now.
236 DiagnosticsEngine &Diags = Context.getDiags();
237 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
238 "cannot mangle this declaration yet");
239 Diags.Report(D->getLocation(), DiagID)
240 << D->getSourceRange();
241 }
242}
243
244void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
245 // <type-encoding> ::= <function-class> <function-type>
246
247 // Don't mangle in the type if this isn't a decl we should typically mangle.
248 if (!Context.shouldMangleDeclName(FD))
249 return;
250
251 // We should never ever see a FunctionNoProtoType at this point.
252 // We don't even know how to mangle their types anyway :).
253 const FunctionProtoType *FT = FD->getType()->castAs<FunctionProtoType>();
254
255 bool InStructor = false, InInstMethod = false;
256 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
257 if (MD) {
258 if (MD->isInstance())
259 InInstMethod = true;
260 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
261 InStructor = true;
262 }
263
264 // First, the function class.
265 mangleFunctionClass(FD);
266
267 mangleType(FT, FD, InStructor, InInstMethod);
268}
269
270void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
271 // <type-encoding> ::= <storage-class> <variable-type>
272 // <storage-class> ::= 0 # private static member
273 // ::= 1 # protected static member
274 // ::= 2 # public static member
275 // ::= 3 # global
276 // ::= 4 # static local
277
278 // The first character in the encoding (after the name) is the storage class.
279 if (VD->isStaticDataMember()) {
280 // If it's a static member, it also encodes the access level.
281 switch (VD->getAccess()) {
282 default:
283 case AS_private: Out << '0'; break;
284 case AS_protected: Out << '1'; break;
285 case AS_public: Out << '2'; break;
286 }
287 }
288 else if (!VD->isStaticLocal())
289 Out << '3';
290 else
291 Out << '4';
292 // Now mangle the type.
293 // <variable-type> ::= <type> <cvr-qualifiers>
294 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
295 // Pointers and references are odd. The type of 'int * const foo;' gets
296 // mangled as 'QAHA' instead of 'PAHB', for example.
297 TypeLoc TL = VD->getTypeSourceInfo()->getTypeLoc();
298 QualType Ty = TL.getType();
299 if (Ty->isPointerType() || Ty->isReferenceType()) {
300 mangleType(Ty, TL.getSourceRange());
301 mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
302 } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
303 // Global arrays are funny, too.
304 mangleType(AT, true);
305 mangleQualifiers(Ty.getQualifiers(), false);
306 } else {
307 mangleType(Ty.getLocalUnqualifiedType(), TL.getSourceRange());
308 mangleQualifiers(Ty.getLocalQualifiers(), false);
309 }
310}
311
312void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
313 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
314 const DeclContext *DC = ND->getDeclContext();
315
316 // Always start with the unqualified name.
317 mangleUnqualifiedName(ND);
318
319 // If this is an extern variable declared locally, the relevant DeclContext
320 // is that of the containing namespace, or the translation unit.
321 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
322 while (!DC->isNamespace() && !DC->isTranslationUnit())
323 DC = DC->getParent();
324
325 manglePostfix(DC);
326
327 // Terminate the whole name with an '@'.
328 Out << '@';
329}
330
331void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
332 llvm::APSInt APSNumber(/*BitWidth=*/64, /*isUnsigned=*/false);
333 APSNumber = Number;
334 mangleNumber(APSNumber);
335}
336
337void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
338 // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
339 // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
340 // ::= [?] @ # 0 (alternate mangling, not emitted by VC)
341 if (Value.isSigned() && Value.isNegative()) {
342 Out << '?';
343 mangleNumber(llvm::APSInt(Value.abs()));
344 return;
345 }
346 llvm::APSInt Temp(Value);
347 // There's a special shorter mangling for 0, but Microsoft
348 // chose not to use it. Instead, 0 gets mangled as "A@". Oh well...
349 if (Value.uge(1) && Value.ule(10)) {
350 --Temp;
351 Temp.print(Out, false);
352 } else {
353 // We have to build up the encoding in reverse order, so it will come
354 // out right when we write it out.
355 char Encoding[64];
356 char *EndPtr = Encoding+sizeof(Encoding);
357 char *CurPtr = EndPtr;
358 llvm::APSInt NibbleMask(Value.getBitWidth(), Value.isUnsigned());
359 NibbleMask = 0xf;
360 do {
361 *--CurPtr = 'A' + Temp.And(NibbleMask).getLimitedValue(0xf);
362 Temp = Temp.lshr(4);
363 } while (Temp != 0);
364 Out.write(CurPtr, EndPtr-CurPtr);
365 Out << '@';
366 }
367}
368
369static const TemplateDecl *
370isTemplate(const NamedDecl *ND,
371 SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs) {
372 // Check if we have a function template.
373 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
374 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
375 if (FD->getTemplateSpecializationArgsAsWritten()) {
376 const ASTTemplateArgumentListInfo *ArgList =
377 FD->getTemplateSpecializationArgsAsWritten();
378 TemplateArgs.append(ArgList->getTemplateArgs(),
379 ArgList->getTemplateArgs() +
380 ArgList->NumTemplateArgs);
381 } else {
382 const TemplateArgumentList *ArgList =
383 FD->getTemplateSpecializationArgs();
384 TemplateArgumentListInfo LI;
385 for (unsigned i = 0, e = ArgList->size(); i != e; ++i)
386 TemplateArgs.push_back(TemplateArgumentLoc(ArgList->get(i),
387 FD->getTypeSourceInfo()));
388 }
389 return TD;
390 }
391 }
392
393 // Check if we have a class template.
394 if (const ClassTemplateSpecializationDecl *Spec =
395 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
396 TypeSourceInfo *TSI = Spec->getTypeAsWritten();
397 if (TSI) {
398 TemplateSpecializationTypeLoc TSTL =
David Blaikie39e6ab42013-02-18 22:06:02 +0000399 TSI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000400 TemplateArgumentListInfo LI(TSTL.getLAngleLoc(), TSTL.getRAngleLoc());
401 for (unsigned i = 0, e = TSTL.getNumArgs(); i != e; ++i)
402 TemplateArgs.push_back(TSTL.getArgLoc(i));
403 } else {
404 TemplateArgumentListInfo LI;
405 const TemplateArgumentList &ArgList =
406 Spec->getTemplateArgs();
407 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
408 TemplateArgs.push_back(TemplateArgumentLoc(ArgList[i],
409 TemplateArgumentLocInfo()));
410 }
411 return Spec->getSpecializedTemplate();
412 }
413
414 return 0;
415}
416
417void
418MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
419 DeclarationName Name) {
420 // <unqualified-name> ::= <operator-name>
421 // ::= <ctor-dtor-name>
422 // ::= <source-name>
423 // ::= <template-name>
424 SmallVector<TemplateArgumentLoc, 2> TemplateArgs;
425 // Check if we have a template.
426 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
427 // We have a template.
428 // Here comes the tricky thing: if we need to mangle something like
429 // void foo(A::X<Y>, B::X<Y>),
430 // the X<Y> part is aliased. However, if you need to mangle
431 // void foo(A::X<A::Y>, A::X<B::Y>),
432 // the A::X<> part is not aliased.
433 // That said, from the mangler's perspective we have a structure like this:
434 // namespace[s] -> type[ -> template-parameters]
435 // but from the Clang perspective we have
436 // type [ -> template-parameters]
437 // \-> namespace[s]
438 // What we do is we create a new mangler, mangle the same type (without
439 // a namespace suffix) using the extra mangler with back references
440 // disabled (to avoid infinite recursion) and then use the mangled type
441 // name as a key to check the mangling of different types for aliasing.
442
443 std::string BackReferenceKey;
444 BackRefMap::iterator Found;
445 if (UseNameBackReferences) {
446 llvm::raw_string_ostream Stream(BackReferenceKey);
447 MicrosoftCXXNameMangler Extra(Context, Stream);
448 Extra.disableBackReferences();
449 Extra.mangleUnqualifiedName(ND, Name);
450 Stream.flush();
451
452 Found = NameBackReferences.find(BackReferenceKey);
453 }
454 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
455 mangleTemplateInstantiationName(TD, TemplateArgs);
456 if (UseNameBackReferences && NameBackReferences.size() < 10) {
457 size_t Size = NameBackReferences.size();
458 NameBackReferences[BackReferenceKey] = Size;
459 }
460 } else {
461 Out << Found->second;
462 }
463 return;
464 }
465
466 switch (Name.getNameKind()) {
467 case DeclarationName::Identifier: {
468 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
469 mangleSourceName(II);
470 break;
471 }
472
473 // Otherwise, an anonymous entity. We must have a declaration.
474 assert(ND && "mangling empty name without declaration");
475
476 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
477 if (NS->isAnonymousNamespace()) {
478 Out << "?A@";
479 break;
480 }
481 }
482
483 // We must have an anonymous struct.
484 const TagDecl *TD = cast<TagDecl>(ND);
485 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
486 assert(TD->getDeclContext() == D->getDeclContext() &&
487 "Typedef should not be in another decl context!");
488 assert(D->getDeclName().getAsIdentifierInfo() &&
489 "Typedef was not named!");
490 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
491 break;
492 }
493
494 // When VC encounters an anonymous type with no tag and no typedef,
495 // it literally emits '<unnamed-tag>'.
496 Out << "<unnamed-tag>";
497 break;
498 }
499
500 case DeclarationName::ObjCZeroArgSelector:
501 case DeclarationName::ObjCOneArgSelector:
502 case DeclarationName::ObjCMultiArgSelector:
503 llvm_unreachable("Can't mangle Objective-C selector names here!");
504
505 case DeclarationName::CXXConstructorName:
506 Out << "?0";
507 break;
508
509 case DeclarationName::CXXDestructorName:
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000510 if (ND == Structor)
511 // If the named decl is the C++ destructor we're mangling,
512 // use the type we were given.
513 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
514 else
515 // Otherwise, use the complete destructor name. This is relevant if a
516 // class with a destructor is declared within a destructor.
517 mangleCXXDtorType(Dtor_Complete);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000518 break;
519
520 case DeclarationName::CXXConversionFunctionName:
521 // <operator-name> ::= ?B # (cast)
522 // The target type is encoded as the return type.
523 Out << "?B";
524 break;
525
526 case DeclarationName::CXXOperatorName:
527 mangleOperatorName(Name.getCXXOverloadedOperator(), ND->getLocation());
528 break;
529
530 case DeclarationName::CXXLiteralOperatorName: {
531 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
532 DiagnosticsEngine Diags = Context.getDiags();
533 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
534 "cannot mangle this literal operator yet");
535 Diags.Report(ND->getLocation(), DiagID);
536 break;
537 }
538
539 case DeclarationName::CXXUsingDirective:
540 llvm_unreachable("Can't mangle a using directive name!");
541 }
542}
543
544void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
545 bool NoFunction) {
546 // <postfix> ::= <unqualified-name> [<postfix>]
547 // ::= <substitution> [<postfix>]
548
549 if (!DC) return;
550
551 while (isa<LinkageSpecDecl>(DC))
552 DC = DC->getParent();
553
554 if (DC->isTranslationUnit())
555 return;
556
557 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
558 Context.mangleBlock(BD, Out);
559 Out << '@';
560 return manglePostfix(DC->getParent(), NoFunction);
561 }
562
563 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
564 return;
565 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
566 mangleObjCMethodName(Method);
567 else if (const FunctionDecl *Func = dyn_cast<FunctionDecl>(DC))
568 mangleLocalName(Func);
569 else {
570 mangleUnqualifiedName(cast<NamedDecl>(DC));
571 manglePostfix(DC->getParent(), NoFunction);
572 }
573}
574
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000575void MicrosoftCXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
576 switch (T) {
577 case Dtor_Deleting:
578 Out << "?_G";
579 return;
580 case Dtor_Base:
581 // FIXME: We should be asked to mangle base dtors.
582 // However, fixing this would require larger changes to the CodeGenModule.
583 // Please put llvm_unreachable here when CGM is changed.
584 // For now, just mangle a base dtor the same way as a complete dtor...
585 case Dtor_Complete:
586 Out << "?1";
587 return;
588 }
589 llvm_unreachable("Unsupported dtor type?");
590}
591
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000592void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
593 SourceLocation Loc) {
594 switch (OO) {
595 // ?0 # constructor
596 // ?1 # destructor
597 // <operator-name> ::= ?2 # new
598 case OO_New: Out << "?2"; break;
599 // <operator-name> ::= ?3 # delete
600 case OO_Delete: Out << "?3"; break;
601 // <operator-name> ::= ?4 # =
602 case OO_Equal: Out << "?4"; break;
603 // <operator-name> ::= ?5 # >>
604 case OO_GreaterGreater: Out << "?5"; break;
605 // <operator-name> ::= ?6 # <<
606 case OO_LessLess: Out << "?6"; break;
607 // <operator-name> ::= ?7 # !
608 case OO_Exclaim: Out << "?7"; break;
609 // <operator-name> ::= ?8 # ==
610 case OO_EqualEqual: Out << "?8"; break;
611 // <operator-name> ::= ?9 # !=
612 case OO_ExclaimEqual: Out << "?9"; break;
613 // <operator-name> ::= ?A # []
614 case OO_Subscript: Out << "?A"; break;
615 // ?B # conversion
616 // <operator-name> ::= ?C # ->
617 case OO_Arrow: Out << "?C"; break;
618 // <operator-name> ::= ?D # *
619 case OO_Star: Out << "?D"; break;
620 // <operator-name> ::= ?E # ++
621 case OO_PlusPlus: Out << "?E"; break;
622 // <operator-name> ::= ?F # --
623 case OO_MinusMinus: Out << "?F"; break;
624 // <operator-name> ::= ?G # -
625 case OO_Minus: Out << "?G"; break;
626 // <operator-name> ::= ?H # +
627 case OO_Plus: Out << "?H"; break;
628 // <operator-name> ::= ?I # &
629 case OO_Amp: Out << "?I"; break;
630 // <operator-name> ::= ?J # ->*
631 case OO_ArrowStar: Out << "?J"; break;
632 // <operator-name> ::= ?K # /
633 case OO_Slash: Out << "?K"; break;
634 // <operator-name> ::= ?L # %
635 case OO_Percent: Out << "?L"; break;
636 // <operator-name> ::= ?M # <
637 case OO_Less: Out << "?M"; break;
638 // <operator-name> ::= ?N # <=
639 case OO_LessEqual: Out << "?N"; break;
640 // <operator-name> ::= ?O # >
641 case OO_Greater: Out << "?O"; break;
642 // <operator-name> ::= ?P # >=
643 case OO_GreaterEqual: Out << "?P"; break;
644 // <operator-name> ::= ?Q # ,
645 case OO_Comma: Out << "?Q"; break;
646 // <operator-name> ::= ?R # ()
647 case OO_Call: Out << "?R"; break;
648 // <operator-name> ::= ?S # ~
649 case OO_Tilde: Out << "?S"; break;
650 // <operator-name> ::= ?T # ^
651 case OO_Caret: Out << "?T"; break;
652 // <operator-name> ::= ?U # |
653 case OO_Pipe: Out << "?U"; break;
654 // <operator-name> ::= ?V # &&
655 case OO_AmpAmp: Out << "?V"; break;
656 // <operator-name> ::= ?W # ||
657 case OO_PipePipe: Out << "?W"; break;
658 // <operator-name> ::= ?X # *=
659 case OO_StarEqual: Out << "?X"; break;
660 // <operator-name> ::= ?Y # +=
661 case OO_PlusEqual: Out << "?Y"; break;
662 // <operator-name> ::= ?Z # -=
663 case OO_MinusEqual: Out << "?Z"; break;
664 // <operator-name> ::= ?_0 # /=
665 case OO_SlashEqual: Out << "?_0"; break;
666 // <operator-name> ::= ?_1 # %=
667 case OO_PercentEqual: Out << "?_1"; break;
668 // <operator-name> ::= ?_2 # >>=
669 case OO_GreaterGreaterEqual: Out << "?_2"; break;
670 // <operator-name> ::= ?_3 # <<=
671 case OO_LessLessEqual: Out << "?_3"; break;
672 // <operator-name> ::= ?_4 # &=
673 case OO_AmpEqual: Out << "?_4"; break;
674 // <operator-name> ::= ?_5 # |=
675 case OO_PipeEqual: Out << "?_5"; break;
676 // <operator-name> ::= ?_6 # ^=
677 case OO_CaretEqual: Out << "?_6"; break;
678 // ?_7 # vftable
679 // ?_8 # vbtable
680 // ?_9 # vcall
681 // ?_A # typeof
682 // ?_B # local static guard
683 // ?_C # string
684 // ?_D # vbase destructor
685 // ?_E # vector deleting destructor
686 // ?_F # default constructor closure
687 // ?_G # scalar deleting destructor
688 // ?_H # vector constructor iterator
689 // ?_I # vector destructor iterator
690 // ?_J # vector vbase constructor iterator
691 // ?_K # virtual displacement map
692 // ?_L # eh vector constructor iterator
693 // ?_M # eh vector destructor iterator
694 // ?_N # eh vector vbase constructor iterator
695 // ?_O # copy constructor closure
696 // ?_P<name> # udt returning <name>
697 // ?_Q # <unknown>
698 // ?_R0 # RTTI Type Descriptor
699 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
700 // ?_R2 # RTTI Base Class Array
701 // ?_R3 # RTTI Class Hierarchy Descriptor
702 // ?_R4 # RTTI Complete Object Locator
703 // ?_S # local vftable
704 // ?_T # local vftable constructor closure
705 // <operator-name> ::= ?_U # new[]
706 case OO_Array_New: Out << "?_U"; break;
707 // <operator-name> ::= ?_V # delete[]
708 case OO_Array_Delete: Out << "?_V"; break;
709
710 case OO_Conditional: {
711 DiagnosticsEngine &Diags = Context.getDiags();
712 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
713 "cannot mangle this conditional operator yet");
714 Diags.Report(Loc, DiagID);
715 break;
716 }
717
718 case OO_None:
719 case NUM_OVERLOADED_OPERATORS:
720 llvm_unreachable("Not an overloaded operator");
721 }
722}
723
724void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
725 // <source name> ::= <identifier> @
726 std::string key = II->getNameStart();
727 BackRefMap::iterator Found;
728 if (UseNameBackReferences)
729 Found = NameBackReferences.find(key);
730 if (!UseNameBackReferences || Found == NameBackReferences.end()) {
731 Out << II->getName() << '@';
732 if (UseNameBackReferences && NameBackReferences.size() < 10) {
733 size_t Size = NameBackReferences.size();
734 NameBackReferences[key] = Size;
735 }
736 } else {
737 Out << Found->second;
738 }
739}
740
741void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
742 Context.mangleObjCMethodName(MD, Out);
743}
744
745// Find out how many function decls live above this one and return an integer
746// suitable for use as the number in a numbered anonymous scope.
747// TODO: Memoize.
748static unsigned getLocalNestingLevel(const FunctionDecl *FD) {
749 const DeclContext *DC = FD->getParent();
750 int level = 1;
751
752 while (DC && !DC->isTranslationUnit()) {
753 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) level++;
754 DC = DC->getParent();
755 }
756
757 return 2*level;
758}
759
760void MicrosoftCXXNameMangler::mangleLocalName(const FunctionDecl *FD) {
761 // <nested-name> ::= <numbered-anonymous-scope> ? <mangled-name>
762 // <numbered-anonymous-scope> ::= ? <number>
763 // Even though the name is rendered in reverse order (e.g.
764 // A::B::C is rendered as C@B@A), VC numbers the scopes from outermost to
765 // innermost. So a method bar in class C local to function foo gets mangled
766 // as something like:
767 // ?bar@C@?1??foo@@YAXXZ@QAEXXZ
768 // This is more apparent when you have a type nested inside a method of a
769 // type nested inside a function. A method baz in class D local to method
770 // bar of class C local to function foo gets mangled as:
771 // ?baz@D@?3??bar@C@?1??foo@@YAXXZ@QAEXXZ@QAEXXZ
772 // This scheme is general enough to support GCC-style nested
773 // functions. You could have a method baz of class C inside a function bar
774 // inside a function foo, like so:
775 // ?baz@C@?3??bar@?1??foo@@YAXXZ@YAXXZ@QAEXXZ
776 int NestLevel = getLocalNestingLevel(FD);
777 Out << '?';
778 mangleNumber(NestLevel);
779 Out << '?';
780 mangle(FD, "?");
781}
782
783void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(
784 const TemplateDecl *TD,
785 const SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs) {
786 // <template-name> ::= <unscoped-template-name> <template-args>
787 // ::= <substitution>
788 // Always start with the unqualified name.
789
790 // Templates have their own context for back references.
791 ArgBackRefMap OuterArgsContext;
792 BackRefMap OuterTemplateContext;
793 NameBackReferences.swap(OuterTemplateContext);
794 TypeBackReferences.swap(OuterArgsContext);
795
796 mangleUnscopedTemplateName(TD);
797 mangleTemplateArgs(TemplateArgs);
798
799 // Restore the previous back reference contexts.
800 NameBackReferences.swap(OuterTemplateContext);
801 TypeBackReferences.swap(OuterArgsContext);
802}
803
804void
805MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
806 // <unscoped-template-name> ::= ?$ <unqualified-name>
807 Out << "?$";
808 mangleUnqualifiedName(TD);
809}
810
811void
812MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
813 bool IsBoolean) {
814 // <integer-literal> ::= $0 <number>
815 Out << "$0";
816 // Make sure booleans are encoded as 0/1.
817 if (IsBoolean && Value.getBoolValue())
818 mangleNumber(1);
819 else
820 mangleNumber(Value);
821}
822
823void
824MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
825 // See if this is a constant expression.
826 llvm::APSInt Value;
827 if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
828 mangleIntegerLiteral(Value, E->getType()->isBooleanType());
829 return;
830 }
831
832 // As bad as this diagnostic is, it's better than crashing.
833 DiagnosticsEngine &Diags = Context.getDiags();
834 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
835 "cannot yet mangle expression type %0");
836 Diags.Report(E->getExprLoc(), DiagID)
837 << E->getStmtClassName() << E->getSourceRange();
838}
839
840void
841MicrosoftCXXNameMangler::mangleTemplateArgs(
842 const SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs) {
843 // <template-args> ::= {<type> | <integer-literal>}+ @
844 unsigned NumTemplateArgs = TemplateArgs.size();
845 for (unsigned i = 0; i < NumTemplateArgs; ++i) {
846 const TemplateArgumentLoc &TAL = TemplateArgs[i];
847 const TemplateArgument &TA = TAL.getArgument();
848 switch (TA.getKind()) {
849 case TemplateArgument::Null:
850 llvm_unreachable("Can't mangle null template arguments!");
851 case TemplateArgument::Type:
852 mangleType(TA.getAsType(), TAL.getSourceRange());
853 break;
854 case TemplateArgument::Integral:
855 mangleIntegerLiteral(TA.getAsIntegral(),
856 TA.getIntegralType()->isBooleanType());
857 break;
858 case TemplateArgument::Expression:
859 mangleExpression(TA.getAsExpr());
860 break;
861 case TemplateArgument::Template:
862 case TemplateArgument::TemplateExpansion:
863 case TemplateArgument::Declaration:
864 case TemplateArgument::NullPtr:
865 case TemplateArgument::Pack: {
866 // Issue a diagnostic.
867 DiagnosticsEngine &Diags = Context.getDiags();
868 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
869 "cannot mangle this %select{ERROR|ERROR|pointer/reference|nullptr|"
870 "integral|template|template pack expansion|ERROR|parameter pack}0 "
871 "template argument yet");
872 Diags.Report(TAL.getLocation(), DiagID)
873 << TA.getKind()
874 << TAL.getSourceRange();
875 }
876 }
877 }
878 Out << '@';
879}
880
881void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
882 bool IsMember) {
883 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
884 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
885 // 'I' means __restrict (32/64-bit).
886 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
887 // keyword!
888 // <base-cvr-qualifiers> ::= A # near
889 // ::= B # near const
890 // ::= C # near volatile
891 // ::= D # near const volatile
892 // ::= E # far (16-bit)
893 // ::= F # far const (16-bit)
894 // ::= G # far volatile (16-bit)
895 // ::= H # far const volatile (16-bit)
896 // ::= I # huge (16-bit)
897 // ::= J # huge const (16-bit)
898 // ::= K # huge volatile (16-bit)
899 // ::= L # huge const volatile (16-bit)
900 // ::= M <basis> # based
901 // ::= N <basis> # based const
902 // ::= O <basis> # based volatile
903 // ::= P <basis> # based const volatile
904 // ::= Q # near member
905 // ::= R # near const member
906 // ::= S # near volatile member
907 // ::= T # near const volatile member
908 // ::= U # far member (16-bit)
909 // ::= V # far const member (16-bit)
910 // ::= W # far volatile member (16-bit)
911 // ::= X # far const volatile member (16-bit)
912 // ::= Y # huge member (16-bit)
913 // ::= Z # huge const member (16-bit)
914 // ::= 0 # huge volatile member (16-bit)
915 // ::= 1 # huge const volatile member (16-bit)
916 // ::= 2 <basis> # based member
917 // ::= 3 <basis> # based const member
918 // ::= 4 <basis> # based volatile member
919 // ::= 5 <basis> # based const volatile member
920 // ::= 6 # near function (pointers only)
921 // ::= 7 # far function (pointers only)
922 // ::= 8 # near method (pointers only)
923 // ::= 9 # far method (pointers only)
924 // ::= _A <basis> # based function (pointers only)
925 // ::= _B <basis> # based function (far?) (pointers only)
926 // ::= _C <basis> # based method (pointers only)
927 // ::= _D <basis> # based method (far?) (pointers only)
928 // ::= _E # block (Clang)
929 // <basis> ::= 0 # __based(void)
930 // ::= 1 # __based(segment)?
931 // ::= 2 <name> # __based(name)
932 // ::= 3 # ?
933 // ::= 4 # ?
934 // ::= 5 # not really based
935 bool HasConst = Quals.hasConst(),
936 HasVolatile = Quals.hasVolatile();
937 if (!IsMember) {
938 if (HasConst && HasVolatile) {
939 Out << 'D';
940 } else if (HasVolatile) {
941 Out << 'C';
942 } else if (HasConst) {
943 Out << 'B';
944 } else {
945 Out << 'A';
946 }
947 } else {
948 if (HasConst && HasVolatile) {
949 Out << 'T';
950 } else if (HasVolatile) {
951 Out << 'S';
952 } else if (HasConst) {
953 Out << 'R';
954 } else {
955 Out << 'Q';
956 }
957 }
958
959 // FIXME: For now, just drop all extension qualifiers on the floor.
960}
961
962void MicrosoftCXXNameMangler::manglePointerQualifiers(Qualifiers Quals) {
963 // <pointer-cvr-qualifiers> ::= P # no qualifiers
964 // ::= Q # const
965 // ::= R # volatile
966 // ::= S # const volatile
967 bool HasConst = Quals.hasConst(),
968 HasVolatile = Quals.hasVolatile();
969 if (HasConst && HasVolatile) {
970 Out << 'S';
971 } else if (HasVolatile) {
972 Out << 'R';
973 } else if (HasConst) {
974 Out << 'Q';
975 } else {
976 Out << 'P';
977 }
978}
979
980void MicrosoftCXXNameMangler::mangleArgumentType(QualType T,
981 SourceRange Range) {
982 void *TypePtr = getASTContext().getCanonicalType(T).getAsOpaquePtr();
983 ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
984
985 if (Found == TypeBackReferences.end()) {
986 size_t OutSizeBefore = Out.GetNumBytesInBuffer();
987
988 mangleType(T, Range, false);
989
990 // See if it's worth creating a back reference.
991 // Only types longer than 1 character are considered
992 // and only 10 back references slots are available:
993 bool LongerThanOneChar = (Out.GetNumBytesInBuffer() - OutSizeBefore > 1);
994 if (LongerThanOneChar && TypeBackReferences.size() < 10) {
995 size_t Size = TypeBackReferences.size();
996 TypeBackReferences[TypePtr] = Size;
997 }
998 } else {
999 Out << Found->second;
1000 }
1001}
1002
1003void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
1004 bool MangleQualifiers) {
1005 // Only operate on the canonical type!
1006 T = getASTContext().getCanonicalType(T);
1007
1008 Qualifiers Quals = T.getLocalQualifiers();
1009 // We have to mangle these now, while we still have enough information.
1010 if (T->isAnyPointerType() || T->isMemberPointerType() ||
1011 T->isBlockPointerType()) {
1012 manglePointerQualifiers(Quals);
1013 } else if (Quals && MangleQualifiers) {
1014 mangleQualifiers(Quals, false);
1015 }
1016
1017 SplitQualType split = T.split();
1018 const Type *ty = split.Ty;
1019
1020 // If we're mangling a qualified array type, push the qualifiers to
1021 // the element type.
1022 if (split.Quals && isa<ArrayType>(T)) {
1023 ty = Context.getASTContext().getAsArrayType(T);
1024 }
1025
1026 switch (ty->getTypeClass()) {
1027#define ABSTRACT_TYPE(CLASS, PARENT)
1028#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1029 case Type::CLASS: \
1030 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1031 return;
1032#define TYPE(CLASS, PARENT) \
1033 case Type::CLASS: \
1034 mangleType(cast<CLASS##Type>(ty), Range); \
1035 break;
1036#include "clang/AST/TypeNodes.def"
1037#undef ABSTRACT_TYPE
1038#undef NON_CANONICAL_TYPE
1039#undef TYPE
1040 }
1041}
1042
1043void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
1044 SourceRange Range) {
1045 // <type> ::= <builtin-type>
1046 // <builtin-type> ::= X # void
1047 // ::= C # signed char
1048 // ::= D # char
1049 // ::= E # unsigned char
1050 // ::= F # short
1051 // ::= G # unsigned short (or wchar_t if it's not a builtin)
1052 // ::= H # int
1053 // ::= I # unsigned int
1054 // ::= J # long
1055 // ::= K # unsigned long
1056 // L # <none>
1057 // ::= M # float
1058 // ::= N # double
1059 // ::= O # long double (__float80 is mangled differently)
1060 // ::= _J # long long, __int64
1061 // ::= _K # unsigned long long, __int64
1062 // ::= _L # __int128
1063 // ::= _M # unsigned __int128
1064 // ::= _N # bool
1065 // _O # <array in parameter>
1066 // ::= _T # __float80 (Intel)
1067 // ::= _W # wchar_t
1068 // ::= _Z # __float80 (Digital Mars)
1069 switch (T->getKind()) {
1070 case BuiltinType::Void: Out << 'X'; break;
1071 case BuiltinType::SChar: Out << 'C'; break;
1072 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
1073 case BuiltinType::UChar: Out << 'E'; break;
1074 case BuiltinType::Short: Out << 'F'; break;
1075 case BuiltinType::UShort: Out << 'G'; break;
1076 case BuiltinType::Int: Out << 'H'; break;
1077 case BuiltinType::UInt: Out << 'I'; break;
1078 case BuiltinType::Long: Out << 'J'; break;
1079 case BuiltinType::ULong: Out << 'K'; break;
1080 case BuiltinType::Float: Out << 'M'; break;
1081 case BuiltinType::Double: Out << 'N'; break;
1082 // TODO: Determine size and mangle accordingly
1083 case BuiltinType::LongDouble: Out << 'O'; break;
1084 case BuiltinType::LongLong: Out << "_J"; break;
1085 case BuiltinType::ULongLong: Out << "_K"; break;
1086 case BuiltinType::Int128: Out << "_L"; break;
1087 case BuiltinType::UInt128: Out << "_M"; break;
1088 case BuiltinType::Bool: Out << "_N"; break;
1089 case BuiltinType::WChar_S:
1090 case BuiltinType::WChar_U: Out << "_W"; break;
1091
1092#define BUILTIN_TYPE(Id, SingletonId)
1093#define PLACEHOLDER_TYPE(Id, SingletonId) \
1094 case BuiltinType::Id:
1095#include "clang/AST/BuiltinTypes.def"
1096 case BuiltinType::Dependent:
1097 llvm_unreachable("placeholder types shouldn't get to name mangling");
1098
1099 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
1100 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
1101 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00001102
1103 case BuiltinType::OCLImage1d: Out << "PAUocl_image1d@@"; break;
1104 case BuiltinType::OCLImage1dArray: Out << "PAUocl_image1darray@@"; break;
1105 case BuiltinType::OCLImage1dBuffer: Out << "PAUocl_image1dbuffer@@"; break;
1106 case BuiltinType::OCLImage2d: Out << "PAUocl_image2d@@"; break;
1107 case BuiltinType::OCLImage2dArray: Out << "PAUocl_image2darray@@"; break;
1108 case BuiltinType::OCLImage3d: Out << "PAUocl_image3d@@"; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00001109 case BuiltinType::OCLSampler: Out << "PAUocl_sampler@@"; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00001110 case BuiltinType::OCLEvent: Out << "PAUocl_event@@"; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001111
1112 case BuiltinType::NullPtr: Out << "$$T"; break;
1113
1114 case BuiltinType::Char16:
1115 case BuiltinType::Char32:
1116 case BuiltinType::Half: {
1117 DiagnosticsEngine &Diags = Context.getDiags();
1118 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1119 "cannot mangle this built-in %0 type yet");
1120 Diags.Report(Range.getBegin(), DiagID)
1121 << T->getName(Context.getASTContext().getPrintingPolicy())
1122 << Range;
1123 break;
1124 }
1125 }
1126}
1127
1128// <type> ::= <function-type>
1129void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T,
1130 SourceRange) {
1131 // Structors only appear in decls, so at this point we know it's not a
1132 // structor type.
1133 // FIXME: This may not be lambda-friendly.
1134 Out << "$$A6";
1135 mangleType(T, NULL, false, false);
1136}
1137void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T,
1138 SourceRange) {
1139 llvm_unreachable("Can't mangle K&R function prototypes");
1140}
1141
1142void MicrosoftCXXNameMangler::mangleType(const FunctionType *T,
1143 const FunctionDecl *D,
1144 bool IsStructor,
1145 bool IsInstMethod) {
1146 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
1147 // <return-type> <argument-list> <throw-spec>
1148 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1149
1150 // If this is a C++ instance method, mangle the CVR qualifiers for the
1151 // this pointer.
1152 if (IsInstMethod)
1153 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
1154
1155 mangleCallingConvention(T, IsInstMethod);
1156
1157 // <return-type> ::= <type>
1158 // ::= @ # structors (they have no declared return type)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001159 if (IsStructor) {
1160 if (isa<CXXDestructorDecl>(D) && D == Structor &&
1161 StructorType == Dtor_Deleting) {
1162 // The scalar deleting destructor takes an extra int argument.
1163 // However, the FunctionType generated has 0 arguments.
1164 // FIXME: This is a temporary hack.
1165 // Maybe should fix the FunctionType creation instead?
1166 Out << "PAXI@Z";
1167 return;
1168 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001169 Out << '@';
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001170 } else {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001171 QualType Result = Proto->getResultType();
1172 const Type* RT = Result.getTypePtr();
1173 if (!RT->isAnyPointerType() && !RT->isReferenceType()) {
1174 if (Result.hasQualifiers() || !RT->isBuiltinType())
1175 Out << '?';
1176 if (!RT->isBuiltinType() && !Result.hasQualifiers()) {
1177 // Lack of qualifiers for user types is mangled as 'A'.
1178 Out << 'A';
1179 }
1180 }
1181
1182 // FIXME: Get the source range for the result type. Or, better yet,
1183 // implement the unimplemented stuff so we don't need accurate source
1184 // location info anymore :).
1185 mangleType(Result, SourceRange());
1186 }
1187
1188 // <argument-list> ::= X # void
1189 // ::= <type>+ @
1190 // ::= <type>* Z # varargs
1191 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1192 Out << 'X';
1193 } else {
1194 if (D) {
1195 // If we got a decl, use the type-as-written to make sure arrays
1196 // get mangled right. Note that we can't rely on the TSI
1197 // existing if (for example) the parameter was synthesized.
1198 for (FunctionDecl::param_const_iterator Parm = D->param_begin(),
1199 ParmEnd = D->param_end(); Parm != ParmEnd; ++Parm) {
1200 TypeSourceInfo *TSI = (*Parm)->getTypeSourceInfo();
1201 QualType Type = TSI ? TSI->getType() : (*Parm)->getType();
1202 mangleArgumentType(Type, (*Parm)->getSourceRange());
1203 }
1204 } else {
1205 // Happens for function pointer type arguments for example.
1206 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
1207 ArgEnd = Proto->arg_type_end();
1208 Arg != ArgEnd; ++Arg)
1209 mangleArgumentType(*Arg, SourceRange());
1210 }
1211 // <builtin-type> ::= Z # ellipsis
1212 if (Proto->isVariadic())
1213 Out << 'Z';
1214 else
1215 Out << '@';
1216 }
1217
1218 mangleThrowSpecification(Proto);
1219}
1220
1221void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
1222 // <function-class> ::= A # private: near
1223 // ::= B # private: far
1224 // ::= C # private: static near
1225 // ::= D # private: static far
1226 // ::= E # private: virtual near
1227 // ::= F # private: virtual far
1228 // ::= G # private: thunk near
1229 // ::= H # private: thunk far
1230 // ::= I # protected: near
1231 // ::= J # protected: far
1232 // ::= K # protected: static near
1233 // ::= L # protected: static far
1234 // ::= M # protected: virtual near
1235 // ::= N # protected: virtual far
1236 // ::= O # protected: thunk near
1237 // ::= P # protected: thunk far
1238 // ::= Q # public: near
1239 // ::= R # public: far
1240 // ::= S # public: static near
1241 // ::= T # public: static far
1242 // ::= U # public: virtual near
1243 // ::= V # public: virtual far
1244 // ::= W # public: thunk near
1245 // ::= X # public: thunk far
1246 // ::= Y # global near
1247 // ::= Z # global far
1248 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
1249 switch (MD->getAccess()) {
1250 default:
1251 case AS_private:
1252 if (MD->isStatic())
1253 Out << 'C';
1254 else if (MD->isVirtual())
1255 Out << 'E';
1256 else
1257 Out << 'A';
1258 break;
1259 case AS_protected:
1260 if (MD->isStatic())
1261 Out << 'K';
1262 else if (MD->isVirtual())
1263 Out << 'M';
1264 else
1265 Out << 'I';
1266 break;
1267 case AS_public:
1268 if (MD->isStatic())
1269 Out << 'S';
1270 else if (MD->isVirtual())
1271 Out << 'U';
1272 else
1273 Out << 'Q';
1274 }
1275 } else
1276 Out << 'Y';
1277}
1278void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
1279 bool IsInstMethod) {
1280 // <calling-convention> ::= A # __cdecl
1281 // ::= B # __export __cdecl
1282 // ::= C # __pascal
1283 // ::= D # __export __pascal
1284 // ::= E # __thiscall
1285 // ::= F # __export __thiscall
1286 // ::= G # __stdcall
1287 // ::= H # __export __stdcall
1288 // ::= I # __fastcall
1289 // ::= J # __export __fastcall
1290 // The 'export' calling conventions are from a bygone era
1291 // (*cough*Win16*cough*) when functions were declared for export with
1292 // that keyword. (It didn't actually export them, it just made them so
1293 // that they could be in a DLL and somebody from another module could call
1294 // them.)
1295 CallingConv CC = T->getCallConv();
1296 if (CC == CC_Default) {
1297 if (IsInstMethod) {
1298 const FunctionProtoType *FPT =
1299 T->getCanonicalTypeUnqualified().castAs<FunctionProtoType>();
1300 bool isVariadic = FPT->isVariadic();
1301 CC = getASTContext().getDefaultCXXMethodCallConv(isVariadic);
1302 } else {
1303 CC = CC_C;
1304 }
1305 }
1306 switch (CC) {
1307 default:
1308 llvm_unreachable("Unsupported CC for mangling");
1309 case CC_Default:
1310 case CC_C: Out << 'A'; break;
1311 case CC_X86Pascal: Out << 'C'; break;
1312 case CC_X86ThisCall: Out << 'E'; break;
1313 case CC_X86StdCall: Out << 'G'; break;
1314 case CC_X86FastCall: Out << 'I'; break;
1315 }
1316}
1317void MicrosoftCXXNameMangler::mangleThrowSpecification(
1318 const FunctionProtoType *FT) {
1319 // <throw-spec> ::= Z # throw(...) (default)
1320 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1321 // ::= <type>+
1322 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1323 // all actually mangled as 'Z'. (They're ignored because their associated
1324 // functionality isn't implemented, and probably never will be.)
1325 Out << 'Z';
1326}
1327
1328void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
1329 SourceRange Range) {
1330 // Probably should be mangled as a template instantiation; need to see what
1331 // VC does first.
1332 DiagnosticsEngine &Diags = Context.getDiags();
1333 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1334 "cannot mangle this unresolved dependent type yet");
1335 Diags.Report(Range.getBegin(), DiagID)
1336 << Range;
1337}
1338
1339// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1340// <union-type> ::= T <name>
1341// <struct-type> ::= U <name>
1342// <class-type> ::= V <name>
1343// <enum-type> ::= W <size> <name>
1344void MicrosoftCXXNameMangler::mangleType(const EnumType *T, SourceRange) {
1345 mangleType(cast<TagType>(T));
1346}
1347void MicrosoftCXXNameMangler::mangleType(const RecordType *T, SourceRange) {
1348 mangleType(cast<TagType>(T));
1349}
1350void MicrosoftCXXNameMangler::mangleType(const TagType *T) {
1351 switch (T->getDecl()->getTagKind()) {
1352 case TTK_Union:
1353 Out << 'T';
1354 break;
1355 case TTK_Struct:
1356 case TTK_Interface:
1357 Out << 'U';
1358 break;
1359 case TTK_Class:
1360 Out << 'V';
1361 break;
1362 case TTK_Enum:
1363 Out << 'W';
1364 Out << getASTContext().getTypeSizeInChars(
1365 cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity();
1366 break;
1367 }
1368 mangleName(T->getDecl());
1369}
1370
1371// <type> ::= <array-type>
1372// <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1373// [Y <dimension-count> <dimension>+]
1374// <element-type> # as global
1375// ::= Q <cvr-qualifiers> [Y <dimension-count> <dimension>+]
1376// <element-type> # as param
1377// It's supposed to be the other way around, but for some strange reason, it
1378// isn't. Today this behavior is retained for the sole purpose of backwards
1379// compatibility.
1380void MicrosoftCXXNameMangler::mangleType(const ArrayType *T, bool IsGlobal) {
1381 // This isn't a recursive mangling, so now we have to do it all in this
1382 // one call.
1383 if (IsGlobal) {
1384 manglePointerQualifiers(T->getElementType().getQualifiers());
1385 } else {
1386 Out << 'Q';
1387 }
1388 mangleExtraDimensions(T->getElementType());
1389}
1390void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
1391 SourceRange) {
1392 mangleType(cast<ArrayType>(T), false);
1393}
1394void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T,
1395 SourceRange) {
1396 mangleType(cast<ArrayType>(T), false);
1397}
1398void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T,
1399 SourceRange) {
1400 mangleType(cast<ArrayType>(T), false);
1401}
1402void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T,
1403 SourceRange) {
1404 mangleType(cast<ArrayType>(T), false);
1405}
1406void MicrosoftCXXNameMangler::mangleExtraDimensions(QualType ElementTy) {
1407 SmallVector<llvm::APInt, 3> Dimensions;
1408 for (;;) {
1409 if (const ConstantArrayType *CAT =
1410 getASTContext().getAsConstantArrayType(ElementTy)) {
1411 Dimensions.push_back(CAT->getSize());
1412 ElementTy = CAT->getElementType();
1413 } else if (ElementTy->isVariableArrayType()) {
1414 const VariableArrayType *VAT =
1415 getASTContext().getAsVariableArrayType(ElementTy);
1416 DiagnosticsEngine &Diags = Context.getDiags();
1417 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1418 "cannot mangle this variable-length array yet");
1419 Diags.Report(VAT->getSizeExpr()->getExprLoc(), DiagID)
1420 << VAT->getBracketsRange();
1421 return;
1422 } else if (ElementTy->isDependentSizedArrayType()) {
1423 // The dependent expression has to be folded into a constant (TODO).
1424 const DependentSizedArrayType *DSAT =
1425 getASTContext().getAsDependentSizedArrayType(ElementTy);
1426 DiagnosticsEngine &Diags = Context.getDiags();
1427 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1428 "cannot mangle this dependent-length array yet");
1429 Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
1430 << DSAT->getBracketsRange();
1431 return;
1432 } else if (ElementTy->isIncompleteArrayType()) continue;
1433 else break;
1434 }
1435 mangleQualifiers(ElementTy.getQualifiers(), false);
1436 // If there are any additional dimensions, mangle them now.
1437 if (Dimensions.size() > 0) {
1438 Out << 'Y';
1439 // <dimension-count> ::= <number> # number of extra dimensions
1440 mangleNumber(Dimensions.size());
1441 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim) {
1442 mangleNumber(Dimensions[Dim].getLimitedValue());
1443 }
1444 }
1445 mangleType(ElementTy.getLocalUnqualifiedType(), SourceRange());
1446}
1447
1448// <type> ::= <pointer-to-member-type>
1449// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1450// <class name> <type>
1451void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
1452 SourceRange Range) {
1453 QualType PointeeType = T->getPointeeType();
1454 if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
1455 Out << '8';
1456 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
1457 mangleType(FPT, NULL, false, true);
1458 } else {
1459 mangleQualifiers(PointeeType.getQualifiers(), true);
1460 mangleName(T->getClass()->castAs<RecordType>()->getDecl());
1461 mangleType(PointeeType.getLocalUnqualifiedType(), Range);
1462 }
1463}
1464
1465void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
1466 SourceRange Range) {
1467 DiagnosticsEngine &Diags = Context.getDiags();
1468 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1469 "cannot mangle this template type parameter type yet");
1470 Diags.Report(Range.getBegin(), DiagID)
1471 << Range;
1472}
1473
1474void MicrosoftCXXNameMangler::mangleType(
1475 const SubstTemplateTypeParmPackType *T,
1476 SourceRange Range) {
1477 DiagnosticsEngine &Diags = Context.getDiags();
1478 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1479 "cannot mangle this substituted parameter pack yet");
1480 Diags.Report(Range.getBegin(), DiagID)
1481 << Range;
1482}
1483
1484// <type> ::= <pointer-type>
1485// <pointer-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1486void MicrosoftCXXNameMangler::mangleType(const PointerType *T,
1487 SourceRange Range) {
1488 QualType PointeeTy = T->getPointeeType();
1489 if (PointeeTy->isArrayType()) {
1490 // Pointers to arrays are mangled like arrays.
1491 mangleExtraDimensions(PointeeTy);
1492 } else if (const FunctionType *FT = PointeeTy->getAs<FunctionType>()) {
1493 // Function pointers are special.
1494 Out << '6';
1495 mangleType(FT, NULL, false, false);
1496 } else {
1497 mangleQualifiers(PointeeTy.getQualifiers(), false);
1498 mangleType(PointeeTy, Range, false);
1499 }
1500}
1501void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T,
1502 SourceRange Range) {
1503 // Object pointers never have qualifiers.
1504 Out << 'A';
1505 mangleType(T->getPointeeType(), Range);
1506}
1507
1508// <type> ::= <reference-type>
1509// <reference-type> ::= A <cvr-qualifiers> <type>
1510void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
1511 SourceRange Range) {
1512 Out << 'A';
1513 QualType PointeeTy = T->getPointeeType();
1514 if (!PointeeTy.hasQualifiers())
1515 // Lack of qualifiers is mangled as 'A'.
1516 Out << 'A';
1517 mangleType(PointeeTy, Range);
1518}
1519
1520// <type> ::= <r-value-reference-type>
1521// <r-value-reference-type> ::= $$Q <cvr-qualifiers> <type>
1522void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
1523 SourceRange Range) {
1524 Out << "$$Q";
1525 QualType PointeeTy = T->getPointeeType();
1526 if (!PointeeTy.hasQualifiers())
1527 // Lack of qualifiers is mangled as 'A'.
1528 Out << 'A';
1529 mangleType(PointeeTy, Range);
1530}
1531
1532void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
1533 SourceRange Range) {
1534 DiagnosticsEngine &Diags = Context.getDiags();
1535 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1536 "cannot mangle this complex number type yet");
1537 Diags.Report(Range.getBegin(), DiagID)
1538 << Range;
1539}
1540
1541void MicrosoftCXXNameMangler::mangleType(const VectorType *T,
1542 SourceRange Range) {
1543 DiagnosticsEngine &Diags = Context.getDiags();
1544 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1545 "cannot mangle this vector type yet");
1546 Diags.Report(Range.getBegin(), DiagID)
1547 << Range;
1548}
1549void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
1550 SourceRange Range) {
1551 DiagnosticsEngine &Diags = Context.getDiags();
1552 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1553 "cannot mangle this extended vector type yet");
1554 Diags.Report(Range.getBegin(), DiagID)
1555 << Range;
1556}
1557void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
1558 SourceRange Range) {
1559 DiagnosticsEngine &Diags = Context.getDiags();
1560 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1561 "cannot mangle this dependent-sized extended vector type yet");
1562 Diags.Report(Range.getBegin(), DiagID)
1563 << Range;
1564}
1565
1566void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T,
1567 SourceRange) {
1568 // ObjC interfaces have structs underlying them.
1569 Out << 'U';
1570 mangleName(T->getDecl());
1571}
1572
1573void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T,
1574 SourceRange Range) {
1575 // We don't allow overloading by different protocol qualification,
1576 // so mangling them isn't necessary.
1577 mangleType(T->getBaseType(), Range);
1578}
1579
1580void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T,
1581 SourceRange Range) {
1582 Out << "_E";
1583
1584 QualType pointee = T->getPointeeType();
1585 mangleType(pointee->castAs<FunctionProtoType>(), NULL, false, false);
1586}
1587
1588void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T,
1589 SourceRange Range) {
1590 DiagnosticsEngine &Diags = Context.getDiags();
1591 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1592 "cannot mangle this injected class name type yet");
1593 Diags.Report(Range.getBegin(), DiagID)
1594 << Range;
1595}
1596
1597void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
1598 SourceRange Range) {
1599 DiagnosticsEngine &Diags = Context.getDiags();
1600 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1601 "cannot mangle this template specialization type yet");
1602 Diags.Report(Range.getBegin(), DiagID)
1603 << Range;
1604}
1605
1606void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T,
1607 SourceRange Range) {
1608 DiagnosticsEngine &Diags = Context.getDiags();
1609 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1610 "cannot mangle this dependent name type yet");
1611 Diags.Report(Range.getBegin(), DiagID)
1612 << Range;
1613}
1614
1615void MicrosoftCXXNameMangler::mangleType(
1616 const DependentTemplateSpecializationType *T,
1617 SourceRange Range) {
1618 DiagnosticsEngine &Diags = Context.getDiags();
1619 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1620 "cannot mangle this dependent template specialization type yet");
1621 Diags.Report(Range.getBegin(), DiagID)
1622 << Range;
1623}
1624
1625void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T,
1626 SourceRange Range) {
1627 DiagnosticsEngine &Diags = Context.getDiags();
1628 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1629 "cannot mangle this pack expansion yet");
1630 Diags.Report(Range.getBegin(), DiagID)
1631 << Range;
1632}
1633
1634void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T,
1635 SourceRange Range) {
1636 DiagnosticsEngine &Diags = Context.getDiags();
1637 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1638 "cannot mangle this typeof(type) yet");
1639 Diags.Report(Range.getBegin(), DiagID)
1640 << Range;
1641}
1642
1643void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T,
1644 SourceRange Range) {
1645 DiagnosticsEngine &Diags = Context.getDiags();
1646 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1647 "cannot mangle this typeof(expression) yet");
1648 Diags.Report(Range.getBegin(), DiagID)
1649 << Range;
1650}
1651
1652void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T,
1653 SourceRange Range) {
1654 DiagnosticsEngine &Diags = Context.getDiags();
1655 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1656 "cannot mangle this decltype() yet");
1657 Diags.Report(Range.getBegin(), DiagID)
1658 << Range;
1659}
1660
1661void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
1662 SourceRange Range) {
1663 DiagnosticsEngine &Diags = Context.getDiags();
1664 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1665 "cannot mangle this unary transform type yet");
1666 Diags.Report(Range.getBegin(), DiagID)
1667 << Range;
1668}
1669
1670void MicrosoftCXXNameMangler::mangleType(const AutoType *T, SourceRange Range) {
1671 DiagnosticsEngine &Diags = Context.getDiags();
1672 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1673 "cannot mangle this 'auto' type yet");
1674 Diags.Report(Range.getBegin(), DiagID)
1675 << Range;
1676}
1677
1678void MicrosoftCXXNameMangler::mangleType(const AtomicType *T,
1679 SourceRange Range) {
1680 DiagnosticsEngine &Diags = Context.getDiags();
1681 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1682 "cannot mangle this C11 atomic type yet");
1683 Diags.Report(Range.getBegin(), DiagID)
1684 << Range;
1685}
1686
1687void MicrosoftMangleContext::mangleName(const NamedDecl *D,
1688 raw_ostream &Out) {
1689 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1690 "Invalid mangleName() call, argument is not a variable or function!");
1691 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1692 "Invalid mangleName() call on 'structor decl!");
1693
1694 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1695 getASTContext().getSourceManager(),
1696 "Mangling declaration");
1697
1698 MicrosoftCXXNameMangler Mangler(*this, Out);
1699 return Mangler.mangle(D);
1700}
1701void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1702 const ThunkInfo &Thunk,
1703 raw_ostream &) {
1704 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1705 "cannot mangle thunk for this method yet");
1706 getDiags().Report(MD->getLocation(), DiagID);
1707}
1708void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1709 CXXDtorType Type,
1710 const ThisAdjustment &,
1711 raw_ostream &) {
1712 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1713 "cannot mangle thunk for this destructor yet");
1714 getDiags().Report(DD->getLocation(), DiagID);
1715}
1716void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
1717 raw_ostream &Out) {
1718 // <mangled-name> ::= ? <operator-name> <class-name> <storage-class>
1719 // <cvr-qualifiers> [<name>] @
1720 // <operator-name> ::= _7 # vftable
1721 // ::= _8 # vbtable
1722 // NOTE: <cvr-qualifiers> here is always 'B' (const). <storage-class>
1723 // is always '6' for vftables and '7' for vbtables. (The difference is
1724 // beyond me.)
1725 // TODO: vbtables.
1726 MicrosoftCXXNameMangler Mangler(*this, Out);
1727 Mangler.getStream() << "\01??_7";
1728 Mangler.mangleName(RD);
1729 Mangler.getStream() << "6B";
1730 // TODO: If the class has more than one vtable, mangle in the class it came
1731 // from.
1732 Mangler.getStream() << '@';
1733}
1734void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
1735 raw_ostream &) {
1736 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1737}
1738void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1739 int64_t Offset,
1740 const CXXRecordDecl *Type,
1741 raw_ostream &) {
1742 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1743}
1744void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
1745 raw_ostream &) {
1746 // FIXME: Give a location...
1747 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1748 "cannot mangle RTTI descriptors for type %0 yet");
1749 getDiags().Report(DiagID)
1750 << T.getBaseTypeIdentifier();
1751}
1752void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
1753 raw_ostream &) {
1754 // FIXME: Give a location...
1755 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1756 "cannot mangle the name of type %0 into RTTI descriptors yet");
1757 getDiags().Report(DiagID)
1758 << T.getBaseTypeIdentifier();
1759}
1760void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1761 CXXCtorType Type,
1762 raw_ostream & Out) {
1763 MicrosoftCXXNameMangler mangler(*this, Out);
1764 mangler.mangle(D);
1765}
1766void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1767 CXXDtorType Type,
1768 raw_ostream & Out) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001769 MicrosoftCXXNameMangler mangler(*this, Out, D, Type);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001770 mangler.mangle(D);
1771}
1772void MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *VD,
1773 raw_ostream &) {
1774 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
1775 "cannot mangle this reference temporary yet");
1776 getDiags().Report(VD->getLocation(), DiagID);
1777}
1778
1779MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
1780 DiagnosticsEngine &Diags) {
1781 return new MicrosoftMangleContext(Context, Diags);
1782}