blob: aaa771b21de2cef95076b899e3bb7e5013012576 [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 Carlsson068f3472009-09-17 05:31:47 +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 Carlssond58d6f72009-09-17 16:12:20 +0000252 const DeclContext *DC = ND->getDeclContext();
253 while (isa<LinkageSpecDecl>(DC)) {
254 assert(cast<LinkageSpecDecl>(DC)->getLanguage() ==
255 LinkageSpecDecl::lang_cxx && "Unexpected linkage decl!");
256 DC = DC->getParent();
257 }
258
259 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000260 const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
Anders Carlsson068f3472009-09-17 05:31:47 +0000261 if (FD && FD->getPrimaryTemplate())
Anders Carlsson201ce742009-09-17 03:17:01 +0000262 mangleUnscopedTemplateName(FD);
Anders Carlsson068f3472009-09-17 05:31:47 +0000263 else
Anders Carlsson201ce742009-09-17 03:17:01 +0000264 mangleUnscopedName(ND);
Anders Carlssond58d6f72009-09-17 16:12:20 +0000265 } else if (isa<FunctionDecl>(DC))
Anders Carlsson1b42c792009-04-02 16:24:45 +0000266 mangleLocalName(ND);
267 else
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000268 mangleNestedName(ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000269}
270
Anders Carlsson201ce742009-09-17 03:17:01 +0000271void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
272 // <unscoped-name> ::= <unqualified-name>
273 // ::= St <unqualified-name> # ::std::
274 if (isStdNamespace(ND->getDeclContext()))
275 Out << "St";
276
277 mangleUnqualifiedName(ND);
278}
279
280void CXXNameMangler::mangleUnscopedTemplateName(const FunctionDecl *FD) {
281 // <unscoped-template-name> ::= <unscoped-name>
282 // ::= <substitution>
Anders Carlsson03c9d532009-09-17 04:02:31 +0000283 if (mangleSubstitution(FD))
284 return;
285
Anders Carlsson201ce742009-09-17 03:17:01 +0000286 mangleUnscopedName(FD);
Anders Carlsson03c9d532009-09-17 04:02:31 +0000287 addSubstitution(FD);
Anders Carlsson201ce742009-09-17 03:17:01 +0000288}
289
Mike Stump77ca8f62009-09-05 07:20:32 +0000290void CXXNameMangler::mangleCalloffset(int64_t nv, int64_t v) {
Mike Stump141c5af2009-09-02 00:25:38 +0000291 // <call-offset> ::= h <nv-offset> _
292 // ::= v <v-offset> _
293 // <nv-offset> ::= <offset number> # non-virtual base override
294 // <v-offset> ::= <offset nubmer> _ <virtual offset number>
295 // # virtual base override, with vcall offset
Mike Stump77ca8f62009-09-05 07:20:32 +0000296 if (v == 0) {
Mike Stump9124bcc2009-09-02 00:56:18 +0000297 Out << "h";
Mike Stump141c5af2009-09-02 00:25:38 +0000298 if (nv < 0) {
299 Out << "n";
300 nv = -nv;
301 }
302 Out << nv;
303 } else {
Mike Stump9124bcc2009-09-02 00:56:18 +0000304 Out << "v";
Mike Stump141c5af2009-09-02 00:25:38 +0000305 if (nv < 0) {
306 Out << "n";
307 nv = -nv;
308 }
309 Out << nv;
310 Out << "_";
311 if (v < 0) {
312 Out << "n";
313 v = -v;
314 }
315 Out << v;
316 }
317 Out << "_";
Mike Stump9124bcc2009-09-02 00:56:18 +0000318}
319
Mike Stumpdec025b2009-09-07 04:27:52 +0000320void CXXNameMangler::mangleThunk(const FunctionDecl *FD, int64_t nv,
321 int64_t v) {
Mike Stump9124bcc2009-09-02 00:56:18 +0000322 // <special-name> ::= T <call-offset> <base encoding>
323 // # base is the nominal target function of thunk
Mike Stumpdec025b2009-09-07 04:27:52 +0000324 Out << "_ZT";
Mike Stump77ca8f62009-09-05 07:20:32 +0000325 mangleCalloffset(nv, v);
Mike Stumpdec025b2009-09-07 04:27:52 +0000326 mangleFunctionEncoding(FD);
Mike Stump9124bcc2009-09-02 00:56:18 +0000327}
328
Mike Stumpdec025b2009-09-07 04:27:52 +0000329 void CXXNameMangler::mangleCovariantThunk(const FunctionDecl *FD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000330 int64_t nv_t, int64_t v_t,
Mike Stump9124bcc2009-09-02 00:56:18 +0000331 int64_t nv_r, int64_t v_r) {
332 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
333 // # base is the nominal target function of thunk
334 // # first call-offset is 'this' adjustment
335 // # second call-offset is result adjustment
Mike Stumpdec025b2009-09-07 04:27:52 +0000336 Out << "_ZTc";
Mike Stump77ca8f62009-09-05 07:20:32 +0000337 mangleCalloffset(nv_t, v_t);
338 mangleCalloffset(nv_r, v_r);
Mike Stumpdec025b2009-09-07 04:27:52 +0000339 mangleFunctionEncoding(FD);
Mike Stump141c5af2009-09-02 00:25:38 +0000340}
341
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000342void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) {
343 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000344 // ::= <ctor-dtor-name>
345 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000346 DeclarationName Name = ND->getDeclName();
347 switch (Name.getNameKind()) {
348 case DeclarationName::Identifier:
349 mangleSourceName(Name.getAsIdentifierInfo());
350 break;
351
352 case DeclarationName::ObjCZeroArgSelector:
353 case DeclarationName::ObjCOneArgSelector:
354 case DeclarationName::ObjCMultiArgSelector:
355 assert(false && "Can't mangle Objective-C selector names here!");
356 break;
357
358 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000359 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000360 // If the named decl is the C++ constructor we're mangling, use the type
361 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000362 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000363 else
364 // Otherwise, use the complete constructor name. This is relevant if a
365 // class with a constructor is declared within a constructor.
366 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000367 break;
368
369 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000370 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000371 // If the named decl is the C++ destructor we're mangling, use the type we
372 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000373 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
374 else
375 // Otherwise, use the complete destructor name. This is relevant if a
376 // class with a destructor is declared within a destructor.
377 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000378 break;
379
380 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000381 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000382 Out << "cv";
383 mangleType(Context.getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000384 break;
385
386 case DeclarationName::CXXOperatorName:
387 mangleOperatorName(Name.getCXXOverloadedOperator(),
388 cast<FunctionDecl>(ND)->getNumParams());
389 break;
390
391 case DeclarationName::CXXUsingDirective:
392 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000393 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000394 }
Anders Carlsson068f3472009-09-17 05:31:47 +0000395
396 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
397 if (const TemplateArgumentList *TemplateArgs
398 = Function->getTemplateSpecializationArgs())
399 mangleTemplateArgumentList(*TemplateArgs);
400 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000401}
402
403void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
404 // <source-name> ::= <positive length number> <identifier>
405 // <number> ::= [n] <non-negative decimal integer>
406 // <identifier> ::= <unqualified source code identifier>
407 Out << II->getLength() << II->getName();
408}
409
410void CXXNameMangler::mangleNestedName(const NamedDecl *ND) {
411 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
412 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
413 // FIXME: no template support
414 Out << 'N';
415 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
416 mangleCVQualifiers(Method->getTypeQualifiers());
417 manglePrefix(ND->getDeclContext());
418 mangleUnqualifiedName(ND);
419 Out << 'E';
420}
421
Anders Carlsson1b42c792009-04-02 16:24:45 +0000422void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
423 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
424 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000425 // <discriminator> := _ <non-negative number>
Anders Carlsson1b42c792009-04-02 16:24:45 +0000426 Out << 'Z';
427 mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext()));
428 Out << 'E';
429 mangleSourceName(ND->getIdentifier());
430}
431
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000432void CXXNameMangler::manglePrefix(const DeclContext *DC) {
433 // <prefix> ::= <prefix> <unqualified-name>
434 // ::= <template-prefix> <template-args>
435 // ::= <template-param>
436 // ::= # empty
437 // ::= <substitution>
438 // FIXME: We only handle mangling of namespaces and classes at the moment.
Anders Carlsson6862fc72009-09-17 04:16:28 +0000439
440 if (mangleSubstitution(cast<NamedDecl>(DC)))
441 return;
442
Anders Carlssonc8dee9c2009-04-01 00:42:16 +0000443 if (!DC->getParent()->isTranslationUnit())
444 manglePrefix(DC->getParent());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000445
446 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(DC))
447 mangleSourceName(Namespace->getIdentifier());
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000448 else if (const RecordDecl *Record = dyn_cast<RecordDecl>(DC)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000449 if (const ClassTemplateSpecializationDecl *D =
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000450 dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
451 mangleType(QualType(D->getTypeForDecl(), 0));
452 } else
453 mangleSourceName(Record->getIdentifier());
454 }
Anders Carlsson6862fc72009-09-17 04:16:28 +0000455
456 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000457}
458
Mike Stump1eb44332009-09-09 15:08:12 +0000459void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000460CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
461 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000462 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000463 case OO_New: Out << "nw"; break;
464 // ::= na # new[]
465 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000466 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000467 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000468 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000469 case OO_Array_Delete: Out << "da"; break;
470 // ::= ps # + (unary)
471 // ::= pl # +
472 case OO_Plus: Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000473 // ::= ng # - (unary)
474 // ::= mi # -
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000475 case OO_Minus: Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000476 // ::= ad # & (unary)
477 // ::= an # &
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000478 case OO_Amp: Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000479 // ::= de # * (unary)
480 // ::= ml # *
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000481 case OO_Star: Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000482 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000483 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000484 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000485 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000486 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000487 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000488 // ::= or # |
489 case OO_Pipe: Out << "or"; break;
490 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000491 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000492 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000493 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000494 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000495 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000496 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000497 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000498 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000499 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000500 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000501 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000502 // ::= rM # %=
503 case OO_PercentEqual: Out << "rM"; break;
504 // ::= aN # &=
505 case OO_AmpEqual: Out << "aN"; break;
506 // ::= oR # |=
507 case OO_PipeEqual: Out << "oR"; break;
508 // ::= eO # ^=
509 case OO_CaretEqual: Out << "eO"; break;
510 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000511 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000512 // ::= rs # >>
513 case OO_GreaterGreater: Out << "rs"; break;
514 // ::= lS # <<=
515 case OO_LessLessEqual: Out << "lS"; break;
516 // ::= rS # >>=
517 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000518 // ::= eq # ==
519 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000520 // ::= ne # !=
521 case OO_ExclaimEqual: Out << "ne"; break;
522 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000523 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000524 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000525 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000526 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000527 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000528 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000529 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000530 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000531 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000532 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000533 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000534 // ::= oo # ||
535 case OO_PipePipe: Out << "oo"; break;
536 // ::= pp # ++
537 case OO_PlusPlus: Out << "pp"; break;
538 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000539 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000540 // ::= cm # ,
541 case OO_Comma: Out << "cm"; break;
542 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000543 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000544 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000545 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000546 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000547 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000548 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000549 case OO_Subscript: Out << "ix"; break;
550 // UNSUPPORTED: ::= qu # ?
551
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000552 case OO_None:
553 case OO_Conditional:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000554 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +0000555 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000556 break;
557 }
558}
559
560void CXXNameMangler::mangleCVQualifiers(unsigned Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +0000561 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000562 if (Quals & QualType::Restrict)
563 Out << 'r';
564 if (Quals & QualType::Volatile)
565 Out << 'V';
566 if (Quals & QualType::Const)
567 Out << 'K';
568}
569
570void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000571 // Only operate on the canonical type!
572 T = Context.getCanonicalType(T);
573
Anders Carlsson76967372009-09-17 00:43:46 +0000574 bool IsSubstitutable = !isa<BuiltinType>(T);
575 if (IsSubstitutable && mangleSubstitution(T))
576 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000577
Anders Carlsson76967372009-09-17 00:43:46 +0000578 if (unsigned CVRQualifiers = T.getCVRQualifiers()) {
579 // <type> ::= <CV-qualifiers> <type>
580 mangleCVQualifiers(CVRQualifiers);
581
582 mangleType(T.getUnqualifiedType());
583 } else {
584 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +0000585#define ABSTRACT_TYPE(CLASS, PARENT)
586#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000587 case Type::CLASS: \
588 llvm::llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
589 return;
John McCallefe6aee2009-09-05 07:56:18 +0000590#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +0000591 case Type::CLASS: \
592 mangleType(static_cast<CLASS##Type*>(T.getTypePtr())); \
593 break;
John McCallefe6aee2009-09-05 07:56:18 +0000594#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +0000595 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000596 }
Anders Carlsson76967372009-09-17 00:43:46 +0000597
598 // Add the substitution.
599 if (IsSubstitutable)
600 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000601}
602
603void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +0000604 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000605 // <builtin-type> ::= v # void
606 // ::= w # wchar_t
607 // ::= b # bool
608 // ::= c # char
609 // ::= a # signed char
610 // ::= h # unsigned char
611 // ::= s # short
612 // ::= t # unsigned short
613 // ::= i # int
614 // ::= j # unsigned int
615 // ::= l # long
616 // ::= m # unsigned long
617 // ::= x # long long, __int64
618 // ::= y # unsigned long long, __int64
619 // ::= n # __int128
620 // UNSUPPORTED: ::= o # unsigned __int128
621 // ::= f # float
622 // ::= d # double
623 // ::= e # long double, __float80
624 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000625 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
626 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
627 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
628 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000629 // ::= Di # char32_t
630 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000631 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000632 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
633 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000634 switch (T->getKind()) {
635 case BuiltinType::Void: Out << 'v'; break;
636 case BuiltinType::Bool: Out << 'b'; break;
637 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
638 case BuiltinType::UChar: Out << 'h'; break;
639 case BuiltinType::UShort: Out << 't'; break;
640 case BuiltinType::UInt: Out << 'j'; break;
641 case BuiltinType::ULong: Out << 'm'; break;
642 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000643 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000644 case BuiltinType::SChar: Out << 'a'; break;
645 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000646 case BuiltinType::Char16: Out << "Ds"; break;
647 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000648 case BuiltinType::Short: Out << 's'; break;
649 case BuiltinType::Int: Out << 'i'; break;
650 case BuiltinType::Long: Out << 'l'; break;
651 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000652 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000653 case BuiltinType::Float: Out << 'f'; break;
654 case BuiltinType::Double: Out << 'd'; break;
655 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000656 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000657
658 case BuiltinType::Overload:
659 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +0000660 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000661 "Overloaded and dependent types shouldn't get to name mangling");
662 break;
Anders Carlssone89d1592009-06-26 18:41:36 +0000663 case BuiltinType::UndeducedAuto:
664 assert(0 && "Should not see undeduced auto here");
665 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +0000666 case BuiltinType::ObjCId: Out << "11objc_object"; break;
667 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000668 }
669}
670
John McCallefe6aee2009-09-05 07:56:18 +0000671// <type> ::= <function-type>
672// <function-type> ::= F [Y] <bare-function-type> E
673void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000674 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +0000675 // FIXME: We don't have enough information in the AST to produce the 'Y'
676 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000677 mangleBareFunctionType(T, /*MangleReturnType=*/true);
678 Out << 'E';
679}
John McCallefe6aee2009-09-05 07:56:18 +0000680void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
681 llvm::llvm_unreachable("Can't mangle K&R function prototypes");
682}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000683void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
684 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +0000685 // We should never be mangling something without a prototype.
686 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
687
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000688 // <bare-function-type> ::= <signature type>+
689 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +0000690 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000691
Anders Carlssonc6c91bc2009-04-01 00:15:23 +0000692 if (Proto->getNumArgs() == 0) {
693 Out << 'v';
694 return;
695 }
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Douglas Gregor72564e72009-02-26 23:50:07 +0000697 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000698 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000699 Arg != ArgEnd; ++Arg)
700 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +0000701
702 // <builtin-type> ::= z # ellipsis
703 if (Proto->isVariadic())
704 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000705}
706
John McCallefe6aee2009-09-05 07:56:18 +0000707// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +0000708// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +0000709void CXXNameMangler::mangleType(const EnumType *T) {
710 mangleType(static_cast<const TagType*>(T));
711}
712void CXXNameMangler::mangleType(const RecordType *T) {
713 mangleType(static_cast<const TagType*>(T));
714}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000715void CXXNameMangler::mangleType(const TagType *T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000716 if (!T->getDecl()->getIdentifier())
717 mangleName(T->getDecl()->getTypedefForAnonDecl());
718 else
719 mangleName(T->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Mike Stump141c5af2009-09-02 00:25:38 +0000721 // If this is a class template specialization, mangle the template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000722 if (ClassTemplateSpecializationDecl *Spec
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000723 = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl()))
Anders Carlsson068f3472009-09-17 05:31:47 +0000724 mangleTemplateArgumentList(Spec->getTemplateArgs());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000725}
726
John McCallefe6aee2009-09-05 07:56:18 +0000727// <type> ::= <array-type>
728// <array-type> ::= A <positive dimension number> _ <element type>
729// ::= A [<dimension expression>] _ <element type>
730void CXXNameMangler::mangleType(const ConstantArrayType *T) {
731 Out << 'A' << T->getSize() << '_';
732 mangleType(T->getElementType());
733}
734void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000735 Out << 'A';
John McCallefe6aee2009-09-05 07:56:18 +0000736 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000737 Out << '_';
738 mangleType(T->getElementType());
739}
John McCallefe6aee2009-09-05 07:56:18 +0000740void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
741 Out << 'A';
742 mangleExpression(T->getSizeExpr());
743 Out << '_';
744 mangleType(T->getElementType());
745}
746void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
747 Out << 'A' << '_';
748 mangleType(T->getElementType());
749}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000750
John McCallefe6aee2009-09-05 07:56:18 +0000751// <type> ::= <pointer-to-member-type>
752// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000753void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000754 Out << 'M';
755 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +0000756 QualType PointeeType = T->getPointeeType();
757 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
758 mangleCVQualifiers(FPT->getTypeQuals());
759 mangleType(FPT);
Mike Stump1eb44332009-09-09 15:08:12 +0000760 } else
Anders Carlsson0e650012009-05-17 17:41:20 +0000761 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000762}
763
John McCallefe6aee2009-09-05 07:56:18 +0000764// <type> ::= <template-param>
765// <template-param> ::= T_ # first template parameter
766// ::= T <parameter-2 non-negative number> _
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000767void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000768 if (T->getIndex() == 0)
769 Out << "T_";
770 else
771 Out << 'T' << (T->getIndex() - 1) << '_';
772}
773
John McCallefe6aee2009-09-05 07:56:18 +0000774// FIXME: <type> ::= <template-template-param> <template-args>
John McCallefe6aee2009-09-05 07:56:18 +0000775
776// <type> ::= P <type> # pointer-to
777void CXXNameMangler::mangleType(const PointerType *T) {
778 Out << 'P';
779 mangleType(T->getPointeeType());
780}
781void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
782 Out << 'P';
783 mangleType(T->getPointeeType());
784}
785
786// <type> ::= R <type> # reference-to
787void CXXNameMangler::mangleType(const LValueReferenceType *T) {
788 Out << 'R';
789 mangleType(T->getPointeeType());
790}
791
792// <type> ::= O <type> # rvalue reference-to (C++0x)
793void CXXNameMangler::mangleType(const RValueReferenceType *T) {
794 Out << 'O';
795 mangleType(T->getPointeeType());
796}
797
798// <type> ::= C <type> # complex pair (C 2000)
799void CXXNameMangler::mangleType(const ComplexType *T) {
800 Out << 'C';
801 mangleType(T->getElementType());
802}
803
804// GNU extension: vector types
805void CXXNameMangler::mangleType(const VectorType *T) {
806 Out << "U8__vector";
807 mangleType(T->getElementType());
808}
809void CXXNameMangler::mangleType(const ExtVectorType *T) {
810 mangleType(static_cast<const VectorType*>(T));
811}
812void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
813 Out << "U8__vector";
814 mangleType(T->getElementType());
815}
816
Anders Carlssona40c5e42009-03-07 22:03:21 +0000817void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
818 mangleSourceName(T->getDecl()->getIdentifier());
819}
820
John McCallefe6aee2009-09-05 07:56:18 +0000821void CXXNameMangler::mangleType(const BlockPointerType *T) {
822 assert(false && "can't mangle block pointer types yet");
823}
824
825void CXXNameMangler::mangleType(const FixedWidthIntType *T) {
826 assert(false && "can't mangle arbitary-precision integer type yet");
827}
828
829void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
830 // TSTs are never canonical unless they're dependent.
831 assert(false && "can't mangle dependent template specializations yet");
832}
833
834void CXXNameMangler::mangleType(const TypenameType *T) {
835 assert(false && "can't mangle dependent typenames yet");
836}
837
838// FIXME: For now, just drop all extension qualifiers on the floor.
839void CXXNameMangler::mangleType(const ExtQualType *T) {
840 mangleType(QualType(T->getBaseType(), 0));
841}
842
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000843void CXXNameMangler::mangleExpression(Expr *E) {
844 assert(false && "Cannot mangle expressions yet");
845}
846
John McCallefe6aee2009-09-05 07:56:18 +0000847// FIXME: <type> ::= G <type> # imaginary (C 2000)
848// FIXME: <type> ::= U <source-name> <type> # vendor extended type qualifier
849
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000850void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
851 // <ctor-dtor-name> ::= C1 # complete object constructor
852 // ::= C2 # base object constructor
853 // ::= C3 # complete object allocating constructor
854 //
855 switch (T) {
856 case Ctor_Complete:
857 Out << "C1";
858 break;
859 case Ctor_Base:
860 Out << "C2";
861 break;
862 case Ctor_CompleteAllocating:
863 Out << "C3";
864 break;
865 }
866}
867
Anders Carlsson27ae5362009-04-17 01:58:57 +0000868void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
869 // <ctor-dtor-name> ::= D0 # deleting destructor
870 // ::= D1 # complete object destructor
871 // ::= D2 # base object destructor
872 //
873 switch (T) {
874 case Dtor_Deleting:
875 Out << "D0";
876 break;
877 case Dtor_Complete:
878 Out << "D1";
879 break;
880 case Dtor_Base:
881 Out << "D2";
882 break;
883 }
884}
885
Anders Carlsson068f3472009-09-17 05:31:47 +0000886void CXXNameMangler::mangleTemplateArgumentList(const TemplateArgumentList &L) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000887 // <template-args> ::= I <template-arg>+ E
888 Out << "I";
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000890 for (unsigned i = 0, e = L.size(); i != e; ++i) {
891 const TemplateArgument &A = L[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Anders Carlsson068f3472009-09-17 05:31:47 +0000893 mangleTemplateArgument(A);
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000894 }
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000896 Out << "E";
897}
898
Anders Carlsson068f3472009-09-17 05:31:47 +0000899void CXXNameMangler::mangleTemplateArgument(const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +0000900 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000901 // ::= X <expression> E # expression
902 // ::= <expr-primary> # simple expressions
903 // ::= I <template-arg>* E # argument pack
904 // ::= sp <expression> # pack expansion of (C++0x)
905 switch (A.getKind()) {
906 default:
907 assert(0 && "Unknown template argument kind!");
908 case TemplateArgument::Type:
909 mangleType(A.getAsType());
910 break;
911 case TemplateArgument::Integral:
912 // <expr-primary> ::= L <type> <value number> E # integer literal
913
914 Out << 'L';
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000916 mangleType(A.getIntegralType());
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000918 const llvm::APSInt *Integral = A.getAsIntegral();
919 if (A.getIntegralType()->isBooleanType()) {
920 // Boolean values are encoded as 0/1.
921 Out << (Integral->getBoolValue() ? '1' : '0');
922 } else {
923 if (Integral->isNegative())
924 Out << 'n';
925 Integral->abs().print(Out, false);
926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000928 Out << 'E';
929 break;
930 }
931}
932
Anders Carlsson76967372009-09-17 00:43:46 +0000933// <substitution> ::= S <seq-id> _
934// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +0000935
936bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
937 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
938}
939
Anders Carlsson76967372009-09-17 00:43:46 +0000940bool CXXNameMangler::mangleSubstitution(QualType T) {
941 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
942
Anders Carlssond3a932a2009-09-17 03:53:28 +0000943 return mangleSubstitution(TypePtr);
944}
945
946bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Anders Carlsson76967372009-09-17 00:43:46 +0000947 llvm::DenseMap<uintptr_t, unsigned>::iterator I =
Anders Carlssond3a932a2009-09-17 03:53:28 +0000948 Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +0000949 if (I == Substitutions.end())
950 return false;
951
952 unsigned SeqID = I->second;
953 if (SeqID == 0)
954 Out << "S_";
955 else {
956 SeqID--;
957
958 // <seq-id> is encoded in base-36, using digits and upper case letters.
959 char Buffer[10];
960 char *BufferPtr = Buffer + 9;
961
962 *BufferPtr = 0;
963 if (SeqID == 0) *--BufferPtr = '0';
964
965 while (SeqID) {
966 assert(BufferPtr > Buffer && "Buffer overflow!");
967
968 unsigned char c = static_cast<unsigned char>(SeqID) % 36;
969
970 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
971 SeqID /= 36;
972 }
973
974 Out << 'S' << BufferPtr << '_';
975 }
976
977 return true;
978}
979
980void CXXNameMangler::addSubstitution(QualType T) {
981 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +0000982 addSubstitution(TypePtr);
983}
984
985void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlsson76967372009-09-17 00:43:46 +0000986 unsigned SeqID = Substitutions.size();
987
Anders Carlssond3a932a2009-09-17 03:53:28 +0000988 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
989 Substitutions[Ptr] = SeqID;
Anders Carlsson76967372009-09-17 00:43:46 +0000990}
991
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000992namespace clang {
Mike Stump141c5af2009-09-02 00:25:38 +0000993 /// \brief Mangles the name of the declaration D and emits that name to the
994 /// given output stream.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000995 ///
Mike Stump141c5af2009-09-02 00:25:38 +0000996 /// If the declaration D requires a mangled name, this routine will emit that
997 /// mangled name to \p os and return true. Otherwise, \p os will be unchanged
998 /// and this routine will return false. In this case, the caller should just
999 /// emit the identifier of the declaration (\c D->getIdentifier()) as its
1000 /// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001001 bool mangleName(const NamedDecl *D, ASTContext &Context,
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001002 llvm::raw_ostream &os) {
Anders Carlsson578aa642009-05-03 16:51:04 +00001003 assert(!isa<CXXConstructorDecl>(D) &&
1004 "Use mangleCXXCtor for constructor decls!");
1005 assert(!isa<CXXDestructorDecl>(D) &&
1006 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001008 CXXNameMangler Mangler(Context, os);
Douglas Gregor6ec36682009-02-18 23:53:56 +00001009 if (!Mangler.mangle(D))
1010 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Douglas Gregor6ec36682009-02-18 23:53:56 +00001012 os.flush();
1013 return true;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001014 }
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Mike Stump141c5af2009-09-02 00:25:38 +00001016 /// \brief Mangles the a thunk with the offset n for the declaration D and
1017 /// emits that name to the given output stream.
Mike Stumpdec025b2009-09-07 04:27:52 +00001018 void mangleThunk(const FunctionDecl *FD, int64_t nv, int64_t v,
Mike Stump883f1272009-09-02 00:28:47 +00001019 ASTContext &Context, llvm::raw_ostream &os) {
Mike Stump141c5af2009-09-02 00:25:38 +00001020 // FIXME: Hum, we might have to thunk these, fix.
Mike Stumpdec025b2009-09-07 04:27:52 +00001021 assert(!isa<CXXDestructorDecl>(FD) &&
Mike Stump141c5af2009-09-02 00:25:38 +00001022 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Mike Stump141c5af2009-09-02 00:25:38 +00001024 CXXNameMangler Mangler(Context, os);
Mike Stumpdec025b2009-09-07 04:27:52 +00001025 Mangler.mangleThunk(FD, nv, v);
Mike Stump141c5af2009-09-02 00:25:38 +00001026 os.flush();
1027 }
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Mike Stump9124bcc2009-09-02 00:56:18 +00001029 /// \brief Mangles the a covariant thunk for the declaration D and emits that
1030 /// name to the given output stream.
Mike Stumpdec025b2009-09-07 04:27:52 +00001031 void mangleCovariantThunk(const FunctionDecl *FD, int64_t nv_t, int64_t v_t,
Mike Stump77ca8f62009-09-05 07:20:32 +00001032 int64_t nv_r, int64_t v_r, ASTContext &Context,
Mike Stump9124bcc2009-09-02 00:56:18 +00001033 llvm::raw_ostream &os) {
1034 // FIXME: Hum, we might have to thunk these, fix.
Mike Stumpdec025b2009-09-07 04:27:52 +00001035 assert(!isa<CXXDestructorDecl>(FD) &&
Mike Stump9124bcc2009-09-02 00:56:18 +00001036 "Use mangleCXXDtor for destructor decls!");
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Mike Stump9124bcc2009-09-02 00:56:18 +00001038 CXXNameMangler Mangler(Context, os);
Mike Stumpdec025b2009-09-07 04:27:52 +00001039 Mangler.mangleCovariantThunk(FD, nv_t, v_t, nv_r, v_r);
Mike Stump9124bcc2009-09-02 00:56:18 +00001040 os.flush();
1041 }
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001043 /// mangleGuardVariable - Returns the mangled name for a guard variable
1044 /// for the passed in VarDecl.
Anders Carlsson41aa8c12009-04-13 18:02:10 +00001045 void mangleGuardVariable(const VarDecl *D, ASTContext &Context,
1046 llvm::raw_ostream &os) {
1047 CXXNameMangler Mangler(Context, os);
1048 Mangler.mangleGuardVariable(D);
1049
1050 os.flush();
1051 }
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001053 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
1054 ASTContext &Context, llvm::raw_ostream &os) {
1055 CXXNameMangler Mangler(Context, os);
1056 Mangler.mangleCXXCtor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001058 os.flush();
1059 }
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Anders Carlsson27ae5362009-04-17 01:58:57 +00001061 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
1062 ASTContext &Context, llvm::raw_ostream &os) {
1063 CXXNameMangler Mangler(Context, os);
1064 Mangler.mangleCXXDtor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Anders Carlsson27ae5362009-04-17 01:58:57 +00001066 os.flush();
1067 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001068
Mike Stumpf1216772009-07-31 18:25:34 +00001069 void mangleCXXVtable(QualType Type, ASTContext &Context,
1070 llvm::raw_ostream &os) {
1071 CXXNameMangler Mangler(Context, os);
1072 Mangler.mangleCXXVtable(Type);
1073
1074 os.flush();
1075 }
Mike Stump738f8c22009-07-31 23:15:31 +00001076
1077 void mangleCXXRtti(QualType Type, ASTContext &Context,
1078 llvm::raw_ostream &os) {
1079 CXXNameMangler Mangler(Context, os);
1080 Mangler.mangleCXXRtti(Type);
1081
1082 os.flush();
1083 }
Mike Stumpf1216772009-07-31 18:25:34 +00001084}