1*67e74705SXin Li //===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===// 2*67e74705SXin Li // 3*67e74705SXin Li // The LLVM Compiler Infrastructure 4*67e74705SXin Li // 5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source 6*67e74705SXin Li // License. See LICENSE.TXT for details. 7*67e74705SXin Li // 8*67e74705SXin Li //===----------------------------------------------------------------------===// 9*67e74705SXin Li // 10*67e74705SXin Li // This contains code dealing with C++ code generation of virtual tables. 11*67e74705SXin Li // 12*67e74705SXin Li //===----------------------------------------------------------------------===// 13*67e74705SXin Li 14*67e74705SXin Li #ifndef LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H 15*67e74705SXin Li #define LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H 16*67e74705SXin Li 17*67e74705SXin Li #include "clang/AST/BaseSubobject.h" 18*67e74705SXin Li #include "clang/AST/CharUnits.h" 19*67e74705SXin Li #include "clang/AST/GlobalDecl.h" 20*67e74705SXin Li #include "clang/AST/VTableBuilder.h" 21*67e74705SXin Li #include "clang/Basic/ABI.h" 22*67e74705SXin Li #include "llvm/ADT/DenseMap.h" 23*67e74705SXin Li #include "llvm/IR/GlobalVariable.h" 24*67e74705SXin Li 25*67e74705SXin Li namespace clang { 26*67e74705SXin Li class CXXRecordDecl; 27*67e74705SXin Li 28*67e74705SXin Li namespace CodeGen { 29*67e74705SXin Li class CodeGenModule; 30*67e74705SXin Li 31*67e74705SXin Li class CodeGenVTables { 32*67e74705SXin Li CodeGenModule &CGM; 33*67e74705SXin Li 34*67e74705SXin Li VTableContextBase *VTContext; 35*67e74705SXin Li 36*67e74705SXin Li /// VTableAddressPointsMapTy - Address points for a single vtable. 37*67e74705SXin Li typedef llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPointsMapTy; 38*67e74705SXin Li 39*67e74705SXin Li typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy; 40*67e74705SXin Li typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy; 41*67e74705SXin Li 42*67e74705SXin Li /// SubVTTIndicies - Contains indices into the various sub-VTTs. 43*67e74705SXin Li SubVTTIndiciesMapTy SubVTTIndicies; 44*67e74705SXin Li 45*67e74705SXin Li typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> 46*67e74705SXin Li SecondaryVirtualPointerIndicesMapTy; 47*67e74705SXin Li 48*67e74705SXin Li /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer 49*67e74705SXin Li /// indices. 50*67e74705SXin Li SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices; 51*67e74705SXin Li 52*67e74705SXin Li /// emitThunk - Emit a single thunk. 53*67e74705SXin Li void emitThunk(GlobalDecl GD, const ThunkInfo &Thunk, bool ForVTable); 54*67e74705SXin Li 55*67e74705SXin Li /// maybeEmitThunkForVTable - Emit the given thunk for the vtable if needed by 56*67e74705SXin Li /// the ABI. 57*67e74705SXin Li void maybeEmitThunkForVTable(GlobalDecl GD, const ThunkInfo &Thunk); 58*67e74705SXin Li 59*67e74705SXin Li public: 60*67e74705SXin Li /// CreateVTableInitializer - Create a vtable initializer for the given record 61*67e74705SXin Li /// decl. 62*67e74705SXin Li /// \param Components - The vtable components; this is really an array of 63*67e74705SXin Li /// VTableComponents. 64*67e74705SXin Li llvm::Constant *CreateVTableInitializer( 65*67e74705SXin Li const CXXRecordDecl *RD, const VTableComponent *Components, 66*67e74705SXin Li unsigned NumComponents, const VTableLayout::VTableThunkTy *VTableThunks, 67*67e74705SXin Li unsigned NumVTableThunks, llvm::Constant *RTTI); 68*67e74705SXin Li 69*67e74705SXin Li CodeGenVTables(CodeGenModule &CGM); 70*67e74705SXin Li getItaniumVTableContext()71*67e74705SXin Li ItaniumVTableContext &getItaniumVTableContext() { 72*67e74705SXin Li return *cast<ItaniumVTableContext>(VTContext); 73*67e74705SXin Li } 74*67e74705SXin Li getMicrosoftVTableContext()75*67e74705SXin Li MicrosoftVTableContext &getMicrosoftVTableContext() { 76*67e74705SXin Li return *cast<MicrosoftVTableContext>(VTContext); 77*67e74705SXin Li } 78*67e74705SXin Li 79*67e74705SXin Li /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the 80*67e74705SXin Li /// given record decl. 81*67e74705SXin Li uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base); 82*67e74705SXin Li 83*67e74705SXin Li /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the 84*67e74705SXin Li /// virtual pointer for the given subobject is located. 85*67e74705SXin Li uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD, 86*67e74705SXin Li BaseSubobject Base); 87*67e74705SXin Li 88*67e74705SXin Li /// GenerateConstructionVTable - Generate a construction vtable for the given 89*67e74705SXin Li /// base subobject. 90*67e74705SXin Li llvm::GlobalVariable * 91*67e74705SXin Li GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base, 92*67e74705SXin Li bool BaseIsVirtual, 93*67e74705SXin Li llvm::GlobalVariable::LinkageTypes Linkage, 94*67e74705SXin Li VTableAddressPointsMapTy& AddressPoints); 95*67e74705SXin Li 96*67e74705SXin Li 97*67e74705SXin Li /// GetAddrOfVTT - Get the address of the VTT for the given record decl. 98*67e74705SXin Li llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD); 99*67e74705SXin Li 100*67e74705SXin Li /// EmitVTTDefinition - Emit the definition of the given vtable. 101*67e74705SXin Li void EmitVTTDefinition(llvm::GlobalVariable *VTT, 102*67e74705SXin Li llvm::GlobalVariable::LinkageTypes Linkage, 103*67e74705SXin Li const CXXRecordDecl *RD); 104*67e74705SXin Li 105*67e74705SXin Li /// EmitThunks - Emit the associated thunks for the given global decl. 106*67e74705SXin Li void EmitThunks(GlobalDecl GD); 107*67e74705SXin Li 108*67e74705SXin Li /// GenerateClassData - Generate all the class data required to be 109*67e74705SXin Li /// generated upon definition of a KeyFunction. This includes the 110*67e74705SXin Li /// vtable, the RTTI data structure (if RTTI is enabled) and the VTT 111*67e74705SXin Li /// (if the class has virtual bases). 112*67e74705SXin Li void GenerateClassData(const CXXRecordDecl *RD); 113*67e74705SXin Li 114*67e74705SXin Li bool isVTableExternal(const CXXRecordDecl *RD); 115*67e74705SXin Li }; 116*67e74705SXin Li 117*67e74705SXin Li } // end namespace CodeGen 118*67e74705SXin Li } // end namespace clang 119*67e74705SXin Li #endif 120