blob: 8241ad61d077c7fd6a6023fff6148e2103fad6ee [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 Carlsson6862fc72009-09-17 04:16:28 +000058 bool mangleSubstitution(const NamedDecl *ND);
Anders Carlsson76967372009-09-17 00:43:46 +000059 bool mangleSubstitution(QualType T);
Anders Carlssond3a932a2009-09-17 03:53:28 +000060 bool mangleSubstitution(uintptr_t Ptr);
61
62 void addSubstitution(const NamedDecl *ND) {
63 addSubstitution(reinterpret_cast<uintptr_t>(ND));
64 }
Anders Carlsson76967372009-09-17 00:43:46 +000065 void addSubstitution(QualType T);
Anders Carlssond3a932a2009-09-17 03:53:28 +000066 void addSubstitution(uintptr_t Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +000067
Anders Carlsson43f17402009-04-02 15:51:53 +000068 bool mangleFunctionDecl(const FunctionDecl *FD);
Mike Stump1eb44332009-09-09 15:08:12 +000069
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000070 void mangleFunctionEncoding(const FunctionDecl *FD);
71 void mangleName(const NamedDecl *ND);
72 void mangleUnqualifiedName(const NamedDecl *ND);
Anders Carlsson201ce742009-09-17 03:17:01 +000073 void mangleUnscopedName(const NamedDecl *ND);
74 void mangleUnscopedTemplateName(const FunctionDecl *ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000075 void mangleSourceName(const IdentifierInfo *II);
Anders Carlsson1b42c792009-04-02 16:24:45 +000076 void mangleLocalName(const NamedDecl *ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000077 void mangleNestedName(const NamedDecl *ND);
78 void manglePrefix(const DeclContext *DC);
79 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
80 void mangleCVQualifiers(unsigned Quals);
81 void mangleType(QualType T);
John McCallefe6aee2009-09-05 07:56:18 +000082
83 // Declare manglers for every type class.
84#define ABSTRACT_TYPE(CLASS, PARENT)
85#define NON_CANONICAL_TYPE(CLASS, PARENT)
86#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
87#include "clang/AST/TypeNodes.def"
88
89 void mangleType(const TagType*);
90 void mangleBareFunctionType(const FunctionType *T,
91 bool MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000092 void mangleExpression(Expr *E);
Anders Carlsson3ac86b52009-04-15 05:36:58 +000093 void mangleCXXCtorType(CXXCtorType T);
Anders Carlsson27ae5362009-04-17 01:58:57 +000094 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +000095
Anders Carlsson7a0ba872009-05-15 16:09:15 +000096 void mangleTemplateArgumentList(const TemplateArgumentList &L);
97 void mangleTemplateArgument(const TemplateArgument &A);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000098 };
99}
100
Anders Carlsson43f17402009-04-02 15:51:53 +0000101static bool isInCLinkageSpecification(const Decl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000102 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000103 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000104 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000105 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
106 }
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Anders Carlsson43f17402009-04-02 15:51:53 +0000108 return false;
109}
110
111bool CXXNameMangler::mangleFunctionDecl(const FunctionDecl *FD) {
Mike Stump141c5af2009-09-02 00:25:38 +0000112 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
113 // (always).
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000114 if (!FD->hasAttr<OverloadableAttr>()) {
Chris Lattner783601d2009-06-13 23:34:16 +0000115 // C functions are not mangled, and "main" is never mangled.
Douglas Gregor48a83b52009-09-12 00:17:51 +0000116 if (!Context.getLangOptions().CPlusPlus || FD->isMain())
Chris Lattner783601d2009-06-13 23:34:16 +0000117 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000118
119 // No mangling in an "implicit extern C" header.
Chris Lattner783601d2009-06-13 23:34:16 +0000120 if (FD->getLocation().isValid() &&
121 Context.getSourceManager().isInExternCSystemHeader(FD->getLocation()))
122 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Chris Lattner783601d2009-06-13 23:34:16 +0000124 // No name mangling in a C linkage specification.
125 if (isInCLinkageSpecification(FD))
126 return false;
127 }
Anders Carlsson43f17402009-04-02 15:51:53 +0000128
129 // If we get here, mangle the decl name!
130 Out << "_Z";
131 mangleFunctionEncoding(FD);
132 return true;
133}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000134
135bool CXXNameMangler::mangle(const NamedDecl *D) {
Mike Stump141c5af2009-09-02 00:25:38 +0000136 // Any decl can be declared with __asm("foo") on it, and this takes precedence
137 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000138 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000139 // If we have an asm name, then we use it as the mangling.
140 Out << '\01'; // LLVM IR Marker for __asm("foo")
141 Out << ALA->getLabel();
142 return true;
143 }
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000145 // <mangled-name> ::= _Z <encoding>
146 // ::= <data name>
147 // ::= <special-name>
148
149 // FIXME: Actually use a visitor to decode these?
Anders Carlsson43f17402009-04-02 15:51:53 +0000150 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
151 return mangleFunctionDecl(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Anders Carlsson329749c2009-04-02 16:05:20 +0000153 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
154 if (!Context.getLangOptions().CPlusPlus ||
Anders Carlsson9ccb0652009-04-11 01:19:45 +0000155 isInCLinkageSpecification(D) ||
156 D->getDeclContext()->isTranslationUnit())
Anders Carlsson329749c2009-04-02 16:05:20 +0000157 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Anders Carlsson329749c2009-04-02 16:05:20 +0000159 Out << "_Z";
160 mangleName(VD);
161 return true;
162 }
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Anders Carlsson43f17402009-04-02 15:51:53 +0000164 return false;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000165}
166
Mike Stump1eb44332009-09-09 15:08:12 +0000167void CXXNameMangler::mangleCXXCtor(const CXXConstructorDecl *D,
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000168 CXXCtorType Type) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000169 assert(!Structor && "Structor already set!");
170 Structor = D;
171 StructorType = Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Anders Carlsson27ae5362009-04-17 01:58:57 +0000173 mangle(D);
174}
175
Mike Stump1eb44332009-09-09 15:08:12 +0000176void CXXNameMangler::mangleCXXDtor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000177 CXXDtorType Type) {
178 assert(!Structor && "Structor already set!");
179 Structor = D;
180 StructorType = Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000182 mangle(D);
183}
184
Mike Stumpf1216772009-07-31 18:25:34 +0000185void CXXNameMangler::mangleCXXVtable(QualType T) {
186 // <special-name> ::= TV <type> # virtual table
187 Out << "_ZTV";
188 mangleType(T);
189}
190
Mike Stump738f8c22009-07-31 23:15:31 +0000191void CXXNameMangler::mangleCXXRtti(QualType T) {
192 // <special-name> ::= TI <type> # typeinfo structure
193 Out << "_ZTI";
194 mangleType(T);
195}
196
Mike Stump1eb44332009-09-09 15:08:12 +0000197void CXXNameMangler::mangleGuardVariable(const VarDecl *D) {
198 // <special-name> ::= GV <object name> # Guard variable for one-time
Mike Stumpf1216772009-07-31 18:25:34 +0000199 // # initialization
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000200
201 Out << "_ZGV";
202 mangleName(D);
203}
204
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000205void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
206 // <encoding> ::= <function name> <bare-function-type>
207 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Mike Stump141c5af2009-09-02 00:25:38 +0000209 // Whether the mangling of a function type includes the return type depends on
210 // the context and the nature of the function. The rules for deciding whether
211 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000212 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000213 // 1. Template functions (names or types) have return types encoded, with
214 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000215 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000216 // e.g. parameters, pointer types, etc., have return type encoded, with the
217 // exceptions listed below.
218 // 3. Non-template function names do not have return types encoded.
219 //
Mike Stump141c5af2009-09-02 00:25:38 +0000220 // The exceptions mentioned in (1) and (2) above, for which the return type is
221 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000222 // 1. Constructors.
223 // 2. Destructors.
224 // 3. Conversion operator functions, e.g. operator int.
225 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000226 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
227 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
228 isa<CXXConversionDecl>(FD)))
229 MangleReturnType = true;
230
231 // Mangle the type of the primary template.
232 FD = PrimaryTemplate->getTemplatedDecl();
233 }
234
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000235 mangleBareFunctionType(FD->getType()->getAsFunctionType(), MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000236}
237
238static bool isStdNamespace(const DeclContext *DC) {
239 if (!DC->isNamespace() || !DC->getParent()->isTranslationUnit())
240 return false;
241
242 const NamespaceDecl *NS = cast<NamespaceDecl>(DC);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000243 return NS->getOriginalNamespace()->getIdentifier()->isStr("std");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000244}
245
246void CXXNameMangler::mangleName(const NamedDecl *ND) {
247 // <name> ::= <nested-name>
248 // ::= <unscoped-name>
249 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000250 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000251 //
Anders Carlsson201ce742009-09-17 03:17:01 +0000252 if (ND->getDeclContext()->isTranslationUnit() ||
253 isStdNamespace(ND->getDeclContext())) {
254 const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
255 if (FD && FD->getPrimaryTemplate())
256 mangleUnscopedTemplateName(FD);
257 else
258 mangleUnscopedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000259 } else if (isa<FunctionDecl>(ND->getDeclContext()))
260 mangleLocalName(ND);
261 else
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000262 mangleNestedName(ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000263}
264
Anders Carlsson201ce742009-09-17 03:17:01 +0000265void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
266 // <unscoped-name> ::= <unqualified-name>
267 // ::= St <unqualified-name> # ::std::
268 if (isStdNamespace(ND->getDeclContext()))
269 Out << "St";
270
271 mangleUnqualifiedName(ND);
272}
273
274void CXXNameMangler::mangleUnscopedTemplateName(const FunctionDecl *FD) {
275 // <unscoped-template-name> ::= <unscoped-name>
276 // ::= <substitution>
Anders Carlsson03c9d532009-09-17 04:02:31 +0000277 if (mangleSubstitution(FD))
278 return;
279
Anders Carlsson201ce742009-09-17 03:17:01 +0000280 mangleUnscopedName(FD);
Anders Carlsson03c9d532009-09-17 04:02:31 +0000281 addSubstitution(FD);
Anders Carlsson201ce742009-09-17 03:17:01 +0000282}
283
Mike Stump77ca8f62009-09-05 07:20:32 +0000284void CXXNameMangler::mangleCalloffset(int64_t nv, int64_t v) {
Mike Stump141c5af2009-09-02 00:25:38 +0000285 // <call-offset> ::= h <nv-offset> _
286 // ::= v <v-offset> _
287 // <nv-offset> ::= <offset number> # non-virtual base override
288 // <v-offset> ::= <offset nubmer> _ <virtual offset number>
289 // # virtual base override, with vcall offset
Mike Stump77ca8f62009-09-05 07:20:32 +0000290 if (v == 0) {
Mike Stump9124bcc2009-09-02 00:56:18 +0000291 Out << "h";
Mike Stump141c5af2009-09-02 00:25:38 +0000292 if (nv < 0) {
293 Out << "n";
294 nv = -nv;
295 }
296 Out << nv;
297 } else {
Mike Stump9124bcc2009-09-02 00:56:18 +0000298 Out << "v";
Mike Stump141c5af2009-09-02 00:25:38 +0000299 if (nv < 0) {
300 Out << "n";
301 nv = -nv;
302 }
303 Out << nv;
304 Out << "_";
305 if (v < 0) {
306 Out << "n";
307 v = -v;
308 }
309 Out << v;
310 }
311 Out << "_";
Mike Stump9124bcc2009-09-02 00:56:18 +0000312}
313
Mike Stumpdec025b2009-09-07 04:27:52 +0000314void CXXNameMangler::mangleThunk(const FunctionDecl *FD, int64_t nv,
315 int64_t v) {
Mike Stump9124bcc2009-09-02 00:56:18 +0000316 // <special-name> ::= T <call-offset> <base encoding>
317 // # base is the nominal target function of thunk
Mike Stumpdec025b2009-09-07 04:27:52 +0000318 Out << "_ZT";
Mike Stump77ca8f62009-09-05 07:20:32 +0000319 mangleCalloffset(nv, v);
Mike Stumpdec025b2009-09-07 04:27:52 +0000320 mangleFunctionEncoding(FD);
Mike Stump9124bcc2009-09-02 00:56:18 +0000321}
322
Mike Stumpdec025b2009-09-07 04:27:52 +0000323 void CXXNameMangler::mangleCovariantThunk(const FunctionDecl *FD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000324 int64_t nv_t, int64_t v_t,
Mike Stump9124bcc2009-09-02 00:56:18 +0000325 int64_t nv_r, int64_t v_r) {
326 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
327 // # base is the nominal target function of thunk
328 // # first call-offset is 'this' adjustment
329 // # second call-offset is result adjustment
Mike Stumpdec025b2009-09-07 04:27:52 +0000330 Out << "_ZTc";
Mike Stump77ca8f62009-09-05 07:20:32 +0000331 mangleCalloffset(nv_t, v_t);
332 mangleCalloffset(nv_r, v_r);
Mike Stumpdec025b2009-09-07 04:27:52 +0000333 mangleFunctionEncoding(FD);
Mike Stump141c5af2009-09-02 00:25:38 +0000334}
335
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000336void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) {
337 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000338 // ::= <ctor-dtor-name>
339 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000340 DeclarationName Name = ND->getDeclName();
341 switch (Name.getNameKind()) {
342 case DeclarationName::Identifier:
343 mangleSourceName(Name.getAsIdentifierInfo());
344 break;
345
346 case DeclarationName::ObjCZeroArgSelector:
347 case DeclarationName::ObjCOneArgSelector:
348 case DeclarationName::ObjCMultiArgSelector:
349 assert(false && "Can't mangle Objective-C selector names here!");
350 break;
351
352 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000353 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000354 // If the named decl is the C++ constructor we're mangling, use the type
355 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000356 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000357 else
358 // Otherwise, use the complete constructor name. This is relevant if a
359 // class with a constructor is declared within a constructor.
360 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000361 break;
362
363 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000364 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000365 // If the named decl is the C++ destructor we're mangling, use the type we
366 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000367 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
368 else
369 // Otherwise, use the complete destructor name. This is relevant if a
370 // class with a destructor is declared within a destructor.
371 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000372 break;
373
374 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000375 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000376 Out << "cv";
377 mangleType(Context.getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000378 break;
379
380 case DeclarationName::CXXOperatorName:
381 mangleOperatorName(Name.getCXXOverloadedOperator(),
382 cast<FunctionDecl>(ND)->getNumParams());
383 break;
384
385 case DeclarationName::CXXUsingDirective:
386 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000387 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000390 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000391 if (const TemplateArgumentList *TemplateArgs
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000392 = Function->getTemplateSpecializationArgs())
393 mangleTemplateArgumentList(*TemplateArgs);
394 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000395}
396
397void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
398 // <source-name> ::= <positive length number> <identifier>
399 // <number> ::= [n] <non-negative decimal integer>
400 // <identifier> ::= <unqualified source code identifier>
401 Out << II->getLength() << II->getName();
402}
403
404void CXXNameMangler::mangleNestedName(const NamedDecl *ND) {
405 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
406 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
407 // FIXME: no template support
408 Out << 'N';
409 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
410 mangleCVQualifiers(Method->getTypeQualifiers());
411 manglePrefix(ND->getDeclContext());
412 mangleUnqualifiedName(ND);
413 Out << 'E';
414}
415
Anders Carlsson1b42c792009-04-02 16:24:45 +0000416void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
417 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
418 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000419 // <discriminator> := _ <non-negative number>
Anders Carlsson1b42c792009-04-02 16:24:45 +0000420 Out << 'Z';
421 mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext()));
422 Out << 'E';
423 mangleSourceName(ND->getIdentifier());
424}
425
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000426void CXXNameMangler::manglePrefix(const DeclContext *DC) {
427 // <prefix> ::= <prefix> <unqualified-name>
428 // ::= <template-prefix> <template-args>
429 // ::= <template-param>
430 // ::= # empty
431 // ::= <substitution>
432 // FIXME: We only handle mangling of namespaces and classes at the moment.
Anders Carlsson6862fc72009-09-17 04:16:28 +0000433
434 if (mangleSubstitution(cast<NamedDecl>(DC)))
435 return;
436
Anders Carlssonc8dee9c2009-04-01 00:42:16 +0000437 if (!DC->getParent()->isTranslationUnit())
438 manglePrefix(DC->getParent());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000439
440 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(DC))
441 mangleSourceName(Namespace->getIdentifier());
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000442 else if (const RecordDecl *Record = dyn_cast<RecordDecl>(DC)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000443 if (const ClassTemplateSpecializationDecl *D =
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000444 dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
445 mangleType(QualType(D->getTypeForDecl(), 0));
446 } else
447 mangleSourceName(Record->getIdentifier());
448 }
Anders Carlsson6862fc72009-09-17 04:16:28 +0000449
450 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000451}
452
Mike Stump1eb44332009-09-09 15:08:12 +0000453void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000454CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
455 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000456 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000457 case OO_New: Out << "nw"; break;
458 // ::= na # new[]
459 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000460 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000461 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000462 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000463 case OO_Array_Delete: Out << "da"; break;
464 // ::= ps # + (unary)
465 // ::= pl # +
466 case OO_Plus: Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000467 // ::= ng # - (unary)
468 // ::= mi # -
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000469 case OO_Minus: Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000470 // ::= ad # & (unary)
471 // ::= an # &
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000472 case OO_Amp: Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000473 // ::= de # * (unary)
474 // ::= ml # *
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000475 case OO_Star: Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000476 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000477 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000478 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000479 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000480 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000481 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000482 // ::= or # |
483 case OO_Pipe: Out << "or"; break;
484 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000485 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000486 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000487 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000488 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000489 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000490 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000491 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000492 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000493 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000494 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000495 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000496 // ::= rM # %=
497 case OO_PercentEqual: Out << "rM"; break;
498 // ::= aN # &=
499 case OO_AmpEqual: Out << "aN"; break;
500 // ::= oR # |=
501 case OO_PipeEqual: Out << "oR"; break;
502 // ::= eO # ^=
503 case OO_CaretEqual: Out << "eO"; break;
504 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000505 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000506 // ::= rs # >>
507 case OO_GreaterGreater: Out << "rs"; break;
508 // ::= lS # <<=
509 case OO_LessLessEqual: Out << "lS"; break;
510 // ::= rS # >>=
511 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000512 // ::= eq # ==
513 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000514 // ::= ne # !=
515 case OO_ExclaimEqual: Out << "ne"; break;
516 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000517 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000518 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000519 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000520 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000521 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000522 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000523 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000524 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000525 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000526 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000527 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000528 // ::= oo # ||
529 case OO_PipePipe: Out << "oo"; break;
530 // ::= pp # ++
531 case OO_PlusPlus: Out << "pp"; break;
532 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000533 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000534 // ::= cm # ,
535 case OO_Comma: Out << "cm"; break;
536 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000537 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000538 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000539 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000540 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000541 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000542 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000543 case OO_Subscript: Out << "ix"; break;
544 // UNSUPPORTED: ::= qu # ?
545
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000546 case OO_None:
547 case OO_Conditional:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000548 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +0000549 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000550 break;
551 }
552}
553
554void CXXNameMangler::mangleCVQualifiers(unsigned Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +0000555 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000556 if (Quals & QualType::Restrict)
557 Out << 'r';
558 if (Quals & QualType::Volatile)
559 Out << 'V';
560 if (Quals & QualType::Const)
561 Out << 'K';
562}
563
564void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000565 // Only operate on the canonical type!
566 T = Context.getCanonicalType(T);
567
Anders Carlsson76967372009-09-17 00:43:46 +0000568 bool IsSubstitutable = !isa<BuiltinType>(T);
569 if (IsSubstitutable && mangleSubstitution(T))
570 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000571
Anders Carlsson76967372009-09-17 00:43:46 +0000572 if (unsigned CVRQualifiers = T.getCVRQualifiers()) {
573 // <type> ::= <CV-qualifiers> <type>
574 mangleCVQualifiers(CVRQualifiers);
575
576 mangleType(T.getUnqualifiedType());
577 } else {
578 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +0000579#define ABSTRACT_TYPE(CLASS, PARENT)
580#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000581 case Type::CLASS: \
582 llvm::llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
583 return;
John McCallefe6aee2009-09-05 07:56:18 +0000584#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000585 case Type::CLASS: \
586 mangleType(static_cast<CLASS##Type*>(T.getTypePtr())); \
587 break;
John McCallefe6aee2009-09-05 07:56:18 +0000588#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +0000589 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000590 }
Anders Carlsson76967372009-09-17 00:43:46 +0000591
592 // Add the substitution.
593 if (IsSubstitutable)
594 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000595}
596
597void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +0000598 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000599 // <builtin-type> ::= v # void
600 // ::= w # wchar_t
601 // ::= b # bool
602 // ::= c # char
603 // ::= a # signed char
604 // ::= h # unsigned char
605 // ::= s # short
606 // ::= t # unsigned short
607 // ::= i # int
608 // ::= j # unsigned int
609 // ::= l # long
610 // ::= m # unsigned long
611 // ::= x # long long, __int64
612 // ::= y # unsigned long long, __int64
613 // ::= n # __int128
614 // UNSUPPORTED: ::= o # unsigned __int128
615 // ::= f # float
616 // ::= d # double
617 // ::= e # long double, __float80
618 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000619 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
620 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
621 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
622 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000623 // ::= Di # char32_t
624 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000625 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000626 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
627 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000628 switch (T->getKind()) {
629 case BuiltinType::Void: Out << 'v'; break;
630 case BuiltinType::Bool: Out << 'b'; break;
631 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
632 case BuiltinType::UChar: Out << 'h'; break;
633 case BuiltinType::UShort: Out << 't'; break;
634 case BuiltinType::UInt: Out << 'j'; break;
635 case BuiltinType::ULong: Out << 'm'; break;
636 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000637 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000638 case BuiltinType::SChar: Out << 'a'; break;
639 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000640 case BuiltinType::Char16: Out << "Ds"; break;
641 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000642 case BuiltinType::Short: Out << 's'; break;
643 case BuiltinType::Int: Out << 'i'; break;
644 case BuiltinType::Long: Out << 'l'; break;
645 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000646 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000647 case BuiltinType::Float: Out << 'f'; break;
648 case BuiltinType::Double: Out << 'd'; break;
649 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000650 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000651
652 case BuiltinType::Overload:
653 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +0000654 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000655 "Overloaded and dependent types shouldn't get to name mangling");
656 break;
Anders Carlssone89d1592009-06-26 18:41:36 +0000657 case BuiltinType::UndeducedAuto:
658 assert(0 && "Should not see undeduced auto here");
659 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +0000660 case BuiltinType::ObjCId: Out << "11objc_object"; break;
661 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000662 }
663}
664
John McCallefe6aee2009-09-05 07:56:18 +0000665// <type> ::= <function-type>
666// <function-type> ::= F [Y] <bare-function-type> E
667void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000668 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +0000669 // FIXME: We don't have enough information in the AST to produce the 'Y'
670 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000671 mangleBareFunctionType(T, /*MangleReturnType=*/true);
672 Out << 'E';
673}
John McCallefe6aee2009-09-05 07:56:18 +0000674void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
675 llvm::llvm_unreachable("Can't mangle K&R function prototypes");
676}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000677void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
678 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +0000679 // We should never be mangling something without a prototype.
680 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
681
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000682 // <bare-function-type> ::= <signature type>+
683 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +0000684 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000685
Anders Carlssonc6c91bc2009-04-01 00:15:23 +0000686 if (Proto->getNumArgs() == 0) {
687 Out << 'v';
688 return;
689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Douglas Gregor72564e72009-02-26 23:50:07 +0000691 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000692 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000693 Arg != ArgEnd; ++Arg)
694 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +0000695
696 // <builtin-type> ::= z # ellipsis
697 if (Proto->isVariadic())
698 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000699}
700
John McCallefe6aee2009-09-05 07:56:18 +0000701// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +0000702// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +0000703void CXXNameMangler::mangleType(const EnumType *T) {
704 mangleType(static_cast<const TagType*>(T));
705}
706void CXXNameMangler::mangleType(const RecordType *T) {
707 mangleType(static_cast<const TagType*>(T));
708}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000709void CXXNameMangler::mangleType(const TagType *T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000710 if (!T->getDecl()->getIdentifier())
711 mangleName(T->getDecl()->getTypedefForAnonDecl());
712 else
713 mangleName(T->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Mike Stump141c5af2009-09-02 00:25:38 +0000715 // If this is a class template specialization, mangle the template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000716 if (ClassTemplateSpecializationDecl *Spec
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000717 = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl()))
718 mangleTemplateArgumentList(Spec->getTemplateArgs());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000719}
720
John McCallefe6aee2009-09-05 07:56:18 +0000721// <type> ::= <array-type>
722// <array-type> ::= A <positive dimension number> _ <element type>
723// ::= A [<dimension expression>] _ <element type>
724void CXXNameMangler::mangleType(const ConstantArrayType *T) {
725 Out << 'A' << T->getSize() << '_';
726 mangleType(T->getElementType());
727}
728void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000729 Out << 'A';
John McCallefe6aee2009-09-05 07:56:18 +0000730 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000731 Out << '_';
732 mangleType(T->getElementType());
733}
John McCallefe6aee2009-09-05 07:56:18 +0000734void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
735 Out << 'A';
736 mangleExpression(T->getSizeExpr());
737 Out << '_';
738 mangleType(T->getElementType());
739}
740void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
741 Out << 'A' << '_';
742 mangleType(T->getElementType());
743}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000744
John McCallefe6aee2009-09-05 07:56:18 +0000745// <type> ::= <pointer-to-member-type>
746// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000747void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000748 Out << 'M';
749 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +0000750 QualType PointeeType = T->getPointeeType();
751 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
752 mangleCVQualifiers(FPT->getTypeQuals());
753 mangleType(FPT);
Mike Stump1eb44332009-09-09 15:08:12 +0000754 } else
Anders Carlsson0e650012009-05-17 17:41:20 +0000755 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000756}
757
John McCallefe6aee2009-09-05 07:56:18 +0000758// <type> ::= <template-param>
759// <template-param> ::= T_ # first template parameter
760// ::= T <parameter-2 non-negative number> _
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000761void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000762 if (T->getIndex() == 0)
763 Out << "T_";
764 else
765 Out << 'T' << (T->getIndex() - 1) << '_';
766}
767
John McCallefe6aee2009-09-05 07:56:18 +0000768// FIXME: <type> ::= <template-template-param> <template-args>
John McCallefe6aee2009-09-05 07:56:18 +0000769
770// <type> ::= P <type> # pointer-to
771void CXXNameMangler::mangleType(const PointerType *T) {
772 Out << 'P';
773 mangleType(T->getPointeeType());
774}
775void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
776 Out << 'P';
777 mangleType(T->getPointeeType());
778}
779
780// <type> ::= R <type> # reference-to
781void CXXNameMangler::mangleType(const LValueReferenceType *T) {
782 Out << 'R';
783 mangleType(T->getPointeeType());
784}
785
786// <type> ::= O <type> # rvalue reference-to (C++0x)
787void CXXNameMangler::mangleType(const RValueReferenceType *T) {
788 Out << 'O';
789 mangleType(T->getPointeeType());
790}
791
792// <type> ::= C <type> # complex pair (C 2000)
793void CXXNameMangler::mangleType(const ComplexType *T) {
794 Out << 'C';
795 mangleType(T->getElementType());
796}
797
798// GNU extension: vector types
799void CXXNameMangler::mangleType(const VectorType *T) {
800 Out << "U8__vector";
801 mangleType(T->getElementType());
802}
803void CXXNameMangler::mangleType(const ExtVectorType *T) {
804 mangleType(static_cast<const VectorType*>(T));
805}
806void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
807 Out << "U8__vector";
808 mangleType(T->getElementType());
809}
810
Anders Carlssona40c5e42009-03-07 22:03:21 +0000811void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
812 mangleSourceName(T->getDecl()->getIdentifier());
813}
814
John McCallefe6aee2009-09-05 07:56:18 +0000815void CXXNameMangler::mangleType(const BlockPointerType *T) {
816 assert(false && "can't mangle block pointer types yet");
817}
818
819void CXXNameMangler::mangleType(const FixedWidthIntType *T) {
820 assert(false && "can't mangle arbitary-precision integer type yet");
821}
822
823void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
824 // TSTs are never canonical unless they're dependent.
825 assert(false && "can't mangle dependent template specializations yet");
826}
827
828void CXXNameMangler::mangleType(const TypenameType *T) {
829 assert(false && "can't mangle dependent typenames yet");
830}
831
832// FIXME: For now, just drop all extension qualifiers on the floor.
833void CXXNameMangler::mangleType(const ExtQualType *T) {
834 mangleType(QualType(T->getBaseType(), 0));
835}
836
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000837void CXXNameMangler::mangleExpression(Expr *E) {
838 assert(false && "Cannot mangle expressions yet");
839}
840
John McCallefe6aee2009-09-05 07:56:18 +0000841// FIXME: <type> ::= G <type> # imaginary (C 2000)
842// FIXME: <type> ::= U <source-name> <type> # vendor extended type qualifier
843
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000844void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
845 // <ctor-dtor-name> ::= C1 # complete object constructor
846 // ::= C2 # base object constructor
847 // ::= C3 # complete object allocating constructor
848 //
849 switch (T) {
850 case Ctor_Complete:
851 Out << "C1";
852 break;
853 case Ctor_Base:
854 Out << "C2";
855 break;
856 case Ctor_CompleteAllocating:
857 Out << "C3";
858 break;
859 }
860}
861
Anders Carlsson27ae5362009-04-17 01:58:57 +0000862void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
863 // <ctor-dtor-name> ::= D0 # deleting destructor
864 // ::= D1 # complete object destructor
865 // ::= D2 # base object destructor
866 //
867 switch (T) {
868 case Dtor_Deleting:
869 Out << "D0";
870 break;
871 case Dtor_Complete:
872 Out << "D1";
873 break;
874 case Dtor_Base:
875 Out << "D2";
876 break;
877 }
878}
879
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000880void CXXNameMangler::mangleTemplateArgumentList(const TemplateArgumentList &L) {
881 // <template-args> ::= I <template-arg>+ E
882 Out << "I";
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000884 for (unsigned i = 0, e = L.size(); i != e; ++i) {
885 const TemplateArgument &A = L[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000887 mangleTemplateArgument(A);
888 }
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000890 Out << "E";
891}
892
893void CXXNameMangler::mangleTemplateArgument(const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +0000894 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000895 // ::= X <expression> E # expression
896 // ::= <expr-primary> # simple expressions
897 // ::= I <template-arg>* E # argument pack
898 // ::= sp <expression> # pack expansion of (C++0x)
899 switch (A.getKind()) {
900 default:
901 assert(0 && "Unknown template argument kind!");
902 case TemplateArgument::Type:
903 mangleType(A.getAsType());
904 break;
905 case TemplateArgument::Integral:
906 // <expr-primary> ::= L <type> <value number> E # integer literal
907
908 Out << 'L';
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000910 mangleType(A.getIntegralType());
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000912 const llvm::APSInt *Integral = A.getAsIntegral();
913 if (A.getIntegralType()->isBooleanType()) {
914 // Boolean values are encoded as 0/1.
915 Out << (Integral->getBoolValue() ? '1' : '0');
916 } else {
917 if (Integral->isNegative())
918 Out << 'n';
919 Integral->abs().print(Out, false);
920 }
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000922 Out << 'E';
923 break;
924 }
925}
926
Anders Carlsson76967372009-09-17 00:43:46 +0000927// <substitution> ::= S <seq-id> _
928// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +0000929
930bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
931 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
932}
933
Anders Carlsson76967372009-09-17 00:43:46 +0000934bool CXXNameMangler::mangleSubstitution(QualType T) {
935 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
936
Anders Carlssond3a932a2009-09-17 03:53:28 +0000937 return mangleSubstitution(TypePtr);
938}
939
940bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Anders Carlsson76967372009-09-17 00:43:46 +0000941 llvm::DenseMap<uintptr_t, unsigned>::iterator I =
Anders Carlssond3a932a2009-09-17 03:53:28 +0000942 Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +0000943 if (I == Substitutions.end())
944 return false;
945
946 unsigned SeqID = I->second;
947 if (SeqID == 0)
948 Out << "S_";
949 else {
950 SeqID--;
951
952 // <seq-id> is encoded in base-36, using digits and upper case letters.
953 char Buffer[10];
954 char *BufferPtr = Buffer + 9;
955
956 *BufferPtr = 0;
957 if (SeqID == 0) *--BufferPtr = '0';
958
959 while (SeqID) {
960 assert(BufferPtr > Buffer && "Buffer overflow!");
961
962 unsigned char c = static_cast<unsigned char>(SeqID) % 36;
963
964 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
965 SeqID /= 36;
966 }
967
968 Out << 'S' << BufferPtr << '_';
969 }
970
971 return true;
972}
973
974void CXXNameMangler::addSubstitution(QualType T) {
975 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +0000976 addSubstitution(TypePtr);
977}
978
979void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlsson76967372009-09-17 00:43:46 +0000980 unsigned SeqID = Substitutions.size();
981
Anders Carlssond3a932a2009-09-17 03:53:28 +0000982 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
983 Substitutions[Ptr] = SeqID;
Anders Carlsson76967372009-09-17 00:43:46 +0000984}
985
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000986namespace clang {
Mike Stump141c5af2009-09-02 00:25:38 +0000987 /// \brief Mangles the name of the declaration D and emits that name to the
988 /// given output stream.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000989 ///
Mike Stump141c5af2009-09-02 00:25:38 +0000990 /// If the declaration D requires a mangled name, this routine will emit that
991 /// mangled name to \p os and return true. Otherwise, \p os will be unchanged
992 /// and this routine will return false. In this case, the caller should just
993 /// emit the identifier of the declaration (\c D->getIdentifier()) as its
994 /// name.
Mike Stump1eb44332009-09-09 15:08:12 +0000995 bool mangleName(const NamedDecl *D, ASTContext &Context,
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000996 llvm::raw_ostream &os) {
Anders Carlsson578aa642009-05-03 16:51:04 +0000997 assert(!isa<CXXConstructorDecl>(D) &&
998 "Use mangleCXXCtor for constructor decls!");
999 assert(!isa<CXXDestructorDecl>(D) &&
1000 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001002 CXXNameMangler Mangler(Context, os);
Douglas Gregor6ec36682009-02-18 23:53:56 +00001003 if (!Mangler.mangle(D))
1004 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Douglas Gregor6ec36682009-02-18 23:53:56 +00001006 os.flush();
1007 return true;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Mike Stump141c5af2009-09-02 00:25:38 +00001010 /// \brief Mangles the a thunk with the offset n for the declaration D and
1011 /// emits that name to the given output stream.
Mike Stumpdec025b2009-09-07 04:27:52 +00001012 void mangleThunk(const FunctionDecl *FD, int64_t nv, int64_t v,
Mike Stump883f1272009-09-02 00:28:47 +00001013 ASTContext &Context, llvm::raw_ostream &os) {
Mike Stump141c5af2009-09-02 00:25:38 +00001014 // FIXME: Hum, we might have to thunk these, fix.
Mike Stumpdec025b2009-09-07 04:27:52 +00001015 assert(!isa<CXXDestructorDecl>(FD) &&
Mike Stump141c5af2009-09-02 00:25:38 +00001016 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Mike Stump141c5af2009-09-02 00:25:38 +00001018 CXXNameMangler Mangler(Context, os);
Mike Stumpdec025b2009-09-07 04:27:52 +00001019 Mangler.mangleThunk(FD, nv, v);
Mike Stump141c5af2009-09-02 00:25:38 +00001020 os.flush();
1021 }
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Mike Stump9124bcc2009-09-02 00:56:18 +00001023 /// \brief Mangles the a covariant thunk for the declaration D and emits that
1024 /// name to the given output stream.
Mike Stumpdec025b2009-09-07 04:27:52 +00001025 void mangleCovariantThunk(const FunctionDecl *FD, int64_t nv_t, int64_t v_t,
Mike Stump77ca8f62009-09-05 07:20:32 +00001026 int64_t nv_r, int64_t v_r, ASTContext &Context,
Mike Stump9124bcc2009-09-02 00:56:18 +00001027 llvm::raw_ostream &os) {
1028 // FIXME: Hum, we might have to thunk these, fix.
Mike Stumpdec025b2009-09-07 04:27:52 +00001029 assert(!isa<CXXDestructorDecl>(FD) &&
Mike Stump9124bcc2009-09-02 00:56:18 +00001030 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Mike Stump9124bcc2009-09-02 00:56:18 +00001032 CXXNameMangler Mangler(Context, os);
Mike Stumpdec025b2009-09-07 04:27:52 +00001033 Mangler.mangleCovariantThunk(FD, nv_t, v_t, nv_r, v_r);
Mike Stump9124bcc2009-09-02 00:56:18 +00001034 os.flush();
1035 }
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001037 /// mangleGuardVariable - Returns the mangled name for a guard variable
1038 /// for the passed in VarDecl.
Anders Carlsson41aa8c12009-04-13 18:02:10 +00001039 void mangleGuardVariable(const VarDecl *D, ASTContext &Context,
1040 llvm::raw_ostream &os) {
1041 CXXNameMangler Mangler(Context, os);
1042 Mangler.mangleGuardVariable(D);
1043
1044 os.flush();
1045 }
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001047 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
1048 ASTContext &Context, llvm::raw_ostream &os) {
1049 CXXNameMangler Mangler(Context, os);
1050 Mangler.mangleCXXCtor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001052 os.flush();
1053 }
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Anders Carlsson27ae5362009-04-17 01:58:57 +00001055 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
1056 ASTContext &Context, llvm::raw_ostream &os) {
1057 CXXNameMangler Mangler(Context, os);
1058 Mangler.mangleCXXDtor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Anders Carlsson27ae5362009-04-17 01:58:57 +00001060 os.flush();
1061 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001062
Mike Stumpf1216772009-07-31 18:25:34 +00001063 void mangleCXXVtable(QualType Type, ASTContext &Context,
1064 llvm::raw_ostream &os) {
1065 CXXNameMangler Mangler(Context, os);
1066 Mangler.mangleCXXVtable(Type);
1067
1068 os.flush();
1069 }
Mike Stump738f8c22009-07-31 23:15:31 +00001070
1071 void mangleCXXRtti(QualType Type, ASTContext &Context,
1072 llvm::raw_ostream &os) {
1073 CXXNameMangler Mangler(Context, os);
1074 Mangler.mangleCXXRtti(Type);
1075
1076 os.flush();
1077 }
Mike Stumpf1216772009-07-31 18:25:34 +00001078}