1 //===--- VTTBuilder.h - C++ VTT layout builder --------------------*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code dealing with generation of the layout of virtual table
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_AST_VTTBUILDER_H
16 #define LLVM_CLANG_AST_VTTBUILDER_H
18 #include "clang/AST/BaseSubobject.h"
19 #include "clang/AST/CXXInheritance.h"
20 #include "clang/AST/GlobalDecl.h"
21 #include "clang/AST/RecordLayout.h"
22 #include "clang/Basic/ABI.h"
23 #include "llvm/ADT/SetVector.h"
29 llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> BaseAndIsVirtual;
34 VTTVTable(const CXXRecordDecl *Base, CharUnits BaseOffset, bool BaseIsVirtual)
35 : BaseAndIsVirtual(Base, BaseIsVirtual), BaseOffset(BaseOffset) {}
36 VTTVTable(BaseSubobject Base, bool BaseIsVirtual)
37 : BaseAndIsVirtual(Base.getBase(), BaseIsVirtual),
38 BaseOffset(Base.getBaseOffset()) {}
40 const CXXRecordDecl *getBase() const {
41 return BaseAndIsVirtual.getPointer();
44 CharUnits getBaseOffset() const {
48 bool isVirtual() const {
49 return BaseAndIsVirtual.getInt();
52 BaseSubobject getBaseSubobject() const {
53 return BaseSubobject(getBase(), getBaseOffset());
59 BaseSubobject VTableBase;
62 VTTComponent(uint64_t VTableIndex, BaseSubobject VTableBase)
63 : VTableIndex(VTableIndex), VTableBase(VTableBase) {}
66 /// VTT builder - Class for building VTT layout information.
71 /// MostDerivedClass - The most derived class for which we're building this
73 const CXXRecordDecl *MostDerivedClass;
75 typedef SmallVector<VTTVTable, 64> VTTVTablesVectorTy;
77 /// VTTVTables - The VTT vtables.
78 VTTVTablesVectorTy VTTVTables;
80 typedef SmallVector<VTTComponent, 64> VTTComponentsVectorTy;
82 /// VTTComponents - The VTT components.
83 VTTComponentsVectorTy VTTComponents;
85 /// MostDerivedClassLayout - the AST record layout of the most derived class.
86 const ASTRecordLayout &MostDerivedClassLayout;
88 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
90 typedef llvm::DenseMap<BaseSubobject, uint64_t> AddressPointsMapTy;
92 /// SubVTTIndicies - The sub-VTT indices for the bases of the most derived
94 llvm::DenseMap<BaseSubobject, uint64_t> SubVTTIndicies;
96 /// SecondaryVirtualPointerIndices - The secondary virtual pointer indices of
97 /// all subobjects of the most derived class.
98 llvm::DenseMap<BaseSubobject, uint64_t> SecondaryVirtualPointerIndices;
100 /// GenerateDefinition - Whether the VTT builder should generate LLVM IR for
102 bool GenerateDefinition;
104 /// AddVTablePointer - Add a vtable pointer to the VTT currently being built.
105 void AddVTablePointer(BaseSubobject Base, uint64_t VTableIndex,
106 const CXXRecordDecl *VTableClass);
108 /// LayoutSecondaryVTTs - Lay out the secondary VTTs of the given base
110 void LayoutSecondaryVTTs(BaseSubobject Base);
112 /// LayoutSecondaryVirtualPointers - Lay out the secondary virtual pointers
113 /// for the given base subobject.
115 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
116 /// or a direct or indirect base of a virtual base.
117 void LayoutSecondaryVirtualPointers(BaseSubobject Base,
118 bool BaseIsMorallyVirtual,
119 uint64_t VTableIndex,
120 const CXXRecordDecl *VTableClass,
121 VisitedVirtualBasesSetTy &VBases);
123 /// LayoutSecondaryVirtualPointers - Lay out the secondary virtual pointers
124 /// for the given base subobject.
125 void LayoutSecondaryVirtualPointers(BaseSubobject Base,
126 uint64_t VTableIndex);
128 /// LayoutVirtualVTTs - Lay out the VTTs for the virtual base classes of the
129 /// given record decl.
130 void LayoutVirtualVTTs(const CXXRecordDecl *RD,
131 VisitedVirtualBasesSetTy &VBases);
133 /// LayoutVTT - Will lay out the VTT for the given subobject, including any
134 /// secondary VTTs, secondary virtual pointers and virtual VTTs.
135 void LayoutVTT(BaseSubobject Base, bool BaseIsVirtual);
138 VTTBuilder(ASTContext &Ctx, const CXXRecordDecl *MostDerivedClass,
139 bool GenerateDefinition);
141 // getVTTComponents - Returns a reference to the VTT components.
142 const VTTComponentsVectorTy &getVTTComponents() const {
143 return VTTComponents;
146 // getVTTVTables - Returns a reference to the VTT vtables.
147 const VTTVTablesVectorTy &getVTTVTables() const {
151 /// getSubVTTIndicies - Returns a reference to the sub-VTT indices.
152 const llvm::DenseMap<BaseSubobject, uint64_t> &getSubVTTIndicies() const {
153 return SubVTTIndicies;
156 /// getSecondaryVirtualPointerIndices - Returns a reference to the secondary
157 /// virtual pointer indices.
158 const llvm::DenseMap<BaseSubobject, uint64_t> &
159 getSecondaryVirtualPointerIndices() const {
160 return SecondaryVirtualPointerIndices;