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 CLANG_CODEGEN_CGVTABLE_H
15 #define CLANG_CODEGEN_CGVTABLE_H
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/GlobalVariable.h"
19 #include "clang/Basic/ABI.h"
20 #include "clang/AST/BaseSubobject.h"
21 #include "clang/AST/CharUnits.h"
22 #include "clang/AST/GlobalDecl.h"
23 #include "clang/AST/VTableBuilder.h"
31 class CodeGenVTables {
34 VTableContext VTContext;
36 /// VTables - All the vtables which have been defined.
37 llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables;
39 /// VTableAddressPointsMapTy - Address points for a single vtable.
40 typedef llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPointsMapTy;
42 typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;
43 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy;
45 /// SubVTTIndicies - Contains indices into the various sub-VTTs.
46 SubVTTIndiciesMapTy SubVTTIndicies;
48 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>
49 SecondaryVirtualPointerIndicesMapTy;
51 /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer
53 SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
55 /// EmitThunk - Emit a single thunk.
56 void EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
57 bool UseAvailableExternallyLinkage);
59 /// MaybeEmitThunkAvailableExternally - Try to emit the given thunk with
60 /// available_externally linkage to allow for inlining of thunks.
61 /// This will be done iff optimizations are enabled and the member function
62 /// doesn't contain any incomplete types.
63 void MaybeEmitThunkAvailableExternally(GlobalDecl GD, const ThunkInfo &Thunk);
65 /// CreateVTableInitializer - Create a vtable initializer for the given record
67 /// \param Components - The vtable components; this is really an array of
69 llvm::Constant *CreateVTableInitializer(const CXXRecordDecl *RD,
70 const VTableComponent *Components,
71 unsigned NumComponents,
72 const VTableLayout::VTableThunkTy *VTableThunks,
73 unsigned NumVTableThunks);
76 CodeGenVTables(CodeGenModule &CGM);
78 VTableContext &getVTableContext() { return VTContext; }
80 /// \brief True if the VTable of this record must be emitted in the
82 bool ShouldEmitVTableInThisTU(const CXXRecordDecl *RD);
84 /// needsVTTParameter - Return whether the given global decl needs a VTT
85 /// parameter, which it does if it's a base constructor or destructor with
87 static bool needsVTTParameter(GlobalDecl GD);
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 /// getAddressPoint - Get the address point of the given subobject in the
100 uint64_t getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD);
102 /// GetAddrOfVTable - Get the address of the vtable for the given record decl.
103 llvm::GlobalVariable *GetAddrOfVTable(const CXXRecordDecl *RD);
105 /// EmitVTableDefinition - Emit the definition of the given vtable.
106 void EmitVTableDefinition(llvm::GlobalVariable *VTable,
107 llvm::GlobalVariable::LinkageTypes Linkage,
108 const CXXRecordDecl *RD);
110 /// GenerateConstructionVTable - Generate a construction vtable for the given
112 llvm::GlobalVariable *
113 GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,
115 llvm::GlobalVariable::LinkageTypes Linkage,
116 VTableAddressPointsMapTy& AddressPoints);
119 /// GetAddrOfVTable - Get the address of the VTT for the given record decl.
120 llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
122 /// EmitVTTDefinition - Emit the definition of the given vtable.
123 void EmitVTTDefinition(llvm::GlobalVariable *VTT,
124 llvm::GlobalVariable::LinkageTypes Linkage,
125 const CXXRecordDecl *RD);
127 /// EmitThunks - Emit the associated thunks for the given global decl.
128 void EmitThunks(GlobalDecl GD);
130 /// GenerateClassData - Generate all the class data required to be generated
131 /// upon definition of a KeyFunction. This includes the vtable, the
132 /// rtti data structure and the VTT.
134 /// \param Linkage - The desired linkage of the vtable, the RTTI and the VTT.
135 void GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
136 const CXXRecordDecl *RD);
139 } // end namespace CodeGen
140 } // end namespace clang