1 //===--- 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/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 #define MANGLE_CHECKER 0
27
28 #if MANGLE_CHECKER
29 #include <cxxabi.h>
30 #endif
31
32 using 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
37 namespace {
38
mangleFunctionBlock(MangleContext & Context,StringRef Outer,const BlockDecl * BD,raw_ostream & Out)39 static void mangleFunctionBlock(MangleContext &Context,
40 StringRef Outer,
41 const BlockDecl *BD,
42 raw_ostream &Out) {
43 unsigned discriminator = Context.getBlockId(BD, true);
44 if (discriminator == 0)
45 Out << "__" << Outer << "_block_invoke";
46 else
47 Out << "__" << Outer << "_block_invoke_" << discriminator+1;
48 }
49
checkMangleDC(const DeclContext * DC,const BlockDecl * BD)50 static 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();
55 // 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();
60 assert(DC == ExpectedDC && "Given decl context did not match expected!");
61 #endif
62 }
63
64 }
65
anchor()66 void MangleContext::anchor() { }
67
mangleGlobalBlock(const BlockDecl * BD,const NamedDecl * ID,raw_ostream & Out)68 void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
69 const NamedDecl *ID,
70 raw_ostream &Out) {
71 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;
83 }
84
mangleCtorBlock(const CXXConstructorDecl * CD,CXXCtorType CT,const BlockDecl * BD,raw_ostream & ResStream)85 void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
86 CXXCtorType CT, const BlockDecl *BD,
87 raw_ostream &ResStream) {
88 checkMangleDC(CD, BD);
89 SmallString<64> Buffer;
90 llvm::raw_svector_ostream Out(Buffer);
91 mangleCXXCtor(CD, CT, Out);
92 Out.flush();
93 mangleFunctionBlock(*this, Buffer, BD, ResStream);
94 }
95
mangleDtorBlock(const CXXDestructorDecl * DD,CXXDtorType DT,const BlockDecl * BD,raw_ostream & ResStream)96 void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD,
97 CXXDtorType DT, const BlockDecl *BD,
98 raw_ostream &ResStream) {
99 checkMangleDC(DD, BD);
100 SmallString<64> Buffer;
101 llvm::raw_svector_ostream Out(Buffer);
102 mangleCXXDtor(DD, DT, Out);
103 Out.flush();
104 mangleFunctionBlock(*this, Buffer, BD, ResStream);
105 }
106
mangleBlock(const DeclContext * DC,const BlockDecl * BD,raw_ostream & Out)107 void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
108 raw_ostream &Out) {
109 assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
110 checkMangleDC(DC, BD);
111
112 SmallString<64> Buffer;
113 llvm::raw_svector_ostream Stream(Buffer);
114 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
115 mangleObjCMethodName(Method, Stream);
116 } else {
117 const NamedDecl *ND = cast<NamedDecl>(DC);
118 if (!shouldMangleDeclName(ND) && ND->getIdentifier())
119 Stream << ND->getIdentifier()->getName();
120 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?
126 mangleName(ND, Stream);
127 }
128 }
129 Stream.flush();
130 mangleFunctionBlock(*this, Buffer, BD, Out);
131 }
132
mangleObjCMethodName(const ObjCMethodDecl * MD,raw_ostream & Out)133 void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
134 raw_ostream &Out) {
135 SmallString<64> Name;
136 llvm::raw_svector_ostream OS(Name);
137
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))
143 OS << '(' << *CID << ')';
144 OS << ' ' << MD->getSelector().getAsString() << ']';
145
146 Out << OS.str().size() << OS.str();
147 }
148
mangleBlock(const BlockDecl * BD,raw_ostream & Out,const NamedDecl * ID)149 void MangleContext::mangleBlock(const BlockDecl *BD,
150 raw_ostream &Out,
151 const NamedDecl *ID) {
152 const DeclContext *DC = BD->getDeclContext();
153 while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC))
154 DC = DC->getParent();
155 if (DC->isFunctionOrMethod())
156 mangleBlock(DC, BD, Out);
157 else
158 mangleGlobalBlock(BD, ID, Out);
159 }
160