blob: 6e96be7219b06d066cb87358d30a32b493de95af [file] [log] [blame]
John McCall358d0562011-03-27 09:00:25 +00001//===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===//
Anders Carlsson2bb27f52009-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 Carlsson0911ae82009-12-06 00:23:49 +000018#include "llvm/GlobalVariable.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000019#include "clang/Basic/ABI.h"
Peter Collingbourne66c3a832011-09-26 01:56:16 +000020#include "clang/AST/BaseSubobject.h"
Ken Dyck16ffcac2011-03-24 01:21:01 +000021#include "clang/AST/CharUnits.h"
Peter Collingbourneee781d52011-06-14 04:02:39 +000022#include "clang/AST/GlobalDecl.h"
Anders Carlsson2bb27f52009-10-11 22:13:54 +000023
24namespace clang {
Anders Carlsson2bb27f52009-10-11 22:13:54 +000025 class CXXRecordDecl;
Benjamin Kramer334af992009-11-26 13:09:03 +000026
Anders Carlsson2bb27f52009-10-11 22:13:54 +000027namespace CodeGen {
28 class CodeGenModule;
Peter Collingbournea8341662011-09-26 01:56:30 +000029 class CodeGenVTables;
Anders Carlssonc7785402009-11-26 02:32:05 +000030
Peter Collingbournea8341662011-09-26 01:56:30 +000031class VTableContext {
32 ASTContext &Context;
Benjamin Kramer334af992009-11-26 13:09:03 +000033
Anders Carlsson11e51402010-04-17 20:15:18 +000034 /// MethodVTableIndices - Contains the index (relative to the vtable address
Anders Carlsson2bb27f52009-10-11 22:13:54 +000035 /// point) where the function pointer for a virtual function is stored.
Anders Carlsson11e51402010-04-17 20:15:18 +000036 typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVTableIndicesTy;
37 MethodVTableIndicesTy MethodVTableIndices;
Benjamin Kramer334af992009-11-26 13:09:03 +000038
Peter Collingbournea8341662011-09-26 01:56:30 +000039 /// NumVirtualFunctionPointers - Contains the number of virtual function
40 /// pointers in the vtable for a given record decl.
41 llvm::DenseMap<const CXXRecordDecl *, uint64_t> NumVirtualFunctionPointers;
42
Anders Carlsson2bb27f52009-10-11 22:13:54 +000043 typedef std::pair<const CXXRecordDecl *,
44 const CXXRecordDecl *> ClassPairTy;
Benjamin Kramer334af992009-11-26 13:09:03 +000045
Anders Carlsson4cbe83c2010-03-11 07:15:17 +000046 /// VirtualBaseClassOffsetOffsets - Contains the vtable offset (relative to
Ken Dyck3a09bc52011-04-07 01:22:42 +000047 /// the address point) in chars where the offsets for virtual bases of a class
Anders Carlsson4cbe83c2010-03-11 07:15:17 +000048 /// are stored.
Ken Dyck3a09bc52011-04-07 01:22:42 +000049 typedef llvm::DenseMap<ClassPairTy, CharUnits>
Anders Carlsson4cbe83c2010-03-11 07:15:17 +000050 VirtualBaseClassOffsetOffsetsMapTy;
51 VirtualBaseClassOffsetOffsetsMapTy VirtualBaseClassOffsetOffsets;
Mike Stumpd846d082009-11-10 07:44:33 +000052
Peter Collingbournea8341662011-09-26 01:56:30 +000053 void ComputeMethodVTableIndices(const CXXRecordDecl *RD);
54
55public:
56 VTableContext(ASTContext &Context) : Context(Context) {}
57
58 /// getNumVirtualFunctionPointers - Return the number of virtual function
59 /// pointers in the vtable for a given record decl.
60 uint64_t getNumVirtualFunctionPointers(const CXXRecordDecl *RD);
61
62 /// getMethodVTableIndex - Return the index (relative to the vtable address
63 /// point) where the function pointer for the given virtual function is
64 /// stored.
65 uint64_t getMethodVTableIndex(GlobalDecl GD);
66
67 /// getVirtualBaseOffsetOffset - Return the offset in chars (relative to the
68 /// vtable address point) where the offset of the virtual base that contains
69 /// the given base is stored, otherwise, if no virtual base contains the given
70 /// class, return 0. Base must be a virtual base class or an unambigious
71 /// base.
72 CharUnits getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
73 const CXXRecordDecl *VBase);
74
75 friend class CodeGenVTables;
76};
77
78class CodeGenVTables {
79 CodeGenModule &CGM;
80
81 VTableContext VTContext;
82
Anders Carlsson11e51402010-04-17 20:15:18 +000083 /// VTables - All the vtables which have been defined.
84 llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables;
Anders Carlssonf942ee02009-11-27 20:47:55 +000085
Chris Lattner01cf8db2011-07-20 06:58:45 +000086 typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
Anders Carlsson5c5abad2010-03-23 16:36:50 +000087 typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
88
89 /// Thunks - Contains all thunks that a given method decl will need.
90 ThunksMapTy Thunks;
John McCall6a7f9f52010-06-02 21:22:02 +000091
92 // The layout entry and a bool indicating whether we've actually emitted
93 // the vtable.
94 typedef llvm::PointerIntPair<uint64_t *, 1, bool> VTableLayoutData;
95 typedef llvm::DenseMap<const CXXRecordDecl *, VTableLayoutData>
96 VTableLayoutMapTy;
Anders Carlssone90954d2010-03-24 16:42:11 +000097
98 /// VTableLayoutMap - Stores the vtable layout for all record decls.
99 /// The layout is stored as an array of 64-bit integers, where the first
100 /// integer is the number of vtable entries in the layout, and the subsequent
101 /// integers are the vtable components.
102 VTableLayoutMapTy VTableLayoutMap;
103
Anders Carlsson8bdbb5b2010-05-03 00:55:11 +0000104 typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;
105 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> AddressPointsMapTy;
Anders Carlssond03325c2010-03-25 00:51:13 +0000106
Anders Carlssona208b392010-03-26 03:56:54 +0000107 /// Address points - Address points for all vtables.
Anders Carlssond03325c2010-03-25 00:51:13 +0000108 AddressPointsMapTy AddressPoints;
Anders Carlssona208b392010-03-26 03:56:54 +0000109
110 /// VTableAddressPointsMapTy - Address points for a single vtable.
111 typedef llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPointsMapTy;
112
Chris Lattner01cf8db2011-07-20 06:58:45 +0000113 typedef SmallVector<std::pair<uint64_t, ThunkInfo>, 1>
Anders Carlssona4147142010-03-25 15:26:28 +0000114 VTableThunksTy;
115
116 typedef llvm::DenseMap<const CXXRecordDecl *, VTableThunksTy>
117 VTableThunksMapTy;
118
119 /// VTableThunksMap - Contains thunks needed by vtables.
120 VTableThunksMapTy VTableThunksMap;
121
Anders Carlssone90954d2010-03-24 16:42:11 +0000122 uint64_t getNumVTableComponents(const CXXRecordDecl *RD) const {
123 assert(VTableLayoutMap.count(RD) && "No vtable layout for this class!");
124
John McCall6a7f9f52010-06-02 21:22:02 +0000125 return VTableLayoutMap.lookup(RD).getPointer()[0];
Anders Carlssone90954d2010-03-24 16:42:11 +0000126 }
127
Anders Carlssona627ac7e2010-03-29 03:38:52 +0000128 const uint64_t *getVTableComponentsData(const CXXRecordDecl *RD) const {
129 assert(VTableLayoutMap.count(RD) && "No vtable layout for this class!");
130
John McCall6a7f9f52010-06-02 21:22:02 +0000131 uint64_t *Components = VTableLayoutMap.lookup(RD).getPointer();
Anders Carlssona627ac7e2010-03-29 03:38:52 +0000132 return &Components[1];
133 }
134
Anders Carlsson8bdbb5b2010-05-03 00:55:11 +0000135 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy;
Anders Carlssonf1a994c2010-03-26 04:23:58 +0000136
137 /// SubVTTIndicies - Contains indices into the various sub-VTTs.
138 SubVTTIndiciesMapTy SubVTTIndicies;
139
Anders Carlsson8bdbb5b2010-05-03 00:55:11 +0000140 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>
Anders Carlssonf1a994c2010-03-26 04:23:58 +0000141 SecondaryVirtualPointerIndicesMapTy;
142
143 /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer
144 /// indices.
145 SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
Anders Carlssone36a6b32010-01-02 01:01:18 +0000146
Anders Carlsson5c5abad2010-03-23 16:36:50 +0000147 /// EmitThunk - Emit a single thunk.
Anders Carlsson8b021832011-02-06 18:31:40 +0000148 void EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
149 bool UseAvailableExternallyLinkage);
150
151 /// MaybeEmitThunkAvailableExternally - Try to emit the given thunk with
152 /// available_externally linkage to allow for inlining of thunks.
153 /// This will be done iff optimizations are enabled and the member function
154 /// doesn't contain any incomplete types.
155 void MaybeEmitThunkAvailableExternally(GlobalDecl GD, const ThunkInfo &Thunk);
156
Anders Carlssone90954d2010-03-24 16:42:11 +0000157 /// ComputeVTableRelatedInformation - Compute and store all vtable related
158 /// information (vtable layout, vbase offset offsets, thunks etc) for the
159 /// given record decl.
John McCall6a7f9f52010-06-02 21:22:02 +0000160 void ComputeVTableRelatedInformation(const CXXRecordDecl *RD,
161 bool VTableRequired);
Anders Carlssone90954d2010-03-24 16:42:11 +0000162
Anders Carlssona4147142010-03-25 15:26:28 +0000163 /// CreateVTableInitializer - Create a vtable initializer for the given record
164 /// decl.
165 /// \param Components - The vtable components; this is really an array of
166 /// VTableComponents.
167 llvm::Constant *CreateVTableInitializer(const CXXRecordDecl *RD,
168 const uint64_t *Components,
169 unsigned NumComponents,
170 const VTableThunksTy &VTableThunks);
Anders Carlssona208b392010-03-26 03:56:54 +0000171
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000172public:
Peter Collingbournea8341662011-09-26 01:56:30 +0000173 CodeGenVTables(CodeGenModule &CGM);
174
175 VTableContext &getVTableContext() { return VTContext; }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000176
Argyrios Kyrtzidis0c34b132010-10-11 03:25:57 +0000177 /// \brief True if the VTable of this record must be emitted in the
178 /// translation unit.
179 bool ShouldEmitVTableInThisTU(const CXXRecordDecl *RD);
Rafael Espindola683fe4f2010-04-19 00:44:22 +0000180
Anders Carlssone36a6b32010-01-02 01:01:18 +0000181 /// needsVTTParameter - Return whether the given global decl needs a VTT
182 /// parameter, which it does if it's a base constructor or destructor with
183 /// virtual bases.
184 static bool needsVTTParameter(GlobalDecl GD);
185
186 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
187 /// given record decl.
Anders Carlsson859b3062010-05-02 23:53:25 +0000188 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
Anders Carlssone36a6b32010-01-02 01:01:18 +0000189
Anders Carlssonf1a994c2010-03-26 04:23:58 +0000190 /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the
191 /// virtual pointer for the given subobject is located.
192 uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
193 BaseSubobject Base);
194
Anders Carlssonf6f24c62010-03-29 02:08:26 +0000195 /// getAddressPoint - Get the address point of the given subobject in the
196 /// class decl.
197 uint64_t getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD);
198
Anders Carlsson67fbf982010-03-24 05:32:05 +0000199 /// GetAddrOfVTable - Get the address of the vtable for the given record decl.
Anders Carlssona086edc2010-03-30 03:35:35 +0000200 llvm::GlobalVariable *GetAddrOfVTable(const CXXRecordDecl *RD);
Anders Carlssonb35ea552010-03-24 03:57:14 +0000201
Anders Carlssona627ac7e2010-03-29 03:38:52 +0000202 /// EmitVTableDefinition - Emit the definition of the given vtable.
203 void EmitVTableDefinition(llvm::GlobalVariable *VTable,
204 llvm::GlobalVariable::LinkageTypes Linkage,
205 const CXXRecordDecl *RD);
206
Anders Carlsson0534b022010-03-25 00:35:49 +0000207 /// GenerateConstructionVTable - Generate a construction vtable for the given
208 /// base subobject.
209 llvm::GlobalVariable *
210 GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,
211 bool BaseIsVirtual,
John McCall358d0562011-03-27 09:00:25 +0000212 llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlssona208b392010-03-26 03:56:54 +0000213 VTableAddressPointsMapTy& AddressPoints);
Anders Carlsson883fc722011-01-29 19:16:51 +0000214
215
216 /// GetAddrOfVTable - Get the address of the VTT for the given record decl.
217 llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
218
219 /// EmitVTTDefinition - Emit the definition of the given vtable.
220 void EmitVTTDefinition(llvm::GlobalVariable *VTT,
221 llvm::GlobalVariable::LinkageTypes Linkage,
222 const CXXRecordDecl *RD);
Rafael Espindolae7113ca2010-03-10 02:19:29 +0000223
Douglas Gregor88d292c2010-05-13 16:44:06 +0000224 /// EmitThunks - Emit the associated thunks for the given global decl.
225 void EmitThunks(GlobalDecl GD);
226
Anders Carlsson55e89f82010-03-23 18:18:41 +0000227 /// GenerateClassData - Generate all the class data required to be generated
Rafael Espindolae7113ca2010-03-10 02:19:29 +0000228 /// upon definition of a KeyFunction. This includes the vtable, the
229 /// rtti data structure and the VTT.
230 ///
231 /// \param Linkage - The desired linkage of the vtable, the RTTI and the VTT.
232 void GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
233 const CXXRecordDecl *RD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000234};
Benjamin Kramer334af992009-11-26 13:09:03 +0000235
Anders Carlsson5f9a8812010-01-14 02:29:07 +0000236} // end namespace CodeGen
237} // end namespace clang
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000238#endif