blob: 3d7fc83322dfead15bed8125ddd4121142110117 [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"
Douglas Gregor6ec36682009-02-18 23:53:56 +000023#include "clang/Basic/SourceManager.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000024#include "llvm/Support/Compiler.h"
25#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000026#include "llvm/Support/ErrorHandling.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000027using namespace clang;
28
29namespace {
30 class VISIBILITY_HIDDEN CXXNameMangler {
31 ASTContext &Context;
32 llvm::raw_ostream &Out;
33
Anders Carlsson27ae5362009-04-17 01:58:57 +000034 const CXXMethodDecl *Structor;
35 unsigned StructorType;
Anders Carlsson3ac86b52009-04-15 05:36:58 +000036 CXXCtorType CtorType;
Mike Stump1eb44332009-09-09 15:08:12 +000037
Anders Carlsson76967372009-09-17 00:43:46 +000038 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
39
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000040 public:
41 CXXNameMangler(ASTContext &C, llvm::raw_ostream &os)
Anders Carlsson27ae5362009-04-17 01:58:57 +000042 : Context(C), Out(os), Structor(0), StructorType(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000043
44 bool mangle(const NamedDecl *D);
Mike Stump77ca8f62009-09-05 07:20:32 +000045 void mangleCalloffset(int64_t nv, int64_t v);
Mike Stumpdec025b2009-09-07 04:27:52 +000046 void mangleThunk(const FunctionDecl *FD, int64_t nv, int64_t v);
47 void mangleCovariantThunk(const FunctionDecl *FD,
Mike Stump77ca8f62009-09-05 07:20:32 +000048 int64_t nv_t, int64_t v_t,
Mike Stump9124bcc2009-09-02 00:56:18 +000049 int64_t nv_r, int64_t v_r);
Anders Carlsson41aa8c12009-04-13 18:02:10 +000050 void mangleGuardVariable(const VarDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000051
Mike Stumpf1216772009-07-31 18:25:34 +000052 void mangleCXXVtable(QualType Type);
Mike Stump738f8c22009-07-31 23:15:31 +000053 void mangleCXXRtti(QualType Type);
Anders Carlsson3ac86b52009-04-15 05:36:58 +000054 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type);
Anders Carlsson27ae5362009-04-17 01:58:57 +000055 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type);
Anders Carlsson3ac86b52009-04-15 05:36:58 +000056
Anders Carlsson43f17402009-04-02 15:51:53 +000057 private:
Anders Carlsson76967372009-09-17 00:43:46 +000058 bool mangleSubstitution(QualType T);
59 void addSubstitution(QualType T);
60
Anders Carlsson43f17402009-04-02 15:51:53 +000061 bool mangleFunctionDecl(const FunctionDecl *FD);
Mike Stump1eb44332009-09-09 15:08:12 +000062
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000063 void mangleFunctionEncoding(const FunctionDecl *FD);
64 void mangleName(const NamedDecl *ND);
65 void mangleUnqualifiedName(const NamedDecl *ND);
66 void mangleSourceName(const IdentifierInfo *II);
Anders Carlsson1b42c792009-04-02 16:24:45 +000067 void mangleLocalName(const NamedDecl *ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000068 void mangleNestedName(const NamedDecl *ND);
69 void manglePrefix(const DeclContext *DC);
70 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
71 void mangleCVQualifiers(unsigned Quals);
72 void mangleType(QualType T);
John McCallefe6aee2009-09-05 07:56:18 +000073
74 // Declare manglers for every type class.
75#define ABSTRACT_TYPE(CLASS, PARENT)
76#define NON_CANONICAL_TYPE(CLASS, PARENT)
77#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
78#include "clang/AST/TypeNodes.def"
79
80 void mangleType(const TagType*);
81 void mangleBareFunctionType(const FunctionType *T,
82 bool MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000083 void mangleExpression(Expr *E);
Anders Carlsson3ac86b52009-04-15 05:36:58 +000084 void mangleCXXCtorType(CXXCtorType T);
Anders Carlsson27ae5362009-04-17 01:58:57 +000085 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +000086
Anders Carlsson7a0ba872009-05-15 16:09:15 +000087 void mangleTemplateArgumentList(const TemplateArgumentList &L);
88 void mangleTemplateArgument(const TemplateArgument &A);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000089 };
90}
91
Anders Carlsson43f17402009-04-02 15:51:53 +000092static bool isInCLinkageSpecification(const Decl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +000093 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +000094 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +000095 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +000096 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
97 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Anders Carlsson43f17402009-04-02 15:51:53 +000099 return false;
100}
101
102bool CXXNameMangler::mangleFunctionDecl(const FunctionDecl *FD) {
Mike Stump141c5af2009-09-02 00:25:38 +0000103 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
104 // (always).
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000105 if (!FD->hasAttr<OverloadableAttr>()) {
Chris Lattner783601d2009-06-13 23:34:16 +0000106 // C functions are not mangled, and "main" is never mangled.
Douglas Gregor48a83b52009-09-12 00:17:51 +0000107 if (!Context.getLangOptions().CPlusPlus || FD->isMain())
Chris Lattner783601d2009-06-13 23:34:16 +0000108 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
110 // No mangling in an "implicit extern C" header.
Chris Lattner783601d2009-06-13 23:34:16 +0000111 if (FD->getLocation().isValid() &&
112 Context.getSourceManager().isInExternCSystemHeader(FD->getLocation()))
113 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner783601d2009-06-13 23:34:16 +0000115 // No name mangling in a C linkage specification.
116 if (isInCLinkageSpecification(FD))
117 return false;
118 }
Anders Carlsson43f17402009-04-02 15:51:53 +0000119
120 // If we get here, mangle the decl name!
121 Out << "_Z";
122 mangleFunctionEncoding(FD);
123 return true;
124}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000125
126bool CXXNameMangler::mangle(const NamedDecl *D) {
Mike Stump141c5af2009-09-02 00:25:38 +0000127 // Any decl can be declared with __asm("foo") on it, and this takes precedence
128 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000129 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000130 // If we have an asm name, then we use it as the mangling.
131 Out << '\01'; // LLVM IR Marker for __asm("foo")
132 Out << ALA->getLabel();
133 return true;
134 }
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000136 // <mangled-name> ::= _Z <encoding>
137 // ::= <data name>
138 // ::= <special-name>
139
140 // FIXME: Actually use a visitor to decode these?
Anders Carlsson43f17402009-04-02 15:51:53 +0000141 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
142 return mangleFunctionDecl(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Anders Carlsson329749c2009-04-02 16:05:20 +0000144 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
145 if (!Context.getLangOptions().CPlusPlus ||
Anders Carlsson9ccb0652009-04-11 01:19:45 +0000146 isInCLinkageSpecification(D) ||
147 D->getDeclContext()->isTranslationUnit())
Anders Carlsson329749c2009-04-02 16:05:20 +0000148 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Anders Carlsson329749c2009-04-02 16:05:20 +0000150 Out << "_Z";
151 mangleName(VD);
152 return true;
153 }
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Anders Carlsson43f17402009-04-02 15:51:53 +0000155 return false;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000156}
157
Mike Stump1eb44332009-09-09 15:08:12 +0000158void CXXNameMangler::mangleCXXCtor(const CXXConstructorDecl *D,
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000159 CXXCtorType Type) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000160 assert(!Structor && "Structor already set!");
161 Structor = D;
162 StructorType = Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Anders Carlsson27ae5362009-04-17 01:58:57 +0000164 mangle(D);
165}
166
Mike Stump1eb44332009-09-09 15:08:12 +0000167void CXXNameMangler::mangleCXXDtor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000168 CXXDtorType Type) {
169 assert(!Structor && "Structor already set!");
170 Structor = D;
171 StructorType = Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000173 mangle(D);
174}
175
Mike Stumpf1216772009-07-31 18:25:34 +0000176void CXXNameMangler::mangleCXXVtable(QualType T) {
177 // <special-name> ::= TV <type> # virtual table
178 Out << "_ZTV";
179 mangleType(T);
180}
181
Mike Stump738f8c22009-07-31 23:15:31 +0000182void CXXNameMangler::mangleCXXRtti(QualType T) {
183 // <special-name> ::= TI <type> # typeinfo structure
184 Out << "_ZTI";
185 mangleType(T);
186}
187
Mike Stump1eb44332009-09-09 15:08:12 +0000188void CXXNameMangler::mangleGuardVariable(const VarDecl *D) {
189 // <special-name> ::= GV <object name> # Guard variable for one-time
Mike Stumpf1216772009-07-31 18:25:34 +0000190 // # initialization
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000191
192 Out << "_ZGV";
193 mangleName(D);
194}
195
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000196void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
197 // <encoding> ::= <function name> <bare-function-type>
198 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Mike Stump141c5af2009-09-02 00:25:38 +0000200 // Whether the mangling of a function type includes the return type depends on
201 // the context and the nature of the function. The rules for deciding whether
202 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000203 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000204 // 1. Template functions (names or types) have return types encoded, with
205 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000206 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000207 // e.g. parameters, pointer types, etc., have return type encoded, with the
208 // exceptions listed below.
209 // 3. Non-template function names do not have return types encoded.
210 //
Mike Stump141c5af2009-09-02 00:25:38 +0000211 // The exceptions mentioned in (1) and (2) above, for which the return type is
212 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000213 // 1. Constructors.
214 // 2. Destructors.
215 // 3. Conversion operator functions, e.g. operator int.
216 bool MangleReturnType = false;
217 if (FD->getPrimaryTemplate() &&
218 !(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
219 isa<CXXConversionDecl>(FD)))
220 MangleReturnType = true;
221 mangleBareFunctionType(FD->getType()->getAsFunctionType(), MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000222}
223
224static bool isStdNamespace(const DeclContext *DC) {
225 if (!DC->isNamespace() || !DC->getParent()->isTranslationUnit())
226 return false;
227
228 const NamespaceDecl *NS = cast<NamespaceDecl>(DC);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000229 return NS->getOriginalNamespace()->getIdentifier()->isStr("std");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000230}
231
232void CXXNameMangler::mangleName(const NamedDecl *ND) {
233 // <name> ::= <nested-name>
234 // ::= <unscoped-name>
235 // ::= <unscoped-template-name> <template-args>
236 // ::= <local-name> # See Scope Encoding below
237 //
238 // <unscoped-name> ::= <unqualified-name>
239 // ::= St <unqualified-name> # ::std::
Mike Stump1eb44332009-09-09 15:08:12 +0000240 if (ND->getDeclContext()->isTranslationUnit())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000241 mangleUnqualifiedName(ND);
242 else if (isStdNamespace(ND->getDeclContext())) {
243 Out << "St";
244 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000245 } else if (isa<FunctionDecl>(ND->getDeclContext()))
246 mangleLocalName(ND);
247 else
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000248 mangleNestedName(ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000249}
250
Mike Stump77ca8f62009-09-05 07:20:32 +0000251void CXXNameMangler::mangleCalloffset(int64_t nv, int64_t v) {
Mike Stump141c5af2009-09-02 00:25:38 +0000252 // <call-offset> ::= h <nv-offset> _
253 // ::= v <v-offset> _
254 // <nv-offset> ::= <offset number> # non-virtual base override
255 // <v-offset> ::= <offset nubmer> _ <virtual offset number>
256 // # virtual base override, with vcall offset
Mike Stump77ca8f62009-09-05 07:20:32 +0000257 if (v == 0) {
Mike Stump9124bcc2009-09-02 00:56:18 +0000258 Out << "h";
Mike Stump141c5af2009-09-02 00:25:38 +0000259 if (nv < 0) {
260 Out << "n";
261 nv = -nv;
262 }
263 Out << nv;
264 } else {
Mike Stump9124bcc2009-09-02 00:56:18 +0000265 Out << "v";
Mike Stump141c5af2009-09-02 00:25:38 +0000266 if (nv < 0) {
267 Out << "n";
268 nv = -nv;
269 }
270 Out << nv;
271 Out << "_";
272 if (v < 0) {
273 Out << "n";
274 v = -v;
275 }
276 Out << v;
277 }
278 Out << "_";
Mike Stump9124bcc2009-09-02 00:56:18 +0000279}
280
Mike Stumpdec025b2009-09-07 04:27:52 +0000281void CXXNameMangler::mangleThunk(const FunctionDecl *FD, int64_t nv,
282 int64_t v) {
Mike Stump9124bcc2009-09-02 00:56:18 +0000283 // <special-name> ::= T <call-offset> <base encoding>
284 // # base is the nominal target function of thunk
Mike Stumpdec025b2009-09-07 04:27:52 +0000285 Out << "_ZT";
Mike Stump77ca8f62009-09-05 07:20:32 +0000286 mangleCalloffset(nv, v);
Mike Stumpdec025b2009-09-07 04:27:52 +0000287 mangleFunctionEncoding(FD);
Mike Stump9124bcc2009-09-02 00:56:18 +0000288}
289
Mike Stumpdec025b2009-09-07 04:27:52 +0000290 void CXXNameMangler::mangleCovariantThunk(const FunctionDecl *FD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000291 int64_t nv_t, int64_t v_t,
Mike Stump9124bcc2009-09-02 00:56:18 +0000292 int64_t nv_r, int64_t v_r) {
293 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
294 // # base is the nominal target function of thunk
295 // # first call-offset is 'this' adjustment
296 // # second call-offset is result adjustment
Mike Stumpdec025b2009-09-07 04:27:52 +0000297 Out << "_ZTc";
Mike Stump77ca8f62009-09-05 07:20:32 +0000298 mangleCalloffset(nv_t, v_t);
299 mangleCalloffset(nv_r, v_r);
Mike Stumpdec025b2009-09-07 04:27:52 +0000300 mangleFunctionEncoding(FD);
Mike Stump141c5af2009-09-02 00:25:38 +0000301}
302
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000303void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) {
304 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000305 // ::= <ctor-dtor-name>
306 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000307 DeclarationName Name = ND->getDeclName();
308 switch (Name.getNameKind()) {
309 case DeclarationName::Identifier:
310 mangleSourceName(Name.getAsIdentifierInfo());
311 break;
312
313 case DeclarationName::ObjCZeroArgSelector:
314 case DeclarationName::ObjCOneArgSelector:
315 case DeclarationName::ObjCMultiArgSelector:
316 assert(false && "Can't mangle Objective-C selector names here!");
317 break;
318
319 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000320 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000321 // If the named decl is the C++ constructor we're mangling, use the type
322 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000323 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000324 else
325 // Otherwise, use the complete constructor name. This is relevant if a
326 // class with a constructor is declared within a constructor.
327 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000328 break;
329
330 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000331 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000332 // If the named decl is the C++ destructor we're mangling, use the type we
333 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000334 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
335 else
336 // Otherwise, use the complete destructor name. This is relevant if a
337 // class with a destructor is declared within a destructor.
338 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000339 break;
340
341 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000342 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000343 Out << "cv";
344 mangleType(Context.getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000345 break;
346
347 case DeclarationName::CXXOperatorName:
348 mangleOperatorName(Name.getCXXOverloadedOperator(),
349 cast<FunctionDecl>(ND)->getNumParams());
350 break;
351
352 case DeclarationName::CXXUsingDirective:
353 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000354 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000357 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000358 if (const TemplateArgumentList *TemplateArgs
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000359 = Function->getTemplateSpecializationArgs())
360 mangleTemplateArgumentList(*TemplateArgs);
361 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000362}
363
364void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
365 // <source-name> ::= <positive length number> <identifier>
366 // <number> ::= [n] <non-negative decimal integer>
367 // <identifier> ::= <unqualified source code identifier>
368 Out << II->getLength() << II->getName();
369}
370
371void CXXNameMangler::mangleNestedName(const NamedDecl *ND) {
372 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
373 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
374 // FIXME: no template support
375 Out << 'N';
376 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
377 mangleCVQualifiers(Method->getTypeQualifiers());
378 manglePrefix(ND->getDeclContext());
379 mangleUnqualifiedName(ND);
380 Out << 'E';
381}
382
Anders Carlsson1b42c792009-04-02 16:24:45 +0000383void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
384 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
385 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000386 // <discriminator> := _ <non-negative number>
Anders Carlsson1b42c792009-04-02 16:24:45 +0000387 Out << 'Z';
388 mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext()));
389 Out << 'E';
390 mangleSourceName(ND->getIdentifier());
391}
392
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000393void CXXNameMangler::manglePrefix(const DeclContext *DC) {
394 // <prefix> ::= <prefix> <unqualified-name>
395 // ::= <template-prefix> <template-args>
396 // ::= <template-param>
397 // ::= # empty
398 // ::= <substitution>
399 // FIXME: We only handle mangling of namespaces and classes at the moment.
Anders Carlssonc8dee9c2009-04-01 00:42:16 +0000400 if (!DC->getParent()->isTranslationUnit())
401 manglePrefix(DC->getParent());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000402
403 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(DC))
404 mangleSourceName(Namespace->getIdentifier());
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000405 else if (const RecordDecl *Record = dyn_cast<RecordDecl>(DC)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000406 if (const ClassTemplateSpecializationDecl *D =
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000407 dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
408 mangleType(QualType(D->getTypeForDecl(), 0));
409 } else
410 mangleSourceName(Record->getIdentifier());
411 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000412}
413
Mike Stump1eb44332009-09-09 15:08:12 +0000414void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000415CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
416 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000417 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000418 case OO_New: Out << "nw"; break;
419 // ::= na # new[]
420 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000421 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000422 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000423 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000424 case OO_Array_Delete: Out << "da"; break;
425 // ::= ps # + (unary)
426 // ::= pl # +
427 case OO_Plus: Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000428 // ::= ng # - (unary)
429 // ::= mi # -
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000430 case OO_Minus: Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000431 // ::= ad # & (unary)
432 // ::= an # &
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000433 case OO_Amp: Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000434 // ::= de # * (unary)
435 // ::= ml # *
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000436 case OO_Star: Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000437 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000438 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000439 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000440 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000441 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000442 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000443 // ::= or # |
444 case OO_Pipe: Out << "or"; break;
445 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000446 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000447 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000448 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000449 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000450 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000451 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000452 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000453 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000454 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000455 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000456 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000457 // ::= rM # %=
458 case OO_PercentEqual: Out << "rM"; break;
459 // ::= aN # &=
460 case OO_AmpEqual: Out << "aN"; break;
461 // ::= oR # |=
462 case OO_PipeEqual: Out << "oR"; break;
463 // ::= eO # ^=
464 case OO_CaretEqual: Out << "eO"; break;
465 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000466 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000467 // ::= rs # >>
468 case OO_GreaterGreater: Out << "rs"; break;
469 // ::= lS # <<=
470 case OO_LessLessEqual: Out << "lS"; break;
471 // ::= rS # >>=
472 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000473 // ::= eq # ==
474 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000475 // ::= ne # !=
476 case OO_ExclaimEqual: Out << "ne"; break;
477 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000478 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000479 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000480 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000481 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000482 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000483 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000484 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000485 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000486 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000487 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000488 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000489 // ::= oo # ||
490 case OO_PipePipe: Out << "oo"; break;
491 // ::= pp # ++
492 case OO_PlusPlus: Out << "pp"; break;
493 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000494 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000495 // ::= cm # ,
496 case OO_Comma: Out << "cm"; break;
497 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000498 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000499 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000500 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000501 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000502 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000503 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000504 case OO_Subscript: Out << "ix"; break;
505 // UNSUPPORTED: ::= qu # ?
506
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000507 case OO_None:
508 case OO_Conditional:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000509 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +0000510 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000511 break;
512 }
513}
514
515void CXXNameMangler::mangleCVQualifiers(unsigned Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +0000516 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000517 if (Quals & QualType::Restrict)
518 Out << 'r';
519 if (Quals & QualType::Volatile)
520 Out << 'V';
521 if (Quals & QualType::Const)
522 Out << 'K';
523}
524
525void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000526 // Only operate on the canonical type!
527 T = Context.getCanonicalType(T);
528
Anders Carlsson76967372009-09-17 00:43:46 +0000529 bool IsSubstitutable = !isa<BuiltinType>(T);
530 if (IsSubstitutable && mangleSubstitution(T))
531 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000532
Anders Carlsson76967372009-09-17 00:43:46 +0000533 if (unsigned CVRQualifiers = T.getCVRQualifiers()) {
534 // <type> ::= <CV-qualifiers> <type>
535 mangleCVQualifiers(CVRQualifiers);
536
537 mangleType(T.getUnqualifiedType());
538 } else {
539 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +0000540#define ABSTRACT_TYPE(CLASS, PARENT)
541#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000542 case Type::CLASS: \
543 llvm::llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
544 return;
John McCallefe6aee2009-09-05 07:56:18 +0000545#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000546 case Type::CLASS: \
547 mangleType(static_cast<CLASS##Type*>(T.getTypePtr())); \
548 break;
John McCallefe6aee2009-09-05 07:56:18 +0000549#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +0000550 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000551 }
Anders Carlsson76967372009-09-17 00:43:46 +0000552
553 // Add the substitution.
554 if (IsSubstitutable)
555 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000556}
557
558void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +0000559 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000560 // <builtin-type> ::= v # void
561 // ::= w # wchar_t
562 // ::= b # bool
563 // ::= c # char
564 // ::= a # signed char
565 // ::= h # unsigned char
566 // ::= s # short
567 // ::= t # unsigned short
568 // ::= i # int
569 // ::= j # unsigned int
570 // ::= l # long
571 // ::= m # unsigned long
572 // ::= x # long long, __int64
573 // ::= y # unsigned long long, __int64
574 // ::= n # __int128
575 // UNSUPPORTED: ::= o # unsigned __int128
576 // ::= f # float
577 // ::= d # double
578 // ::= e # long double, __float80
579 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000580 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
581 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
582 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
583 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000584 // ::= Di # char32_t
585 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000586 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000587 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
588 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000589 switch (T->getKind()) {
590 case BuiltinType::Void: Out << 'v'; break;
591 case BuiltinType::Bool: Out << 'b'; break;
592 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
593 case BuiltinType::UChar: Out << 'h'; break;
594 case BuiltinType::UShort: Out << 't'; break;
595 case BuiltinType::UInt: Out << 'j'; break;
596 case BuiltinType::ULong: Out << 'm'; break;
597 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000598 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000599 case BuiltinType::SChar: Out << 'a'; break;
600 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000601 case BuiltinType::Char16: Out << "Ds"; break;
602 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000603 case BuiltinType::Short: Out << 's'; break;
604 case BuiltinType::Int: Out << 'i'; break;
605 case BuiltinType::Long: Out << 'l'; break;
606 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000607 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000608 case BuiltinType::Float: Out << 'f'; break;
609 case BuiltinType::Double: Out << 'd'; break;
610 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000611 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000612
613 case BuiltinType::Overload:
614 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +0000615 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000616 "Overloaded and dependent types shouldn't get to name mangling");
617 break;
Anders Carlssone89d1592009-06-26 18:41:36 +0000618 case BuiltinType::UndeducedAuto:
619 assert(0 && "Should not see undeduced auto here");
620 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +0000621 case BuiltinType::ObjCId: Out << "11objc_object"; break;
622 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000623 }
624}
625
John McCallefe6aee2009-09-05 07:56:18 +0000626// <type> ::= <function-type>
627// <function-type> ::= F [Y] <bare-function-type> E
628void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000629 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +0000630 // FIXME: We don't have enough information in the AST to produce the 'Y'
631 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000632 mangleBareFunctionType(T, /*MangleReturnType=*/true);
633 Out << 'E';
634}
John McCallefe6aee2009-09-05 07:56:18 +0000635void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
636 llvm::llvm_unreachable("Can't mangle K&R function prototypes");
637}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000638void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
639 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +0000640 // We should never be mangling something without a prototype.
641 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
642
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000643 // <bare-function-type> ::= <signature type>+
644 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +0000645 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000646
Anders Carlssonc6c91bc2009-04-01 00:15:23 +0000647 if (Proto->getNumArgs() == 0) {
648 Out << 'v';
649 return;
650 }
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Douglas Gregor72564e72009-02-26 23:50:07 +0000652 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000653 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000654 Arg != ArgEnd; ++Arg)
655 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +0000656
657 // <builtin-type> ::= z # ellipsis
658 if (Proto->isVariadic())
659 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000660}
661
John McCallefe6aee2009-09-05 07:56:18 +0000662// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +0000663// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +0000664void CXXNameMangler::mangleType(const EnumType *T) {
665 mangleType(static_cast<const TagType*>(T));
666}
667void CXXNameMangler::mangleType(const RecordType *T) {
668 mangleType(static_cast<const TagType*>(T));
669}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000670void CXXNameMangler::mangleType(const TagType *T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000671 if (!T->getDecl()->getIdentifier())
672 mangleName(T->getDecl()->getTypedefForAnonDecl());
673 else
674 mangleName(T->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Mike Stump141c5af2009-09-02 00:25:38 +0000676 // If this is a class template specialization, mangle the template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000677 if (ClassTemplateSpecializationDecl *Spec
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000678 = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl()))
679 mangleTemplateArgumentList(Spec->getTemplateArgs());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000680}
681
John McCallefe6aee2009-09-05 07:56:18 +0000682// <type> ::= <array-type>
683// <array-type> ::= A <positive dimension number> _ <element type>
684// ::= A [<dimension expression>] _ <element type>
685void CXXNameMangler::mangleType(const ConstantArrayType *T) {
686 Out << 'A' << T->getSize() << '_';
687 mangleType(T->getElementType());
688}
689void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000690 Out << 'A';
John McCallefe6aee2009-09-05 07:56:18 +0000691 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000692 Out << '_';
693 mangleType(T->getElementType());
694}
John McCallefe6aee2009-09-05 07:56:18 +0000695void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
696 Out << 'A';
697 mangleExpression(T->getSizeExpr());
698 Out << '_';
699 mangleType(T->getElementType());
700}
701void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
702 Out << 'A' << '_';
703 mangleType(T->getElementType());
704}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000705
John McCallefe6aee2009-09-05 07:56:18 +0000706// <type> ::= <pointer-to-member-type>
707// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000708void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000709 Out << 'M';
710 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +0000711 QualType PointeeType = T->getPointeeType();
712 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
713 mangleCVQualifiers(FPT->getTypeQuals());
714 mangleType(FPT);
Mike Stump1eb44332009-09-09 15:08:12 +0000715 } else
Anders Carlsson0e650012009-05-17 17:41:20 +0000716 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000717}
718
John McCallefe6aee2009-09-05 07:56:18 +0000719// <type> ::= <template-param>
720// <template-param> ::= T_ # first template parameter
721// ::= T <parameter-2 non-negative number> _
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000722void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000723 if (T->getIndex() == 0)
724 Out << "T_";
725 else
726 Out << 'T' << (T->getIndex() - 1) << '_';
727}
728
John McCallefe6aee2009-09-05 07:56:18 +0000729// FIXME: <type> ::= <template-template-param> <template-args>
730// FIXME: <type> ::= <substitution> # See Compression below
731
732// <type> ::= P <type> # pointer-to
733void CXXNameMangler::mangleType(const PointerType *T) {
734 Out << 'P';
735 mangleType(T->getPointeeType());
736}
737void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
738 Out << 'P';
739 mangleType(T->getPointeeType());
740}
741
742// <type> ::= R <type> # reference-to
743void CXXNameMangler::mangleType(const LValueReferenceType *T) {
744 Out << 'R';
745 mangleType(T->getPointeeType());
746}
747
748// <type> ::= O <type> # rvalue reference-to (C++0x)
749void CXXNameMangler::mangleType(const RValueReferenceType *T) {
750 Out << 'O';
751 mangleType(T->getPointeeType());
752}
753
754// <type> ::= C <type> # complex pair (C 2000)
755void CXXNameMangler::mangleType(const ComplexType *T) {
756 Out << 'C';
757 mangleType(T->getElementType());
758}
759
760// GNU extension: vector types
761void CXXNameMangler::mangleType(const VectorType *T) {
762 Out << "U8__vector";
763 mangleType(T->getElementType());
764}
765void CXXNameMangler::mangleType(const ExtVectorType *T) {
766 mangleType(static_cast<const VectorType*>(T));
767}
768void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
769 Out << "U8__vector";
770 mangleType(T->getElementType());
771}
772
Anders Carlssona40c5e42009-03-07 22:03:21 +0000773void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
774 mangleSourceName(T->getDecl()->getIdentifier());
775}
776
John McCallefe6aee2009-09-05 07:56:18 +0000777void CXXNameMangler::mangleType(const BlockPointerType *T) {
778 assert(false && "can't mangle block pointer types yet");
779}
780
781void CXXNameMangler::mangleType(const FixedWidthIntType *T) {
782 assert(false && "can't mangle arbitary-precision integer type yet");
783}
784
785void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
786 // TSTs are never canonical unless they're dependent.
787 assert(false && "can't mangle dependent template specializations yet");
788}
789
790void CXXNameMangler::mangleType(const TypenameType *T) {
791 assert(false && "can't mangle dependent typenames yet");
792}
793
794// FIXME: For now, just drop all extension qualifiers on the floor.
795void CXXNameMangler::mangleType(const ExtQualType *T) {
796 mangleType(QualType(T->getBaseType(), 0));
797}
798
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000799void CXXNameMangler::mangleExpression(Expr *E) {
800 assert(false && "Cannot mangle expressions yet");
801}
802
John McCallefe6aee2009-09-05 07:56:18 +0000803// FIXME: <type> ::= G <type> # imaginary (C 2000)
804// FIXME: <type> ::= U <source-name> <type> # vendor extended type qualifier
805
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000806void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
807 // <ctor-dtor-name> ::= C1 # complete object constructor
808 // ::= C2 # base object constructor
809 // ::= C3 # complete object allocating constructor
810 //
811 switch (T) {
812 case Ctor_Complete:
813 Out << "C1";
814 break;
815 case Ctor_Base:
816 Out << "C2";
817 break;
818 case Ctor_CompleteAllocating:
819 Out << "C3";
820 break;
821 }
822}
823
Anders Carlsson27ae5362009-04-17 01:58:57 +0000824void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
825 // <ctor-dtor-name> ::= D0 # deleting destructor
826 // ::= D1 # complete object destructor
827 // ::= D2 # base object destructor
828 //
829 switch (T) {
830 case Dtor_Deleting:
831 Out << "D0";
832 break;
833 case Dtor_Complete:
834 Out << "D1";
835 break;
836 case Dtor_Base:
837 Out << "D2";
838 break;
839 }
840}
841
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000842void CXXNameMangler::mangleTemplateArgumentList(const TemplateArgumentList &L) {
843 // <template-args> ::= I <template-arg>+ E
844 Out << "I";
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000846 for (unsigned i = 0, e = L.size(); i != e; ++i) {
847 const TemplateArgument &A = L[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000849 mangleTemplateArgument(A);
850 }
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000852 Out << "E";
853}
854
855void CXXNameMangler::mangleTemplateArgument(const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +0000856 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000857 // ::= X <expression> E # expression
858 // ::= <expr-primary> # simple expressions
859 // ::= I <template-arg>* E # argument pack
860 // ::= sp <expression> # pack expansion of (C++0x)
861 switch (A.getKind()) {
862 default:
863 assert(0 && "Unknown template argument kind!");
864 case TemplateArgument::Type:
865 mangleType(A.getAsType());
866 break;
867 case TemplateArgument::Integral:
868 // <expr-primary> ::= L <type> <value number> E # integer literal
869
870 Out << 'L';
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000872 mangleType(A.getIntegralType());
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000874 const llvm::APSInt *Integral = A.getAsIntegral();
875 if (A.getIntegralType()->isBooleanType()) {
876 // Boolean values are encoded as 0/1.
877 Out << (Integral->getBoolValue() ? '1' : '0');
878 } else {
879 if (Integral->isNegative())
880 Out << 'n';
881 Integral->abs().print(Out, false);
882 }
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000884 Out << 'E';
885 break;
886 }
887}
888
Anders Carlsson76967372009-09-17 00:43:46 +0000889// <substitution> ::= S <seq-id> _
890// ::= S_
891bool CXXNameMangler::mangleSubstitution(QualType T) {
892 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
893
894 llvm::DenseMap<uintptr_t, unsigned>::iterator I =
895 Substitutions.find(TypePtr);
896 if (I == Substitutions.end())
897 return false;
898
899 unsigned SeqID = I->second;
900 if (SeqID == 0)
901 Out << "S_";
902 else {
903 SeqID--;
904
905 // <seq-id> is encoded in base-36, using digits and upper case letters.
906 char Buffer[10];
907 char *BufferPtr = Buffer + 9;
908
909 *BufferPtr = 0;
910 if (SeqID == 0) *--BufferPtr = '0';
911
912 while (SeqID) {
913 assert(BufferPtr > Buffer && "Buffer overflow!");
914
915 unsigned char c = static_cast<unsigned char>(SeqID) % 36;
916
917 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
918 SeqID /= 36;
919 }
920
921 Out << 'S' << BufferPtr << '_';
922 }
923
924 return true;
925}
926
927void CXXNameMangler::addSubstitution(QualType T) {
928 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
929 unsigned SeqID = Substitutions.size();
930
931 assert(!Substitutions.count(TypePtr) && "Substitution already exists!");
932 Substitutions[TypePtr] = SeqID;
933}
934
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000935namespace clang {
Mike Stump141c5af2009-09-02 00:25:38 +0000936 /// \brief Mangles the name of the declaration D and emits that name to the
937 /// given output stream.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000938 ///
Mike Stump141c5af2009-09-02 00:25:38 +0000939 /// If the declaration D requires a mangled name, this routine will emit that
940 /// mangled name to \p os and return true. Otherwise, \p os will be unchanged
941 /// and this routine will return false. In this case, the caller should just
942 /// emit the identifier of the declaration (\c D->getIdentifier()) as its
943 /// name.
Mike Stump1eb44332009-09-09 15:08:12 +0000944 bool mangleName(const NamedDecl *D, ASTContext &Context,
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000945 llvm::raw_ostream &os) {
Anders Carlsson578aa642009-05-03 16:51:04 +0000946 assert(!isa<CXXConstructorDecl>(D) &&
947 "Use mangleCXXCtor for constructor decls!");
948 assert(!isa<CXXDestructorDecl>(D) &&
949 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000951 CXXNameMangler Mangler(Context, os);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000952 if (!Mangler.mangle(D))
953 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Douglas Gregor6ec36682009-02-18 23:53:56 +0000955 os.flush();
956 return true;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000957 }
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Mike Stump141c5af2009-09-02 00:25:38 +0000959 /// \brief Mangles the a thunk with the offset n for the declaration D and
960 /// emits that name to the given output stream.
Mike Stumpdec025b2009-09-07 04:27:52 +0000961 void mangleThunk(const FunctionDecl *FD, int64_t nv, int64_t v,
Mike Stump883f1272009-09-02 00:28:47 +0000962 ASTContext &Context, llvm::raw_ostream &os) {
Mike Stump141c5af2009-09-02 00:25:38 +0000963 // FIXME: Hum, we might have to thunk these, fix.
Mike Stumpdec025b2009-09-07 04:27:52 +0000964 assert(!isa<CXXDestructorDecl>(FD) &&
Mike Stump141c5af2009-09-02 00:25:38 +0000965 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Mike Stump141c5af2009-09-02 00:25:38 +0000967 CXXNameMangler Mangler(Context, os);
Mike Stumpdec025b2009-09-07 04:27:52 +0000968 Mangler.mangleThunk(FD, nv, v);
Mike Stump141c5af2009-09-02 00:25:38 +0000969 os.flush();
970 }
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Mike Stump9124bcc2009-09-02 00:56:18 +0000972 /// \brief Mangles the a covariant thunk for the declaration D and emits that
973 /// name to the given output stream.
Mike Stumpdec025b2009-09-07 04:27:52 +0000974 void mangleCovariantThunk(const FunctionDecl *FD, int64_t nv_t, int64_t v_t,
Mike Stump77ca8f62009-09-05 07:20:32 +0000975 int64_t nv_r, int64_t v_r, ASTContext &Context,
Mike Stump9124bcc2009-09-02 00:56:18 +0000976 llvm::raw_ostream &os) {
977 // FIXME: Hum, we might have to thunk these, fix.
Mike Stumpdec025b2009-09-07 04:27:52 +0000978 assert(!isa<CXXDestructorDecl>(FD) &&
Mike Stump9124bcc2009-09-02 00:56:18 +0000979 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Mike Stump9124bcc2009-09-02 00:56:18 +0000981 CXXNameMangler Mangler(Context, os);
Mike Stumpdec025b2009-09-07 04:27:52 +0000982 Mangler.mangleCovariantThunk(FD, nv_t, v_t, nv_r, v_r);
Mike Stump9124bcc2009-09-02 00:56:18 +0000983 os.flush();
984 }
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000986 /// mangleGuardVariable - Returns the mangled name for a guard variable
987 /// for the passed in VarDecl.
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000988 void mangleGuardVariable(const VarDecl *D, ASTContext &Context,
989 llvm::raw_ostream &os) {
990 CXXNameMangler Mangler(Context, os);
991 Mangler.mangleGuardVariable(D);
992
993 os.flush();
994 }
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000996 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
997 ASTContext &Context, llvm::raw_ostream &os) {
998 CXXNameMangler Mangler(Context, os);
999 Mangler.mangleCXXCtor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001001 os.flush();
1002 }
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Anders Carlsson27ae5362009-04-17 01:58:57 +00001004 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
1005 ASTContext &Context, llvm::raw_ostream &os) {
1006 CXXNameMangler Mangler(Context, os);
1007 Mangler.mangleCXXDtor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Anders Carlsson27ae5362009-04-17 01:58:57 +00001009 os.flush();
1010 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001011
Mike Stumpf1216772009-07-31 18:25:34 +00001012 void mangleCXXVtable(QualType Type, ASTContext &Context,
1013 llvm::raw_ostream &os) {
1014 CXXNameMangler Mangler(Context, os);
1015 Mangler.mangleCXXVtable(Type);
1016
1017 os.flush();
1018 }
Mike Stump738f8c22009-07-31 23:15:31 +00001019
1020 void mangleCXXRtti(QualType Type, ASTContext &Context,
1021 llvm::raw_ostream &os) {
1022 CXXNameMangler Mangler(Context, os);
1023 Mangler.mangleCXXRtti(Type);
1024
1025 os.flush();
1026 }
Mike Stumpf1216772009-07-31 18:25:34 +00001027}