clang  3.7.0
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 
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 {
57 };
58 
59 static bool isExternC(const NamedDecl *ND) {
60  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
61  return FD->isExternC();
62  return cast<VarDecl>(ND)->isExternC();
63 }
64 
66  const NamedDecl *ND) {
67  const TargetInfo &TI = Context.getTargetInfo();
68  const llvm::Triple &Triple = TI.getTriple();
69  if (!Triple.isOSWindows() ||
70  !(Triple.getArch() == llvm::Triple::x86 ||
71  Triple.getArch() == llvm::Triple::x86_64))
72  return CCM_Other;
73 
74  if (Context.getLangOpts().CPlusPlus && !isExternC(ND) &&
76  return CCM_Other;
77 
78  const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
79  if (!FD)
80  return CCM_Other;
81  QualType T = FD->getType();
82 
83  const FunctionType *FT = T->castAs<FunctionType>();
84 
85  CallingConv CC = FT->getCallConv();
86  switch (CC) {
87  default:
88  return CCM_Other;
89  case CC_X86FastCall:
90  return CCM_Fast;
91  case CC_X86StdCall:
92  return CCM_Std;
93  case CC_X86VectorCall:
94  return CCM_Vector;
95  }
96 }
97 
100 
101  CCMangling CC = getCallingConvMangling(ASTContext, D);
102  if (CC != CCM_Other)
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  StringRef UserLabelPrefix =
131  if (!UserLabelPrefix.empty() && !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 
156  if (!MCXX)
157  Out << D->getIdentifier()->getName();
158  else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
159  mangleObjCMethodName(OMD, Out);
160  else
161  mangleCXXName(D, Out);
162 
163  const FunctionDecl *FD = cast<FunctionDecl>(D);
164  const FunctionType *FT = FD->getType()->castAs<FunctionType>();
165  const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT);
166  if (CC == CCM_Vector)
167  Out << '@';
168  Out << '@';
169  if (!Proto) {
170  Out << '0';
171  return;
172  }
173  assert(!Proto->isVariadic());
174  unsigned ArgWords = 0;
175  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
176  if (!MD->isStatic())
177  ++ArgWords;
178  for (const auto &AT : Proto->param_types())
179  // Size should be aligned to pointer size.
180  ArgWords += llvm::RoundUpToAlignment(ASTContext.getTypeSize(AT),
181  TI.getPointerWidth(0)) /
182  TI.getPointerWidth(0);
183  Out << ((TI.getPointerWidth(0) / 8) * ArgWords);
184 }
185 
187  const NamedDecl *ID,
188  raw_ostream &Out) {
189  unsigned discriminator = getBlockId(BD, false);
190  if (ID) {
191  if (shouldMangleDeclName(ID))
192  mangleName(ID, Out);
193  else {
194  Out << ID->getIdentifier()->getName();
195  }
196  }
197  if (discriminator == 0)
198  Out << "_block_invoke";
199  else
200  Out << "_block_invoke_" << discriminator+1;
201 }
202 
204  CXXCtorType CT, const BlockDecl *BD,
205  raw_ostream &ResStream) {
206  SmallString<64> Buffer;
207  llvm::raw_svector_ostream Out(Buffer);
208  mangleCXXCtor(CD, CT, Out);
209  Out.flush();
210  mangleFunctionBlock(*this, Buffer, BD, ResStream);
211 }
212 
214  CXXDtorType DT, const BlockDecl *BD,
215  raw_ostream &ResStream) {
216  SmallString<64> Buffer;
217  llvm::raw_svector_ostream Out(Buffer);
218  mangleCXXDtor(DD, DT, Out);
219  Out.flush();
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  Stream.flush();
257  mangleFunctionBlock(*this, Buffer, BD, Out);
258 }
259 
261  raw_ostream &Out) {
262  SmallString<64> Name;
263  llvm::raw_svector_ostream OS(Name);
264 
265  const ObjCContainerDecl *CD =
266  dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
267  assert (CD && "Missing container decl in GetNameForMethod");
268  OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName();
269  if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD))
270  OS << '(' << *CID << ')';
271  OS << ' ';
272  MD->getSelector().print(OS);
273  OS << ']';
274 
275  Out << OS.str().size() << OS.str();
276 }
Defines the clang::ASTContext interface.
StringRef getName() const
Definition: Decl.h:168
Complete object ctor.
Definition: ABI.h:26
IdentifierInfo * getIdentifier() const
Definition: Decl.h:163
virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, raw_ostream &)=0
Defines the SourceManager interface.
virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, raw_ostream &)=0
Defines the C++ template declaration subclasses.
ASTContext & getASTContext() const
Definition: Mangle.h:69
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2147
CallingConv getCallConv() const
Definition: Type.h:2960
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:1701
Defines the clang::Expr interface and subclasses for C++ expressions.
static CCMangling getCallingConvMangling(const ASTContext &Context, const NamedDecl *ND)
Definition: Mangle.cpp:65
bool hasAttr() const
Definition: DeclBase.h:487
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:89
bool shouldMangleDeclName(const NamedDecl *D)
Definition: Mangle.cpp:98
T * getAttr() const
Definition: DeclBase.h:484
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:518
const LangOptions & getLangOpts() const
Definition: ASTContext.h:533
Enums/classes describing ABI related information about constructors, destructors and thunks...
void print(llvm::raw_ostream &OS) const
Prints the full selector name (e.g. "foo:bar:").
QualType getType() const
Definition: Decl.h:538
void mangleName(const NamedDecl *D, raw_ostream &)
Definition: Mangle.cpp:117
ASTContext * Context
ID
Defines the set of possible language-specific address spaces.
Definition: AddressSpaces.h:27
Exposes information about the current target.
CXXDtorType
C++ destructor types.
Definition: ABI.h:34
const char * getUserLabelPrefix() const
Returns the default value of the USER_LABEL_PREFIX macro, which is the prefix given to user symbols b...
StringRef getName() const
Return the actual identifier string.
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2358
DeclContext * getDeclContext()
Definition: DeclBase.h:381
bool isInstanceMethod() const
Definition: DeclObjC.h:419
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:1174
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:204
unsigned getBlockId(const BlockDecl *BD, bool Local)
Definition: Mangle.h:75
void mangleDtorBlock(const CXXDestructorDecl *CD, CXXDtorType DT, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:213
virtual bool shouldMangleCXXName(const NamedDecl *D)=0
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1717
virtual void mangleCXXName(const NamedDecl *D, raw_ostream &)=0
const T * castAs() const
Definition: Type.h:5586
Complete object dtor.
Definition: ABI.h:36
CXXCtorType
C++ constructor types.
Definition: ABI.h:25
void mangleGlobalBlock(const BlockDecl *BD, const NamedDecl *ID, raw_ostream &Out)
Definition: Mangle.cpp:186
void mangleBlock(const DeclContext *DC, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:223
void mangleObjCMethodName(const ObjCMethodDecl *MD, raw_ostream &)
Definition: Mangle.cpp:260
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Selector getSelector() const
Definition: DeclObjC.h:328
bool hasAttrs() const
Definition: DeclBase.h:427
static void mangleFunctionBlock(MangleContext &Context, StringRef Outer, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:39
uint64_t getPointerWidth(unsigned AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Defines the clang::TargetInfo interface.
CCMangling
Definition: Mangle.cpp:52
void mangleCtorBlock(const CXXConstructorDecl *CD, CXXCtorType CT, const BlockDecl *BD, raw_ostream &Out)
Definition: Mangle.cpp:203
static bool isExternC(const NamedDecl *ND)
Definition: Mangle.cpp:59