blob: 03ebf98a7f161fa976a511b6dc8dfff15f9c71ee [file] [log] [blame]
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00001//===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Implements generic name mangling support for blocks and Objective-C.
10//
11//===----------------------------------------------------------------------===//
Rafael Espindola002667c2013-10-16 01:40:34 +000012#include "clang/AST/Attr.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000013#include "clang/AST/ASTContext.h"
14#include "clang/AST/Decl.h"
15#include "clang/AST/DeclCXX.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/AST/DeclTemplate.h"
18#include "clang/AST/ExprCXX.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000019#include "clang/AST/Mangle.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000020#include "clang/Basic/ABI.h"
21#include "clang/Basic/SourceManager.h"
Rafael Espindola002667c2013-10-16 01:40:34 +000022#include "clang/Basic/TargetInfo.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000023#include "llvm/ADT/StringExtras.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000024#include "llvm/Support/ErrorHandling.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "llvm/Support/raw_ostream.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000026
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000027using namespace clang;
28
29// FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves
30// much to be desired. Come up with a better mangling scheme.
31
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000032static void mangleFunctionBlock(MangleContext &Context,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000033 StringRef Outer,
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000034 const BlockDecl *BD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000035 raw_ostream &Out) {
Fariborz Jahanian63628032012-06-26 16:06:38 +000036 unsigned discriminator = Context.getBlockId(BD, true);
37 if (discriminator == 0)
38 Out << "__" << Outer << "_block_invoke";
39 else
Fangrui Song6907ce22018-07-30 19:24:48 +000040 Out << "__" << Outer << "_block_invoke_" << discriminator+1;
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000041}
42
David Blaikie68e081d2011-12-20 02:48:34 +000043void MangleContext::anchor() { }
44
Reid Kleckner80944df2014-10-31 22:00:51 +000045enum CCMangling {
46 CCM_Other,
47 CCM_Fast,
Erich Keane757d3172016-11-02 18:29:35 +000048 CCM_RegCall,
Reid Kleckner80944df2014-10-31 22:00:51 +000049 CCM_Vector,
50 CCM_Std
Rafael Espindola002667c2013-10-16 01:40:34 +000051};
52
53static bool isExternC(const NamedDecl *ND) {
54 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
55 return FD->isExternC();
56 return cast<VarDecl>(ND)->isExternC();
57}
58
Reid Kleckner80944df2014-10-31 22:00:51 +000059static CCMangling getCallingConvMangling(const ASTContext &Context,
60 const NamedDecl *ND) {
Rafael Espindola002667c2013-10-16 01:40:34 +000061 const TargetInfo &TI = Context.getTargetInfo();
Benjamin Kramer9299637dc2014-03-04 19:31:42 +000062 const llvm::Triple &Triple = TI.getTriple();
Reid Kleckner80944df2014-10-31 22:00:51 +000063 if (!Triple.isOSWindows() ||
64 !(Triple.getArch() == llvm::Triple::x86 ||
65 Triple.getArch() == llvm::Triple::x86_64))
66 return CCM_Other;
Rafael Espindola002667c2013-10-16 01:40:34 +000067
68 if (Context.getLangOpts().CPlusPlus && !isExternC(ND) &&
69 TI.getCXXABI() == TargetCXXABI::Microsoft)
Reid Kleckner80944df2014-10-31 22:00:51 +000070 return CCM_Other;
Rafael Espindola002667c2013-10-16 01:40:34 +000071
72 const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
73 if (!FD)
Reid Kleckner80944df2014-10-31 22:00:51 +000074 return CCM_Other;
Rafael Espindola002667c2013-10-16 01:40:34 +000075 QualType T = FD->getType();
76
77 const FunctionType *FT = T->castAs<FunctionType>();
78
79 CallingConv CC = FT->getCallConv();
80 switch (CC) {
81 default:
Reid Kleckner80944df2014-10-31 22:00:51 +000082 return CCM_Other;
Rafael Espindola002667c2013-10-16 01:40:34 +000083 case CC_X86FastCall:
Reid Kleckner80944df2014-10-31 22:00:51 +000084 return CCM_Fast;
Rafael Espindola002667c2013-10-16 01:40:34 +000085 case CC_X86StdCall:
Reid Kleckner80944df2014-10-31 22:00:51 +000086 return CCM_Std;
87 case CC_X86VectorCall:
88 return CCM_Vector;
Rafael Espindola002667c2013-10-16 01:40:34 +000089 }
90}
91
92bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
93 const ASTContext &ASTContext = getASTContext();
94
Reid Kleckner80944df2014-10-31 22:00:51 +000095 CCMangling CC = getCallingConvMangling(ASTContext, D);
96 if (CC != CCM_Other)
Rafael Espindola002667c2013-10-16 01:40:34 +000097 return true;
98
Richard Smithcd4a7a42017-09-07 00:55:55 +000099 // If the declaration has an owning module for linkage purposes that needs to
100 // be mangled, we must mangle its name.
101 if (!D->hasExternalFormalLinkage() && D->getOwningModuleForLinkage())
102 return true;
103
Rafael Espindola002667c2013-10-16 01:40:34 +0000104 // In C, functions with no attributes never need to be mangled. Fastpath them.
105 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
106 return false;
107
108 // Any decl can be declared with __asm("foo") on it, and this takes precedence
109 // over all other naming in the .o file.
110 if (D->hasAttr<AsmLabelAttr>())
111 return true;
112
113 return shouldMangleCXXName(D);
114}
115
116void MangleContext::mangleName(const NamedDecl *D, raw_ostream &Out) {
117 // Any decl can be declared with __asm("foo") on it, and this takes precedence
118 // over all other naming in the .o file.
119 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
120 // If we have an asm name, then we use it as the mangling.
121
122 // Adding the prefix can cause problems when one file has a "foo" and
123 // another has a "\01foo". That is known to happen on ELF with the
124 // tricks normally used for producing aliases (PR9177). Fortunately the
125 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
126 // marker. We also avoid adding the marker if this is an alias for an
127 // LLVM intrinsic.
James Y Knightb214cbc2016-03-04 19:00:41 +0000128 char GlobalPrefix =
129 getASTContext().getTargetInfo().getDataLayout().getGlobalPrefix();
130 if (GlobalPrefix && !ALA->getLabel().startswith("llvm."))
Rafael Espindola002667c2013-10-16 01:40:34 +0000131 Out << '\01'; // LLVM IR Marker for __asm("foo")
132
133 Out << ALA->getLabel();
134 return;
135 }
136
137 const ASTContext &ASTContext = getASTContext();
Reid Kleckner80944df2014-10-31 22:00:51 +0000138 CCMangling CC = getCallingConvMangling(ASTContext, D);
Rafael Espindola002667c2013-10-16 01:40:34 +0000139 bool MCXX = shouldMangleCXXName(D);
140 const TargetInfo &TI = Context.getTargetInfo();
Reid Kleckner80944df2014-10-31 22:00:51 +0000141 if (CC == CCM_Other || (MCXX && TI.getCXXABI() == TargetCXXABI::Microsoft)) {
Saleem Abdulrasool3b434472014-10-14 17:20:18 +0000142 if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
143 mangleObjCMethodName(OMD, Out);
144 else
145 mangleCXXName(D, Out);
Rafael Espindola002667c2013-10-16 01:40:34 +0000146 return;
147 }
148
149 Out << '\01';
Reid Kleckner80944df2014-10-31 22:00:51 +0000150 if (CC == CCM_Std)
Rafael Espindola002667c2013-10-16 01:40:34 +0000151 Out << '_';
Reid Kleckner80944df2014-10-31 22:00:51 +0000152 else if (CC == CCM_Fast)
Rafael Espindola002667c2013-10-16 01:40:34 +0000153 Out << '@';
Erich Keane757d3172016-11-02 18:29:35 +0000154 else if (CC == CCM_RegCall)
155 Out << "__regcall3__";
Rafael Espindola002667c2013-10-16 01:40:34 +0000156
157 if (!MCXX)
158 Out << D->getIdentifier()->getName();
Saleem Abdulrasool3b434472014-10-14 17:20:18 +0000159 else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
160 mangleObjCMethodName(OMD, Out);
Rafael Espindola002667c2013-10-16 01:40:34 +0000161 else
162 mangleCXXName(D, Out);
163
164 const FunctionDecl *FD = cast<FunctionDecl>(D);
165 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
166 const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT);
Reid Kleckner80944df2014-10-31 22:00:51 +0000167 if (CC == CCM_Vector)
168 Out << '@';
Rafael Espindola002667c2013-10-16 01:40:34 +0000169 Out << '@';
170 if (!Proto) {
171 Out << '0';
172 return;
173 }
174 assert(!Proto->isVariadic());
175 unsigned ArgWords = 0;
176 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
177 if (!MD->isStatic())
178 ++ArgWords;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000179 for (const auto &AT : Proto->param_types())
Reid Kleckner80944df2014-10-31 22:00:51 +0000180 // Size should be aligned to pointer size.
Rui Ueyama83aa9792016-01-14 21:00:27 +0000181 ArgWords +=
182 llvm::alignTo(ASTContext.getTypeSize(AT), TI.getPointerWidth(0)) /
183 TI.getPointerWidth(0);
Reid Kleckner80944df2014-10-31 22:00:51 +0000184 Out << ((TI.getPointerWidth(0) / 8) * ArgWords);
Rafael Espindola002667c2013-10-16 01:40:34 +0000185}
186
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000187void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
Fariborz Jahanian63628032012-06-26 16:06:38 +0000188 const NamedDecl *ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000189 raw_ostream &Out) {
Fariborz Jahanian63628032012-06-26 16:06:38 +0000190 unsigned discriminator = getBlockId(BD, false);
191 if (ID) {
192 if (shouldMangleDeclName(ID))
193 mangleName(ID, Out);
194 else {
195 Out << ID->getIdentifier()->getName();
196 }
197 }
198 if (discriminator == 0)
199 Out << "_block_invoke";
200 else
201 Out << "_block_invoke_" << discriminator+1;
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000202}
203
204void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
205 CXXCtorType CT, const BlockDecl *BD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000206 raw_ostream &ResStream) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000207 SmallString<64> Buffer;
Rafael Espindolaac00f5d2011-02-10 23:59:36 +0000208 llvm::raw_svector_ostream Out(Buffer);
209 mangleCXXCtor(CD, CT, Out);
Rafael Espindolaac00f5d2011-02-10 23:59:36 +0000210 mangleFunctionBlock(*this, Buffer, BD, ResStream);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000211}
212
213void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD,
214 CXXDtorType DT, const BlockDecl *BD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000215 raw_ostream &ResStream) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000216 SmallString<64> Buffer;
Rafael Espindolaac00f5d2011-02-10 23:59:36 +0000217 llvm::raw_svector_ostream Out(Buffer);
218 mangleCXXDtor(DD, DT, Out);
Rafael Espindolaac00f5d2011-02-10 23:59:36 +0000219 mangleFunctionBlock(*this, Buffer, BD, ResStream);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000220}
221
222void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000223 raw_ostream &Out) {
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000224 assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000225
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000226 SmallString<64> Buffer;
Rafael Espindola3968cd02011-02-11 02:52:17 +0000227 llvm::raw_svector_ostream Stream(Buffer);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000228 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
Rafael Espindola3968cd02011-02-11 02:52:17 +0000229 mangleObjCMethodName(Method, Stream);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000230 } else {
Saleem Abdulrasool64ab4de2014-10-14 17:20:14 +0000231 assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) &&
232 "expected a NamedDecl or BlockDecl");
233 if (isa<BlockDecl>(DC))
234 for (; DC && isa<BlockDecl>(DC); DC = DC->getParent())
235 (void) getBlockId(cast<BlockDecl>(DC), true);
Fariborz Jahanian68e79382014-11-14 23:55:27 +0000236 assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) &&
237 "expected a TranslationUnitDecl or a NamedDecl");
Fariborz Jahanian05834e22014-12-02 18:42:51 +0000238 if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
239 mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
240 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
241 mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
242 else if (auto ND = dyn_cast<NamedDecl>(DC)) {
Fariborz Jahanian68e79382014-11-14 23:55:27 +0000243 if (!shouldMangleDeclName(ND) && ND->getIdentifier())
244 Stream << ND->getIdentifier()->getName();
245 else {
246 // FIXME: We were doing a mangleUnqualifiedName() before, but that's
247 // a private member of a class that will soon itself be private to the
248 // Itanium C++ ABI object. What should we do now? Right now, I'm just
249 // calling the mangleName() method on the MangleContext; is there a
250 // better way?
251 mangleName(ND, Stream);
252 }
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000253 }
254 }
Rafael Espindolaac00f5d2011-02-10 23:59:36 +0000255 mangleFunctionBlock(*this, Buffer, BD, Out);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000256}
257
Argyrios Kyrtzidisca741ce2016-02-14 22:30:14 +0000258void MangleContext::mangleObjCMethodNameWithoutSize(const ObjCMethodDecl *MD,
259 raw_ostream &OS) {
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000260 const ObjCContainerDecl *CD =
261 dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
262 assert (CD && "Missing container decl in GetNameForMethod");
Argyrios Kyrtzidisa166a2b2017-03-07 09:26:07 +0000263 OS << (MD->isInstanceMethod() ? '-' : '+') << '[';
264 if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD)) {
265 OS << CID->getClassInterface()->getName();
Benjamin Kramer2f569922012-02-07 11:57:45 +0000266 OS << '(' << *CID << ')';
Argyrios Kyrtzidisa166a2b2017-03-07 09:26:07 +0000267 } else {
268 OS << CD->getName();
269 }
Aaron Ballmanb190f972014-01-03 17:59:55 +0000270 OS << ' ';
271 MD->getSelector().print(OS);
272 OS << ']';
Argyrios Kyrtzidisca741ce2016-02-14 22:30:14 +0000273}
274
275void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
276 raw_ostream &Out) {
277 SmallString<64> Name;
278 llvm::raw_svector_ostream OS(Name);
279
280 mangleObjCMethodNameWithoutSize(MD, OS);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000281 Out << OS.str().size() << OS.str();
282}