blob: 6918ce482641f4f24e1f35e7e73202eb5cfebccb [file] [log] [blame]
Peter Collingbourne14110472011-01-13 18:57:25 +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//
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000010// This provides C++ name mangling targeting the Microsoft Visual C++ ABI.
Peter Collingbourne14110472011-01-13 18:57:25 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Mangle.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/CharUnits.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
21#include "clang/AST/ExprCXX.h"
22#include "clang/Basic/ABI.h"
23
24using namespace clang;
25
26namespace {
27
28/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the
29/// Microsoft Visual C++ ABI.
30class MicrosoftCXXNameMangler {
31 MangleContext &Context;
Chris Lattner5f9e2722011-07-23 10:55:15 +000032 raw_ostream &Out;
Peter Collingbourne14110472011-01-13 18:57:25 +000033
34 ASTContext &getASTContext() const { return Context.getASTContext(); }
35
36public:
Chris Lattner5f9e2722011-07-23 10:55:15 +000037 MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_)
Rafael Espindolac4850c22011-02-10 23:59:36 +000038 : Context(C), Out(Out_) { }
Peter Collingbourne14110472011-01-13 18:57:25 +000039
Chris Lattner5f9e2722011-07-23 10:55:15 +000040 void mangle(const NamedDecl *D, StringRef Prefix = "?");
Peter Collingbourne14110472011-01-13 18:57:25 +000041 void mangleName(const NamedDecl *ND);
42 void mangleFunctionEncoding(const FunctionDecl *FD);
43 void mangleVariableEncoding(const VarDecl *VD);
44 void mangleNumber(int64_t Number);
Charles Davis9fd23592012-05-26 23:12:19 +000045 void mangleNumber(const llvm::APSInt &Value);
Peter Collingbourne14110472011-01-13 18:57:25 +000046 void mangleType(QualType T);
47
48private:
49 void mangleUnqualifiedName(const NamedDecl *ND) {
50 mangleUnqualifiedName(ND, ND->getDeclName());
51 }
52 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name);
53 void mangleSourceName(const IdentifierInfo *II);
54 void manglePostfix(const DeclContext *DC, bool NoFunction=false);
55 void mangleOperatorName(OverloadedOperatorKind OO);
56 void mangleQualifiers(Qualifiers Quals, bool IsMember);
57
Charles Davis9fd23592012-05-26 23:12:19 +000058 void mangleUnscopedTemplateName(const TemplateDecl *ND);
59 void mangleTemplateInstantiationName(const TemplateDecl *TD,
60 const TemplateArgument *TemplateArgs,
61 unsigned NumTemplateArgs,
62 SourceLocation InstantiationLoc);
Peter Collingbourne14110472011-01-13 18:57:25 +000063 void mangleObjCMethodName(const ObjCMethodDecl *MD);
64
65 // Declare manglers for every type class.
66#define ABSTRACT_TYPE(CLASS, PARENT)
67#define NON_CANONICAL_TYPE(CLASS, PARENT)
68#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
69#include "clang/AST/TypeNodes.def"
70
71 void mangleType(const TagType*);
72 void mangleType(const FunctionType *T, const FunctionDecl *D,
73 bool IsStructor, bool IsInstMethod);
74 void mangleType(const ArrayType *T, bool IsGlobal);
75 void mangleExtraDimensions(QualType T);
76 void mangleFunctionClass(const FunctionDecl *FD);
77 void mangleCallingConvention(const FunctionType *T, bool IsInstMethod = false);
Charles Davis9fd23592012-05-26 23:12:19 +000078 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Number);
Peter Collingbourne14110472011-01-13 18:57:25 +000079 void mangleThrowSpecification(const FunctionProtoType *T);
80
Charles Davis9fd23592012-05-26 23:12:19 +000081 void mangleTemplateArgs(const TemplateArgument *TemplateArgs, unsigned NumTemplateArgs,
82 SourceLocation InstantiationLoc);
83
Peter Collingbourne14110472011-01-13 18:57:25 +000084};
85
86/// MicrosoftMangleContext - Overrides the default MangleContext for the
87/// Microsoft Visual C++ ABI.
88class MicrosoftMangleContext : public MangleContext {
89public:
90 MicrosoftMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +000091 DiagnosticsEngine &Diags) : MangleContext(Context, Diags) { }
Peter Collingbourne14110472011-01-13 18:57:25 +000092 virtual bool shouldMangleDeclName(const NamedDecl *D);
Chris Lattner5f9e2722011-07-23 10:55:15 +000093 virtual void mangleName(const NamedDecl *D, raw_ostream &Out);
Peter Collingbourne14110472011-01-13 18:57:25 +000094 virtual void mangleThunk(const CXXMethodDecl *MD,
95 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +000096 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000097 virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
98 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +000099 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000100 virtual void mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000101 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000102 virtual void mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000103 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000104 virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
105 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000106 raw_ostream &);
107 virtual void mangleCXXRTTI(QualType T, raw_ostream &);
108 virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000109 virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000110 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000111 virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000112 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000113 virtual void mangleReferenceTemporary(const clang::VarDecl *,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000114 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000115};
116
117}
118
119static bool isInCLinkageSpecification(const Decl *D) {
120 D = D->getCanonicalDecl();
121 for (const DeclContext *DC = D->getDeclContext();
122 !DC->isTranslationUnit(); DC = DC->getParent()) {
123 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
124 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
125 }
126
127 return false;
128}
129
130bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) {
131 // In C, functions with no attributes never need to be mangled. Fastpath them.
David Blaikie4e4d0842012-03-11 07:00:24 +0000132 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
Peter Collingbourne14110472011-01-13 18:57:25 +0000133 return false;
134
135 // Any decl can be declared with __asm("foo") on it, and this takes precedence
136 // over all other naming in the .o file.
137 if (D->hasAttr<AsmLabelAttr>())
138 return true;
139
140 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
141 // (always) as does passing a C++ member function and a function
142 // whose name is not a simple identifier.
143 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
144 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
145 !FD->getDeclName().isIdentifier()))
146 return true;
147
148 // Otherwise, no mangling is done outside C++ mode.
David Blaikie4e4d0842012-03-11 07:00:24 +0000149 if (!getASTContext().getLangOpts().CPlusPlus)
Peter Collingbourne14110472011-01-13 18:57:25 +0000150 return false;
151
152 // Variables at global scope with internal linkage are not mangled.
153 if (!FD) {
154 const DeclContext *DC = D->getDeclContext();
155 if (DC->isTranslationUnit() && D->getLinkage() == InternalLinkage)
156 return false;
157 }
158
159 // C functions and "main" are not mangled.
160 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
161 return false;
162
163 return true;
164}
165
166void MicrosoftCXXNameMangler::mangle(const NamedDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000167 StringRef Prefix) {
Peter Collingbourne14110472011-01-13 18:57:25 +0000168 // MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
169 // Therefore it's really important that we don't decorate the
170 // name with leading underscores or leading/trailing at signs. So, emit a
171 // asm marker at the start so we get the name right.
172 Out << '\01'; // LLVM IR Marker for __asm("foo")
173
174 // Any decl can be declared with __asm("foo") on it, and this takes precedence
175 // over all other naming in the .o file.
176 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
177 // If we have an asm name, then we use it as the mangling.
178 Out << ALA->getLabel();
179 return;
180 }
181
182 // <mangled-name> ::= ? <name> <type-encoding>
183 Out << Prefix;
184 mangleName(D);
185 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
186 mangleFunctionEncoding(FD);
187 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
188 mangleVariableEncoding(VD);
189 // TODO: Fields? Can MSVC even mangle them?
190}
191
192void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
193 // <type-encoding> ::= <function-class> <function-type>
194
195 // Don't mangle in the type if this isn't a decl we should typically mangle.
196 if (!Context.shouldMangleDeclName(FD))
197 return;
198
199 // We should never ever see a FunctionNoProtoType at this point.
200 // We don't even know how to mangle their types anyway :).
201 const FunctionProtoType *FT = cast<FunctionProtoType>(FD->getType());
202
203 bool InStructor = false, InInstMethod = false;
204 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
205 if (MD) {
206 if (MD->isInstance())
207 InInstMethod = true;
208 if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))
209 InStructor = true;
210 }
211
212 // First, the function class.
213 mangleFunctionClass(FD);
214
215 mangleType(FT, FD, InStructor, InInstMethod);
216}
217
218void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) {
219 // <type-encoding> ::= <storage-class> <variable-type>
220 // <storage-class> ::= 0 # private static member
221 // ::= 1 # protected static member
222 // ::= 2 # public static member
223 // ::= 3 # global
224 // ::= 4 # static local
225
226 // The first character in the encoding (after the name) is the storage class.
227 if (VD->isStaticDataMember()) {
228 // If it's a static member, it also encodes the access level.
229 switch (VD->getAccess()) {
230 default:
231 case AS_private: Out << '0'; break;
232 case AS_protected: Out << '1'; break;
233 case AS_public: Out << '2'; break;
234 }
235 }
236 else if (!VD->isStaticLocal())
237 Out << '3';
238 else
239 Out << '4';
240 // Now mangle the type.
241 // <variable-type> ::= <type> <cvr-qualifiers>
242 // ::= <type> A # pointers, references, arrays
243 // Pointers and references are odd. The type of 'int * const foo;' gets
244 // mangled as 'QAHA' instead of 'PAHB', for example.
245 QualType Ty = VD->getType();
246 if (Ty->isPointerType() || Ty->isReferenceType()) {
247 mangleType(Ty);
248 Out << 'A';
249 } else if (Ty->isArrayType()) {
250 // Global arrays are funny, too.
John McCallf4c73712011-01-19 06:33:43 +0000251 mangleType(cast<ArrayType>(Ty.getTypePtr()), true);
Peter Collingbourne14110472011-01-13 18:57:25 +0000252 Out << 'A';
253 } else {
254 mangleType(Ty.getLocalUnqualifiedType());
255 mangleQualifiers(Ty.getLocalQualifiers(), false);
256 }
257}
258
259void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) {
260 // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @
261 const DeclContext *DC = ND->getDeclContext();
262
263 // Always start with the unqualified name.
264 mangleUnqualifiedName(ND);
265
266 // If this is an extern variable declared locally, the relevant DeclContext
267 // is that of the containing namespace, or the translation unit.
268 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
269 while (!DC->isNamespace() && !DC->isTranslationUnit())
270 DC = DC->getParent();
271
272 manglePostfix(DC);
273
274 // Terminate the whole name with an '@'.
275 Out << '@';
276}
277
278void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
Charles Davis9fd23592012-05-26 23:12:19 +0000279 // <number> ::= [?] <decimal digit> # 1 <= Number <= 10
280 // ::= [?] <hex digit>+ @ # 0 or > 9; A = 0, B = 1, etc...
281 // ::= [?] @ # 0 (alternate mangling, not emitted by VC)
Peter Collingbourne14110472011-01-13 18:57:25 +0000282 if (Number < 0) {
283 Out << '?';
284 Number = -Number;
285 }
Charles Davis9fd23592012-05-26 23:12:19 +0000286 // Oddly enough, there's a special shorter mangling for 0, but Microsoft chose not
287 // to use it. Instead, 0 gets mangled as "A@". Oh well...
288 if (Number >= 1 && Number <= 10)
Peter Collingbourne14110472011-01-13 18:57:25 +0000289 Out << Number-1;
Charles Davis9fd23592012-05-26 23:12:19 +0000290 else {
Peter Collingbourne14110472011-01-13 18:57:25 +0000291 // We have to build up the encoding in reverse order, so it will come
292 // out right when we write it out.
293 char Encoding[16];
294 char *EndPtr = Encoding+sizeof(Encoding);
295 char *CurPtr = EndPtr;
Charles Davis9fd23592012-05-26 23:12:19 +0000296 do {
Peter Collingbourne14110472011-01-13 18:57:25 +0000297 *--CurPtr = 'A' + (Number % 16);
298 Number /= 16;
Charles Davis9fd23592012-05-26 23:12:19 +0000299 } while (Number);
Peter Collingbourne14110472011-01-13 18:57:25 +0000300 Out.write(CurPtr, EndPtr-CurPtr);
301 Out << '@';
302 }
303}
304
Charles Davis9fd23592012-05-26 23:12:19 +0000305void MicrosoftCXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
306 if (Value.isSigned() && Value.isNegative()) {
307 Out << '?';
308 mangleNumber(llvm::APSInt(Value.abs()));
309 return;
310 }
311 if (Value.uge(1) && Value.ule(10))
312 (Value-llvm::APSInt(llvm::APInt(Value.getBitWidth(), 1, Value.isSigned()))).print(Out,
313 false);
314 else {
315 // We have to build up the encoding in reverse order, so it will come
316 // out right when we write it out.
317 char Encoding[64];
318 char *EndPtr = Encoding+sizeof(Encoding);
319 char *CurPtr = EndPtr;
320 llvm::APSInt Fifteen(Value.getBitWidth(), 15);
321 for (int i = 0, e = Value.getActiveBits() / 4; i != e; ++i) {
322 *--CurPtr = 'A' + Value.And(Fifteen).lshr(i*4).getLimitedValue(15);
323 Fifteen = Fifteen.shl(4);
324 };
325 Out.write(CurPtr, EndPtr-CurPtr);
326 Out << '@';
327 }
328}
329
330static const TemplateDecl *
331isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
332 // Check if we have a function template.
333 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
334 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
335 TemplateArgs = FD->getTemplateSpecializationArgs();
336 return TD;
337 }
338 }
339
340 // Check if we have a class template.
341 if (const ClassTemplateSpecializationDecl *Spec =
342 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
343 TemplateArgs = &Spec->getTemplateArgs();
344 return Spec->getSpecializedTemplate();
345 }
346
347 return 0;
348}
349
Peter Collingbourne14110472011-01-13 18:57:25 +0000350void
351MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
352 DeclarationName Name) {
353 // <unqualified-name> ::= <operator-name>
354 // ::= <ctor-dtor-name>
355 // ::= <source-name>
Charles Davis9fd23592012-05-26 23:12:19 +0000356 // ::= <template-name>
357 const TemplateArgumentList *TemplateArgs;
358 // Check if we have a template.
359 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
360 mangleTemplateInstantiationName(TD, TemplateArgs->data(), TemplateArgs->size(),
361 ND->getLocation());
362 return;
363 }
364
Peter Collingbourne14110472011-01-13 18:57:25 +0000365 switch (Name.getNameKind()) {
366 case DeclarationName::Identifier: {
367 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
368 mangleSourceName(II);
369 break;
370 }
371
372 // Otherwise, an anonymous entity. We must have a declaration.
373 assert(ND && "mangling empty name without declaration");
374
375 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
376 if (NS->isAnonymousNamespace()) {
377 Out << "?A";
378 break;
379 }
380 }
381
382 // We must have an anonymous struct.
383 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smith162e1c12011-04-15 14:24:37 +0000384 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Peter Collingbourne14110472011-01-13 18:57:25 +0000385 assert(TD->getDeclContext() == D->getDeclContext() &&
386 "Typedef should not be in another decl context!");
387 assert(D->getDeclName().getAsIdentifierInfo() &&
388 "Typedef was not named!");
389 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
390 break;
391 }
392
393 // When VC encounters an anonymous type with no tag and no typedef,
394 // it literally emits '<unnamed-tag>'.
395 Out << "<unnamed-tag>";
396 break;
397 }
398
399 case DeclarationName::ObjCZeroArgSelector:
400 case DeclarationName::ObjCOneArgSelector:
401 case DeclarationName::ObjCMultiArgSelector:
David Blaikieb219cfc2011-09-23 05:06:16 +0000402 llvm_unreachable("Can't mangle Objective-C selector names here!");
Peter Collingbourne14110472011-01-13 18:57:25 +0000403
404 case DeclarationName::CXXConstructorName:
Michael J. Spencer50118da2011-12-01 09:55:00 +0000405 Out << "?0";
406 break;
Peter Collingbourne14110472011-01-13 18:57:25 +0000407
408 case DeclarationName::CXXDestructorName:
Michael J. Spencer50118da2011-12-01 09:55:00 +0000409 Out << "?1";
410 break;
Peter Collingbourne14110472011-01-13 18:57:25 +0000411
412 case DeclarationName::CXXConversionFunctionName:
413 // <operator-name> ::= ?B # (cast)
414 // The target type is encoded as the return type.
415 Out << "?B";
416 break;
417
418 case DeclarationName::CXXOperatorName:
419 mangleOperatorName(Name.getCXXOverloadedOperator());
420 break;
421
422 case DeclarationName::CXXLiteralOperatorName:
423 // FIXME: Was this added in VS2010? Does MS even know how to mangle this?
David Blaikieb219cfc2011-09-23 05:06:16 +0000424 llvm_unreachable("Don't know how to mangle literal operators yet!");
Peter Collingbourne14110472011-01-13 18:57:25 +0000425
426 case DeclarationName::CXXUsingDirective:
David Blaikieb219cfc2011-09-23 05:06:16 +0000427 llvm_unreachable("Can't mangle a using directive name!");
Peter Collingbourne14110472011-01-13 18:57:25 +0000428 }
429}
430
431void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC,
432 bool NoFunction) {
433 // <postfix> ::= <unqualified-name> [<postfix>]
Peter Collingbourne14110472011-01-13 18:57:25 +0000434 // ::= <template-param>
435 // ::= <substitution> [<postfix>]
436
437 if (!DC) return;
438
439 while (isa<LinkageSpecDecl>(DC))
440 DC = DC->getParent();
441
442 if (DC->isTranslationUnit())
443 return;
444
445 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
Rafael Espindolac4850c22011-02-10 23:59:36 +0000446 Context.mangleBlock(BD, Out);
447 Out << '@';
Peter Collingbourne14110472011-01-13 18:57:25 +0000448 return manglePostfix(DC->getParent(), NoFunction);
449 }
450
451 if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
452 return;
453 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
454 mangleObjCMethodName(Method);
455 else {
456 mangleUnqualifiedName(cast<NamedDecl>(DC));
457 manglePostfix(DC->getParent(), NoFunction);
458 }
459}
460
461void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO) {
462 switch (OO) {
463 // ?0 # constructor
464 // ?1 # destructor
465 // <operator-name> ::= ?2 # new
466 case OO_New: Out << "?2"; break;
467 // <operator-name> ::= ?3 # delete
468 case OO_Delete: Out << "?3"; break;
469 // <operator-name> ::= ?4 # =
470 case OO_Equal: Out << "?4"; break;
471 // <operator-name> ::= ?5 # >>
472 case OO_GreaterGreater: Out << "?5"; break;
473 // <operator-name> ::= ?6 # <<
474 case OO_LessLess: Out << "?6"; break;
475 // <operator-name> ::= ?7 # !
476 case OO_Exclaim: Out << "?7"; break;
477 // <operator-name> ::= ?8 # ==
478 case OO_EqualEqual: Out << "?8"; break;
479 // <operator-name> ::= ?9 # !=
480 case OO_ExclaimEqual: Out << "?9"; break;
481 // <operator-name> ::= ?A # []
482 case OO_Subscript: Out << "?A"; break;
483 // ?B # conversion
484 // <operator-name> ::= ?C # ->
485 case OO_Arrow: Out << "?C"; break;
486 // <operator-name> ::= ?D # *
487 case OO_Star: Out << "?D"; break;
488 // <operator-name> ::= ?E # ++
489 case OO_PlusPlus: Out << "?E"; break;
490 // <operator-name> ::= ?F # --
491 case OO_MinusMinus: Out << "?F"; break;
492 // <operator-name> ::= ?G # -
493 case OO_Minus: Out << "?G"; break;
494 // <operator-name> ::= ?H # +
495 case OO_Plus: Out << "?H"; break;
496 // <operator-name> ::= ?I # &
497 case OO_Amp: Out << "?I"; break;
498 // <operator-name> ::= ?J # ->*
499 case OO_ArrowStar: Out << "?J"; break;
500 // <operator-name> ::= ?K # /
501 case OO_Slash: Out << "?K"; break;
502 // <operator-name> ::= ?L # %
503 case OO_Percent: Out << "?L"; break;
504 // <operator-name> ::= ?M # <
505 case OO_Less: Out << "?M"; break;
506 // <operator-name> ::= ?N # <=
507 case OO_LessEqual: Out << "?N"; break;
508 // <operator-name> ::= ?O # >
509 case OO_Greater: Out << "?O"; break;
510 // <operator-name> ::= ?P # >=
511 case OO_GreaterEqual: Out << "?P"; break;
512 // <operator-name> ::= ?Q # ,
513 case OO_Comma: Out << "?Q"; break;
514 // <operator-name> ::= ?R # ()
515 case OO_Call: Out << "?R"; break;
516 // <operator-name> ::= ?S # ~
517 case OO_Tilde: Out << "?S"; break;
518 // <operator-name> ::= ?T # ^
519 case OO_Caret: Out << "?T"; break;
520 // <operator-name> ::= ?U # |
521 case OO_Pipe: Out << "?U"; break;
522 // <operator-name> ::= ?V # &&
523 case OO_AmpAmp: Out << "?V"; break;
524 // <operator-name> ::= ?W # ||
525 case OO_PipePipe: Out << "?W"; break;
526 // <operator-name> ::= ?X # *=
527 case OO_StarEqual: Out << "?X"; break;
528 // <operator-name> ::= ?Y # +=
529 case OO_PlusEqual: Out << "?Y"; break;
530 // <operator-name> ::= ?Z # -=
531 case OO_MinusEqual: Out << "?Z"; break;
532 // <operator-name> ::= ?_0 # /=
533 case OO_SlashEqual: Out << "?_0"; break;
534 // <operator-name> ::= ?_1 # %=
535 case OO_PercentEqual: Out << "?_1"; break;
536 // <operator-name> ::= ?_2 # >>=
537 case OO_GreaterGreaterEqual: Out << "?_2"; break;
538 // <operator-name> ::= ?_3 # <<=
539 case OO_LessLessEqual: Out << "?_3"; break;
540 // <operator-name> ::= ?_4 # &=
541 case OO_AmpEqual: Out << "?_4"; break;
542 // <operator-name> ::= ?_5 # |=
543 case OO_PipeEqual: Out << "?_5"; break;
544 // <operator-name> ::= ?_6 # ^=
545 case OO_CaretEqual: Out << "?_6"; break;
546 // ?_7 # vftable
547 // ?_8 # vbtable
548 // ?_9 # vcall
549 // ?_A # typeof
550 // ?_B # local static guard
551 // ?_C # string
552 // ?_D # vbase destructor
553 // ?_E # vector deleting destructor
554 // ?_F # default constructor closure
555 // ?_G # scalar deleting destructor
556 // ?_H # vector constructor iterator
557 // ?_I # vector destructor iterator
558 // ?_J # vector vbase constructor iterator
559 // ?_K # virtual displacement map
560 // ?_L # eh vector constructor iterator
561 // ?_M # eh vector destructor iterator
562 // ?_N # eh vector vbase constructor iterator
563 // ?_O # copy constructor closure
564 // ?_P<name> # udt returning <name>
565 // ?_Q # <unknown>
566 // ?_R0 # RTTI Type Descriptor
567 // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
568 // ?_R2 # RTTI Base Class Array
569 // ?_R3 # RTTI Class Hierarchy Descriptor
570 // ?_R4 # RTTI Complete Object Locator
571 // ?_S # local vftable
572 // ?_T # local vftable constructor closure
573 // <operator-name> ::= ?_U # new[]
574 case OO_Array_New: Out << "?_U"; break;
575 // <operator-name> ::= ?_V # delete[]
576 case OO_Array_Delete: Out << "?_V"; break;
577
578 case OO_Conditional:
David Blaikieb219cfc2011-09-23 05:06:16 +0000579 llvm_unreachable("Don't know how to mangle ?:");
Peter Collingbourne14110472011-01-13 18:57:25 +0000580
581 case OO_None:
582 case NUM_OVERLOADED_OPERATORS:
David Blaikieb219cfc2011-09-23 05:06:16 +0000583 llvm_unreachable("Not an overloaded operator");
Peter Collingbourne14110472011-01-13 18:57:25 +0000584 }
585}
586
587void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
588 // <source name> ::= <identifier> @
589 Out << II->getName() << '@';
590}
591
Charles Davis9fd23592012-05-26 23:12:19 +0000592void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(const TemplateDecl *TD,
593 const TemplateArgument *TemplateArgs,
594 unsigned NumTemplateArgs,
595 SourceLocation InstantiationLoc) {
596 // <template-name> ::= <unscoped-template-name> <template-args>
597 // ::= <substitution>
598 // Always start with the unqualified name.
599 mangleUnscopedTemplateName(TD);
600 mangleTemplateArgs(TemplateArgs, NumTemplateArgs, InstantiationLoc);
601}
602
Peter Collingbourne14110472011-01-13 18:57:25 +0000603void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000604 Context.mangleObjCMethodName(MD, Out);
Peter Collingbourne14110472011-01-13 18:57:25 +0000605}
606
Charles Davis9fd23592012-05-26 23:12:19 +0000607void
608MicrosoftCXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *TD) {
609 // <unscoped-template-name> ::= ?$ <unqualified-name>
610 Out << "?$";
611 mangleUnqualifiedName(TD);
612}
613
614void
615MicrosoftCXXNameMangler::mangleIntegerLiteral(QualType T, const llvm::APSInt &Value) {
616 // <integer-literal> ::= $0 <number>
617 Out << "$0";
618 // Make sure booleans are encoded as 0/1.
619 if (T->isBooleanType())
620 Out << (Value.getBoolValue() ? "0" : "A@");
621 else
622 mangleNumber(Value);
623}
624
625void
626MicrosoftCXXNameMangler::mangleTemplateArgs(const TemplateArgument *TemplateArgs,
627 unsigned NumTemplateArgs,
628 SourceLocation InstantiationLoc) {
629 // <template-args> ::= {<type> | <integer-literal>}+ @
630 for (unsigned int i = 0; i < NumTemplateArgs; ++i) {
631 const TemplateArgument &TA = TemplateArgs[i];
632 switch (TA.getKind()) {
633 case TemplateArgument::Null:
634 llvm_unreachable("Can't mangle null template arguments!");
635 case TemplateArgument::Type:
636 mangleType(TA.getAsType());
637 break;
638 case TemplateArgument::Integral:
639 mangleIntegerLiteral(TA.getIntegralType(), *TA.getAsIntegral());
640 break;
641 default: {
642 // Issue a diagnostic.
643 DiagnosticsEngine &Diags = Context.getDiags();
644 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
645 "cannot yet mangle this %select{null|type|pointer/reference|integral|template|"
646 "template pack expansion|expression|parameter pack}0 template argument");
647 Diags.Report(InstantiationLoc, DiagID)
648 << TA.getKind();
649 }
650 }
651 }
652 Out << '@';
653}
654
Peter Collingbourne14110472011-01-13 18:57:25 +0000655void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals,
656 bool IsMember) {
657 // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers>
658 // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only);
659 // 'I' means __restrict (32/64-bit).
660 // Note that the MSVC __restrict keyword isn't the same as the C99 restrict
661 // keyword!
662 // <base-cvr-qualifiers> ::= A # near
663 // ::= B # near const
664 // ::= C # near volatile
665 // ::= D # near const volatile
666 // ::= E # far (16-bit)
667 // ::= F # far const (16-bit)
668 // ::= G # far volatile (16-bit)
669 // ::= H # far const volatile (16-bit)
670 // ::= I # huge (16-bit)
671 // ::= J # huge const (16-bit)
672 // ::= K # huge volatile (16-bit)
673 // ::= L # huge const volatile (16-bit)
674 // ::= M <basis> # based
675 // ::= N <basis> # based const
676 // ::= O <basis> # based volatile
677 // ::= P <basis> # based const volatile
678 // ::= Q # near member
679 // ::= R # near const member
680 // ::= S # near volatile member
681 // ::= T # near const volatile member
682 // ::= U # far member (16-bit)
683 // ::= V # far const member (16-bit)
684 // ::= W # far volatile member (16-bit)
685 // ::= X # far const volatile member (16-bit)
686 // ::= Y # huge member (16-bit)
687 // ::= Z # huge const member (16-bit)
688 // ::= 0 # huge volatile member (16-bit)
689 // ::= 1 # huge const volatile member (16-bit)
690 // ::= 2 <basis> # based member
691 // ::= 3 <basis> # based const member
692 // ::= 4 <basis> # based volatile member
693 // ::= 5 <basis> # based const volatile member
694 // ::= 6 # near function (pointers only)
695 // ::= 7 # far function (pointers only)
696 // ::= 8 # near method (pointers only)
697 // ::= 9 # far method (pointers only)
698 // ::= _A <basis> # based function (pointers only)
699 // ::= _B <basis> # based function (far?) (pointers only)
700 // ::= _C <basis> # based method (pointers only)
701 // ::= _D <basis> # based method (far?) (pointers only)
702 // ::= _E # block (Clang)
703 // <basis> ::= 0 # __based(void)
704 // ::= 1 # __based(segment)?
705 // ::= 2 <name> # __based(name)
706 // ::= 3 # ?
707 // ::= 4 # ?
708 // ::= 5 # not really based
709 if (!IsMember) {
710 if (!Quals.hasVolatile()) {
711 if (!Quals.hasConst())
712 Out << 'A';
713 else
714 Out << 'B';
715 } else {
716 if (!Quals.hasConst())
717 Out << 'C';
718 else
719 Out << 'D';
720 }
721 } else {
722 if (!Quals.hasVolatile()) {
723 if (!Quals.hasConst())
724 Out << 'Q';
725 else
726 Out << 'R';
727 } else {
728 if (!Quals.hasConst())
729 Out << 'S';
730 else
731 Out << 'T';
732 }
733 }
734
735 // FIXME: For now, just drop all extension qualifiers on the floor.
736}
737
738void MicrosoftCXXNameMangler::mangleType(QualType T) {
739 // Only operate on the canonical type!
740 T = getASTContext().getCanonicalType(T);
741
742 Qualifiers Quals = T.getLocalQualifiers();
743 if (Quals) {
744 // We have to mangle these now, while we still have enough information.
745 // <pointer-cvr-qualifiers> ::= P # pointer
746 // ::= Q # const pointer
747 // ::= R # volatile pointer
748 // ::= S # const volatile pointer
749 if (T->isAnyPointerType() || T->isMemberPointerType() ||
750 T->isBlockPointerType()) {
751 if (!Quals.hasVolatile())
752 Out << 'Q';
753 else {
754 if (!Quals.hasConst())
755 Out << 'R';
756 else
757 Out << 'S';
758 }
759 } else
760 // Just emit qualifiers like normal.
761 // NB: When we mangle a pointer/reference type, and the pointee
762 // type has no qualifiers, the lack of qualifier gets mangled
763 // in there.
764 mangleQualifiers(Quals, false);
765 } else if (T->isAnyPointerType() || T->isMemberPointerType() ||
766 T->isBlockPointerType()) {
767 Out << 'P';
768 }
769 switch (T->getTypeClass()) {
770#define ABSTRACT_TYPE(CLASS, PARENT)
771#define NON_CANONICAL_TYPE(CLASS, PARENT) \
772case Type::CLASS: \
773llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
774return;
775#define TYPE(CLASS, PARENT) \
776case Type::CLASS: \
777mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \
778break;
779#include "clang/AST/TypeNodes.def"
780 }
781}
782
783void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T) {
784 // <type> ::= <builtin-type>
785 // <builtin-type> ::= X # void
786 // ::= C # signed char
787 // ::= D # char
788 // ::= E # unsigned char
789 // ::= F # short
790 // ::= G # unsigned short (or wchar_t if it's not a builtin)
791 // ::= H # int
792 // ::= I # unsigned int
793 // ::= J # long
794 // ::= K # unsigned long
795 // L # <none>
796 // ::= M # float
797 // ::= N # double
798 // ::= O # long double (__float80 is mangled differently)
Peter Collingbourne14110472011-01-13 18:57:25 +0000799 // ::= _J # long long, __int64
800 // ::= _K # unsigned long long, __int64
801 // ::= _L # __int128
802 // ::= _M # unsigned __int128
803 // ::= _N # bool
804 // _O # <array in parameter>
805 // ::= _T # __float80 (Intel)
806 // ::= _W # wchar_t
807 // ::= _Z # __float80 (Digital Mars)
808 switch (T->getKind()) {
809 case BuiltinType::Void: Out << 'X'; break;
810 case BuiltinType::SChar: Out << 'C'; break;
811 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break;
812 case BuiltinType::UChar: Out << 'E'; break;
813 case BuiltinType::Short: Out << 'F'; break;
814 case BuiltinType::UShort: Out << 'G'; break;
815 case BuiltinType::Int: Out << 'H'; break;
816 case BuiltinType::UInt: Out << 'I'; break;
817 case BuiltinType::Long: Out << 'J'; break;
818 case BuiltinType::ULong: Out << 'K'; break;
819 case BuiltinType::Float: Out << 'M'; break;
820 case BuiltinType::Double: Out << 'N'; break;
821 // TODO: Determine size and mangle accordingly
822 case BuiltinType::LongDouble: Out << 'O'; break;
Peter Collingbourne14110472011-01-13 18:57:25 +0000823 case BuiltinType::LongLong: Out << "_J"; break;
824 case BuiltinType::ULongLong: Out << "_K"; break;
825 case BuiltinType::Int128: Out << "_L"; break;
826 case BuiltinType::UInt128: Out << "_M"; break;
827 case BuiltinType::Bool: Out << "_N"; break;
828 case BuiltinType::WChar_S:
829 case BuiltinType::WChar_U: Out << "_W"; break;
830
John McCalle0a22d02011-10-18 21:02:43 +0000831#define BUILTIN_TYPE(Id, SingletonId)
832#define PLACEHOLDER_TYPE(Id, SingletonId) \
833 case BuiltinType::Id:
834#include "clang/AST/BuiltinTypes.def"
Peter Collingbourne14110472011-01-13 18:57:25 +0000835 case BuiltinType::Dependent:
John McCalle0a22d02011-10-18 21:02:43 +0000836 llvm_unreachable("placeholder types shouldn't get to name mangling");
837
Peter Collingbourne14110472011-01-13 18:57:25 +0000838 case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
839 case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
840 case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
841
842 case BuiltinType::Char16:
843 case BuiltinType::Char32:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000844 case BuiltinType::Half:
Peter Collingbourne14110472011-01-13 18:57:25 +0000845 case BuiltinType::NullPtr:
John McCalle0a22d02011-10-18 21:02:43 +0000846 assert(0 && "Don't know how to mangle this type yet");
Peter Collingbourne14110472011-01-13 18:57:25 +0000847 }
848}
849
850// <type> ::= <function-type>
851void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T) {
852 // Structors only appear in decls, so at this point we know it's not a
853 // structor type.
854 // I'll probably have mangleType(MemberPointerType) call the mangleType()
855 // method directly.
856 mangleType(T, NULL, false, false);
857}
858void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T) {
859 llvm_unreachable("Can't mangle K&R function prototypes");
860}
861
862void MicrosoftCXXNameMangler::mangleType(const FunctionType *T,
863 const FunctionDecl *D,
864 bool IsStructor,
865 bool IsInstMethod) {
866 // <function-type> ::= <this-cvr-qualifiers> <calling-convention>
867 // <return-type> <argument-list> <throw-spec>
868 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
869
870 // If this is a C++ instance method, mangle the CVR qualifiers for the
871 // this pointer.
872 if (IsInstMethod)
873 mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false);
874
875 mangleCallingConvention(T, IsInstMethod);
876
877 // <return-type> ::= <type>
878 // ::= @ # structors (they have no declared return type)
879 if (IsStructor)
880 Out << '@';
881 else
882 mangleType(Proto->getResultType());
883
884 // <argument-list> ::= X # void
885 // ::= <type>+ @
886 // ::= <type>* Z # varargs
887 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
888 Out << 'X';
889 } else {
890 if (D) {
John McCall3a8ac072012-05-01 02:33:44 +0000891 // If we got a decl, use the type-as-written to make sure arrays
892 // get mangled right. Note that we can't rely on the TSI
893 // existing if (for example) the parameter was synthesized.
Peter Collingbourne14110472011-01-13 18:57:25 +0000894 for (FunctionDecl::param_const_iterator Parm = D->param_begin(),
John McCall3a8ac072012-05-01 02:33:44 +0000895 ParmEnd = D->param_end(); Parm != ParmEnd; ++Parm) {
896 if (TypeSourceInfo *typeAsWritten = (*Parm)->getTypeSourceInfo())
897 mangleType(typeAsWritten->getType());
898 else
899 mangleType((*Parm)->getType());
900 }
Peter Collingbourne14110472011-01-13 18:57:25 +0000901 } else {
902 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
903 ArgEnd = Proto->arg_type_end();
904 Arg != ArgEnd; ++Arg)
905 mangleType(*Arg);
906 }
907 // <builtin-type> ::= Z # ellipsis
908 if (Proto->isVariadic())
909 Out << 'Z';
910 else
911 Out << '@';
912 }
913
914 mangleThrowSpecification(Proto);
915}
916
917void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) {
918 // <function-class> ::= A # private: near
919 // ::= B # private: far
920 // ::= C # private: static near
921 // ::= D # private: static far
922 // ::= E # private: virtual near
923 // ::= F # private: virtual far
924 // ::= G # private: thunk near
925 // ::= H # private: thunk far
926 // ::= I # protected: near
927 // ::= J # protected: far
928 // ::= K # protected: static near
929 // ::= L # protected: static far
930 // ::= M # protected: virtual near
931 // ::= N # protected: virtual far
932 // ::= O # protected: thunk near
933 // ::= P # protected: thunk far
934 // ::= Q # public: near
935 // ::= R # public: far
936 // ::= S # public: static near
937 // ::= T # public: static far
938 // ::= U # public: virtual near
939 // ::= V # public: virtual far
940 // ::= W # public: thunk near
941 // ::= X # public: thunk far
942 // ::= Y # global near
943 // ::= Z # global far
944 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
945 switch (MD->getAccess()) {
946 default:
947 case AS_private:
948 if (MD->isStatic())
949 Out << 'C';
950 else if (MD->isVirtual())
951 Out << 'E';
952 else
953 Out << 'A';
954 break;
955 case AS_protected:
956 if (MD->isStatic())
957 Out << 'K';
958 else if (MD->isVirtual())
959 Out << 'M';
960 else
961 Out << 'I';
962 break;
963 case AS_public:
964 if (MD->isStatic())
965 Out << 'S';
966 else if (MD->isVirtual())
967 Out << 'U';
968 else
969 Out << 'Q';
970 }
971 } else
972 Out << 'Y';
973}
974void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
975 bool IsInstMethod) {
976 // <calling-convention> ::= A # __cdecl
977 // ::= B # __export __cdecl
978 // ::= C # __pascal
979 // ::= D # __export __pascal
980 // ::= E # __thiscall
981 // ::= F # __export __thiscall
982 // ::= G # __stdcall
983 // ::= H # __export __stdcall
984 // ::= I # __fastcall
985 // ::= J # __export __fastcall
986 // The 'export' calling conventions are from a bygone era
987 // (*cough*Win16*cough*) when functions were declared for export with
988 // that keyword. (It didn't actually export them, it just made them so
989 // that they could be in a DLL and somebody from another module could call
990 // them.)
991 CallingConv CC = T->getCallConv();
992 if (CC == CC_Default)
993 CC = IsInstMethod ? getASTContext().getDefaultMethodCallConv() : CC_C;
994 switch (CC) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +0000995 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000996 llvm_unreachable("Unsupported CC for mangling");
Peter Collingbourne14110472011-01-13 18:57:25 +0000997 case CC_Default:
998 case CC_C: Out << 'A'; break;
999 case CC_X86Pascal: Out << 'C'; break;
1000 case CC_X86ThisCall: Out << 'E'; break;
1001 case CC_X86StdCall: Out << 'G'; break;
1002 case CC_X86FastCall: Out << 'I'; break;
1003 }
1004}
1005void MicrosoftCXXNameMangler::mangleThrowSpecification(
1006 const FunctionProtoType *FT) {
1007 // <throw-spec> ::= Z # throw(...) (default)
1008 // ::= @ # throw() or __declspec/__attribute__((nothrow))
1009 // ::= <type>+
1010 // NOTE: Since the Microsoft compiler ignores throw specifications, they are
1011 // all actually mangled as 'Z'. (They're ignored because their associated
1012 // functionality isn't implemented, and probably never will be.)
1013 Out << 'Z';
1014}
1015
1016void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001017 llvm_unreachable("Don't know how to mangle UnresolvedUsingTypes yet!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001018}
1019
1020// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
1021// <union-type> ::= T <name>
1022// <struct-type> ::= U <name>
1023// <class-type> ::= V <name>
1024// <enum-type> ::= W <size> <name>
1025void MicrosoftCXXNameMangler::mangleType(const EnumType *T) {
1026 mangleType(static_cast<const TagType*>(T));
1027}
1028void MicrosoftCXXNameMangler::mangleType(const RecordType *T) {
1029 mangleType(static_cast<const TagType*>(T));
1030}
1031void MicrosoftCXXNameMangler::mangleType(const TagType *T) {
1032 switch (T->getDecl()->getTagKind()) {
1033 case TTK_Union:
1034 Out << 'T';
1035 break;
1036 case TTK_Struct:
1037 Out << 'U';
1038 break;
1039 case TTK_Class:
1040 Out << 'V';
1041 break;
1042 case TTK_Enum:
1043 Out << 'W';
1044 Out << getASTContext().getTypeSizeInChars(
1045 cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity();
1046 break;
1047 }
1048 mangleName(T->getDecl());
1049}
1050
1051// <type> ::= <array-type>
1052// <array-type> ::= P <cvr-qualifiers> [Y <dimension-count> <dimension>+]
1053// <element-type> # as global
1054// ::= Q <cvr-qualifiers> [Y <dimension-count> <dimension>+]
1055// <element-type> # as param
1056// It's supposed to be the other way around, but for some strange reason, it
1057// isn't. Today this behavior is retained for the sole purpose of backwards
1058// compatibility.
1059void MicrosoftCXXNameMangler::mangleType(const ArrayType *T, bool IsGlobal) {
1060 // This isn't a recursive mangling, so now we have to do it all in this
1061 // one call.
1062 if (IsGlobal)
1063 Out << 'P';
1064 else
1065 Out << 'Q';
1066 mangleExtraDimensions(T->getElementType());
1067}
1068void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T) {
1069 mangleType(static_cast<const ArrayType *>(T), false);
1070}
1071void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T) {
1072 mangleType(static_cast<const ArrayType *>(T), false);
1073}
1074void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1075 mangleType(static_cast<const ArrayType *>(T), false);
1076}
1077void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T) {
1078 mangleType(static_cast<const ArrayType *>(T), false);
1079}
1080void MicrosoftCXXNameMangler::mangleExtraDimensions(QualType ElementTy) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001081 SmallVector<llvm::APInt, 3> Dimensions;
Peter Collingbourne14110472011-01-13 18:57:25 +00001082 for (;;) {
1083 if (ElementTy->isConstantArrayType()) {
1084 const ConstantArrayType *CAT =
1085 static_cast<const ConstantArrayType *>(ElementTy.getTypePtr());
1086 Dimensions.push_back(CAT->getSize());
1087 ElementTy = CAT->getElementType();
1088 } else if (ElementTy->isVariableArrayType()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001089 llvm_unreachable("Don't know how to mangle VLAs!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001090 } else if (ElementTy->isDependentSizedArrayType()) {
1091 // The dependent expression has to be folded into a constant (TODO).
David Blaikieb219cfc2011-09-23 05:06:16 +00001092 llvm_unreachable("Don't know how to mangle dependent-sized arrays!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001093 } else if (ElementTy->isIncompleteArrayType()) continue;
1094 else break;
1095 }
1096 mangleQualifiers(ElementTy.getQualifiers(), false);
1097 // If there are any additional dimensions, mangle them now.
1098 if (Dimensions.size() > 0) {
1099 Out << 'Y';
1100 // <dimension-count> ::= <number> # number of extra dimensions
1101 mangleNumber(Dimensions.size());
1102 for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim) {
1103 mangleNumber(Dimensions[Dim].getLimitedValue());
1104 }
1105 }
1106 mangleType(ElementTy.getLocalUnqualifiedType());
1107}
1108
1109// <type> ::= <pointer-to-member-type>
1110// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
1111// <class name> <type>
1112void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T) {
1113 QualType PointeeType = T->getPointeeType();
1114 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
1115 Out << '8';
1116 mangleName(cast<RecordType>(T->getClass())->getDecl());
1117 mangleType(FPT, NULL, false, true);
1118 } else {
1119 mangleQualifiers(PointeeType.getQualifiers(), true);
1120 mangleName(cast<RecordType>(T->getClass())->getDecl());
1121 mangleType(PointeeType.getLocalUnqualifiedType());
1122 }
1123}
1124
1125void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001126 llvm_unreachable("Don't know how to mangle TemplateTypeParmTypes yet!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001127}
1128
Douglas Gregorc3069d62011-01-14 02:55:32 +00001129void MicrosoftCXXNameMangler::mangleType(
1130 const SubstTemplateTypeParmPackType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001131 llvm_unreachable(
Douglas Gregorc3069d62011-01-14 02:55:32 +00001132 "Don't know how to mangle SubstTemplateTypeParmPackTypes yet!");
1133}
1134
Peter Collingbourne14110472011-01-13 18:57:25 +00001135// <type> ::= <pointer-type>
1136// <pointer-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> <type>
1137void MicrosoftCXXNameMangler::mangleType(const PointerType *T) {
1138 QualType PointeeTy = T->getPointeeType();
1139 if (PointeeTy->isArrayType()) {
1140 // Pointers to arrays are mangled like arrays.
1141 mangleExtraDimensions(T->getPointeeType());
1142 } else if (PointeeTy->isFunctionType()) {
1143 // Function pointers are special.
1144 Out << '6';
1145 mangleType(static_cast<const FunctionType *>(PointeeTy.getTypePtr()),
1146 NULL, false, false);
1147 } else {
1148 if (!PointeeTy.hasQualifiers())
1149 // Lack of qualifiers is mangled as 'A'.
1150 Out << 'A';
1151 mangleType(PointeeTy);
1152 }
1153}
1154void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1155 // Object pointers never have qualifiers.
1156 Out << 'A';
1157 mangleType(T->getPointeeType());
1158}
1159
1160// <type> ::= <reference-type>
1161// <reference-type> ::= A <cvr-qualifiers> <type>
1162void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T) {
1163 Out << 'A';
1164 QualType PointeeTy = T->getPointeeType();
1165 if (!PointeeTy.hasQualifiers())
1166 // Lack of qualifiers is mangled as 'A'.
1167 Out << 'A';
1168 mangleType(PointeeTy);
1169}
1170
1171void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001172 llvm_unreachable("Don't know how to mangle RValueReferenceTypes yet!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001173}
1174
1175void MicrosoftCXXNameMangler::mangleType(const ComplexType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001176 llvm_unreachable("Don't know how to mangle ComplexTypes yet!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001177}
1178
1179void MicrosoftCXXNameMangler::mangleType(const VectorType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001180 llvm_unreachable("Don't know how to mangle VectorTypes yet!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001181}
1182void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001183 llvm_unreachable("Don't know how to mangle ExtVectorTypes yet!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001184}
1185void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001186 llvm_unreachable(
1187 "Don't know how to mangle DependentSizedExtVectorTypes yet!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001188}
1189
1190void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1191 // ObjC interfaces have structs underlying them.
1192 Out << 'U';
1193 mangleName(T->getDecl());
1194}
1195
1196void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T) {
1197 // We don't allow overloading by different protocol qualification,
1198 // so mangling them isn't necessary.
1199 mangleType(T->getBaseType());
1200}
1201
1202void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T) {
1203 Out << "_E";
1204 mangleType(T->getPointeeType());
1205}
1206
1207void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001208 llvm_unreachable("Don't know how to mangle InjectedClassNameTypes yet!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001209}
1210
1211void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001212 llvm_unreachable("Don't know how to mangle TemplateSpecializationTypes yet!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001213}
1214
1215void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001216 llvm_unreachable("Don't know how to mangle DependentNameTypes yet!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001217}
1218
1219void MicrosoftCXXNameMangler::mangleType(
1220 const DependentTemplateSpecializationType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001221 llvm_unreachable(
Peter Collingbourne14110472011-01-13 18:57:25 +00001222 "Don't know how to mangle DependentTemplateSpecializationTypes yet!");
1223}
1224
1225void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001226 llvm_unreachable("Don't know how to mangle PackExpansionTypes yet!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001227}
1228
1229void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001230 llvm_unreachable("Don't know how to mangle TypeOfTypes yet!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001231}
1232
1233void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001234 llvm_unreachable("Don't know how to mangle TypeOfExprTypes yet!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001235}
1236
1237void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001238 llvm_unreachable("Don't know how to mangle DecltypeTypes yet!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001239}
1240
Sean Huntca63c202011-05-24 22:41:36 +00001241void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001242 llvm_unreachable("Don't know how to mangle UnaryTransformationTypes yet!");
Sean Huntca63c202011-05-24 22:41:36 +00001243}
1244
Richard Smith34b41d92011-02-20 03:19:35 +00001245void MicrosoftCXXNameMangler::mangleType(const AutoType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001246 llvm_unreachable("Don't know how to mangle AutoTypes yet!");
Richard Smith34b41d92011-02-20 03:19:35 +00001247}
1248
Eli Friedmanb001de72011-10-06 23:00:33 +00001249void MicrosoftCXXNameMangler::mangleType(const AtomicType *T) {
1250 llvm_unreachable("Don't know how to mangle AtomicTypes yet!");
1251}
1252
Peter Collingbourne14110472011-01-13 18:57:25 +00001253void MicrosoftMangleContext::mangleName(const NamedDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001254 raw_ostream &Out) {
Peter Collingbourne14110472011-01-13 18:57:25 +00001255 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1256 "Invalid mangleName() call, argument is not a variable or function!");
1257 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1258 "Invalid mangleName() call on 'structor decl!");
1259
1260 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1261 getASTContext().getSourceManager(),
1262 "Mangling declaration");
1263
Rafael Espindolac4850c22011-02-10 23:59:36 +00001264 MicrosoftCXXNameMangler Mangler(*this, Out);
Peter Collingbourne14110472011-01-13 18:57:25 +00001265 return Mangler.mangle(D);
1266}
1267void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD,
1268 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001269 raw_ostream &) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001270 llvm_unreachable("Can't yet mangle thunks!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001271}
1272void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
1273 CXXDtorType Type,
1274 const ThisAdjustment &,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001275 raw_ostream &) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001276 llvm_unreachable("Can't yet mangle destructor thunks!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001277}
1278void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001279 raw_ostream &) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001280 llvm_unreachable("Can't yet mangle virtual tables!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001281}
1282void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001283 raw_ostream &) {
Peter Collingbourne14110472011-01-13 18:57:25 +00001284 llvm_unreachable("The MS C++ ABI does not have virtual table tables!");
1285}
1286void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
1287 int64_t Offset,
1288 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001289 raw_ostream &) {
Peter Collingbourne14110472011-01-13 18:57:25 +00001290 llvm_unreachable("The MS C++ ABI does not have constructor vtables!");
1291}
1292void MicrosoftMangleContext::mangleCXXRTTI(QualType T,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001293 raw_ostream &) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001294 llvm_unreachable("Can't yet mangle RTTI!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001295}
1296void MicrosoftMangleContext::mangleCXXRTTIName(QualType T,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001297 raw_ostream &) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001298 llvm_unreachable("Can't yet mangle RTTI names!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001299}
1300void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
1301 CXXCtorType Type,
Michael J. Spencer50118da2011-12-01 09:55:00 +00001302 raw_ostream & Out) {
1303 MicrosoftCXXNameMangler mangler(*this, Out);
1304 mangler.mangle(D);
Peter Collingbourne14110472011-01-13 18:57:25 +00001305}
1306void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
1307 CXXDtorType Type,
Michael J. Spencer50118da2011-12-01 09:55:00 +00001308 raw_ostream & Out) {
1309 MicrosoftCXXNameMangler mangler(*this, Out);
1310 mangler.mangle(D);
Peter Collingbourne14110472011-01-13 18:57:25 +00001311}
1312void MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001313 raw_ostream &) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001314 llvm_unreachable("Can't yet mangle reference temporaries!");
Peter Collingbourne14110472011-01-13 18:57:25 +00001315}
1316
1317MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +00001318 DiagnosticsEngine &Diags) {
Peter Collingbourne14110472011-01-13 18:57:25 +00001319 return new MicrosoftMangleContext(Context, Diags);
1320}