clang 10.0.0git

ItaniumCXXABI.cpp
Go to the documentation of this file.
1 //===------- ItaniumCXXABI.cpp - AST support for the Itanium C++ ABI ------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This provides C++ AST support targeting the Itanium C++ ABI, which is
10 // documented at:
11 // http://www.codesourcery.com/public/cxx-abi/abi.html
12 // http://www.codesourcery.com/public/cxx-abi/abi-eh.html
13 //
14 // It also supports the closely-related ARM C++ ABI, documented at:
15 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "CXXABI.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/DeclCXX.h"
22 #include "clang/AST/Mangle.h"
23 #include "clang/AST/MangleNumberingContext.h"
24 #include "clang/AST/RecordLayout.h"
25 #include "clang/AST/Type.h"
26 #include "clang/Basic/TargetInfo.h"
27 #include "llvm/ADT/FoldingSet.h"
28 #include "llvm/ADT/iterator.h"
29
30 using namespace clang;
31
32 namespace {
33
34 /// According to Itanium C++ ABI 5.1.2:
35 /// the name of an anonymous union is considered to be
36 /// the name of the first named data member found by a pre-order,
37 /// depth-first, declaration-order walk of the data members of
38 /// the anonymous union.
39 /// If there is no such data member (i.e., if all of the data members
40 /// in the union are unnamed), then there is no way for a program to
41 /// refer to the anonymous union, and there is therefore no need to mangle its name.
42 ///
43 /// Returns the name of anonymous union VarDecl or nullptr if it is not found.
44 static const IdentifierInfo *findAnonymousUnionVarDeclName(const VarDecl& VD) {
45 const RecordType *RT = VD.getType()->getAs<RecordType>();
46 assert(RT && "type of VarDecl is expected to be RecordType.");
47 assert(RT->getDecl()->isUnion() && "RecordType is expected to be a union.");
48 if (const FieldDecl *FD = RT->getDecl()->findFirstNamedDataMember()) {
49 return FD->getIdentifier();
50 }
51
52 return nullptr;
53 }
54
55 /// The name of a decomposition declaration.
56 struct DecompositionDeclName {
57 using BindingArray = ArrayRef<const BindingDecl*>;
58
59 /// Representative example of a set of bindings with these names.
60 BindingArray Bindings;
61
62 /// Iterators over the sequence of identifiers in the name.
63 struct Iterator
64 : llvm::iterator_adaptor_base<Iterator, BindingArray::const_iterator,
65 std::random_access_iterator_tag,
66 const IdentifierInfo *> {
67 Iterator(BindingArray::const_iterator It) : iterator_adaptor_base(It) {}
68 const IdentifierInfo *operator*() const {
69 return (*this->I)->getIdentifier();
70 }
71 };
72 Iterator begin() const { return Iterator(Bindings.begin()); }
73 Iterator end() const { return Iterator(Bindings.end()); }
74 };
75 }
76
77 namespace llvm {
78 template<typename T> bool isDenseMapKeyEmpty(T V) {
79 return llvm::DenseMapInfo<T>::isEqual(
80 V, llvm::DenseMapInfo<T>::getEmptyKey());
81 }
82 template<typename T> bool isDenseMapKeyTombstone(T V) {
83 return llvm::DenseMapInfo<T>::isEqual(
84 V, llvm::DenseMapInfo<T>::getTombstoneKey());
85 }
86
87 template<typename T>
88 Optional<bool> areDenseMapKeysEqualSpecialValues(T LHS, T RHS) {
89 bool LHSEmpty = isDenseMapKeyEmpty(LHS);
90 bool RHSEmpty = isDenseMapKeyEmpty(RHS);
91 if (LHSEmpty || RHSEmpty)
92 return LHSEmpty && RHSEmpty;
93
94 bool LHSTombstone = isDenseMapKeyTombstone(LHS);
95 bool RHSTombstone = isDenseMapKeyTombstone(RHS);
96 if (LHSTombstone || RHSTombstone)
97 return LHSTombstone && RHSTombstone;
98
99 return None;
100 }
101
102 template<>
103 struct DenseMapInfo<DecompositionDeclName> {
104 using ArrayInfo = llvm::DenseMapInfo<ArrayRef<const BindingDecl*>>;
105 static DecompositionDeclName getEmptyKey() {
106 return {ArrayInfo::getEmptyKey()};
107 }
108 static DecompositionDeclName getTombstoneKey() {
109 return {ArrayInfo::getTombstoneKey()};
110 }
111 static unsigned getHashValue(DecompositionDeclName Key) {
112 assert(!isEqual(Key, getEmptyKey()) && !isEqual(Key, getTombstoneKey()));
113 return llvm::hash_combine_range(Key.begin(), Key.end());
114 }
115 static bool isEqual(DecompositionDeclName LHS, DecompositionDeclName RHS) {
116 if (Optional<bool> Result = areDenseMapKeysEqualSpecialValues(
117 LHS.Bindings, RHS.Bindings))
118 return *Result;
119
120 return LHS.Bindings.size() == RHS.Bindings.size() &&
121 std::equal(LHS.begin(), LHS.end(), RHS.begin());
122 }
123 };
124 }
125
126 namespace {
127
128 /// Keeps track of the mangled names of lambda expressions and block
129 /// literals within a particular context.
130 class ItaniumNumberingContext : public MangleNumberingContext {
131 ItaniumMangleContext *Mangler;
132 llvm::StringMap<unsigned> LambdaManglingNumbers;
133 unsigned BlockManglingNumber = 0;
134 llvm::DenseMap<const IdentifierInfo *, unsigned> VarManglingNumbers;
135 llvm::DenseMap<const IdentifierInfo *, unsigned> TagManglingNumbers;
136 llvm::DenseMap<DecompositionDeclName, unsigned>
137 DecompsitionDeclManglingNumbers;
138
139 public:
140 ItaniumNumberingContext(ItaniumMangleContext *Mangler) : Mangler(Mangler) {}
141
142 unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
143 const CXXRecordDecl *Lambda = CallOperator->getParent();
144 assert(Lambda->isLambda());
145
146 // Computation of the <lambda-sig> is non-trivial and subtle. Rather than
147 // duplicating it here, just mangle the <lambda-sig> directly.
148 llvm::SmallString<128> LambdaSig;
149 llvm::raw_svector_ostream Out(LambdaSig);
150 Mangler->mangleLambdaSig(Lambda, Out);
151
152 return ++LambdaManglingNumbers[LambdaSig];
153 }
154
155 unsigned getManglingNumber(const BlockDecl *BD) override {
156 return ++BlockManglingNumber;
157 }
158
159 unsigned getStaticLocalNumber(const VarDecl *VD) override {
160 return 0;
161 }
162
163 /// Variable decls are numbered by identifier.
164 unsigned getManglingNumber(const VarDecl *VD, unsigned) override {
165 if (auto *DD = dyn_cast<DecompositionDecl>(VD)) {
166 DecompositionDeclName Name{DD->bindings()};
167 return ++DecompsitionDeclManglingNumbers[Name];
168 }
169
170 const IdentifierInfo *Identifier = VD->getIdentifier();
171 if (!Identifier) {
172 // VarDecl without an identifier represents an anonymous union
173 // declaration.
174 Identifier = findAnonymousUnionVarDeclName(*VD);
175 }
176 return ++VarManglingNumbers[Identifier];
177 }
178
179 unsigned getManglingNumber(const TagDecl *TD, unsigned) override {
180 return ++TagManglingNumbers[TD->getIdentifier()];
181 }
182 };
183
184 class ItaniumCXXABI : public CXXABI {
185 private:
186 std::unique_ptr<MangleContext> Mangler;
187 protected:
188 ASTContext &Context;
189 public:
190 ItaniumCXXABI(ASTContext &Ctx)
191 : Mangler(Ctx.createMangleContext()), Context(Ctx) {}
192
193 MemberPointerInfo
194 getMemberPointerInfo(const MemberPointerType *MPT) const override {
195 const TargetInfo &Target = Context.getTargetInfo();
196 TargetInfo::IntType PtrDiff = Target.getPtrDiffType(0);
197 MemberPointerInfo MPI;
198 MPI.Width = Target.getTypeWidth(PtrDiff);
199 MPI.Align = Target.getTypeAlign(PtrDiff);
200 MPI.HasPadding = false;
201 if (MPT->isMemberFunctionPointer())
202 MPI.Width *= 2;
203 return MPI;
204 }
205
206 CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
207 const llvm::Triple &T = Context.getTargetInfo().getTriple();
208 if (!isVariadic && T.isWindowsGNUEnvironment() &&
209 T.getArch() == llvm::Triple::x86)
210 return CC_X86ThisCall;
211 return Context.getTargetInfo().getDefaultCallingConv();
212 }
213
214 // We cheat and just check that the class has a vtable pointer, and that it's
215 // only big enough to have a vtable pointer and nothing more (or less).
216 bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
217
218 // Check that the class has a vtable pointer.
219 if (!RD->isDynamicClass())
220 return false;
221
222 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
223 CharUnits PointerSize =
224 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
225 return Layout.getNonVirtualSize() == PointerSize;
226 }
227
228 const CXXConstructorDecl *
229 getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
230 return nullptr;
231 }
232
233 void addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
234 CXXConstructorDecl *CD) override {}
235
236 void addTypedefNameForUnnamedTagDecl(TagDecl *TD,
237 TypedefNameDecl *DD) override {}
238
239 TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override {
240 return nullptr;
241 }
242
243 void addDeclaratorForUnnamedTagDecl(TagDecl *TD,
244 DeclaratorDecl *DD) override {}
245
246 DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override {
247 return nullptr;
248 }
249
250 std::unique_ptr<MangleNumberingContext>
251 createMangleNumberingContext() const override {
252 return std::make_unique<ItaniumNumberingContext>(
253 cast<ItaniumMangleContext>(Mangler.get()));
254 }
255 };
256 }
257
258 CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) {
259 return new ItaniumCXXABI(Ctx);
260 }
ASTContext.h
Defines the clang::ASTContext interface.
clang::TargetInfo::getTypeWidth
unsigned getTypeWidth(IntType T) const
Return the width (in bits) of the specified integer type enum.
Definition: TargetInfo.cpp:219
Identifier
StringRef Identifier
Definition: Format.cpp:1833
clang::ASTContext::createMangleContext
MangleContext * createMangleContext(const TargetInfo *T=nullptr)
If T is null pointer, assume the target in ASTContext.
Definition: ASTContext.cpp:10335
llvm
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Definition: Dominators.h:30
clang::ASTContext::getASTRecordLayout
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
Definition: RecordLayoutBuilder.cpp:3023
clang::TargetInfo::getTriple
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:994
Type.h
C Language Family Type Representation.
llvm::areDenseMapKeysEqualSpecialValues
Optional< bool > areDenseMapKeysEqualSpecialValues(T LHS, T RHS)
Definition: ItaniumCXXABI.cpp:88
clang::ASTContext::getTargetInfo
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:707
llvm::DenseMapInfo
Definition: TypeOrdering.h:37
clang::CXXConstructorDecl
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2383
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:820
clang::Type::getAs
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:7002
clang::MultiVersionKind::Target
clang::NamedDecl::getIdentifier
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:244
clang::ItaniumMangleContext::mangleLambdaSig
virtual void mangleLambdaSig(const CXXRecordDecl *Lambda, raw_ostream &)=0
clang::TargetInfo::getPointerWidth
uint64_t getPointerWidth(unsigned AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition: TargetInfo.h:361
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:57
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
llvm::Optional< bool >
clang::FieldDecl
Represents a member of a struct/union/class.
Definition: Decl.h:2729
llvm::DenseMapInfo< DecompositionDeclName >::getTombstoneKey
static DecompositionDeclName getTombstoneKey()
Definition: ItaniumCXXABI.cpp:108
operator*
clang::CharUnits operator*(clang::CharUnits::QuantityType Scale, const clang::CharUnits &CU)
Definition: CharUnits.h:212
llvm::DenseMapInfo< DecompositionDeclName >::isEqual
static bool isEqual(DecompositionDeclName LHS, DecompositionDeclName RHS)
Definition: ItaniumCXXABI.cpp:115
clang::CharUnits
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
clang::MangleNumberingContext
Keeps track of the mangled names of lambda expressions and block literals within a particular context...
Definition: MangleNumberingContext.h:31
clang::CXXRecordDecl::isLambda
bool isLambda() const
Determine whether this class describes a lambda function object.
Definition: DeclCXX.h:960
llvm::isDenseMapKeyTombstone
bool isDenseMapKeyTombstone(T V)
Definition: ItaniumCXXABI.cpp:82
clang::CreateItaniumCXXABI
CXXABI * CreateItaniumCXXABI(ASTContext &Ctx)
Creates an instance of a C++ ABI class.
Definition: ItaniumCXXABI.cpp:258
Mangle.h
llvm::ArrayRef
Definition: LLVM.h:31
llvm::SmallString< 128 >
llvm::DenseMapInfo< DecompositionDeclName >::getHashValue
static unsigned getHashValue(DecompositionDeclName Key)
Definition: ItaniumCXXABI.cpp:111
clang::CXXRecordDecl::isDynamicClass
bool isDynamicClass() const
Definition: DeclCXX.h:553
clang::DeclaratorDecl
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:671
clang::CC_X86ThisCall
Definition: Specifiers.h:269
clang::ASTRecordLayout
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:38
clang::TargetInfo
Exposes information about the current target.
Definition: TargetInfo.h:164
clang::BlockDecl
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4037
clang::RecordDecl::findFirstNamedDataMember
const FieldDecl * findFirstNamedDataMember() const
Finds the first data member which has a name.
Definition: Decl.cpp:4517
clang::TargetInfo::getDefaultCallingConv
virtual CallingConv getDefaultCallingConv() const
Gets the default calling convention for the given target and declaration context. ...
Definition: TargetInfo.h:1276
V
#define V(N, I)
Definition: ASTContext.h:2941
llvm::DenseMapInfo< DecompositionDeclName >::getEmptyKey
static DecompositionDeclName getEmptyKey()
Definition: ItaniumCXXABI.cpp:105
clang::CXXABI
Implements C++ ABI-specific semantic analysis functions.
Definition: CXXABI.h:29
clang::ObjCSubstitutionContext::Result
The result type of a method or function.
clang::CallingConv
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:265
clang::TargetInfo::getTypeAlign
unsigned getTypeAlign(IntType T) const
Return the alignment (in bits) of the specified integer type enum.
Definition: TargetInfo.cpp:290
clang::RecordType::getDecl
RecordDecl * getDecl() const
Definition: Type.h:4505
clang::TargetInfo::getPtrDiffType
IntType getPtrDiffType(unsigned AddrSpace) const
Definition: TargetInfo.h:290
clang::TagDecl
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3219
clang::CXXMethodDecl
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1931
clang::MultiVersionKind::None
clang::TransferrableTargetInfo::IntType
IntType
===-— Target Data Type Query Methods ----------------------------—===//
Definition: TargetInfo.h:107
clang::TypedefNameDecl
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:3071
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::CXXMethodDecl::getParent
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined...
Definition: DeclCXX.h:2046
clang::MemberPointerType
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: Type.h:2833
clang::RecordType
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4495
clang::ASTContext::toCharUnitsFromBits
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
Definition: ASTContext.cpp:2372
DeclCXX.h
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
clang::CXXRecordDecl
Represents a C++ struct/union/class.
Definition: DeclCXX.h:253
clang::MemberPointerType::isMemberFunctionPointer
bool isMemberFunctionPointer() const
Returns true if the member type (i.e.
Definition: Type.h:2857
RecordLayout.h
clang::ASTRecordLayout::getNonVirtualSize
CharUnits getNonVirtualSize() const
getNonVirtualSize - Get the non-virtual size (in chars) of an object, which is the size of the object...
Definition: RecordLayout.h:202
TargetInfo.h
Defines the clang::TargetInfo interface.
llvm::isDenseMapKeyEmpty
bool isDenseMapKeyEmpty(T V)
Definition: ItaniumCXXABI.cpp:78
clang::TagDecl::isUnion
bool isUnion() const
Definition: Decl.h:3407
clang::ItaniumMangleContext
Definition: Mangle.h:153
clang::ValueDecl::getType
QualType getType() const
Definition: Decl.h:630
MangleNumberingContext.h
CXXABI.h