blob: b2f940185858b1f90d125ed50f22d08387d81aca [file] [log] [blame]
John McCallbda0d6b2011-03-27 09:00:25 +00001//===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===//
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"
Ken Dyck4230d522011-03-24 01:21:01 +000020#include "clang/AST/CharUnits.h"
Anders Carlssona0fdd912009-11-13 17:08:56 +000021#include "GlobalDecl.h"
Anders Carlssondbd920c2009-10-11 22:13:54 +000022
23namespace clang {
Anders Carlssondbd920c2009-10-11 22:13:54 +000024 class CXXRecordDecl;
Benjamin Kramer39411b92009-11-26 13:09:03 +000025
Anders Carlssondbd920c2009-10-11 22:13:54 +000026namespace CodeGen {
27 class CodeGenModule;
Anders Carlssona94822e2009-11-26 02:32:05 +000028
Anders Carlsson6b4333d2010-01-13 20:11:15 +000029// BaseSubobject - Uniquely identifies a direct or indirect base class.
30// Stores both the base class decl and the offset from the most derived class to
31// the base class.
32class BaseSubobject {
33 /// Base - The base class declaration.
34 const CXXRecordDecl *Base;
35
36 /// BaseOffset - The offset from the most derived class to the base class.
Ken Dyck4230d522011-03-24 01:21:01 +000037 CharUnits BaseOffset;
Anders Carlsson6b4333d2010-01-13 20:11:15 +000038
39public:
Ken Dyck4230d522011-03-24 01:21:01 +000040 BaseSubobject(const CXXRecordDecl *Base, CharUnits BaseOffset)
Anders Carlsson6b4333d2010-01-13 20:11:15 +000041 : Base(Base), BaseOffset(BaseOffset) { }
42
43 /// getBase - Returns the base class declaration.
44 const CXXRecordDecl *getBase() const { return Base; }
45
46 /// getBaseOffset - Returns the base class offset.
Ken Dyck4230d522011-03-24 01:21:01 +000047 CharUnits getBaseOffset() const { return BaseOffset; }
Anders Carlsson7e37a692010-01-14 01:39:42 +000048
Anders Carlsson6b4333d2010-01-13 20:11:15 +000049 friend bool operator==(const BaseSubobject &LHS, const BaseSubobject &RHS) {
50 return LHS.Base == RHS.Base && LHS.BaseOffset == RHS.BaseOffset;
51 }
52};
Anders Carlsson7e37a692010-01-14 01:39:42 +000053
54} // end namespace CodeGen
55} // end namespace clang
56
57namespace llvm {
58
59template<> struct DenseMapInfo<clang::CodeGen::BaseSubobject> {
60 static clang::CodeGen::BaseSubobject getEmptyKey() {
61 return clang::CodeGen::BaseSubobject(
62 DenseMapInfo<const clang::CXXRecordDecl *>::getEmptyKey(),
Ken Dyck4230d522011-03-24 01:21:01 +000063 clang::CharUnits::fromQuantity(DenseMapInfo<int64_t>::getEmptyKey()));
Anders Carlsson7e37a692010-01-14 01:39:42 +000064 }
65
66 static clang::CodeGen::BaseSubobject getTombstoneKey() {
67 return clang::CodeGen::BaseSubobject(
68 DenseMapInfo<const clang::CXXRecordDecl *>::getTombstoneKey(),
Ken Dyck4230d522011-03-24 01:21:01 +000069 clang::CharUnits::fromQuantity(DenseMapInfo<int64_t>::getTombstoneKey()));
Anders Carlsson7e37a692010-01-14 01:39:42 +000070 }
71
72 static unsigned getHashValue(const clang::CodeGen::BaseSubobject &Base) {
73 return
74 DenseMapInfo<const clang::CXXRecordDecl *>::getHashValue(Base.getBase()) ^
Ken Dyck4230d522011-03-24 01:21:01 +000075 DenseMapInfo<int64_t>::getHashValue(Base.getBaseOffset().getQuantity());
Anders Carlsson7e37a692010-01-14 01:39:42 +000076 }
77
78 static bool isEqual(const clang::CodeGen::BaseSubobject &LHS,
79 const clang::CodeGen::BaseSubobject &RHS) {
80 return LHS == RHS;
81 }
82};
83
Anders Carlsson1bb60992010-01-14 02:29:07 +000084// It's OK to treat BaseSubobject as a POD type.
85template <> struct isPodLike<clang::CodeGen::BaseSubobject> {
86 static const bool value = true;
87};
88
Anders Carlsson7e37a692010-01-14 01:39:42 +000089}
90
91namespace clang {
92namespace CodeGen {
93
Anders Carlssonaf440352010-03-23 04:11:45 +000094class CodeGenVTables {
Anders Carlssondbd920c2009-10-11 22:13:54 +000095 CodeGenModule &CGM;
Benjamin Kramer39411b92009-11-26 13:09:03 +000096
Anders Carlsson046c2942010-04-17 20:15:18 +000097 /// MethodVTableIndices - Contains the index (relative to the vtable address
Anders Carlssondbd920c2009-10-11 22:13:54 +000098 /// point) where the function pointer for a virtual function is stored.
Anders Carlsson046c2942010-04-17 20:15:18 +000099 typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVTableIndicesTy;
100 MethodVTableIndicesTy MethodVTableIndices;
Benjamin Kramer39411b92009-11-26 13:09:03 +0000101
Anders Carlssondbd920c2009-10-11 22:13:54 +0000102 typedef std::pair<const CXXRecordDecl *,
103 const CXXRecordDecl *> ClassPairTy;
Benjamin Kramer39411b92009-11-26 13:09:03 +0000104
Anders Carlssonbba16072010-03-11 07:15:17 +0000105 /// VirtualBaseClassOffsetOffsets - Contains the vtable offset (relative to
106 /// the address point) in bytes where the offsets for virtual bases of a class
107 /// are stored.
108 typedef llvm::DenseMap<ClassPairTy, int64_t>
109 VirtualBaseClassOffsetOffsetsMapTy;
110 VirtualBaseClassOffsetOffsetsMapTy VirtualBaseClassOffsetOffsets;
Mike Stump380dd752009-11-10 07:44:33 +0000111
Anders Carlsson046c2942010-04-17 20:15:18 +0000112 /// VTables - All the vtables which have been defined.
113 llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables;
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000114
115 /// NumVirtualFunctionPointers - Contains the number of virtual function
116 /// pointers in the vtable for a given record decl.
117 llvm::DenseMap<const CXXRecordDecl *, uint64_t> NumVirtualFunctionPointers;
118
Anders Carlssonfbf6ed42010-03-23 16:36:50 +0000119 typedef llvm::SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
120 typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
121
122 /// Thunks - Contains all thunks that a given method decl will need.
123 ThunksMapTy Thunks;
John McCalle2132352010-06-02 21:22:02 +0000124
125 // The layout entry and a bool indicating whether we've actually emitted
126 // the vtable.
127 typedef llvm::PointerIntPair<uint64_t *, 1, bool> VTableLayoutData;
128 typedef llvm::DenseMap<const CXXRecordDecl *, VTableLayoutData>
129 VTableLayoutMapTy;
Anders Carlssonccd83d72010-03-24 16:42:11 +0000130
131 /// VTableLayoutMap - Stores the vtable layout for all record decls.
132 /// The layout is stored as an array of 64-bit integers, where the first
133 /// integer is the number of vtable entries in the layout, and the subsequent
134 /// integers are the vtable components.
135 VTableLayoutMapTy VTableLayoutMap;
136
Anders Carlsson3855a072010-05-03 00:55:11 +0000137 typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;
138 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> AddressPointsMapTy;
Anders Carlsson66d567d2010-03-25 00:51:13 +0000139
Anders Carlsson2c822f12010-03-26 03:56:54 +0000140 /// Address points - Address points for all vtables.
Anders Carlsson66d567d2010-03-25 00:51:13 +0000141 AddressPointsMapTy AddressPoints;
Anders Carlsson2c822f12010-03-26 03:56:54 +0000142
143 /// VTableAddressPointsMapTy - Address points for a single vtable.
144 typedef llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPointsMapTy;
145
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000146 typedef llvm::SmallVector<std::pair<uint64_t, ThunkInfo>, 1>
147 VTableThunksTy;
148
149 typedef llvm::DenseMap<const CXXRecordDecl *, VTableThunksTy>
150 VTableThunksMapTy;
151
152 /// VTableThunksMap - Contains thunks needed by vtables.
153 VTableThunksMapTy VTableThunksMap;
154
Anders Carlssonccd83d72010-03-24 16:42:11 +0000155 uint64_t getNumVTableComponents(const CXXRecordDecl *RD) const {
156 assert(VTableLayoutMap.count(RD) && "No vtable layout for this class!");
157
John McCalle2132352010-06-02 21:22:02 +0000158 return VTableLayoutMap.lookup(RD).getPointer()[0];
Anders Carlssonccd83d72010-03-24 16:42:11 +0000159 }
160
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000161 const uint64_t *getVTableComponentsData(const CXXRecordDecl *RD) const {
162 assert(VTableLayoutMap.count(RD) && "No vtable layout for this class!");
163
John McCalle2132352010-06-02 21:22:02 +0000164 uint64_t *Components = VTableLayoutMap.lookup(RD).getPointer();
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000165 return &Components[1];
166 }
167
Anders Carlsson3855a072010-05-03 00:55:11 +0000168 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy;
Anders Carlssone1dcc222010-03-26 04:23:58 +0000169
170 /// SubVTTIndicies - Contains indices into the various sub-VTTs.
171 SubVTTIndiciesMapTy SubVTTIndicies;
172
Anders Carlsson3855a072010-05-03 00:55:11 +0000173 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>
Anders Carlssone1dcc222010-03-26 04:23:58 +0000174 SecondaryVirtualPointerIndicesMapTy;
175
176 /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer
177 /// indices.
178 SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
Anders Carlssonc997d422010-01-02 01:01:18 +0000179
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000180 /// getNumVirtualFunctionPointers - Return the number of virtual function
181 /// pointers in the vtable for a given record decl.
182 uint64_t getNumVirtualFunctionPointers(const CXXRecordDecl *RD);
183
Anders Carlsson046c2942010-04-17 20:15:18 +0000184 void ComputeMethodVTableIndices(const CXXRecordDecl *RD);
Anders Carlssonc3a46ef2009-12-06 01:09:21 +0000185
Anders Carlssonfbf6ed42010-03-23 16:36:50 +0000186 /// EmitThunk - Emit a single thunk.
Anders Carlsson14e82fd2011-02-06 18:31:40 +0000187 void EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
188 bool UseAvailableExternallyLinkage);
189
190 /// MaybeEmitThunkAvailableExternally - Try to emit the given thunk with
191 /// available_externally linkage to allow for inlining of thunks.
192 /// This will be done iff optimizations are enabled and the member function
193 /// doesn't contain any incomplete types.
194 void MaybeEmitThunkAvailableExternally(GlobalDecl GD, const ThunkInfo &Thunk);
195
Anders Carlssonccd83d72010-03-24 16:42:11 +0000196 /// ComputeVTableRelatedInformation - Compute and store all vtable related
197 /// information (vtable layout, vbase offset offsets, thunks etc) for the
198 /// given record decl.
John McCalle2132352010-06-02 21:22:02 +0000199 void ComputeVTableRelatedInformation(const CXXRecordDecl *RD,
200 bool VTableRequired);
Anders Carlssonccd83d72010-03-24 16:42:11 +0000201
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000202 /// CreateVTableInitializer - Create a vtable initializer for the given record
203 /// decl.
204 /// \param Components - The vtable components; this is really an array of
205 /// VTableComponents.
206 llvm::Constant *CreateVTableInitializer(const CXXRecordDecl *RD,
207 const uint64_t *Components,
208 unsigned NumComponents,
209 const VTableThunksTy &VTableThunks);
Anders Carlsson2c822f12010-03-26 03:56:54 +0000210
Anders Carlssondbd920c2009-10-11 22:13:54 +0000211public:
Anders Carlssonaf440352010-03-23 04:11:45 +0000212 CodeGenVTables(CodeGenModule &CGM)
Anders Carlssondbd920c2009-10-11 22:13:54 +0000213 : CGM(CGM) { }
214
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000215 /// \brief True if the VTable of this record must be emitted in the
216 /// translation unit.
217 bool ShouldEmitVTableInThisTU(const CXXRecordDecl *RD);
Rafael Espindolab8cab182010-04-19 00:44:22 +0000218
Anders Carlssonc997d422010-01-02 01:01:18 +0000219 /// needsVTTParameter - Return whether the given global decl needs a VTT
220 /// parameter, which it does if it's a base constructor or destructor with
221 /// virtual bases.
222 static bool needsVTTParameter(GlobalDecl GD);
223
224 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
225 /// given record decl.
Anders Carlssonc11bb212010-05-02 23:53:25 +0000226 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
Anders Carlssonc997d422010-01-02 01:01:18 +0000227
Anders Carlssone1dcc222010-03-26 04:23:58 +0000228 /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the
229 /// virtual pointer for the given subobject is located.
230 uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
231 BaseSubobject Base);
232
Anders Carlsson046c2942010-04-17 20:15:18 +0000233 /// getMethodVTableIndex - Return the index (relative to the vtable address
Benjamin Kramer39411b92009-11-26 13:09:03 +0000234 /// point) where the function pointer for the given virtual function is
Anders Carlssondbd920c2009-10-11 22:13:54 +0000235 /// stored.
Anders Carlsson046c2942010-04-17 20:15:18 +0000236 uint64_t getMethodVTableIndex(GlobalDecl GD);
Benjamin Kramer39411b92009-11-26 13:09:03 +0000237
Anders Carlssonbba16072010-03-11 07:15:17 +0000238 /// getVirtualBaseOffsetOffset - Return the offset in bytes (relative to the
239 /// vtable address point) where the offset of the virtual base that contains
240 /// the given base is stored, otherwise, if no virtual base contains the given
Mike Stumpab28c132009-10-13 22:54:56 +0000241 /// class, return 0. Base must be a virtual base class or an unambigious
242 /// base.
Anders Carlssonbba16072010-03-11 07:15:17 +0000243 int64_t getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
244 const CXXRecordDecl *VBase);
Mike Stump380dd752009-11-10 07:44:33 +0000245
Anders Carlsson64c9eca2010-03-29 02:08:26 +0000246 /// getAddressPoint - Get the address point of the given subobject in the
247 /// class decl.
248 uint64_t getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD);
249
Anders Carlsson5eea8762010-03-24 05:32:05 +0000250 /// GetAddrOfVTable - Get the address of the vtable for the given record decl.
Anders Carlsson9dc338a2010-03-30 03:35:35 +0000251 llvm::GlobalVariable *GetAddrOfVTable(const CXXRecordDecl *RD);
Anders Carlsson5c6c1d92010-03-24 03:57:14 +0000252
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000253 /// EmitVTableDefinition - Emit the definition of the given vtable.
254 void EmitVTableDefinition(llvm::GlobalVariable *VTable,
255 llvm::GlobalVariable::LinkageTypes Linkage,
256 const CXXRecordDecl *RD);
257
Anders Carlssonff143f82010-03-25 00:35:49 +0000258 /// GenerateConstructionVTable - Generate a construction vtable for the given
259 /// base subobject.
260 llvm::GlobalVariable *
261 GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,
262 bool BaseIsVirtual,
John McCallbda0d6b2011-03-27 09:00:25 +0000263 llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlsson2c822f12010-03-26 03:56:54 +0000264 VTableAddressPointsMapTy& AddressPoints);
Anders Carlsson1cbce122011-01-29 19:16:51 +0000265
266
267 /// GetAddrOfVTable - Get the address of the VTT for the given record decl.
268 llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
269
270 /// EmitVTTDefinition - Emit the definition of the given vtable.
271 void EmitVTTDefinition(llvm::GlobalVariable *VTT,
272 llvm::GlobalVariable::LinkageTypes Linkage,
273 const CXXRecordDecl *RD);
Rafael Espindolabbf58bb2010-03-10 02:19:29 +0000274
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000275 /// EmitThunks - Emit the associated thunks for the given global decl.
276 void EmitThunks(GlobalDecl GD);
277
Anders Carlsson7986ad52010-03-23 18:18:41 +0000278 /// GenerateClassData - Generate all the class data required to be generated
Rafael Espindolabbf58bb2010-03-10 02:19:29 +0000279 /// upon definition of a KeyFunction. This includes the vtable, the
280 /// rtti data structure and the VTT.
281 ///
282 /// \param Linkage - The desired linkage of the vtable, the RTTI and the VTT.
283 void GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
284 const CXXRecordDecl *RD);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000285};
Benjamin Kramer39411b92009-11-26 13:09:03 +0000286
Anders Carlsson1bb60992010-01-14 02:29:07 +0000287} // end namespace CodeGen
288} // end namespace clang
Anders Carlssondbd920c2009-10-11 22:13:54 +0000289#endif