blob: 6670b4ed72c198f0bf8fddc7b98c7a612e923597 [file] [log] [blame]
Anders Carlssondbd920c2009-10-11 22:13:54 +00001//===--- CGVtable.h - Emit LLVM Code for C++ vtables ----------------------===//
2//
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"
Eli Friedman72649ed2009-12-06 22:01:30 +000018#include "llvm/ADT/DenseSet.h"
Anders Carlsson35272252009-12-06 00:23:49 +000019#include "llvm/GlobalVariable.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 Carlsson80d8d7d2010-03-23 15:13:06 +000028/// ReturnAdjustment - A return adjustment.
29struct ReturnAdjustment {
30 /// NonVirtual - The non-virtual adjustment from the derived object to its
31 /// nearest virtual base.
32 int64_t NonVirtual;
33
34 /// VBaseOffsetOffset - The offset (in bytes), relative to the address point
35 /// of the virtual base class offset.
36 int64_t VBaseOffsetOffset;
37
38 ReturnAdjustment() : NonVirtual(0), VBaseOffsetOffset(0) { }
39
40 bool isEmpty() const { return !NonVirtual && !VBaseOffsetOffset; }
41
42 friend bool operator==(const ReturnAdjustment &LHS,
43 const ReturnAdjustment &RHS) {
44 return LHS.NonVirtual == RHS.NonVirtual &&
45 LHS.VBaseOffsetOffset == RHS.VBaseOffsetOffset;
46 }
47
48 friend bool operator<(const ReturnAdjustment &LHS,
49 const ReturnAdjustment &RHS) {
50 if (LHS.NonVirtual < RHS.NonVirtual)
51 return true;
52
53 return LHS.NonVirtual == RHS.NonVirtual &&
54 LHS.VBaseOffsetOffset < RHS.VBaseOffsetOffset;
55 }
56};
57
58/// ThisAdjustment - A 'this' pointer adjustment.
59struct ThisAdjustment {
60 /// NonVirtual - The non-virtual adjustment from the derived object to its
61 /// nearest virtual base.
62 int64_t NonVirtual;
63
64 /// VCallOffsetOffset - The offset (in bytes), relative to the address point,
65 /// of the virtual call offset.
66 int64_t VCallOffsetOffset;
67
68 ThisAdjustment() : NonVirtual(0), VCallOffsetOffset(0) { }
69
70 bool isEmpty() const { return !NonVirtual && !VCallOffsetOffset; }
71
72 friend bool operator==(const ThisAdjustment &LHS,
73 const ThisAdjustment &RHS) {
74 return LHS.NonVirtual == RHS.NonVirtual &&
75 LHS.VCallOffsetOffset == RHS.VCallOffsetOffset;
76 }
77
78 friend bool operator<(const ThisAdjustment &LHS,
79 const ThisAdjustment &RHS) {
80 if (LHS.NonVirtual < RHS.NonVirtual)
81 return true;
82
83 return LHS.NonVirtual == RHS.NonVirtual &&
84 LHS.VCallOffsetOffset < RHS.VCallOffsetOffset;
85 }
Anders Carlsson80d8d7d2010-03-23 15:13:06 +000086};
87
Anders Carlssonb4e4c962010-03-23 15:17:13 +000088/// ThunkInfo - The 'this' pointer adjustment as well as an optional return
89/// adjustment for a thunk.
90struct ThunkInfo {
91 /// This - The 'this' pointer adjustment.
92 ThisAdjustment This;
93
94 /// Return - The return adjustment.
95 ReturnAdjustment Return;
96
97 ThunkInfo() { }
98
99 ThunkInfo(const ThisAdjustment &This, const ReturnAdjustment &Return)
100 : This(This), Return(Return) { }
101
102 friend bool operator==(const ThunkInfo &LHS, const ThunkInfo &RHS) {
103 return LHS.This == RHS.This && LHS.Return == RHS.Return;
104 }
105
106 friend bool operator<(const ThunkInfo &LHS, const ThunkInfo &RHS) {
107 if (LHS.This < RHS.This)
108 return true;
109
110 return LHS.This == RHS.This && LHS.Return < RHS.Return;
111 }
112
113 bool isEmpty() const { return This.isEmpty() && Return.isEmpty(); }
114};
115
Benjamin Kramer39411b92009-11-26 13:09:03 +0000116/// ThunkAdjustment - Virtual and non-virtual adjustment for thunks.
117class ThunkAdjustment {
118public:
Anders Carlssona94822e2009-11-26 02:32:05 +0000119 ThunkAdjustment(int64_t NonVirtual, int64_t Virtual)
Benjamin Kramer39411b92009-11-26 13:09:03 +0000120 : NonVirtual(NonVirtual),
Anders Carlssona94822e2009-11-26 02:32:05 +0000121 Virtual(Virtual) { }
Benjamin Kramer39411b92009-11-26 13:09:03 +0000122
Anders Carlssona94822e2009-11-26 02:32:05 +0000123 ThunkAdjustment()
124 : NonVirtual(0), Virtual(0) { }
Benjamin Kramer39411b92009-11-26 13:09:03 +0000125
Anders Carlssona94822e2009-11-26 02:32:05 +0000126 // isEmpty - Return whether this thunk adjustment is empty.
Benjamin Kramer39411b92009-11-26 13:09:03 +0000127 bool isEmpty() const {
Anders Carlssona94822e2009-11-26 02:32:05 +0000128 return NonVirtual == 0 && Virtual == 0;
129 }
Benjamin Kramer39411b92009-11-26 13:09:03 +0000130
Anders Carlssona94822e2009-11-26 02:32:05 +0000131 /// NonVirtual - The non-virtual adjustment.
132 int64_t NonVirtual;
Benjamin Kramer39411b92009-11-26 13:09:03 +0000133
Anders Carlssona94822e2009-11-26 02:32:05 +0000134 /// Virtual - The virtual adjustment.
135 int64_t Virtual;
136};
137
Anders Carlsson7622cd32009-11-26 03:09:37 +0000138/// CovariantThunkAdjustment - Adjustment of the 'this' pointer and the
139/// return pointer for covariant thunks.
Benjamin Kramer39411b92009-11-26 13:09:03 +0000140class CovariantThunkAdjustment {
141public:
Anders Carlsson7622cd32009-11-26 03:09:37 +0000142 CovariantThunkAdjustment(const ThunkAdjustment &ThisAdjustment,
143 const ThunkAdjustment &ReturnAdjustment)
144 : ThisAdjustment(ThisAdjustment), ReturnAdjustment(ReturnAdjustment) { }
145
146 CovariantThunkAdjustment() { }
147
148 ThunkAdjustment ThisAdjustment;
149 ThunkAdjustment ReturnAdjustment;
150};
151
Anders Carlsson6b4333d2010-01-13 20:11:15 +0000152// BaseSubobject - Uniquely identifies a direct or indirect base class.
153// Stores both the base class decl and the offset from the most derived class to
154// the base class.
155class BaseSubobject {
156 /// Base - The base class declaration.
157 const CXXRecordDecl *Base;
158
159 /// BaseOffset - The offset from the most derived class to the base class.
160 uint64_t BaseOffset;
161
162public:
163 BaseSubobject(const CXXRecordDecl *Base, uint64_t BaseOffset)
164 : Base(Base), BaseOffset(BaseOffset) { }
165
166 /// getBase - Returns the base class declaration.
167 const CXXRecordDecl *getBase() const { return Base; }
168
169 /// getBaseOffset - Returns the base class offset.
170 uint64_t getBaseOffset() const { return BaseOffset; }
Anders Carlsson7e37a692010-01-14 01:39:42 +0000171
Anders Carlsson6b4333d2010-01-13 20:11:15 +0000172 friend bool operator==(const BaseSubobject &LHS, const BaseSubobject &RHS) {
173 return LHS.Base == RHS.Base && LHS.BaseOffset == RHS.BaseOffset;
174 }
175};
Anders Carlsson7e37a692010-01-14 01:39:42 +0000176
177} // end namespace CodeGen
178} // end namespace clang
179
180namespace llvm {
181
182template<> struct DenseMapInfo<clang::CodeGen::BaseSubobject> {
183 static clang::CodeGen::BaseSubobject getEmptyKey() {
184 return clang::CodeGen::BaseSubobject(
185 DenseMapInfo<const clang::CXXRecordDecl *>::getEmptyKey(),
186 DenseMapInfo<uint64_t>::getEmptyKey());
187 }
188
189 static clang::CodeGen::BaseSubobject getTombstoneKey() {
190 return clang::CodeGen::BaseSubobject(
191 DenseMapInfo<const clang::CXXRecordDecl *>::getTombstoneKey(),
192 DenseMapInfo<uint64_t>::getTombstoneKey());
193 }
194
195 static unsigned getHashValue(const clang::CodeGen::BaseSubobject &Base) {
196 return
197 DenseMapInfo<const clang::CXXRecordDecl *>::getHashValue(Base.getBase()) ^
198 DenseMapInfo<uint64_t>::getHashValue(Base.getBaseOffset());
199 }
200
201 static bool isEqual(const clang::CodeGen::BaseSubobject &LHS,
202 const clang::CodeGen::BaseSubobject &RHS) {
203 return LHS == RHS;
204 }
205};
206
Anders Carlsson1bb60992010-01-14 02:29:07 +0000207// It's OK to treat BaseSubobject as a POD type.
208template <> struct isPodLike<clang::CodeGen::BaseSubobject> {
209 static const bool value = true;
210};
211
Anders Carlsson7e37a692010-01-14 01:39:42 +0000212}
213
214namespace clang {
215namespace CodeGen {
216
Anders Carlssonaf440352010-03-23 04:11:45 +0000217class CodeGenVTables {
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000218public:
219 typedef std::vector<std::pair<GlobalDecl, ThunkAdjustment> >
220 AdjustmentVectorTy;
221
Anders Carlsson21431c52010-01-02 18:02:32 +0000222 typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t;
223 typedef llvm::DenseMap<CtorVtable_t, int64_t> AddrSubMap_t;
224 typedef llvm::DenseMap<const CXXRecordDecl *, AddrSubMap_t *> AddrMap_t;
Anders Carlsson21431c52010-01-02 18:02:32 +0000225
Anders Carlsson1bb60992010-01-14 02:29:07 +0000226 typedef llvm::DenseMap<BaseSubobject, uint64_t> AddressPointsMapTy;
227
Anders Carlsson5c6c1d92010-03-24 03:57:14 +0000228 const CodeGenVTables::AddrSubMap_t& getAddressPoints(const CXXRecordDecl *RD);
229
230 llvm::DenseMap<const CXXRecordDecl *, AddrMap_t*> AddressPoints;
231
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000232private:
Anders Carlssondbd920c2009-10-11 22:13:54 +0000233 CodeGenModule &CGM;
Benjamin Kramer39411b92009-11-26 13:09:03 +0000234
Anders Carlsson5c6c1d92010-03-24 03:57:14 +0000235
Anders Carlssondbd920c2009-10-11 22:13:54 +0000236 /// MethodVtableIndices - Contains the index (relative to the vtable address
237 /// point) where the function pointer for a virtual function is stored.
Anders Carlssona0fdd912009-11-13 17:08:56 +0000238 typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVtableIndicesTy;
Anders Carlssondbd920c2009-10-11 22:13:54 +0000239 MethodVtableIndicesTy MethodVtableIndices;
Benjamin Kramer39411b92009-11-26 13:09:03 +0000240
Anders Carlssondbd920c2009-10-11 22:13:54 +0000241 typedef std::pair<const CXXRecordDecl *,
242 const CXXRecordDecl *> ClassPairTy;
Benjamin Kramer39411b92009-11-26 13:09:03 +0000243
Anders Carlssonbba16072010-03-11 07:15:17 +0000244 /// VirtualBaseClassOffsetOffsets - Contains the vtable offset (relative to
245 /// the address point) in bytes where the offsets for virtual bases of a class
246 /// are stored.
247 typedef llvm::DenseMap<ClassPairTy, int64_t>
248 VirtualBaseClassOffsetOffsetsMapTy;
249 VirtualBaseClassOffsetOffsetsMapTy VirtualBaseClassOffsetOffsets;
Mike Stump380dd752009-11-10 07:44:33 +0000250
Anders Carlsson8c2d36f2009-12-06 00:01:05 +0000251 /// Vtables - All the vtables which have been defined.
252 llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> Vtables;
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000253
254 /// NumVirtualFunctionPointers - Contains the number of virtual function
255 /// pointers in the vtable for a given record decl.
256 llvm::DenseMap<const CXXRecordDecl *, uint64_t> NumVirtualFunctionPointers;
257
Anders Carlssonfbf6ed42010-03-23 16:36:50 +0000258 typedef llvm::SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
259 typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
260
261 /// Thunks - Contains all thunks that a given method decl will need.
262 ThunksMapTy Thunks;
Anders Carlssonfbf6ed42010-03-23 16:36:50 +0000263
Anders Carlssonccd83d72010-03-24 16:42:11 +0000264 typedef llvm::DenseMap<const CXXRecordDecl *, uint64_t *> VTableLayoutMapTy;
265
266 /// VTableLayoutMap - Stores the vtable layout for all record decls.
267 /// The layout is stored as an array of 64-bit integers, where the first
268 /// integer is the number of vtable entries in the layout, and the subsequent
269 /// integers are the vtable components.
270 VTableLayoutMapTy VTableLayoutMap;
271
272 uint64_t getNumVTableComponents(const CXXRecordDecl *RD) const {
273 assert(VTableLayoutMap.count(RD) && "No vtable layout for this class!");
274
275 return VTableLayoutMap.lookup(RD)[0];
276 }
277
Anders Carlssonc997d422010-01-02 01:01:18 +0000278 typedef llvm::DenseMap<ClassPairTy, uint64_t> SubVTTIndiciesTy;
279 SubVTTIndiciesTy SubVTTIndicies;
280
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000281 /// getNumVirtualFunctionPointers - Return the number of virtual function
282 /// pointers in the vtable for a given record decl.
283 uint64_t getNumVirtualFunctionPointers(const CXXRecordDecl *RD);
284
285 void ComputeMethodVtableIndices(const CXXRecordDecl *RD);
Rafael Espindolabbf58bb2010-03-10 02:19:29 +0000286
Anders Carlsson35272252009-12-06 00:23:49 +0000287 llvm::GlobalVariable *
Anders Carlsson35272252009-12-06 00:23:49 +0000288 GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlsson5794c972009-12-06 00:53:22 +0000289 bool GenerateDefinition, const CXXRecordDecl *LayoutClass,
Anders Carlsson5d7af6b2010-02-28 00:36:23 +0000290 const CXXRecordDecl *RD, uint64_t Offset, bool IsVirtual,
Anders Carlsson1bb60992010-01-14 02:29:07 +0000291 AddressPointsMapTy& AddressPoints);
Anders Carlssonc3a46ef2009-12-06 01:09:21 +0000292
293 llvm::GlobalVariable *GenerateVTT(llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlssonc997d422010-01-02 01:01:18 +0000294 bool GenerateDefinition,
Anders Carlssonc3a46ef2009-12-06 01:09:21 +0000295 const CXXRecordDecl *RD);
296
Anders Carlssonfbf6ed42010-03-23 16:36:50 +0000297 /// EmitThunk - Emit a single thunk.
298 void EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk);
299
Anders Carlssonee5ab9f2010-03-23 04:59:02 +0000300 /// EmitThunks - Emit the associated thunks for the given global decl.
301 void EmitThunks(GlobalDecl GD);
302
Anders Carlssonccd83d72010-03-24 16:42:11 +0000303 /// ComputeVTableRelatedInformation - Compute and store all vtable related
304 /// information (vtable layout, vbase offset offsets, thunks etc) for the
305 /// given record decl.
306 void ComputeVTableRelatedInformation(const CXXRecordDecl *RD);
307
Anders Carlssondbd920c2009-10-11 22:13:54 +0000308public:
Anders Carlssonaf440352010-03-23 04:11:45 +0000309 CodeGenVTables(CodeGenModule &CGM)
Anders Carlssondbd920c2009-10-11 22:13:54 +0000310 : CGM(CGM) { }
311
Anders Carlssonc997d422010-01-02 01:01:18 +0000312 /// needsVTTParameter - Return whether the given global decl needs a VTT
313 /// parameter, which it does if it's a base constructor or destructor with
314 /// virtual bases.
315 static bool needsVTTParameter(GlobalDecl GD);
316
317 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
318 /// given record decl.
319 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, const CXXRecordDecl *Base);
320
Anders Carlssondbd920c2009-10-11 22:13:54 +0000321 /// getMethodVtableIndex - Return the index (relative to the vtable address
Benjamin Kramer39411b92009-11-26 13:09:03 +0000322 /// point) where the function pointer for the given virtual function is
Anders Carlssondbd920c2009-10-11 22:13:54 +0000323 /// stored.
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000324 uint64_t getMethodVtableIndex(GlobalDecl GD);
Benjamin Kramer39411b92009-11-26 13:09:03 +0000325
Anders Carlssonbba16072010-03-11 07:15:17 +0000326 /// getVirtualBaseOffsetOffset - Return the offset in bytes (relative to the
327 /// vtable address point) where the offset of the virtual base that contains
328 /// the given base is stored, otherwise, if no virtual base contains the given
Mike Stumpab28c132009-10-13 22:54:56 +0000329 /// class, return 0. Base must be a virtual base class or an unambigious
330 /// base.
Anders Carlssonbba16072010-03-11 07:15:17 +0000331 int64_t getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
332 const CXXRecordDecl *VBase);
Mike Stump380dd752009-11-10 07:44:33 +0000333
Anders Carlsson5eea8762010-03-24 05:32:05 +0000334 /// GetAddrOfVTable - Get the address of the vtable for the given record decl.
335 llvm::Constant *GetAddrOfVTable(const CXXRecordDecl *RD);
Anders Carlsson5c6c1d92010-03-24 03:57:14 +0000336
Anders Carlsson1bb60992010-01-14 02:29:07 +0000337 /// CtorVtableInfo - Information about a constructor vtable.
338 struct CtorVtableInfo {
339 /// Vtable - The vtable itself.
340 llvm::GlobalVariable *Vtable;
341
342 /// AddressPoints - The address points in this constructor vtable.
343 AddressPointsMapTy AddressPoints;
344
345 CtorVtableInfo() : Vtable(0) { }
346 };
347
348 CtorVtableInfo getCtorVtable(const CXXRecordDecl *RD,
Anders Carlsson5d7af6b2010-02-28 00:36:23 +0000349 const BaseSubobject &Base,
350 bool BaseIsVirtual);
Anders Carlsson1a5e0d72009-11-30 23:41:22 +0000351
Anders Carlssonc997d422010-01-02 01:01:18 +0000352 llvm::GlobalVariable *getVTT(const CXXRecordDecl *RD);
Anders Carlsson1a5e0d72009-11-30 23:41:22 +0000353
Anders Carlsson13189d02010-03-23 04:15:00 +0000354 // EmitVTableRelatedData - Will emit any thunks that the global decl might
355 // have, as well as the vtable itself if the global decl is the key function.
356 void EmitVTableRelatedData(GlobalDecl GD);
Rafael Espindolabbf58bb2010-03-10 02:19:29 +0000357
Anders Carlsson7986ad52010-03-23 18:18:41 +0000358 /// GenerateClassData - Generate all the class data required to be generated
Rafael Espindolabbf58bb2010-03-10 02:19:29 +0000359 /// upon definition of a KeyFunction. This includes the vtable, the
360 /// rtti data structure and the VTT.
361 ///
362 /// \param Linkage - The desired linkage of the vtable, the RTTI and the VTT.
363 void GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
364 const CXXRecordDecl *RD);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000365};
Benjamin Kramer39411b92009-11-26 13:09:03 +0000366
Anders Carlsson1bb60992010-01-14 02:29:07 +0000367} // end namespace CodeGen
368} // end namespace clang
Anders Carlssondbd920c2009-10-11 22:13:54 +0000369#endif