Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1 | //===--- 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 Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseSet.h" |
Anders Carlsson | 3527225 | 2009-12-06 00:23:49 +0000 | [diff] [blame] | 19 | #include "llvm/GlobalVariable.h" |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 20 | #include "GlobalDecl.h" |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 21 | |
| 22 | namespace clang { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 23 | class CXXRecordDecl; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 24 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 25 | namespace CodeGen { |
| 26 | class CodeGenModule; |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 27 | |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 28 | /// ThunkAdjustment - Virtual and non-virtual adjustment for thunks. |
| 29 | class ThunkAdjustment { |
| 30 | public: |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 31 | ThunkAdjustment(int64_t NonVirtual, int64_t Virtual) |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 32 | : NonVirtual(NonVirtual), |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 33 | Virtual(Virtual) { } |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 34 | |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 35 | ThunkAdjustment() |
| 36 | : NonVirtual(0), Virtual(0) { } |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 37 | |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 38 | // isEmpty - Return whether this thunk adjustment is empty. |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 39 | bool isEmpty() const { |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 40 | return NonVirtual == 0 && Virtual == 0; |
| 41 | } |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 42 | |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 43 | /// NonVirtual - The non-virtual adjustment. |
| 44 | int64_t NonVirtual; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 45 | |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 46 | /// Virtual - The virtual adjustment. |
| 47 | int64_t Virtual; |
| 48 | }; |
| 49 | |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 50 | /// CovariantThunkAdjustment - Adjustment of the 'this' pointer and the |
| 51 | /// return pointer for covariant thunks. |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 52 | class CovariantThunkAdjustment { |
| 53 | public: |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 54 | 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 Carlsson | 6b4333d | 2010-01-13 20:11:15 +0000 | [diff] [blame] | 64 | // 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. |
| 67 | class 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 | |
| 74 | public: |
| 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 Carlsson | 7e37a69 | 2010-01-14 01:39:42 +0000 | [diff] [blame] | 83 | |
Anders Carlsson | 6b4333d | 2010-01-13 20:11:15 +0000 | [diff] [blame] | 84 | friend bool operator==(const BaseSubobject &LHS, const BaseSubobject &RHS) { |
| 85 | return LHS.Base == RHS.Base && LHS.BaseOffset == RHS.BaseOffset; |
| 86 | } |
| 87 | }; |
Anders Carlsson | 7e37a69 | 2010-01-14 01:39:42 +0000 | [diff] [blame] | 88 | |
| 89 | } // end namespace CodeGen |
| 90 | } // end namespace clang |
| 91 | |
| 92 | namespace llvm { |
| 93 | |
| 94 | template<> 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 Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 119 | // It's OK to treat BaseSubobject as a POD type. |
| 120 | template <> struct isPodLike<clang::CodeGen::BaseSubobject> { |
| 121 | static const bool value = true; |
| 122 | }; |
| 123 | |
Anders Carlsson | 7e37a69 | 2010-01-14 01:39:42 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | namespace clang { |
| 127 | namespace CodeGen { |
| 128 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 129 | class CGVtableInfo { |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 130 | public: |
| 131 | typedef std::vector<std::pair<GlobalDecl, ThunkAdjustment> > |
| 132 | AdjustmentVectorTy; |
| 133 | |
Anders Carlsson | 21431c5 | 2010-01-02 18:02:32 +0000 | [diff] [blame] | 134 | 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 Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 139 | typedef llvm::DenseMap<BaseSubobject, uint64_t> AddressPointsMapTy; |
| 140 | |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 141 | private: |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 142 | CodeGenModule &CGM; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 143 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 144 | /// MethodVtableIndices - Contains the index (relative to the vtable address |
| 145 | /// point) where the function pointer for a virtual function is stored. |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 146 | typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVtableIndicesTy; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 147 | MethodVtableIndicesTy MethodVtableIndices; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 148 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 149 | typedef std::pair<const CXXRecordDecl *, |
| 150 | const CXXRecordDecl *> ClassPairTy; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 151 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 152 | /// 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 Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 156 | |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 157 | /// Vtables - All the vtables which have been defined. |
| 158 | llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> Vtables; |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 159 | |
| 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 Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 164 | typedef llvm::DenseMap<GlobalDecl, AdjustmentVectorTy> SavedAdjustmentsTy; |
| 165 | SavedAdjustmentsTy SavedAdjustments; |
| 166 | llvm::DenseSet<const CXXRecordDecl*> SavedAdjustmentRecords; |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 167 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 168 | typedef llvm::DenseMap<ClassPairTy, uint64_t> SubVTTIndiciesTy; |
| 169 | SubVTTIndiciesTy SubVTTIndicies; |
| 170 | |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 171 | /// 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 Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 177 | /// 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 Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 180 | /// |
| 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 Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 184 | |
Anders Carlsson | 3527225 | 2009-12-06 00:23:49 +0000 | [diff] [blame] | 185 | llvm::GlobalVariable * |
Anders Carlsson | 3527225 | 2009-12-06 00:23:49 +0000 | [diff] [blame] | 186 | GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage, |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 187 | bool GenerateDefinition, const CXXRecordDecl *LayoutClass, |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 188 | const CXXRecordDecl *RD, uint64_t Offset, |
| 189 | AddressPointsMapTy& AddressPoints); |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 190 | |
| 191 | llvm::GlobalVariable *GenerateVTT(llvm::GlobalVariable::LinkageTypes Linkage, |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 192 | bool GenerateDefinition, |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 193 | const CXXRecordDecl *RD); |
| 194 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 195 | public: |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 196 | CGVtableInfo(CodeGenModule &CGM) |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 197 | : CGM(CGM) { } |
| 198 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 199 | /// 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 Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 208 | /// getMethodVtableIndex - Return the index (relative to the vtable address |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 209 | /// point) where the function pointer for the given virtual function is |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 210 | /// stored. |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 211 | uint64_t getMethodVtableIndex(GlobalDecl GD); |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 212 | |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 213 | /// 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 Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 218 | int64_t getVirtualBaseOffsetIndex(const CXXRecordDecl *RD, |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 219 | const CXXRecordDecl *VBase); |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 220 | |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 221 | AdjustmentVectorTy *getAdjustments(GlobalDecl GD); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 222 | |
Anders Carlsson | 9ac95b9 | 2009-12-05 21:03:56 +0000 | [diff] [blame] | 223 | /// 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 Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 228 | llvm::GlobalVariable *getVtable(const CXXRecordDecl *RD); |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 229 | |
| 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 Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 243 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 244 | llvm::GlobalVariable *getVTT(const CXXRecordDecl *RD); |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 245 | |
| 246 | void MaybeEmitVtable(GlobalDecl GD); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 247 | }; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 248 | |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 249 | } // end namespace CodeGen |
| 250 | } // end namespace clang |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 251 | #endif |