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 /// emitThunk - Emit a single thunk. 61 void emitThunk(GlobalDecl GD, const ThunkInfo &Thunk, bool ForVTable); 62 63 /// maybeEmitThunkForVTable - Emit the given thunk for the vtable if needed by 64 /// the ABI. 65 void maybeEmitThunkForVTable(GlobalDecl GD, const ThunkInfo &Thunk); 66 67 void addVTableComponent(ConstantArrayBuilder &builder, 68 const VTableLayout &layout, unsigned idx, 69 llvm::Constant *rtti, 70 unsigned &nextVTableThunkIndex); 71 72 public: 73 /// Add vtable components for the given vtable layout to the given 74 /// global initializer. 75 void createVTableInitializer(ConstantStructBuilder &builder, 76 const VTableLayout &layout, 77 llvm::Constant *rtti); 78 79 CodeGenVTables(CodeGenModule &CGM); 80 81 ItaniumVTableContext &getItaniumVTableContext() { 82 return *cast<ItaniumVTableContext>(VTContext); 83 } 84 85 MicrosoftVTableContext &getMicrosoftVTableContext() { 86 return *cast<MicrosoftVTableContext>(VTContext); 87 } 88 89 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the 90 /// given record decl. 91 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base); 92 93 /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the 94 /// virtual pointer for the given subobject is located. 95 uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD, 96 BaseSubobject Base); 97 98 /// GenerateConstructionVTable - Generate a construction vtable for the given 99 /// base subobject. 100 llvm::GlobalVariable * 101 GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base, 102 bool BaseIsVirtual, 103 llvm::GlobalVariable::LinkageTypes Linkage, 104 VTableAddressPointsMapTy& AddressPoints); 105 106 107 /// GetAddrOfVTT - Get the address of the VTT for the given record decl. 108 llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD); 109 110 /// EmitVTTDefinition - Emit the definition of the given vtable. 111 void EmitVTTDefinition(llvm::GlobalVariable *VTT, 112 llvm::GlobalVariable::LinkageTypes Linkage, 113 const CXXRecordDecl *RD); 114 115 /// EmitThunks - Emit the associated thunks for the given global decl. 116 void EmitThunks(GlobalDecl GD); 117 118 /// GenerateClassData - Generate all the class data required to be 119 /// generated upon definition of a KeyFunction. This includes the 120 /// vtable, the RTTI data structure (if RTTI is enabled) and the VTT 121 /// (if the class has virtual bases). 122 void GenerateClassData(const CXXRecordDecl *RD); 123 124 bool isVTableExternal(const CXXRecordDecl *RD); 125 126 /// Returns the type of a vtable with the given layout. Normally a struct of 127 /// arrays of pointers, with one struct element for each vtable in the vtable 128 /// group. 129 llvm::Type *getVTableType(const VTableLayout &layout); 130 }; 131 132 } // end namespace CodeGen 133 } // end namespace clang 134 #endif 135