blob: 29b4c8a837787e27afa1e0607d4a6e713457c7d0 [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);
Mike Stump9124bcc2009-09-02 00:56:18 +000042 void mangleCalloffset(bool Virtual, int64_t nv, int64_t v);
Mike Stump883f1272009-09-02 00:28:47 +000043 void mangleThunk(const NamedDecl *ND, bool Virtual, int64_t nv, int64_t v);
Mike Stump9124bcc2009-09-02 00:56:18 +000044 void mangleCovariantThunk(const NamedDecl *ND, bool VirtualThis,
45 int64_t nv_t, int64_t v_t, bool VirtualResult,
46 int64_t nv_r, int64_t v_r);
Anders Carlsson41aa8c12009-04-13 18:02:10 +000047 void mangleGuardVariable(const VarDecl *D);
Anders Carlsson27ae5362009-04-17 01:58:57 +000048
Mike Stumpf1216772009-07-31 18:25:34 +000049 void mangleCXXVtable(QualType Type);
Mike Stump738f8c22009-07-31 23:15:31 +000050 void mangleCXXRtti(QualType Type);
Anders Carlsson3ac86b52009-04-15 05:36:58 +000051 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type);
Anders Carlsson27ae5362009-04-17 01:58:57 +000052 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type);
Anders Carlsson3ac86b52009-04-15 05:36:58 +000053
Anders Carlsson43f17402009-04-02 15:51:53 +000054 private:
55 bool mangleFunctionDecl(const FunctionDecl *FD);
56
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000057 void mangleFunctionEncoding(const FunctionDecl *FD);
58 void mangleName(const NamedDecl *ND);
59 void mangleUnqualifiedName(const NamedDecl *ND);
60 void mangleSourceName(const IdentifierInfo *II);
Anders Carlsson1b42c792009-04-02 16:24:45 +000061 void mangleLocalName(const NamedDecl *ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000062 void mangleNestedName(const NamedDecl *ND);
63 void manglePrefix(const DeclContext *DC);
64 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
65 void mangleCVQualifiers(unsigned Quals);
66 void mangleType(QualType T);
67 void mangleType(const BuiltinType *T);
68 void mangleType(const FunctionType *T);
69 void mangleBareFunctionType(const FunctionType *T, bool MangleReturnType);
70 void mangleType(const TagType *T);
71 void mangleType(const ArrayType *T);
72 void mangleType(const MemberPointerType *T);
73 void mangleType(const TemplateTypeParmType *T);
Anders Carlssona40c5e42009-03-07 22:03:21 +000074 void mangleType(const ObjCInterfaceType *T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000075 void mangleExpression(Expr *E);
Anders Carlsson3ac86b52009-04-15 05:36:58 +000076 void mangleCXXCtorType(CXXCtorType T);
Anders Carlsson27ae5362009-04-17 01:58:57 +000077 void mangleCXXDtorType(CXXDtorType T);
Anders Carlsson7a0ba872009-05-15 16:09:15 +000078
79 void mangleTemplateArgumentList(const TemplateArgumentList &L);
80 void mangleTemplateArgument(const TemplateArgument &A);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000081 };
82}
83
Anders Carlsson43f17402009-04-02 15:51:53 +000084static bool isInCLinkageSpecification(const Decl *D) {
85 for (const DeclContext *DC = D->getDeclContext();
86 !DC->isTranslationUnit(); DC = DC->getParent()) {
87 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
88 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
89 }
90
91 return false;
92}
93
94bool CXXNameMangler::mangleFunctionDecl(const FunctionDecl *FD) {
Mike Stump141c5af2009-09-02 00:25:38 +000095 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
96 // (always).
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +000097 if (!FD->hasAttr<OverloadableAttr>()) {
Chris Lattner783601d2009-06-13 23:34:16 +000098 // C functions are not mangled, and "main" is never mangled.
John McCall07a5c222009-08-15 02:09:25 +000099 if (!Context.getLangOptions().CPlusPlus || FD->isMain(Context))
Chris Lattner783601d2009-06-13 23:34:16 +0000100 return false;
101
102 // No mangling in an "implicit extern C" header.
103 if (FD->getLocation().isValid() &&
104 Context.getSourceManager().isInExternCSystemHeader(FD->getLocation()))
105 return false;
106
107 // No name mangling in a C linkage specification.
108 if (isInCLinkageSpecification(FD))
109 return false;
110 }
Anders Carlsson43f17402009-04-02 15:51:53 +0000111
112 // If we get here, mangle the decl name!
113 Out << "_Z";
114 mangleFunctionEncoding(FD);
115 return true;
116}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000117
118bool CXXNameMangler::mangle(const NamedDecl *D) {
Mike Stump141c5af2009-09-02 00:25:38 +0000119 // Any decl can be declared with __asm("foo") on it, and this takes precedence
120 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000121 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000122 // If we have an asm name, then we use it as the mangling.
123 Out << '\01'; // LLVM IR Marker for __asm("foo")
124 Out << ALA->getLabel();
125 return true;
126 }
127
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000128 // <mangled-name> ::= _Z <encoding>
129 // ::= <data name>
130 // ::= <special-name>
131
132 // FIXME: Actually use a visitor to decode these?
Anders Carlsson43f17402009-04-02 15:51:53 +0000133 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
134 return mangleFunctionDecl(FD);
Chris Lattnerbc7a0292009-03-21 06:19:20 +0000135
Anders Carlsson329749c2009-04-02 16:05:20 +0000136 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
137 if (!Context.getLangOptions().CPlusPlus ||
Anders Carlsson9ccb0652009-04-11 01:19:45 +0000138 isInCLinkageSpecification(D) ||
139 D->getDeclContext()->isTranslationUnit())
Anders Carlsson329749c2009-04-02 16:05:20 +0000140 return false;
141
142 Out << "_Z";
143 mangleName(VD);
144 return true;
145 }
146
Anders Carlsson43f17402009-04-02 15:51:53 +0000147 return false;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000148}
149
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000150void CXXNameMangler::mangleCXXCtor(const CXXConstructorDecl *D,
151 CXXCtorType Type) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000152 assert(!Structor && "Structor already set!");
153 Structor = D;
154 StructorType = Type;
155
156 mangle(D);
157}
158
159void CXXNameMangler::mangleCXXDtor(const CXXDestructorDecl *D,
160 CXXDtorType Type) {
161 assert(!Structor && "Structor already set!");
162 Structor = D;
163 StructorType = Type;
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000164
165 mangle(D);
166}
167
Mike Stumpf1216772009-07-31 18:25:34 +0000168void CXXNameMangler::mangleCXXVtable(QualType T) {
169 // <special-name> ::= TV <type> # virtual table
170 Out << "_ZTV";
171 mangleType(T);
172}
173
Mike Stump738f8c22009-07-31 23:15:31 +0000174void CXXNameMangler::mangleCXXRtti(QualType T) {
175 // <special-name> ::= TI <type> # typeinfo structure
176 Out << "_ZTI";
177 mangleType(T);
178}
179
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000180void CXXNameMangler::mangleGuardVariable(const VarDecl *D)
181{
182 // <special-name> ::= GV <object name> # Guard variable for one-time
Mike Stumpf1216772009-07-31 18:25:34 +0000183 // # initialization
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000184
185 Out << "_ZGV";
186 mangleName(D);
187}
188
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000189void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
190 // <encoding> ::= <function name> <bare-function-type>
191 mangleName(FD);
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000192
Mike Stump141c5af2009-09-02 00:25:38 +0000193 // Whether the mangling of a function type includes the return type depends on
194 // the context and the nature of the function. The rules for deciding whether
195 // the return type is included are:
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000196 //
197 // 1. Template functions (names or types) have return types encoded, with
198 // the exceptions listed below.
199 // 2. Function types not appearing as part of a function name mangling,
200 // e.g. parameters, pointer types, etc., have return type encoded, with the
201 // exceptions listed below.
202 // 3. Non-template function names do not have return types encoded.
203 //
Mike Stump141c5af2009-09-02 00:25:38 +0000204 // The exceptions mentioned in (1) and (2) above, for which the return type is
205 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000206 // 1. Constructors.
207 // 2. Destructors.
208 // 3. Conversion operator functions, e.g. operator int.
209 bool MangleReturnType = false;
210 if (FD->getPrimaryTemplate() &&
211 !(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
212 isa<CXXConversionDecl>(FD)))
213 MangleReturnType = true;
214 mangleBareFunctionType(FD->getType()->getAsFunctionType(), MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000215}
216
217static bool isStdNamespace(const DeclContext *DC) {
218 if (!DC->isNamespace() || !DC->getParent()->isTranslationUnit())
219 return false;
220
221 const NamespaceDecl *NS = cast<NamespaceDecl>(DC);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000222 return NS->getOriginalNamespace()->getIdentifier()->isStr("std");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000223}
224
225void CXXNameMangler::mangleName(const NamedDecl *ND) {
226 // <name> ::= <nested-name>
227 // ::= <unscoped-name>
228 // ::= <unscoped-template-name> <template-args>
229 // ::= <local-name> # See Scope Encoding below
230 //
231 // <unscoped-name> ::= <unqualified-name>
232 // ::= St <unqualified-name> # ::std::
233 if (ND->getDeclContext()->isTranslationUnit())
234 mangleUnqualifiedName(ND);
235 else if (isStdNamespace(ND->getDeclContext())) {
236 Out << "St";
237 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000238 } else if (isa<FunctionDecl>(ND->getDeclContext()))
239 mangleLocalName(ND);
240 else
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000241 mangleNestedName(ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000242}
243
Mike Stump9124bcc2009-09-02 00:56:18 +0000244void CXXNameMangler::mangleCalloffset(bool Virtual, int64_t nv,
245 int64_t v) {
Mike Stump141c5af2009-09-02 00:25:38 +0000246 // <call-offset> ::= h <nv-offset> _
247 // ::= v <v-offset> _
248 // <nv-offset> ::= <offset number> # non-virtual base override
249 // <v-offset> ::= <offset nubmer> _ <virtual offset number>
250 // # virtual base override, with vcall offset
251 if (!Virtual) {
Mike Stump9124bcc2009-09-02 00:56:18 +0000252 Out << "h";
Mike Stump141c5af2009-09-02 00:25:38 +0000253 if (nv < 0) {
254 Out << "n";
255 nv = -nv;
256 }
257 Out << nv;
258 } else {
Mike Stump9124bcc2009-09-02 00:56:18 +0000259 Out << "v";
Mike Stump141c5af2009-09-02 00:25:38 +0000260 if (nv < 0) {
261 Out << "n";
262 nv = -nv;
263 }
264 Out << nv;
265 Out << "_";
266 if (v < 0) {
267 Out << "n";
268 v = -v;
269 }
270 Out << v;
271 }
272 Out << "_";
Mike Stump9124bcc2009-09-02 00:56:18 +0000273}
274
275void CXXNameMangler::mangleThunk(const NamedDecl *D, bool Virtual, int64_t nv,
276 int64_t v) {
277 // <special-name> ::= T <call-offset> <base encoding>
278 // # base is the nominal target function of thunk
279 Out << "_T";
280 mangleCalloffset(Virtual, nv, v);
281 mangleName(D);
282}
283
284 void CXXNameMangler::mangleCovariantThunk(const NamedDecl *D,
285 bool VirtualThis, int64_t nv_t,
286 int64_t v_t, bool VirtualResult,
287 int64_t nv_r, int64_t v_r) {
288 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
289 // # base is the nominal target function of thunk
290 // # first call-offset is 'this' adjustment
291 // # second call-offset is result adjustment
292 Out << "_Tc";
293 mangleCalloffset(VirtualThis, nv_t, v_t);
294 mangleCalloffset(VirtualResult, nv_r, v_r);
Mike Stump141c5af2009-09-02 00:25:38 +0000295 mangleName(D);
296}
297
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000298void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) {
299 // <unqualified-name> ::= <operator-name>
300 // ::= <ctor-dtor-name>
301 // ::= <source-name>
302 DeclarationName Name = ND->getDeclName();
303 switch (Name.getNameKind()) {
304 case DeclarationName::Identifier:
305 mangleSourceName(Name.getAsIdentifierInfo());
306 break;
307
308 case DeclarationName::ObjCZeroArgSelector:
309 case DeclarationName::ObjCOneArgSelector:
310 case DeclarationName::ObjCMultiArgSelector:
311 assert(false && "Can't mangle Objective-C selector names here!");
312 break;
313
314 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000315 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000316 // If the named decl is the C++ constructor we're mangling, use the type
317 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000318 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000319 else
320 // Otherwise, use the complete constructor name. This is relevant if a
321 // class with a constructor is declared within a constructor.
322 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000323 break;
324
325 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000326 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000327 // If the named decl is the C++ destructor we're mangling, use the type we
328 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000329 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
330 else
331 // Otherwise, use the complete destructor name. This is relevant if a
332 // class with a destructor is declared within a destructor.
333 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000334 break;
335
336 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor219cc612009-02-13 01:28:03 +0000337 // <operator-name> ::= cv <type> # (cast)
338 Out << "cv";
339 mangleType(Context.getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000340 break;
341
342 case DeclarationName::CXXOperatorName:
343 mangleOperatorName(Name.getCXXOverloadedOperator(),
344 cast<FunctionDecl>(ND)->getNumParams());
345 break;
346
347 case DeclarationName::CXXUsingDirective:
348 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000349 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000350 }
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000351
352 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
353 if (const TemplateArgumentList *TemplateArgs
354 = Function->getTemplateSpecializationArgs())
355 mangleTemplateArgumentList(*TemplateArgs);
356 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000357}
358
359void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
360 // <source-name> ::= <positive length number> <identifier>
361 // <number> ::= [n] <non-negative decimal integer>
362 // <identifier> ::= <unqualified source code identifier>
363 Out << II->getLength() << II->getName();
364}
365
366void CXXNameMangler::mangleNestedName(const NamedDecl *ND) {
367 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
368 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
369 // FIXME: no template support
370 Out << 'N';
371 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
372 mangleCVQualifiers(Method->getTypeQualifiers());
373 manglePrefix(ND->getDeclContext());
374 mangleUnqualifiedName(ND);
375 Out << 'E';
376}
377
Anders Carlsson1b42c792009-04-02 16:24:45 +0000378void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
379 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
380 // := Z <function encoding> E s [<discriminator>]
381 // <discriminator> := _ <non-negative number>
382 Out << 'Z';
383 mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext()));
384 Out << 'E';
385 mangleSourceName(ND->getIdentifier());
386}
387
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000388void CXXNameMangler::manglePrefix(const DeclContext *DC) {
389 // <prefix> ::= <prefix> <unqualified-name>
390 // ::= <template-prefix> <template-args>
391 // ::= <template-param>
392 // ::= # empty
393 // ::= <substitution>
394 // FIXME: We only handle mangling of namespaces and classes at the moment.
Anders Carlssonc8dee9c2009-04-01 00:42:16 +0000395 if (!DC->getParent()->isTranslationUnit())
396 manglePrefix(DC->getParent());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000397
398 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(DC))
399 mangleSourceName(Namespace->getIdentifier());
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000400 else if (const RecordDecl *Record = dyn_cast<RecordDecl>(DC)) {
401 if (const ClassTemplateSpecializationDecl *D =
402 dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
403 mangleType(QualType(D->getTypeForDecl(), 0));
404 } else
405 mangleSourceName(Record->getIdentifier());
406 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000407}
408
409void
410CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
411 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000412 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000413 case OO_New: Out << "nw"; break;
414 // ::= na # new[]
415 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000416 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000417 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000418 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000419 case OO_Array_Delete: Out << "da"; break;
420 // ::= ps # + (unary)
421 // ::= pl # +
422 case OO_Plus: Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000423 // ::= ng # - (unary)
424 // ::= mi # -
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000425 case OO_Minus: Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000426 // ::= ad # & (unary)
427 // ::= an # &
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000428 case OO_Amp: Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000429 // ::= de # * (unary)
430 // ::= ml # *
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000431 case OO_Star: Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000432 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000433 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000434 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000435 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000436 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000437 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000438 // ::= or # |
439 case OO_Pipe: Out << "or"; break;
440 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000441 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000442 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000443 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000444 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000445 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000446 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000447 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000448 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000449 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000450 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000451 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000452 // ::= rM # %=
453 case OO_PercentEqual: Out << "rM"; break;
454 // ::= aN # &=
455 case OO_AmpEqual: Out << "aN"; break;
456 // ::= oR # |=
457 case OO_PipeEqual: Out << "oR"; break;
458 // ::= eO # ^=
459 case OO_CaretEqual: Out << "eO"; break;
460 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000461 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000462 // ::= rs # >>
463 case OO_GreaterGreater: Out << "rs"; break;
464 // ::= lS # <<=
465 case OO_LessLessEqual: Out << "lS"; break;
466 // ::= rS # >>=
467 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000468 // ::= eq # ==
469 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000470 // ::= ne # !=
471 case OO_ExclaimEqual: Out << "ne"; break;
472 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000473 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000474 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000475 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000476 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000477 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000478 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000479 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000480 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000481 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000482 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000483 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000484 // ::= oo # ||
485 case OO_PipePipe: Out << "oo"; break;
486 // ::= pp # ++
487 case OO_PlusPlus: Out << "pp"; break;
488 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000489 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000490 // ::= cm # ,
491 case OO_Comma: Out << "cm"; break;
492 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000493 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000494 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000495 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000496 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000497 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000498 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000499 case OO_Subscript: Out << "ix"; break;
500 // UNSUPPORTED: ::= qu # ?
501
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000502 case OO_None:
503 case OO_Conditional:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000504 case NUM_OVERLOADED_OPERATORS:
Douglas Gregor6ec36682009-02-18 23:53:56 +0000505 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000506 break;
507 }
508}
509
510void CXXNameMangler::mangleCVQualifiers(unsigned Quals) {
511 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
512 if (Quals & QualType::Restrict)
513 Out << 'r';
514 if (Quals & QualType::Volatile)
515 Out << 'V';
516 if (Quals & QualType::Const)
517 Out << 'K';
518}
519
520void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000521 // Only operate on the canonical type!
522 T = Context.getCanonicalType(T);
523
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000524 // FIXME: Should we have a TypeNodes.def to make this easier? (YES!)
525
526 // <type> ::= <CV-qualifiers> <type>
527 mangleCVQualifiers(T.getCVRQualifiers());
528
529 // ::= <builtin-type>
Anders Carlsson4843e582009-03-10 17:07:44 +0000530 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr()))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000531 mangleType(BT);
532 // ::= <function-type>
533 else if (const FunctionType *FT = dyn_cast<FunctionType>(T.getTypePtr()))
534 mangleType(FT);
535 // ::= <class-enum-type>
536 else if (const TagType *TT = dyn_cast<TagType>(T.getTypePtr()))
537 mangleType(TT);
538 // ::= <array-type>
539 else if (const ArrayType *AT = dyn_cast<ArrayType>(T.getTypePtr()))
540 mangleType(AT);
541 // ::= <pointer-to-member-type>
542 else if (const MemberPointerType *MPT
543 = dyn_cast<MemberPointerType>(T.getTypePtr()))
544 mangleType(MPT);
545 // ::= <template-param>
546 else if (const TemplateTypeParmType *TypeParm
547 = dyn_cast<TemplateTypeParmType>(T.getTypePtr()))
548 mangleType(TypeParm);
549 // FIXME: ::= <template-template-param> <template-args>
550 // FIXME: ::= <substitution> # See Compression below
551 // ::= P <type> # pointer-to
552 else if (const PointerType *PT = dyn_cast<PointerType>(T.getTypePtr())) {
553 Out << 'P';
554 mangleType(PT->getPointeeType());
555 }
Steve Naroff14108da2009-07-10 23:34:53 +0000556 else if (const ObjCObjectPointerType *PT =
557 dyn_cast<ObjCObjectPointerType>(T.getTypePtr())) {
558 Out << 'P';
559 mangleType(PT->getPointeeType());
560 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000561 // ::= R <type> # reference-to
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000562 else if (const LValueReferenceType *RT =
563 dyn_cast<LValueReferenceType>(T.getTypePtr())) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000564 Out << 'R';
565 mangleType(RT->getPointeeType());
566 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000567 // ::= O <type> # rvalue reference-to (C++0x)
568 else if (const RValueReferenceType *RT =
569 dyn_cast<RValueReferenceType>(T.getTypePtr())) {
570 Out << 'O';
571 mangleType(RT->getPointeeType());
572 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000573 // ::= C <type> # complex pair (C 2000)
574 else if (const ComplexType *CT = dyn_cast<ComplexType>(T.getTypePtr())) {
575 Out << 'C';
576 mangleType(CT->getElementType());
577 } else if (const VectorType *VT = dyn_cast<VectorType>(T.getTypePtr())) {
578 // GNU extension: vector types
579 Out << "U8__vector";
580 mangleType(VT->getElementType());
Anders Carlssona40c5e42009-03-07 22:03:21 +0000581 } else if (const ObjCInterfaceType *IT =
582 dyn_cast<ObjCInterfaceType>(T.getTypePtr())) {
583 mangleType(IT);
John McCall2191b202009-09-05 06:31:47 +0000584 } else if (const ElaboratedType *ET =
585 dyn_cast<ElaboratedType>(T.getTypePtr())) {
586 mangleType(ET->getUnderlyingType());
Anders Carlsson4843e582009-03-10 17:07:44 +0000587 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000588 // FIXME: ::= G <type> # imaginary (C 2000)
589 // FIXME: ::= U <source-name> <type> # vendor extended type qualifier
590 else
591 assert(false && "Cannot mangle unknown type");
592}
593
594void CXXNameMangler::mangleType(const BuiltinType *T) {
595 // <builtin-type> ::= v # void
596 // ::= w # wchar_t
597 // ::= b # bool
598 // ::= c # char
599 // ::= a # signed char
600 // ::= h # unsigned char
601 // ::= s # short
602 // ::= t # unsigned short
603 // ::= i # int
604 // ::= j # unsigned int
605 // ::= l # long
606 // ::= m # unsigned long
607 // ::= x # long long, __int64
608 // ::= y # unsigned long long, __int64
609 // ::= n # __int128
610 // UNSUPPORTED: ::= o # unsigned __int128
611 // ::= f # float
612 // ::= d # double
613 // ::= e # long double, __float80
614 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000615 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
616 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
617 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
618 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000619 // ::= Di # char32_t
620 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000621 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000622 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
623 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000624 switch (T->getKind()) {
625 case BuiltinType::Void: Out << 'v'; break;
626 case BuiltinType::Bool: Out << 'b'; break;
627 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
628 case BuiltinType::UChar: Out << 'h'; break;
629 case BuiltinType::UShort: Out << 't'; break;
630 case BuiltinType::UInt: Out << 'j'; break;
631 case BuiltinType::ULong: Out << 'm'; break;
632 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000633 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000634 case BuiltinType::SChar: Out << 'a'; break;
635 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000636 case BuiltinType::Char16: Out << "Ds"; break;
637 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000638 case BuiltinType::Short: Out << 's'; break;
639 case BuiltinType::Int: Out << 'i'; break;
640 case BuiltinType::Long: Out << 'l'; break;
641 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000642 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000643 case BuiltinType::Float: Out << 'f'; break;
644 case BuiltinType::Double: Out << 'd'; break;
645 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000646 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000647
648 case BuiltinType::Overload:
649 case BuiltinType::Dependent:
650 assert(false &&
651 "Overloaded and dependent types shouldn't get to name mangling");
652 break;
Anders Carlssone89d1592009-06-26 18:41:36 +0000653 case BuiltinType::UndeducedAuto:
654 assert(0 && "Should not see undeduced auto here");
655 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +0000656 case BuiltinType::ObjCId: Out << "11objc_object"; break;
657 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000658 }
659}
660
661void CXXNameMangler::mangleType(const FunctionType *T) {
662 // <function-type> ::= F [Y] <bare-function-type> E
663 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +0000664 // FIXME: We don't have enough information in the AST to produce the 'Y'
665 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000666 mangleBareFunctionType(T, /*MangleReturnType=*/true);
667 Out << 'E';
668}
669
670void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
671 bool MangleReturnType) {
672 // <bare-function-type> ::= <signature type>+
673 if (MangleReturnType)
674 mangleType(T->getResultType());
675
Douglas Gregor72564e72009-02-26 23:50:07 +0000676 const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000677 assert(Proto && "Can't mangle K&R function prototypes");
678
Anders Carlssonc6c91bc2009-04-01 00:15:23 +0000679 if (Proto->getNumArgs() == 0) {
680 Out << 'v';
681 return;
682 }
683
Douglas Gregor72564e72009-02-26 23:50:07 +0000684 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000685 ArgEnd = Proto->arg_type_end();
686 Arg != ArgEnd; ++Arg)
687 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +0000688
689 // <builtin-type> ::= z # ellipsis
690 if (Proto->isVariadic())
691 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000692}
693
694void CXXNameMangler::mangleType(const TagType *T) {
695 // <class-enum-type> ::= <name>
Anders Carlsson4843e582009-03-10 17:07:44 +0000696
697 if (!T->getDecl()->getIdentifier())
698 mangleName(T->getDecl()->getTypedefForAnonDecl());
699 else
700 mangleName(T->getDecl());
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000701
Mike Stump141c5af2009-09-02 00:25:38 +0000702 // If this is a class template specialization, mangle the template arguments.
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000703 if (ClassTemplateSpecializationDecl *Spec
704 = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl()))
705 mangleTemplateArgumentList(Spec->getTemplateArgs());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000706}
707
708void CXXNameMangler::mangleType(const ArrayType *T) {
709 // <array-type> ::= A <positive dimension number> _ <element type>
710 // ::= A [<dimension expression>] _ <element type>
711 Out << 'A';
712 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T))
713 Out << CAT->getSize();
714 else if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(T))
715 mangleExpression(VAT->getSizeExpr());
716 else if (const DependentSizedArrayType *DSAT
717 = dyn_cast<DependentSizedArrayType>(T))
718 mangleExpression(DSAT->getSizeExpr());
719
720 Out << '_';
721 mangleType(T->getElementType());
722}
723
724void CXXNameMangler::mangleType(const MemberPointerType *T) {
725 // <pointer-to-member-type> ::= M <class type> <member type>
726 Out << 'M';
727 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +0000728 QualType PointeeType = T->getPointeeType();
729 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
730 mangleCVQualifiers(FPT->getTypeQuals());
731 mangleType(FPT);
732 } else
733 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000734}
735
736void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
737 // <template-param> ::= T_ # first template parameter
738 // ::= T <parameter-2 non-negative number> _
739 if (T->getIndex() == 0)
740 Out << "T_";
741 else
742 Out << 'T' << (T->getIndex() - 1) << '_';
743}
744
Anders Carlssona40c5e42009-03-07 22:03:21 +0000745void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
746 mangleSourceName(T->getDecl()->getIdentifier());
747}
748
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000749void CXXNameMangler::mangleExpression(Expr *E) {
750 assert(false && "Cannot mangle expressions yet");
751}
752
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000753void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
754 // <ctor-dtor-name> ::= C1 # complete object constructor
755 // ::= C2 # base object constructor
756 // ::= C3 # complete object allocating constructor
757 //
758 switch (T) {
759 case Ctor_Complete:
760 Out << "C1";
761 break;
762 case Ctor_Base:
763 Out << "C2";
764 break;
765 case Ctor_CompleteAllocating:
766 Out << "C3";
767 break;
768 }
769}
770
Anders Carlsson27ae5362009-04-17 01:58:57 +0000771void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
772 // <ctor-dtor-name> ::= D0 # deleting destructor
773 // ::= D1 # complete object destructor
774 // ::= D2 # base object destructor
775 //
776 switch (T) {
777 case Dtor_Deleting:
778 Out << "D0";
779 break;
780 case Dtor_Complete:
781 Out << "D1";
782 break;
783 case Dtor_Base:
784 Out << "D2";
785 break;
786 }
787}
788
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000789void CXXNameMangler::mangleTemplateArgumentList(const TemplateArgumentList &L) {
790 // <template-args> ::= I <template-arg>+ E
791 Out << "I";
792
793 for (unsigned i = 0, e = L.size(); i != e; ++i) {
794 const TemplateArgument &A = L[i];
795
796 mangleTemplateArgument(A);
797 }
798
799 Out << "E";
800}
801
802void CXXNameMangler::mangleTemplateArgument(const TemplateArgument &A) {
803 // <template-arg> ::= <type> # type or template
804 // ::= X <expression> E # expression
805 // ::= <expr-primary> # simple expressions
806 // ::= I <template-arg>* E # argument pack
807 // ::= sp <expression> # pack expansion of (C++0x)
808 switch (A.getKind()) {
809 default:
810 assert(0 && "Unknown template argument kind!");
811 case TemplateArgument::Type:
812 mangleType(A.getAsType());
813 break;
814 case TemplateArgument::Integral:
815 // <expr-primary> ::= L <type> <value number> E # integer literal
816
817 Out << 'L';
818
819 mangleType(A.getIntegralType());
820
821 const llvm::APSInt *Integral = A.getAsIntegral();
822 if (A.getIntegralType()->isBooleanType()) {
823 // Boolean values are encoded as 0/1.
824 Out << (Integral->getBoolValue() ? '1' : '0');
825 } else {
826 if (Integral->isNegative())
827 Out << 'n';
828 Integral->abs().print(Out, false);
829 }
830
831 Out << 'E';
832 break;
833 }
834}
835
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000836namespace clang {
Mike Stump141c5af2009-09-02 00:25:38 +0000837 /// \brief Mangles the name of the declaration D and emits that name to the
838 /// given output stream.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000839 ///
Mike Stump141c5af2009-09-02 00:25:38 +0000840 /// If the declaration D requires a mangled name, this routine will emit that
841 /// mangled name to \p os and return true. Otherwise, \p os will be unchanged
842 /// and this routine will return false. In this case, the caller should just
843 /// emit the identifier of the declaration (\c D->getIdentifier()) as its
844 /// name.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000845 bool mangleName(const NamedDecl *D, ASTContext &Context,
846 llvm::raw_ostream &os) {
Anders Carlsson578aa642009-05-03 16:51:04 +0000847 assert(!isa<CXXConstructorDecl>(D) &&
848 "Use mangleCXXCtor for constructor decls!");
849 assert(!isa<CXXDestructorDecl>(D) &&
850 "Use mangleCXXDtor for destructor decls!");
851
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000852 CXXNameMangler Mangler(Context, os);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000853 if (!Mangler.mangle(D))
854 return false;
855
856 os.flush();
857 return true;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000858 }
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000859
Mike Stump141c5af2009-09-02 00:25:38 +0000860 /// \brief Mangles the a thunk with the offset n for the declaration D and
861 /// emits that name to the given output stream.
Mike Stump883f1272009-09-02 00:28:47 +0000862 void mangleThunk(const NamedDecl *D, bool Virtual, int64_t nv, int64_t v,
863 ASTContext &Context, llvm::raw_ostream &os) {
Mike Stump141c5af2009-09-02 00:25:38 +0000864 // FIXME: Hum, we might have to thunk these, fix.
865 assert(!isa<CXXConstructorDecl>(D) &&
866 "Use mangleCXXCtor for constructor decls!");
867 assert(!isa<CXXDestructorDecl>(D) &&
868 "Use mangleCXXDtor for destructor decls!");
869
870 CXXNameMangler Mangler(Context, os);
Mike Stump883f1272009-09-02 00:28:47 +0000871 Mangler.mangleThunk(D, Virtual, nv, v);
Mike Stump141c5af2009-09-02 00:25:38 +0000872 os.flush();
873 }
874
Mike Stump9124bcc2009-09-02 00:56:18 +0000875 /// \brief Mangles the a covariant thunk for the declaration D and emits that
876 /// name to the given output stream.
877 void mangleCovariantThunk(const NamedDecl *D, bool VirtualThis, int64_t nv_t,
878 int64_t v_t, bool VirtualResult, int64_t nv_r,
879 int64_t v_r, ASTContext &Context,
880 llvm::raw_ostream &os) {
881 // FIXME: Hum, we might have to thunk these, fix.
882 assert(!isa<CXXConstructorDecl>(D) &&
883 "Use mangleCXXCtor for constructor decls!");
884 assert(!isa<CXXDestructorDecl>(D) &&
885 "Use mangleCXXDtor for destructor decls!");
886
887 CXXNameMangler Mangler(Context, os);
888 Mangler.mangleCovariantThunk(D, VirtualThis, nv_t, v_t, VirtualResult,
889 nv_r, v_r);
890 os.flush();
891 }
892
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000893 /// mangleGuardVariable - Returns the mangled name for a guard variable
894 /// for the passed in VarDecl.
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000895 void mangleGuardVariable(const VarDecl *D, ASTContext &Context,
896 llvm::raw_ostream &os) {
897 CXXNameMangler Mangler(Context, os);
898 Mangler.mangleGuardVariable(D);
899
900 os.flush();
901 }
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000902
903 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
904 ASTContext &Context, llvm::raw_ostream &os) {
905 CXXNameMangler Mangler(Context, os);
906 Mangler.mangleCXXCtor(D, Type);
907
908 os.flush();
909 }
Anders Carlsson27ae5362009-04-17 01:58:57 +0000910
911 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
912 ASTContext &Context, llvm::raw_ostream &os) {
913 CXXNameMangler Mangler(Context, os);
914 Mangler.mangleCXXDtor(D, Type);
915
916 os.flush();
917 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000918
Mike Stumpf1216772009-07-31 18:25:34 +0000919 void mangleCXXVtable(QualType Type, ASTContext &Context,
920 llvm::raw_ostream &os) {
921 CXXNameMangler Mangler(Context, os);
922 Mangler.mangleCXXVtable(Type);
923
924 os.flush();
925 }
Mike Stump738f8c22009-07-31 23:15:31 +0000926
927 void mangleCXXRtti(QualType Type, ASTContext &Context,
928 llvm::raw_ostream &os) {
929 CXXNameMangler Mangler(Context, os);
930 Mangler.mangleCXXRtti(Type);
931
932 os.flush();
933 }
Mike Stumpf1216772009-07-31 18:25:34 +0000934}