clang  8.0.0svn
Mangle.cpp
Go to the documentation of this file.
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/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"
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"
27 
28 using namespace clang;
29 
30 // FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves
31 // much to be desired. Come up with a better mangling scheme.
32 
33 static void mangleFunctionBlock(MangleContext &Context,
34  StringRef Outer,
35  const BlockDecl *BD,
36  raw_ostream &Out) {
37  unsigned discriminator = Context.getBlockId(BD, true);
38  if (discriminator == 0)
39  Out << "__" << Outer << "_block_invoke";
40  else
41  Out << "__" << Outer << "_block_invoke_" << discriminator+1;
42 }
43 
44 void MangleContext::anchor() { }
45 
46 enum CCMangling {
52 };
53 
54 static bool isExternC(const NamedDecl *ND) {
55  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
56  return FD->isExternC();
57  return cast<VarDecl>(ND)->isExternC();
58 }
59 
61  const NamedDecl *ND) {
62  const TargetInfo &TI = Context.getTargetInfo();
63  const llvm::Triple &Triple = TI.getTriple();
64  if (!Triple.isOSWindows() ||
65  !(Triple.getArch() == llvm::Triple::x86 ||
66  Triple.getArch() == llvm::Triple::x86_64))
67  return CCM_Other;
68 
69  if (Context.getLangOpts().CPlusPlus && !isExternC(ND) &&
71  return CCM_Other;
72 
73  const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
74  if (!FD)
75  return CCM_Other;
76  QualType T = FD->getType();
77 
78  const FunctionType *FT = T->castAs<FunctionType>();
79 
80  CallingConv CC = FT->getCallConv();
81  switch (CC) {
82  default:
83  return CCM_Other;
84  case CC_X86FastCall:
85  return CCM_Fast;
86  case CC_X86StdCall:
87  return CCM_Std;
88  case CC_X86VectorCall:
89  return CCM_Vector;
90  }
91 }
92 
95 
96  CCMangling CC = getCallingConvMangling(ASTContext, D);
97  if (CC != CCM_Other)
98  return true;
99 
100  // If the declaration has an owning module for linkage purposes that needs to
101  // be mangled, we must mangle its name.
103  return true;
104 
105  // In C, functions with no attributes never need to be mangled. Fastpath them.
106  if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
107  return false;
108 
109  // Any decl can be declared with __asm("foo") on it, and this takes precedence
110  // over all other naming in the .o file.
111  if (D->hasAttr<AsmLabelAttr>())
112  return true;
113 
114  return shouldMangleCXXName(D);
115 }
116 
117 void MangleContext::mangleName(const NamedDecl *D, raw_ostream &Out) {
118  // Any decl can be declared with __asm("foo") on it, and this takes precedence
119  // over all other naming in the .o file.
120  if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
121  // If we have an asm name, then we use it as the mangling.
122 
123  // Adding the prefix can cause problems when one file has a "foo" and
124  // another has a "\01foo". That is known to happen on ELF with the
125  // tricks normally used for producing aliases (PR9177). Fortunately the
126  // llvm mangler on ELF is a nop, so we can just avoid adding the \01
127  // marker. We also avoid adding the marker if this is an alias for an
128  // LLVM intrinsic.
129  char GlobalPrefix =
130  getASTContext().getTargetInfo().getDataLayout().getGlobalPrefix();
131  if (GlobalPrefix && !ALA->getLabel().startswith("llvm."))
132  Out << '\01'; // LLVM IR Marker for __asm("foo")
133 
134  Out << ALA->getLabel();
135  return;
136  }
137 
139  CCMangling CC = getCallingConvMangling(ASTContext, D);
140  bool MCXX = shouldMangleCXXName(D);
141  const TargetInfo &TI = Context.getTargetInfo();
142  if (CC == CCM_Other || (MCXX && TI.getCXXABI() == TargetCXXABI::Microsoft)) {
143  if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
144  mangleObjCMethodName(OMD, Out);
145  else
146  mangleCXXName(D, Out);
147  return;
148  }
149 
150  Out << '\01';
151  if (CC == CCM_Std)
152  Out << '_';
153  else if (CC == CCM_Fast)
154  Out << '@';
155  else if (CC == CCM_RegCall)
156  Out << "__regcall3__";
157 
158  if (!MCXX)
159  Out << D->getIdentifier()->getName();
160  else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
161  mangleObjCMethodName(OMD, Out);
162  else
163  mangleCXXName(D, Out);
164 
165  const FunctionDecl *FD = cast<FunctionDecl>(D);
166  const FunctionType *FT = FD->getType()->castAs<FunctionType>();
167  const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT);
168  if (CC == CCM_Vector)
169  Out << '@';
170  Out << '@';
171  if (!Proto) {
172  Out << '0';
173  return;
174  }
175  assert(!Proto->isVariadic());
176  unsigned ArgWords = 0;
177  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
178  if (!MD->isStatic())
179  ++ArgWords;
180  for (const auto &AT : Proto->param_types())
181  // Size should be aligned to pointer size.
182  ArgWords +=
183  llvm::alignTo(ASTContext.getTypeSize(AT), TI.getPointerWidth(0)) /
184  TI.getPointerWidth(0);
185  Out << ((TI.getPointerWidth(0) / 8) * ArgWords);
186 }
187 
189  const NamedDecl *ID,
190  raw_ostream &Out) {
191  unsigned discriminator = getBlockId(BD, false);
192  if (ID) {
193  if (shouldMangleDeclName(ID))
194  mangleName(ID, Out);
195  else {
196  Out << ID->getIdentifier()->getName();
197  }
198  }
199  if (discriminator == 0)
200  Out << "_block_invoke";
201  else
202  Out << "_block_invoke_" << discriminator+1;
203 }
204 
206  CXXCtorType CT, const BlockDecl *BD,
207  raw_ostream &ResStream) {
208  SmallString<64> Buffer;
209  llvm::raw_svector_ostream Out(Buffer);
210  mangleCXXCtor(CD, CT, Out);
211  mangleFunctionBlock(*this, Buffer, BD, ResStream);
212 }
213 
215  CXXDtorType DT, const BlockDecl *BD,
216  raw_ostream &ResStream) {
217  SmallString<64> Buffer;
218  llvm::raw_svector_ostream Out(Buffer);
219  mangleCXXDtor(DD, DT, Out);
220  mangleFunctionBlock(*this, Buffer, BD, ResStream);
221 }
222 
224  raw_ostream &Out) {
225  assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
226 
227  SmallString<64> Buffer;
228  llvm::raw_svector_ostream Stream(Buffer);
229  if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
230  mangleObjCMethodName(Method, Stream);
231  } else {
232  assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) &&
233  "expected a NamedDecl or BlockDecl");
234  if (isa<BlockDecl>(DC))
235  for (; DC && isa<BlockDecl>(DC); DC = DC->getParent())
236  (void) getBlockId(cast<BlockDecl>(DC), true);
237  assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) &&
238  "expected a TranslationUnitDecl or a NamedDecl");
239  if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
240  mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
241  else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
242  mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
243  else if (auto ND = dyn_cast<NamedDecl>(DC)) {
244  if (!shouldMangleDeclName(ND) && ND->getIdentifier())
245  Stream << ND->getIdentifier()->getName();
246  else {
247  // FIXME: We were doing a mangleUnqualifiedName() before, but that's
248  // a private member of a class that will soon itself be private to the
249  // Itanium C++ ABI object. What should we do now? Right now, I'm just
250  // calling the mangleName() method on the MangleContext; is there a
251  // better way?
252  mangleName(ND, Stream);
253  }
254  }
255  }
256  mangleFunctionBlock(*this, Buffer, BD, Out);
257 }
258 
260  raw_ostream &OS) {
261  const ObjCContainerDecl *CD =
262  dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
263  assert (CD && "Missing container decl in GetNameForMethod");
264  OS << (MD->isInstanceMethod() ? '-' : '+') << '[';
265  if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD)) {
266  OS << CID->getClassInterface()->getName();
267  OS << '(' << *CID << ')';
268  } else {
269  OS << CD->getName();
270  }
271  OS << ' ';
272  MD->getSelector().print(OS);
273  OS << ']';
274 }
275 
277  raw_ostream &Out) {
278  SmallString<64> Name;
279  llvm::raw_svector_ostream OS(Name);
280 
282  Out << OS.str().size() << OS.str();
283 }
Defines the clang::ASTContext interface.
Represents a function declaration or definition.
Definition: Decl.h:1739
Complete object ctor.
Definition: ABI.h:26
A (possibly-)qualified type.
Definition: Type.h:638
Module * getOwningModuleForLinkage(bool IgnoreLinkage=false) const
Get the module that owns this declaration for linkage purposes.
Definition: Decl.cpp:1481
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:3359
virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, raw_ostream &)=0
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:949
Defines the SourceManager interface.
virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, raw_ostream &)=0
Defines the C++ template declaration subclasses.
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:689
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2484
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:139
Defines the clang::Expr interface and subclasses for C++ expressions.
void mangleObjCMethodNameWithoutSize(const ObjCMethodDecl *MD, raw_ostream &)
Definition: Mangle.cpp:259
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:269
uint64_t getPointerWidth(unsigned AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition: TargetInfo.h:348
static CCMangling getCallingConvMangling(const ASTContext &Context, const NamedDecl *ND)
Definition: Mangle.cpp:60
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Definition: TargetInfo.h:1018
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
bool shouldMangleDeclName(const NamedDecl *D)
Definition: Mangle.cpp:93
ObjCContainerDecl - Represents a container for method declarations.
Definition: DeclObjC.h:961
The Microsoft ABI is the ABI used by Microsoft Visual Studio (and compatible compilers).
Definition: TargetCXXABI.h:113
Enums/classes describing ABI related information about constructors, destructors and thunks...
void mangleName(const NamedDecl *D, raw_ostream &)
Definition: Mangle.cpp:117
bool hasAttr() const
Definition: DeclBase.h:531
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3691
Exposes information about the current target.
Definition: TargetInfo.h:54
CXXDtorType
C++ destructor types.
Definition: ABI.h:34
ASTContext & getASTContext() const
Definition: Mangle.h:71
Pepresents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:3867
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:6797
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2706
DeclContext * getDeclContext()
Definition: DeclBase.h:427
void print(llvm::raw_ostream &OS) const
Prints the full selector name (e.g. "foo:bar:").
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:1752
bool isInstanceMethod() const
Definition: DeclObjC.h:414
Selector getSelector() const
Definition: DeclObjC.h:321
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:236
bool hasAttrs() const
Definition: DeclBase.h:473
unsigned getBlockId(const BlockDecl *BD, bool Local)
Definition: Mangle.h:77
void mangleDtorBlock(const CXXDestructorDecl *CD, CXXDtorType DT, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:214
virtual bool shouldMangleCXXName(const NamedDecl *D)=0
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2041
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition: Mangle.h:43
virtual void mangleCXXName(const NamedDecl *D, raw_ostream &)=0
const llvm::DataLayout & getDataLayout() const
Definition: TargetInfo.h:953
Complete object dtor.
Definition: ABI.h:36
CXXCtorType
C++ constructor types.
Definition: ABI.h:25
StringRef getName() const
Return the actual identifier string.
void mangleGlobalBlock(const BlockDecl *BD, const NamedDecl *ID, raw_ostream &Out)
Definition: Mangle.cpp:188
void mangleBlock(const DeclContext *DC, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:223
Dataflow Directional Tag Classes.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1262
void mangleObjCMethodName(const ObjCMethodDecl *MD, raw_ostream &)
Definition: Mangle.cpp:276
T * getAttr() const
Definition: DeclBase.h:527
static void mangleFunctionBlock(MangleContext &Context, StringRef Outer, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:33
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2069
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
CallingConv getCallConv() const
Definition: Type.h:3627
Defines the clang::TargetInfo interface.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:275
QualType getType() const
Definition: Decl.h:647
This represents a decl that may have a name.
Definition: Decl.h:248
CCMangling
Definition: Mangle.cpp:46
void mangleCtorBlock(const CXXConstructorDecl *CD, CXXCtorType CT, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:205
const LangOptions & getLangOpts() const
Definition: ASTContext.h:706
ObjCCategoryImplDecl - An object of this class encapsulates a category @implementation declaration...
Definition: DeclObjC.h:2491
bool hasExternalFormalLinkage() const
True if this decl has external linkage.
Definition: Decl.h:375
static bool isExternC(const NamedDecl *ND)
Definition: Mangle.cpp:54