clang  6.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 #define MANGLE_CHECKER 0
29 
30 #if MANGLE_CHECKER
31 #include <cxxabi.h>
32 #endif
33 
34 using 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 
39 static void mangleFunctionBlock(MangleContext &Context,
40  StringRef Outer,
41  const BlockDecl *BD,
42  raw_ostream &Out) {
43  unsigned discriminator = Context.getBlockId(BD, true);
44  if (discriminator == 0)
45  Out << "__" << Outer << "_block_invoke";
46  else
47  Out << "__" << Outer << "_block_invoke_" << discriminator+1;
48 }
49 
50 void MangleContext::anchor() { }
51 
52 enum CCMangling {
58 };
59 
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();
64 }
65 
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))
73  return CCM_Other;
74 
75  if (Context.getLangOpts().CPlusPlus && !isExternC(ND) &&
77  return CCM_Other;
78 
79  const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
80  if (!FD)
81  return CCM_Other;
82  QualType T = FD->getType();
83 
84  const FunctionType *FT = T->castAs<FunctionType>();
85 
86  CallingConv CC = FT->getCallConv();
87  switch (CC) {
88  default:
89  return CCM_Other;
90  case CC_X86FastCall:
91  return CCM_Fast;
92  case CC_X86StdCall:
93  return CCM_Std;
94  case CC_X86VectorCall:
95  return CCM_Vector;
96  }
97 }
98 
101 
102  CCMangling CC = getCallingConvMangling(ASTContext, D);
103  if (CC != CCM_Other)
104  return true;
105 
106  // If the declaration has an owning module for linkage purposes that needs to
107  // be mangled, we must mangle its name.
109  return true;
110 
111  // 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 
123 void 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.
135  char GlobalPrefix =
136  getASTContext().getTargetInfo().getDataLayout().getGlobalPrefix();
137  if (GlobalPrefix && !ALA->getLabel().startswith("llvm."))
138  Out << '\01'; // LLVM IR Marker for __asm("foo")
139 
140  Out << ALA->getLabel();
141  return;
142  }
143 
145  CCMangling CC = getCallingConvMangling(ASTContext, D);
146  bool MCXX = shouldMangleCXXName(D);
147  const TargetInfo &TI = Context.getTargetInfo();
148  if (CC == CCM_Other || (MCXX && TI.getCXXABI() == TargetCXXABI::Microsoft)) {
149  if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
150  mangleObjCMethodName(OMD, Out);
151  else
152  mangleCXXName(D, Out);
153  return;
154  }
155 
156  Out << '\01';
157  if (CC == CCM_Std)
158  Out << '_';
159  else if (CC == CCM_Fast)
160  Out << '@';
161  else if (CC == CCM_RegCall)
162  Out << "__regcall3__";
163 
164  if (!MCXX)
165  Out << D->getIdentifier()->getName();
166  else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
167  mangleObjCMethodName(OMD, Out);
168  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);
174  if (CC == CCM_Vector)
175  Out << '@';
176  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;
186  for (const auto &AT : Proto->param_types())
187  // Size should be aligned to pointer size.
188  ArgWords +=
189  llvm::alignTo(ASTContext.getTypeSize(AT), TI.getPointerWidth(0)) /
190  TI.getPointerWidth(0);
191  Out << ((TI.getPointerWidth(0) / 8) * ArgWords);
192 }
193 
195  const NamedDecl *ID,
196  raw_ostream &Out) {
197  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;
209 }
210 
212  CXXCtorType CT, const BlockDecl *BD,
213  raw_ostream &ResStream) {
214  SmallString<64> Buffer;
215  llvm::raw_svector_ostream Out(Buffer);
216  mangleCXXCtor(CD, CT, Out);
217  mangleFunctionBlock(*this, Buffer, BD, ResStream);
218 }
219 
221  CXXDtorType DT, const BlockDecl *BD,
222  raw_ostream &ResStream) {
223  SmallString<64> Buffer;
224  llvm::raw_svector_ostream Out(Buffer);
225  mangleCXXDtor(DD, DT, Out);
226  mangleFunctionBlock(*this, Buffer, BD, ResStream);
227 }
228 
230  raw_ostream &Out) {
231  assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
232 
233  SmallString<64> Buffer;
234  llvm::raw_svector_ostream Stream(Buffer);
235  if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
236  mangleObjCMethodName(Method, Stream);
237  } else {
238  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);
243  assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) &&
244  "expected a TranslationUnitDecl or a NamedDecl");
245  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)) {
250  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  }
260  }
261  }
262  mangleFunctionBlock(*this, Buffer, BD, Out);
263 }
264 
266  raw_ostream &OS) {
267  const ObjCContainerDecl *CD =
268  dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
269  assert (CD && "Missing container decl in GetNameForMethod");
270  OS << (MD->isInstanceMethod() ? '-' : '+') << '[';
271  if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD)) {
272  OS << CID->getClassInterface()->getName();
273  OS << '(' << *CID << ')';
274  } else {
275  OS << CD->getName();
276  }
277  OS << ' ';
278  MD->getSelector().print(OS);
279  OS << ']';
280 }
281 
283  raw_ostream &Out) {
284  SmallString<64> Name;
285  llvm::raw_svector_ostream OS(Name);
286 
288  Out << OS.str().size() << OS.str();
289 }
Defines the clang::ASTContext interface.
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1698
Complete object ctor.
Definition: ABI.h:26
A (possibly-)qualified type.
Definition: Type.h:653
Module * getOwningModuleForLinkage(bool IgnoreLinkage=false) const
Get the module that owns this declaration for linkage purposes.
Definition: Decl.cpp:1432
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:3058
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:790
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:671
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2397
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:265
IdentifierInfo * getIdentifier() const
getIdentifier - Get the identifier that names this declaration, if there is one.
Definition: Decl.h:265
uint64_t getPointerWidth(unsigned AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition: TargetInfo.h:311
static CCMangling getCallingConvMangling(const ASTContext &Context, const NamedDecl *ND)
Definition: Mangle.cpp:66
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Definition: TargetInfo.h:859
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
bool shouldMangleDeclName(const NamedDecl *D)
Definition: Mangle.cpp:99
ObjCContainerDecl - Represents a container for method declarations.
Definition: DeclObjC.h:986
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:123
bool hasAttr() const
Definition: DeclBase.h:535
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3270
Exposes information about the current target.
Definition: TargetInfo.h:54
CXXDtorType
C++ destructor types.
Definition: ABI.h:34
ASTContext & getASTContext() const
Definition: Mangle.h:70
BlockDecl - This represents a block literal declaration, which is like an unnamed FunctionDecl...
Definition: Decl.h:3689
const FunctionProtoType * T
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:6370
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2620
DeclContext * getDeclContext()
Definition: DeclBase.h:425
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:1331
bool isInstanceMethod() const
Definition: DeclObjC.h:452
Selector getSelector() const
Definition: DeclObjC.h:359
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:233
bool hasAttrs() const
Definition: DeclBase.h:471
unsigned getBlockId(const BlockDecl *BD, bool Local)
Definition: Mangle.h:76
void mangleDtorBlock(const CXXDestructorDecl *CD, CXXDtorType DT, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:220
virtual bool shouldMangleCXXName(const NamedDecl *D)=0
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1964
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition: Mangle.h:42
virtual void mangleCXXName(const NamedDecl *D, raw_ostream &)=0
const llvm::DataLayout & getDataLayout() const
Definition: TargetInfo.h:794
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:194
void mangleBlock(const DeclContext *DC, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:229
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:1252
void mangleObjCMethodName(const ObjCMethodDecl *MD, raw_ostream &)
Definition: Mangle.cpp:282
T * getAttr() const
Definition: DeclBase.h:531
static void mangleFunctionBlock(MangleContext &Context, StringRef Outer, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:39
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2007
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:3213
Defines the clang::TargetInfo interface.
StringRef getName() const
getName - Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:270
QualType getType() const
Definition: Decl.h:639
NamedDecl - This represents a decl with a name.
Definition: Decl.h:245
CCMangling
Definition: Mangle.cpp:52
void mangleCtorBlock(const CXXConstructorDecl *CD, CXXCtorType CT, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:211
const LangOptions & getLangOpts() const
Definition: ASTContext.h:688
ObjCCategoryImplDecl - An object of this class encapsulates a category @implementation declaration...
Definition: DeclObjC.h:2518
bool hasExternalFormalLinkage() const
True if this decl has external linkage.
Definition: Decl.h:367
static bool isExternC(const NamedDecl *ND)
Definition: Mangle.cpp:60