clang API Documentation

VTTBuilder.h
Go to the documentation of this file.
00001 //===--- VTTBuilder.h - C++ VTT layout builder --------------------*- C++ -*-=//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This contains code dealing with generation of the layout of virtual table
00011 // tables (VTT).
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_AST_VTTBUILDER_H
00016 #define LLVM_CLANG_AST_VTTBUILDER_H
00017 
00018 #include "clang/AST/BaseSubobject.h"
00019 #include "clang/AST/CXXInheritance.h"
00020 #include "clang/AST/GlobalDecl.h"
00021 #include "clang/AST/RecordLayout.h"
00022 #include "clang/Basic/ABI.h"
00023 #include "llvm/ADT/SetVector.h"
00024 #include <utility>
00025 
00026 namespace clang {
00027 
00028 class VTTVTable {
00029   llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> BaseAndIsVirtual;
00030   CharUnits BaseOffset;
00031 
00032 public:
00033   VTTVTable() {}
00034   VTTVTable(const CXXRecordDecl *Base, CharUnits BaseOffset, bool BaseIsVirtual)
00035     : BaseAndIsVirtual(Base, BaseIsVirtual), BaseOffset(BaseOffset) {}
00036   VTTVTable(BaseSubobject Base, bool BaseIsVirtual)
00037     : BaseAndIsVirtual(Base.getBase(), BaseIsVirtual),
00038       BaseOffset(Base.getBaseOffset()) {}
00039 
00040   const CXXRecordDecl *getBase() const {
00041     return BaseAndIsVirtual.getPointer();
00042   }
00043 
00044   CharUnits getBaseOffset() const {
00045     return BaseOffset;
00046   }
00047 
00048   bool isVirtual() const {
00049     return BaseAndIsVirtual.getInt();
00050   }
00051 
00052   BaseSubobject getBaseSubobject() const {
00053     return BaseSubobject(getBase(), getBaseOffset());
00054   }
00055 };
00056 
00057 struct VTTComponent {
00058   uint64_t VTableIndex;
00059   BaseSubobject VTableBase;
00060 
00061   VTTComponent() {}
00062   VTTComponent(uint64_t VTableIndex, BaseSubobject VTableBase)
00063     : VTableIndex(VTableIndex), VTableBase(VTableBase) {}
00064 };
00065 
00066 /// VTT builder - Class for building VTT layout information.
00067 class VTTBuilder {
00068   
00069   ASTContext &Ctx;
00070 
00071   /// MostDerivedClass - The most derived class for which we're building this
00072   /// vtable.
00073   const CXXRecordDecl *MostDerivedClass;
00074 
00075   typedef SmallVector<VTTVTable, 64> VTTVTablesVectorTy;
00076   
00077   /// VTTVTables - The VTT vtables.
00078   VTTVTablesVectorTy VTTVTables;
00079   
00080   typedef SmallVector<VTTComponent, 64> VTTComponentsVectorTy;
00081   
00082   /// VTTComponents - The VTT components.
00083   VTTComponentsVectorTy VTTComponents;
00084   
00085   /// MostDerivedClassLayout - the AST record layout of the most derived class.
00086   const ASTRecordLayout &MostDerivedClassLayout;
00087 
00088   typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
00089 
00090   typedef llvm::DenseMap<BaseSubobject, uint64_t> AddressPointsMapTy;
00091 
00092   /// SubVTTIndicies - The sub-VTT indices for the bases of the most derived
00093   /// class.
00094   llvm::DenseMap<BaseSubobject, uint64_t> SubVTTIndicies;
00095 
00096   /// SecondaryVirtualPointerIndices - The secondary virtual pointer indices of
00097   /// all subobjects of the most derived class.
00098   llvm::DenseMap<BaseSubobject, uint64_t> SecondaryVirtualPointerIndices;
00099 
00100   /// GenerateDefinition - Whether the VTT builder should generate LLVM IR for
00101   /// the VTT.
00102   bool GenerateDefinition;
00103 
00104   /// AddVTablePointer - Add a vtable pointer to the VTT currently being built.
00105   ///
00106   /// \param AddressPoints - If the vtable is a construction vtable, this has
00107   /// the address points for it.
00108   void AddVTablePointer(BaseSubobject Base, uint64_t VTableIndex,
00109                         const CXXRecordDecl *VTableClass);
00110                         
00111   /// LayoutSecondaryVTTs - Lay out the secondary VTTs of the given base 
00112   /// subobject.
00113   void LayoutSecondaryVTTs(BaseSubobject Base);
00114   
00115   /// LayoutSecondaryVirtualPointers - Lay out the secondary virtual pointers
00116   /// for the given base subobject.
00117   ///
00118   /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
00119   /// or a direct or indirect base of a virtual base.
00120   ///
00121   /// \param AddressPoints - If the vtable is a construction vtable, this has
00122   /// the address points for it.
00123   void LayoutSecondaryVirtualPointers(BaseSubobject Base, 
00124                                       bool BaseIsMorallyVirtual,
00125                                       uint64_t VTableIndex,
00126                                       const CXXRecordDecl *VTableClass,
00127                                       VisitedVirtualBasesSetTy &VBases);
00128   
00129   /// LayoutSecondaryVirtualPointers - Lay out the secondary virtual pointers
00130   /// for the given base subobject.
00131   ///
00132   /// \param AddressPoints - If the vtable is a construction vtable, this has
00133   /// the address points for it.
00134   void LayoutSecondaryVirtualPointers(BaseSubobject Base, 
00135                                       uint64_t VTableIndex);
00136 
00137   /// LayoutVirtualVTTs - Lay out the VTTs for the virtual base classes of the
00138   /// given record decl.
00139   void LayoutVirtualVTTs(const CXXRecordDecl *RD,
00140                          VisitedVirtualBasesSetTy &VBases);
00141   
00142   /// LayoutVTT - Will lay out the VTT for the given subobject, including any
00143   /// secondary VTTs, secondary virtual pointers and virtual VTTs.
00144   void LayoutVTT(BaseSubobject Base, bool BaseIsVirtual);
00145   
00146 public:
00147   VTTBuilder(ASTContext &Ctx, const CXXRecordDecl *MostDerivedClass,
00148              bool GenerateDefinition);
00149 
00150   // getVTTComponents - Returns a reference to the VTT components.
00151   const VTTComponentsVectorTy &getVTTComponents() const {
00152     return VTTComponents;
00153   }
00154   
00155   // getVTTVTables - Returns a reference to the VTT vtables.
00156   const VTTVTablesVectorTy &getVTTVTables() const {
00157     return VTTVTables;
00158   }
00159   
00160   /// getSubVTTIndicies - Returns a reference to the sub-VTT indices.
00161   const llvm::DenseMap<BaseSubobject, uint64_t> &getSubVTTIndicies() const {
00162     return SubVTTIndicies;
00163   }
00164   
00165   /// getSecondaryVirtualPointerIndices - Returns a reference to the secondary
00166   /// virtual pointer indices.
00167   const llvm::DenseMap<BaseSubobject, uint64_t> &
00168   getSecondaryVirtualPointerIndices() const {
00169     return SecondaryVirtualPointerIndices;
00170   }
00171 
00172 };
00173 
00174 }
00175 
00176 #endif