blob: 56a2500274a720565160b79b55a8c5ed602d58f2 [file] [log] [blame]
Peter Collingbourne0ff0b372011-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//===----------------------------------------------------------------------===//
Rafael Espindola002667c2013-10-16 01:40:34 +000013#include "clang/AST/Attr.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000014#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"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000020#include "clang/AST/Mangle.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000021#include "clang/Basic/ABI.h"
22#include "clang/Basic/SourceManager.h"
Rafael Espindola002667c2013-10-16 01:40:34 +000023#include "clang/Basic/TargetInfo.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000024#include "llvm/ADT/StringExtras.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000025#include "llvm/Support/ErrorHandling.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "llvm/Support/raw_ostream.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000027
28#define MANGLE_CHECKER 0
29
30#if MANGLE_CHECKER
31#include <cxxabi.h>
32#endif
33
34using namespace clang;
35
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.
38
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000039static void mangleFunctionBlock(MangleContext &Context,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000040 StringRef Outer,
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000041 const BlockDecl *BD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000042 raw_ostream &Out) {
Fariborz Jahanian63628032012-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 Collingbourne0ff0b372011-01-13 18:57:25 +000048}
49
David Blaikie68e081d2011-12-20 02:48:34 +000050void MangleContext::anchor() { }
51
Reid Kleckner80944df2014-10-31 22:00:51 +000052enum CCMangling {
53 CCM_Other,
54 CCM_Fast,
Erich Keane757d3172016-11-02 18:29:35 +000055 CCM_RegCall,
Reid Kleckner80944df2014-10-31 22:00:51 +000056 CCM_Vector,
57 CCM_Std
Rafael Espindola002667c2013-10-16 01:40:34 +000058};
59
60static bool isExternC(const NamedDecl *ND) {
61 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
62 return FD->isExternC();
63 return cast<VarDecl>(ND)->isExternC();
64}
65
Reid Kleckner80944df2014-10-31 22:00:51 +000066static CCMangling getCallingConvMangling(const ASTContext &Context,
67 const NamedDecl *ND) {
Rafael Espindola002667c2013-10-16 01:40:34 +000068 const TargetInfo &TI = Context.getTargetInfo();
Benjamin Kramer9299637dc2014-03-04 19:31:42 +000069 const llvm::Triple &Triple = TI.getTriple();
Reid Kleckner80944df2014-10-31 22:00:51 +000070 if (!Triple.isOSWindows() ||
71 !(Triple.getArch() == llvm::Triple::x86 ||
72 Triple.getArch() == llvm::Triple::x86_64))
73 return CCM_Other;
Rafael Espindola002667c2013-10-16 01:40:34 +000074
75 if (Context.getLangOpts().CPlusPlus && !isExternC(ND) &&
76 TI.getCXXABI() == TargetCXXABI::Microsoft)
Reid Kleckner80944df2014-10-31 22:00:51 +000077 return CCM_Other;
Rafael Espindola002667c2013-10-16 01:40:34 +000078
79 const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
80 if (!FD)
Reid Kleckner80944df2014-10-31 22:00:51 +000081 return CCM_Other;
Rafael Espindola002667c2013-10-16 01:40:34 +000082 QualType T = FD->getType();
83
84 const FunctionType *FT = T->castAs<FunctionType>();
85
86 CallingConv CC = FT->getCallConv();
87 switch (CC) {
88 default:
Reid Kleckner80944df2014-10-31 22:00:51 +000089 return CCM_Other;
Rafael Espindola002667c2013-10-16 01:40:34 +000090 case CC_X86FastCall:
Reid Kleckner80944df2014-10-31 22:00:51 +000091 return CCM_Fast;
Rafael Espindola002667c2013-10-16 01:40:34 +000092 case CC_X86StdCall:
Reid Kleckner80944df2014-10-31 22:00:51 +000093 return CCM_Std;
94 case CC_X86VectorCall:
95 return CCM_Vector;
Rafael Espindola002667c2013-10-16 01:40:34 +000096 }
97}
98
99bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
100 const ASTContext &ASTContext = getASTContext();
101
Reid Kleckner80944df2014-10-31 22:00:51 +0000102 CCMangling CC = getCallingConvMangling(ASTContext, D);
103 if (CC != CCM_Other)
Rafael Espindola002667c2013-10-16 01:40:34 +0000104 return true;
105
Richard Smithcd4a7a42017-09-07 00:55:55 +0000106 // If the declaration has an owning module for linkage purposes that needs to
107 // be mangled, we must mangle its name.
108 if (!D->hasExternalFormalLinkage() && D->getOwningModuleForLinkage())
109 return true;
110
Rafael Espindola002667c2013-10-16 01:40:34 +0000111 // In C, functions with no attributes never need to be mangled. Fastpath them.
112 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
113 return false;
114
115 // Any decl can be declared with __asm("foo") on it, and this takes precedence
116 // over all other naming in the .o file.
117 if (D->hasAttr<AsmLabelAttr>())
118 return true;
119
120 return shouldMangleCXXName(D);
121}
122
123void MangleContext::mangleName(const NamedDecl *D, raw_ostream &Out) {
124 // Any decl can be declared with __asm("foo") on it, and this takes precedence
125 // over all other naming in the .o file.
126 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
127 // If we have an asm name, then we use it as the mangling.
128
129 // Adding the prefix can cause problems when one file has a "foo" and
130 // another has a "\01foo". That is known to happen on ELF with the
131 // tricks normally used for producing aliases (PR9177). Fortunately the
132 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
133 // marker. We also avoid adding the marker if this is an alias for an
134 // LLVM intrinsic.
James Y Knightb214cbc2016-03-04 19:00:41 +0000135 char GlobalPrefix =
136 getASTContext().getTargetInfo().getDataLayout().getGlobalPrefix();
137 if (GlobalPrefix && !ALA->getLabel().startswith("llvm."))
Rafael Espindola002667c2013-10-16 01:40:34 +0000138 Out << '\01'; // LLVM IR Marker for __asm("foo")
139
140 Out << ALA->getLabel();
141 return;
142 }
143
144 const ASTContext &ASTContext = getASTContext();
Reid Kleckner80944df2014-10-31 22:00:51 +0000145 CCMangling CC = getCallingConvMangling(ASTContext, D);
Rafael Espindola002667c2013-10-16 01:40:34 +0000146 bool MCXX = shouldMangleCXXName(D);
147 const TargetInfo &TI = Context.getTargetInfo();
Reid Kleckner80944df2014-10-31 22:00:51 +0000148 if (CC == CCM_Other || (MCXX && TI.getCXXABI() == TargetCXXABI::Microsoft)) {
Saleem Abdulrasool3b434472014-10-14 17:20:18 +0000149 if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
150 mangleObjCMethodName(OMD, Out);
151 else
152 mangleCXXName(D, Out);
Rafael Espindola002667c2013-10-16 01:40:34 +0000153 return;
154 }
155
156 Out << '\01';
Reid Kleckner80944df2014-10-31 22:00:51 +0000157 if (CC == CCM_Std)
Rafael Espindola002667c2013-10-16 01:40:34 +0000158 Out << '_';
Reid Kleckner80944df2014-10-31 22:00:51 +0000159 else if (CC == CCM_Fast)
Rafael Espindola002667c2013-10-16 01:40:34 +0000160 Out << '@';
Erich Keane757d3172016-11-02 18:29:35 +0000161 else if (CC == CCM_RegCall)
162 Out << "__regcall3__";
Rafael Espindola002667c2013-10-16 01:40:34 +0000163
164 if (!MCXX)
165 Out << D->getIdentifier()->getName();
Saleem Abdulrasool3b434472014-10-14 17:20:18 +0000166 else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
167 mangleObjCMethodName(OMD, Out);
Rafael Espindola002667c2013-10-16 01:40:34 +0000168 else
169 mangleCXXName(D, Out);
170
171 const FunctionDecl *FD = cast<FunctionDecl>(D);
172 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
173 const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT);
Reid Kleckner80944df2014-10-31 22:00:51 +0000174 if (CC == CCM_Vector)
175 Out << '@';
Rafael Espindola002667c2013-10-16 01:40:34 +0000176 Out << '@';
177 if (!Proto) {
178 Out << '0';
179 return;
180 }
181 assert(!Proto->isVariadic());
182 unsigned ArgWords = 0;
183 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
184 if (!MD->isStatic())
185 ++ArgWords;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000186 for (const auto &AT : Proto->param_types())
Reid Kleckner80944df2014-10-31 22:00:51 +0000187 // Size should be aligned to pointer size.
Rui Ueyama83aa9792016-01-14 21:00:27 +0000188 ArgWords +=
189 llvm::alignTo(ASTContext.getTypeSize(AT), TI.getPointerWidth(0)) /
190 TI.getPointerWidth(0);
Reid Kleckner80944df2014-10-31 22:00:51 +0000191 Out << ((TI.getPointerWidth(0) / 8) * ArgWords);
Rafael Espindola002667c2013-10-16 01:40:34 +0000192}
193
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000194void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
Fariborz Jahanian63628032012-06-26 16:06:38 +0000195 const NamedDecl *ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000196 raw_ostream &Out) {
Fariborz Jahanian63628032012-06-26 16:06:38 +0000197 unsigned discriminator = getBlockId(BD, false);
198 if (ID) {
199 if (shouldMangleDeclName(ID))
200 mangleName(ID, Out);
201 else {
202 Out << ID->getIdentifier()->getName();
203 }
204 }
205 if (discriminator == 0)
206 Out << "_block_invoke";
207 else
208 Out << "_block_invoke_" << discriminator+1;
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000209}
210
211void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
212 CXXCtorType CT, const BlockDecl *BD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000213 raw_ostream &ResStream) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000214 SmallString<64> Buffer;
Rafael Espindolaac00f5d2011-02-10 23:59:36 +0000215 llvm::raw_svector_ostream Out(Buffer);
216 mangleCXXCtor(CD, CT, Out);
Rafael Espindolaac00f5d2011-02-10 23:59:36 +0000217 mangleFunctionBlock(*this, Buffer, BD, ResStream);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000218}
219
220void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD,
221 CXXDtorType DT, const BlockDecl *BD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000222 raw_ostream &ResStream) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000223 SmallString<64> Buffer;
Rafael Espindolaac00f5d2011-02-10 23:59:36 +0000224 llvm::raw_svector_ostream Out(Buffer);
225 mangleCXXDtor(DD, DT, Out);
Rafael Espindolaac00f5d2011-02-10 23:59:36 +0000226 mangleFunctionBlock(*this, Buffer, BD, ResStream);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000227}
228
229void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000230 raw_ostream &Out) {
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000231 assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000232
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000233 SmallString<64> Buffer;
Rafael Espindola3968cd02011-02-11 02:52:17 +0000234 llvm::raw_svector_ostream Stream(Buffer);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000235 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
Rafael Espindola3968cd02011-02-11 02:52:17 +0000236 mangleObjCMethodName(Method, Stream);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000237 } else {
Saleem Abdulrasool64ab4de2014-10-14 17:20:14 +0000238 assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) &&
239 "expected a NamedDecl or BlockDecl");
240 if (isa<BlockDecl>(DC))
241 for (; DC && isa<BlockDecl>(DC); DC = DC->getParent())
242 (void) getBlockId(cast<BlockDecl>(DC), true);
Fariborz Jahanian68e79382014-11-14 23:55:27 +0000243 assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) &&
244 "expected a TranslationUnitDecl or a NamedDecl");
Fariborz Jahanian05834e22014-12-02 18:42:51 +0000245 if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
246 mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
247 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
248 mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
249 else if (auto ND = dyn_cast<NamedDecl>(DC)) {
Fariborz Jahanian68e79382014-11-14 23:55:27 +0000250 if (!shouldMangleDeclName(ND) && ND->getIdentifier())
251 Stream << ND->getIdentifier()->getName();
252 else {
253 // FIXME: We were doing a mangleUnqualifiedName() before, but that's
254 // a private member of a class that will soon itself be private to the
255 // Itanium C++ ABI object. What should we do now? Right now, I'm just
256 // calling the mangleName() method on the MangleContext; is there a
257 // better way?
258 mangleName(ND, Stream);
259 }
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000260 }
261 }
Rafael Espindolaac00f5d2011-02-10 23:59:36 +0000262 mangleFunctionBlock(*this, Buffer, BD, Out);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000263}
264
Argyrios Kyrtzidisca741ce2016-02-14 22:30:14 +0000265void MangleContext::mangleObjCMethodNameWithoutSize(const ObjCMethodDecl *MD,
266 raw_ostream &OS) {
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000267 const ObjCContainerDecl *CD =
268 dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
269 assert (CD && "Missing container decl in GetNameForMethod");
Argyrios Kyrtzidisa166a2b2017-03-07 09:26:07 +0000270 OS << (MD->isInstanceMethod() ? '-' : '+') << '[';
271 if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD)) {
272 OS << CID->getClassInterface()->getName();
Benjamin Kramer2f569922012-02-07 11:57:45 +0000273 OS << '(' << *CID << ')';
Argyrios Kyrtzidisa166a2b2017-03-07 09:26:07 +0000274 } else {
275 OS << CD->getName();
276 }
Aaron Ballmanb190f972014-01-03 17:59:55 +0000277 OS << ' ';
278 MD->getSelector().print(OS);
279 OS << ']';
Argyrios Kyrtzidisca741ce2016-02-14 22:30:14 +0000280}
281
282void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
283 raw_ostream &Out) {
284 SmallString<64> Name;
285 llvm::raw_svector_ostream OS(Name);
286
287 mangleObjCMethodNameWithoutSize(MD, OS);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000288 Out << OS.str().size() << OS.str();
289}