blob: 21398677dc6f0a468c99c3e7d95ca3c6bf0d60e2 [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);
Anders Carlsson201ce742009-09-17 03:17:01 +000066 void mangleUnscopedName(const NamedDecl *ND);
67 void mangleUnscopedTemplateName(const FunctionDecl *ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000068 void mangleSourceName(const IdentifierInfo *II);
Anders Carlsson1b42c792009-04-02 16:24:45 +000069 void mangleLocalName(const NamedDecl *ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000070 void mangleNestedName(const NamedDecl *ND);
71 void manglePrefix(const DeclContext *DC);
72 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
73 void mangleCVQualifiers(unsigned Quals);
74 void mangleType(QualType T);
John McCallefe6aee2009-09-05 07:56:18 +000075
76 // Declare manglers for every type class.
77#define ABSTRACT_TYPE(CLASS, PARENT)
78#define NON_CANONICAL_TYPE(CLASS, PARENT)
79#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
80#include "clang/AST/TypeNodes.def"
81
82 void mangleType(const TagType*);
83 void mangleBareFunctionType(const FunctionType *T,
84 bool MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000085 void mangleExpression(Expr *E);
Anders Carlsson3ac86b52009-04-15 05:36:58 +000086 void mangleCXXCtorType(CXXCtorType T);
Anders Carlsson27ae5362009-04-17 01:58:57 +000087 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +000088
Anders Carlsson7a0ba872009-05-15 16:09:15 +000089 void mangleTemplateArgumentList(const TemplateArgumentList &L);
90 void mangleTemplateArgument(const TemplateArgument &A);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000091 };
92}
93
Anders Carlsson43f17402009-04-02 15:51:53 +000094static bool isInCLinkageSpecification(const Decl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +000095 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +000096 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +000097 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +000098 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
99 }
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Anders Carlsson43f17402009-04-02 15:51:53 +0000101 return false;
102}
103
104bool CXXNameMangler::mangleFunctionDecl(const FunctionDecl *FD) {
Mike Stump141c5af2009-09-02 00:25:38 +0000105 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
106 // (always).
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000107 if (!FD->hasAttr<OverloadableAttr>()) {
Chris Lattner783601d2009-06-13 23:34:16 +0000108 // C functions are not mangled, and "main" is never mangled.
Douglas Gregor48a83b52009-09-12 00:17:51 +0000109 if (!Context.getLangOptions().CPlusPlus || FD->isMain())
Chris Lattner783601d2009-06-13 23:34:16 +0000110 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000111
112 // No mangling in an "implicit extern C" header.
Chris Lattner783601d2009-06-13 23:34:16 +0000113 if (FD->getLocation().isValid() &&
114 Context.getSourceManager().isInExternCSystemHeader(FD->getLocation()))
115 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chris Lattner783601d2009-06-13 23:34:16 +0000117 // No name mangling in a C linkage specification.
118 if (isInCLinkageSpecification(FD))
119 return false;
120 }
Anders Carlsson43f17402009-04-02 15:51:53 +0000121
122 // If we get here, mangle the decl name!
123 Out << "_Z";
124 mangleFunctionEncoding(FD);
125 return true;
126}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000127
128bool CXXNameMangler::mangle(const NamedDecl *D) {
Mike Stump141c5af2009-09-02 00:25:38 +0000129 // Any decl can be declared with __asm("foo") on it, and this takes precedence
130 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000131 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000132 // If we have an asm name, then we use it as the mangling.
133 Out << '\01'; // LLVM IR Marker for __asm("foo")
134 Out << ALA->getLabel();
135 return true;
136 }
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000138 // <mangled-name> ::= _Z <encoding>
139 // ::= <data name>
140 // ::= <special-name>
141
142 // FIXME: Actually use a visitor to decode these?
Anders Carlsson43f17402009-04-02 15:51:53 +0000143 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
144 return mangleFunctionDecl(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Anders Carlsson329749c2009-04-02 16:05:20 +0000146 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
147 if (!Context.getLangOptions().CPlusPlus ||
Anders Carlsson9ccb0652009-04-11 01:19:45 +0000148 isInCLinkageSpecification(D) ||
149 D->getDeclContext()->isTranslationUnit())
Anders Carlsson329749c2009-04-02 16:05:20 +0000150 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Anders Carlsson329749c2009-04-02 16:05:20 +0000152 Out << "_Z";
153 mangleName(VD);
154 return true;
155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Anders Carlsson43f17402009-04-02 15:51:53 +0000157 return false;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000158}
159
Mike Stump1eb44332009-09-09 15:08:12 +0000160void CXXNameMangler::mangleCXXCtor(const CXXConstructorDecl *D,
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000161 CXXCtorType Type) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000162 assert(!Structor && "Structor already set!");
163 Structor = D;
164 StructorType = Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Anders Carlsson27ae5362009-04-17 01:58:57 +0000166 mangle(D);
167}
168
Mike Stump1eb44332009-09-09 15:08:12 +0000169void CXXNameMangler::mangleCXXDtor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000170 CXXDtorType Type) {
171 assert(!Structor && "Structor already set!");
172 Structor = D;
173 StructorType = Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000175 mangle(D);
176}
177
Mike Stumpf1216772009-07-31 18:25:34 +0000178void CXXNameMangler::mangleCXXVtable(QualType T) {
179 // <special-name> ::= TV <type> # virtual table
180 Out << "_ZTV";
181 mangleType(T);
182}
183
Mike Stump738f8c22009-07-31 23:15:31 +0000184void CXXNameMangler::mangleCXXRtti(QualType T) {
185 // <special-name> ::= TI <type> # typeinfo structure
186 Out << "_ZTI";
187 mangleType(T);
188}
189
Mike Stump1eb44332009-09-09 15:08:12 +0000190void CXXNameMangler::mangleGuardVariable(const VarDecl *D) {
191 // <special-name> ::= GV <object name> # Guard variable for one-time
Mike Stumpf1216772009-07-31 18:25:34 +0000192 // # initialization
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000193
194 Out << "_ZGV";
195 mangleName(D);
196}
197
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000198void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
199 // <encoding> ::= <function name> <bare-function-type>
200 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Mike Stump141c5af2009-09-02 00:25:38 +0000202 // Whether the mangling of a function type includes the return type depends on
203 // the context and the nature of the function. The rules for deciding whether
204 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000205 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000206 // 1. Template functions (names or types) have return types encoded, with
207 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000208 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000209 // e.g. parameters, pointer types, etc., have return type encoded, with the
210 // exceptions listed below.
211 // 3. Non-template function names do not have return types encoded.
212 //
Mike Stump141c5af2009-09-02 00:25:38 +0000213 // The exceptions mentioned in (1) and (2) above, for which the return type is
214 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000215 // 1. Constructors.
216 // 2. Destructors.
217 // 3. Conversion operator functions, e.g. operator int.
218 bool MangleReturnType = false;
219 if (FD->getPrimaryTemplate() &&
220 !(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
221 isa<CXXConversionDecl>(FD)))
222 MangleReturnType = true;
223 mangleBareFunctionType(FD->getType()->getAsFunctionType(), MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000224}
225
226static bool isStdNamespace(const DeclContext *DC) {
227 if (!DC->isNamespace() || !DC->getParent()->isTranslationUnit())
228 return false;
229
230 const NamespaceDecl *NS = cast<NamespaceDecl>(DC);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000231 return NS->getOriginalNamespace()->getIdentifier()->isStr("std");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000232}
233
234void CXXNameMangler::mangleName(const NamedDecl *ND) {
235 // <name> ::= <nested-name>
236 // ::= <unscoped-name>
237 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000238 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000239 //
Anders Carlsson201ce742009-09-17 03:17:01 +0000240 if (ND->getDeclContext()->isTranslationUnit() ||
241 isStdNamespace(ND->getDeclContext())) {
242 const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
243 if (FD && FD->getPrimaryTemplate())
244 mangleUnscopedTemplateName(FD);
245 else
246 mangleUnscopedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000247 } else if (isa<FunctionDecl>(ND->getDeclContext()))
248 mangleLocalName(ND);
249 else
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000250 mangleNestedName(ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000251}
252
Anders Carlsson201ce742009-09-17 03:17:01 +0000253void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
254 // <unscoped-name> ::= <unqualified-name>
255 // ::= St <unqualified-name> # ::std::
256 if (isStdNamespace(ND->getDeclContext()))
257 Out << "St";
258
259 mangleUnqualifiedName(ND);
260}
261
262void CXXNameMangler::mangleUnscopedTemplateName(const FunctionDecl *FD) {
263 // <unscoped-template-name> ::= <unscoped-name>
264 // ::= <substitution>
265 mangleUnscopedName(FD);
266}
267
Mike Stump77ca8f62009-09-05 07:20:32 +0000268void CXXNameMangler::mangleCalloffset(int64_t nv, int64_t v) {
Mike Stump141c5af2009-09-02 00:25:38 +0000269 // <call-offset> ::= h <nv-offset> _
270 // ::= v <v-offset> _
271 // <nv-offset> ::= <offset number> # non-virtual base override
272 // <v-offset> ::= <offset nubmer> _ <virtual offset number>
273 // # virtual base override, with vcall offset
Mike Stump77ca8f62009-09-05 07:20:32 +0000274 if (v == 0) {
Mike Stump9124bcc2009-09-02 00:56:18 +0000275 Out << "h";
Mike Stump141c5af2009-09-02 00:25:38 +0000276 if (nv < 0) {
277 Out << "n";
278 nv = -nv;
279 }
280 Out << nv;
281 } else {
Mike Stump9124bcc2009-09-02 00:56:18 +0000282 Out << "v";
Mike Stump141c5af2009-09-02 00:25:38 +0000283 if (nv < 0) {
284 Out << "n";
285 nv = -nv;
286 }
287 Out << nv;
288 Out << "_";
289 if (v < 0) {
290 Out << "n";
291 v = -v;
292 }
293 Out << v;
294 }
295 Out << "_";
Mike Stump9124bcc2009-09-02 00:56:18 +0000296}
297
Mike Stumpdec025b2009-09-07 04:27:52 +0000298void CXXNameMangler::mangleThunk(const FunctionDecl *FD, int64_t nv,
299 int64_t v) {
Mike Stump9124bcc2009-09-02 00:56:18 +0000300 // <special-name> ::= T <call-offset> <base encoding>
301 // # base is the nominal target function of thunk
Mike Stumpdec025b2009-09-07 04:27:52 +0000302 Out << "_ZT";
Mike Stump77ca8f62009-09-05 07:20:32 +0000303 mangleCalloffset(nv, v);
Mike Stumpdec025b2009-09-07 04:27:52 +0000304 mangleFunctionEncoding(FD);
Mike Stump9124bcc2009-09-02 00:56:18 +0000305}
306
Mike Stumpdec025b2009-09-07 04:27:52 +0000307 void CXXNameMangler::mangleCovariantThunk(const FunctionDecl *FD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000308 int64_t nv_t, int64_t v_t,
Mike Stump9124bcc2009-09-02 00:56:18 +0000309 int64_t nv_r, int64_t v_r) {
310 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
311 // # base is the nominal target function of thunk
312 // # first call-offset is 'this' adjustment
313 // # second call-offset is result adjustment
Mike Stumpdec025b2009-09-07 04:27:52 +0000314 Out << "_ZTc";
Mike Stump77ca8f62009-09-05 07:20:32 +0000315 mangleCalloffset(nv_t, v_t);
316 mangleCalloffset(nv_r, v_r);
Mike Stumpdec025b2009-09-07 04:27:52 +0000317 mangleFunctionEncoding(FD);
Mike Stump141c5af2009-09-02 00:25:38 +0000318}
319
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000320void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) {
321 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000322 // ::= <ctor-dtor-name>
323 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000324 DeclarationName Name = ND->getDeclName();
325 switch (Name.getNameKind()) {
326 case DeclarationName::Identifier:
327 mangleSourceName(Name.getAsIdentifierInfo());
328 break;
329
330 case DeclarationName::ObjCZeroArgSelector:
331 case DeclarationName::ObjCOneArgSelector:
332 case DeclarationName::ObjCMultiArgSelector:
333 assert(false && "Can't mangle Objective-C selector names here!");
334 break;
335
336 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000337 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000338 // If the named decl is the C++ constructor we're mangling, use the type
339 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000340 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000341 else
342 // Otherwise, use the complete constructor name. This is relevant if a
343 // class with a constructor is declared within a constructor.
344 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000345 break;
346
347 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000348 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000349 // If the named decl is the C++ destructor we're mangling, use the type we
350 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000351 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
352 else
353 // Otherwise, use the complete destructor name. This is relevant if a
354 // class with a destructor is declared within a destructor.
355 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000356 break;
357
358 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000359 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000360 Out << "cv";
361 mangleType(Context.getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000362 break;
363
364 case DeclarationName::CXXOperatorName:
365 mangleOperatorName(Name.getCXXOverloadedOperator(),
366 cast<FunctionDecl>(ND)->getNumParams());
367 break;
368
369 case DeclarationName::CXXUsingDirective:
370 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000371 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000372 }
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000374 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000375 if (const TemplateArgumentList *TemplateArgs
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000376 = Function->getTemplateSpecializationArgs())
377 mangleTemplateArgumentList(*TemplateArgs);
378 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000379}
380
381void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
382 // <source-name> ::= <positive length number> <identifier>
383 // <number> ::= [n] <non-negative decimal integer>
384 // <identifier> ::= <unqualified source code identifier>
385 Out << II->getLength() << II->getName();
386}
387
388void CXXNameMangler::mangleNestedName(const NamedDecl *ND) {
389 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
390 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
391 // FIXME: no template support
392 Out << 'N';
393 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
394 mangleCVQualifiers(Method->getTypeQualifiers());
395 manglePrefix(ND->getDeclContext());
396 mangleUnqualifiedName(ND);
397 Out << 'E';
398}
399
Anders Carlsson1b42c792009-04-02 16:24:45 +0000400void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
401 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
402 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000403 // <discriminator> := _ <non-negative number>
Anders Carlsson1b42c792009-04-02 16:24:45 +0000404 Out << 'Z';
405 mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext()));
406 Out << 'E';
407 mangleSourceName(ND->getIdentifier());
408}
409
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000410void CXXNameMangler::manglePrefix(const DeclContext *DC) {
411 // <prefix> ::= <prefix> <unqualified-name>
412 // ::= <template-prefix> <template-args>
413 // ::= <template-param>
414 // ::= # empty
415 // ::= <substitution>
416 // FIXME: We only handle mangling of namespaces and classes at the moment.
Anders Carlssonc8dee9c2009-04-01 00:42:16 +0000417 if (!DC->getParent()->isTranslationUnit())
418 manglePrefix(DC->getParent());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000419
420 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(DC))
421 mangleSourceName(Namespace->getIdentifier());
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000422 else if (const RecordDecl *Record = dyn_cast<RecordDecl>(DC)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000423 if (const ClassTemplateSpecializationDecl *D =
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000424 dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
425 mangleType(QualType(D->getTypeForDecl(), 0));
426 } else
427 mangleSourceName(Record->getIdentifier());
428 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000429}
430
Mike Stump1eb44332009-09-09 15:08:12 +0000431void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000432CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
433 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000434 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000435 case OO_New: Out << "nw"; break;
436 // ::= na # new[]
437 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000438 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000439 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000440 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000441 case OO_Array_Delete: Out << "da"; break;
442 // ::= ps # + (unary)
443 // ::= pl # +
444 case OO_Plus: Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000445 // ::= ng # - (unary)
446 // ::= mi # -
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000447 case OO_Minus: Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000448 // ::= ad # & (unary)
449 // ::= an # &
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000450 case OO_Amp: Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000451 // ::= de # * (unary)
452 // ::= ml # *
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000453 case OO_Star: Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000454 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000455 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000456 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000457 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000458 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000459 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000460 // ::= or # |
461 case OO_Pipe: Out << "or"; break;
462 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000463 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000464 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000465 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000466 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000467 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000468 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000469 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000470 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000471 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000472 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000473 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000474 // ::= rM # %=
475 case OO_PercentEqual: Out << "rM"; break;
476 // ::= aN # &=
477 case OO_AmpEqual: Out << "aN"; break;
478 // ::= oR # |=
479 case OO_PipeEqual: Out << "oR"; break;
480 // ::= eO # ^=
481 case OO_CaretEqual: Out << "eO"; break;
482 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000483 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000484 // ::= rs # >>
485 case OO_GreaterGreater: Out << "rs"; break;
486 // ::= lS # <<=
487 case OO_LessLessEqual: Out << "lS"; break;
488 // ::= rS # >>=
489 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000490 // ::= eq # ==
491 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000492 // ::= ne # !=
493 case OO_ExclaimEqual: Out << "ne"; break;
494 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000495 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000496 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000497 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000498 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000499 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000500 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000501 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000502 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000503 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000504 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000505 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000506 // ::= oo # ||
507 case OO_PipePipe: Out << "oo"; break;
508 // ::= pp # ++
509 case OO_PlusPlus: Out << "pp"; break;
510 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000511 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000512 // ::= cm # ,
513 case OO_Comma: Out << "cm"; break;
514 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000515 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000516 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000517 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000518 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000519 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000520 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000521 case OO_Subscript: Out << "ix"; break;
522 // UNSUPPORTED: ::= qu # ?
523
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000524 case OO_None:
525 case OO_Conditional:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000526 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +0000527 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000528 break;
529 }
530}
531
532void CXXNameMangler::mangleCVQualifiers(unsigned Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +0000533 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000534 if (Quals & QualType::Restrict)
535 Out << 'r';
536 if (Quals & QualType::Volatile)
537 Out << 'V';
538 if (Quals & QualType::Const)
539 Out << 'K';
540}
541
542void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000543 // Only operate on the canonical type!
544 T = Context.getCanonicalType(T);
545
Anders Carlsson76967372009-09-17 00:43:46 +0000546 bool IsSubstitutable = !isa<BuiltinType>(T);
547 if (IsSubstitutable && mangleSubstitution(T))
548 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000549
Anders Carlsson76967372009-09-17 00:43:46 +0000550 if (unsigned CVRQualifiers = T.getCVRQualifiers()) {
551 // <type> ::= <CV-qualifiers> <type>
552 mangleCVQualifiers(CVRQualifiers);
553
554 mangleType(T.getUnqualifiedType());
555 } else {
556 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +0000557#define ABSTRACT_TYPE(CLASS, PARENT)
558#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000559 case Type::CLASS: \
560 llvm::llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
561 return;
John McCallefe6aee2009-09-05 07:56:18 +0000562#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000563 case Type::CLASS: \
564 mangleType(static_cast<CLASS##Type*>(T.getTypePtr())); \
565 break;
John McCallefe6aee2009-09-05 07:56:18 +0000566#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +0000567 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000568 }
Anders Carlsson76967372009-09-17 00:43:46 +0000569
570 // Add the substitution.
571 if (IsSubstitutable)
572 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000573}
574
575void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +0000576 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000577 // <builtin-type> ::= v # void
578 // ::= w # wchar_t
579 // ::= b # bool
580 // ::= c # char
581 // ::= a # signed char
582 // ::= h # unsigned char
583 // ::= s # short
584 // ::= t # unsigned short
585 // ::= i # int
586 // ::= j # unsigned int
587 // ::= l # long
588 // ::= m # unsigned long
589 // ::= x # long long, __int64
590 // ::= y # unsigned long long, __int64
591 // ::= n # __int128
592 // UNSUPPORTED: ::= o # unsigned __int128
593 // ::= f # float
594 // ::= d # double
595 // ::= e # long double, __float80
596 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000597 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
598 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
599 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
600 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000601 // ::= Di # char32_t
602 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000603 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000604 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
605 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000606 switch (T->getKind()) {
607 case BuiltinType::Void: Out << 'v'; break;
608 case BuiltinType::Bool: Out << 'b'; break;
609 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
610 case BuiltinType::UChar: Out << 'h'; break;
611 case BuiltinType::UShort: Out << 't'; break;
612 case BuiltinType::UInt: Out << 'j'; break;
613 case BuiltinType::ULong: Out << 'm'; break;
614 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000615 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000616 case BuiltinType::SChar: Out << 'a'; break;
617 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000618 case BuiltinType::Char16: Out << "Ds"; break;
619 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000620 case BuiltinType::Short: Out << 's'; break;
621 case BuiltinType::Int: Out << 'i'; break;
622 case BuiltinType::Long: Out << 'l'; break;
623 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000624 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000625 case BuiltinType::Float: Out << 'f'; break;
626 case BuiltinType::Double: Out << 'd'; break;
627 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000628 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000629
630 case BuiltinType::Overload:
631 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +0000632 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000633 "Overloaded and dependent types shouldn't get to name mangling");
634 break;
Anders Carlssone89d1592009-06-26 18:41:36 +0000635 case BuiltinType::UndeducedAuto:
636 assert(0 && "Should not see undeduced auto here");
637 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +0000638 case BuiltinType::ObjCId: Out << "11objc_object"; break;
639 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000640 }
641}
642
John McCallefe6aee2009-09-05 07:56:18 +0000643// <type> ::= <function-type>
644// <function-type> ::= F [Y] <bare-function-type> E
645void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000646 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +0000647 // FIXME: We don't have enough information in the AST to produce the 'Y'
648 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000649 mangleBareFunctionType(T, /*MangleReturnType=*/true);
650 Out << 'E';
651}
John McCallefe6aee2009-09-05 07:56:18 +0000652void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
653 llvm::llvm_unreachable("Can't mangle K&R function prototypes");
654}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000655void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
656 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +0000657 // We should never be mangling something without a prototype.
658 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
659
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000660 // <bare-function-type> ::= <signature type>+
661 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +0000662 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000663
Anders Carlssonc6c91bc2009-04-01 00:15:23 +0000664 if (Proto->getNumArgs() == 0) {
665 Out << 'v';
666 return;
667 }
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Douglas Gregor72564e72009-02-26 23:50:07 +0000669 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000670 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000671 Arg != ArgEnd; ++Arg)
672 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +0000673
674 // <builtin-type> ::= z # ellipsis
675 if (Proto->isVariadic())
676 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000677}
678
John McCallefe6aee2009-09-05 07:56:18 +0000679// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +0000680// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +0000681void CXXNameMangler::mangleType(const EnumType *T) {
682 mangleType(static_cast<const TagType*>(T));
683}
684void CXXNameMangler::mangleType(const RecordType *T) {
685 mangleType(static_cast<const TagType*>(T));
686}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000687void CXXNameMangler::mangleType(const TagType *T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000688 if (!T->getDecl()->getIdentifier())
689 mangleName(T->getDecl()->getTypedefForAnonDecl());
690 else
691 mangleName(T->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Mike Stump141c5af2009-09-02 00:25:38 +0000693 // If this is a class template specialization, mangle the template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000694 if (ClassTemplateSpecializationDecl *Spec
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000695 = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl()))
696 mangleTemplateArgumentList(Spec->getTemplateArgs());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000697}
698
John McCallefe6aee2009-09-05 07:56:18 +0000699// <type> ::= <array-type>
700// <array-type> ::= A <positive dimension number> _ <element type>
701// ::= A [<dimension expression>] _ <element type>
702void CXXNameMangler::mangleType(const ConstantArrayType *T) {
703 Out << 'A' << T->getSize() << '_';
704 mangleType(T->getElementType());
705}
706void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000707 Out << 'A';
John McCallefe6aee2009-09-05 07:56:18 +0000708 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000709 Out << '_';
710 mangleType(T->getElementType());
711}
John McCallefe6aee2009-09-05 07:56:18 +0000712void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
713 Out << 'A';
714 mangleExpression(T->getSizeExpr());
715 Out << '_';
716 mangleType(T->getElementType());
717}
718void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
719 Out << 'A' << '_';
720 mangleType(T->getElementType());
721}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000722
John McCallefe6aee2009-09-05 07:56:18 +0000723// <type> ::= <pointer-to-member-type>
724// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000725void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000726 Out << 'M';
727 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +0000728 QualType PointeeType = T->getPointeeType();
729 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
730 mangleCVQualifiers(FPT->getTypeQuals());
731 mangleType(FPT);
Mike Stump1eb44332009-09-09 15:08:12 +0000732 } else
Anders Carlsson0e650012009-05-17 17:41:20 +0000733 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000734}
735
John McCallefe6aee2009-09-05 07:56:18 +0000736// <type> ::= <template-param>
737// <template-param> ::= T_ # first template parameter
738// ::= T <parameter-2 non-negative number> _
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000739void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000740 if (T->getIndex() == 0)
741 Out << "T_";
742 else
743 Out << 'T' << (T->getIndex() - 1) << '_';
744}
745
John McCallefe6aee2009-09-05 07:56:18 +0000746// FIXME: <type> ::= <template-template-param> <template-args>
747// FIXME: <type> ::= <substitution> # See Compression below
748
749// <type> ::= P <type> # pointer-to
750void CXXNameMangler::mangleType(const PointerType *T) {
751 Out << 'P';
752 mangleType(T->getPointeeType());
753}
754void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
755 Out << 'P';
756 mangleType(T->getPointeeType());
757}
758
759// <type> ::= R <type> # reference-to
760void CXXNameMangler::mangleType(const LValueReferenceType *T) {
761 Out << 'R';
762 mangleType(T->getPointeeType());
763}
764
765// <type> ::= O <type> # rvalue reference-to (C++0x)
766void CXXNameMangler::mangleType(const RValueReferenceType *T) {
767 Out << 'O';
768 mangleType(T->getPointeeType());
769}
770
771// <type> ::= C <type> # complex pair (C 2000)
772void CXXNameMangler::mangleType(const ComplexType *T) {
773 Out << 'C';
774 mangleType(T->getElementType());
775}
776
777// GNU extension: vector types
778void CXXNameMangler::mangleType(const VectorType *T) {
779 Out << "U8__vector";
780 mangleType(T->getElementType());
781}
782void CXXNameMangler::mangleType(const ExtVectorType *T) {
783 mangleType(static_cast<const VectorType*>(T));
784}
785void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
786 Out << "U8__vector";
787 mangleType(T->getElementType());
788}
789
Anders Carlssona40c5e42009-03-07 22:03:21 +0000790void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
791 mangleSourceName(T->getDecl()->getIdentifier());
792}
793
John McCallefe6aee2009-09-05 07:56:18 +0000794void CXXNameMangler::mangleType(const BlockPointerType *T) {
795 assert(false && "can't mangle block pointer types yet");
796}
797
798void CXXNameMangler::mangleType(const FixedWidthIntType *T) {
799 assert(false && "can't mangle arbitary-precision integer type yet");
800}
801
802void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
803 // TSTs are never canonical unless they're dependent.
804 assert(false && "can't mangle dependent template specializations yet");
805}
806
807void CXXNameMangler::mangleType(const TypenameType *T) {
808 assert(false && "can't mangle dependent typenames yet");
809}
810
811// FIXME: For now, just drop all extension qualifiers on the floor.
812void CXXNameMangler::mangleType(const ExtQualType *T) {
813 mangleType(QualType(T->getBaseType(), 0));
814}
815
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000816void CXXNameMangler::mangleExpression(Expr *E) {
817 assert(false && "Cannot mangle expressions yet");
818}
819
John McCallefe6aee2009-09-05 07:56:18 +0000820// FIXME: <type> ::= G <type> # imaginary (C 2000)
821// FIXME: <type> ::= U <source-name> <type> # vendor extended type qualifier
822
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000823void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
824 // <ctor-dtor-name> ::= C1 # complete object constructor
825 // ::= C2 # base object constructor
826 // ::= C3 # complete object allocating constructor
827 //
828 switch (T) {
829 case Ctor_Complete:
830 Out << "C1";
831 break;
832 case Ctor_Base:
833 Out << "C2";
834 break;
835 case Ctor_CompleteAllocating:
836 Out << "C3";
837 break;
838 }
839}
840
Anders Carlsson27ae5362009-04-17 01:58:57 +0000841void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
842 // <ctor-dtor-name> ::= D0 # deleting destructor
843 // ::= D1 # complete object destructor
844 // ::= D2 # base object destructor
845 //
846 switch (T) {
847 case Dtor_Deleting:
848 Out << "D0";
849 break;
850 case Dtor_Complete:
851 Out << "D1";
852 break;
853 case Dtor_Base:
854 Out << "D2";
855 break;
856 }
857}
858
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000859void CXXNameMangler::mangleTemplateArgumentList(const TemplateArgumentList &L) {
860 // <template-args> ::= I <template-arg>+ E
861 Out << "I";
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000863 for (unsigned i = 0, e = L.size(); i != e; ++i) {
864 const TemplateArgument &A = L[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000866 mangleTemplateArgument(A);
867 }
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000869 Out << "E";
870}
871
872void CXXNameMangler::mangleTemplateArgument(const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +0000873 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000874 // ::= X <expression> E # expression
875 // ::= <expr-primary> # simple expressions
876 // ::= I <template-arg>* E # argument pack
877 // ::= sp <expression> # pack expansion of (C++0x)
878 switch (A.getKind()) {
879 default:
880 assert(0 && "Unknown template argument kind!");
881 case TemplateArgument::Type:
882 mangleType(A.getAsType());
883 break;
884 case TemplateArgument::Integral:
885 // <expr-primary> ::= L <type> <value number> E # integer literal
886
887 Out << 'L';
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000889 mangleType(A.getIntegralType());
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000891 const llvm::APSInt *Integral = A.getAsIntegral();
892 if (A.getIntegralType()->isBooleanType()) {
893 // Boolean values are encoded as 0/1.
894 Out << (Integral->getBoolValue() ? '1' : '0');
895 } else {
896 if (Integral->isNegative())
897 Out << 'n';
898 Integral->abs().print(Out, false);
899 }
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000901 Out << 'E';
902 break;
903 }
904}
905
Anders Carlsson76967372009-09-17 00:43:46 +0000906// <substitution> ::= S <seq-id> _
907// ::= S_
908bool CXXNameMangler::mangleSubstitution(QualType T) {
909 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
910
911 llvm::DenseMap<uintptr_t, unsigned>::iterator I =
912 Substitutions.find(TypePtr);
913 if (I == Substitutions.end())
914 return false;
915
916 unsigned SeqID = I->second;
917 if (SeqID == 0)
918 Out << "S_";
919 else {
920 SeqID--;
921
922 // <seq-id> is encoded in base-36, using digits and upper case letters.
923 char Buffer[10];
924 char *BufferPtr = Buffer + 9;
925
926 *BufferPtr = 0;
927 if (SeqID == 0) *--BufferPtr = '0';
928
929 while (SeqID) {
930 assert(BufferPtr > Buffer && "Buffer overflow!");
931
932 unsigned char c = static_cast<unsigned char>(SeqID) % 36;
933
934 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
935 SeqID /= 36;
936 }
937
938 Out << 'S' << BufferPtr << '_';
939 }
940
941 return true;
942}
943
944void CXXNameMangler::addSubstitution(QualType T) {
945 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
946 unsigned SeqID = Substitutions.size();
947
948 assert(!Substitutions.count(TypePtr) && "Substitution already exists!");
949 Substitutions[TypePtr] = SeqID;
950}
951
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000952namespace clang {
Mike Stump141c5af2009-09-02 00:25:38 +0000953 /// \brief Mangles the name of the declaration D and emits that name to the
954 /// given output stream.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000955 ///
Mike Stump141c5af2009-09-02 00:25:38 +0000956 /// If the declaration D requires a mangled name, this routine will emit that
957 /// mangled name to \p os and return true. Otherwise, \p os will be unchanged
958 /// and this routine will return false. In this case, the caller should just
959 /// emit the identifier of the declaration (\c D->getIdentifier()) as its
960 /// name.
Mike Stump1eb44332009-09-09 15:08:12 +0000961 bool mangleName(const NamedDecl *D, ASTContext &Context,
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000962 llvm::raw_ostream &os) {
Anders Carlsson578aa642009-05-03 16:51:04 +0000963 assert(!isa<CXXConstructorDecl>(D) &&
964 "Use mangleCXXCtor for constructor decls!");
965 assert(!isa<CXXDestructorDecl>(D) &&
966 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000968 CXXNameMangler Mangler(Context, os);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000969 if (!Mangler.mangle(D))
970 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Douglas Gregor6ec36682009-02-18 23:53:56 +0000972 os.flush();
973 return true;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000974 }
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Mike Stump141c5af2009-09-02 00:25:38 +0000976 /// \brief Mangles the a thunk with the offset n for the declaration D and
977 /// emits that name to the given output stream.
Mike Stumpdec025b2009-09-07 04:27:52 +0000978 void mangleThunk(const FunctionDecl *FD, int64_t nv, int64_t v,
Mike Stump883f1272009-09-02 00:28:47 +0000979 ASTContext &Context, llvm::raw_ostream &os) {
Mike Stump141c5af2009-09-02 00:25:38 +0000980 // FIXME: Hum, we might have to thunk these, fix.
Mike Stumpdec025b2009-09-07 04:27:52 +0000981 assert(!isa<CXXDestructorDecl>(FD) &&
Mike Stump141c5af2009-09-02 00:25:38 +0000982 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Mike Stump141c5af2009-09-02 00:25:38 +0000984 CXXNameMangler Mangler(Context, os);
Mike Stumpdec025b2009-09-07 04:27:52 +0000985 Mangler.mangleThunk(FD, nv, v);
Mike Stump141c5af2009-09-02 00:25:38 +0000986 os.flush();
987 }
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Mike Stump9124bcc2009-09-02 00:56:18 +0000989 /// \brief Mangles the a covariant thunk for the declaration D and emits that
990 /// name to the given output stream.
Mike Stumpdec025b2009-09-07 04:27:52 +0000991 void mangleCovariantThunk(const FunctionDecl *FD, int64_t nv_t, int64_t v_t,
Mike Stump77ca8f62009-09-05 07:20:32 +0000992 int64_t nv_r, int64_t v_r, ASTContext &Context,
Mike Stump9124bcc2009-09-02 00:56:18 +0000993 llvm::raw_ostream &os) {
994 // FIXME: Hum, we might have to thunk these, fix.
Mike Stumpdec025b2009-09-07 04:27:52 +0000995 assert(!isa<CXXDestructorDecl>(FD) &&
Mike Stump9124bcc2009-09-02 00:56:18 +0000996 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Mike Stump9124bcc2009-09-02 00:56:18 +0000998 CXXNameMangler Mangler(Context, os);
Mike Stumpdec025b2009-09-07 04:27:52 +0000999 Mangler.mangleCovariantThunk(FD, nv_t, v_t, nv_r, v_r);
Mike Stump9124bcc2009-09-02 00:56:18 +00001000 os.flush();
1001 }
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001003 /// mangleGuardVariable - Returns the mangled name for a guard variable
1004 /// for the passed in VarDecl.
Anders Carlsson41aa8c12009-04-13 18:02:10 +00001005 void mangleGuardVariable(const VarDecl *D, ASTContext &Context,
1006 llvm::raw_ostream &os) {
1007 CXXNameMangler Mangler(Context, os);
1008 Mangler.mangleGuardVariable(D);
1009
1010 os.flush();
1011 }
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001013 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
1014 ASTContext &Context, llvm::raw_ostream &os) {
1015 CXXNameMangler Mangler(Context, os);
1016 Mangler.mangleCXXCtor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001018 os.flush();
1019 }
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Anders Carlsson27ae5362009-04-17 01:58:57 +00001021 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
1022 ASTContext &Context, llvm::raw_ostream &os) {
1023 CXXNameMangler Mangler(Context, os);
1024 Mangler.mangleCXXDtor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Anders Carlsson27ae5362009-04-17 01:58:57 +00001026 os.flush();
1027 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001028
Mike Stumpf1216772009-07-31 18:25:34 +00001029 void mangleCXXVtable(QualType Type, ASTContext &Context,
1030 llvm::raw_ostream &os) {
1031 CXXNameMangler Mangler(Context, os);
1032 Mangler.mangleCXXVtable(Type);
1033
1034 os.flush();
1035 }
Mike Stump738f8c22009-07-31 23:15:31 +00001036
1037 void mangleCXXRtti(QualType Type, ASTContext &Context,
1038 llvm::raw_ostream &os) {
1039 CXXNameMangler Mangler(Context, os);
1040 Mangler.mangleCXXRtti(Type);
1041
1042 os.flush();
1043 }
Mike Stumpf1216772009-07-31 18:25:34 +00001044}