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" |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 18 | #include "GlobalDecl.h" |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 19 | |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 20 | namespace llvm { |
| 21 | class Constant; |
| 22 | } |
| 23 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 24 | namespace clang { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 25 | class CXXRecordDecl; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 26 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 27 | namespace CodeGen { |
| 28 | class CodeGenModule; |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 29 | |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 30 | /// ThunkAdjustment - Virtual and non-virtual adjustment for thunks. |
| 31 | class ThunkAdjustment { |
| 32 | public: |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 33 | ThunkAdjustment(int64_t NonVirtual, int64_t Virtual) |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 34 | : NonVirtual(NonVirtual), |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 35 | Virtual(Virtual) { } |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 36 | |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 37 | ThunkAdjustment() |
| 38 | : NonVirtual(0), Virtual(0) { } |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 39 | |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 40 | // isEmpty - Return whether this thunk adjustment is empty. |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 41 | bool isEmpty() const { |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 42 | return NonVirtual == 0 && Virtual == 0; |
| 43 | } |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 44 | |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 45 | /// NonVirtual - The non-virtual adjustment. |
| 46 | int64_t NonVirtual; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 47 | |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 48 | /// Virtual - The virtual adjustment. |
| 49 | int64_t Virtual; |
| 50 | }; |
| 51 | |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 52 | /// CovariantThunkAdjustment - Adjustment of the 'this' pointer and the |
| 53 | /// return pointer for covariant thunks. |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 54 | class CovariantThunkAdjustment { |
| 55 | public: |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 56 | CovariantThunkAdjustment(const ThunkAdjustment &ThisAdjustment, |
| 57 | const ThunkAdjustment &ReturnAdjustment) |
| 58 | : ThisAdjustment(ThisAdjustment), ReturnAdjustment(ReturnAdjustment) { } |
| 59 | |
| 60 | CovariantThunkAdjustment() { } |
| 61 | |
| 62 | ThunkAdjustment ThisAdjustment; |
| 63 | ThunkAdjustment ReturnAdjustment; |
| 64 | }; |
| 65 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 66 | class CGVtableInfo { |
| 67 | CodeGenModule &CGM; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 68 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 69 | /// MethodVtableIndices - Contains the index (relative to the vtable address |
| 70 | /// point) where the function pointer for a virtual function is stored. |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 71 | typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVtableIndicesTy; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 72 | MethodVtableIndicesTy MethodVtableIndices; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 73 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 74 | typedef std::pair<const CXXRecordDecl *, |
| 75 | const CXXRecordDecl *> ClassPairTy; |
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 | /// VirtualBaseClassIndicies - Contains the index into the vtable where the |
| 78 | /// offsets for virtual bases of a class are stored. |
| 79 | typedef llvm::DenseMap<ClassPairTy, int64_t> VirtualBaseClassIndiciesTy; |
| 80 | VirtualBaseClassIndiciesTy VirtualBaseClassIndicies; |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 81 | |
| 82 | llvm::DenseMap<const CXXRecordDecl *, llvm::Constant *> Vtables; |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 83 | |
| 84 | /// NumVirtualFunctionPointers - Contains the number of virtual function |
| 85 | /// pointers in the vtable for a given record decl. |
| 86 | llvm::DenseMap<const CXXRecordDecl *, uint64_t> NumVirtualFunctionPointers; |
| 87 | |
| 88 | /// getNumVirtualFunctionPointers - Return the number of virtual function |
| 89 | /// pointers in the vtable for a given record decl. |
| 90 | uint64_t getNumVirtualFunctionPointers(const CXXRecordDecl *RD); |
| 91 | |
| 92 | void ComputeMethodVtableIndices(const CXXRecordDecl *RD); |
| 93 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 94 | /// GenerateClassData - Generate all the class data requires to be generated |
| 95 | /// upon definition of a KeyFunction. This includes the vtable, the |
| 96 | /// rtti data structure and the VTT. |
| 97 | void GenerateClassData(const CXXRecordDecl *RD); |
| 98 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 99 | public: |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 100 | CGVtableInfo(CodeGenModule &CGM) |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 101 | : CGM(CGM) { } |
| 102 | |
| 103 | /// getMethodVtableIndex - Return the index (relative to the vtable address |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 104 | /// point) where the function pointer for the given virtual function is |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 105 | /// stored. |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 106 | uint64_t getMethodVtableIndex(GlobalDecl GD); |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 107 | |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 108 | /// getVirtualBaseOffsetIndex - Return the index (relative to the vtable |
| 109 | /// address point) where the offset of the virtual base that contains the |
| 110 | /// given Base is stored, otherwise, if no virtual base contains the given |
| 111 | /// class, return 0. Base must be a virtual base class or an unambigious |
| 112 | /// base. |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 113 | int64_t getVirtualBaseOffsetIndex(const CXXRecordDecl *RD, |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 114 | const CXXRecordDecl *VBase); |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 115 | |
Anders Carlsson | 9ac95b9 | 2009-12-05 21:03:56 +0000 | [diff] [blame^] | 116 | /// getVtableAddressPoint - returns the address point of the vtable for the |
| 117 | /// given record decl. |
| 118 | /// FIXME: This should return a list of address points. |
| 119 | uint64_t getVtableAddressPoint(const CXXRecordDecl *RD); |
| 120 | |
Mike Stump | 8cfcb52 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 121 | llvm::Constant *getVtable(const CXXRecordDecl *RD); |
| 122 | llvm::Constant *getCtorVtable(const CXXRecordDecl *RD, |
| 123 | const CXXRecordDecl *Class, uint64_t Offset); |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 124 | |
| 125 | |
| 126 | void MaybeEmitVtable(GlobalDecl GD); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 127 | }; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 128 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | #endif |