1 //===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implements generic name mangling support for blocks and Objective-C.
12 //===----------------------------------------------------------------------===//
13 #include "clang/AST/Attr.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/AST/Mangle.h"
21 #include "clang/Basic/ABI.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "clang/Basic/TargetInfo.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
28 #define MANGLE_CHECKER 0
34 using namespace clang;
36 // FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves
37 // much to be desired. Come up with a better mangling scheme.
39 static void mangleFunctionBlock(MangleContext &Context,
43 unsigned discriminator = Context.getBlockId(BD, true);
44 if (discriminator == 0)
45 Out << "__" << Outer << "_block_invoke";
47 Out << "__" << Outer << "_block_invoke_" << discriminator+1;
50 void MangleContext::anchor() { }
60 static bool isExternC(const NamedDecl *ND) {
61 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
62 return FD->isExternC();
63 return cast<VarDecl>(ND)->isExternC();
66 static CCMangling getCallingConvMangling(const ASTContext &Context,
67 const NamedDecl *ND) {
68 const TargetInfo &TI = Context.getTargetInfo();
69 const llvm::Triple &Triple = TI.getTriple();
70 if (!Triple.isOSWindows() ||
71 !(Triple.getArch() == llvm::Triple::x86 ||
72 Triple.getArch() == llvm::Triple::x86_64))
75 if (Context.getLangOpts().CPlusPlus && !isExternC(ND) &&
76 TI.getCXXABI() == TargetCXXABI::Microsoft)
79 const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
82 QualType T = FD->getType();
84 const FunctionType *FT = T->castAs<FunctionType>();
86 CallingConv CC = FT->getCallConv();
94 case CC_X86VectorCall:
99 bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
100 const ASTContext &ASTContext = getASTContext();
102 CCMangling CC = getCallingConvMangling(ASTContext, D);
106 // In C, functions with no attributes never need to be mangled. Fastpath them.
107 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
110 // Any decl can be declared with __asm("foo") on it, and this takes precedence
111 // over all other naming in the .o file.
112 if (D->hasAttr<AsmLabelAttr>())
115 return shouldMangleCXXName(D);
118 void MangleContext::mangleName(const NamedDecl *D, raw_ostream &Out) {
119 // Any decl can be declared with __asm("foo") on it, and this takes precedence
120 // over all other naming in the .o file.
121 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
122 // If we have an asm name, then we use it as the mangling.
124 // Adding the prefix can cause problems when one file has a "foo" and
125 // another has a "\01foo". That is known to happen on ELF with the
126 // tricks normally used for producing aliases (PR9177). Fortunately the
127 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
128 // marker. We also avoid adding the marker if this is an alias for an
131 getASTContext().getTargetInfo().getDataLayout().getGlobalPrefix();
132 if (GlobalPrefix && !ALA->getLabel().startswith("llvm."))
133 Out << '\01'; // LLVM IR Marker for __asm("foo")
135 Out << ALA->getLabel();
139 const ASTContext &ASTContext = getASTContext();
140 CCMangling CC = getCallingConvMangling(ASTContext, D);
141 bool MCXX = shouldMangleCXXName(D);
142 const TargetInfo &TI = Context.getTargetInfo();
143 if (CC == CCM_Other || (MCXX && TI.getCXXABI() == TargetCXXABI::Microsoft)) {
144 if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
145 mangleObjCMethodName(OMD, Out);
147 mangleCXXName(D, Out);
154 else if (CC == CCM_Fast)
156 else if (CC == CCM_RegCall)
157 Out << "__regcall3__";
160 Out << D->getIdentifier()->getName();
161 else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
162 mangleObjCMethodName(OMD, Out);
164 mangleCXXName(D, Out);
166 const FunctionDecl *FD = cast<FunctionDecl>(D);
167 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
168 const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT);
169 if (CC == CCM_Vector)
176 assert(!Proto->isVariadic());
177 unsigned ArgWords = 0;
178 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
181 for (const auto &AT : Proto->param_types())
182 // Size should be aligned to pointer size.
184 llvm::alignTo(ASTContext.getTypeSize(AT), TI.getPointerWidth(0)) /
185 TI.getPointerWidth(0);
186 Out << ((TI.getPointerWidth(0) / 8) * ArgWords);
189 void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
192 unsigned discriminator = getBlockId(BD, false);
194 if (shouldMangleDeclName(ID))
197 Out << ID->getIdentifier()->getName();
200 if (discriminator == 0)
201 Out << "_block_invoke";
203 Out << "_block_invoke_" << discriminator+1;
206 void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
207 CXXCtorType CT, const BlockDecl *BD,
208 raw_ostream &ResStream) {
209 SmallString<64> Buffer;
210 llvm::raw_svector_ostream Out(Buffer);
211 mangleCXXCtor(CD, CT, Out);
212 mangleFunctionBlock(*this, Buffer, BD, ResStream);
215 void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD,
216 CXXDtorType DT, const BlockDecl *BD,
217 raw_ostream &ResStream) {
218 SmallString<64> Buffer;
219 llvm::raw_svector_ostream Out(Buffer);
220 mangleCXXDtor(DD, DT, Out);
221 mangleFunctionBlock(*this, Buffer, BD, ResStream);
224 void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
226 assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
228 SmallString<64> Buffer;
229 llvm::raw_svector_ostream Stream(Buffer);
230 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
231 mangleObjCMethodName(Method, Stream);
233 assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) &&
234 "expected a NamedDecl or BlockDecl");
235 if (isa<BlockDecl>(DC))
236 for (; DC && isa<BlockDecl>(DC); DC = DC->getParent())
237 (void) getBlockId(cast<BlockDecl>(DC), true);
238 assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) &&
239 "expected a TranslationUnitDecl or a NamedDecl");
240 if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
241 mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
242 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
243 mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
244 else if (auto ND = dyn_cast<NamedDecl>(DC)) {
245 if (!shouldMangleDeclName(ND) && ND->getIdentifier())
246 Stream << ND->getIdentifier()->getName();
248 // FIXME: We were doing a mangleUnqualifiedName() before, but that's
249 // a private member of a class that will soon itself be private to the
250 // Itanium C++ ABI object. What should we do now? Right now, I'm just
251 // calling the mangleName() method on the MangleContext; is there a
253 mangleName(ND, Stream);
257 mangleFunctionBlock(*this, Buffer, BD, Out);
260 void MangleContext::mangleObjCMethodNameWithoutSize(const ObjCMethodDecl *MD,
262 const ObjCContainerDecl *CD =
263 dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
264 assert (CD && "Missing container decl in GetNameForMethod");
265 OS << (MD->isInstanceMethod() ? '-' : '+') << '[';
266 if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD)) {
267 OS << CID->getClassInterface()->getName();
268 OS << '(' << *CID << ')';
273 MD->getSelector().print(OS);
277 void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
279 SmallString<64> Name;
280 llvm::raw_svector_ostream OS(Name);
282 mangleObjCMethodNameWithoutSize(MD, OS);
283 Out << OS.str().size() << OS.str();