1221345Sdim//===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===// 2207619Srdivacky// 3207619Srdivacky// The LLVM Compiler Infrastructure 4207619Srdivacky// 5207619Srdivacky// This file is distributed under the University of Illinois Open Source 6207619Srdivacky// License. See LICENSE.TXT for details. 7207619Srdivacky// 8207619Srdivacky//===----------------------------------------------------------------------===// 9207619Srdivacky// 10207619Srdivacky// This contains code dealing with C++ code generation of virtual tables. 11207619Srdivacky// 12207619Srdivacky//===----------------------------------------------------------------------===// 13207619Srdivacky 14207619Srdivacky#ifndef CLANG_CODEGEN_CGVTABLE_H 15207619Srdivacky#define CLANG_CODEGEN_CGVTABLE_H 16207619Srdivacky 17226890Sdim#include "clang/AST/BaseSubobject.h" 18221345Sdim#include "clang/AST/CharUnits.h" 19224145Sdim#include "clang/AST/GlobalDecl.h" 20226890Sdim#include "clang/AST/VTableBuilder.h" 21252723Sdim#include "clang/Basic/ABI.h" 22252723Sdim#include "llvm/ADT/DenseMap.h" 23252723Sdim#include "llvm/IR/GlobalVariable.h" 24207619Srdivacky 25207619Srdivackynamespace clang { 26207619Srdivacky class CXXRecordDecl; 27207619Srdivacky 28207619Srdivackynamespace CodeGen { 29207619Srdivacky class CodeGenModule; 30207619Srdivacky 31207619Srdivackyclass CodeGenVTables { 32207619Srdivacky CodeGenModule &CGM; 33207619Srdivacky 34263509Sdim // FIXME: Consider moving ItaniumVTContext and MicrosoftVTContext into 35263509Sdim // respective CXXABI classes? 36263509Sdim ItaniumVTableContext ItaniumVTContext; 37263509Sdim OwningPtr<MicrosoftVTableContext> MicrosoftVTContext; 38207619Srdivacky 39207619Srdivacky /// VTableAddressPointsMapTy - Address points for a single vtable. 40207619Srdivacky typedef llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPointsMapTy; 41207619Srdivacky 42226890Sdim typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy; 43207619Srdivacky typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy; 44207619Srdivacky 45207619Srdivacky /// SubVTTIndicies - Contains indices into the various sub-VTTs. 46207619Srdivacky SubVTTIndiciesMapTy SubVTTIndicies; 47207619Srdivacky 48207619Srdivacky typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> 49207619Srdivacky SecondaryVirtualPointerIndicesMapTy; 50207619Srdivacky 51207619Srdivacky /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer 52207619Srdivacky /// indices. 53207619Srdivacky SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices; 54207619Srdivacky 55263509Sdim /// emitThunk - Emit a single thunk. 56263509Sdim void emitThunk(GlobalDecl GD, const ThunkInfo &Thunk, bool ForVTable); 57207619Srdivacky 58263509Sdim /// maybeEmitThunkForVTable - Emit the given thunk for the vtable if needed by 59263509Sdim /// the ABI. 60263509Sdim void maybeEmitThunkForVTable(GlobalDecl GD, const ThunkInfo &Thunk); 61218893Sdim 62263509Sdimpublic: 63207619Srdivacky /// CreateVTableInitializer - Create a vtable initializer for the given record 64207619Srdivacky /// decl. 65207619Srdivacky /// \param Components - The vtable components; this is really an array of 66207619Srdivacky /// VTableComponents. 67207619Srdivacky llvm::Constant *CreateVTableInitializer(const CXXRecordDecl *RD, 68226890Sdim const VTableComponent *Components, 69207619Srdivacky unsigned NumComponents, 70226890Sdim const VTableLayout::VTableThunkTy *VTableThunks, 71226890Sdim unsigned NumVTableThunks); 72207619Srdivacky 73226890Sdim CodeGenVTables(CodeGenModule &CGM); 74207619Srdivacky 75263509Sdim ItaniumVTableContext &getItaniumVTableContext() { return ItaniumVTContext; } 76226890Sdim 77263509Sdim MicrosoftVTableContext &getMicrosoftVTableContext() { 78263509Sdim return *MicrosoftVTContext.get(); 79263509Sdim } 80207619Srdivacky 81207619Srdivacky /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the 82207619Srdivacky /// given record decl. 83207619Srdivacky uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base); 84207619Srdivacky 85207619Srdivacky /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the 86207619Srdivacky /// virtual pointer for the given subobject is located. 87207619Srdivacky uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD, 88207619Srdivacky BaseSubobject Base); 89207619Srdivacky 90207619Srdivacky /// getAddressPoint - Get the address point of the given subobject in the 91207619Srdivacky /// class decl. 92207619Srdivacky uint64_t getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD); 93207619Srdivacky 94207619Srdivacky /// GenerateConstructionVTable - Generate a construction vtable for the given 95207619Srdivacky /// base subobject. 96207619Srdivacky llvm::GlobalVariable * 97207619Srdivacky GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base, 98207619Srdivacky bool BaseIsVirtual, 99221345Sdim llvm::GlobalVariable::LinkageTypes Linkage, 100207619Srdivacky VTableAddressPointsMapTy& AddressPoints); 101207619Srdivacky 102218893Sdim 103218893Sdim /// GetAddrOfVTable - Get the address of the VTT for the given record decl. 104218893Sdim llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD); 105218893Sdim 106218893Sdim /// EmitVTTDefinition - Emit the definition of the given vtable. 107218893Sdim void EmitVTTDefinition(llvm::GlobalVariable *VTT, 108218893Sdim llvm::GlobalVariable::LinkageTypes Linkage, 109218893Sdim const CXXRecordDecl *RD); 110218893Sdim 111208600Srdivacky /// EmitThunks - Emit the associated thunks for the given global decl. 112208600Srdivacky void EmitThunks(GlobalDecl GD); 113208600Srdivacky 114252723Sdim /// GenerateClassData - Generate all the class data required to be 115252723Sdim /// generated upon definition of a KeyFunction. This includes the 116252723Sdim /// vtable, the RTTI data structure (if RTTI is enabled) and the VTT 117252723Sdim /// (if the class has virtual bases). 118252723Sdim void GenerateClassData(const CXXRecordDecl *RD); 119252723Sdim 120252723Sdim bool isVTableExternal(const CXXRecordDecl *RD); 121207619Srdivacky}; 122207619Srdivacky 123207619Srdivacky} // end namespace CodeGen 124207619Srdivacky} // end namespace clang 125207619Srdivacky#endif 126