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 /// emitThunk - Emit a single thunk.
61 void emitThunk(GlobalDecl GD, const ThunkInfo &Thunk, bool ForVTable);
63 /// maybeEmitThunkForVTable - Emit the given thunk for the vtable if needed by
65 void maybeEmitThunkForVTable(GlobalDecl GD, const ThunkInfo &Thunk);
67 void addVTableComponent(ConstantArrayBuilder &builder,
68 const VTableLayout &layout, unsigned idx,
70 unsigned &nextVTableThunkIndex);
73 /// Add vtable components for the given vtable layout to the given
74 /// global initializer.
75 void createVTableInitializer(ConstantStructBuilder &builder,
76 const VTableLayout &layout,
77 llvm::Constant *rtti);
79 CodeGenVTables(CodeGenModule &CGM);
81 ItaniumVTableContext &getItaniumVTableContext() {
82 return *cast<ItaniumVTableContext>(VTContext);
85 MicrosoftVTableContext &getMicrosoftVTableContext() {
86 return *cast<MicrosoftVTableContext>(VTContext);
89 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
90 /// given record decl.
91 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
93 /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the
94 /// virtual pointer for the given subobject is located.
95 uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
98 /// GenerateConstructionVTable - Generate a construction vtable for the given
100 llvm::GlobalVariable *
101 GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,
103 llvm::GlobalVariable::LinkageTypes Linkage,
104 VTableAddressPointsMapTy& AddressPoints);
107 /// GetAddrOfVTT - Get the address of the VTT for the given record decl.
108 llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
110 /// EmitVTTDefinition - Emit the definition of the given vtable.
111 void EmitVTTDefinition(llvm::GlobalVariable *VTT,
112 llvm::GlobalVariable::LinkageTypes Linkage,
113 const CXXRecordDecl *RD);
115 /// EmitThunks - Emit the associated thunks for the given global decl.
116 void EmitThunks(GlobalDecl GD);
118 /// GenerateClassData - Generate all the class data required to be
119 /// generated upon definition of a KeyFunction. This includes the
120 /// vtable, the RTTI data structure (if RTTI is enabled) and the VTT
121 /// (if the class has virtual bases).
122 void GenerateClassData(const CXXRecordDecl *RD);
124 bool isVTableExternal(const CXXRecordDecl *RD);
126 /// Returns the type of a vtable with the given layout. Normally a struct of
127 /// arrays of pointers, with one struct element for each vtable in the vtable
129 llvm::Type *getVTableType(const VTableLayout &layout);
132 } // end namespace CodeGen
133 } // end namespace clang