John McCall | 358d056 | 2011-03-27 09:00:25 +0000 | [diff] [blame] | 1 | //===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===// |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 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" |
Anders Carlsson | 0911ae8 | 2009-12-06 00:23:49 +0000 | [diff] [blame] | 18 | #include "llvm/GlobalVariable.h" |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 19 | #include "clang/Basic/ABI.h" |
Peter Collingbourne | 66c3a83 | 2011-09-26 01:56:16 +0000 | [diff] [blame] | 20 | #include "clang/AST/BaseSubobject.h" |
Ken Dyck | 16ffcac | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 21 | #include "clang/AST/CharUnits.h" |
Peter Collingbourne | ee781d5 | 2011-06-14 04:02:39 +0000 | [diff] [blame] | 22 | #include "clang/AST/GlobalDecl.h" |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 23 | |
| 24 | namespace clang { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 25 | class CXXRecordDecl; |
Benjamin Kramer | 334af99 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 26 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 27 | namespace CodeGen { |
| 28 | class CodeGenModule; |
Anders Carlsson | c778540 | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 29 | |
Anders Carlsson | a864caf | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 30 | class CodeGenVTables { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 31 | CodeGenModule &CGM; |
Benjamin Kramer | 334af99 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 32 | |
Anders Carlsson | 11e5140 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 33 | /// MethodVTableIndices - Contains the index (relative to the vtable address |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 34 | /// point) where the function pointer for a virtual function is stored. |
Anders Carlsson | 11e5140 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 35 | typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVTableIndicesTy; |
| 36 | MethodVTableIndicesTy MethodVTableIndices; |
Benjamin Kramer | 334af99 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 37 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 38 | typedef std::pair<const CXXRecordDecl *, |
| 39 | const CXXRecordDecl *> ClassPairTy; |
Benjamin Kramer | 334af99 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 40 | |
Anders Carlsson | 4cbe83c | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 41 | /// VirtualBaseClassOffsetOffsets - Contains the vtable offset (relative to |
Ken Dyck | 3a09bc5 | 2011-04-07 01:22:42 +0000 | [diff] [blame] | 42 | /// the address point) in chars where the offsets for virtual bases of a class |
Anders Carlsson | 4cbe83c | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 43 | /// are stored. |
Ken Dyck | 3a09bc5 | 2011-04-07 01:22:42 +0000 | [diff] [blame] | 44 | typedef llvm::DenseMap<ClassPairTy, CharUnits> |
Anders Carlsson | 4cbe83c | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 45 | VirtualBaseClassOffsetOffsetsMapTy; |
| 46 | VirtualBaseClassOffsetOffsetsMapTy VirtualBaseClassOffsetOffsets; |
Mike Stump | d846d08 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 47 | |
Anders Carlsson | 11e5140 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 48 | /// VTables - All the vtables which have been defined. |
| 49 | llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables; |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 50 | |
| 51 | /// NumVirtualFunctionPointers - Contains the number of virtual function |
| 52 | /// pointers in the vtable for a given record decl. |
| 53 | llvm::DenseMap<const CXXRecordDecl *, uint64_t> NumVirtualFunctionPointers; |
| 54 | |
Chris Lattner | 01cf8db | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 55 | typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy; |
Anders Carlsson | 5c5abad | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 56 | typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy; |
| 57 | |
| 58 | /// Thunks - Contains all thunks that a given method decl will need. |
| 59 | ThunksMapTy Thunks; |
John McCall | 6a7f9f5 | 2010-06-02 21:22:02 +0000 | [diff] [blame] | 60 | |
| 61 | // The layout entry and a bool indicating whether we've actually emitted |
| 62 | // the vtable. |
| 63 | typedef llvm::PointerIntPair<uint64_t *, 1, bool> VTableLayoutData; |
| 64 | typedef llvm::DenseMap<const CXXRecordDecl *, VTableLayoutData> |
| 65 | VTableLayoutMapTy; |
Anders Carlsson | e90954d | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 66 | |
| 67 | /// VTableLayoutMap - Stores the vtable layout for all record decls. |
| 68 | /// The layout is stored as an array of 64-bit integers, where the first |
| 69 | /// integer is the number of vtable entries in the layout, and the subsequent |
| 70 | /// integers are the vtable components. |
| 71 | VTableLayoutMapTy VTableLayoutMap; |
| 72 | |
Anders Carlsson | 8bdbb5b | 2010-05-03 00:55:11 +0000 | [diff] [blame] | 73 | typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy; |
| 74 | typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> AddressPointsMapTy; |
Anders Carlsson | d03325c | 2010-03-25 00:51:13 +0000 | [diff] [blame] | 75 | |
Anders Carlsson | a208b39 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 76 | /// Address points - Address points for all vtables. |
Anders Carlsson | d03325c | 2010-03-25 00:51:13 +0000 | [diff] [blame] | 77 | AddressPointsMapTy AddressPoints; |
Anders Carlsson | a208b39 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 78 | |
| 79 | /// VTableAddressPointsMapTy - Address points for a single vtable. |
| 80 | typedef llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPointsMapTy; |
| 81 | |
Chris Lattner | 01cf8db | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 82 | typedef SmallVector<std::pair<uint64_t, ThunkInfo>, 1> |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 83 | VTableThunksTy; |
| 84 | |
| 85 | typedef llvm::DenseMap<const CXXRecordDecl *, VTableThunksTy> |
| 86 | VTableThunksMapTy; |
| 87 | |
| 88 | /// VTableThunksMap - Contains thunks needed by vtables. |
| 89 | VTableThunksMapTy VTableThunksMap; |
| 90 | |
Anders Carlsson | e90954d | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 91 | uint64_t getNumVTableComponents(const CXXRecordDecl *RD) const { |
| 92 | assert(VTableLayoutMap.count(RD) && "No vtable layout for this class!"); |
| 93 | |
John McCall | 6a7f9f5 | 2010-06-02 21:22:02 +0000 | [diff] [blame] | 94 | return VTableLayoutMap.lookup(RD).getPointer()[0]; |
Anders Carlsson | e90954d | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Anders Carlsson | a627ac7e | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 97 | const uint64_t *getVTableComponentsData(const CXXRecordDecl *RD) const { |
| 98 | assert(VTableLayoutMap.count(RD) && "No vtable layout for this class!"); |
| 99 | |
John McCall | 6a7f9f5 | 2010-06-02 21:22:02 +0000 | [diff] [blame] | 100 | uint64_t *Components = VTableLayoutMap.lookup(RD).getPointer(); |
Anders Carlsson | a627ac7e | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 101 | return &Components[1]; |
| 102 | } |
| 103 | |
Anders Carlsson | 8bdbb5b | 2010-05-03 00:55:11 +0000 | [diff] [blame] | 104 | typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy; |
Anders Carlsson | f1a994c | 2010-03-26 04:23:58 +0000 | [diff] [blame] | 105 | |
| 106 | /// SubVTTIndicies - Contains indices into the various sub-VTTs. |
| 107 | SubVTTIndiciesMapTy SubVTTIndicies; |
| 108 | |
Anders Carlsson | 8bdbb5b | 2010-05-03 00:55:11 +0000 | [diff] [blame] | 109 | typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> |
Anders Carlsson | f1a994c | 2010-03-26 04:23:58 +0000 | [diff] [blame] | 110 | SecondaryVirtualPointerIndicesMapTy; |
| 111 | |
| 112 | /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer |
| 113 | /// indices. |
| 114 | SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices; |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 115 | |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 116 | /// getNumVirtualFunctionPointers - Return the number of virtual function |
| 117 | /// pointers in the vtable for a given record decl. |
| 118 | uint64_t getNumVirtualFunctionPointers(const CXXRecordDecl *RD); |
| 119 | |
Anders Carlsson | 11e5140 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 120 | void ComputeMethodVTableIndices(const CXXRecordDecl *RD); |
Anders Carlsson | fe5f7d9 | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 121 | |
Anders Carlsson | 5c5abad | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 122 | /// EmitThunk - Emit a single thunk. |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 123 | void EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk, |
| 124 | bool UseAvailableExternallyLinkage); |
| 125 | |
| 126 | /// MaybeEmitThunkAvailableExternally - Try to emit the given thunk with |
| 127 | /// available_externally linkage to allow for inlining of thunks. |
| 128 | /// This will be done iff optimizations are enabled and the member function |
| 129 | /// doesn't contain any incomplete types. |
| 130 | void MaybeEmitThunkAvailableExternally(GlobalDecl GD, const ThunkInfo &Thunk); |
| 131 | |
Anders Carlsson | e90954d | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 132 | /// ComputeVTableRelatedInformation - Compute and store all vtable related |
| 133 | /// information (vtable layout, vbase offset offsets, thunks etc) for the |
| 134 | /// given record decl. |
John McCall | 6a7f9f5 | 2010-06-02 21:22:02 +0000 | [diff] [blame] | 135 | void ComputeVTableRelatedInformation(const CXXRecordDecl *RD, |
| 136 | bool VTableRequired); |
Anders Carlsson | e90954d | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 137 | |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 138 | /// CreateVTableInitializer - Create a vtable initializer for the given record |
| 139 | /// decl. |
| 140 | /// \param Components - The vtable components; this is really an array of |
| 141 | /// VTableComponents. |
| 142 | llvm::Constant *CreateVTableInitializer(const CXXRecordDecl *RD, |
| 143 | const uint64_t *Components, |
| 144 | unsigned NumComponents, |
| 145 | const VTableThunksTy &VTableThunks); |
Anders Carlsson | a208b39 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 146 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 147 | public: |
Anders Carlsson | a864caf | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 148 | CodeGenVTables(CodeGenModule &CGM) |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 149 | : CGM(CGM) { } |
| 150 | |
Argyrios Kyrtzidis | 0c34b13 | 2010-10-11 03:25:57 +0000 | [diff] [blame] | 151 | /// \brief True if the VTable of this record must be emitted in the |
| 152 | /// translation unit. |
| 153 | bool ShouldEmitVTableInThisTU(const CXXRecordDecl *RD); |
Rafael Espindola | 683fe4f | 2010-04-19 00:44:22 +0000 | [diff] [blame] | 154 | |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 155 | /// needsVTTParameter - Return whether the given global decl needs a VTT |
| 156 | /// parameter, which it does if it's a base constructor or destructor with |
| 157 | /// virtual bases. |
| 158 | static bool needsVTTParameter(GlobalDecl GD); |
| 159 | |
| 160 | /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the |
| 161 | /// given record decl. |
Anders Carlsson | 859b306 | 2010-05-02 23:53:25 +0000 | [diff] [blame] | 162 | uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base); |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 163 | |
Anders Carlsson | f1a994c | 2010-03-26 04:23:58 +0000 | [diff] [blame] | 164 | /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the |
| 165 | /// virtual pointer for the given subobject is located. |
| 166 | uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD, |
| 167 | BaseSubobject Base); |
| 168 | |
Anders Carlsson | 11e5140 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 169 | /// getMethodVTableIndex - Return the index (relative to the vtable address |
Benjamin Kramer | 334af99 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 170 | /// point) where the function pointer for the given virtual function is |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 171 | /// stored. |
Anders Carlsson | 11e5140 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 172 | uint64_t getMethodVTableIndex(GlobalDecl GD); |
Benjamin Kramer | 334af99 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 173 | |
Ken Dyck | bb4e977 | 2011-04-07 12:37:09 +0000 | [diff] [blame] | 174 | /// getVirtualBaseOffsetOffset - Return the offset in chars (relative to the |
Anders Carlsson | 4cbe83c | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 175 | /// vtable address point) where the offset of the virtual base that contains |
| 176 | /// the given base is stored, otherwise, if no virtual base contains the given |
Mike Stump | 2843121 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 177 | /// class, return 0. Base must be a virtual base class or an unambigious |
| 178 | /// base. |
Ken Dyck | bb4e977 | 2011-04-07 12:37:09 +0000 | [diff] [blame] | 179 | CharUnits getVirtualBaseOffsetOffset(const CXXRecordDecl *RD, |
| 180 | const CXXRecordDecl *VBase); |
Mike Stump | d846d08 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 181 | |
Anders Carlsson | f6f24c6 | 2010-03-29 02:08:26 +0000 | [diff] [blame] | 182 | /// getAddressPoint - Get the address point of the given subobject in the |
| 183 | /// class decl. |
| 184 | uint64_t getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD); |
| 185 | |
Anders Carlsson | 67fbf98 | 2010-03-24 05:32:05 +0000 | [diff] [blame] | 186 | /// GetAddrOfVTable - Get the address of the vtable for the given record decl. |
Anders Carlsson | a086edc | 2010-03-30 03:35:35 +0000 | [diff] [blame] | 187 | llvm::GlobalVariable *GetAddrOfVTable(const CXXRecordDecl *RD); |
Anders Carlsson | b35ea55 | 2010-03-24 03:57:14 +0000 | [diff] [blame] | 188 | |
Anders Carlsson | a627ac7e | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 189 | /// EmitVTableDefinition - Emit the definition of the given vtable. |
| 190 | void EmitVTableDefinition(llvm::GlobalVariable *VTable, |
| 191 | llvm::GlobalVariable::LinkageTypes Linkage, |
| 192 | const CXXRecordDecl *RD); |
| 193 | |
Anders Carlsson | 0534b02 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 194 | /// GenerateConstructionVTable - Generate a construction vtable for the given |
| 195 | /// base subobject. |
| 196 | llvm::GlobalVariable * |
| 197 | GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base, |
| 198 | bool BaseIsVirtual, |
John McCall | 358d056 | 2011-03-27 09:00:25 +0000 | [diff] [blame] | 199 | llvm::GlobalVariable::LinkageTypes Linkage, |
Anders Carlsson | a208b39 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 200 | VTableAddressPointsMapTy& AddressPoints); |
Anders Carlsson | 883fc72 | 2011-01-29 19:16:51 +0000 | [diff] [blame] | 201 | |
| 202 | |
| 203 | /// GetAddrOfVTable - Get the address of the VTT for the given record decl. |
| 204 | llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD); |
| 205 | |
| 206 | /// EmitVTTDefinition - Emit the definition of the given vtable. |
| 207 | void EmitVTTDefinition(llvm::GlobalVariable *VTT, |
| 208 | llvm::GlobalVariable::LinkageTypes Linkage, |
| 209 | const CXXRecordDecl *RD); |
Rafael Espindola | e7113ca | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 210 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 211 | /// EmitThunks - Emit the associated thunks for the given global decl. |
| 212 | void EmitThunks(GlobalDecl GD); |
| 213 | |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 214 | /// GenerateClassData - Generate all the class data required to be generated |
Rafael Espindola | e7113ca | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 215 | /// upon definition of a KeyFunction. This includes the vtable, the |
| 216 | /// rtti data structure and the VTT. |
| 217 | /// |
| 218 | /// \param Linkage - The desired linkage of the vtable, the RTTI and the VTT. |
| 219 | void GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage, |
| 220 | const CXXRecordDecl *RD); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 221 | }; |
Benjamin Kramer | 334af99 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 222 | |
Anders Carlsson | 5f9a881 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 223 | } // end namespace CodeGen |
| 224 | } // end namespace clang |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 225 | #endif |