blob: 9b5fd74ad5151f4497821d74eb7c624cda67015d [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>
Anders Carlsson03c9d532009-09-17 04:02:31 +0000279 if (mangleSubstitution(FD))
280 return;
281
Anders Carlsson201ce742009-09-17 03:17:01 +0000282 mangleUnscopedName(FD);
Anders Carlsson03c9d532009-09-17 04:02:31 +0000283 addSubstitution(FD);
Anders Carlsson201ce742009-09-17 03:17:01 +0000284}
285
Mike Stump77ca8f62009-09-05 07:20:32 +0000286void CXXNameMangler::mangleCalloffset(int64_t nv, int64_t v) {
Mike Stump141c5af2009-09-02 00:25:38 +0000287 // <call-offset> ::= h <nv-offset> _
288 // ::= v <v-offset> _
289 // <nv-offset> ::= <offset number> # non-virtual base override
290 // <v-offset> ::= <offset nubmer> _ <virtual offset number>
291 // # virtual base override, with vcall offset
Mike Stump77ca8f62009-09-05 07:20:32 +0000292 if (v == 0) {
Mike Stump9124bcc2009-09-02 00:56:18 +0000293 Out << "h";
Mike Stump141c5af2009-09-02 00:25:38 +0000294 if (nv < 0) {
295 Out << "n";
296 nv = -nv;
297 }
298 Out << nv;
299 } else {
Mike Stump9124bcc2009-09-02 00:56:18 +0000300 Out << "v";
Mike Stump141c5af2009-09-02 00:25:38 +0000301 if (nv < 0) {
302 Out << "n";
303 nv = -nv;
304 }
305 Out << nv;
306 Out << "_";
307 if (v < 0) {
308 Out << "n";
309 v = -v;
310 }
311 Out << v;
312 }
313 Out << "_";
Mike Stump9124bcc2009-09-02 00:56:18 +0000314}
315
Mike Stumpdec025b2009-09-07 04:27:52 +0000316void CXXNameMangler::mangleThunk(const FunctionDecl *FD, int64_t nv,
317 int64_t v) {
Mike Stump9124bcc2009-09-02 00:56:18 +0000318 // <special-name> ::= T <call-offset> <base encoding>
319 // # base is the nominal target function of thunk
Mike Stumpdec025b2009-09-07 04:27:52 +0000320 Out << "_ZT";
Mike Stump77ca8f62009-09-05 07:20:32 +0000321 mangleCalloffset(nv, v);
Mike Stumpdec025b2009-09-07 04:27:52 +0000322 mangleFunctionEncoding(FD);
Mike Stump9124bcc2009-09-02 00:56:18 +0000323}
324
Mike Stumpdec025b2009-09-07 04:27:52 +0000325 void CXXNameMangler::mangleCovariantThunk(const FunctionDecl *FD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000326 int64_t nv_t, int64_t v_t,
Mike Stump9124bcc2009-09-02 00:56:18 +0000327 int64_t nv_r, int64_t v_r) {
328 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
329 // # base is the nominal target function of thunk
330 // # first call-offset is 'this' adjustment
331 // # second call-offset is result adjustment
Mike Stumpdec025b2009-09-07 04:27:52 +0000332 Out << "_ZTc";
Mike Stump77ca8f62009-09-05 07:20:32 +0000333 mangleCalloffset(nv_t, v_t);
334 mangleCalloffset(nv_r, v_r);
Mike Stumpdec025b2009-09-07 04:27:52 +0000335 mangleFunctionEncoding(FD);
Mike Stump141c5af2009-09-02 00:25:38 +0000336}
337
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000338void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) {
339 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000340 // ::= <ctor-dtor-name>
341 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000342 DeclarationName Name = ND->getDeclName();
343 switch (Name.getNameKind()) {
344 case DeclarationName::Identifier:
345 mangleSourceName(Name.getAsIdentifierInfo());
346 break;
347
348 case DeclarationName::ObjCZeroArgSelector:
349 case DeclarationName::ObjCOneArgSelector:
350 case DeclarationName::ObjCMultiArgSelector:
351 assert(false && "Can't mangle Objective-C selector names here!");
352 break;
353
354 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000355 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000356 // If the named decl is the C++ constructor we're mangling, use the type
357 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000358 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000359 else
360 // Otherwise, use the complete constructor name. This is relevant if a
361 // class with a constructor is declared within a constructor.
362 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000363 break;
364
365 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000366 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000367 // If the named decl is the C++ destructor we're mangling, use the type we
368 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000369 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
370 else
371 // Otherwise, use the complete destructor name. This is relevant if a
372 // class with a destructor is declared within a destructor.
373 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000374 break;
375
376 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000377 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000378 Out << "cv";
379 mangleType(Context.getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000380 break;
381
382 case DeclarationName::CXXOperatorName:
383 mangleOperatorName(Name.getCXXOverloadedOperator(),
384 cast<FunctionDecl>(ND)->getNumParams());
385 break;
386
387 case DeclarationName::CXXUsingDirective:
388 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000389 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000392 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000393 if (const TemplateArgumentList *TemplateArgs
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000394 = Function->getTemplateSpecializationArgs())
395 mangleTemplateArgumentList(*TemplateArgs);
396 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000397}
398
399void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
400 // <source-name> ::= <positive length number> <identifier>
401 // <number> ::= [n] <non-negative decimal integer>
402 // <identifier> ::= <unqualified source code identifier>
403 Out << II->getLength() << II->getName();
404}
405
406void CXXNameMangler::mangleNestedName(const NamedDecl *ND) {
407 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
408 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
409 // FIXME: no template support
410 Out << 'N';
411 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
412 mangleCVQualifiers(Method->getTypeQualifiers());
413 manglePrefix(ND->getDeclContext());
414 mangleUnqualifiedName(ND);
415 Out << 'E';
416}
417
Anders Carlsson1b42c792009-04-02 16:24:45 +0000418void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
419 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
420 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000421 // <discriminator> := _ <non-negative number>
Anders Carlsson1b42c792009-04-02 16:24:45 +0000422 Out << 'Z';
423 mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext()));
424 Out << 'E';
425 mangleSourceName(ND->getIdentifier());
426}
427
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000428void CXXNameMangler::manglePrefix(const DeclContext *DC) {
429 // <prefix> ::= <prefix> <unqualified-name>
430 // ::= <template-prefix> <template-args>
431 // ::= <template-param>
432 // ::= # empty
433 // ::= <substitution>
434 // FIXME: We only handle mangling of namespaces and classes at the moment.
Anders Carlssonc8dee9c2009-04-01 00:42:16 +0000435 if (!DC->getParent()->isTranslationUnit())
436 manglePrefix(DC->getParent());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000437
438 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(DC))
439 mangleSourceName(Namespace->getIdentifier());
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000440 else if (const RecordDecl *Record = dyn_cast<RecordDecl>(DC)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000441 if (const ClassTemplateSpecializationDecl *D =
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000442 dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
443 mangleType(QualType(D->getTypeForDecl(), 0));
444 } else
445 mangleSourceName(Record->getIdentifier());
446 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000447}
448
Mike Stump1eb44332009-09-09 15:08:12 +0000449void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000450CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
451 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000452 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000453 case OO_New: Out << "nw"; break;
454 // ::= na # new[]
455 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000456 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000457 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000458 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000459 case OO_Array_Delete: Out << "da"; break;
460 // ::= ps # + (unary)
461 // ::= pl # +
462 case OO_Plus: Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000463 // ::= ng # - (unary)
464 // ::= mi # -
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000465 case OO_Minus: Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000466 // ::= ad # & (unary)
467 // ::= an # &
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000468 case OO_Amp: Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000469 // ::= de # * (unary)
470 // ::= ml # *
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000471 case OO_Star: Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000472 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000473 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000474 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000475 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000476 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000477 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000478 // ::= or # |
479 case OO_Pipe: Out << "or"; break;
480 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000481 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000482 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000483 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000484 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000485 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000486 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000487 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000488 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000489 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000490 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000491 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000492 // ::= rM # %=
493 case OO_PercentEqual: Out << "rM"; break;
494 // ::= aN # &=
495 case OO_AmpEqual: Out << "aN"; break;
496 // ::= oR # |=
497 case OO_PipeEqual: Out << "oR"; break;
498 // ::= eO # ^=
499 case OO_CaretEqual: Out << "eO"; break;
500 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000501 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000502 // ::= rs # >>
503 case OO_GreaterGreater: Out << "rs"; break;
504 // ::= lS # <<=
505 case OO_LessLessEqual: Out << "lS"; break;
506 // ::= rS # >>=
507 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000508 // ::= eq # ==
509 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000510 // ::= ne # !=
511 case OO_ExclaimEqual: Out << "ne"; break;
512 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000513 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000514 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000515 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000516 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000517 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000518 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000519 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000520 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000521 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000522 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000523 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000524 // ::= oo # ||
525 case OO_PipePipe: Out << "oo"; break;
526 // ::= pp # ++
527 case OO_PlusPlus: Out << "pp"; break;
528 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000529 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000530 // ::= cm # ,
531 case OO_Comma: Out << "cm"; break;
532 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000533 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000534 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000535 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000536 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000537 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000538 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000539 case OO_Subscript: Out << "ix"; break;
540 // UNSUPPORTED: ::= qu # ?
541
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000542 case OO_None:
543 case OO_Conditional:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000544 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +0000545 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000546 break;
547 }
548}
549
550void CXXNameMangler::mangleCVQualifiers(unsigned Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +0000551 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000552 if (Quals & QualType::Restrict)
553 Out << 'r';
554 if (Quals & QualType::Volatile)
555 Out << 'V';
556 if (Quals & QualType::Const)
557 Out << 'K';
558}
559
560void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000561 // Only operate on the canonical type!
562 T = Context.getCanonicalType(T);
563
Anders Carlsson76967372009-09-17 00:43:46 +0000564 bool IsSubstitutable = !isa<BuiltinType>(T);
565 if (IsSubstitutable && mangleSubstitution(T))
566 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000567
Anders Carlsson76967372009-09-17 00:43:46 +0000568 if (unsigned CVRQualifiers = T.getCVRQualifiers()) {
569 // <type> ::= <CV-qualifiers> <type>
570 mangleCVQualifiers(CVRQualifiers);
571
572 mangleType(T.getUnqualifiedType());
573 } else {
574 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +0000575#define ABSTRACT_TYPE(CLASS, PARENT)
576#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000577 case Type::CLASS: \
578 llvm::llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
579 return;
John McCallefe6aee2009-09-05 07:56:18 +0000580#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000581 case Type::CLASS: \
582 mangleType(static_cast<CLASS##Type*>(T.getTypePtr())); \
583 break;
John McCallefe6aee2009-09-05 07:56:18 +0000584#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +0000585 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000586 }
Anders Carlsson76967372009-09-17 00:43:46 +0000587
588 // Add the substitution.
589 if (IsSubstitutable)
590 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000591}
592
593void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +0000594 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000595 // <builtin-type> ::= v # void
596 // ::= w # wchar_t
597 // ::= b # bool
598 // ::= c # char
599 // ::= a # signed char
600 // ::= h # unsigned char
601 // ::= s # short
602 // ::= t # unsigned short
603 // ::= i # int
604 // ::= j # unsigned int
605 // ::= l # long
606 // ::= m # unsigned long
607 // ::= x # long long, __int64
608 // ::= y # unsigned long long, __int64
609 // ::= n # __int128
610 // UNSUPPORTED: ::= o # unsigned __int128
611 // ::= f # float
612 // ::= d # double
613 // ::= e # long double, __float80
614 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000615 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
616 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
617 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
618 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000619 // ::= Di # char32_t
620 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000621 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000622 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
623 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000624 switch (T->getKind()) {
625 case BuiltinType::Void: Out << 'v'; break;
626 case BuiltinType::Bool: Out << 'b'; break;
627 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
628 case BuiltinType::UChar: Out << 'h'; break;
629 case BuiltinType::UShort: Out << 't'; break;
630 case BuiltinType::UInt: Out << 'j'; break;
631 case BuiltinType::ULong: Out << 'm'; break;
632 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000633 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000634 case BuiltinType::SChar: Out << 'a'; break;
635 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000636 case BuiltinType::Char16: Out << "Ds"; break;
637 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000638 case BuiltinType::Short: Out << 's'; break;
639 case BuiltinType::Int: Out << 'i'; break;
640 case BuiltinType::Long: Out << 'l'; break;
641 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000642 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000643 case BuiltinType::Float: Out << 'f'; break;
644 case BuiltinType::Double: Out << 'd'; break;
645 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000646 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000647
648 case BuiltinType::Overload:
649 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +0000650 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000651 "Overloaded and dependent types shouldn't get to name mangling");
652 break;
Anders Carlssone89d1592009-06-26 18:41:36 +0000653 case BuiltinType::UndeducedAuto:
654 assert(0 && "Should not see undeduced auto here");
655 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +0000656 case BuiltinType::ObjCId: Out << "11objc_object"; break;
657 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000658 }
659}
660
John McCallefe6aee2009-09-05 07:56:18 +0000661// <type> ::= <function-type>
662// <function-type> ::= F [Y] <bare-function-type> E
663void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000664 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +0000665 // FIXME: We don't have enough information in the AST to produce the 'Y'
666 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000667 mangleBareFunctionType(T, /*MangleReturnType=*/true);
668 Out << 'E';
669}
John McCallefe6aee2009-09-05 07:56:18 +0000670void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
671 llvm::llvm_unreachable("Can't mangle K&R function prototypes");
672}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000673void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
674 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +0000675 // We should never be mangling something without a prototype.
676 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
677
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000678 // <bare-function-type> ::= <signature type>+
679 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +0000680 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000681
Anders Carlssonc6c91bc2009-04-01 00:15:23 +0000682 if (Proto->getNumArgs() == 0) {
683 Out << 'v';
684 return;
685 }
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Douglas Gregor72564e72009-02-26 23:50:07 +0000687 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000688 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000689 Arg != ArgEnd; ++Arg)
690 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +0000691
692 // <builtin-type> ::= z # ellipsis
693 if (Proto->isVariadic())
694 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000695}
696
John McCallefe6aee2009-09-05 07:56:18 +0000697// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +0000698// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +0000699void CXXNameMangler::mangleType(const EnumType *T) {
700 mangleType(static_cast<const TagType*>(T));
701}
702void CXXNameMangler::mangleType(const RecordType *T) {
703 mangleType(static_cast<const TagType*>(T));
704}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000705void CXXNameMangler::mangleType(const TagType *T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000706 if (!T->getDecl()->getIdentifier())
707 mangleName(T->getDecl()->getTypedefForAnonDecl());
708 else
709 mangleName(T->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Mike Stump141c5af2009-09-02 00:25:38 +0000711 // If this is a class template specialization, mangle the template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000712 if (ClassTemplateSpecializationDecl *Spec
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000713 = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl()))
714 mangleTemplateArgumentList(Spec->getTemplateArgs());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000715}
716
John McCallefe6aee2009-09-05 07:56:18 +0000717// <type> ::= <array-type>
718// <array-type> ::= A <positive dimension number> _ <element type>
719// ::= A [<dimension expression>] _ <element type>
720void CXXNameMangler::mangleType(const ConstantArrayType *T) {
721 Out << 'A' << T->getSize() << '_';
722 mangleType(T->getElementType());
723}
724void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000725 Out << 'A';
John McCallefe6aee2009-09-05 07:56:18 +0000726 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000727 Out << '_';
728 mangleType(T->getElementType());
729}
John McCallefe6aee2009-09-05 07:56:18 +0000730void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
731 Out << 'A';
732 mangleExpression(T->getSizeExpr());
733 Out << '_';
734 mangleType(T->getElementType());
735}
736void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
737 Out << 'A' << '_';
738 mangleType(T->getElementType());
739}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000740
John McCallefe6aee2009-09-05 07:56:18 +0000741// <type> ::= <pointer-to-member-type>
742// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000743void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000744 Out << 'M';
745 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +0000746 QualType PointeeType = T->getPointeeType();
747 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
748 mangleCVQualifiers(FPT->getTypeQuals());
749 mangleType(FPT);
Mike Stump1eb44332009-09-09 15:08:12 +0000750 } else
Anders Carlsson0e650012009-05-17 17:41:20 +0000751 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000752}
753
John McCallefe6aee2009-09-05 07:56:18 +0000754// <type> ::= <template-param>
755// <template-param> ::= T_ # first template parameter
756// ::= T <parameter-2 non-negative number> _
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000757void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000758 if (T->getIndex() == 0)
759 Out << "T_";
760 else
761 Out << 'T' << (T->getIndex() - 1) << '_';
762}
763
John McCallefe6aee2009-09-05 07:56:18 +0000764// FIXME: <type> ::= <template-template-param> <template-args>
John McCallefe6aee2009-09-05 07:56:18 +0000765
766// <type> ::= P <type> # pointer-to
767void CXXNameMangler::mangleType(const PointerType *T) {
768 Out << 'P';
769 mangleType(T->getPointeeType());
770}
771void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
772 Out << 'P';
773 mangleType(T->getPointeeType());
774}
775
776// <type> ::= R <type> # reference-to
777void CXXNameMangler::mangleType(const LValueReferenceType *T) {
778 Out << 'R';
779 mangleType(T->getPointeeType());
780}
781
782// <type> ::= O <type> # rvalue reference-to (C++0x)
783void CXXNameMangler::mangleType(const RValueReferenceType *T) {
784 Out << 'O';
785 mangleType(T->getPointeeType());
786}
787
788// <type> ::= C <type> # complex pair (C 2000)
789void CXXNameMangler::mangleType(const ComplexType *T) {
790 Out << 'C';
791 mangleType(T->getElementType());
792}
793
794// GNU extension: vector types
795void CXXNameMangler::mangleType(const VectorType *T) {
796 Out << "U8__vector";
797 mangleType(T->getElementType());
798}
799void CXXNameMangler::mangleType(const ExtVectorType *T) {
800 mangleType(static_cast<const VectorType*>(T));
801}
802void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
803 Out << "U8__vector";
804 mangleType(T->getElementType());
805}
806
Anders Carlssona40c5e42009-03-07 22:03:21 +0000807void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
808 mangleSourceName(T->getDecl()->getIdentifier());
809}
810
John McCallefe6aee2009-09-05 07:56:18 +0000811void CXXNameMangler::mangleType(const BlockPointerType *T) {
812 assert(false && "can't mangle block pointer types yet");
813}
814
815void CXXNameMangler::mangleType(const FixedWidthIntType *T) {
816 assert(false && "can't mangle arbitary-precision integer type yet");
817}
818
819void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
820 // TSTs are never canonical unless they're dependent.
821 assert(false && "can't mangle dependent template specializations yet");
822}
823
824void CXXNameMangler::mangleType(const TypenameType *T) {
825 assert(false && "can't mangle dependent typenames yet");
826}
827
828// FIXME: For now, just drop all extension qualifiers on the floor.
829void CXXNameMangler::mangleType(const ExtQualType *T) {
830 mangleType(QualType(T->getBaseType(), 0));
831}
832
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000833void CXXNameMangler::mangleExpression(Expr *E) {
834 assert(false && "Cannot mangle expressions yet");
835}
836
John McCallefe6aee2009-09-05 07:56:18 +0000837// FIXME: <type> ::= G <type> # imaginary (C 2000)
838// FIXME: <type> ::= U <source-name> <type> # vendor extended type qualifier
839
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000840void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
841 // <ctor-dtor-name> ::= C1 # complete object constructor
842 // ::= C2 # base object constructor
843 // ::= C3 # complete object allocating constructor
844 //
845 switch (T) {
846 case Ctor_Complete:
847 Out << "C1";
848 break;
849 case Ctor_Base:
850 Out << "C2";
851 break;
852 case Ctor_CompleteAllocating:
853 Out << "C3";
854 break;
855 }
856}
857
Anders Carlsson27ae5362009-04-17 01:58:57 +0000858void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
859 // <ctor-dtor-name> ::= D0 # deleting destructor
860 // ::= D1 # complete object destructor
861 // ::= D2 # base object destructor
862 //
863 switch (T) {
864 case Dtor_Deleting:
865 Out << "D0";
866 break;
867 case Dtor_Complete:
868 Out << "D1";
869 break;
870 case Dtor_Base:
871 Out << "D2";
872 break;
873 }
874}
875
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000876void CXXNameMangler::mangleTemplateArgumentList(const TemplateArgumentList &L) {
877 // <template-args> ::= I <template-arg>+ E
878 Out << "I";
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000880 for (unsigned i = 0, e = L.size(); i != e; ++i) {
881 const TemplateArgument &A = L[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000883 mangleTemplateArgument(A);
884 }
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000886 Out << "E";
887}
888
889void CXXNameMangler::mangleTemplateArgument(const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +0000890 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000891 // ::= X <expression> E # expression
892 // ::= <expr-primary> # simple expressions
893 // ::= I <template-arg>* E # argument pack
894 // ::= sp <expression> # pack expansion of (C++0x)
895 switch (A.getKind()) {
896 default:
897 assert(0 && "Unknown template argument kind!");
898 case TemplateArgument::Type:
899 mangleType(A.getAsType());
900 break;
901 case TemplateArgument::Integral:
902 // <expr-primary> ::= L <type> <value number> E # integer literal
903
904 Out << 'L';
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000906 mangleType(A.getIntegralType());
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000908 const llvm::APSInt *Integral = A.getAsIntegral();
909 if (A.getIntegralType()->isBooleanType()) {
910 // Boolean values are encoded as 0/1.
911 Out << (Integral->getBoolValue() ? '1' : '0');
912 } else {
913 if (Integral->isNegative())
914 Out << 'n';
915 Integral->abs().print(Out, false);
916 }
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000918 Out << 'E';
919 break;
920 }
921}
922
Anders Carlsson76967372009-09-17 00:43:46 +0000923// <substitution> ::= S <seq-id> _
924// ::= S_
925bool CXXNameMangler::mangleSubstitution(QualType T) {
926 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
927
Anders Carlssond3a932a2009-09-17 03:53:28 +0000928 return mangleSubstitution(TypePtr);
929}
930
931bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Anders Carlsson76967372009-09-17 00:43:46 +0000932 llvm::DenseMap<uintptr_t, unsigned>::iterator I =
Anders Carlssond3a932a2009-09-17 03:53:28 +0000933 Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +0000934 if (I == Substitutions.end())
935 return false;
936
937 unsigned SeqID = I->second;
938 if (SeqID == 0)
939 Out << "S_";
940 else {
941 SeqID--;
942
943 // <seq-id> is encoded in base-36, using digits and upper case letters.
944 char Buffer[10];
945 char *BufferPtr = Buffer + 9;
946
947 *BufferPtr = 0;
948 if (SeqID == 0) *--BufferPtr = '0';
949
950 while (SeqID) {
951 assert(BufferPtr > Buffer && "Buffer overflow!");
952
953 unsigned char c = static_cast<unsigned char>(SeqID) % 36;
954
955 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
956 SeqID /= 36;
957 }
958
959 Out << 'S' << BufferPtr << '_';
960 }
961
962 return true;
963}
964
965void CXXNameMangler::addSubstitution(QualType T) {
966 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +0000967 addSubstitution(TypePtr);
968}
969
970void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlsson76967372009-09-17 00:43:46 +0000971 unsigned SeqID = Substitutions.size();
972
Anders Carlssond3a932a2009-09-17 03:53:28 +0000973 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
974 Substitutions[Ptr] = SeqID;
Anders Carlsson76967372009-09-17 00:43:46 +0000975}
976
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000977namespace clang {
Mike Stump141c5af2009-09-02 00:25:38 +0000978 /// \brief Mangles the name of the declaration D and emits that name to the
979 /// given output stream.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000980 ///
Mike Stump141c5af2009-09-02 00:25:38 +0000981 /// If the declaration D requires a mangled name, this routine will emit that
982 /// mangled name to \p os and return true. Otherwise, \p os will be unchanged
983 /// and this routine will return false. In this case, the caller should just
984 /// emit the identifier of the declaration (\c D->getIdentifier()) as its
985 /// name.
Mike Stump1eb44332009-09-09 15:08:12 +0000986 bool mangleName(const NamedDecl *D, ASTContext &Context,
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000987 llvm::raw_ostream &os) {
Anders Carlsson578aa642009-05-03 16:51:04 +0000988 assert(!isa<CXXConstructorDecl>(D) &&
989 "Use mangleCXXCtor for constructor decls!");
990 assert(!isa<CXXDestructorDecl>(D) &&
991 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000993 CXXNameMangler Mangler(Context, os);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000994 if (!Mangler.mangle(D))
995 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Douglas Gregor6ec36682009-02-18 23:53:56 +0000997 os.flush();
998 return true;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000999 }
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Mike Stump141c5af2009-09-02 00:25:38 +00001001 /// \brief Mangles the a thunk with the offset n for the declaration D and
1002 /// emits that name to the given output stream.
Mike Stumpdec025b2009-09-07 04:27:52 +00001003 void mangleThunk(const FunctionDecl *FD, int64_t nv, int64_t v,
Mike Stump883f1272009-09-02 00:28:47 +00001004 ASTContext &Context, llvm::raw_ostream &os) {
Mike Stump141c5af2009-09-02 00:25:38 +00001005 // FIXME: Hum, we might have to thunk these, fix.
Mike Stumpdec025b2009-09-07 04:27:52 +00001006 assert(!isa<CXXDestructorDecl>(FD) &&
Mike Stump141c5af2009-09-02 00:25:38 +00001007 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Mike Stump141c5af2009-09-02 00:25:38 +00001009 CXXNameMangler Mangler(Context, os);
Mike Stumpdec025b2009-09-07 04:27:52 +00001010 Mangler.mangleThunk(FD, nv, v);
Mike Stump141c5af2009-09-02 00:25:38 +00001011 os.flush();
1012 }
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Mike Stump9124bcc2009-09-02 00:56:18 +00001014 /// \brief Mangles the a covariant thunk for the declaration D and emits that
1015 /// name to the given output stream.
Mike Stumpdec025b2009-09-07 04:27:52 +00001016 void mangleCovariantThunk(const FunctionDecl *FD, int64_t nv_t, int64_t v_t,
Mike Stump77ca8f62009-09-05 07:20:32 +00001017 int64_t nv_r, int64_t v_r, ASTContext &Context,
Mike Stump9124bcc2009-09-02 00:56:18 +00001018 llvm::raw_ostream &os) {
1019 // FIXME: Hum, we might have to thunk these, fix.
Mike Stumpdec025b2009-09-07 04:27:52 +00001020 assert(!isa<CXXDestructorDecl>(FD) &&
Mike Stump9124bcc2009-09-02 00:56:18 +00001021 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Mike Stump9124bcc2009-09-02 00:56:18 +00001023 CXXNameMangler Mangler(Context, os);
Mike Stumpdec025b2009-09-07 04:27:52 +00001024 Mangler.mangleCovariantThunk(FD, nv_t, v_t, nv_r, v_r);
Mike Stump9124bcc2009-09-02 00:56:18 +00001025 os.flush();
1026 }
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001028 /// mangleGuardVariable - Returns the mangled name for a guard variable
1029 /// for the passed in VarDecl.
Anders Carlsson41aa8c12009-04-13 18:02:10 +00001030 void mangleGuardVariable(const VarDecl *D, ASTContext &Context,
1031 llvm::raw_ostream &os) {
1032 CXXNameMangler Mangler(Context, os);
1033 Mangler.mangleGuardVariable(D);
1034
1035 os.flush();
1036 }
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001038 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
1039 ASTContext &Context, llvm::raw_ostream &os) {
1040 CXXNameMangler Mangler(Context, os);
1041 Mangler.mangleCXXCtor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001043 os.flush();
1044 }
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Anders Carlsson27ae5362009-04-17 01:58:57 +00001046 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
1047 ASTContext &Context, llvm::raw_ostream &os) {
1048 CXXNameMangler Mangler(Context, os);
1049 Mangler.mangleCXXDtor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Anders Carlsson27ae5362009-04-17 01:58:57 +00001051 os.flush();
1052 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001053
Mike Stumpf1216772009-07-31 18:25:34 +00001054 void mangleCXXVtable(QualType Type, ASTContext &Context,
1055 llvm::raw_ostream &os) {
1056 CXXNameMangler Mangler(Context, os);
1057 Mangler.mangleCXXVtable(Type);
1058
1059 os.flush();
1060 }
Mike Stump738f8c22009-07-31 23:15:31 +00001061
1062 void mangleCXXRtti(QualType Type, ASTContext &Context,
1063 llvm::raw_ostream &os) {
1064 CXXNameMangler Mangler(Context, os);
1065 Mangler.mangleCXXRtti(Type);
1066
1067 os.flush();
1068 }
Mike Stumpf1216772009-07-31 18:25:34 +00001069}