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