blob: 471d6384d6b25fea70974b3d6716753801bbad89 [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
Benjamin Kramer39411b92009-11-26 13:09:03 +000028/// ThunkAdjustment - Virtual and non-virtual adjustment for thunks.
29class ThunkAdjustment {
30public:
Anders Carlssona94822e2009-11-26 02:32:05 +000031 ThunkAdjustment(int64_t NonVirtual, int64_t Virtual)
Benjamin Kramer39411b92009-11-26 13:09:03 +000032 : NonVirtual(NonVirtual),
Anders Carlssona94822e2009-11-26 02:32:05 +000033 Virtual(Virtual) { }
Benjamin Kramer39411b92009-11-26 13:09:03 +000034
Anders Carlssona94822e2009-11-26 02:32:05 +000035 ThunkAdjustment()
36 : NonVirtual(0), Virtual(0) { }
Benjamin Kramer39411b92009-11-26 13:09:03 +000037
Anders Carlssona94822e2009-11-26 02:32:05 +000038 // isEmpty - Return whether this thunk adjustment is empty.
Benjamin Kramer39411b92009-11-26 13:09:03 +000039 bool isEmpty() const {
Anders Carlssona94822e2009-11-26 02:32:05 +000040 return NonVirtual == 0 && Virtual == 0;
41 }
Benjamin Kramer39411b92009-11-26 13:09:03 +000042
Anders Carlssona94822e2009-11-26 02:32:05 +000043 /// NonVirtual - The non-virtual adjustment.
44 int64_t NonVirtual;
Benjamin Kramer39411b92009-11-26 13:09:03 +000045
Anders Carlssona94822e2009-11-26 02:32:05 +000046 /// Virtual - The virtual adjustment.
47 int64_t Virtual;
48};
49
Anders Carlsson7622cd32009-11-26 03:09:37 +000050/// CovariantThunkAdjustment - Adjustment of the 'this' pointer and the
51/// return pointer for covariant thunks.
Benjamin Kramer39411b92009-11-26 13:09:03 +000052class CovariantThunkAdjustment {
53public:
Anders Carlsson7622cd32009-11-26 03:09:37 +000054 CovariantThunkAdjustment(const ThunkAdjustment &ThisAdjustment,
55 const ThunkAdjustment &ReturnAdjustment)
56 : ThisAdjustment(ThisAdjustment), ReturnAdjustment(ReturnAdjustment) { }
57
58 CovariantThunkAdjustment() { }
59
60 ThunkAdjustment ThisAdjustment;
61 ThunkAdjustment ReturnAdjustment;
62};
63
Anders Carlsson6b4333d2010-01-13 20:11:15 +000064// BaseSubobject - Uniquely identifies a direct or indirect base class.
65// Stores both the base class decl and the offset from the most derived class to
66// the base class.
67class BaseSubobject {
68 /// Base - The base class declaration.
69 const CXXRecordDecl *Base;
70
71 /// BaseOffset - The offset from the most derived class to the base class.
72 uint64_t BaseOffset;
73
74public:
75 BaseSubobject(const CXXRecordDecl *Base, uint64_t BaseOffset)
76 : Base(Base), BaseOffset(BaseOffset) { }
77
78 /// getBase - Returns the base class declaration.
79 const CXXRecordDecl *getBase() const { return Base; }
80
81 /// getBaseOffset - Returns the base class offset.
82 uint64_t getBaseOffset() const { return BaseOffset; }
Anders Carlsson7e37a692010-01-14 01:39:42 +000083
Anders Carlsson6b4333d2010-01-13 20:11:15 +000084 friend bool operator==(const BaseSubobject &LHS, const BaseSubobject &RHS) {
85 return LHS.Base == RHS.Base && LHS.BaseOffset == RHS.BaseOffset;
86 }
87};
Anders Carlsson7e37a692010-01-14 01:39:42 +000088
89} // end namespace CodeGen
90} // end namespace clang
91
92namespace llvm {
93
94template<> struct DenseMapInfo<clang::CodeGen::BaseSubobject> {
95 static clang::CodeGen::BaseSubobject getEmptyKey() {
96 return clang::CodeGen::BaseSubobject(
97 DenseMapInfo<const clang::CXXRecordDecl *>::getEmptyKey(),
98 DenseMapInfo<uint64_t>::getEmptyKey());
99 }
100
101 static clang::CodeGen::BaseSubobject getTombstoneKey() {
102 return clang::CodeGen::BaseSubobject(
103 DenseMapInfo<const clang::CXXRecordDecl *>::getTombstoneKey(),
104 DenseMapInfo<uint64_t>::getTombstoneKey());
105 }
106
107 static unsigned getHashValue(const clang::CodeGen::BaseSubobject &Base) {
108 return
109 DenseMapInfo<const clang::CXXRecordDecl *>::getHashValue(Base.getBase()) ^
110 DenseMapInfo<uint64_t>::getHashValue(Base.getBaseOffset());
111 }
112
113 static bool isEqual(const clang::CodeGen::BaseSubobject &LHS,
114 const clang::CodeGen::BaseSubobject &RHS) {
115 return LHS == RHS;
116 }
117};
118
Anders Carlsson1bb60992010-01-14 02:29:07 +0000119// It's OK to treat BaseSubobject as a POD type.
120template <> struct isPodLike<clang::CodeGen::BaseSubobject> {
121 static const bool value = true;
122};
123
Anders Carlsson7e37a692010-01-14 01:39:42 +0000124}
125
126namespace clang {
127namespace CodeGen {
128
Anders Carlssondbd920c2009-10-11 22:13:54 +0000129class CGVtableInfo {
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000130public:
131 typedef std::vector<std::pair<GlobalDecl, ThunkAdjustment> >
132 AdjustmentVectorTy;
133
Anders Carlsson21431c52010-01-02 18:02:32 +0000134 typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t;
135 typedef llvm::DenseMap<CtorVtable_t, int64_t> AddrSubMap_t;
136 typedef llvm::DenseMap<const CXXRecordDecl *, AddrSubMap_t *> AddrMap_t;
137 llvm::DenseMap<const CXXRecordDecl *, AddrMap_t*> AddressPoints;
138
Anders Carlsson1bb60992010-01-14 02:29:07 +0000139 typedef llvm::DenseMap<BaseSubobject, uint64_t> AddressPointsMapTy;
140
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000141private:
Anders Carlssondbd920c2009-10-11 22:13:54 +0000142 CodeGenModule &CGM;
Benjamin Kramer39411b92009-11-26 13:09:03 +0000143
Anders Carlssondbd920c2009-10-11 22:13:54 +0000144 /// MethodVtableIndices - Contains the index (relative to the vtable address
145 /// point) where the function pointer for a virtual function is stored.
Anders Carlssona0fdd912009-11-13 17:08:56 +0000146 typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVtableIndicesTy;
Anders Carlssondbd920c2009-10-11 22:13:54 +0000147 MethodVtableIndicesTy MethodVtableIndices;
Benjamin Kramer39411b92009-11-26 13:09:03 +0000148
Anders Carlssondbd920c2009-10-11 22:13:54 +0000149 typedef std::pair<const CXXRecordDecl *,
150 const CXXRecordDecl *> ClassPairTy;
Benjamin Kramer39411b92009-11-26 13:09:03 +0000151
Anders Carlssondbd920c2009-10-11 22:13:54 +0000152 /// VirtualBaseClassIndicies - Contains the index into the vtable where the
153 /// offsets for virtual bases of a class are stored.
154 typedef llvm::DenseMap<ClassPairTy, int64_t> VirtualBaseClassIndiciesTy;
155 VirtualBaseClassIndiciesTy VirtualBaseClassIndicies;
Mike Stump380dd752009-11-10 07:44:33 +0000156
Anders Carlsson8c2d36f2009-12-06 00:01:05 +0000157 /// Vtables - All the vtables which have been defined.
158 llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> Vtables;
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000159
160 /// NumVirtualFunctionPointers - Contains the number of virtual function
161 /// pointers in the vtable for a given record decl.
162 llvm::DenseMap<const CXXRecordDecl *, uint64_t> NumVirtualFunctionPointers;
163
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000164 typedef llvm::DenseMap<GlobalDecl, AdjustmentVectorTy> SavedAdjustmentsTy;
165 SavedAdjustmentsTy SavedAdjustments;
166 llvm::DenseSet<const CXXRecordDecl*> SavedAdjustmentRecords;
Eli Friedman72649ed2009-12-06 22:01:30 +0000167
Anders Carlssonc997d422010-01-02 01:01:18 +0000168 typedef llvm::DenseMap<ClassPairTy, uint64_t> SubVTTIndiciesTy;
169 SubVTTIndiciesTy SubVTTIndicies;
170
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000171 /// getNumVirtualFunctionPointers - Return the number of virtual function
172 /// pointers in the vtable for a given record decl.
173 uint64_t getNumVirtualFunctionPointers(const CXXRecordDecl *RD);
174
175 void ComputeMethodVtableIndices(const CXXRecordDecl *RD);
176
Anders Carlsson1a5e0d72009-11-30 23:41:22 +0000177 /// GenerateClassData - Generate all the class data requires to be generated
178 /// upon definition of a KeyFunction. This includes the vtable, the
179 /// rtti data structure and the VTT.
Anders Carlsson5794c972009-12-06 00:53:22 +0000180 ///
181 /// \param Linkage - The desired linkage of the vtable, the RTTI and the VTT.
182 void GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
183 const CXXRecordDecl *RD);
Anders Carlsson8c2d36f2009-12-06 00:01:05 +0000184
Anders Carlsson35272252009-12-06 00:23:49 +0000185 llvm::GlobalVariable *
Anders Carlsson35272252009-12-06 00:23:49 +0000186 GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlsson5794c972009-12-06 00:53:22 +0000187 bool GenerateDefinition, const CXXRecordDecl *LayoutClass,
Anders Carlsson1bb60992010-01-14 02:29:07 +0000188 const CXXRecordDecl *RD, uint64_t Offset,
189 AddressPointsMapTy& AddressPoints);
Anders Carlssonc3a46ef2009-12-06 01:09:21 +0000190
191 llvm::GlobalVariable *GenerateVTT(llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlssonc997d422010-01-02 01:01:18 +0000192 bool GenerateDefinition,
Anders Carlssonc3a46ef2009-12-06 01:09:21 +0000193 const CXXRecordDecl *RD);
194
Anders Carlssondbd920c2009-10-11 22:13:54 +0000195public:
Benjamin Kramer39411b92009-11-26 13:09:03 +0000196 CGVtableInfo(CodeGenModule &CGM)
Anders Carlssondbd920c2009-10-11 22:13:54 +0000197 : CGM(CGM) { }
198
Anders Carlssonc997d422010-01-02 01:01:18 +0000199 /// needsVTTParameter - Return whether the given global decl needs a VTT
200 /// parameter, which it does if it's a base constructor or destructor with
201 /// virtual bases.
202 static bool needsVTTParameter(GlobalDecl GD);
203
204 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
205 /// given record decl.
206 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, const CXXRecordDecl *Base);
207
Anders Carlssondbd920c2009-10-11 22:13:54 +0000208 /// getMethodVtableIndex - Return the index (relative to the vtable address
Benjamin Kramer39411b92009-11-26 13:09:03 +0000209 /// point) where the function pointer for the given virtual function is
Anders Carlssondbd920c2009-10-11 22:13:54 +0000210 /// stored.
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000211 uint64_t getMethodVtableIndex(GlobalDecl GD);
Benjamin Kramer39411b92009-11-26 13:09:03 +0000212
Mike Stumpab28c132009-10-13 22:54:56 +0000213 /// getVirtualBaseOffsetIndex - Return the index (relative to the vtable
214 /// address point) where the offset of the virtual base that contains the
215 /// given Base is stored, otherwise, if no virtual base contains the given
216 /// class, return 0. Base must be a virtual base class or an unambigious
217 /// base.
Benjamin Kramer39411b92009-11-26 13:09:03 +0000218 int64_t getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
Anders Carlssondbd920c2009-10-11 22:13:54 +0000219 const CXXRecordDecl *VBase);
Mike Stump380dd752009-11-10 07:44:33 +0000220
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000221 AdjustmentVectorTy *getAdjustments(GlobalDecl GD);
Eli Friedman72649ed2009-12-06 22:01:30 +0000222
Anders Carlsson9ac95b92009-12-05 21:03:56 +0000223 /// getVtableAddressPoint - returns the address point of the vtable for the
224 /// given record decl.
225 /// FIXME: This should return a list of address points.
226 uint64_t getVtableAddressPoint(const CXXRecordDecl *RD);
227
Anders Carlsson8c2d36f2009-12-06 00:01:05 +0000228 llvm::GlobalVariable *getVtable(const CXXRecordDecl *RD);
Anders Carlsson1bb60992010-01-14 02:29:07 +0000229
230 /// CtorVtableInfo - Information about a constructor vtable.
231 struct CtorVtableInfo {
232 /// Vtable - The vtable itself.
233 llvm::GlobalVariable *Vtable;
234
235 /// AddressPoints - The address points in this constructor vtable.
236 AddressPointsMapTy AddressPoints;
237
238 CtorVtableInfo() : Vtable(0) { }
239 };
240
241 CtorVtableInfo getCtorVtable(const CXXRecordDecl *RD,
242 const BaseSubobject &Base);
Anders Carlsson1a5e0d72009-11-30 23:41:22 +0000243
Anders Carlssonc997d422010-01-02 01:01:18 +0000244 llvm::GlobalVariable *getVTT(const CXXRecordDecl *RD);
Anders Carlsson1a5e0d72009-11-30 23:41:22 +0000245
246 void MaybeEmitVtable(GlobalDecl GD);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000247};
Benjamin Kramer39411b92009-11-26 13:09:03 +0000248
Anders Carlsson1bb60992010-01-14 02:29:07 +0000249} // end namespace CodeGen
250} // end namespace clang
Anders Carlssondbd920c2009-10-11 22:13:54 +0000251#endif