1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | #include "CXXABI.h" |
20 | #include "clang/AST/ASTContext.h" |
21 | #include "clang/AST/DeclCXX.h" |
22 | #include "clang/AST/MangleNumberingContext.h" |
23 | #include "clang/AST/RecordLayout.h" |
24 | #include "clang/AST/Type.h" |
25 | #include "clang/Basic/TargetInfo.h" |
26 | #include "llvm/ADT/iterator.h" |
27 | |
28 | using namespace clang; |
29 | |
30 | namespace { |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | static const IdentifierInfo *findAnonymousUnionVarDeclName(const VarDecl& VD) { |
43 | const RecordType *RT = VD.getType()->getAs<RecordType>(); |
44 | (0) . __assert_fail ("RT && \"type of VarDecl is expected to be RecordType.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ItaniumCXXABI.cpp", 44, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(RT && "type of VarDecl is expected to be RecordType."); |
45 | (0) . __assert_fail ("RT->getDecl()->isUnion() && \"RecordType is expected to be a union.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ItaniumCXXABI.cpp", 45, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(RT->getDecl()->isUnion() && "RecordType is expected to be a union."); |
46 | if (const FieldDecl *FD = RT->getDecl()->findFirstNamedDataMember()) { |
47 | return FD->getIdentifier(); |
48 | } |
49 | |
50 | return nullptr; |
51 | } |
52 | |
53 | |
54 | struct DecompositionDeclName { |
55 | using BindingArray = ArrayRef<const BindingDecl*>; |
56 | |
57 | |
58 | BindingArray Bindings; |
59 | |
60 | |
61 | struct Iterator |
62 | : llvm::iterator_adaptor_base<Iterator, BindingArray::const_iterator, |
63 | std::random_access_iterator_tag, |
64 | const IdentifierInfo *> { |
65 | Iterator(BindingArray::const_iterator It) : iterator_adaptor_base(It) {} |
66 | const IdentifierInfo *operator*() const { |
67 | return (*this->I)->getIdentifier(); |
68 | } |
69 | }; |
70 | Iterator begin() const { return Iterator(Bindings.begin()); } |
71 | Iterator end() const { return Iterator(Bindings.end()); } |
72 | }; |
73 | } |
74 | |
75 | namespace llvm { |
76 | template<> |
77 | struct DenseMapInfo<DecompositionDeclName> { |
78 | using ArrayInfo = llvm::DenseMapInfo<ArrayRef<const BindingDecl*>>; |
79 | using IdentInfo = llvm::DenseMapInfo<const IdentifierInfo*>; |
80 | static DecompositionDeclName getEmptyKey() { |
81 | return {ArrayInfo::getEmptyKey()}; |
82 | } |
83 | static DecompositionDeclName getTombstoneKey() { |
84 | return {ArrayInfo::getTombstoneKey()}; |
85 | } |
86 | static unsigned getHashValue(DecompositionDeclName Key) { |
87 | assert(!isEqual(Key, getEmptyKey()) && !isEqual(Key, getTombstoneKey())); |
88 | return llvm::hash_combine_range(Key.begin(), Key.end()); |
89 | } |
90 | static bool isEqual(DecompositionDeclName LHS, DecompositionDeclName RHS) { |
91 | if (ArrayInfo::isEqual(LHS.Bindings, ArrayInfo::getEmptyKey())) |
92 | return ArrayInfo::isEqual(RHS.Bindings, ArrayInfo::getEmptyKey()); |
93 | if (ArrayInfo::isEqual(LHS.Bindings, ArrayInfo::getTombstoneKey())) |
94 | return ArrayInfo::isEqual(RHS.Bindings, ArrayInfo::getTombstoneKey()); |
95 | return LHS.Bindings.size() == RHS.Bindings.size() && |
96 | std::equal(LHS.begin(), LHS.end(), RHS.begin()); |
97 | } |
98 | }; |
99 | } |
100 | |
101 | namespace { |
102 | |
103 | |
104 | |
105 | class ItaniumNumberingContext : public MangleNumberingContext { |
106 | llvm::DenseMap<const Type *, unsigned> ManglingNumbers; |
107 | llvm::DenseMap<const IdentifierInfo *, unsigned> VarManglingNumbers; |
108 | llvm::DenseMap<const IdentifierInfo *, unsigned> TagManglingNumbers; |
109 | llvm::DenseMap<DecompositionDeclName, unsigned> |
110 | DecompsitionDeclManglingNumbers; |
111 | |
112 | public: |
113 | unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override { |
114 | const FunctionProtoType *Proto = |
115 | CallOperator->getType()->getAs<FunctionProtoType>(); |
116 | ASTContext &Context = CallOperator->getASTContext(); |
117 | |
118 | FunctionProtoType::ExtProtoInfo EPI; |
119 | EPI.Variadic = Proto->isVariadic(); |
120 | QualType Key = |
121 | Context.getFunctionType(Context.VoidTy, Proto->getParamTypes(), EPI); |
122 | Key = Context.getCanonicalType(Key); |
123 | return ++ManglingNumbers[Key->castAs<FunctionProtoType>()]; |
124 | } |
125 | |
126 | unsigned getManglingNumber(const BlockDecl *BD) override { |
127 | const Type *Ty = nullptr; |
128 | return ++ManglingNumbers[Ty]; |
129 | } |
130 | |
131 | unsigned getStaticLocalNumber(const VarDecl *VD) override { |
132 | return 0; |
133 | } |
134 | |
135 | |
136 | unsigned getManglingNumber(const VarDecl *VD, unsigned) override { |
137 | if (auto *DD = dyn_cast<DecompositionDecl>(VD)) { |
138 | DecompositionDeclName Name{DD->bindings()}; |
139 | return ++DecompsitionDeclManglingNumbers[Name]; |
140 | } |
141 | |
142 | const IdentifierInfo *Identifier = VD->getIdentifier(); |
143 | if (!Identifier) { |
144 | |
145 | |
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 | |
184 | |
185 | bool isNearlyEmpty(const CXXRecordDecl *RD) const override { |
186 | |
187 | |
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 | |
226 | CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) { |
227 | return new ItaniumCXXABI(Ctx); |
228 | } |
229 | |