clang  7.0.0svn
ItaniumCXXABI.cpp
Go to the documentation of this file.
1 //===------- ItaniumCXXABI.cpp - AST support for the Itanium C++ ABI ------===//
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 // This provides C++ AST support targeting the Itanium C++ ABI, which is
11 // documented at:
12 // http://www.codesourcery.com/public/cxx-abi/abi.html
13 // http://www.codesourcery.com/public/cxx-abi/abi-eh.html
14 //
15 // It also supports the closely-related ARM C++ ABI, documented at:
16 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #include "CXXABI.h"
21 #include "clang/AST/ASTContext.h"
22 #include "clang/AST/DeclCXX.h"
24 #include "clang/AST/RecordLayout.h"
25 #include "clang/AST/Type.h"
26 #include "clang/Basic/TargetInfo.h"
27 #include "llvm/ADT/iterator.h"
28 
29 using namespace clang;
30 
31 namespace {
32 
33 /// According to Itanium C++ ABI 5.1.2:
34 /// the name of an anonymous union is considered to be
35 /// the name of the first named data member found by a pre-order,
36 /// depth-first, declaration-order walk of the data members of
37 /// the anonymous union.
38 /// If there is no such data member (i.e., if all of the data members
39 /// in the union are unnamed), then there is no way for a program to
40 /// refer to the anonymous union, and there is therefore no need to mangle its name.
41 ///
42 /// Returns the name of anonymous union VarDecl or nullptr if it is not found.
43 static const IdentifierInfo *findAnonymousUnionVarDeclName(const VarDecl& VD) {
44  const RecordType *RT = VD.getType()->getAs<RecordType>();
45  assert(RT && "type of VarDecl is expected to be RecordType.");
46  assert(RT->getDecl()->isUnion() && "RecordType is expected to be a union.");
47  if (const FieldDecl *FD = RT->getDecl()->findFirstNamedDataMember()) {
48  return FD->getIdentifier();
49  }
50 
51  return nullptr;
52 }
53 
54 /// The name of a decomposition declaration.
55 struct DecompositionDeclName {
56  using BindingArray = ArrayRef<const BindingDecl*>;
57 
58  /// Representative example of a set of bindings with these names.
59  BindingArray Bindings;
60 
61  /// Iterators over the sequence of identifiers in the name.
62  struct Iterator
63  : llvm::iterator_adaptor_base<Iterator, BindingArray::const_iterator,
64  std::random_access_iterator_tag,
65  const IdentifierInfo *> {
66  Iterator(BindingArray::const_iterator It) : iterator_adaptor_base(It) {}
67  const IdentifierInfo *operator*() const {
68  return (*this->I)->getIdentifier();
69  }
70  };
71  Iterator begin() const { return Iterator(Bindings.begin()); }
72  Iterator end() const { return Iterator(Bindings.end()); }
73 };
74 }
75 
76 namespace llvm {
77 template<>
78 struct DenseMapInfo<DecompositionDeclName> {
81  static DecompositionDeclName getEmptyKey() {
82  return {ArrayInfo::getEmptyKey()};
83  }
84  static DecompositionDeclName getTombstoneKey() {
85  return {ArrayInfo::getTombstoneKey()};
86  }
87  static unsigned getHashValue(DecompositionDeclName Key) {
88  assert(!isEqual(Key, getEmptyKey()) && !isEqual(Key, getTombstoneKey()));
89  return llvm::hash_combine_range(Key.begin(), Key.end());
90  }
91  static bool isEqual(DecompositionDeclName LHS, DecompositionDeclName RHS) {
92  if (ArrayInfo::isEqual(LHS.Bindings, ArrayInfo::getEmptyKey()))
93  return ArrayInfo::isEqual(RHS.Bindings, ArrayInfo::getEmptyKey());
94  if (ArrayInfo::isEqual(LHS.Bindings, ArrayInfo::getTombstoneKey()))
95  return ArrayInfo::isEqual(RHS.Bindings, ArrayInfo::getTombstoneKey());
96  return LHS.Bindings.size() == RHS.Bindings.size() &&
97  std::equal(LHS.begin(), LHS.end(), RHS.begin());
98  }
99 };
100 }
101 
102 namespace {
103 
104 /// Keeps track of the mangled names of lambda expressions and block
105 /// literals within a particular context.
106 class ItaniumNumberingContext : public MangleNumberingContext {
107  llvm::DenseMap<const Type *, unsigned> ManglingNumbers;
108  llvm::DenseMap<const IdentifierInfo *, unsigned> VarManglingNumbers;
109  llvm::DenseMap<const IdentifierInfo *, unsigned> TagManglingNumbers;
110  llvm::DenseMap<DecompositionDeclName, unsigned>
111  DecompsitionDeclManglingNumbers;
112 
113 public:
114  unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
115  const FunctionProtoType *Proto =
116  CallOperator->getType()->getAs<FunctionProtoType>();
117  ASTContext &Context = CallOperator->getASTContext();
118 
120  EPI.Variadic = Proto->isVariadic();
121  QualType Key =
122  Context.getFunctionType(Context.VoidTy, Proto->getParamTypes(), EPI);
123  Key = Context.getCanonicalType(Key);
124  return ++ManglingNumbers[Key->castAs<FunctionProtoType>()];
125  }
126 
127  unsigned getManglingNumber(const BlockDecl *BD) override {
128  const Type *Ty = nullptr;
129  return ++ManglingNumbers[Ty];
130  }
131 
132  unsigned getStaticLocalNumber(const VarDecl *VD) override {
133  return 0;
134  }
135 
136  /// Variable decls are numbered by identifier.
137  unsigned getManglingNumber(const VarDecl *VD, unsigned) override {
138  if (auto *DD = dyn_cast<DecompositionDecl>(VD))
139  return ++DecompsitionDeclManglingNumbers[
140  DecompositionDeclName{DD->bindings()}];
141 
142  const IdentifierInfo *Identifier = VD->getIdentifier();
143  if (!Identifier) {
144  // VarDecl without an identifier represents an anonymous union
145  // declaration.
146  Identifier = findAnonymousUnionVarDeclName(*VD);
147  }
148  return ++VarManglingNumbers[Identifier];
149  }
150 
151  unsigned getManglingNumber(const TagDecl *TD, unsigned) override {
152  return ++TagManglingNumbers[TD->getIdentifier()];
153  }
154 };
155 
156 class ItaniumCXXABI : public CXXABI {
157 protected:
158  ASTContext &Context;
159 public:
160  ItaniumCXXABI(ASTContext &Ctx) : Context(Ctx) { }
161 
162  MemberPointerInfo
163  getMemberPointerInfo(const MemberPointerType *MPT) const override {
164  const TargetInfo &Target = Context.getTargetInfo();
165  TargetInfo::IntType PtrDiff = Target.getPtrDiffType(0);
166  MemberPointerInfo MPI;
167  MPI.Width = Target.getTypeWidth(PtrDiff);
168  MPI.Align = Target.getTypeAlign(PtrDiff);
169  MPI.HasPadding = false;
170  if (MPT->isMemberFunctionPointer())
171  MPI.Width *= 2;
172  return MPI;
173  }
174 
175  CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
176  const llvm::Triple &T = Context.getTargetInfo().getTriple();
177  if (!isVariadic && T.isWindowsGNUEnvironment() &&
178  T.getArch() == llvm::Triple::x86)
179  return CC_X86ThisCall;
180  return CC_C;
181  }
182 
183  // We cheat and just check that the class has a vtable pointer, and that it's
184  // only big enough to have a vtable pointer and nothing more (or less).
185  bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
186 
187  // Check that the class has a vtable pointer.
188  if (!RD->isDynamicClass())
189  return false;
190 
191  const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
192  CharUnits PointerSize =
193  Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
194  return Layout.getNonVirtualSize() == PointerSize;
195  }
196 
197  const CXXConstructorDecl *
198  getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
199  return nullptr;
200  }
201 
202  void addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
203  CXXConstructorDecl *CD) override {}
204 
205  void addTypedefNameForUnnamedTagDecl(TagDecl *TD,
206  TypedefNameDecl *DD) override {}
207 
208  TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override {
209  return nullptr;
210  }
211 
212  void addDeclaratorForUnnamedTagDecl(TagDecl *TD,
213  DeclaratorDecl *DD) override {}
214 
215  DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override {
216  return nullptr;
217  }
218 
219  std::unique_ptr<MangleNumberingContext>
220  createMangleNumberingContext() const override {
221  return llvm::make_unique<ItaniumNumberingContext>();
222  }
223 };
224 }
225 
227  return new ItaniumCXXABI(Ctx);
228 }
Defines the clang::ASTContext interface.
unsigned getTypeWidth(IntType T) const
Return the width (in bits) of the specified integer type enum.
Definition: TargetInfo.cpp:200
A (possibly-)qualified type.
Definition: Type.h:654
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:837
C Language Family Type Representation.
bool isVariadic() const
Definition: Type.h:3710
The base class of the type hierarchy.
Definition: Type.h:1420
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:672
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2463
Represents a variable declaration or definition.
Definition: Decl.h:812
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6455
Extra information about a function prototype.
Definition: Type.h:3487
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:319
One of these records is kept for each identifier that is lexed.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:150
ArrayRef< QualType > getParamTypes() const
Definition: Type.h:3611
Represents a member of a struct/union/class.
Definition: Decl.h:2521
static DecompositionDeclName getTombstoneKey()
clang::CharUnits operator*(clang::CharUnits::QuantityType Scale, const clang::CharUnits &CU)
Definition: CharUnits.h:208
static bool isEqual(DecompositionDeclName LHS, DecompositionDeclName RHS)
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
Keeps track of the mangled names of lambda expressions and block literals within a particular context...
CXXABI * CreateItaniumCXXABI(ASTContext &Ctx)
Creates an instance of a C++ ABI class.
static unsigned getHashValue(DecompositionDeclName Key)
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3368
bool isDynamicClass() const
Definition: DeclCXX.h:789
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:688
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:39
Exposes information about the current target.
Definition: TargetInfo.h:54
Pepresents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:3838
const FieldDecl * findFirstNamedDataMember() const
Finds the first data member which has a name.
Definition: Decl.cpp:4119
const FunctionProtoType * T
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:6518
static DecompositionDeclName getEmptyKey()
Implements C++ ABI-specific semantic analysis functions.
Definition: CXXABI.h:30
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:236
unsigned getTypeAlign(IntType T) const
Return the alignment (in bits) of the specified integer type enum.
Definition: TargetInfo.cpp:271
RecordDecl * getDecl() const
Definition: Type.h:4081
IntType getPtrDiffType(unsigned AddrSpace) const
Definition: TargetInfo.h:256
QualType getCanonicalType() const
Definition: Type.h:5863
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3006
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:376
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2031
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:2858
IntType
===-— Target Data Type Query Methods ----------------------------—===//
Definition: TargetInfo.h:136
Dataflow Directional Tag Classes.
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: Type.h:2593
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4071
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
Represents a C++ struct/union/class.
Definition: DeclCXX.h:300
bool isMemberFunctionPointer() const
Returns true if the member type (i.e.
Definition: Type.h:2617
CharUnits getNonVirtualSize() const
getNonVirtualSize - Get the non-virtual size (in chars) of an object, which is the size of the object...
Definition: RecordLayout.h:193
Defines the clang::TargetInfo interface.
bool isUnion() const
Definition: Decl.h:3225
QualType getType() const
Definition: Decl.h:647