blob: 049353faae99bf428d93f0078996d0b392e81c91 [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"
Douglas Gregor6ec36682009-02-18 23:53:56 +000022#include "clang/Basic/SourceManager.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000023#include "llvm/Support/Compiler.h"
24#include "llvm/Support/raw_ostream.h"
25using namespace clang;
26
27namespace {
28 class VISIBILITY_HIDDEN CXXNameMangler {
29 ASTContext &Context;
30 llvm::raw_ostream &Out;
31
32 public:
33 CXXNameMangler(ASTContext &C, llvm::raw_ostream &os)
34 : Context(C), Out(os) { }
35
36 bool mangle(const NamedDecl *D);
Anders Carlsson41aa8c12009-04-13 18:02:10 +000037 void mangleGuardVariable(const VarDecl *D);
Anders Carlsson43f17402009-04-02 15:51:53 +000038
39 private:
40 bool mangleFunctionDecl(const FunctionDecl *FD);
41
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000042 void mangleFunctionEncoding(const FunctionDecl *FD);
43 void mangleName(const NamedDecl *ND);
44 void mangleUnqualifiedName(const NamedDecl *ND);
45 void mangleSourceName(const IdentifierInfo *II);
Anders Carlsson1b42c792009-04-02 16:24:45 +000046 void mangleLocalName(const NamedDecl *ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000047 void mangleNestedName(const NamedDecl *ND);
48 void manglePrefix(const DeclContext *DC);
49 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
50 void mangleCVQualifiers(unsigned Quals);
51 void mangleType(QualType T);
52 void mangleType(const BuiltinType *T);
53 void mangleType(const FunctionType *T);
54 void mangleBareFunctionType(const FunctionType *T, bool MangleReturnType);
55 void mangleType(const TagType *T);
56 void mangleType(const ArrayType *T);
57 void mangleType(const MemberPointerType *T);
58 void mangleType(const TemplateTypeParmType *T);
Anders Carlssona40c5e42009-03-07 22:03:21 +000059 void mangleType(const ObjCInterfaceType *T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000060 void mangleExpression(Expr *E);
61 };
62}
63
Anders Carlsson43f17402009-04-02 15:51:53 +000064static bool isInCLinkageSpecification(const Decl *D) {
65 for (const DeclContext *DC = D->getDeclContext();
66 !DC->isTranslationUnit(); DC = DC->getParent()) {
67 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
68 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
69 }
70
71 return false;
72}
73
74bool CXXNameMangler::mangleFunctionDecl(const FunctionDecl *FD) {
75 // Clang's "overloadable" attribute extension to C/C++ implies
76 // name mangling (always).
77 if (FD->getAttr<OverloadableAttr>()) {
78 ; // fall into mangling code unconditionally.
79 } else if (// C functions are not mangled
80 !Context.getLangOptions().CPlusPlus ||
81 // "main" is not mangled in C++
82 FD->isMain() ||
83 // No mangling in an "implicit extern C" header.
84 Context.getSourceManager().getFileCharacteristic(FD->getLocation())
85 == SrcMgr::C_ExternCSystem ||
86 // No name mangling in a C linkage specification.
87 isInCLinkageSpecification(FD))
88 return false;
89
90 // If we get here, mangle the decl name!
91 Out << "_Z";
92 mangleFunctionEncoding(FD);
93 return true;
94}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000095
96bool CXXNameMangler::mangle(const NamedDecl *D) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +000097 // Any decl can be declared with __asm("foo") on it, and this takes
98 // precedence over all other naming in the .o file.
99 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
100 // If we have an asm name, then we use it as the mangling.
101 Out << '\01'; // LLVM IR Marker for __asm("foo")
102 Out << ALA->getLabel();
103 return true;
104 }
105
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000106 // <mangled-name> ::= _Z <encoding>
107 // ::= <data name>
108 // ::= <special-name>
109
110 // FIXME: Actually use a visitor to decode these?
Anders Carlsson43f17402009-04-02 15:51:53 +0000111 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
112 return mangleFunctionDecl(FD);
Chris Lattnerbc7a0292009-03-21 06:19:20 +0000113
Anders Carlsson329749c2009-04-02 16:05:20 +0000114 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
115 if (!Context.getLangOptions().CPlusPlus ||
Anders Carlsson9ccb0652009-04-11 01:19:45 +0000116 isInCLinkageSpecification(D) ||
117 D->getDeclContext()->isTranslationUnit())
Anders Carlsson329749c2009-04-02 16:05:20 +0000118 return false;
119
120 Out << "_Z";
121 mangleName(VD);
122 return true;
123 }
124
Anders Carlsson43f17402009-04-02 15:51:53 +0000125 return false;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000126}
127
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000128void CXXNameMangler::mangleGuardVariable(const VarDecl *D)
129{
130 // <special-name> ::= GV <object name> # Guard variable for one-time
131 // # initialization
132
133 Out << "_ZGV";
134 mangleName(D);
135}
136
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000137void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
138 // <encoding> ::= <function name> <bare-function-type>
139 mangleName(FD);
140 mangleBareFunctionType(FD->getType()->getAsFunctionType(), false);
141}
142
143static bool isStdNamespace(const DeclContext *DC) {
144 if (!DC->isNamespace() || !DC->getParent()->isTranslationUnit())
145 return false;
146
147 const NamespaceDecl *NS = cast<NamespaceDecl>(DC);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000148 return NS->getOriginalNamespace()->getIdentifier()->isStr("std");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000149}
150
151void CXXNameMangler::mangleName(const NamedDecl *ND) {
152 // <name> ::= <nested-name>
153 // ::= <unscoped-name>
154 // ::= <unscoped-template-name> <template-args>
155 // ::= <local-name> # See Scope Encoding below
156 //
157 // <unscoped-name> ::= <unqualified-name>
158 // ::= St <unqualified-name> # ::std::
159 if (ND->getDeclContext()->isTranslationUnit())
160 mangleUnqualifiedName(ND);
161 else if (isStdNamespace(ND->getDeclContext())) {
162 Out << "St";
163 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000164 } else if (isa<FunctionDecl>(ND->getDeclContext()))
165 mangleLocalName(ND);
166 else
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000167 mangleNestedName(ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000168}
169
170void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) {
171 // <unqualified-name> ::= <operator-name>
172 // ::= <ctor-dtor-name>
173 // ::= <source-name>
174 DeclarationName Name = ND->getDeclName();
175 switch (Name.getNameKind()) {
176 case DeclarationName::Identifier:
177 mangleSourceName(Name.getAsIdentifierInfo());
178 break;
179
180 case DeclarationName::ObjCZeroArgSelector:
181 case DeclarationName::ObjCOneArgSelector:
182 case DeclarationName::ObjCMultiArgSelector:
183 assert(false && "Can't mangle Objective-C selector names here!");
184 break;
185
186 case DeclarationName::CXXConstructorName:
187 // <ctor-dtor-name> ::= C1 # complete object constructor
188 // ::= C2 # base object constructor
189 // ::= C3 # complete object allocating constructor
190 //
191 // FIXME: We don't even have all of these constructors
192 // in the AST yet.
193 Out << "C1";
194 break;
195
196 case DeclarationName::CXXDestructorName:
197 // <ctor-dtor-name> ::= D0 # deleting destructor
198 // ::= D1 # complete object destructor
199 // ::= D2 # base object destructor
200 //
201 // FIXME: We don't even have all of these destructors in the AST
202 // yet.
203 Out << "D0";
204 break;
205
206 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor219cc612009-02-13 01:28:03 +0000207 // <operator-name> ::= cv <type> # (cast)
208 Out << "cv";
209 mangleType(Context.getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000210 break;
211
212 case DeclarationName::CXXOperatorName:
213 mangleOperatorName(Name.getCXXOverloadedOperator(),
214 cast<FunctionDecl>(ND)->getNumParams());
215 break;
216
217 case DeclarationName::CXXUsingDirective:
218 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000219 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000220 }
221}
222
223void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
224 // <source-name> ::= <positive length number> <identifier>
225 // <number> ::= [n] <non-negative decimal integer>
226 // <identifier> ::= <unqualified source code identifier>
227 Out << II->getLength() << II->getName();
228}
229
230void CXXNameMangler::mangleNestedName(const NamedDecl *ND) {
231 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
232 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
233 // FIXME: no template support
234 Out << 'N';
235 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
236 mangleCVQualifiers(Method->getTypeQualifiers());
237 manglePrefix(ND->getDeclContext());
238 mangleUnqualifiedName(ND);
239 Out << 'E';
240}
241
Anders Carlsson1b42c792009-04-02 16:24:45 +0000242void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
243 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
244 // := Z <function encoding> E s [<discriminator>]
245 // <discriminator> := _ <non-negative number>
246 Out << 'Z';
247 mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext()));
248 Out << 'E';
249 mangleSourceName(ND->getIdentifier());
250}
251
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000252void CXXNameMangler::manglePrefix(const DeclContext *DC) {
253 // <prefix> ::= <prefix> <unqualified-name>
254 // ::= <template-prefix> <template-args>
255 // ::= <template-param>
256 // ::= # empty
257 // ::= <substitution>
258 // FIXME: We only handle mangling of namespaces and classes at the moment.
Anders Carlssonc8dee9c2009-04-01 00:42:16 +0000259 if (!DC->getParent()->isTranslationUnit())
260 manglePrefix(DC->getParent());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000261
262 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(DC))
263 mangleSourceName(Namespace->getIdentifier());
264 else if (const RecordDecl *Record = dyn_cast<RecordDecl>(DC))
265 mangleSourceName(Record->getIdentifier());
266}
267
268void
269CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
270 switch (OO) {
271 // <operator-name> ::= nw # new
272 case OO_New: Out << "nw"; break;
273 // ::= na # new[]
274 case OO_Array_New: Out << "na"; break;
275 // ::= dl # delete
276 case OO_Delete: Out << "dl"; break;
277 // ::= da # delete[]
278 case OO_Array_Delete: Out << "da"; break;
279 // ::= ps # + (unary)
280 // ::= pl # +
281 case OO_Plus: Out << (Arity == 1? "ps" : "pl"); break;
282 // ::= ng # - (unary)
283 // ::= mi # -
284 case OO_Minus: Out << (Arity == 1? "ng" : "mi"); break;
285 // ::= ad # & (unary)
286 // ::= an # &
287 case OO_Amp: Out << (Arity == 1? "ad" : "an"); break;
288 // ::= de # * (unary)
289 // ::= ml # *
290 case OO_Star: Out << (Arity == 1? "de" : "ml"); break;
291 // ::= co # ~
292 case OO_Tilde: Out << "co"; break;
293 // ::= dv # /
294 case OO_Slash: Out << "dv"; break;
295 // ::= rm # %
296 case OO_Percent: Out << "rm"; break;
297 // ::= or # |
298 case OO_Pipe: Out << "or"; break;
299 // ::= eo # ^
300 case OO_Caret: Out << "eo"; break;
301 // ::= aS # =
302 case OO_Equal: Out << "aS"; break;
303 // ::= pL # +=
304 case OO_PlusEqual: Out << "pL"; break;
305 // ::= mI # -=
306 case OO_MinusEqual: Out << "mI"; break;
307 // ::= mL # *=
308 case OO_StarEqual: Out << "mL"; break;
309 // ::= dV # /=
310 case OO_SlashEqual: Out << "dV"; break;
311 // ::= rM # %=
312 case OO_PercentEqual: Out << "rM"; break;
313 // ::= aN # &=
314 case OO_AmpEqual: Out << "aN"; break;
315 // ::= oR # |=
316 case OO_PipeEqual: Out << "oR"; break;
317 // ::= eO # ^=
318 case OO_CaretEqual: Out << "eO"; break;
319 // ::= ls # <<
320 case OO_LessLess: Out << "ls"; break;
321 // ::= rs # >>
322 case OO_GreaterGreater: Out << "rs"; break;
323 // ::= lS # <<=
324 case OO_LessLessEqual: Out << "lS"; break;
325 // ::= rS # >>=
326 case OO_GreaterGreaterEqual: Out << "rS"; break;
327 // ::= eq # ==
328 case OO_EqualEqual: Out << "eq"; break;
329 // ::= ne # !=
330 case OO_ExclaimEqual: Out << "ne"; break;
331 // ::= lt # <
332 case OO_Less: Out << "lt"; break;
333 // ::= gt # >
334 case OO_Greater: Out << "gt"; break;
335 // ::= le # <=
336 case OO_LessEqual: Out << "le"; break;
337 // ::= ge # >=
338 case OO_GreaterEqual: Out << "ge"; break;
339 // ::= nt # !
340 case OO_Exclaim: Out << "nt"; break;
341 // ::= aa # &&
342 case OO_AmpAmp: Out << "aa"; break;
343 // ::= oo # ||
344 case OO_PipePipe: Out << "oo"; break;
345 // ::= pp # ++
346 case OO_PlusPlus: Out << "pp"; break;
347 // ::= mm # --
348 case OO_MinusMinus: Out << "mm"; break;
349 // ::= cm # ,
350 case OO_Comma: Out << "cm"; break;
351 // ::= pm # ->*
352 case OO_ArrowStar: Out << "pm"; break;
353 // ::= pt # ->
354 case OO_Arrow: Out << "pt"; break;
355 // ::= cl # ()
356 case OO_Call: Out << "cl"; break;
357 // ::= ix # []
358 case OO_Subscript: Out << "ix"; break;
359 // UNSUPPORTED: ::= qu # ?
360
361 case OO_None:
362 case NUM_OVERLOADED_OPERATORS:
Douglas Gregor6ec36682009-02-18 23:53:56 +0000363 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000364 break;
365 }
366}
367
368void CXXNameMangler::mangleCVQualifiers(unsigned Quals) {
369 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
370 if (Quals & QualType::Restrict)
371 Out << 'r';
372 if (Quals & QualType::Volatile)
373 Out << 'V';
374 if (Quals & QualType::Const)
375 Out << 'K';
376}
377
378void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +0000379 // Only operate on the canonical type!
380 T = Context.getCanonicalType(T);
381
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000382 // FIXME: Should we have a TypeNodes.def to make this easier? (YES!)
383
384 // <type> ::= <CV-qualifiers> <type>
385 mangleCVQualifiers(T.getCVRQualifiers());
386
387 // ::= <builtin-type>
Anders Carlsson4843e582009-03-10 17:07:44 +0000388 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr()))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000389 mangleType(BT);
390 // ::= <function-type>
391 else if (const FunctionType *FT = dyn_cast<FunctionType>(T.getTypePtr()))
392 mangleType(FT);
393 // ::= <class-enum-type>
394 else if (const TagType *TT = dyn_cast<TagType>(T.getTypePtr()))
395 mangleType(TT);
396 // ::= <array-type>
397 else if (const ArrayType *AT = dyn_cast<ArrayType>(T.getTypePtr()))
398 mangleType(AT);
399 // ::= <pointer-to-member-type>
400 else if (const MemberPointerType *MPT
401 = dyn_cast<MemberPointerType>(T.getTypePtr()))
402 mangleType(MPT);
403 // ::= <template-param>
404 else if (const TemplateTypeParmType *TypeParm
405 = dyn_cast<TemplateTypeParmType>(T.getTypePtr()))
406 mangleType(TypeParm);
407 // FIXME: ::= <template-template-param> <template-args>
408 // FIXME: ::= <substitution> # See Compression below
409 // ::= P <type> # pointer-to
410 else if (const PointerType *PT = dyn_cast<PointerType>(T.getTypePtr())) {
411 Out << 'P';
412 mangleType(PT->getPointeeType());
413 }
414 // ::= R <type> # reference-to
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000415 else if (const LValueReferenceType *RT =
416 dyn_cast<LValueReferenceType>(T.getTypePtr())) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000417 Out << 'R';
418 mangleType(RT->getPointeeType());
419 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000420 // ::= O <type> # rvalue reference-to (C++0x)
421 else if (const RValueReferenceType *RT =
422 dyn_cast<RValueReferenceType>(T.getTypePtr())) {
423 Out << 'O';
424 mangleType(RT->getPointeeType());
425 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000426 // ::= C <type> # complex pair (C 2000)
427 else if (const ComplexType *CT = dyn_cast<ComplexType>(T.getTypePtr())) {
428 Out << 'C';
429 mangleType(CT->getElementType());
430 } else if (const VectorType *VT = dyn_cast<VectorType>(T.getTypePtr())) {
431 // GNU extension: vector types
432 Out << "U8__vector";
433 mangleType(VT->getElementType());
Anders Carlssona40c5e42009-03-07 22:03:21 +0000434 } else if (const ObjCInterfaceType *IT =
435 dyn_cast<ObjCInterfaceType>(T.getTypePtr())) {
436 mangleType(IT);
Anders Carlsson4843e582009-03-10 17:07:44 +0000437 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000438 // FIXME: ::= G <type> # imaginary (C 2000)
439 // FIXME: ::= U <source-name> <type> # vendor extended type qualifier
440 else
441 assert(false && "Cannot mangle unknown type");
442}
443
444void CXXNameMangler::mangleType(const BuiltinType *T) {
445 // <builtin-type> ::= v # void
446 // ::= w # wchar_t
447 // ::= b # bool
448 // ::= c # char
449 // ::= a # signed char
450 // ::= h # unsigned char
451 // ::= s # short
452 // ::= t # unsigned short
453 // ::= i # int
454 // ::= j # unsigned int
455 // ::= l # long
456 // ::= m # unsigned long
457 // ::= x # long long, __int64
458 // ::= y # unsigned long long, __int64
459 // ::= n # __int128
460 // UNSUPPORTED: ::= o # unsigned __int128
461 // ::= f # float
462 // ::= d # double
463 // ::= e # long double, __float80
464 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000465 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
466 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
467 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
468 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
469 // UNSUPPORTED: ::= Di # char32_t
470 // UNSUPPORTED: ::= Ds # char16_t
471 // ::= u <source-name> # vendor extended type
472 switch (T->getKind()) {
473 case BuiltinType::Void: Out << 'v'; break;
474 case BuiltinType::Bool: Out << 'b'; break;
475 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
476 case BuiltinType::UChar: Out << 'h'; break;
477 case BuiltinType::UShort: Out << 't'; break;
478 case BuiltinType::UInt: Out << 'j'; break;
479 case BuiltinType::ULong: Out << 'm'; break;
480 case BuiltinType::ULongLong: Out << 'y'; break;
481 case BuiltinType::SChar: Out << 'a'; break;
482 case BuiltinType::WChar: Out << 'w'; break;
483 case BuiltinType::Short: Out << 's'; break;
484 case BuiltinType::Int: Out << 'i'; break;
485 case BuiltinType::Long: Out << 'l'; break;
486 case BuiltinType::LongLong: Out << 'x'; break;
487 case BuiltinType::Float: Out << 'f'; break;
488 case BuiltinType::Double: Out << 'd'; break;
489 case BuiltinType::LongDouble: Out << 'e'; break;
490
491 case BuiltinType::Overload:
492 case BuiltinType::Dependent:
493 assert(false &&
494 "Overloaded and dependent types shouldn't get to name mangling");
495 break;
496 }
497}
498
499void CXXNameMangler::mangleType(const FunctionType *T) {
500 // <function-type> ::= F [Y] <bare-function-type> E
501 Out << 'F';
502 // FIXME: We don't have enough information in the AST to produce the
503 // 'Y' encoding for extern "C" function types.
504 mangleBareFunctionType(T, /*MangleReturnType=*/true);
505 Out << 'E';
506}
507
508void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
509 bool MangleReturnType) {
510 // <bare-function-type> ::= <signature type>+
511 if (MangleReturnType)
512 mangleType(T->getResultType());
513
Douglas Gregor72564e72009-02-26 23:50:07 +0000514 const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000515 assert(Proto && "Can't mangle K&R function prototypes");
516
Anders Carlssonc6c91bc2009-04-01 00:15:23 +0000517 if (Proto->getNumArgs() == 0) {
518 Out << 'v';
519 return;
520 }
521
Douglas Gregor72564e72009-02-26 23:50:07 +0000522 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000523 ArgEnd = Proto->arg_type_end();
524 Arg != ArgEnd; ++Arg)
525 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +0000526
527 // <builtin-type> ::= z # ellipsis
528 if (Proto->isVariadic())
529 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000530}
531
532void CXXNameMangler::mangleType(const TagType *T) {
533 // <class-enum-type> ::= <name>
Anders Carlsson4843e582009-03-10 17:07:44 +0000534
535 if (!T->getDecl()->getIdentifier())
536 mangleName(T->getDecl()->getTypedefForAnonDecl());
537 else
538 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000539}
540
541void CXXNameMangler::mangleType(const ArrayType *T) {
542 // <array-type> ::= A <positive dimension number> _ <element type>
543 // ::= A [<dimension expression>] _ <element type>
544 Out << 'A';
545 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T))
546 Out << CAT->getSize();
547 else if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(T))
548 mangleExpression(VAT->getSizeExpr());
549 else if (const DependentSizedArrayType *DSAT
550 = dyn_cast<DependentSizedArrayType>(T))
551 mangleExpression(DSAT->getSizeExpr());
552
553 Out << '_';
554 mangleType(T->getElementType());
555}
556
557void CXXNameMangler::mangleType(const MemberPointerType *T) {
558 // <pointer-to-member-type> ::= M <class type> <member type>
559 Out << 'M';
560 mangleType(QualType(T->getClass(), 0));
561 mangleType(T->getPointeeType());
562}
563
564void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
565 // <template-param> ::= T_ # first template parameter
566 // ::= T <parameter-2 non-negative number> _
567 if (T->getIndex() == 0)
568 Out << "T_";
569 else
570 Out << 'T' << (T->getIndex() - 1) << '_';
571}
572
Anders Carlssona40c5e42009-03-07 22:03:21 +0000573void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
574 mangleSourceName(T->getDecl()->getIdentifier());
575}
576
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000577void CXXNameMangler::mangleExpression(Expr *E) {
578 assert(false && "Cannot mangle expressions yet");
579}
580
581namespace clang {
582 /// \brief Mangles the name of the declaration D and emits that name
583 /// to the given output stream.
584 ///
585 /// If the declaration D requires a mangled name, this routine will
586 /// emit that mangled name to \p os and return true. Otherwise, \p
587 /// os will be unchanged and this routine will return false. In this
588 /// case, the caller should just emit the identifier of the declaration
589 /// (\c D->getIdentifier()) as its name.
590 bool mangleName(const NamedDecl *D, ASTContext &Context,
591 llvm::raw_ostream &os) {
592 CXXNameMangler Mangler(Context, os);
Douglas Gregor6ec36682009-02-18 23:53:56 +0000593 if (!Mangler.mangle(D))
594 return false;
595
596 os.flush();
597 return true;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000598 }
Anders Carlsson41aa8c12009-04-13 18:02:10 +0000599
600 /// mangleGuardVariable - Mangles the m
601 void mangleGuardVariable(const VarDecl *D, ASTContext &Context,
602 llvm::raw_ostream &os) {
603 CXXNameMangler Mangler(Context, os);
604 Mangler.mangleGuardVariable(D);
605
606 os.flush();
607 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000608}
609