blob: b293560ec90fa44fbdd8062536ba92c18a3a1bec [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);
Anders Carlsson4843e582009-03-10 17:07:44 +0000584 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000585 // FIXME: ::= G <type> # imaginary (C 2000)
586 // FIXME: ::= U <source-name> <type> # vendor extended type qualifier
587 else
588 assert(false && "Cannot mangle unknown type");
589}
590
591void CXXNameMangler::mangleType(const BuiltinType *T) {
592 // <builtin-type> ::= v # void
593 // ::= w # wchar_t
594 // ::= b # bool
595 // ::= c # char
596 // ::= a # signed char
597 // ::= h # unsigned char
598 // ::= s # short
599 // ::= t # unsigned short
600 // ::= i # int
601 // ::= j # unsigned int
602 // ::= l # long
603 // ::= m # unsigned long
604 // ::= x # long long, __int64
605 // ::= y # unsigned long long, __int64
606 // ::= n # __int128
607 // UNSUPPORTED: ::= o # unsigned __int128
608 // ::= f # float
609 // ::= d # double
610 // ::= e # long double, __float80
611 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000612 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
613 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
614 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
615 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000616 // ::= Di # char32_t
617 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000618 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000619 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
620 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000621 switch (T->getKind()) {
622 case BuiltinType::Void: Out << 'v'; break;
623 case BuiltinType::Bool: Out << 'b'; break;
624 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
625 case BuiltinType::UChar: Out << 'h'; break;
626 case BuiltinType::UShort: Out << 't'; break;
627 case BuiltinType::UInt: Out << 'j'; break;
628 case BuiltinType::ULong: Out << 'm'; break;
629 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000630 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000631 case BuiltinType::SChar: Out << 'a'; break;
632 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000633 case BuiltinType::Char16: Out << "Ds"; break;
634 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000635 case BuiltinType::Short: Out << 's'; break;
636 case BuiltinType::Int: Out << 'i'; break;
637 case BuiltinType::Long: Out << 'l'; break;
638 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000639 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000640 case BuiltinType::Float: Out << 'f'; break;
641 case BuiltinType::Double: Out << 'd'; break;
642 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000643 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000644
645 case BuiltinType::Overload:
646 case BuiltinType::Dependent:
647 assert(false &&
648 "Overloaded and dependent types shouldn't get to name mangling");
649 break;
Anders Carlssone89d1592009-06-26 18:41:36 +0000650 case BuiltinType::UndeducedAuto:
651 assert(0 && "Should not see undeduced auto here");
652 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +0000653 case BuiltinType::ObjCId: Out << "11objc_object"; break;
654 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000655 }
656}
657
658void CXXNameMangler::mangleType(const FunctionType *T) {
659 // <function-type> ::= F [Y] <bare-function-type> E
660 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +0000661 // FIXME: We don't have enough information in the AST to produce the 'Y'
662 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000663 mangleBareFunctionType(T, /*MangleReturnType=*/true);
664 Out << 'E';
665}
666
667void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
668 bool MangleReturnType) {
669 // <bare-function-type> ::= <signature type>+
670 if (MangleReturnType)
671 mangleType(T->getResultType());
672
Douglas Gregor72564e72009-02-26 23:50:07 +0000673 const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000674 assert(Proto && "Can't mangle K&R function prototypes");
675
Anders Carlssonc6c91bc2009-04-01 00:15:23 +0000676 if (Proto->getNumArgs() == 0) {
677 Out << 'v';
678 return;
679 }
680
Douglas Gregor72564e72009-02-26 23:50:07 +0000681 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000682 ArgEnd = Proto->arg_type_end();
683 Arg != ArgEnd; ++Arg)
684 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +0000685
686 // <builtin-type> ::= z # ellipsis
687 if (Proto->isVariadic())
688 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000689}
690
691void CXXNameMangler::mangleType(const TagType *T) {
692 // <class-enum-type> ::= <name>
Anders Carlsson4843e582009-03-10 17:07:44 +0000693
694 if (!T->getDecl()->getIdentifier())
695 mangleName(T->getDecl()->getTypedefForAnonDecl());
696 else
697 mangleName(T->getDecl());
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000698
Mike Stump141c5af2009-09-02 00:25:38 +0000699 // If this is a class template specialization, mangle the template arguments.
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000700 if (ClassTemplateSpecializationDecl *Spec
701 = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl()))
702 mangleTemplateArgumentList(Spec->getTemplateArgs());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000703}
704
705void CXXNameMangler::mangleType(const ArrayType *T) {
706 // <array-type> ::= A <positive dimension number> _ <element type>
707 // ::= A [<dimension expression>] _ <element type>
708 Out << 'A';
709 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T))
710 Out << CAT->getSize();
711 else if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(T))
712 mangleExpression(VAT->getSizeExpr());
713 else if (const DependentSizedArrayType *DSAT
714 = dyn_cast<DependentSizedArrayType>(T))
715 mangleExpression(DSAT->getSizeExpr());
716
717 Out << '_';
718 mangleType(T->getElementType());
719}
720
721void CXXNameMangler::mangleType(const MemberPointerType *T) {
722 // <pointer-to-member-type> ::= M <class type> <member type>
723 Out << 'M';
724 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +0000725 QualType PointeeType = T->getPointeeType();
726 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
727 mangleCVQualifiers(FPT->getTypeQuals());
728 mangleType(FPT);
729 } else
730 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000731}
732
733void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
734 // <template-param> ::= T_ # first template parameter
735 // ::= T <parameter-2 non-negative number> _
736 if (T->getIndex() == 0)
737 Out << "T_";
738 else
739 Out << 'T' << (T->getIndex() - 1) << '_';
740}
741
Anders Carlssona40c5e42009-03-07 22:03:21 +0000742void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
743 mangleSourceName(T->getDecl()->getIdentifier());
744}
745
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000746void CXXNameMangler::mangleExpression(Expr *E) {
747 assert(false && "Cannot mangle expressions yet");
748}
749
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000750void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
751 // <ctor-dtor-name> ::= C1 # complete object constructor
752 // ::= C2 # base object constructor
753 // ::= C3 # complete object allocating constructor
754 //
755 switch (T) {
756 case Ctor_Complete:
757 Out << "C1";
758 break;
759 case Ctor_Base:
760 Out << "C2";
761 break;
762 case Ctor_CompleteAllocating:
763 Out << "C3";
764 break;
765 }
766}
767
Anders Carlsson27ae5362009-04-17 01:58:57 +0000768void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
769 // <ctor-dtor-name> ::= D0 # deleting destructor
770 // ::= D1 # complete object destructor
771 // ::= D2 # base object destructor
772 //
773 switch (T) {
774 case Dtor_Deleting:
775 Out << "D0";
776 break;
777 case Dtor_Complete:
778 Out << "D1";
779 break;
780 case Dtor_Base:
781 Out << "D2";
782 break;
783 }
784}
785
Anders Carlsson7a0ba872009-05-15 16:09:15 +0000786void CXXNameMangler::mangleTemplateArgumentList(const TemplateArgumentList &L) {
787 // <template-args> ::= I <template-arg>+ E
788 Out << "I";
789
790 for (unsigned i = 0, e = L.size(); i != e; ++i) {
791 const TemplateArgument &A = L[i];
792
793 mangleTemplateArgument(A);
794 }
795
796 Out << "E";
797}
798
799void CXXNameMangler::mangleTemplateArgument(const TemplateArgument &A) {
800 // <template-arg> ::= <type> # type or template
801 // ::= X <expression> E # expression
802 // ::= <expr-primary> # simple expressions
803 // ::= I <template-arg>* E # argument pack
804 // ::= sp <expression> # pack expansion of (C++0x)
805 switch (A.getKind()) {
806 default:
807 assert(0 && "Unknown template argument kind!");
808 case TemplateArgument::Type:
809 mangleType(A.getAsType());
810 break;
811 case TemplateArgument::Integral:
812 // <expr-primary> ::= L <type> <value number> E # integer literal
813
814 Out << 'L';
815
816 mangleType(A.getIntegralType());
817
818 const llvm::APSInt *Integral = A.getAsIntegral();
819 if (A.getIntegralType()->isBooleanType()) {
820 // Boolean values are encoded as 0/1.
821 Out << (Integral->getBoolValue() ? '1' : '0');
822 } else {
823 if (Integral->isNegative())
824 Out << 'n';
825 Integral->abs().print(Out, false);
826 }
827
828 Out << 'E';
829 break;
830 }
831}
832
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000833namespace clang {
Mike Stump141c5af2009-09-02 00:25:38 +0000834 /// \brief Mangles the name of the declaration D and emits that name to the
835 /// given output stream.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000836 ///
Mike Stump141c5af2009-09-02 00:25:38 +0000837 /// If the declaration D requires a mangled name, this routine will emit that
838 /// mangled name to \p os and return true. Otherwise, \p os will be unchanged
839 /// and this routine will return false. In this case, the caller should just
840 /// emit the identifier of the declaration (\c D->getIdentifier()) as its
841 /// name.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000842 bool mangleName(const NamedDecl *D, ASTContext &Context,
843 llvm::raw_ostream &os) {
Anders Carlsson578aa642009-05-03 16:51:04 +0000844 assert(!isa<CXXConstructorDecl>(D) &&
845 "Use mangleCXXCtor for constructor decls!");
846 assert(!isa<CXXDestructorDecl>(D) &&
847 "Use mangleCXXDtor for destructor decls!");
848
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000849 CXXNameMangler Mangler(Context, os);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000850 if (!Mangler.mangle(D))
851 return false;
852
853 os.flush();
854 return true;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000855 }
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000856
Mike Stump141c5af2009-09-02 00:25:38 +0000857 /// \brief Mangles the a thunk with the offset n for the declaration D and
858 /// emits that name to the given output stream.
Mike Stump883f1272009-09-02 00:28:47 +0000859 void mangleThunk(const NamedDecl *D, bool Virtual, int64_t nv, int64_t v,
860 ASTContext &Context, llvm::raw_ostream &os) {
Mike Stump141c5af2009-09-02 00:25:38 +0000861 // FIXME: Hum, we might have to thunk these, fix.
862 assert(!isa<CXXConstructorDecl>(D) &&
863 "Use mangleCXXCtor for constructor decls!");
864 assert(!isa<CXXDestructorDecl>(D) &&
865 "Use mangleCXXDtor for destructor decls!");
866
867 CXXNameMangler Mangler(Context, os);
Mike Stump883f1272009-09-02 00:28:47 +0000868 Mangler.mangleThunk(D, Virtual, nv, v);
Mike Stump141c5af2009-09-02 00:25:38 +0000869 os.flush();
870 }
871
Mike Stump9124bcc2009-09-02 00:56:18 +0000872 /// \brief Mangles the a covariant thunk for the declaration D and emits that
873 /// name to the given output stream.
874 void mangleCovariantThunk(const NamedDecl *D, bool VirtualThis, int64_t nv_t,
875 int64_t v_t, bool VirtualResult, int64_t nv_r,
876 int64_t v_r, ASTContext &Context,
877 llvm::raw_ostream &os) {
878 // FIXME: Hum, we might have to thunk these, fix.
879 assert(!isa<CXXConstructorDecl>(D) &&
880 "Use mangleCXXCtor for constructor decls!");
881 assert(!isa<CXXDestructorDecl>(D) &&
882 "Use mangleCXXDtor for destructor decls!");
883
884 CXXNameMangler Mangler(Context, os);
885 Mangler.mangleCovariantThunk(D, VirtualThis, nv_t, v_t, VirtualResult,
886 nv_r, v_r);
887 os.flush();
888 }
889
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000890 /// mangleGuardVariable - Returns the mangled name for a guard variable
891 /// for the passed in VarDecl.
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000892 void mangleGuardVariable(const VarDecl *D, ASTContext &Context,
893 llvm::raw_ostream &os) {
894 CXXNameMangler Mangler(Context, os);
895 Mangler.mangleGuardVariable(D);
896
897 os.flush();
898 }
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000899
900 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
901 ASTContext &Context, llvm::raw_ostream &os) {
902 CXXNameMangler Mangler(Context, os);
903 Mangler.mangleCXXCtor(D, Type);
904
905 os.flush();
906 }
Anders Carlsson27ae5362009-04-17 01:58:57 +0000907
908 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
909 ASTContext &Context, llvm::raw_ostream &os) {
910 CXXNameMangler Mangler(Context, os);
911 Mangler.mangleCXXDtor(D, Type);
912
913 os.flush();
914 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000915
Mike Stumpf1216772009-07-31 18:25:34 +0000916 void mangleCXXVtable(QualType Type, ASTContext &Context,
917 llvm::raw_ostream &os) {
918 CXXNameMangler Mangler(Context, os);
919 Mangler.mangleCXXVtable(Type);
920
921 os.flush();
922 }
Mike Stump738f8c22009-07-31 23:15:31 +0000923
924 void mangleCXXRtti(QualType Type, ASTContext &Context,
925 llvm::raw_ostream &os) {
926 CXXNameMangler Mangler(Context, os);
927 Mangler.mangleCXXRtti(Type);
928
929 os.flush();
930 }
Mike Stumpf1216772009-07-31 18:25:34 +0000931}