1 //===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This contains code dealing with C++ code generation of virtual tables.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H
14 #define LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H
16 #include "clang/AST/BaseSubobject.h"
17 #include "clang/AST/CharUnits.h"
18 #include "clang/AST/GlobalDecl.h"
19 #include "clang/AST/VTableBuilder.h"
20 #include "clang/Basic/ABI.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/IR/GlobalVariable.h"
29 class ConstantArrayBuilder;
30 class ConstantStructBuilder;
32 class CodeGenVTables {
35 VTableContextBase *VTContext;
37 /// VTableAddressPointsMapTy - Address points for a single vtable.
38 typedef VTableLayout::AddressPointsMapTy VTableAddressPointsMapTy;
40 typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;
41 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy;
43 /// SubVTTIndicies - Contains indices into the various sub-VTTs.
44 SubVTTIndiciesMapTy SubVTTIndicies;
46 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>
47 SecondaryVirtualPointerIndicesMapTy;
49 /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer
51 SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
53 /// Cache for the pure virtual member call function.
54 llvm::Constant *PureVirtualFn = nullptr;
56 /// Cache for the deleted virtual member call function.
57 llvm::Constant *DeletedVirtualFn = nullptr;
59 /// Get the address of a thunk and emit it if necessary.
60 llvm::Constant *maybeEmitThunk(GlobalDecl GD,
61 const ThunkInfo &ThunkAdjustments,
64 void addVTableComponent(ConstantArrayBuilder &builder,
65 const VTableLayout &layout, unsigned idx,
67 unsigned &nextVTableThunkIndex);
70 /// Add vtable components for the given vtable layout to the given
71 /// global initializer.
72 void createVTableInitializer(ConstantStructBuilder &builder,
73 const VTableLayout &layout,
74 llvm::Constant *rtti);
76 CodeGenVTables(CodeGenModule &CGM);
78 ItaniumVTableContext &getItaniumVTableContext() {
79 return *cast<ItaniumVTableContext>(VTContext);
82 MicrosoftVTableContext &getMicrosoftVTableContext() {
83 return *cast<MicrosoftVTableContext>(VTContext);
86 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
87 /// given record decl.
88 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
90 /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the
91 /// virtual pointer for the given subobject is located.
92 uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
95 /// GenerateConstructionVTable - Generate a construction vtable for the given
97 llvm::GlobalVariable *
98 GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,
100 llvm::GlobalVariable::LinkageTypes Linkage,
101 VTableAddressPointsMapTy& AddressPoints);
104 /// GetAddrOfVTT - Get the address of the VTT for the given record decl.
105 llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
107 /// EmitVTTDefinition - Emit the definition of the given vtable.
108 void EmitVTTDefinition(llvm::GlobalVariable *VTT,
109 llvm::GlobalVariable::LinkageTypes Linkage,
110 const CXXRecordDecl *RD);
112 /// EmitThunks - Emit the associated thunks for the given global decl.
113 void EmitThunks(GlobalDecl GD);
115 /// GenerateClassData - Generate all the class data required to be
116 /// generated upon definition of a KeyFunction. This includes the
117 /// vtable, the RTTI data structure (if RTTI is enabled) and the VTT
118 /// (if the class has virtual bases).
119 void GenerateClassData(const CXXRecordDecl *RD);
121 bool isVTableExternal(const CXXRecordDecl *RD);
123 /// Returns the type of a vtable with the given layout. Normally a struct of
124 /// arrays of pointers, with one struct element for each vtable in the vtable
126 llvm::Type *getVTableType(const VTableLayout &layout);
129 } // end namespace CodeGen
130 } // end namespace clang