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