1 //===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code dealing with C++ code generation of virtual tables.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H
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"
30 class ConstantArrayBuilder;
31 class ConstantStructBuilder;
33 class CodeGenVTables {
36 VTableContextBase *VTContext;
38 /// VTableAddressPointsMapTy - Address points for a single vtable.
39 typedef VTableLayout::AddressPointsMapTy VTableAddressPointsMapTy;
41 typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;
42 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy;
44 /// SubVTTIndicies - Contains indices into the various sub-VTTs.
45 SubVTTIndiciesMapTy SubVTTIndicies;
47 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>
48 SecondaryVirtualPointerIndicesMapTy;
50 /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer
52 SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
54 /// Cache for the pure virtual member call function.
55 llvm::Constant *PureVirtualFn = nullptr;
57 /// Cache for the deleted virtual member call function.
58 llvm::Constant *DeletedVirtualFn = nullptr;
60 /// Get the address of a thunk and emit it if necessary.
61 llvm::Constant *maybeEmitThunk(GlobalDecl GD,
62 const ThunkInfo &ThunkAdjustments,
65 void addVTableComponent(ConstantArrayBuilder &builder,
66 const VTableLayout &layout, unsigned idx,
68 unsigned &nextVTableThunkIndex);
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);
77 CodeGenVTables(CodeGenModule &CGM);
79 ItaniumVTableContext &getItaniumVTableContext() {
80 return *cast<ItaniumVTableContext>(VTContext);
83 MicrosoftVTableContext &getMicrosoftVTableContext() {
84 return *cast<MicrosoftVTableContext>(VTContext);
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);
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,
96 /// GenerateConstructionVTable - Generate a construction vtable for the given
98 llvm::GlobalVariable *
99 GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,
101 llvm::GlobalVariable::LinkageTypes Linkage,
102 VTableAddressPointsMapTy& AddressPoints);
105 /// GetAddrOfVTT - Get the address of the VTT for the given record decl.
106 llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
108 /// EmitVTTDefinition - Emit the definition of the given vtable.
109 void EmitVTTDefinition(llvm::GlobalVariable *VTT,
110 llvm::GlobalVariable::LinkageTypes Linkage,
111 const CXXRecordDecl *RD);
113 /// EmitThunks - Emit the associated thunks for the given global decl.
114 void EmitThunks(GlobalDecl GD);
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);
122 bool isVTableExternal(const CXXRecordDecl *RD);
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
127 llvm::Type *getVTableType(const VTableLayout &layout);
130 } // end namespace CodeGen
131 } // end namespace clang