blob: c0451bfa02c4079f58275c61c15238f5d485763a [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;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000219 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
220 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
221 isa<CXXConversionDecl>(FD)))
222 MangleReturnType = true;
223
224 // Mangle the type of the primary template.
225 FD = PrimaryTemplate->getTemplatedDecl();
226 }
227
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000228 mangleBareFunctionType(FD->getType()->getAsFunctionType(), MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000229}
230
231static bool isStdNamespace(const DeclContext *DC) {
232 if (!DC->isNamespace() || !DC->getParent()->isTranslationUnit())
233 return false;
234
235 const NamespaceDecl *NS = cast<NamespaceDecl>(DC);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000236 return NS->getOriginalNamespace()->getIdentifier()->isStr("std");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000237}
238
239void CXXNameMangler::mangleName(const NamedDecl *ND) {
240 // <name> ::= <nested-name>
241 // ::= <unscoped-name>
242 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000243 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000244 //
Anders Carlsson201ce742009-09-17 03:17:01 +0000245 if (ND->getDeclContext()->isTranslationUnit() ||
246 isStdNamespace(ND->getDeclContext())) {
247 const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
248 if (FD && FD->getPrimaryTemplate())
249 mangleUnscopedTemplateName(FD);
250 else
251 mangleUnscopedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000252 } else if (isa<FunctionDecl>(ND->getDeclContext()))
253 mangleLocalName(ND);
254 else
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000255 mangleNestedName(ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000256}
257
Anders Carlsson201ce742009-09-17 03:17:01 +0000258void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
259 // <unscoped-name> ::= <unqualified-name>
260 // ::= St <unqualified-name> # ::std::
261 if (isStdNamespace(ND->getDeclContext()))
262 Out << "St";
263
264 mangleUnqualifiedName(ND);
265}
266
267void CXXNameMangler::mangleUnscopedTemplateName(const FunctionDecl *FD) {
268 // <unscoped-template-name> ::= <unscoped-name>
269 // ::= <substitution>
270 mangleUnscopedName(FD);
271}
272
Mike Stump77ca8f62009-09-05 07:20:32 +0000273void CXXNameMangler::mangleCalloffset(int64_t nv, int64_t v) {
Mike Stump141c5af2009-09-02 00:25:38 +0000274 // <call-offset> ::= h <nv-offset> _
275 // ::= v <v-offset> _
276 // <nv-offset> ::= <offset number> # non-virtual base override
277 // <v-offset> ::= <offset nubmer> _ <virtual offset number>
278 // # virtual base override, with vcall offset
Mike Stump77ca8f62009-09-05 07:20:32 +0000279 if (v == 0) {
Mike Stump9124bcc2009-09-02 00:56:18 +0000280 Out << "h";
Mike Stump141c5af2009-09-02 00:25:38 +0000281 if (nv < 0) {
282 Out << "n";
283 nv = -nv;
284 }
285 Out << nv;
286 } else {
Mike Stump9124bcc2009-09-02 00:56:18 +0000287 Out << "v";
Mike Stump141c5af2009-09-02 00:25:38 +0000288 if (nv < 0) {
289 Out << "n";
290 nv = -nv;
291 }
292 Out << nv;
293 Out << "_";
294 if (v < 0) {
295 Out << "n";
296 v = -v;
297 }
298 Out << v;
299 }
300 Out << "_";
Mike Stump9124bcc2009-09-02 00:56:18 +0000301}
302
Mike Stumpdec025b2009-09-07 04:27:52 +0000303void CXXNameMangler::mangleThunk(const FunctionDecl *FD, int64_t nv,
304 int64_t v) {
Mike Stump9124bcc2009-09-02 00:56:18 +0000305 // <special-name> ::= T <call-offset> <base encoding>
306 // # base is the nominal target function of thunk
Mike Stumpdec025b2009-09-07 04:27:52 +0000307 Out << "_ZT";
Mike Stump77ca8f62009-09-05 07:20:32 +0000308 mangleCalloffset(nv, v);
Mike Stumpdec025b2009-09-07 04:27:52 +0000309 mangleFunctionEncoding(FD);
Mike Stump9124bcc2009-09-02 00:56:18 +0000310}
311
Mike Stumpdec025b2009-09-07 04:27:52 +0000312 void CXXNameMangler::mangleCovariantThunk(const FunctionDecl *FD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000313 int64_t nv_t, int64_t v_t,
Mike Stump9124bcc2009-09-02 00:56:18 +0000314 int64_t nv_r, int64_t v_r) {
315 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
316 // # base is the nominal target function of thunk
317 // # first call-offset is 'this' adjustment
318 // # second call-offset is result adjustment
Mike Stumpdec025b2009-09-07 04:27:52 +0000319 Out << "_ZTc";
Mike Stump77ca8f62009-09-05 07:20:32 +0000320 mangleCalloffset(nv_t, v_t);
321 mangleCalloffset(nv_r, v_r);
Mike Stumpdec025b2009-09-07 04:27:52 +0000322 mangleFunctionEncoding(FD);
Mike Stump141c5af2009-09-02 00:25:38 +0000323}
324
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000325void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) {
326 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000327 // ::= <ctor-dtor-name>
328 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000329 DeclarationName Name = ND->getDeclName();
330 switch (Name.getNameKind()) {
331 case DeclarationName::Identifier:
332 mangleSourceName(Name.getAsIdentifierInfo());
333 break;
334
335 case DeclarationName::ObjCZeroArgSelector:
336 case DeclarationName::ObjCOneArgSelector:
337 case DeclarationName::ObjCMultiArgSelector:
338 assert(false && "Can't mangle Objective-C selector names here!");
339 break;
340
341 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000342 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000343 // If the named decl is the C++ constructor we're mangling, use the type
344 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000345 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000346 else
347 // Otherwise, use the complete constructor name. This is relevant if a
348 // class with a constructor is declared within a constructor.
349 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000350 break;
351
352 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000353 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000354 // If the named decl is the C++ destructor we're mangling, use the type we
355 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000356 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
357 else
358 // Otherwise, use the complete destructor name. This is relevant if a
359 // class with a destructor is declared within a destructor.
360 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000361 break;
362
363 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000364 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000365 Out << "cv";
366 mangleType(Context.getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000367 break;
368
369 case DeclarationName::CXXOperatorName:
370 mangleOperatorName(Name.getCXXOverloadedOperator(),
371 cast<FunctionDecl>(ND)->getNumParams());
372 break;
373
374 case DeclarationName::CXXUsingDirective:
375 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000376 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000377 }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000379 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000380 if (const TemplateArgumentList *TemplateArgs
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000381 = Function->getTemplateSpecializationArgs())
382 mangleTemplateArgumentList(*TemplateArgs);
383 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000384}
385
386void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
387 // <source-name> ::= <positive length number> <identifier>
388 // <number> ::= [n] <non-negative decimal integer>
389 // <identifier> ::= <unqualified source code identifier>
390 Out << II->getLength() << II->getName();
391}
392
393void CXXNameMangler::mangleNestedName(const NamedDecl *ND) {
394 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
395 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
396 // FIXME: no template support
397 Out << 'N';
398 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
399 mangleCVQualifiers(Method->getTypeQualifiers());
400 manglePrefix(ND->getDeclContext());
401 mangleUnqualifiedName(ND);
402 Out << 'E';
403}
404
Anders Carlsson1b42c792009-04-02 16:24:45 +0000405void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
406 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
407 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000408 // <discriminator> := _ <non-negative number>
Anders Carlsson1b42c792009-04-02 16:24:45 +0000409 Out << 'Z';
410 mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext()));
411 Out << 'E';
412 mangleSourceName(ND->getIdentifier());
413}
414
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000415void CXXNameMangler::manglePrefix(const DeclContext *DC) {
416 // <prefix> ::= <prefix> <unqualified-name>
417 // ::= <template-prefix> <template-args>
418 // ::= <template-param>
419 // ::= # empty
420 // ::= <substitution>
421 // FIXME: We only handle mangling of namespaces and classes at the moment.
Anders Carlssonc8dee9c2009-04-01 00:42:16 +0000422 if (!DC->getParent()->isTranslationUnit())
423 manglePrefix(DC->getParent());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000424
425 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(DC))
426 mangleSourceName(Namespace->getIdentifier());
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000427 else if (const RecordDecl *Record = dyn_cast<RecordDecl>(DC)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000428 if (const ClassTemplateSpecializationDecl *D =
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000429 dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
430 mangleType(QualType(D->getTypeForDecl(), 0));
431 } else
432 mangleSourceName(Record->getIdentifier());
433 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000434}
435
Mike Stump1eb44332009-09-09 15:08:12 +0000436void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000437CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
438 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000439 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000440 case OO_New: Out << "nw"; break;
441 // ::= na # new[]
442 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000443 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000444 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000445 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000446 case OO_Array_Delete: Out << "da"; break;
447 // ::= ps # + (unary)
448 // ::= pl # +
449 case OO_Plus: Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000450 // ::= ng # - (unary)
451 // ::= mi # -
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000452 case OO_Minus: Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000453 // ::= ad # & (unary)
454 // ::= an # &
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000455 case OO_Amp: Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000456 // ::= de # * (unary)
457 // ::= ml # *
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000458 case OO_Star: Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000459 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000460 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000461 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000462 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000463 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000464 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000465 // ::= or # |
466 case OO_Pipe: Out << "or"; break;
467 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000468 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000469 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000470 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000471 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000472 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000473 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000474 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000475 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000476 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000477 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000478 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000479 // ::= rM # %=
480 case OO_PercentEqual: Out << "rM"; break;
481 // ::= aN # &=
482 case OO_AmpEqual: Out << "aN"; break;
483 // ::= oR # |=
484 case OO_PipeEqual: Out << "oR"; break;
485 // ::= eO # ^=
486 case OO_CaretEqual: Out << "eO"; break;
487 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000488 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000489 // ::= rs # >>
490 case OO_GreaterGreater: Out << "rs"; break;
491 // ::= lS # <<=
492 case OO_LessLessEqual: Out << "lS"; break;
493 // ::= rS # >>=
494 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000495 // ::= eq # ==
496 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000497 // ::= ne # !=
498 case OO_ExclaimEqual: Out << "ne"; break;
499 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000500 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000501 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000502 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000503 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000504 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000505 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000506 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000507 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000508 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000509 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000510 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000511 // ::= oo # ||
512 case OO_PipePipe: Out << "oo"; break;
513 // ::= pp # ++
514 case OO_PlusPlus: Out << "pp"; break;
515 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000516 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000517 // ::= cm # ,
518 case OO_Comma: Out << "cm"; break;
519 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000520 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000521 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000522 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000523 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000524 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000525 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000526 case OO_Subscript: Out << "ix"; break;
527 // UNSUPPORTED: ::= qu # ?
528
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000529 case OO_None:
530 case OO_Conditional:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000531 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +0000532 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000533 break;
534 }
535}
536
537void CXXNameMangler::mangleCVQualifiers(unsigned Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +0000538 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000539 if (Quals & QualType::Restrict)
540 Out << 'r';
541 if (Quals & QualType::Volatile)
542 Out << 'V';
543 if (Quals & QualType::Const)
544 Out << 'K';
545}
546
547void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000548 // Only operate on the canonical type!
549 T = Context.getCanonicalType(T);
550
Anders Carlsson76967372009-09-17 00:43:46 +0000551 bool IsSubstitutable = !isa<BuiltinType>(T);
552 if (IsSubstitutable && mangleSubstitution(T))
553 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000554
Anders Carlsson76967372009-09-17 00:43:46 +0000555 if (unsigned CVRQualifiers = T.getCVRQualifiers()) {
556 // <type> ::= <CV-qualifiers> <type>
557 mangleCVQualifiers(CVRQualifiers);
558
559 mangleType(T.getUnqualifiedType());
560 } else {
561 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +0000562#define ABSTRACT_TYPE(CLASS, PARENT)
563#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000564 case Type::CLASS: \
565 llvm::llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
566 return;
John McCallefe6aee2009-09-05 07:56:18 +0000567#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000568 case Type::CLASS: \
569 mangleType(static_cast<CLASS##Type*>(T.getTypePtr())); \
570 break;
John McCallefe6aee2009-09-05 07:56:18 +0000571#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +0000572 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000573 }
Anders Carlsson76967372009-09-17 00:43:46 +0000574
575 // Add the substitution.
576 if (IsSubstitutable)
577 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000578}
579
580void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +0000581 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000582 // <builtin-type> ::= v # void
583 // ::= w # wchar_t
584 // ::= b # bool
585 // ::= c # char
586 // ::= a # signed char
587 // ::= h # unsigned char
588 // ::= s # short
589 // ::= t # unsigned short
590 // ::= i # int
591 // ::= j # unsigned int
592 // ::= l # long
593 // ::= m # unsigned long
594 // ::= x # long long, __int64
595 // ::= y # unsigned long long, __int64
596 // ::= n # __int128
597 // UNSUPPORTED: ::= o # unsigned __int128
598 // ::= f # float
599 // ::= d # double
600 // ::= e # long double, __float80
601 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000602 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
603 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
604 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
605 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000606 // ::= Di # char32_t
607 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000608 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000609 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
610 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000611 switch (T->getKind()) {
612 case BuiltinType::Void: Out << 'v'; break;
613 case BuiltinType::Bool: Out << 'b'; break;
614 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
615 case BuiltinType::UChar: Out << 'h'; break;
616 case BuiltinType::UShort: Out << 't'; break;
617 case BuiltinType::UInt: Out << 'j'; break;
618 case BuiltinType::ULong: Out << 'm'; break;
619 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000620 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000621 case BuiltinType::SChar: Out << 'a'; break;
622 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000623 case BuiltinType::Char16: Out << "Ds"; break;
624 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000625 case BuiltinType::Short: Out << 's'; break;
626 case BuiltinType::Int: Out << 'i'; break;
627 case BuiltinType::Long: Out << 'l'; break;
628 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000629 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000630 case BuiltinType::Float: Out << 'f'; break;
631 case BuiltinType::Double: Out << 'd'; break;
632 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000633 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000634
635 case BuiltinType::Overload:
636 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +0000637 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000638 "Overloaded and dependent types shouldn't get to name mangling");
639 break;
Anders Carlssone89d1592009-06-26 18:41:36 +0000640 case BuiltinType::UndeducedAuto:
641 assert(0 && "Should not see undeduced auto here");
642 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +0000643 case BuiltinType::ObjCId: Out << "11objc_object"; break;
644 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000645 }
646}
647
John McCallefe6aee2009-09-05 07:56:18 +0000648// <type> ::= <function-type>
649// <function-type> ::= F [Y] <bare-function-type> E
650void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000651 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +0000652 // FIXME: We don't have enough information in the AST to produce the 'Y'
653 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000654 mangleBareFunctionType(T, /*MangleReturnType=*/true);
655 Out << 'E';
656}
John McCallefe6aee2009-09-05 07:56:18 +0000657void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
658 llvm::llvm_unreachable("Can't mangle K&R function prototypes");
659}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000660void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
661 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +0000662 // We should never be mangling something without a prototype.
663 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
664
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000665 // <bare-function-type> ::= <signature type>+
666 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +0000667 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000668
Anders Carlssonc6c91bc2009-04-01 00:15:23 +0000669 if (Proto->getNumArgs() == 0) {
670 Out << 'v';
671 return;
672 }
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Douglas Gregor72564e72009-02-26 23:50:07 +0000674 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000675 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000676 Arg != ArgEnd; ++Arg)
677 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +0000678
679 // <builtin-type> ::= z # ellipsis
680 if (Proto->isVariadic())
681 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000682}
683
John McCallefe6aee2009-09-05 07:56:18 +0000684// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +0000685// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +0000686void CXXNameMangler::mangleType(const EnumType *T) {
687 mangleType(static_cast<const TagType*>(T));
688}
689void CXXNameMangler::mangleType(const RecordType *T) {
690 mangleType(static_cast<const TagType*>(T));
691}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000692void CXXNameMangler::mangleType(const TagType *T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000693 if (!T->getDecl()->getIdentifier())
694 mangleName(T->getDecl()->getTypedefForAnonDecl());
695 else
696 mangleName(T->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Mike Stump141c5af2009-09-02 00:25:38 +0000698 // If this is a class template specialization, mangle the template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000699 if (ClassTemplateSpecializationDecl *Spec
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000700 = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl()))
701 mangleTemplateArgumentList(Spec->getTemplateArgs());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000702}
703
John McCallefe6aee2009-09-05 07:56:18 +0000704// <type> ::= <array-type>
705// <array-type> ::= A <positive dimension number> _ <element type>
706// ::= A [<dimension expression>] _ <element type>
707void CXXNameMangler::mangleType(const ConstantArrayType *T) {
708 Out << 'A' << T->getSize() << '_';
709 mangleType(T->getElementType());
710}
711void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000712 Out << 'A';
John McCallefe6aee2009-09-05 07:56:18 +0000713 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000714 Out << '_';
715 mangleType(T->getElementType());
716}
John McCallefe6aee2009-09-05 07:56:18 +0000717void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
718 Out << 'A';
719 mangleExpression(T->getSizeExpr());
720 Out << '_';
721 mangleType(T->getElementType());
722}
723void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
724 Out << 'A' << '_';
725 mangleType(T->getElementType());
726}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000727
John McCallefe6aee2009-09-05 07:56:18 +0000728// <type> ::= <pointer-to-member-type>
729// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000730void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000731 Out << 'M';
732 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +0000733 QualType PointeeType = T->getPointeeType();
734 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
735 mangleCVQualifiers(FPT->getTypeQuals());
736 mangleType(FPT);
Mike Stump1eb44332009-09-09 15:08:12 +0000737 } else
Anders Carlsson0e650012009-05-17 17:41:20 +0000738 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000739}
740
John McCallefe6aee2009-09-05 07:56:18 +0000741// <type> ::= <template-param>
742// <template-param> ::= T_ # first template parameter
743// ::= T <parameter-2 non-negative number> _
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000744void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000745 if (T->getIndex() == 0)
746 Out << "T_";
747 else
748 Out << 'T' << (T->getIndex() - 1) << '_';
749}
750
John McCallefe6aee2009-09-05 07:56:18 +0000751// FIXME: <type> ::= <template-template-param> <template-args>
John McCallefe6aee2009-09-05 07:56:18 +0000752
753// <type> ::= P <type> # pointer-to
754void CXXNameMangler::mangleType(const PointerType *T) {
755 Out << 'P';
756 mangleType(T->getPointeeType());
757}
758void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
759 Out << 'P';
760 mangleType(T->getPointeeType());
761}
762
763// <type> ::= R <type> # reference-to
764void CXXNameMangler::mangleType(const LValueReferenceType *T) {
765 Out << 'R';
766 mangleType(T->getPointeeType());
767}
768
769// <type> ::= O <type> # rvalue reference-to (C++0x)
770void CXXNameMangler::mangleType(const RValueReferenceType *T) {
771 Out << 'O';
772 mangleType(T->getPointeeType());
773}
774
775// <type> ::= C <type> # complex pair (C 2000)
776void CXXNameMangler::mangleType(const ComplexType *T) {
777 Out << 'C';
778 mangleType(T->getElementType());
779}
780
781// GNU extension: vector types
782void CXXNameMangler::mangleType(const VectorType *T) {
783 Out << "U8__vector";
784 mangleType(T->getElementType());
785}
786void CXXNameMangler::mangleType(const ExtVectorType *T) {
787 mangleType(static_cast<const VectorType*>(T));
788}
789void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
790 Out << "U8__vector";
791 mangleType(T->getElementType());
792}
793
Anders Carlssona40c5e42009-03-07 22:03:21 +0000794void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
795 mangleSourceName(T->getDecl()->getIdentifier());
796}
797
John McCallefe6aee2009-09-05 07:56:18 +0000798void CXXNameMangler::mangleType(const BlockPointerType *T) {
799 assert(false && "can't mangle block pointer types yet");
800}
801
802void CXXNameMangler::mangleType(const FixedWidthIntType *T) {
803 assert(false && "can't mangle arbitary-precision integer type yet");
804}
805
806void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
807 // TSTs are never canonical unless they're dependent.
808 assert(false && "can't mangle dependent template specializations yet");
809}
810
811void CXXNameMangler::mangleType(const TypenameType *T) {
812 assert(false && "can't mangle dependent typenames yet");
813}
814
815// FIXME: For now, just drop all extension qualifiers on the floor.
816void CXXNameMangler::mangleType(const ExtQualType *T) {
817 mangleType(QualType(T->getBaseType(), 0));
818}
819
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000820void CXXNameMangler::mangleExpression(Expr *E) {
821 assert(false && "Cannot mangle expressions yet");
822}
823
John McCallefe6aee2009-09-05 07:56:18 +0000824// FIXME: <type> ::= G <type> # imaginary (C 2000)
825// FIXME: <type> ::= U <source-name> <type> # vendor extended type qualifier
826
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000827void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
828 // <ctor-dtor-name> ::= C1 # complete object constructor
829 // ::= C2 # base object constructor
830 // ::= C3 # complete object allocating constructor
831 //
832 switch (T) {
833 case Ctor_Complete:
834 Out << "C1";
835 break;
836 case Ctor_Base:
837 Out << "C2";
838 break;
839 case Ctor_CompleteAllocating:
840 Out << "C3";
841 break;
842 }
843}
844
Anders Carlsson27ae5362009-04-17 01:58:57 +0000845void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
846 // <ctor-dtor-name> ::= D0 # deleting destructor
847 // ::= D1 # complete object destructor
848 // ::= D2 # base object destructor
849 //
850 switch (T) {
851 case Dtor_Deleting:
852 Out << "D0";
853 break;
854 case Dtor_Complete:
855 Out << "D1";
856 break;
857 case Dtor_Base:
858 Out << "D2";
859 break;
860 }
861}
862
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000863void CXXNameMangler::mangleTemplateArgumentList(const TemplateArgumentList &L) {
864 // <template-args> ::= I <template-arg>+ E
865 Out << "I";
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000867 for (unsigned i = 0, e = L.size(); i != e; ++i) {
868 const TemplateArgument &A = L[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000870 mangleTemplateArgument(A);
871 }
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000873 Out << "E";
874}
875
876void CXXNameMangler::mangleTemplateArgument(const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +0000877 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000878 // ::= X <expression> E # expression
879 // ::= <expr-primary> # simple expressions
880 // ::= I <template-arg>* E # argument pack
881 // ::= sp <expression> # pack expansion of (C++0x)
882 switch (A.getKind()) {
883 default:
884 assert(0 && "Unknown template argument kind!");
885 case TemplateArgument::Type:
886 mangleType(A.getAsType());
887 break;
888 case TemplateArgument::Integral:
889 // <expr-primary> ::= L <type> <value number> E # integer literal
890
891 Out << 'L';
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000893 mangleType(A.getIntegralType());
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000895 const llvm::APSInt *Integral = A.getAsIntegral();
896 if (A.getIntegralType()->isBooleanType()) {
897 // Boolean values are encoded as 0/1.
898 Out << (Integral->getBoolValue() ? '1' : '0');
899 } else {
900 if (Integral->isNegative())
901 Out << 'n';
902 Integral->abs().print(Out, false);
903 }
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000905 Out << 'E';
906 break;
907 }
908}
909
Anders Carlsson76967372009-09-17 00:43:46 +0000910// <substitution> ::= S <seq-id> _
911// ::= S_
912bool CXXNameMangler::mangleSubstitution(QualType T) {
913 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
914
915 llvm::DenseMap<uintptr_t, unsigned>::iterator I =
916 Substitutions.find(TypePtr);
917 if (I == Substitutions.end())
918 return false;
919
920 unsigned SeqID = I->second;
921 if (SeqID == 0)
922 Out << "S_";
923 else {
924 SeqID--;
925
926 // <seq-id> is encoded in base-36, using digits and upper case letters.
927 char Buffer[10];
928 char *BufferPtr = Buffer + 9;
929
930 *BufferPtr = 0;
931 if (SeqID == 0) *--BufferPtr = '0';
932
933 while (SeqID) {
934 assert(BufferPtr > Buffer && "Buffer overflow!");
935
936 unsigned char c = static_cast<unsigned char>(SeqID) % 36;
937
938 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
939 SeqID /= 36;
940 }
941
942 Out << 'S' << BufferPtr << '_';
943 }
944
945 return true;
946}
947
948void CXXNameMangler::addSubstitution(QualType T) {
949 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
950 unsigned SeqID = Substitutions.size();
951
952 assert(!Substitutions.count(TypePtr) && "Substitution already exists!");
953 Substitutions[TypePtr] = SeqID;
954}
955
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000956namespace clang {
Mike Stump141c5af2009-09-02 00:25:38 +0000957 /// \brief Mangles the name of the declaration D and emits that name to the
958 /// given output stream.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000959 ///
Mike Stump141c5af2009-09-02 00:25:38 +0000960 /// If the declaration D requires a mangled name, this routine will emit that
961 /// mangled name to \p os and return true. Otherwise, \p os will be unchanged
962 /// and this routine will return false. In this case, the caller should just
963 /// emit the identifier of the declaration (\c D->getIdentifier()) as its
964 /// name.
Mike Stump1eb44332009-09-09 15:08:12 +0000965 bool mangleName(const NamedDecl *D, ASTContext &Context,
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000966 llvm::raw_ostream &os) {
Anders Carlsson578aa642009-05-03 16:51:04 +0000967 assert(!isa<CXXConstructorDecl>(D) &&
968 "Use mangleCXXCtor for constructor decls!");
969 assert(!isa<CXXDestructorDecl>(D) &&
970 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000972 CXXNameMangler Mangler(Context, os);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000973 if (!Mangler.mangle(D))
974 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Douglas Gregor6ec36682009-02-18 23:53:56 +0000976 os.flush();
977 return true;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000978 }
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Mike Stump141c5af2009-09-02 00:25:38 +0000980 /// \brief Mangles the a thunk with the offset n for the declaration D and
981 /// emits that name to the given output stream.
Mike Stumpdec025b2009-09-07 04:27:52 +0000982 void mangleThunk(const FunctionDecl *FD, int64_t nv, int64_t v,
Mike Stump883f1272009-09-02 00:28:47 +0000983 ASTContext &Context, llvm::raw_ostream &os) {
Mike Stump141c5af2009-09-02 00:25:38 +0000984 // FIXME: Hum, we might have to thunk these, fix.
Mike Stumpdec025b2009-09-07 04:27:52 +0000985 assert(!isa<CXXDestructorDecl>(FD) &&
Mike Stump141c5af2009-09-02 00:25:38 +0000986 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Mike Stump141c5af2009-09-02 00:25:38 +0000988 CXXNameMangler Mangler(Context, os);
Mike Stumpdec025b2009-09-07 04:27:52 +0000989 Mangler.mangleThunk(FD, nv, v);
Mike Stump141c5af2009-09-02 00:25:38 +0000990 os.flush();
991 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Mike Stump9124bcc2009-09-02 00:56:18 +0000993 /// \brief Mangles the a covariant thunk for the declaration D and emits that
994 /// name to the given output stream.
Mike Stumpdec025b2009-09-07 04:27:52 +0000995 void mangleCovariantThunk(const FunctionDecl *FD, int64_t nv_t, int64_t v_t,
Mike Stump77ca8f62009-09-05 07:20:32 +0000996 int64_t nv_r, int64_t v_r, ASTContext &Context,
Mike Stump9124bcc2009-09-02 00:56:18 +0000997 llvm::raw_ostream &os) {
998 // FIXME: Hum, we might have to thunk these, fix.
Mike Stumpdec025b2009-09-07 04:27:52 +0000999 assert(!isa<CXXDestructorDecl>(FD) &&
Mike Stump9124bcc2009-09-02 00:56:18 +00001000 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Mike Stump9124bcc2009-09-02 00:56:18 +00001002 CXXNameMangler Mangler(Context, os);
Mike Stumpdec025b2009-09-07 04:27:52 +00001003 Mangler.mangleCovariantThunk(FD, nv_t, v_t, nv_r, v_r);
Mike Stump9124bcc2009-09-02 00:56:18 +00001004 os.flush();
1005 }
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001007 /// mangleGuardVariable - Returns the mangled name for a guard variable
1008 /// for the passed in VarDecl.
Anders Carlsson41aa8c12009-04-13 18:02:10 +00001009 void mangleGuardVariable(const VarDecl *D, ASTContext &Context,
1010 llvm::raw_ostream &os) {
1011 CXXNameMangler Mangler(Context, os);
1012 Mangler.mangleGuardVariable(D);
1013
1014 os.flush();
1015 }
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001017 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
1018 ASTContext &Context, llvm::raw_ostream &os) {
1019 CXXNameMangler Mangler(Context, os);
1020 Mangler.mangleCXXCtor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001022 os.flush();
1023 }
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Anders Carlsson27ae5362009-04-17 01:58:57 +00001025 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
1026 ASTContext &Context, llvm::raw_ostream &os) {
1027 CXXNameMangler Mangler(Context, os);
1028 Mangler.mangleCXXDtor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Anders Carlsson27ae5362009-04-17 01:58:57 +00001030 os.flush();
1031 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001032
Mike Stumpf1216772009-07-31 18:25:34 +00001033 void mangleCXXVtable(QualType Type, ASTContext &Context,
1034 llvm::raw_ostream &os) {
1035 CXXNameMangler Mangler(Context, os);
1036 Mangler.mangleCXXVtable(Type);
1037
1038 os.flush();
1039 }
Mike Stump738f8c22009-07-31 23:15:31 +00001040
1041 void mangleCXXRtti(QualType Type, ASTContext &Context,
1042 llvm::raw_ostream &os) {
1043 CXXNameMangler Mangler(Context, os);
1044 Mangler.mangleCXXRtti(Type);
1045
1046 os.flush();
1047 }
Mike Stumpf1216772009-07-31 18:25:34 +00001048}