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 | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 64 | class CGVtableInfo { |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame^] | 65 | public: |
| 66 | typedef std::vector<std::pair<GlobalDecl, ThunkAdjustment> > |
| 67 | AdjustmentVectorTy; |
| 68 | |
| 69 | private: |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 70 | CodeGenModule &CGM; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 71 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 72 | /// MethodVtableIndices - Contains the index (relative to the vtable address |
| 73 | /// point) where the function pointer for a virtual function is stored. |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 74 | typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVtableIndicesTy; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 75 | MethodVtableIndicesTy MethodVtableIndices; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 76 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 77 | typedef std::pair<const CXXRecordDecl *, |
| 78 | const CXXRecordDecl *> ClassPairTy; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 79 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 80 | /// VirtualBaseClassIndicies - Contains the index into the vtable where the |
| 81 | /// offsets for virtual bases of a class are stored. |
| 82 | typedef llvm::DenseMap<ClassPairTy, int64_t> VirtualBaseClassIndiciesTy; |
| 83 | VirtualBaseClassIndiciesTy VirtualBaseClassIndicies; |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 84 | |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 85 | /// Vtables - All the vtables which have been defined. |
| 86 | llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> Vtables; |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 87 | |
| 88 | /// NumVirtualFunctionPointers - Contains the number of virtual function |
| 89 | /// pointers in the vtable for a given record decl. |
| 90 | llvm::DenseMap<const CXXRecordDecl *, uint64_t> NumVirtualFunctionPointers; |
| 91 | |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame^] | 92 | typedef llvm::DenseMap<GlobalDecl, AdjustmentVectorTy> SavedAdjustmentsTy; |
| 93 | SavedAdjustmentsTy SavedAdjustments; |
| 94 | llvm::DenseSet<const CXXRecordDecl*> SavedAdjustmentRecords; |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 95 | |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 96 | /// getNumVirtualFunctionPointers - Return the number of virtual function |
| 97 | /// pointers in the vtable for a given record decl. |
| 98 | uint64_t getNumVirtualFunctionPointers(const CXXRecordDecl *RD); |
| 99 | |
| 100 | void ComputeMethodVtableIndices(const CXXRecordDecl *RD); |
| 101 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 102 | /// GenerateClassData - Generate all the class data requires to be generated |
| 103 | /// upon definition of a KeyFunction. This includes the vtable, the |
| 104 | /// rtti data structure and the VTT. |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 105 | /// |
| 106 | /// \param Linkage - The desired linkage of the vtable, the RTTI and the VTT. |
| 107 | void GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage, |
| 108 | const CXXRecordDecl *RD); |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 109 | |
Anders Carlsson | 3527225 | 2009-12-06 00:23:49 +0000 | [diff] [blame] | 110 | llvm::GlobalVariable * |
Anders Carlsson | 3527225 | 2009-12-06 00:23:49 +0000 | [diff] [blame] | 111 | GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage, |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 112 | bool GenerateDefinition, const CXXRecordDecl *LayoutClass, |
| 113 | const CXXRecordDecl *RD, uint64_t Offset); |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 114 | |
| 115 | llvm::GlobalVariable *GenerateVTT(llvm::GlobalVariable::LinkageTypes Linkage, |
| 116 | const CXXRecordDecl *RD); |
| 117 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 118 | public: |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 119 | CGVtableInfo(CodeGenModule &CGM) |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 120 | : CGM(CGM) { } |
| 121 | |
| 122 | /// getMethodVtableIndex - Return the index (relative to the vtable address |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 123 | /// point) where the function pointer for the given virtual function is |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 124 | /// stored. |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 125 | uint64_t getMethodVtableIndex(GlobalDecl GD); |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 126 | |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 127 | /// getVirtualBaseOffsetIndex - Return the index (relative to the vtable |
| 128 | /// address point) where the offset of the virtual base that contains the |
| 129 | /// given Base is stored, otherwise, if no virtual base contains the given |
| 130 | /// class, return 0. Base must be a virtual base class or an unambigious |
| 131 | /// base. |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 132 | int64_t getVirtualBaseOffsetIndex(const CXXRecordDecl *RD, |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 133 | const CXXRecordDecl *VBase); |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 134 | |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame^] | 135 | AdjustmentVectorTy *getAdjustments(GlobalDecl GD); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 136 | |
Anders Carlsson | 9ac95b9 | 2009-12-05 21:03:56 +0000 | [diff] [blame] | 137 | /// getVtableAddressPoint - returns the address point of the vtable for the |
| 138 | /// given record decl. |
| 139 | /// FIXME: This should return a list of address points. |
| 140 | uint64_t getVtableAddressPoint(const CXXRecordDecl *RD); |
| 141 | |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 142 | llvm::GlobalVariable *getVtable(const CXXRecordDecl *RD); |
| 143 | llvm::GlobalVariable *getCtorVtable(const CXXRecordDecl *RD, |
| 144 | const CXXRecordDecl *Class, |
| 145 | uint64_t Offset); |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 146 | |
| 147 | |
| 148 | void MaybeEmitVtable(GlobalDecl GD); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 149 | }; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 150 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | #endif |