blob: 0caaec28169fa062abde5cf531dd9b297d0996d6 [file] [log] [blame]
Anders Carlsson2bb27f52009-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 Friedman8174f2c2009-12-06 22:01:30 +000018#include "llvm/ADT/DenseSet.h"
Anders Carlsson0911ae82009-12-06 00:23:49 +000019#include "llvm/GlobalVariable.h"
Anders Carlssonfb4dda42009-11-13 17:08:56 +000020#include "GlobalDecl.h"
Anders Carlsson2bb27f52009-10-11 22:13:54 +000021
22namespace clang {
Anders Carlsson2bb27f52009-10-11 22:13:54 +000023 class CXXRecordDecl;
Benjamin Kramer334af992009-11-26 13:09:03 +000024
Anders Carlsson2bb27f52009-10-11 22:13:54 +000025namespace CodeGen {
26 class CodeGenModule;
Anders Carlssonc7785402009-11-26 02:32:05 +000027
Benjamin Kramer334af992009-11-26 13:09:03 +000028/// ThunkAdjustment - Virtual and non-virtual adjustment for thunks.
29class ThunkAdjustment {
30public:
Anders Carlssonc7785402009-11-26 02:32:05 +000031 ThunkAdjustment(int64_t NonVirtual, int64_t Virtual)
Benjamin Kramer334af992009-11-26 13:09:03 +000032 : NonVirtual(NonVirtual),
Anders Carlssonc7785402009-11-26 02:32:05 +000033 Virtual(Virtual) { }
Benjamin Kramer334af992009-11-26 13:09:03 +000034
Anders Carlssonc7785402009-11-26 02:32:05 +000035 ThunkAdjustment()
36 : NonVirtual(0), Virtual(0) { }
Benjamin Kramer334af992009-11-26 13:09:03 +000037
Anders Carlssonc7785402009-11-26 02:32:05 +000038 // isEmpty - Return whether this thunk adjustment is empty.
Benjamin Kramer334af992009-11-26 13:09:03 +000039 bool isEmpty() const {
Anders Carlssonc7785402009-11-26 02:32:05 +000040 return NonVirtual == 0 && Virtual == 0;
41 }
Benjamin Kramer334af992009-11-26 13:09:03 +000042
Anders Carlssonc7785402009-11-26 02:32:05 +000043 /// NonVirtual - The non-virtual adjustment.
44 int64_t NonVirtual;
Benjamin Kramer334af992009-11-26 13:09:03 +000045
Anders Carlssonc7785402009-11-26 02:32:05 +000046 /// Virtual - The virtual adjustment.
47 int64_t Virtual;
48};
49
Anders Carlsson2f87c4f2009-11-26 03:09:37 +000050/// CovariantThunkAdjustment - Adjustment of the 'this' pointer and the
51/// return pointer for covariant thunks.
Benjamin Kramer334af992009-11-26 13:09:03 +000052class CovariantThunkAdjustment {
53public:
Anders Carlsson2f87c4f2009-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 Carlsson20871482010-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 Carlsson2a4adbe2010-01-14 01:39:42 +000083
Anders Carlsson20871482010-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 Carlsson2a4adbe2010-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
119}
120
121namespace clang {
122namespace CodeGen {
123
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000124class CGVtableInfo {
Eli Friedman31bc3ad2009-12-07 23:56:34 +0000125public:
126 typedef std::vector<std::pair<GlobalDecl, ThunkAdjustment> >
127 AdjustmentVectorTy;
128
Anders Carlsson93a18842010-01-02 18:02:32 +0000129 typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t;
130 typedef llvm::DenseMap<CtorVtable_t, int64_t> AddrSubMap_t;
131 typedef llvm::DenseMap<const CXXRecordDecl *, AddrSubMap_t *> AddrMap_t;
132 llvm::DenseMap<const CXXRecordDecl *, AddrMap_t*> AddressPoints;
133
Eli Friedman31bc3ad2009-12-07 23:56:34 +0000134private:
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000135 CodeGenModule &CGM;
Benjamin Kramer334af992009-11-26 13:09:03 +0000136
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000137 /// MethodVtableIndices - Contains the index (relative to the vtable address
138 /// point) where the function pointer for a virtual function is stored.
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000139 typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVtableIndicesTy;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000140 MethodVtableIndicesTy MethodVtableIndices;
Benjamin Kramer334af992009-11-26 13:09:03 +0000141
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000142 typedef std::pair<const CXXRecordDecl *,
143 const CXXRecordDecl *> ClassPairTy;
Benjamin Kramer334af992009-11-26 13:09:03 +0000144
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000145 /// VirtualBaseClassIndicies - Contains the index into the vtable where the
146 /// offsets for virtual bases of a class are stored.
147 typedef llvm::DenseMap<ClassPairTy, int64_t> VirtualBaseClassIndiciesTy;
148 VirtualBaseClassIndiciesTy VirtualBaseClassIndicies;
Mike Stumpd846d082009-11-10 07:44:33 +0000149
Anders Carlsson7e28c5f2009-12-06 00:01:05 +0000150 /// Vtables - All the vtables which have been defined.
151 llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> Vtables;
Anders Carlssonf942ee02009-11-27 20:47:55 +0000152
153 /// NumVirtualFunctionPointers - Contains the number of virtual function
154 /// pointers in the vtable for a given record decl.
155 llvm::DenseMap<const CXXRecordDecl *, uint64_t> NumVirtualFunctionPointers;
156
Eli Friedman31bc3ad2009-12-07 23:56:34 +0000157 typedef llvm::DenseMap<GlobalDecl, AdjustmentVectorTy> SavedAdjustmentsTy;
158 SavedAdjustmentsTy SavedAdjustments;
159 llvm::DenseSet<const CXXRecordDecl*> SavedAdjustmentRecords;
Eli Friedman8174f2c2009-12-06 22:01:30 +0000160
Anders Carlssone36a6b32010-01-02 01:01:18 +0000161 typedef llvm::DenseMap<ClassPairTy, uint64_t> SubVTTIndiciesTy;
162 SubVTTIndiciesTy SubVTTIndicies;
163
Anders Carlssonf942ee02009-11-27 20:47:55 +0000164 /// getNumVirtualFunctionPointers - Return the number of virtual function
165 /// pointers in the vtable for a given record decl.
166 uint64_t getNumVirtualFunctionPointers(const CXXRecordDecl *RD);
167
168 void ComputeMethodVtableIndices(const CXXRecordDecl *RD);
169
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +0000170 /// GenerateClassData - Generate all the class data requires to be generated
171 /// upon definition of a KeyFunction. This includes the vtable, the
172 /// rtti data structure and the VTT.
Anders Carlsson232324c2009-12-06 00:53:22 +0000173 ///
174 /// \param Linkage - The desired linkage of the vtable, the RTTI and the VTT.
175 void GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
176 const CXXRecordDecl *RD);
Anders Carlsson7e28c5f2009-12-06 00:01:05 +0000177
Anders Carlsson0911ae82009-12-06 00:23:49 +0000178 llvm::GlobalVariable *
Anders Carlsson0911ae82009-12-06 00:23:49 +0000179 GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlsson232324c2009-12-06 00:53:22 +0000180 bool GenerateDefinition, const CXXRecordDecl *LayoutClass,
181 const CXXRecordDecl *RD, uint64_t Offset);
Anders Carlssonfe5f7d92009-12-06 01:09:21 +0000182
183 llvm::GlobalVariable *GenerateVTT(llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlssone36a6b32010-01-02 01:01:18 +0000184 bool GenerateDefinition,
Anders Carlssonfe5f7d92009-12-06 01:09:21 +0000185 const CXXRecordDecl *RD);
186
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000187public:
Benjamin Kramer334af992009-11-26 13:09:03 +0000188 CGVtableInfo(CodeGenModule &CGM)
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000189 : CGM(CGM) { }
190
Anders Carlssone36a6b32010-01-02 01:01:18 +0000191 /// needsVTTParameter - Return whether the given global decl needs a VTT
192 /// parameter, which it does if it's a base constructor or destructor with
193 /// virtual bases.
194 static bool needsVTTParameter(GlobalDecl GD);
195
196 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
197 /// given record decl.
198 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, const CXXRecordDecl *Base);
199
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000200 /// getMethodVtableIndex - Return the index (relative to the vtable address
Benjamin Kramer334af992009-11-26 13:09:03 +0000201 /// point) where the function pointer for the given virtual function is
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000202 /// stored.
Anders Carlssonf942ee02009-11-27 20:47:55 +0000203 uint64_t getMethodVtableIndex(GlobalDecl GD);
Benjamin Kramer334af992009-11-26 13:09:03 +0000204
Mike Stump28431212009-10-13 22:54:56 +0000205 /// getVirtualBaseOffsetIndex - Return the index (relative to the vtable
206 /// address point) where the offset of the virtual base that contains the
207 /// given Base is stored, otherwise, if no virtual base contains the given
208 /// class, return 0. Base must be a virtual base class or an unambigious
209 /// base.
Benjamin Kramer334af992009-11-26 13:09:03 +0000210 int64_t getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000211 const CXXRecordDecl *VBase);
Mike Stumpd846d082009-11-10 07:44:33 +0000212
Eli Friedman31bc3ad2009-12-07 23:56:34 +0000213 AdjustmentVectorTy *getAdjustments(GlobalDecl GD);
Eli Friedman8174f2c2009-12-06 22:01:30 +0000214
Anders Carlssonc8e39ec2009-12-05 21:03:56 +0000215 /// getVtableAddressPoint - returns the address point of the vtable for the
216 /// given record decl.
217 /// FIXME: This should return a list of address points.
218 uint64_t getVtableAddressPoint(const CXXRecordDecl *RD);
219
Anders Carlsson7e28c5f2009-12-06 00:01:05 +0000220 llvm::GlobalVariable *getVtable(const CXXRecordDecl *RD);
221 llvm::GlobalVariable *getCtorVtable(const CXXRecordDecl *RD,
222 const CXXRecordDecl *Class,
223 uint64_t Offset);
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +0000224
Anders Carlssone36a6b32010-01-02 01:01:18 +0000225 llvm::GlobalVariable *getVTT(const CXXRecordDecl *RD);
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +0000226
227 void MaybeEmitVtable(GlobalDecl GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000228};
Benjamin Kramer334af992009-11-26 13:09:03 +0000229
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000230}
231}
232#endif