/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/AST/VTableBuilder.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- VTableBuilder.h - C++ vtable layout builder --------------*- C++ -*-=// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This contains code dealing with generation of the layout of virtual tables. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #ifndef LLVM_CLANG_AST_VTABLEBUILDER_H |
14 | | #define LLVM_CLANG_AST_VTABLEBUILDER_H |
15 | | |
16 | | #include "clang/AST/BaseSubobject.h" |
17 | | #include "clang/AST/CXXInheritance.h" |
18 | | #include "clang/AST/GlobalDecl.h" |
19 | | #include "clang/AST/RecordLayout.h" |
20 | | #include "clang/Basic/ABI.h" |
21 | | #include "llvm/ADT/DenseMap.h" |
22 | | #include <memory> |
23 | | #include <utility> |
24 | | |
25 | | namespace clang { |
26 | | class CXXRecordDecl; |
27 | | |
28 | | /// Represents a single component in a vtable. |
29 | | class VTableComponent { |
30 | | public: |
31 | | enum Kind { |
32 | | CK_VCallOffset, |
33 | | CK_VBaseOffset, |
34 | | CK_OffsetToTop, |
35 | | CK_RTTI, |
36 | | CK_FunctionPointer, |
37 | | |
38 | | /// A pointer to the complete destructor. |
39 | | CK_CompleteDtorPointer, |
40 | | |
41 | | /// A pointer to the deleting destructor. |
42 | | CK_DeletingDtorPointer, |
43 | | |
44 | | /// An entry that is never used. |
45 | | /// |
46 | | /// In some cases, a vtable function pointer will end up never being |
47 | | /// called. Such vtable function pointers are represented as a |
48 | | /// CK_UnusedFunctionPointer. |
49 | | CK_UnusedFunctionPointer |
50 | | }; |
51 | | |
52 | | VTableComponent() = default; |
53 | | |
54 | 780 | static VTableComponent MakeVCallOffset(CharUnits Offset) { |
55 | 780 | return VTableComponent(CK_VCallOffset, Offset); |
56 | 780 | } |
57 | | |
58 | 1.56k | static VTableComponent MakeVBaseOffset(CharUnits Offset) { |
59 | 1.56k | return VTableComponent(CK_VBaseOffset, Offset); |
60 | 1.56k | } |
61 | | |
62 | 4.94k | static VTableComponent MakeOffsetToTop(CharUnits Offset) { |
63 | 4.94k | return VTableComponent(CK_OffsetToTop, Offset); |
64 | 4.94k | } |
65 | | |
66 | 5.42k | static VTableComponent MakeRTTI(const CXXRecordDecl *RD) { |
67 | 5.42k | return VTableComponent(CK_RTTI, reinterpret_cast<uintptr_t>(RD)); |
68 | 5.42k | } |
69 | | |
70 | 12.2k | static VTableComponent MakeFunction(const CXXMethodDecl *MD) { |
71 | 12.2k | assert(!isa<CXXDestructorDecl>(MD) && |
72 | 12.2k | "Don't use MakeFunction with destructors!"); |
73 | | |
74 | 12.2k | return VTableComponent(CK_FunctionPointer, |
75 | 12.2k | reinterpret_cast<uintptr_t>(MD)); |
76 | 12.2k | } |
77 | | |
78 | 2.77k | static VTableComponent MakeCompleteDtor(const CXXDestructorDecl *DD) { |
79 | 2.77k | return VTableComponent(CK_CompleteDtorPointer, |
80 | 2.77k | reinterpret_cast<uintptr_t>(DD)); |
81 | 2.77k | } |
82 | | |
83 | 3.13k | static VTableComponent MakeDeletingDtor(const CXXDestructorDecl *DD) { |
84 | 3.13k | return VTableComponent(CK_DeletingDtorPointer, |
85 | 3.13k | reinterpret_cast<uintptr_t>(DD)); |
86 | 3.13k | } |
87 | | |
88 | 43 | static VTableComponent MakeUnusedFunction(const CXXMethodDecl *MD) { |
89 | 43 | assert(!isa<CXXDestructorDecl>(MD) && |
90 | 43 | "Don't use MakeUnusedFunction with destructors!"); |
91 | 43 | return VTableComponent(CK_UnusedFunctionPointer, |
92 | 43 | reinterpret_cast<uintptr_t>(MD)); |
93 | 43 | } |
94 | | |
95 | | /// Get the kind of this vtable component. |
96 | 99.8k | Kind getKind() const { |
97 | 99.8k | return (Kind)(Value & 0x7); |
98 | 99.8k | } |
99 | | |
100 | 868 | CharUnits getVCallOffset() const { |
101 | 868 | assert(getKind() == CK_VCallOffset && "Invalid component kind!"); |
102 | | |
103 | 868 | return getOffset(); |
104 | 868 | } |
105 | | |
106 | 1.91k | CharUnits getVBaseOffset() const { |
107 | 1.91k | assert(getKind() == CK_VBaseOffset && "Invalid component kind!"); |
108 | | |
109 | 1.91k | return getOffset(); |
110 | 1.91k | } |
111 | | |
112 | 2.99k | CharUnits getOffsetToTop() const { |
113 | 2.99k | assert(getKind() == CK_OffsetToTop && "Invalid component kind!"); |
114 | | |
115 | 2.99k | return getOffset(); |
116 | 2.99k | } |
117 | | |
118 | 1.00k | const CXXRecordDecl *getRTTIDecl() const { |
119 | 1.00k | assert(isRTTIKind() && "Invalid component kind!"); |
120 | 1.00k | return reinterpret_cast<CXXRecordDecl *>(getPointer()); |
121 | 1.00k | } |
122 | | |
123 | 7.06k | const CXXMethodDecl *getFunctionDecl() const { |
124 | 7.06k | assert(isFunctionPointerKind() && "Invalid component kind!"); |
125 | 7.06k | if (isDestructorKind()) |
126 | 668 | return getDestructorDecl(); |
127 | 6.39k | return reinterpret_cast<CXXMethodDecl *>(getPointer()); |
128 | 6.39k | } |
129 | | |
130 | 2.81k | const CXXDestructorDecl *getDestructorDecl() const { |
131 | 2.81k | assert(isDestructorKind() && "Invalid component kind!"); |
132 | 2.81k | return reinterpret_cast<CXXDestructorDecl *>(getPointer()); |
133 | 2.81k | } |
134 | | |
135 | 26 | const CXXMethodDecl *getUnusedFunctionDecl() const { |
136 | 26 | assert(getKind() == CK_UnusedFunctionPointer && "Invalid component kind!"); |
137 | 26 | return reinterpret_cast<CXXMethodDecl *>(getPointer()); |
138 | 26 | } |
139 | | |
140 | 9.87k | bool isDestructorKind() const { return isDestructorKind(getKind()); } |
141 | | |
142 | 3.25k | bool isUsedFunctionPointerKind() const { |
143 | 3.25k | return isUsedFunctionPointerKind(getKind()); |
144 | 3.25k | } |
145 | | |
146 | 16.2k | bool isFunctionPointerKind() const { |
147 | 16.2k | return isFunctionPointerKind(getKind()); |
148 | 16.2k | } |
149 | | |
150 | 4.21k | bool isRTTIKind() const { return isRTTIKind(getKind()); } |
151 | | |
152 | 160 | GlobalDecl getGlobalDecl() const { |
153 | 160 | assert(isUsedFunctionPointerKind() && |
154 | 160 | "GlobalDecl can be created only from virtual function"); |
155 | | |
156 | 160 | auto *DtorDecl = dyn_cast<CXXDestructorDecl>(getFunctionDecl()); |
157 | 160 | switch (getKind()) { |
158 | 89 | case CK_FunctionPointer: |
159 | 89 | return GlobalDecl(getFunctionDecl()); |
160 | 67 | case CK_CompleteDtorPointer: |
161 | 67 | return GlobalDecl(DtorDecl, CXXDtorType::Dtor_Complete); |
162 | 4 | case CK_DeletingDtorPointer: |
163 | 4 | return GlobalDecl(DtorDecl, CXXDtorType::Dtor_Deleting); |
164 | 0 | case CK_VCallOffset: |
165 | 0 | case CK_VBaseOffset: |
166 | 0 | case CK_OffsetToTop: |
167 | 0 | case CK_RTTI: |
168 | 0 | case CK_UnusedFunctionPointer: |
169 | 0 | llvm_unreachable("Only function pointers kinds"); |
170 | 0 | } |
171 | 0 | llvm_unreachable("Should already return"); |
172 | 0 | } |
173 | | |
174 | | private: |
175 | 34.4k | static bool isFunctionPointerKind(Kind ComponentKind) { |
176 | 34.4k | return isUsedFunctionPointerKind(ComponentKind) || |
177 | 69 | ComponentKind == CK_UnusedFunctionPointer; |
178 | 34.4k | } |
179 | 37.7k | static bool isUsedFunctionPointerKind(Kind ComponentKind) { |
180 | 37.7k | return ComponentKind == CK_FunctionPointer || |
181 | 11.6k | isDestructorKind(ComponentKind); |
182 | 37.7k | } |
183 | 21.5k | static bool isDestructorKind(Kind ComponentKind) { |
184 | 21.5k | return ComponentKind == CK_CompleteDtorPointer || |
185 | 14.8k | ComponentKind == CK_DeletingDtorPointer; |
186 | 21.5k | } |
187 | 27.8k | static bool isRTTIKind(Kind ComponentKind) { |
188 | 27.8k | return ComponentKind == CK_RTTI; |
189 | 27.8k | } |
190 | | |
191 | 7.29k | VTableComponent(Kind ComponentKind, CharUnits Offset) { |
192 | 7.29k | assert((ComponentKind == CK_VCallOffset || |
193 | 7.29k | ComponentKind == CK_VBaseOffset || |
194 | 7.29k | ComponentKind == CK_OffsetToTop) && "Invalid component kind!"); |
195 | 7.29k | assert(Offset.getQuantity() < (1LL << 56) && "Offset is too big!"); |
196 | 7.29k | assert(Offset.getQuantity() >= -(1LL << 56) && "Offset is too small!"); |
197 | | |
198 | 7.29k | Value = (uint64_t(Offset.getQuantity()) << 3) | ComponentKind; |
199 | 7.29k | } |
200 | | |
201 | 23.6k | VTableComponent(Kind ComponentKind, uintptr_t Ptr) { |
202 | 23.6k | assert((isRTTIKind(ComponentKind) || isFunctionPointerKind(ComponentKind)) && |
203 | 23.6k | "Invalid component kind!"); |
204 | | |
205 | 23.6k | assert((Ptr & 7) == 0 && "Pointer not sufficiently aligned!"); |
206 | | |
207 | 23.6k | Value = Ptr | ComponentKind; |
208 | 23.6k | } |
209 | | |
210 | 5.77k | CharUnits getOffset() const { |
211 | 5.77k | assert((getKind() == CK_VCallOffset || getKind() == CK_VBaseOffset || |
212 | 5.77k | getKind() == CK_OffsetToTop) && "Invalid component kind!"); |
213 | | |
214 | 5.77k | return CharUnits::fromQuantity(Value >> 3); |
215 | 5.77k | } |
216 | | |
217 | 10.2k | uintptr_t getPointer() const { |
218 | 10.2k | assert((getKind() == CK_RTTI || isFunctionPointerKind()) && |
219 | 10.2k | "Invalid component kind!"); |
220 | | |
221 | 10.2k | return static_cast<uintptr_t>(Value & ~7ULL); |
222 | 10.2k | } |
223 | | |
224 | | /// The kind is stored in the lower 3 bits of the value. For offsets, we |
225 | | /// make use of the facts that classes can't be larger than 2^55 bytes, |
226 | | /// so we store the offset in the lower part of the 61 bits that remain. |
227 | | /// (The reason that we're not simply using a PointerIntPair here is that we |
228 | | /// need the offsets to be 64-bit, even when on a 32-bit machine). |
229 | | int64_t Value; |
230 | | }; |
231 | | |
232 | | class VTableLayout { |
233 | | public: |
234 | | typedef std::pair<uint64_t, ThunkInfo> VTableThunkTy; |
235 | | struct AddressPointLocation { |
236 | | unsigned VTableIndex, AddressPointIndex; |
237 | | }; |
238 | | typedef llvm::DenseMap<BaseSubobject, AddressPointLocation> |
239 | | AddressPointsMapTy; |
240 | | |
241 | | // Mapping between the VTable index and address point index. This is useful |
242 | | // when you don't care about the base subobjects and only want the address |
243 | | // point for a given vtable index. |
244 | | typedef llvm::SmallVector<unsigned, 4> AddressPointsIndexMapTy; |
245 | | |
246 | | private: |
247 | | // Stores the component indices of the first component of each virtual table in |
248 | | // the virtual table group. To save a little memory in the common case where |
249 | | // the vtable group contains a single vtable, an empty vector here represents |
250 | | // the vector {0}. |
251 | | OwningArrayRef<size_t> VTableIndices; |
252 | | |
253 | | OwningArrayRef<VTableComponent> VTableComponents; |
254 | | |
255 | | /// Contains thunks needed by vtables, sorted by indices. |
256 | | OwningArrayRef<VTableThunkTy> VTableThunks; |
257 | | |
258 | | /// Address points for all vtables. |
259 | | AddressPointsMapTy AddressPoints; |
260 | | |
261 | | /// Address points for all vtable indices. |
262 | | AddressPointsIndexMapTy AddressPointIndices; |
263 | | |
264 | | public: |
265 | | VTableLayout(ArrayRef<size_t> VTableIndices, |
266 | | ArrayRef<VTableComponent> VTableComponents, |
267 | | ArrayRef<VTableThunkTy> VTableThunks, |
268 | | const AddressPointsMapTy &AddressPoints); |
269 | | ~VTableLayout(); |
270 | | |
271 | 20.1k | ArrayRef<VTableComponent> vtable_components() const { |
272 | 20.1k | return VTableComponents; |
273 | 20.1k | } |
274 | | |
275 | 6.97k | ArrayRef<VTableThunkTy> vtable_thunks() const { |
276 | 6.97k | return VTableThunks; |
277 | 6.97k | } |
278 | | |
279 | 3.16k | AddressPointLocation getAddressPoint(BaseSubobject Base) const { |
280 | 3.16k | assert(AddressPoints.count(Base) && "Did not find address point!"); |
281 | 3.16k | return AddressPoints.find(Base)->second; |
282 | 3.16k | } |
283 | | |
284 | 374 | const AddressPointsMapTy &getAddressPoints() const { |
285 | 374 | return AddressPoints; |
286 | 374 | } |
287 | | |
288 | 2.73k | const AddressPointsIndexMapTy &getAddressPointIndices() const { |
289 | 2.73k | return AddressPointIndices; |
290 | 2.73k | } |
291 | | |
292 | 6.01k | size_t getNumVTables() const { |
293 | 6.01k | if (VTableIndices.empty()) |
294 | 5.23k | return 1; |
295 | 773 | return VTableIndices.size(); |
296 | 773 | } |
297 | | |
298 | 3.41k | size_t getVTableOffset(size_t i) const { |
299 | 3.41k | if (VTableIndices.empty()) { |
300 | 2.45k | assert(i == 0); |
301 | 2.45k | return 0; |
302 | 2.45k | } |
303 | 958 | return VTableIndices[i]; |
304 | 958 | } |
305 | | |
306 | 7.09k | size_t getVTableSize(size_t i) const { |
307 | 7.09k | if (VTableIndices.empty()) { |
308 | 5.23k | assert(i == 0); |
309 | 5.23k | return vtable_components().size(); |
310 | 5.23k | } |
311 | | |
312 | 1.86k | size_t thisIndex = VTableIndices[i]; |
313 | 1.86k | size_t nextIndex = (i + 1 == VTableIndices.size()) |
314 | 773 | ? vtable_components().size() |
315 | 1.08k | : VTableIndices[i + 1]; |
316 | 1.86k | return nextIndex - thisIndex; |
317 | 1.86k | } |
318 | | }; |
319 | | |
320 | | class VTableContextBase { |
321 | | public: |
322 | | typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy; |
323 | | |
324 | 57.3k | bool isMicrosoft() const { return IsMicrosoftABI; } |
325 | | |
326 | 27.4k | virtual ~VTableContextBase() {} |
327 | | |
328 | | protected: |
329 | | typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy; |
330 | | |
331 | | /// Contains all thunks that a given method decl will need. |
332 | | ThunksMapTy Thunks; |
333 | | |
334 | | /// Compute and store all vtable related information (vtable layout, vbase |
335 | | /// offset offsets, thunks etc) for the given record decl. |
336 | | virtual void computeVTableRelatedInformation(const CXXRecordDecl *RD) = 0; |
337 | | |
338 | 31.4k | VTableContextBase(bool MS) : IsMicrosoftABI(MS) {} |
339 | | |
340 | | public: |
341 | 3.33k | virtual const ThunkInfoVectorTy *getThunkInfo(GlobalDecl GD) { |
342 | 3.33k | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()->getCanonicalDecl()); |
343 | 3.33k | computeVTableRelatedInformation(MD->getParent()); |
344 | | |
345 | | // This assumes that all the destructors present in the vtable |
346 | | // use exactly the same set of thunks. |
347 | 3.33k | ThunksMapTy::const_iterator I = Thunks.find(MD); |
348 | 3.33k | if (I == Thunks.end()) { |
349 | | // We did not find a thunk for this method. |
350 | 2.91k | return nullptr; |
351 | 2.91k | } |
352 | | |
353 | 420 | return &I->second; |
354 | 420 | } |
355 | | |
356 | | bool IsMicrosoftABI; |
357 | | |
358 | | /// Determine whether this function should be assigned a vtable slot. |
359 | | static bool hasVtableSlot(const CXXMethodDecl *MD); |
360 | | }; |
361 | | |
362 | | class ItaniumVTableContext : public VTableContextBase { |
363 | | private: |
364 | | |
365 | | /// Contains the index (relative to the vtable address point) |
366 | | /// where the function pointer for a virtual function is stored. |
367 | | typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVTableIndicesTy; |
368 | | MethodVTableIndicesTy MethodVTableIndices; |
369 | | |
370 | | typedef llvm::DenseMap<const CXXRecordDecl *, |
371 | | std::unique_ptr<const VTableLayout>> |
372 | | VTableLayoutMapTy; |
373 | | VTableLayoutMapTy VTableLayouts; |
374 | | |
375 | | typedef std::pair<const CXXRecordDecl *, |
376 | | const CXXRecordDecl *> ClassPairTy; |
377 | | |
378 | | /// vtable offsets for offsets of virtual bases of a class. |
379 | | /// |
380 | | /// Contains the vtable offset (relative to the address point) in chars |
381 | | /// where the offsets for virtual bases of a class are stored. |
382 | | typedef llvm::DenseMap<ClassPairTy, CharUnits> |
383 | | VirtualBaseClassOffsetOffsetsMapTy; |
384 | | VirtualBaseClassOffsetOffsetsMapTy VirtualBaseClassOffsetOffsets; |
385 | | |
386 | | void computeVTableRelatedInformation(const CXXRecordDecl *RD) override; |
387 | | |
388 | | public: |
389 | | enum VTableComponentLayout { |
390 | | /// Components in the vtable are pointers to other structs/functions. |
391 | | Pointer, |
392 | | |
393 | | /// Components in the vtable are relative offsets between the vtable and the |
394 | | /// other structs/functions. |
395 | | Relative, |
396 | | }; |
397 | | |
398 | | ItaniumVTableContext(ASTContext &Context, |
399 | | VTableComponentLayout ComponentLayout = Pointer); |
400 | | ~ItaniumVTableContext() override; |
401 | | |
402 | 7.73k | const VTableLayout &getVTableLayout(const CXXRecordDecl *RD) { |
403 | 7.73k | computeVTableRelatedInformation(RD); |
404 | 7.73k | assert(VTableLayouts.count(RD) && "No layout for this record decl!"); |
405 | | |
406 | 7.73k | return *VTableLayouts[RD]; |
407 | 7.73k | } |
408 | | |
409 | | std::unique_ptr<VTableLayout> createConstructionVTableLayout( |
410 | | const CXXRecordDecl *MostDerivedClass, CharUnits MostDerivedClassOffset, |
411 | | bool MostDerivedClassIsVirtual, const CXXRecordDecl *LayoutClass); |
412 | | |
413 | | /// Locate a virtual function in the vtable. |
414 | | /// |
415 | | /// Return the index (relative to the vtable address point) where the |
416 | | /// function pointer for the given virtual function is stored. |
417 | | uint64_t getMethodVTableIndex(GlobalDecl GD); |
418 | | |
419 | | /// Return the offset in chars (relative to the vtable address point) where |
420 | | /// the offset of the virtual base that contains the given base is stored, |
421 | | /// otherwise, if no virtual base contains the given class, return 0. |
422 | | /// |
423 | | /// Base must be a virtual base class or an unambiguous base. |
424 | | CharUnits getVirtualBaseOffsetOffset(const CXXRecordDecl *RD, |
425 | | const CXXRecordDecl *VBase); |
426 | | |
427 | 49.2k | static bool classof(const VTableContextBase *VT) { |
428 | 49.2k | return !VT->isMicrosoft(); |
429 | 49.2k | } |
430 | | |
431 | 0 | VTableComponentLayout getVTableComponentLayout() const { |
432 | 0 | return ComponentLayout; |
433 | 0 | } |
434 | | |
435 | 0 | bool isPointerLayout() const { return ComponentLayout == Pointer; } |
436 | 36.8k | bool isRelativeLayout() const { return ComponentLayout == Relative; } |
437 | | |
438 | | private: |
439 | | VTableComponentLayout ComponentLayout; |
440 | | }; |
441 | | |
442 | | /// Holds information about the inheritance path to a virtual base or function |
443 | | /// table pointer. A record may contain as many vfptrs or vbptrs as there are |
444 | | /// base subobjects. |
445 | | struct VPtrInfo { |
446 | | typedef SmallVector<const CXXRecordDecl *, 1> BasePath; |
447 | | |
448 | | VPtrInfo(const CXXRecordDecl *RD) |
449 | 861 | : ObjectWithVPtr(RD), IntroducingObject(RD), NextBaseToMangle(RD) {} |
450 | | |
451 | | /// This is the most derived class that has this vptr at offset zero. When |
452 | | /// single inheritance is used, this is always the most derived class. If |
453 | | /// multiple inheritance is used, it may be any direct or indirect base. |
454 | | const CXXRecordDecl *ObjectWithVPtr; |
455 | | |
456 | | /// This is the class that introduced the vptr by declaring new virtual |
457 | | /// methods or virtual bases. |
458 | | const CXXRecordDecl *IntroducingObject; |
459 | | |
460 | | /// IntroducingObject is at this offset from its containing complete object or |
461 | | /// virtual base. |
462 | | CharUnits NonVirtualOffset; |
463 | | |
464 | | /// The bases from the inheritance path that got used to mangle the vbtable |
465 | | /// name. This is not really a full path like a CXXBasePath. It holds the |
466 | | /// subset of records that need to be mangled into the vbtable symbol name in |
467 | | /// order to get a unique name. |
468 | | BasePath MangledPath; |
469 | | |
470 | | /// The next base to push onto the mangled path if this path is ambiguous in a |
471 | | /// derived class. If it's null, then it's already been pushed onto the path. |
472 | | const CXXRecordDecl *NextBaseToMangle; |
473 | | |
474 | | /// The set of possibly indirect vbases that contain this vbtable. When a |
475 | | /// derived class indirectly inherits from the same vbase twice, we only keep |
476 | | /// vtables and their paths from the first instance. |
477 | | BasePath ContainingVBases; |
478 | | |
479 | | /// This holds the base classes path from the complete type to the first base |
480 | | /// with the given vfptr offset, in the base-to-derived order. Only used for |
481 | | /// vftables. |
482 | | BasePath PathToIntroducingObject; |
483 | | |
484 | | /// Static offset from the top of the most derived class to this vfptr, |
485 | | /// including any virtual base offset. Only used for vftables. |
486 | | CharUnits FullOffsetInMDC; |
487 | | |
488 | | /// The vptr is stored inside the non-virtual component of this virtual base. |
489 | 7.07k | const CXXRecordDecl *getVBaseWithVPtr() const { |
490 | 3.73k | return ContainingVBases.empty() ? nullptr : ContainingVBases.front()3.33k ; |
491 | 7.07k | } |
492 | | }; |
493 | | |
494 | | typedef SmallVector<std::unique_ptr<VPtrInfo>, 2> VPtrInfoVector; |
495 | | |
496 | | /// All virtual base related information about a given record decl. Includes |
497 | | /// information on all virtual base tables and the path components that are used |
498 | | /// to mangle them. |
499 | | struct VirtualBaseInfo { |
500 | | /// A map from virtual base to vbtable index for doing a conversion from the |
501 | | /// the derived class to the a base. |
502 | | llvm::DenseMap<const CXXRecordDecl *, unsigned> VBTableIndices; |
503 | | |
504 | | /// Information on all virtual base tables used when this record is the most |
505 | | /// derived class. |
506 | | VPtrInfoVector VBPtrPaths; |
507 | | }; |
508 | | |
509 | | struct MethodVFTableLocation { |
510 | | /// If nonzero, holds the vbtable index of the virtual base with the vfptr. |
511 | | uint64_t VBTableIndex; |
512 | | |
513 | | /// If nonnull, holds the last vbase which contains the vfptr that the |
514 | | /// method definition is adjusted to. |
515 | | const CXXRecordDecl *VBase; |
516 | | |
517 | | /// This is the offset of the vfptr from the start of the last vbase, or the |
518 | | /// complete type if there are no virtual bases. |
519 | | CharUnits VFPtrOffset; |
520 | | |
521 | | /// Method's index in the vftable. |
522 | | uint64_t Index; |
523 | | |
524 | | MethodVFTableLocation() |
525 | | : VBTableIndex(0), VBase(nullptr), VFPtrOffset(CharUnits::Zero()), |
526 | 1.30k | Index(0) {} |
527 | | |
528 | | MethodVFTableLocation(uint64_t VBTableIndex, const CXXRecordDecl *VBase, |
529 | | CharUnits VFPtrOffset, uint64_t Index) |
530 | | : VBTableIndex(VBTableIndex), VBase(VBase), VFPtrOffset(VFPtrOffset), |
531 | 1.30k | Index(Index) {} |
532 | | |
533 | 187 | bool operator<(const MethodVFTableLocation &other) const { |
534 | 187 | if (VBTableIndex != other.VBTableIndex) { |
535 | 8 | assert(VBase != other.VBase); |
536 | 8 | return VBTableIndex < other.VBTableIndex; |
537 | 8 | } |
538 | 179 | return std::tie(VFPtrOffset, Index) < |
539 | 179 | std::tie(other.VFPtrOffset, other.Index); |
540 | 179 | } |
541 | | }; |
542 | | |
543 | | class MicrosoftVTableContext : public VTableContextBase { |
544 | | public: |
545 | | |
546 | | private: |
547 | | ASTContext &Context; |
548 | | |
549 | | typedef llvm::DenseMap<GlobalDecl, MethodVFTableLocation> |
550 | | MethodVFTableLocationsTy; |
551 | | MethodVFTableLocationsTy MethodVFTableLocations; |
552 | | |
553 | | typedef llvm::DenseMap<const CXXRecordDecl *, std::unique_ptr<VPtrInfoVector>> |
554 | | VFPtrLocationsMapTy; |
555 | | VFPtrLocationsMapTy VFPtrLocations; |
556 | | |
557 | | typedef std::pair<const CXXRecordDecl *, CharUnits> VFTableIdTy; |
558 | | typedef llvm::DenseMap<VFTableIdTy, std::unique_ptr<const VTableLayout>> |
559 | | VFTableLayoutMapTy; |
560 | | VFTableLayoutMapTy VFTableLayouts; |
561 | | |
562 | | llvm::DenseMap<const CXXRecordDecl *, std::unique_ptr<VirtualBaseInfo>> |
563 | | VBaseInfo; |
564 | | |
565 | | void enumerateVFPtrs(const CXXRecordDecl *ForClass, VPtrInfoVector &Result); |
566 | | |
567 | | void computeVTableRelatedInformation(const CXXRecordDecl *RD) override; |
568 | | |
569 | | void dumpMethodLocations(const CXXRecordDecl *RD, |
570 | | const MethodVFTableLocationsTy &NewMethods, |
571 | | raw_ostream &); |
572 | | |
573 | | const VirtualBaseInfo & |
574 | | computeVBTableRelatedInformation(const CXXRecordDecl *RD); |
575 | | |
576 | | void computeVTablePaths(bool ForVBTables, const CXXRecordDecl *RD, |
577 | | VPtrInfoVector &Paths); |
578 | | |
579 | | public: |
580 | | MicrosoftVTableContext(ASTContext &Context) |
581 | 727 | : VTableContextBase(/*MS=*/true), Context(Context) {} |
582 | | |
583 | | ~MicrosoftVTableContext() override; |
584 | | |
585 | | const VPtrInfoVector &getVFPtrOffsets(const CXXRecordDecl *RD); |
586 | | |
587 | | const VTableLayout &getVFTableLayout(const CXXRecordDecl *RD, |
588 | | CharUnits VFPtrOffset); |
589 | | |
590 | | MethodVFTableLocation getMethodVFTableLocation(GlobalDecl GD); |
591 | | |
592 | 508 | const ThunkInfoVectorTy *getThunkInfo(GlobalDecl GD) override { |
593 | | // Complete destructors don't have a slot in a vftable, so no thunks needed. |
594 | 508 | if (isa<CXXDestructorDecl>(GD.getDecl()) && |
595 | 291 | GD.getDtorType() == Dtor_Complete) |
596 | 77 | return nullptr; |
597 | 431 | return VTableContextBase::getThunkInfo(GD); |
598 | 431 | } |
599 | | |
600 | | /// Returns the index of VBase in the vbtable of Derived. |
601 | | /// VBase must be a morally virtual base of Derived. |
602 | | /// The vbtable is an array of i32 offsets. The first entry is a self entry, |
603 | | /// and the rest are offsets from the vbptr to virtual bases. |
604 | | unsigned getVBTableIndex(const CXXRecordDecl *Derived, |
605 | | const CXXRecordDecl *VBase); |
606 | | |
607 | | const VPtrInfoVector &enumerateVBTables(const CXXRecordDecl *RD); |
608 | | |
609 | 8.10k | static bool classof(const VTableContextBase *VT) { return VT->isMicrosoft(); } |
610 | | }; |
611 | | |
612 | | } // namespace clang |
613 | | |
614 | | #endif |