blob: d5f83719ec62130a9697fb25f7b776191c214a19 [file] [log] [blame]
Peter Collingbourne14110472011-01-13 18:57:25 +00001//===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===//
2//
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 generic name mangling support for blocks and Objective-C.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/AST/Mangle.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclCXX.h"
17#include "clang/AST/DeclObjC.h"
18#include "clang/AST/DeclTemplate.h"
19#include "clang/AST/ExprCXX.h"
20#include "clang/Basic/ABI.h"
21#include "clang/Basic/SourceManager.h"
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/Support/raw_ostream.h"
24#include "llvm/Support/ErrorHandling.h"
25
26#define MANGLE_CHECKER 0
27
28#if MANGLE_CHECKER
29#include <cxxabi.h>
30#endif
31
32using namespace clang;
33
34// FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves
35// much to be desired. Come up with a better mangling scheme.
36
37namespace {
38
39static void mangleFunctionBlock(MangleContext &Context,
Chris Lattner5f9e2722011-07-23 10:55:15 +000040 StringRef Outer,
Peter Collingbourne14110472011-01-13 18:57:25 +000041 const BlockDecl *BD,
Chris Lattner5f9e2722011-07-23 10:55:15 +000042 raw_ostream &Out) {
Fariborz Jahanian4904bf42012-06-26 16:06:38 +000043 unsigned discriminator = Context.getBlockId(BD, true);
44 if (discriminator == 0)
45 Out << "__" << Outer << "_block_invoke";
46 else
47 Out << "__" << Outer << "_block_invoke_" << discriminator+1;
Peter Collingbourne14110472011-01-13 18:57:25 +000048}
49
50static void checkMangleDC(const DeclContext *DC, const BlockDecl *BD) {
51#ifndef NDEBUG
52 const DeclContext *ExpectedDC = BD->getDeclContext();
53 while (isa<BlockDecl>(ExpectedDC) || isa<EnumDecl>(ExpectedDC))
54 ExpectedDC = ExpectedDC->getParent();
Richard Smith7a614d82011-06-11 17:19:42 +000055 // In-class initializers for non-static data members are lexically defined
56 // within the class, but are mangled as if they were specified as constructor
57 // member initializers.
58 if (isa<CXXRecordDecl>(ExpectedDC) && DC != ExpectedDC)
59 DC = DC->getParent();
Peter Collingbourne14110472011-01-13 18:57:25 +000060 assert(DC == ExpectedDC && "Given decl context did not match expected!");
61#endif
62}
63
64}
65
David Blaikie99ba9e32011-12-20 02:48:34 +000066void MangleContext::anchor() { }
67
Peter Collingbourne14110472011-01-13 18:57:25 +000068void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
Fariborz Jahanian4904bf42012-06-26 16:06:38 +000069 const NamedDecl *ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +000070 raw_ostream &Out) {
Fariborz Jahanian4904bf42012-06-26 16:06:38 +000071 unsigned discriminator = getBlockId(BD, false);
72 if (ID) {
73 if (shouldMangleDeclName(ID))
74 mangleName(ID, Out);
75 else {
76 Out << ID->getIdentifier()->getName();
77 }
78 }
79 if (discriminator == 0)
80 Out << "_block_invoke";
81 else
82 Out << "_block_invoke_" << discriminator+1;
Peter Collingbourne14110472011-01-13 18:57:25 +000083}
84
85void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
86 CXXCtorType CT, const BlockDecl *BD,
Chris Lattner5f9e2722011-07-23 10:55:15 +000087 raw_ostream &ResStream) {
Peter Collingbourne14110472011-01-13 18:57:25 +000088 checkMangleDC(CD, BD);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000089 SmallString<64> Buffer;
Rafael Espindolac4850c22011-02-10 23:59:36 +000090 llvm::raw_svector_ostream Out(Buffer);
91 mangleCXXCtor(CD, CT, Out);
92 Out.flush();
93 mangleFunctionBlock(*this, Buffer, BD, ResStream);
Peter Collingbourne14110472011-01-13 18:57:25 +000094}
95
96void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD,
97 CXXDtorType DT, const BlockDecl *BD,
Chris Lattner5f9e2722011-07-23 10:55:15 +000098 raw_ostream &ResStream) {
Peter Collingbourne14110472011-01-13 18:57:25 +000099 checkMangleDC(DD, BD);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000100 SmallString<64> Buffer;
Rafael Espindolac4850c22011-02-10 23:59:36 +0000101 llvm::raw_svector_ostream Out(Buffer);
102 mangleCXXDtor(DD, DT, Out);
103 Out.flush();
104 mangleFunctionBlock(*this, Buffer, BD, ResStream);
Peter Collingbourne14110472011-01-13 18:57:25 +0000105}
106
107void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000108 raw_ostream &Out) {
Peter Collingbourne14110472011-01-13 18:57:25 +0000109 assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
110 checkMangleDC(DC, BD);
111
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000112 SmallString<64> Buffer;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000113 llvm::raw_svector_ostream Stream(Buffer);
Peter Collingbourne14110472011-01-13 18:57:25 +0000114 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000115 mangleObjCMethodName(Method, Stream);
Peter Collingbourne14110472011-01-13 18:57:25 +0000116 } else {
117 const NamedDecl *ND = cast<NamedDecl>(DC);
Fariborz Jahanian4904bf42012-06-26 16:06:38 +0000118 if (!shouldMangleDeclName(ND) && ND->getIdentifier())
119 Stream << ND->getIdentifier()->getName();
Peter Collingbourne14110472011-01-13 18:57:25 +0000120 else {
121 // FIXME: We were doing a mangleUnqualifiedName() before, but that's
122 // a private member of a class that will soon itself be private to the
123 // Itanium C++ ABI object. What should we do now? Right now, I'm just
124 // calling the mangleName() method on the MangleContext; is there a
125 // better way?
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000126 mangleName(ND, Stream);
Peter Collingbourne14110472011-01-13 18:57:25 +0000127 }
128 }
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000129 Stream.flush();
Rafael Espindolac4850c22011-02-10 23:59:36 +0000130 mangleFunctionBlock(*this, Buffer, BD, Out);
Peter Collingbourne14110472011-01-13 18:57:25 +0000131}
132
133void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000134 raw_ostream &Out) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000135 SmallString<64> Name;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000136 llvm::raw_svector_ostream OS(Name);
Peter Collingbourne14110472011-01-13 18:57:25 +0000137
138 const ObjCContainerDecl *CD =
139 dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
140 assert (CD && "Missing container decl in GetNameForMethod");
141 OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName();
142 if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000143 OS << '(' << *CID << ')';
Peter Collingbourne14110472011-01-13 18:57:25 +0000144 OS << ' ' << MD->getSelector().getAsString() << ']';
145
146 Out << OS.str().size() << OS.str();
147}
148
149void MangleContext::mangleBlock(const BlockDecl *BD,
Fariborz Jahanian4904bf42012-06-26 16:06:38 +0000150 raw_ostream &Out,
151 const NamedDecl *ID) {
Peter Collingbourne14110472011-01-13 18:57:25 +0000152 const DeclContext *DC = BD->getDeclContext();
153 while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC))
154 DC = DC->getParent();
155 if (DC->isFunctionOrMethod())
Rafael Espindolac4850c22011-02-10 23:59:36 +0000156 mangleBlock(DC, BD, Out);
Peter Collingbourne14110472011-01-13 18:57:25 +0000157 else
Fariborz Jahanian4904bf42012-06-26 16:06:38 +0000158 mangleGlobalBlock(BD, ID, Out);
Peter Collingbourne14110472011-01-13 18:57:25 +0000159}