blob: 2e0580f5e79b2a170e08d56214be7bfae94ef270 [file] [log] [blame]
Douglas Gregor6ec36682009-02-18 23:53:56 +00001//===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===//
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002//
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// Implements C++ name mangling according to the Itanium C++ ABI,
11// which is used in GCC 3.2 and newer (and many compilers that are
12// ABI-compatible with GCC):
13//
14// http://www.codesourcery.com/public/cxx-abi/abi.html
15//
16//===----------------------------------------------------------------------===//
17#include "Mangle.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
Anders Carlssona40c5e42009-03-07 22:03:21 +000021#include "clang/AST/DeclObjC.h"
Anders Carlsson7a0ba872009-05-15 16:09:15 +000022#include "clang/AST/DeclTemplate.h"
Anders Carlsson50755b02009-09-27 20:11:34 +000023#include "clang/AST/ExprCXX.h"
Douglas Gregor6ec36682009-02-18 23:53:56 +000024#include "clang/Basic/SourceManager.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000025#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000026#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000027#include "llvm/Support/ErrorHandling.h"
Anders Carlssonb73a5be2009-11-26 02:49:32 +000028#include "CGVtable.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000029
30#define MANGLE_CHECKER 0
31
32#if MANGLE_CHECKER
33#include <cxxabi.h>
34#endif
35
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000036using namespace clang;
Anders Carlssonb73a5be2009-11-26 02:49:32 +000037using namespace CodeGen;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000038
39namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000040
41static const DeclContext *GetLocalClassFunctionDeclContext(
42 const DeclContext *DC) {
43 if (isa<CXXRecordDecl>(DC)) {
44 while (!DC->isNamespace() && !DC->isTranslationUnit() &&
45 !isa<FunctionDecl>(DC))
46 DC = DC->getParent();
47 if (isa<FunctionDecl>(DC))
48 return DC;
49 }
50 return 0;
51}
52
Anders Carlsson7e120032009-11-24 05:36:32 +000053static const CXXMethodDecl *getStructor(const CXXMethodDecl *MD) {
54 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
55 "Passed in decl is not a ctor or dtor!");
56
57 if (const TemplateDecl *TD = MD->getPrimaryTemplate()) {
58 MD = cast<CXXMethodDecl>(TD->getTemplatedDecl());
59
60 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
61 "Templated decl is not a ctor or dtor!");
62 }
63
64 return MD;
65}
John McCall1dd73832010-02-04 01:42:13 +000066
67static const unsigned UnknownArity = ~0U;
Anders Carlsson7e120032009-11-24 05:36:32 +000068
Daniel Dunbar1b077112009-11-21 09:06:10 +000069/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +000070class CXXNameMangler {
Daniel Dunbar1b077112009-11-21 09:06:10 +000071 MangleContext &Context;
Daniel Dunbar94fd26d2009-11-21 09:06:22 +000072 llvm::raw_svector_ostream Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000073
Daniel Dunbar1b077112009-11-21 09:06:10 +000074 const CXXMethodDecl *Structor;
75 unsigned StructorType;
Fariborz Jahanian57058532010-03-03 19:41:08 +000076
Daniel Dunbar1b077112009-11-21 09:06:10 +000077 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +000078
John McCall1dd73832010-02-04 01:42:13 +000079 ASTContext &getASTContext() const { return Context.getASTContext(); }
80
Daniel Dunbarc0747712009-11-21 09:12:13 +000081public:
Daniel Dunbar94fd26d2009-11-21 09:06:22 +000082 CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res)
83 : Context(C), Out(Res), Structor(0), StructorType(0) { }
Daniel Dunbar77939c92009-11-21 09:06:31 +000084 CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res,
85 const CXXConstructorDecl *D, CXXCtorType Type)
Anders Carlsson7e120032009-11-24 05:36:32 +000086 : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type) { }
Daniel Dunbar77939c92009-11-21 09:06:31 +000087 CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res,
88 const CXXDestructorDecl *D, CXXDtorType Type)
Anders Carlsson7e120032009-11-24 05:36:32 +000089 : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000090
Anders Carlssonf98574b2010-02-05 07:31:37 +000091#if MANGLE_CHECKER
92 ~CXXNameMangler() {
93 if (Out.str()[0] == '\01')
94 return;
95
96 int status = 0;
97 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
98 assert(status == 0 && "Could not demangle mangled name!");
99 free(result);
100 }
101#endif
Daniel Dunbarc0747712009-11-21 09:12:13 +0000102 llvm::raw_svector_ostream &getStream() { return Out; }
103
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000104 void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z");
Anders Carlssonb73a5be2009-11-26 02:49:32 +0000105 void mangleCallOffset(const ThunkAdjustment &Adjustment);
Anders Carlssona94822e2009-11-26 02:32:05 +0000106 void mangleNumber(int64_t Number);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000107 void mangleFunctionEncoding(const FunctionDecl *FD);
108 void mangleName(const NamedDecl *ND);
109 void mangleType(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Daniel Dunbarc0747712009-11-21 09:12:13 +0000111private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000112 bool mangleSubstitution(const NamedDecl *ND);
113 bool mangleSubstitution(QualType T);
114 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000115
Daniel Dunbar1b077112009-11-21 09:06:10 +0000116 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000117
Daniel Dunbar1b077112009-11-21 09:06:10 +0000118 void addSubstitution(const NamedDecl *ND) {
119 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000120
Daniel Dunbar1b077112009-11-21 09:06:10 +0000121 addSubstitution(reinterpret_cast<uintptr_t>(ND));
122 }
123 void addSubstitution(QualType T);
124 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000125
John McCall1dd73832010-02-04 01:42:13 +0000126 void mangleUnresolvedScope(NestedNameSpecifier *Qualifier);
127 void mangleUnresolvedName(NestedNameSpecifier *Qualifier,
128 DeclarationName Name,
129 unsigned KnownArity = UnknownArity);
130
Daniel Dunbar1b077112009-11-21 09:06:10 +0000131 void mangleName(const TemplateDecl *TD,
132 const TemplateArgument *TemplateArgs,
133 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000134 void mangleUnqualifiedName(const NamedDecl *ND) {
135 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
136 }
137 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
138 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000139 void mangleUnscopedName(const NamedDecl *ND);
140 void mangleUnscopedTemplateName(const TemplateDecl *ND);
141 void mangleSourceName(const IdentifierInfo *II);
142 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000143 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
144 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000145 void mangleNestedName(const TemplateDecl *TD,
146 const TemplateArgument *TemplateArgs,
147 unsigned NumTemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000148 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000149 void mangleTemplatePrefix(const TemplateDecl *ND);
150 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
151 void mangleQualifiers(Qualifiers Quals);
John McCallefe6aee2009-09-05 07:56:18 +0000152
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000153 void mangleObjCMethodName(const ObjCMethodDecl *MD);
154
Daniel Dunbar1b077112009-11-21 09:06:10 +0000155 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000156#define ABSTRACT_TYPE(CLASS, PARENT)
157#define NON_CANONICAL_TYPE(CLASS, PARENT)
158#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
159#include "clang/AST/TypeNodes.def"
160
Daniel Dunbar1b077112009-11-21 09:06:10 +0000161 void mangleType(const TagType*);
162 void mangleBareFunctionType(const FunctionType *T,
163 bool MangleReturnType);
Anders Carlssone170ba72009-12-14 01:45:37 +0000164
165 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCall2f27bf82010-02-04 02:56:29 +0000166 void mangleMemberExpr(const Expr *Base, bool IsArrow,
167 NestedNameSpecifier *Qualifier,
168 DeclarationName Name,
169 unsigned KnownArity);
John McCall1dd73832010-02-04 01:42:13 +0000170 void mangleCalledExpression(const Expr *E, unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000171 void mangleExpression(const Expr *E);
172 void mangleCXXCtorType(CXXCtorType T);
173 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Daniel Dunbar1b077112009-11-21 09:06:10 +0000175 void mangleTemplateArgs(const TemplateArgument *TemplateArgs,
176 unsigned NumTemplateArgs);
Anders Carlsson9e85c742009-12-23 19:30:55 +0000177 void mangleTemplateArgs(const TemplateArgumentList &L);
178 void mangleTemplateArg(const TemplateArgument &A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000179
Daniel Dunbar1b077112009-11-21 09:06:10 +0000180 void mangleTemplateParameter(unsigned Index);
181};
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000182}
183
Anders Carlsson43f17402009-04-02 15:51:53 +0000184static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000185 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000186 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000187 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000188 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000189 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Anders Carlsson43f17402009-04-02 15:51:53 +0000192 return false;
193}
194
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000195bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
196 // In C, functions with no attributes never need to be mangled. Fastpath them.
197 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
198 return false;
199
200 // Any decl can be declared with __asm("foo") on it, and this takes precedence
201 // over all other naming in the .o file.
202 if (D->hasAttr<AsmLabelAttr>())
203 return true;
204
Mike Stump141c5af2009-09-02 00:25:38 +0000205 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000206 // (always) as does passing a C++ member function and a function
207 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000208 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
209 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
210 !FD->getDeclName().isIdentifier()))
211 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000213 // Otherwise, no mangling is done outside C++ mode.
214 if (!getASTContext().getLangOptions().CPlusPlus)
215 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Sean Hunt31455252010-01-24 03:04:27 +0000217 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000218 if (!FD) {
219 const DeclContext *DC = D->getDeclContext();
220 // Check for extern variable declared locally.
221 if (isa<FunctionDecl>(DC) && D->hasLinkage())
222 while (!DC->isNamespace() && !DC->isTranslationUnit())
223 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000224 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000225 return false;
226 }
227
228 // C functions and "main" are not mangled.
229 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000230 return false;
231
Anders Carlsson43f17402009-04-02 15:51:53 +0000232 return true;
233}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000234
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000235void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000236 // Any decl can be declared with __asm("foo") on it, and this takes precedence
237 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000238 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000239 // If we have an asm name, then we use it as the mangling.
240 Out << '\01'; // LLVM IR Marker for __asm("foo")
241 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000242 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000243 }
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Sean Hunt31455252010-01-24 03:04:27 +0000245 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000246 // ::= <data name>
247 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000248 Out << Prefix;
249 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000250 mangleFunctionEncoding(FD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000251 else
252 mangleName(cast<VarDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000253}
254
255void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
256 // <encoding> ::= <function name> <bare-function-type>
257 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000259 // Don't mangle in the type if this isn't a decl we should typically mangle.
260 if (!Context.shouldMangleDeclName(FD))
261 return;
262
Mike Stump141c5af2009-09-02 00:25:38 +0000263 // Whether the mangling of a function type includes the return type depends on
264 // the context and the nature of the function. The rules for deciding whether
265 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000266 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000267 // 1. Template functions (names or types) have return types encoded, with
268 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000269 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000270 // e.g. parameters, pointer types, etc., have return type encoded, with the
271 // exceptions listed below.
272 // 3. Non-template function names do not have return types encoded.
273 //
Mike Stump141c5af2009-09-02 00:25:38 +0000274 // The exceptions mentioned in (1) and (2) above, for which the return type is
275 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000276 // 1. Constructors.
277 // 2. Destructors.
278 // 3. Conversion operator functions, e.g. operator int.
279 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000280 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
281 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
282 isa<CXXConversionDecl>(FD)))
283 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000284
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000285 // Mangle the type of the primary template.
286 FD = PrimaryTemplate->getTemplatedDecl();
287 }
288
John McCall54e14c42009-10-22 22:37:11 +0000289 // Do the canonicalization out here because parameter types can
290 // undergo additional canonicalization (e.g. array decay).
291 FunctionType *FT = cast<FunctionType>(Context.getASTContext()
292 .getCanonicalType(FD->getType()));
293
294 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000295}
296
Anders Carlsson47846d22009-12-04 06:23:23 +0000297/// isStd - Return whether a given namespace is the 'std' namespace.
298static bool isStd(const NamespaceDecl *NS) {
John McCall9aeed322009-10-01 00:25:31 +0000299 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
300 return II && II->isStr("std");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000301}
302
Anders Carlsson47846d22009-12-04 06:23:23 +0000303static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
304 while (isa<LinkageSpecDecl>(DC)) {
305 assert(cast<LinkageSpecDecl>(DC)->getLanguage() ==
306 LinkageSpecDecl::lang_cxx && "Unexpected linkage decl!");
307 DC = DC->getParent();
308 }
309
310 return DC;
311}
312
313// isStdNamespace - Return whether a given decl context is a toplevel 'std'
314// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000315static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000316 if (!DC->isNamespace())
317 return false;
318
319 if (!IgnoreLinkageSpecDecls(DC->getParent())->isTranslationUnit())
320 return false;
321
322 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000323}
324
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000325static const TemplateDecl *
326isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000327 // Check if we have a function template.
328 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000329 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000330 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000331 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000332 }
333 }
334
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000335 // Check if we have a class template.
336 if (const ClassTemplateSpecializationDecl *Spec =
337 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
338 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000339 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000340 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000341
Anders Carlsson2744a062009-09-18 19:00:18 +0000342 return 0;
343}
344
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000345void CXXNameMangler::mangleName(const NamedDecl *ND) {
346 // <name> ::= <nested-name>
347 // ::= <unscoped-name>
348 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000349 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000350 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000351 const DeclContext *DC = ND->getDeclContext();
Fariborz Jahanian57058532010-03-03 19:41:08 +0000352
353 if (GetLocalClassFunctionDeclContext(DC)) {
354 mangleLocalName(ND);
355 return;
356 }
357
Eli Friedman7facf842009-12-02 20:32:49 +0000358 // If this is an extern variable declared locally, the relevant DeclContext
359 // is that of the containing namespace, or the translation unit.
360 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
361 while (!DC->isNamespace() && !DC->isTranslationUnit())
362 DC = DC->getParent();
363
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000364 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000365 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000366
Anders Carlssond58d6f72009-09-17 16:12:20 +0000367 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000368 // Check if we have a template.
369 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000370 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000371 mangleUnscopedTemplateName(TD);
Anders Carlsson9e85c742009-12-23 19:30:55 +0000372 mangleTemplateArgs(*TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000373 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000374 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000375
Anders Carlsson7482e242009-09-18 04:29:09 +0000376 mangleUnscopedName(ND);
377 return;
378 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000379
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000380 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000381 mangleLocalName(ND);
382 return;
383 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000384
Eli Friedman7facf842009-12-02 20:32:49 +0000385 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000386}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000387void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000388 const TemplateArgument *TemplateArgs,
389 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000390 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000391
Anders Carlsson7624f212009-09-18 02:42:01 +0000392 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000393 mangleUnscopedTemplateName(TD);
Anders Carlsson7624f212009-09-18 02:42:01 +0000394 mangleTemplateArgs(TemplateArgs, NumTemplateArgs);
395 } else {
396 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
397 }
398}
399
Anders Carlsson201ce742009-09-17 03:17:01 +0000400void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
401 // <unscoped-name> ::= <unqualified-name>
402 // ::= St <unqualified-name> # ::std::
403 if (isStdNamespace(ND->getDeclContext()))
404 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000405
Anders Carlsson201ce742009-09-17 03:17:01 +0000406 mangleUnqualifiedName(ND);
407}
408
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000409void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000410 // <unscoped-template-name> ::= <unscoped-name>
411 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000412 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000413 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000414
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000415 // <template-template-param> ::= <template-param>
416 if (const TemplateTemplateParmDecl *TTP
417 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
418 mangleTemplateParameter(TTP->getIndex());
419 return;
420 }
421
Anders Carlsson1668f202009-09-26 20:13:56 +0000422 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000423 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000424}
425
Anders Carlssona94822e2009-11-26 02:32:05 +0000426void CXXNameMangler::mangleNumber(int64_t Number) {
427 // <number> ::= [n] <non-negative decimal integer>
428 if (Number < 0) {
429 Out << 'n';
430 Number = -Number;
431 }
432
433 Out << Number;
434}
435
Anders Carlssonb73a5be2009-11-26 02:49:32 +0000436void CXXNameMangler::mangleCallOffset(const ThunkAdjustment &Adjustment) {
Mike Stump141c5af2009-09-02 00:25:38 +0000437 // <call-offset> ::= h <nv-offset> _
438 // ::= v <v-offset> _
439 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000440 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000441 // # virtual base override, with vcall offset
Anders Carlssonb73a5be2009-11-26 02:49:32 +0000442 if (!Adjustment.Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000443 Out << 'h';
Anders Carlssonb73a5be2009-11-26 02:49:32 +0000444 mangleNumber(Adjustment.NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000445 Out << '_';
446 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000447 }
Anders Carlssona94822e2009-11-26 02:32:05 +0000448
449 Out << 'v';
Anders Carlssonb73a5be2009-11-26 02:49:32 +0000450 mangleNumber(Adjustment.NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000451 Out << '_';
Anders Carlssonb73a5be2009-11-26 02:49:32 +0000452 mangleNumber(Adjustment.Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000453 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000454}
455
John McCall1dd73832010-02-04 01:42:13 +0000456void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
457 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
458 switch (Qualifier->getKind()) {
459 case NestedNameSpecifier::Global:
460 // nothing
461 break;
462 case NestedNameSpecifier::Namespace:
463 mangleName(Qualifier->getAsNamespace());
464 break;
465 case NestedNameSpecifier::TypeSpec:
466 case NestedNameSpecifier::TypeSpecWithTemplate:
467 mangleType(QualType(Qualifier->getAsType(), 0));
468 break;
469 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000470 // Member expressions can have these without prefixes.
471 if (Qualifier->getPrefix())
472 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000473 mangleSourceName(Qualifier->getAsIdentifier());
474 break;
475 }
476}
477
478/// Mangles a name which was not resolved to a specific entity.
479void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
480 DeclarationName Name,
481 unsigned KnownArity) {
482 if (Qualifier)
483 mangleUnresolvedScope(Qualifier);
484 // FIXME: ambiguity of unqualified lookup with ::
485
486 mangleUnqualifiedName(0, Name, KnownArity);
487}
488
489void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
490 DeclarationName Name,
491 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000492 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000493 // ::= <ctor-dtor-name>
494 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000495 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000496 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000497 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000498 // We must avoid conflicts between internally- and externally-
Anders Carlssonaec25232010-02-06 04:52:27 +0000499 // linked variable declaration names in the same TU.
500 // This naming convention is the same as that followed by GCC, though it
501 // shouldn't actually matter.
502 if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000503 ND->getDeclContext()->isFileContext())
504 Out << 'L';
505
Anders Carlssonc4355b62009-10-07 01:45:02 +0000506 mangleSourceName(II);
507 break;
508 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000509
John McCall1dd73832010-02-04 01:42:13 +0000510 // Otherwise, an anonymous entity. We must have a declaration.
511 assert(ND && "mangling empty name without declaration");
512
513 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
514 if (NS->isAnonymousNamespace()) {
515 // This is how gcc mangles these names.
516 Out << "12_GLOBAL__N_1";
517 break;
518 }
519 }
520
Anders Carlssonc4355b62009-10-07 01:45:02 +0000521 // We must have an anonymous struct.
522 const TagDecl *TD = cast<TagDecl>(ND);
523 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
524 assert(TD->getDeclContext() == D->getDeclContext() &&
525 "Typedef should not be in another decl context!");
526 assert(D->getDeclName().getAsIdentifierInfo() &&
527 "Typedef was not named!");
528 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
529 break;
530 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000531
Anders Carlssonc4355b62009-10-07 01:45:02 +0000532 // Get a unique id for the anonymous struct.
533 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
534
535 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000536 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000537 // where n is the length of the string.
538 llvm::SmallString<8> Str;
539 Str += "$_";
540 Str += llvm::utostr(AnonStructId);
541
542 Out << Str.size();
543 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000544 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000545 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000546
547 case DeclarationName::ObjCZeroArgSelector:
548 case DeclarationName::ObjCOneArgSelector:
549 case DeclarationName::ObjCMultiArgSelector:
550 assert(false && "Can't mangle Objective-C selector names here!");
551 break;
552
553 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000554 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000555 // If the named decl is the C++ constructor we're mangling, use the type
556 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000557 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000558 else
559 // Otherwise, use the complete constructor name. This is relevant if a
560 // class with a constructor is declared within a constructor.
561 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000562 break;
563
564 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000565 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000566 // If the named decl is the C++ destructor we're mangling, use the type we
567 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000568 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
569 else
570 // Otherwise, use the complete destructor name. This is relevant if a
571 // class with a destructor is declared within a destructor.
572 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000573 break;
574
575 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000576 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000577 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000578 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000579 break;
580
Anders Carlsson8257d412009-12-22 06:36:32 +0000581 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000582 unsigned Arity;
583 if (ND) {
584 Arity = cast<FunctionDecl>(ND)->getNumParams();
Anders Carlsson8257d412009-12-22 06:36:32 +0000585
John McCall1dd73832010-02-04 01:42:13 +0000586 // If we have a C++ member function, we need to include the 'this' pointer.
587 // FIXME: This does not make sense for operators that are static, but their
588 // names stay the same regardless of the arity (operator new for instance).
589 if (isa<CXXMethodDecl>(ND))
590 Arity++;
591 } else
592 Arity = KnownArity;
593
Anders Carlsson8257d412009-12-22 06:36:32 +0000594 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000595 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000596 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000597
Sean Hunt3e518bd2009-11-29 07:34:05 +0000598 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000599 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000600 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000601 mangleSourceName(Name.getCXXLiteralIdentifier());
602 break;
603
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000604 case DeclarationName::CXXUsingDirective:
605 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000606 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000607 }
608}
609
610void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
611 // <source-name> ::= <positive length number> <identifier>
612 // <number> ::= [n] <non-negative decimal integer>
613 // <identifier> ::= <unqualified source code identifier>
614 Out << II->getLength() << II->getName();
615}
616
Eli Friedman7facf842009-12-02 20:32:49 +0000617void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000618 const DeclContext *DC,
619 bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000620 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
621 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000622
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000623 Out << 'N';
624 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
John McCall0953e762009-09-24 19:53:00 +0000625 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000626
Anders Carlsson2744a062009-09-18 19:00:18 +0000627 // Check if we have a template.
628 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000629 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000630 mangleTemplatePrefix(TD);
Anders Carlsson9e85c742009-12-23 19:30:55 +0000631 mangleTemplateArgs(*TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000632 }
633 else {
634 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000635 mangleUnqualifiedName(ND);
636 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000637
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000638 Out << 'E';
639}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000640void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000641 const TemplateArgument *TemplateArgs,
642 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000643 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
644
Anders Carlsson7624f212009-09-18 02:42:01 +0000645 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000646
Anders Carlssone45117b2009-09-27 19:53:49 +0000647 mangleTemplatePrefix(TD);
Anders Carlsson7624f212009-09-18 02:42:01 +0000648 mangleTemplateArgs(TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000649
Anders Carlsson7624f212009-09-18 02:42:01 +0000650 Out << 'E';
651}
652
Anders Carlsson1b42c792009-04-02 16:24:45 +0000653void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
654 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
655 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000656 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000657 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000658 Out << 'Z';
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000659
Fariborz Jahanian57058532010-03-03 19:41:08 +0000660 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC))
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000661 mangleObjCMethodName(MD);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000662 else if (const DeclContext *CDC = GetLocalClassFunctionDeclContext(DC)) {
663 mangleFunctionEncoding(cast<FunctionDecl>(CDC));
664 Out << 'E';
665 mangleNestedName(ND, DC, true /*NoFunction*/);
666
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000667 // FIXME. This still does not cover all cases.
668 unsigned disc;
669 if (Context.getNextDiscriminator(ND, disc)) {
670 if (disc < 10)
671 Out << '_' << disc;
672 else
673 Out << "__" << disc << '_';
674 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000675
676 return;
677 }
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000678 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000679 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000680
Anders Carlsson1b42c792009-04-02 16:24:45 +0000681 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000682 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000683}
684
Fariborz Jahanian57058532010-03-03 19:41:08 +0000685void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000686 // <prefix> ::= <prefix> <unqualified-name>
687 // ::= <template-prefix> <template-args>
688 // ::= <template-param>
689 // ::= # empty
690 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000691
Anders Carlssonadd28822009-09-22 20:33:31 +0000692 while (isa<LinkageSpecDecl>(DC))
693 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000694
Anders Carlsson9263e912009-09-18 18:39:58 +0000695 if (DC->isTranslationUnit())
696 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000697
Anders Carlsson6862fc72009-09-17 04:16:28 +0000698 if (mangleSubstitution(cast<NamedDecl>(DC)))
699 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000700
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000701 // Check if we have a template.
702 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000703 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000704 mangleTemplatePrefix(TD);
Anders Carlsson9e85c742009-12-23 19:30:55 +0000705 mangleTemplateArgs(*TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000706 }
707 else if(NoFunction && isa<FunctionDecl>(DC))
708 return;
709 else {
710 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000711 mangleUnqualifiedName(cast<NamedDecl>(DC));
712 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000713
Anders Carlsson6862fc72009-09-17 04:16:28 +0000714 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000715}
716
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000717void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000718 // <template-prefix> ::= <prefix> <template unqualified-name>
719 // ::= <template-param>
720 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000721 // <template-template-param> ::= <template-param>
722 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000723
Anders Carlssonaeb85372009-09-26 22:18:22 +0000724 if (mangleSubstitution(ND))
725 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000726
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000727 // <template-template-param> ::= <template-param>
728 if (const TemplateTemplateParmDecl *TTP
729 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
730 mangleTemplateParameter(TTP->getIndex());
731 return;
732 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000733
Anders Carlssonaa73ab12009-09-18 18:47:07 +0000734 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +0000735 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +0000736 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +0000737}
738
Mike Stump1eb44332009-09-09 15:08:12 +0000739void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000740CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
741 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000742 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000743 case OO_New: Out << "nw"; break;
744 // ::= na # new[]
745 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000746 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000747 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000748 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000749 case OO_Array_Delete: Out << "da"; break;
750 // ::= ps # + (unary)
751 // ::= pl # +
Anders Carlsson8257d412009-12-22 06:36:32 +0000752 case OO_Plus:
753 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
754 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000755 // ::= ng # - (unary)
756 // ::= mi # -
Anders Carlsson8257d412009-12-22 06:36:32 +0000757 case OO_Minus:
758 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
759 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000760 // ::= ad # & (unary)
761 // ::= an # &
Anders Carlsson8257d412009-12-22 06:36:32 +0000762 case OO_Amp:
763 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
764 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000765 // ::= de # * (unary)
766 // ::= ml # *
Anders Carlsson8257d412009-12-22 06:36:32 +0000767 case OO_Star:
768 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
769 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000770 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000771 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000772 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000773 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000774 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000775 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000776 // ::= or # |
777 case OO_Pipe: Out << "or"; break;
778 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000779 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000780 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000781 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000782 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000783 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000784 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000785 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000786 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000787 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000788 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000789 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000790 // ::= rM # %=
791 case OO_PercentEqual: Out << "rM"; break;
792 // ::= aN # &=
793 case OO_AmpEqual: Out << "aN"; break;
794 // ::= oR # |=
795 case OO_PipeEqual: Out << "oR"; break;
796 // ::= eO # ^=
797 case OO_CaretEqual: Out << "eO"; break;
798 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000799 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000800 // ::= rs # >>
801 case OO_GreaterGreater: Out << "rs"; break;
802 // ::= lS # <<=
803 case OO_LessLessEqual: Out << "lS"; break;
804 // ::= rS # >>=
805 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000806 // ::= eq # ==
807 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000808 // ::= ne # !=
809 case OO_ExclaimEqual: Out << "ne"; break;
810 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000811 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000812 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000813 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000814 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000815 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000816 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000817 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000818 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000819 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000820 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000821 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000822 // ::= oo # ||
823 case OO_PipePipe: Out << "oo"; break;
824 // ::= pp # ++
825 case OO_PlusPlus: Out << "pp"; break;
826 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000827 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000828 // ::= cm # ,
829 case OO_Comma: Out << "cm"; break;
830 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000831 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000832 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000833 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000834 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000835 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000836 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000837 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +0000838
839 // ::= qu # ?
840 // The conditional operator can't be overloaded, but we still handle it when
841 // mangling expressions.
842 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000843
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000844 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000845 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +0000846 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000847 break;
848 }
849}
850
John McCall0953e762009-09-24 19:53:00 +0000851void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +0000852 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +0000853 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000854 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +0000855 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000856 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +0000857 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000858 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +0000859
860 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000861}
862
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000863void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
864 llvm::SmallString<64> Name;
865 llvm::raw_svector_ostream OS(Name);
866
867 const ObjCContainerDecl *CD =
868 dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
869 assert (CD && "Missing container decl in GetNameForMethod");
870 OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName();
871 if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD))
872 OS << '(' << CID->getNameAsString() << ')';
873 OS << ' ' << MD->getSelector().getAsString() << ']';
874
875 Out << OS.str().size() << OS.str();
876}
877
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000878void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000879 // Only operate on the canonical type!
Anders Carlssonb5404912009-10-07 01:06:45 +0000880 T = Context.getASTContext().getCanonicalType(T);
Anders Carlsson4843e582009-03-10 17:07:44 +0000881
Douglas Gregora4923eb2009-11-16 21:35:15 +0000882 bool IsSubstitutable = T.hasLocalQualifiers() || !isa<BuiltinType>(T);
Anders Carlsson76967372009-09-17 00:43:46 +0000883 if (IsSubstitutable && mangleSubstitution(T))
884 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000885
Douglas Gregora4923eb2009-11-16 21:35:15 +0000886 if (Qualifiers Quals = T.getLocalQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +0000887 mangleQualifiers(Quals);
888 // Recurse: even if the qualified type isn't yet substitutable,
889 // the unqualified type might be.
Douglas Gregora4923eb2009-11-16 21:35:15 +0000890 mangleType(T.getLocalUnqualifiedType());
Anders Carlsson76967372009-09-17 00:43:46 +0000891 } else {
892 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +0000893#define ABSTRACT_TYPE(CLASS, PARENT)
894#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000895 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000896 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +0000897 return;
John McCallefe6aee2009-09-05 07:56:18 +0000898#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000899 case Type::CLASS: \
John McCall0953e762009-09-24 19:53:00 +0000900 mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \
Anders Carlsson76967372009-09-17 00:43:46 +0000901 break;
John McCallefe6aee2009-09-05 07:56:18 +0000902#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +0000903 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000904 }
Anders Carlsson76967372009-09-17 00:43:46 +0000905
906 // Add the substitution.
907 if (IsSubstitutable)
908 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000909}
910
911void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +0000912 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000913 // <builtin-type> ::= v # void
914 // ::= w # wchar_t
915 // ::= b # bool
916 // ::= c # char
917 // ::= a # signed char
918 // ::= h # unsigned char
919 // ::= s # short
920 // ::= t # unsigned short
921 // ::= i # int
922 // ::= j # unsigned int
923 // ::= l # long
924 // ::= m # unsigned long
925 // ::= x # long long, __int64
926 // ::= y # unsigned long long, __int64
927 // ::= n # __int128
928 // UNSUPPORTED: ::= o # unsigned __int128
929 // ::= f # float
930 // ::= d # double
931 // ::= e # long double, __float80
932 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000933 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
934 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
935 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
936 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000937 // ::= Di # char32_t
938 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000939 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000940 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
941 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000942 switch (T->getKind()) {
943 case BuiltinType::Void: Out << 'v'; break;
944 case BuiltinType::Bool: Out << 'b'; break;
945 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
946 case BuiltinType::UChar: Out << 'h'; break;
947 case BuiltinType::UShort: Out << 't'; break;
948 case BuiltinType::UInt: Out << 'j'; break;
949 case BuiltinType::ULong: Out << 'm'; break;
950 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000951 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000952 case BuiltinType::SChar: Out << 'a'; break;
953 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000954 case BuiltinType::Char16: Out << "Ds"; break;
955 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000956 case BuiltinType::Short: Out << 's'; break;
957 case BuiltinType::Int: Out << 'i'; break;
958 case BuiltinType::Long: Out << 'l'; break;
959 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000960 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000961 case BuiltinType::Float: Out << 'f'; break;
962 case BuiltinType::Double: Out << 'd'; break;
963 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000964 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000965
966 case BuiltinType::Overload:
967 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +0000968 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000969 "Overloaded and dependent types shouldn't get to name mangling");
970 break;
Anders Carlssone89d1592009-06-26 18:41:36 +0000971 case BuiltinType::UndeducedAuto:
972 assert(0 && "Should not see undeduced auto here");
973 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +0000974 case BuiltinType::ObjCId: Out << "11objc_object"; break;
975 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000976 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000977 }
978}
979
John McCallefe6aee2009-09-05 07:56:18 +0000980// <type> ::= <function-type>
981// <function-type> ::= F [Y] <bare-function-type> E
982void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000983 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +0000984 // FIXME: We don't have enough information in the AST to produce the 'Y'
985 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000986 mangleBareFunctionType(T, /*MangleReturnType=*/true);
987 Out << 'E';
988}
John McCallefe6aee2009-09-05 07:56:18 +0000989void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000990 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +0000991}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000992void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
993 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +0000994 // We should never be mangling something without a prototype.
995 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
996
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000997 // <bare-function-type> ::= <signature type>+
998 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +0000999 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001000
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001001 if (Proto->getNumArgs() == 0) {
1002 Out << 'v';
1003 return;
1004 }
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Douglas Gregor72564e72009-02-26 23:50:07 +00001006 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001007 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001008 Arg != ArgEnd; ++Arg)
1009 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001010
1011 // <builtin-type> ::= z # ellipsis
1012 if (Proto->isVariadic())
1013 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001014}
1015
John McCallefe6aee2009-09-05 07:56:18 +00001016// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001017// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001018void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1019 mangleName(T->getDecl());
1020}
1021
1022// <type> ::= <class-enum-type>
1023// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001024void CXXNameMangler::mangleType(const EnumType *T) {
1025 mangleType(static_cast<const TagType*>(T));
1026}
1027void CXXNameMangler::mangleType(const RecordType *T) {
1028 mangleType(static_cast<const TagType*>(T));
1029}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001030void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001031 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001032}
1033
John McCallefe6aee2009-09-05 07:56:18 +00001034// <type> ::= <array-type>
1035// <array-type> ::= A <positive dimension number> _ <element type>
1036// ::= A [<dimension expression>] _ <element type>
1037void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1038 Out << 'A' << T->getSize() << '_';
1039 mangleType(T->getElementType());
1040}
1041void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001042 Out << 'A';
John McCallefe6aee2009-09-05 07:56:18 +00001043 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001044 Out << '_';
1045 mangleType(T->getElementType());
1046}
John McCallefe6aee2009-09-05 07:56:18 +00001047void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1048 Out << 'A';
1049 mangleExpression(T->getSizeExpr());
1050 Out << '_';
1051 mangleType(T->getElementType());
1052}
1053void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
1054 Out << 'A' << '_';
1055 mangleType(T->getElementType());
1056}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001057
John McCallefe6aee2009-09-05 07:56:18 +00001058// <type> ::= <pointer-to-member-type>
1059// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001060void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001061 Out << 'M';
1062 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001063 QualType PointeeType = T->getPointeeType();
1064 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001065 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Anders Carlsson0e650012009-05-17 17:41:20 +00001066 mangleType(FPT);
Mike Stump1eb44332009-09-09 15:08:12 +00001067 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001068 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001069}
1070
John McCallefe6aee2009-09-05 07:56:18 +00001071// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001072void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001073 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001074}
1075
John McCallefe6aee2009-09-05 07:56:18 +00001076// FIXME: <type> ::= <template-template-param> <template-args>
John McCallefe6aee2009-09-05 07:56:18 +00001077
1078// <type> ::= P <type> # pointer-to
1079void CXXNameMangler::mangleType(const PointerType *T) {
1080 Out << 'P';
1081 mangleType(T->getPointeeType());
1082}
1083void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1084 Out << 'P';
1085 mangleType(T->getPointeeType());
1086}
1087
1088// <type> ::= R <type> # reference-to
1089void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1090 Out << 'R';
1091 mangleType(T->getPointeeType());
1092}
1093
1094// <type> ::= O <type> # rvalue reference-to (C++0x)
1095void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1096 Out << 'O';
1097 mangleType(T->getPointeeType());
1098}
1099
1100// <type> ::= C <type> # complex pair (C 2000)
1101void CXXNameMangler::mangleType(const ComplexType *T) {
1102 Out << 'C';
1103 mangleType(T->getElementType());
1104}
1105
1106// GNU extension: vector types
1107void CXXNameMangler::mangleType(const VectorType *T) {
1108 Out << "U8__vector";
1109 mangleType(T->getElementType());
1110}
1111void CXXNameMangler::mangleType(const ExtVectorType *T) {
1112 mangleType(static_cast<const VectorType*>(T));
1113}
1114void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
1115 Out << "U8__vector";
1116 mangleType(T->getElementType());
1117}
1118
Anders Carlssona40c5e42009-03-07 22:03:21 +00001119void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1120 mangleSourceName(T->getDecl()->getIdentifier());
1121}
1122
John McCallefe6aee2009-09-05 07:56:18 +00001123void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001124 Out << "U13block_pointer";
1125 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001126}
1127
John McCallefe6aee2009-09-05 07:56:18 +00001128void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Anders Carlsson7624f212009-09-18 02:42:01 +00001129 TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl();
1130 assert(TD && "FIXME: Support dependent template names!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001131
Anders Carlsson7624f212009-09-18 02:42:01 +00001132 mangleName(TD, T->getArgs(), T->getNumArgs());
John McCallefe6aee2009-09-05 07:56:18 +00001133}
1134
1135void CXXNameMangler::mangleType(const TypenameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001136 // Typename types are always nested
1137 Out << 'N';
1138
1139 const Type *QTy = T->getQualifier()->getAsType();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001140 if (const TemplateSpecializationType *TST =
Anders Carlssonae352482009-09-26 02:26:02 +00001141 dyn_cast<TemplateSpecializationType>(QTy)) {
Anders Carlsson88599172009-09-27 01:06:07 +00001142 if (!mangleSubstitution(QualType(TST, 0))) {
1143 TemplateDecl *TD = TST->getTemplateName().getAsTemplateDecl();
Douglas Gregor2d565b32010-02-06 01:09:36 +00001144 assert(TD && "FIXME: Support dependent template names");
Anders Carlsson88599172009-09-27 01:06:07 +00001145 mangleTemplatePrefix(TD);
1146 mangleTemplateArgs(TST->getArgs(), TST->getNumArgs());
1147 addSubstitution(QualType(TST, 0));
1148 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001149 } else if (const TemplateTypeParmType *TTPT =
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001150 dyn_cast<TemplateTypeParmType>(QTy)) {
1151 // We use the QualType mangle type variant here because it handles
1152 // substitutions.
1153 mangleType(QualType(TTPT, 0));
Anders Carlssonae352482009-09-26 02:26:02 +00001154 } else
1155 assert(false && "Unhandled type!");
1156
1157 mangleSourceName(T->getIdentifier());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001158
Anders Carlssonae352482009-09-26 02:26:02 +00001159 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001160}
1161
John McCallad5e7382010-03-01 23:49:17 +00001162void CXXNameMangler::mangleType(const TypeOfType *T) {
1163 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1164 // "extension with parameters" mangling.
1165 Out << "u6typeof";
1166}
1167
1168void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1169 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1170 // "extension with parameters" mangling.
1171 Out << "u6typeof";
1172}
1173
1174void CXXNameMangler::mangleType(const DecltypeType *T) {
1175 Expr *E = T->getUnderlyingExpr();
1176
1177 // type ::= Dt <expression> E # decltype of an id-expression
1178 // # or class member access
1179 // ::= DT <expression> E # decltype of an expression
1180
1181 // This purports to be an exhaustive list of id-expressions and
1182 // class member accesses. Note that we do not ignore parentheses;
1183 // parentheses change the semantics of decltype for these
1184 // expressions (and cause the mangler to use the other form).
1185 if (isa<DeclRefExpr>(E) ||
1186 isa<MemberExpr>(E) ||
1187 isa<UnresolvedLookupExpr>(E) ||
1188 isa<DependentScopeDeclRefExpr>(E) ||
1189 isa<CXXDependentScopeMemberExpr>(E) ||
1190 isa<UnresolvedMemberExpr>(E))
1191 Out << "Dt";
1192 else
1193 Out << "DT";
1194 mangleExpression(E);
1195 Out << 'E';
1196}
1197
Anders Carlssone170ba72009-12-14 01:45:37 +00001198void CXXNameMangler::mangleIntegerLiteral(QualType T,
1199 const llvm::APSInt &Value) {
1200 // <expr-primary> ::= L <type> <value number> E # integer literal
1201 Out << 'L';
1202
1203 mangleType(T);
1204 if (T->isBooleanType()) {
1205 // Boolean values are encoded as 0/1.
1206 Out << (Value.getBoolValue() ? '1' : '0');
1207 } else {
1208 if (Value.isNegative())
1209 Out << 'n';
1210 Value.abs().print(Out, false);
1211 }
1212 Out << 'E';
1213
1214}
1215
John McCall1dd73832010-02-04 01:42:13 +00001216void CXXNameMangler::mangleCalledExpression(const Expr *E, unsigned Arity) {
1217 if (E->getType() != getASTContext().OverloadTy)
1218 mangleExpression(E);
John McCall2f27bf82010-02-04 02:56:29 +00001219 // propagate arity to dependent overloads?
John McCall1dd73832010-02-04 01:42:13 +00001220
1221 llvm::PointerIntPair<OverloadExpr*,1> R
1222 = OverloadExpr::find(const_cast<Expr*>(E));
1223 if (R.getInt())
1224 Out << "an"; // &
1225 const OverloadExpr *Ovl = R.getPointer();
John McCall2f27bf82010-02-04 02:56:29 +00001226 if (const UnresolvedMemberExpr *ME = dyn_cast<UnresolvedMemberExpr>(Ovl)) {
1227 mangleMemberExpr(ME->getBase(), ME->isArrow(), ME->getQualifier(),
1228 ME->getMemberName(), Arity);
1229 return;
1230 }
John McCall1dd73832010-02-04 01:42:13 +00001231
1232 mangleUnresolvedName(Ovl->getQualifier(), Ovl->getName(), Arity);
1233}
1234
John McCall2f27bf82010-02-04 02:56:29 +00001235/// Mangles a member expression. Implicit accesses are not handled,
1236/// but that should be okay, because you shouldn't be able to
1237/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001238void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1239 bool IsArrow,
1240 NestedNameSpecifier *Qualifier,
1241 DeclarationName Member,
1242 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001243 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1244 // OTOH, gcc also mangles the name as an expression.
1245 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001246 mangleExpression(Base);
1247 mangleUnresolvedName(Qualifier, Member, Arity);
1248}
1249
Anders Carlssond553f8c2009-09-21 01:21:10 +00001250void CXXNameMangler::mangleExpression(const Expr *E) {
1251 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001252 // ::= <binary operator-name> <expression> <expression>
1253 // ::= <trinary operator-name> <expression> <expression> <expression>
1254 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001255 // ::= cv <type> expression # conversion with one argument
1256 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
John McCall09cc1412010-02-03 00:55:45 +00001257 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001258 // ::= at <type> # alignof (a type)
1259 // ::= <template-param>
1260 // ::= <function-param>
1261 // ::= sr <type> <unqualified-name> # dependent name
1262 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1263 // ::= sZ <template-param> # size of a parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001264 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001265 // <expr-primary> ::= L <type> <value number> E # integer literal
1266 // ::= L <type <value float> E # floating literal
1267 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001268 switch (E->getStmtClass()) {
John McCall09cc1412010-02-03 00:55:45 +00001269 default:
1270 llvm_unreachable("unexpected statement kind");
1271 break;
1272
John McCall1dd73832010-02-04 01:42:13 +00001273 case Expr::CallExprClass: {
1274 const CallExpr *CE = cast<CallExpr>(E);
1275 Out << "cl";
1276 mangleCalledExpression(CE->getCallee(), CE->getNumArgs());
1277 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1278 mangleExpression(CE->getArg(I));
1279 Out << "E";
John McCall09cc1412010-02-03 00:55:45 +00001280 break;
John McCall1dd73832010-02-04 01:42:13 +00001281 }
John McCall09cc1412010-02-03 00:55:45 +00001282
John McCall2f27bf82010-02-04 02:56:29 +00001283 case Expr::MemberExprClass: {
1284 const MemberExpr *ME = cast<MemberExpr>(E);
1285 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1286 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
1287 UnknownArity);
1288 break;
1289 }
1290
1291 case Expr::UnresolvedMemberExprClass: {
1292 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1293 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1294 ME->getQualifier(), ME->getMemberName(),
1295 UnknownArity);
1296 break;
1297 }
1298
1299 case Expr::CXXDependentScopeMemberExprClass: {
1300 const CXXDependentScopeMemberExpr *ME
1301 = cast<CXXDependentScopeMemberExpr>(E);
1302 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1303 ME->getQualifier(), ME->getMember(),
1304 UnknownArity);
1305 break;
1306 }
1307
John McCall1dd73832010-02-04 01:42:13 +00001308 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001309 // The ABI doesn't cover how to mangle overload sets, so we mangle
1310 // using something as close as possible to the original lookup
1311 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001312 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
1313 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), UnknownArity);
1314 break;
1315 }
1316
1317 case Expr::CXXUnresolvedConstructExprClass: {
1318 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1319 unsigned N = CE->arg_size();
1320
1321 Out << "cv";
1322 mangleType(CE->getType());
1323 if (N != 1) Out << "_";
1324 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
1325 if (N != 1) Out << "E";
John McCall09cc1412010-02-03 00:55:45 +00001326 break;
John McCall1dd73832010-02-04 01:42:13 +00001327 }
John McCall09cc1412010-02-03 00:55:45 +00001328
John McCall1dd73832010-02-04 01:42:13 +00001329 case Expr::CXXTemporaryObjectExprClass:
1330 case Expr::CXXConstructExprClass: {
1331 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1332 unsigned N = CE->getNumArgs();
1333
1334 Out << "cv";
1335 mangleType(CE->getType());
1336 if (N != 1) Out << "_";
1337 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
1338 if (N != 1) Out << "E";
John McCall09cc1412010-02-03 00:55:45 +00001339 break;
John McCall1dd73832010-02-04 01:42:13 +00001340 }
1341
1342 case Expr::SizeOfAlignOfExprClass: {
1343 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
1344 if (SAE->isSizeOf()) Out << "s";
1345 else Out << "a";
1346 if (SAE->isArgumentType()) {
1347 Out << "t";
1348 mangleType(SAE->getArgumentType());
1349 } else {
1350 Out << "z";
1351 mangleExpression(SAE->getArgumentExpr());
1352 }
1353 break;
1354 }
Anders Carlssona7694082009-11-06 02:50:19 +00001355
Anders Carlssone170ba72009-12-14 01:45:37 +00001356 case Expr::UnaryOperatorClass: {
1357 const UnaryOperator *UO = cast<UnaryOperator>(E);
1358 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
1359 /*Arity=*/1);
1360 mangleExpression(UO->getSubExpr());
1361 break;
1362 }
1363
1364 case Expr::BinaryOperatorClass: {
1365 const BinaryOperator *BO = cast<BinaryOperator>(E);
1366 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
1367 /*Arity=*/2);
1368 mangleExpression(BO->getLHS());
1369 mangleExpression(BO->getRHS());
1370 break;
John McCall2f27bf82010-02-04 02:56:29 +00001371 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001372
1373 case Expr::ConditionalOperatorClass: {
1374 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1375 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1376 mangleExpression(CO->getCond());
1377 mangleExpression(CO->getLHS());
1378 mangleExpression(CO->getRHS());
1379 break;
1380 }
1381
Douglas Gregor46287c72010-01-29 16:37:09 +00001382 case Expr::ImplicitCastExprClass: {
1383 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr());
1384 break;
1385 }
1386
1387 case Expr::CStyleCastExprClass:
1388 case Expr::CXXStaticCastExprClass:
1389 case Expr::CXXDynamicCastExprClass:
1390 case Expr::CXXReinterpretCastExprClass:
1391 case Expr::CXXConstCastExprClass:
1392 case Expr::CXXFunctionalCastExprClass: {
1393 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1394 Out << "cv";
1395 mangleType(ECE->getType());
1396 mangleExpression(ECE->getSubExpr());
1397 break;
1398 }
1399
Anders Carlsson58040a52009-12-16 05:48:46 +00001400 case Expr::CXXOperatorCallExprClass: {
1401 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1402 unsigned NumArgs = CE->getNumArgs();
1403 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1404 // Mangle the arguments.
1405 for (unsigned i = 0; i != NumArgs; ++i)
1406 mangleExpression(CE->getArg(i));
1407 break;
1408 }
1409
Anders Carlssona7694082009-11-06 02:50:19 +00001410 case Expr::ParenExprClass:
1411 mangleExpression(cast<ParenExpr>(E)->getSubExpr());
1412 break;
1413
Anders Carlssond553f8c2009-09-21 01:21:10 +00001414 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001415 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001416
Anders Carlssond553f8c2009-09-21 01:21:10 +00001417 switch (D->getKind()) {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001418 default:
1419 // <expr-primary> ::= L <mangled-name> E # external name
1420 Out << 'L';
1421 mangle(D, "_Z");
1422 Out << 'E';
1423 break;
1424
Anders Carlssond553f8c2009-09-21 01:21:10 +00001425 case Decl::NonTypeTemplateParm: {
1426 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001427 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00001428 break;
1429 }
1430
1431 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001432
Anders Carlsson50755b02009-09-27 20:11:34 +00001433 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001434 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001435
John McCall865d4472009-11-19 22:55:06 +00001436 case Expr::DependentScopeDeclRefExprClass: {
1437 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001438 NestedNameSpecifier *NNS = DRE->getQualifier();
1439 const Type *QTy = NNS->getAsType();
1440
1441 // When we're dealing with a nested-name-specifier that has just a
1442 // dependent identifier in it, mangle that as a typename. FIXME:
1443 // It isn't clear that we ever actually want to have such a
1444 // nested-name-specifier; why not just represent it as a typename type?
1445 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
1446 QTy = getASTContext().getTypenameType(NNS->getPrefix(),
1447 NNS->getAsIdentifier())
1448 .getTypePtr();
1449 }
Anders Carlsson50755b02009-09-27 20:11:34 +00001450 assert(QTy && "Qualifier was not type!");
1451
1452 // ::= sr <type> <unqualified-name> # dependent name
1453 Out << "sr";
1454 mangleType(QualType(QTy, 0));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001455
Anders Carlsson50755b02009-09-27 20:11:34 +00001456 assert(DRE->getDeclName().getNameKind() == DeclarationName::Identifier &&
1457 "Unhandled decl name kind!");
1458 mangleSourceName(DRE->getDeclName().getAsIdentifierInfo());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001459
Anders Carlsson50755b02009-09-27 20:11:34 +00001460 break;
1461 }
1462
John McCall1dd73832010-02-04 01:42:13 +00001463 case Expr::FloatingLiteralClass: {
1464 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
1465 Out << "L";
1466 mangleType(FL->getType());
1467
1468 // TODO: avoid this copy with careful stream management.
1469 llvm::SmallVector<char,20> Buffer;
1470 FL->getValue().bitcastToAPInt().toString(Buffer, 16, false);
1471 Out.write(Buffer.data(), Buffer.size());
1472
1473 Out << "E";
1474 break;
1475 }
1476
Anders Carlssone170ba72009-12-14 01:45:37 +00001477 case Expr::IntegerLiteralClass:
1478 mangleIntegerLiteral(E->getType(),
1479 llvm::APSInt(cast<IntegerLiteral>(E)->getValue()));
1480 break;
1481
Anders Carlssond553f8c2009-09-21 01:21:10 +00001482 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001483}
1484
John McCallefe6aee2009-09-05 07:56:18 +00001485// FIXME: <type> ::= G <type> # imaginary (C 2000)
1486// FIXME: <type> ::= U <source-name> <type> # vendor extended type qualifier
1487
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001488void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
1489 // <ctor-dtor-name> ::= C1 # complete object constructor
1490 // ::= C2 # base object constructor
1491 // ::= C3 # complete object allocating constructor
1492 //
1493 switch (T) {
1494 case Ctor_Complete:
1495 Out << "C1";
1496 break;
1497 case Ctor_Base:
1498 Out << "C2";
1499 break;
1500 case Ctor_CompleteAllocating:
1501 Out << "C3";
1502 break;
1503 }
1504}
1505
Anders Carlsson27ae5362009-04-17 01:58:57 +00001506void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
1507 // <ctor-dtor-name> ::= D0 # deleting destructor
1508 // ::= D1 # complete object destructor
1509 // ::= D2 # base object destructor
1510 //
1511 switch (T) {
1512 case Dtor_Deleting:
1513 Out << "D0";
1514 break;
1515 case Dtor_Complete:
1516 Out << "D1";
1517 break;
1518 case Dtor_Base:
1519 Out << "D2";
1520 break;
1521 }
1522}
1523
Anders Carlsson9e85c742009-12-23 19:30:55 +00001524void CXXNameMangler::mangleTemplateArgs(const TemplateArgumentList &L) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00001525 // <template-args> ::= I <template-arg>+ E
1526 Out << "I";
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00001527 for (unsigned i = 0, e = L.size(); i != e; ++i)
Anders Carlsson9e85c742009-12-23 19:30:55 +00001528 mangleTemplateArg(L[i]);
Anders Carlsson7a0ba872009-05-15 16:09:15 +00001529 Out << "E";
1530}
1531
Anders Carlsson7624f212009-09-18 02:42:01 +00001532void CXXNameMangler::mangleTemplateArgs(const TemplateArgument *TemplateArgs,
1533 unsigned NumTemplateArgs) {
1534 // <template-args> ::= I <template-arg>+ E
1535 Out << "I";
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00001536 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Anders Carlsson9e85c742009-12-23 19:30:55 +00001537 mangleTemplateArg(TemplateArgs[i]);
Anders Carlsson7624f212009-09-18 02:42:01 +00001538 Out << "E";
1539}
1540
Anders Carlsson9e85c742009-12-23 19:30:55 +00001541void CXXNameMangler::mangleTemplateArg(const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00001542 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00001543 // ::= X <expression> E # expression
1544 // ::= <expr-primary> # simple expressions
1545 // ::= I <template-arg>* E # argument pack
1546 // ::= sp <expression> # pack expansion of (C++0x)
1547 switch (A.getKind()) {
1548 default:
1549 assert(0 && "Unknown template argument kind!");
1550 case TemplateArgument::Type:
1551 mangleType(A.getAsType());
1552 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00001553 case TemplateArgument::Template:
Douglas Gregor2d565b32010-02-06 01:09:36 +00001554 assert(A.getAsTemplate().getAsTemplateDecl() &&
1555 "FIXME: Support dependent template names");
Anders Carlsson9e85c742009-12-23 19:30:55 +00001556 mangleName(A.getAsTemplate().getAsTemplateDecl());
1557 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001558 case TemplateArgument::Expression:
1559 Out << 'X';
1560 mangleExpression(A.getAsExpr());
1561 Out << 'E';
1562 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001563 case TemplateArgument::Integral:
1564 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00001565 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00001566 case TemplateArgument::Declaration: {
1567 // <expr-primary> ::= L <mangled-name> E # external name
1568
1569 // FIXME: Clang produces AST's where pointer-to-member-function expressions
1570 // and pointer-to-function expressions are represented as a declaration not
1571 // an expression; this is not how gcc represents them and this changes the
1572 // mangling.
1573 Out << 'L';
1574 // References to external entities use the mangled name; if the name would
1575 // not normally be manged then mangle it as unqualified.
1576 //
1577 // FIXME: The ABI specifies that external names here should have _Z, but
1578 // gcc leaves this off.
1579 mangle(cast<NamedDecl>(A.getAsDecl()), "Z");
1580 Out << 'E';
1581 break;
1582 }
1583 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00001584}
1585
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001586void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
1587 // <template-param> ::= T_ # first template parameter
1588 // ::= T <parameter-2 non-negative number> _
1589 if (Index == 0)
1590 Out << "T_";
1591 else
1592 Out << 'T' << (Index - 1) << '_';
1593}
1594
Anders Carlsson76967372009-09-17 00:43:46 +00001595// <substitution> ::= S <seq-id> _
1596// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00001597bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00001598 // Try one of the standard substitutions first.
1599 if (mangleStandardSubstitution(ND))
1600 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001601
Anders Carlsson433d1372009-11-07 04:26:04 +00001602 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00001603 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
1604}
1605
Anders Carlsson76967372009-09-17 00:43:46 +00001606bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00001607 if (!T.getCVRQualifiers()) {
1608 if (const RecordType *RT = T->getAs<RecordType>())
1609 return mangleSubstitution(RT->getDecl());
1610 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001611
Anders Carlsson76967372009-09-17 00:43:46 +00001612 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
1613
Anders Carlssond3a932a2009-09-17 03:53:28 +00001614 return mangleSubstitution(TypePtr);
1615}
1616
1617bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001618 llvm::DenseMap<uintptr_t, unsigned>::iterator I =
Anders Carlssond3a932a2009-09-17 03:53:28 +00001619 Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00001620 if (I == Substitutions.end())
1621 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001622
Anders Carlsson76967372009-09-17 00:43:46 +00001623 unsigned SeqID = I->second;
1624 if (SeqID == 0)
1625 Out << "S_";
1626 else {
1627 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001628
Anders Carlsson76967372009-09-17 00:43:46 +00001629 // <seq-id> is encoded in base-36, using digits and upper case letters.
1630 char Buffer[10];
1631 char *BufferPtr = Buffer + 9;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001632
Anders Carlsson76967372009-09-17 00:43:46 +00001633 *BufferPtr = 0;
1634 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001635
Anders Carlsson76967372009-09-17 00:43:46 +00001636 while (SeqID) {
1637 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001638
Anders Carlsson76967372009-09-17 00:43:46 +00001639 unsigned char c = static_cast<unsigned char>(SeqID) % 36;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001640
Anders Carlsson76967372009-09-17 00:43:46 +00001641 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
1642 SeqID /= 36;
1643 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001644
Anders Carlsson76967372009-09-17 00:43:46 +00001645 Out << 'S' << BufferPtr << '_';
1646 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001647
Anders Carlsson76967372009-09-17 00:43:46 +00001648 return true;
1649}
1650
Anders Carlssonf514b542009-09-27 00:12:57 +00001651static bool isCharType(QualType T) {
1652 if (T.isNull())
1653 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001654
Anders Carlssonf514b542009-09-27 00:12:57 +00001655 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
1656 T->isSpecificBuiltinType(BuiltinType::Char_U);
1657}
1658
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001659/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00001660/// specialization of a given name with a single argument of type char.
1661static bool isCharSpecialization(QualType T, const char *Name) {
1662 if (T.isNull())
1663 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001664
Anders Carlssonf514b542009-09-27 00:12:57 +00001665 const RecordType *RT = T->getAs<RecordType>();
1666 if (!RT)
1667 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001668
1669 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00001670 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
1671 if (!SD)
1672 return false;
1673
1674 if (!isStdNamespace(SD->getDeclContext()))
1675 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001676
Anders Carlssonf514b542009-09-27 00:12:57 +00001677 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
1678 if (TemplateArgs.size() != 1)
1679 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001680
Anders Carlssonf514b542009-09-27 00:12:57 +00001681 if (!isCharType(TemplateArgs[0].getAsType()))
1682 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001683
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001684 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00001685}
1686
Anders Carlsson91f88602009-12-07 19:56:42 +00001687template <std::size_t StrLen>
1688bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl *SD,
1689 const char (&Str)[StrLen]) {
1690 if (!SD->getIdentifier()->isStr(Str))
1691 return false;
1692
1693 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
1694 if (TemplateArgs.size() != 2)
1695 return false;
1696
1697 if (!isCharType(TemplateArgs[0].getAsType()))
1698 return false;
1699
1700 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
1701 return false;
1702
1703 return true;
1704}
1705
Anders Carlssone7c8cb62009-09-26 20:53:44 +00001706bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
1707 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00001708 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00001709 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00001710 Out << "St";
1711 return true;
1712 }
1713 }
1714
1715 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
1716 if (!isStdNamespace(TD->getDeclContext()))
1717 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001718
Anders Carlsson8c031552009-09-26 23:10:05 +00001719 // <substitution> ::= Sa # ::std::allocator
1720 if (TD->getIdentifier()->isStr("allocator")) {
1721 Out << "Sa";
1722 return true;
1723 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001724
Anders Carlsson189d59c2009-09-26 23:14:39 +00001725 // <<substitution> ::= Sb # ::std::basic_string
1726 if (TD->getIdentifier()->isStr("basic_string")) {
1727 Out << "Sb";
1728 return true;
1729 }
Anders Carlsson8c031552009-09-26 23:10:05 +00001730 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001731
1732 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00001733 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00001734 if (!isStdNamespace(SD->getDeclContext()))
1735 return false;
1736
Anders Carlssonf514b542009-09-27 00:12:57 +00001737 // <substitution> ::= Ss # ::std::basic_string<char,
1738 // ::std::char_traits<char>,
1739 // ::std::allocator<char> >
1740 if (SD->getIdentifier()->isStr("basic_string")) {
1741 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001742
Anders Carlssonf514b542009-09-27 00:12:57 +00001743 if (TemplateArgs.size() != 3)
1744 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001745
Anders Carlssonf514b542009-09-27 00:12:57 +00001746 if (!isCharType(TemplateArgs[0].getAsType()))
1747 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001748
Anders Carlssonf514b542009-09-27 00:12:57 +00001749 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
1750 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001751
Anders Carlssonf514b542009-09-27 00:12:57 +00001752 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
1753 return false;
1754
1755 Out << "Ss";
1756 return true;
1757 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001758
Anders Carlsson91f88602009-12-07 19:56:42 +00001759 // <substitution> ::= Si # ::std::basic_istream<char,
1760 // ::std::char_traits<char> >
1761 if (isStreamCharSpecialization(SD, "basic_istream")) {
1762 Out << "Si";
1763 return true;
1764 }
1765
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001766 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00001767 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00001768 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00001769 Out << "So";
1770 return true;
1771 }
Anders Carlsson91f88602009-12-07 19:56:42 +00001772
1773 // <substitution> ::= Sd # ::std::basic_iostream<char,
1774 // ::std::char_traits<char> >
1775 if (isStreamCharSpecialization(SD, "basic_iostream")) {
1776 Out << "Sd";
1777 return true;
1778 }
Anders Carlssonf514b542009-09-27 00:12:57 +00001779 }
Anders Carlsson8c031552009-09-26 23:10:05 +00001780 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00001781}
1782
Anders Carlsson76967372009-09-17 00:43:46 +00001783void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00001784 if (!T.getCVRQualifiers()) {
1785 if (const RecordType *RT = T->getAs<RecordType>()) {
1786 addSubstitution(RT->getDecl());
1787 return;
1788 }
1789 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001790
Anders Carlsson76967372009-09-17 00:43:46 +00001791 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00001792 addSubstitution(TypePtr);
1793}
1794
1795void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlsson76967372009-09-17 00:43:46 +00001796 unsigned SeqID = Substitutions.size();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001797
Anders Carlssond3a932a2009-09-17 03:53:28 +00001798 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001799 Substitutions[Ptr] = SeqID;
Anders Carlsson76967372009-09-17 00:43:46 +00001800}
1801
Daniel Dunbar1b077112009-11-21 09:06:10 +00001802//
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Daniel Dunbar1b077112009-11-21 09:06:10 +00001804/// \brief Mangles the name of the declaration D and emits that name to the
1805/// given output stream.
1806///
1807/// If the declaration D requires a mangled name, this routine will emit that
1808/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
1809/// and this routine will return false. In this case, the caller should just
1810/// emit the identifier of the declaration (\c D->getIdentifier()) as its
1811/// name.
Daniel Dunbarf981bf82009-11-21 09:14:52 +00001812void MangleContext::mangleName(const NamedDecl *D,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001813 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00001814 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1815 "Invalid mangleName() call, argument is not a variable or function!");
1816 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1817 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001818
Daniel Dunbar1b077112009-11-21 09:06:10 +00001819 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1820 getASTContext().getSourceManager(),
1821 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001823 CXXNameMangler Mangler(*this, Res);
1824 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00001825}
Mike Stump1eb44332009-09-09 15:08:12 +00001826
Daniel Dunbar1b077112009-11-21 09:06:10 +00001827void MangleContext::mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001828 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00001829 CXXNameMangler Mangler(*this, Res, D, Type);
1830 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00001831}
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Daniel Dunbar1b077112009-11-21 09:06:10 +00001833void MangleContext::mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001834 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00001835 CXXNameMangler Mangler(*this, Res, D, Type);
1836 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00001837}
Mike Stumpf1216772009-07-31 18:25:34 +00001838
Daniel Dunbarc0747712009-11-21 09:12:13 +00001839/// \brief Mangles the a thunk with the offset n for the declaration D and
1840/// emits that name to the given output stream.
Anders Carlssona94822e2009-11-26 02:32:05 +00001841void MangleContext::mangleThunk(const FunctionDecl *FD,
Anders Carlssonb73a5be2009-11-26 02:49:32 +00001842 const ThunkAdjustment &ThisAdjustment,
Daniel Dunbarc0747712009-11-21 09:12:13 +00001843 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00001844 assert(!isa<CXXDestructorDecl>(FD) &&
1845 "Use mangleCXXDtor for destructor decls!");
1846
1847 // <special-name> ::= T <call-offset> <base encoding>
1848 // # base is the nominal target function of thunk
1849 CXXNameMangler Mangler(*this, Res);
1850 Mangler.getStream() << "_ZT";
Anders Carlssonb73a5be2009-11-26 02:49:32 +00001851 Mangler.mangleCallOffset(ThisAdjustment);
Daniel Dunbarc0747712009-11-21 09:12:13 +00001852 Mangler.mangleFunctionEncoding(FD);
1853}
1854
Eli Friedman61d89b62009-12-03 00:03:05 +00001855void MangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *D,
1856 CXXDtorType Type,
1857 const ThunkAdjustment &ThisAdjustment,
1858 llvm::SmallVectorImpl<char> &Res) {
1859 // <special-name> ::= T <call-offset> <base encoding>
1860 // # base is the nominal target function of thunk
1861 CXXNameMangler Mangler(*this, Res, D, Type);
1862 Mangler.getStream() << "_ZT";
1863 Mangler.mangleCallOffset(ThisAdjustment);
1864 Mangler.mangleFunctionEncoding(D);
1865}
1866
Daniel Dunbarc0747712009-11-21 09:12:13 +00001867/// \brief Mangles the a covariant thunk for the declaration D and emits that
1868/// name to the given output stream.
Anders Carlsson7622cd32009-11-26 03:09:37 +00001869void
1870MangleContext::mangleCovariantThunk(const FunctionDecl *FD,
1871 const CovariantThunkAdjustment& Adjustment,
1872 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00001873 assert(!isa<CXXDestructorDecl>(FD) &&
Eli Friedman61d89b62009-12-03 00:03:05 +00001874 "No such thing as a covariant thunk for a destructor!");
Daniel Dunbarc0747712009-11-21 09:12:13 +00001875
1876 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
1877 // # base is the nominal target function of thunk
1878 // # first call-offset is 'this' adjustment
1879 // # second call-offset is result adjustment
1880 CXXNameMangler Mangler(*this, Res);
1881 Mangler.getStream() << "_ZTc";
Anders Carlsson7622cd32009-11-26 03:09:37 +00001882 Mangler.mangleCallOffset(Adjustment.ThisAdjustment);
1883 Mangler.mangleCallOffset(Adjustment.ReturnAdjustment);
Daniel Dunbarc0747712009-11-21 09:12:13 +00001884 Mangler.mangleFunctionEncoding(FD);
1885}
1886
1887/// mangleGuardVariable - Returns the mangled name for a guard variable
1888/// for the passed in VarDecl.
1889void MangleContext::mangleGuardVariable(const VarDecl *D,
1890 llvm::SmallVectorImpl<char> &Res) {
1891 // <special-name> ::= GV <object name> # Guard variable for one-time
1892 // # initialization
1893 CXXNameMangler Mangler(*this, Res);
1894 Mangler.getStream() << "_ZGV";
1895 Mangler.mangleName(D);
1896}
1897
Daniel Dunbar1b077112009-11-21 09:06:10 +00001898void MangleContext::mangleCXXVtable(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001899 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00001900 // <special-name> ::= TV <type> # virtual table
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001901 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00001902 Mangler.getStream() << "_ZTV";
1903 Mangler.mangleName(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00001904}
Mike Stump82d75b02009-11-10 01:58:37 +00001905
Daniel Dunbar1b077112009-11-21 09:06:10 +00001906void MangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001907 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00001908 // <special-name> ::= TT <type> # VTT structure
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001909 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00001910 Mangler.getStream() << "_ZTT";
1911 Mangler.mangleName(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00001912}
Mike Stumpab3f7e92009-11-10 01:41:59 +00001913
Daniel Dunbar1b077112009-11-21 09:06:10 +00001914void MangleContext::mangleCXXCtorVtable(const CXXRecordDecl *RD, int64_t Offset,
1915 const CXXRecordDecl *Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001916 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00001917 // <special-name> ::= TC <type> <offset number> _ <base type>
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001918 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00001919 Mangler.getStream() << "_ZTC";
1920 Mangler.mangleName(RD);
1921 Mangler.getStream() << Offset;
1922 Mangler.getStream() << "_";
1923 Mangler.mangleName(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00001924}
Mike Stump738f8c22009-07-31 23:15:31 +00001925
Mike Stumpde050572009-12-02 18:57:08 +00001926void MangleContext::mangleCXXRTTI(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001927 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00001928 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00001929 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001930 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00001931 Mangler.getStream() << "_ZTI";
1932 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00001933}
Mike Stump67795982009-11-14 00:14:13 +00001934
Mike Stumpde050572009-12-02 18:57:08 +00001935void MangleContext::mangleCXXRTTIName(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001936 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00001937 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001938 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00001939 Mangler.getStream() << "_ZTS";
1940 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00001941}