blob: 20d54b34105dddad9cc9dac651794cd744cefaea [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
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000217 // No mangling in an "implicit extern C" header.
218 if (D->getLocation().isValid() &&
219 getASTContext().getSourceManager().
220 isInExternCSystemHeader(D->getLocation()))
221 return false;
Anders Carlsson43f17402009-04-02 15:51:53 +0000222
Sean Hunt31455252010-01-24 03:04:27 +0000223 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000224 if (!FD) {
225 const DeclContext *DC = D->getDeclContext();
226 // Check for extern variable declared locally.
227 if (isa<FunctionDecl>(DC) && D->hasLinkage())
228 while (!DC->isNamespace() && !DC->isTranslationUnit())
229 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000230 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000231 return false;
232 }
233
234 // C functions and "main" are not mangled.
235 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000236 return false;
237
Anders Carlsson43f17402009-04-02 15:51:53 +0000238 return true;
239}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000240
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000241void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000242 // Any decl can be declared with __asm("foo") on it, and this takes precedence
243 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000244 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000245 // If we have an asm name, then we use it as the mangling.
246 Out << '\01'; // LLVM IR Marker for __asm("foo")
247 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000248 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000249 }
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Sean Hunt31455252010-01-24 03:04:27 +0000251 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000252 // ::= <data name>
253 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000254 Out << Prefix;
255 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000256 mangleFunctionEncoding(FD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000257 else
258 mangleName(cast<VarDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000259}
260
261void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
262 // <encoding> ::= <function name> <bare-function-type>
263 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000265 // Don't mangle in the type if this isn't a decl we should typically mangle.
266 if (!Context.shouldMangleDeclName(FD))
267 return;
268
Mike Stump141c5af2009-09-02 00:25:38 +0000269 // Whether the mangling of a function type includes the return type depends on
270 // the context and the nature of the function. The rules for deciding whether
271 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000272 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000273 // 1. Template functions (names or types) have return types encoded, with
274 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000275 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000276 // e.g. parameters, pointer types, etc., have return type encoded, with the
277 // exceptions listed below.
278 // 3. Non-template function names do not have return types encoded.
279 //
Mike Stump141c5af2009-09-02 00:25:38 +0000280 // The exceptions mentioned in (1) and (2) above, for which the return type is
281 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000282 // 1. Constructors.
283 // 2. Destructors.
284 // 3. Conversion operator functions, e.g. operator int.
285 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000286 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
287 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
288 isa<CXXConversionDecl>(FD)))
289 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000290
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000291 // Mangle the type of the primary template.
292 FD = PrimaryTemplate->getTemplatedDecl();
293 }
294
John McCall54e14c42009-10-22 22:37:11 +0000295 // Do the canonicalization out here because parameter types can
296 // undergo additional canonicalization (e.g. array decay).
297 FunctionType *FT = cast<FunctionType>(Context.getASTContext()
298 .getCanonicalType(FD->getType()));
299
300 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000301}
302
Anders Carlsson47846d22009-12-04 06:23:23 +0000303/// isStd - Return whether a given namespace is the 'std' namespace.
304static bool isStd(const NamespaceDecl *NS) {
John McCall9aeed322009-10-01 00:25:31 +0000305 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
306 return II && II->isStr("std");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000307}
308
Anders Carlsson47846d22009-12-04 06:23:23 +0000309static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
310 while (isa<LinkageSpecDecl>(DC)) {
311 assert(cast<LinkageSpecDecl>(DC)->getLanguage() ==
312 LinkageSpecDecl::lang_cxx && "Unexpected linkage decl!");
313 DC = DC->getParent();
314 }
315
316 return DC;
317}
318
319// isStdNamespace - Return whether a given decl context is a toplevel 'std'
320// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000321static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000322 if (!DC->isNamespace())
323 return false;
324
325 if (!IgnoreLinkageSpecDecls(DC->getParent())->isTranslationUnit())
326 return false;
327
328 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000329}
330
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000331static const TemplateDecl *
332isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000333 // Check if we have a function template.
334 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000335 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000336 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000337 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000338 }
339 }
340
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000341 // Check if we have a class template.
342 if (const ClassTemplateSpecializationDecl *Spec =
343 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
344 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000345 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000346 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000347
Anders Carlsson2744a062009-09-18 19:00:18 +0000348 return 0;
349}
350
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000351void CXXNameMangler::mangleName(const NamedDecl *ND) {
352 // <name> ::= <nested-name>
353 // ::= <unscoped-name>
354 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000355 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000356 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000357 const DeclContext *DC = ND->getDeclContext();
Fariborz Jahanian57058532010-03-03 19:41:08 +0000358
359 if (GetLocalClassFunctionDeclContext(DC)) {
360 mangleLocalName(ND);
361 return;
362 }
363
Eli Friedman7facf842009-12-02 20:32:49 +0000364 // If this is an extern variable declared locally, the relevant DeclContext
365 // is that of the containing namespace, or the translation unit.
366 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
367 while (!DC->isNamespace() && !DC->isTranslationUnit())
368 DC = DC->getParent();
369
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000370 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000371 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000372
Anders Carlssond58d6f72009-09-17 16:12:20 +0000373 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000374 // Check if we have a template.
375 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000376 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000377 mangleUnscopedTemplateName(TD);
Anders Carlsson9e85c742009-12-23 19:30:55 +0000378 mangleTemplateArgs(*TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000379 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000380 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000381
Anders Carlsson7482e242009-09-18 04:29:09 +0000382 mangleUnscopedName(ND);
383 return;
384 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000385
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000386 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000387 mangleLocalName(ND);
388 return;
389 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000390
Eli Friedman7facf842009-12-02 20:32:49 +0000391 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000392}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000393void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000394 const TemplateArgument *TemplateArgs,
395 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000396 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000397
Anders Carlsson7624f212009-09-18 02:42:01 +0000398 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000399 mangleUnscopedTemplateName(TD);
Anders Carlsson7624f212009-09-18 02:42:01 +0000400 mangleTemplateArgs(TemplateArgs, NumTemplateArgs);
401 } else {
402 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
403 }
404}
405
Anders Carlsson201ce742009-09-17 03:17:01 +0000406void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
407 // <unscoped-name> ::= <unqualified-name>
408 // ::= St <unqualified-name> # ::std::
409 if (isStdNamespace(ND->getDeclContext()))
410 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000411
Anders Carlsson201ce742009-09-17 03:17:01 +0000412 mangleUnqualifiedName(ND);
413}
414
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000415void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000416 // <unscoped-template-name> ::= <unscoped-name>
417 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000418 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000419 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000420
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000421 // <template-template-param> ::= <template-param>
422 if (const TemplateTemplateParmDecl *TTP
423 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
424 mangleTemplateParameter(TTP->getIndex());
425 return;
426 }
427
Anders Carlsson1668f202009-09-26 20:13:56 +0000428 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000429 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000430}
431
Anders Carlssona94822e2009-11-26 02:32:05 +0000432void CXXNameMangler::mangleNumber(int64_t Number) {
433 // <number> ::= [n] <non-negative decimal integer>
434 if (Number < 0) {
435 Out << 'n';
436 Number = -Number;
437 }
438
439 Out << Number;
440}
441
Anders Carlssonb73a5be2009-11-26 02:49:32 +0000442void CXXNameMangler::mangleCallOffset(const ThunkAdjustment &Adjustment) {
Mike Stump141c5af2009-09-02 00:25:38 +0000443 // <call-offset> ::= h <nv-offset> _
444 // ::= v <v-offset> _
445 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000446 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000447 // # virtual base override, with vcall offset
Anders Carlssonb73a5be2009-11-26 02:49:32 +0000448 if (!Adjustment.Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000449 Out << 'h';
Anders Carlssonb73a5be2009-11-26 02:49:32 +0000450 mangleNumber(Adjustment.NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000451 Out << '_';
452 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000453 }
Anders Carlssona94822e2009-11-26 02:32:05 +0000454
455 Out << 'v';
Anders Carlssonb73a5be2009-11-26 02:49:32 +0000456 mangleNumber(Adjustment.NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000457 Out << '_';
Anders Carlssonb73a5be2009-11-26 02:49:32 +0000458 mangleNumber(Adjustment.Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000459 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000460}
461
John McCall1dd73832010-02-04 01:42:13 +0000462void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
463 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
464 switch (Qualifier->getKind()) {
465 case NestedNameSpecifier::Global:
466 // nothing
467 break;
468 case NestedNameSpecifier::Namespace:
469 mangleName(Qualifier->getAsNamespace());
470 break;
471 case NestedNameSpecifier::TypeSpec:
472 case NestedNameSpecifier::TypeSpecWithTemplate:
473 mangleType(QualType(Qualifier->getAsType(), 0));
474 break;
475 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000476 // Member expressions can have these without prefixes.
477 if (Qualifier->getPrefix())
478 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000479 mangleSourceName(Qualifier->getAsIdentifier());
480 break;
481 }
482}
483
484/// Mangles a name which was not resolved to a specific entity.
485void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
486 DeclarationName Name,
487 unsigned KnownArity) {
488 if (Qualifier)
489 mangleUnresolvedScope(Qualifier);
490 // FIXME: ambiguity of unqualified lookup with ::
491
492 mangleUnqualifiedName(0, Name, KnownArity);
493}
494
495void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
496 DeclarationName Name,
497 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000498 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000499 // ::= <ctor-dtor-name>
500 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000501 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000502 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000503 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000504 // We must avoid conflicts between internally- and externally-
Anders Carlssonaec25232010-02-06 04:52:27 +0000505 // linked variable declaration names in the same TU.
506 // This naming convention is the same as that followed by GCC, though it
507 // shouldn't actually matter.
508 if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000509 ND->getDeclContext()->isFileContext())
510 Out << 'L';
511
Anders Carlssonc4355b62009-10-07 01:45:02 +0000512 mangleSourceName(II);
513 break;
514 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000515
John McCall1dd73832010-02-04 01:42:13 +0000516 // Otherwise, an anonymous entity. We must have a declaration.
517 assert(ND && "mangling empty name without declaration");
518
519 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
520 if (NS->isAnonymousNamespace()) {
521 // This is how gcc mangles these names.
522 Out << "12_GLOBAL__N_1";
523 break;
524 }
525 }
526
Anders Carlssonc4355b62009-10-07 01:45:02 +0000527 // We must have an anonymous struct.
528 const TagDecl *TD = cast<TagDecl>(ND);
529 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
530 assert(TD->getDeclContext() == D->getDeclContext() &&
531 "Typedef should not be in another decl context!");
532 assert(D->getDeclName().getAsIdentifierInfo() &&
533 "Typedef was not named!");
534 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
535 break;
536 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000537
Anders Carlssonc4355b62009-10-07 01:45:02 +0000538 // Get a unique id for the anonymous struct.
539 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
540
541 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000542 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000543 // where n is the length of the string.
544 llvm::SmallString<8> Str;
545 Str += "$_";
546 Str += llvm::utostr(AnonStructId);
547
548 Out << Str.size();
549 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000550 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000551 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000552
553 case DeclarationName::ObjCZeroArgSelector:
554 case DeclarationName::ObjCOneArgSelector:
555 case DeclarationName::ObjCMultiArgSelector:
556 assert(false && "Can't mangle Objective-C selector names here!");
557 break;
558
559 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000560 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000561 // If the named decl is the C++ constructor we're mangling, use the type
562 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000563 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000564 else
565 // Otherwise, use the complete constructor name. This is relevant if a
566 // class with a constructor is declared within a constructor.
567 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000568 break;
569
570 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000571 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000572 // If the named decl is the C++ destructor we're mangling, use the type we
573 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000574 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
575 else
576 // Otherwise, use the complete destructor name. This is relevant if a
577 // class with a destructor is declared within a destructor.
578 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000579 break;
580
581 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000582 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000583 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000584 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000585 break;
586
Anders Carlsson8257d412009-12-22 06:36:32 +0000587 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000588 unsigned Arity;
589 if (ND) {
590 Arity = cast<FunctionDecl>(ND)->getNumParams();
Anders Carlsson8257d412009-12-22 06:36:32 +0000591
John McCall1dd73832010-02-04 01:42:13 +0000592 // If we have a C++ member function, we need to include the 'this' pointer.
593 // FIXME: This does not make sense for operators that are static, but their
594 // names stay the same regardless of the arity (operator new for instance).
595 if (isa<CXXMethodDecl>(ND))
596 Arity++;
597 } else
598 Arity = KnownArity;
599
Anders Carlsson8257d412009-12-22 06:36:32 +0000600 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000601 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000602 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000603
Sean Hunt3e518bd2009-11-29 07:34:05 +0000604 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000605 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000606 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000607 mangleSourceName(Name.getCXXLiteralIdentifier());
608 break;
609
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000610 case DeclarationName::CXXUsingDirective:
611 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000612 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000613 }
614}
615
616void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
617 // <source-name> ::= <positive length number> <identifier>
618 // <number> ::= [n] <non-negative decimal integer>
619 // <identifier> ::= <unqualified source code identifier>
620 Out << II->getLength() << II->getName();
621}
622
Eli Friedman7facf842009-12-02 20:32:49 +0000623void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000624 const DeclContext *DC,
625 bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000626 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
627 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000628
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000629 Out << 'N';
630 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
John McCall0953e762009-09-24 19:53:00 +0000631 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000632
Anders Carlsson2744a062009-09-18 19:00:18 +0000633 // Check if we have a template.
634 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000635 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000636 mangleTemplatePrefix(TD);
Anders Carlsson9e85c742009-12-23 19:30:55 +0000637 mangleTemplateArgs(*TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000638 }
639 else {
640 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000641 mangleUnqualifiedName(ND);
642 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000643
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000644 Out << 'E';
645}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000646void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000647 const TemplateArgument *TemplateArgs,
648 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000649 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
650
Anders Carlsson7624f212009-09-18 02:42:01 +0000651 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000652
Anders Carlssone45117b2009-09-27 19:53:49 +0000653 mangleTemplatePrefix(TD);
Anders Carlsson7624f212009-09-18 02:42:01 +0000654 mangleTemplateArgs(TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000655
Anders Carlsson7624f212009-09-18 02:42:01 +0000656 Out << 'E';
657}
658
Anders Carlsson1b42c792009-04-02 16:24:45 +0000659void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
660 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
661 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000662 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000663 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000664 Out << 'Z';
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000665
Fariborz Jahanian57058532010-03-03 19:41:08 +0000666 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC))
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000667 mangleObjCMethodName(MD);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000668 else if (const DeclContext *CDC = GetLocalClassFunctionDeclContext(DC)) {
669 mangleFunctionEncoding(cast<FunctionDecl>(CDC));
670 Out << 'E';
671 mangleNestedName(ND, DC, true /*NoFunction*/);
672
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000673 // FIXME. This still does not cover all cases.
674 unsigned disc;
675 if (Context.getNextDiscriminator(ND, disc)) {
676 if (disc < 10)
677 Out << '_' << disc;
678 else
679 Out << "__" << disc << '_';
680 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000681
682 return;
683 }
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000684 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000685 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000686
Anders Carlsson1b42c792009-04-02 16:24:45 +0000687 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000688 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000689}
690
Fariborz Jahanian57058532010-03-03 19:41:08 +0000691void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000692 // <prefix> ::= <prefix> <unqualified-name>
693 // ::= <template-prefix> <template-args>
694 // ::= <template-param>
695 // ::= # empty
696 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000697
Anders Carlssonadd28822009-09-22 20:33:31 +0000698 while (isa<LinkageSpecDecl>(DC))
699 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000700
Anders Carlsson9263e912009-09-18 18:39:58 +0000701 if (DC->isTranslationUnit())
702 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000703
Anders Carlsson6862fc72009-09-17 04:16:28 +0000704 if (mangleSubstitution(cast<NamedDecl>(DC)))
705 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000706
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000707 // Check if we have a template.
708 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000709 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000710 mangleTemplatePrefix(TD);
Anders Carlsson9e85c742009-12-23 19:30:55 +0000711 mangleTemplateArgs(*TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000712 }
713 else if(NoFunction && isa<FunctionDecl>(DC))
714 return;
715 else {
716 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000717 mangleUnqualifiedName(cast<NamedDecl>(DC));
718 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000719
Anders Carlsson6862fc72009-09-17 04:16:28 +0000720 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000721}
722
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000723void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000724 // <template-prefix> ::= <prefix> <template unqualified-name>
725 // ::= <template-param>
726 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000727 // <template-template-param> ::= <template-param>
728 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000729
Anders Carlssonaeb85372009-09-26 22:18:22 +0000730 if (mangleSubstitution(ND))
731 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000732
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000733 // <template-template-param> ::= <template-param>
734 if (const TemplateTemplateParmDecl *TTP
735 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
736 mangleTemplateParameter(TTP->getIndex());
737 return;
738 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000739
Anders Carlssonaa73ab12009-09-18 18:47:07 +0000740 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +0000741 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +0000742 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +0000743}
744
Mike Stump1eb44332009-09-09 15:08:12 +0000745void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000746CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
747 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000748 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000749 case OO_New: Out << "nw"; break;
750 // ::= na # new[]
751 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000752 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000753 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000754 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000755 case OO_Array_Delete: Out << "da"; break;
756 // ::= ps # + (unary)
757 // ::= pl # +
Anders Carlsson8257d412009-12-22 06:36:32 +0000758 case OO_Plus:
759 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
760 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000761 // ::= ng # - (unary)
762 // ::= mi # -
Anders Carlsson8257d412009-12-22 06:36:32 +0000763 case OO_Minus:
764 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
765 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000766 // ::= ad # & (unary)
767 // ::= an # &
Anders Carlsson8257d412009-12-22 06:36:32 +0000768 case OO_Amp:
769 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
770 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000771 // ::= de # * (unary)
772 // ::= ml # *
Anders Carlsson8257d412009-12-22 06:36:32 +0000773 case OO_Star:
774 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
775 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000776 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000777 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000778 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000779 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000780 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000781 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000782 // ::= or # |
783 case OO_Pipe: Out << "or"; break;
784 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000785 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000786 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000787 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000788 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000789 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000790 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000791 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000792 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000793 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000794 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000795 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000796 // ::= rM # %=
797 case OO_PercentEqual: Out << "rM"; break;
798 // ::= aN # &=
799 case OO_AmpEqual: Out << "aN"; break;
800 // ::= oR # |=
801 case OO_PipeEqual: Out << "oR"; break;
802 // ::= eO # ^=
803 case OO_CaretEqual: Out << "eO"; break;
804 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000805 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000806 // ::= rs # >>
807 case OO_GreaterGreater: Out << "rs"; break;
808 // ::= lS # <<=
809 case OO_LessLessEqual: Out << "lS"; break;
810 // ::= rS # >>=
811 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000812 // ::= eq # ==
813 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000814 // ::= ne # !=
815 case OO_ExclaimEqual: Out << "ne"; break;
816 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000817 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000818 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000819 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000820 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000821 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000822 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000823 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000824 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000825 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000826 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000827 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000828 // ::= oo # ||
829 case OO_PipePipe: Out << "oo"; break;
830 // ::= pp # ++
831 case OO_PlusPlus: Out << "pp"; break;
832 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000833 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000834 // ::= cm # ,
835 case OO_Comma: Out << "cm"; break;
836 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000837 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000838 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000839 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000840 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000841 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000842 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000843 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +0000844
845 // ::= qu # ?
846 // The conditional operator can't be overloaded, but we still handle it when
847 // mangling expressions.
848 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000849
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000850 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000851 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +0000852 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000853 break;
854 }
855}
856
John McCall0953e762009-09-24 19:53:00 +0000857void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +0000858 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +0000859 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000860 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +0000861 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000862 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +0000863 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000864 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +0000865
866 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000867}
868
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000869void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
870 llvm::SmallString<64> Name;
871 llvm::raw_svector_ostream OS(Name);
872
873 const ObjCContainerDecl *CD =
874 dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
875 assert (CD && "Missing container decl in GetNameForMethod");
876 OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName();
877 if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD))
878 OS << '(' << CID->getNameAsString() << ')';
879 OS << ' ' << MD->getSelector().getAsString() << ']';
880
881 Out << OS.str().size() << OS.str();
882}
883
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000884void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000885 // Only operate on the canonical type!
Anders Carlssonb5404912009-10-07 01:06:45 +0000886 T = Context.getASTContext().getCanonicalType(T);
Anders Carlsson4843e582009-03-10 17:07:44 +0000887
Douglas Gregora4923eb2009-11-16 21:35:15 +0000888 bool IsSubstitutable = T.hasLocalQualifiers() || !isa<BuiltinType>(T);
Anders Carlsson76967372009-09-17 00:43:46 +0000889 if (IsSubstitutable && mangleSubstitution(T))
890 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000891
Douglas Gregora4923eb2009-11-16 21:35:15 +0000892 if (Qualifiers Quals = T.getLocalQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +0000893 mangleQualifiers(Quals);
894 // Recurse: even if the qualified type isn't yet substitutable,
895 // the unqualified type might be.
Douglas Gregora4923eb2009-11-16 21:35:15 +0000896 mangleType(T.getLocalUnqualifiedType());
Anders Carlsson76967372009-09-17 00:43:46 +0000897 } else {
898 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +0000899#define ABSTRACT_TYPE(CLASS, PARENT)
900#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000901 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000902 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +0000903 return;
John McCallefe6aee2009-09-05 07:56:18 +0000904#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000905 case Type::CLASS: \
John McCall0953e762009-09-24 19:53:00 +0000906 mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \
Anders Carlsson76967372009-09-17 00:43:46 +0000907 break;
John McCallefe6aee2009-09-05 07:56:18 +0000908#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +0000909 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000910 }
Anders Carlsson76967372009-09-17 00:43:46 +0000911
912 // Add the substitution.
913 if (IsSubstitutable)
914 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000915}
916
917void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +0000918 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000919 // <builtin-type> ::= v # void
920 // ::= w # wchar_t
921 // ::= b # bool
922 // ::= c # char
923 // ::= a # signed char
924 // ::= h # unsigned char
925 // ::= s # short
926 // ::= t # unsigned short
927 // ::= i # int
928 // ::= j # unsigned int
929 // ::= l # long
930 // ::= m # unsigned long
931 // ::= x # long long, __int64
932 // ::= y # unsigned long long, __int64
933 // ::= n # __int128
934 // UNSUPPORTED: ::= o # unsigned __int128
935 // ::= f # float
936 // ::= d # double
937 // ::= e # long double, __float80
938 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000939 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
940 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
941 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
942 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000943 // ::= Di # char32_t
944 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000945 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000946 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
947 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000948 switch (T->getKind()) {
949 case BuiltinType::Void: Out << 'v'; break;
950 case BuiltinType::Bool: Out << 'b'; break;
951 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
952 case BuiltinType::UChar: Out << 'h'; break;
953 case BuiltinType::UShort: Out << 't'; break;
954 case BuiltinType::UInt: Out << 'j'; break;
955 case BuiltinType::ULong: Out << 'm'; break;
956 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000957 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000958 case BuiltinType::SChar: Out << 'a'; break;
959 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000960 case BuiltinType::Char16: Out << "Ds"; break;
961 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000962 case BuiltinType::Short: Out << 's'; break;
963 case BuiltinType::Int: Out << 'i'; break;
964 case BuiltinType::Long: Out << 'l'; break;
965 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000966 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000967 case BuiltinType::Float: Out << 'f'; break;
968 case BuiltinType::Double: Out << 'd'; break;
969 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000970 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000971
972 case BuiltinType::Overload:
973 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +0000974 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000975 "Overloaded and dependent types shouldn't get to name mangling");
976 break;
Anders Carlssone89d1592009-06-26 18:41:36 +0000977 case BuiltinType::UndeducedAuto:
978 assert(0 && "Should not see undeduced auto here");
979 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +0000980 case BuiltinType::ObjCId: Out << "11objc_object"; break;
981 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000982 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000983 }
984}
985
John McCallefe6aee2009-09-05 07:56:18 +0000986// <type> ::= <function-type>
987// <function-type> ::= F [Y] <bare-function-type> E
988void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000989 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +0000990 // FIXME: We don't have enough information in the AST to produce the 'Y'
991 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000992 mangleBareFunctionType(T, /*MangleReturnType=*/true);
993 Out << 'E';
994}
John McCallefe6aee2009-09-05 07:56:18 +0000995void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000996 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +0000997}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000998void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
999 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001000 // We should never be mangling something without a prototype.
1001 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1002
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001003 // <bare-function-type> ::= <signature type>+
1004 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001005 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001006
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001007 if (Proto->getNumArgs() == 0) {
1008 Out << 'v';
1009 return;
1010 }
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Douglas Gregor72564e72009-02-26 23:50:07 +00001012 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001013 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001014 Arg != ArgEnd; ++Arg)
1015 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001016
1017 // <builtin-type> ::= z # ellipsis
1018 if (Proto->isVariadic())
1019 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001020}
1021
John McCallefe6aee2009-09-05 07:56:18 +00001022// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001023// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001024void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1025 mangleName(T->getDecl());
1026}
1027
1028// <type> ::= <class-enum-type>
1029// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001030void CXXNameMangler::mangleType(const EnumType *T) {
1031 mangleType(static_cast<const TagType*>(T));
1032}
1033void CXXNameMangler::mangleType(const RecordType *T) {
1034 mangleType(static_cast<const TagType*>(T));
1035}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001036void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001037 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001038}
1039
John McCallefe6aee2009-09-05 07:56:18 +00001040// <type> ::= <array-type>
1041// <array-type> ::= A <positive dimension number> _ <element type>
1042// ::= A [<dimension expression>] _ <element type>
1043void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1044 Out << 'A' << T->getSize() << '_';
1045 mangleType(T->getElementType());
1046}
1047void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001048 Out << 'A';
John McCallefe6aee2009-09-05 07:56:18 +00001049 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001050 Out << '_';
1051 mangleType(T->getElementType());
1052}
John McCallefe6aee2009-09-05 07:56:18 +00001053void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1054 Out << 'A';
1055 mangleExpression(T->getSizeExpr());
1056 Out << '_';
1057 mangleType(T->getElementType());
1058}
1059void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
1060 Out << 'A' << '_';
1061 mangleType(T->getElementType());
1062}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001063
John McCallefe6aee2009-09-05 07:56:18 +00001064// <type> ::= <pointer-to-member-type>
1065// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001066void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001067 Out << 'M';
1068 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001069 QualType PointeeType = T->getPointeeType();
1070 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001071 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Anders Carlsson0e650012009-05-17 17:41:20 +00001072 mangleType(FPT);
Mike Stump1eb44332009-09-09 15:08:12 +00001073 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001074 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001075}
1076
John McCallefe6aee2009-09-05 07:56:18 +00001077// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001078void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001079 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001080}
1081
John McCallefe6aee2009-09-05 07:56:18 +00001082// FIXME: <type> ::= <template-template-param> <template-args>
John McCallefe6aee2009-09-05 07:56:18 +00001083
1084// <type> ::= P <type> # pointer-to
1085void CXXNameMangler::mangleType(const PointerType *T) {
1086 Out << 'P';
1087 mangleType(T->getPointeeType());
1088}
1089void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1090 Out << 'P';
1091 mangleType(T->getPointeeType());
1092}
1093
1094// <type> ::= R <type> # reference-to
1095void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1096 Out << 'R';
1097 mangleType(T->getPointeeType());
1098}
1099
1100// <type> ::= O <type> # rvalue reference-to (C++0x)
1101void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1102 Out << 'O';
1103 mangleType(T->getPointeeType());
1104}
1105
1106// <type> ::= C <type> # complex pair (C 2000)
1107void CXXNameMangler::mangleType(const ComplexType *T) {
1108 Out << 'C';
1109 mangleType(T->getElementType());
1110}
1111
1112// GNU extension: vector types
1113void CXXNameMangler::mangleType(const VectorType *T) {
1114 Out << "U8__vector";
1115 mangleType(T->getElementType());
1116}
1117void CXXNameMangler::mangleType(const ExtVectorType *T) {
1118 mangleType(static_cast<const VectorType*>(T));
1119}
1120void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
1121 Out << "U8__vector";
1122 mangleType(T->getElementType());
1123}
1124
Anders Carlssona40c5e42009-03-07 22:03:21 +00001125void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1126 mangleSourceName(T->getDecl()->getIdentifier());
1127}
1128
John McCallefe6aee2009-09-05 07:56:18 +00001129void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001130 Out << "U13block_pointer";
1131 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001132}
1133
John McCallefe6aee2009-09-05 07:56:18 +00001134void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Anders Carlsson7624f212009-09-18 02:42:01 +00001135 TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl();
1136 assert(TD && "FIXME: Support dependent template names!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001137
Anders Carlsson7624f212009-09-18 02:42:01 +00001138 mangleName(TD, T->getArgs(), T->getNumArgs());
John McCallefe6aee2009-09-05 07:56:18 +00001139}
1140
1141void CXXNameMangler::mangleType(const TypenameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001142 // Typename types are always nested
1143 Out << 'N';
1144
1145 const Type *QTy = T->getQualifier()->getAsType();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001146 if (const TemplateSpecializationType *TST =
Anders Carlssonae352482009-09-26 02:26:02 +00001147 dyn_cast<TemplateSpecializationType>(QTy)) {
Anders Carlsson88599172009-09-27 01:06:07 +00001148 if (!mangleSubstitution(QualType(TST, 0))) {
1149 TemplateDecl *TD = TST->getTemplateName().getAsTemplateDecl();
Douglas Gregor2d565b32010-02-06 01:09:36 +00001150 assert(TD && "FIXME: Support dependent template names");
Anders Carlsson88599172009-09-27 01:06:07 +00001151 mangleTemplatePrefix(TD);
1152 mangleTemplateArgs(TST->getArgs(), TST->getNumArgs());
1153 addSubstitution(QualType(TST, 0));
1154 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001155 } else if (const TemplateTypeParmType *TTPT =
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001156 dyn_cast<TemplateTypeParmType>(QTy)) {
1157 // We use the QualType mangle type variant here because it handles
1158 // substitutions.
1159 mangleType(QualType(TTPT, 0));
Anders Carlssonae352482009-09-26 02:26:02 +00001160 } else
1161 assert(false && "Unhandled type!");
1162
1163 mangleSourceName(T->getIdentifier());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001164
Anders Carlssonae352482009-09-26 02:26:02 +00001165 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001166}
1167
John McCallad5e7382010-03-01 23:49:17 +00001168void CXXNameMangler::mangleType(const TypeOfType *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 TypeOfExprType *T) {
1175 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1176 // "extension with parameters" mangling.
1177 Out << "u6typeof";
1178}
1179
1180void CXXNameMangler::mangleType(const DecltypeType *T) {
1181 Expr *E = T->getUnderlyingExpr();
1182
1183 // type ::= Dt <expression> E # decltype of an id-expression
1184 // # or class member access
1185 // ::= DT <expression> E # decltype of an expression
1186
1187 // This purports to be an exhaustive list of id-expressions and
1188 // class member accesses. Note that we do not ignore parentheses;
1189 // parentheses change the semantics of decltype for these
1190 // expressions (and cause the mangler to use the other form).
1191 if (isa<DeclRefExpr>(E) ||
1192 isa<MemberExpr>(E) ||
1193 isa<UnresolvedLookupExpr>(E) ||
1194 isa<DependentScopeDeclRefExpr>(E) ||
1195 isa<CXXDependentScopeMemberExpr>(E) ||
1196 isa<UnresolvedMemberExpr>(E))
1197 Out << "Dt";
1198 else
1199 Out << "DT";
1200 mangleExpression(E);
1201 Out << 'E';
1202}
1203
Anders Carlssone170ba72009-12-14 01:45:37 +00001204void CXXNameMangler::mangleIntegerLiteral(QualType T,
1205 const llvm::APSInt &Value) {
1206 // <expr-primary> ::= L <type> <value number> E # integer literal
1207 Out << 'L';
1208
1209 mangleType(T);
1210 if (T->isBooleanType()) {
1211 // Boolean values are encoded as 0/1.
1212 Out << (Value.getBoolValue() ? '1' : '0');
1213 } else {
1214 if (Value.isNegative())
1215 Out << 'n';
1216 Value.abs().print(Out, false);
1217 }
1218 Out << 'E';
1219
1220}
1221
John McCall1dd73832010-02-04 01:42:13 +00001222void CXXNameMangler::mangleCalledExpression(const Expr *E, unsigned Arity) {
1223 if (E->getType() != getASTContext().OverloadTy)
1224 mangleExpression(E);
John McCall2f27bf82010-02-04 02:56:29 +00001225 // propagate arity to dependent overloads?
John McCall1dd73832010-02-04 01:42:13 +00001226
1227 llvm::PointerIntPair<OverloadExpr*,1> R
1228 = OverloadExpr::find(const_cast<Expr*>(E));
1229 if (R.getInt())
1230 Out << "an"; // &
1231 const OverloadExpr *Ovl = R.getPointer();
John McCall2f27bf82010-02-04 02:56:29 +00001232 if (const UnresolvedMemberExpr *ME = dyn_cast<UnresolvedMemberExpr>(Ovl)) {
1233 mangleMemberExpr(ME->getBase(), ME->isArrow(), ME->getQualifier(),
1234 ME->getMemberName(), Arity);
1235 return;
1236 }
John McCall1dd73832010-02-04 01:42:13 +00001237
1238 mangleUnresolvedName(Ovl->getQualifier(), Ovl->getName(), Arity);
1239}
1240
John McCall2f27bf82010-02-04 02:56:29 +00001241/// Mangles a member expression. Implicit accesses are not handled,
1242/// but that should be okay, because you shouldn't be able to
1243/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001244void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1245 bool IsArrow,
1246 NestedNameSpecifier *Qualifier,
1247 DeclarationName Member,
1248 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001249 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1250 // OTOH, gcc also mangles the name as an expression.
1251 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001252 mangleExpression(Base);
1253 mangleUnresolvedName(Qualifier, Member, Arity);
1254}
1255
Anders Carlssond553f8c2009-09-21 01:21:10 +00001256void CXXNameMangler::mangleExpression(const Expr *E) {
1257 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001258 // ::= <binary operator-name> <expression> <expression>
1259 // ::= <trinary operator-name> <expression> <expression> <expression>
1260 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001261 // ::= cv <type> expression # conversion with one argument
1262 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
John McCall09cc1412010-02-03 00:55:45 +00001263 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001264 // ::= at <type> # alignof (a type)
1265 // ::= <template-param>
1266 // ::= <function-param>
1267 // ::= sr <type> <unqualified-name> # dependent name
1268 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1269 // ::= sZ <template-param> # size of a parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001270 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001271 // <expr-primary> ::= L <type> <value number> E # integer literal
1272 // ::= L <type <value float> E # floating literal
1273 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001274 switch (E->getStmtClass()) {
John McCall09cc1412010-02-03 00:55:45 +00001275 default:
1276 llvm_unreachable("unexpected statement kind");
1277 break;
1278
John McCall1dd73832010-02-04 01:42:13 +00001279 case Expr::CallExprClass: {
1280 const CallExpr *CE = cast<CallExpr>(E);
1281 Out << "cl";
1282 mangleCalledExpression(CE->getCallee(), CE->getNumArgs());
1283 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1284 mangleExpression(CE->getArg(I));
1285 Out << "E";
John McCall09cc1412010-02-03 00:55:45 +00001286 break;
John McCall1dd73832010-02-04 01:42:13 +00001287 }
John McCall09cc1412010-02-03 00:55:45 +00001288
John McCall2f27bf82010-02-04 02:56:29 +00001289 case Expr::MemberExprClass: {
1290 const MemberExpr *ME = cast<MemberExpr>(E);
1291 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1292 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
1293 UnknownArity);
1294 break;
1295 }
1296
1297 case Expr::UnresolvedMemberExprClass: {
1298 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1299 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1300 ME->getQualifier(), ME->getMemberName(),
1301 UnknownArity);
1302 break;
1303 }
1304
1305 case Expr::CXXDependentScopeMemberExprClass: {
1306 const CXXDependentScopeMemberExpr *ME
1307 = cast<CXXDependentScopeMemberExpr>(E);
1308 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1309 ME->getQualifier(), ME->getMember(),
1310 UnknownArity);
1311 break;
1312 }
1313
John McCall1dd73832010-02-04 01:42:13 +00001314 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001315 // The ABI doesn't cover how to mangle overload sets, so we mangle
1316 // using something as close as possible to the original lookup
1317 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001318 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
1319 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), UnknownArity);
1320 break;
1321 }
1322
1323 case Expr::CXXUnresolvedConstructExprClass: {
1324 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1325 unsigned N = CE->arg_size();
1326
1327 Out << "cv";
1328 mangleType(CE->getType());
1329 if (N != 1) Out << "_";
1330 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
1331 if (N != 1) Out << "E";
John McCall09cc1412010-02-03 00:55:45 +00001332 break;
John McCall1dd73832010-02-04 01:42:13 +00001333 }
John McCall09cc1412010-02-03 00:55:45 +00001334
John McCall1dd73832010-02-04 01:42:13 +00001335 case Expr::CXXTemporaryObjectExprClass:
1336 case Expr::CXXConstructExprClass: {
1337 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1338 unsigned N = CE->getNumArgs();
1339
1340 Out << "cv";
1341 mangleType(CE->getType());
1342 if (N != 1) Out << "_";
1343 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
1344 if (N != 1) Out << "E";
John McCall09cc1412010-02-03 00:55:45 +00001345 break;
John McCall1dd73832010-02-04 01:42:13 +00001346 }
1347
1348 case Expr::SizeOfAlignOfExprClass: {
1349 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
1350 if (SAE->isSizeOf()) Out << "s";
1351 else Out << "a";
1352 if (SAE->isArgumentType()) {
1353 Out << "t";
1354 mangleType(SAE->getArgumentType());
1355 } else {
1356 Out << "z";
1357 mangleExpression(SAE->getArgumentExpr());
1358 }
1359 break;
1360 }
Anders Carlssona7694082009-11-06 02:50:19 +00001361
Anders Carlssone170ba72009-12-14 01:45:37 +00001362 case Expr::UnaryOperatorClass: {
1363 const UnaryOperator *UO = cast<UnaryOperator>(E);
1364 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
1365 /*Arity=*/1);
1366 mangleExpression(UO->getSubExpr());
1367 break;
1368 }
1369
1370 case Expr::BinaryOperatorClass: {
1371 const BinaryOperator *BO = cast<BinaryOperator>(E);
1372 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
1373 /*Arity=*/2);
1374 mangleExpression(BO->getLHS());
1375 mangleExpression(BO->getRHS());
1376 break;
John McCall2f27bf82010-02-04 02:56:29 +00001377 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001378
1379 case Expr::ConditionalOperatorClass: {
1380 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1381 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1382 mangleExpression(CO->getCond());
1383 mangleExpression(CO->getLHS());
1384 mangleExpression(CO->getRHS());
1385 break;
1386 }
1387
Douglas Gregor46287c72010-01-29 16:37:09 +00001388 case Expr::ImplicitCastExprClass: {
1389 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr());
1390 break;
1391 }
1392
1393 case Expr::CStyleCastExprClass:
1394 case Expr::CXXStaticCastExprClass:
1395 case Expr::CXXDynamicCastExprClass:
1396 case Expr::CXXReinterpretCastExprClass:
1397 case Expr::CXXConstCastExprClass:
1398 case Expr::CXXFunctionalCastExprClass: {
1399 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1400 Out << "cv";
1401 mangleType(ECE->getType());
1402 mangleExpression(ECE->getSubExpr());
1403 break;
1404 }
1405
Anders Carlsson58040a52009-12-16 05:48:46 +00001406 case Expr::CXXOperatorCallExprClass: {
1407 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1408 unsigned NumArgs = CE->getNumArgs();
1409 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1410 // Mangle the arguments.
1411 for (unsigned i = 0; i != NumArgs; ++i)
1412 mangleExpression(CE->getArg(i));
1413 break;
1414 }
1415
Anders Carlssona7694082009-11-06 02:50:19 +00001416 case Expr::ParenExprClass:
1417 mangleExpression(cast<ParenExpr>(E)->getSubExpr());
1418 break;
1419
Anders Carlssond553f8c2009-09-21 01:21:10 +00001420 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001421 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001422
Anders Carlssond553f8c2009-09-21 01:21:10 +00001423 switch (D->getKind()) {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001424 default:
1425 // <expr-primary> ::= L <mangled-name> E # external name
1426 Out << 'L';
1427 mangle(D, "_Z");
1428 Out << 'E';
1429 break;
1430
Anders Carlssond553f8c2009-09-21 01:21:10 +00001431 case Decl::NonTypeTemplateParm: {
1432 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001433 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00001434 break;
1435 }
1436
1437 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001438
Anders Carlsson50755b02009-09-27 20:11:34 +00001439 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001440 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001441
John McCall865d4472009-11-19 22:55:06 +00001442 case Expr::DependentScopeDeclRefExprClass: {
1443 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001444 NestedNameSpecifier *NNS = DRE->getQualifier();
1445 const Type *QTy = NNS->getAsType();
1446
1447 // When we're dealing with a nested-name-specifier that has just a
1448 // dependent identifier in it, mangle that as a typename. FIXME:
1449 // It isn't clear that we ever actually want to have such a
1450 // nested-name-specifier; why not just represent it as a typename type?
1451 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
1452 QTy = getASTContext().getTypenameType(NNS->getPrefix(),
1453 NNS->getAsIdentifier())
1454 .getTypePtr();
1455 }
Anders Carlsson50755b02009-09-27 20:11:34 +00001456 assert(QTy && "Qualifier was not type!");
1457
1458 // ::= sr <type> <unqualified-name> # dependent name
1459 Out << "sr";
1460 mangleType(QualType(QTy, 0));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001461
Anders Carlsson50755b02009-09-27 20:11:34 +00001462 assert(DRE->getDeclName().getNameKind() == DeclarationName::Identifier &&
1463 "Unhandled decl name kind!");
1464 mangleSourceName(DRE->getDeclName().getAsIdentifierInfo());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001465
Anders Carlsson50755b02009-09-27 20:11:34 +00001466 break;
1467 }
1468
John McCall1dd73832010-02-04 01:42:13 +00001469 case Expr::FloatingLiteralClass: {
1470 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
1471 Out << "L";
1472 mangleType(FL->getType());
1473
1474 // TODO: avoid this copy with careful stream management.
1475 llvm::SmallVector<char,20> Buffer;
1476 FL->getValue().bitcastToAPInt().toString(Buffer, 16, false);
1477 Out.write(Buffer.data(), Buffer.size());
1478
1479 Out << "E";
1480 break;
1481 }
1482
Anders Carlssone170ba72009-12-14 01:45:37 +00001483 case Expr::IntegerLiteralClass:
1484 mangleIntegerLiteral(E->getType(),
1485 llvm::APSInt(cast<IntegerLiteral>(E)->getValue()));
1486 break;
1487
Anders Carlssond553f8c2009-09-21 01:21:10 +00001488 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001489}
1490
John McCallefe6aee2009-09-05 07:56:18 +00001491// FIXME: <type> ::= G <type> # imaginary (C 2000)
1492// FIXME: <type> ::= U <source-name> <type> # vendor extended type qualifier
1493
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001494void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
1495 // <ctor-dtor-name> ::= C1 # complete object constructor
1496 // ::= C2 # base object constructor
1497 // ::= C3 # complete object allocating constructor
1498 //
1499 switch (T) {
1500 case Ctor_Complete:
1501 Out << "C1";
1502 break;
1503 case Ctor_Base:
1504 Out << "C2";
1505 break;
1506 case Ctor_CompleteAllocating:
1507 Out << "C3";
1508 break;
1509 }
1510}
1511
Anders Carlsson27ae5362009-04-17 01:58:57 +00001512void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
1513 // <ctor-dtor-name> ::= D0 # deleting destructor
1514 // ::= D1 # complete object destructor
1515 // ::= D2 # base object destructor
1516 //
1517 switch (T) {
1518 case Dtor_Deleting:
1519 Out << "D0";
1520 break;
1521 case Dtor_Complete:
1522 Out << "D1";
1523 break;
1524 case Dtor_Base:
1525 Out << "D2";
1526 break;
1527 }
1528}
1529
Anders Carlsson9e85c742009-12-23 19:30:55 +00001530void CXXNameMangler::mangleTemplateArgs(const TemplateArgumentList &L) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00001531 // <template-args> ::= I <template-arg>+ E
1532 Out << "I";
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00001533 for (unsigned i = 0, e = L.size(); i != e; ++i)
Anders Carlsson9e85c742009-12-23 19:30:55 +00001534 mangleTemplateArg(L[i]);
Anders Carlsson7a0ba872009-05-15 16:09:15 +00001535 Out << "E";
1536}
1537
Anders Carlsson7624f212009-09-18 02:42:01 +00001538void CXXNameMangler::mangleTemplateArgs(const TemplateArgument *TemplateArgs,
1539 unsigned NumTemplateArgs) {
1540 // <template-args> ::= I <template-arg>+ E
1541 Out << "I";
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00001542 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Anders Carlsson9e85c742009-12-23 19:30:55 +00001543 mangleTemplateArg(TemplateArgs[i]);
Anders Carlsson7624f212009-09-18 02:42:01 +00001544 Out << "E";
1545}
1546
Anders Carlsson9e85c742009-12-23 19:30:55 +00001547void CXXNameMangler::mangleTemplateArg(const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00001548 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00001549 // ::= X <expression> E # expression
1550 // ::= <expr-primary> # simple expressions
1551 // ::= I <template-arg>* E # argument pack
1552 // ::= sp <expression> # pack expansion of (C++0x)
1553 switch (A.getKind()) {
1554 default:
1555 assert(0 && "Unknown template argument kind!");
1556 case TemplateArgument::Type:
1557 mangleType(A.getAsType());
1558 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00001559 case TemplateArgument::Template:
Douglas Gregor2d565b32010-02-06 01:09:36 +00001560 assert(A.getAsTemplate().getAsTemplateDecl() &&
1561 "FIXME: Support dependent template names");
Anders Carlsson9e85c742009-12-23 19:30:55 +00001562 mangleName(A.getAsTemplate().getAsTemplateDecl());
1563 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001564 case TemplateArgument::Expression:
1565 Out << 'X';
1566 mangleExpression(A.getAsExpr());
1567 Out << 'E';
1568 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001569 case TemplateArgument::Integral:
1570 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00001571 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00001572 case TemplateArgument::Declaration: {
1573 // <expr-primary> ::= L <mangled-name> E # external name
1574
1575 // FIXME: Clang produces AST's where pointer-to-member-function expressions
1576 // and pointer-to-function expressions are represented as a declaration not
1577 // an expression; this is not how gcc represents them and this changes the
1578 // mangling.
1579 Out << 'L';
1580 // References to external entities use the mangled name; if the name would
1581 // not normally be manged then mangle it as unqualified.
1582 //
1583 // FIXME: The ABI specifies that external names here should have _Z, but
1584 // gcc leaves this off.
1585 mangle(cast<NamedDecl>(A.getAsDecl()), "Z");
1586 Out << 'E';
1587 break;
1588 }
1589 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00001590}
1591
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001592void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
1593 // <template-param> ::= T_ # first template parameter
1594 // ::= T <parameter-2 non-negative number> _
1595 if (Index == 0)
1596 Out << "T_";
1597 else
1598 Out << 'T' << (Index - 1) << '_';
1599}
1600
Anders Carlsson76967372009-09-17 00:43:46 +00001601// <substitution> ::= S <seq-id> _
1602// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00001603bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00001604 // Try one of the standard substitutions first.
1605 if (mangleStandardSubstitution(ND))
1606 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001607
Anders Carlsson433d1372009-11-07 04:26:04 +00001608 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00001609 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
1610}
1611
Anders Carlsson76967372009-09-17 00:43:46 +00001612bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00001613 if (!T.getCVRQualifiers()) {
1614 if (const RecordType *RT = T->getAs<RecordType>())
1615 return mangleSubstitution(RT->getDecl());
1616 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001617
Anders Carlsson76967372009-09-17 00:43:46 +00001618 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
1619
Anders Carlssond3a932a2009-09-17 03:53:28 +00001620 return mangleSubstitution(TypePtr);
1621}
1622
1623bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001624 llvm::DenseMap<uintptr_t, unsigned>::iterator I =
Anders Carlssond3a932a2009-09-17 03:53:28 +00001625 Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00001626 if (I == Substitutions.end())
1627 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001628
Anders Carlsson76967372009-09-17 00:43:46 +00001629 unsigned SeqID = I->second;
1630 if (SeqID == 0)
1631 Out << "S_";
1632 else {
1633 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001634
Anders Carlsson76967372009-09-17 00:43:46 +00001635 // <seq-id> is encoded in base-36, using digits and upper case letters.
1636 char Buffer[10];
1637 char *BufferPtr = Buffer + 9;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001638
Anders Carlsson76967372009-09-17 00:43:46 +00001639 *BufferPtr = 0;
1640 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001641
Anders Carlsson76967372009-09-17 00:43:46 +00001642 while (SeqID) {
1643 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001644
Anders Carlsson76967372009-09-17 00:43:46 +00001645 unsigned char c = static_cast<unsigned char>(SeqID) % 36;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001646
Anders Carlsson76967372009-09-17 00:43:46 +00001647 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
1648 SeqID /= 36;
1649 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001650
Anders Carlsson76967372009-09-17 00:43:46 +00001651 Out << 'S' << BufferPtr << '_';
1652 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001653
Anders Carlsson76967372009-09-17 00:43:46 +00001654 return true;
1655}
1656
Anders Carlssonf514b542009-09-27 00:12:57 +00001657static bool isCharType(QualType T) {
1658 if (T.isNull())
1659 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001660
Anders Carlssonf514b542009-09-27 00:12:57 +00001661 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
1662 T->isSpecificBuiltinType(BuiltinType::Char_U);
1663}
1664
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001665/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00001666/// specialization of a given name with a single argument of type char.
1667static bool isCharSpecialization(QualType T, const char *Name) {
1668 if (T.isNull())
1669 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001670
Anders Carlssonf514b542009-09-27 00:12:57 +00001671 const RecordType *RT = T->getAs<RecordType>();
1672 if (!RT)
1673 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001674
1675 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00001676 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
1677 if (!SD)
1678 return false;
1679
1680 if (!isStdNamespace(SD->getDeclContext()))
1681 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001682
Anders Carlssonf514b542009-09-27 00:12:57 +00001683 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
1684 if (TemplateArgs.size() != 1)
1685 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001686
Anders Carlssonf514b542009-09-27 00:12:57 +00001687 if (!isCharType(TemplateArgs[0].getAsType()))
1688 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001689
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001690 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00001691}
1692
Anders Carlsson91f88602009-12-07 19:56:42 +00001693template <std::size_t StrLen>
1694bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl *SD,
1695 const char (&Str)[StrLen]) {
1696 if (!SD->getIdentifier()->isStr(Str))
1697 return false;
1698
1699 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
1700 if (TemplateArgs.size() != 2)
1701 return false;
1702
1703 if (!isCharType(TemplateArgs[0].getAsType()))
1704 return false;
1705
1706 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
1707 return false;
1708
1709 return true;
1710}
1711
Anders Carlssone7c8cb62009-09-26 20:53:44 +00001712bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
1713 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00001714 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00001715 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00001716 Out << "St";
1717 return true;
1718 }
1719 }
1720
1721 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
1722 if (!isStdNamespace(TD->getDeclContext()))
1723 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001724
Anders Carlsson8c031552009-09-26 23:10:05 +00001725 // <substitution> ::= Sa # ::std::allocator
1726 if (TD->getIdentifier()->isStr("allocator")) {
1727 Out << "Sa";
1728 return true;
1729 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001730
Anders Carlsson189d59c2009-09-26 23:14:39 +00001731 // <<substitution> ::= Sb # ::std::basic_string
1732 if (TD->getIdentifier()->isStr("basic_string")) {
1733 Out << "Sb";
1734 return true;
1735 }
Anders Carlsson8c031552009-09-26 23:10:05 +00001736 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001737
1738 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00001739 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00001740 if (!isStdNamespace(SD->getDeclContext()))
1741 return false;
1742
Anders Carlssonf514b542009-09-27 00:12:57 +00001743 // <substitution> ::= Ss # ::std::basic_string<char,
1744 // ::std::char_traits<char>,
1745 // ::std::allocator<char> >
1746 if (SD->getIdentifier()->isStr("basic_string")) {
1747 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001748
Anders Carlssonf514b542009-09-27 00:12:57 +00001749 if (TemplateArgs.size() != 3)
1750 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001751
Anders Carlssonf514b542009-09-27 00:12:57 +00001752 if (!isCharType(TemplateArgs[0].getAsType()))
1753 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001754
Anders Carlssonf514b542009-09-27 00:12:57 +00001755 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
1756 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001757
Anders Carlssonf514b542009-09-27 00:12:57 +00001758 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
1759 return false;
1760
1761 Out << "Ss";
1762 return true;
1763 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001764
Anders Carlsson91f88602009-12-07 19:56:42 +00001765 // <substitution> ::= Si # ::std::basic_istream<char,
1766 // ::std::char_traits<char> >
1767 if (isStreamCharSpecialization(SD, "basic_istream")) {
1768 Out << "Si";
1769 return true;
1770 }
1771
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001772 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00001773 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00001774 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00001775 Out << "So";
1776 return true;
1777 }
Anders Carlsson91f88602009-12-07 19:56:42 +00001778
1779 // <substitution> ::= Sd # ::std::basic_iostream<char,
1780 // ::std::char_traits<char> >
1781 if (isStreamCharSpecialization(SD, "basic_iostream")) {
1782 Out << "Sd";
1783 return true;
1784 }
Anders Carlssonf514b542009-09-27 00:12:57 +00001785 }
Anders Carlsson8c031552009-09-26 23:10:05 +00001786 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00001787}
1788
Anders Carlsson76967372009-09-17 00:43:46 +00001789void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00001790 if (!T.getCVRQualifiers()) {
1791 if (const RecordType *RT = T->getAs<RecordType>()) {
1792 addSubstitution(RT->getDecl());
1793 return;
1794 }
1795 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001796
Anders Carlsson76967372009-09-17 00:43:46 +00001797 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00001798 addSubstitution(TypePtr);
1799}
1800
1801void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlsson76967372009-09-17 00:43:46 +00001802 unsigned SeqID = Substitutions.size();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001803
Anders Carlssond3a932a2009-09-17 03:53:28 +00001804 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001805 Substitutions[Ptr] = SeqID;
Anders Carlsson76967372009-09-17 00:43:46 +00001806}
1807
Daniel Dunbar1b077112009-11-21 09:06:10 +00001808//
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Daniel Dunbar1b077112009-11-21 09:06:10 +00001810/// \brief Mangles the name of the declaration D and emits that name to the
1811/// given output stream.
1812///
1813/// If the declaration D requires a mangled name, this routine will emit that
1814/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
1815/// and this routine will return false. In this case, the caller should just
1816/// emit the identifier of the declaration (\c D->getIdentifier()) as its
1817/// name.
Daniel Dunbarf981bf82009-11-21 09:14:52 +00001818void MangleContext::mangleName(const NamedDecl *D,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001819 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00001820 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
1821 "Invalid mangleName() call, argument is not a variable or function!");
1822 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
1823 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001824
Daniel Dunbar1b077112009-11-21 09:06:10 +00001825 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
1826 getASTContext().getSourceManager(),
1827 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001829 CXXNameMangler Mangler(*this, Res);
1830 return 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::mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType 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 Stump1eb44332009-09-09 15:08:12 +00001838
Daniel Dunbar1b077112009-11-21 09:06:10 +00001839void MangleContext::mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001840 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00001841 CXXNameMangler Mangler(*this, Res, D, Type);
1842 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00001843}
Mike Stumpf1216772009-07-31 18:25:34 +00001844
Daniel Dunbarc0747712009-11-21 09:12:13 +00001845/// \brief Mangles the a thunk with the offset n for the declaration D and
1846/// emits that name to the given output stream.
Anders Carlssona94822e2009-11-26 02:32:05 +00001847void MangleContext::mangleThunk(const FunctionDecl *FD,
Anders Carlssonb73a5be2009-11-26 02:49:32 +00001848 const ThunkAdjustment &ThisAdjustment,
Daniel Dunbarc0747712009-11-21 09:12:13 +00001849 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00001850 assert(!isa<CXXDestructorDecl>(FD) &&
1851 "Use mangleCXXDtor for destructor decls!");
1852
1853 // <special-name> ::= T <call-offset> <base encoding>
1854 // # base is the nominal target function of thunk
1855 CXXNameMangler Mangler(*this, Res);
1856 Mangler.getStream() << "_ZT";
Anders Carlssonb73a5be2009-11-26 02:49:32 +00001857 Mangler.mangleCallOffset(ThisAdjustment);
Daniel Dunbarc0747712009-11-21 09:12:13 +00001858 Mangler.mangleFunctionEncoding(FD);
1859}
1860
Eli Friedman61d89b62009-12-03 00:03:05 +00001861void MangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *D,
1862 CXXDtorType Type,
1863 const ThunkAdjustment &ThisAdjustment,
1864 llvm::SmallVectorImpl<char> &Res) {
1865 // <special-name> ::= T <call-offset> <base encoding>
1866 // # base is the nominal target function of thunk
1867 CXXNameMangler Mangler(*this, Res, D, Type);
1868 Mangler.getStream() << "_ZT";
1869 Mangler.mangleCallOffset(ThisAdjustment);
1870 Mangler.mangleFunctionEncoding(D);
1871}
1872
Daniel Dunbarc0747712009-11-21 09:12:13 +00001873/// \brief Mangles the a covariant thunk for the declaration D and emits that
1874/// name to the given output stream.
Anders Carlsson7622cd32009-11-26 03:09:37 +00001875void
1876MangleContext::mangleCovariantThunk(const FunctionDecl *FD,
1877 const CovariantThunkAdjustment& Adjustment,
1878 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00001879 assert(!isa<CXXDestructorDecl>(FD) &&
Eli Friedman61d89b62009-12-03 00:03:05 +00001880 "No such thing as a covariant thunk for a destructor!");
Daniel Dunbarc0747712009-11-21 09:12:13 +00001881
1882 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
1883 // # base is the nominal target function of thunk
1884 // # first call-offset is 'this' adjustment
1885 // # second call-offset is result adjustment
1886 CXXNameMangler Mangler(*this, Res);
1887 Mangler.getStream() << "_ZTc";
Anders Carlsson7622cd32009-11-26 03:09:37 +00001888 Mangler.mangleCallOffset(Adjustment.ThisAdjustment);
1889 Mangler.mangleCallOffset(Adjustment.ReturnAdjustment);
Daniel Dunbarc0747712009-11-21 09:12:13 +00001890 Mangler.mangleFunctionEncoding(FD);
1891}
1892
1893/// mangleGuardVariable - Returns the mangled name for a guard variable
1894/// for the passed in VarDecl.
1895void MangleContext::mangleGuardVariable(const VarDecl *D,
1896 llvm::SmallVectorImpl<char> &Res) {
1897 // <special-name> ::= GV <object name> # Guard variable for one-time
1898 // # initialization
1899 CXXNameMangler Mangler(*this, Res);
1900 Mangler.getStream() << "_ZGV";
1901 Mangler.mangleName(D);
1902}
1903
Daniel Dunbar1b077112009-11-21 09:06:10 +00001904void MangleContext::mangleCXXVtable(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001905 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00001906 // <special-name> ::= TV <type> # virtual table
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001907 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00001908 Mangler.getStream() << "_ZTV";
1909 Mangler.mangleName(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00001910}
Mike Stump82d75b02009-11-10 01:58:37 +00001911
Daniel Dunbar1b077112009-11-21 09:06:10 +00001912void MangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001913 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00001914 // <special-name> ::= TT <type> # VTT structure
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001915 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00001916 Mangler.getStream() << "_ZTT";
1917 Mangler.mangleName(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00001918}
Mike Stumpab3f7e92009-11-10 01:41:59 +00001919
Daniel Dunbar1b077112009-11-21 09:06:10 +00001920void MangleContext::mangleCXXCtorVtable(const CXXRecordDecl *RD, int64_t Offset,
1921 const CXXRecordDecl *Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001922 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00001923 // <special-name> ::= TC <type> <offset number> _ <base type>
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001924 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00001925 Mangler.getStream() << "_ZTC";
1926 Mangler.mangleName(RD);
1927 Mangler.getStream() << Offset;
1928 Mangler.getStream() << "_";
1929 Mangler.mangleName(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00001930}
Mike Stump738f8c22009-07-31 23:15:31 +00001931
Mike Stumpde050572009-12-02 18:57:08 +00001932void MangleContext::mangleCXXRTTI(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001933 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00001934 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00001935 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001936 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00001937 Mangler.getStream() << "_ZTI";
1938 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00001939}
Mike Stump67795982009-11-14 00:14:13 +00001940
Mike Stumpde050572009-12-02 18:57:08 +00001941void MangleContext::mangleCXXRTTIName(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001942 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00001943 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00001944 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00001945 Mangler.getStream() << "_ZTS";
1946 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00001947}