blob: 7c119fa4224121559dbed380971cb952cec4fb67 [file] [log] [blame]
Anders Carlsson461e3262010-04-08 16:30:25 +00001//===--- CGVTables.h - Emit LLVM Code for C++ vtables ---------------------===//
Anders Carlssondbd920c2009-10-11 22:13:54 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation of virtual tables.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CLANG_CODEGEN_CGVTABLE_H
15#define CLANG_CODEGEN_CGVTABLE_H
16
17#include "llvm/ADT/DenseMap.h"
Anders Carlsson35272252009-12-06 00:23:49 +000018#include "llvm/GlobalVariable.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000019#include "clang/Basic/ABI.h"
Anders Carlssona0fdd912009-11-13 17:08:56 +000020#include "GlobalDecl.h"
Anders Carlssondbd920c2009-10-11 22:13:54 +000021
22namespace clang {
Anders Carlssondbd920c2009-10-11 22:13:54 +000023 class CXXRecordDecl;
Benjamin Kramer39411b92009-11-26 13:09:03 +000024
Anders Carlssondbd920c2009-10-11 22:13:54 +000025namespace CodeGen {
26 class CodeGenModule;
Anders Carlssona94822e2009-11-26 02:32:05 +000027
Anders Carlsson6b4333d2010-01-13 20:11:15 +000028// BaseSubobject - Uniquely identifies a direct or indirect base class.
29// Stores both the base class decl and the offset from the most derived class to
30// the base class.
31class BaseSubobject {
32 /// Base - The base class declaration.
33 const CXXRecordDecl *Base;
34
35 /// BaseOffset - The offset from the most derived class to the base class.
36 uint64_t BaseOffset;
37
38public:
39 BaseSubobject(const CXXRecordDecl *Base, uint64_t BaseOffset)
40 : Base(Base), BaseOffset(BaseOffset) { }
41
42 /// getBase - Returns the base class declaration.
43 const CXXRecordDecl *getBase() const { return Base; }
44
45 /// getBaseOffset - Returns the base class offset.
46 uint64_t getBaseOffset() const { return BaseOffset; }
Anders Carlsson7e37a692010-01-14 01:39:42 +000047
Anders Carlsson6b4333d2010-01-13 20:11:15 +000048 friend bool operator==(const BaseSubobject &LHS, const BaseSubobject &RHS) {
49 return LHS.Base == RHS.Base && LHS.BaseOffset == RHS.BaseOffset;
50 }
51};
Anders Carlsson7e37a692010-01-14 01:39:42 +000052
53} // end namespace CodeGen
54} // end namespace clang
55
56namespace llvm {
57
58template<> struct DenseMapInfo<clang::CodeGen::BaseSubobject> {
59 static clang::CodeGen::BaseSubobject getEmptyKey() {
60 return clang::CodeGen::BaseSubobject(
61 DenseMapInfo<const clang::CXXRecordDecl *>::getEmptyKey(),
62 DenseMapInfo<uint64_t>::getEmptyKey());
63 }
64
65 static clang::CodeGen::BaseSubobject getTombstoneKey() {
66 return clang::CodeGen::BaseSubobject(
67 DenseMapInfo<const clang::CXXRecordDecl *>::getTombstoneKey(),
68 DenseMapInfo<uint64_t>::getTombstoneKey());
69 }
70
71 static unsigned getHashValue(const clang::CodeGen::BaseSubobject &Base) {
72 return
73 DenseMapInfo<const clang::CXXRecordDecl *>::getHashValue(Base.getBase()) ^
74 DenseMapInfo<uint64_t>::getHashValue(Base.getBaseOffset());
75 }
76
77 static bool isEqual(const clang::CodeGen::BaseSubobject &LHS,
78 const clang::CodeGen::BaseSubobject &RHS) {
79 return LHS == RHS;
80 }
81};
82
Anders Carlsson1bb60992010-01-14 02:29:07 +000083// It's OK to treat BaseSubobject as a POD type.
84template <> struct isPodLike<clang::CodeGen::BaseSubobject> {
85 static const bool value = true;
86};
87
Anders Carlsson7e37a692010-01-14 01:39:42 +000088}
89
90namespace clang {
91namespace CodeGen {
92
Anders Carlssonaf440352010-03-23 04:11:45 +000093class CodeGenVTables {
Anders Carlssondbd920c2009-10-11 22:13:54 +000094 CodeGenModule &CGM;
Benjamin Kramer39411b92009-11-26 13:09:03 +000095
Anders Carlsson046c2942010-04-17 20:15:18 +000096 /// MethodVTableIndices - Contains the index (relative to the vtable address
Anders Carlssondbd920c2009-10-11 22:13:54 +000097 /// point) where the function pointer for a virtual function is stored.
Anders Carlsson046c2942010-04-17 20:15:18 +000098 typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVTableIndicesTy;
99 MethodVTableIndicesTy MethodVTableIndices;
Benjamin Kramer39411b92009-11-26 13:09:03 +0000100
Anders Carlssondbd920c2009-10-11 22:13:54 +0000101 typedef std::pair<const CXXRecordDecl *,
102 const CXXRecordDecl *> ClassPairTy;
Benjamin Kramer39411b92009-11-26 13:09:03 +0000103
Anders Carlssonbba16072010-03-11 07:15:17 +0000104 /// VirtualBaseClassOffsetOffsets - Contains the vtable offset (relative to
105 /// the address point) in bytes where the offsets for virtual bases of a class
106 /// are stored.
107 typedef llvm::DenseMap<ClassPairTy, int64_t>
108 VirtualBaseClassOffsetOffsetsMapTy;
109 VirtualBaseClassOffsetOffsetsMapTy VirtualBaseClassOffsetOffsets;
Mike Stump380dd752009-11-10 07:44:33 +0000110
Anders Carlsson046c2942010-04-17 20:15:18 +0000111 /// VTables - All the vtables which have been defined.
112 llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables;
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000113
114 /// NumVirtualFunctionPointers - Contains the number of virtual function
115 /// pointers in the vtable for a given record decl.
116 llvm::DenseMap<const CXXRecordDecl *, uint64_t> NumVirtualFunctionPointers;
117
Anders Carlssonfbf6ed42010-03-23 16:36:50 +0000118 typedef llvm::SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
119 typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
120
121 /// Thunks - Contains all thunks that a given method decl will need.
122 ThunksMapTy Thunks;
John McCalle2132352010-06-02 21:22:02 +0000123
124 // The layout entry and a bool indicating whether we've actually emitted
125 // the vtable.
126 typedef llvm::PointerIntPair<uint64_t *, 1, bool> VTableLayoutData;
127 typedef llvm::DenseMap<const CXXRecordDecl *, VTableLayoutData>
128 VTableLayoutMapTy;
Anders Carlssonccd83d72010-03-24 16:42:11 +0000129
130 /// VTableLayoutMap - Stores the vtable layout for all record decls.
131 /// The layout is stored as an array of 64-bit integers, where the first
132 /// integer is the number of vtable entries in the layout, and the subsequent
133 /// integers are the vtable components.
134 VTableLayoutMapTy VTableLayoutMap;
135
Anders Carlsson3855a072010-05-03 00:55:11 +0000136 typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;
137 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> AddressPointsMapTy;
Anders Carlsson66d567d2010-03-25 00:51:13 +0000138
Anders Carlsson2c822f12010-03-26 03:56:54 +0000139 /// Address points - Address points for all vtables.
Anders Carlsson66d567d2010-03-25 00:51:13 +0000140 AddressPointsMapTy AddressPoints;
Anders Carlsson2c822f12010-03-26 03:56:54 +0000141
142 /// VTableAddressPointsMapTy - Address points for a single vtable.
143 typedef llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPointsMapTy;
144
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000145 typedef llvm::SmallVector<std::pair<uint64_t, ThunkInfo>, 1>
146 VTableThunksTy;
147
148 typedef llvm::DenseMap<const CXXRecordDecl *, VTableThunksTy>
149 VTableThunksMapTy;
150
151 /// VTableThunksMap - Contains thunks needed by vtables.
152 VTableThunksMapTy VTableThunksMap;
153
Anders Carlssonccd83d72010-03-24 16:42:11 +0000154 uint64_t getNumVTableComponents(const CXXRecordDecl *RD) const {
155 assert(VTableLayoutMap.count(RD) && "No vtable layout for this class!");
156
John McCalle2132352010-06-02 21:22:02 +0000157 return VTableLayoutMap.lookup(RD).getPointer()[0];
Anders Carlssonccd83d72010-03-24 16:42:11 +0000158 }
159
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000160 const uint64_t *getVTableComponentsData(const CXXRecordDecl *RD) const {
161 assert(VTableLayoutMap.count(RD) && "No vtable layout for this class!");
162
John McCalle2132352010-06-02 21:22:02 +0000163 uint64_t *Components = VTableLayoutMap.lookup(RD).getPointer();
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000164 return &Components[1];
165 }
166
Anders Carlsson3855a072010-05-03 00:55:11 +0000167 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy;
Anders Carlssone1dcc222010-03-26 04:23:58 +0000168
169 /// SubVTTIndicies - Contains indices into the various sub-VTTs.
170 SubVTTIndiciesMapTy SubVTTIndicies;
171
Anders Carlsson3855a072010-05-03 00:55:11 +0000172 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>
Anders Carlssone1dcc222010-03-26 04:23:58 +0000173 SecondaryVirtualPointerIndicesMapTy;
174
175 /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer
176 /// indices.
177 SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
Anders Carlssonc997d422010-01-02 01:01:18 +0000178
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000179 /// getNumVirtualFunctionPointers - Return the number of virtual function
180 /// pointers in the vtable for a given record decl.
181 uint64_t getNumVirtualFunctionPointers(const CXXRecordDecl *RD);
182
Anders Carlsson046c2942010-04-17 20:15:18 +0000183 void ComputeMethodVTableIndices(const CXXRecordDecl *RD);
Anders Carlssonc3a46ef2009-12-06 01:09:21 +0000184
Anders Carlssonfbf6ed42010-03-23 16:36:50 +0000185 /// EmitThunk - Emit a single thunk.
Anders Carlsson14e82fd2011-02-06 18:31:40 +0000186 void EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
187 bool UseAvailableExternallyLinkage);
188
189 /// MaybeEmitThunkAvailableExternally - Try to emit the given thunk with
190 /// available_externally linkage to allow for inlining of thunks.
191 /// This will be done iff optimizations are enabled and the member function
192 /// doesn't contain any incomplete types.
193 void MaybeEmitThunkAvailableExternally(GlobalDecl GD, const ThunkInfo &Thunk);
194
Anders Carlssonccd83d72010-03-24 16:42:11 +0000195 /// ComputeVTableRelatedInformation - Compute and store all vtable related
196 /// information (vtable layout, vbase offset offsets, thunks etc) for the
197 /// given record decl.
John McCalle2132352010-06-02 21:22:02 +0000198 void ComputeVTableRelatedInformation(const CXXRecordDecl *RD,
199 bool VTableRequired);
Anders Carlssonccd83d72010-03-24 16:42:11 +0000200
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000201 /// CreateVTableInitializer - Create a vtable initializer for the given record
202 /// decl.
203 /// \param Components - The vtable components; this is really an array of
204 /// VTableComponents.
205 llvm::Constant *CreateVTableInitializer(const CXXRecordDecl *RD,
206 const uint64_t *Components,
207 unsigned NumComponents,
208 const VTableThunksTy &VTableThunks);
Anders Carlsson2c822f12010-03-26 03:56:54 +0000209
Anders Carlssondbd920c2009-10-11 22:13:54 +0000210public:
Anders Carlssonaf440352010-03-23 04:11:45 +0000211 CodeGenVTables(CodeGenModule &CGM)
Anders Carlssondbd920c2009-10-11 22:13:54 +0000212 : CGM(CGM) { }
213
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000214 /// \brief True if the VTable of this record must be emitted in the
215 /// translation unit.
216 bool ShouldEmitVTableInThisTU(const CXXRecordDecl *RD);
Rafael Espindolab8cab182010-04-19 00:44:22 +0000217
Anders Carlssonc997d422010-01-02 01:01:18 +0000218 /// needsVTTParameter - Return whether the given global decl needs a VTT
219 /// parameter, which it does if it's a base constructor or destructor with
220 /// virtual bases.
221 static bool needsVTTParameter(GlobalDecl GD);
222
223 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
224 /// given record decl.
Anders Carlssonc11bb212010-05-02 23:53:25 +0000225 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
Anders Carlssonc997d422010-01-02 01:01:18 +0000226
Anders Carlssone1dcc222010-03-26 04:23:58 +0000227 /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the
228 /// virtual pointer for the given subobject is located.
229 uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
230 BaseSubobject Base);
231
Anders Carlsson046c2942010-04-17 20:15:18 +0000232 /// getMethodVTableIndex - Return the index (relative to the vtable address
Benjamin Kramer39411b92009-11-26 13:09:03 +0000233 /// point) where the function pointer for the given virtual function is
Anders Carlssondbd920c2009-10-11 22:13:54 +0000234 /// stored.
Anders Carlsson046c2942010-04-17 20:15:18 +0000235 uint64_t getMethodVTableIndex(GlobalDecl GD);
Benjamin Kramer39411b92009-11-26 13:09:03 +0000236
Anders Carlssonbba16072010-03-11 07:15:17 +0000237 /// getVirtualBaseOffsetOffset - Return the offset in bytes (relative to the
238 /// vtable address point) where the offset of the virtual base that contains
239 /// the given base is stored, otherwise, if no virtual base contains the given
Mike Stumpab28c132009-10-13 22:54:56 +0000240 /// class, return 0. Base must be a virtual base class or an unambigious
241 /// base.
Anders Carlssonbba16072010-03-11 07:15:17 +0000242 int64_t getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
243 const CXXRecordDecl *VBase);
Mike Stump380dd752009-11-10 07:44:33 +0000244
Anders Carlsson64c9eca2010-03-29 02:08:26 +0000245 /// getAddressPoint - Get the address point of the given subobject in the
246 /// class decl.
247 uint64_t getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD);
248
Anders Carlsson5eea8762010-03-24 05:32:05 +0000249 /// GetAddrOfVTable - Get the address of the vtable for the given record decl.
Anders Carlsson9dc338a2010-03-30 03:35:35 +0000250 llvm::GlobalVariable *GetAddrOfVTable(const CXXRecordDecl *RD);
Anders Carlsson5c6c1d92010-03-24 03:57:14 +0000251
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000252 /// EmitVTableDefinition - Emit the definition of the given vtable.
253 void EmitVTableDefinition(llvm::GlobalVariable *VTable,
254 llvm::GlobalVariable::LinkageTypes Linkage,
255 const CXXRecordDecl *RD);
256
Anders Carlssonff143f82010-03-25 00:35:49 +0000257 /// GenerateConstructionVTable - Generate a construction vtable for the given
258 /// base subobject.
259 llvm::GlobalVariable *
260 GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,
261 bool BaseIsVirtual,
Anders Carlsson2c822f12010-03-26 03:56:54 +0000262 VTableAddressPointsMapTy& AddressPoints);
Anders Carlsson1cbce122011-01-29 19:16:51 +0000263
264
265 /// GetAddrOfVTable - Get the address of the VTT for the given record decl.
266 llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
267
268 /// EmitVTTDefinition - Emit the definition of the given vtable.
269 void EmitVTTDefinition(llvm::GlobalVariable *VTT,
270 llvm::GlobalVariable::LinkageTypes Linkage,
271 const CXXRecordDecl *RD);
Rafael Espindolabbf58bb2010-03-10 02:19:29 +0000272
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000273 /// EmitThunks - Emit the associated thunks for the given global decl.
274 void EmitThunks(GlobalDecl GD);
275
Anders Carlsson7986ad52010-03-23 18:18:41 +0000276 /// GenerateClassData - Generate all the class data required to be generated
Rafael Espindolabbf58bb2010-03-10 02:19:29 +0000277 /// upon definition of a KeyFunction. This includes the vtable, the
278 /// rtti data structure and the VTT.
279 ///
280 /// \param Linkage - The desired linkage of the vtable, the RTTI and the VTT.
281 void GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
282 const CXXRecordDecl *RD);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000283};
Benjamin Kramer39411b92009-11-26 13:09:03 +0000284
Anders Carlsson1bb60992010-01-14 02:29:07 +0000285} // end namespace CodeGen
286} // end namespace clang
Anders Carlssondbd920c2009-10-11 22:13:54 +0000287#endif