1f4a2713aSLionel Sambuc //===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // Implements generic name mangling support for blocks and Objective-C.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc #include "clang/AST/Attr.h"
14f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
15f4a2713aSLionel Sambuc #include "clang/AST/Decl.h"
16f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h"
17f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
18f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h"
19f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
20*0a6a1f1dSLionel Sambuc #include "clang/AST/Mangle.h"
21f4a2713aSLionel Sambuc #include "clang/Basic/ABI.h"
22f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
23f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
24f4a2713aSLionel Sambuc #include "llvm/ADT/StringExtras.h"
25f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
26f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
27f4a2713aSLionel Sambuc
28f4a2713aSLionel Sambuc #define MANGLE_CHECKER 0
29f4a2713aSLionel Sambuc
30f4a2713aSLionel Sambuc #if MANGLE_CHECKER
31f4a2713aSLionel Sambuc #include <cxxabi.h>
32f4a2713aSLionel Sambuc #endif
33f4a2713aSLionel Sambuc
34f4a2713aSLionel Sambuc using namespace clang;
35f4a2713aSLionel Sambuc
36f4a2713aSLionel Sambuc // FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves
37f4a2713aSLionel Sambuc // much to be desired. Come up with a better mangling scheme.
38f4a2713aSLionel Sambuc
mangleFunctionBlock(MangleContext & Context,StringRef Outer,const BlockDecl * BD,raw_ostream & Out)39f4a2713aSLionel Sambuc static void mangleFunctionBlock(MangleContext &Context,
40f4a2713aSLionel Sambuc StringRef Outer,
41f4a2713aSLionel Sambuc const BlockDecl *BD,
42f4a2713aSLionel Sambuc raw_ostream &Out) {
43f4a2713aSLionel Sambuc unsigned discriminator = Context.getBlockId(BD, true);
44f4a2713aSLionel Sambuc if (discriminator == 0)
45f4a2713aSLionel Sambuc Out << "__" << Outer << "_block_invoke";
46f4a2713aSLionel Sambuc else
47f4a2713aSLionel Sambuc Out << "__" << Outer << "_block_invoke_" << discriminator+1;
48f4a2713aSLionel Sambuc }
49f4a2713aSLionel Sambuc
anchor()50f4a2713aSLionel Sambuc void MangleContext::anchor() { }
51f4a2713aSLionel Sambuc
52*0a6a1f1dSLionel Sambuc enum CCMangling {
53*0a6a1f1dSLionel Sambuc CCM_Other,
54*0a6a1f1dSLionel Sambuc CCM_Fast,
55*0a6a1f1dSLionel Sambuc CCM_Vector,
56*0a6a1f1dSLionel Sambuc CCM_Std
57f4a2713aSLionel Sambuc };
58f4a2713aSLionel Sambuc
isExternC(const NamedDecl * ND)59f4a2713aSLionel Sambuc static bool isExternC(const NamedDecl *ND) {
60f4a2713aSLionel Sambuc if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
61f4a2713aSLionel Sambuc return FD->isExternC();
62f4a2713aSLionel Sambuc return cast<VarDecl>(ND)->isExternC();
63f4a2713aSLionel Sambuc }
64f4a2713aSLionel Sambuc
getCallingConvMangling(const ASTContext & Context,const NamedDecl * ND)65*0a6a1f1dSLionel Sambuc static CCMangling getCallingConvMangling(const ASTContext &Context,
66f4a2713aSLionel Sambuc const NamedDecl *ND) {
67f4a2713aSLionel Sambuc const TargetInfo &TI = Context.getTargetInfo();
68*0a6a1f1dSLionel Sambuc const llvm::Triple &Triple = TI.getTriple();
69*0a6a1f1dSLionel Sambuc if (!Triple.isOSWindows() ||
70*0a6a1f1dSLionel Sambuc !(Triple.getArch() == llvm::Triple::x86 ||
71*0a6a1f1dSLionel Sambuc Triple.getArch() == llvm::Triple::x86_64))
72*0a6a1f1dSLionel Sambuc return CCM_Other;
73f4a2713aSLionel Sambuc
74f4a2713aSLionel Sambuc if (Context.getLangOpts().CPlusPlus && !isExternC(ND) &&
75f4a2713aSLionel Sambuc TI.getCXXABI() == TargetCXXABI::Microsoft)
76*0a6a1f1dSLionel Sambuc return CCM_Other;
77f4a2713aSLionel Sambuc
78f4a2713aSLionel Sambuc const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
79f4a2713aSLionel Sambuc if (!FD)
80*0a6a1f1dSLionel Sambuc return CCM_Other;
81f4a2713aSLionel Sambuc QualType T = FD->getType();
82f4a2713aSLionel Sambuc
83f4a2713aSLionel Sambuc const FunctionType *FT = T->castAs<FunctionType>();
84f4a2713aSLionel Sambuc
85f4a2713aSLionel Sambuc CallingConv CC = FT->getCallConv();
86f4a2713aSLionel Sambuc switch (CC) {
87f4a2713aSLionel Sambuc default:
88*0a6a1f1dSLionel Sambuc return CCM_Other;
89f4a2713aSLionel Sambuc case CC_X86FastCall:
90*0a6a1f1dSLionel Sambuc return CCM_Fast;
91f4a2713aSLionel Sambuc case CC_X86StdCall:
92*0a6a1f1dSLionel Sambuc return CCM_Std;
93*0a6a1f1dSLionel Sambuc case CC_X86VectorCall:
94*0a6a1f1dSLionel Sambuc return CCM_Vector;
95f4a2713aSLionel Sambuc }
96f4a2713aSLionel Sambuc }
97f4a2713aSLionel Sambuc
shouldMangleDeclName(const NamedDecl * D)98f4a2713aSLionel Sambuc bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
99f4a2713aSLionel Sambuc const ASTContext &ASTContext = getASTContext();
100f4a2713aSLionel Sambuc
101*0a6a1f1dSLionel Sambuc CCMangling CC = getCallingConvMangling(ASTContext, D);
102*0a6a1f1dSLionel Sambuc if (CC != CCM_Other)
103f4a2713aSLionel Sambuc return true;
104f4a2713aSLionel Sambuc
105f4a2713aSLionel Sambuc // In C, functions with no attributes never need to be mangled. Fastpath them.
106f4a2713aSLionel Sambuc if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
107f4a2713aSLionel Sambuc return false;
108f4a2713aSLionel Sambuc
109f4a2713aSLionel Sambuc // Any decl can be declared with __asm("foo") on it, and this takes precedence
110f4a2713aSLionel Sambuc // over all other naming in the .o file.
111f4a2713aSLionel Sambuc if (D->hasAttr<AsmLabelAttr>())
112f4a2713aSLionel Sambuc return true;
113f4a2713aSLionel Sambuc
114f4a2713aSLionel Sambuc return shouldMangleCXXName(D);
115f4a2713aSLionel Sambuc }
116f4a2713aSLionel Sambuc
mangleName(const NamedDecl * D,raw_ostream & Out)117f4a2713aSLionel Sambuc void MangleContext::mangleName(const NamedDecl *D, raw_ostream &Out) {
118f4a2713aSLionel Sambuc // Any decl can be declared with __asm("foo") on it, and this takes precedence
119f4a2713aSLionel Sambuc // over all other naming in the .o file.
120f4a2713aSLionel Sambuc if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
121f4a2713aSLionel Sambuc // If we have an asm name, then we use it as the mangling.
122f4a2713aSLionel Sambuc
123f4a2713aSLionel Sambuc // Adding the prefix can cause problems when one file has a "foo" and
124f4a2713aSLionel Sambuc // another has a "\01foo". That is known to happen on ELF with the
125f4a2713aSLionel Sambuc // tricks normally used for producing aliases (PR9177). Fortunately the
126f4a2713aSLionel Sambuc // llvm mangler on ELF is a nop, so we can just avoid adding the \01
127f4a2713aSLionel Sambuc // marker. We also avoid adding the marker if this is an alias for an
128f4a2713aSLionel Sambuc // LLVM intrinsic.
129f4a2713aSLionel Sambuc StringRef UserLabelPrefix =
130f4a2713aSLionel Sambuc getASTContext().getTargetInfo().getUserLabelPrefix();
131f4a2713aSLionel Sambuc if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
132f4a2713aSLionel Sambuc Out << '\01'; // LLVM IR Marker for __asm("foo")
133f4a2713aSLionel Sambuc
134f4a2713aSLionel Sambuc Out << ALA->getLabel();
135f4a2713aSLionel Sambuc return;
136f4a2713aSLionel Sambuc }
137f4a2713aSLionel Sambuc
138f4a2713aSLionel Sambuc const ASTContext &ASTContext = getASTContext();
139*0a6a1f1dSLionel Sambuc CCMangling CC = getCallingConvMangling(ASTContext, D);
140f4a2713aSLionel Sambuc bool MCXX = shouldMangleCXXName(D);
141f4a2713aSLionel Sambuc const TargetInfo &TI = Context.getTargetInfo();
142*0a6a1f1dSLionel Sambuc if (CC == CCM_Other || (MCXX && TI.getCXXABI() == TargetCXXABI::Microsoft)) {
143*0a6a1f1dSLionel Sambuc if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
144*0a6a1f1dSLionel Sambuc mangleObjCMethodName(OMD, Out);
145*0a6a1f1dSLionel Sambuc else
146f4a2713aSLionel Sambuc mangleCXXName(D, Out);
147f4a2713aSLionel Sambuc return;
148f4a2713aSLionel Sambuc }
149f4a2713aSLionel Sambuc
150f4a2713aSLionel Sambuc Out << '\01';
151*0a6a1f1dSLionel Sambuc if (CC == CCM_Std)
152f4a2713aSLionel Sambuc Out << '_';
153*0a6a1f1dSLionel Sambuc else if (CC == CCM_Fast)
154f4a2713aSLionel Sambuc Out << '@';
155f4a2713aSLionel Sambuc
156f4a2713aSLionel Sambuc if (!MCXX)
157f4a2713aSLionel Sambuc Out << D->getIdentifier()->getName();
158*0a6a1f1dSLionel Sambuc else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
159*0a6a1f1dSLionel Sambuc mangleObjCMethodName(OMD, Out);
160f4a2713aSLionel Sambuc else
161f4a2713aSLionel Sambuc mangleCXXName(D, Out);
162f4a2713aSLionel Sambuc
163f4a2713aSLionel Sambuc const FunctionDecl *FD = cast<FunctionDecl>(D);
164f4a2713aSLionel Sambuc const FunctionType *FT = FD->getType()->castAs<FunctionType>();
165f4a2713aSLionel Sambuc const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT);
166*0a6a1f1dSLionel Sambuc if (CC == CCM_Vector)
167*0a6a1f1dSLionel Sambuc Out << '@';
168f4a2713aSLionel Sambuc Out << '@';
169f4a2713aSLionel Sambuc if (!Proto) {
170f4a2713aSLionel Sambuc Out << '0';
171f4a2713aSLionel Sambuc return;
172f4a2713aSLionel Sambuc }
173f4a2713aSLionel Sambuc assert(!Proto->isVariadic());
174f4a2713aSLionel Sambuc unsigned ArgWords = 0;
175f4a2713aSLionel Sambuc if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
176f4a2713aSLionel Sambuc if (!MD->isStatic())
177f4a2713aSLionel Sambuc ++ArgWords;
178*0a6a1f1dSLionel Sambuc for (const auto &AT : Proto->param_types())
179*0a6a1f1dSLionel Sambuc // Size should be aligned to pointer size.
180*0a6a1f1dSLionel Sambuc ArgWords += llvm::RoundUpToAlignment(ASTContext.getTypeSize(AT),
181*0a6a1f1dSLionel Sambuc TI.getPointerWidth(0)) /
182*0a6a1f1dSLionel Sambuc TI.getPointerWidth(0);
183*0a6a1f1dSLionel Sambuc Out << ((TI.getPointerWidth(0) / 8) * ArgWords);
184f4a2713aSLionel Sambuc }
185f4a2713aSLionel Sambuc
mangleGlobalBlock(const BlockDecl * BD,const NamedDecl * ID,raw_ostream & Out)186f4a2713aSLionel Sambuc void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
187f4a2713aSLionel Sambuc const NamedDecl *ID,
188f4a2713aSLionel Sambuc raw_ostream &Out) {
189f4a2713aSLionel Sambuc unsigned discriminator = getBlockId(BD, false);
190f4a2713aSLionel Sambuc if (ID) {
191f4a2713aSLionel Sambuc if (shouldMangleDeclName(ID))
192f4a2713aSLionel Sambuc mangleName(ID, Out);
193f4a2713aSLionel Sambuc else {
194f4a2713aSLionel Sambuc Out << ID->getIdentifier()->getName();
195f4a2713aSLionel Sambuc }
196f4a2713aSLionel Sambuc }
197f4a2713aSLionel Sambuc if (discriminator == 0)
198f4a2713aSLionel Sambuc Out << "_block_invoke";
199f4a2713aSLionel Sambuc else
200f4a2713aSLionel Sambuc Out << "_block_invoke_" << discriminator+1;
201f4a2713aSLionel Sambuc }
202f4a2713aSLionel Sambuc
mangleCtorBlock(const CXXConstructorDecl * CD,CXXCtorType CT,const BlockDecl * BD,raw_ostream & ResStream)203f4a2713aSLionel Sambuc void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
204f4a2713aSLionel Sambuc CXXCtorType CT, const BlockDecl *BD,
205f4a2713aSLionel Sambuc raw_ostream &ResStream) {
206f4a2713aSLionel Sambuc SmallString<64> Buffer;
207f4a2713aSLionel Sambuc llvm::raw_svector_ostream Out(Buffer);
208f4a2713aSLionel Sambuc mangleCXXCtor(CD, CT, Out);
209f4a2713aSLionel Sambuc Out.flush();
210f4a2713aSLionel Sambuc mangleFunctionBlock(*this, Buffer, BD, ResStream);
211f4a2713aSLionel Sambuc }
212f4a2713aSLionel Sambuc
mangleDtorBlock(const CXXDestructorDecl * DD,CXXDtorType DT,const BlockDecl * BD,raw_ostream & ResStream)213f4a2713aSLionel Sambuc void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD,
214f4a2713aSLionel Sambuc CXXDtorType DT, const BlockDecl *BD,
215f4a2713aSLionel Sambuc raw_ostream &ResStream) {
216f4a2713aSLionel Sambuc SmallString<64> Buffer;
217f4a2713aSLionel Sambuc llvm::raw_svector_ostream Out(Buffer);
218f4a2713aSLionel Sambuc mangleCXXDtor(DD, DT, Out);
219f4a2713aSLionel Sambuc Out.flush();
220f4a2713aSLionel Sambuc mangleFunctionBlock(*this, Buffer, BD, ResStream);
221f4a2713aSLionel Sambuc }
222f4a2713aSLionel Sambuc
mangleBlock(const DeclContext * DC,const BlockDecl * BD,raw_ostream & Out)223f4a2713aSLionel Sambuc void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
224f4a2713aSLionel Sambuc raw_ostream &Out) {
225f4a2713aSLionel Sambuc assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
226f4a2713aSLionel Sambuc
227f4a2713aSLionel Sambuc SmallString<64> Buffer;
228f4a2713aSLionel Sambuc llvm::raw_svector_ostream Stream(Buffer);
229f4a2713aSLionel Sambuc if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
230f4a2713aSLionel Sambuc mangleObjCMethodName(Method, Stream);
231f4a2713aSLionel Sambuc } else {
232*0a6a1f1dSLionel Sambuc assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) &&
233*0a6a1f1dSLionel Sambuc "expected a NamedDecl or BlockDecl");
234*0a6a1f1dSLionel Sambuc if (isa<BlockDecl>(DC))
235*0a6a1f1dSLionel Sambuc for (; DC && isa<BlockDecl>(DC); DC = DC->getParent())
236*0a6a1f1dSLionel Sambuc (void) getBlockId(cast<BlockDecl>(DC), true);
237*0a6a1f1dSLionel Sambuc assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) &&
238*0a6a1f1dSLionel Sambuc "expected a TranslationUnitDecl or a NamedDecl");
239*0a6a1f1dSLionel Sambuc if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
240*0a6a1f1dSLionel Sambuc mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
241*0a6a1f1dSLionel Sambuc else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
242*0a6a1f1dSLionel Sambuc mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
243*0a6a1f1dSLionel Sambuc else if (auto ND = dyn_cast<NamedDecl>(DC)) {
244f4a2713aSLionel Sambuc if (!shouldMangleDeclName(ND) && ND->getIdentifier())
245f4a2713aSLionel Sambuc Stream << ND->getIdentifier()->getName();
246f4a2713aSLionel Sambuc else {
247f4a2713aSLionel Sambuc // FIXME: We were doing a mangleUnqualifiedName() before, but that's
248f4a2713aSLionel Sambuc // a private member of a class that will soon itself be private to the
249f4a2713aSLionel Sambuc // Itanium C++ ABI object. What should we do now? Right now, I'm just
250f4a2713aSLionel Sambuc // calling the mangleName() method on the MangleContext; is there a
251f4a2713aSLionel Sambuc // better way?
252f4a2713aSLionel Sambuc mangleName(ND, Stream);
253f4a2713aSLionel Sambuc }
254f4a2713aSLionel Sambuc }
255*0a6a1f1dSLionel Sambuc }
256f4a2713aSLionel Sambuc Stream.flush();
257f4a2713aSLionel Sambuc mangleFunctionBlock(*this, Buffer, BD, Out);
258f4a2713aSLionel Sambuc }
259f4a2713aSLionel Sambuc
mangleObjCMethodName(const ObjCMethodDecl * MD,raw_ostream & Out)260f4a2713aSLionel Sambuc void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
261f4a2713aSLionel Sambuc raw_ostream &Out) {
262f4a2713aSLionel Sambuc SmallString<64> Name;
263f4a2713aSLionel Sambuc llvm::raw_svector_ostream OS(Name);
264f4a2713aSLionel Sambuc
265f4a2713aSLionel Sambuc const ObjCContainerDecl *CD =
266f4a2713aSLionel Sambuc dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
267f4a2713aSLionel Sambuc assert (CD && "Missing container decl in GetNameForMethod");
268f4a2713aSLionel Sambuc OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName();
269f4a2713aSLionel Sambuc if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD))
270f4a2713aSLionel Sambuc OS << '(' << *CID << ')';
271*0a6a1f1dSLionel Sambuc OS << ' ';
272*0a6a1f1dSLionel Sambuc MD->getSelector().print(OS);
273*0a6a1f1dSLionel Sambuc OS << ']';
274f4a2713aSLionel Sambuc
275f4a2713aSLionel Sambuc Out << OS.str().size() << OS.str();
276f4a2713aSLionel Sambuc }
277