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 CLANG_CODEGEN_CGVTABLE_H 15 #define CLANG_CODEGEN_CGVTABLE_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 31 class CodeGenVTables { 32 CodeGenModule &CGM; 33 34 // FIXME: Consider moving VTContext and VFTContext into respective CXXABI 35 // classes? 36 VTableContext VTContext; 37 OwningPtr<MicrosoftVFTableContext> VFTContext; 38 39 /// VTables - All the vtables which have been defined. 40 llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables; 41 42 /// VTableAddressPointsMapTy - Address points for a single vtable. 43 typedef llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPointsMapTy; 44 45 typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy; 46 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy; 47 48 /// SubVTTIndicies - Contains indices into the various sub-VTTs. 49 SubVTTIndiciesMapTy SubVTTIndicies; 50 51 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> 52 SecondaryVirtualPointerIndicesMapTy; 53 54 /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer 55 /// indices. 56 SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices; 57 58 /// EmitThunk - Emit a single thunk. 59 void EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk, 60 bool UseAvailableExternallyLinkage); 61 62 /// MaybeEmitThunkAvailableExternally - Try to emit the given thunk with 63 /// available_externally linkage to allow for inlining of thunks. 64 /// This will be done iff optimizations are enabled and the member function 65 /// doesn't contain any incomplete types. 66 void MaybeEmitThunkAvailableExternally(GlobalDecl GD, const ThunkInfo &Thunk); 67 68 /// CreateVTableInitializer - Create a vtable initializer for the given record 69 /// decl. 70 /// \param Components - The vtable components; this is really an array of 71 /// VTableComponents. 72 llvm::Constant *CreateVTableInitializer(const CXXRecordDecl *RD, 73 const VTableComponent *Components, 74 unsigned NumComponents, 75 const VTableLayout::VTableThunkTy *VTableThunks, 76 unsigned NumVTableThunks); 77 78 public: 79 CodeGenVTables(CodeGenModule &CGM); 80 81 VTableContext &getVTableContext() { return VTContext; } 82 83 MicrosoftVFTableContext &getVFTableContext() { return *VFTContext.get(); } 84 85 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the 86 /// given record decl. 87 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base); 88 89 /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the 90 /// virtual pointer for the given subobject is located. 91 uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD, 92 BaseSubobject Base); 93 94 /// getAddressPoint - Get the address point of the given subobject in the 95 /// class decl. 96 uint64_t getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD); 97 98 /// GetAddrOfVTable - Get the address of the vtable for the given record decl. 99 llvm::GlobalVariable *GetAddrOfVTable(const CXXRecordDecl *RD); 100 101 /// EmitVTableDefinition - Emit the definition of the given vtable. 102 void EmitVTableDefinition(llvm::GlobalVariable *VTable, 103 llvm::GlobalVariable::LinkageTypes Linkage, 104 const CXXRecordDecl *RD); 105 106 /// GenerateConstructionVTable - Generate a construction vtable for the given 107 /// base subobject. 108 llvm::GlobalVariable * 109 GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base, 110 bool BaseIsVirtual, 111 llvm::GlobalVariable::LinkageTypes Linkage, 112 VTableAddressPointsMapTy& AddressPoints); 113 114 115 /// GetAddrOfVTable - Get the address of the VTT for the given record decl. 116 llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD); 117 118 /// EmitVTTDefinition - Emit the definition of the given vtable. 119 void EmitVTTDefinition(llvm::GlobalVariable *VTT, 120 llvm::GlobalVariable::LinkageTypes Linkage, 121 const CXXRecordDecl *RD); 122 123 /// EmitThunks - Emit the associated thunks for the given global decl. 124 void EmitThunks(GlobalDecl GD); 125 126 /// GenerateClassData - Generate all the class data required to be 127 /// generated upon definition of a KeyFunction. This includes the 128 /// vtable, the RTTI data structure (if RTTI is enabled) and the VTT 129 /// (if the class has virtual bases). 130 void GenerateClassData(const CXXRecordDecl *RD); 131 132 bool isVTableExternal(const CXXRecordDecl *RD); 133 }; 134 135 } // end namespace CodeGen 136 } // end namespace clang 137 #endif 138