blob: 6429bd7c463f98500b767839474e62074b2a96ec [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 Carlssond3a932a2009-09-17 03:53:28 +000058 bool mangleSubstitution(const NamedDecl *ND) {
59 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
60 }
Anders Carlsson76967372009-09-17 00:43:46 +000061 bool mangleSubstitution(QualType T);
Anders Carlssond3a932a2009-09-17 03:53:28 +000062 bool mangleSubstitution(uintptr_t Ptr);
63
64 void addSubstitution(const NamedDecl *ND) {
65 addSubstitution(reinterpret_cast<uintptr_t>(ND));
66 }
Anders Carlsson76967372009-09-17 00:43:46 +000067 void addSubstitution(QualType T);
Anders Carlssond3a932a2009-09-17 03:53:28 +000068 void addSubstitution(uintptr_t Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +000069
Anders Carlsson43f17402009-04-02 15:51:53 +000070 bool mangleFunctionDecl(const FunctionDecl *FD);
Mike Stump1eb44332009-09-09 15:08:12 +000071
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000072 void mangleFunctionEncoding(const FunctionDecl *FD);
73 void mangleName(const NamedDecl *ND);
74 void mangleUnqualifiedName(const NamedDecl *ND);
Anders Carlsson201ce742009-09-17 03:17:01 +000075 void mangleUnscopedName(const NamedDecl *ND);
76 void mangleUnscopedTemplateName(const FunctionDecl *ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000077 void mangleSourceName(const IdentifierInfo *II);
Anders Carlsson1b42c792009-04-02 16:24:45 +000078 void mangleLocalName(const NamedDecl *ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000079 void mangleNestedName(const NamedDecl *ND);
80 void manglePrefix(const DeclContext *DC);
81 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
82 void mangleCVQualifiers(unsigned Quals);
83 void mangleType(QualType T);
John McCallefe6aee2009-09-05 07:56:18 +000084
85 // Declare manglers for every type class.
86#define ABSTRACT_TYPE(CLASS, PARENT)
87#define NON_CANONICAL_TYPE(CLASS, PARENT)
88#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
89#include "clang/AST/TypeNodes.def"
90
91 void mangleType(const TagType*);
92 void mangleBareFunctionType(const FunctionType *T,
93 bool MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000094 void mangleExpression(Expr *E);
Anders Carlsson3ac86b52009-04-15 05:36:58 +000095 void mangleCXXCtorType(CXXCtorType T);
Anders Carlsson27ae5362009-04-17 01:58:57 +000096 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +000097
Anders Carlsson7a0ba872009-05-15 16:09:15 +000098 void mangleTemplateArgumentList(const TemplateArgumentList &L);
99 void mangleTemplateArgument(const TemplateArgument &A);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000100 };
101}
102
Anders Carlsson43f17402009-04-02 15:51:53 +0000103static bool isInCLinkageSpecification(const Decl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000104 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000105 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000106 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000107 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
108 }
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Anders Carlsson43f17402009-04-02 15:51:53 +0000110 return false;
111}
112
113bool CXXNameMangler::mangleFunctionDecl(const FunctionDecl *FD) {
Mike Stump141c5af2009-09-02 00:25:38 +0000114 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
115 // (always).
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000116 if (!FD->hasAttr<OverloadableAttr>()) {
Chris Lattner783601d2009-06-13 23:34:16 +0000117 // C functions are not mangled, and "main" is never mangled.
Douglas Gregor48a83b52009-09-12 00:17:51 +0000118 if (!Context.getLangOptions().CPlusPlus || FD->isMain())
Chris Lattner783601d2009-06-13 23:34:16 +0000119 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000120
121 // No mangling in an "implicit extern C" header.
Chris Lattner783601d2009-06-13 23:34:16 +0000122 if (FD->getLocation().isValid() &&
123 Context.getSourceManager().isInExternCSystemHeader(FD->getLocation()))
124 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Chris Lattner783601d2009-06-13 23:34:16 +0000126 // No name mangling in a C linkage specification.
127 if (isInCLinkageSpecification(FD))
128 return false;
129 }
Anders Carlsson43f17402009-04-02 15:51:53 +0000130
131 // If we get here, mangle the decl name!
132 Out << "_Z";
133 mangleFunctionEncoding(FD);
134 return true;
135}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000136
137bool CXXNameMangler::mangle(const NamedDecl *D) {
Mike Stump141c5af2009-09-02 00:25:38 +0000138 // Any decl can be declared with __asm("foo") on it, and this takes precedence
139 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000140 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000141 // If we have an asm name, then we use it as the mangling.
142 Out << '\01'; // LLVM IR Marker for __asm("foo")
143 Out << ALA->getLabel();
144 return true;
145 }
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000147 // <mangled-name> ::= _Z <encoding>
148 // ::= <data name>
149 // ::= <special-name>
150
151 // FIXME: Actually use a visitor to decode these?
Anders Carlsson43f17402009-04-02 15:51:53 +0000152 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
153 return mangleFunctionDecl(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Anders Carlsson329749c2009-04-02 16:05:20 +0000155 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
156 if (!Context.getLangOptions().CPlusPlus ||
Anders Carlsson9ccb0652009-04-11 01:19:45 +0000157 isInCLinkageSpecification(D) ||
158 D->getDeclContext()->isTranslationUnit())
Anders Carlsson329749c2009-04-02 16:05:20 +0000159 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Anders Carlsson329749c2009-04-02 16:05:20 +0000161 Out << "_Z";
162 mangleName(VD);
163 return true;
164 }
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Anders Carlsson43f17402009-04-02 15:51:53 +0000166 return false;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000167}
168
Mike Stump1eb44332009-09-09 15:08:12 +0000169void CXXNameMangler::mangleCXXCtor(const CXXConstructorDecl *D,
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000170 CXXCtorType Type) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000171 assert(!Structor && "Structor already set!");
172 Structor = D;
173 StructorType = Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Anders Carlsson27ae5362009-04-17 01:58:57 +0000175 mangle(D);
176}
177
Mike Stump1eb44332009-09-09 15:08:12 +0000178void CXXNameMangler::mangleCXXDtor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000179 CXXDtorType Type) {
180 assert(!Structor && "Structor already set!");
181 Structor = D;
182 StructorType = Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000184 mangle(D);
185}
186
Mike Stumpf1216772009-07-31 18:25:34 +0000187void CXXNameMangler::mangleCXXVtable(QualType T) {
188 // <special-name> ::= TV <type> # virtual table
189 Out << "_ZTV";
190 mangleType(T);
191}
192
Mike Stump738f8c22009-07-31 23:15:31 +0000193void CXXNameMangler::mangleCXXRtti(QualType T) {
194 // <special-name> ::= TI <type> # typeinfo structure
195 Out << "_ZTI";
196 mangleType(T);
197}
198
Mike Stump1eb44332009-09-09 15:08:12 +0000199void CXXNameMangler::mangleGuardVariable(const VarDecl *D) {
200 // <special-name> ::= GV <object name> # Guard variable for one-time
Mike Stumpf1216772009-07-31 18:25:34 +0000201 // # initialization
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000202
203 Out << "_ZGV";
204 mangleName(D);
205}
206
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000207void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
208 // <encoding> ::= <function name> <bare-function-type>
209 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Mike Stump141c5af2009-09-02 00:25:38 +0000211 // Whether the mangling of a function type includes the return type depends on
212 // the context and the nature of the function. The rules for deciding whether
213 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000214 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000215 // 1. Template functions (names or types) have return types encoded, with
216 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000217 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000218 // e.g. parameters, pointer types, etc., have return type encoded, with the
219 // exceptions listed below.
220 // 3. Non-template function names do not have return types encoded.
221 //
Mike Stump141c5af2009-09-02 00:25:38 +0000222 // The exceptions mentioned in (1) and (2) above, for which the return type is
223 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000224 // 1. Constructors.
225 // 2. Destructors.
226 // 3. Conversion operator functions, e.g. operator int.
227 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000228 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
229 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
230 isa<CXXConversionDecl>(FD)))
231 MangleReturnType = true;
232
233 // Mangle the type of the primary template.
234 FD = PrimaryTemplate->getTemplatedDecl();
235 }
236
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000237 mangleBareFunctionType(FD->getType()->getAsFunctionType(), MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000238}
239
240static bool isStdNamespace(const DeclContext *DC) {
241 if (!DC->isNamespace() || !DC->getParent()->isTranslationUnit())
242 return false;
243
244 const NamespaceDecl *NS = cast<NamespaceDecl>(DC);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000245 return NS->getOriginalNamespace()->getIdentifier()->isStr("std");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000246}
247
248void CXXNameMangler::mangleName(const NamedDecl *ND) {
249 // <name> ::= <nested-name>
250 // ::= <unscoped-name>
251 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000252 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000253 //
Anders Carlsson201ce742009-09-17 03:17:01 +0000254 if (ND->getDeclContext()->isTranslationUnit() ||
255 isStdNamespace(ND->getDeclContext())) {
256 const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
257 if (FD && FD->getPrimaryTemplate())
258 mangleUnscopedTemplateName(FD);
259 else
260 mangleUnscopedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000261 } else if (isa<FunctionDecl>(ND->getDeclContext()))
262 mangleLocalName(ND);
263 else
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000264 mangleNestedName(ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000265}
266
Anders Carlsson201ce742009-09-17 03:17:01 +0000267void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
268 // <unscoped-name> ::= <unqualified-name>
269 // ::= St <unqualified-name> # ::std::
270 if (isStdNamespace(ND->getDeclContext()))
271 Out << "St";
272
273 mangleUnqualifiedName(ND);
274}
275
276void CXXNameMangler::mangleUnscopedTemplateName(const FunctionDecl *FD) {
277 // <unscoped-template-name> ::= <unscoped-name>
278 // ::= <substitution>
279 mangleUnscopedName(FD);
280}
281
Mike Stump77ca8f62009-09-05 07:20:32 +0000282void CXXNameMangler::mangleCalloffset(int64_t nv, int64_t v) {
Mike Stump141c5af2009-09-02 00:25:38 +0000283 // <call-offset> ::= h <nv-offset> _
284 // ::= v <v-offset> _
285 // <nv-offset> ::= <offset number> # non-virtual base override
286 // <v-offset> ::= <offset nubmer> _ <virtual offset number>
287 // # virtual base override, with vcall offset
Mike Stump77ca8f62009-09-05 07:20:32 +0000288 if (v == 0) {
Mike Stump9124bcc2009-09-02 00:56:18 +0000289 Out << "h";
Mike Stump141c5af2009-09-02 00:25:38 +0000290 if (nv < 0) {
291 Out << "n";
292 nv = -nv;
293 }
294 Out << nv;
295 } else {
Mike Stump9124bcc2009-09-02 00:56:18 +0000296 Out << "v";
Mike Stump141c5af2009-09-02 00:25:38 +0000297 if (nv < 0) {
298 Out << "n";
299 nv = -nv;
300 }
301 Out << nv;
302 Out << "_";
303 if (v < 0) {
304 Out << "n";
305 v = -v;
306 }
307 Out << v;
308 }
309 Out << "_";
Mike Stump9124bcc2009-09-02 00:56:18 +0000310}
311
Mike Stumpdec025b2009-09-07 04:27:52 +0000312void CXXNameMangler::mangleThunk(const FunctionDecl *FD, int64_t nv,
313 int64_t v) {
Mike Stump9124bcc2009-09-02 00:56:18 +0000314 // <special-name> ::= T <call-offset> <base encoding>
315 // # base is the nominal target function of thunk
Mike Stumpdec025b2009-09-07 04:27:52 +0000316 Out << "_ZT";
Mike Stump77ca8f62009-09-05 07:20:32 +0000317 mangleCalloffset(nv, v);
Mike Stumpdec025b2009-09-07 04:27:52 +0000318 mangleFunctionEncoding(FD);
Mike Stump9124bcc2009-09-02 00:56:18 +0000319}
320
Mike Stumpdec025b2009-09-07 04:27:52 +0000321 void CXXNameMangler::mangleCovariantThunk(const FunctionDecl *FD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000322 int64_t nv_t, int64_t v_t,
Mike Stump9124bcc2009-09-02 00:56:18 +0000323 int64_t nv_r, int64_t v_r) {
324 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
325 // # base is the nominal target function of thunk
326 // # first call-offset is 'this' adjustment
327 // # second call-offset is result adjustment
Mike Stumpdec025b2009-09-07 04:27:52 +0000328 Out << "_ZTc";
Mike Stump77ca8f62009-09-05 07:20:32 +0000329 mangleCalloffset(nv_t, v_t);
330 mangleCalloffset(nv_r, v_r);
Mike Stumpdec025b2009-09-07 04:27:52 +0000331 mangleFunctionEncoding(FD);
Mike Stump141c5af2009-09-02 00:25:38 +0000332}
333
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000334void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) {
335 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000336 // ::= <ctor-dtor-name>
337 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000338 DeclarationName Name = ND->getDeclName();
339 switch (Name.getNameKind()) {
340 case DeclarationName::Identifier:
341 mangleSourceName(Name.getAsIdentifierInfo());
342 break;
343
344 case DeclarationName::ObjCZeroArgSelector:
345 case DeclarationName::ObjCOneArgSelector:
346 case DeclarationName::ObjCMultiArgSelector:
347 assert(false && "Can't mangle Objective-C selector names here!");
348 break;
349
350 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000351 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000352 // If the named decl is the C++ constructor we're mangling, use the type
353 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000354 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000355 else
356 // Otherwise, use the complete constructor name. This is relevant if a
357 // class with a constructor is declared within a constructor.
358 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000359 break;
360
361 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000362 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000363 // If the named decl is the C++ destructor we're mangling, use the type we
364 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000365 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
366 else
367 // Otherwise, use the complete destructor name. This is relevant if a
368 // class with a destructor is declared within a destructor.
369 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000370 break;
371
372 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000373 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000374 Out << "cv";
375 mangleType(Context.getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000376 break;
377
378 case DeclarationName::CXXOperatorName:
379 mangleOperatorName(Name.getCXXOverloadedOperator(),
380 cast<FunctionDecl>(ND)->getNumParams());
381 break;
382
383 case DeclarationName::CXXUsingDirective:
384 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000385 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000386 }
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000388 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000389 if (const TemplateArgumentList *TemplateArgs
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000390 = Function->getTemplateSpecializationArgs())
391 mangleTemplateArgumentList(*TemplateArgs);
392 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000393}
394
395void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
396 // <source-name> ::= <positive length number> <identifier>
397 // <number> ::= [n] <non-negative decimal integer>
398 // <identifier> ::= <unqualified source code identifier>
399 Out << II->getLength() << II->getName();
400}
401
402void CXXNameMangler::mangleNestedName(const NamedDecl *ND) {
403 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
404 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
405 // FIXME: no template support
406 Out << 'N';
407 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
408 mangleCVQualifiers(Method->getTypeQualifiers());
409 manglePrefix(ND->getDeclContext());
410 mangleUnqualifiedName(ND);
411 Out << 'E';
412}
413
Anders Carlsson1b42c792009-04-02 16:24:45 +0000414void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
415 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
416 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000417 // <discriminator> := _ <non-negative number>
Anders Carlsson1b42c792009-04-02 16:24:45 +0000418 Out << 'Z';
419 mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext()));
420 Out << 'E';
421 mangleSourceName(ND->getIdentifier());
422}
423
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000424void CXXNameMangler::manglePrefix(const DeclContext *DC) {
425 // <prefix> ::= <prefix> <unqualified-name>
426 // ::= <template-prefix> <template-args>
427 // ::= <template-param>
428 // ::= # empty
429 // ::= <substitution>
430 // FIXME: We only handle mangling of namespaces and classes at the moment.
Anders Carlssonc8dee9c2009-04-01 00:42:16 +0000431 if (!DC->getParent()->isTranslationUnit())
432 manglePrefix(DC->getParent());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000433
434 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(DC))
435 mangleSourceName(Namespace->getIdentifier());
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000436 else if (const RecordDecl *Record = dyn_cast<RecordDecl>(DC)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000437 if (const ClassTemplateSpecializationDecl *D =
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000438 dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
439 mangleType(QualType(D->getTypeForDecl(), 0));
440 } else
441 mangleSourceName(Record->getIdentifier());
442 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000443}
444
Mike Stump1eb44332009-09-09 15:08:12 +0000445void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000446CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
447 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000448 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000449 case OO_New: Out << "nw"; break;
450 // ::= na # new[]
451 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000452 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000453 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000454 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000455 case OO_Array_Delete: Out << "da"; break;
456 // ::= ps # + (unary)
457 // ::= pl # +
458 case OO_Plus: Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000459 // ::= ng # - (unary)
460 // ::= mi # -
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000461 case OO_Minus: Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000462 // ::= ad # & (unary)
463 // ::= an # &
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000464 case OO_Amp: Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000465 // ::= de # * (unary)
466 // ::= ml # *
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000467 case OO_Star: Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000468 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000469 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000470 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000471 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000472 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000473 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000474 // ::= or # |
475 case OO_Pipe: Out << "or"; break;
476 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000477 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000478 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000479 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000480 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000481 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000482 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000483 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000484 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000485 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000486 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000487 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000488 // ::= rM # %=
489 case OO_PercentEqual: Out << "rM"; break;
490 // ::= aN # &=
491 case OO_AmpEqual: Out << "aN"; break;
492 // ::= oR # |=
493 case OO_PipeEqual: Out << "oR"; break;
494 // ::= eO # ^=
495 case OO_CaretEqual: Out << "eO"; break;
496 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000497 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000498 // ::= rs # >>
499 case OO_GreaterGreater: Out << "rs"; break;
500 // ::= lS # <<=
501 case OO_LessLessEqual: Out << "lS"; break;
502 // ::= rS # >>=
503 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000504 // ::= eq # ==
505 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000506 // ::= ne # !=
507 case OO_ExclaimEqual: Out << "ne"; break;
508 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000509 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000510 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000511 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000512 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000513 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000514 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000515 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000516 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000517 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000518 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000519 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000520 // ::= oo # ||
521 case OO_PipePipe: Out << "oo"; break;
522 // ::= pp # ++
523 case OO_PlusPlus: Out << "pp"; break;
524 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000525 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000526 // ::= cm # ,
527 case OO_Comma: Out << "cm"; break;
528 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000529 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000530 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000531 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000532 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000533 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000534 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000535 case OO_Subscript: Out << "ix"; break;
536 // UNSUPPORTED: ::= qu # ?
537
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000538 case OO_None:
539 case OO_Conditional:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000540 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +0000541 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000542 break;
543 }
544}
545
546void CXXNameMangler::mangleCVQualifiers(unsigned Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +0000547 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000548 if (Quals & QualType::Restrict)
549 Out << 'r';
550 if (Quals & QualType::Volatile)
551 Out << 'V';
552 if (Quals & QualType::Const)
553 Out << 'K';
554}
555
556void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000557 // Only operate on the canonical type!
558 T = Context.getCanonicalType(T);
559
Anders Carlsson76967372009-09-17 00:43:46 +0000560 bool IsSubstitutable = !isa<BuiltinType>(T);
561 if (IsSubstitutable && mangleSubstitution(T))
562 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000563
Anders Carlsson76967372009-09-17 00:43:46 +0000564 if (unsigned CVRQualifiers = T.getCVRQualifiers()) {
565 // <type> ::= <CV-qualifiers> <type>
566 mangleCVQualifiers(CVRQualifiers);
567
568 mangleType(T.getUnqualifiedType());
569 } else {
570 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +0000571#define ABSTRACT_TYPE(CLASS, PARENT)
572#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000573 case Type::CLASS: \
574 llvm::llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
575 return;
John McCallefe6aee2009-09-05 07:56:18 +0000576#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000577 case Type::CLASS: \
578 mangleType(static_cast<CLASS##Type*>(T.getTypePtr())); \
579 break;
John McCallefe6aee2009-09-05 07:56:18 +0000580#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +0000581 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000582 }
Anders Carlsson76967372009-09-17 00:43:46 +0000583
584 // Add the substitution.
585 if (IsSubstitutable)
586 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000587}
588
589void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +0000590 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000591 // <builtin-type> ::= v # void
592 // ::= w # wchar_t
593 // ::= b # bool
594 // ::= c # char
595 // ::= a # signed char
596 // ::= h # unsigned char
597 // ::= s # short
598 // ::= t # unsigned short
599 // ::= i # int
600 // ::= j # unsigned int
601 // ::= l # long
602 // ::= m # unsigned long
603 // ::= x # long long, __int64
604 // ::= y # unsigned long long, __int64
605 // ::= n # __int128
606 // UNSUPPORTED: ::= o # unsigned __int128
607 // ::= f # float
608 // ::= d # double
609 // ::= e # long double, __float80
610 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000611 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
612 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
613 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
614 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000615 // ::= Di # char32_t
616 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000617 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000618 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
619 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000620 switch (T->getKind()) {
621 case BuiltinType::Void: Out << 'v'; break;
622 case BuiltinType::Bool: Out << 'b'; break;
623 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
624 case BuiltinType::UChar: Out << 'h'; break;
625 case BuiltinType::UShort: Out << 't'; break;
626 case BuiltinType::UInt: Out << 'j'; break;
627 case BuiltinType::ULong: Out << 'm'; break;
628 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000629 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000630 case BuiltinType::SChar: Out << 'a'; break;
631 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000632 case BuiltinType::Char16: Out << "Ds"; break;
633 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000634 case BuiltinType::Short: Out << 's'; break;
635 case BuiltinType::Int: Out << 'i'; break;
636 case BuiltinType::Long: Out << 'l'; break;
637 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000638 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000639 case BuiltinType::Float: Out << 'f'; break;
640 case BuiltinType::Double: Out << 'd'; break;
641 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000642 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000643
644 case BuiltinType::Overload:
645 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +0000646 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000647 "Overloaded and dependent types shouldn't get to name mangling");
648 break;
Anders Carlssone89d1592009-06-26 18:41:36 +0000649 case BuiltinType::UndeducedAuto:
650 assert(0 && "Should not see undeduced auto here");
651 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +0000652 case BuiltinType::ObjCId: Out << "11objc_object"; break;
653 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000654 }
655}
656
John McCallefe6aee2009-09-05 07:56:18 +0000657// <type> ::= <function-type>
658// <function-type> ::= F [Y] <bare-function-type> E
659void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000660 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +0000661 // FIXME: We don't have enough information in the AST to produce the 'Y'
662 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000663 mangleBareFunctionType(T, /*MangleReturnType=*/true);
664 Out << 'E';
665}
John McCallefe6aee2009-09-05 07:56:18 +0000666void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
667 llvm::llvm_unreachable("Can't mangle K&R function prototypes");
668}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000669void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
670 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +0000671 // We should never be mangling something without a prototype.
672 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
673
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000674 // <bare-function-type> ::= <signature type>+
675 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +0000676 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000677
Anders Carlssonc6c91bc2009-04-01 00:15:23 +0000678 if (Proto->getNumArgs() == 0) {
679 Out << 'v';
680 return;
681 }
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Douglas Gregor72564e72009-02-26 23:50:07 +0000683 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000684 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000685 Arg != ArgEnd; ++Arg)
686 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +0000687
688 // <builtin-type> ::= z # ellipsis
689 if (Proto->isVariadic())
690 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000691}
692
John McCallefe6aee2009-09-05 07:56:18 +0000693// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +0000694// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +0000695void CXXNameMangler::mangleType(const EnumType *T) {
696 mangleType(static_cast<const TagType*>(T));
697}
698void CXXNameMangler::mangleType(const RecordType *T) {
699 mangleType(static_cast<const TagType*>(T));
700}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000701void CXXNameMangler::mangleType(const TagType *T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000702 if (!T->getDecl()->getIdentifier())
703 mangleName(T->getDecl()->getTypedefForAnonDecl());
704 else
705 mangleName(T->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Mike Stump141c5af2009-09-02 00:25:38 +0000707 // If this is a class template specialization, mangle the template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000708 if (ClassTemplateSpecializationDecl *Spec
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000709 = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl()))
710 mangleTemplateArgumentList(Spec->getTemplateArgs());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000711}
712
John McCallefe6aee2009-09-05 07:56:18 +0000713// <type> ::= <array-type>
714// <array-type> ::= A <positive dimension number> _ <element type>
715// ::= A [<dimension expression>] _ <element type>
716void CXXNameMangler::mangleType(const ConstantArrayType *T) {
717 Out << 'A' << T->getSize() << '_';
718 mangleType(T->getElementType());
719}
720void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000721 Out << 'A';
John McCallefe6aee2009-09-05 07:56:18 +0000722 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000723 Out << '_';
724 mangleType(T->getElementType());
725}
John McCallefe6aee2009-09-05 07:56:18 +0000726void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
727 Out << 'A';
728 mangleExpression(T->getSizeExpr());
729 Out << '_';
730 mangleType(T->getElementType());
731}
732void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
733 Out << 'A' << '_';
734 mangleType(T->getElementType());
735}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000736
John McCallefe6aee2009-09-05 07:56:18 +0000737// <type> ::= <pointer-to-member-type>
738// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000739void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000740 Out << 'M';
741 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +0000742 QualType PointeeType = T->getPointeeType();
743 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
744 mangleCVQualifiers(FPT->getTypeQuals());
745 mangleType(FPT);
Mike Stump1eb44332009-09-09 15:08:12 +0000746 } else
Anders Carlsson0e650012009-05-17 17:41:20 +0000747 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000748}
749
John McCallefe6aee2009-09-05 07:56:18 +0000750// <type> ::= <template-param>
751// <template-param> ::= T_ # first template parameter
752// ::= T <parameter-2 non-negative number> _
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000753void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000754 if (T->getIndex() == 0)
755 Out << "T_";
756 else
757 Out << 'T' << (T->getIndex() - 1) << '_';
758}
759
John McCallefe6aee2009-09-05 07:56:18 +0000760// FIXME: <type> ::= <template-template-param> <template-args>
John McCallefe6aee2009-09-05 07:56:18 +0000761
762// <type> ::= P <type> # pointer-to
763void CXXNameMangler::mangleType(const PointerType *T) {
764 Out << 'P';
765 mangleType(T->getPointeeType());
766}
767void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
768 Out << 'P';
769 mangleType(T->getPointeeType());
770}
771
772// <type> ::= R <type> # reference-to
773void CXXNameMangler::mangleType(const LValueReferenceType *T) {
774 Out << 'R';
775 mangleType(T->getPointeeType());
776}
777
778// <type> ::= O <type> # rvalue reference-to (C++0x)
779void CXXNameMangler::mangleType(const RValueReferenceType *T) {
780 Out << 'O';
781 mangleType(T->getPointeeType());
782}
783
784// <type> ::= C <type> # complex pair (C 2000)
785void CXXNameMangler::mangleType(const ComplexType *T) {
786 Out << 'C';
787 mangleType(T->getElementType());
788}
789
790// GNU extension: vector types
791void CXXNameMangler::mangleType(const VectorType *T) {
792 Out << "U8__vector";
793 mangleType(T->getElementType());
794}
795void CXXNameMangler::mangleType(const ExtVectorType *T) {
796 mangleType(static_cast<const VectorType*>(T));
797}
798void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
799 Out << "U8__vector";
800 mangleType(T->getElementType());
801}
802
Anders Carlssona40c5e42009-03-07 22:03:21 +0000803void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
804 mangleSourceName(T->getDecl()->getIdentifier());
805}
806
John McCallefe6aee2009-09-05 07:56:18 +0000807void CXXNameMangler::mangleType(const BlockPointerType *T) {
808 assert(false && "can't mangle block pointer types yet");
809}
810
811void CXXNameMangler::mangleType(const FixedWidthIntType *T) {
812 assert(false && "can't mangle arbitary-precision integer type yet");
813}
814
815void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
816 // TSTs are never canonical unless they're dependent.
817 assert(false && "can't mangle dependent template specializations yet");
818}
819
820void CXXNameMangler::mangleType(const TypenameType *T) {
821 assert(false && "can't mangle dependent typenames yet");
822}
823
824// FIXME: For now, just drop all extension qualifiers on the floor.
825void CXXNameMangler::mangleType(const ExtQualType *T) {
826 mangleType(QualType(T->getBaseType(), 0));
827}
828
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000829void CXXNameMangler::mangleExpression(Expr *E) {
830 assert(false && "Cannot mangle expressions yet");
831}
832
John McCallefe6aee2009-09-05 07:56:18 +0000833// FIXME: <type> ::= G <type> # imaginary (C 2000)
834// FIXME: <type> ::= U <source-name> <type> # vendor extended type qualifier
835
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000836void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
837 // <ctor-dtor-name> ::= C1 # complete object constructor
838 // ::= C2 # base object constructor
839 // ::= C3 # complete object allocating constructor
840 //
841 switch (T) {
842 case Ctor_Complete:
843 Out << "C1";
844 break;
845 case Ctor_Base:
846 Out << "C2";
847 break;
848 case Ctor_CompleteAllocating:
849 Out << "C3";
850 break;
851 }
852}
853
Anders Carlsson27ae5362009-04-17 01:58:57 +0000854void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
855 // <ctor-dtor-name> ::= D0 # deleting destructor
856 // ::= D1 # complete object destructor
857 // ::= D2 # base object destructor
858 //
859 switch (T) {
860 case Dtor_Deleting:
861 Out << "D0";
862 break;
863 case Dtor_Complete:
864 Out << "D1";
865 break;
866 case Dtor_Base:
867 Out << "D2";
868 break;
869 }
870}
871
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000872void CXXNameMangler::mangleTemplateArgumentList(const TemplateArgumentList &L) {
873 // <template-args> ::= I <template-arg>+ E
874 Out << "I";
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000876 for (unsigned i = 0, e = L.size(); i != e; ++i) {
877 const TemplateArgument &A = L[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000879 mangleTemplateArgument(A);
880 }
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000882 Out << "E";
883}
884
885void CXXNameMangler::mangleTemplateArgument(const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +0000886 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000887 // ::= X <expression> E # expression
888 // ::= <expr-primary> # simple expressions
889 // ::= I <template-arg>* E # argument pack
890 // ::= sp <expression> # pack expansion of (C++0x)
891 switch (A.getKind()) {
892 default:
893 assert(0 && "Unknown template argument kind!");
894 case TemplateArgument::Type:
895 mangleType(A.getAsType());
896 break;
897 case TemplateArgument::Integral:
898 // <expr-primary> ::= L <type> <value number> E # integer literal
899
900 Out << 'L';
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000902 mangleType(A.getIntegralType());
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000904 const llvm::APSInt *Integral = A.getAsIntegral();
905 if (A.getIntegralType()->isBooleanType()) {
906 // Boolean values are encoded as 0/1.
907 Out << (Integral->getBoolValue() ? '1' : '0');
908 } else {
909 if (Integral->isNegative())
910 Out << 'n';
911 Integral->abs().print(Out, false);
912 }
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000914 Out << 'E';
915 break;
916 }
917}
918
Anders Carlsson76967372009-09-17 00:43:46 +0000919// <substitution> ::= S <seq-id> _
920// ::= S_
921bool CXXNameMangler::mangleSubstitution(QualType T) {
922 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
923
Anders Carlssond3a932a2009-09-17 03:53:28 +0000924 return mangleSubstitution(TypePtr);
925}
926
927bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Anders Carlsson76967372009-09-17 00:43:46 +0000928 llvm::DenseMap<uintptr_t, unsigned>::iterator I =
Anders Carlssond3a932a2009-09-17 03:53:28 +0000929 Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +0000930 if (I == Substitutions.end())
931 return false;
932
933 unsigned SeqID = I->second;
934 if (SeqID == 0)
935 Out << "S_";
936 else {
937 SeqID--;
938
939 // <seq-id> is encoded in base-36, using digits and upper case letters.
940 char Buffer[10];
941 char *BufferPtr = Buffer + 9;
942
943 *BufferPtr = 0;
944 if (SeqID == 0) *--BufferPtr = '0';
945
946 while (SeqID) {
947 assert(BufferPtr > Buffer && "Buffer overflow!");
948
949 unsigned char c = static_cast<unsigned char>(SeqID) % 36;
950
951 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
952 SeqID /= 36;
953 }
954
955 Out << 'S' << BufferPtr << '_';
956 }
957
958 return true;
959}
960
961void CXXNameMangler::addSubstitution(QualType T) {
962 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +0000963 addSubstitution(TypePtr);
964}
965
966void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlsson76967372009-09-17 00:43:46 +0000967 unsigned SeqID = Substitutions.size();
968
Anders Carlssond3a932a2009-09-17 03:53:28 +0000969 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
970 Substitutions[Ptr] = SeqID;
Anders Carlsson76967372009-09-17 00:43:46 +0000971}
972
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000973namespace clang {
Mike Stump141c5af2009-09-02 00:25:38 +0000974 /// \brief Mangles the name of the declaration D and emits that name to the
975 /// given output stream.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000976 ///
Mike Stump141c5af2009-09-02 00:25:38 +0000977 /// If the declaration D requires a mangled name, this routine will emit that
978 /// mangled name to \p os and return true. Otherwise, \p os will be unchanged
979 /// and this routine will return false. In this case, the caller should just
980 /// emit the identifier of the declaration (\c D->getIdentifier()) as its
981 /// name.
Mike Stump1eb44332009-09-09 15:08:12 +0000982 bool mangleName(const NamedDecl *D, ASTContext &Context,
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000983 llvm::raw_ostream &os) {
Anders Carlsson578aa642009-05-03 16:51:04 +0000984 assert(!isa<CXXConstructorDecl>(D) &&
985 "Use mangleCXXCtor for constructor decls!");
986 assert(!isa<CXXDestructorDecl>(D) &&
987 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000989 CXXNameMangler Mangler(Context, os);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000990 if (!Mangler.mangle(D))
991 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Douglas Gregor6ec36682009-02-18 23:53:56 +0000993 os.flush();
994 return true;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000995 }
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Mike Stump141c5af2009-09-02 00:25:38 +0000997 /// \brief Mangles the a thunk with the offset n for the declaration D and
998 /// emits that name to the given output stream.
Mike Stumpdec025b2009-09-07 04:27:52 +0000999 void mangleThunk(const FunctionDecl *FD, int64_t nv, int64_t v,
Mike Stump883f1272009-09-02 00:28:47 +00001000 ASTContext &Context, llvm::raw_ostream &os) {
Mike Stump141c5af2009-09-02 00:25:38 +00001001 // FIXME: Hum, we might have to thunk these, fix.
Mike Stumpdec025b2009-09-07 04:27:52 +00001002 assert(!isa<CXXDestructorDecl>(FD) &&
Mike Stump141c5af2009-09-02 00:25:38 +00001003 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Mike Stump141c5af2009-09-02 00:25:38 +00001005 CXXNameMangler Mangler(Context, os);
Mike Stumpdec025b2009-09-07 04:27:52 +00001006 Mangler.mangleThunk(FD, nv, v);
Mike Stump141c5af2009-09-02 00:25:38 +00001007 os.flush();
1008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Mike Stump9124bcc2009-09-02 00:56:18 +00001010 /// \brief Mangles the a covariant thunk for the declaration D and emits that
1011 /// name to the given output stream.
Mike Stumpdec025b2009-09-07 04:27:52 +00001012 void mangleCovariantThunk(const FunctionDecl *FD, int64_t nv_t, int64_t v_t,
Mike Stump77ca8f62009-09-05 07:20:32 +00001013 int64_t nv_r, int64_t v_r, ASTContext &Context,
Mike Stump9124bcc2009-09-02 00:56:18 +00001014 llvm::raw_ostream &os) {
1015 // FIXME: Hum, we might have to thunk these, fix.
Mike Stumpdec025b2009-09-07 04:27:52 +00001016 assert(!isa<CXXDestructorDecl>(FD) &&
Mike Stump9124bcc2009-09-02 00:56:18 +00001017 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Mike Stump9124bcc2009-09-02 00:56:18 +00001019 CXXNameMangler Mangler(Context, os);
Mike Stumpdec025b2009-09-07 04:27:52 +00001020 Mangler.mangleCovariantThunk(FD, nv_t, v_t, nv_r, v_r);
Mike Stump9124bcc2009-09-02 00:56:18 +00001021 os.flush();
1022 }
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001024 /// mangleGuardVariable - Returns the mangled name for a guard variable
1025 /// for the passed in VarDecl.
Anders Carlsson41aa8c12009-04-13 18:02:10 +00001026 void mangleGuardVariable(const VarDecl *D, ASTContext &Context,
1027 llvm::raw_ostream &os) {
1028 CXXNameMangler Mangler(Context, os);
1029 Mangler.mangleGuardVariable(D);
1030
1031 os.flush();
1032 }
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001034 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
1035 ASTContext &Context, llvm::raw_ostream &os) {
1036 CXXNameMangler Mangler(Context, os);
1037 Mangler.mangleCXXCtor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001039 os.flush();
1040 }
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Anders Carlsson27ae5362009-04-17 01:58:57 +00001042 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
1043 ASTContext &Context, llvm::raw_ostream &os) {
1044 CXXNameMangler Mangler(Context, os);
1045 Mangler.mangleCXXDtor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Anders Carlsson27ae5362009-04-17 01:58:57 +00001047 os.flush();
1048 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001049
Mike Stumpf1216772009-07-31 18:25:34 +00001050 void mangleCXXVtable(QualType Type, ASTContext &Context,
1051 llvm::raw_ostream &os) {
1052 CXXNameMangler Mangler(Context, os);
1053 Mangler.mangleCXXVtable(Type);
1054
1055 os.flush();
1056 }
Mike Stump738f8c22009-07-31 23:15:31 +00001057
1058 void mangleCXXRtti(QualType Type, ASTContext &Context,
1059 llvm::raw_ostream &os) {
1060 CXXNameMangler Mangler(Context, os);
1061 Mangler.mangleCXXRtti(Type);
1062
1063 os.flush();
1064 }
Mike Stumpf1216772009-07-31 18:25:34 +00001065}