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