1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "clang/AST/VTableBuilder.h" |
14 | #include "clang/AST/ASTContext.h" |
15 | #include "clang/AST/ASTDiagnostic.h" |
16 | #include "clang/AST/CXXInheritance.h" |
17 | #include "clang/AST/RecordLayout.h" |
18 | #include "clang/Basic/TargetInfo.h" |
19 | #include "llvm/ADT/SetOperations.h" |
20 | #include "llvm/ADT/SmallPtrSet.h" |
21 | #include "llvm/Support/Format.h" |
22 | #include "llvm/Support/raw_ostream.h" |
23 | #include <algorithm> |
24 | #include <cstdio> |
25 | |
26 | using namespace clang; |
27 | |
28 | #define DUMP_OVERRIDERS 0 |
29 | |
30 | namespace { |
31 | |
32 | |
33 | |
34 | struct BaseOffset { |
35 | |
36 | const CXXRecordDecl *DerivedClass; |
37 | |
38 | |
39 | |
40 | |
41 | const CXXRecordDecl *VirtualBase; |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | CharUnits NonVirtualOffset; |
48 | |
49 | BaseOffset() : DerivedClass(nullptr), VirtualBase(nullptr), |
50 | NonVirtualOffset(CharUnits::Zero()) { } |
51 | BaseOffset(const CXXRecordDecl *DerivedClass, |
52 | const CXXRecordDecl *VirtualBase, CharUnits NonVirtualOffset) |
53 | : DerivedClass(DerivedClass), VirtualBase(VirtualBase), |
54 | NonVirtualOffset(NonVirtualOffset) { } |
55 | |
56 | bool isEmpty() const { return NonVirtualOffset.isZero() && !VirtualBase; } |
57 | }; |
58 | |
59 | |
60 | |
61 | class FinalOverriders { |
62 | public: |
63 | |
64 | struct OverriderInfo { |
65 | |
66 | const CXXMethodDecl *Method; |
67 | |
68 | |
69 | |
70 | const CXXRecordDecl *VirtualBase; |
71 | |
72 | |
73 | CharUnits Offset; |
74 | |
75 | OverriderInfo() : Method(nullptr), VirtualBase(nullptr), |
76 | Offset(CharUnits::Zero()) { } |
77 | }; |
78 | |
79 | private: |
80 | |
81 | |
82 | const CXXRecordDecl *MostDerivedClass; |
83 | |
84 | |
85 | |
86 | |
87 | const CharUnits MostDerivedClassOffset; |
88 | |
89 | |
90 | |
91 | |
92 | const CXXRecordDecl *LayoutClass; |
93 | |
94 | ASTContext &Context; |
95 | |
96 | |
97 | const ASTRecordLayout &MostDerivedClassLayout; |
98 | |
99 | |
100 | |
101 | typedef std::pair<const CXXMethodDecl *, CharUnits> MethodBaseOffsetPairTy; |
102 | |
103 | typedef llvm::DenseMap<MethodBaseOffsetPairTy, |
104 | OverriderInfo> OverridersMapTy; |
105 | |
106 | |
107 | |
108 | OverridersMapTy OverridersMap; |
109 | |
110 | |
111 | |
112 | |
113 | typedef llvm::DenseMap<std::pair<const CXXRecordDecl *, unsigned>, |
114 | CharUnits> SubobjectOffsetMapTy; |
115 | |
116 | typedef llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCountMapTy; |
117 | |
118 | |
119 | |
120 | void ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual, |
121 | CharUnits OffsetInLayoutClass, |
122 | SubobjectOffsetMapTy &SubobjectOffsets, |
123 | SubobjectOffsetMapTy &SubobjectLayoutClassOffsets, |
124 | SubobjectCountMapTy &SubobjectCounts); |
125 | |
126 | typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy; |
127 | |
128 | |
129 | |
130 | void dump(raw_ostream &Out, BaseSubobject Base, |
131 | VisitedVirtualBasesSetTy& VisitedVirtualBases); |
132 | |
133 | public: |
134 | FinalOverriders(const CXXRecordDecl *MostDerivedClass, |
135 | CharUnits MostDerivedClassOffset, |
136 | const CXXRecordDecl *LayoutClass); |
137 | |
138 | |
139 | |
140 | OverriderInfo getOverrider(const CXXMethodDecl *MD, |
141 | CharUnits BaseOffset) const { |
142 | (0) . __assert_fail ("OverridersMap.count(std..make_pair(MD, BaseOffset)) && \"Did not find overrider!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 143, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(OverridersMap.count(std::make_pair(MD, BaseOffset)) && |
143 | (0) . __assert_fail ("OverridersMap.count(std..make_pair(MD, BaseOffset)) && \"Did not find overrider!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 143, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Did not find overrider!"); |
144 | |
145 | return OverridersMap.lookup(std::make_pair(MD, BaseOffset)); |
146 | } |
147 | |
148 | |
149 | void dump() { |
150 | VisitedVirtualBasesSetTy VisitedVirtualBases; |
151 | dump(llvm::errs(), BaseSubobject(MostDerivedClass, CharUnits::Zero()), |
152 | VisitedVirtualBases); |
153 | } |
154 | |
155 | }; |
156 | |
157 | FinalOverriders::FinalOverriders(const CXXRecordDecl *MostDerivedClass, |
158 | CharUnits MostDerivedClassOffset, |
159 | const CXXRecordDecl *LayoutClass) |
160 | : MostDerivedClass(MostDerivedClass), |
161 | MostDerivedClassOffset(MostDerivedClassOffset), LayoutClass(LayoutClass), |
162 | Context(MostDerivedClass->getASTContext()), |
163 | MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)) { |
164 | |
165 | |
166 | SubobjectOffsetMapTy SubobjectOffsets; |
167 | SubobjectOffsetMapTy SubobjectLayoutClassOffsets; |
168 | SubobjectCountMapTy SubobjectCounts; |
169 | ComputeBaseOffsets(BaseSubobject(MostDerivedClass, CharUnits::Zero()), |
170 | , |
171 | MostDerivedClassOffset, |
172 | SubobjectOffsets, SubobjectLayoutClassOffsets, |
173 | SubobjectCounts); |
174 | |
175 | |
176 | CXXFinalOverriderMap FinalOverriders; |
177 | MostDerivedClass->getFinalOverriders(FinalOverriders); |
178 | |
179 | for (const auto &Overrider : FinalOverriders) { |
180 | const CXXMethodDecl *MD = Overrider.first; |
181 | const OverridingMethods &Methods = Overrider.second; |
182 | |
183 | for (const auto &M : Methods) { |
184 | unsigned SubobjectNumber = M.first; |
185 | (0) . __assert_fail ("SubobjectOffsets.count(std..make_pair(MD->getParent(), SubobjectNumber)) && \"Did not find subobject offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 187, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SubobjectOffsets.count(std::make_pair(MD->getParent(), |
186 | (0) . __assert_fail ("SubobjectOffsets.count(std..make_pair(MD->getParent(), SubobjectNumber)) && \"Did not find subobject offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 187, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> SubobjectNumber)) && |
187 | (0) . __assert_fail ("SubobjectOffsets.count(std..make_pair(MD->getParent(), SubobjectNumber)) && \"Did not find subobject offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 187, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Did not find subobject offset!"); |
188 | |
189 | CharUnits BaseOffset = SubobjectOffsets[std::make_pair(MD->getParent(), |
190 | SubobjectNumber)]; |
191 | |
192 | (0) . __assert_fail ("M.second.size() == 1 && \"Final overrider is not unique!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 192, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(M.second.size() == 1 && "Final overrider is not unique!"); |
193 | const UniqueVirtualMethod &Method = M.second.front(); |
194 | |
195 | const CXXRecordDecl *OverriderRD = Method.Method->getParent(); |
196 | (0) . __assert_fail ("SubobjectLayoutClassOffsets.count( std..make_pair(OverriderRD, Method.Subobject)) && \"Did not find subobject offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 198, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SubobjectLayoutClassOffsets.count( |
197 | (0) . __assert_fail ("SubobjectLayoutClassOffsets.count( std..make_pair(OverriderRD, Method.Subobject)) && \"Did not find subobject offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 198, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> std::make_pair(OverriderRD, Method.Subobject)) |
198 | (0) . __assert_fail ("SubobjectLayoutClassOffsets.count( std..make_pair(OverriderRD, Method.Subobject)) && \"Did not find subobject offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 198, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> && "Did not find subobject offset!"); |
199 | CharUnits OverriderOffset = |
200 | SubobjectLayoutClassOffsets[std::make_pair(OverriderRD, |
201 | Method.Subobject)]; |
202 | |
203 | OverriderInfo& Overrider = OverridersMap[std::make_pair(MD, BaseOffset)]; |
204 | (0) . __assert_fail ("!Overrider.Method && \"Overrider should not exist yet!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 204, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Overrider.Method && "Overrider should not exist yet!"); |
205 | |
206 | Overrider.Offset = OverriderOffset; |
207 | Overrider.Method = Method.Method; |
208 | Overrider.VirtualBase = Method.InVirtualSubobject; |
209 | } |
210 | } |
211 | |
212 | #if DUMP_OVERRIDERS |
213 | |
214 | dump(); |
215 | #endif |
216 | } |
217 | |
218 | static BaseOffset ComputeBaseOffset(const ASTContext &Context, |
219 | const CXXRecordDecl *DerivedRD, |
220 | const CXXBasePath &Path) { |
221 | CharUnits NonVirtualOffset = CharUnits::Zero(); |
222 | |
223 | unsigned NonVirtualStart = 0; |
224 | const CXXRecordDecl *VirtualBase = nullptr; |
225 | |
226 | |
227 | for (int I = Path.size(), E = 0; I != E; --I) { |
228 | const CXXBasePathElement &Element = Path[I - 1]; |
229 | |
230 | if (Element.Base->isVirtual()) { |
231 | NonVirtualStart = I; |
232 | QualType VBaseType = Element.Base->getType(); |
233 | VirtualBase = VBaseType->getAsCXXRecordDecl(); |
234 | break; |
235 | } |
236 | } |
237 | |
238 | |
239 | for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) { |
240 | const CXXBasePathElement &Element = Path[I]; |
241 | |
242 | |
243 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class); |
244 | |
245 | const CXXRecordDecl *Base = Element.Base->getType()->getAsCXXRecordDecl(); |
246 | |
247 | NonVirtualOffset += Layout.getBaseClassOffset(Base); |
248 | } |
249 | |
250 | |
251 | |
252 | |
253 | return BaseOffset(DerivedRD, VirtualBase, NonVirtualOffset); |
254 | |
255 | } |
256 | |
257 | static BaseOffset ComputeBaseOffset(const ASTContext &Context, |
258 | const CXXRecordDecl *BaseRD, |
259 | const CXXRecordDecl *DerivedRD) { |
260 | CXXBasePaths Paths(, |
261 | , ); |
262 | |
263 | if (!DerivedRD->isDerivedFrom(BaseRD, Paths)) |
264 | llvm_unreachable("Class must be derived from the passed in base class!"); |
265 | |
266 | return ComputeBaseOffset(Context, DerivedRD, Paths.front()); |
267 | } |
268 | |
269 | static BaseOffset |
270 | ComputeReturnAdjustmentBaseOffset(ASTContext &Context, |
271 | const CXXMethodDecl *DerivedMD, |
272 | const CXXMethodDecl *BaseMD) { |
273 | const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>(); |
274 | const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>(); |
275 | |
276 | |
277 | CanQualType CanDerivedReturnType = |
278 | Context.getCanonicalType(DerivedFT->getReturnType()); |
279 | CanQualType CanBaseReturnType = |
280 | Context.getCanonicalType(BaseFT->getReturnType()); |
281 | |
282 | (0) . __assert_fail ("CanDerivedReturnType->getTypeClass() == CanBaseReturnType->getTypeClass() && \"Types must have same type class!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 284, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CanDerivedReturnType->getTypeClass() == |
283 | (0) . __assert_fail ("CanDerivedReturnType->getTypeClass() == CanBaseReturnType->getTypeClass() && \"Types must have same type class!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 284, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> CanBaseReturnType->getTypeClass() && |
284 | (0) . __assert_fail ("CanDerivedReturnType->getTypeClass() == CanBaseReturnType->getTypeClass() && \"Types must have same type class!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 284, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Types must have same type class!"); |
285 | |
286 | if (CanDerivedReturnType == CanBaseReturnType) { |
287 | |
288 | return BaseOffset(); |
289 | } |
290 | |
291 | if (isa<ReferenceType>(CanDerivedReturnType)) { |
292 | CanDerivedReturnType = |
293 | CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType(); |
294 | CanBaseReturnType = |
295 | CanBaseReturnType->getAs<ReferenceType>()->getPointeeType(); |
296 | } else if (isa<PointerType>(CanDerivedReturnType)) { |
297 | CanDerivedReturnType = |
298 | CanDerivedReturnType->getAs<PointerType>()->getPointeeType(); |
299 | CanBaseReturnType = |
300 | CanBaseReturnType->getAs<PointerType>()->getPointeeType(); |
301 | } else { |
302 | llvm_unreachable("Unexpected return type!"); |
303 | } |
304 | |
305 | |
306 | |
307 | |
308 | if (CanDerivedReturnType.getUnqualifiedType() == |
309 | CanBaseReturnType.getUnqualifiedType()) { |
310 | |
311 | return BaseOffset(); |
312 | } |
313 | |
314 | const CXXRecordDecl *DerivedRD = |
315 | cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl()); |
316 | |
317 | const CXXRecordDecl *BaseRD = |
318 | cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl()); |
319 | |
320 | return ComputeBaseOffset(Context, BaseRD, DerivedRD); |
321 | } |
322 | |
323 | void |
324 | FinalOverriders::ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual, |
325 | CharUnits OffsetInLayoutClass, |
326 | SubobjectOffsetMapTy &SubobjectOffsets, |
327 | SubobjectOffsetMapTy &SubobjectLayoutClassOffsets, |
328 | SubobjectCountMapTy &SubobjectCounts) { |
329 | const CXXRecordDecl *RD = Base.getBase(); |
330 | |
331 | unsigned SubobjectNumber = 0; |
332 | if (!IsVirtual) |
333 | SubobjectNumber = ++SubobjectCounts[RD]; |
334 | |
335 | |
336 | (0) . __assert_fail ("!SubobjectOffsets.count(std..make_pair(RD, SubobjectNumber)) && \"Subobject offset already exists!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 337, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!SubobjectOffsets.count(std::make_pair(RD, SubobjectNumber)) |
337 | (0) . __assert_fail ("!SubobjectOffsets.count(std..make_pair(RD, SubobjectNumber)) && \"Subobject offset already exists!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 337, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> && "Subobject offset already exists!"); |
338 | (0) . __assert_fail ("!SubobjectLayoutClassOffsets.count(std..make_pair(RD, SubobjectNumber)) && \"Subobject offset already exists!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 339, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!SubobjectLayoutClassOffsets.count(std::make_pair(RD, SubobjectNumber)) |
339 | (0) . __assert_fail ("!SubobjectLayoutClassOffsets.count(std..make_pair(RD, SubobjectNumber)) && \"Subobject offset already exists!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 339, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> && "Subobject offset already exists!"); |
340 | |
341 | SubobjectOffsets[std::make_pair(RD, SubobjectNumber)] = Base.getBaseOffset(); |
342 | SubobjectLayoutClassOffsets[std::make_pair(RD, SubobjectNumber)] = |
343 | OffsetInLayoutClass; |
344 | |
345 | |
346 | for (const auto &B : RD->bases()) { |
347 | const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl(); |
348 | |
349 | CharUnits BaseOffset; |
350 | CharUnits BaseOffsetInLayoutClass; |
351 | if (B.isVirtual()) { |
352 | |
353 | if (SubobjectOffsets.count(std::make_pair(BaseDecl, 0))) |
354 | continue; |
355 | |
356 | const ASTRecordLayout &LayoutClassLayout = |
357 | Context.getASTRecordLayout(LayoutClass); |
358 | |
359 | BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl); |
360 | BaseOffsetInLayoutClass = |
361 | LayoutClassLayout.getVBaseClassOffset(BaseDecl); |
362 | } else { |
363 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
364 | CharUnits Offset = Layout.getBaseClassOffset(BaseDecl); |
365 | |
366 | BaseOffset = Base.getBaseOffset() + Offset; |
367 | BaseOffsetInLayoutClass = OffsetInLayoutClass + Offset; |
368 | } |
369 | |
370 | ComputeBaseOffsets(BaseSubobject(BaseDecl, BaseOffset), |
371 | B.isVirtual(), BaseOffsetInLayoutClass, |
372 | SubobjectOffsets, SubobjectLayoutClassOffsets, |
373 | SubobjectCounts); |
374 | } |
375 | } |
376 | |
377 | void FinalOverriders::dump(raw_ostream &Out, BaseSubobject Base, |
378 | VisitedVirtualBasesSetTy &VisitedVirtualBases) { |
379 | const CXXRecordDecl *RD = Base.getBase(); |
380 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
381 | |
382 | for (const auto &B : RD->bases()) { |
383 | const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl(); |
384 | |
385 | |
386 | if (!BaseDecl->isPolymorphic()) |
387 | continue; |
388 | |
389 | CharUnits BaseOffset; |
390 | if (B.isVirtual()) { |
391 | if (!VisitedVirtualBases.insert(BaseDecl).second) { |
392 | |
393 | continue; |
394 | } |
395 | |
396 | BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl); |
397 | } else { |
398 | BaseOffset = Layout.getBaseClassOffset(BaseDecl) + Base.getBaseOffset(); |
399 | } |
400 | |
401 | dump(Out, BaseSubobject(BaseDecl, BaseOffset), VisitedVirtualBases); |
402 | } |
403 | |
404 | Out << "Final overriders for ("; |
405 | RD->printQualifiedName(Out); |
406 | Out << ", "; |
407 | Out << Base.getBaseOffset().getQuantity() << ")\n"; |
408 | |
409 | |
410 | for (const auto *MD : RD->methods()) { |
411 | if (!MD->isVirtual()) |
412 | continue; |
413 | MD = MD->getCanonicalDecl(); |
414 | |
415 | OverriderInfo Overrider = getOverrider(MD, Base.getBaseOffset()); |
416 | |
417 | Out << " "; |
418 | MD->printQualifiedName(Out); |
419 | Out << " - ("; |
420 | Overrider.Method->printQualifiedName(Out); |
421 | Out << ", " << Overrider.Offset.getQuantity() << ')'; |
422 | |
423 | BaseOffset Offset; |
424 | if (!Overrider.Method->isPure()) |
425 | Offset = ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD); |
426 | |
427 | if (!Offset.isEmpty()) { |
428 | Out << " [ret-adj: "; |
429 | if (Offset.VirtualBase) { |
430 | Offset.VirtualBase->printQualifiedName(Out); |
431 | Out << " vbase, "; |
432 | } |
433 | |
434 | Out << Offset.NonVirtualOffset.getQuantity() << " nv]"; |
435 | } |
436 | |
437 | Out << "\n"; |
438 | } |
439 | } |
440 | |
441 | |
442 | struct VCallOffsetMap { |
443 | |
444 | typedef std::pair<const CXXMethodDecl *, CharUnits> MethodAndOffsetPairTy; |
445 | |
446 | |
447 | |
448 | SmallVector<MethodAndOffsetPairTy, 16> Offsets; |
449 | |
450 | |
451 | |
452 | static bool MethodsCanShareVCallOffset(const CXXMethodDecl *LHS, |
453 | const CXXMethodDecl *RHS); |
454 | |
455 | public: |
456 | |
457 | |
458 | |
459 | bool AddVCallOffset(const CXXMethodDecl *MD, CharUnits OffsetOffset); |
460 | |
461 | |
462 | |
463 | CharUnits getVCallOffsetOffset(const CXXMethodDecl *MD); |
464 | |
465 | |
466 | bool empty() const { return Offsets.empty(); } |
467 | }; |
468 | |
469 | static bool HasSameVirtualSignature(const CXXMethodDecl *LHS, |
470 | const CXXMethodDecl *RHS) { |
471 | const FunctionProtoType *LT = |
472 | cast<FunctionProtoType>(LHS->getType().getCanonicalType()); |
473 | const FunctionProtoType *RT = |
474 | cast<FunctionProtoType>(RHS->getType().getCanonicalType()); |
475 | |
476 | |
477 | if (LT == RT) return true; |
478 | |
479 | |
480 | |
481 | |
482 | if (LT->getMethodQuals() != RT->getMethodQuals()) |
483 | return false; |
484 | return LT->getParamTypes() == RT->getParamTypes(); |
485 | } |
486 | |
487 | bool VCallOffsetMap::MethodsCanShareVCallOffset(const CXXMethodDecl *LHS, |
488 | const CXXMethodDecl *RHS) { |
489 | (0) . __assert_fail ("LHS->isVirtual() && \"LHS must be virtual!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 489, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(LHS->isVirtual() && "LHS must be virtual!"); |
490 | (0) . __assert_fail ("RHS->isVirtual() && \"LHS must be virtual!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 490, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(RHS->isVirtual() && "LHS must be virtual!"); |
491 | |
492 | |
493 | if (isa<CXXDestructorDecl>(LHS)) |
494 | return isa<CXXDestructorDecl>(RHS); |
495 | |
496 | |
497 | |
498 | |
499 | DeclarationName LHSName = LHS->getDeclName(); |
500 | DeclarationName RHSName = RHS->getDeclName(); |
501 | if (LHSName != RHSName) |
502 | return false; |
503 | |
504 | |
505 | return HasSameVirtualSignature(LHS, RHS); |
506 | } |
507 | |
508 | bool VCallOffsetMap::AddVCallOffset(const CXXMethodDecl *MD, |
509 | CharUnits OffsetOffset) { |
510 | |
511 | for (const auto &OffsetPair : Offsets) { |
512 | if (MethodsCanShareVCallOffset(OffsetPair.first, MD)) |
513 | return false; |
514 | } |
515 | |
516 | |
517 | Offsets.push_back(MethodAndOffsetPairTy(MD, OffsetOffset)); |
518 | return true; |
519 | } |
520 | |
521 | CharUnits VCallOffsetMap::getVCallOffsetOffset(const CXXMethodDecl *MD) { |
522 | |
523 | for (const auto &OffsetPair : Offsets) { |
524 | if (MethodsCanShareVCallOffset(OffsetPair.first, MD)) |
525 | return OffsetPair.second; |
526 | } |
527 | |
528 | llvm_unreachable("Should always find a vcall offset offset!"); |
529 | } |
530 | |
531 | |
532 | class VCallAndVBaseOffsetBuilder { |
533 | public: |
534 | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> |
535 | VBaseOffsetOffsetsMapTy; |
536 | |
537 | private: |
538 | |
539 | |
540 | const CXXRecordDecl *MostDerivedClass; |
541 | |
542 | |
543 | |
544 | |
545 | const CXXRecordDecl *LayoutClass; |
546 | |
547 | |
548 | ASTContext &Context; |
549 | |
550 | |
551 | typedef SmallVector<VTableComponent, 64> VTableComponentVectorTy; |
552 | VTableComponentVectorTy Components; |
553 | |
554 | |
555 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases; |
556 | |
557 | |
558 | VCallOffsetMap VCallOffsets; |
559 | |
560 | |
561 | |
562 | |
563 | VBaseOffsetOffsetsMapTy VBaseOffsetOffsets; |
564 | |
565 | |
566 | |
567 | const FinalOverriders *Overriders; |
568 | |
569 | |
570 | |
571 | void AddVCallAndVBaseOffsets(BaseSubobject Base, bool BaseIsVirtual, |
572 | CharUnits RealBaseOffset); |
573 | |
574 | |
575 | void AddVCallOffsets(BaseSubobject Base, CharUnits VBaseOffset); |
576 | |
577 | |
578 | void AddVBaseOffsets(const CXXRecordDecl *Base, |
579 | CharUnits OffsetInLayoutClass); |
580 | |
581 | |
582 | |
583 | CharUnits getCurrentOffsetOffset() const; |
584 | |
585 | public: |
586 | VCallAndVBaseOffsetBuilder(const CXXRecordDecl *MostDerivedClass, |
587 | const CXXRecordDecl *LayoutClass, |
588 | const FinalOverriders *Overriders, |
589 | BaseSubobject Base, bool BaseIsVirtual, |
590 | CharUnits OffsetInLayoutClass) |
591 | : MostDerivedClass(MostDerivedClass), LayoutClass(LayoutClass), |
592 | Context(MostDerivedClass->getASTContext()), Overriders(Overriders) { |
593 | |
594 | |
595 | AddVCallAndVBaseOffsets(Base, BaseIsVirtual, OffsetInLayoutClass); |
596 | } |
597 | |
598 | |
599 | typedef VTableComponentVectorTy::const_reverse_iterator const_iterator; |
600 | const_iterator components_begin() const { return Components.rbegin(); } |
601 | const_iterator components_end() const { return Components.rend(); } |
602 | |
603 | const VCallOffsetMap &getVCallOffsets() const { return VCallOffsets; } |
604 | const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const { |
605 | return VBaseOffsetOffsets; |
606 | } |
607 | }; |
608 | |
609 | void |
610 | VCallAndVBaseOffsetBuilder::AddVCallAndVBaseOffsets(BaseSubobject Base, |
611 | bool BaseIsVirtual, |
612 | CharUnits RealBaseOffset) { |
613 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base.getBase()); |
614 | |
615 | |
616 | |
617 | |
618 | |
619 | |
620 | |
621 | |
622 | |
623 | |
624 | if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { |
625 | bool PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual(); |
626 | |
627 | CharUnits PrimaryBaseOffset; |
628 | |
629 | |
630 | if (PrimaryBaseIsVirtual) { |
631 | (0) . __assert_fail ("Layout.getVBaseClassOffset(PrimaryBase).isZero() && \"Primary vbase should have a zero offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 632, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() && |
632 | (0) . __assert_fail ("Layout.getVBaseClassOffset(PrimaryBase).isZero() && \"Primary vbase should have a zero offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 632, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Primary vbase should have a zero offset!"); |
633 | |
634 | const ASTRecordLayout &MostDerivedClassLayout = |
635 | Context.getASTRecordLayout(MostDerivedClass); |
636 | |
637 | PrimaryBaseOffset = |
638 | MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase); |
639 | } else { |
640 | (0) . __assert_fail ("Layout.getBaseClassOffset(PrimaryBase).isZero() && \"Primary base should have a zero offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 641, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Layout.getBaseClassOffset(PrimaryBase).isZero() && |
641 | (0) . __assert_fail ("Layout.getBaseClassOffset(PrimaryBase).isZero() && \"Primary base should have a zero offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 641, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Primary base should have a zero offset!"); |
642 | |
643 | PrimaryBaseOffset = Base.getBaseOffset(); |
644 | } |
645 | |
646 | AddVCallAndVBaseOffsets( |
647 | BaseSubobject(PrimaryBase,PrimaryBaseOffset), |
648 | PrimaryBaseIsVirtual, RealBaseOffset); |
649 | } |
650 | |
651 | AddVBaseOffsets(Base.getBase(), RealBaseOffset); |
652 | |
653 | |
654 | if (BaseIsVirtual) |
655 | AddVCallOffsets(Base, RealBaseOffset); |
656 | } |
657 | |
658 | CharUnits VCallAndVBaseOffsetBuilder::getCurrentOffsetOffset() const { |
659 | |
660 | |
661 | |
662 | |
663 | int64_t OffsetIndex = -(int64_t)(3 + Components.size()); |
664 | |
665 | CharUnits PointerWidth = |
666 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
667 | CharUnits OffsetOffset = PointerWidth * OffsetIndex; |
668 | return OffsetOffset; |
669 | } |
670 | |
671 | void VCallAndVBaseOffsetBuilder::AddVCallOffsets(BaseSubobject Base, |
672 | CharUnits VBaseOffset) { |
673 | const CXXRecordDecl *RD = Base.getBase(); |
674 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
675 | |
676 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
677 | |
678 | |
679 | |
680 | |
681 | if (PrimaryBase && !Layout.isPrimaryBaseVirtual()) { |
682 | |
683 | (0) . __assert_fail ("Layout.getBaseClassOffset(PrimaryBase).isZero() && \"Primary base should have a zero offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 684, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Layout.getBaseClassOffset(PrimaryBase).isZero() && |
684 | (0) . __assert_fail ("Layout.getBaseClassOffset(PrimaryBase).isZero() && \"Primary base should have a zero offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 684, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Primary base should have a zero offset!"); |
685 | |
686 | AddVCallOffsets(BaseSubobject(PrimaryBase, Base.getBaseOffset()), |
687 | VBaseOffset); |
688 | } |
689 | |
690 | |
691 | for (const auto *MD : RD->methods()) { |
692 | if (!MD->isVirtual()) |
693 | continue; |
694 | MD = MD->getCanonicalDecl(); |
695 | |
696 | CharUnits OffsetOffset = getCurrentOffsetOffset(); |
697 | |
698 | |
699 | |
700 | if (!VCallOffsets.AddVCallOffset(MD, OffsetOffset)) |
701 | continue; |
702 | |
703 | CharUnits Offset = CharUnits::Zero(); |
704 | |
705 | if (Overriders) { |
706 | |
707 | FinalOverriders::OverriderInfo Overrider = |
708 | Overriders->getOverrider(MD, Base.getBaseOffset()); |
709 | |
710 | |
711 | |
712 | Offset = Overrider.Offset - VBaseOffset; |
713 | } |
714 | |
715 | Components.push_back( |
716 | VTableComponent::MakeVCallOffset(Offset)); |
717 | } |
718 | |
719 | |
720 | for (const auto &B : RD->bases()) { |
721 | if (B.isVirtual()) |
722 | continue; |
723 | |
724 | const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl(); |
725 | if (BaseDecl == PrimaryBase) |
726 | continue; |
727 | |
728 | |
729 | CharUnits BaseOffset = Base.getBaseOffset() + |
730 | Layout.getBaseClassOffset(BaseDecl); |
731 | |
732 | AddVCallOffsets(BaseSubobject(BaseDecl, BaseOffset), |
733 | VBaseOffset); |
734 | } |
735 | } |
736 | |
737 | void |
738 | VCallAndVBaseOffsetBuilder::AddVBaseOffsets(const CXXRecordDecl *RD, |
739 | CharUnits OffsetInLayoutClass) { |
740 | const ASTRecordLayout &LayoutClassLayout = |
741 | Context.getASTRecordLayout(LayoutClass); |
742 | |
743 | |
744 | for (const auto &B : RD->bases()) { |
745 | const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl(); |
746 | |
747 | |
748 | if (B.isVirtual() && VisitedVirtualBases.insert(BaseDecl).second) { |
749 | CharUnits Offset = |
750 | LayoutClassLayout.getVBaseClassOffset(BaseDecl) - OffsetInLayoutClass; |
751 | |
752 | |
753 | (0) . __assert_fail ("!VBaseOffsetOffsets.count(BaseDecl) && \"vbase offset offset already exists!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 754, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!VBaseOffsetOffsets.count(BaseDecl) && |
754 | (0) . __assert_fail ("!VBaseOffsetOffsets.count(BaseDecl) && \"vbase offset offset already exists!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 754, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "vbase offset offset already exists!"); |
755 | |
756 | CharUnits VBaseOffsetOffset = getCurrentOffsetOffset(); |
757 | VBaseOffsetOffsets.insert( |
758 | std::make_pair(BaseDecl, VBaseOffsetOffset)); |
759 | |
760 | Components.push_back( |
761 | VTableComponent::MakeVBaseOffset(Offset)); |
762 | } |
763 | |
764 | |
765 | AddVBaseOffsets(BaseDecl, OffsetInLayoutClass); |
766 | } |
767 | } |
768 | |
769 | |
770 | class ItaniumVTableBuilder { |
771 | public: |
772 | |
773 | |
774 | typedef llvm::SmallSetVector<const CXXRecordDecl *, 8> |
775 | PrimaryBasesSetVectorTy; |
776 | |
777 | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> |
778 | VBaseOffsetOffsetsMapTy; |
779 | |
780 | typedef VTableLayout::AddressPointsMapTy AddressPointsMapTy; |
781 | |
782 | typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVTableIndicesTy; |
783 | |
784 | private: |
785 | |
786 | ItaniumVTableContext &VTables; |
787 | |
788 | |
789 | |
790 | const CXXRecordDecl *MostDerivedClass; |
791 | |
792 | |
793 | |
794 | const CharUnits MostDerivedClassOffset; |
795 | |
796 | |
797 | |
798 | bool MostDerivedClassIsVirtual; |
799 | |
800 | |
801 | |
802 | |
803 | const CXXRecordDecl *LayoutClass; |
804 | |
805 | |
806 | ASTContext &Context; |
807 | |
808 | |
809 | const FinalOverriders Overriders; |
810 | |
811 | |
812 | |
813 | llvm::DenseMap<const CXXRecordDecl *, VCallOffsetMap> VCallOffsetsForVBases; |
814 | |
815 | |
816 | |
817 | VBaseOffsetOffsetsMapTy VBaseOffsetOffsets; |
818 | |
819 | |
820 | SmallVector<VTableComponent, 64> Components; |
821 | |
822 | |
823 | AddressPointsMapTy AddressPoints; |
824 | |
825 | |
826 | |
827 | struct MethodInfo { |
828 | |
829 | const CharUnits BaseOffset; |
830 | |
831 | |
832 | |
833 | const CharUnits BaseOffsetInLayoutClass; |
834 | |
835 | |
836 | |
837 | const uint64_t VTableIndex; |
838 | |
839 | MethodInfo(CharUnits BaseOffset, CharUnits BaseOffsetInLayoutClass, |
840 | uint64_t VTableIndex) |
841 | : BaseOffset(BaseOffset), |
842 | BaseOffsetInLayoutClass(BaseOffsetInLayoutClass), |
843 | VTableIndex(VTableIndex) { } |
844 | |
845 | MethodInfo() |
846 | : BaseOffset(CharUnits::Zero()), |
847 | BaseOffsetInLayoutClass(CharUnits::Zero()), |
848 | VTableIndex(0) { } |
849 | |
850 | MethodInfo(MethodInfo const&) = default; |
851 | }; |
852 | |
853 | typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy; |
854 | |
855 | |
856 | |
857 | MethodInfoMapTy MethodInfoMap; |
858 | |
859 | |
860 | |
861 | MethodVTableIndicesTy MethodVTableIndices; |
862 | |
863 | typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy; |
864 | |
865 | |
866 | |
867 | VTableThunksMapTy VTableThunks; |
868 | |
869 | typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy; |
870 | typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy; |
871 | |
872 | |
873 | |
874 | ThunksMapTy Thunks; |
875 | |
876 | |
877 | void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk); |
878 | |
879 | |
880 | |
881 | void ComputeThisAdjustments(); |
882 | |
883 | typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy; |
884 | |
885 | |
886 | |
887 | VisitedVirtualBasesSetTy PrimaryVirtualBases; |
888 | |
889 | |
890 | |
891 | ReturnAdjustment ComputeReturnAdjustment(BaseOffset Offset); |
892 | |
893 | |
894 | |
895 | BaseOffset ComputeThisAdjustmentBaseOffset(BaseSubobject Base, |
896 | BaseSubobject Derived) const; |
897 | |
898 | |
899 | |
900 | |
901 | ThisAdjustment |
902 | ComputeThisAdjustment(const CXXMethodDecl *MD, |
903 | CharUnits BaseOffsetInLayoutClass, |
904 | FinalOverriders::OverriderInfo Overrider); |
905 | |
906 | |
907 | |
908 | void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment); |
909 | |
910 | |
911 | |
912 | |
913 | |
914 | |
915 | |
916 | |
917 | |
918 | |
919 | |
920 | |
921 | |
922 | |
923 | |
924 | |
925 | |
926 | |
927 | |
928 | |
929 | bool IsOverriderUsed(const CXXMethodDecl *Overrider, |
930 | CharUnits BaseOffsetInLayoutClass, |
931 | const CXXRecordDecl *FirstBaseInPrimaryBaseChain, |
932 | CharUnits FirstBaseOffsetInLayoutClass) const; |
933 | |
934 | |
935 | |
936 | |
937 | void AddMethods(BaseSubobject Base, CharUnits BaseOffsetInLayoutClass, |
938 | const CXXRecordDecl *FirstBaseInPrimaryBaseChain, |
939 | CharUnits FirstBaseOffsetInLayoutClass, |
940 | PrimaryBasesSetVectorTy &PrimaryBases); |
941 | |
942 | |
943 | |
944 | void LayoutVTable(); |
945 | |
946 | |
947 | |
948 | |
949 | |
950 | |
951 | |
952 | |
953 | |
954 | void LayoutPrimaryAndSecondaryVTables(BaseSubobject Base, |
955 | bool BaseIsMorallyVirtual, |
956 | bool BaseIsVirtualInLayoutClass, |
957 | CharUnits OffsetInLayoutClass); |
958 | |
959 | |
960 | |
961 | |
962 | |
963 | |
964 | void LayoutSecondaryVTables(BaseSubobject Base, bool BaseIsMorallyVirtual, |
965 | CharUnits OffsetInLayoutClass); |
966 | |
967 | |
968 | |
969 | void DeterminePrimaryVirtualBases(const CXXRecordDecl *RD, |
970 | CharUnits OffsetInLayoutClass, |
971 | VisitedVirtualBasesSetTy &VBases); |
972 | |
973 | |
974 | |
975 | void LayoutVTablesForVirtualBases(const CXXRecordDecl *RD, |
976 | VisitedVirtualBasesSetTy &VBases); |
977 | |
978 | |
979 | |
980 | bool isBuildingConstructorVTable() const { |
981 | return MostDerivedClass != LayoutClass; |
982 | } |
983 | |
984 | public: |
985 | |
986 | |
987 | SmallVector<size_t, 4> VTableIndices; |
988 | |
989 | ItaniumVTableBuilder(ItaniumVTableContext &VTables, |
990 | const CXXRecordDecl *MostDerivedClass, |
991 | CharUnits MostDerivedClassOffset, |
992 | bool MostDerivedClassIsVirtual, |
993 | const CXXRecordDecl *LayoutClass) |
994 | : VTables(VTables), MostDerivedClass(MostDerivedClass), |
995 | MostDerivedClassOffset(MostDerivedClassOffset), |
996 | MostDerivedClassIsVirtual(MostDerivedClassIsVirtual), |
997 | LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()), |
998 | Overriders(MostDerivedClass, MostDerivedClassOffset, LayoutClass) { |
999 | assert(!Context.getTargetInfo().getCXXABI().isMicrosoft()); |
1000 | |
1001 | LayoutVTable(); |
1002 | |
1003 | if (Context.getLangOpts().DumpVTableLayouts) |
1004 | dumpLayout(llvm::outs()); |
1005 | } |
1006 | |
1007 | uint64_t getNumThunks() const { |
1008 | return Thunks.size(); |
1009 | } |
1010 | |
1011 | ThunksMapTy::const_iterator thunks_begin() const { |
1012 | return Thunks.begin(); |
1013 | } |
1014 | |
1015 | ThunksMapTy::const_iterator thunks_end() const { |
1016 | return Thunks.end(); |
1017 | } |
1018 | |
1019 | const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const { |
1020 | return VBaseOffsetOffsets; |
1021 | } |
1022 | |
1023 | const AddressPointsMapTy &getAddressPoints() const { |
1024 | return AddressPoints; |
1025 | } |
1026 | |
1027 | MethodVTableIndicesTy::const_iterator vtable_indices_begin() const { |
1028 | return MethodVTableIndices.begin(); |
1029 | } |
1030 | |
1031 | MethodVTableIndicesTy::const_iterator vtable_indices_end() const { |
1032 | return MethodVTableIndices.end(); |
1033 | } |
1034 | |
1035 | ArrayRef<VTableComponent> vtable_components() const { return Components; } |
1036 | |
1037 | AddressPointsMapTy::const_iterator address_points_begin() const { |
1038 | return AddressPoints.begin(); |
1039 | } |
1040 | |
1041 | AddressPointsMapTy::const_iterator address_points_end() const { |
1042 | return AddressPoints.end(); |
1043 | } |
1044 | |
1045 | VTableThunksMapTy::const_iterator vtable_thunks_begin() const { |
1046 | return VTableThunks.begin(); |
1047 | } |
1048 | |
1049 | VTableThunksMapTy::const_iterator vtable_thunks_end() const { |
1050 | return VTableThunks.end(); |
1051 | } |
1052 | |
1053 | |
1054 | void dumpLayout(raw_ostream&); |
1055 | }; |
1056 | |
1057 | void ItaniumVTableBuilder::AddThunk(const CXXMethodDecl *MD, |
1058 | const ThunkInfo &Thunk) { |
1059 | (0) . __assert_fail ("!isBuildingConstructorVTable() && \"Can't add thunks for construction vtable\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1060, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!isBuildingConstructorVTable() && |
1060 | (0) . __assert_fail ("!isBuildingConstructorVTable() && \"Can't add thunks for construction vtable\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1060, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Can't add thunks for construction vtable"); |
1061 | |
1062 | SmallVectorImpl<ThunkInfo> &ThunksVector = Thunks[MD]; |
1063 | |
1064 | |
1065 | if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) != |
1066 | ThunksVector.end()) |
1067 | return; |
1068 | |
1069 | ThunksVector.push_back(Thunk); |
1070 | } |
1071 | |
1072 | typedef llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverriddenMethodsSetTy; |
1073 | |
1074 | |
1075 | |
1076 | |
1077 | |
1078 | template <class VisitorTy> |
1079 | static void |
1080 | visitAllOverriddenMethods(const CXXMethodDecl *MD, VisitorTy &Visitor) { |
1081 | (0) . __assert_fail ("MD->isVirtual() && \"Method is not virtual!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1081, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(MD->isVirtual() && "Method is not virtual!"); |
1082 | |
1083 | for (const CXXMethodDecl *OverriddenMD : MD->overridden_methods()) { |
1084 | if (!Visitor(OverriddenMD)) |
1085 | continue; |
1086 | visitAllOverriddenMethods(OverriddenMD, Visitor); |
1087 | } |
1088 | } |
1089 | |
1090 | |
1091 | |
1092 | static void |
1093 | ComputeAllOverriddenMethods(const CXXMethodDecl *MD, |
1094 | OverriddenMethodsSetTy& OverriddenMethods) { |
1095 | auto OverriddenMethodsCollector = [&](const CXXMethodDecl *MD) { |
1096 | |
1097 | return OverriddenMethods.insert(MD).second; |
1098 | }; |
1099 | visitAllOverriddenMethods(MD, OverriddenMethodsCollector); |
1100 | } |
1101 | |
1102 | void ItaniumVTableBuilder::ComputeThisAdjustments() { |
1103 | |
1104 | |
1105 | for (const auto &MI : MethodInfoMap) { |
1106 | const CXXMethodDecl *MD = MI.first; |
1107 | const MethodInfo &MethodInfo = MI.second; |
1108 | |
1109 | |
1110 | uint64_t VTableIndex = MethodInfo.VTableIndex; |
1111 | if (Components[VTableIndex].getKind() == |
1112 | VTableComponent::CK_UnusedFunctionPointer) |
1113 | continue; |
1114 | |
1115 | |
1116 | FinalOverriders::OverriderInfo Overrider = |
1117 | Overriders.getOverrider(MD, MethodInfo.BaseOffset); |
1118 | |
1119 | |
1120 | if (MethodInfo.BaseOffsetInLayoutClass == Overrider.Offset) { |
1121 | |
1122 | |
1123 | |
1124 | |
1125 | |
1126 | if (VTableThunks.lookup(VTableIndex).Return.isEmpty()) |
1127 | continue; |
1128 | } |
1129 | |
1130 | ThisAdjustment ThisAdjustment = |
1131 | ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider); |
1132 | |
1133 | if (ThisAdjustment.isEmpty()) |
1134 | continue; |
1135 | |
1136 | |
1137 | VTableThunks[VTableIndex].This = ThisAdjustment; |
1138 | |
1139 | if (isa<CXXDestructorDecl>(MD)) { |
1140 | |
1141 | VTableThunks[VTableIndex + 1].This = ThisAdjustment; |
1142 | } |
1143 | } |
1144 | |
1145 | |
1146 | MethodInfoMap.clear(); |
1147 | |
1148 | if (isBuildingConstructorVTable()) { |
1149 | |
1150 | return; |
1151 | } |
1152 | |
1153 | for (const auto &TI : VTableThunks) { |
1154 | const VTableComponent &Component = Components[TI.first]; |
1155 | const ThunkInfo &Thunk = TI.second; |
1156 | const CXXMethodDecl *MD; |
1157 | |
1158 | switch (Component.getKind()) { |
1159 | default: |
1160 | llvm_unreachable("Unexpected vtable component kind!"); |
1161 | case VTableComponent::CK_FunctionPointer: |
1162 | MD = Component.getFunctionDecl(); |
1163 | break; |
1164 | case VTableComponent::CK_CompleteDtorPointer: |
1165 | MD = Component.getDestructorDecl(); |
1166 | break; |
1167 | case VTableComponent::CK_DeletingDtorPointer: |
1168 | |
1169 | continue; |
1170 | } |
1171 | |
1172 | if (MD->getParent() == MostDerivedClass) |
1173 | AddThunk(MD, Thunk); |
1174 | } |
1175 | } |
1176 | |
1177 | ReturnAdjustment |
1178 | ItaniumVTableBuilder::ComputeReturnAdjustment(BaseOffset Offset) { |
1179 | ReturnAdjustment Adjustment; |
1180 | |
1181 | if (!Offset.isEmpty()) { |
1182 | if (Offset.VirtualBase) { |
1183 | |
1184 | if (Offset.DerivedClass == MostDerivedClass) { |
1185 | |
1186 | Adjustment.Virtual.Itanium.VBaseOffsetOffset = |
1187 | VBaseOffsetOffsets.lookup(Offset.VirtualBase).getQuantity(); |
1188 | } else { |
1189 | Adjustment.Virtual.Itanium.VBaseOffsetOffset = |
1190 | VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass, |
1191 | Offset.VirtualBase).getQuantity(); |
1192 | } |
1193 | } |
1194 | |
1195 | Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity(); |
1196 | } |
1197 | |
1198 | return Adjustment; |
1199 | } |
1200 | |
1201 | BaseOffset ItaniumVTableBuilder::ComputeThisAdjustmentBaseOffset( |
1202 | BaseSubobject Base, BaseSubobject Derived) const { |
1203 | const CXXRecordDecl *BaseRD = Base.getBase(); |
1204 | const CXXRecordDecl *DerivedRD = Derived.getBase(); |
1205 | |
1206 | CXXBasePaths Paths(, |
1207 | , ); |
1208 | |
1209 | if (!DerivedRD->isDerivedFrom(BaseRD, Paths)) |
1210 | llvm_unreachable("Class must be derived from the passed in base class!"); |
1211 | |
1212 | |
1213 | |
1214 | for (const CXXBasePath &Path : Paths) { |
1215 | BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, Path); |
1216 | |
1217 | CharUnits OffsetToBaseSubobject = Offset.NonVirtualOffset; |
1218 | |
1219 | if (Offset.VirtualBase) { |
1220 | |
1221 | |
1222 | const ASTRecordLayout &LayoutClassLayout = |
1223 | Context.getASTRecordLayout(LayoutClass); |
1224 | |
1225 | |
1226 | |
1227 | OffsetToBaseSubobject += |
1228 | LayoutClassLayout.getVBaseClassOffset(Offset.VirtualBase); |
1229 | } else { |
1230 | |
1231 | |
1232 | OffsetToBaseSubobject += Derived.getBaseOffset(); |
1233 | } |
1234 | |
1235 | |
1236 | if (OffsetToBaseSubobject == Base.getBaseOffset()) { |
1237 | |
1238 | |
1239 | Offset.NonVirtualOffset = -Offset.NonVirtualOffset; |
1240 | return Offset; |
1241 | } |
1242 | } |
1243 | |
1244 | return BaseOffset(); |
1245 | } |
1246 | |
1247 | ThisAdjustment ItaniumVTableBuilder::ComputeThisAdjustment( |
1248 | const CXXMethodDecl *MD, CharUnits BaseOffsetInLayoutClass, |
1249 | FinalOverriders::OverriderInfo Overrider) { |
1250 | |
1251 | if (Overrider.Method->isPure()) |
1252 | return ThisAdjustment(); |
1253 | |
1254 | BaseSubobject OverriddenBaseSubobject(MD->getParent(), |
1255 | BaseOffsetInLayoutClass); |
1256 | |
1257 | BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(), |
1258 | Overrider.Offset); |
1259 | |
1260 | |
1261 | BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject, |
1262 | OverriderBaseSubobject); |
1263 | if (Offset.isEmpty()) |
1264 | return ThisAdjustment(); |
1265 | |
1266 | ThisAdjustment Adjustment; |
1267 | |
1268 | if (Offset.VirtualBase) { |
1269 | |
1270 | VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase]; |
1271 | |
1272 | if (VCallOffsets.empty()) { |
1273 | |
1274 | |
1275 | VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, MostDerivedClass, |
1276 | , |
1277 | BaseSubobject(Offset.VirtualBase, |
1278 | CharUnits::Zero()), |
1279 | , |
1280 | |
1281 | CharUnits::Zero()); |
1282 | |
1283 | VCallOffsets = Builder.getVCallOffsets(); |
1284 | } |
1285 | |
1286 | Adjustment.Virtual.Itanium.VCallOffsetOffset = |
1287 | VCallOffsets.getVCallOffsetOffset(MD).getQuantity(); |
1288 | } |
1289 | |
1290 | |
1291 | Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity(); |
1292 | |
1293 | return Adjustment; |
1294 | } |
1295 | |
1296 | void ItaniumVTableBuilder::AddMethod(const CXXMethodDecl *MD, |
1297 | ReturnAdjustment ReturnAdjustment) { |
1298 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
1299 | (0) . __assert_fail ("ReturnAdjustment.isEmpty() && \"Destructor can't have return adjustment!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1300, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ReturnAdjustment.isEmpty() && |
1300 | (0) . __assert_fail ("ReturnAdjustment.isEmpty() && \"Destructor can't have return adjustment!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1300, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Destructor can't have return adjustment!"); |
1301 | |
1302 | |
1303 | Components.push_back(VTableComponent::MakeCompleteDtor(DD)); |
1304 | Components.push_back(VTableComponent::MakeDeletingDtor(DD)); |
1305 | } else { |
1306 | |
1307 | if (!ReturnAdjustment.isEmpty()) |
1308 | VTableThunks[Components.size()].Return = ReturnAdjustment; |
1309 | |
1310 | |
1311 | Components.push_back(VTableComponent::MakeFunction(MD)); |
1312 | } |
1313 | } |
1314 | |
1315 | |
1316 | |
1317 | |
1318 | |
1319 | |
1320 | |
1321 | |
1322 | |
1323 | |
1324 | |
1325 | |
1326 | static bool OverridesIndirectMethodInBases( |
1327 | const CXXMethodDecl *MD, |
1328 | ItaniumVTableBuilder::PrimaryBasesSetVectorTy &Bases) { |
1329 | if (Bases.count(MD->getParent())) |
1330 | return true; |
1331 | |
1332 | for (const CXXMethodDecl *OverriddenMD : MD->overridden_methods()) { |
1333 | |
1334 | if (OverridesIndirectMethodInBases(OverriddenMD, Bases)) |
1335 | return true; |
1336 | } |
1337 | |
1338 | return false; |
1339 | } |
1340 | |
1341 | bool ItaniumVTableBuilder::IsOverriderUsed( |
1342 | const CXXMethodDecl *Overrider, CharUnits BaseOffsetInLayoutClass, |
1343 | const CXXRecordDecl *FirstBaseInPrimaryBaseChain, |
1344 | CharUnits FirstBaseOffsetInLayoutClass) const { |
1345 | |
1346 | |
1347 | if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass) |
1348 | return true; |
1349 | |
1350 | |
1351 | |
1352 | |
1353 | |
1354 | |
1355 | |
1356 | if (Overrider->getParent() == FirstBaseInPrimaryBaseChain) |
1357 | return true; |
1358 | |
1359 | ItaniumVTableBuilder::PrimaryBasesSetVectorTy PrimaryBases; |
1360 | |
1361 | const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain; |
1362 | PrimaryBases.insert(RD); |
1363 | |
1364 | |
1365 | |
1366 | while (true) { |
1367 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
1368 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
1369 | |
1370 | if (!PrimaryBase) |
1371 | break; |
1372 | |
1373 | if (Layout.isPrimaryBaseVirtual()) { |
1374 | (0) . __assert_fail ("Layout.getVBaseClassOffset(PrimaryBase).isZero() && \"Primary base should always be at offset 0!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1375, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() && |
1375 | (0) . __assert_fail ("Layout.getVBaseClassOffset(PrimaryBase).isZero() && \"Primary base should always be at offset 0!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1375, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Primary base should always be at offset 0!"); |
1376 | |
1377 | const ASTRecordLayout &LayoutClassLayout = |
1378 | Context.getASTRecordLayout(LayoutClass); |
1379 | |
1380 | |
1381 | |
1382 | if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) != |
1383 | FirstBaseOffsetInLayoutClass) { |
1384 | |
1385 | break; |
1386 | } |
1387 | } else { |
1388 | (0) . __assert_fail ("Layout.getBaseClassOffset(PrimaryBase).isZero() && \"Primary base should always be at offset 0!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1389, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Layout.getBaseClassOffset(PrimaryBase).isZero() && |
1389 | (0) . __assert_fail ("Layout.getBaseClassOffset(PrimaryBase).isZero() && \"Primary base should always be at offset 0!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1389, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Primary base should always be at offset 0!"); |
1390 | } |
1391 | |
1392 | if (!PrimaryBases.insert(PrimaryBase)) |
1393 | llvm_unreachable("Found a duplicate primary base!"); |
1394 | |
1395 | RD = PrimaryBase; |
1396 | } |
1397 | |
1398 | |
1399 | |
1400 | return OverridesIndirectMethodInBases(Overrider, PrimaryBases); |
1401 | } |
1402 | |
1403 | typedef llvm::SmallSetVector<const CXXRecordDecl *, 8> BasesSetVectorTy; |
1404 | |
1405 | |
1406 | |
1407 | |
1408 | static const CXXMethodDecl * |
1409 | FindNearestOverriddenMethod(const CXXMethodDecl *MD, |
1410 | BasesSetVectorTy &Bases) { |
1411 | OverriddenMethodsSetTy OverriddenMethods; |
1412 | ComputeAllOverriddenMethods(MD, OverriddenMethods); |
1413 | |
1414 | for (const CXXRecordDecl *PrimaryBase : |
1415 | llvm::make_range(Bases.rbegin(), Bases.rend())) { |
1416 | |
1417 | for (const CXXMethodDecl *OverriddenMD : OverriddenMethods) { |
1418 | |
1419 | if (OverriddenMD->getParent() == PrimaryBase) |
1420 | return OverriddenMD; |
1421 | } |
1422 | } |
1423 | |
1424 | return nullptr; |
1425 | } |
1426 | |
1427 | void ItaniumVTableBuilder::AddMethods( |
1428 | BaseSubobject Base, CharUnits BaseOffsetInLayoutClass, |
1429 | const CXXRecordDecl *FirstBaseInPrimaryBaseChain, |
1430 | CharUnits FirstBaseOffsetInLayoutClass, |
1431 | PrimaryBasesSetVectorTy &PrimaryBases) { |
1432 | |
1433 | |
1434 | |
1435 | |
1436 | |
1437 | |
1438 | |
1439 | |
1440 | |
1441 | const CXXRecordDecl *RD = Base.getBase(); |
1442 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
1443 | |
1444 | if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { |
1445 | CharUnits PrimaryBaseOffset; |
1446 | CharUnits PrimaryBaseOffsetInLayoutClass; |
1447 | if (Layout.isPrimaryBaseVirtual()) { |
1448 | (0) . __assert_fail ("Layout.getVBaseClassOffset(PrimaryBase).isZero() && \"Primary vbase should have a zero offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1449, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() && |
1449 | (0) . __assert_fail ("Layout.getVBaseClassOffset(PrimaryBase).isZero() && \"Primary vbase should have a zero offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1449, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Primary vbase should have a zero offset!"); |
1450 | |
1451 | const ASTRecordLayout &MostDerivedClassLayout = |
1452 | Context.getASTRecordLayout(MostDerivedClass); |
1453 | |
1454 | PrimaryBaseOffset = |
1455 | MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase); |
1456 | |
1457 | const ASTRecordLayout &LayoutClassLayout = |
1458 | Context.getASTRecordLayout(LayoutClass); |
1459 | |
1460 | PrimaryBaseOffsetInLayoutClass = |
1461 | LayoutClassLayout.getVBaseClassOffset(PrimaryBase); |
1462 | } else { |
1463 | (0) . __assert_fail ("Layout.getBaseClassOffset(PrimaryBase).isZero() && \"Primary base should have a zero offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1464, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Layout.getBaseClassOffset(PrimaryBase).isZero() && |
1464 | (0) . __assert_fail ("Layout.getBaseClassOffset(PrimaryBase).isZero() && \"Primary base should have a zero offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1464, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Primary base should have a zero offset!"); |
1465 | |
1466 | PrimaryBaseOffset = Base.getBaseOffset(); |
1467 | PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass; |
1468 | } |
1469 | |
1470 | AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset), |
1471 | PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain, |
1472 | FirstBaseOffsetInLayoutClass, PrimaryBases); |
1473 | |
1474 | if (!PrimaryBases.insert(PrimaryBase)) |
1475 | llvm_unreachable("Found a duplicate primary base!"); |
1476 | } |
1477 | |
1478 | const CXXDestructorDecl *ImplicitVirtualDtor = nullptr; |
1479 | |
1480 | typedef llvm::SmallVector<const CXXMethodDecl *, 8> NewVirtualFunctionsTy; |
1481 | NewVirtualFunctionsTy NewVirtualFunctions; |
1482 | |
1483 | |
1484 | for (const auto *MD : RD->methods()) { |
1485 | if (!MD->isVirtual()) |
1486 | continue; |
1487 | MD = MD->getCanonicalDecl(); |
1488 | |
1489 | |
1490 | FinalOverriders::OverriderInfo Overrider = |
1491 | Overriders.getOverrider(MD, Base.getBaseOffset()); |
1492 | |
1493 | |
1494 | |
1495 | |
1496 | if (const CXXMethodDecl *OverriddenMD = |
1497 | FindNearestOverriddenMethod(MD, PrimaryBases)) { |
1498 | if (ComputeReturnAdjustmentBaseOffset(Context, MD, |
1499 | OverriddenMD).isEmpty()) { |
1500 | |
1501 | |
1502 | (0) . __assert_fail ("MethodInfoMap.count(OverriddenMD) && \"Did not find the overridden method!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1503, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(MethodInfoMap.count(OverriddenMD) && |
1503 | (0) . __assert_fail ("MethodInfoMap.count(OverriddenMD) && \"Did not find the overridden method!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1503, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Did not find the overridden method!"); |
1504 | MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD]; |
1505 | |
1506 | MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass, |
1507 | OverriddenMethodInfo.VTableIndex); |
1508 | |
1509 | (0) . __assert_fail ("!MethodInfoMap.count(MD) && \"Should not have method info for this method yet!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1510, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!MethodInfoMap.count(MD) && |
1510 | (0) . __assert_fail ("!MethodInfoMap.count(MD) && \"Should not have method info for this method yet!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1510, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Should not have method info for this method yet!"); |
1511 | |
1512 | MethodInfoMap.insert(std::make_pair(MD, MethodInfo)); |
1513 | MethodInfoMap.erase(OverriddenMD); |
1514 | |
1515 | |
1516 | |
1517 | |
1518 | |
1519 | if (!isBuildingConstructorVTable() && OverriddenMD != MD) { |
1520 | |
1521 | ThisAdjustment ThisAdjustment = |
1522 | ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass, |
1523 | Overrider); |
1524 | |
1525 | if (ThisAdjustment.Virtual.Itanium.VCallOffsetOffset && |
1526 | Overrider.Method->getParent() == MostDerivedClass) { |
1527 | |
1528 | |
1529 | |
1530 | |
1531 | BaseOffset ReturnAdjustmentOffset = |
1532 | ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD); |
1533 | ReturnAdjustment ReturnAdjustment = |
1534 | ComputeReturnAdjustment(ReturnAdjustmentOffset); |
1535 | |
1536 | |
1537 | AddThunk(Overrider.Method, |
1538 | ThunkInfo(ThisAdjustment, ReturnAdjustment)); |
1539 | } |
1540 | } |
1541 | |
1542 | continue; |
1543 | } |
1544 | } |
1545 | |
1546 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
1547 | if (MD->isImplicit()) { |
1548 | |
1549 | |
1550 | |
1551 | |
1552 | (0) . __assert_fail ("!ImplicitVirtualDtor && \"Did already see an implicit virtual dtor!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1553, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!ImplicitVirtualDtor && |
1553 | (0) . __assert_fail ("!ImplicitVirtualDtor && \"Did already see an implicit virtual dtor!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1553, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Did already see an implicit virtual dtor!"); |
1554 | ImplicitVirtualDtor = DD; |
1555 | continue; |
1556 | } |
1557 | } |
1558 | |
1559 | NewVirtualFunctions.push_back(MD); |
1560 | } |
1561 | |
1562 | if (ImplicitVirtualDtor) |
1563 | NewVirtualFunctions.push_back(ImplicitVirtualDtor); |
1564 | |
1565 | for (const CXXMethodDecl *MD : NewVirtualFunctions) { |
1566 | |
1567 | FinalOverriders::OverriderInfo Overrider = |
1568 | Overriders.getOverrider(MD, Base.getBaseOffset()); |
1569 | |
1570 | |
1571 | MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass, |
1572 | Components.size()); |
1573 | |
1574 | (0) . __assert_fail ("!MethodInfoMap.count(MD) && \"Should not have method info for this method yet!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1575, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!MethodInfoMap.count(MD) && |
1575 | (0) . __assert_fail ("!MethodInfoMap.count(MD) && \"Should not have method info for this method yet!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1575, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Should not have method info for this method yet!"); |
1576 | MethodInfoMap.insert(std::make_pair(MD, MethodInfo)); |
1577 | |
1578 | |
1579 | const CXXMethodDecl *OverriderMD = Overrider.Method; |
1580 | if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass, |
1581 | FirstBaseInPrimaryBaseChain, |
1582 | FirstBaseOffsetInLayoutClass)) { |
1583 | Components.push_back(VTableComponent::MakeUnusedFunction(OverriderMD)); |
1584 | continue; |
1585 | } |
1586 | |
1587 | |
1588 | |
1589 | BaseOffset ReturnAdjustmentOffset; |
1590 | if (!OverriderMD->isPure()) { |
1591 | ReturnAdjustmentOffset = |
1592 | ComputeReturnAdjustmentBaseOffset(Context, OverriderMD, MD); |
1593 | } |
1594 | |
1595 | ReturnAdjustment ReturnAdjustment = |
1596 | ComputeReturnAdjustment(ReturnAdjustmentOffset); |
1597 | |
1598 | AddMethod(Overrider.Method, ReturnAdjustment); |
1599 | } |
1600 | } |
1601 | |
1602 | void ItaniumVTableBuilder::LayoutVTable() { |
1603 | LayoutPrimaryAndSecondaryVTables(BaseSubobject(MostDerivedClass, |
1604 | CharUnits::Zero()), |
1605 | , |
1606 | MostDerivedClassIsVirtual, |
1607 | MostDerivedClassOffset); |
1608 | |
1609 | VisitedVirtualBasesSetTy VBases; |
1610 | |
1611 | |
1612 | DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset, |
1613 | VBases); |
1614 | VBases.clear(); |
1615 | |
1616 | LayoutVTablesForVirtualBases(MostDerivedClass, VBases); |
1617 | |
1618 | |
1619 | bool IsAppleKext = Context.getLangOpts().AppleKext; |
1620 | if (IsAppleKext) |
1621 | Components.push_back(VTableComponent::MakeVCallOffset(CharUnits::Zero())); |
1622 | } |
1623 | |
1624 | void ItaniumVTableBuilder::LayoutPrimaryAndSecondaryVTables( |
1625 | BaseSubobject Base, bool BaseIsMorallyVirtual, |
1626 | bool BaseIsVirtualInLayoutClass, CharUnits OffsetInLayoutClass) { |
1627 | (0) . __assert_fail ("Base.getBase()->isDynamicClass() && \"class does not have a vtable!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 1627, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!"); |
1628 | |
1629 | unsigned VTableIndex = Components.size(); |
1630 | VTableIndices.push_back(VTableIndex); |
1631 | |
1632 | |
1633 | VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders, |
1634 | Base, BaseIsVirtualInLayoutClass, |
1635 | OffsetInLayoutClass); |
1636 | Components.append(Builder.components_begin(), Builder.components_end()); |
1637 | |
1638 | |
1639 | if (BaseIsVirtualInLayoutClass && !Builder.getVCallOffsets().empty()) { |
1640 | VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()]; |
1641 | |
1642 | if (VCallOffsets.empty()) |
1643 | VCallOffsets = Builder.getVCallOffsets(); |
1644 | } |
1645 | |
1646 | |
1647 | |
1648 | if (Base.getBase() == MostDerivedClass) |
1649 | VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets(); |
1650 | |
1651 | |
1652 | CharUnits OffsetToTop = MostDerivedClassOffset - OffsetInLayoutClass; |
1653 | Components.push_back(VTableComponent::MakeOffsetToTop(OffsetToTop)); |
1654 | |
1655 | |
1656 | Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass)); |
1657 | |
1658 | uint64_t AddressPoint = Components.size(); |
1659 | |
1660 | |
1661 | PrimaryBasesSetVectorTy PrimaryBases; |
1662 | AddMethods(Base, OffsetInLayoutClass, |
1663 | Base.getBase(), OffsetInLayoutClass, |
1664 | PrimaryBases); |
1665 | |
1666 | const CXXRecordDecl *RD = Base.getBase(); |
1667 | if (RD == MostDerivedClass) { |
1668 | assert(MethodVTableIndices.empty()); |
1669 | for (const auto &I : MethodInfoMap) { |
1670 | const CXXMethodDecl *MD = I.first; |
1671 | const MethodInfo &MI = I.second; |
1672 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
1673 | MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] |
1674 | = MI.VTableIndex - AddressPoint; |
1675 | MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] |
1676 | = MI.VTableIndex + 1 - AddressPoint; |
1677 | } else { |
1678 | MethodVTableIndices[MD] = MI.VTableIndex - AddressPoint; |
1679 | } |
1680 | } |
1681 | } |
1682 | |
1683 | |
1684 | ComputeThisAdjustments(); |
1685 | |
1686 | |
1687 | while (true) { |
1688 | AddressPoints.insert( |
1689 | std::make_pair(BaseSubobject(RD, OffsetInLayoutClass), |
1690 | VTableLayout::AddressPointLocation{ |
1691 | unsigned(VTableIndices.size() - 1), |
1692 | unsigned(AddressPoint - VTableIndex)})); |
1693 | |
1694 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
1695 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
1696 | |
1697 | if (!PrimaryBase) |
1698 | break; |
1699 | |
1700 | if (Layout.isPrimaryBaseVirtual()) { |
1701 | |
1702 | |
1703 | const ASTRecordLayout &LayoutClassLayout = |
1704 | Context.getASTRecordLayout(LayoutClass); |
1705 | |
1706 | if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) != |
1707 | OffsetInLayoutClass) { |
1708 | |
1709 | break; |
1710 | } |
1711 | } |
1712 | |
1713 | RD = PrimaryBase; |
1714 | } |
1715 | |
1716 | |
1717 | LayoutSecondaryVTables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass); |
1718 | } |
1719 | |
1720 | void |
1721 | ItaniumVTableBuilder::LayoutSecondaryVTables(BaseSubobject Base, |
1722 | bool BaseIsMorallyVirtual, |
1723 | CharUnits OffsetInLayoutClass) { |
1724 | |
1725 | |
1726 | |
1727 | |
1728 | |
1729 | const CXXRecordDecl *RD = Base.getBase(); |
1730 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
1731 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
1732 | |
1733 | for (const auto &B : RD->bases()) { |
1734 | |
1735 | if (B.isVirtual()) |
1736 | continue; |
1737 | |
1738 | const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl(); |
1739 | |
1740 | |
1741 | if (!BaseDecl->isDynamicClass()) |
1742 | continue; |
1743 | |
1744 | if (isBuildingConstructorVTable()) { |
1745 | |
1746 | |
1747 | |
1748 | |
1749 | |
1750 | if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases()) |
1751 | continue; |
1752 | } |
1753 | |
1754 | |
1755 | CharUnits RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl); |
1756 | CharUnits BaseOffset = Base.getBaseOffset() + RelativeBaseOffset; |
1757 | |
1758 | CharUnits BaseOffsetInLayoutClass = |
1759 | OffsetInLayoutClass + RelativeBaseOffset; |
1760 | |
1761 | |
1762 | |
1763 | if (BaseDecl == PrimaryBase) { |
1764 | LayoutSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset), |
1765 | BaseIsMorallyVirtual, BaseOffsetInLayoutClass); |
1766 | continue; |
1767 | } |
1768 | |
1769 | |
1770 | LayoutPrimaryAndSecondaryVTables( |
1771 | BaseSubobject(BaseDecl, BaseOffset), |
1772 | BaseIsMorallyVirtual, |
1773 | , |
1774 | BaseOffsetInLayoutClass); |
1775 | } |
1776 | } |
1777 | |
1778 | void ItaniumVTableBuilder::DeterminePrimaryVirtualBases( |
1779 | const CXXRecordDecl *RD, CharUnits OffsetInLayoutClass, |
1780 | VisitedVirtualBasesSetTy &VBases) { |
1781 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
1782 | |
1783 | |
1784 | if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { |
1785 | |
1786 | |
1787 | if (Layout.isPrimaryBaseVirtual()) { |
1788 | bool IsPrimaryVirtualBase = true; |
1789 | |
1790 | if (isBuildingConstructorVTable()) { |
1791 | |
1792 | |
1793 | const ASTRecordLayout &LayoutClassLayout = |
1794 | Context.getASTRecordLayout(LayoutClass); |
1795 | |
1796 | CharUnits PrimaryBaseOffsetInLayoutClass = |
1797 | LayoutClassLayout.getVBaseClassOffset(PrimaryBase); |
1798 | |
1799 | |
1800 | |
1801 | if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass) |
1802 | IsPrimaryVirtualBase = false; |
1803 | } |
1804 | |
1805 | if (IsPrimaryVirtualBase) |
1806 | PrimaryVirtualBases.insert(PrimaryBase); |
1807 | } |
1808 | } |
1809 | |
1810 | |
1811 | for (const auto &B : RD->bases()) { |
1812 | const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl(); |
1813 | |
1814 | CharUnits BaseOffsetInLayoutClass; |
1815 | |
1816 | if (B.isVirtual()) { |
1817 | if (!VBases.insert(BaseDecl).second) |
1818 | continue; |
1819 | |
1820 | const ASTRecordLayout &LayoutClassLayout = |
1821 | Context.getASTRecordLayout(LayoutClass); |
1822 | |
1823 | BaseOffsetInLayoutClass = |
1824 | LayoutClassLayout.getVBaseClassOffset(BaseDecl); |
1825 | } else { |
1826 | BaseOffsetInLayoutClass = |
1827 | OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl); |
1828 | } |
1829 | |
1830 | DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases); |
1831 | } |
1832 | } |
1833 | |
1834 | void ItaniumVTableBuilder::LayoutVTablesForVirtualBases( |
1835 | const CXXRecordDecl *RD, VisitedVirtualBasesSetTy &VBases) { |
1836 | |
1837 | |
1838 | |
1839 | |
1840 | for (const auto &B : RD->bases()) { |
1841 | const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl(); |
1842 | |
1843 | |
1844 | |
1845 | if (B.isVirtual() && BaseDecl->isDynamicClass() && |
1846 | !PrimaryVirtualBases.count(BaseDecl) && |
1847 | VBases.insert(BaseDecl).second) { |
1848 | const ASTRecordLayout &MostDerivedClassLayout = |
1849 | Context.getASTRecordLayout(MostDerivedClass); |
1850 | CharUnits BaseOffset = |
1851 | MostDerivedClassLayout.getVBaseClassOffset(BaseDecl); |
1852 | |
1853 | const ASTRecordLayout &LayoutClassLayout = |
1854 | Context.getASTRecordLayout(LayoutClass); |
1855 | CharUnits BaseOffsetInLayoutClass = |
1856 | LayoutClassLayout.getVBaseClassOffset(BaseDecl); |
1857 | |
1858 | LayoutPrimaryAndSecondaryVTables( |
1859 | BaseSubobject(BaseDecl, BaseOffset), |
1860 | , |
1861 | , |
1862 | BaseOffsetInLayoutClass); |
1863 | } |
1864 | |
1865 | |
1866 | |
1867 | if (BaseDecl->getNumVBases()) |
1868 | LayoutVTablesForVirtualBases(BaseDecl, VBases); |
1869 | } |
1870 | } |
1871 | |
1872 | |
1873 | void ItaniumVTableBuilder::dumpLayout(raw_ostream &Out) { |
1874 | |
1875 | |
1876 | |
1877 | if (isBuildingConstructorVTable()) { |
1878 | Out << "Construction vtable for ('"; |
1879 | MostDerivedClass->printQualifiedName(Out); |
1880 | Out << "', "; |
1881 | Out << MostDerivedClassOffset.getQuantity() << ") in '"; |
1882 | LayoutClass->printQualifiedName(Out); |
1883 | } else { |
1884 | Out << "Vtable for '"; |
1885 | MostDerivedClass->printQualifiedName(Out); |
1886 | } |
1887 | Out << "' (" << Components.size() << " entries).\n"; |
1888 | |
1889 | |
1890 | |
1891 | |
1892 | |
1893 | std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex; |
1894 | for (const auto &AP : AddressPoints) { |
1895 | const BaseSubobject &Base = AP.first; |
1896 | uint64_t Index = |
1897 | VTableIndices[AP.second.VTableIndex] + AP.second.AddressPointIndex; |
1898 | |
1899 | AddressPointsByIndex.insert(std::make_pair(Index, Base)); |
1900 | } |
1901 | |
1902 | for (unsigned I = 0, E = Components.size(); I != E; ++I) { |
1903 | uint64_t Index = I; |
1904 | |
1905 | Out << llvm::format("%4d | ", I); |
1906 | |
1907 | const VTableComponent &Component = Components[I]; |
1908 | |
1909 | |
1910 | switch (Component.getKind()) { |
1911 | |
1912 | case VTableComponent::CK_VCallOffset: |
1913 | Out << "vcall_offset (" |
1914 | << Component.getVCallOffset().getQuantity() |
1915 | << ")"; |
1916 | break; |
1917 | |
1918 | case VTableComponent::CK_VBaseOffset: |
1919 | Out << "vbase_offset (" |
1920 | << Component.getVBaseOffset().getQuantity() |
1921 | << ")"; |
1922 | break; |
1923 | |
1924 | case VTableComponent::CK_OffsetToTop: |
1925 | Out << "offset_to_top (" |
1926 | << Component.getOffsetToTop().getQuantity() |
1927 | << ")"; |
1928 | break; |
1929 | |
1930 | case VTableComponent::CK_RTTI: |
1931 | Component.getRTTIDecl()->printQualifiedName(Out); |
1932 | Out << " RTTI"; |
1933 | break; |
1934 | |
1935 | case VTableComponent::CK_FunctionPointer: { |
1936 | const CXXMethodDecl *MD = Component.getFunctionDecl(); |
1937 | |
1938 | std::string Str = |
1939 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
1940 | MD); |
1941 | Out << Str; |
1942 | if (MD->isPure()) |
1943 | Out << " [pure]"; |
1944 | |
1945 | if (MD->isDeleted()) |
1946 | Out << " [deleted]"; |
1947 | |
1948 | ThunkInfo Thunk = VTableThunks.lookup(I); |
1949 | if (!Thunk.isEmpty()) { |
1950 | |
1951 | if (!Thunk.Return.isEmpty()) { |
1952 | Out << "\n [return adjustment: "; |
1953 | Out << Thunk.Return.NonVirtual << " non-virtual"; |
1954 | |
1955 | if (Thunk.Return.Virtual.Itanium.VBaseOffsetOffset) { |
1956 | Out << ", " << Thunk.Return.Virtual.Itanium.VBaseOffsetOffset; |
1957 | Out << " vbase offset offset"; |
1958 | } |
1959 | |
1960 | Out << ']'; |
1961 | } |
1962 | |
1963 | |
1964 | if (!Thunk.This.isEmpty()) { |
1965 | Out << "\n [this adjustment: "; |
1966 | Out << Thunk.This.NonVirtual << " non-virtual"; |
1967 | |
1968 | if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) { |
1969 | Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset; |
1970 | Out << " vcall offset offset"; |
1971 | } |
1972 | |
1973 | Out << ']'; |
1974 | } |
1975 | } |
1976 | |
1977 | break; |
1978 | } |
1979 | |
1980 | case VTableComponent::CK_CompleteDtorPointer: |
1981 | case VTableComponent::CK_DeletingDtorPointer: { |
1982 | bool IsComplete = |
1983 | Component.getKind() == VTableComponent::CK_CompleteDtorPointer; |
1984 | |
1985 | const CXXDestructorDecl *DD = Component.getDestructorDecl(); |
1986 | |
1987 | DD->printQualifiedName(Out); |
1988 | if (IsComplete) |
1989 | Out << "() [complete]"; |
1990 | else |
1991 | Out << "() [deleting]"; |
1992 | |
1993 | if (DD->isPure()) |
1994 | Out << " [pure]"; |
1995 | |
1996 | ThunkInfo Thunk = VTableThunks.lookup(I); |
1997 | if (!Thunk.isEmpty()) { |
1998 | |
1999 | if (!Thunk.This.isEmpty()) { |
2000 | Out << "\n [this adjustment: "; |
2001 | Out << Thunk.This.NonVirtual << " non-virtual"; |
2002 | |
2003 | if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) { |
2004 | Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset; |
2005 | Out << " vcall offset offset"; |
2006 | } |
2007 | |
2008 | Out << ']'; |
2009 | } |
2010 | } |
2011 | |
2012 | break; |
2013 | } |
2014 | |
2015 | case VTableComponent::CK_UnusedFunctionPointer: { |
2016 | const CXXMethodDecl *MD = Component.getUnusedFunctionDecl(); |
2017 | |
2018 | std::string Str = |
2019 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
2020 | MD); |
2021 | Out << "[unused] " << Str; |
2022 | if (MD->isPure()) |
2023 | Out << " [pure]"; |
2024 | } |
2025 | |
2026 | } |
2027 | |
2028 | Out << '\n'; |
2029 | |
2030 | |
2031 | uint64_t NextIndex = Index + 1; |
2032 | if (AddressPointsByIndex.count(NextIndex)) { |
2033 | if (AddressPointsByIndex.count(NextIndex) == 1) { |
2034 | const BaseSubobject &Base = |
2035 | AddressPointsByIndex.find(NextIndex)->second; |
2036 | |
2037 | Out << " -- ("; |
2038 | Base.getBase()->printQualifiedName(Out); |
2039 | Out << ", " << Base.getBaseOffset().getQuantity(); |
2040 | Out << ") vtable address --\n"; |
2041 | } else { |
2042 | CharUnits BaseOffset = |
2043 | AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset(); |
2044 | |
2045 | |
2046 | std::set<std::string> ClassNames; |
2047 | for (const auto &I : |
2048 | llvm::make_range(AddressPointsByIndex.equal_range(NextIndex))) { |
2049 | (0) . __assert_fail ("I.second.getBaseOffset() == BaseOffset && \"Invalid base offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2050, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(I.second.getBaseOffset() == BaseOffset && |
2050 | (0) . __assert_fail ("I.second.getBaseOffset() == BaseOffset && \"Invalid base offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2050, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invalid base offset!"); |
2051 | const CXXRecordDecl *RD = I.second.getBase(); |
2052 | ClassNames.insert(RD->getQualifiedNameAsString()); |
2053 | } |
2054 | |
2055 | for (const std::string &Name : ClassNames) { |
2056 | Out << " -- (" << Name; |
2057 | Out << ", " << BaseOffset.getQuantity() << ") vtable address --\n"; |
2058 | } |
2059 | } |
2060 | } |
2061 | } |
2062 | |
2063 | Out << '\n'; |
2064 | |
2065 | if (isBuildingConstructorVTable()) |
2066 | return; |
2067 | |
2068 | if (MostDerivedClass->getNumVBases()) { |
2069 | |
2070 | |
2071 | |
2072 | std::map<std::string, CharUnits> ClassNamesAndOffsets; |
2073 | for (const auto &I : VBaseOffsetOffsets) { |
2074 | std::string ClassName = I.first->getQualifiedNameAsString(); |
2075 | CharUnits OffsetOffset = I.second; |
2076 | ClassNamesAndOffsets.insert(std::make_pair(ClassName, OffsetOffset)); |
2077 | } |
2078 | |
2079 | Out << "Virtual base offset offsets for '"; |
2080 | MostDerivedClass->printQualifiedName(Out); |
2081 | Out << "' ("; |
2082 | Out << ClassNamesAndOffsets.size(); |
2083 | Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n"; |
2084 | |
2085 | for (const auto &I : ClassNamesAndOffsets) |
2086 | Out << " " << I.first << " | " << I.second.getQuantity() << '\n'; |
2087 | |
2088 | Out << "\n"; |
2089 | } |
2090 | |
2091 | if (!Thunks.empty()) { |
2092 | |
2093 | std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls; |
2094 | |
2095 | for (const auto &I : Thunks) { |
2096 | const CXXMethodDecl *MD = I.first; |
2097 | std::string MethodName = |
2098 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
2099 | MD); |
2100 | |
2101 | MethodNamesAndDecls.insert(std::make_pair(MethodName, MD)); |
2102 | } |
2103 | |
2104 | for (const auto &I : MethodNamesAndDecls) { |
2105 | const std::string &MethodName = I.first; |
2106 | const CXXMethodDecl *MD = I.second; |
2107 | |
2108 | ThunkInfoVectorTy ThunksVector = Thunks[MD]; |
2109 | llvm::sort(ThunksVector, [](const ThunkInfo &LHS, const ThunkInfo &RHS) { |
2110 | assert(LHS.Method == nullptr && RHS.Method == nullptr); |
2111 | return std::tie(LHS.This, LHS.Return) < std::tie(RHS.This, RHS.Return); |
2112 | }); |
2113 | |
2114 | Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size(); |
2115 | Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n"; |
2116 | |
2117 | for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) { |
2118 | const ThunkInfo &Thunk = ThunksVector[I]; |
2119 | |
2120 | Out << llvm::format("%4d | ", I); |
2121 | |
2122 | |
2123 | if (!Thunk.Return.isEmpty()) { |
2124 | Out << "return adjustment: " << Thunk.Return.NonVirtual; |
2125 | Out << " non-virtual"; |
2126 | if (Thunk.Return.Virtual.Itanium.VBaseOffsetOffset) { |
2127 | Out << ", " << Thunk.Return.Virtual.Itanium.VBaseOffsetOffset; |
2128 | Out << " vbase offset offset"; |
2129 | } |
2130 | |
2131 | if (!Thunk.This.isEmpty()) |
2132 | Out << "\n "; |
2133 | } |
2134 | |
2135 | |
2136 | if (!Thunk.This.isEmpty()) { |
2137 | Out << "this adjustment: "; |
2138 | Out << Thunk.This.NonVirtual << " non-virtual"; |
2139 | |
2140 | if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) { |
2141 | Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset; |
2142 | Out << " vcall offset offset"; |
2143 | } |
2144 | } |
2145 | |
2146 | Out << '\n'; |
2147 | } |
2148 | |
2149 | Out << '\n'; |
2150 | } |
2151 | } |
2152 | |
2153 | |
2154 | |
2155 | std::map<uint64_t, std::string> IndicesMap; |
2156 | |
2157 | for (const auto *MD : MostDerivedClass->methods()) { |
2158 | |
2159 | if (!MD->isVirtual()) |
2160 | continue; |
2161 | MD = MD->getCanonicalDecl(); |
2162 | |
2163 | std::string MethodName = |
2164 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
2165 | MD); |
2166 | |
2167 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
2168 | GlobalDecl GD(DD, Dtor_Complete); |
2169 | assert(MethodVTableIndices.count(GD)); |
2170 | uint64_t VTableIndex = MethodVTableIndices[GD]; |
2171 | IndicesMap[VTableIndex] = MethodName + " [complete]"; |
2172 | IndicesMap[VTableIndex + 1] = MethodName + " [deleting]"; |
2173 | } else { |
2174 | assert(MethodVTableIndices.count(MD)); |
2175 | IndicesMap[MethodVTableIndices[MD]] = MethodName; |
2176 | } |
2177 | } |
2178 | |
2179 | |
2180 | if (!IndicesMap.empty()) { |
2181 | Out << "VTable indices for '"; |
2182 | MostDerivedClass->printQualifiedName(Out); |
2183 | Out << "' (" << IndicesMap.size() << " entries).\n"; |
2184 | |
2185 | for (const auto &I : IndicesMap) { |
2186 | uint64_t VTableIndex = I.first; |
2187 | const std::string &MethodName = I.second; |
2188 | |
2189 | Out << llvm::format("%4" PRIu64 " | ", VTableIndex) << MethodName |
2190 | << '\n'; |
2191 | } |
2192 | } |
2193 | |
2194 | Out << '\n'; |
2195 | } |
2196 | } |
2197 | |
2198 | VTableLayout::VTableLayout(ArrayRef<size_t> VTableIndices, |
2199 | ArrayRef<VTableComponent> VTableComponents, |
2200 | ArrayRef<VTableThunkTy> VTableThunks, |
2201 | const AddressPointsMapTy &AddressPoints) |
2202 | : VTableComponents(VTableComponents), VTableThunks(VTableThunks), |
2203 | AddressPoints(AddressPoints) { |
2204 | if (VTableIndices.size() <= 1) |
2205 | assert(VTableIndices.size() == 1 && VTableIndices[0] == 0); |
2206 | else |
2207 | this->VTableIndices = OwningArrayRef<size_t>(VTableIndices); |
2208 | |
2209 | llvm::sort(this->VTableThunks, [](const VTableLayout::VTableThunkTy &LHS, |
2210 | const VTableLayout::VTableThunkTy &RHS) { |
2211 | (0) . __assert_fail ("(LHS.first != RHS.first || LHS.second == RHS.second) && \"Different thunks should have unique indices!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2212, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((LHS.first != RHS.first || LHS.second == RHS.second) && |
2212 | (0) . __assert_fail ("(LHS.first != RHS.first || LHS.second == RHS.second) && \"Different thunks should have unique indices!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2212, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Different thunks should have unique indices!"); |
2213 | return LHS.first < RHS.first; |
2214 | }); |
2215 | } |
2216 | |
2217 | VTableLayout::~VTableLayout() { } |
2218 | |
2219 | ItaniumVTableContext::ItaniumVTableContext(ASTContext &Context) |
2220 | : VTableContextBase() {} |
2221 | |
2222 | ItaniumVTableContext::~ItaniumVTableContext() {} |
2223 | |
2224 | uint64_t ItaniumVTableContext::getMethodVTableIndex(GlobalDecl GD) { |
2225 | GD = GD.getCanonicalDecl(); |
2226 | MethodVTableIndicesTy::iterator I = MethodVTableIndices.find(GD); |
2227 | if (I != MethodVTableIndices.end()) |
2228 | return I->second; |
2229 | |
2230 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
2231 | |
2232 | computeVTableRelatedInformation(RD); |
2233 | |
2234 | I = MethodVTableIndices.find(GD); |
2235 | (0) . __assert_fail ("I != MethodVTableIndices.end() && \"Did not find index!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2235, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(I != MethodVTableIndices.end() && "Did not find index!"); |
2236 | return I->second; |
2237 | } |
2238 | |
2239 | CharUnits |
2240 | ItaniumVTableContext::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD, |
2241 | const CXXRecordDecl *VBase) { |
2242 | ClassPairTy ClassPair(RD, VBase); |
2243 | |
2244 | VirtualBaseClassOffsetOffsetsMapTy::iterator I = |
2245 | VirtualBaseClassOffsetOffsets.find(ClassPair); |
2246 | if (I != VirtualBaseClassOffsetOffsets.end()) |
2247 | return I->second; |
2248 | |
2249 | VCallAndVBaseOffsetBuilder Builder(RD, RD, , |
2250 | BaseSubobject(RD, CharUnits::Zero()), |
2251 | , |
2252 | ::Zero()); |
2253 | |
2254 | for (const auto &I : Builder.getVBaseOffsetOffsets()) { |
2255 | |
2256 | ClassPairTy ClassPair(RD, I.first); |
2257 | |
2258 | VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I.second)); |
2259 | } |
2260 | |
2261 | I = VirtualBaseClassOffsetOffsets.find(ClassPair); |
2262 | (0) . __assert_fail ("I != VirtualBaseClassOffsetOffsets.end() && \"Did not find index!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2262, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!"); |
2263 | |
2264 | return I->second; |
2265 | } |
2266 | |
2267 | static std::unique_ptr<VTableLayout> |
2268 | CreateVTableLayout(const ItaniumVTableBuilder &Builder) { |
2269 | SmallVector<VTableLayout::VTableThunkTy, 1> |
2270 | VTableThunks(Builder.vtable_thunks_begin(), Builder.vtable_thunks_end()); |
2271 | |
2272 | return llvm::make_unique<VTableLayout>( |
2273 | Builder.VTableIndices, Builder.vtable_components(), VTableThunks, |
2274 | Builder.getAddressPoints()); |
2275 | } |
2276 | |
2277 | void |
2278 | ItaniumVTableContext::computeVTableRelatedInformation(const CXXRecordDecl *RD) { |
2279 | std::unique_ptr<const VTableLayout> &Entry = VTableLayouts[RD]; |
2280 | |
2281 | |
2282 | if (Entry) |
2283 | return; |
2284 | |
2285 | ItaniumVTableBuilder Builder(*this, RD, CharUnits::Zero(), |
2286 | , RD); |
2287 | Entry = CreateVTableLayout(Builder); |
2288 | |
2289 | MethodVTableIndices.insert(Builder.vtable_indices_begin(), |
2290 | Builder.vtable_indices_end()); |
2291 | |
2292 | |
2293 | Thunks.insert(Builder.thunks_begin(), Builder.thunks_end()); |
2294 | |
2295 | |
2296 | |
2297 | |
2298 | if (!RD->getNumVBases()) |
2299 | return; |
2300 | |
2301 | const CXXRecordDecl *VBase = |
2302 | RD->vbases_begin()->getType()->getAsCXXRecordDecl(); |
2303 | |
2304 | if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase))) |
2305 | return; |
2306 | |
2307 | for (const auto &I : Builder.getVBaseOffsetOffsets()) { |
2308 | |
2309 | ClassPairTy ClassPair(RD, I.first); |
2310 | |
2311 | VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I.second)); |
2312 | } |
2313 | } |
2314 | |
2315 | std::unique_ptr<VTableLayout> |
2316 | ItaniumVTableContext::createConstructionVTableLayout( |
2317 | const CXXRecordDecl *MostDerivedClass, CharUnits MostDerivedClassOffset, |
2318 | bool MostDerivedClassIsVirtual, const CXXRecordDecl *LayoutClass) { |
2319 | ItaniumVTableBuilder Builder(*this, MostDerivedClass, MostDerivedClassOffset, |
2320 | MostDerivedClassIsVirtual, LayoutClass); |
2321 | return CreateVTableLayout(Builder); |
2322 | } |
2323 | |
2324 | namespace { |
2325 | |
2326 | |
2327 | |
2328 | |
2329 | |
2330 | |
2331 | |
2332 | |
2333 | |
2334 | |
2335 | |
2336 | |
2337 | |
2338 | |
2339 | |
2340 | |
2341 | |
2342 | |
2343 | |
2344 | |
2345 | |
2346 | |
2347 | |
2348 | |
2349 | |
2350 | |
2351 | |
2352 | |
2353 | |
2354 | |
2355 | |
2356 | |
2357 | |
2358 | |
2359 | |
2360 | |
2361 | |
2362 | |
2363 | |
2364 | |
2365 | |
2366 | |
2367 | |
2368 | class VFTableBuilder { |
2369 | public: |
2370 | typedef llvm::DenseMap<GlobalDecl, MethodVFTableLocation> |
2371 | MethodVFTableLocationsTy; |
2372 | |
2373 | typedef llvm::iterator_range<MethodVFTableLocationsTy::const_iterator> |
2374 | method_locations_range; |
2375 | |
2376 | private: |
2377 | |
2378 | MicrosoftVTableContext &VTables; |
2379 | |
2380 | |
2381 | ASTContext &Context; |
2382 | |
2383 | |
2384 | |
2385 | const CXXRecordDecl *MostDerivedClass; |
2386 | |
2387 | const ASTRecordLayout &MostDerivedClassLayout; |
2388 | |
2389 | const VPtrInfo &WhichVFPtr; |
2390 | |
2391 | |
2392 | const FinalOverriders Overriders; |
2393 | |
2394 | |
2395 | SmallVector<VTableComponent, 64> Components; |
2396 | |
2397 | MethodVFTableLocationsTy MethodVFTableLocations; |
2398 | |
2399 | |
2400 | bool HasRTTIComponent = false; |
2401 | |
2402 | |
2403 | |
2404 | struct MethodInfo { |
2405 | |
2406 | |
2407 | const uint64_t VBTableIndex; |
2408 | |
2409 | |
2410 | const uint64_t VFTableIndex; |
2411 | |
2412 | |
2413 | |
2414 | |
2415 | bool Shadowed; |
2416 | |
2417 | |
2418 | |
2419 | bool ; |
2420 | |
2421 | MethodInfo(uint64_t VBTableIndex, uint64_t VFTableIndex, |
2422 | bool = false) |
2423 | : VBTableIndex(VBTableIndex), VFTableIndex(VFTableIndex), |
2424 | Shadowed(false), UsesExtraSlot(UsesExtraSlot) {} |
2425 | |
2426 | MethodInfo() |
2427 | : VBTableIndex(0), VFTableIndex(0), Shadowed(false), |
2428 | UsesExtraSlot(false) {} |
2429 | }; |
2430 | |
2431 | typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy; |
2432 | |
2433 | |
2434 | |
2435 | MethodInfoMapTy MethodInfoMap; |
2436 | |
2437 | typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy; |
2438 | |
2439 | |
2440 | |
2441 | VTableThunksMapTy VTableThunks; |
2442 | |
2443 | typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy; |
2444 | typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy; |
2445 | |
2446 | |
2447 | |
2448 | ThunksMapTy Thunks; |
2449 | |
2450 | |
2451 | void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk) { |
2452 | SmallVector<ThunkInfo, 1> &ThunksVector = Thunks[MD]; |
2453 | |
2454 | |
2455 | if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) != |
2456 | ThunksVector.end()) |
2457 | return; |
2458 | |
2459 | ThunksVector.push_back(Thunk); |
2460 | } |
2461 | |
2462 | |
2463 | |
2464 | CharUnits ComputeThisOffset(FinalOverriders::OverriderInfo Overrider); |
2465 | |
2466 | void CalculateVtordispAdjustment(FinalOverriders::OverriderInfo Overrider, |
2467 | CharUnits ThisOffset, ThisAdjustment &TA); |
2468 | |
2469 | |
2470 | |
2471 | void AddMethod(const CXXMethodDecl *MD, ThunkInfo TI) { |
2472 | if (!TI.isEmpty()) { |
2473 | VTableThunks[Components.size()] = TI; |
2474 | AddThunk(MD, TI); |
2475 | } |
2476 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
2477 | (0) . __assert_fail ("TI.Return.isEmpty() && \"Destructor can't have return adjustment!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2478, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(TI.Return.isEmpty() && |
2478 | (0) . __assert_fail ("TI.Return.isEmpty() && \"Destructor can't have return adjustment!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2478, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Destructor can't have return adjustment!"); |
2479 | Components.push_back(VTableComponent::MakeDeletingDtor(DD)); |
2480 | } else { |
2481 | Components.push_back(VTableComponent::MakeFunction(MD)); |
2482 | } |
2483 | } |
2484 | |
2485 | |
2486 | |
2487 | void AddMethods(BaseSubobject Base, unsigned BaseDepth, |
2488 | const CXXRecordDecl *LastVBase, |
2489 | BasesSetVectorTy &VisitedBases); |
2490 | |
2491 | void LayoutVFTable() { |
2492 | |
2493 | if (HasRTTIComponent) |
2494 | Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass)); |
2495 | |
2496 | BasesSetVectorTy VisitedBases; |
2497 | AddMethods(BaseSubobject(MostDerivedClass, CharUnits::Zero()), 0, nullptr, |
2498 | VisitedBases); |
2499 | (0) . __assert_fail ("(HasRTTIComponent ? Components.size() - 1 . Components.size()) && \"vftable can't be empty\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2500, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((HasRTTIComponent ? Components.size() - 1 : Components.size()) && |
2500 | (0) . __assert_fail ("(HasRTTIComponent ? Components.size() - 1 . Components.size()) && \"vftable can't be empty\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2500, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "vftable can't be empty"); |
2501 | |
2502 | assert(MethodVFTableLocations.empty()); |
2503 | for (const auto &I : MethodInfoMap) { |
2504 | const CXXMethodDecl *MD = I.first; |
2505 | const MethodInfo &MI = I.second; |
2506 | getCanonicalDecl()", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2506, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(MD == MD->getCanonicalDecl()); |
2507 | |
2508 | |
2509 | |
2510 | if (MD->getParent() != MostDerivedClass || MI.Shadowed) |
2511 | continue; |
2512 | MethodVFTableLocation Loc(MI.VBTableIndex, WhichVFPtr.getVBaseWithVPtr(), |
2513 | WhichVFPtr.NonVirtualOffset, MI.VFTableIndex); |
2514 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
2515 | MethodVFTableLocations[GlobalDecl(DD, Dtor_Deleting)] = Loc; |
2516 | } else { |
2517 | MethodVFTableLocations[MD] = Loc; |
2518 | } |
2519 | } |
2520 | } |
2521 | |
2522 | public: |
2523 | VFTableBuilder(MicrosoftVTableContext &VTables, |
2524 | const CXXRecordDecl *MostDerivedClass, const VPtrInfo &Which) |
2525 | : VTables(VTables), |
2526 | Context(MostDerivedClass->getASTContext()), |
2527 | MostDerivedClass(MostDerivedClass), |
2528 | MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)), |
2529 | WhichVFPtr(Which), |
2530 | Overriders(MostDerivedClass, CharUnits(), MostDerivedClass) { |
2531 | |
2532 | |
2533 | |
2534 | |
2535 | |
2536 | if (Context.getLangOpts().RTTIData) |
2537 | HasRTTIComponent = true; |
2538 | |
2539 | LayoutVFTable(); |
2540 | |
2541 | if (Context.getLangOpts().DumpVTableLayouts) |
2542 | dumpLayout(llvm::outs()); |
2543 | } |
2544 | |
2545 | uint64_t getNumThunks() const { return Thunks.size(); } |
2546 | |
2547 | ThunksMapTy::const_iterator thunks_begin() const { return Thunks.begin(); } |
2548 | |
2549 | ThunksMapTy::const_iterator thunks_end() const { return Thunks.end(); } |
2550 | |
2551 | method_locations_range vtable_locations() const { |
2552 | return method_locations_range(MethodVFTableLocations.begin(), |
2553 | MethodVFTableLocations.end()); |
2554 | } |
2555 | |
2556 | ArrayRef<VTableComponent> vtable_components() const { return Components; } |
2557 | |
2558 | VTableThunksMapTy::const_iterator vtable_thunks_begin() const { |
2559 | return VTableThunks.begin(); |
2560 | } |
2561 | |
2562 | VTableThunksMapTy::const_iterator vtable_thunks_end() const { |
2563 | return VTableThunks.end(); |
2564 | } |
2565 | |
2566 | void dumpLayout(raw_ostream &); |
2567 | }; |
2568 | |
2569 | } |
2570 | |
2571 | |
2572 | |
2573 | |
2574 | |
2575 | |
2576 | |
2577 | |
2578 | |
2579 | |
2580 | |
2581 | |
2582 | |
2583 | |
2584 | |
2585 | |
2586 | |
2587 | |
2588 | |
2589 | |
2590 | |
2591 | |
2592 | |
2593 | |
2594 | |
2595 | |
2596 | |
2597 | |
2598 | |
2599 | |
2600 | |
2601 | |
2602 | |
2603 | |
2604 | |
2605 | |
2606 | |
2607 | |
2608 | |
2609 | |
2610 | |
2611 | |
2612 | |
2613 | |
2614 | |
2615 | |
2616 | |
2617 | |
2618 | |
2619 | |
2620 | |
2621 | |
2622 | |
2623 | |
2624 | |
2625 | CharUnits |
2626 | VFTableBuilder::ComputeThisOffset(FinalOverriders::OverriderInfo Overrider) { |
2627 | BasesSetVectorTy Bases; |
2628 | |
2629 | { |
2630 | |
2631 | OverriddenMethodsSetTy VisitedOverriddenMethods; |
2632 | auto InitialOverriddenDefinitionCollector = [&]( |
2633 | const CXXMethodDecl *OverriddenMD) { |
2634 | if (OverriddenMD->size_overridden_methods() == 0) |
2635 | Bases.insert(OverriddenMD->getParent()); |
2636 | |
2637 | return VisitedOverriddenMethods.insert(OverriddenMD).second; |
2638 | }; |
2639 | visitAllOverriddenMethods(Overrider.Method, |
2640 | InitialOverriddenDefinitionCollector); |
2641 | } |
2642 | |
2643 | |
2644 | |
2645 | if (Bases.size() == 0) |
2646 | return Overrider.Offset; |
2647 | |
2648 | CXXBasePaths Paths; |
2649 | Overrider.Method->getParent()->lookupInBases( |
2650 | [&Bases](const CXXBaseSpecifier *Specifier, CXXBasePath &) { |
2651 | return Bases.count(Specifier->getType()->getAsCXXRecordDecl()); |
2652 | }, |
2653 | Paths); |
2654 | |
2655 | |
2656 | |
2657 | |
2658 | |
2659 | CharUnits Ret; |
2660 | bool First = true; |
2661 | |
2662 | const ASTRecordLayout &OverriderRDLayout = |
2663 | Context.getASTRecordLayout(Overrider.Method->getParent()); |
2664 | for (const CXXBasePath &Path : Paths) { |
2665 | CharUnits ThisOffset = Overrider.Offset; |
2666 | CharUnits LastVBaseOffset; |
2667 | |
2668 | |
2669 | |
2670 | |
2671 | for (const CXXBasePathElement &Element : Path) { |
2672 | QualType CurTy = Element.Base->getType(); |
2673 | const CXXRecordDecl *PrevRD = Element.Class, |
2674 | *CurRD = CurTy->getAsCXXRecordDecl(); |
2675 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(PrevRD); |
2676 | |
2677 | if (Element.Base->isVirtual()) { |
2678 | |
2679 | |
2680 | |
2681 | |
2682 | |
2683 | |
2684 | |
2685 | |
2686 | |
2687 | |
2688 | LastVBaseOffset = ThisOffset = |
2689 | Overrider.Offset + OverriderRDLayout.getVBaseClassOffset(CurRD); |
2690 | } else { |
2691 | ThisOffset += Layout.getBaseClassOffset(CurRD); |
2692 | } |
2693 | } |
2694 | |
2695 | if (isa<CXXDestructorDecl>(Overrider.Method)) { |
2696 | if (LastVBaseOffset.isZero()) { |
2697 | |
2698 | |
2699 | |
2700 | ThisOffset = Overrider.Offset; |
2701 | } else { |
2702 | |
2703 | |
2704 | ThisOffset = LastVBaseOffset; |
2705 | } |
2706 | } |
2707 | |
2708 | if (Ret > ThisOffset || First) { |
2709 | First = false; |
2710 | Ret = ThisOffset; |
2711 | } |
2712 | } |
2713 | |
2714 | (0) . __assert_fail ("!First && \"Method not found in the given subobject?\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2714, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!First && "Method not found in the given subobject?"); |
2715 | return Ret; |
2716 | } |
2717 | |
2718 | |
2719 | |
2720 | |
2721 | |
2722 | |
2723 | |
2724 | |
2725 | |
2726 | |
2727 | |
2728 | |
2729 | |
2730 | |
2731 | |
2732 | |
2733 | |
2734 | |
2735 | |
2736 | |
2737 | |
2738 | |
2739 | |
2740 | |
2741 | |
2742 | |
2743 | |
2744 | |
2745 | |
2746 | |
2747 | |
2748 | |
2749 | |
2750 | |
2751 | |
2752 | |
2753 | |
2754 | |
2755 | |
2756 | |
2757 | |
2758 | |
2759 | |
2760 | |
2761 | |
2762 | |
2763 | |
2764 | |
2765 | |
2766 | |
2767 | |
2768 | |
2769 | |
2770 | |
2771 | |
2772 | |
2773 | |
2774 | |
2775 | |
2776 | |
2777 | |
2778 | |
2779 | |
2780 | |
2781 | |
2782 | |
2783 | |
2784 | |
2785 | |
2786 | |
2787 | |
2788 | |
2789 | |
2790 | |
2791 | |
2792 | |
2793 | |
2794 | |
2795 | |
2796 | |
2797 | |
2798 | |
2799 | |
2800 | |
2801 | |
2802 | |
2803 | |
2804 | |
2805 | |
2806 | |
2807 | |
2808 | |
2809 | |
2810 | |
2811 | |
2812 | |
2813 | |
2814 | |
2815 | |
2816 | void VFTableBuilder::CalculateVtordispAdjustment( |
2817 | FinalOverriders::OverriderInfo Overrider, CharUnits ThisOffset, |
2818 | ThisAdjustment &TA) { |
2819 | const ASTRecordLayout::VBaseOffsetsMapTy &VBaseMap = |
2820 | MostDerivedClassLayout.getVBaseOffsetsMap(); |
2821 | const ASTRecordLayout::VBaseOffsetsMapTy::const_iterator &VBaseMapEntry = |
2822 | VBaseMap.find(WhichVFPtr.getVBaseWithVPtr()); |
2823 | assert(VBaseMapEntry != VBaseMap.end()); |
2824 | |
2825 | |
2826 | |
2827 | if (!VBaseMapEntry->second.hasVtorDisp() || |
2828 | Overrider.VirtualBase == WhichVFPtr.getVBaseWithVPtr()) |
2829 | return; |
2830 | |
2831 | |
2832 | |
2833 | CharUnits OffsetOfVBaseWithVFPtr = VBaseMapEntry->second.VBaseOffset; |
2834 | TA.Virtual.Microsoft.VtordispOffset = |
2835 | (OffsetOfVBaseWithVFPtr - WhichVFPtr.FullOffsetInMDC).getQuantity() - 4; |
2836 | |
2837 | |
2838 | |
2839 | if (Overrider.Method->getParent() == MostDerivedClass || |
2840 | !Overrider.VirtualBase) |
2841 | return; |
2842 | |
2843 | |
2844 | |
2845 | TA.Virtual.Microsoft.VBPtrOffset = |
2846 | (OffsetOfVBaseWithVFPtr + WhichVFPtr.NonVirtualOffset - |
2847 | MostDerivedClassLayout.getVBPtrOffset()).getQuantity(); |
2848 | TA.Virtual.Microsoft.VBOffsetOffset = |
2849 | Context.getTypeSizeInChars(Context.IntTy).getQuantity() * |
2850 | VTables.getVBTableIndex(MostDerivedClass, Overrider.VirtualBase); |
2851 | |
2852 | TA.NonVirtual = (ThisOffset - Overrider.Offset).getQuantity(); |
2853 | } |
2854 | |
2855 | static void GroupNewVirtualOverloads( |
2856 | const CXXRecordDecl *RD, |
2857 | SmallVector<const CXXMethodDecl *, 10> &VirtualMethods) { |
2858 | |
2859 | |
2860 | |
2861 | |
2862 | |
2863 | |
2864 | typedef SmallVector<const CXXMethodDecl *, 1> MethodGroup; |
2865 | SmallVector<MethodGroup, 10> Groups; |
2866 | typedef llvm::DenseMap<DeclarationName, unsigned> VisitedGroupIndicesTy; |
2867 | VisitedGroupIndicesTy VisitedGroupIndices; |
2868 | for (const auto *D : RD->decls()) { |
2869 | const auto *ND = dyn_cast<NamedDecl>(D); |
2870 | if (!ND) |
2871 | continue; |
2872 | VisitedGroupIndicesTy::iterator J; |
2873 | bool Inserted; |
2874 | std::tie(J, Inserted) = VisitedGroupIndices.insert( |
2875 | std::make_pair(ND->getDeclName(), Groups.size())); |
2876 | if (Inserted) |
2877 | Groups.push_back(MethodGroup()); |
2878 | if (const auto *MD = dyn_cast<CXXMethodDecl>(ND)) |
2879 | if (MD->isVirtual()) |
2880 | Groups[J->second].push_back(MD->getCanonicalDecl()); |
2881 | } |
2882 | |
2883 | for (const MethodGroup &Group : Groups) |
2884 | VirtualMethods.append(Group.rbegin(), Group.rend()); |
2885 | } |
2886 | |
2887 | static bool isDirectVBase(const CXXRecordDecl *Base, const CXXRecordDecl *RD) { |
2888 | for (const auto &B : RD->bases()) { |
2889 | if (B.isVirtual() && B.getType()->getAsCXXRecordDecl() == Base) |
2890 | return true; |
2891 | } |
2892 | return false; |
2893 | } |
2894 | |
2895 | void VFTableBuilder::AddMethods(BaseSubobject Base, unsigned BaseDepth, |
2896 | const CXXRecordDecl *LastVBase, |
2897 | BasesSetVectorTy &VisitedBases) { |
2898 | const CXXRecordDecl *RD = Base.getBase(); |
2899 | if (!RD->isPolymorphic()) |
2900 | return; |
2901 | |
2902 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
2903 | |
2904 | |
2905 | |
2906 | |
2907 | const CXXRecordDecl *NextBase = nullptr, *NextLastVBase = LastVBase; |
2908 | CharUnits NextBaseOffset; |
2909 | if (BaseDepth < WhichVFPtr.PathToIntroducingObject.size()) { |
2910 | NextBase = WhichVFPtr.PathToIntroducingObject[BaseDepth]; |
2911 | if (isDirectVBase(NextBase, RD)) { |
2912 | NextLastVBase = NextBase; |
2913 | NextBaseOffset = MostDerivedClassLayout.getVBaseClassOffset(NextBase); |
2914 | } else { |
2915 | NextBaseOffset = |
2916 | Base.getBaseOffset() + Layout.getBaseClassOffset(NextBase); |
2917 | } |
2918 | } else if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { |
2919 | (0) . __assert_fail ("!Layout.isPrimaryBaseVirtual() && \"No primary virtual bases in this ABI\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2920, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Layout.isPrimaryBaseVirtual() && |
2920 | (0) . __assert_fail ("!Layout.isPrimaryBaseVirtual() && \"No primary virtual bases in this ABI\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2920, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "No primary virtual bases in this ABI"); |
2921 | NextBase = PrimaryBase; |
2922 | NextBaseOffset = Base.getBaseOffset(); |
2923 | } |
2924 | |
2925 | if (NextBase) { |
2926 | AddMethods(BaseSubobject(NextBase, NextBaseOffset), BaseDepth + 1, |
2927 | NextLastVBase, VisitedBases); |
2928 | if (!VisitedBases.insert(NextBase)) |
2929 | llvm_unreachable("Found a duplicate primary base!"); |
2930 | } |
2931 | |
2932 | SmallVector<const CXXMethodDecl*, 10> VirtualMethods; |
2933 | |
2934 | GroupNewVirtualOverloads(RD, VirtualMethods); |
2935 | |
2936 | |
2937 | |
2938 | |
2939 | |
2940 | |
2941 | |
2942 | |
2943 | |
2944 | for (const CXXMethodDecl *MD : VirtualMethods) { |
2945 | FinalOverriders::OverriderInfo FinalOverrider = |
2946 | Overriders.getOverrider(MD, Base.getBaseOffset()); |
2947 | const CXXMethodDecl *FinalOverriderMD = FinalOverrider.Method; |
2948 | const CXXMethodDecl *OverriddenMD = |
2949 | FindNearestOverriddenMethod(MD, VisitedBases); |
2950 | |
2951 | ThisAdjustment ThisAdjustmentOffset; |
2952 | bool ReturnAdjustingThunk = false, ForceReturnAdjustmentMangling = false; |
2953 | CharUnits ThisOffset = ComputeThisOffset(FinalOverrider); |
2954 | ThisAdjustmentOffset.NonVirtual = |
2955 | (ThisOffset - WhichVFPtr.FullOffsetInMDC).getQuantity(); |
2956 | if ((OverriddenMD || FinalOverriderMD != MD) && |
2957 | WhichVFPtr.getVBaseWithVPtr()) |
2958 | CalculateVtordispAdjustment(FinalOverrider, ThisOffset, |
2959 | ThisAdjustmentOffset); |
2960 | |
2961 | unsigned VBIndex = |
2962 | LastVBase ? VTables.getVBTableIndex(MostDerivedClass, LastVBase) : 0; |
2963 | |
2964 | if (OverriddenMD) { |
2965 | |
2966 | |
2967 | MethodInfoMapTy::iterator OverriddenMDIterator = |
2968 | MethodInfoMap.find(OverriddenMD); |
2969 | |
2970 | |
2971 | if (OverriddenMDIterator == MethodInfoMap.end()) |
2972 | continue; |
2973 | |
2974 | MethodInfo &OverriddenMethodInfo = OverriddenMDIterator->second; |
2975 | |
2976 | VBIndex = OverriddenMethodInfo.VBTableIndex; |
2977 | |
2978 | |
2979 | |
2980 | |
2981 | |
2982 | |
2983 | ReturnAdjustingThunk = !ComputeReturnAdjustmentBaseOffset( |
2984 | Context, MD, OverriddenMD).isEmpty() || |
2985 | OverriddenMethodInfo.UsesExtraSlot; |
2986 | |
2987 | if (!ReturnAdjustingThunk) { |
2988 | |
2989 | |
2990 | MethodInfo MI(VBIndex, OverriddenMethodInfo.VFTableIndex); |
2991 | MethodInfoMap.erase(OverriddenMDIterator); |
2992 | |
2993 | (0) . __assert_fail ("!MethodInfoMap.count(MD) && \"Should not have method info for this method yet!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2994, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!MethodInfoMap.count(MD) && |
2994 | (0) . __assert_fail ("!MethodInfoMap.count(MD) && \"Should not have method info for this method yet!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 2994, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Should not have method info for this method yet!"); |
2995 | MethodInfoMap.insert(std::make_pair(MD, MI)); |
2996 | continue; |
2997 | } |
2998 | |
2999 | |
3000 | |
3001 | OverriddenMethodInfo.Shadowed = true; |
3002 | |
3003 | |
3004 | |
3005 | ForceReturnAdjustmentMangling = |
3006 | !(MD == FinalOverriderMD && ThisAdjustmentOffset.isEmpty()); |
3007 | } else if (Base.getBaseOffset() != WhichVFPtr.FullOffsetInMDC || |
3008 | MD->size_overridden_methods()) { |
3009 | |
3010 | |
3011 | |
3012 | continue; |
3013 | } |
3014 | |
3015 | |
3016 | |
3017 | MethodInfo MI(VBIndex, |
3018 | HasRTTIComponent ? Components.size() - 1 : Components.size(), |
3019 | ReturnAdjustingThunk); |
3020 | |
3021 | (0) . __assert_fail ("!MethodInfoMap.count(MD) && \"Should not have method info for this method yet!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 3022, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!MethodInfoMap.count(MD) && |
3022 | (0) . __assert_fail ("!MethodInfoMap.count(MD) && \"Should not have method info for this method yet!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 3022, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Should not have method info for this method yet!"); |
3023 | MethodInfoMap.insert(std::make_pair(MD, MI)); |
3024 | |
3025 | |
3026 | |
3027 | BaseOffset ReturnAdjustmentOffset; |
3028 | ReturnAdjustment ReturnAdjustment; |
3029 | if (!FinalOverriderMD->isPure()) { |
3030 | ReturnAdjustmentOffset = |
3031 | ComputeReturnAdjustmentBaseOffset(Context, FinalOverriderMD, MD); |
3032 | } |
3033 | if (!ReturnAdjustmentOffset.isEmpty()) { |
3034 | ForceReturnAdjustmentMangling = true; |
3035 | ReturnAdjustment.NonVirtual = |
3036 | ReturnAdjustmentOffset.NonVirtualOffset.getQuantity(); |
3037 | if (ReturnAdjustmentOffset.VirtualBase) { |
3038 | const ASTRecordLayout &DerivedLayout = |
3039 | Context.getASTRecordLayout(ReturnAdjustmentOffset.DerivedClass); |
3040 | ReturnAdjustment.Virtual.Microsoft.VBPtrOffset = |
3041 | DerivedLayout.getVBPtrOffset().getQuantity(); |
3042 | ReturnAdjustment.Virtual.Microsoft.VBIndex = |
3043 | VTables.getVBTableIndex(ReturnAdjustmentOffset.DerivedClass, |
3044 | ReturnAdjustmentOffset.VirtualBase); |
3045 | } |
3046 | } |
3047 | |
3048 | AddMethod(FinalOverriderMD, |
3049 | ThunkInfo(ThisAdjustmentOffset, ReturnAdjustment, |
3050 | ForceReturnAdjustmentMangling ? MD : nullptr)); |
3051 | } |
3052 | } |
3053 | |
3054 | static void PrintBasePath(const VPtrInfo::BasePath &Path, raw_ostream &Out) { |
3055 | for (const CXXRecordDecl *Elem : |
3056 | llvm::make_range(Path.rbegin(), Path.rend())) { |
3057 | Out << "'"; |
3058 | Elem->printQualifiedName(Out); |
3059 | Out << "' in "; |
3060 | } |
3061 | } |
3062 | |
3063 | static void dumpMicrosoftThunkAdjustment(const ThunkInfo &TI, raw_ostream &Out, |
3064 | bool ContinueFirstLine) { |
3065 | const ReturnAdjustment &R = TI.Return; |
3066 | bool Multiline = false; |
3067 | const char *LinePrefix = "\n "; |
3068 | if (!R.isEmpty() || TI.Method) { |
3069 | if (!ContinueFirstLine) |
3070 | Out << LinePrefix; |
3071 | Out << "[return adjustment (to type '" |
3072 | << TI.Method->getReturnType().getCanonicalType().getAsString() |
3073 | << "'): "; |
3074 | if (R.Virtual.Microsoft.VBPtrOffset) |
3075 | Out << "vbptr at offset " << R.Virtual.Microsoft.VBPtrOffset << ", "; |
3076 | if (R.Virtual.Microsoft.VBIndex) |
3077 | Out << "vbase #" << R.Virtual.Microsoft.VBIndex << ", "; |
3078 | Out << R.NonVirtual << " non-virtual]"; |
3079 | Multiline = true; |
3080 | } |
3081 | |
3082 | const ThisAdjustment &T = TI.This; |
3083 | if (!T.isEmpty()) { |
3084 | if (Multiline || !ContinueFirstLine) |
3085 | Out << LinePrefix; |
3086 | Out << "[this adjustment: "; |
3087 | if (!TI.This.Virtual.isEmpty()) { |
3088 | assert(T.Virtual.Microsoft.VtordispOffset < 0); |
3089 | Out << "vtordisp at " << T.Virtual.Microsoft.VtordispOffset << ", "; |
3090 | if (T.Virtual.Microsoft.VBPtrOffset) { |
3091 | Out << "vbptr at " << T.Virtual.Microsoft.VBPtrOffset |
3092 | << " to the left,"; |
3093 | 0", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 3093, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(T.Virtual.Microsoft.VBOffsetOffset > 0); |
3094 | Out << LinePrefix << " vboffset at " |
3095 | << T.Virtual.Microsoft.VBOffsetOffset << " in the vbtable, "; |
3096 | } |
3097 | } |
3098 | Out << T.NonVirtual << " non-virtual]"; |
3099 | } |
3100 | } |
3101 | |
3102 | void VFTableBuilder::dumpLayout(raw_ostream &Out) { |
3103 | Out << "VFTable for "; |
3104 | PrintBasePath(WhichVFPtr.PathToIntroducingObject, Out); |
3105 | Out << "'"; |
3106 | MostDerivedClass->printQualifiedName(Out); |
3107 | Out << "' (" << Components.size() |
3108 | << (Components.size() == 1 ? " entry" : " entries") << ").\n"; |
3109 | |
3110 | for (unsigned I = 0, E = Components.size(); I != E; ++I) { |
3111 | Out << llvm::format("%4d | ", I); |
3112 | |
3113 | const VTableComponent &Component = Components[I]; |
3114 | |
3115 | |
3116 | switch (Component.getKind()) { |
3117 | case VTableComponent::CK_RTTI: |
3118 | Component.getRTTIDecl()->printQualifiedName(Out); |
3119 | Out << " RTTI"; |
3120 | break; |
3121 | |
3122 | case VTableComponent::CK_FunctionPointer: { |
3123 | const CXXMethodDecl *MD = Component.getFunctionDecl(); |
3124 | |
3125 | |
3126 | |
3127 | std::string Str = PredefinedExpr::ComputeName( |
3128 | PredefinedExpr::PrettyFunctionNoVirtual, MD); |
3129 | Out << Str; |
3130 | if (MD->isPure()) |
3131 | Out << " [pure]"; |
3132 | |
3133 | if (MD->isDeleted()) |
3134 | Out << " [deleted]"; |
3135 | |
3136 | ThunkInfo Thunk = VTableThunks.lookup(I); |
3137 | if (!Thunk.isEmpty()) |
3138 | dumpMicrosoftThunkAdjustment(Thunk, Out, ); |
3139 | |
3140 | break; |
3141 | } |
3142 | |
3143 | case VTableComponent::CK_DeletingDtorPointer: { |
3144 | const CXXDestructorDecl *DD = Component.getDestructorDecl(); |
3145 | |
3146 | DD->printQualifiedName(Out); |
3147 | Out << "() [scalar deleting]"; |
3148 | |
3149 | if (DD->isPure()) |
3150 | Out << " [pure]"; |
3151 | |
3152 | ThunkInfo Thunk = VTableThunks.lookup(I); |
3153 | if (!Thunk.isEmpty()) { |
3154 | (0) . __assert_fail ("Thunk.Return.isEmpty() && \"No return adjustment needed for destructors!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 3155, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Thunk.Return.isEmpty() && |
3155 | (0) . __assert_fail ("Thunk.Return.isEmpty() && \"No return adjustment needed for destructors!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 3155, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "No return adjustment needed for destructors!"); |
3156 | dumpMicrosoftThunkAdjustment(Thunk, Out, ); |
3157 | } |
3158 | |
3159 | break; |
3160 | } |
3161 | |
3162 | default: |
3163 | DiagnosticsEngine &Diags = Context.getDiagnostics(); |
3164 | unsigned DiagID = Diags.getCustomDiagID( |
3165 | DiagnosticsEngine::Error, |
3166 | "Unexpected vftable component type %0 for component number %1"); |
3167 | Diags.Report(MostDerivedClass->getLocation(), DiagID) |
3168 | << I << Component.getKind(); |
3169 | } |
3170 | |
3171 | Out << '\n'; |
3172 | } |
3173 | |
3174 | Out << '\n'; |
3175 | |
3176 | if (!Thunks.empty()) { |
3177 | |
3178 | std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls; |
3179 | |
3180 | for (const auto &I : Thunks) { |
3181 | const CXXMethodDecl *MD = I.first; |
3182 | std::string MethodName = PredefinedExpr::ComputeName( |
3183 | PredefinedExpr::PrettyFunctionNoVirtual, MD); |
3184 | |
3185 | MethodNamesAndDecls.insert(std::make_pair(MethodName, MD)); |
3186 | } |
3187 | |
3188 | for (const auto &MethodNameAndDecl : MethodNamesAndDecls) { |
3189 | const std::string &MethodName = MethodNameAndDecl.first; |
3190 | const CXXMethodDecl *MD = MethodNameAndDecl.second; |
3191 | |
3192 | ThunkInfoVectorTy ThunksVector = Thunks[MD]; |
3193 | std::stable_sort(ThunksVector.begin(), ThunksVector.end(), |
3194 | [](const ThunkInfo &LHS, const ThunkInfo &RHS) { |
3195 | |
3196 | |
3197 | return std::tie(LHS.This, LHS.Return) < std::tie(RHS.This, RHS.Return); |
3198 | }); |
3199 | |
3200 | Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size(); |
3201 | Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n"; |
3202 | |
3203 | for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) { |
3204 | const ThunkInfo &Thunk = ThunksVector[I]; |
3205 | |
3206 | Out << llvm::format("%4d | ", I); |
3207 | dumpMicrosoftThunkAdjustment(Thunk, Out, ); |
3208 | Out << '\n'; |
3209 | } |
3210 | |
3211 | Out << '\n'; |
3212 | } |
3213 | } |
3214 | |
3215 | Out.flush(); |
3216 | } |
3217 | |
3218 | static bool setsIntersect(const llvm::SmallPtrSet<const CXXRecordDecl *, 4> &A, |
3219 | ArrayRef<const CXXRecordDecl *> B) { |
3220 | for (const CXXRecordDecl *Decl : B) { |
3221 | if (A.count(Decl)) |
3222 | return true; |
3223 | } |
3224 | return false; |
3225 | } |
3226 | |
3227 | static bool rebucketPaths(VPtrInfoVector &Paths); |
3228 | |
3229 | |
3230 | |
3231 | |
3232 | |
3233 | |
3234 | |
3235 | |
3236 | |
3237 | |
3238 | |
3239 | |
3240 | |
3241 | |
3242 | |
3243 | |
3244 | |
3245 | |
3246 | |
3247 | |
3248 | |
3249 | |
3250 | void MicrosoftVTableContext::computeVTablePaths(bool ForVBTables, |
3251 | const CXXRecordDecl *RD, |
3252 | VPtrInfoVector &Paths) { |
3253 | assert(Paths.empty()); |
3254 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
3255 | |
3256 | |
3257 | if (ForVBTables ? Layout.hasOwnVBPtr() : Layout.hasOwnVFPtr()) |
3258 | Paths.push_back(llvm::make_unique<VPtrInfo>(RD)); |
3259 | |
3260 | |
3261 | |
3262 | llvm::SmallPtrSet<const CXXRecordDecl*, 4> VBasesSeen; |
3263 | for (const auto &B : RD->bases()) { |
3264 | const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl(); |
3265 | if (B.isVirtual() && VBasesSeen.count(Base)) |
3266 | continue; |
3267 | |
3268 | if (!Base->isDynamicClass()) |
3269 | continue; |
3270 | |
3271 | const VPtrInfoVector &BasePaths = |
3272 | ForVBTables ? enumerateVBTables(Base) : getVFPtrOffsets(Base); |
3273 | |
3274 | for (const std::unique_ptr<VPtrInfo> &BaseInfo : BasePaths) { |
3275 | |
3276 | |
3277 | if (setsIntersect(VBasesSeen, BaseInfo->ContainingVBases)) |
3278 | continue; |
3279 | |
3280 | |
3281 | auto P = llvm::make_unique<VPtrInfo>(*BaseInfo); |
3282 | |
3283 | |
3284 | |
3285 | if (P->MangledPath.empty() || P->MangledPath.back() != Base) |
3286 | P->NextBaseToMangle = Base; |
3287 | |
3288 | |
3289 | |
3290 | |
3291 | if (P->ObjectWithVPtr == Base && |
3292 | Base == (ForVBTables ? Layout.getBaseSharingVBPtr() |
3293 | : Layout.getPrimaryBase())) |
3294 | P->ObjectWithVPtr = RD; |
3295 | |
3296 | |
3297 | |
3298 | if (B.isVirtual()) |
3299 | P->ContainingVBases.push_back(Base); |
3300 | else if (P->ContainingVBases.empty()) |
3301 | P->NonVirtualOffset += Layout.getBaseClassOffset(Base); |
3302 | |
3303 | |
3304 | P->FullOffsetInMDC = P->NonVirtualOffset; |
3305 | if (const CXXRecordDecl *VB = P->getVBaseWithVPtr()) |
3306 | P->FullOffsetInMDC += Layout.getVBaseClassOffset(VB); |
3307 | |
3308 | Paths.push_back(std::move(P)); |
3309 | } |
3310 | |
3311 | if (B.isVirtual()) |
3312 | VBasesSeen.insert(Base); |
3313 | |
3314 | |
3315 | |
3316 | for (const auto &VB : Base->vbases()) |
3317 | VBasesSeen.insert(VB.getType()->getAsCXXRecordDecl()); |
3318 | } |
3319 | |
3320 | |
3321 | |
3322 | bool Changed = true; |
3323 | while (Changed) |
3324 | Changed = rebucketPaths(Paths); |
3325 | } |
3326 | |
3327 | static bool extendPath(VPtrInfo &P) { |
3328 | if (P.NextBaseToMangle) { |
3329 | P.MangledPath.push_back(P.NextBaseToMangle); |
3330 | P.NextBaseToMangle = nullptr; |
3331 | return true; |
3332 | } |
3333 | return false; |
3334 | } |
3335 | |
3336 | static bool rebucketPaths(VPtrInfoVector &Paths) { |
3337 | |
3338 | |
3339 | |
3340 | |
3341 | |
3342 | |
3343 | llvm::SmallVector<std::reference_wrapper<VPtrInfo>, 2> PathsSorted; |
3344 | PathsSorted.reserve(Paths.size()); |
3345 | for (auto& P : Paths) |
3346 | PathsSorted.push_back(*P); |
3347 | llvm::sort(PathsSorted, [](const VPtrInfo &LHS, const VPtrInfo &RHS) { |
3348 | return LHS.MangledPath < RHS.MangledPath; |
3349 | }); |
3350 | bool Changed = false; |
3351 | for (size_t I = 0, E = PathsSorted.size(); I != E;) { |
3352 | |
3353 | size_t BucketStart = I; |
3354 | do { |
3355 | ++I; |
3356 | } while (I != E && |
3357 | PathsSorted[BucketStart].get().MangledPath == |
3358 | PathsSorted[I].get().MangledPath); |
3359 | |
3360 | |
3361 | if (I - BucketStart > 1) { |
3362 | for (size_t II = BucketStart; II != I; ++II) |
3363 | Changed |= extendPath(PathsSorted[II]); |
3364 | (0) . __assert_fail ("Changed && \"no paths were extended to fix ambiguity\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 3364, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Changed && "no paths were extended to fix ambiguity"); |
3365 | } |
3366 | } |
3367 | return Changed; |
3368 | } |
3369 | |
3370 | MicrosoftVTableContext::~MicrosoftVTableContext() {} |
3371 | |
3372 | namespace { |
3373 | typedef llvm::SetVector<BaseSubobject, std::vector<BaseSubobject>, |
3374 | llvm::DenseSet<BaseSubobject>> FullPathTy; |
3375 | } |
3376 | |
3377 | |
3378 | |
3379 | static void findPathsToSubobject(ASTContext &Context, |
3380 | const ASTRecordLayout &MostDerivedLayout, |
3381 | const CXXRecordDecl *RD, CharUnits Offset, |
3382 | BaseSubobject IntroducingObject, |
3383 | FullPathTy &FullPath, |
3384 | std::list<FullPathTy> &Paths) { |
3385 | if (BaseSubobject(RD, Offset) == IntroducingObject) { |
3386 | Paths.push_back(FullPath); |
3387 | return; |
3388 | } |
3389 | |
3390 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
3391 | |
3392 | for (const CXXBaseSpecifier &BS : RD->bases()) { |
3393 | const CXXRecordDecl *Base = BS.getType()->getAsCXXRecordDecl(); |
3394 | CharUnits NewOffset = BS.isVirtual() |
3395 | ? MostDerivedLayout.getVBaseClassOffset(Base) |
3396 | : Offset + Layout.getBaseClassOffset(Base); |
3397 | FullPath.insert(BaseSubobject(Base, NewOffset)); |
3398 | findPathsToSubobject(Context, MostDerivedLayout, Base, NewOffset, |
3399 | IntroducingObject, FullPath, Paths); |
3400 | FullPath.pop_back(); |
3401 | } |
3402 | } |
3403 | |
3404 | |
3405 | static void removeRedundantPaths(std::list<FullPathTy> &FullPaths) { |
3406 | FullPaths.remove_if([&](const FullPathTy &SpecificPath) { |
3407 | for (const FullPathTy &OtherPath : FullPaths) { |
3408 | if (&SpecificPath == &OtherPath) |
3409 | continue; |
3410 | if (llvm::all_of(SpecificPath, [&](const BaseSubobject &BSO) { |
3411 | return OtherPath.count(BSO) != 0; |
3412 | })) { |
3413 | return true; |
3414 | } |
3415 | } |
3416 | return false; |
3417 | }); |
3418 | } |
3419 | |
3420 | static CharUnits getOffsetOfFullPath(ASTContext &Context, |
3421 | const CXXRecordDecl *RD, |
3422 | const FullPathTy &FullPath) { |
3423 | const ASTRecordLayout &MostDerivedLayout = |
3424 | Context.getASTRecordLayout(RD); |
3425 | CharUnits Offset = CharUnits::fromQuantity(-1); |
3426 | for (const BaseSubobject &BSO : FullPath) { |
3427 | const CXXRecordDecl *Base = BSO.getBase(); |
3428 | |
3429 | if (Base == RD) { |
3430 | assert(Offset.getQuantity() == -1); |
3431 | Offset = CharUnits::Zero(); |
3432 | continue; |
3433 | } |
3434 | assert(Offset.getQuantity() != -1); |
3435 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
3436 | |
3437 | |
3438 | const CXXBaseSpecifier *BaseBS = std::find_if( |
3439 | RD->bases_begin(), RD->bases_end(), [&](const CXXBaseSpecifier &BS) { |
3440 | return BS.getType()->getAsCXXRecordDecl() == Base; |
3441 | }); |
3442 | Offset = BaseBS->isVirtual() ? MostDerivedLayout.getVBaseClassOffset(Base) |
3443 | : Offset + Layout.getBaseClassOffset(Base); |
3444 | RD = Base; |
3445 | } |
3446 | return Offset; |
3447 | } |
3448 | |
3449 | |
3450 | |
3451 | |
3452 | static const FullPathTy *selectBestPath(ASTContext &Context, |
3453 | const CXXRecordDecl *RD, |
3454 | const VPtrInfo &Info, |
3455 | std::list<FullPathTy> &FullPaths) { |
3456 | |
3457 | if (FullPaths.empty()) |
3458 | return nullptr; |
3459 | if (FullPaths.size() == 1) |
3460 | return &FullPaths.front(); |
3461 | |
3462 | const FullPathTy *BestPath = nullptr; |
3463 | typedef std::set<const CXXMethodDecl *> OverriderSetTy; |
3464 | OverriderSetTy LastOverrides; |
3465 | for (const FullPathTy &SpecificPath : FullPaths) { |
3466 | assert(!SpecificPath.empty()); |
3467 | OverriderSetTy CurrentOverrides; |
3468 | const CXXRecordDecl *TopLevelRD = SpecificPath.begin()->getBase(); |
3469 | |
3470 | |
3471 | CharUnits BaseOffset = |
3472 | getOffsetOfFullPath(Context, TopLevelRD, SpecificPath); |
3473 | FinalOverriders Overriders(TopLevelRD, CharUnits::Zero(), TopLevelRD); |
3474 | for (const CXXMethodDecl *MD : Info.IntroducingObject->methods()) { |
3475 | if (!MD->isVirtual()) |
3476 | continue; |
3477 | FinalOverriders::OverriderInfo OI = |
3478 | Overriders.getOverrider(MD->getCanonicalDecl(), BaseOffset); |
3479 | const CXXMethodDecl *OverridingMethod = OI.Method; |
3480 | |
3481 | |
3482 | if (ComputeReturnAdjustmentBaseOffset(Context, OverridingMethod, MD) |
3483 | .isEmpty()) |
3484 | continue; |
3485 | |
3486 | |
3487 | const CXXRecordDecl *OverridingParent = OverridingMethod->getParent(); |
3488 | if (llvm::none_of(SpecificPath, [&](const BaseSubobject &BSO) { |
3489 | return BSO.getBase() == OverridingParent; |
3490 | })) |
3491 | continue; |
3492 | CurrentOverrides.insert(OverridingMethod); |
3493 | } |
3494 | OverriderSetTy NewOverrides = |
3495 | llvm::set_difference(CurrentOverrides, LastOverrides); |
3496 | if (NewOverrides.empty()) |
3497 | continue; |
3498 | OverriderSetTy MissingOverrides = |
3499 | llvm::set_difference(LastOverrides, CurrentOverrides); |
3500 | if (MissingOverrides.empty()) { |
3501 | |
3502 | BestPath = &SpecificPath; |
3503 | std::swap(CurrentOverrides, LastOverrides); |
3504 | } else { |
3505 | |
3506 | DiagnosticsEngine &Diags = Context.getDiagnostics(); |
3507 | const CXXMethodDecl *CovariantMD = *NewOverrides.begin(); |
3508 | const CXXMethodDecl *ConflictMD = *MissingOverrides.begin(); |
3509 | Diags.Report(RD->getLocation(), diag::err_vftable_ambiguous_component) |
3510 | << RD; |
3511 | Diags.Report(CovariantMD->getLocation(), diag::note_covariant_thunk) |
3512 | << CovariantMD; |
3513 | Diags.Report(ConflictMD->getLocation(), diag::note_covariant_thunk) |
3514 | << ConflictMD; |
3515 | } |
3516 | } |
3517 | |
3518 | |
3519 | return BestPath ? BestPath : &FullPaths.front(); |
3520 | } |
3521 | |
3522 | static void computeFullPathsForVFTables(ASTContext &Context, |
3523 | const CXXRecordDecl *RD, |
3524 | VPtrInfoVector &Paths) { |
3525 | const ASTRecordLayout &MostDerivedLayout = Context.getASTRecordLayout(RD); |
3526 | FullPathTy FullPath; |
3527 | std::list<FullPathTy> FullPaths; |
3528 | for (const std::unique_ptr<VPtrInfo>& Info : Paths) { |
3529 | findPathsToSubobject( |
3530 | Context, MostDerivedLayout, RD, CharUnits::Zero(), |
3531 | BaseSubobject(Info->IntroducingObject, Info->FullOffsetInMDC), FullPath, |
3532 | FullPaths); |
3533 | FullPath.clear(); |
3534 | removeRedundantPaths(FullPaths); |
3535 | Info->PathToIntroducingObject.clear(); |
3536 | if (const FullPathTy *BestPath = |
3537 | selectBestPath(Context, RD, *Info, FullPaths)) |
3538 | for (const BaseSubobject &BSO : *BestPath) |
3539 | Info->PathToIntroducingObject.push_back(BSO.getBase()); |
3540 | FullPaths.clear(); |
3541 | } |
3542 | } |
3543 | |
3544 | static bool vfptrIsEarlierInMDC(const ASTRecordLayout &Layout, |
3545 | const MethodVFTableLocation &LHS, |
3546 | const MethodVFTableLocation &RHS) { |
3547 | CharUnits L = LHS.VFPtrOffset; |
3548 | CharUnits R = RHS.VFPtrOffset; |
3549 | if (LHS.VBase) |
3550 | L += Layout.getVBaseClassOffset(LHS.VBase); |
3551 | if (RHS.VBase) |
3552 | R += Layout.getVBaseClassOffset(RHS.VBase); |
3553 | return L < R; |
3554 | } |
3555 | |
3556 | void MicrosoftVTableContext::computeVTableRelatedInformation( |
3557 | const CXXRecordDecl *RD) { |
3558 | isDynamicClass()", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 3558, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(RD->isDynamicClass()); |
3559 | |
3560 | |
3561 | if (VFPtrLocations.count(RD)) |
3562 | return; |
3563 | |
3564 | const VTableLayout::AddressPointsMapTy EmptyAddressPointsMap; |
3565 | |
3566 | { |
3567 | auto VFPtrs = llvm::make_unique<VPtrInfoVector>(); |
3568 | computeVTablePaths(, RD, *VFPtrs); |
3569 | computeFullPathsForVFTables(Context, RD, *VFPtrs); |
3570 | VFPtrLocations[RD] = std::move(VFPtrs); |
3571 | } |
3572 | |
3573 | MethodVFTableLocationsTy NewMethodLocations; |
3574 | for (const std::unique_ptr<VPtrInfo> &VFPtr : *VFPtrLocations[RD]) { |
3575 | VFTableBuilder Builder(*this, RD, *VFPtr); |
3576 | |
3577 | VFTableIdTy id(RD, VFPtr->FullOffsetInMDC); |
3578 | assert(VFTableLayouts.count(id) == 0); |
3579 | SmallVector<VTableLayout::VTableThunkTy, 1> VTableThunks( |
3580 | Builder.vtable_thunks_begin(), Builder.vtable_thunks_end()); |
3581 | VFTableLayouts[id] = llvm::make_unique<VTableLayout>( |
3582 | ArrayRef<size_t>{0}, Builder.vtable_components(), VTableThunks, |
3583 | EmptyAddressPointsMap); |
3584 | Thunks.insert(Builder.thunks_begin(), Builder.thunks_end()); |
3585 | |
3586 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
3587 | for (const auto &Loc : Builder.vtable_locations()) { |
3588 | auto Insert = NewMethodLocations.insert(Loc); |
3589 | if (!Insert.second) { |
3590 | const MethodVFTableLocation &NewLoc = Loc.second; |
3591 | MethodVFTableLocation &OldLoc = Insert.first->second; |
3592 | if (vfptrIsEarlierInMDC(Layout, NewLoc, OldLoc)) |
3593 | OldLoc = NewLoc; |
3594 | } |
3595 | } |
3596 | } |
3597 | |
3598 | MethodVFTableLocations.insert(NewMethodLocations.begin(), |
3599 | NewMethodLocations.end()); |
3600 | if (Context.getLangOpts().DumpVTableLayouts) |
3601 | dumpMethodLocations(RD, NewMethodLocations, llvm::outs()); |
3602 | } |
3603 | |
3604 | void MicrosoftVTableContext::dumpMethodLocations( |
3605 | const CXXRecordDecl *RD, const MethodVFTableLocationsTy &NewMethods, |
3606 | raw_ostream &Out) { |
3607 | |
3608 | |
3609 | std::map<MethodVFTableLocation, std::string> IndicesMap; |
3610 | bool HasNonzeroOffset = false; |
3611 | |
3612 | for (const auto &I : NewMethods) { |
3613 | const CXXMethodDecl *MD = cast<const CXXMethodDecl>(I.first.getDecl()); |
3614 | isVirtual()", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 3614, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(MD->isVirtual()); |
3615 | |
3616 | std::string MethodName = PredefinedExpr::ComputeName( |
3617 | PredefinedExpr::PrettyFunctionNoVirtual, MD); |
3618 | |
3619 | if (isa<CXXDestructorDecl>(MD)) { |
3620 | IndicesMap[I.second] = MethodName + " [scalar deleting]"; |
3621 | } else { |
3622 | IndicesMap[I.second] = MethodName; |
3623 | } |
3624 | |
3625 | if (!I.second.VFPtrOffset.isZero() || I.second.VBTableIndex != 0) |
3626 | HasNonzeroOffset = true; |
3627 | } |
3628 | |
3629 | |
3630 | if (!IndicesMap.empty()) { |
3631 | Out << "VFTable indices for "; |
3632 | Out << "'"; |
3633 | RD->printQualifiedName(Out); |
3634 | Out << "' (" << IndicesMap.size() |
3635 | << (IndicesMap.size() == 1 ? " entry" : " entries") << ").\n"; |
3636 | |
3637 | CharUnits LastVFPtrOffset = CharUnits::fromQuantity(-1); |
3638 | uint64_t LastVBIndex = 0; |
3639 | for (const auto &I : IndicesMap) { |
3640 | CharUnits VFPtrOffset = I.first.VFPtrOffset; |
3641 | uint64_t VBIndex = I.first.VBTableIndex; |
3642 | if (HasNonzeroOffset && |
3643 | (VFPtrOffset != LastVFPtrOffset || VBIndex != LastVBIndex)) { |
3644 | LastVBIndex || VFPtrOffset > LastVFPtrOffset", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 3644, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(VBIndex > LastVBIndex || VFPtrOffset > LastVFPtrOffset); |
3645 | Out << " -- accessible via "; |
3646 | if (VBIndex) |
3647 | Out << "vbtable index " << VBIndex << ", "; |
3648 | Out << "vfptr at offset " << VFPtrOffset.getQuantity() << " --\n"; |
3649 | LastVFPtrOffset = VFPtrOffset; |
3650 | LastVBIndex = VBIndex; |
3651 | } |
3652 | |
3653 | uint64_t VTableIndex = I.first.Index; |
3654 | const std::string &MethodName = I.second; |
3655 | Out << llvm::format("%4" PRIu64 " | ", VTableIndex) << MethodName << '\n'; |
3656 | } |
3657 | Out << '\n'; |
3658 | } |
3659 | |
3660 | Out.flush(); |
3661 | } |
3662 | |
3663 | const VirtualBaseInfo &MicrosoftVTableContext::computeVBTableRelatedInformation( |
3664 | const CXXRecordDecl *RD) { |
3665 | VirtualBaseInfo *VBI; |
3666 | |
3667 | { |
3668 | |
3669 | |
3670 | std::unique_ptr<VirtualBaseInfo> &Entry = VBaseInfo[RD]; |
3671 | if (Entry) |
3672 | return *Entry; |
3673 | Entry = llvm::make_unique<VirtualBaseInfo>(); |
3674 | VBI = Entry.get(); |
3675 | } |
3676 | |
3677 | computeVTablePaths(, RD, VBI->VBPtrPaths); |
3678 | |
3679 | |
3680 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
3681 | if (const CXXRecordDecl *VBPtrBase = Layout.getBaseSharingVBPtr()) { |
3682 | |
3683 | |
3684 | const VirtualBaseInfo &BaseInfo = |
3685 | computeVBTableRelatedInformation(VBPtrBase); |
3686 | VBI->VBTableIndices.insert(BaseInfo.VBTableIndices.begin(), |
3687 | BaseInfo.VBTableIndices.end()); |
3688 | } |
3689 | |
3690 | |
3691 | |
3692 | unsigned VBTableIndex = 1 + VBI->VBTableIndices.size(); |
3693 | for (const auto &VB : RD->vbases()) { |
3694 | const CXXRecordDecl *CurVBase = VB.getType()->getAsCXXRecordDecl(); |
3695 | if (!VBI->VBTableIndices.count(CurVBase)) |
3696 | VBI->VBTableIndices[CurVBase] = VBTableIndex++; |
3697 | } |
3698 | |
3699 | return *VBI; |
3700 | } |
3701 | |
3702 | unsigned MicrosoftVTableContext::getVBTableIndex(const CXXRecordDecl *Derived, |
3703 | const CXXRecordDecl *VBase) { |
3704 | const VirtualBaseInfo &VBInfo = computeVBTableRelatedInformation(Derived); |
3705 | assert(VBInfo.VBTableIndices.count(VBase)); |
3706 | return VBInfo.VBTableIndices.find(VBase)->second; |
3707 | } |
3708 | |
3709 | const VPtrInfoVector & |
3710 | MicrosoftVTableContext::enumerateVBTables(const CXXRecordDecl *RD) { |
3711 | return computeVBTableRelatedInformation(RD).VBPtrPaths; |
3712 | } |
3713 | |
3714 | const VPtrInfoVector & |
3715 | MicrosoftVTableContext::getVFPtrOffsets(const CXXRecordDecl *RD) { |
3716 | computeVTableRelatedInformation(RD); |
3717 | |
3718 | (0) . __assert_fail ("VFPtrLocations.count(RD) && \"Couldn't find vfptr locations\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 3718, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(VFPtrLocations.count(RD) && "Couldn't find vfptr locations"); |
3719 | return *VFPtrLocations[RD]; |
3720 | } |
3721 | |
3722 | const VTableLayout & |
3723 | MicrosoftVTableContext::getVFTableLayout(const CXXRecordDecl *RD, |
3724 | CharUnits VFPtrOffset) { |
3725 | computeVTableRelatedInformation(RD); |
3726 | |
3727 | VFTableIdTy id(RD, VFPtrOffset); |
3728 | (0) . __assert_fail ("VFTableLayouts.count(id) && \"Couldn't find a VFTable at this offset\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 3728, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(VFTableLayouts.count(id) && "Couldn't find a VFTable at this offset"); |
3729 | return *VFTableLayouts[id]; |
3730 | } |
3731 | |
3732 | MethodVFTableLocation |
3733 | MicrosoftVTableContext::getMethodVFTableLocation(GlobalDecl GD) { |
3734 | (0) . __assert_fail ("cast(GD.getDecl())->isVirtual() && \"Only use this method for virtual methods or dtors\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 3735, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(cast<CXXMethodDecl>(GD.getDecl())->isVirtual() && |
3735 | (0) . __assert_fail ("cast(GD.getDecl())->isVirtual() && \"Only use this method for virtual methods or dtors\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 3735, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Only use this method for virtual methods or dtors"); |
3736 | if (isa<CXXDestructorDecl>(GD.getDecl())) |
3737 | assert(GD.getDtorType() == Dtor_Deleting); |
3738 | |
3739 | GD = GD.getCanonicalDecl(); |
3740 | |
3741 | MethodVFTableLocationsTy::iterator I = MethodVFTableLocations.find(GD); |
3742 | if (I != MethodVFTableLocations.end()) |
3743 | return I->second; |
3744 | |
3745 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
3746 | |
3747 | computeVTableRelatedInformation(RD); |
3748 | |
3749 | I = MethodVFTableLocations.find(GD); |
3750 | (0) . __assert_fail ("I != MethodVFTableLocations.end() && \"Did not find index!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/VTableBuilder.cpp", 3750, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(I != MethodVFTableLocations.end() && "Did not find index!"); |
3751 | return I->second; |
3752 | } |
3753 | |