1 //===- VTTBuilder.h - C++ VTT layout builder --------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This contains code dealing with generation of the layout of virtual table
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_VTTBUILDER_H
15 #define LLVM_CLANG_AST_VTTBUILDER_H
17 #include "clang/AST/BaseSubobject.h"
18 #include "clang/AST/CharUnits.h"
19 #include "clang/Basic/LLVM.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/PointerIntPair.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
29 class ASTRecordLayout;
33 llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> BaseAndIsVirtual;
37 VTTVTable() = default;
38 VTTVTable(const CXXRecordDecl *Base, CharUnits BaseOffset, bool BaseIsVirtual)
39 : BaseAndIsVirtual(Base, BaseIsVirtual), BaseOffset(BaseOffset) {}
40 VTTVTable(BaseSubobject Base, bool BaseIsVirtual)
41 : BaseAndIsVirtual(Base.getBase(), BaseIsVirtual),
42 BaseOffset(Base.getBaseOffset()) {}
44 const CXXRecordDecl *getBase() const {
45 return BaseAndIsVirtual.getPointer();
48 CharUnits getBaseOffset() const {
52 bool isVirtual() const {
53 return BaseAndIsVirtual.getInt();
56 BaseSubobject getBaseSubobject() const {
57 return BaseSubobject(getBase(), getBaseOffset());
63 BaseSubobject VTableBase;
65 VTTComponent() = default;
66 VTTComponent(uint64_t VTableIndex, BaseSubobject VTableBase)
67 : VTableIndex(VTableIndex), VTableBase(VTableBase) {}
70 /// Class for building VTT layout information.
74 /// The most derived class for which we're building this vtable.
75 const CXXRecordDecl *MostDerivedClass;
77 using VTTVTablesVectorTy = SmallVector<VTTVTable, 64>;
80 VTTVTablesVectorTy VTTVTables;
82 using VTTComponentsVectorTy = SmallVector<VTTComponent, 64>;
84 /// The VTT components.
85 VTTComponentsVectorTy VTTComponents;
87 /// The AST record layout of the most derived class.
88 const ASTRecordLayout &MostDerivedClassLayout;
90 using VisitedVirtualBasesSetTy = llvm::SmallPtrSet<const CXXRecordDecl *, 4>;
92 using AddressPointsMapTy = llvm::DenseMap<BaseSubobject, uint64_t>;
94 /// The sub-VTT indices for the bases of the most derived class.
95 llvm::DenseMap<BaseSubobject, uint64_t> SubVTTIndicies;
97 /// The secondary virtual pointer indices of all subobjects of
98 /// the most derived class.
99 llvm::DenseMap<BaseSubobject, uint64_t> SecondaryVirtualPointerIndices;
101 /// Whether the VTT builder should generate LLVM IR for the VTT.
102 bool GenerateDefinition;
104 /// Add a vtable pointer to the VTT currently being built.
105 void AddVTablePointer(BaseSubobject Base, uint64_t VTableIndex,
106 const CXXRecordDecl *VTableClass);
108 /// Lay out the secondary VTTs of the given base subobject.
109 void LayoutSecondaryVTTs(BaseSubobject Base);
111 /// Lay out the secondary virtual pointers for the given base
114 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
115 /// or a direct or indirect base of a virtual base.
116 void LayoutSecondaryVirtualPointers(BaseSubobject Base,
117 bool BaseIsMorallyVirtual,
118 uint64_t VTableIndex,
119 const CXXRecordDecl *VTableClass,
120 VisitedVirtualBasesSetTy &VBases);
122 /// Lay out the secondary virtual pointers for the given base
124 void LayoutSecondaryVirtualPointers(BaseSubobject Base,
125 uint64_t VTableIndex);
127 /// Lay out the VTTs for the virtual base classes of the given
128 /// record declaration.
129 void LayoutVirtualVTTs(const CXXRecordDecl *RD,
130 VisitedVirtualBasesSetTy &VBases);
132 /// Lay out the VTT for the given subobject, including any
133 /// secondary VTTs, secondary virtual pointers and virtual VTTs.
134 void LayoutVTT(BaseSubobject Base, bool BaseIsVirtual);
137 VTTBuilder(ASTContext &Ctx, const CXXRecordDecl *MostDerivedClass,
138 bool GenerateDefinition);
140 // Returns a reference to the VTT components.
141 const VTTComponentsVectorTy &getVTTComponents() const {
142 return VTTComponents;
145 // Returns a reference to the VTT vtables.
146 const VTTVTablesVectorTy &getVTTVTables() const {
150 /// Returns a reference to the sub-VTT indices.
151 const llvm::DenseMap<BaseSubobject, uint64_t> &getSubVTTIndicies() const {
152 return SubVTTIndicies;
155 /// Returns a reference to the secondary virtual pointer indices.
156 const llvm::DenseMap<BaseSubobject, uint64_t> &
157 getSecondaryVirtualPointerIndices() const {
158 return SecondaryVirtualPointerIndices;
164 #endif // LLVM_CLANG_AST_VTTBUILDER_H