blob: 71b84e70013c7defad35913b437de375cb6162dc [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"
26using namespace clang;
27
28namespace {
29 class VISIBILITY_HIDDEN CXXNameMangler {
30 ASTContext &Context;
31 llvm::raw_ostream &Out;
32
Anders Carlsson27ae5362009-04-17 01:58:57 +000033 const CXXMethodDecl *Structor;
34 unsigned StructorType;
Anders Carlsson3ac86b52009-04-15 05:36:58 +000035 CXXCtorType CtorType;
36
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000037 public:
38 CXXNameMangler(ASTContext &C, llvm::raw_ostream &os)
Anders Carlsson27ae5362009-04-17 01:58:57 +000039 : Context(C), Out(os), Structor(0), StructorType(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000040
41 bool mangle(const NamedDecl *D);
Anders Carlsson41aa8c12009-04-13 18:02:10 +000042 void mangleGuardVariable(const VarDecl *D);
Anders Carlsson27ae5362009-04-17 01:58:57 +000043
Mike Stumpf1216772009-07-31 18:25:34 +000044 void mangleCXXVtable(QualType Type);
Mike Stump738f8c22009-07-31 23:15:31 +000045 void mangleCXXRtti(QualType Type);
Anders Carlsson3ac86b52009-04-15 05:36:58 +000046 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type);
Anders Carlsson27ae5362009-04-17 01:58:57 +000047 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type);
Anders Carlsson3ac86b52009-04-15 05:36:58 +000048
Anders Carlsson43f17402009-04-02 15:51:53 +000049 private:
50 bool mangleFunctionDecl(const FunctionDecl *FD);
51
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000052 void mangleFunctionEncoding(const FunctionDecl *FD);
53 void mangleName(const NamedDecl *ND);
54 void mangleUnqualifiedName(const NamedDecl *ND);
55 void mangleSourceName(const IdentifierInfo *II);
Anders Carlsson1b42c792009-04-02 16:24:45 +000056 void mangleLocalName(const NamedDecl *ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000057 void mangleNestedName(const NamedDecl *ND);
58 void manglePrefix(const DeclContext *DC);
59 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
60 void mangleCVQualifiers(unsigned Quals);
61 void mangleType(QualType T);
62 void mangleType(const BuiltinType *T);
63 void mangleType(const FunctionType *T);
64 void mangleBareFunctionType(const FunctionType *T, bool MangleReturnType);
65 void mangleType(const TagType *T);
66 void mangleType(const ArrayType *T);
67 void mangleType(const MemberPointerType *T);
68 void mangleType(const TemplateTypeParmType *T);
Anders Carlssona40c5e42009-03-07 22:03:21 +000069 void mangleType(const ObjCInterfaceType *T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000070 void mangleExpression(Expr *E);
Anders Carlsson3ac86b52009-04-15 05:36:58 +000071 void mangleCXXCtorType(CXXCtorType T);
Anders Carlsson27ae5362009-04-17 01:58:57 +000072 void mangleCXXDtorType(CXXDtorType T);
Anders Carlsson7a0ba872009-05-15 16:09:15 +000073
74 void mangleTemplateArgumentList(const TemplateArgumentList &L);
75 void mangleTemplateArgument(const TemplateArgument &A);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000076 };
77}
78
Anders Carlsson43f17402009-04-02 15:51:53 +000079static bool isInCLinkageSpecification(const Decl *D) {
80 for (const DeclContext *DC = D->getDeclContext();
81 !DC->isTranslationUnit(); DC = DC->getParent()) {
82 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
83 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
84 }
85
86 return false;
87}
88
89bool CXXNameMangler::mangleFunctionDecl(const FunctionDecl *FD) {
90 // Clang's "overloadable" attribute extension to C/C++ implies
91 // name mangling (always).
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +000092 if (!FD->hasAttr<OverloadableAttr>()) {
Chris Lattner783601d2009-06-13 23:34:16 +000093 // C functions are not mangled, and "main" is never mangled.
John McCall07a5c222009-08-15 02:09:25 +000094 if (!Context.getLangOptions().CPlusPlus || FD->isMain(Context))
Chris Lattner783601d2009-06-13 23:34:16 +000095 return false;
96
97 // No mangling in an "implicit extern C" header.
98 if (FD->getLocation().isValid() &&
99 Context.getSourceManager().isInExternCSystemHeader(FD->getLocation()))
100 return false;
101
102 // No name mangling in a C linkage specification.
103 if (isInCLinkageSpecification(FD))
104 return false;
105 }
Anders Carlsson43f17402009-04-02 15:51:53 +0000106
107 // If we get here, mangle the decl name!
108 Out << "_Z";
109 mangleFunctionEncoding(FD);
110 return true;
111}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000112
113bool CXXNameMangler::mangle(const NamedDecl *D) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000114 // Any decl can be declared with __asm("foo") on it, and this takes
115 // precedence over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000116 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000117 // If we have an asm name, then we use it as the mangling.
118 Out << '\01'; // LLVM IR Marker for __asm("foo")
119 Out << ALA->getLabel();
120 return true;
121 }
122
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000123 // <mangled-name> ::= _Z <encoding>
124 // ::= <data name>
125 // ::= <special-name>
126
127 // FIXME: Actually use a visitor to decode these?
Anders Carlsson43f17402009-04-02 15:51:53 +0000128 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
129 return mangleFunctionDecl(FD);
Chris Lattnerbc7a0292009-03-21 06:19:20 +0000130
Anders Carlsson329749c2009-04-02 16:05:20 +0000131 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
132 if (!Context.getLangOptions().CPlusPlus ||
Anders Carlsson9ccb0652009-04-11 01:19:45 +0000133 isInCLinkageSpecification(D) ||
134 D->getDeclContext()->isTranslationUnit())
Anders Carlsson329749c2009-04-02 16:05:20 +0000135 return false;
136
137 Out << "_Z";
138 mangleName(VD);
139 return true;
140 }
141
Anders Carlsson43f17402009-04-02 15:51:53 +0000142 return false;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000143}
144
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000145void CXXNameMangler::mangleCXXCtor(const CXXConstructorDecl *D,
146 CXXCtorType Type) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000147 assert(!Structor && "Structor already set!");
148 Structor = D;
149 StructorType = Type;
150
151 mangle(D);
152}
153
154void CXXNameMangler::mangleCXXDtor(const CXXDestructorDecl *D,
155 CXXDtorType Type) {
156 assert(!Structor && "Structor already set!");
157 Structor = D;
158 StructorType = Type;
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000159
160 mangle(D);
161}
162
Mike Stumpf1216772009-07-31 18:25:34 +0000163void CXXNameMangler::mangleCXXVtable(QualType T) {
164 // <special-name> ::= TV <type> # virtual table
165 Out << "_ZTV";
166 mangleType(T);
167}
168
Mike Stump738f8c22009-07-31 23:15:31 +0000169void CXXNameMangler::mangleCXXRtti(QualType T) {
170 // <special-name> ::= TI <type> # typeinfo structure
171 Out << "_ZTI";
172 mangleType(T);
173}
174
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000175void CXXNameMangler::mangleGuardVariable(const VarDecl *D)
176{
177 // <special-name> ::= GV <object name> # Guard variable for one-time
Mike Stumpf1216772009-07-31 18:25:34 +0000178 // # initialization
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000179
180 Out << "_ZGV";
181 mangleName(D);
182}
183
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000184void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
185 // <encoding> ::= <function name> <bare-function-type>
186 mangleName(FD);
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000187
188 // Whether the mangling of a function type includes the return type depends
189 // on the context and the nature of the function. The rules for deciding
190 // whether the return type is included are:
191 //
192 // 1. Template functions (names or types) have return types encoded, with
193 // the exceptions listed below.
194 // 2. Function types not appearing as part of a function name mangling,
195 // e.g. parameters, pointer types, etc., have return type encoded, with the
196 // exceptions listed below.
197 // 3. Non-template function names do not have return types encoded.
198 //
199 // The exceptions mentioned in (1) and (2) above, for which the return
200 // type is never included, are
201 // 1. Constructors.
202 // 2. Destructors.
203 // 3. Conversion operator functions, e.g. operator int.
204 bool MangleReturnType = false;
205 if (FD->getPrimaryTemplate() &&
206 !(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
207 isa<CXXConversionDecl>(FD)))
208 MangleReturnType = true;
209 mangleBareFunctionType(FD->getType()->getAsFunctionType(), MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000210}
211
212static bool isStdNamespace(const DeclContext *DC) {
213 if (!DC->isNamespace() || !DC->getParent()->isTranslationUnit())
214 return false;
215
216 const NamespaceDecl *NS = cast<NamespaceDecl>(DC);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000217 return NS->getOriginalNamespace()->getIdentifier()->isStr("std");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000218}
219
220void CXXNameMangler::mangleName(const NamedDecl *ND) {
221 // <name> ::= <nested-name>
222 // ::= <unscoped-name>
223 // ::= <unscoped-template-name> <template-args>
224 // ::= <local-name> # See Scope Encoding below
225 //
226 // <unscoped-name> ::= <unqualified-name>
227 // ::= St <unqualified-name> # ::std::
228 if (ND->getDeclContext()->isTranslationUnit())
229 mangleUnqualifiedName(ND);
230 else if (isStdNamespace(ND->getDeclContext())) {
231 Out << "St";
232 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000233 } else if (isa<FunctionDecl>(ND->getDeclContext()))
234 mangleLocalName(ND);
235 else
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000236 mangleNestedName(ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000237}
238
239void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) {
240 // <unqualified-name> ::= <operator-name>
241 // ::= <ctor-dtor-name>
242 // ::= <source-name>
243 DeclarationName Name = ND->getDeclName();
244 switch (Name.getNameKind()) {
245 case DeclarationName::Identifier:
246 mangleSourceName(Name.getAsIdentifierInfo());
247 break;
248
249 case DeclarationName::ObjCZeroArgSelector:
250 case DeclarationName::ObjCOneArgSelector:
251 case DeclarationName::ObjCMultiArgSelector:
252 assert(false && "Can't mangle Objective-C selector names here!");
253 break;
254
255 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000256 if (ND == Structor)
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000257 // If the named decl is the C++ constructor we're mangling, use the
258 // type we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000259 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000260 else
261 // Otherwise, use the complete constructor name. This is relevant if a
262 // class with a constructor is declared within a constructor.
263 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000264 break;
265
266 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000267 if (ND == Structor)
268 // If the named decl is the C++ destructor we're mangling, use the
269 // type we were given.
270 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
271 else
272 // Otherwise, use the complete destructor name. This is relevant if a
273 // class with a destructor is declared within a destructor.
274 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000275 break;
276
277 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor219cc612009-02-13 01:28:03 +0000278 // <operator-name> ::= cv <type> # (cast)
279 Out << "cv";
280 mangleType(Context.getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000281 break;
282
283 case DeclarationName::CXXOperatorName:
284 mangleOperatorName(Name.getCXXOverloadedOperator(),
285 cast<FunctionDecl>(ND)->getNumParams());
286 break;
287
288 case DeclarationName::CXXUsingDirective:
289 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000290 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000291 }
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000292
293 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
294 if (const TemplateArgumentList *TemplateArgs
295 = Function->getTemplateSpecializationArgs())
296 mangleTemplateArgumentList(*TemplateArgs);
297 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000298}
299
300void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
301 // <source-name> ::= <positive length number> <identifier>
302 // <number> ::= [n] <non-negative decimal integer>
303 // <identifier> ::= <unqualified source code identifier>
304 Out << II->getLength() << II->getName();
305}
306
307void CXXNameMangler::mangleNestedName(const NamedDecl *ND) {
308 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
309 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
310 // FIXME: no template support
311 Out << 'N';
312 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
313 mangleCVQualifiers(Method->getTypeQualifiers());
314 manglePrefix(ND->getDeclContext());
315 mangleUnqualifiedName(ND);
316 Out << 'E';
317}
318
Anders Carlsson1b42c792009-04-02 16:24:45 +0000319void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
320 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
321 // := Z <function encoding> E s [<discriminator>]
322 // <discriminator> := _ <non-negative number>
323 Out << 'Z';
324 mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext()));
325 Out << 'E';
326 mangleSourceName(ND->getIdentifier());
327}
328
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000329void CXXNameMangler::manglePrefix(const DeclContext *DC) {
330 // <prefix> ::= <prefix> <unqualified-name>
331 // ::= <template-prefix> <template-args>
332 // ::= <template-param>
333 // ::= # empty
334 // ::= <substitution>
335 // FIXME: We only handle mangling of namespaces and classes at the moment.
Anders Carlssonc8dee9c2009-04-01 00:42:16 +0000336 if (!DC->getParent()->isTranslationUnit())
337 manglePrefix(DC->getParent());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000338
339 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(DC))
340 mangleSourceName(Namespace->getIdentifier());
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000341 else if (const RecordDecl *Record = dyn_cast<RecordDecl>(DC)) {
342 if (const ClassTemplateSpecializationDecl *D =
343 dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
344 mangleType(QualType(D->getTypeForDecl(), 0));
345 } else
346 mangleSourceName(Record->getIdentifier());
347 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000348}
349
350void
351CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
352 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000353 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000354 case OO_New: Out << "nw"; break;
355 // ::= na # new[]
356 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000357 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000358 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000359 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000360 case OO_Array_Delete: Out << "da"; break;
361 // ::= ps # + (unary)
362 // ::= pl # +
363 case OO_Plus: Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000364 // ::= ng # - (unary)
365 // ::= mi # -
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000366 case OO_Minus: Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000367 // ::= ad # & (unary)
368 // ::= an # &
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000369 case OO_Amp: Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000370 // ::= de # * (unary)
371 // ::= ml # *
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000372 case OO_Star: Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000373 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000374 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000375 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000376 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000377 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000378 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000379 // ::= or # |
380 case OO_Pipe: Out << "or"; break;
381 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000382 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000383 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000384 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000385 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000386 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000387 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000388 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000389 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000390 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000391 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000392 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000393 // ::= rM # %=
394 case OO_PercentEqual: Out << "rM"; break;
395 // ::= aN # &=
396 case OO_AmpEqual: Out << "aN"; break;
397 // ::= oR # |=
398 case OO_PipeEqual: Out << "oR"; break;
399 // ::= eO # ^=
400 case OO_CaretEqual: Out << "eO"; break;
401 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000402 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000403 // ::= rs # >>
404 case OO_GreaterGreater: Out << "rs"; break;
405 // ::= lS # <<=
406 case OO_LessLessEqual: Out << "lS"; break;
407 // ::= rS # >>=
408 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000409 // ::= eq # ==
410 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000411 // ::= ne # !=
412 case OO_ExclaimEqual: Out << "ne"; break;
413 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000414 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000415 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000416 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000417 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000418 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000419 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000420 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000421 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000422 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000423 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000424 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000425 // ::= oo # ||
426 case OO_PipePipe: Out << "oo"; break;
427 // ::= pp # ++
428 case OO_PlusPlus: Out << "pp"; break;
429 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000430 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000431 // ::= cm # ,
432 case OO_Comma: Out << "cm"; break;
433 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000434 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000435 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000436 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000437 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000438 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000439 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000440 case OO_Subscript: Out << "ix"; break;
441 // UNSUPPORTED: ::= qu # ?
442
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000443 case OO_None:
444 case OO_Conditional:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000445 case NUM_OVERLOADED_OPERATORS:
Douglas Gregor6ec36682009-02-18 23:53:56 +0000446 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000447 break;
448 }
449}
450
451void CXXNameMangler::mangleCVQualifiers(unsigned Quals) {
452 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
453 if (Quals & QualType::Restrict)
454 Out << 'r';
455 if (Quals & QualType::Volatile)
456 Out << 'V';
457 if (Quals & QualType::Const)
458 Out << 'K';
459}
460
461void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000462 // Only operate on the canonical type!
463 T = Context.getCanonicalType(T);
464
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000465 // FIXME: Should we have a TypeNodes.def to make this easier? (YES!)
466
467 // <type> ::= <CV-qualifiers> <type>
468 mangleCVQualifiers(T.getCVRQualifiers());
469
470 // ::= <builtin-type>
Anders Carlsson4843e582009-03-10 17:07:44 +0000471 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr()))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000472 mangleType(BT);
473 // ::= <function-type>
474 else if (const FunctionType *FT = dyn_cast<FunctionType>(T.getTypePtr()))
475 mangleType(FT);
476 // ::= <class-enum-type>
477 else if (const TagType *TT = dyn_cast<TagType>(T.getTypePtr()))
478 mangleType(TT);
479 // ::= <array-type>
480 else if (const ArrayType *AT = dyn_cast<ArrayType>(T.getTypePtr()))
481 mangleType(AT);
482 // ::= <pointer-to-member-type>
483 else if (const MemberPointerType *MPT
484 = dyn_cast<MemberPointerType>(T.getTypePtr()))
485 mangleType(MPT);
486 // ::= <template-param>
487 else if (const TemplateTypeParmType *TypeParm
488 = dyn_cast<TemplateTypeParmType>(T.getTypePtr()))
489 mangleType(TypeParm);
490 // FIXME: ::= <template-template-param> <template-args>
491 // FIXME: ::= <substitution> # See Compression below
492 // ::= P <type> # pointer-to
493 else if (const PointerType *PT = dyn_cast<PointerType>(T.getTypePtr())) {
494 Out << 'P';
495 mangleType(PT->getPointeeType());
496 }
Steve Naroff14108da2009-07-10 23:34:53 +0000497 else if (const ObjCObjectPointerType *PT =
498 dyn_cast<ObjCObjectPointerType>(T.getTypePtr())) {
499 Out << 'P';
500 mangleType(PT->getPointeeType());
501 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000502 // ::= R <type> # reference-to
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000503 else if (const LValueReferenceType *RT =
504 dyn_cast<LValueReferenceType>(T.getTypePtr())) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000505 Out << 'R';
506 mangleType(RT->getPointeeType());
507 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000508 // ::= O <type> # rvalue reference-to (C++0x)
509 else if (const RValueReferenceType *RT =
510 dyn_cast<RValueReferenceType>(T.getTypePtr())) {
511 Out << 'O';
512 mangleType(RT->getPointeeType());
513 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000514 // ::= C <type> # complex pair (C 2000)
515 else if (const ComplexType *CT = dyn_cast<ComplexType>(T.getTypePtr())) {
516 Out << 'C';
517 mangleType(CT->getElementType());
518 } else if (const VectorType *VT = dyn_cast<VectorType>(T.getTypePtr())) {
519 // GNU extension: vector types
520 Out << "U8__vector";
521 mangleType(VT->getElementType());
Anders Carlssona40c5e42009-03-07 22:03:21 +0000522 } else if (const ObjCInterfaceType *IT =
523 dyn_cast<ObjCInterfaceType>(T.getTypePtr())) {
524 mangleType(IT);
Anders Carlsson4843e582009-03-10 17:07:44 +0000525 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000526 // FIXME: ::= G <type> # imaginary (C 2000)
527 // FIXME: ::= U <source-name> <type> # vendor extended type qualifier
528 else
529 assert(false && "Cannot mangle unknown type");
530}
531
532void CXXNameMangler::mangleType(const BuiltinType *T) {
533 // <builtin-type> ::= v # void
534 // ::= w # wchar_t
535 // ::= b # bool
536 // ::= c # char
537 // ::= a # signed char
538 // ::= h # unsigned char
539 // ::= s # short
540 // ::= t # unsigned short
541 // ::= i # int
542 // ::= j # unsigned int
543 // ::= l # long
544 // ::= m # unsigned long
545 // ::= x # long long, __int64
546 // ::= y # unsigned long long, __int64
547 // ::= n # __int128
548 // UNSUPPORTED: ::= o # unsigned __int128
549 // ::= f # float
550 // ::= d # double
551 // ::= e # long double, __float80
552 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000553 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
554 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
555 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
556 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000557 // ::= Di # char32_t
558 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000559 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000560 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
561 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000562 switch (T->getKind()) {
563 case BuiltinType::Void: Out << 'v'; break;
564 case BuiltinType::Bool: Out << 'b'; break;
565 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
566 case BuiltinType::UChar: Out << 'h'; break;
567 case BuiltinType::UShort: Out << 't'; break;
568 case BuiltinType::UInt: Out << 'j'; break;
569 case BuiltinType::ULong: Out << 'm'; break;
570 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000571 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000572 case BuiltinType::SChar: Out << 'a'; break;
573 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000574 case BuiltinType::Char16: Out << "Ds"; break;
575 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000576 case BuiltinType::Short: Out << 's'; break;
577 case BuiltinType::Int: Out << 'i'; break;
578 case BuiltinType::Long: Out << 'l'; break;
579 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000580 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000581 case BuiltinType::Float: Out << 'f'; break;
582 case BuiltinType::Double: Out << 'd'; break;
583 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000584 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000585
586 case BuiltinType::Overload:
587 case BuiltinType::Dependent:
588 assert(false &&
589 "Overloaded and dependent types shouldn't get to name mangling");
590 break;
Anders Carlssone89d1592009-06-26 18:41:36 +0000591 case BuiltinType::UndeducedAuto:
592 assert(0 && "Should not see undeduced auto here");
593 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +0000594 case BuiltinType::ObjCId: Out << "11objc_object"; break;
595 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000596 }
597}
598
599void CXXNameMangler::mangleType(const FunctionType *T) {
600 // <function-type> ::= F [Y] <bare-function-type> E
601 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +0000602 // FIXME: We don't have enough information in the AST to produce the 'Y'
603 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000604 mangleBareFunctionType(T, /*MangleReturnType=*/true);
605 Out << 'E';
606}
607
608void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
609 bool MangleReturnType) {
610 // <bare-function-type> ::= <signature type>+
611 if (MangleReturnType)
612 mangleType(T->getResultType());
613
Douglas Gregor72564e72009-02-26 23:50:07 +0000614 const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000615 assert(Proto && "Can't mangle K&R function prototypes");
616
Anders Carlssonc6c91bc2009-04-01 00:15:23 +0000617 if (Proto->getNumArgs() == 0) {
618 Out << 'v';
619 return;
620 }
621
Douglas Gregor72564e72009-02-26 23:50:07 +0000622 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000623 ArgEnd = Proto->arg_type_end();
624 Arg != ArgEnd; ++Arg)
625 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +0000626
627 // <builtin-type> ::= z # ellipsis
628 if (Proto->isVariadic())
629 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000630}
631
632void CXXNameMangler::mangleType(const TagType *T) {
633 // <class-enum-type> ::= <name>
Anders Carlsson4843e582009-03-10 17:07:44 +0000634
635 if (!T->getDecl()->getIdentifier())
636 mangleName(T->getDecl()->getTypedefForAnonDecl());
637 else
638 mangleName(T->getDecl());
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000639
640 // If this is a class template specialization, mangle the template
641 // arguments.
642 if (ClassTemplateSpecializationDecl *Spec
643 = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl()))
644 mangleTemplateArgumentList(Spec->getTemplateArgs());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000645}
646
647void CXXNameMangler::mangleType(const ArrayType *T) {
648 // <array-type> ::= A <positive dimension number> _ <element type>
649 // ::= A [<dimension expression>] _ <element type>
650 Out << 'A';
651 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T))
652 Out << CAT->getSize();
653 else if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(T))
654 mangleExpression(VAT->getSizeExpr());
655 else if (const DependentSizedArrayType *DSAT
656 = dyn_cast<DependentSizedArrayType>(T))
657 mangleExpression(DSAT->getSizeExpr());
658
659 Out << '_';
660 mangleType(T->getElementType());
661}
662
663void CXXNameMangler::mangleType(const MemberPointerType *T) {
664 // <pointer-to-member-type> ::= M <class type> <member type>
665 Out << 'M';
666 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +0000667 QualType PointeeType = T->getPointeeType();
668 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
669 mangleCVQualifiers(FPT->getTypeQuals());
670 mangleType(FPT);
671 } else
672 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000673}
674
675void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
676 // <template-param> ::= T_ # first template parameter
677 // ::= T <parameter-2 non-negative number> _
678 if (T->getIndex() == 0)
679 Out << "T_";
680 else
681 Out << 'T' << (T->getIndex() - 1) << '_';
682}
683
Anders Carlssona40c5e42009-03-07 22:03:21 +0000684void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
685 mangleSourceName(T->getDecl()->getIdentifier());
686}
687
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000688void CXXNameMangler::mangleExpression(Expr *E) {
689 assert(false && "Cannot mangle expressions yet");
690}
691
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000692void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
693 // <ctor-dtor-name> ::= C1 # complete object constructor
694 // ::= C2 # base object constructor
695 // ::= C3 # complete object allocating constructor
696 //
697 switch (T) {
698 case Ctor_Complete:
699 Out << "C1";
700 break;
701 case Ctor_Base:
702 Out << "C2";
703 break;
704 case Ctor_CompleteAllocating:
705 Out << "C3";
706 break;
707 }
708}
709
Anders Carlsson27ae5362009-04-17 01:58:57 +0000710void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
711 // <ctor-dtor-name> ::= D0 # deleting destructor
712 // ::= D1 # complete object destructor
713 // ::= D2 # base object destructor
714 //
715 switch (T) {
716 case Dtor_Deleting:
717 Out << "D0";
718 break;
719 case Dtor_Complete:
720 Out << "D1";
721 break;
722 case Dtor_Base:
723 Out << "D2";
724 break;
725 }
726}
727
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000728void CXXNameMangler::mangleTemplateArgumentList(const TemplateArgumentList &L) {
729 // <template-args> ::= I <template-arg>+ E
730 Out << "I";
731
732 for (unsigned i = 0, e = L.size(); i != e; ++i) {
733 const TemplateArgument &A = L[i];
734
735 mangleTemplateArgument(A);
736 }
737
738 Out << "E";
739}
740
741void CXXNameMangler::mangleTemplateArgument(const TemplateArgument &A) {
742 // <template-arg> ::= <type> # type or template
743 // ::= X <expression> E # expression
744 // ::= <expr-primary> # simple expressions
745 // ::= I <template-arg>* E # argument pack
746 // ::= sp <expression> # pack expansion of (C++0x)
747 switch (A.getKind()) {
748 default:
749 assert(0 && "Unknown template argument kind!");
750 case TemplateArgument::Type:
751 mangleType(A.getAsType());
752 break;
753 case TemplateArgument::Integral:
754 // <expr-primary> ::= L <type> <value number> E # integer literal
755
756 Out << 'L';
757
758 mangleType(A.getIntegralType());
759
760 const llvm::APSInt *Integral = A.getAsIntegral();
761 if (A.getIntegralType()->isBooleanType()) {
762 // Boolean values are encoded as 0/1.
763 Out << (Integral->getBoolValue() ? '1' : '0');
764 } else {
765 if (Integral->isNegative())
766 Out << 'n';
767 Integral->abs().print(Out, false);
768 }
769
770 Out << 'E';
771 break;
772 }
773}
774
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000775namespace clang {
776 /// \brief Mangles the name of the declaration D and emits that name
777 /// to the given output stream.
778 ///
779 /// If the declaration D requires a mangled name, this routine will
780 /// emit that mangled name to \p os and return true. Otherwise, \p
781 /// os will be unchanged and this routine will return false. In this
782 /// case, the caller should just emit the identifier of the declaration
783 /// (\c D->getIdentifier()) as its name.
784 bool mangleName(const NamedDecl *D, ASTContext &Context,
785 llvm::raw_ostream &os) {
Anders Carlsson578aa642009-05-03 16:51:04 +0000786 assert(!isa<CXXConstructorDecl>(D) &&
787 "Use mangleCXXCtor for constructor decls!");
788 assert(!isa<CXXDestructorDecl>(D) &&
789 "Use mangleCXXDtor for destructor decls!");
790
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000791 CXXNameMangler Mangler(Context, os);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000792 if (!Mangler.mangle(D))
793 return false;
794
795 os.flush();
796 return true;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000797 }
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000798
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000799 /// mangleGuardVariable - Returns the mangled name for a guard variable
800 /// for the passed in VarDecl.
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000801 void mangleGuardVariable(const VarDecl *D, ASTContext &Context,
802 llvm::raw_ostream &os) {
803 CXXNameMangler Mangler(Context, os);
804 Mangler.mangleGuardVariable(D);
805
806 os.flush();
807 }
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000808
809 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
810 ASTContext &Context, llvm::raw_ostream &os) {
811 CXXNameMangler Mangler(Context, os);
812 Mangler.mangleCXXCtor(D, Type);
813
814 os.flush();
815 }
Anders Carlsson27ae5362009-04-17 01:58:57 +0000816
817 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
818 ASTContext &Context, llvm::raw_ostream &os) {
819 CXXNameMangler Mangler(Context, os);
820 Mangler.mangleCXXDtor(D, Type);
821
822 os.flush();
823 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000824
Mike Stumpf1216772009-07-31 18:25:34 +0000825 void mangleCXXVtable(QualType Type, ASTContext &Context,
826 llvm::raw_ostream &os) {
827 CXXNameMangler Mangler(Context, os);
828 Mangler.mangleCXXVtable(Type);
829
830 os.flush();
831 }
Mike Stump738f8c22009-07-31 23:15:31 +0000832
833 void mangleCXXRtti(QualType Type, ASTContext &Context,
834 llvm::raw_ostream &os) {
835 CXXNameMangler Mangler(Context, os);
836 Mangler.mangleCXXRtti(Type);
837
838 os.flush();
839 }
Mike Stumpf1216772009-07-31 18:25:34 +0000840}